Blame view
fs/splice.c
38.7 KB
457c89965 treewide: Add SPD... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
5274f052e [PATCH] Introduce... |
2 3 4 5 6 7 8 9 10 11 12 |
/* * "splice": joining two ropes together by interweaving their strands. * * This is the "extended pipe" functionality, where a pipe is used as * an arbitrary in-memory buffer. Think of a pipe as a small kernel * buffer that you can use to transfer data from one end to the other. * * The traditional unix read/write is extended with a "splice()" operation * that transfers data buffers to or from a pipe buffer. * * Named by Larry McVoy, original implementation from Linus, extended by |
c2058e061 [PATCH] splice: a... |
13 14 |
* Jens to support splicing to files, network, direct splicing, etc and * fixing lots of bugs. |
5274f052e [PATCH] Introduce... |
15 |
* |
0fe234795 [PATCH] Update ax... |
16 |
* Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk> |
c2058e061 [PATCH] splice: a... |
17 18 |
* Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org> * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu> |
5274f052e [PATCH] Introduce... |
19 20 |
* */ |
be297968d mm: only include ... |
21 |
#include <linux/bvec.h> |
5274f052e [PATCH] Introduce... |
22 23 24 |
#include <linux/fs.h> #include <linux/file.h> #include <linux/pagemap.h> |
d6b29d7ce splice: divorce t... |
25 |
#include <linux/splice.h> |
08e552c69 memcg: synchroniz... |
26 |
#include <linux/memcontrol.h> |
5274f052e [PATCH] Introduce... |
27 |
#include <linux/mm_inline.h> |
5abc97aa2 [PATCH] splice: a... |
28 |
#include <linux/swap.h> |
4f6f0bd2f [PATCH] splice: i... |
29 |
#include <linux/writeback.h> |
630d9c472 fs: reduce the us... |
30 |
#include <linux/export.h> |
4f6f0bd2f [PATCH] splice: i... |
31 |
#include <linux/syscalls.h> |
912d35f86 [PATCH] Add suppo... |
32 |
#include <linux/uio.h> |
29ce20586 security: revalid... |
33 |
#include <linux/security.h> |
5a0e3ad6a include cleanup: ... |
34 |
#include <linux/gfp.h> |
35f9c09fe tcp: tcp_sendpage... |
35 |
#include <linux/socket.h> |
174cd4b1e sched/headers: Pr... |
36 |
#include <linux/sched/signal.h> |
06ae43f34 Don't bother with... |
37 |
#include "internal.h" |
5274f052e [PATCH] Introduce... |
38 |
|
83f9135bd [PATCH] splice: a... |
39 40 41 42 43 44 |
/* * Attempt to steal a page from a pipe buffer. This should perhaps go into * a vm helper function, it's already simplified quite a bit by the * addition of remove_mapping(). If success is returned, the caller may * attempt to reuse this page for another destination. */ |
c928f642c fs: rename pipe_b... |
45 46 |
static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
5abc97aa2 [PATCH] splice: a... |
47 48 |
{ struct page *page = buf->page; |
9e94cd4fd [PATCH] splice: r... |
49 |
struct address_space *mapping; |
5abc97aa2 [PATCH] splice: a... |
50 |
|
9e0267c26 [PATCH] splice: f... |
51 |
lock_page(page); |
9e94cd4fd [PATCH] splice: r... |
52 53 54 |
mapping = page_mapping(page); if (mapping) { WARN_ON(!PageUptodate(page)); |
5abc97aa2 [PATCH] splice: a... |
55 |
|
9e94cd4fd [PATCH] splice: r... |
56 57 58 59 60 61 62 63 64 |
/* * At least for ext2 with nobh option, we need to wait on * writeback completing on this page, since we'll remove it * from the pagecache. Otherwise truncate wont wait on the * page, allowing the disk blocks to be reused by someone else * before we actually wrote our data to them. fs corruption * ensues. */ wait_on_page_writeback(page); |
ad8d6f0a7 [PATCH] splice: p... |
65 |
|
266cf658e FS-Cache: Recruit... |
66 67 |
if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) |
ca39d651d splice: handle tr... |
68 |
goto out_unlock; |
4f6f0bd2f [PATCH] splice: i... |
69 |
|
9e94cd4fd [PATCH] splice: r... |
70 71 72 73 74 75 |
/* * If we succeeded in removing the mapping, set LRU flag * and return good. */ if (remove_mapping(mapping, page)) { buf->flags |= PIPE_BUF_FLAG_LRU; |
c928f642c fs: rename pipe_b... |
76 |
return true; |
9e94cd4fd [PATCH] splice: r... |
77 |
} |
9e0267c26 [PATCH] splice: f... |
78 |
} |
5abc97aa2 [PATCH] splice: a... |
79 |
|
9e94cd4fd [PATCH] splice: r... |
80 81 82 83 |
/* * Raced with truncate or failed to remove page from current * address space, unlock and return failure. */ |
ca39d651d splice: handle tr... |
84 |
out_unlock: |
9e94cd4fd [PATCH] splice: r... |
85 |
unlock_page(page); |
c928f642c fs: rename pipe_b... |
86 |
return false; |
5abc97aa2 [PATCH] splice: a... |
87 |
} |
76ad4d111 [PATCH] splice: r... |
88 |
static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, |
5274f052e [PATCH] Introduce... |
89 90 |
struct pipe_buffer *buf) { |
09cbfeaf1 mm, fs: get rid o... |
91 |
put_page(buf->page); |
1432873af [PATCH] splice: L... |
92 |
buf->flags &= ~PIPE_BUF_FLAG_LRU; |
5274f052e [PATCH] Introduce... |
93 |
} |
0845718da pipe: add documen... |
94 95 96 97 |
/* * Check whether the contents of buf is OK to access. Since the content * is a page cache page, IO may be in flight. */ |
cac36bb06 pipe: change the ... |
98 99 |
static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
5274f052e [PATCH] Introduce... |
100 101 |
{ struct page *page = buf->page; |
49d0b21be [PATCH] splice: o... |
102 |
int err; |
5274f052e [PATCH] Introduce... |
103 104 |
if (!PageUptodate(page)) { |
49d0b21be [PATCH] splice: o... |
105 106 107 108 |
lock_page(page); /* * Page got truncated/unhashed. This will cause a 0-byte |
73d62d83e [PATCH] splice: c... |
109 |
* splice, if this is the first page. |
49d0b21be [PATCH] splice: o... |
110 111 112 113 114 |
*/ if (!page->mapping) { err = -ENODATA; goto error; } |
5274f052e [PATCH] Introduce... |
115 |
|
49d0b21be [PATCH] splice: o... |
116 |
/* |
73d62d83e [PATCH] splice: c... |
117 |
* Uh oh, read-error from disk. |
49d0b21be [PATCH] splice: o... |
118 119 120 121 122 123 124 |
*/ if (!PageUptodate(page)) { err = -EIO; goto error; } /* |
f84d75199 [PATCH] pipe: int... |
125 |
* Page is ok afterall, we are done. |
49d0b21be [PATCH] splice: o... |
126 |
*/ |
5274f052e [PATCH] Introduce... |
127 |
unlock_page(page); |
5274f052e [PATCH] Introduce... |
128 |
} |
f84d75199 [PATCH] pipe: int... |
129 |
return 0; |
49d0b21be [PATCH] splice: o... |
130 131 |
error: unlock_page(page); |
f84d75199 [PATCH] pipe: int... |
132 |
return err; |
70524490e [PATCH] splice: a... |
133 |
} |
708e3508c tmpfs: clone shme... |
134 |
const struct pipe_buf_operations page_cache_pipe_buf_ops = { |
c928f642c fs: rename pipe_b... |
135 136 137 138 |
.confirm = page_cache_pipe_buf_confirm, .release = page_cache_pipe_buf_release, .try_steal = page_cache_pipe_buf_try_steal, .get = generic_pipe_buf_get, |
5274f052e [PATCH] Introduce... |
139 |
}; |
c928f642c fs: rename pipe_b... |
140 141 |
static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) |
912d35f86 [PATCH] Add suppo... |
142 |
{ |
7afa6fd03 [PATCH] vmsplice:... |
143 |
if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) |
c928f642c fs: rename pipe_b... |
144 |
return false; |
7afa6fd03 [PATCH] vmsplice:... |
145 |
|
1432873af [PATCH] splice: L... |
146 |
buf->flags |= PIPE_BUF_FLAG_LRU; |
c928f642c fs: rename pipe_b... |
147 |
return generic_pipe_buf_try_steal(pipe, buf); |
912d35f86 [PATCH] Add suppo... |
148 |
} |
d4c3cca94 [PATCH] constify ... |
149 |
static const struct pipe_buf_operations user_page_pipe_buf_ops = { |
c928f642c fs: rename pipe_b... |
150 151 152 |
.release = page_cache_pipe_buf_release, .try_steal = user_page_pipe_buf_try_steal, .get = generic_pipe_buf_get, |
912d35f86 [PATCH] Add suppo... |
153 |
}; |
825cdcb1a splice: add wakeu... |
154 155 156 |
static void wakeup_pipe_readers(struct pipe_inode_info *pipe) { smp_mb(); |
0ddad21d3 pipe: use exclusi... |
157 158 |
if (waitqueue_active(&pipe->rd_wait)) wake_up_interruptible(&pipe->rd_wait); |
825cdcb1a splice: add wakeu... |
159 160 |
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); } |
932cc6d4f splice: completel... |
161 162 163 164 165 166 |
/** * splice_to_pipe - fill passed data into a pipe * @pipe: pipe to fill * @spd: data to fill * * Description: |
79685b8de docbook: add pipe... |
167 |
* @spd contains a map of pages and len/offset tuples, along with |
932cc6d4f splice: completel... |
168 169 170 |
* the struct pipe_buf_operations associated with these pages. This * function will link that data to the pipe. * |
83f9135bd [PATCH] splice: a... |
171 |
*/ |
d6b29d7ce splice: divorce t... |
172 173 |
ssize_t splice_to_pipe(struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) |
5274f052e [PATCH] Introduce... |
174 |
{ |
00de00bda splice: fix leak ... |
175 |
unsigned int spd_pages = spd->nr_pages; |
8cefc107c pipe: Use head an... |
176 177 178 |
unsigned int tail = pipe->tail; unsigned int head = pipe->head; unsigned int mask = pipe->ring_size - 1; |
8924feff6 splice: lift pipe... |
179 |
int ret = 0, page_nr = 0; |
5274f052e [PATCH] Introduce... |
180 |
|
d6785d915 splice: handle ze... |
181 182 |
if (!spd_pages) return 0; |
8924feff6 splice: lift pipe... |
183 184 185 186 187 |
if (unlikely(!pipe->readers)) { send_sig(SIGPIPE, current, 0); ret = -EPIPE; goto out; } |
5274f052e [PATCH] Introduce... |
188 |
|
6718b6f85 pipe: Allow pipes... |
189 |
while (!pipe_full(head, tail, pipe->max_usage)) { |
8cefc107c pipe: Use head an... |
190 |
struct pipe_buffer *buf = &pipe->bufs[head & mask]; |
5274f052e [PATCH] Introduce... |
191 |
|
8924feff6 splice: lift pipe... |
192 193 194 195 196 |
buf->page = spd->pages[page_nr]; buf->offset = spd->partial[page_nr].offset; buf->len = spd->partial[page_nr].len; buf->private = spd->partial[page_nr].private; buf->ops = spd->ops; |
5a81e6a17 vfs: fix uninitia... |
197 |
buf->flags = 0; |
5274f052e [PATCH] Introduce... |
198 |
|
8cefc107c pipe: Use head an... |
199 200 |
head++; pipe->head = head; |
8924feff6 splice: lift pipe... |
201 202 |
page_nr++; ret += buf->len; |
29e350944 splice: add SPLIC... |
203 |
|
8924feff6 splice: lift pipe... |
204 |
if (!--spd->nr_pages) |
5274f052e [PATCH] Introduce... |
205 |
break; |
5274f052e [PATCH] Introduce... |
206 |
} |
8924feff6 splice: lift pipe... |
207 208 |
if (!ret) ret = -EAGAIN; |
5274f052e [PATCH] Introduce... |
209 |
|
8924feff6 splice: lift pipe... |
210 |
out: |
00de00bda splice: fix leak ... |
211 |
while (page_nr < spd_pages) |
bbdfc2f70 [SPLICE]: Don't a... |
212 |
spd->spd_release(spd, page_nr++); |
5274f052e [PATCH] Introduce... |
213 214 215 |
return ret; } |
2b514574f net: af_unix: imp... |
216 |
EXPORT_SYMBOL_GPL(splice_to_pipe); |
5274f052e [PATCH] Introduce... |
217 |
|
79fddc4ef new helper: add_t... |
218 219 |
ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { |
8cefc107c pipe: Use head an... |
220 221 222 |
unsigned int head = pipe->head; unsigned int tail = pipe->tail; unsigned int mask = pipe->ring_size - 1; |
79fddc4ef new helper: add_t... |
223 224 225 226 227 |
int ret; if (unlikely(!pipe->readers)) { send_sig(SIGPIPE, current, 0); ret = -EPIPE; |
6718b6f85 pipe: Allow pipes... |
228 |
} else if (pipe_full(head, tail, pipe->max_usage)) { |
79fddc4ef new helper: add_t... |
229 230 |
ret = -EAGAIN; } else { |
8cefc107c pipe: Use head an... |
231 232 |
pipe->bufs[head & mask] = *buf; pipe->head = head + 1; |
79fddc4ef new helper: add_t... |
233 234 |
return buf->len; } |
a779638cf pipe: add pipe_bu... |
235 |
pipe_buf_release(pipe, buf); |
79fddc4ef new helper: add_t... |
236 237 238 |
return ret; } EXPORT_SYMBOL(add_to_pipe); |
35f3d14db pipe: add support... |
239 240 241 242 |
/* * Check if we need to grow the arrays holding pages and partial page * descriptions. */ |
047fe3605 splice: fix racy ... |
243 |
int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) |
35f3d14db pipe: add support... |
244 |
{ |
6718b6f85 pipe: Allow pipes... |
245 |
unsigned int max_usage = READ_ONCE(pipe->max_usage); |
047fe3605 splice: fix racy ... |
246 |
|
8cefc107c pipe: Use head an... |
247 248 |
spd->nr_pages_max = max_usage; if (max_usage <= PIPE_DEF_BUFFERS) |
35f3d14db pipe: add support... |
249 |
return 0; |
8cefc107c pipe: Use head an... |
250 251 |
spd->pages = kmalloc_array(max_usage, sizeof(struct page *), GFP_KERNEL); spd->partial = kmalloc_array(max_usage, sizeof(struct partial_page), |
6da2ec560 treewide: kmalloc... |
252 |
GFP_KERNEL); |
35f3d14db pipe: add support... |
253 254 255 256 257 258 259 260 |
if (spd->pages && spd->partial) return 0; kfree(spd->pages); kfree(spd->partial); return -ENOMEM; } |
047fe3605 splice: fix racy ... |
261 |
void splice_shrink_spd(struct splice_pipe_desc *spd) |
35f3d14db pipe: add support... |
262 |
{ |
047fe3605 splice: fix racy ... |
263 |
if (spd->nr_pages_max <= PIPE_DEF_BUFFERS) |
35f3d14db pipe: add support... |
264 265 266 267 268 |
return; kfree(spd->pages); kfree(spd->partial); } |
83f9135bd [PATCH] splice: a... |
269 270 271 |
/** * generic_file_splice_read - splice data from file to a pipe * @in: file to splice from |
932cc6d4f splice: completel... |
272 |
* @ppos: position in @in |
83f9135bd [PATCH] splice: a... |
273 274 275 276 |
* @pipe: pipe to splice to * @len: number of bytes to splice * @flags: splice modifier flags * |
932cc6d4f splice: completel... |
277 278 |
* Description: * Will read pages from given file and fill them into a pipe. Can be |
82c156f85 switch generic_fi... |
279 |
* used as long as it has more or less sane ->read_iter(). |
932cc6d4f splice: completel... |
280 |
* |
83f9135bd [PATCH] splice: a... |
281 |
*/ |
cbb7e577e [PATCH] splice: p... |
282 283 284 |
ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) |
5274f052e [PATCH] Introduce... |
285 |
{ |
82c156f85 switch generic_fi... |
286 287 |
struct iov_iter to; struct kiocb kiocb; |
8cefc107c pipe: Use head an... |
288 289 |
unsigned int i_head; int ret; |
be64f884b dax: unify ext2/4... |
290 |
|
aa563d7bc iov_iter: Separat... |
291 |
iov_iter_pipe(&to, READ, pipe, len); |
8cefc107c pipe: Use head an... |
292 |
i_head = to.head; |
82c156f85 switch generic_fi... |
293 294 |
init_sync_kiocb(&kiocb, in); kiocb.ki_pos = *ppos; |
bb7462b6f vfs: use helpers ... |
295 |
ret = call_read_iter(in, &kiocb, &to); |
723590ed5 splice: update mt... |
296 |
if (ret > 0) { |
82c156f85 switch generic_fi... |
297 |
*ppos = kiocb.ki_pos; |
723590ed5 splice: update mt... |
298 |
file_accessed(in); |
82c156f85 switch generic_fi... |
299 |
} else if (ret < 0) { |
8cefc107c pipe: Use head an... |
300 |
to.head = i_head; |
c3a690240 fix ITER_PIPE int... |
301 302 |
to.iov_offset = 0; iov_iter_advance(&to, 0); /* to free what was emitted */ |
82c156f85 switch generic_fi... |
303 304 305 306 307 308 |
/* * callers of ->splice_read() expect -EAGAIN on * "can't put anything in there", rather than -EFAULT. */ if (ret == -EFAULT) ret = -EAGAIN; |
723590ed5 splice: update mt... |
309 |
} |
5274f052e [PATCH] Introduce... |
310 311 312 |
return ret; } |
059a8f373 [PATCH] splice: e... |
313 |
EXPORT_SYMBOL(generic_file_splice_read); |
241699cd7 new iov_iter flav... |
314 |
const struct pipe_buf_operations default_pipe_buf_ops = { |
c928f642c fs: rename pipe_b... |
315 316 317 |
.release = generic_pipe_buf_release, .try_steal = generic_pipe_buf_try_steal, .get = generic_pipe_buf_get, |
6818173bd splice: implement... |
318 |
}; |
28a625cbc fuse: fix pipe_bu... |
319 320 |
/* Pipe buffer operations for a socket and similar. */ const struct pipe_buf_operations nosteal_pipe_buf_ops = { |
c928f642c fs: rename pipe_b... |
321 322 |
.release = generic_pipe_buf_release, .get = generic_pipe_buf_get, |
28a625cbc fuse: fix pipe_bu... |
323 324 |
}; EXPORT_SYMBOL(nosteal_pipe_buf_ops); |
5274f052e [PATCH] Introduce... |
325 |
/* |
4f6f0bd2f [PATCH] splice: i... |
326 |
* Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' |
016b661e2 [PATCH] splice: f... |
327 |
* using sendpage(). Return the number of bytes sent. |
5274f052e [PATCH] Introduce... |
328 |
*/ |
76ad4d111 [PATCH] splice: r... |
329 |
static int pipe_to_sendpage(struct pipe_inode_info *pipe, |
5274f052e [PATCH] Introduce... |
330 331 |
struct pipe_buffer *buf, struct splice_desc *sd) { |
6a14b90bb vmsplice: add vms... |
332 |
struct file *file = sd->u.file; |
5274f052e [PATCH] Introduce... |
333 |
loff_t pos = sd->pos; |
a8adbe378 fs/splice: Pull b... |
334 |
int more; |
5274f052e [PATCH] Introduce... |
335 |
|
72c2d5319 file->f_op is nev... |
336 |
if (!likely(file->f_op->sendpage)) |
a8adbe378 fs/splice: Pull b... |
337 |
return -EINVAL; |
35f9c09fe tcp: tcp_sendpage... |
338 |
more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0; |
ae62ca7b0 tcp: fix MSG_SEND... |
339 |
|
8cefc107c pipe: Use head an... |
340 341 |
if (sd->len < sd->total_len && pipe_occupancy(pipe->head, pipe->tail) > 1) |
35f9c09fe tcp: tcp_sendpage... |
342 |
more |= MSG_SENDPAGE_NOTLAST; |
ae62ca7b0 tcp: fix MSG_SEND... |
343 |
|
a8adbe378 fs/splice: Pull b... |
344 345 |
return file->f_op->sendpage(file, buf->page, buf->offset, sd->len, &pos, more); |
5274f052e [PATCH] Introduce... |
346 |
} |
b3c2d2ddd splice: split up ... |
347 348 349 |
static void wakeup_pipe_writers(struct pipe_inode_info *pipe) { smp_mb(); |
0ddad21d3 pipe: use exclusi... |
350 351 |
if (waitqueue_active(&pipe->wr_wait)) wake_up_interruptible(&pipe->wr_wait); |
b3c2d2ddd splice: split up ... |
352 353 |
kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); } |
932cc6d4f splice: completel... |
354 |
/** |
b3c2d2ddd splice: split up ... |
355 |
* splice_from_pipe_feed - feed available data from a pipe to a file |
932cc6d4f splice: completel... |
356 357 358 359 360 |
* @pipe: pipe to splice from * @sd: information to @actor * @actor: handler that splices the data * * Description: |
b3c2d2ddd splice: split up ... |
361 362 363 364 365 366 367 |
* This function loops over the pipe and calls @actor to do the * actual moving of a single struct pipe_buffer to the desired * destination. It returns when there's no more buffers left in * the pipe or if the requested number of bytes (@sd->total_len) * have been copied. It returns a positive number (one) if the * pipe needs to be filled with more data, zero if the required * number of bytes have been copied and -errno on error. |
932cc6d4f splice: completel... |
368 |
* |
b3c2d2ddd splice: split up ... |
369 370 371 372 |
* This, together with splice_from_pipe_{begin,end,next}, may be * used to implement the functionality of __splice_from_pipe() when * locking is required around copying the pipe buffers to the * destination. |
83f9135bd [PATCH] splice: a... |
373 |
*/ |
96f9bc8fb fs/splice.c: remo... |
374 |
static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, |
b3c2d2ddd splice: split up ... |
375 |
splice_actor *actor) |
5274f052e [PATCH] Introduce... |
376 |
{ |
8cefc107c pipe: Use head an... |
377 378 379 |
unsigned int head = pipe->head; unsigned int tail = pipe->tail; unsigned int mask = pipe->ring_size - 1; |
b3c2d2ddd splice: split up ... |
380 |
int ret; |
5274f052e [PATCH] Introduce... |
381 |
|
ec057595c pipe: fix incorre... |
382 |
while (!pipe_empty(head, tail)) { |
8cefc107c pipe: Use head an... |
383 |
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; |
5274f052e [PATCH] Introduce... |
384 |
|
b3c2d2ddd splice: split up ... |
385 386 387 |
sd->len = buf->len; if (sd->len > sd->total_len) sd->len = sd->total_len; |
5274f052e [PATCH] Introduce... |
388 |
|
fba597db4 pipe: add pipe_bu... |
389 |
ret = pipe_buf_confirm(pipe, buf); |
a8adbe378 fs/splice: Pull b... |
390 |
if (unlikely(ret)) { |
b3c2d2ddd splice: split up ... |
391 392 393 394 |
if (ret == -ENODATA) ret = 0; return ret; } |
a8adbe378 fs/splice: Pull b... |
395 396 397 398 |
ret = actor(pipe, buf, sd); if (ret <= 0) return ret; |
b3c2d2ddd splice: split up ... |
399 400 401 402 403 404 405 406 407 |
buf->offset += ret; buf->len -= ret; sd->num_spliced += ret; sd->len -= ret; sd->pos += ret; sd->total_len -= ret; if (!buf->len) { |
a779638cf pipe: add pipe_bu... |
408 |
pipe_buf_release(pipe, buf); |
8cefc107c pipe: Use head an... |
409 410 |
tail++; pipe->tail = tail; |
6447a3cf1 get rid of pipe->... |
411 |
if (pipe->files) |
b3c2d2ddd splice: split up ... |
412 413 |
sd->need_wakeup = true; } |
5274f052e [PATCH] Introduce... |
414 |
|
b3c2d2ddd splice: split up ... |
415 416 417 |
if (!sd->total_len) return 0; } |
5274f052e [PATCH] Introduce... |
418 |
|
b3c2d2ddd splice: split up ... |
419 420 |
return 1; } |
5274f052e [PATCH] Introduce... |
421 |
|
d1a819a2e splice: teach spl... |
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
/* We know we have a pipe buffer, but maybe it's empty? */ static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) { unsigned int tail = pipe->tail; unsigned int mask = pipe->ring_size - 1; struct pipe_buffer *buf = &pipe->bufs[tail & mask]; if (unlikely(!buf->len)) { pipe_buf_release(pipe, buf); pipe->tail = tail+1; return true; } return false; } |
b3c2d2ddd splice: split up ... |
437 438 439 440 441 442 443 444 445 446 |
/** * splice_from_pipe_next - wait for some data to splice from * @pipe: pipe to splice from * @sd: information about the splice operation * * Description: * This function will wait for some data and return a positive * value (one) if pipe buffers are available. It will return zero * or -errno if no more data needs to be spliced. */ |
96f9bc8fb fs/splice.c: remo... |
447 |
static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) |
b3c2d2ddd splice: split up ... |
448 |
{ |
c725bfce7 vfs: Make sendfil... |
449 450 451 452 453 454 |
/* * Check for signal early to make process killable when there are * always buffers available */ if (signal_pending(current)) return -ERESTARTSYS; |
d1a819a2e splice: teach spl... |
455 |
repeat: |
8cefc107c pipe: Use head an... |
456 |
while (pipe_empty(pipe->head, pipe->tail)) { |
b3c2d2ddd splice: split up ... |
457 458 |
if (!pipe->writers) return 0; |
016b661e2 [PATCH] splice: f... |
459 |
|
a28c8b9db pipe: remove 'wai... |
460 |
if (sd->num_spliced) |
b3c2d2ddd splice: split up ... |
461 |
return 0; |
73d62d83e [PATCH] splice: c... |
462 |
|
b3c2d2ddd splice: split up ... |
463 464 |
if (sd->flags & SPLICE_F_NONBLOCK) return -EAGAIN; |
5274f052e [PATCH] Introduce... |
465 |
|
b3c2d2ddd splice: split up ... |
466 467 |
if (signal_pending(current)) return -ERESTARTSYS; |
5274f052e [PATCH] Introduce... |
468 |
|
b3c2d2ddd splice: split up ... |
469 470 471 |
if (sd->need_wakeup) { wakeup_pipe_writers(pipe); sd->need_wakeup = false; |
5274f052e [PATCH] Introduce... |
472 |
} |
472e5b056 pipe: remove pipe... |
473 |
pipe_wait_readable(pipe); |
b3c2d2ddd splice: split up ... |
474 |
} |
29e350944 splice: add SPLIC... |
475 |
|
d1a819a2e splice: teach spl... |
476 477 |
if (eat_empty_buffer(pipe)) goto repeat; |
b3c2d2ddd splice: split up ... |
478 479 |
return 1; } |
5274f052e [PATCH] Introduce... |
480 |
|
b3c2d2ddd splice: split up ... |
481 482 |
/** * splice_from_pipe_begin - start splicing from pipe |
b80901bbf splice: fix new k... |
483 |
* @sd: information about the splice operation |
b3c2d2ddd splice: split up ... |
484 485 486 487 488 489 |
* * Description: * This function should be called before a loop containing * splice_from_pipe_next() and splice_from_pipe_feed() to * initialize the necessary fields of @sd. */ |
96f9bc8fb fs/splice.c: remo... |
490 |
static void splice_from_pipe_begin(struct splice_desc *sd) |
b3c2d2ddd splice: split up ... |
491 492 493 494 |
{ sd->num_spliced = 0; sd->need_wakeup = false; } |
5274f052e [PATCH] Introduce... |
495 |
|
b3c2d2ddd splice: split up ... |
496 497 498 499 500 501 502 503 504 505 |
/** * splice_from_pipe_end - finish splicing from pipe * @pipe: pipe to splice from * @sd: information about the splice operation * * Description: * This function will wake up pipe writers if necessary. It should * be called after a loop containing splice_from_pipe_next() and * splice_from_pipe_feed(). */ |
96f9bc8fb fs/splice.c: remo... |
506 |
static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) |
b3c2d2ddd splice: split up ... |
507 508 509 510 |
{ if (sd->need_wakeup) wakeup_pipe_writers(pipe); } |
5274f052e [PATCH] Introduce... |
511 |
|
b3c2d2ddd splice: split up ... |
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
/** * __splice_from_pipe - splice data from a pipe to given actor * @pipe: pipe to splice from * @sd: information to @actor * @actor: handler that splices the data * * Description: * This function does little more than loop over the pipe and call * @actor to do the actual moving of a single struct pipe_buffer to * the desired destination. See pipe_to_file, pipe_to_sendpage, or * pipe_to_user. * */ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { int ret; |
5274f052e [PATCH] Introduce... |
529 |
|
b3c2d2ddd splice: split up ... |
530 531 |
splice_from_pipe_begin(sd); do { |
c2489e07c vfs: Avoid softlo... |
532 |
cond_resched(); |
b3c2d2ddd splice: split up ... |
533 534 535 536 537 538 539 |
ret = splice_from_pipe_next(pipe, sd); if (ret > 0) ret = splice_from_pipe_feed(pipe, sd, actor); } while (ret > 0); splice_from_pipe_end(pipe, sd); return sd->num_spliced ? sd->num_spliced : ret; |
5274f052e [PATCH] Introduce... |
540 |
} |
40bee44ea Export __splice_f... |
541 |
EXPORT_SYMBOL(__splice_from_pipe); |
5274f052e [PATCH] Introduce... |
542 |
|
932cc6d4f splice: completel... |
543 544 545 546 547 548 549 550 551 552 |
/** * splice_from_pipe - splice data from a pipe to a file * @pipe: pipe to splice from * @out: file to splice to * @ppos: position in @out * @len: how many bytes to splice * @flags: splice modifier flags * @actor: handler that splices the data * * Description: |
2933970b9 splice: remove i_... |
553 |
* See __splice_from_pipe. This function locks the pipe inode, |
932cc6d4f splice: completel... |
554 555 556 |
* otherwise it's identical to __splice_from_pipe(). * */ |
6da618098 [PATCH] Introduce... |
557 558 559 560 561 |
ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags, splice_actor *actor) { ssize_t ret; |
c66ab6fa7 splice: abstract ... |
562 563 564 565 |
struct splice_desc sd = { .total_len = len, .flags = flags, .pos = *ppos, |
6a14b90bb vmsplice: add vms... |
566 |
.u.file = out, |
c66ab6fa7 splice: abstract ... |
567 |
}; |
6da618098 [PATCH] Introduce... |
568 |
|
61e0d47c3 splice: add helpe... |
569 |
pipe_lock(pipe); |
c66ab6fa7 splice: abstract ... |
570 |
ret = __splice_from_pipe(pipe, &sd, actor); |
61e0d47c3 splice: add helpe... |
571 |
pipe_unlock(pipe); |
6da618098 [PATCH] Introduce... |
572 573 574 575 576 |
return ret; } /** |
8d0207652 ->splice_write() ... |
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
* iter_file_splice_write - splice data from a pipe to a file * @pipe: pipe info * @out: file to write to * @ppos: position in @out * @len: number of bytes to splice * @flags: splice modifier flags * * Description: * Will either move or copy pages (determined by @flags options) from * the given pipe inode to the given file. * This one is ->write_iter-based. * */ ssize_t iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { struct splice_desc sd = { .total_len = len, .flags = flags, .pos = *ppos, .u.file = out, }; |
6718b6f85 pipe: Allow pipes... |
600 |
int nbufs = pipe->max_usage; |
8d0207652 ->splice_write() ... |
601 602 603 604 605 606 607 608 609 610 611 612 |
struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec), GFP_KERNEL); ssize_t ret; if (unlikely(!array)) return -ENOMEM; pipe_lock(pipe); splice_from_pipe_begin(&sd); while (sd.total_len) { struct iov_iter from; |
ec057595c pipe: fix incorre... |
613 |
unsigned int head, tail, mask; |
8d0207652 ->splice_write() ... |
614 |
size_t left; |
8cefc107c pipe: Use head an... |
615 |
int n; |
8d0207652 ->splice_write() ... |
616 617 618 619 |
ret = splice_from_pipe_next(pipe, &sd); if (ret <= 0) break; |
6718b6f85 pipe: Allow pipes... |
620 |
if (unlikely(nbufs < pipe->max_usage)) { |
8d0207652 ->splice_write() ... |
621 |
kfree(array); |
6718b6f85 pipe: Allow pipes... |
622 |
nbufs = pipe->max_usage; |
8d0207652 ->splice_write() ... |
623 624 625 626 627 628 629 |
array = kcalloc(nbufs, sizeof(struct bio_vec), GFP_KERNEL); if (!array) { ret = -ENOMEM; break; } } |
ec057595c pipe: fix incorre... |
630 631 632 |
head = pipe->head; tail = pipe->tail; mask = pipe->ring_size - 1; |
8d0207652 ->splice_write() ... |
633 634 |
/* build the vector */ left = sd.total_len; |
8cefc107c pipe: Use head an... |
635 636 |
for (n = 0; !pipe_empty(head, tail) && left && n < nbufs; tail++, n++) { struct pipe_buffer *buf = &pipe->bufs[tail & mask]; |
8d0207652 ->splice_write() ... |
637 638 639 640 |
size_t this_len = buf->len; if (this_len > left) this_len = left; |
fba597db4 pipe: add pipe_bu... |
641 |
ret = pipe_buf_confirm(pipe, buf); |
8d0207652 ->splice_write() ... |
642 643 644 645 646 647 648 649 650 651 652 |
if (unlikely(ret)) { if (ret == -ENODATA) ret = 0; goto done; } array[n].bv_page = buf->page; array[n].bv_len = this_len; array[n].bv_offset = buf->offset; left -= this_len; } |
aa563d7bc iov_iter: Separat... |
653 |
iov_iter_bvec(&from, WRITE, array, n, sd.total_len - left); |
abbb65899 fs: implement vfs... |
654 |
ret = vfs_iter_write(out, &from, &sd.pos, 0); |
8d0207652 ->splice_write() ... |
655 656 657 658 659 |
if (ret <= 0) break; sd.num_spliced += ret; sd.total_len -= ret; |
dbe4e192a fs: add vfs_iter_... |
660 |
*ppos = sd.pos; |
8d0207652 ->splice_write() ... |
661 662 |
/* dismiss the fully eaten buffers, adjust the partial one */ |
8cefc107c pipe: Use head an... |
663 |
tail = pipe->tail; |
8d0207652 ->splice_write() ... |
664 |
while (ret) { |
8cefc107c pipe: Use head an... |
665 |
struct pipe_buffer *buf = &pipe->bufs[tail & mask]; |
8d0207652 ->splice_write() ... |
666 |
if (ret >= buf->len) { |
8d0207652 ->splice_write() ... |
667 668 |
ret -= buf->len; buf->len = 0; |
a779638cf pipe: add pipe_bu... |
669 |
pipe_buf_release(pipe, buf); |
8cefc107c pipe: Use head an... |
670 671 |
tail++; pipe->tail = tail; |
8d0207652 ->splice_write() ... |
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
if (pipe->files) sd.need_wakeup = true; } else { buf->offset += ret; buf->len -= ret; ret = 0; } } } done: kfree(array); splice_from_pipe_end(pipe, &sd); pipe_unlock(pipe); if (sd.num_spliced) ret = sd.num_spliced; return ret; } EXPORT_SYMBOL(iter_file_splice_write); |
83f9135bd [PATCH] splice: a... |
694 695 |
/** * generic_splice_sendpage - splice data from a pipe to a socket |
932cc6d4f splice: completel... |
696 |
* @pipe: pipe to splice from |
83f9135bd [PATCH] splice: a... |
697 |
* @out: socket to write to |
932cc6d4f splice: completel... |
698 |
* @ppos: position in @out |
83f9135bd [PATCH] splice: a... |
699 700 701 |
* @len: number of bytes to splice * @flags: splice modifier flags * |
932cc6d4f splice: completel... |
702 703 704 |
* Description: * Will send @len bytes from the pipe to a network socket. No data copying * is involved. |
83f9135bd [PATCH] splice: a... |
705 706 |
* */ |
3a326a2ce [PATCH] introduce... |
707 |
ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, |
cbb7e577e [PATCH] splice: p... |
708 |
loff_t *ppos, size_t len, unsigned int flags) |
5274f052e [PATCH] Introduce... |
709 |
{ |
00522fb41 [PATCH] splice: r... |
710 |
return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); |
5274f052e [PATCH] Introduce... |
711 |
} |
059a8f373 [PATCH] splice: e... |
712 |
EXPORT_SYMBOL(generic_splice_sendpage); |
a0f067802 [PATCH] splice ex... |
713 |
|
fb5c9b8aa Revert "Revert "f... |
714 715 716 717 718 719 720 721 |
static int warn_unsupported(struct file *file, const char *op) { pr_debug_ratelimited( "splice %s not supported for file %pD4 (pid: %d comm: %.20s) ", op, file, current->pid, current->comm); return -EINVAL; } |
83f9135bd [PATCH] splice: a... |
722 723 724 |
/* * Attempt to initiate a splice from pipe to file. */ |
3a326a2ce [PATCH] introduce... |
725 |
static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, |
cbb7e577e [PATCH] splice: p... |
726 |
loff_t *ppos, size_t len, unsigned int flags) |
5274f052e [PATCH] Introduce... |
727 |
{ |
fb5c9b8aa Revert "Revert "f... |
728 729 730 |
if (unlikely(!out->f_op->splice_write)) return warn_unsupported(out, "write"); return out->f_op->splice_write(pipe, out, ppos, len, flags); |
5274f052e [PATCH] Introduce... |
731 |
} |
83f9135bd [PATCH] splice: a... |
732 733 734 |
/* * Attempt to initiate a splice from a file to a pipe. */ |
cbb7e577e [PATCH] splice: p... |
735 736 737 |
static long do_splice_to(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) |
5274f052e [PATCH] Introduce... |
738 |
{ |
5274f052e [PATCH] Introduce... |
739 |
int ret; |
49570e9b2 [PATCH] splice: u... |
740 |
if (unlikely(!(in->f_mode & FMODE_READ))) |
5274f052e [PATCH] Introduce... |
741 |
return -EBADF; |
cbb7e577e [PATCH] splice: p... |
742 |
ret = rw_verify_area(READ, in, ppos, len); |
5274f052e [PATCH] Introduce... |
743 744 |
if (unlikely(ret < 0)) return ret; |
03cc0789a do_splice_to(): c... |
745 746 |
if (unlikely(len > MAX_RW_COUNT)) len = MAX_RW_COUNT; |
fb5c9b8aa Revert "Revert "f... |
747 748 749 |
if (unlikely(!in->f_op->splice_read)) return warn_unsupported(in, "read"); return in->f_op->splice_read(in, ppos, pipe, len, flags); |
5274f052e [PATCH] Introduce... |
750 |
} |
932cc6d4f splice: completel... |
751 752 753 754 755 756 757 758 759 |
/** * splice_direct_to_actor - splices data directly between two non-pipes * @in: file to splice from * @sd: actor information on where to splice to * @actor: handles the data splicing * * Description: * This is a special case helper to splice directly between two * points, without requiring an explicit pipe. Internally an allocated |
79685b8de docbook: add pipe... |
760 |
* pipe is cached in the process, and reused during the lifetime of |
932cc6d4f splice: completel... |
761 762 |
* that process. * |
c66ab6fa7 splice: abstract ... |
763 764 765 |
*/ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, splice_direct_actor *actor) |
b92ce5589 [PATCH] splice: a... |
766 767 768 769 |
{ struct pipe_inode_info *pipe; long ret, bytes; umode_t i_mode; |
c66ab6fa7 splice: abstract ... |
770 |
size_t len; |
0ff28d9f4 splice: sendfile(... |
771 |
int i, flags, more; |
b92ce5589 [PATCH] splice: a... |
772 773 774 775 776 777 |
/* * We require the input being a regular file, as we don't want to * randomly drop data for eg socket -> socket splicing. Use the * piped splicing for that! */ |
496ad9aa8 new helper: file_... |
778 |
i_mode = file_inode(in)->i_mode; |
b92ce5589 [PATCH] splice: a... |
779 780 781 782 783 784 785 786 |
if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode))) return -EINVAL; /* * neither in nor out is a pipe, setup an internal pipe attached to * 'out' and transfer the wanted data from 'in' to 'out' through that */ pipe = current->splice_pipe; |
49570e9b2 [PATCH] splice: u... |
787 |
if (unlikely(!pipe)) { |
7bee130e2 get rid of alloc_... |
788 |
pipe = alloc_pipe_info(); |
b92ce5589 [PATCH] splice: a... |
789 790 791 792 793 |
if (!pipe) return -ENOMEM; /* * We don't have an immediate reader, but we'll read the stuff |
00522fb41 [PATCH] splice: r... |
794 |
* out of the pipe right after the splice_to_pipe(). So set |
b92ce5589 [PATCH] splice: a... |
795 796 797 798 799 800 801 802 |
* PIPE_READERS appropriately. */ pipe->readers = 1; current->splice_pipe = pipe; } /* |
73d62d83e [PATCH] splice: c... |
803 |
* Do the splice. |
b92ce5589 [PATCH] splice: a... |
804 805 806 |
*/ ret = 0; bytes = 0; |
c66ab6fa7 splice: abstract ... |
807 808 809 810 811 812 813 |
len = sd->total_len; flags = sd->flags; /* * Don't block on output, we have to drain the direct pipe. */ sd->flags &= ~SPLICE_F_NONBLOCK; |
0ff28d9f4 splice: sendfile(... |
814 |
more = sd->flags & SPLICE_F_MORE; |
b92ce5589 [PATCH] splice: a... |
815 |
|
8cefc107c pipe: Use head an... |
816 |
WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); |
176144455 splice: don't rea... |
817 |
|
b92ce5589 [PATCH] splice: a... |
818 |
while (len) { |
8cefc107c pipe: Use head an... |
819 |
unsigned int p_space; |
51a92c0f6 splice: fix offse... |
820 |
size_t read_len; |
a82c53a0e splice: fix sendf... |
821 |
loff_t pos = sd->pos, prev_pos = pos; |
b92ce5589 [PATCH] splice: a... |
822 |
|
176144455 splice: don't rea... |
823 |
/* Don't try to read more the pipe has space for. */ |
6718b6f85 pipe: Allow pipes... |
824 |
p_space = pipe->max_usage - |
8cefc107c pipe: Use head an... |
825 826 |
pipe_occupancy(pipe->head, pipe->tail); read_len = min_t(size_t, len, p_space << PAGE_SHIFT); |
176144455 splice: don't rea... |
827 |
ret = do_splice_to(in, &pos, pipe, read_len, flags); |
51a92c0f6 splice: fix offse... |
828 |
if (unlikely(ret <= 0)) |
b92ce5589 [PATCH] splice: a... |
829 830 831 |
goto out_release; read_len = ret; |
c66ab6fa7 splice: abstract ... |
832 |
sd->total_len = read_len; |
b92ce5589 [PATCH] splice: a... |
833 834 |
/* |
0ff28d9f4 splice: sendfile(... |
835 836 837 838 839 840 841 842 843 |
* If more data is pending, set SPLICE_F_MORE * If this is the last data and SPLICE_F_MORE was not set * initially, clears it. */ if (read_len < len) sd->flags |= SPLICE_F_MORE; else if (!more) sd->flags &= ~SPLICE_F_MORE; /* |
b92ce5589 [PATCH] splice: a... |
844 845 846 847 |
* NOTE: nonblocking mode only applies to the input. We * must not do the output in nonblocking mode as then we * could get stuck data in the internal pipe: */ |
c66ab6fa7 splice: abstract ... |
848 |
ret = actor(pipe, sd); |
a82c53a0e splice: fix sendf... |
849 850 |
if (unlikely(ret <= 0)) { sd->pos = prev_pos; |
b92ce5589 [PATCH] splice: a... |
851 |
goto out_release; |
a82c53a0e splice: fix sendf... |
852 |
} |
b92ce5589 [PATCH] splice: a... |
853 854 855 |
bytes += ret; len -= ret; |
bcd4f3acb splice: direct sp... |
856 |
sd->pos = pos; |
b92ce5589 [PATCH] splice: a... |
857 |
|
a82c53a0e splice: fix sendf... |
858 859 |
if (ret < read_len) { sd->pos = prev_pos + ret; |
51a92c0f6 splice: fix offse... |
860 |
goto out_release; |
a82c53a0e splice: fix sendf... |
861 |
} |
b92ce5589 [PATCH] splice: a... |
862 |
} |
9e97198db splice: fix probl... |
863 |
done: |
8cefc107c pipe: Use head an... |
864 |
pipe->tail = pipe->head = 0; |
808487085 splice: always up... |
865 |
file_accessed(in); |
b92ce5589 [PATCH] splice: a... |
866 867 868 869 870 871 872 |
return bytes; out_release: /* * If we did an incomplete transfer we must release * the pipe buffers in question: */ |
8cefc107c pipe: Use head an... |
873 874 |
for (i = 0; i < pipe->ring_size; i++) { struct pipe_buffer *buf = &pipe->bufs[i]; |
b92ce5589 [PATCH] splice: a... |
875 |
|
a779638cf pipe: add pipe_bu... |
876 877 |
if (buf->ops) pipe_buf_release(pipe, buf); |
b92ce5589 [PATCH] splice: a... |
878 |
} |
b92ce5589 [PATCH] splice: a... |
879 |
|
9e97198db splice: fix probl... |
880 881 |
if (!bytes) bytes = ret; |
c66ab6fa7 splice: abstract ... |
882 |
|
9e97198db splice: fix probl... |
883 |
goto done; |
c66ab6fa7 splice: abstract ... |
884 885 886 887 888 889 |
} EXPORT_SYMBOL(splice_direct_to_actor); static int direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) { |
6a14b90bb vmsplice: add vms... |
890 |
struct file *file = sd->u.file; |
c66ab6fa7 splice: abstract ... |
891 |
|
7995bd287 splice: don't pas... |
892 |
return do_splice_from(pipe, file, sd->opos, sd->total_len, |
2cb4b05e7 splice: direct_sp... |
893 |
sd->flags); |
c66ab6fa7 splice: abstract ... |
894 |
} |
932cc6d4f splice: completel... |
895 896 897 898 899 |
/** * do_splice_direct - splices data directly between two files * @in: file to splice from * @ppos: input file offset * @out: file to splice to |
acdb37c36 fs: fix new splic... |
900 |
* @opos: output file offset |
932cc6d4f splice: completel... |
901 902 903 904 905 906 907 908 909 910 |
* @len: number of bytes to splice * @flags: splice modifier flags * * Description: * For use by do_sendfile(). splice can easily emulate sendfile, but * doing it in the application would incur an extra system call * (splice in + splice out, as compared to just sendfile()). So this helper * can splice directly through a process-private pipe. * */ |
c66ab6fa7 splice: abstract ... |
911 |
long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, |
7995bd287 splice: don't pas... |
912 |
loff_t *opos, size_t len, unsigned int flags) |
c66ab6fa7 splice: abstract ... |
913 914 915 916 917 918 |
{ struct splice_desc sd = { .len = len, .total_len = len, .flags = flags, .pos = *ppos, |
6a14b90bb vmsplice: add vms... |
919 |
.u.file = out, |
7995bd287 splice: don't pas... |
920 |
.opos = opos, |
c66ab6fa7 splice: abstract ... |
921 |
}; |
51a92c0f6 splice: fix offse... |
922 |
long ret; |
c66ab6fa7 splice: abstract ... |
923 |
|
18c67cb9f splice: lift chec... |
924 925 926 927 928 929 930 931 932 |
if (unlikely(!(out->f_mode & FMODE_WRITE))) return -EBADF; if (unlikely(out->f_flags & O_APPEND)) return -EINVAL; ret = rw_verify_area(WRITE, out, opos, len); if (unlikely(ret < 0)) return ret; |
c66ab6fa7 splice: abstract ... |
933 |
ret = splice_direct_to_actor(in, &sd, direct_splice_actor); |
51a92c0f6 splice: fix offse... |
934 |
if (ret > 0) |
a82c53a0e splice: fix sendf... |
935 |
*ppos = sd.pos; |
51a92c0f6 splice: fix offse... |
936 |
|
c66ab6fa7 splice: abstract ... |
937 |
return ret; |
b92ce5589 [PATCH] splice: a... |
938 |
} |
1c118596a vfs: export do_sp... |
939 |
EXPORT_SYMBOL(do_splice_direct); |
b92ce5589 [PATCH] splice: a... |
940 |
|
8924feff6 splice: lift pipe... |
941 942 |
static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) { |
52bce9116 splice: reinstate... |
943 944 945 946 947 |
for (;;) { if (unlikely(!pipe->readers)) { send_sig(SIGPIPE, current, 0); return -EPIPE; } |
6718b6f85 pipe: Allow pipes... |
948 |
if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) |
52bce9116 splice: reinstate... |
949 |
return 0; |
8924feff6 splice: lift pipe... |
950 951 952 953 |
if (flags & SPLICE_F_NONBLOCK) return -EAGAIN; if (signal_pending(current)) return -ERESTARTSYS; |
472e5b056 pipe: remove pipe... |
954 |
pipe_wait_writable(pipe); |
8924feff6 splice: lift pipe... |
955 |
} |
8924feff6 splice: lift pipe... |
956 |
} |
7c77f0b3f splice: implement... |
957 958 959 |
static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, struct pipe_inode_info *opipe, size_t len, unsigned int flags); |
ddac0d39c [PATCH] splice: f... |
960 961 |
/* |
83f9135bd [PATCH] splice: a... |
962 963 |
* Determine where to splice to/from. */ |
ee6e00c86 splice: change ex... |
964 965 |
long do_splice(struct file *in, loff_t *off_in, struct file *out, loff_t *off_out, size_t len, unsigned int flags) |
5274f052e [PATCH] Introduce... |
966 |
{ |
7c77f0b3f splice: implement... |
967 968 |
struct pipe_inode_info *ipipe; struct pipe_inode_info *opipe; |
7995bd287 splice: don't pas... |
969 |
loff_t offset; |
a4514ebd8 [PATCH] splice: o... |
970 |
long ret; |
5274f052e [PATCH] Introduce... |
971 |
|
90da2e3f2 splice: move f_mo... |
972 973 974 |
if (unlikely(!(in->f_mode & FMODE_READ) || !(out->f_mode & FMODE_WRITE))) return -EBADF; |
c73be61ce pipe: Add general... |
975 976 |
ipipe = get_pipe_info(in, true); opipe = get_pipe_info(out, true); |
7c77f0b3f splice: implement... |
977 978 979 980 |
if (ipipe && opipe) { if (off_in || off_out) return -ESPIPE; |
7c77f0b3f splice: implement... |
981 982 983 |
/* Splicing to self would be fun, but... */ if (ipipe == opipe) return -EINVAL; |
ee5e00119 fs: Make splice()... |
984 985 |
if ((in->f_flags | out->f_flags) & O_NONBLOCK) flags |= SPLICE_F_NONBLOCK; |
7c77f0b3f splice: implement... |
986 987 988 989 |
return splice_pipe_to_pipe(ipipe, opipe, len, flags); } if (ipipe) { |
529565dcb [PATCH] splice: a... |
990 991 |
if (off_in) return -ESPIPE; |
b92ce5589 [PATCH] splice: a... |
992 |
if (off_out) { |
19c9a49b4 splice: check f_m... |
993 |
if (!(out->f_mode & FMODE_PWRITE)) |
b92ce5589 [PATCH] splice: a... |
994 |
return -EINVAL; |
ee6e00c86 splice: change ex... |
995 |
offset = *off_out; |
7995bd287 splice: don't pas... |
996 997 998 |
} else { offset = out->f_pos; } |
529565dcb [PATCH] splice: a... |
999 |
|
18c67cb9f splice: lift chec... |
1000 1001 1002 1003 1004 1005 |
if (unlikely(out->f_flags & O_APPEND)) return -EINVAL; ret = rw_verify_area(WRITE, out, &offset, len); if (unlikely(ret < 0)) return ret; |
ee5e00119 fs: Make splice()... |
1006 1007 |
if (in->f_flags & O_NONBLOCK) flags |= SPLICE_F_NONBLOCK; |
500368f7f lift file_*_write... |
1008 |
file_start_write(out); |
7995bd287 splice: don't pas... |
1009 |
ret = do_splice_from(ipipe, out, &offset, len, flags); |
500368f7f lift file_*_write... |
1010 |
file_end_write(out); |
a4514ebd8 [PATCH] splice: o... |
1011 |
|
7995bd287 splice: don't pas... |
1012 1013 |
if (!off_out) out->f_pos = offset; |
ee6e00c86 splice: change ex... |
1014 1015 |
else *off_out = offset; |
a4514ebd8 [PATCH] splice: o... |
1016 1017 |
return ret; |
529565dcb [PATCH] splice: a... |
1018 |
} |
5274f052e [PATCH] Introduce... |
1019 |
|
7c77f0b3f splice: implement... |
1020 |
if (opipe) { |
529565dcb [PATCH] splice: a... |
1021 1022 |
if (off_out) return -ESPIPE; |
b92ce5589 [PATCH] splice: a... |
1023 |
if (off_in) { |
19c9a49b4 splice: check f_m... |
1024 |
if (!(in->f_mode & FMODE_PREAD)) |
b92ce5589 [PATCH] splice: a... |
1025 |
return -EINVAL; |
ee6e00c86 splice: change ex... |
1026 |
offset = *off_in; |
7995bd287 splice: don't pas... |
1027 1028 1029 |
} else { offset = in->f_pos; } |
529565dcb [PATCH] splice: a... |
1030 |
|
ee5e00119 fs: Make splice()... |
1031 1032 |
if (out->f_flags & O_NONBLOCK) flags |= SPLICE_F_NONBLOCK; |
8924feff6 splice: lift pipe... |
1033 1034 |
pipe_lock(opipe); ret = wait_for_space(opipe, flags); |
3253d9d09 splice: only read... |
1035 |
if (!ret) { |
6a965666b Merge tag 'notifi... |
1036 |
unsigned int p_space; |
3253d9d09 splice: only read... |
1037 1038 |
/* Don't try to read more the pipe has space for. */ |
6a965666b Merge tag 'notifi... |
1039 1040 |
p_space = opipe->max_usage - pipe_occupancy(opipe->head, opipe->tail); len = min_t(size_t, len, p_space << PAGE_SHIFT); |
3253d9d09 splice: only read... |
1041 |
|
8924feff6 splice: lift pipe... |
1042 |
ret = do_splice_to(in, &offset, opipe, len, flags); |
3253d9d09 splice: only read... |
1043 |
} |
8924feff6 splice: lift pipe... |
1044 1045 1046 |
pipe_unlock(opipe); if (ret > 0) wakeup_pipe_readers(opipe); |
7995bd287 splice: don't pas... |
1047 1048 |
if (!off_in) in->f_pos = offset; |
ee6e00c86 splice: change ex... |
1049 1050 |
else *off_in = offset; |
a4514ebd8 [PATCH] splice: o... |
1051 1052 |
return ret; |
529565dcb [PATCH] splice: a... |
1053 |
} |
5274f052e [PATCH] Introduce... |
1054 1055 1056 |
return -EINVAL; } |
ee6e00c86 splice: change ex... |
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 |
static long __do_splice(struct file *in, loff_t __user *off_in, struct file *out, loff_t __user *off_out, size_t len, unsigned int flags) { struct pipe_inode_info *ipipe; struct pipe_inode_info *opipe; loff_t offset, *__off_in = NULL, *__off_out = NULL; long ret; ipipe = get_pipe_info(in, true); opipe = get_pipe_info(out, true); if (ipipe && off_in) return -ESPIPE; if (opipe && off_out) return -ESPIPE; if (off_out) { if (copy_from_user(&offset, off_out, sizeof(loff_t))) return -EFAULT; __off_out = &offset; } if (off_in) { if (copy_from_user(&offset, off_in, sizeof(loff_t))) return -EFAULT; __off_in = &offset; } ret = do_splice(in, __off_in, out, __off_out, len, flags); if (ret < 0) return ret; if (__off_out && copy_to_user(off_out, __off_out, sizeof(loff_t))) return -EFAULT; if (__off_in && copy_to_user(off_in, __off_in, sizeof(loff_t))) return -EFAULT; return ret; } |
79fddc4ef new helper: add_t... |
1096 1097 1098 |
static int iter_to_pipe(struct iov_iter *from, struct pipe_inode_info *pipe, unsigned flags) |
912d35f86 [PATCH] Add suppo... |
1099 |
{ |
79fddc4ef new helper: add_t... |
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 |
struct pipe_buffer buf = { .ops = &user_page_pipe_buf_ops, .flags = flags }; size_t total = 0; int ret = 0; bool failed = false; while (iov_iter_count(from) && !failed) { struct page *pages[16]; |
db85a9eb2 splice: switch ge... |
1110 1111 |
ssize_t copied; size_t start; |
79fddc4ef new helper: add_t... |
1112 |
int n; |
db85a9eb2 splice: switch ge... |
1113 |
|
79fddc4ef new helper: add_t... |
1114 1115 1116 1117 1118 |
copied = iov_iter_get_pages(from, pages, ~0UL, 16, &start); if (copied <= 0) { ret = copied; break; } |
db85a9eb2 splice: switch ge... |
1119 |
|
79fddc4ef new helper: add_t... |
1120 |
for (n = 0; copied; n++, start = 0) { |
db85a9eb2 splice: switch ge... |
1121 |
int size = min_t(int, copied, PAGE_SIZE - start); |
79fddc4ef new helper: add_t... |
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
if (!failed) { buf.page = pages[n]; buf.offset = start; buf.len = size; ret = add_to_pipe(pipe, &buf); if (unlikely(ret < 0)) { failed = true; } else { iov_iter_advance(from, ret); total += ret; } } else { put_page(pages[n]); } |
db85a9eb2 splice: switch ge... |
1136 |
copied -= size; |
912d35f86 [PATCH] Add suppo... |
1137 |
} |
912d35f86 [PATCH] Add suppo... |
1138 |
} |
79fddc4ef new helper: add_t... |
1139 |
return total ? total : ret; |
912d35f86 [PATCH] Add suppo... |
1140 |
} |
6a14b90bb vmsplice: add vms... |
1141 1142 1143 |
static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, struct splice_desc *sd) { |
6130f5315 switch vmsplice_t... |
1144 1145 |
int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data); return n == sd->len ? n : -EFAULT; |
6a14b90bb vmsplice: add vms... |
1146 1147 1148 1149 1150 1151 |
} /* * For lack of a better implementation, implement vmsplice() to userspace * as a simple copy of the pipes pages to the user iov. */ |
87a3002af vmsplice(): lift ... |
1152 1153 |
static long vmsplice_to_user(struct file *file, struct iov_iter *iter, unsigned int flags) |
6a14b90bb vmsplice: add vms... |
1154 |
{ |
c73be61ce pipe: Add general... |
1155 |
struct pipe_inode_info *pipe = get_pipe_info(file, true); |
87a3002af vmsplice(): lift ... |
1156 1157 1158 1159 1160 1161 |
struct splice_desc sd = { .total_len = iov_iter_count(iter), .flags = flags, .u.data = iter }; long ret = 0; |
6a14b90bb vmsplice: add vms... |
1162 |
|
6a14b90bb vmsplice: add vms... |
1163 1164 |
if (!pipe) return -EBADF; |
345995fa4 vmsplice_to_user(... |
1165 1166 1167 1168 1169 |
if (sd.total_len) { pipe_lock(pipe); ret = __splice_from_pipe(pipe, &sd, pipe_to_user); pipe_unlock(pipe); } |
6a14b90bb vmsplice: add vms... |
1170 1171 1172 |
return ret; } |
912d35f86 [PATCH] Add suppo... |
1173 1174 1175 1176 |
/* * vmsplice splices a user address range into a pipe. It can be thought of * as splice-from-memory, where the regular splice is splice-from-file (or * to file). In both cases the output is a pipe, naturally. |
912d35f86 [PATCH] Add suppo... |
1177 |
*/ |
87a3002af vmsplice(): lift ... |
1178 1179 |
static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter, unsigned int flags) |
912d35f86 [PATCH] Add suppo... |
1180 |
{ |
ddac0d39c [PATCH] splice: f... |
1181 |
struct pipe_inode_info *pipe; |
87a3002af vmsplice(): lift ... |
1182 |
long ret = 0; |
79fddc4ef new helper: add_t... |
1183 1184 1185 1186 |
unsigned buf_flag = 0; if (flags & SPLICE_F_GIFT) buf_flag = PIPE_BUF_FLAG_GIFT; |
912d35f86 [PATCH] Add suppo... |
1187 |
|
c73be61ce pipe: Add general... |
1188 |
pipe = get_pipe_info(file, true); |
ddac0d39c [PATCH] splice: f... |
1189 |
if (!pipe) |
912d35f86 [PATCH] Add suppo... |
1190 |
return -EBADF; |
912d35f86 [PATCH] Add suppo... |
1191 |
|
8924feff6 splice: lift pipe... |
1192 1193 |
pipe_lock(pipe); ret = wait_for_space(pipe, flags); |
79fddc4ef new helper: add_t... |
1194 |
if (!ret) |
87a3002af vmsplice(): lift ... |
1195 |
ret = iter_to_pipe(iter, pipe, buf_flag); |
8924feff6 splice: lift pipe... |
1196 1197 1198 |
pipe_unlock(pipe); if (ret > 0) wakeup_pipe_readers(pipe); |
35f3d14db pipe: add support... |
1199 |
return ret; |
912d35f86 [PATCH] Add suppo... |
1200 |
} |
87a3002af vmsplice(): lift ... |
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 |
static int vmsplice_type(struct fd f, int *type) { if (!f.file) return -EBADF; if (f.file->f_mode & FMODE_WRITE) { *type = WRITE; } else if (f.file->f_mode & FMODE_READ) { *type = READ; } else { fdput(f); return -EBADF; } return 0; } |
6a14b90bb vmsplice: add vms... |
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
/* * Note that vmsplice only really supports true splicing _from_ user memory * to a pipe, not the other way around. Splicing from user memory is a simple * operation that can be supported without any funky alignment restrictions * or nasty vm tricks. We simply map in the user memory and fill them into * a pipe. The reverse isn't quite as easy, though. There are two possible * solutions for that: * * - memcpy() the data internally, at which point we might as well just * do a regular read() on the buffer anyway. * - Lots of nasty vm tricks, that are neither fast nor flexible (it * has restriction limitations on both ends of the pipe). * * Currently we punt and implement it as a normal copy, see pipe_to_user(). * */ |
87a3002af vmsplice(): lift ... |
1231 |
SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov, |
30cfe4ef8 fs: add do_vmspli... |
1232 1233 |
unsigned long, nr_segs, unsigned int, flags) { |
87a3002af vmsplice(): lift ... |
1234 1235 1236 |
struct iovec iovstack[UIO_FASTIOV]; struct iovec *iov = iovstack; struct iov_iter iter; |
87e5e6dab uio: make import_... |
1237 |
ssize_t error; |
87a3002af vmsplice(): lift ... |
1238 1239 |
struct fd f; int type; |
3a173d222 Revert "Revert "f... |
1240 1241 |
if (unlikely(flags & ~SPLICE_F_ALL)) return -EINVAL; |
87a3002af vmsplice(): lift ... |
1242 1243 1244 1245 1246 1247 1248 |
f = fdget(fd); error = vmsplice_type(f, &type); if (error) return error; error = import_iovec(type, uiov, nr_segs, ARRAY_SIZE(iovstack), &iov, &iter); |
3a173d222 Revert "Revert "f... |
1249 1250 |
if (error < 0) goto out_fdput; |
30cfe4ef8 fs: add do_vmspli... |
1251 |
|
3a173d222 Revert "Revert "f... |
1252 1253 1254 1255 1256 1257 |
if (!iov_iter_count(&iter)) error = 0; else if (iov_iter_rw(&iter) == WRITE) error = vmsplice_to_pipe(f.file, &iter, flags); else error = vmsplice_to_user(f.file, &iter, flags); |
3ea6bc589 Revert "fs: remov... |
1258 |
|
3a173d222 Revert "Revert "f... |
1259 1260 |
kfree(iov); out_fdput: |
87a3002af vmsplice(): lift ... |
1261 1262 |
fdput(f); return error; |
76b021d05 convert vmsplice ... |
1263 |
} |
76b021d05 convert vmsplice ... |
1264 |
|
836f92adf [CVE-2009-0029] S... |
1265 1266 1267 |
SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in, int, fd_out, loff_t __user *, off_out, size_t, len, unsigned int, flags) |
5274f052e [PATCH] Introduce... |
1268 |
{ |
2903ff019 switch simple cas... |
1269 |
struct fd in, out; |
5274f052e [PATCH] Introduce... |
1270 |
long error; |
5274f052e [PATCH] Introduce... |
1271 1272 1273 |
if (unlikely(!len)) return 0; |
3d6ea290f splice/tee/vmspli... |
1274 1275 |
if (unlikely(flags & ~SPLICE_F_ALL)) return -EINVAL; |
5274f052e [PATCH] Introduce... |
1276 |
error = -EBADF; |
2903ff019 switch simple cas... |
1277 1278 |
in = fdget(fd_in); if (in.file) { |
90da2e3f2 splice: move f_mo... |
1279 1280 |
out = fdget(fd_out); if (out.file) { |
ee6e00c86 splice: change ex... |
1281 1282 |
error = __do_splice(in.file, off_in, out.file, off_out, len, flags); |
90da2e3f2 splice: move f_mo... |
1283 |
fdput(out); |
5274f052e [PATCH] Introduce... |
1284 |
} |
2903ff019 switch simple cas... |
1285 |
fdput(in); |
5274f052e [PATCH] Introduce... |
1286 |
} |
5274f052e [PATCH] Introduce... |
1287 1288 |
return error; } |
70524490e [PATCH] splice: a... |
1289 1290 |
/* |
aadd06e5c [PATCH] splice: f... |
1291 1292 1293 |
* Make sure there's data to read. Wait for input if we can, otherwise * return an appropriate error. */ |
7c77f0b3f splice: implement... |
1294 |
static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
aadd06e5c [PATCH] splice: f... |
1295 1296 1297 1298 |
{ int ret; /* |
8cefc107c pipe: Use head an... |
1299 |
* Check the pipe occupancy without the inode lock first. This function |
aadd06e5c [PATCH] splice: f... |
1300 1301 |
* is speculative anyways, so missing one is ok. */ |
8cefc107c pipe: Use head an... |
1302 |
if (!pipe_empty(pipe->head, pipe->tail)) |
aadd06e5c [PATCH] splice: f... |
1303 1304 1305 |
return 0; ret = 0; |
61e0d47c3 splice: add helpe... |
1306 |
pipe_lock(pipe); |
aadd06e5c [PATCH] splice: f... |
1307 |
|
8cefc107c pipe: Use head an... |
1308 |
while (pipe_empty(pipe->head, pipe->tail)) { |
aadd06e5c [PATCH] splice: f... |
1309 1310 1311 1312 1313 1314 |
if (signal_pending(current)) { ret = -ERESTARTSYS; break; } if (!pipe->writers) break; |
a28c8b9db pipe: remove 'wai... |
1315 1316 1317 |
if (flags & SPLICE_F_NONBLOCK) { ret = -EAGAIN; break; |
aadd06e5c [PATCH] splice: f... |
1318 |
} |
472e5b056 pipe: remove pipe... |
1319 |
pipe_wait_readable(pipe); |
aadd06e5c [PATCH] splice: f... |
1320 |
} |
61e0d47c3 splice: add helpe... |
1321 |
pipe_unlock(pipe); |
aadd06e5c [PATCH] splice: f... |
1322 1323 1324 1325 1326 1327 1328 |
return ret; } /* * Make sure there's writeable room. Wait for room if we can, otherwise * return an appropriate error. */ |
7c77f0b3f splice: implement... |
1329 |
static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) |
aadd06e5c [PATCH] splice: f... |
1330 1331 1332 1333 |
{ int ret; /* |
8cefc107c pipe: Use head an... |
1334 |
* Check pipe occupancy without the inode lock first. This function |
aadd06e5c [PATCH] splice: f... |
1335 1336 |
* is speculative anyways, so missing one is ok. */ |
566d13628 pipe: Fix pipe_fu... |
1337 |
if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) |
aadd06e5c [PATCH] splice: f... |
1338 1339 1340 |
return 0; ret = 0; |
61e0d47c3 splice: add helpe... |
1341 |
pipe_lock(pipe); |
aadd06e5c [PATCH] splice: f... |
1342 |
|
6718b6f85 pipe: Allow pipes... |
1343 |
while (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { |
aadd06e5c [PATCH] splice: f... |
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
if (!pipe->readers) { send_sig(SIGPIPE, current, 0); ret = -EPIPE; break; } if (flags & SPLICE_F_NONBLOCK) { ret = -EAGAIN; break; } if (signal_pending(current)) { ret = -ERESTARTSYS; break; } |
472e5b056 pipe: remove pipe... |
1357 |
pipe_wait_writable(pipe); |
aadd06e5c [PATCH] splice: f... |
1358 |
} |
61e0d47c3 splice: add helpe... |
1359 |
pipe_unlock(pipe); |
aadd06e5c [PATCH] splice: f... |
1360 1361 1362 1363 |
return ret; } /* |
7c77f0b3f splice: implement... |
1364 1365 1366 1367 1368 1369 1370 |
* Splice contents of ipipe to opipe. */ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, struct pipe_inode_info *opipe, size_t len, unsigned int flags) { struct pipe_buffer *ibuf, *obuf; |
8cefc107c pipe: Use head an... |
1371 1372 1373 1374 |
unsigned int i_head, o_head; unsigned int i_tail, o_tail; unsigned int i_mask, o_mask; int ret = 0; |
7c77f0b3f splice: implement... |
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 |
bool input_wakeup = false; retry: ret = ipipe_prep(ipipe, flags); if (ret) return ret; ret = opipe_prep(opipe, flags); if (ret) return ret; /* * Potential ABBA deadlock, work around it by ordering lock * grabbing by pipe info address. Otherwise two different processes * could deadlock (one doing tee from A -> B, the other from B -> A). */ pipe_double_lock(ipipe, opipe); |
8cefc107c pipe: Use head an... |
1393 1394 1395 1396 |
i_tail = ipipe->tail; i_mask = ipipe->ring_size - 1; o_head = opipe->head; o_mask = opipe->ring_size - 1; |
7c77f0b3f splice: implement... |
1397 |
do { |
8cefc107c pipe: Use head an... |
1398 |
size_t o_len; |
7c77f0b3f splice: implement... |
1399 1400 1401 1402 1403 1404 |
if (!opipe->readers) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; } |
8cefc107c pipe: Use head an... |
1405 1406 1407 1408 |
i_head = ipipe->head; o_tail = opipe->tail; if (pipe_empty(i_head, i_tail) && !ipipe->writers) |
7c77f0b3f splice: implement... |
1409 1410 1411 1412 1413 1414 |
break; /* * Cannot make any progress, because either the input * pipe is empty or the output pipe is full. */ |
8cefc107c pipe: Use head an... |
1415 |
if (pipe_empty(i_head, i_tail) || |
6718b6f85 pipe: Allow pipes... |
1416 |
pipe_full(o_head, o_tail, opipe->max_usage)) { |
7c77f0b3f splice: implement... |
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 |
/* Already processed some buffers, break */ if (ret) break; if (flags & SPLICE_F_NONBLOCK) { ret = -EAGAIN; break; } /* * We raced with another reader/writer and haven't * managed to process any buffers. A zero return * value means EOF, so retry instead. */ pipe_unlock(ipipe); pipe_unlock(opipe); goto retry; } |
8cefc107c pipe: Use head an... |
1435 1436 |
ibuf = &ipipe->bufs[i_tail & i_mask]; obuf = &opipe->bufs[o_head & o_mask]; |
7c77f0b3f splice: implement... |
1437 1438 1439 1440 1441 1442 1443 |
if (len >= ibuf->len) { /* * Simply move the whole buffer from ipipe to opipe */ *obuf = *ibuf; ibuf->ops = NULL; |
8cefc107c pipe: Use head an... |
1444 1445 |
i_tail++; ipipe->tail = i_tail; |
7c77f0b3f splice: implement... |
1446 |
input_wakeup = true; |
8cefc107c pipe: Use head an... |
1447 1448 1449 |
o_len = obuf->len; o_head++; opipe->head = o_head; |
7c77f0b3f splice: implement... |
1450 1451 1452 1453 1454 |
} else { /* * Get a reference to this pipe buffer, * so we can copy the contents over. */ |
15fab63e1 fs: prevent page ... |
1455 1456 1457 1458 1459 |
if (!pipe_buf_get(ipipe, ibuf)) { if (ret == 0) ret = -EFAULT; break; } |
7c77f0b3f splice: implement... |
1460 1461 1462 |
*obuf = *ibuf; /* |
f6dd97558 pipe: merge anon_... |
1463 |
* Don't inherit the gift and merge flags, we need to |
7c77f0b3f splice: implement... |
1464 1465 1466 |
* prevent multiple steals of this page. */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
f6dd97558 pipe: merge anon_... |
1467 |
obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; |
a0ce2f0aa splice: don't mer... |
1468 |
|
7c77f0b3f splice: implement... |
1469 |
obuf->len = len; |
8cefc107c pipe: Use head an... |
1470 1471 1472 1473 1474 |
ibuf->offset += len; ibuf->len -= len; o_len = len; o_head++; opipe->head = o_head; |
7c77f0b3f splice: implement... |
1475 |
} |
8cefc107c pipe: Use head an... |
1476 1477 |
ret += o_len; len -= o_len; |
7c77f0b3f splice: implement... |
1478 1479 1480 1481 1482 1483 1484 1485 |
} while (len); pipe_unlock(ipipe); pipe_unlock(opipe); /* * If we put data in the output pipe, wakeup any potential readers. */ |
825cdcb1a splice: add wakeu... |
1486 1487 |
if (ret > 0) wakeup_pipe_readers(opipe); |
7c77f0b3f splice: implement... |
1488 1489 1490 1491 1492 1493 1494 |
if (input_wakeup) wakeup_pipe_writers(ipipe); return ret; } /* |
70524490e [PATCH] splice: a... |
1495 1496 1497 1498 1499 1500 1501 |
* Link contents of ipipe to opipe. */ static int link_pipe(struct pipe_inode_info *ipipe, struct pipe_inode_info *opipe, size_t len, unsigned int flags) { struct pipe_buffer *ibuf, *obuf; |
8cefc107c pipe: Use head an... |
1502 1503 1504 1505 |
unsigned int i_head, o_head; unsigned int i_tail, o_tail; unsigned int i_mask, o_mask; int ret = 0; |
70524490e [PATCH] splice: a... |
1506 1507 1508 |
/* * Potential ABBA deadlock, work around it by ordering lock |
61e0d47c3 splice: add helpe... |
1509 |
* grabbing by pipe info address. Otherwise two different processes |
70524490e [PATCH] splice: a... |
1510 1511 |
* could deadlock (one doing tee from A -> B, the other from B -> A). */ |
61e0d47c3 splice: add helpe... |
1512 |
pipe_double_lock(ipipe, opipe); |
70524490e [PATCH] splice: a... |
1513 |
|
8cefc107c pipe: Use head an... |
1514 1515 1516 1517 |
i_tail = ipipe->tail; i_mask = ipipe->ring_size - 1; o_head = opipe->head; o_mask = opipe->ring_size - 1; |
aadd06e5c [PATCH] splice: f... |
1518 |
do { |
70524490e [PATCH] splice: a... |
1519 1520 1521 1522 1523 1524 |
if (!opipe->readers) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; } |
70524490e [PATCH] splice: a... |
1525 |
|
8cefc107c pipe: Use head an... |
1526 1527 |
i_head = ipipe->head; o_tail = opipe->tail; |
aadd06e5c [PATCH] splice: f... |
1528 |
/* |
8cefc107c pipe: Use head an... |
1529 |
* If we have iterated all input buffers or run out of |
aadd06e5c [PATCH] splice: f... |
1530 1531 |
* output room, break. */ |
8cefc107c pipe: Use head an... |
1532 |
if (pipe_empty(i_head, i_tail) || |
6718b6f85 pipe: Allow pipes... |
1533 |
pipe_full(o_head, o_tail, opipe->max_usage)) |
aadd06e5c [PATCH] splice: f... |
1534 |
break; |
70524490e [PATCH] splice: a... |
1535 |
|
8cefc107c pipe: Use head an... |
1536 1537 |
ibuf = &ipipe->bufs[i_tail & i_mask]; obuf = &opipe->bufs[o_head & o_mask]; |
70524490e [PATCH] splice: a... |
1538 1539 |
/* |
aadd06e5c [PATCH] splice: f... |
1540 1541 |
* Get a reference to this pipe buffer, * so we can copy the contents over. |
70524490e [PATCH] splice: a... |
1542 |
*/ |
15fab63e1 fs: prevent page ... |
1543 1544 1545 1546 1547 |
if (!pipe_buf_get(ipipe, ibuf)) { if (ret == 0) ret = -EFAULT; break; } |
aadd06e5c [PATCH] splice: f... |
1548 |
|
aadd06e5c [PATCH] splice: f... |
1549 |
*obuf = *ibuf; |
2a27250e6 [PATCH] tee: link... |
1550 |
/* |
f6dd97558 pipe: merge anon_... |
1551 1552 |
* Don't inherit the gift and merge flag, we need to prevent * multiple steals of this page. |
2a27250e6 [PATCH] tee: link... |
1553 |
*/ |
aadd06e5c [PATCH] splice: f... |
1554 |
obuf->flags &= ~PIPE_BUF_FLAG_GIFT; |
f6dd97558 pipe: merge anon_... |
1555 |
obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE; |
a0ce2f0aa splice: don't mer... |
1556 |
|
aadd06e5c [PATCH] splice: f... |
1557 1558 |
if (obuf->len > len) obuf->len = len; |
aadd06e5c [PATCH] splice: f... |
1559 1560 |
ret += obuf->len; len -= obuf->len; |
8cefc107c pipe: Use head an... |
1561 1562 1563 1564 |
o_head++; opipe->head = o_head; i_tail++; |
aadd06e5c [PATCH] splice: f... |
1565 |
} while (len); |
70524490e [PATCH] splice: a... |
1566 |
|
61e0d47c3 splice: add helpe... |
1567 1568 |
pipe_unlock(ipipe); pipe_unlock(opipe); |
70524490e [PATCH] splice: a... |
1569 |
|
aadd06e5c [PATCH] splice: f... |
1570 1571 1572 |
/* * If we put data in the output pipe, wakeup any potential readers. */ |
825cdcb1a splice: add wakeu... |
1573 1574 |
if (ret > 0) wakeup_pipe_readers(opipe); |
70524490e [PATCH] splice: a... |
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 |
return ret; } /* * This is a tee(1) implementation that works on pipes. It doesn't copy * any data, it simply references the 'in' pages on the 'out' pipe. * The 'flags' used are the SPLICE_F_* variants, currently the only * applicable one is SPLICE_F_NONBLOCK. */ |
9dafdfc2f splice: export do... |
1585 |
long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) |
70524490e [PATCH] splice: a... |
1586 |
{ |
c73be61ce pipe: Add general... |
1587 1588 |
struct pipe_inode_info *ipipe = get_pipe_info(in, true); struct pipe_inode_info *opipe = get_pipe_info(out, true); |
aadd06e5c [PATCH] splice: f... |
1589 |
int ret = -EINVAL; |
70524490e [PATCH] splice: a... |
1590 |
|
90da2e3f2 splice: move f_mo... |
1591 1592 1593 |
if (unlikely(!(in->f_mode & FMODE_READ) || !(out->f_mode & FMODE_WRITE))) return -EBADF; |
70524490e [PATCH] splice: a... |
1594 |
/* |
aadd06e5c [PATCH] splice: f... |
1595 1596 |
* Duplicate the contents of ipipe to opipe without actually * copying the data. |
70524490e [PATCH] splice: a... |
1597 |
*/ |
aadd06e5c [PATCH] splice: f... |
1598 |
if (ipipe && opipe && ipipe != opipe) { |
ee5e00119 fs: Make splice()... |
1599 1600 |
if ((in->f_flags | out->f_flags) & O_NONBLOCK) flags |= SPLICE_F_NONBLOCK; |
aadd06e5c [PATCH] splice: f... |
1601 1602 1603 1604 |
/* * Keep going, unless we encounter an error. The ipipe/opipe * ordering doesn't really matter. */ |
7c77f0b3f splice: implement... |
1605 |
ret = ipipe_prep(ipipe, flags); |
aadd06e5c [PATCH] splice: f... |
1606 |
if (!ret) { |
7c77f0b3f splice: implement... |
1607 |
ret = opipe_prep(opipe, flags); |
02cf01aea splice: only retu... |
1608 |
if (!ret) |
aadd06e5c [PATCH] splice: f... |
1609 |
ret = link_pipe(ipipe, opipe, len, flags); |
aadd06e5c [PATCH] splice: f... |
1610 1611 |
} } |
70524490e [PATCH] splice: a... |
1612 |
|
aadd06e5c [PATCH] splice: f... |
1613 |
return ret; |
70524490e [PATCH] splice: a... |
1614 |
} |
836f92adf [CVE-2009-0029] S... |
1615 |
SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) |
70524490e [PATCH] splice: a... |
1616 |
{ |
90da2e3f2 splice: move f_mo... |
1617 |
struct fd in, out; |
2903ff019 switch simple cas... |
1618 |
int error; |
70524490e [PATCH] splice: a... |
1619 |
|
3d6ea290f splice/tee/vmspli... |
1620 1621 |
if (unlikely(flags & ~SPLICE_F_ALL)) return -EINVAL; |
70524490e [PATCH] splice: a... |
1622 1623 1624 1625 |
if (unlikely(!len)) return 0; error = -EBADF; |
2903ff019 switch simple cas... |
1626 1627 |
in = fdget(fdin); if (in.file) { |
90da2e3f2 splice: move f_mo... |
1628 1629 1630 1631 |
out = fdget(fdout); if (out.file) { error = do_tee(in.file, out.file, len, flags); fdput(out); |
70524490e [PATCH] splice: a... |
1632 |
} |
2903ff019 switch simple cas... |
1633 |
fdput(in); |
70524490e [PATCH] splice: a... |
1634 1635 1636 1637 |
} return error; } |