Lines Matching +full:min +full:- +full:len
1 // SPDX-License-Identifier: GPL-2.0
16 if (thr->task) { in bch2_thread_with_file_exit()
17 kthread_stop(thr->task); in bch2_thread_with_file_exit()
18 put_task_struct(thr->task); in bch2_thread_with_file_exit()
27 int ret, fd = -1; in bch2_run_thread_with_file()
30 if (fops->read && fops->write) in bch2_run_thread_with_file()
32 else if (fops->read) in bch2_run_thread_with_file()
34 else if (fops->write) in bch2_run_thread_with_file()
40 thr->ret = 0; in bch2_run_thread_with_file()
41 thr->task = kthread_create(fn, thr, "%s", name); in bch2_run_thread_with_file()
42 ret = PTR_ERR_OR_ZERO(thr->task); in bch2_run_thread_with_file()
56 get_task_struct(thr->task); in bch2_run_thread_with_file()
57 wake_up_process(thr->task); in bch2_run_thread_with_file()
63 if (thr->task) in bch2_run_thread_with_file()
64 kthread_stop(thr->task); in bch2_run_thread_with_file()
70 return thr->stdio.output_buf.pos || in thread_with_stdio_has_output()
71 thr->output2.nr || in thread_with_stdio_has_output()
72 thr->thr.done; in thread_with_stdio_has_output()
76 size_t len, loff_t *ppos) in thread_with_stdio_read() argument
79 container_of(file->private_data, struct thread_with_stdio, thr); in thread_with_stdio_read()
83 if ((file->f_flags & O_NONBLOCK) && in thread_with_stdio_read()
85 return -EAGAIN; in thread_with_stdio_read()
87 ret = wait_event_interruptible(thr->stdio.output_wait, in thread_with_stdio_read()
92 if (thr->thr.done) in thread_with_stdio_read()
95 while (len) { in thread_with_stdio_read()
96 ret = darray_make_room(&thr->output2, thr->stdio.output_buf.pos); in thread_with_stdio_read()
100 spin_lock_irq(&thr->stdio.output_lock); in thread_with_stdio_read()
101 b = min_t(size_t, darray_room(thr->output2), thr->stdio.output_buf.pos); in thread_with_stdio_read()
103 memcpy(&darray_top(thr->output2), thr->stdio.output_buf.buf, b); in thread_with_stdio_read()
104 memmove(thr->stdio.output_buf.buf, in thread_with_stdio_read()
105 thr->stdio.output_buf.buf + b, in thread_with_stdio_read()
106 thr->stdio.output_buf.pos - b); in thread_with_stdio_read()
108 thr->output2.nr += b; in thread_with_stdio_read()
109 thr->stdio.output_buf.pos -= b; in thread_with_stdio_read()
110 spin_unlock_irq(&thr->stdio.output_lock); in thread_with_stdio_read()
112 b = min(len, thr->output2.nr); in thread_with_stdio_read()
116 b -= copy_to_user(buf, thr->output2.data, b); in thread_with_stdio_read()
118 ret = -EFAULT; in thread_with_stdio_read()
124 len -= b; in thread_with_stdio_read()
126 memmove(thr->output2.data, in thread_with_stdio_read()
127 thr->output2.data + b, in thread_with_stdio_read()
128 thr->output2.nr - b); in thread_with_stdio_read()
129 thr->output2.nr -= b; in thread_with_stdio_read()
138 container_of(file->private_data, struct thread_with_stdio, thr); in thread_with_stdio_release()
140 bch2_thread_with_file_exit(&thr->thr); in thread_with_stdio_release()
141 printbuf_exit(&thr->stdio.input_buf); in thread_with_stdio_release()
142 printbuf_exit(&thr->stdio.output_buf); in thread_with_stdio_release()
143 darray_exit(&thr->output2); in thread_with_stdio_release()
144 thr->exit(thr); in thread_with_stdio_release()
152 return thr->stdio.input_buf.pos < WRITE_BUFFER || thr->thr.done; in thread_with_stdio_has_input_space()
156 size_t len, loff_t *ppos) in thread_with_stdio_write() argument
159 container_of(file->private_data, struct thread_with_stdio, thr); in thread_with_stdio_write()
160 struct printbuf *buf = &thr->stdio.input_buf; in thread_with_stdio_write()
164 while (len) { in thread_with_stdio_write()
165 if (thr->thr.done) { in thread_with_stdio_write()
166 ret = -EPIPE; in thread_with_stdio_write()
170 size_t b = len - fault_in_readable(ubuf, len); in thread_with_stdio_write()
172 ret = -EFAULT; in thread_with_stdio_write()
176 spin_lock(&thr->stdio.input_lock); in thread_with_stdio_write()
177 if (buf->pos < WRITE_BUFFER) in thread_with_stdio_write()
178 bch2_printbuf_make_room(buf, min(b, WRITE_BUFFER - buf->pos)); in thread_with_stdio_write()
179 b = min(len, printbuf_remaining_size(buf)); in thread_with_stdio_write()
181 if (b && !copy_from_user_nofault(&buf->buf[buf->pos], ubuf, b)) { in thread_with_stdio_write()
183 len -= b; in thread_with_stdio_write()
185 buf->pos += b; in thread_with_stdio_write()
187 spin_unlock(&thr->stdio.input_lock); in thread_with_stdio_write()
190 wake_up(&thr->stdio.input_wait); in thread_with_stdio_write()
192 if ((file->f_flags & O_NONBLOCK)) { in thread_with_stdio_write()
193 ret = -EAGAIN; in thread_with_stdio_write()
197 ret = wait_event_interruptible(thr->stdio.input_wait, in thread_with_stdio_write()
210 container_of(file->private_data, struct thread_with_stdio, thr); in thread_with_stdio_poll()
212 poll_wait(file, &thr->stdio.output_wait, wait); in thread_with_stdio_poll()
213 poll_wait(file, &thr->stdio.input_wait, wait); in thread_with_stdio_poll()
221 if (thr->thr.done) in thread_with_stdio_poll()
238 thr->stdio.input_buf = PRINTBUF; in bch2_run_thread_with_stdio()
239 thr->stdio.input_buf.atomic++; in bch2_run_thread_with_stdio()
240 spin_lock_init(&thr->stdio.input_lock); in bch2_run_thread_with_stdio()
241 init_waitqueue_head(&thr->stdio.input_wait); in bch2_run_thread_with_stdio()
243 thr->stdio.output_buf = PRINTBUF; in bch2_run_thread_with_stdio()
244 thr->stdio.output_buf.atomic++; in bch2_run_thread_with_stdio()
245 spin_lock_init(&thr->stdio.output_lock); in bch2_run_thread_with_stdio()
246 init_waitqueue_head(&thr->stdio.output_wait); in bch2_run_thread_with_stdio()
248 darray_init(&thr->output2); in bch2_run_thread_with_stdio()
249 thr->exit = exit; in bch2_run_thread_with_stdio()
251 return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, fn); in bch2_run_thread_with_stdio()
254 int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *buf, size_t len) in bch2_stdio_redirect_read() argument
256 wait_event(stdio->input_wait, in bch2_stdio_redirect_read()
257 stdio->input_buf.pos || stdio->done); in bch2_stdio_redirect_read()
259 if (stdio->done) in bch2_stdio_redirect_read()
260 return -1; in bch2_stdio_redirect_read()
262 spin_lock(&stdio->input_lock); in bch2_stdio_redirect_read()
263 int ret = min(len, stdio->input_buf.pos); in bch2_stdio_redirect_read()
264 stdio->input_buf.pos -= ret; in bch2_stdio_redirect_read()
265 memcpy(buf, stdio->input_buf.buf, ret); in bch2_stdio_redirect_read()
266 memmove(stdio->input_buf.buf, in bch2_stdio_redirect_read()
267 stdio->input_buf.buf + ret, in bch2_stdio_redirect_read()
268 stdio->input_buf.pos); in bch2_stdio_redirect_read()
269 spin_unlock(&stdio->input_lock); in bch2_stdio_redirect_read()
271 wake_up(&stdio->input_wait); in bch2_stdio_redirect_read()
275 int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, char *buf, size_t len) in bch2_stdio_redirect_readline() argument
277 wait_event(stdio->input_wait, in bch2_stdio_redirect_readline()
278 stdio->input_buf.pos || stdio->done); in bch2_stdio_redirect_readline()
280 if (stdio->done) in bch2_stdio_redirect_readline()
281 return -1; in bch2_stdio_redirect_readline()
283 spin_lock(&stdio->input_lock); in bch2_stdio_redirect_readline()
284 int ret = min(len, stdio->input_buf.pos); in bch2_stdio_redirect_readline()
285 char *n = memchr(stdio->input_buf.buf, '\n', ret); in bch2_stdio_redirect_readline()
287 ret = min(ret, n + 1 - stdio->input_buf.buf); in bch2_stdio_redirect_readline()
288 stdio->input_buf.pos -= ret; in bch2_stdio_redirect_readline()
289 memcpy(buf, stdio->input_buf.buf, ret); in bch2_stdio_redirect_readline()
290 memmove(stdio->input_buf.buf, in bch2_stdio_redirect_readline()
291 stdio->input_buf.buf + ret, in bch2_stdio_redirect_readline()
292 stdio->input_buf.pos); in bch2_stdio_redirect_readline()
293 spin_unlock(&stdio->input_lock); in bch2_stdio_redirect_readline()
295 wake_up(&stdio->input_wait); in bch2_stdio_redirect_readline()