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