Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5274f052 JA |
2 | /* |
3 | * "splice": joining two ropes together by interweaving their strands. | |
4 | * | |
5 | * This is the "extended pipe" functionality, where a pipe is used as | |
6 | * an arbitrary in-memory buffer. Think of a pipe as a small kernel | |
7 | * buffer that you can use to transfer data from one end to the other. | |
8 | * | |
9 | * The traditional unix read/write is extended with a "splice()" operation | |
10 | * that transfers data buffers to or from a pipe buffer. | |
11 | * | |
12 | * Named by Larry McVoy, original implementation from Linus, extended by | |
c2058e06 JA |
13 | * Jens to support splicing to files, network, direct splicing, etc and |
14 | * fixing lots of bugs. | |
5274f052 | 15 | * |
0fe23479 | 16 | * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> |
c2058e06 JA |
17 | * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> |
18 | * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> | |
5274f052 JA |
19 | * |
20 | */ | |
be297968 | 21 | #include <linux/bvec.h> |
5274f052 JA |
22 | #include <linux/fs.h> |
23 | #include <linux/file.h> | |
24 | #include <linux/pagemap.h> | |
d6b29d7c | 25 | #include <linux/splice.h> |
08e552c6 | 26 | #include <linux/memcontrol.h> |
5274f052 | 27 | #include <linux/mm_inline.h> |
5abc97aa | 28 | #include <linux/swap.h> |
4f6f0bd2 | 29 | #include <linux/writeback.h> |
630d9c47 | 30 | #include <linux/export.h> |
4f6f0bd2 | 31 | #include <linux/syscalls.h> |
912d35f8 | 32 | #include <linux/uio.h> |
29ce2058 | 33 | #include <linux/security.h> |
5a0e3ad6 | 34 | #include <linux/gfp.h> |
35f9c09f | 35 | #include <linux/socket.h> |
174cd4b1 IM |
36 | #include <linux/sched/signal.h> |
37 | ||
06ae43f3 | 38 | #include "internal.h" |
5274f052 | 39 | |
83f9135b JA |
40 | /* |
41 | * Attempt to steal a page from a pipe buffer. This should perhaps go into | |
42 | * a vm helper function, it's already simplified quite a bit by the | |
43 | * addition of remove_mapping(). If success is returned, the caller may | |
44 | * attempt to reuse this page for another destination. | |
45 | */ | |
c928f642 CH |
46 | static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe, |
47 | struct pipe_buffer *buf) | |
5abc97aa JA |
48 | { |
49 | struct page *page = buf->page; | |
9e94cd4f | 50 | struct address_space *mapping; |
5abc97aa | 51 | |
9e0267c2 JA |
52 | lock_page(page); |
53 | ||
9e94cd4f JA |
54 | mapping = page_mapping(page); |
55 | if (mapping) { | |
56 | WARN_ON(!PageUptodate(page)); | |
5abc97aa | 57 | |
9e94cd4f JA |
58 | /* |
59 | * At least for ext2 with nobh option, we need to wait on | |
60 | * writeback completing on this page, since we'll remove it | |
61 | * from the pagecache. Otherwise truncate wont wait on the | |
62 | * page, allowing the disk blocks to be reused by someone else | |
63 | * before we actually wrote our data to them. fs corruption | |
64 | * ensues. | |
65 | */ | |
66 | wait_on_page_writeback(page); | |
ad8d6f0a | 67 | |
266cf658 DH |
68 | if (page_has_private(page) && |
69 | !try_to_release_page(page, GFP_KERNEL)) | |
ca39d651 | 70 | goto out_unlock; |
4f6f0bd2 | 71 | |
9e94cd4f JA |
72 | /* |
73 | * If we succeeded in removing the mapping, set LRU flag | |
74 | * and return good. | |
75 | */ | |
76 | if (remove_mapping(mapping, page)) { | |
77 | buf->flags |= PIPE_BUF_FLAG_LRU; | |
c928f642 | 78 | return true; |
9e94cd4f | 79 | } |
9e0267c2 | 80 | } |
5abc97aa | 81 | |
9e94cd4f JA |
82 | /* |
83 | * Raced with truncate or failed to remove page from current | |
84 | * address space, unlock and return failure. | |
85 | */ | |
ca39d651 | 86 | out_unlock: |
9e94cd4f | 87 | unlock_page(page); |
c928f642 | 88 | return false; |
5abc97aa JA |
89 | } |
90 | ||
76ad4d11 | 91 | static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, |
5274f052 JA |
92 | struct pipe_buffer *buf) |
93 | { | |
09cbfeaf | 94 | put_page(buf->page); |
1432873a | 95 | buf->flags &= ~PIPE_BUF_FLAG_LRU; |
5274f052 JA |
96 | } |
97 | ||
0845718d JA |
98 | /* |
99 | * Check whether the contents of buf is OK to access. Since the content | |
100 | * is a page cache page, IO may be in flight. | |
101 | */ | |
cac36bb0 JA |
102 | static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, |
103 | struct pipe_buffer *buf) | |
5274f052 JA |
104 | { |
105 | struct page *page = buf->page; | |
49d0b21b | 106 | int err; |
5274f052 JA |
107 | |
108 | if (!PageUptodate(page)) { | |
49d0b21b JA |
109 | lock_page(page); |
110 | ||
111 | /* | |
112 | * Page got truncated/unhashed. This will cause a 0-byte | |
73d62d83 | 113 | * splice, if this is the first page. |
49d0b21b JA |
114 | */ |
115 | if (!page->mapping) { | |
116 | err = -ENODATA; | |
117 | goto error; | |
118 | } | |
5274f052 | 119 | |
49d0b21b | 120 | /* |
73d62d83 | 121 | * Uh oh, read-error from disk. |
49d0b21b JA |
122 | */ |
123 | if (!PageUptodate(page)) { | |
124 | err = -EIO; | |
125 | goto error; | |
126 | } | |
127 | ||
128 | /* | |
f84d7519 | 129 | * Page is ok afterall, we are done. |
49d0b21b | 130 | */ |
5274f052 | 131 | unlock_page(page); |
5274f052 JA |
132 | } |
133 | ||
f84d7519 | 134 | return 0; |
49d0b21b JA |
135 | error: |
136 | unlock_page(page); | |
f84d7519 | 137 | return err; |
70524490 JA |
138 | } |
139 | ||
708e3508 | 140 | const struct pipe_buf_operations page_cache_pipe_buf_ops = { |
c928f642 CH |
141 | .confirm = page_cache_pipe_buf_confirm, |
142 | .release = page_cache_pipe_buf_release, | |
143 | .try_steal = page_cache_pipe_buf_try_steal, | |
144 | .get = generic_pipe_buf_get, | |
5274f052 JA |
145 | }; |
146 | ||
c928f642 CH |
147 | static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe, |
148 | struct pipe_buffer *buf) | |
912d35f8 | 149 | { |
7afa6fd0 | 150 | if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) |
c928f642 | 151 | return false; |
7afa6fd0 | 152 | |
1432873a | 153 | buf->flags |= PIPE_BUF_FLAG_LRU; |
c928f642 | 154 | return generic_pipe_buf_try_steal(pipe, buf); |
912d35f8 JA |
155 | } |
156 | ||
d4c3cca9 | 157 | static const struct pipe_buf_operations user_page_pipe_buf_ops = { |
c928f642 CH |
158 | .release = page_cache_pipe_buf_release, |
159 | .try_steal = user_page_pipe_buf_try_steal, | |
160 | .get = generic_pipe_buf_get, | |
912d35f8 JA |
161 | }; |
162 | ||
825cdcb1 NK |
163 | static void wakeup_pipe_readers(struct pipe_inode_info *pipe) |
164 | { | |
165 | smp_mb(); | |
0ddad21d LT |
166 | if (waitqueue_active(&pipe->rd_wait)) |
167 | wake_up_interruptible(&pipe->rd_wait); | |
825cdcb1 NK |
168 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
169 | } | |
170 | ||
932cc6d4 JA |
171 | /** |
172 | * splice_to_pipe - fill passed data into a pipe | |
173 | * @pipe: pipe to fill | |
174 | * @spd: data to fill | |
175 | * | |
176 | * Description: | |
79685b8d | 177 | * @spd contains a map of pages and len/offset tuples, along with |
932cc6d4 JA |
178 | * the struct pipe_buf_operations associated with these pages. This |
179 | * function will link that data to the pipe. | |
180 | * | |
83f9135b | 181 | */ |
d6b29d7c JA |
182 | ssize_t splice_to_pipe(struct pipe_inode_info *pipe, |
183 | struct splice_pipe_desc *spd) | |
5274f052 | 184 | { |
00de00bd | 185 | unsigned int spd_pages = spd->nr_pages; |
8cefc107 DH |
186 | unsigned int tail = pipe->tail; |
187 | unsigned int head = pipe->head; | |
188 | unsigned int mask = pipe->ring_size - 1; | |
8924feff | 189 | int ret = 0, page_nr = 0; |
5274f052 | 190 | |
d6785d91 RV |
191 | if (!spd_pages) |
192 | return 0; | |
193 | ||
8924feff AV |
194 | if (unlikely(!pipe->readers)) { |
195 | send_sig(SIGPIPE, current, 0); | |
196 | ret = -EPIPE; | |
197 | goto out; | |
198 | } | |
5274f052 | 199 | |
6718b6f8 | 200 | while (!pipe_full(head, tail, pipe->max_usage)) { |
8cefc107 | 201 | struct pipe_buffer *buf = &pipe->bufs[head & mask]; |
5274f052 | 202 | |
8924feff AV |
203 | buf->page = spd->pages[page_nr]; |
204 | buf->offset = spd->partial[page_nr].offset; | |
205 | buf->len = spd->partial[page_nr].len; | |
206 | buf->private = spd->partial[page_nr].private; | |
207 | buf->ops = spd->ops; | |
5a81e6a1 | 208 | buf->flags = 0; |
5274f052 | 209 | |
8cefc107 DH |
210 | head++; |
211 | pipe->head = head; | |
8924feff AV |
212 | page_nr++; |
213 | ret += buf->len; | |
29e35094 | 214 | |
8924feff | 215 | if (!--spd->nr_pages) |
5274f052 | 216 | break; |
5274f052 JA |
217 | } |
218 | ||
8924feff AV |
219 | if (!ret) |
220 | ret = -EAGAIN; | |
5274f052 | 221 | |
8924feff | 222 | out: |
00de00bd | 223 | while (page_nr < spd_pages) |
bbdfc2f7 | 224 | spd->spd_release(spd, page_nr++); |
5274f052 JA |
225 | |
226 | return ret; | |
227 | } | |
2b514574 | 228 | EXPORT_SYMBOL_GPL(splice_to_pipe); |
5274f052 | 229 | |
79fddc4e AV |
230 | ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
231 | { | |
8cefc107 DH |
232 | unsigned int head = pipe->head; |
233 | unsigned int tail = pipe->tail; | |
234 | unsigned int mask = pipe->ring_size - 1; | |
79fddc4e AV |
235 | int ret; |
236 | ||
237 | if (unlikely(!pipe->readers)) { | |
238 | send_sig(SIGPIPE, current, 0); | |
239 | ret = -EPIPE; | |
6718b6f8 | 240 | } else if (pipe_full(head, tail, pipe->max_usage)) { |
79fddc4e AV |
241 | ret = -EAGAIN; |
242 | } else { | |
8cefc107 DH |
243 | pipe->bufs[head & mask] = *buf; |
244 | pipe->head = head + 1; | |
79fddc4e AV |
245 | return buf->len; |
246 | } | |
a779638c | 247 | pipe_buf_release(pipe, buf); |
79fddc4e AV |
248 | return ret; |
249 | } | |
250 | EXPORT_SYMBOL(add_to_pipe); | |
251 | ||
35f3d14d JA |
252 | /* |
253 | * Check if we need to grow the arrays holding pages and partial page | |
254 | * descriptions. | |
255 | */ | |
047fe360 | 256 | int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) |
35f3d14d | 257 | { |
6718b6f8 | 258 | unsigned int max_usage = READ_ONCE(pipe->max_usage); |
047fe360 | 259 | |
8cefc107 DH |
260 | spd->nr_pages_max = max_usage; |
261 | if (max_usage <= PIPE_DEF_BUFFERS) | |
35f3d14d JA |
262 | return 0; |
263 | ||
8cefc107 DH |
264 | spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); |
265 | spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), | |
6da2ec56 | 266 | GFP_KERNEL); |
35f3d14d JA |
267 | |
268 | if (spd->pages && spd->partial) | |
269 | return 0; | |
270 | ||
271 | kfree(spd->pages); | |
272 | kfree(spd->partial); | |
273 | return -ENOMEM; | |
274 | } | |
275 | ||
047fe360 | 276 | void splice_shrink_spd(struct splice_pipe_desc *spd) |
35f3d14d | 277 | { |
047fe360 | 278 | if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) |
35f3d14d JA |
279 | return; |
280 | ||
281 | kfree(spd->pages); | |
282 | kfree(spd->partial); | |
283 | } | |
284 | ||
83f9135b JA |
285 | /** |
286 | * generic_file_splice_read - splice data from file to a pipe | |
287 | * @in: file to splice from | |
932cc6d4 | 288 | * @ppos: position in @in |
83f9135b JA |
289 | * @pipe: pipe to splice to |
290 | * @len: number of bytes to splice | |
291 | * @flags: splice modifier flags | |
292 | * | |
932cc6d4 JA |
293 | * Description: |
294 | * Will read pages from given file and fill them into a pipe. Can be | |
82c156f8 | 295 | * used as long as it has more or less sane ->read_iter(). |
932cc6d4 | 296 | * |
83f9135b | 297 | */ |
cbb7e577 JA |
298 | ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, |
299 | struct pipe_inode_info *pipe, size_t len, | |
300 | unsigned int flags) | |
5274f052 | 301 | { |
82c156f8 AV |
302 | struct iov_iter to; |
303 | struct kiocb kiocb; | |
8cefc107 DH |
304 | unsigned int i_head; |
305 | int ret; | |
be64f884 | 306 | |
aa563d7b | 307 | iov_iter_pipe(&to, READ, pipe, len); |
8cefc107 | 308 | i_head = to.head; |
82c156f8 AV |
309 | init_sync_kiocb(&kiocb, in); |
310 | kiocb.ki_pos = *ppos; | |
bb7462b6 | 311 | ret = call_read_iter(in, &kiocb, &to); |
723590ed | 312 | if (ret > 0) { |
82c156f8 | 313 | *ppos = kiocb.ki_pos; |
723590ed | 314 | file_accessed(in); |
82c156f8 | 315 | } else if (ret < 0) { |
8cefc107 | 316 | to.head = i_head; |
c3a69024 AV |
317 | to.iov_offset = 0; |
318 | iov_iter_advance(&to, 0); /* to free what was emitted */ | |
82c156f8 AV |
319 | /* |
320 | * callers of ->splice_read() expect -EAGAIN on | |
321 | * "can't put anything in there", rather than -EFAULT. | |
322 | */ | |
323 | if (ret == -EFAULT) | |
324 | ret = -EAGAIN; | |
723590ed | 325 | } |
5274f052 JA |
326 | |
327 | return ret; | |
328 | } | |
059a8f37 JA |
329 | EXPORT_SYMBOL(generic_file_splice_read); |
330 | ||
241699cd | 331 | const struct pipe_buf_operations default_pipe_buf_ops = { |
c928f642 CH |
332 | .release = generic_pipe_buf_release, |
333 | .try_steal = generic_pipe_buf_try_steal, | |
334 | .get = generic_pipe_buf_get, | |
6818173b MS |
335 | }; |
336 | ||
28a625cb MS |
337 | /* Pipe buffer operations for a socket and similar. */ |
338 | const struct pipe_buf_operations nosteal_pipe_buf_ops = { | |
c928f642 CH |
339 | .release = generic_pipe_buf_release, |
340 | .get = generic_pipe_buf_get, | |
28a625cb MS |
341 | }; |
342 | EXPORT_SYMBOL(nosteal_pipe_buf_ops); | |
343 | ||
523ac9af | 344 | static ssize_t kernel_readv(struct file *file, const struct kvec *vec, |
6818173b MS |
345 | unsigned long vlen, loff_t offset) |
346 | { | |
347 | mm_segment_t old_fs; | |
348 | loff_t pos = offset; | |
349 | ssize_t res; | |
350 | ||
351 | old_fs = get_fs(); | |
736706be | 352 | set_fs(KERNEL_DS); |
6818173b | 353 | /* The cast to a user pointer is valid due to the set_fs() */ |
793b80ef | 354 | res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0); |
6818173b MS |
355 | set_fs(old_fs); |
356 | ||
357 | return res; | |
358 | } | |
359 | ||
82c156f8 | 360 | static ssize_t default_file_splice_read(struct file *in, loff_t *ppos, |
6818173b MS |
361 | struct pipe_inode_info *pipe, size_t len, |
362 | unsigned int flags) | |
363 | { | |
523ac9af AV |
364 | struct kvec *vec, __vec[PIPE_DEF_BUFFERS]; |
365 | struct iov_iter to; | |
366 | struct page **pages; | |
6818173b | 367 | unsigned int nr_pages; |
8cefc107 | 368 | unsigned int mask; |
13c0f52b | 369 | size_t offset, base, copied = 0; |
6818173b | 370 | ssize_t res; |
6818173b | 371 | int i; |
35f3d14d | 372 | |
6718b6f8 | 373 | if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) |
523ac9af | 374 | return -EAGAIN; |
35f3d14d | 375 | |
523ac9af AV |
376 | /* |
377 | * Try to keep page boundaries matching to source pagecache ones - | |
378 | * it probably won't be much help, but... | |
379 | */ | |
09cbfeaf | 380 | offset = *ppos & ~PAGE_MASK; |
6818173b | 381 | |
aa563d7b | 382 | iov_iter_pipe(&to, READ, pipe, len + offset); |
6818173b | 383 | |
13c0f52b | 384 | res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base); |
523ac9af AV |
385 | if (res <= 0) |
386 | return -ENOMEM; | |
6818173b | 387 | |
13c0f52b | 388 | nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE); |
6818173b | 389 | |
523ac9af AV |
390 | vec = __vec; |
391 | if (nr_pages > PIPE_DEF_BUFFERS) { | |
6da2ec56 | 392 | vec = kmalloc_array(nr_pages, sizeof(struct kvec), GFP_KERNEL); |
523ac9af AV |
393 | if (unlikely(!vec)) { |
394 | res = -ENOMEM; | |
395 | goto out; | |
396 | } | |
77f6bf57 | 397 | } |
6818173b | 398 | |
8cefc107 DH |
399 | mask = pipe->ring_size - 1; |
400 | pipe->bufs[to.head & mask].offset = offset; | |
401 | pipe->bufs[to.head & mask].len -= offset; | |
523ac9af AV |
402 | |
403 | for (i = 0; i < nr_pages; i++) { | |
404 | size_t this_len = min_t(size_t, len, PAGE_SIZE - offset); | |
405 | vec[i].iov_base = page_address(pages[i]) + offset; | |
406 | vec[i].iov_len = this_len; | |
407 | len -= this_len; | |
408 | offset = 0; | |
6818173b | 409 | } |
6818173b | 410 | |
523ac9af AV |
411 | res = kernel_readv(in, vec, nr_pages, *ppos); |
412 | if (res > 0) { | |
413 | copied = res; | |
6818173b | 414 | *ppos += res; |
523ac9af | 415 | } |
6818173b | 416 | |
35f3d14d JA |
417 | if (vec != __vec) |
418 | kfree(vec); | |
523ac9af AV |
419 | out: |
420 | for (i = 0; i < nr_pages; i++) | |
421 | put_page(pages[i]); | |
422 | kvfree(pages); | |
423 | iov_iter_advance(&to, copied); /* truncates and discards */ | |
6818173b | 424 | return res; |
6818173b | 425 | } |
6818173b | 426 | |
5274f052 | 427 | /* |
4f6f0bd2 | 428 | * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' |
016b661e | 429 | * using sendpage(). Return the number of bytes sent. |
5274f052 | 430 | */ |
76ad4d11 | 431 | static int pipe_to_sendpage(struct pipe_inode_info *pipe, |
5274f052 JA |
432 | struct pipe_buffer *buf, struct splice_desc *sd) |
433 | { | |
6a14b90b | 434 | struct file *file = sd->u.file; |
5274f052 | 435 | loff_t pos = sd->pos; |
a8adbe37 | 436 | int more; |
5274f052 | 437 | |
72c2d531 | 438 | if (!likely(file->f_op->sendpage)) |
a8adbe37 MM |
439 | return -EINVAL; |
440 | ||
35f9c09f | 441 | more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; |
ae62ca7b | 442 | |
8cefc107 DH |
443 | if (sd->len < sd->total_len && |
444 | pipe_occupancy(pipe->head, pipe->tail) > 1) | |
35f9c09f | 445 | more |= MSG_SENDPAGE_NOTLAST; |
ae62ca7b | 446 | |
a8adbe37 MM |
447 | return file->f_op->sendpage(file, buf->page, buf->offset, |
448 | sd->len, &pos, more); | |
5274f052 JA |
449 | } |
450 | ||
b3c2d2dd MS |
451 | static void wakeup_pipe_writers(struct pipe_inode_info *pipe) |
452 | { | |
453 | smp_mb(); | |
0ddad21d LT |
454 | if (waitqueue_active(&pipe->wr_wait)) |
455 | wake_up_interruptible(&pipe->wr_wait); | |
b3c2d2dd MS |
456 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
457 | } | |
458 | ||
932cc6d4 | 459 | /** |
b3c2d2dd | 460 | * splice_from_pipe_feed - feed available data from a pipe to a file |
932cc6d4 JA |
461 | * @pipe: pipe to splice from |
462 | * @sd: information to @actor | |
463 | * @actor: handler that splices the data | |
464 | * | |
465 | * Description: | |
b3c2d2dd MS |
466 | * This function loops over the pipe and calls @actor to do the |
467 | * actual moving of a single struct pipe_buffer to the desired | |
468 | * destination. It returns when there's no more buffers left in | |
469 | * the pipe or if the requested number of bytes (@sd->total_len) | |
470 | * have been copied. It returns a positive number (one) if the | |
471 | * pipe needs to be filled with more data, zero if the required | |
472 | * number of bytes have been copied and -errno on error. | |
932cc6d4 | 473 | * |
b3c2d2dd MS |
474 | * This, together with splice_from_pipe_{begin,end,next}, may be |
475 | * used to implement the functionality of __splice_from_pipe() when | |
476 | * locking is required around copying the pipe buffers to the | |
477 | * destination. | |
83f9135b | 478 | */ |
96f9bc8f | 479 | static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, |
b3c2d2dd | 480 | splice_actor *actor) |
5274f052 | 481 | { |
8cefc107 DH |
482 | unsigned int head = pipe->head; |
483 | unsigned int tail = pipe->tail; | |
484 | unsigned int mask = pipe->ring_size - 1; | |
b3c2d2dd | 485 | int ret; |
5274f052 | 486 | |
ec057595 | 487 | while (!pipe_empty(head, tail)) { |
8cefc107 | 488 | struct pipe_buffer *buf = &pipe->bufs[tail & mask]; |
5274f052 | 489 | |
b3c2d2dd MS |
490 | sd->len = buf->len; |
491 | if (sd->len > sd->total_len) | |
492 | sd->len = sd->total_len; | |
5274f052 | 493 | |
fba597db | 494 | ret = pipe_buf_confirm(pipe, buf); |
a8adbe37 | 495 | if (unlikely(ret)) { |
b3c2d2dd MS |
496 | if (ret == -ENODATA) |
497 | ret = 0; | |
498 | return ret; | |
499 | } | |
a8adbe37 MM |
500 | |
501 | ret = actor(pipe, buf, sd); | |
502 | if (ret <= 0) | |
503 | return ret; | |
504 | ||
b3c2d2dd MS |
505 | buf->offset += ret; |
506 | buf->len -= ret; | |
507 | ||
508 | sd->num_spliced += ret; | |
509 | sd->len -= ret; | |
510 | sd->pos += ret; | |
511 | sd->total_len -= ret; | |
512 | ||
513 | if (!buf->len) { | |
a779638c | 514 | pipe_buf_release(pipe, buf); |
8cefc107 DH |
515 | tail++; |
516 | pipe->tail = tail; | |
6447a3cf | 517 | if (pipe->files) |
b3c2d2dd MS |
518 | sd->need_wakeup = true; |
519 | } | |
5274f052 | 520 | |
b3c2d2dd MS |
521 | if (!sd->total_len) |
522 | return 0; | |
523 | } | |
5274f052 | 524 | |
b3c2d2dd MS |
525 | return 1; |
526 | } | |
5274f052 | 527 | |
d1a819a2 LT |
528 | /* We know we have a pipe buffer, but maybe it's empty? */ |
529 | static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) | |
530 | { | |
531 | unsigned int tail = pipe->tail; | |
532 | unsigned int mask = pipe->ring_size - 1; | |
533 | struct pipe_buffer *buf = &pipe->bufs[tail & mask]; | |
534 | ||
535 | if (unlikely(!buf->len)) { | |
536 | pipe_buf_release(pipe, buf); | |
537 | pipe->tail = tail+1; | |
538 | return true; | |
539 | } | |
540 | ||
541 | return false; | |
542 | } | |
543 | ||
b3c2d2dd MS |
544 | /** |
545 | * splice_from_pipe_next - wait for some data to splice from | |
546 | * @pipe: pipe to splice from | |
547 | * @sd: information about the splice operation | |
548 | * | |
549 | * Description: | |
550 | * This function will wait for some data and return a positive | |
551 | * value (one) if pipe buffers are available. It will return zero | |
552 | * or -errno if no more data needs to be spliced. | |
553 | */ | |
96f9bc8f | 554 | static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) |
b3c2d2dd | 555 | { |
c725bfce JK |
556 | /* |
557 | * Check for signal early to make process killable when there are | |
558 | * always buffers available | |
559 | */ | |
560 | if (signal_pending(current)) | |
561 | return -ERESTARTSYS; | |
562 | ||
d1a819a2 | 563 | repeat: |
8cefc107 | 564 | while (pipe_empty(pipe->head, pipe->tail)) { |
b3c2d2dd MS |
565 | if (!pipe->writers) |
566 | return 0; | |
016b661e | 567 | |
a28c8b9d | 568 | if (sd->num_spliced) |
b3c2d2dd | 569 | return 0; |
73d62d83 | 570 | |
b3c2d2dd MS |
571 | if (sd->flags & SPLICE_F_NONBLOCK) |
572 | return -EAGAIN; | |
5274f052 | 573 | |
b3c2d2dd MS |
574 | if (signal_pending(current)) |
575 | return -ERESTARTSYS; | |
5274f052 | 576 | |
b3c2d2dd MS |
577 | if (sd->need_wakeup) { |
578 | wakeup_pipe_writers(pipe); | |
579 | sd->need_wakeup = false; | |
5274f052 JA |
580 | } |
581 | ||
472e5b05 | 582 | pipe_wait_readable(pipe); |
b3c2d2dd | 583 | } |
29e35094 | 584 | |
d1a819a2 LT |
585 | if (eat_empty_buffer(pipe)) |
586 | goto repeat; | |
587 | ||
b3c2d2dd MS |
588 | return 1; |
589 | } | |
5274f052 | 590 | |
b3c2d2dd MS |
591 | /** |
592 | * splice_from_pipe_begin - start splicing from pipe | |
b80901bb | 593 | * @sd: information about the splice operation |
b3c2d2dd MS |
594 | * |
595 | * Description: | |
596 | * This function should be called before a loop containing | |
597 | * splice_from_pipe_next() and splice_from_pipe_feed() to | |
598 | * initialize the necessary fields of @sd. | |
599 | */ | |
96f9bc8f | 600 | static void splice_from_pipe_begin(struct splice_desc *sd) |
b3c2d2dd MS |
601 | { |
602 | sd->num_spliced = 0; | |
603 | sd->need_wakeup = false; | |
604 | } | |
5274f052 | 605 | |
b3c2d2dd MS |
606 | /** |
607 | * splice_from_pipe_end - finish splicing from pipe | |
608 | * @pipe: pipe to splice from | |
609 | * @sd: information about the splice operation | |
610 | * | |
611 | * Description: | |
612 | * This function will wake up pipe writers if necessary. It should | |
613 | * be called after a loop containing splice_from_pipe_next() and | |
614 | * splice_from_pipe_feed(). | |
615 | */ | |
96f9bc8f | 616 | static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) |
b3c2d2dd MS |
617 | { |
618 | if (sd->need_wakeup) | |
619 | wakeup_pipe_writers(pipe); | |
620 | } | |
5274f052 | 621 | |
b3c2d2dd MS |
622 | /** |
623 | * __splice_from_pipe - splice data from a pipe to given actor | |
624 | * @pipe: pipe to splice from | |
625 | * @sd: information to @actor | |
626 | * @actor: handler that splices the data | |
627 | * | |
628 | * Description: | |
629 | * This function does little more than loop over the pipe and call | |
630 | * @actor to do the actual moving of a single struct pipe_buffer to | |
631 | * the desired destination. See pipe_to_file, pipe_to_sendpage, or | |
632 | * pipe_to_user. | |
633 | * | |
634 | */ | |
635 | ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, | |
636 | splice_actor *actor) | |
637 | { | |
638 | int ret; | |
5274f052 | 639 | |
b3c2d2dd MS |
640 | splice_from_pipe_begin(sd); |
641 | do { | |
c2489e07 | 642 | cond_resched(); |
b3c2d2dd MS |
643 | ret = splice_from_pipe_next(pipe, sd); |
644 | if (ret > 0) | |
645 | ret = splice_from_pipe_feed(pipe, sd, actor); | |
646 | } while (ret > 0); | |
647 | splice_from_pipe_end(pipe, sd); | |
648 | ||
649 | return sd->num_spliced ? sd->num_spliced : ret; | |
5274f052 | 650 | } |
40bee44e | 651 | EXPORT_SYMBOL(__splice_from_pipe); |
5274f052 | 652 | |
932cc6d4 JA |
653 | /** |
654 | * splice_from_pipe - splice data from a pipe to a file | |
655 | * @pipe: pipe to splice from | |
656 | * @out: file to splice to | |
657 | * @ppos: position in @out | |
658 | * @len: how many bytes to splice | |
659 | * @flags: splice modifier flags | |
660 | * @actor: handler that splices the data | |
661 | * | |
662 | * Description: | |
2933970b | 663 | * See __splice_from_pipe. This function locks the pipe inode, |
932cc6d4 JA |
664 | * otherwise it's identical to __splice_from_pipe(). |
665 | * | |
666 | */ | |
6da61809 MF |
667 | ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, |
668 | loff_t *ppos, size_t len, unsigned int flags, | |
669 | splice_actor *actor) | |
670 | { | |
671 | ssize_t ret; | |
c66ab6fa JA |
672 | struct splice_desc sd = { |
673 | .total_len = len, | |
674 | .flags = flags, | |
675 | .pos = *ppos, | |
6a14b90b | 676 | .u.file = out, |
c66ab6fa | 677 | }; |
6da61809 | 678 | |
61e0d47c | 679 | pipe_lock(pipe); |
c66ab6fa | 680 | ret = __splice_from_pipe(pipe, &sd, actor); |
61e0d47c | 681 | pipe_unlock(pipe); |
6da61809 MF |
682 | |
683 | return ret; | |
684 | } | |
685 | ||
8d020765 AV |
686 | /** |
687 | * iter_file_splice_write - splice data from a pipe to a file | |
688 | * @pipe: pipe info | |
689 | * @out: file to write to | |
690 | * @ppos: position in @out | |
691 | * @len: number of bytes to splice | |
692 | * @flags: splice modifier flags | |
693 | * | |
694 | * Description: | |
695 | * Will either move or copy pages (determined by @flags options) from | |
696 | * the given pipe inode to the given file. | |
697 | * This one is ->write_iter-based. | |
698 | * | |
699 | */ | |
700 | ssize_t | |
701 | iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, | |
702 | loff_t *ppos, size_t len, unsigned int flags) | |
703 | { | |
704 | struct splice_desc sd = { | |
705 | .total_len = len, | |
706 | .flags = flags, | |
707 | .pos = *ppos, | |
708 | .u.file = out, | |
709 | }; | |
6718b6f8 | 710 | int nbufs = pipe->max_usage; |
8d020765 AV |
711 | struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), |
712 | GFP_KERNEL); | |
713 | ssize_t ret; | |
714 | ||
715 | if (unlikely(!array)) | |
716 | return -ENOMEM; | |
717 | ||
718 | pipe_lock(pipe); | |
719 | ||
720 | splice_from_pipe_begin(&sd); | |
721 | while (sd.total_len) { | |
722 | struct iov_iter from; | |
ec057595 | 723 | unsigned int head, tail, mask; |
8d020765 | 724 | size_t left; |
8cefc107 | 725 | int n; |
8d020765 AV |
726 | |
727 | ret = splice_from_pipe_next(pipe, &sd); | |
728 | if (ret <= 0) | |
729 | break; | |
730 | ||
6718b6f8 | 731 | if (unlikely(nbufs < pipe->max_usage)) { |
8d020765 | 732 | kfree(array); |
6718b6f8 | 733 | nbufs = pipe->max_usage; |
8d020765 AV |
734 | array = kcalloc(nbufs, sizeof(struct bio_vec), |
735 | GFP_KERNEL); | |
736 | if (!array) { | |
737 | ret = -ENOMEM; | |
738 | break; | |
739 | } | |
740 | } | |
741 | ||
ec057595 LT |
742 | head = pipe->head; |
743 | tail = pipe->tail; | |
744 | mask = pipe->ring_size - 1; | |
745 | ||
8d020765 AV |
746 | /* build the vector */ |
747 | left = sd.total_len; | |
8cefc107 DH |
748 | for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) { |
749 | struct pipe_buffer *buf = &pipe->bufs[tail & mask]; | |
8d020765 AV |
750 | size_t this_len = buf->len; |
751 | ||
752 | if (this_len > left) | |
753 | this_len = left; | |
754 | ||
fba597db | 755 | ret = pipe_buf_confirm(pipe, buf); |
8d020765 AV |
756 | if (unlikely(ret)) { |
757 | if (ret == -ENODATA) | |
758 | ret = 0; | |
759 | goto done; | |
760 | } | |
761 | ||
762 | array[n].bv_page = buf->page; | |
763 | array[n].bv_len = this_len; | |
764 | array[n].bv_offset = buf->offset; | |
765 | left -= this_len; | |
766 | } | |
767 | ||
aa563d7b | 768 | iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left); |
abbb6589 | 769 | ret = vfs_iter_write(out, &from, &sd.pos, 0); |
8d020765 AV |
770 | if (ret <= 0) |
771 | break; | |
772 | ||
773 | sd.num_spliced += ret; | |
774 | sd.total_len -= ret; | |
dbe4e192 | 775 | *ppos = sd.pos; |
8d020765 AV |
776 | |
777 | /* dismiss the fully eaten buffers, adjust the partial one */ | |
8cefc107 | 778 | tail = pipe->tail; |
8d020765 | 779 | while (ret) { |
8cefc107 | 780 | struct pipe_buffer *buf = &pipe->bufs[tail & mask]; |
8d020765 | 781 | if (ret >= buf->len) { |
8d020765 AV |
782 | ret -= buf->len; |
783 | buf->len = 0; | |
a779638c | 784 | pipe_buf_release(pipe, buf); |
8cefc107 DH |
785 | tail++; |
786 | pipe->tail = tail; | |
8d020765 AV |
787 | if (pipe->files) |
788 | sd.need_wakeup = true; | |
789 | } else { | |
790 | buf->offset += ret; | |
791 | buf->len -= ret; | |
792 | ret = 0; | |
793 | } | |
794 | } | |
795 | } | |
796 | done: | |
797 | kfree(array); | |
798 | splice_from_pipe_end(pipe, &sd); | |
799 | ||
800 | pipe_unlock(pipe); | |
801 | ||
802 | if (sd.num_spliced) | |
803 | ret = sd.num_spliced; | |
804 | ||
805 | return ret; | |
806 | } | |
807 | ||
808 | EXPORT_SYMBOL(iter_file_splice_write); | |
809 | ||
b2858d7d MS |
810 | static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
811 | struct splice_desc *sd) | |
0b0a47f5 | 812 | { |
b2858d7d MS |
813 | int ret; |
814 | void *data; | |
06ae43f3 | 815 | loff_t tmp = sd->pos; |
b2858d7d | 816 | |
fbb32750 | 817 | data = kmap(buf->page); |
06ae43f3 | 818 | ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp); |
fbb32750 | 819 | kunmap(buf->page); |
b2858d7d MS |
820 | |
821 | return ret; | |
0b0a47f5 MS |
822 | } |
823 | ||
824 | static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, | |
825 | struct file *out, loff_t *ppos, | |
826 | size_t len, unsigned int flags) | |
827 | { | |
b2858d7d | 828 | ssize_t ret; |
0b0a47f5 | 829 | |
b2858d7d MS |
830 | ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); |
831 | if (ret > 0) | |
832 | *ppos += ret; | |
0b0a47f5 | 833 | |
b2858d7d | 834 | return ret; |
0b0a47f5 MS |
835 | } |
836 | ||
83f9135b JA |
837 | /** |
838 | * generic_splice_sendpage - splice data from a pipe to a socket | |
932cc6d4 | 839 | * @pipe: pipe to splice from |
83f9135b | 840 | * @out: socket to write to |
932cc6d4 | 841 | * @ppos: position in @out |
83f9135b JA |
842 | * @len: number of bytes to splice |
843 | * @flags: splice modifier flags | |
844 | * | |
932cc6d4 JA |
845 | * Description: |
846 | * Will send @len bytes from the pipe to a network socket. No data copying | |
847 | * is involved. | |
83f9135b JA |
848 | * |
849 | */ | |
3a326a2c | 850 | ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, |
cbb7e577 | 851 | loff_t *ppos, size_t len, unsigned int flags) |
5274f052 | 852 | { |
00522fb4 | 853 | return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); |
5274f052 JA |
854 | } |
855 | ||
059a8f37 | 856 | EXPORT_SYMBOL(generic_splice_sendpage); |
a0f06780 | 857 | |
83f9135b JA |
858 | /* |
859 | * Attempt to initiate a splice from pipe to file. | |
860 | */ | |
3a326a2c | 861 | static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, |
cbb7e577 | 862 | loff_t *ppos, size_t len, unsigned int flags) |
5274f052 | 863 | { |
72c2d531 | 864 | if (out->f_op->splice_write) |
00c285d0 CH |
865 | return out->f_op->splice_write(pipe, out, ppos, len, flags); |
866 | return default_file_splice_write(pipe, out, ppos, len, flags); | |
5274f052 JA |
867 | } |
868 | ||
83f9135b JA |
869 | /* |
870 | * Attempt to initiate a splice from a file to a pipe. | |
871 | */ | |
cbb7e577 JA |
872 | static long do_splice_to(struct file *in, loff_t *ppos, |
873 | struct pipe_inode_info *pipe, size_t len, | |
874 | unsigned int flags) | |
5274f052 | 875 | { |
5274f052 JA |
876 | int ret; |
877 | ||
49570e9b | 878 | if (unlikely(!(in->f_mode & FMODE_READ))) |
5274f052 JA |
879 | return -EBADF; |
880 | ||
cbb7e577 | 881 | ret = rw_verify_area(READ, in, ppos, len); |
5274f052 JA |
882 | if (unlikely(ret < 0)) |
883 | return ret; | |
884 | ||
03cc0789 AV |
885 | if (unlikely(len > MAX_RW_COUNT)) |
886 | len = MAX_RW_COUNT; | |
887 | ||
72c2d531 | 888 | if (in->f_op->splice_read) |
2bc01060 CH |
889 | return in->f_op->splice_read(in, ppos, pipe, len, flags); |
890 | return default_file_splice_read(in, ppos, pipe, len, flags); | |
5274f052 JA |
891 | } |
892 | ||
932cc6d4 JA |
893 | /** |
894 | * splice_direct_to_actor - splices data directly between two non-pipes | |
895 | * @in: file to splice from | |
896 | * @sd: actor information on where to splice to | |
897 | * @actor: handles the data splicing | |
898 | * | |
899 | * Description: | |
900 | * This is a special case helper to splice directly between two | |
901 | * points, without requiring an explicit pipe. Internally an allocated | |
79685b8d | 902 | * pipe is cached in the process, and reused during the lifetime of |
932cc6d4 JA |
903 | * that process. |
904 | * | |
c66ab6fa JA |
905 | */ |
906 | ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, | |
907 | splice_direct_actor *actor) | |
b92ce558 JA |
908 | { |
909 | struct pipe_inode_info *pipe; | |
910 | long ret, bytes; | |
911 | umode_t i_mode; | |
c66ab6fa | 912 | size_t len; |
0ff28d9f | 913 | int i, flags, more; |
b92ce558 JA |
914 | |
915 | /* | |
916 | * We require the input being a regular file, as we don't want to | |
917 | * randomly drop data for eg socket -> socket splicing. Use the | |
918 | * piped splicing for that! | |
919 | */ | |
496ad9aa | 920 | i_mode = file_inode(in)->i_mode; |
b92ce558 JA |
921 | if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) |
922 | return -EINVAL; | |
923 | ||
924 | /* | |
925 | * neither in nor out is a pipe, setup an internal pipe attached to | |
926 | * 'out' and transfer the wanted data from 'in' to 'out' through that | |
927 | */ | |
928 | pipe = current->splice_pipe; | |
49570e9b | 929 | if (unlikely(!pipe)) { |
7bee130e | 930 | pipe = alloc_pipe_info(); |
b92ce558 JA |
931 | if (!pipe) |
932 | return -ENOMEM; | |
933 | ||
934 | /* | |
935 | * We don't have an immediate reader, but we'll read the stuff | |
00522fb4 | 936 | * out of the pipe right after the splice_to_pipe(). So set |
b92ce558 JA |
937 | * PIPE_READERS appropriately. |
938 | */ | |
939 | pipe->readers = 1; | |
940 | ||
941 | current->splice_pipe = pipe; | |
942 | } | |
943 | ||
944 | /* | |
73d62d83 | 945 | * Do the splice. |
b92ce558 JA |
946 | */ |
947 | ret = 0; | |
948 | bytes = 0; | |
c66ab6fa JA |
949 | len = sd->total_len; |
950 | flags = sd->flags; | |
951 | ||
952 | /* | |
953 | * Don't block on output, we have to drain the direct pipe. | |
954 | */ | |
955 | sd->flags &= ~SPLICE_F_NONBLOCK; | |
0ff28d9f | 956 | more = sd->flags & SPLICE_F_MORE; |
b92ce558 | 957 | |
8cefc107 | 958 | WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); |
17614445 | 959 | |
b92ce558 | 960 | while (len) { |
8cefc107 | 961 | unsigned int p_space; |
51a92c0f | 962 | size_t read_len; |
a82c53a0 | 963 | loff_t pos = sd->pos, prev_pos = pos; |
b92ce558 | 964 | |
17614445 | 965 | /* Don't try to read more the pipe has space for. */ |
6718b6f8 | 966 | p_space = pipe->max_usage - |
8cefc107 DH |
967 | pipe_occupancy(pipe->head, pipe->tail); |
968 | read_len = min_t(size_t, len, p_space << PAGE_SHIFT); | |
17614445 | 969 | ret = do_splice_to(in, &pos, pipe, read_len, flags); |
51a92c0f | 970 | if (unlikely(ret <= 0)) |
b92ce558 JA |
971 | goto out_release; |
972 | ||
973 | read_len = ret; | |
c66ab6fa | 974 | sd->total_len = read_len; |
b92ce558 | 975 | |
0ff28d9f CL |
976 | /* |
977 | * If more data is pending, set SPLICE_F_MORE | |
978 | * If this is the last data and SPLICE_F_MORE was not set | |
979 | * initially, clears it. | |
980 | */ | |
981 | if (read_len < len) | |
982 | sd->flags |= SPLICE_F_MORE; | |
983 | else if (!more) | |
984 | sd->flags &= ~SPLICE_F_MORE; | |
b92ce558 JA |
985 | /* |
986 | * NOTE: nonblocking mode only applies to the input. We | |
987 | * must not do the output in nonblocking mode as then we | |
988 | * could get stuck data in the internal pipe: | |
989 | */ | |
c66ab6fa | 990 | ret = actor(pipe, sd); |
a82c53a0 TZ |
991 | if (unlikely(ret <= 0)) { |
992 | sd->pos = prev_pos; | |
b92ce558 | 993 | goto out_release; |
a82c53a0 | 994 | } |
b92ce558 JA |
995 | |
996 | bytes += ret; | |
997 | len -= ret; | |
bcd4f3ac | 998 | sd->pos = pos; |
b92ce558 | 999 | |
a82c53a0 TZ |
1000 | if (ret < read_len) { |
1001 | sd->pos = prev_pos + ret; | |
51a92c0f | 1002 | goto out_release; |
a82c53a0 | 1003 | } |
b92ce558 JA |
1004 | } |
1005 | ||
9e97198d | 1006 | done: |
8cefc107 | 1007 | pipe->tail = pipe->head = 0; |
80848708 | 1008 | file_accessed(in); |
b92ce558 JA |
1009 | return bytes; |
1010 | ||
1011 | out_release: | |
1012 | /* | |
1013 | * If we did an incomplete transfer we must release | |
1014 | * the pipe buffers in question: | |
1015 | */ | |
8cefc107 DH |
1016 | for (i = 0; i < pipe->ring_size; i++) { |
1017 | struct pipe_buffer *buf = &pipe->bufs[i]; | |
b92ce558 | 1018 | |
a779638c MS |
1019 | if (buf->ops) |
1020 | pipe_buf_release(pipe, buf); | |
b92ce558 | 1021 | } |
b92ce558 | 1022 | |
9e97198d JA |
1023 | if (!bytes) |
1024 | bytes = ret; | |
c66ab6fa | 1025 | |
9e97198d | 1026 | goto done; |
c66ab6fa JA |
1027 | } |
1028 | EXPORT_SYMBOL(splice_direct_to_actor); | |
1029 | ||
1030 | static int direct_splice_actor(struct pipe_inode_info *pipe, | |
1031 | struct splice_desc *sd) | |
1032 | { | |
6a14b90b | 1033 | struct file *file = sd->u.file; |
c66ab6fa | 1034 | |
7995bd28 | 1035 | return do_splice_from(pipe, file, sd->opos, sd->total_len, |
2cb4b05e | 1036 | sd->flags); |
c66ab6fa JA |
1037 | } |
1038 | ||
932cc6d4 JA |
1039 | /** |
1040 | * do_splice_direct - splices data directly between two files | |
1041 | * @in: file to splice from | |
1042 | * @ppos: input file offset | |
1043 | * @out: file to splice to | |
acdb37c3 | 1044 | * @opos: output file offset |
932cc6d4 JA |
1045 | * @len: number of bytes to splice |
1046 | * @flags: splice modifier flags | |
1047 | * | |
1048 | * Description: | |
1049 | * For use by do_sendfile(). splice can easily emulate sendfile, but | |
1050 | * doing it in the application would incur an extra system call | |
1051 | * (splice in + splice out, as compared to just sendfile()). So this helper | |
1052 | * can splice directly through a process-private pipe. | |
1053 | * | |
1054 | */ | |
c66ab6fa | 1055 | long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, |
7995bd28 | 1056 | loff_t *opos, size_t len, unsigned int flags) |
c66ab6fa JA |
1057 | { |
1058 | struct splice_desc sd = { | |
1059 | .len = len, | |
1060 | .total_len = len, | |
1061 | .flags = flags, | |
1062 | .pos = *ppos, | |
6a14b90b | 1063 | .u.file = out, |
7995bd28 | 1064 | .opos = opos, |
c66ab6fa | 1065 | }; |
51a92c0f | 1066 | long ret; |
c66ab6fa | 1067 | |
18c67cb9 AV |
1068 | if (unlikely(!(out->f_mode & FMODE_WRITE))) |
1069 | return -EBADF; | |
1070 | ||
1071 | if (unlikely(out->f_flags & O_APPEND)) | |
1072 | return -EINVAL; | |
1073 | ||
1074 | ret = rw_verify_area(WRITE, out, opos, len); | |
1075 | if (unlikely(ret < 0)) | |
1076 | return ret; | |
1077 | ||
c66ab6fa | 1078 | ret = splice_direct_to_actor(in, &sd, direct_splice_actor); |
51a92c0f | 1079 | if (ret > 0) |
a82c53a0 | 1080 | *ppos = sd.pos; |
51a92c0f | 1081 | |
c66ab6fa | 1082 | return ret; |
b92ce558 | 1083 | } |
1c118596 | 1084 | EXPORT_SYMBOL(do_splice_direct); |
b92ce558 | 1085 | |
8924feff AV |
1086 | static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) |
1087 | { | |
52bce911 LT |
1088 | for (;;) { |
1089 | if (unlikely(!pipe->readers)) { | |
1090 | send_sig(SIGPIPE, current, 0); | |
1091 | return -EPIPE; | |
1092 | } | |
6718b6f8 | 1093 | if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) |
52bce911 | 1094 | return 0; |
8924feff AV |
1095 | if (flags & SPLICE_F_NONBLOCK) |
1096 | return -EAGAIN; | |
1097 | if (signal_pending(current)) | |
1098 | return -ERESTARTSYS; | |
472e5b05 | 1099 | pipe_wait_writable(pipe); |
8924feff | 1100 | } |
8924feff AV |
1101 | } |
1102 | ||
7c77f0b3 MS |
1103 | static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, |
1104 | struct pipe_inode_info *opipe, | |
1105 | size_t len, unsigned int flags); | |
ddac0d39 | 1106 | |
83f9135b JA |
1107 | /* |
1108 | * Determine where to splice to/from. | |
1109 | */ | |
ee6e00c8 JA |
1110 | long do_splice(struct file *in, loff_t *off_in, struct file *out, |
1111 | loff_t *off_out, size_t len, unsigned int flags) | |
5274f052 | 1112 | { |
7c77f0b3 MS |
1113 | struct pipe_inode_info *ipipe; |
1114 | struct pipe_inode_info *opipe; | |
7995bd28 | 1115 | loff_t offset; |
a4514ebd | 1116 | long ret; |
5274f052 | 1117 | |
90da2e3f PB |
1118 | if (unlikely(!(in->f_mode & FMODE_READ) || |
1119 | !(out->f_mode & FMODE_WRITE))) | |
1120 | return -EBADF; | |
1121 | ||
c73be61c DH |
1122 | ipipe = get_pipe_info(in, true); |
1123 | opipe = get_pipe_info(out, true); | |
7c77f0b3 MS |
1124 | |
1125 | if (ipipe && opipe) { | |
1126 | if (off_in || off_out) | |
1127 | return -ESPIPE; | |
1128 | ||
7c77f0b3 MS |
1129 | /* Splicing to self would be fun, but... */ |
1130 | if (ipipe == opipe) | |
1131 | return -EINVAL; | |
1132 | ||
ee5e0011 SK |
1133 | if ((in->f_flags | out->f_flags) & O_NONBLOCK) |
1134 | flags |= SPLICE_F_NONBLOCK; | |
1135 | ||
7c77f0b3 MS |
1136 | return splice_pipe_to_pipe(ipipe, opipe, len, flags); |
1137 | } | |
1138 | ||
1139 | if (ipipe) { | |
529565dc IM |
1140 | if (off_in) |
1141 | return -ESPIPE; | |
b92ce558 | 1142 | if (off_out) { |
19c9a49b | 1143 | if (!(out->f_mode & FMODE_PWRITE)) |
b92ce558 | 1144 | return -EINVAL; |
ee6e00c8 | 1145 | offset = *off_out; |
7995bd28 AV |
1146 | } else { |
1147 | offset = out->f_pos; | |
1148 | } | |
529565dc | 1149 | |
18c67cb9 AV |
1150 | if (unlikely(out->f_flags & O_APPEND)) |
1151 | return -EINVAL; | |
1152 | ||
1153 | ret = rw_verify_area(WRITE, out, &offset, len); | |
1154 | if (unlikely(ret < 0)) | |
1155 | return ret; | |
1156 | ||
ee5e0011 SK |
1157 | if (in->f_flags & O_NONBLOCK) |
1158 | flags |= SPLICE_F_NONBLOCK; | |
1159 | ||
500368f7 | 1160 | file_start_write(out); |
7995bd28 | 1161 | ret = do_splice_from(ipipe, out, &offset, len, flags); |
500368f7 | 1162 | file_end_write(out); |
a4514ebd | 1163 | |
7995bd28 AV |
1164 | if (!off_out) |
1165 | out->f_pos = offset; | |
ee6e00c8 JA |
1166 | else |
1167 | *off_out = offset; | |
a4514ebd JA |
1168 | |
1169 | return ret; | |
529565dc | 1170 | } |
5274f052 | 1171 | |
7c77f0b3 | 1172 | if (opipe) { |
529565dc IM |
1173 | if (off_out) |
1174 | return -ESPIPE; | |
b92ce558 | 1175 | if (off_in) { |
19c9a49b | 1176 | if (!(in->f_mode & FMODE_PREAD)) |
b92ce558 | 1177 | return -EINVAL; |
ee6e00c8 | 1178 | offset = *off_in; |
7995bd28 AV |
1179 | } else { |
1180 | offset = in->f_pos; | |
1181 | } | |
529565dc | 1182 | |
ee5e0011 SK |
1183 | if (out->f_flags & O_NONBLOCK) |
1184 | flags |= SPLICE_F_NONBLOCK; | |
1185 | ||
8924feff AV |
1186 | pipe_lock(opipe); |
1187 | ret = wait_for_space(opipe, flags); | |
3253d9d0 | 1188 | if (!ret) { |
6a965666 | 1189 | unsigned int p_space; |
3253d9d0 DW |
1190 | |
1191 | /* Don't try to read more the pipe has space for. */ | |
6a965666 LT |
1192 | p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail); |
1193 | len = min_t(size_t, len, p_space << PAGE_SHIFT); | |
3253d9d0 | 1194 | |
8924feff | 1195 | ret = do_splice_to(in, &offset, opipe, len, flags); |
3253d9d0 | 1196 | } |
8924feff AV |
1197 | pipe_unlock(opipe); |
1198 | if (ret > 0) | |
1199 | wakeup_pipe_readers(opipe); | |
7995bd28 AV |
1200 | if (!off_in) |
1201 | in->f_pos = offset; | |
ee6e00c8 JA |
1202 | else |
1203 | *off_in = offset; | |
a4514ebd JA |
1204 | |
1205 | return ret; | |
529565dc | 1206 | } |
5274f052 JA |
1207 | |
1208 | return -EINVAL; | |
1209 | } | |
1210 | ||
ee6e00c8 JA |
1211 | static long __do_splice(struct file *in, loff_t __user *off_in, |
1212 | struct file *out, loff_t __user *off_out, | |
1213 | size_t len, unsigned int flags) | |
1214 | { | |
1215 | struct pipe_inode_info *ipipe; | |
1216 | struct pipe_inode_info *opipe; | |
1217 | loff_t offset, *__off_in = NULL, *__off_out = NULL; | |
1218 | long ret; | |
1219 | ||
1220 | ipipe = get_pipe_info(in, true); | |
1221 | opipe = get_pipe_info(out, true); | |
1222 | ||
1223 | if (ipipe && off_in) | |
1224 | return -ESPIPE; | |
1225 | if (opipe && off_out) | |
1226 | return -ESPIPE; | |
1227 | ||
1228 | if (off_out) { | |
1229 | if (copy_from_user(&offset, off_out, sizeof(loff_t))) | |
1230 | return -EFAULT; | |
1231 | __off_out = &offset; | |
1232 | } | |
1233 | if (off_in) { | |
1234 | if (copy_from_user(&offset, off_in, sizeof(loff_t))) | |
1235 | return -EFAULT; | |
1236 | __off_in = &offset; | |
1237 | } | |
1238 | ||
1239 | ret = do_splice(in, __off_in, out, __off_out, len, flags); | |
1240 | if (ret < 0) | |
1241 | return ret; | |
1242 | ||
1243 | if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t))) | |
1244 | return -EFAULT; | |
1245 | if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t))) | |
1246 | return -EFAULT; | |
1247 | ||
1248 | return ret; | |
1249 | } | |
1250 | ||
79fddc4e AV |
1251 | static int iter_to_pipe(struct iov_iter *from, |
1252 | struct pipe_inode_info *pipe, | |
1253 | unsigned flags) | |
912d35f8 | 1254 | { |
79fddc4e AV |
1255 | struct pipe_buffer buf = { |
1256 | .ops = &user_page_pipe_buf_ops, | |
1257 | .flags = flags | |
1258 | }; | |
1259 | size_t total = 0; | |
1260 | int ret = 0; | |
1261 | bool failed = false; | |
1262 | ||
1263 | while (iov_iter_count(from) && !failed) { | |
1264 | struct page *pages[16]; | |
db85a9eb AV |
1265 | ssize_t copied; |
1266 | size_t start; | |
79fddc4e | 1267 | int n; |
db85a9eb | 1268 | |
79fddc4e AV |
1269 | copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); |
1270 | if (copied <= 0) { | |
1271 | ret = copied; | |
1272 | break; | |
1273 | } | |
db85a9eb | 1274 | |
79fddc4e | 1275 | for (n = 0; copied; n++, start = 0) { |
db85a9eb | 1276 | int size = min_t(int, copied, PAGE_SIZE - start); |
79fddc4e AV |
1277 | if (!failed) { |
1278 | buf.page = pages[n]; | |
1279 | buf.offset = start; | |
1280 | buf.len = size; | |
1281 | ret = add_to_pipe(pipe, &buf); | |
1282 | if (unlikely(ret < 0)) { | |
1283 | failed = true; | |
1284 | } else { | |
1285 | iov_iter_advance(from, ret); | |
1286 | total += ret; | |
1287 | } | |
1288 | } else { | |
1289 | put_page(pages[n]); | |
1290 | } | |
db85a9eb | 1291 | copied -= size; |
912d35f8 | 1292 | } |
912d35f8 | 1293 | } |
79fddc4e | 1294 | return total ? total : ret; |
912d35f8 JA |
1295 | } |
1296 | ||
6a14b90b JA |
1297 | static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
1298 | struct splice_desc *sd) | |
1299 | { | |
6130f531 AV |
1300 | int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); |
1301 | return n == sd->len ? n : -EFAULT; | |
6a14b90b JA |
1302 | } |
1303 | ||
1304 | /* | |
1305 | * For lack of a better implementation, implement vmsplice() to userspace | |
1306 | * as a simple copy of the pipes pages to the user iov. | |
1307 | */ | |
87a3002a AV |
1308 | static long vmsplice_to_user(struct file *file, struct iov_iter *iter, |
1309 | unsigned int flags) | |
6a14b90b | 1310 | { |
c73be61c | 1311 | struct pipe_inode_info *pipe = get_pipe_info(file, true); |
87a3002a AV |
1312 | struct splice_desc sd = { |
1313 | .total_len = iov_iter_count(iter), | |
1314 | .flags = flags, | |
1315 | .u.data = iter | |
1316 | }; | |
1317 | long ret = 0; | |
6a14b90b | 1318 | |
6a14b90b JA |
1319 | if (!pipe) |
1320 | return -EBADF; | |
1321 | ||
345995fa AV |
1322 | if (sd.total_len) { |
1323 | pipe_lock(pipe); | |
1324 | ret = __splice_from_pipe(pipe, &sd, pipe_to_user); | |
1325 | pipe_unlock(pipe); | |
1326 | } | |
6a14b90b JA |
1327 | |
1328 | return ret; | |
1329 | } | |
1330 | ||
912d35f8 JA |
1331 | /* |
1332 | * vmsplice splices a user address range into a pipe. It can be thought of | |
1333 | * as splice-from-memory, where the regular splice is splice-from-file (or | |
1334 | * to file). In both cases the output is a pipe, naturally. | |
912d35f8 | 1335 | */ |
87a3002a AV |
1336 | static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, |
1337 | unsigned int flags) | |
912d35f8 | 1338 | { |
ddac0d39 | 1339 | struct pipe_inode_info *pipe; |
87a3002a | 1340 | long ret = 0; |
79fddc4e AV |
1341 | unsigned buf_flag = 0; |
1342 | ||
1343 | if (flags & SPLICE_F_GIFT) | |
1344 | buf_flag = PIPE_BUF_FLAG_GIFT; | |
912d35f8 | 1345 | |
c73be61c | 1346 | pipe = get_pipe_info(file, true); |
ddac0d39 | 1347 | if (!pipe) |
912d35f8 | 1348 | return -EBADF; |
912d35f8 | 1349 | |
8924feff AV |
1350 | pipe_lock(pipe); |
1351 | ret = wait_for_space(pipe, flags); | |
79fddc4e | 1352 | if (!ret) |
87a3002a | 1353 | ret = iter_to_pipe(iter, pipe, buf_flag); |
8924feff AV |
1354 | pipe_unlock(pipe); |
1355 | if (ret > 0) | |
1356 | wakeup_pipe_readers(pipe); | |
35f3d14d | 1357 | return ret; |
912d35f8 JA |
1358 | } |
1359 | ||
87a3002a AV |
1360 | static int vmsplice_type(struct fd f, int *type) |
1361 | { | |
1362 | if (!f.file) | |
1363 | return -EBADF; | |
1364 | if (f.file->f_mode & FMODE_WRITE) { | |
1365 | *type = WRITE; | |
1366 | } else if (f.file->f_mode & FMODE_READ) { | |
1367 | *type = READ; | |
1368 | } else { | |
1369 | fdput(f); | |
1370 | return -EBADF; | |
1371 | } | |
1372 | return 0; | |
1373 | } | |
1374 | ||
6a14b90b JA |
1375 | /* |
1376 | * Note that vmsplice only really supports true splicing _from_ user memory | |
1377 | * to a pipe, not the other way around. Splicing from user memory is a simple | |
1378 | * operation that can be supported without any funky alignment restrictions | |
1379 | * or nasty vm tricks. We simply map in the user memory and fill them into | |
1380 | * a pipe. The reverse isn't quite as easy, though. There are two possible | |
1381 | * solutions for that: | |
1382 | * | |
1383 | * - memcpy() the data internally, at which point we might as well just | |
1384 | * do a regular read() on the buffer anyway. | |
1385 | * - Lots of nasty vm tricks, that are neither fast nor flexible (it | |
1386 | * has restriction limitations on both ends of the pipe). | |
1387 | * | |
1388 | * Currently we punt and implement it as a normal copy, see pipe_to_user(). | |
1389 | * | |
1390 | */ | |
87a3002a | 1391 | SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, |
30cfe4ef DB |
1392 | unsigned long, nr_segs, unsigned int, flags) |
1393 | { | |
87a3002a AV |
1394 | struct iovec iovstack[UIO_FASTIOV]; |
1395 | struct iovec *iov = iovstack; | |
1396 | struct iov_iter iter; | |
87e5e6da | 1397 | ssize_t error; |
87a3002a AV |
1398 | struct fd f; |
1399 | int type; | |
1400 | ||
598b3cec CH |
1401 | if (unlikely(flags & ~SPLICE_F_ALL)) |
1402 | return -EINVAL; | |
1403 | ||
87a3002a AV |
1404 | f = fdget(fd); |
1405 | error = vmsplice_type(f, &type); | |
1406 | if (error) | |
1407 | return error; | |
1408 | ||
1409 | error = import_iovec(type, uiov, nr_segs, | |
1410 | ARRAY_SIZE(iovstack), &iov, &iter); | |
598b3cec CH |
1411 | if (error < 0) |
1412 | goto out_fdput; | |
30cfe4ef | 1413 | |
598b3cec CH |
1414 | if (!iov_iter_count(&iter)) |
1415 | error = 0; | |
1416 | else if (iov_iter_rw(&iter) == WRITE) | |
1417 | error = vmsplice_to_pipe(f.file, &iter, flags); | |
1418 | else | |
1419 | error = vmsplice_to_user(f.file, &iter, flags); | |
87a3002a | 1420 | |
598b3cec CH |
1421 | kfree(iov); |
1422 | out_fdput: | |
87a3002a AV |
1423 | fdput(f); |
1424 | return error; | |
76b021d0 | 1425 | } |
76b021d0 | 1426 | |
836f92ad HC |
1427 | SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, |
1428 | int, fd_out, loff_t __user *, off_out, | |
1429 | size_t, len, unsigned int, flags) | |
5274f052 | 1430 | { |
2903ff01 | 1431 | struct fd in, out; |
5274f052 | 1432 | long error; |
5274f052 JA |
1433 | |
1434 | if (unlikely(!len)) | |
1435 | return 0; | |
1436 | ||
3d6ea290 AV |
1437 | if (unlikely(flags & ~SPLICE_F_ALL)) |
1438 | return -EINVAL; | |
1439 | ||
5274f052 | 1440 | error = -EBADF; |
2903ff01 AV |
1441 | in = fdget(fd_in); |
1442 | if (in.file) { | |
90da2e3f PB |
1443 | out = fdget(fd_out); |
1444 | if (out.file) { | |
ee6e00c8 JA |
1445 | error = __do_splice(in.file, off_in, out.file, off_out, |
1446 | len, flags); | |
90da2e3f | 1447 | fdput(out); |
5274f052 | 1448 | } |
2903ff01 | 1449 | fdput(in); |
5274f052 | 1450 | } |
5274f052 JA |
1451 | return error; |
1452 | } | |
70524490 | 1453 | |
aadd06e5 JA |
1454 | /* |
1455 | * Make sure there's data to read. Wait for input if we can, otherwise | |
1456 | * return an appropriate error. | |
1457 | */ | |
7c77f0b3 | 1458 | static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
aadd06e5 JA |
1459 | { |
1460 | int ret; | |
1461 | ||
1462 | /* | |
8cefc107 | 1463 | * Check the pipe occupancy without the inode lock first. This function |
aadd06e5 JA |
1464 | * is speculative anyways, so missing one is ok. |
1465 | */ | |
8cefc107 | 1466 | if (!pipe_empty(pipe->head, pipe->tail)) |
aadd06e5 JA |
1467 | return 0; |
1468 | ||
1469 | ret = 0; | |
61e0d47c | 1470 | pipe_lock(pipe); |
aadd06e5 | 1471 | |
8cefc107 | 1472 | while (pipe_empty(pipe->head, pipe->tail)) { |
aadd06e5 JA |
1473 | if (signal_pending(current)) { |
1474 | ret = -ERESTARTSYS; | |
1475 | break; | |
1476 | } | |
1477 | if (!pipe->writers) | |
1478 | break; | |
a28c8b9d LT |
1479 | if (flags & SPLICE_F_NONBLOCK) { |
1480 | ret = -EAGAIN; | |
1481 | break; | |
aadd06e5 | 1482 | } |
472e5b05 | 1483 | pipe_wait_readable(pipe); |
aadd06e5 JA |
1484 | } |
1485 | ||
61e0d47c | 1486 | pipe_unlock(pipe); |
aadd06e5 JA |
1487 | return ret; |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * Make sure there's writeable room. Wait for room if we can, otherwise | |
1492 | * return an appropriate error. | |
1493 | */ | |
7c77f0b3 | 1494 | static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
aadd06e5 JA |
1495 | { |
1496 | int ret; | |
1497 | ||
1498 | /* | |
8cefc107 | 1499 | * Check pipe occupancy without the inode lock first. This function |
aadd06e5 JA |
1500 | * is speculative anyways, so missing one is ok. |
1501 | */ | |
566d1362 | 1502 | if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) |
aadd06e5 JA |
1503 | return 0; |
1504 | ||
1505 | ret = 0; | |
61e0d47c | 1506 | pipe_lock(pipe); |
aadd06e5 | 1507 | |
6718b6f8 | 1508 | while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { |
aadd06e5 JA |
1509 | if (!pipe->readers) { |
1510 | send_sig(SIGPIPE, current, 0); | |
1511 | ret = -EPIPE; | |
1512 | break; | |
1513 | } | |
1514 | if (flags & SPLICE_F_NONBLOCK) { | |
1515 | ret = -EAGAIN; | |
1516 | break; | |
1517 | } | |
1518 | if (signal_pending(current)) { | |
1519 | ret = -ERESTARTSYS; | |
1520 | break; | |
1521 | } | |
472e5b05 | 1522 | pipe_wait_writable(pipe); |
aadd06e5 JA |
1523 | } |
1524 | ||
61e0d47c | 1525 | pipe_unlock(pipe); |
aadd06e5 JA |
1526 | return ret; |
1527 | } | |
1528 | ||
7c77f0b3 MS |
1529 | /* |
1530 | * Splice contents of ipipe to opipe. | |
1531 | */ | |
1532 | static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, | |
1533 | struct pipe_inode_info *opipe, | |
1534 | size_t len, unsigned int flags) | |
1535 | { | |
1536 | struct pipe_buffer *ibuf, *obuf; | |
8cefc107 DH |
1537 | unsigned int i_head, o_head; |
1538 | unsigned int i_tail, o_tail; | |
1539 | unsigned int i_mask, o_mask; | |
1540 | int ret = 0; | |
7c77f0b3 MS |
1541 | bool input_wakeup = false; |
1542 | ||
1543 | ||
1544 | retry: | |
1545 | ret = ipipe_prep(ipipe, flags); | |
1546 | if (ret) | |
1547 | return ret; | |
1548 | ||
1549 | ret = opipe_prep(opipe, flags); | |
1550 | if (ret) | |
1551 | return ret; | |
1552 | ||
1553 | /* | |
1554 | * Potential ABBA deadlock, work around it by ordering lock | |
1555 | * grabbing by pipe info address. Otherwise two different processes | |
1556 | * could deadlock (one doing tee from A -> B, the other from B -> A). | |
1557 | */ | |
1558 | pipe_double_lock(ipipe, opipe); | |
1559 | ||
8cefc107 DH |
1560 | i_tail = ipipe->tail; |
1561 | i_mask = ipipe->ring_size - 1; | |
1562 | o_head = opipe->head; | |
1563 | o_mask = opipe->ring_size - 1; | |
1564 | ||
7c77f0b3 | 1565 | do { |
8cefc107 DH |
1566 | size_t o_len; |
1567 | ||
7c77f0b3 MS |
1568 | if (!opipe->readers) { |
1569 | send_sig(SIGPIPE, current, 0); | |
1570 | if (!ret) | |
1571 | ret = -EPIPE; | |
1572 | break; | |
1573 | } | |
1574 | ||
8cefc107 DH |
1575 | i_head = ipipe->head; |
1576 | o_tail = opipe->tail; | |
1577 | ||
1578 | if (pipe_empty(i_head, i_tail) && !ipipe->writers) | |
7c77f0b3 MS |
1579 | break; |
1580 | ||
1581 | /* | |
1582 | * Cannot make any progress, because either the input | |
1583 | * pipe is empty or the output pipe is full. | |
1584 | */ | |
8cefc107 | 1585 | if (pipe_empty(i_head, i_tail) || |
6718b6f8 | 1586 | pipe_full(o_head, o_tail, opipe->max_usage)) { |
7c77f0b3 MS |
1587 | /* Already processed some buffers, break */ |
1588 | if (ret) | |
1589 | break; | |
1590 | ||
1591 | if (flags & SPLICE_F_NONBLOCK) { | |
1592 | ret = -EAGAIN; | |
1593 | break; | |
1594 | } | |
1595 | ||
1596 | /* | |
1597 | * We raced with another reader/writer and haven't | |
1598 | * managed to process any buffers. A zero return | |
1599 | * value means EOF, so retry instead. | |
1600 | */ | |
1601 | pipe_unlock(ipipe); | |
1602 | pipe_unlock(opipe); | |
1603 | goto retry; | |
1604 | } | |
1605 | ||
8cefc107 DH |
1606 | ibuf = &ipipe->bufs[i_tail & i_mask]; |
1607 | obuf = &opipe->bufs[o_head & o_mask]; | |
7c77f0b3 MS |
1608 | |
1609 | if (len >= ibuf->len) { | |
1610 | /* | |
1611 | * Simply move the whole buffer from ipipe to opipe | |
1612 | */ | |
1613 | *obuf = *ibuf; | |
1614 | ibuf->ops = NULL; | |
8cefc107 DH |
1615 | i_tail++; |
1616 | ipipe->tail = i_tail; | |
7c77f0b3 | 1617 | input_wakeup = true; |
8cefc107 DH |
1618 | o_len = obuf->len; |
1619 | o_head++; | |
1620 | opipe->head = o_head; | |
7c77f0b3 MS |
1621 | } else { |
1622 | /* | |
1623 | * Get a reference to this pipe buffer, | |
1624 | * so we can copy the contents over. | |
1625 | */ | |
15fab63e MW |
1626 | if (!pipe_buf_get(ipipe, ibuf)) { |
1627 | if (ret == 0) | |
1628 | ret = -EFAULT; | |
1629 | break; | |
1630 | } | |
7c77f0b3 MS |
1631 | *obuf = *ibuf; |
1632 | ||
1633 | /* | |
f6dd9755 | 1634 | * Don't inherit the gift and merge flags, we need to |
7c77f0b3 MS |
1635 | * prevent multiple steals of this page. |
1636 | */ | |
1637 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; | |
f6dd9755 | 1638 | obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; |
a0ce2f0a | 1639 | |
7c77f0b3 | 1640 | obuf->len = len; |
8cefc107 DH |
1641 | ibuf->offset += len; |
1642 | ibuf->len -= len; | |
1643 | o_len = len; | |
1644 | o_head++; | |
1645 | opipe->head = o_head; | |
7c77f0b3 | 1646 | } |
8cefc107 DH |
1647 | ret += o_len; |
1648 | len -= o_len; | |
7c77f0b3 MS |
1649 | } while (len); |
1650 | ||
1651 | pipe_unlock(ipipe); | |
1652 | pipe_unlock(opipe); | |
1653 | ||
1654 | /* | |
1655 | * If we put data in the output pipe, wakeup any potential readers. | |
1656 | */ | |
825cdcb1 NK |
1657 | if (ret > 0) |
1658 | wakeup_pipe_readers(opipe); | |
1659 | ||
7c77f0b3 MS |
1660 | if (input_wakeup) |
1661 | wakeup_pipe_writers(ipipe); | |
1662 | ||
1663 | return ret; | |
1664 | } | |
1665 | ||
70524490 JA |
1666 | /* |
1667 | * Link contents of ipipe to opipe. | |
1668 | */ | |
1669 | static int link_pipe(struct pipe_inode_info *ipipe, | |
1670 | struct pipe_inode_info *opipe, | |
1671 | size_t len, unsigned int flags) | |
1672 | { | |
1673 | struct pipe_buffer *ibuf, *obuf; | |
8cefc107 DH |
1674 | unsigned int i_head, o_head; |
1675 | unsigned int i_tail, o_tail; | |
1676 | unsigned int i_mask, o_mask; | |
1677 | int ret = 0; | |
70524490 JA |
1678 | |
1679 | /* | |
1680 | * Potential ABBA deadlock, work around it by ordering lock | |
61e0d47c | 1681 | * grabbing by pipe info address. Otherwise two different processes |
70524490 JA |
1682 | * could deadlock (one doing tee from A -> B, the other from B -> A). |
1683 | */ | |
61e0d47c | 1684 | pipe_double_lock(ipipe, opipe); |
70524490 | 1685 | |
8cefc107 DH |
1686 | i_tail = ipipe->tail; |
1687 | i_mask = ipipe->ring_size - 1; | |
1688 | o_head = opipe->head; | |
1689 | o_mask = opipe->ring_size - 1; | |
1690 | ||
aadd06e5 | 1691 | do { |
70524490 JA |
1692 | if (!opipe->readers) { |
1693 | send_sig(SIGPIPE, current, 0); | |
1694 | if (!ret) | |
1695 | ret = -EPIPE; | |
1696 | break; | |
1697 | } | |
70524490 | 1698 | |
8cefc107 DH |
1699 | i_head = ipipe->head; |
1700 | o_tail = opipe->tail; | |
1701 | ||
aadd06e5 | 1702 | /* |
8cefc107 | 1703 | * If we have iterated all input buffers or run out of |
aadd06e5 JA |
1704 | * output room, break. |
1705 | */ | |
8cefc107 | 1706 | if (pipe_empty(i_head, i_tail) || |
6718b6f8 | 1707 | pipe_full(o_head, o_tail, opipe->max_usage)) |
aadd06e5 | 1708 | break; |
70524490 | 1709 | |
8cefc107 DH |
1710 | ibuf = &ipipe->bufs[i_tail & i_mask]; |
1711 | obuf = &opipe->bufs[o_head & o_mask]; | |
70524490 JA |
1712 | |
1713 | /* | |
aadd06e5 JA |
1714 | * Get a reference to this pipe buffer, |
1715 | * so we can copy the contents over. | |
70524490 | 1716 | */ |
15fab63e MW |
1717 | if (!pipe_buf_get(ipipe, ibuf)) { |
1718 | if (ret == 0) | |
1719 | ret = -EFAULT; | |
1720 | break; | |
1721 | } | |
aadd06e5 | 1722 | |
aadd06e5 JA |
1723 | *obuf = *ibuf; |
1724 | ||
2a27250e | 1725 | /* |
f6dd9755 CH |
1726 | * Don't inherit the gift and merge flag, we need to prevent |
1727 | * multiple steals of this page. | |
2a27250e | 1728 | */ |
aadd06e5 | 1729 | obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
f6dd9755 | 1730 | obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; |
a0ce2f0a | 1731 | |
aadd06e5 JA |
1732 | if (obuf->len > len) |
1733 | obuf->len = len; | |
aadd06e5 JA |
1734 | ret += obuf->len; |
1735 | len -= obuf->len; | |
8cefc107 DH |
1736 | |
1737 | o_head++; | |
1738 | opipe->head = o_head; | |
1739 | i_tail++; | |
aadd06e5 | 1740 | } while (len); |
70524490 | 1741 | |
61e0d47c MS |
1742 | pipe_unlock(ipipe); |
1743 | pipe_unlock(opipe); | |
70524490 | 1744 | |
aadd06e5 JA |
1745 | /* |
1746 | * If we put data in the output pipe, wakeup any potential readers. | |
1747 | */ | |
825cdcb1 NK |
1748 | if (ret > 0) |
1749 | wakeup_pipe_readers(opipe); | |
70524490 JA |
1750 | |
1751 | return ret; | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * This is a tee(1) implementation that works on pipes. It doesn't copy | |
1756 | * any data, it simply references the 'in' pages on the 'out' pipe. | |
1757 | * The 'flags' used are the SPLICE_F_* variants, currently the only | |
1758 | * applicable one is SPLICE_F_NONBLOCK. | |
1759 | */ | |
9dafdfc2 | 1760 | long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) |
70524490 | 1761 | { |
c73be61c DH |
1762 | struct pipe_inode_info *ipipe = get_pipe_info(in, true); |
1763 | struct pipe_inode_info *opipe = get_pipe_info(out, true); | |
aadd06e5 | 1764 | int ret = -EINVAL; |
70524490 | 1765 | |
90da2e3f PB |
1766 | if (unlikely(!(in->f_mode & FMODE_READ) || |
1767 | !(out->f_mode & FMODE_WRITE))) | |
1768 | return -EBADF; | |
1769 | ||
70524490 | 1770 | /* |
aadd06e5 JA |
1771 | * Duplicate the contents of ipipe to opipe without actually |
1772 | * copying the data. | |
70524490 | 1773 | */ |
aadd06e5 | 1774 | if (ipipe && opipe && ipipe != opipe) { |
ee5e0011 SK |
1775 | if ((in->f_flags | out->f_flags) & O_NONBLOCK) |
1776 | flags |= SPLICE_F_NONBLOCK; | |
1777 | ||
aadd06e5 JA |
1778 | /* |
1779 | * Keep going, unless we encounter an error. The ipipe/opipe | |
1780 | * ordering doesn't really matter. | |
1781 | */ | |
7c77f0b3 | 1782 | ret = ipipe_prep(ipipe, flags); |
aadd06e5 | 1783 | if (!ret) { |
7c77f0b3 | 1784 | ret = opipe_prep(opipe, flags); |
02cf01ae | 1785 | if (!ret) |
aadd06e5 | 1786 | ret = link_pipe(ipipe, opipe, len, flags); |
aadd06e5 JA |
1787 | } |
1788 | } | |
70524490 | 1789 | |
aadd06e5 | 1790 | return ret; |
70524490 JA |
1791 | } |
1792 | ||
836f92ad | 1793 | SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) |
70524490 | 1794 | { |
90da2e3f | 1795 | struct fd in, out; |
2903ff01 | 1796 | int error; |
70524490 | 1797 | |
3d6ea290 AV |
1798 | if (unlikely(flags & ~SPLICE_F_ALL)) |
1799 | return -EINVAL; | |
1800 | ||
70524490 JA |
1801 | if (unlikely(!len)) |
1802 | return 0; | |
1803 | ||
1804 | error = -EBADF; | |
2903ff01 AV |
1805 | in = fdget(fdin); |
1806 | if (in.file) { | |
90da2e3f PB |
1807 | out = fdget(fdout); |
1808 | if (out.file) { | |
1809 | error = do_tee(in.file, out.file, len, flags); | |
1810 | fdput(out); | |
70524490 | 1811 | } |
2903ff01 | 1812 | fdput(in); |
70524490 JA |
1813 | } |
1814 | ||
1815 | return error; | |
1816 | } |