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