/* * "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 * Jens to support splicing to files, network, direct splicing, etc and * fixing lots of bugs. * * Copyright (C) 2005-2006 Jens Axboe * Copyright (C) 2005-2006 Linus Torvalds * Copyright (C) 2006 Ingo Molnar * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define DEBUG_SPLICE 1 //#define DEBUG_SPLICE_DMA 1 #define SPLICE_MAGIC 0xDEADBEEF #ifdef CONFIG_SPLICE_DMA static DEFINE_MUTEX(splice_dma_lock); extern int splice_dma_memcpy(struct splice_dma_desc *sd_p, unsigned int len); extern int ppc460ex_sgdma_pipebufs_memcpy(struct pipe_inode_info *pipe, void *dest, unsigned int length); #endif #ifdef SPLICE_DEFER_DMA #define MAX_SPLICE_WRITES 4 #define MAX_SPLICE_PIPES (MAX_SPLICE_WRITES + 1) static struct splice_dma_desc_defer* splice_write_queue[MAX_SPLICE_WRITES]; static int splice_queue_ptr = 0; static int splice_queue_count = 0; static struct splice_pipe_defer* splice_write_pipes[MAX_SPLICE_PIPES]; static void put_splice_pipe(struct pipe_inode_info *pipe); #endif //#define SPLICE_NO_IO 1 /* * 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. */ static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { struct page *page = buf->page; struct address_space *mapping; lock_page(page); mapping = page_mapping(page); if (mapping) { WARN_ON(!PageUptodate(page)); /* * 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); if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) goto out_unlock; /* * If we succeeded in removing the mapping, set LRU flag * and return good. */ if (remove_mapping(mapping, page)) { buf->flags |= PIPE_BUF_FLAG_LRU; return 0; } } /* * Raced with truncate or failed to remove page from current * address space, unlock and return failure. */ out_unlock: unlock_page(page); return 1; } static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { page_cache_release(buf->page); buf->flags &= ~PIPE_BUF_FLAG_LRU; } /* * Check whether the contents of buf is OK to access. Since the content * is a page cache page, IO may be in flight. */ static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { struct page *page = buf->page; int err; if (!PageUptodate(page)) { lock_page(page); /* * Page got truncated/unhashed. This will cause a 0-byte * splice, if this is the first page. */ if (!page->mapping) { err = -ENODATA; goto error; } /* * Uh oh, read-error from disk. */ if (!PageUptodate(page)) { err = -EIO; goto error; } /* * Page is ok afterall, we are done. */ unlock_page(page); } return 0; error: unlock_page(page); return err; } static const struct pipe_buf_operations page_cache_pipe_buf_ops = { .can_merge = 0, .map = generic_pipe_buf_map, .unmap = generic_pipe_buf_unmap, .confirm = page_cache_pipe_buf_confirm, .release = page_cache_pipe_buf_release, .steal = page_cache_pipe_buf_steal, .get = generic_pipe_buf_get, }; static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { if (!(buf->flags & PIPE_BUF_FLAG_GIFT)) return 1; buf->flags |= PIPE_BUF_FLAG_LRU; return generic_pipe_buf_steal(pipe, buf); } static const struct pipe_buf_operations user_page_pipe_buf_ops = { .can_merge = 0, .map = generic_pipe_buf_map, .unmap = generic_pipe_buf_unmap, .confirm = generic_pipe_buf_confirm, .release = page_cache_pipe_buf_release, .steal = user_page_pipe_buf_steal, .get = generic_pipe_buf_get, }; /** * splice_to_pipe - fill passed data into a pipe * @pipe: pipe to fill * @spd: data to fill * * Description: * @spd contains a map of pages and len/offset tuples, along with * the struct pipe_buf_operations associated with these pages. This * function will link that data to the pipe. * */ ssize_t splice_to_pipe(struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) { unsigned int spd_pages = spd->nr_pages; int ret, do_wakeup, page_nr; ret = 0; do_wakeup = 0; page_nr = 0; pipe_lock(pipe); for (;;) { if (!pipe->readers) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; printk("%s:%d No readers breaking\n", __FUNCTION__, __LINE__); } //printk("pipe->nrbufs %d page_nr %d spd->nr_pages %d \n", pipe->nrbufs, page_nr, spd->nr_pages); if (pipe->nrbufs < PIPE_BUFFERS) { int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); struct pipe_buffer *buf = pipe->bufs + newbuf; 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; if (spd->flags & SPLICE_F_GIFT) buf->flags |= PIPE_BUF_FLAG_GIFT; pipe->nrbufs++; page_nr++; ret += buf->len; if (pipe->inode) do_wakeup = 1; if (!--spd->nr_pages) break; if (pipe->nrbufs < PIPE_BUFFERS) continue; break; } if (spd->flags & SPLICE_F_NONBLOCK) { if (!ret) ret = -EAGAIN; break; } if (signal_pending(current)) { if (!ret) ret = -ERESTARTSYS; break; } if (do_wakeup) { smp_mb(); if (waitqueue_active(&pipe->wait)) wake_up_interruptible_sync(&pipe->wait); kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); do_wakeup = 0; } pipe->waiting_writers++; pipe_wait(pipe); pipe->waiting_writers--; } pipe_unlock(pipe); if (do_wakeup) { smp_mb(); if (waitqueue_active(&pipe->wait)) wake_up_interruptible(&pipe->wait); kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); } while (page_nr < spd_pages) spd->spd_release(spd, page_nr++); return ret; } static void spd_release_page(struct splice_pipe_desc *spd, unsigned int i) { page_cache_release(spd->pages[i]); } static int __generic_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { struct address_space *mapping = in->f_mapping; unsigned int loff, nr_pages, req_pages; struct page *pages[PIPE_BUFFERS]; struct partial_page partial[PIPE_BUFFERS]; struct page *page; pgoff_t index, end_index; loff_t isize; int error, page_nr; struct splice_pipe_desc spd = { .pages = pages, .partial = partial, .flags = flags, .ops = &page_cache_pipe_buf_ops, .spd_release = spd_release_page, }; index = *ppos >> PAGE_CACHE_SHIFT; loff = *ppos & ~PAGE_CACHE_MASK; req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; nr_pages = min(req_pages, (unsigned)PIPE_BUFFERS); /* * Lookup the (hopefully) full range of pages we need. */ spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, pages); index += spd.nr_pages; /* * If find_get_pages_contig() returned fewer pages than we needed, * readahead/allocate the rest and fill in the holes. */ if (spd.nr_pages < nr_pages) page_cache_sync_readahead(mapping, &in->f_ra, in, index, req_pages - spd.nr_pages); error = 0; while (spd.nr_pages < nr_pages) { /* * Page could be there, find_get_pages_contig() breaks on * the first hole. */ page = find_get_page(mapping, index); if (!page) { /* * page didn't exist, allocate one. */ page = page_cache_alloc_cold(mapping); if (!page) break; error = add_to_page_cache_lru(page, mapping, index, mapping_gfp_mask(mapping)); if (unlikely(error)) { page_cache_release(page); if (error == -EEXIST) continue; break; } /* * add_to_page_cache() locks the page, unlock it * to avoid convoluting the logic below even more. */ unlock_page(page); } pages[spd.nr_pages++] = page; index++; } /* * Now loop over the map and see if we need to start IO on any * pages, fill in the partial map, etc. */ index = *ppos >> PAGE_CACHE_SHIFT; nr_pages = spd.nr_pages; spd.nr_pages = 0; for (page_nr = 0; page_nr < nr_pages; page_nr++) { unsigned int this_len; if (!len) break; /* * this_len is the max we'll use from this page */ this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); page = pages[page_nr]; if (PageReadahead(page)) page_cache_async_readahead(mapping, &in->f_ra, in, page, index, req_pages - page_nr); /* * If the page isn't uptodate, we may need to start io on it */ if (!PageUptodate(page)) { /* * If in nonblock mode then dont block on waiting * for an in-flight io page */ if (flags & SPLICE_F_NONBLOCK) { if (!trylock_page(page)) { error = -EAGAIN; break; } } else lock_page(page); /* * Page was truncated, or invalidated by the * filesystem. Redo the find/create, but this time the * page is kept locked, so there's no chance of another * race with truncate/invalidate. */ if (!page->mapping) { unlock_page(page); page = find_or_create_page(mapping, index, mapping_gfp_mask(mapping)); if (!page) { error = -ENOMEM; break; } page_cache_release(pages[page_nr]); pages[page_nr] = page; } /* * page was already under io and is now done, great */ if (PageUptodate(page)) { unlock_page(page); goto fill_it; } /* * need to read in the page */ error = mapping->a_ops->readpage(in, page); if (unlikely(error)) { /* * We really should re-lookup the page here, * but it complicates things a lot. Instead * lets just do what we already stored, and * we'll get it the next time we are called. */ if (error == AOP_TRUNCATED_PAGE) error = 0; break; } } fill_it: /* * i_size must be checked after PageUptodate. */ isize = i_size_read(mapping->host); end_index = (isize - 1) >> PAGE_CACHE_SHIFT; if (unlikely(!isize || index > end_index)) break; /* * if this is the last page, see if we need to shrink * the length and stop */ if (end_index == index) { unsigned int plen; /* * max good bytes in this page */ plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; if (plen <= loff) break; /* * force quit after adding this page */ this_len = min(this_len, plen - loff); len = this_len; } partial[page_nr].offset = loff; partial[page_nr].len = this_len; len -= this_len; loff = 0; spd.nr_pages++; index++; } /* * Release any pages at the end, if we quit early. 'page_nr' is how far * we got, 'nr_pages' is how many pages are in the map. */ while (page_nr < nr_pages) page_cache_release(pages[page_nr++]); in->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT; if (spd.nr_pages) return splice_to_pipe(pipe, &spd); return error; } /** * generic_file_splice_read - splice data from file to a pipe * @in: file to splice from * @ppos: position in @in * @pipe: pipe to splice to * @len: number of bytes to splice * @flags: splice modifier flags * * Description: * Will read pages from given file and fill them into a pipe. Can be * used as long as the address_space operations for the source implements * a readpage() hook. * */ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { loff_t isize, left; int ret; isize = i_size_read(in->f_mapping->host); if (unlikely(*ppos >= isize)) return 0; left = isize - *ppos; if (unlikely(left < len)) len = left; ret = __generic_file_splice_read(in, ppos, pipe, len, flags); if (ret > 0) { *ppos += ret; file_accessed(in); } return ret; } EXPORT_SYMBOL(generic_file_splice_read); static const struct pipe_buf_operations default_pipe_buf_ops = { .can_merge = 0, .map = generic_pipe_buf_map, .unmap = generic_pipe_buf_unmap, .confirm = generic_pipe_buf_confirm, .release = generic_pipe_buf_release, .steal = generic_pipe_buf_steal, .get = generic_pipe_buf_get, }; static ssize_t kernel_readv(struct file *file, const struct iovec *vec, unsigned long vlen, loff_t offset) { mm_segment_t old_fs; loff_t pos = offset; ssize_t res; old_fs = get_fs(); set_fs(get_ds()); /* The cast to a user pointer is valid due to the set_fs() */ res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos); set_fs(old_fs); return res; } static ssize_t kernel_write(struct file *file, const char *buf, size_t count, loff_t pos) { mm_segment_t old_fs; ssize_t res; old_fs = get_fs(); set_fs(get_ds()); /* The cast to a user pointer is valid due to the set_fs() */ res = vfs_write(file, (const char __user *)buf, count, &pos); set_fs(old_fs); return res; } ssize_t default_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { unsigned int nr_pages; unsigned int nr_freed; size_t offset; struct page *pages[PIPE_BUFFERS]; struct partial_page partial[PIPE_BUFFERS]; struct iovec vec[PIPE_BUFFERS]; pgoff_t index; ssize_t res; size_t this_len; int error; int i; struct splice_pipe_desc spd = { .pages = pages, .partial = partial, .flags = flags, .ops = &default_pipe_buf_ops, .spd_release = spd_release_page, }; index = *ppos >> PAGE_CACHE_SHIFT; offset = *ppos & ~PAGE_CACHE_MASK; nr_pages = (len + offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; for (i = 0; i < nr_pages && i < PIPE_BUFFERS && len; i++) { struct page *page; page = alloc_page(GFP_USER); error = -ENOMEM; if (!page) goto err; this_len = min_t(size_t, len, PAGE_CACHE_SIZE - offset); vec[i].iov_base = (void __user *) page_address(page); vec[i].iov_len = this_len; pages[i] = page; spd.nr_pages++; len -= this_len; offset = 0; } res = kernel_readv(in, vec, spd.nr_pages, *ppos); if (res < 0) { error = res; goto err; } error = 0; if (!res) goto err; nr_freed = 0; for (i = 0; i < spd.nr_pages; i++) { this_len = min_t(size_t, vec[i].iov_len, res); partial[i].offset = 0; partial[i].len = this_len; if (!this_len) { __free_page(pages[i]); pages[i] = NULL; nr_freed++; } res -= this_len; } spd.nr_pages -= nr_freed; res = splice_to_pipe(pipe, &spd); if (res > 0) *ppos += res; return res; err: for (i = 0; i < spd.nr_pages; i++) __free_page(pages[i]); return error; } EXPORT_SYMBOL(default_file_splice_read); /* * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' * using sendpage(). Return the number of bytes sent. */ static int pipe_to_sendpage(struct pipe_inode_info *pipe, struct pipe_buffer *buf, struct splice_desc *sd) { struct file *file = sd->u.file; loff_t pos = sd->pos; int ret, more; ret = buf->ops->confirm(pipe, buf); if (!ret) { more = (sd->flags & SPLICE_F_MORE) || sd->len < sd->total_len; ret = file->f_op->sendpage(file, buf->page, buf->offset, sd->len, &pos, more); } return ret; } /* * This is a little more tricky than the file -> pipe splicing. There are * basically three cases: * * - Destination page already exists in the address space and there * are users of it. For that case we have no other option that * copying the data. Tough luck. * - Destination page already exists in the address space, but there * are no users of it. Make sure it's uptodate, then drop it. Fall * through to last case. * - Destination page does not exist, we can add the pipe page to * the page cache and avoid the copy. * * If asked to move pages to the output file (SPLICE_F_MOVE is set in * sd->flags), we attempt to migrate pages from the pipe to the output * file address space page cache. This is possible if no one else has * the pipe page referenced outside of the pipe and page cache. If * SPLICE_F_MOVE isn't set, or we cannot move the page, we simply create * a new page in the output file page cache and fill/dirty that. */ int pipe_to_file(struct pipe_inode_info *pipe, struct pipe_buffer *buf, struct splice_desc *sd) { struct file *file = sd->u.file; struct address_space *mapping = file->f_mapping; unsigned int offset, this_len; struct page *page; void *fsdata; int ret; /* * make sure the data in this buffer is uptodate */ ret = buf->ops->confirm(pipe, buf); if (unlikely(ret)) return ret; offset = sd->pos & ~PAGE_CACHE_MASK; this_len = sd->len; if (this_len + offset > PAGE_CACHE_SIZE) this_len = PAGE_CACHE_SIZE - offset; ret = pagecache_write_begin(file, mapping, sd->pos, this_len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); if (unlikely(ret)) goto out; if (buf->page != page) { /* * Careful, ->map() uses KM_USER0! */ char *src = buf->ops->map(pipe, buf, 1); char *dst = kmap_atomic(page, KM_USER1); memcpy(dst + offset, src + buf->offset, this_len); flush_dcache_page(page); kunmap_atomic(dst, KM_USER1); buf->ops->unmap(pipe, buf, src); } ret = pagecache_write_end(file, mapping, sd->pos, this_len, this_len, page, fsdata); out: return ret; } EXPORT_SYMBOL(pipe_to_file); static void wakeup_pipe_writers(struct pipe_inode_info *pipe) { smp_mb(); if (waitqueue_active(&pipe->wait)) wake_up_interruptible(&pipe->wait); kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); } /** * splice_from_pipe_feed - feed available data from a pipe to a file * @pipe: pipe to splice from * @sd: information to @actor * @actor: handler that splices the data * * Description: * 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. * * 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. */ int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { int ret; while (pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; sd->len = buf->len; if (sd->len > sd->total_len) sd->len = sd->total_len; ret = actor(pipe, buf, sd); if (ret <= 0) { if (ret == -ENODATA) ret = 0; return ret; } buf->offset += ret; buf->len -= ret; sd->num_spliced += ret; sd->len -= ret; sd->pos += ret; sd->total_len -= ret; if (!buf->len) { buf->ops = NULL; ops->release(pipe, buf); pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); pipe->nrbufs--; if (pipe->inode) sd->need_wakeup = true; } if (!sd->total_len) return 0; } return 1; } EXPORT_SYMBOL(splice_from_pipe_feed); static void release_splice_pipebufs_special(struct pipe_inode_info *pipe) { for (;;) { if (pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; buf->len = 0; ops->release(pipe, buf); pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); pipe->nrbufs--; } else break; } } /** * __splice_from_pipe_special - 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. This function is a modified form of __splice_from_pipe * and is customized for splice writes. * */ ssize_t __splice_from_pipe_special(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { int ret, retval, do_wakeup, len, not_copied, splice_size, to_copy, remaining; unsigned int offset; void *fsdata; struct file *file = sd->u.file; struct address_space *mapping = file->f_mapping; loff_t page_pos = sd->pos; int nrbufs = 0; ret = 0; retval = 0; do_wakeup = 0; len = sd->total_len; sd->len = len; nrbufs = pipe->nrbufs; remaining = 0; offset = sd->pos & ~PAGE_CACHE_MASK; if (signal_pending(current)) { release_splice_pipebufs_special(pipe); return -ERESTARTSYS; } #if 0 printk("%s:%s:%d len = %d, pipe->nrbufs = %d sd->pos=%lx\n", __FILE__, __FUNCTION__, __LINE__, len, pipe->nrbufs, sd->pos); printk("%s:%s:%d len = %d, pipe->nrbufs = %d\n", __FILE__, __FUNCTION__, __LINE__, len, pipe->nrbufs); for(i=pipe->curbuf; inrbufs; i++) { struct pipe_buffer *pbuf = pipe->bufs + i; printk("%s:%s:%d curbuf=%d pbuf->len=%d addr = %p\n", __FILE__, __FUNCTION__, __LINE__, pipe->curbuf + i, pbuf->len, (page_address(pbuf->page) + pbuf->offset)); } #endif for (;;) { if (pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; offset = sd->pos & ~PAGE_CACHE_MASK; if(len/PAGE_SIZE) not_copied = PAGE_SIZE; else not_copied = len; if (not_copied + offset > PAGE_CACHE_SIZE) not_copied = PAGE_CACHE_SIZE - offset; splice_size = not_copied; struct page *page = NULL; ret = pagecache_write_begin(file, mapping, page_pos, splice_size, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); if (unlikely(ret)) goto out; char *dst = kmap(page); while(not_copied) { buf = pipe->bufs + pipe->curbuf; ops = buf->ops; #if 0 printk("%s:%s:%d buf->len=%d pipe->nrbufs=%d\n", __FILE__, __FUNCTION__, __LINE__, buf->len, pipe->nrbufs); ret = buf->ops->confirm(pipe, buf); if (unlikely(ret)) return ret; #endif char *src = buf->ops->map(pipe, buf, 0); offset = sd->pos & ~PAGE_CACHE_MASK; if(not_copied >= buf->len) { to_copy = buf->len; if(remaining) { to_copy = remaining; } //memcpy(dst + offset, src + buf->offset, to_copy); cacheable_memcpy(dst + offset, src + buf->offset, to_copy); not_copied -= to_copy; remaining = 0; buf->offset += to_copy; buf->len -= to_copy; buf->ops->unmap(pipe, buf, src); ops->release(pipe, buf); pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); pipe->nrbufs--; if(!pipe->nrbufs) break; } else { to_copy = not_copied; //memcpy(dst + offset, src + buf->offset, not_copied); cacheable_memcpy(dst + offset, src + buf->offset, not_copied); remaining = buf->len - not_copied; buf->offset += to_copy; buf->len -= to_copy; not_copied = 0; buf->ops->unmap(pipe, buf, src); } sd->pos += to_copy; } kunmap(page); retval += pagecache_write_end(file, mapping, page_pos, splice_size, splice_size, page, fsdata); len -= splice_size; sd->total_len -= splice_size; sd->len -= splice_size; page_pos += splice_size; //ret += splice_size; #if 0 printk("%s:%s:%d len=%d pipe->nrbufs=%d splice_size=%d retval=%d\n", __FILE__, __FUNCTION__, __LINE__, len, pipe->nrbufs, splice_size, retval); #endif if(!len) break; } if(!pipe->nrbufs) break; if (sd->flags & SPLICE_F_NONBLOCK) { if (!retval) retval = -EAGAIN; break; } } out: // printk("%s:%s:%d - returning %d\n", __FILE__, __FUNCTION__, __LINE__, retval); return retval; } EXPORT_SYMBOL(__splice_from_pipe_special); #ifdef CONFIG_SPLICE_DMA ssize_t __splice_from_pipe_dma(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { int ret, retval, do_wakeup, len, not_copied, splice_size, to_copy, remaining; int begin_buf; int nrbufs; int mapcount; unsigned int offset; void *fsdata; struct file *file; struct address_space *mapping; loff_t page_pos; struct splice_dma_desc *sd_p = NULL; unsigned long *pipe_addr_map = NULL; begin_buf = pipe->curbuf; nrbufs = pipe->nrbufs; file = sd->u.file; mapping = file->f_mapping; page_pos = sd->pos; ret = 0; retval = 0; do_wakeup = 0; len = sd->total_len; sd->len = len; remaining = 0; offset = sd->pos & ~PAGE_CACHE_MASK; #ifdef SPLICE_NO_IO release_splice_pipebufs_special(pipe); return sd->total_len; #endif if (signal_pending(current)) { retval = -ERESTARTSYS; goto cleanup; } sd_p = kzalloc(sizeof(*sd_p), GFP_ATOMIC); if(!sd_p) { retval = -ENOMEM; goto cleanup; } pipe_addr_map = kzalloc(sizeof(*pipe_addr_map) * PIPE_BUFFERS, GFP_ATOMIC); if(!pipe_addr_map) { retval = -ENOMEM; goto cleanup; } for (;;) { if (pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; offset = sd->pos & ~PAGE_CACHE_MASK; if(len/PAGE_SIZE) not_copied = PAGE_SIZE; else not_copied = len; if (not_copied + offset > PAGE_CACHE_SIZE) not_copied = PAGE_CACHE_SIZE - offset; splice_size = not_copied; struct page *page = NULL; ret = pagecache_write_begin(file, mapping, page_pos, splice_size, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); if (unlikely(ret)) { retval = ret; goto cleanup; } mapcount = 0; sd_p->page = page; sd_p->page_offset = page_pos; sd_p->n_elems = 0; char *dst = kmap(page); while(not_copied) { buf = pipe->bufs + pipe->curbuf; ops = buf->ops; #if 0 printk("%s:%s:%d buf->len=%d pipe->nrbufs=%d\n", __FILE__, __FUNCTION__, __LINE__, buf->len, pipe->nrbufs); ret = buf->ops->confirm(pipe, buf); if (unlikely(ret)) { retval = ret; goto cleanup; } #endif char *src = buf->ops->map(pipe, buf, 0); offset = sd->pos & ~PAGE_CACHE_MASK; pipe_addr_map[pipe->curbuf] = (unsigned long)src; if(not_copied >= buf->len) { to_copy = buf->len; if(remaining) { to_copy = remaining; } #if 0 printk("%s:%s:%d - src=0x%08x dst=0x%08x size=%d\n", __FILE__, __FUNCTION__, __LINE__, src + buf->offset, dst + offset, to_copy); #endif sd_p->src_addrs[mapcount] = (unsigned long)(src + buf->offset); sd_p->dst_addrs[mapcount] = (unsigned long)(dst + offset); #ifdef DEBUG_SPLICE printk("src data %s\n",(char *)sd_p->src_addrs[mapcount]); #endif sd_p->xfr_size[mapcount] = to_copy; mapcount++; sd_p->n_elems++; not_copied -= to_copy; remaining = 0; buf->offset += to_copy; buf->len -= to_copy; #if 0 printk("%s:%s:%d releasing pipe buf not_copied=%d\n", __FILE__, __FUNCTION__, __LINE__, not_copied); #endif pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); pipe->nrbufs--; } else { to_copy = not_copied; #if 0 printk("%s:%s:%d - src=0x%08x dst=0x%08x size=%d\n", __FILE__, __FUNCTION__, __LINE__, src + buf->offset, dst + offset, not_copied); #endif sd_p->src_addrs[mapcount] = (unsigned long)(src + buf->offset); sd_p->dst_addrs[mapcount] = (unsigned long)(dst + offset); #ifdef DEBUG_SPLICE printk("src data %s\n",(char *)sd_p->src_addrs[mapcount]); #endif sd_p->xfr_size[mapcount] = not_copied; mapcount++; sd_p->n_elems++; remaining = buf->len - not_copied; buf->offset += to_copy; buf->len -= to_copy; not_copied = 0; } sd->pos += to_copy; if(!pipe->nrbufs) break; } mutex_lock(&splice_dma_lock); ret = splice_dma_memcpy(sd_p, splice_size); mutex_unlock(&splice_dma_lock); if(ret) { printk("%s:%s:%d - splice_dma_memcpy failed with %d\n", __FILE__, __FUNCTION__, __LINE__, ret); kunmap(page); pagecache_write_end(file, mapping, page_pos, 0, 0, page, fsdata); retval = ret; goto cleanup; } kunmap(page); retval += pagecache_write_end(file, mapping, page_pos, splice_size, splice_size, page, fsdata); len -= splice_size; sd->total_len -= splice_size; sd->len -= splice_size; page_pos += splice_size; //ret += splice_size; #if 0 printk("%s:%s:%d len=%d pipe->nrbufs=%d splice_size=%d retval=%d\n", __FILE__, __FUNCTION__, __LINE__, len, pipe->nrbufs, splice_size, retval); #endif if(!len) break; } if(!pipe->nrbufs) break; if (sd->flags & SPLICE_F_NONBLOCK) { if (!retval) retval = -EAGAIN; break; } } cleanup: if(sd_p) kfree(sd_p); pipe->curbuf = begin_buf; for (;;) { if (nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; buf->len = 0; if(pipe_addr_map && (pipe_addr_map[pipe->curbuf])) { char *src = (char *)(pipe_addr_map[pipe->curbuf]); buf->ops->unmap(pipe, buf, src); } ops->release(pipe, buf); pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); nrbufs--; } if(!nrbufs) break; } if(pipe_addr_map) kfree(pipe_addr_map); // printk("%s:%s:%d - returning %d\n", __FILE__, __FUNCTION__, __LINE__, retval); return retval; } EXPORT_SYMBOL(__splice_from_pipe_dma); #endif #ifdef SPLICE_DEFER_DMA static int splice_setup_xfr_sglist(struct splice_dma_desc_defer *sd_def, struct splice_dma_desc *sd_p, int *mapcount) { unsigned int offset; struct splice_desc *sd = &(sd_def->sd); struct pipe_inode_info *pipe = sd_def->pipe; //int curbuf = sd_def->pipe_curbuf; //int nrbufs = sd_def->pipe_nrbufs; struct page *page = sd_def->page; int len = sd->total_len; int count = *mapcount; int ret = 0, n_elems = 0, not_copied = 0, to_copy = 0; int remaining = 0; //printk("%s:%s:%d consuming pipe %p\n", __FILE__, __FUNCTION__, __LINE__, pipe); offset = sd->pos & ~PAGE_CACHE_MASK; if( (len > PAGE_SIZE) || ((len + offset) > PAGE_CACHE_SIZE)) { #if 0 printk("%s:%s:%d - filesystem data is not block aligned" "len=%d , offset=0x%08x\n", __FILE__, __FUNCTION__, __LINE__, len, offset); #endif ret = -EFAULT; goto out; } if(pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; offset = sd->pos & ~PAGE_CACHE_MASK; not_copied = len; //printk("%s:%s:%d - calling page_address\n", __FILE__, __FUNCTION__, __LINE__); char *dst = kmap(page); #if 0 printk("%s:%s:%d - kmap returns %p for page=%p\n", __FILE__, __FUNCTION__, __LINE__, dst, page); #endif while(not_copied) { buf = pipe->bufs + pipe->curbuf; ops = buf->ops; ret = buf->ops->confirm(pipe, buf); if (unlikely(ret)) { printk("%s:%s:%d - ret is %d\n", __FILE__, __FUNCTION__, __LINE__, ret); goto out; } char *src = buf->ops->map(pipe, buf, 0); #if 0 printk("%s:%s:%d - buf->ops->map returns %p\n", __FILE__, __FUNCTION__, __LINE__, src); #endif offset = sd->pos & ~PAGE_CACHE_MASK; sd_def->pipe_addr_map[n_elems++] = (unsigned long)src; if(not_copied >= buf->len) { to_copy = buf->len; if(remaining) { to_copy = remaining; } sd_p->src_addrs[count] = (unsigned long)(src + buf->offset); sd_p->dst_addrs[count] = (unsigned long)(dst + offset); sd_p->xfr_size[count] = to_copy; #if 0 sd_def->src_addrs[count] = (unsigned long)(src + buf->offset); sd_def->dst_addrs[count] = (unsigned long)(dst + offset); sd_def->xfr_size[count] = to_copy; #endif #if 0 printk("%s:%s:%d - src=%p, dst=%p, size=%d index=%d sd_p->n_elems=%d\n", __FILE__, __FUNCTION__, __LINE__, sd_p->src_addrs[count], sd_p->dst_addrs[count], sd_p->xfr_size[count], count, sd_p->n_elems); #endif count++; sd_p->n_elems++; not_copied -= to_copy; remaining = 0; buf->offset += to_copy; buf->len -= to_copy; pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); pipe->nrbufs--; } else { to_copy = not_copied; sd_p->src_addrs[count] = (unsigned long)(src + buf->offset); sd_p->dst_addrs[count] = (unsigned long)(dst + offset); sd_p->xfr_size[count] = not_copied; #if 0 sd_def->src_addrs[count] = (unsigned long)(src + buf->offset); sd_def->dst_addrs[count] = (unsigned long)(dst + offset); sd_def->xfr_size[count] = not_copied; #endif #if 0 printk("%s:%s:%d - src=%p, dst=%p, size=%d index=%d\n", __FILE__, __FUNCTION__, __LINE__, sd_p->src_addrs[count], sd_p->dst_addrs[count], sd_p->xfr_size[count], count); #endif count++; sd_p->n_elems++; remaining = buf->len - not_copied; buf->offset += to_copy; not_copied = 0; } sd->pos += to_copy; if(!pipe->nrbufs) break; } } sd_def->n_elems = n_elems; *mapcount = count; out: //printk("%s:%s:%d returning %d\n", __FILE__, __FUNCTION__, __LINE__, ret); //printk("%s:%s:%d sd_p->n_elems=%d\n", __FILE__, __FUNCTION__, __LINE__, sd_p->n_elems); return ret; } static void release_splice_pipebufs(struct splice_dma_desc_defer *sd_def) { int n = 0; struct pipe_inode_info *pipe = sd_def->pipe; int nrbufs = sd_def->pipe_nrbufs; int curbuf = sd_def->pipe_curbuf; for (;;) { if (nrbufs) { struct pipe_buffer *buf = pipe->bufs + curbuf; #if 0 printk("%s:%s:%d - releasing buf %d\n", __FILE__, __FUNCTION__, __LINE__, curbuf); #endif const struct pipe_buf_operations *ops = buf->ops; buf->len = 0; if(sd_def->pipe_addr_map[n]) { char *src = (char *)(sd_def->pipe_addr_map[n]); buf->ops->unmap(pipe, buf, src); } ops->release(pipe, buf); curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1); nrbufs--; n++; } else break; } } static int splice_flush_dma_writes(void) { loff_t pos; void *fsdata; struct file *file; struct address_space *mapping; int size=0, ret=0, i=0, mapcount=0; struct splice_desc *sd; struct splice_dma_desc_defer *sd_def = NULL; struct page *page = NULL; int total_splice_length = 0; struct pipe_inode_info *pipe = NULL; //printk("%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); struct splice_dma_desc *sd_p = NULL; sd_p = kzalloc(sizeof(*sd_p), GFP_ATOMIC); if(!sd_p) return -ENOMEM; for(i=0; i < MAX_SPLICE_WRITES; i++) { sd_def = (struct splice_dma_desc_defer *)splice_write_queue[i]; if(sd_def && sd_def->valid) { page = NULL; sd = &(sd_def->sd); pos = sd->pos; file = sd->u.file; mapping = file->f_mapping; size = sd->total_len; #if 0 printk("%s:%s:%d - calling pagecache_write_begin i=%d\n", __FILE__, __FUNCTION__, __LINE__, i); printk("file is %s , mapping is %s\n", (file) ? "Not NULL":"NULL", (mapping) ? "Not NULL":"NULL"); #endif ret = pagecache_write_begin(file, mapping, pos, size, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); if (unlikely(ret)) { printk("%s:%s:%d - pagecache_write_begin returns %d\n", __FILE__, __FUNCTION__, __LINE__, ret); goto out; } sd_def->page = page; sd_def->fsdata = fsdata; ret = splice_setup_xfr_sglist(sd_def, sd_p, &mapcount); #if 0 printk("%s:%s:%d - sd_p->n_elems = %d\n", __FILE__, __FUNCTION__, __LINE__, sd_p->n_elems); #endif if(ret) goto out; total_splice_length += size; } } //printk("%s:%s:%d - nrelems = %d\n", __FILE__, __FUNCTION__, __LINE__, sd_p->n_elems); ret = splice_dma_memcpy(sd_p, total_splice_length); if(ret) { printk("%s:%s:%d - splice_dma_memcpy failed with %d\n", __FILE__, __FUNCTION__, __LINE__, ret); goto out; } for(i=0; i < MAX_SPLICE_WRITES; i++) { sd_def = splice_write_queue[i]; if(sd_def && sd_def->valid) { sd = &(sd_def->sd); page = sd_def->page; pos = sd->pos; file = sd->u.file; mapping = file->f_mapping; size = sd->total_len; fsdata = sd_def->fsdata; pipe = sd_def->pipe; ret = pagecache_write_end(file, mapping, pos, size, size, page, fsdata); sd_def->valid = 0; kunmap(page); release_splice_pipebufs(sd_def); #if 0 printk("%s:%s:%d - done with pipe %p\n", __FILE__, __FUNCTION__, __LINE__, pipe); #endif put_splice_pipe(pipe); //sd_def->pipe = NULL; } } out: //printk("%s:%s:%d returning %d \n", __FILE__, __FUNCTION__, __LINE__, ret); kfree(sd_p); return ret; } ssize_t __splice_write_defer(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { unsigned int offset; int ret = 0, i = 0; struct splice_dma_desc_defer *sd_def = NULL; int len = sd->total_len; int total_splice_length = 0; int splice_dma_flush = 0; struct file *file = sd->u.file; int same_blk = 0; // int curbuf = pipe->curbuf; // int nrbufs = pipe->nrbufs; offset = sd->pos & ~PAGE_CACHE_MASK; //printk("%s:%s:%d sd->pos = %llx\n", __FILE__, __FUNCTION__, __LINE__, sd->pos); /* un-aligned page writes */ if((offset & (~PAGE_CACHE_MASK)) || (len > PAGE_SIZE)) { #if 0 printk("%s:%s:%d - un-aligned page writes\n", __FILE__, __FUNCTION__, __LINE__); #endif return __splice_from_pipe_dma(pipe, sd, actor); } if((sd->pos == 0) && (len < PAGE_SIZE)) return __splice_from_pipe_dma(pipe, sd, actor); #if 0 if((splice_queue_ptr + 1) >= MAX_SPLICE_WRITES) splice_dma_flush = 1; #endif //printk("%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); /* If write request for same page in same file exists in splice queue */ for(i = 0; i < MAX_SPLICE_WRITES; i++) { sd_def = (struct splice_dma_desc_defer *)splice_write_queue[i]; if(sd_def && sd_def->valid) { if ((sd_def->sd.u.file == file) && (sd_def->sd.pos == sd->pos) ) { //printk("%s:%s:%d - same blk writes\n", __FILE__, __FUNCTION__, __LINE__); //pipe->curbuf = sd_def->pipe_curbuf; //pipe->nrbufs = sd_def->pipe_nrbufs; __splice_from_pipe_dma(sd_def->pipe, &(sd_def->sd), actor); sd_def-> valid = 0; //same_blk = 1; --splice_queue_count; //return __splice_from_pipe_dma(pipe, sd, actor); } } } //pipe->curbuf = curbuf; //pipe->nrbufs = nrbufs; #if 0 if(same_blk) splice_dma_flush = 0; #endif sd_def = splice_write_queue[splice_queue_ptr]; if(!sd_def) { /* Fisrt time alloc */ sd_def = kzalloc(sizeof(*sd_def), GFP_KERNEL); if(!sd_def) { ret = -ENOMEM; goto out; } splice_write_queue[splice_queue_ptr] = sd_def; } sd_def = splice_write_queue[splice_queue_ptr]; sd_def->sd.total_len = sd->total_len; sd_def->sd.flags = sd->flags; sd_def->sd.pos = sd->pos; sd_def->sd.u.file = sd->u.file; sd_def->f_offset = offset; sd_def->pipe_curbuf = pipe->curbuf; sd_def->pipe_nrbufs = pipe->nrbufs; sd_def->pipe = pipe; sd_def->valid = 1; ++splice_queue_count; #if 0 printk("%s:%s:%d - pipe->curbuf=%d , pipe->nrbufs=%d splice_queue_ptr=%d\n", __FILE__, __FUNCTION__, __LINE__, pipe->curbuf, pipe->nrbufs, splice_queue_ptr); #endif splice_queue_ptr = (splice_queue_ptr + 1) & (MAX_SPLICE_WRITES - 1); if(splice_queue_count >= MAX_SPLICE_WRITES) { /* flush splice queue */ //printk("%s:%s:%d - dma flush\n", __FILE__, __FUNCTION__, __LINE__); splice_queue_count = 0; ret = splice_flush_dma_writes(); if(ret <= 0) goto out; } //pipe->curbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); //pipe->nrbufs = 0; ret = len; out: return ret; } EXPORT_SYMBOL(__splice_write_defer); #endif /** * 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: * See __splice_from_pipe. This function locks the input and output inodes, * otherwise it's identical to __splice_from_pipe(). This is a special * implementation fpr receive file. * */ ssize_t splice_from_pipe_special(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags, splice_actor *actor) { ssize_t ret; struct splice_desc sd = { .total_len = len, .flags = flags, .pos = *ppos, .u.file = out, }; if (signal_pending(current)) { release_splice_pipebufs_special(pipe); return -ERESTARTSYS; } /* * The actor worker might be calling ->prepare_write and * ->commit_write. Most of the time, these expect i_mutex to * be held. Since this may result in an ABBA deadlock with * pipe->inode, we have to order lock acquiry here. */ pipe_lock(pipe); #ifdef CONFIG_SPLICE_DMA ret = __splice_from_pipe_dma(pipe, &sd, actor); #else ret = __splice_from_pipe_special(pipe, &sd, actor); #endif pipe_unlock(pipe); return ret; } /** * 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. */ int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd) { while (!pipe->nrbufs) { if (!pipe->writers) return 0; if (!pipe->waiting_writers && sd->num_spliced) return 0; if (sd->flags & SPLICE_F_NONBLOCK) return -EAGAIN; if (signal_pending(current)) return -ERESTARTSYS; if (sd->need_wakeup) { wakeup_pipe_writers(pipe); sd->need_wakeup = false; } pipe_wait(pipe); } return 1; } EXPORT_SYMBOL(splice_from_pipe_next); /** * splice_from_pipe_begin - start splicing from pipe * @sd: information about the splice operation * * 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. */ void splice_from_pipe_begin(struct splice_desc *sd) { sd->num_spliced = 0; sd->need_wakeup = false; } EXPORT_SYMBOL(splice_from_pipe_begin); /** * 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(). */ void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd) { if (sd->need_wakeup) wakeup_pipe_writers(pipe); } EXPORT_SYMBOL(splice_from_pipe_end); /** * __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; splice_from_pipe_begin(sd); do { 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; } EXPORT_SYMBOL(__splice_from_pipe); /** * 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: * See __splice_from_pipe. This function locks the pipe inode, * otherwise it's identical to __splice_from_pipe(). * */ 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; struct splice_desc sd = { .total_len = len, .flags = flags, .pos = *ppos, .u.file = out, }; pipe_lock(pipe); ret = __splice_from_pipe(pipe, &sd, actor); pipe_unlock(pipe); return ret; } /** * generic_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. * */ ssize_t generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { struct address_space *mapping = out->f_mapping; struct inode *inode = mapping->host; ssize_t ret; int err = 0; if (signal_pending(current)) { release_splice_pipebufs_special(pipe); return -ERESTARTSYS; } err = file_remove_suid(out); if(err) return err; file_update_time(out); mutex_lock(&inode->i_mutex); ret = splice_from_pipe_special(pipe, out, ppos, len, flags, pipe_to_file); mutex_unlock(&inode->i_mutex); if (ret > 0) { unsigned long nr_pages; int err; nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; err = generic_write_sync(out, *ppos, ret); if (err) ret = err; else *ppos += ret; balance_dirty_pages_ratelimited_nr(mapping, nr_pages); } return ret; } EXPORT_SYMBOL(generic_file_splice_write); static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf, struct splice_desc *sd) { int ret; void *data; ret = buf->ops->confirm(pipe, buf); if (ret) return ret; data = buf->ops->map(pipe, buf, 0); ret = kernel_write(sd->u.file, data + buf->offset, sd->len, sd->pos); buf->ops->unmap(pipe, buf, data); return ret; } static ssize_t default_file_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { ssize_t ret; ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf); if (ret > 0) *ppos += ret; return ret; } /** * generic_splice_sendpage - splice data from a pipe to a socket * @pipe: pipe to splice from * @out: socket to write to * @ppos: position in @out * @len: number of bytes to splice * @flags: splice modifier flags * * Description: * Will send @len bytes from the pipe to a network socket. No data copying * is involved. * */ ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage); } EXPORT_SYMBOL(generic_splice_sendpage); /* * Attempt to initiate a splice from pipe to file. */ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); int ret; if (unlikely(!(out->f_mode & FMODE_WRITE))) return -EBADF; if (unlikely(out->f_flags & O_APPEND)) return -EINVAL; if (signal_pending(current)) { release_splice_pipebufs_special(pipe); return -ERESTARTSYS; } ret = rw_verify_area(WRITE, out, ppos, len); if (unlikely(ret < 0)) return ret; splice_write = out->f_op->splice_write; if (!splice_write) splice_write = default_file_splice_write; return splice_write(pipe, out, ppos, len, flags); } /* * Attempt to initiate a splice from a file to a pipe. */ static long do_splice_to(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int ret; if (unlikely(!(in->f_mode & FMODE_READ))) return -EBADF; ret = rw_verify_area(READ, in, ppos, len); if (unlikely(ret < 0)) return ret; splice_read = in->f_op->splice_read; if (!splice_read) splice_read = default_file_splice_read; return splice_read(in, ppos, pipe, len, flags); } /** * 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 * pipe is cached in the process, and reused during the lifetime of * that process. * */ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, splice_direct_actor *actor) { struct pipe_inode_info *pipe; long ret, bytes; umode_t i_mode; size_t len; int i, flags; /* * 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! */ i_mode = in->f_path.dentry->d_inode->i_mode; 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; if (unlikely(!pipe)) { pipe = alloc_pipe_info(NULL); if (!pipe) return -ENOMEM; /* * We don't have an immediate reader, but we'll read the stuff * out of the pipe right after the splice_to_pipe(). So set * PIPE_READERS appropriately. */ pipe->readers = 1; current->splice_pipe = pipe; } /* * Do the splice. */ ret = 0; bytes = 0; len = sd->total_len; flags = sd->flags; /* * Don't block on output, we have to drain the direct pipe. */ sd->flags &= ~SPLICE_F_NONBLOCK; while (len) { size_t read_len; loff_t pos = sd->pos, prev_pos = pos; ret = do_splice_to(in, &pos, pipe, len, flags); if (unlikely(ret <= 0)) goto out_release; read_len = ret; sd->total_len = read_len; /* * 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: */ ret = actor(pipe, sd); if (unlikely(ret <= 0)) { sd->pos = prev_pos; goto out_release; } bytes += ret; len -= ret; sd->pos = pos; if (ret < read_len) { sd->pos = prev_pos + ret; goto out_release; } } done: pipe->nrbufs = pipe->curbuf = 0; file_accessed(in); return bytes; out_release: /* * If we did an incomplete transfer we must release * the pipe buffers in question: */ for (i = 0; i < PIPE_BUFFERS; i++) { struct pipe_buffer *buf = pipe->bufs + i; if (buf->ops) { buf->ops->release(pipe, buf); buf->ops = NULL; } } if (!bytes) bytes = ret; goto done; } EXPORT_SYMBOL(splice_direct_to_actor); static int direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) { struct file *file = sd->u.file; return do_splice_from(pipe, file, &sd->pos, sd->total_len, sd->flags); } /** * do_splice_direct - splices data directly between two files * @in: file to splice from * @ppos: input file offset * @out: file to splice to * @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. * */ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out, size_t len, unsigned int flags) { struct splice_desc sd = { .len = len, .total_len = len, .flags = flags, .pos = *ppos, .u.file = out, }; long ret; ret = splice_direct_to_actor(in, &sd, direct_splice_actor); if (ret > 0) *ppos = sd.pos; return ret; } static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, struct pipe_inode_info *opipe, size_t len, unsigned int flags); /* * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same * location, so checking ->i_pipe is not enough to verify that this is a * pipe. */ static inline struct pipe_inode_info *pipe_info(struct inode *inode) { if (S_ISFIFO(inode->i_mode)) return inode->i_pipe; return NULL; } /* * Determine where to splice to/from. */ 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; long ret; ipipe = pipe_info(in->f_path.dentry->d_inode); opipe = pipe_info(out->f_path.dentry->d_inode); if (ipipe && opipe) { if (off_in || off_out) return -ESPIPE; if (!(in->f_mode & FMODE_READ)) return -EBADF; if (!(out->f_mode & FMODE_WRITE)) return -EBADF; /* Splicing to self would be fun, but... */ if (ipipe == opipe) return -EINVAL; return splice_pipe_to_pipe(ipipe, opipe, len, flags); } if (ipipe) { if (off_in) return -ESPIPE; if (off_out) { if (out->f_op->llseek == no_llseek) return -EINVAL; if (copy_from_user(&offset, off_out, sizeof(loff_t))) return -EFAULT; off = &offset; } else off = &out->f_pos; ret = do_splice_from(ipipe, out, off, len, flags); if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) ret = -EFAULT; return ret; } if (opipe) { if (off_out) return -ESPIPE; if (off_in) { if (in->f_op->llseek == no_llseek) return -EINVAL; if (copy_from_user(&offset, off_in, sizeof(loff_t))) return -EFAULT; off = &offset; } else off = &in->f_pos; ret = do_splice_to(in, off, opipe, len, flags); if (off_in && copy_to_user(off_in, off, sizeof(loff_t))) ret = -EFAULT; return ret; } return -EINVAL; } #if 0 static void print_pipe(struct pipe_inode_info *pipe) { int nrbufs = pipe->nrbufs; int i = 0; printk("pipe->nrbufs = %d\n", pipe->nrbufs); for(i=0; ibufs[i].len = %d, pipe->bufs[i].offset = %d\n", i, pipe->bufs[i].len, pipe->bufs[i].offset); } static void print_data(struct pipe_inode_info *pipe) { int i; int nrbufs = pipe->nrbufs; char strbuf[256]; struct pipe_buffer *buf = NULL; void *mem = NULL; for(i=0; ibufs[i]); buf->ops->confirm(pipe, buf); mem = (void *)page_address(buf->page); memset(strbuf, 0, 256); memcpy(strbuf, mem, 256); printk("%s:%s:%d - memory in strbuf is %s\n", __FILE__, __FUNCTION__, __LINE__, strbuf); } } static void adjust_pipe_splice_bufs(struct pipe_inode_info *pipe, void *splice_mem, int nrpages, int total_len) { unsigned long offset; int old_curbuf = pipe->curbuf; int len = total_len; #ifdef DEBUG_SPLICE printk("%s:%s:%d pipe->nrbufs=%d \n", __FILE__, __FUNCTION__, __LINE__, pipe->nrbufs); #endif if (pipe->inode) { printk("%s:%s:%d - locking pipe->inode->i_mutex\n", __FILE__, __FUNCTION__, __LINE__); mutex_lock(&pipe->inode->i_mutex); } for(;;) { if(pipe->nrbufs) { struct pipe_buffer *buf = pipe->bufs + pipe->curbuf; const struct pipe_buf_operations *ops = buf->ops; if(buf->len) { #ifdef DEBUG_SPLICE printk("%s:%s:%d - freeing buf %d\n", __FILE__, __FUNCTION__, __LINE__, pipe->curbuf); #endif buf->len = 0; ops->release(pipe, buf); buf->private = 0; } pipe->nrbufs--; pipe->curbuf = (pipe->curbuf + 1) & (PIPE_BUFFERS - 1); } else break; } pipe->curbuf = old_curbuf; #ifdef DEBUG_SPLICE printk("%s:%s:%d - pipe->curbuf = %d pipe->nrbufs = %d\n", __FILE__, __FUNCTION__, __LINE__, pipe->curbuf, pipe->nrbufs); #endif for(;;) { int newbuf = (pipe->curbuf + pipe->nrbufs) & (PIPE_BUFFERS - 1); struct pipe_buffer *buf = pipe->bufs + newbuf; offset = PAGE_SIZE * pipe->nrbufs; if(len) { if(len/PAGE_SIZE) { buf->page = virt_to_page(splice_mem + offset); buf->len = PAGE_SIZE; buf->offset = 0; len -= PAGE_SIZE; pipe->nrbufs++; buf->private = SPLICE_MAGIC; } else { /* partial Page */ buf->page = virt_to_page(splice_mem + offset); buf->len = len; buf->offset = 0; len = 0; pipe->nrbufs++; buf->private = SPLICE_MAGIC; break; } } else break; } if (pipe->inode) { printk("%s:%s:%d - unlocking pipe->inode->i_mutex\n", __FILE__, __FUNCTION__, __LINE__); mutex_unlock(&pipe->inode->i_mutex); } } static void ppc460ex_pipebufs_memcpy(struct pipe_inode_info *pipe, void *splice_mem, int length) { int len = 0; int nrbufs = pipe->nrbufs; void *src = NULL; int curbuf = pipe->curbuf; if (pipe->inode) { printk("%s:%s:%d - locking pipe->inode->i_mutex\n", __FILE__, __FUNCTION__, __LINE__); mutex_lock(&pipe->inode->i_mutex); } for(;;) { if(nrbufs) { struct pipe_buffer *buf = pipe->bufs + curbuf; if(len < length) { if(!buf->len) continue; src = page_address(buf->page); memcpy(splice_mem + len, src+buf->offset, buf->len); //flush_dcache_page(buf->page); len += buf->len; curbuf = (curbuf + 1) & (PIPE_BUFFERS - 1); --nrbufs; } else /* all pipe buf copied*/ break; } else break; } if (pipe->inode) { printk("%s:%s:%d - locking pipe->inode->i_mutex\n", __FILE__, __FUNCTION__, __LINE__); mutex_unlock(&pipe->inode->i_mutex); } } #endif /* 0 */ #ifdef SPLICE_DEFER_DMA static void put_splice_pipe(struct pipe_inode_info *pipe) { #if 0 int index = pipe->private; struct splice_pipe_defer *pipe_def = splice_write_pipes[index]; pipe_def->available = 1; #endif int i = 0; struct splice_pipe_defer *pipe_def = NULL; struct pipe_inode_info *pipe_local = NULL; //printk("%s:%s:%d - pipe =%p\n", __FILE__, __FUNCTION__, __LINE__, pipe); for(i=0; ipipe) { pipe_local = pipe_def->pipe; #if 0 printk("%s:%s:%d - pipe_local =%p\n", __FILE__, __FUNCTION__, __LINE__, pipe_local); #endif if(pipe == pipe_local) { pipe_def->available = 1; return; } } } printk("%s:%s:%d - pipe not found !!!!\n", __FILE__, __FUNCTION__, __LINE__); } static struct pipe_inode_info *get_splice_pipe(void) { int i = 0; struct splice_pipe_defer *pipe_def = NULL; struct pipe_inode_info *pipe = NULL; for(i=0; iavailable) { pipe_def->available = 0; pipe = pipe_def->pipe; //pipe->private = i; goto out; } } else { pipe_def = kzalloc(sizeof(*pipe_def), GFP_KERNEL); if(!pipe_def) { printk("%s:%s:%d - error allocating memory for splice_pipe_defer\n", __FILE__, __FUNCTION__, __LINE__); goto out; } pipe = alloc_pipe_info(NULL); if (!pipe) { printk("%s %s:%d alloc_pipe_info failed\n", __FUNCTION__, __FILE__, __LINE__); goto out; } pipe_def->pipe = pipe; pipe_def->available = 0; splice_write_pipes[i] = pipe_def; //pipe->private = i; break; } } out: return pipe; } #endif static long do_splice_2(int fd_in, struct file *in, loff_t __user *off_in, int fd_out, struct file *out, loff_t __user *off_out, size_t len, unsigned int flags) { struct pipe_inode_info *pipe; loff_t offset, *off; long ret; if (signal_pending(current)) return -ERESTARTSYS; #ifdef DEBUG_SPLICE printk("%s:%s:%d - len=%d\n", __FILE__, __FUNCTION__, __LINE__, len); #endif #ifndef SPLICE_DEFER_DMA pipe = alloc_pipe_info(NULL); if (!pipe) { printk("%s %s:%d alloc_pipe_info failed\n", __FUNCTION__, __FILE__, __LINE__); return -ENOMEM; } /* * We don't have an immediate reader, but we'll read the stuff * out of the pipe right after the splice_to_pipe(). So set * PIPE_READERS appropriately. */ pipe->readers = 1; #else pipe = get_splice_pipe(); if(!pipe) return -ENOMEM; pipe->readers = 1; #endif if (off_in) { printk(KERN_ERR "%s:%s:%d off_in is seekable\n", __FUNCTION__, __FILE__, __LINE__); ret = -ESPIPE; goto out; } if (off_out) { if (out->f_op->llseek == no_llseek) { printk(KERN_ERR "%s:%s:%d out is non-seekable\n", __FUNCTION__, __FILE__, __LINE__); ret = -EINVAL; goto out; } if (copy_from_user(&offset, off_out, sizeof(loff_t))) { printk(KERN_ERR "%s:%s:%d copy_from_user failed\n", __FUNCTION__, __FILE__, __LINE__); ret = -EFAULT; goto out; } off = &offset; } else off = &out->f_pos; #ifdef DEBUG_SPLICE printk("%s:%s:%d - calling in->f_op.splice_read len=%d\n", __FILE__, __FUNCTION__, __LINE__, len); #endif ret = in->f_op->splice_read(in, off_in, pipe, len, flags); #ifdef DEBUG_SPLICE printk("%s:%s:%d - ret from sock_splice_read =%d\n", __FILE__, __FUNCTION__, __LINE__, ret); #endif if(!ret) { printk(KERN_ERR "%s:%s:%d sock_splice_read read nothing\n", __FILE__, __FUNCTION__, __LINE__); ret = -EAGAIN; goto out; } #ifdef DEBUG_SPLICE printk("%s:%s:%d - Number of pipe bufs = %d\n", __FILE__, __FUNCTION__, __LINE__, pipe->nrbufs); #endif len = ret; ret = do_splice_from(pipe, out, off, len, flags); #ifdef DEBUG_SPLICE printk("%s:%s:%d do_splice_from returns %d\n", __FILE__, __FUNCTION__, __LINE__, ret); #endif if (off_out && copy_to_user(off_out, off, sizeof(loff_t))) { printk(KERN_ERR "%s:%s:%d copy_to_user failed\n", __FILE__, __FUNCTION__, __LINE__); ret = -EFAULT; } #ifdef DEBUG_SPLICE printk("%s:%s:%d returning ret=%d\n", __FILE__, __FUNCTION__, __LINE__, ret); #endif out: kfree(pipe); return ret; } /* * Do a copy-from-user while holding the mmap_semaphore for reading, in a * manner safe from deadlocking with simultaneous mmap() (grabbing mmap_sem * for writing) and page faulting on the user memory pointed to by src. * This assumes that we will very rarely hit the partial != 0 path, or this * will not be a win. */ static int copy_from_user_mmap_sem(void *dst, const void __user *src, size_t n) { int partial; if (!access_ok(VERIFY_READ, src, n)) return -EFAULT; pagefault_disable(); partial = __copy_from_user_inatomic(dst, src, n); pagefault_enable(); /* * Didn't copy everything, drop the mmap_sem and do a faulting copy */ if (unlikely(partial)) { up_read(¤t->mm->mmap_sem); partial = copy_from_user(dst, src, n); down_read(¤t->mm->mmap_sem); } return partial; } /* * Map an iov into an array of pages and offset/length tupples. With the * partial_page structure, we can map several non-contiguous ranges into * our ones pages[] map instead of splitting that operation into pieces. * Could easily be exported as a generic helper for other users, in which * case one would probably want to add a 'max_nr_pages' parameter as well. */ static int get_iovec_page_array(const struct iovec __user *iov, unsigned int nr_vecs, struct page **pages, struct partial_page *partial, int aligned) { int buffers = 0, error = 0; while (nr_vecs) { unsigned long off, npages; struct iovec entry; void __user *base; size_t len; int i; error = -EFAULT; if (copy_from_user(&entry, iov, sizeof(entry))) break; base = entry.iov_base; len = entry.iov_len; /* * Sanity check this iovec. 0 read succeeds. */ error = 0; if (unlikely(!len)) break; error = -EFAULT; if (!access_ok(VERIFY_READ, base, len)) break; /* * Get this base offset and number of pages, then map * in the user pages. */ off = (unsigned long) base & ~PAGE_MASK; /* * If asked for alignment, the offset must be zero and the * length a multiple of the PAGE_SIZE. */ error = -EINVAL; if (aligned && (off || len & ~PAGE_MASK)) break; npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT; if (npages > PIPE_BUFFERS - buffers) npages = PIPE_BUFFERS - buffers; error = get_user_pages_fast((unsigned long)base, npages, 0, &pages[buffers]); if (unlikely(error <= 0)) break; /* * Fill this contiguous range into the partial page map. */ for (i = 0; i < error; i++) { const int plen = min_t(size_t, len, PAGE_SIZE - off); partial[buffers].offset = off; partial[buffers].len = plen; off = 0; len -= plen; buffers++; } /* * We didn't complete this iov, stop here since it probably * means we have to move some of this into a pipe to * be able to continue. */ if (len) break; /* * Don't continue if we mapped fewer pages than we asked for, * or if we mapped the max number of pages that we have * room for. */ if (error < npages || buffers == PIPE_BUFFERS) break; nr_vecs--; iov++; } if (buffers) return buffers; return error; } static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf, struct splice_desc *sd) { char *src; int ret; ret = buf->ops->confirm(pipe, buf); if (unlikely(ret)) return ret; /* * See if we can use the atomic maps, by prefaulting in the * pages and doing an atomic copy */ if (!fault_in_pages_writeable(sd->u.userptr, sd->len)) { src = buf->ops->map(pipe, buf, 1); ret = __copy_to_user_inatomic(sd->u.userptr, src + buf->offset, sd->len); buf->ops->unmap(pipe, buf, src); if (!ret) { ret = sd->len; goto out; } } /* * No dice, use slow non-atomic map and copy */ src = buf->ops->map(pipe, buf, 0); ret = sd->len; if (copy_to_user(sd->u.userptr, src + buf->offset, sd->len)) ret = -EFAULT; buf->ops->unmap(pipe, buf, src); out: if (ret > 0) sd->u.userptr += ret; return ret; } /* * For lack of a better implementation, implement vmsplice() to userspace * as a simple copy of the pipes pages to the user iov. */ static long vmsplice_to_user(struct file *file, const struct iovec __user *iov, unsigned long nr_segs, unsigned int flags) { struct pipe_inode_info *pipe; struct splice_desc sd; ssize_t size; int error; long ret; pipe = pipe_info(file->f_path.dentry->d_inode); if (!pipe) return -EBADF; pipe_lock(pipe); error = ret = 0; while (nr_segs) { void __user *base; size_t len; /* * Get user address base and length for this iovec. */ error = get_user(base, &iov->iov_base); if (unlikely(error)) break; error = get_user(len, &iov->iov_len); if (unlikely(error)) break; /* * Sanity check this iovec. 0 read succeeds. */ if (unlikely(!len)) break; if (unlikely(!base)) { error = -EFAULT; break; } if (unlikely(!access_ok(VERIFY_WRITE, base, len))) { error = -EFAULT; break; } sd.len = 0; sd.total_len = len; sd.flags = flags; sd.u.userptr = base; sd.pos = 0; size = __splice_from_pipe(pipe, &sd, pipe_to_user); if (size < 0) { if (!ret) ret = size; break; } ret += size; if (size < len) break; nr_segs--; iov++; } pipe_unlock(pipe); if (!ret) ret = error; return ret; } /* * 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. */ static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov, unsigned long nr_segs, unsigned int flags) { struct pipe_inode_info *pipe; struct page *pages[PIPE_BUFFERS]; struct partial_page partial[PIPE_BUFFERS]; struct splice_pipe_desc spd = { .pages = pages, .partial = partial, .flags = flags, .ops = &user_page_pipe_buf_ops, .spd_release = spd_release_page, }; pipe = pipe_info(file->f_path.dentry->d_inode); if (!pipe) return -EBADF; spd.nr_pages = get_iovec_page_array(iov, nr_segs, pages, partial, flags & SPLICE_F_GIFT); if (spd.nr_pages <= 0) return spd.nr_pages; return splice_to_pipe(pipe, &spd); } /* * 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(). * */ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov, unsigned long, nr_segs, unsigned int, flags) { struct file *file; long error; int fput; if (unlikely(nr_segs > UIO_MAXIOV)) return -EINVAL; else if (unlikely(!nr_segs)) return 0; error = -EBADF; file = fget_light(fd, &fput); if (file) { if (file->f_mode & FMODE_WRITE) error = vmsplice_to_pipe(file, iov, nr_segs, flags); else if (file->f_mode & FMODE_READ) error = vmsplice_to_user(file, iov, nr_segs, flags); fput_light(file, fput); } return error; } 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) { long error = 0; struct file *in, *out; int fput_in, fput_out; if (unlikely(!len)) return 0; if (signal_pending(current)) return -ERESTARTSYS; in = fget_light(fd_in, &fput_in); if (in) { if (in->f_mode & FMODE_READ) { out = fget_light(fd_out, &fput_out); if (out) { if (out->f_mode & FMODE_WRITE) #if 0 error = do_splice(in, off_in, out, off_out, len, flags); #else { error = do_splice_2(fd_in, in, off_in, fd_out, out, off_out, len, flags); } #endif fput_light(out, fput_out); } } fput_light(in, fput_in); } return error; } /* * Make sure there's data to read. Wait for input if we can, otherwise * return an appropriate error. */ static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) { int ret; /* * Check ->nrbufs without the inode lock first. This function * is speculative anyways, so missing one is ok. */ if (pipe->nrbufs) return 0; ret = 0; pipe_lock(pipe); while (!pipe->nrbufs) { if (signal_pending(current)) { ret = -ERESTARTSYS; break; } if (!pipe->writers) break; if (!pipe->waiting_writers) { if (flags & SPLICE_F_NONBLOCK) { ret = -EAGAIN; break; } } pipe_wait(pipe); } pipe_unlock(pipe); return ret; } /* * Make sure there's writeable room. Wait for room if we can, otherwise * return an appropriate error. */ static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) { int ret; /* * Check ->nrbufs without the inode lock first. This function * is speculative anyways, so missing one is ok. */ if (pipe->nrbufs < PIPE_BUFFERS) return 0; ret = 0; pipe_lock(pipe); while (pipe->nrbufs >= PIPE_BUFFERS) { 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; } pipe->waiting_writers++; pipe_wait(pipe); pipe->waiting_writers--; } pipe_unlock(pipe); return ret; } /* * 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; int ret = 0, nbuf; 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); do { if (!opipe->readers) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; } if (!ipipe->nrbufs && !ipipe->writers) break; /* * Cannot make any progress, because either the input * pipe is empty or the output pipe is full. */ if (!ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) { /* 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; } ibuf = ipipe->bufs + ipipe->curbuf; nbuf = (opipe->curbuf + opipe->nrbufs) % PIPE_BUFFERS; obuf = opipe->bufs + nbuf; if (len >= ibuf->len) { /* * Simply move the whole buffer from ipipe to opipe */ *obuf = *ibuf; ibuf->ops = NULL; opipe->nrbufs++; ipipe->curbuf = (ipipe->curbuf + 1) % PIPE_BUFFERS; ipipe->nrbufs--; input_wakeup = true; } else { /* * Get a reference to this pipe buffer, * so we can copy the contents over. */ ibuf->ops->get(ipipe, ibuf); *obuf = *ibuf; /* * Don't inherit the gift flag, we need to * prevent multiple steals of this page. */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; obuf->len = len; opipe->nrbufs++; ibuf->offset += obuf->len; ibuf->len -= obuf->len; } ret += obuf->len; len -= obuf->len; } while (len); pipe_unlock(ipipe); pipe_unlock(opipe); /* * If we put data in the output pipe, wakeup any potential readers. */ if (ret > 0) { smp_mb(); if (waitqueue_active(&opipe->wait)) wake_up_interruptible(&opipe->wait); kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); } if (input_wakeup) wakeup_pipe_writers(ipipe); return ret; } /* * 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; int ret = 0, i = 0, nbuf; /* * 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); do { if (!opipe->readers) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; } /* * If we have iterated all input buffers or ran out of * output room, break. */ if (i >= ipipe->nrbufs || opipe->nrbufs >= PIPE_BUFFERS) break; ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (PIPE_BUFFERS - 1)); nbuf = (opipe->curbuf + opipe->nrbufs) & (PIPE_BUFFERS - 1); /* * Get a reference to this pipe buffer, * so we can copy the contents over. */ ibuf->ops->get(ipipe, ibuf); obuf = opipe->bufs + nbuf; *obuf = *ibuf; /* * Don't inherit the gift flag, we need to * prevent multiple steals of this page. */ obuf->flags &= ~PIPE_BUF_FLAG_GIFT; if (obuf->len > len) obuf->len = len; opipe->nrbufs++; ret += obuf->len; len -= obuf->len; i++; } while (len); /* * return EAGAIN if we have the potential of some data in the * future, otherwise just return 0 */ if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK)) ret = -EAGAIN; pipe_unlock(ipipe); pipe_unlock(opipe); /* * If we put data in the output pipe, wakeup any potential readers. */ if (ret > 0) { smp_mb(); if (waitqueue_active(&opipe->wait)) wake_up_interruptible(&opipe->wait); kill_fasync(&opipe->fasync_readers, SIGIO, POLL_IN); } 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. */ static long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags) { struct pipe_inode_info *ipipe = pipe_info(in->f_path.dentry->d_inode); struct pipe_inode_info *opipe = pipe_info(out->f_path.dentry->d_inode); int ret = -EINVAL; /* * Duplicate the contents of ipipe to opipe without actually * copying the data. */ if (ipipe && opipe && ipipe != opipe) { /* * Keep going, unless we encounter an error. The ipipe/opipe * ordering doesn't really matter. */ ret = ipipe_prep(ipipe, flags); if (!ret) { ret = opipe_prep(opipe, flags); if (!ret) ret = link_pipe(ipipe, opipe, len, flags); } } return ret; } SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags) { struct file *in; int error, fput_in; if (unlikely(!len)) return 0; error = -EBADF; in = fget_light(fdin, &fput_in); if (in) { if (in->f_mode & FMODE_READ) { int fput_out; struct file *out = fget_light(fdout, &fput_out); if (out) { if (out->f_mode & FMODE_WRITE) error = do_tee(in, out, len, flags); fput_light(out, fput_out); } } fput_light(in, fput_in); } return error; }