aboutsummaryrefslogtreecommitdiffstats
path: root/fs/splice.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/splice.c')
-rw-r--r--fs/splice.c581
1 files changed, 420 insertions, 161 deletions
diff --git a/fs/splice.c b/fs/splice.c
index 0559e7577a04..a285fd746dc0 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -27,15 +27,22 @@
27#include <linux/buffer_head.h> 27#include <linux/buffer_head.h>
28#include <linux/module.h> 28#include <linux/module.h>
29#include <linux/syscalls.h> 29#include <linux/syscalls.h>
30#include <linux/uio.h>
31
32struct partial_page {
33 unsigned int offset;
34 unsigned int len;
35};
30 36
31/* 37/*
32 * Passed to the actors 38 * Passed to splice_to_pipe
33 */ 39 */
34struct splice_desc { 40struct splice_pipe_desc {
35 unsigned int len, total_len; /* current and remaining length */ 41 struct page **pages; /* page map */
42 struct partial_page *partial; /* pages[] may not be contig */
43 int nr_pages; /* number of pages in map */
36 unsigned int flags; /* splice flags */ 44 unsigned int flags; /* splice flags */
37 struct file *file; /* file to read/write */ 45 struct pipe_buf_operations *ops;/* ops associated with output pipe */
38 loff_t pos; /* file position */
39}; 46};
40 47
41/* 48/*
@@ -44,7 +51,7 @@ struct splice_desc {
44 * addition of remove_mapping(). If success is returned, the caller may 51 * addition of remove_mapping(). If success is returned, the caller may
45 * attempt to reuse this page for another destination. 52 * attempt to reuse this page for another destination.
46 */ 53 */
47static int page_cache_pipe_buf_steal(struct pipe_inode_info *info, 54static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
48 struct pipe_buffer *buf) 55 struct pipe_buffer *buf)
49{ 56{
50 struct page *page = buf->page; 57 struct page *page = buf->page;
@@ -71,21 +78,19 @@ static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
71 return 1; 78 return 1;
72 } 79 }
73 80
74 buf->flags |= PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU; 81 buf->flags |= PIPE_BUF_FLAG_LRU;
75 return 0; 82 return 0;
76} 83}
77 84
78static void page_cache_pipe_buf_release(struct pipe_inode_info *info, 85static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
79 struct pipe_buffer *buf) 86 struct pipe_buffer *buf)
80{ 87{
81 page_cache_release(buf->page); 88 page_cache_release(buf->page);
82 buf->page = NULL; 89 buf->flags &= ~PIPE_BUF_FLAG_LRU;
83 buf->flags &= ~(PIPE_BUF_FLAG_STOLEN | PIPE_BUF_FLAG_LRU);
84} 90}
85 91
86static void *page_cache_pipe_buf_map(struct file *file, 92static int page_cache_pipe_buf_pin(struct pipe_inode_info *pipe,
87 struct pipe_inode_info *info, 93 struct pipe_buffer *buf)
88 struct pipe_buffer *buf)
89{ 94{
90 struct page *page = buf->page; 95 struct page *page = buf->page;
91 int err; 96 int err;
@@ -111,51 +116,59 @@ static void *page_cache_pipe_buf_map(struct file *file,
111 } 116 }
112 117
113 /* 118 /*
114 * Page is ok afterall, fall through to mapping. 119 * Page is ok afterall, we are done.
115 */ 120 */
116 unlock_page(page); 121 unlock_page(page);
117 } 122 }
118 123
119 return kmap(page); 124 return 0;
120error: 125error:
121 unlock_page(page); 126 unlock_page(page);
122 return ERR_PTR(err); 127 return err;
123} 128}
124 129
125static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info, 130static struct pipe_buf_operations page_cache_pipe_buf_ops = {
126 struct pipe_buffer *buf) 131 .can_merge = 0,
127{ 132 .map = generic_pipe_buf_map,
128 kunmap(buf->page); 133 .unmap = generic_pipe_buf_unmap,
129} 134 .pin = page_cache_pipe_buf_pin,
135 .release = page_cache_pipe_buf_release,
136 .steal = page_cache_pipe_buf_steal,
137 .get = generic_pipe_buf_get,
138};
130 139
131static void page_cache_pipe_buf_get(struct pipe_inode_info *info, 140static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
132 struct pipe_buffer *buf) 141 struct pipe_buffer *buf)
133{ 142{
134 page_cache_get(buf->page); 143 if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
144 return 1;
145
146 buf->flags |= PIPE_BUF_FLAG_LRU;
147 return generic_pipe_buf_steal(pipe, buf);
135} 148}
136 149
137static struct pipe_buf_operations page_cache_pipe_buf_ops = { 150static struct pipe_buf_operations user_page_pipe_buf_ops = {
138 .can_merge = 0, 151 .can_merge = 0,
139 .map = page_cache_pipe_buf_map, 152 .map = generic_pipe_buf_map,
140 .unmap = page_cache_pipe_buf_unmap, 153 .unmap = generic_pipe_buf_unmap,
154 .pin = generic_pipe_buf_pin,
141 .release = page_cache_pipe_buf_release, 155 .release = page_cache_pipe_buf_release,
142 .steal = page_cache_pipe_buf_steal, 156 .steal = user_page_pipe_buf_steal,
143 .get = page_cache_pipe_buf_get, 157 .get = generic_pipe_buf_get,
144}; 158};
145 159
146/* 160/*
147 * Pipe output worker. This sets up our pipe format with the page cache 161 * Pipe output worker. This sets up our pipe format with the page cache
148 * pipe buffer operations. Otherwise very similar to the regular pipe_writev(). 162 * pipe buffer operations. Otherwise very similar to the regular pipe_writev().
149 */ 163 */
150static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages, 164static ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
151 int nr_pages, unsigned long len, 165 struct splice_pipe_desc *spd)
152 unsigned int offset, unsigned int flags)
153{ 166{
154 int ret, do_wakeup, i; 167 int ret, do_wakeup, page_nr;
155 168
156 ret = 0; 169 ret = 0;
157 do_wakeup = 0; 170 do_wakeup = 0;
158 i = 0; 171 page_nr = 0;
159 172
160 if (pipe->inode) 173 if (pipe->inode)
161 mutex_lock(&pipe->inode->i_mutex); 174 mutex_lock(&pipe->inode->i_mutex);
@@ -171,27 +184,22 @@ static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
171 if (pipe->nrbufs < PIPE_BUFFERS) { 184 if (pipe->nrbufs < PIPE_BUFFERS) {
172 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); 185 int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1);
173 struct pipe_buffer *buf = pipe->bufs + newbuf; 186 struct pipe_buffer *buf = pipe->bufs + newbuf;
174 struct page *page = pages[i++];
175 unsigned long this_len;
176 187
177 this_len = PAGE_CACHE_SIZE - offset; 188 buf->page = spd->pages[page_nr];
178 if (this_len > len) 189 buf->offset = spd->partial[page_nr].offset;
179 this_len = len; 190 buf->len = spd->partial[page_nr].len;
191 buf->ops = spd->ops;
192 if (spd->flags & SPLICE_F_GIFT)
193 buf->flags |= PIPE_BUF_FLAG_GIFT;
180 194
181 buf->page = page;
182 buf->offset = offset;
183 buf->len = this_len;
184 buf->ops = &page_cache_pipe_buf_ops;
185 pipe->nrbufs++; 195 pipe->nrbufs++;
196 page_nr++;
197 ret += buf->len;
198
186 if (pipe->inode) 199 if (pipe->inode)
187 do_wakeup = 1; 200 do_wakeup = 1;
188 201
189 ret += this_len; 202 if (!--spd->nr_pages)
190 len -= this_len;
191 offset = 0;
192 if (!--nr_pages)
193 break;
194 if (!len)
195 break; 203 break;
196 if (pipe->nrbufs < PIPE_BUFFERS) 204 if (pipe->nrbufs < PIPE_BUFFERS)
197 continue; 205 continue;
@@ -199,7 +207,7 @@ static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
199 break; 207 break;
200 } 208 }
201 209
202 if (flags & SPLICE_F_NONBLOCK) { 210 if (spd->flags & SPLICE_F_NONBLOCK) {
203 if (!ret) 211 if (!ret)
204 ret = -EAGAIN; 212 ret = -EAGAIN;
205 break; 213 break;
@@ -234,8 +242,8 @@ static ssize_t move_to_pipe(struct pipe_inode_info *pipe, struct page **pages,
234 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); 242 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
235 } 243 }
236 244
237 while (i < nr_pages) 245 while (page_nr < spd->nr_pages)
238 page_cache_release(pages[i++]); 246 page_cache_release(spd->pages[page_nr++]);
239 247
240 return ret; 248 return ret;
241} 249}
@@ -246,17 +254,24 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
246 unsigned int flags) 254 unsigned int flags)
247{ 255{
248 struct address_space *mapping = in->f_mapping; 256 struct address_space *mapping = in->f_mapping;
249 unsigned int loff, offset, nr_pages; 257 unsigned int loff, nr_pages;
250 struct page *pages[PIPE_BUFFERS]; 258 struct page *pages[PIPE_BUFFERS];
259 struct partial_page partial[PIPE_BUFFERS];
251 struct page *page; 260 struct page *page;
252 pgoff_t index, end_index; 261 pgoff_t index, end_index;
253 loff_t isize; 262 loff_t isize;
254 size_t bytes; 263 size_t total_len;
255 int i, error; 264 int error, page_nr;
265 struct splice_pipe_desc spd = {
266 .pages = pages,
267 .partial = partial,
268 .flags = flags,
269 .ops = &page_cache_pipe_buf_ops,
270 };
256 271
257 index = *ppos >> PAGE_CACHE_SHIFT; 272 index = *ppos >> PAGE_CACHE_SHIFT;
258 loff = offset = *ppos & ~PAGE_CACHE_MASK; 273 loff = *ppos & ~PAGE_CACHE_MASK;
259 nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 274 nr_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
260 275
261 if (nr_pages > PIPE_BUFFERS) 276 if (nr_pages > PIPE_BUFFERS)
262 nr_pages = PIPE_BUFFERS; 277 nr_pages = PIPE_BUFFERS;
@@ -266,47 +281,83 @@ __generic_file_splice_read(struct file *in, loff_t *ppos,
266 * read-ahead if this is a non-zero offset (we are likely doing small 281 * read-ahead if this is a non-zero offset (we are likely doing small
267 * chunk splice and the page is already there) for a single page. 282 * chunk splice and the page is already there) for a single page.
268 */ 283 */
269 if (!offset || nr_pages > 1) 284 if (!loff || nr_pages > 1)
270 do_page_cache_readahead(mapping, in, index, nr_pages); 285 page_cache_readahead(mapping, &in->f_ra, in, index, nr_pages);
271 286
272 /* 287 /*
273 * Now fill in the holes: 288 * Now fill in the holes:
274 */ 289 */
275 error = 0; 290 error = 0;
276 bytes = 0; 291 total_len = 0;
277 for (i = 0; i < nr_pages; i++, index++) {
278 unsigned int this_len;
279 292
280 if (!len) 293 /*
281 break; 294 * Lookup the (hopefully) full range of pages we need.
295 */
296 spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages);
282 297
298 /*
299 * If find_get_pages_contig() returned fewer pages than we needed,
300 * allocate the rest.
301 */
302 index += spd.nr_pages;
303 while (spd.nr_pages < nr_pages) {
283 /* 304 /*
284 * this_len is the max we'll use from this page 305 * Page could be there, find_get_pages_contig() breaks on
285 */ 306 * the first hole.
286 this_len = min(len, PAGE_CACHE_SIZE - loff);
287find_page:
288 /*
289 * lookup the page for this index
290 */ 307 */
291 page = find_get_page(mapping, index); 308 page = find_get_page(mapping, index);
292 if (!page) { 309 if (!page) {
293 /* 310 /*
294 * page didn't exist, allocate one 311 * Make sure the read-ahead engine is notified
312 * about this failure.
313 */
314 handle_ra_miss(mapping, &in->f_ra, index);
315
316 /*
317 * page didn't exist, allocate one.
295 */ 318 */
296 page = page_cache_alloc_cold(mapping); 319 page = page_cache_alloc_cold(mapping);
297 if (!page) 320 if (!page)
298 break; 321 break;
299 322
300 error = add_to_page_cache_lru(page, mapping, index, 323 error = add_to_page_cache_lru(page, mapping, index,
301 mapping_gfp_mask(mapping)); 324 mapping_gfp_mask(mapping));
302 if (unlikely(error)) { 325 if (unlikely(error)) {
303 page_cache_release(page); 326 page_cache_release(page);
327 if (error == -EEXIST)
328 continue;
304 break; 329 break;
305 } 330 }
306 331 /*
307 goto readpage; 332 * add_to_page_cache() locks the page, unlock it
333 * to avoid convoluting the logic below even more.
334 */
335 unlock_page(page);
308 } 336 }
309 337
338 pages[spd.nr_pages++] = page;
339 index++;
340 }
341
342 /*
343 * Now loop over the map and see if we need to start IO on any
344 * pages, fill in the partial map, etc.
345 */
346 index = *ppos >> PAGE_CACHE_SHIFT;
347 nr_pages = spd.nr_pages;
348 spd.nr_pages = 0;
349 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
350 unsigned int this_len;
351
352 if (!len)
353 break;
354
355 /*
356 * this_len is the max we'll use from this page
357 */
358 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
359 page = pages[page_nr];
360
310 /* 361 /*
311 * If the page isn't uptodate, we may need to start io on it 362 * If the page isn't uptodate, we may need to start io on it
312 */ 363 */
@@ -327,7 +378,6 @@ find_page:
327 */ 378 */
328 if (!page->mapping) { 379 if (!page->mapping) {
329 unlock_page(page); 380 unlock_page(page);
330 page_cache_release(page);
331 break; 381 break;
332 } 382 }
333 /* 383 /*
@@ -338,16 +388,20 @@ find_page:
338 goto fill_it; 388 goto fill_it;
339 } 389 }
340 390
341readpage:
342 /* 391 /*
343 * need to read in the page 392 * need to read in the page
344 */ 393 */
345 error = mapping->a_ops->readpage(in, page); 394 error = mapping->a_ops->readpage(in, page);
346
347 if (unlikely(error)) { 395 if (unlikely(error)) {
348 page_cache_release(page); 396 /*
397 * We really should re-lookup the page here,
398 * but it complicates things a lot. Instead
399 * lets just do what we already stored, and
400 * we'll get it the next time we are called.
401 */
349 if (error == AOP_TRUNCATED_PAGE) 402 if (error == AOP_TRUNCATED_PAGE)
350 goto find_page; 403 error = 0;
404
351 break; 405 break;
352 } 406 }
353 407
@@ -356,10 +410,8 @@ readpage:
356 */ 410 */
357 isize = i_size_read(mapping->host); 411 isize = i_size_read(mapping->host);
358 end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 412 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
359 if (unlikely(!isize || index > end_index)) { 413 if (unlikely(!isize || index > end_index))
360 page_cache_release(page);
361 break; 414 break;
362 }
363 415
364 /* 416 /*
365 * if this is the last page, see if we need to shrink 417 * if this is the last page, see if we need to shrink
@@ -367,26 +419,35 @@ readpage:
367 */ 419 */
368 if (end_index == index) { 420 if (end_index == index) {
369 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK); 421 loff = PAGE_CACHE_SIZE - (isize & ~PAGE_CACHE_MASK);
370 if (bytes + loff > isize) { 422 if (total_len + loff > isize)
371 page_cache_release(page);
372 break; 423 break;
373 }
374 /* 424 /*
375 * force quit after adding this page 425 * force quit after adding this page
376 */ 426 */
377 nr_pages = i; 427 len = this_len;
378 this_len = min(this_len, loff); 428 this_len = min(this_len, loff);
429 loff = 0;
379 } 430 }
380 } 431 }
381fill_it: 432fill_it:
382 pages[i] = page; 433 partial[page_nr].offset = loff;
383 bytes += this_len; 434 partial[page_nr].len = this_len;
384 len -= this_len; 435 len -= this_len;
436 total_len += this_len;
385 loff = 0; 437 loff = 0;
438 spd.nr_pages++;
439 index++;
386 } 440 }
387 441
388 if (i) 442 /*
389 return move_to_pipe(pipe, pages, i, bytes, offset, flags); 443 * Release any pages at the end, if we quit early. 'i' is how far
444 * we got, 'nr_pages' is how many pages are in the map.
445 */
446 while (page_nr < nr_pages)
447 page_cache_release(pages[page_nr++]);
448
449 if (spd.nr_pages)
450 return splice_to_pipe(pipe, &spd);
390 451
391 return error; 452 return error;
392} 453}
@@ -439,38 +500,24 @@ EXPORT_SYMBOL(generic_file_splice_read);
439 500
440/* 501/*
441 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' 502 * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
442 * using sendpage(). 503 * using sendpage(). Return the number of bytes sent.
443 */ 504 */
444static int pipe_to_sendpage(struct pipe_inode_info *info, 505static int pipe_to_sendpage(struct pipe_inode_info *pipe,
445 struct pipe_buffer *buf, struct splice_desc *sd) 506 struct pipe_buffer *buf, struct splice_desc *sd)
446{ 507{
447 struct file *file = sd->file; 508 struct file *file = sd->file;
448 loff_t pos = sd->pos; 509 loff_t pos = sd->pos;
449 unsigned int offset; 510 int ret, more;
450 ssize_t ret;
451 void *ptr;
452 int more;
453 511
454 /* 512 ret = buf->ops->pin(pipe, buf);
455 * Sub-optimal, but we are limited by the pipe ->map. We don't 513 if (!ret) {
456 * need a kmap'ed buffer here, we just want to make sure we 514 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
457 * have the page pinned if the pipe page originates from the
458 * page cache.
459 */
460 ptr = buf->ops->map(file, info, buf);
461 if (IS_ERR(ptr))
462 return PTR_ERR(ptr);
463
464 offset = pos & ~PAGE_CACHE_MASK;
465 more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len;
466 515
467 ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,more); 516 ret = file->f_op->sendpage(file, buf->page, buf->offset,
468 517 sd->len, &pos, more);
469 buf->ops->unmap(info, buf); 518 }
470 if (ret == sd->len)
471 return 0;
472 519
473 return -EIO; 520 return ret;
474} 521}
475 522
476/* 523/*
@@ -493,43 +540,51 @@ static int pipe_to_sendpage(struct pipe_inode_info *info,
493 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create 540 * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create
494 * a new page in the output file page cache and fill/dirty that. 541 * a new page in the output file page cache and fill/dirty that.
495 */ 542 */
496static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf, 543static int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
497 struct splice_desc *sd) 544 struct splice_desc *sd)
498{ 545{
499 struct file *file = sd->file; 546 struct file *file = sd->file;
500 struct address_space *mapping = file->f_mapping; 547 struct address_space *mapping = file->f_mapping;
501 gfp_t gfp_mask = mapping_gfp_mask(mapping); 548 gfp_t gfp_mask = mapping_gfp_mask(mapping);
502 unsigned int offset; 549 unsigned int offset, this_len;
503 struct page *page; 550 struct page *page;
504 pgoff_t index; 551 pgoff_t index;
505 char *src;
506 int ret; 552 int ret;
507 553
508 /* 554 /*
509 * make sure the data in this buffer is uptodate 555 * make sure the data in this buffer is uptodate
510 */ 556 */
511 src = buf->ops->map(file, info, buf); 557 ret = buf->ops->pin(pipe, buf);
512 if (IS_ERR(src)) 558 if (unlikely(ret))
513 return PTR_ERR(src); 559 return ret;
514 560
515 index = sd->pos >> PAGE_CACHE_SHIFT; 561 index = sd->pos >> PAGE_CACHE_SHIFT;
516 offset = sd->pos & ~PAGE_CACHE_MASK; 562 offset = sd->pos & ~PAGE_CACHE_MASK;
517 563
564 this_len = sd->len;
565 if (this_len + offset > PAGE_CACHE_SIZE)
566 this_len = PAGE_CACHE_SIZE - offset;
567
518 /* 568 /*
519 * Reuse buf page, if SPLICE_F_MOVE is set. 569 * Reuse buf page, if SPLICE_F_MOVE is set and we are doing a full
570 * page.
520 */ 571 */
521 if (sd->flags & SPLICE_F_MOVE) { 572 if ((sd->flags & SPLICE_F_MOVE) && this_len == PAGE_CACHE_SIZE) {
522 /* 573 /*
523 * If steal succeeds, buf->page is now pruned from the vm 574 * If steal succeeds, buf->page is now pruned from the
524 * side (LRU and page cache) and we can reuse it. The page 575 * pagecache and we can reuse it. The page will also be
525 * will also be looked on successful return. 576 * locked on successful return.
526 */ 577 */
527 if (buf->ops->steal(info, buf)) 578 if (buf->ops->steal(pipe, buf))
528 goto find_page; 579 goto find_page;
529 580
530 page = buf->page; 581 page = buf->page;
531 if (add_to_page_cache(page, mapping, index, gfp_mask)) 582 if (add_to_page_cache(page, mapping, index, gfp_mask)) {
583 unlock_page(page);
532 goto find_page; 584 goto find_page;
585 }
586
587 page_cache_get(page);
533 588
534 if (!(buf->flags & PIPE_BUF_FLAG_LRU)) 589 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
535 lru_cache_add(page); 590 lru_cache_add(page);
@@ -558,7 +613,7 @@ find_page:
558 * the full page. 613 * the full page.
559 */ 614 */
560 if (!PageUptodate(page)) { 615 if (!PageUptodate(page)) {
561 if (sd->len < PAGE_CACHE_SIZE) { 616 if (this_len < PAGE_CACHE_SIZE) {
562 ret = mapping->a_ops->readpage(file, page); 617 ret = mapping->a_ops->readpage(file, page);
563 if (unlikely(ret)) 618 if (unlikely(ret))
564 goto out; 619 goto out;
@@ -582,51 +637,67 @@ find_page:
582 } 637 }
583 } 638 }
584 639
585 ret = mapping->a_ops->prepare_write(file, page, 0, sd->len); 640 ret = mapping->a_ops->prepare_write(file, page, offset, offset+this_len);
586 if (ret == AOP_TRUNCATED_PAGE) { 641 if (unlikely(ret)) {
642 loff_t isize = i_size_read(mapping->host);
643
644 if (ret != AOP_TRUNCATED_PAGE)
645 unlock_page(page);
587 page_cache_release(page); 646 page_cache_release(page);
588 goto find_page; 647 if (ret == AOP_TRUNCATED_PAGE)
589 } else if (ret) 648 goto find_page;
649
650 /*
651 * prepare_write() may have instantiated a few blocks
652 * outside i_size. Trim these off again.
653 */
654 if (sd->pos + this_len > isize)
655 vmtruncate(mapping->host, isize);
656
590 goto out; 657 goto out;
658 }
591 659
592 if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) { 660 if (buf->page != page) {
593 char *dst = kmap_atomic(page, KM_USER0); 661 /*
662 * Careful, ->map() uses KM_USER0!
663 */
664 char *src = buf->ops->map(pipe, buf, 1);
665 char *dst = kmap_atomic(page, KM_USER1);
594 666
595 memcpy(dst + offset, src + buf->offset, sd->len); 667 memcpy(dst + offset, src + buf->offset, this_len);
596 flush_dcache_page(page); 668 flush_dcache_page(page);
597 kunmap_atomic(dst, KM_USER0); 669 kunmap_atomic(dst, KM_USER1);
670 buf->ops->unmap(pipe, buf, src);
598 } 671 }
599 672
600 ret = mapping->a_ops->commit_write(file, page, 0, sd->len); 673 ret = mapping->a_ops->commit_write(file, page, offset, offset+this_len);
601 if (ret == AOP_TRUNCATED_PAGE) { 674 if (!ret) {
675 /*
676 * Return the number of bytes written and mark page as
677 * accessed, we are now done!
678 */
679 ret = this_len;
680 mark_page_accessed(page);
681 balance_dirty_pages_ratelimited(mapping);
682 } else if (ret == AOP_TRUNCATED_PAGE) {
602 page_cache_release(page); 683 page_cache_release(page);
603 goto find_page; 684 goto find_page;
604 } else if (ret) 685 }
605 goto out;
606
607 mark_page_accessed(page);
608 balance_dirty_pages_ratelimited(mapping);
609out: 686out:
610 if (!(buf->flags & PIPE_BUF_FLAG_STOLEN)) 687 page_cache_release(page);
611 page_cache_release(page);
612
613 unlock_page(page); 688 unlock_page(page);
614out_nomem: 689out_nomem:
615 buf->ops->unmap(info, buf);
616 return ret; 690 return ret;
617} 691}
618 692
619typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
620 struct splice_desc *);
621
622/* 693/*
623 * Pipe input worker. Most of this logic works like a regular pipe, the 694 * Pipe input worker. Most of this logic works like a regular pipe, the
624 * key here is the 'actor' worker passed in that actually moves the data 695 * key here is the 'actor' worker passed in that actually moves the data
625 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above. 696 * to the wanted destination. See pipe_to_file/pipe_to_sendpage above.
626 */ 697 */
627static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out, 698ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
628 loff_t *ppos, size_t len, unsigned int flags, 699 loff_t *ppos, size_t len, unsigned int flags,
629 splice_actor *actor) 700 splice_actor *actor)
630{ 701{
631 int ret, do_wakeup, err; 702 int ret, do_wakeup, err;
632 struct splice_desc sd; 703 struct splice_desc sd;
@@ -652,16 +723,22 @@ static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
652 sd.len = sd.total_len; 723 sd.len = sd.total_len;
653 724
654 err = actor(pipe, buf, &sd); 725 err = actor(pipe, buf, &sd);
655 if (err) { 726 if (err <= 0) {
656 if (!ret && err != -ENODATA) 727 if (!ret && err != -ENODATA)
657 ret = err; 728 ret = err;
658 729
659 break; 730 break;
660 } 731 }
661 732
662 ret += sd.len; 733 ret += err;
663 buf->offset += sd.len; 734 buf->offset += err;
664 buf->len -= sd.len; 735 buf->len -= err;
736
737 sd.len -= err;
738 sd.pos += err;
739 sd.total_len -= err;
740 if (sd.len)
741 continue;
665 742
666 if (!buf->len) { 743 if (!buf->len) {
667 buf->ops = NULL; 744 buf->ops = NULL;
@@ -672,8 +749,6 @@ static ssize_t move_from_pipe(struct pipe_inode_info *pipe, struct file *out,
672 do_wakeup = 1; 749 do_wakeup = 1;
673 } 750 }
674 751
675 sd.pos += sd.len;
676 sd.total_len -= sd.len;
677 if (!sd.total_len) 752 if (!sd.total_len)
678 break; 753 break;
679 } 754 }
@@ -741,7 +816,7 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
741 struct address_space *mapping = out->f_mapping; 816 struct address_space *mapping = out->f_mapping;
742 ssize_t ret; 817 ssize_t ret;
743 818
744 ret = move_from_pipe(pipe, out, ppos, len, flags, pipe_to_file); 819 ret = splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_file);
745 if (ret > 0) { 820 if (ret > 0) {
746 struct inode *inode = mapping->host; 821 struct inode *inode = mapping->host;
747 822
@@ -783,7 +858,7 @@ EXPORT_SYMBOL(generic_file_splice_write);
783ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, 858ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
784 loff_t *ppos, size_t len, unsigned int flags) 859 loff_t *ppos, size_t len, unsigned int flags)
785{ 860{
786 return move_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); 861 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
787} 862}
788 863
789EXPORT_SYMBOL(generic_splice_sendpage); 864EXPORT_SYMBOL(generic_splice_sendpage);
@@ -870,7 +945,7 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
870 945
871 /* 946 /*
872 * We don't have an immediate reader, but we'll read the stuff 947 * We don't have an immediate reader, but we'll read the stuff
873 * out of the pipe right after the move_to_pipe(). So set 948 * out of the pipe right after the splice_to_pipe(). So set
874 * PIPE_READERS appropriately. 949 * PIPE_READERS appropriately.
875 */ 950 */
876 pipe->readers = 1; 951 pipe->readers = 1;
@@ -1010,6 +1085,184 @@ static long do_splice(struct file *in, loff_t __user *off_in,
1010 return -EINVAL; 1085 return -EINVAL;
1011} 1086}
1012 1087
1088/*
1089 * Map an iov into an array of pages and offset/length tupples. With the
1090 * partial_page structure, we can map several non-contiguous ranges into
1091 * our ones pages[] map instead of splitting that operation into pieces.
1092 * Could easily be exported as a generic helper for other users, in which
1093 * case one would probably want to add a 'max_nr_pages' parameter as well.
1094 */
1095static int get_iovec_page_array(const struct iovec __user *iov,
1096 unsigned int nr_vecs, struct page **pages,
1097 struct partial_page *partial, int aligned)
1098{
1099 int buffers = 0, error = 0;
1100
1101 /*
1102 * It's ok to take the mmap_sem for reading, even
1103 * across a "get_user()".
1104 */
1105 down_read(&current->mm->mmap_sem);
1106
1107 while (nr_vecs) {
1108 unsigned long off, npages;
1109 void __user *base;
1110 size_t len;
1111 int i;
1112
1113 /*
1114 * Get user address base and length for this iovec.
1115 */
1116 error = get_user(base, &iov->iov_base);
1117 if (unlikely(error))
1118 break;
1119 error = get_user(len, &iov->iov_len);
1120 if (unlikely(error))
1121 break;
1122
1123 /*
1124 * Sanity check this iovec. 0 read succeeds.
1125 */
1126 if (unlikely(!len))
1127 break;
1128 error = -EFAULT;
1129 if (unlikely(!base))
1130 break;
1131
1132 /*
1133 * Get this base offset and number of pages, then map
1134 * in the user pages.
1135 */
1136 off = (unsigned long) base & ~PAGE_MASK;
1137
1138 /*
1139 * If asked for alignment, the offset must be zero and the
1140 * length a multiple of the PAGE_SIZE.
1141 */
1142 error = -EINVAL;
1143 if (aligned && (off || len & ~PAGE_MASK))
1144 break;
1145
1146 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1147 if (npages > PIPE_BUFFERS - buffers)
1148 npages = PIPE_BUFFERS - buffers;
1149
1150 error = get_user_pages(current, current->mm,
1151 (unsigned long) base, npages, 0, 0,
1152 &pages[buffers], NULL);
1153
1154 if (unlikely(error <= 0))
1155 break;
1156
1157 /*
1158 * Fill this contiguous range into the partial page map.
1159 */
1160 for (i = 0; i < error; i++) {
1161 const int plen = min_t(size_t, len, PAGE_SIZE - off);
1162
1163 partial[buffers].offset = off;
1164 partial[buffers].len = plen;
1165
1166 off = 0;
1167 len -= plen;
1168 buffers++;
1169 }
1170
1171 /*
1172 * We didn't complete this iov, stop here since it probably
1173 * means we have to move some of this into a pipe to
1174 * be able to continue.
1175 */
1176 if (len)
1177 break;
1178
1179 /*
1180 * Don't continue if we mapped fewer pages than we asked for,
1181 * or if we mapped the max number of pages that we have
1182 * room for.
1183 */
1184 if (error < npages || buffers == PIPE_BUFFERS)
1185 break;
1186
1187 nr_vecs--;
1188 iov++;
1189 }
1190
1191 up_read(&current->mm->mmap_sem);
1192
1193 if (buffers)
1194 return buffers;
1195
1196 return error;
1197}
1198
1199/*
1200 * vmsplice splices a user address range into a pipe. It can be thought of
1201 * as splice-from-memory, where the regular splice is splice-from-file (or
1202 * to file). In both cases the output is a pipe, naturally.
1203 *
1204 * Note that vmsplice only supports splicing _from_ user memory to a pipe,
1205 * not the other way around. Splicing from user memory is a simple operation
1206 * that can be supported without any funky alignment restrictions or nasty
1207 * vm tricks. We simply map in the user memory and fill them into a pipe.
1208 * The reverse isn't quite as easy, though. There are two possible solutions
1209 * for that:
1210 *
1211 * - memcpy() the data internally, at which point we might as well just
1212 * do a regular read() on the buffer anyway.
1213 * - Lots of nasty vm tricks, that are neither fast nor flexible (it
1214 * has restriction limitations on both ends of the pipe).
1215 *
1216 * Alas, it isn't here.
1217 *
1218 */
1219static long do_vmsplice(struct file *file, const struct iovec __user *iov,
1220 unsigned long nr_segs, unsigned int flags)
1221{
1222 struct pipe_inode_info *pipe = file->f_dentry->d_inode->i_pipe;
1223 struct page *pages[PIPE_BUFFERS];
1224 struct partial_page partial[PIPE_BUFFERS];
1225 struct splice_pipe_desc spd = {
1226 .pages = pages,
1227 .partial = partial,
1228 .flags = flags,
1229 .ops = &user_page_pipe_buf_ops,
1230 };
1231
1232 if (unlikely(!pipe))
1233 return -EBADF;
1234 if (unlikely(nr_segs > UIO_MAXIOV))
1235 return -EINVAL;
1236 else if (unlikely(!nr_segs))
1237 return 0;
1238
1239 spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial,
1240 flags & SPLICE_F_GIFT);
1241 if (spd.nr_pages <= 0)
1242 return spd.nr_pages;
1243
1244 return splice_to_pipe(pipe, &spd);
1245}
1246
1247asmlinkage long sys_vmsplice(int fd, const struct iovec __user *iov,
1248 unsigned long nr_segs, unsigned int flags)
1249{
1250 struct file *file;
1251 long error;
1252 int fput;
1253
1254 error = -EBADF;
1255 file = fget_light(fd, &fput);
1256 if (file) {
1257 if (file->f_mode & FMODE_WRITE)
1258 error = do_vmsplice(file, iov, nr_segs, flags);
1259
1260 fput_light(file, fput);
1261 }
1262
1263 return error;
1264}
1265
1013asmlinkage long sys_splice(int fd_in, loff_t __user *off_in, 1266asmlinkage long sys_splice(int fd_in, loff_t __user *off_in,
1014 int fd_out, loff_t __user *off_out, 1267 int fd_out, loff_t __user *off_out,
1015 size_t len, unsigned int flags) 1268 size_t len, unsigned int flags)
@@ -1092,6 +1345,12 @@ static int link_pipe(struct pipe_inode_info *ipipe,
1092 obuf = opipe->bufs + nbuf; 1345 obuf = opipe->bufs + nbuf;
1093 *obuf = *ibuf; 1346 *obuf = *ibuf;
1094 1347
1348 /*
1349 * Don't inherit the gift flag, we need to
1350 * prevent multiple steals of this page.
1351 */
1352 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1353
1095 if (obuf->len > len) 1354 if (obuf->len > len)
1096 obuf->len = len; 1355 obuf->len = len;
1097 1356