[PATCH] splice: improve writeback and clean up page stealing
[linux-2.6.git] / fs / splice.c
1 /*
2  * "splice": joining two ropes together by interweaving their strands.
3  *
4  * This is the "extended pipe" functionality, where a pipe is used as
5  * an arbitrary in-memory buffer. Think of a pipe as a small kernel
6  * buffer that you can use to transfer data from one end to the other.
7  *
8  * The traditional unix read/write is extended with a "splice()" operation
9  * that transfers data buffers to or from a pipe buffer.
10  *
11  * Named by Larry McVoy, original implementation from Linus, extended by
12  * Jens to support splicing to files and fixing the initial implementation
13  * bugs.
14  *
15  * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
16  * Copyright (C) 2005 Linus Torvalds <torvalds@osdl.org>
17  *
18  */
19 #include <linux/fs.h>
20 #include <linux/file.h>
21 #include <linux/pagemap.h>
22 #include <linux/pipe_fs_i.h>
23 #include <linux/mm_inline.h>
24 #include <linux/swap.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h>
27 #include <linux/module.h>
28 #include <linux/syscalls.h>
29
30 /*
31  * Passed to the actors
32  */
33 struct splice_desc {
34         unsigned int len, total_len;    /* current and remaining length */
35         unsigned int flags;             /* splice flags */
36         struct file *file;              /* file to read/write */
37         loff_t pos;                     /* file position */
38 };
39
40 static int page_cache_pipe_buf_steal(struct pipe_inode_info *info,
41                                      struct pipe_buffer *buf)
42 {
43         struct page *page = buf->page;
44         struct address_space *mapping = page_mapping(page);
45
46         WARN_ON(!PageLocked(page));
47         WARN_ON(!PageUptodate(page));
48
49         if (PagePrivate(page))
50                 try_to_release_page(page, mapping_gfp_mask(mapping));
51
52         if (!remove_mapping(mapping, page))
53                 return 1;
54
55         if (PageLRU(page)) {
56                 struct zone *zone = page_zone(page);
57
58                 spin_lock_irq(&zone->lru_lock);
59                 BUG_ON(!PageLRU(page));
60                 __ClearPageLRU(page);
61                 del_page_from_lru(zone, page);
62                 spin_unlock_irq(&zone->lru_lock);
63         }
64
65         return 0;
66 }
67
68 static void page_cache_pipe_buf_release(struct pipe_inode_info *info,
69                                         struct pipe_buffer *buf)
70 {
71         page_cache_release(buf->page);
72         buf->page = NULL;
73 }
74
75 static void *page_cache_pipe_buf_map(struct file *file,
76                                      struct pipe_inode_info *info,
77                                      struct pipe_buffer *buf)
78 {
79         struct page *page = buf->page;
80
81         lock_page(page);
82
83         if (!PageUptodate(page)) {
84                 unlock_page(page);
85                 return ERR_PTR(-EIO);
86         }
87
88         if (!page->mapping) {
89                 unlock_page(page);
90                 return ERR_PTR(-ENODATA);
91         }
92
93         return kmap(buf->page);
94 }
95
96 static void page_cache_pipe_buf_unmap(struct pipe_inode_info *info,
97                                       struct pipe_buffer *buf)
98 {
99         unlock_page(buf->page);
100         kunmap(buf->page);
101 }
102
103 static struct pipe_buf_operations page_cache_pipe_buf_ops = {
104         .can_merge = 0,
105         .map = page_cache_pipe_buf_map,
106         .unmap = page_cache_pipe_buf_unmap,
107         .release = page_cache_pipe_buf_release,
108         .steal = page_cache_pipe_buf_steal,
109 };
110
111 static ssize_t move_to_pipe(struct inode *inode, struct page **pages,
112                             int nr_pages, unsigned long offset,
113                             unsigned long len, unsigned int flags)
114 {
115         struct pipe_inode_info *info;
116         int ret, do_wakeup, i;
117
118         ret = 0;
119         do_wakeup = 0;
120         i = 0;
121
122         mutex_lock(PIPE_MUTEX(*inode));
123
124         info = inode->i_pipe;
125         for (;;) {
126                 int bufs;
127
128                 if (!PIPE_READERS(*inode)) {
129                         send_sig(SIGPIPE, current, 0);
130                         if (!ret)
131                                 ret = -EPIPE;
132                         break;
133                 }
134
135                 bufs = info->nrbufs;
136                 if (bufs < PIPE_BUFFERS) {
137                         int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS - 1);
138                         struct pipe_buffer *buf = info->bufs + newbuf;
139                         struct page *page = pages[i++];
140                         unsigned long this_len;
141
142                         this_len = PAGE_CACHE_SIZE - offset;
143                         if (this_len > len)
144                                 this_len = len;
145
146                         buf->page = page;
147                         buf->offset = offset;
148                         buf->len = this_len;
149                         buf->ops = &page_cache_pipe_buf_ops;
150                         info->nrbufs = ++bufs;
151                         do_wakeup = 1;
152
153                         ret += this_len;
154                         len -= this_len;
155                         offset = 0;
156                         if (!--nr_pages)
157                                 break;
158                         if (!len)
159                                 break;
160                         if (bufs < PIPE_BUFFERS)
161                                 continue;
162
163                         break;
164                 }
165
166                 if (flags & SPLICE_F_NONBLOCK) {
167                         if (!ret)
168                                 ret = -EAGAIN;
169                         break;
170                 }
171
172                 if (signal_pending(current)) {
173                         if (!ret)
174                                 ret = -ERESTARTSYS;
175                         break;
176                 }
177
178                 if (do_wakeup) {
179                         wake_up_interruptible_sync(PIPE_WAIT(*inode));
180                         kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO,
181                                     POLL_IN);
182                         do_wakeup = 0;
183                 }
184
185                 PIPE_WAITING_WRITERS(*inode)++;
186                 pipe_wait(inode);
187                 PIPE_WAITING_WRITERS(*inode)--;
188         }
189
190         mutex_unlock(PIPE_MUTEX(*inode));
191
192         if (do_wakeup) {
193                 wake_up_interruptible(PIPE_WAIT(*inode));
194                 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
195         }
196
197         while (i < nr_pages)
198                 page_cache_release(pages[i++]);
199
200         return ret;
201 }
202
203 static int __generic_file_splice_read(struct file *in, struct inode *pipe,
204                                       size_t len, unsigned int flags)
205 {
206         struct address_space *mapping = in->f_mapping;
207         unsigned int offset, nr_pages;
208         struct page *pages[PIPE_BUFFERS], *shadow[PIPE_BUFFERS];
209         struct page *page;
210         pgoff_t index, pidx;
211         int i, j;
212
213         index = in->f_pos >> PAGE_CACHE_SHIFT;
214         offset = in->f_pos & ~PAGE_CACHE_MASK;
215         nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
216
217         if (nr_pages > PIPE_BUFFERS)
218                 nr_pages = PIPE_BUFFERS;
219
220         /*
221          * initiate read-ahead on this page range
222          */
223         do_page_cache_readahead(mapping, in, index, nr_pages);
224
225         /*
226          * Get as many pages from the page cache as possible..
227          * Start IO on the page cache entries we create (we
228          * can assume that any pre-existing ones we find have
229          * already had IO started on them).
230          */
231         i = find_get_pages(mapping, index, nr_pages, pages);
232
233         /*
234          * common case - we found all pages and they are contiguous,
235          * kick them off
236          */
237         if (i && (pages[i - 1]->index == index + i - 1))
238                 goto splice_them;
239
240         /*
241          * fill shadow[] with pages at the right locations, so we only
242          * have to fill holes
243          */
244         memset(shadow, 0, nr_pages * sizeof(struct page *));
245         for (j = 0; j < i; j++)
246                 shadow[pages[j]->index - index] = pages[j];
247
248         /*
249          * now fill in the holes
250          */
251         for (i = 0, pidx = index; i < nr_pages; pidx++, i++) {
252                 int error;
253
254                 if (shadow[i])
255                         continue;
256
257                 /*
258                  * no page there, look one up / create it
259                  */
260                 page = find_or_create_page(mapping, pidx,
261                                                    mapping_gfp_mask(mapping));
262                 if (!page)
263                         break;
264
265                 if (PageUptodate(page))
266                         unlock_page(page);
267                 else {
268                         error = mapping->a_ops->readpage(in, page);
269
270                         if (unlikely(error)) {
271                                 page_cache_release(page);
272                                 break;
273                         }
274                 }
275                 shadow[i] = page;
276         }
277
278         if (!i) {
279                 for (i = 0; i < nr_pages; i++) {
280                          if (shadow[i])
281                                 page_cache_release(shadow[i]);
282                 }
283                 return 0;
284         }
285
286         memcpy(pages, shadow, i * sizeof(struct page *));
287
288         /*
289          * Now we splice them into the pipe..
290          */
291 splice_them:
292         return move_to_pipe(pipe, pages, i, offset, len, flags);
293 }
294
295 ssize_t generic_file_splice_read(struct file *in, struct inode *pipe,
296                                  size_t len, unsigned int flags)
297 {
298         ssize_t spliced;
299         int ret;
300
301         ret = 0;
302         spliced = 0;
303         while (len) {
304                 ret = __generic_file_splice_read(in, pipe, len, flags);
305
306                 if (ret <= 0)
307                         break;
308
309                 in->f_pos += ret;
310                 len -= ret;
311                 spliced += ret;
312
313                 if (!(flags & SPLICE_F_NONBLOCK))
314                         continue;
315                 ret = -EAGAIN;
316                 break;
317         }
318
319         if (spliced)
320                 return spliced;
321
322         return ret;
323 }
324
325 /*
326  * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
327  * using sendpage().
328  */
329 static int pipe_to_sendpage(struct pipe_inode_info *info,
330                             struct pipe_buffer *buf, struct splice_desc *sd)
331 {
332         struct file *file = sd->file;
333         loff_t pos = sd->pos;
334         unsigned int offset;
335         ssize_t ret;
336         void *ptr;
337
338         /*
339          * sub-optimal, but we are limited by the pipe ->map. we don't
340          * need a kmap'ed buffer here, we just want to make sure we
341          * have the page pinned if the pipe page originates from the
342          * page cache
343          */
344         ptr = buf->ops->map(file, info, buf);
345         if (IS_ERR(ptr))
346                 return PTR_ERR(ptr);
347
348         offset = pos & ~PAGE_CACHE_MASK;
349
350         ret = file->f_op->sendpage(file, buf->page, offset, sd->len, &pos,
351                                         sd->len < sd->total_len);
352
353         buf->ops->unmap(info, buf);
354         if (ret == sd->len)
355                 return 0;
356
357         return -EIO;
358 }
359
360 /*
361  * This is a little more tricky than the file -> pipe splicing. There are
362  * basically three cases:
363  *
364  *      - Destination page already exists in the address space and there
365  *        are users of it. For that case we have no other option that
366  *        copying the data. Tough luck.
367  *      - Destination page already exists in the address space, but there
368  *        are no users of it. Make sure it's uptodate, then drop it. Fall
369  *        through to last case.
370  *      - Destination page does not exist, we can add the pipe page to
371  *        the page cache and avoid the copy.
372  *
373  * For now we just do the slower thing and always copy pages over, it's
374  * easier than migrating pages from the pipe to the target file. For the
375  * case of doing file | file splicing, the migrate approach had some LRU
376  * nastiness...
377  */
378 static int pipe_to_file(struct pipe_inode_info *info, struct pipe_buffer *buf,
379                         struct splice_desc *sd)
380 {
381         struct file *file = sd->file;
382         struct address_space *mapping = file->f_mapping;
383         unsigned int offset;
384         struct page *page;
385         pgoff_t index;
386         char *src;
387         int ret, stolen;
388
389         /*
390          * after this, page will be locked and unmapped
391          */
392         src = buf->ops->map(file, info, buf);
393         if (IS_ERR(src))
394                 return PTR_ERR(src);
395
396         index = sd->pos >> PAGE_CACHE_SHIFT;
397         offset = sd->pos & ~PAGE_CACHE_MASK;
398         stolen = 0;
399
400         /*
401          * reuse buf page, if SPLICE_F_MOVE is set
402          */
403         if (sd->flags & SPLICE_F_MOVE) {
404                 if (buf->ops->steal(info, buf))
405                         goto find_page;
406
407                 page = buf->page;
408                 stolen = 1;
409                 if (add_to_page_cache_lru(page, mapping, index,
410                                                 mapping_gfp_mask(mapping)))
411                         goto find_page;
412         } else {
413 find_page:
414                 ret = -ENOMEM;
415                 page = find_or_create_page(mapping, index,
416                                                 mapping_gfp_mask(mapping));
417                 if (!page)
418                         goto out;
419
420                 /*
421                  * If the page is uptodate, it is also locked. If it isn't
422                  * uptodate, we can mark it uptodate if we are filling the
423                  * full page. Otherwise we need to read it in first...
424                  */
425                 if (!PageUptodate(page)) {
426                         if (sd->len < PAGE_CACHE_SIZE) {
427                                 ret = mapping->a_ops->readpage(file, page);
428                                 if (unlikely(ret))
429                                         goto out;
430
431                                 lock_page(page);
432
433                                 if (!PageUptodate(page)) {
434                                         /*
435                                          * page got invalidated, repeat
436                                          */
437                                         if (!page->mapping) {
438                                                 unlock_page(page);
439                                                 page_cache_release(page);
440                                                 goto find_page;
441                                         }
442                                         ret = -EIO;
443                                         goto out;
444                                 }
445                         } else {
446                                 WARN_ON(!PageLocked(page));
447                                 SetPageUptodate(page);
448                         }
449                 }
450         }
451
452         ret = mapping->a_ops->prepare_write(file, page, 0, sd->len);
453         if (ret == AOP_TRUNCATED_PAGE) {
454                 page_cache_release(page);
455                 goto find_page;
456         } else if (ret)
457                 goto out;
458
459         if (!stolen) {
460                 char *dst = kmap_atomic(page, KM_USER0);
461
462                 memcpy(dst + offset, src + buf->offset, sd->len);
463                 flush_dcache_page(page);
464                 kunmap_atomic(dst, KM_USER0);
465         }
466
467         ret = mapping->a_ops->commit_write(file, page, 0, sd->len);
468         if (ret == AOP_TRUNCATED_PAGE) {
469                 page_cache_release(page);
470                 goto find_page;
471         } else if (ret)
472                 goto out;
473
474         balance_dirty_pages_ratelimited(mapping);
475 out:
476         if (!stolen) {
477                 page_cache_release(page);
478                 unlock_page(page);
479         }
480         buf->ops->unmap(info, buf);
481         return ret;
482 }
483
484 typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *,
485                            struct splice_desc *);
486
487 static ssize_t move_from_pipe(struct inode *inode, struct file *out,
488                               size_t len, unsigned int flags,
489                               splice_actor *actor)
490 {
491         struct pipe_inode_info *info;
492         int ret, do_wakeup, err;
493         struct splice_desc sd;
494
495         ret = 0;
496         do_wakeup = 0;
497
498         sd.total_len = len;
499         sd.flags = flags;
500         sd.file = out;
501         sd.pos = out->f_pos;
502
503         mutex_lock(PIPE_MUTEX(*inode));
504
505         info = inode->i_pipe;
506         for (;;) {
507                 int bufs = info->nrbufs;
508
509                 if (bufs) {
510                         int curbuf = info->curbuf;
511                         struct pipe_buffer *buf = info->bufs + curbuf;
512                         struct pipe_buf_operations *ops = buf->ops;
513
514                         sd.len = buf->len;
515                         if (sd.len > sd.total_len)
516                                 sd.len = sd.total_len;
517
518                         err = actor(info, buf, &sd);
519                         if (err) {
520                                 if (!ret && err != -ENODATA)
521                                         ret = err;
522
523                                 break;
524                         }
525
526                         ret += sd.len;
527                         buf->offset += sd.len;
528                         buf->len -= sd.len;
529                         if (!buf->len) {
530                                 buf->ops = NULL;
531                                 ops->release(info, buf);
532                                 curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1);
533                                 info->curbuf = curbuf;
534                                 info->nrbufs = --bufs;
535                                 do_wakeup = 1;
536                         }
537
538                         sd.pos += sd.len;
539                         sd.total_len -= sd.len;
540                         if (!sd.total_len)
541                                 break;
542                 }
543
544                 if (bufs)
545                         continue;
546                 if (!PIPE_WRITERS(*inode))
547                         break;
548                 if (!PIPE_WAITING_WRITERS(*inode)) {
549                         if (ret)
550                                 break;
551                 }
552
553                 if (flags & SPLICE_F_NONBLOCK) {
554                         if (!ret)
555                                 ret = -EAGAIN;
556                         break;
557                 }
558
559                 if (signal_pending(current)) {
560                         if (!ret)
561                                 ret = -ERESTARTSYS;
562                         break;
563                 }
564
565                 if (do_wakeup) {
566                         wake_up_interruptible_sync(PIPE_WAIT(*inode));
567                         kill_fasync(PIPE_FASYNC_WRITERS(*inode),SIGIO,POLL_OUT);
568                         do_wakeup = 0;
569                 }
570
571                 pipe_wait(inode);
572         }
573
574         mutex_unlock(PIPE_MUTEX(*inode));
575
576         if (do_wakeup) {
577                 wake_up_interruptible(PIPE_WAIT(*inode));
578                 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
579         }
580
581         mutex_lock(&out->f_mapping->host->i_mutex);
582         out->f_pos = sd.pos;
583         mutex_unlock(&out->f_mapping->host->i_mutex);
584         return ret;
585
586 }
587
588 ssize_t generic_file_splice_write(struct inode *inode, struct file *out,
589                                   size_t len, unsigned int flags)
590 {
591         struct address_space *mapping = out->f_mapping;
592         ssize_t ret = move_from_pipe(inode, out, len, flags, pipe_to_file);
593
594         /*
595          * if file or inode is SYNC and we actually wrote some data, sync it
596          */
597         if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(mapping->host))
598             && ret > 0) {
599                 struct inode *inode = mapping->host;
600                 int err;
601
602                 mutex_lock(&inode->i_mutex);
603                 err = generic_osync_inode(mapping->host, mapping,
604                                                 OSYNC_METADATA|OSYNC_DATA);
605                 mutex_unlock(&inode->i_mutex);
606
607                 if (err)
608                         ret = err;
609         }
610
611         return ret;
612 }
613
614 ssize_t generic_splice_sendpage(struct inode *inode, struct file *out,
615                                 size_t len, unsigned int flags)
616 {
617         return move_from_pipe(inode, out, len, flags, pipe_to_sendpage);
618 }
619
620 EXPORT_SYMBOL(generic_file_splice_write);
621 EXPORT_SYMBOL(generic_file_splice_read);
622
623 static long do_splice_from(struct inode *pipe, struct file *out, size_t len,
624                            unsigned int flags)
625 {
626         loff_t pos;
627         int ret;
628
629         if (!out->f_op || !out->f_op->splice_write)
630                 return -EINVAL;
631
632         if (!(out->f_mode & FMODE_WRITE))
633                 return -EBADF;
634
635         pos = out->f_pos;
636         ret = rw_verify_area(WRITE, out, &pos, len);
637         if (unlikely(ret < 0))
638                 return ret;
639
640         return out->f_op->splice_write(pipe, out, len, flags);
641 }
642
643 static long do_splice_to(struct file *in, struct inode *pipe, size_t len,
644                          unsigned int flags)
645 {
646         loff_t pos, isize, left;
647         int ret;
648
649         if (!in->f_op || !in->f_op->splice_read)
650                 return -EINVAL;
651
652         if (!(in->f_mode & FMODE_READ))
653                 return -EBADF;
654
655         pos = in->f_pos;
656         ret = rw_verify_area(READ, in, &pos, len);
657         if (unlikely(ret < 0))
658                 return ret;
659
660         isize = i_size_read(in->f_mapping->host);
661         if (unlikely(in->f_pos >= isize))
662                 return 0;
663         
664         left = isize - in->f_pos;
665         if (left < len)
666                 len = left;
667
668         return in->f_op->splice_read(in, pipe, len, flags);
669 }
670
671 static long do_splice(struct file *in, struct file *out, size_t len,
672                       unsigned int flags)
673 {
674         struct inode *pipe;
675
676         pipe = in->f_dentry->d_inode;
677         if (pipe->i_pipe)
678                 return do_splice_from(pipe, out, len, flags);
679
680         pipe = out->f_dentry->d_inode;
681         if (pipe->i_pipe)
682                 return do_splice_to(in, pipe, len, flags);
683
684         return -EINVAL;
685 }
686
687 asmlinkage long sys_splice(int fdin, int fdout, size_t len, unsigned int flags)
688 {
689         long error;
690         struct file *in, *out;
691         int fput_in, fput_out;
692
693         if (unlikely(!len))
694                 return 0;
695
696         error = -EBADF;
697         in = fget_light(fdin, &fput_in);
698         if (in) {
699                 if (in->f_mode & FMODE_READ) {
700                         out = fget_light(fdout, &fput_out);
701                         if (out) {
702                                 if (out->f_mode & FMODE_WRITE)
703                                         error = do_splice(in, out, len, flags);
704                                 fput_light(out, fput_out);
705                         }
706                 }
707
708                 fput_light(in, fput_in);
709         }
710
711         return error;
712 }