Blame view

fs/splice.c 46.2 KB
5274f052e   Jens Axboe   [PATCH] Introduce...
1
2
3
4
5
6
7
8
9
10
11
  /*
   * "splice": joining two ropes together by interweaving their strands.
   *
   * This is the "extended pipe" functionality, where a pipe is used as
   * an arbitrary in-memory buffer. Think of a pipe as a small kernel
   * buffer that you can use to transfer data from one end to the other.
   *
   * The traditional unix read/write is extended with a "splice()" operation
   * that transfers data buffers to or from a pipe buffer.
   *
   * Named by Larry McVoy, original implementation from Linus, extended by
c2058e061   Jens Axboe   [PATCH] splice: a...
12
13
   * Jens to support splicing to files, network, direct splicing, etc and
   * fixing lots of bugs.
5274f052e   Jens Axboe   [PATCH] Introduce...
14
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
15
   * Copyright (C) 2005-2006 Jens Axboe <axboe@kernel.dk>
c2058e061   Jens Axboe   [PATCH] splice: a...
16
17
   * Copyright (C) 2005-2006 Linus Torvalds <torvalds@osdl.org>
   * Copyright (C) 2006 Ingo Molnar <mingo@elte.hu>
5274f052e   Jens Axboe   [PATCH] Introduce...
18
19
20
21
22
   *
   */
  #include <linux/fs.h>
  #include <linux/file.h>
  #include <linux/pagemap.h>
d6b29d7ce   Jens Axboe   splice: divorce t...
23
  #include <linux/splice.h>
08e552c69   KAMEZAWA Hiroyuki   memcg: synchroniz...
24
  #include <linux/memcontrol.h>
5274f052e   Jens Axboe   [PATCH] Introduce...
25
  #include <linux/mm_inline.h>
5abc97aa2   Jens Axboe   [PATCH] splice: a...
26
  #include <linux/swap.h>
4f6f0bd2f   Jens Axboe   [PATCH] splice: i...
27
  #include <linux/writeback.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
28
  #include <linux/export.h>
4f6f0bd2f   Jens Axboe   [PATCH] splice: i...
29
  #include <linux/syscalls.h>
912d35f86   Jens Axboe   [PATCH] Add suppo...
30
  #include <linux/uio.h>
29ce20586   James Morris   security: revalid...
31
  #include <linux/security.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
32
  #include <linux/gfp.h>
35f9c09fe   Eric Dumazet   tcp: tcp_sendpage...
33
  #include <linux/socket.h>
76b021d05   Al Viro   convert vmsplice ...
34
  #include <linux/compat.h>
06ae43f34   Al Viro   Don't bother with...
35
  #include "internal.h"
5274f052e   Jens Axboe   [PATCH] Introduce...
36

83f9135bd   Jens Axboe   [PATCH] splice: a...
37
38
39
40
41
42
  /*
   * 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.
   */
76ad4d111   Jens Axboe   [PATCH] splice: r...
43
  static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
5abc97aa2   Jens Axboe   [PATCH] splice: a...
44
45
46
  				     struct pipe_buffer *buf)
  {
  	struct page *page = buf->page;
9e94cd4fd   Jens Axboe   [PATCH] splice: r...
47
  	struct address_space *mapping;
5abc97aa2   Jens Axboe   [PATCH] splice: a...
48

9e0267c26   Jens Axboe   [PATCH] splice: f...
49
  	lock_page(page);
9e94cd4fd   Jens Axboe   [PATCH] splice: r...
50
51
52
  	mapping = page_mapping(page);
  	if (mapping) {
  		WARN_ON(!PageUptodate(page));
5abc97aa2   Jens Axboe   [PATCH] splice: a...
53

9e94cd4fd   Jens Axboe   [PATCH] splice: r...
54
55
56
57
58
59
60
61
62
  		/*
  		 * At least for ext2 with nobh option, we need to wait on
  		 * writeback completing on this page, since we'll remove it
  		 * from the pagecache.  Otherwise truncate wont wait on the
  		 * page, allowing the disk blocks to be reused by someone else
  		 * before we actually wrote our data to them. fs corruption
  		 * ensues.
  		 */
  		wait_on_page_writeback(page);
ad8d6f0a7   Jens Axboe   [PATCH] splice: p...
63

266cf658e   David Howells   FS-Cache: Recruit...
64
65
  		if (page_has_private(page) &&
  		    !try_to_release_page(page, GFP_KERNEL))
ca39d651d   Jens Axboe   splice: handle tr...
66
  			goto out_unlock;
4f6f0bd2f   Jens Axboe   [PATCH] splice: i...
67

9e94cd4fd   Jens Axboe   [PATCH] splice: r...
68
69
70
71
72
73
74
75
  		/*
  		 * If we succeeded in removing the mapping, set LRU flag
  		 * and return good.
  		 */
  		if (remove_mapping(mapping, page)) {
  			buf->flags |= PIPE_BUF_FLAG_LRU;
  			return 0;
  		}
9e0267c26   Jens Axboe   [PATCH] splice: f...
76
  	}
5abc97aa2   Jens Axboe   [PATCH] splice: a...
77

9e94cd4fd   Jens Axboe   [PATCH] splice: r...
78
79
80
81
  	/*
  	 * Raced with truncate or failed to remove page from current
  	 * address space, unlock and return failure.
  	 */
ca39d651d   Jens Axboe   splice: handle tr...
82
  out_unlock:
9e94cd4fd   Jens Axboe   [PATCH] splice: r...
83
84
  	unlock_page(page);
  	return 1;
5abc97aa2   Jens Axboe   [PATCH] splice: a...
85
  }
76ad4d111   Jens Axboe   [PATCH] splice: r...
86
  static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
5274f052e   Jens Axboe   [PATCH] Introduce...
87
88
  					struct pipe_buffer *buf)
  {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
89
  	put_page(buf->page);
1432873af   Jens Axboe   [PATCH] splice: L...
90
  	buf->flags &= ~PIPE_BUF_FLAG_LRU;
5274f052e   Jens Axboe   [PATCH] Introduce...
91
  }
0845718da   Jens Axboe   pipe: add documen...
92
93
94
95
  /*
   * Check whether the contents of buf is OK to access. Since the content
   * is a page cache page, IO may be in flight.
   */
cac36bb06   Jens Axboe   pipe: change the ...
96
97
  static int page_cache_pipe_buf_confirm(struct pipe_inode_info *pipe,
  				       struct pipe_buffer *buf)
5274f052e   Jens Axboe   [PATCH] Introduce...
98
99
  {
  	struct page *page = buf->page;
49d0b21be   Jens Axboe   [PATCH] splice: o...
100
  	int err;
5274f052e   Jens Axboe   [PATCH] Introduce...
101
102
  
  	if (!PageUptodate(page)) {
49d0b21be   Jens Axboe   [PATCH] splice: o...
103
104
105
106
  		lock_page(page);
  
  		/*
  		 * Page got truncated/unhashed. This will cause a 0-byte
73d62d83e   Ingo Molnar   [PATCH] splice: c...
107
  		 * splice, if this is the first page.
49d0b21be   Jens Axboe   [PATCH] splice: o...
108
109
110
111
112
  		 */
  		if (!page->mapping) {
  			err = -ENODATA;
  			goto error;
  		}
5274f052e   Jens Axboe   [PATCH] Introduce...
113

49d0b21be   Jens Axboe   [PATCH] splice: o...
114
  		/*
73d62d83e   Ingo Molnar   [PATCH] splice: c...
115
  		 * Uh oh, read-error from disk.
49d0b21be   Jens Axboe   [PATCH] splice: o...
116
117
118
119
120
121
122
  		 */
  		if (!PageUptodate(page)) {
  			err = -EIO;
  			goto error;
  		}
  
  		/*
f84d75199   Jens Axboe   [PATCH] pipe: int...
123
  		 * Page is ok afterall, we are done.
49d0b21be   Jens Axboe   [PATCH] splice: o...
124
  		 */
5274f052e   Jens Axboe   [PATCH] Introduce...
125
  		unlock_page(page);
5274f052e   Jens Axboe   [PATCH] Introduce...
126
  	}
f84d75199   Jens Axboe   [PATCH] pipe: int...
127
  	return 0;
49d0b21be   Jens Axboe   [PATCH] splice: o...
128
129
  error:
  	unlock_page(page);
f84d75199   Jens Axboe   [PATCH] pipe: int...
130
  	return err;
70524490e   Jens Axboe   [PATCH] splice: a...
131
  }
708e3508c   Hugh Dickins   tmpfs: clone shme...
132
  const struct pipe_buf_operations page_cache_pipe_buf_ops = {
5274f052e   Jens Axboe   [PATCH] Introduce...
133
  	.can_merge = 0,
cac36bb06   Jens Axboe   pipe: change the ...
134
  	.confirm = page_cache_pipe_buf_confirm,
5274f052e   Jens Axboe   [PATCH] Introduce...
135
  	.release = page_cache_pipe_buf_release,
5abc97aa2   Jens Axboe   [PATCH] splice: a...
136
  	.steal = page_cache_pipe_buf_steal,
f84d75199   Jens Axboe   [PATCH] pipe: int...
137
  	.get = generic_pipe_buf_get,
5274f052e   Jens Axboe   [PATCH] Introduce...
138
  };
912d35f86   Jens Axboe   [PATCH] Add suppo...
139
140
141
  static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
  				    struct pipe_buffer *buf)
  {
7afa6fd03   Jens Axboe   [PATCH] vmsplice:...
142
143
  	if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
  		return 1;
1432873af   Jens Axboe   [PATCH] splice: L...
144
  	buf->flags |= PIPE_BUF_FLAG_LRU;
330ab7161   Jens Axboe   [PATCH] vmsplice:...
145
  	return generic_pipe_buf_steal(pipe, buf);
912d35f86   Jens Axboe   [PATCH] Add suppo...
146
  }
d4c3cca94   Eric Dumazet   [PATCH] constify ...
147
  static const struct pipe_buf_operations user_page_pipe_buf_ops = {
912d35f86   Jens Axboe   [PATCH] Add suppo...
148
  	.can_merge = 0,
cac36bb06   Jens Axboe   pipe: change the ...
149
  	.confirm = generic_pipe_buf_confirm,
912d35f86   Jens Axboe   [PATCH] Add suppo...
150
151
  	.release = page_cache_pipe_buf_release,
  	.steal = user_page_pipe_buf_steal,
f84d75199   Jens Axboe   [PATCH] pipe: int...
152
  	.get = generic_pipe_buf_get,
912d35f86   Jens Axboe   [PATCH] Add suppo...
153
  };
825cdcb1a   Namhyung Kim   splice: add wakeu...
154
155
156
157
158
159
160
  static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
  {
  	smp_mb();
  	if (waitqueue_active(&pipe->wait))
  		wake_up_interruptible(&pipe->wait);
  	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  }
932cc6d4f   Jens Axboe   splice: completel...
161
162
163
164
165
166
  /**
   * splice_to_pipe - fill passed data into a pipe
   * @pipe:	pipe to fill
   * @spd:	data to fill
   *
   * Description:
79685b8de   Randy Dunlap   docbook: add pipe...
167
   *    @spd contains a map of pages and len/offset tuples, along with
932cc6d4f   Jens Axboe   splice: completel...
168
169
170
   *    the struct pipe_buf_operations associated with these pages. This
   *    function will link that data to the pipe.
   *
83f9135bd   Jens Axboe   [PATCH] splice: a...
171
   */
d6b29d7ce   Jens Axboe   splice: divorce t...
172
173
  ssize_t splice_to_pipe(struct pipe_inode_info *pipe,
  		       struct splice_pipe_desc *spd)
5274f052e   Jens Axboe   [PATCH] Introduce...
174
  {
00de00bda   Jens Axboe   splice: fix leak ...
175
  	unsigned int spd_pages = spd->nr_pages;
912d35f86   Jens Axboe   [PATCH] Add suppo...
176
  	int ret, do_wakeup, page_nr;
5274f052e   Jens Axboe   [PATCH] Introduce...
177

d6785d915   Rabin Vincent   splice: handle ze...
178
179
  	if (!spd_pages)
  		return 0;
5274f052e   Jens Axboe   [PATCH] Introduce...
180
181
  	ret = 0;
  	do_wakeup = 0;
912d35f86   Jens Axboe   [PATCH] Add suppo...
182
  	page_nr = 0;
5274f052e   Jens Axboe   [PATCH] Introduce...
183

61e0d47c3   Miklos Szeredi   splice: add helpe...
184
  	pipe_lock(pipe);
5274f052e   Jens Axboe   [PATCH] Introduce...
185

5274f052e   Jens Axboe   [PATCH] Introduce...
186
  	for (;;) {
3a326a2ce   Ingo Molnar   [PATCH] introduce...
187
  		if (!pipe->readers) {
5274f052e   Jens Axboe   [PATCH] Introduce...
188
189
190
191
192
  			send_sig(SIGPIPE, current, 0);
  			if (!ret)
  				ret = -EPIPE;
  			break;
  		}
35f3d14db   Jens Axboe   pipe: add support...
193
194
  		if (pipe->nrbufs < pipe->buffers) {
  			int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
3a326a2ce   Ingo Molnar   [PATCH] introduce...
195
  			struct pipe_buffer *buf = pipe->bufs + newbuf;
5274f052e   Jens Axboe   [PATCH] Introduce...
196

912d35f86   Jens Axboe   [PATCH] Add suppo...
197
198
199
  			buf->page = spd->pages[page_nr];
  			buf->offset = spd->partial[page_nr].offset;
  			buf->len = spd->partial[page_nr].len;
497f9625c   Jens Axboe   pipe: allow passi...
200
  			buf->private = spd->partial[page_nr].private;
912d35f86   Jens Axboe   [PATCH] Add suppo...
201
  			buf->ops = spd->ops;
7afa6fd03   Jens Axboe   [PATCH] vmsplice:...
202
203
  			if (spd->flags & SPLICE_F_GIFT)
  				buf->flags |= PIPE_BUF_FLAG_GIFT;
6f767b042   Jens Axboe   [PATCH] splice: s...
204
  			pipe->nrbufs++;
912d35f86   Jens Axboe   [PATCH] Add suppo...
205
206
  			page_nr++;
  			ret += buf->len;
6447a3cf1   Al Viro   get rid of pipe->...
207
  			if (pipe->files)
6f767b042   Jens Axboe   [PATCH] splice: s...
208
  				do_wakeup = 1;
5274f052e   Jens Axboe   [PATCH] Introduce...
209

912d35f86   Jens Axboe   [PATCH] Add suppo...
210
  			if (!--spd->nr_pages)
5274f052e   Jens Axboe   [PATCH] Introduce...
211
  				break;
35f3d14db   Jens Axboe   pipe: add support...
212
  			if (pipe->nrbufs < pipe->buffers)
5274f052e   Jens Axboe   [PATCH] Introduce...
213
214
215
216
  				continue;
  
  			break;
  		}
912d35f86   Jens Axboe   [PATCH] Add suppo...
217
  		if (spd->flags & SPLICE_F_NONBLOCK) {
29e350944   Linus Torvalds   splice: add SPLIC...
218
219
220
221
  			if (!ret)
  				ret = -EAGAIN;
  			break;
  		}
5274f052e   Jens Axboe   [PATCH] Introduce...
222
223
224
225
226
227
228
  		if (signal_pending(current)) {
  			if (!ret)
  				ret = -ERESTARTSYS;
  			break;
  		}
  
  		if (do_wakeup) {
c0bd1f650   Jens Axboe   [PATCH] splice: o...
229
  			smp_mb();
3a326a2ce   Ingo Molnar   [PATCH] introduce...
230
231
232
  			if (waitqueue_active(&pipe->wait))
  				wake_up_interruptible_sync(&pipe->wait);
  			kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
5274f052e   Jens Axboe   [PATCH] Introduce...
233
234
  			do_wakeup = 0;
  		}
3a326a2ce   Ingo Molnar   [PATCH] introduce...
235
236
237
  		pipe->waiting_writers++;
  		pipe_wait(pipe);
  		pipe->waiting_writers--;
5274f052e   Jens Axboe   [PATCH] Introduce...
238
  	}
61e0d47c3   Miklos Szeredi   splice: add helpe...
239
  	pipe_unlock(pipe);
5274f052e   Jens Axboe   [PATCH] Introduce...
240

825cdcb1a   Namhyung Kim   splice: add wakeu...
241
242
  	if (do_wakeup)
  		wakeup_pipe_readers(pipe);
5274f052e   Jens Axboe   [PATCH] Introduce...
243

00de00bda   Jens Axboe   splice: fix leak ...
244
  	while (page_nr < spd_pages)
bbdfc2f70   Jens Axboe   [SPLICE]: Don't a...
245
  		spd->spd_release(spd, page_nr++);
5274f052e   Jens Axboe   [PATCH] Introduce...
246
247
248
  
  	return ret;
  }
2b514574f   Hannes Frederic Sowa   net: af_unix: imp...
249
  EXPORT_SYMBOL_GPL(splice_to_pipe);
5274f052e   Jens Axboe   [PATCH] Introduce...
250

708e3508c   Hugh Dickins   tmpfs: clone shme...
251
  void spd_release_page(struct splice_pipe_desc *spd, unsigned int i)
bbdfc2f70   Jens Axboe   [SPLICE]: Don't a...
252
  {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
253
  	put_page(spd->pages[i]);
bbdfc2f70   Jens Axboe   [SPLICE]: Don't a...
254
  }
35f3d14db   Jens Axboe   pipe: add support...
255
256
257
258
  /*
   * Check if we need to grow the arrays holding pages and partial page
   * descriptions.
   */
047fe3605   Eric Dumazet   splice: fix racy ...
259
  int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd)
35f3d14db   Jens Axboe   pipe: add support...
260
  {
047fe3605   Eric Dumazet   splice: fix racy ...
261
262
263
264
  	unsigned int buffers = ACCESS_ONCE(pipe->buffers);
  
  	spd->nr_pages_max = buffers;
  	if (buffers <= PIPE_DEF_BUFFERS)
35f3d14db   Jens Axboe   pipe: add support...
265
  		return 0;
047fe3605   Eric Dumazet   splice: fix racy ...
266
267
  	spd->pages = kmalloc(buffers * sizeof(struct page *), GFP_KERNEL);
  	spd->partial = kmalloc(buffers * sizeof(struct partial_page), GFP_KERNEL);
35f3d14db   Jens Axboe   pipe: add support...
268
269
270
271
272
273
274
275
  
  	if (spd->pages && spd->partial)
  		return 0;
  
  	kfree(spd->pages);
  	kfree(spd->partial);
  	return -ENOMEM;
  }
047fe3605   Eric Dumazet   splice: fix racy ...
276
  void splice_shrink_spd(struct splice_pipe_desc *spd)
35f3d14db   Jens Axboe   pipe: add support...
277
  {
047fe3605   Eric Dumazet   splice: fix racy ...
278
  	if (spd->nr_pages_max <= PIPE_DEF_BUFFERS)
35f3d14db   Jens Axboe   pipe: add support...
279
280
281
282
283
  		return;
  
  	kfree(spd->pages);
  	kfree(spd->partial);
  }
3a326a2ce   Ingo Molnar   [PATCH] introduce...
284
  static int
cbb7e577e   Jens Axboe   [PATCH] splice: p...
285
286
287
  __generic_file_splice_read(struct file *in, loff_t *ppos,
  			   struct pipe_inode_info *pipe, size_t len,
  			   unsigned int flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
288
289
  {
  	struct address_space *mapping = in->f_mapping;
d8983910a   Fengguang Wu   readahead: pass r...
290
  	unsigned int loff, nr_pages, req_pages;
35f3d14db   Jens Axboe   pipe: add support...
291
292
  	struct page *pages[PIPE_DEF_BUFFERS];
  	struct partial_page partial[PIPE_DEF_BUFFERS];
5274f052e   Jens Axboe   [PATCH] Introduce...
293
  	struct page *page;
91ad66ef4   Jens Axboe   [PATCH] splice: c...
294
295
  	pgoff_t index, end_index;
  	loff_t isize;
eb20796bf   Jens Axboe   [PATCH] splice: m...
296
  	int error, page_nr;
912d35f86   Jens Axboe   [PATCH] Add suppo...
297
298
299
  	struct splice_pipe_desc spd = {
  		.pages = pages,
  		.partial = partial,
047fe3605   Eric Dumazet   splice: fix racy ...
300
  		.nr_pages_max = PIPE_DEF_BUFFERS,
912d35f86   Jens Axboe   [PATCH] Add suppo...
301
302
  		.flags = flags,
  		.ops = &page_cache_pipe_buf_ops,
bbdfc2f70   Jens Axboe   [SPLICE]: Don't a...
303
  		.spd_release = spd_release_page,
912d35f86   Jens Axboe   [PATCH] Add suppo...
304
  	};
5274f052e   Jens Axboe   [PATCH] Introduce...
305

35f3d14db   Jens Axboe   pipe: add support...
306
307
  	if (splice_grow_spd(pipe, &spd))
  		return -ENOMEM;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
308
309
310
  	index = *ppos >> PAGE_SHIFT;
  	loff = *ppos & ~PAGE_MASK;
  	req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
047fe3605   Eric Dumazet   splice: fix racy ...
311
  	nr_pages = min(req_pages, spd.nr_pages_max);
5274f052e   Jens Axboe   [PATCH] Introduce...
312
313
  
  	/*
eb20796bf   Jens Axboe   [PATCH] splice: m...
314
315
  	 * Lookup the (hopefully) full range of pages we need.
  	 */
35f3d14db   Jens Axboe   pipe: add support...
316
  	spd.nr_pages = find_get_pages_contig(mapping, index, nr_pages, spd.pages);
431a4820b   Fengguang Wu   readahead: move s...
317
  	index += spd.nr_pages;
82aa5d618   Jens Axboe   [PATCH] splice: f...
318

eb20796bf   Jens Axboe   [PATCH] splice: m...
319
320
  	/*
  	 * If find_get_pages_contig() returned fewer pages than we needed,
431a4820b   Fengguang Wu   readahead: move s...
321
  	 * readahead/allocate the rest and fill in the holes.
eb20796bf   Jens Axboe   [PATCH] splice: m...
322
  	 */
431a4820b   Fengguang Wu   readahead: move s...
323
  	if (spd.nr_pages < nr_pages)
cf914a7d6   Rusty Russell   readahead: split ...
324
325
  		page_cache_sync_readahead(mapping, &in->f_ra, in,
  				index, req_pages - spd.nr_pages);
431a4820b   Fengguang Wu   readahead: move s...
326

932cc6d4f   Jens Axboe   splice: completel...
327
  	error = 0;
eb20796bf   Jens Axboe   [PATCH] splice: m...
328
  	while (spd.nr_pages < nr_pages) {
82aa5d618   Jens Axboe   [PATCH] splice: f...
329
  		/*
eb20796bf   Jens Axboe   [PATCH] splice: m...
330
331
  		 * Page could be there, find_get_pages_contig() breaks on
  		 * the first hole.
5274f052e   Jens Axboe   [PATCH] Introduce...
332
  		 */
7480a9043   Jens Axboe   [PATCH] splice: s...
333
334
  		page = find_get_page(mapping, index);
  		if (!page) {
e27dedd84   Jens Axboe   [PATCH] splice: c...
335
  			/*
eb20796bf   Jens Axboe   [PATCH] splice: m...
336
  			 * page didn't exist, allocate one.
7480a9043   Jens Axboe   [PATCH] splice: s...
337
338
339
340
341
342
  			 */
  			page = page_cache_alloc_cold(mapping);
  			if (!page)
  				break;
  
  			error = add_to_page_cache_lru(page, mapping, index,
c62d25556   Michal Hocko   mm, fs: introduce...
343
  				   mapping_gfp_constraint(mapping, GFP_KERNEL));
7480a9043   Jens Axboe   [PATCH] splice: s...
344
  			if (unlikely(error)) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
345
  				put_page(page);
a0548871e   Jens Axboe   [PATCH] splice: r...
346
347
  				if (error == -EEXIST)
  					continue;
7480a9043   Jens Axboe   [PATCH] splice: s...
348
349
  				break;
  			}
eb20796bf   Jens Axboe   [PATCH] splice: m...
350
351
352
353
354
  			/*
  			 * add_to_page_cache() locks the page, unlock it
  			 * to avoid convoluting the logic below even more.
  			 */
  			unlock_page(page);
7480a9043   Jens Axboe   [PATCH] splice: s...
355
  		}
35f3d14db   Jens Axboe   pipe: add support...
356
  		spd.pages[spd.nr_pages++] = page;
eb20796bf   Jens Axboe   [PATCH] splice: m...
357
358
359
360
361
362
363
  		index++;
  	}
  
  	/*
  	 * Now loop over the map and see if we need to start IO on any
  	 * pages, fill in the partial map, etc.
  	 */
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
364
  	index = *ppos >> PAGE_SHIFT;
eb20796bf   Jens Axboe   [PATCH] splice: m...
365
366
367
368
369
370
371
372
373
374
375
  	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
  		 */
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
376
  		this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
35f3d14db   Jens Axboe   pipe: add support...
377
  		page = spd.pages[page_nr];
eb20796bf   Jens Axboe   [PATCH] splice: m...
378

a08a166fe   Fengguang Wu   readahead: conver...
379
  		if (PageReadahead(page))
cf914a7d6   Rusty Russell   readahead: split ...
380
  			page_cache_async_readahead(mapping, &in->f_ra, in,
d8983910a   Fengguang Wu   readahead: pass r...
381
  					page, index, req_pages - page_nr);
a08a166fe   Fengguang Wu   readahead: conver...
382

7480a9043   Jens Axboe   [PATCH] splice: s...
383
384
385
386
  		/*
  		 * If the page isn't uptodate, we may need to start io on it
  		 */
  		if (!PageUptodate(page)) {
6965031d3   Miklos Szeredi   splice: fix misus...
387
  			lock_page(page);
7480a9043   Jens Axboe   [PATCH] splice: s...
388
389
  
  			/*
32502b841   Miklos Szeredi   splice: fix gener...
390
391
392
393
  			 * 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.
7480a9043   Jens Axboe   [PATCH] splice: s...
394
395
396
  			 */
  			if (!page->mapping) {
  				unlock_page(page);
90330e689   Abhi Das   fs: __generic_fil...
397
  retry_lookup:
32502b841   Miklos Szeredi   splice: fix gener...
398
399
400
401
402
403
404
  				page = find_or_create_page(mapping, index,
  						mapping_gfp_mask(mapping));
  
  				if (!page) {
  					error = -ENOMEM;
  					break;
  				}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
405
  				put_page(spd.pages[page_nr]);
35f3d14db   Jens Axboe   pipe: add support...
406
  				spd.pages[page_nr] = page;
7480a9043   Jens Axboe   [PATCH] splice: s...
407
408
409
410
411
412
413
414
  			}
  			/*
  			 * page was already under io and is now done, great
  			 */
  			if (PageUptodate(page)) {
  				unlock_page(page);
  				goto fill_it;
  			}
5274f052e   Jens Axboe   [PATCH] Introduce...
415

7480a9043   Jens Axboe   [PATCH] splice: s...
416
417
418
419
  			/*
  			 * need to read in the page
  			 */
  			error = mapping->a_ops->readpage(in, page);
5274f052e   Jens Axboe   [PATCH] Introduce...
420
  			if (unlikely(error)) {
eb20796bf   Jens Axboe   [PATCH] splice: m...
421
  				/*
90330e689   Abhi Das   fs: __generic_fil...
422
  				 * Re-lookup the page
eb20796bf   Jens Axboe   [PATCH] splice: m...
423
  				 */
7480a9043   Jens Axboe   [PATCH] splice: s...
424
  				if (error == AOP_TRUNCATED_PAGE)
90330e689   Abhi Das   fs: __generic_fil...
425
  					goto retry_lookup;
eb20796bf   Jens Axboe   [PATCH] splice: m...
426

5274f052e   Jens Axboe   [PATCH] Introduce...
427
428
  				break;
  			}
620a324b7   Jens Axboe   splice: __generic...
429
430
431
432
433
434
  		}
  fill_it:
  		/*
  		 * i_size must be checked after PageUptodate.
  		 */
  		isize = i_size_read(mapping->host);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
435
  		end_index = (isize - 1) >> PAGE_SHIFT;
620a324b7   Jens Axboe   splice: __generic...
436
437
438
439
440
441
442
443
444
  		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;
91ad66ef4   Jens Axboe   [PATCH] splice: c...
445
446
  
  			/*
620a324b7   Jens Axboe   splice: __generic...
447
  			 * max good bytes in this page
91ad66ef4   Jens Axboe   [PATCH] splice: c...
448
  			 */
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
449
  			plen = ((isize - 1) & ~PAGE_MASK) + 1;
620a324b7   Jens Axboe   splice: __generic...
450
  			if (plen <= loff)
91ad66ef4   Jens Axboe   [PATCH] splice: c...
451
  				break;
91ad66ef4   Jens Axboe   [PATCH] splice: c...
452
453
  
  			/*
620a324b7   Jens Axboe   splice: __generic...
454
  			 * force quit after adding this page
91ad66ef4   Jens Axboe   [PATCH] splice: c...
455
  			 */
620a324b7   Jens Axboe   splice: __generic...
456
457
  			this_len = min(this_len, plen - loff);
  			len = this_len;
5274f052e   Jens Axboe   [PATCH] Introduce...
458
  		}
620a324b7   Jens Axboe   splice: __generic...
459

35f3d14db   Jens Axboe   pipe: add support...
460
461
  		spd.partial[page_nr].offset = loff;
  		spd.partial[page_nr].len = this_len;
82aa5d618   Jens Axboe   [PATCH] splice: f...
462
  		len -= this_len;
91ad66ef4   Jens Axboe   [PATCH] splice: c...
463
  		loff = 0;
eb20796bf   Jens Axboe   [PATCH] splice: m...
464
465
  		spd.nr_pages++;
  		index++;
5274f052e   Jens Axboe   [PATCH] Introduce...
466
  	}
eb20796bf   Jens Axboe   [PATCH] splice: m...
467
  	/*
475ecade6   Hugh Dickins   splice: __generic...
468
  	 * Release any pages at the end, if we quit early. 'page_nr' is how far
eb20796bf   Jens Axboe   [PATCH] splice: m...
469
470
471
  	 * we got, 'nr_pages' is how many pages are in the map.
  	 */
  	while (page_nr < nr_pages)
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
472
473
  		put_page(spd.pages[page_nr++]);
  	in->f_ra.prev_pos = (loff_t)index << PAGE_SHIFT;
eb20796bf   Jens Axboe   [PATCH] splice: m...
474

912d35f86   Jens Axboe   [PATCH] Add suppo...
475
  	if (spd.nr_pages)
35f3d14db   Jens Axboe   pipe: add support...
476
  		error = splice_to_pipe(pipe, &spd);
5274f052e   Jens Axboe   [PATCH] Introduce...
477

047fe3605   Eric Dumazet   splice: fix racy ...
478
  	splice_shrink_spd(&spd);
7480a9043   Jens Axboe   [PATCH] splice: s...
479
  	return error;
5274f052e   Jens Axboe   [PATCH] Introduce...
480
  }
83f9135bd   Jens Axboe   [PATCH] splice: a...
481
482
483
  /**
   * generic_file_splice_read - splice data from file to a pipe
   * @in:		file to splice from
932cc6d4f   Jens Axboe   splice: completel...
484
   * @ppos:	position in @in
83f9135bd   Jens Axboe   [PATCH] splice: a...
485
486
487
488
   * @pipe:	pipe to splice to
   * @len:	number of bytes to splice
   * @flags:	splice modifier flags
   *
932cc6d4f   Jens Axboe   splice: completel...
489
490
491
492
493
   * 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.
   *
83f9135bd   Jens Axboe   [PATCH] splice: a...
494
   */
cbb7e577e   Jens Axboe   [PATCH] splice: p...
495
496
497
  ssize_t generic_file_splice_read(struct file *in, loff_t *ppos,
  				 struct pipe_inode_info *pipe, size_t len,
  				 unsigned int flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
498
  {
d366d3988   Jens Axboe   splice: move inod...
499
  	loff_t isize, left;
8191ecd1d   Jens Axboe   splice: fix infin...
500
  	int ret;
d366d3988   Jens Axboe   splice: move inod...
501

be64f884b   Boaz Harrosh   dax: unify ext2/4...
502
503
  	if (IS_DAX(in->f_mapping->host))
  		return default_file_splice_read(in, ppos, pipe, len, flags);
d366d3988   Jens Axboe   splice: move inod...
504
505
506
507
508
509
510
  	isize = i_size_read(in->f_mapping->host);
  	if (unlikely(*ppos >= isize))
  		return 0;
  
  	left = isize - *ppos;
  	if (unlikely(left < len))
  		len = left;
5274f052e   Jens Axboe   [PATCH] Introduce...
511

8191ecd1d   Jens Axboe   splice: fix infin...
512
  	ret = __generic_file_splice_read(in, ppos, pipe, len, flags);
723590ed5   Miklos Szeredi   splice: update mt...
513
  	if (ret > 0) {
cbb7e577e   Jens Axboe   [PATCH] splice: p...
514
  		*ppos += ret;
723590ed5   Miklos Szeredi   splice: update mt...
515
516
  		file_accessed(in);
  	}
5274f052e   Jens Axboe   [PATCH] Introduce...
517
518
519
  
  	return ret;
  }
059a8f373   Jens Axboe   [PATCH] splice: e...
520
  EXPORT_SYMBOL(generic_file_splice_read);
6818173bd   Miklos Szeredi   splice: implement...
521
522
  static const struct pipe_buf_operations default_pipe_buf_ops = {
  	.can_merge = 0,
6818173bd   Miklos Szeredi   splice: implement...
523
524
525
526
527
  	.confirm = generic_pipe_buf_confirm,
  	.release = generic_pipe_buf_release,
  	.steal = generic_pipe_buf_steal,
  	.get = generic_pipe_buf_get,
  };
28a625cbc   Miklos Szeredi   fuse: fix pipe_bu...
528
529
530
531
532
533
534
535
536
  static int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
  				    struct pipe_buffer *buf)
  {
  	return 1;
  }
  
  /* Pipe buffer operations for a socket and similar. */
  const struct pipe_buf_operations nosteal_pipe_buf_ops = {
  	.can_merge = 0,
28a625cbc   Miklos Szeredi   fuse: fix pipe_bu...
537
538
539
540
541
542
  	.confirm = generic_pipe_buf_confirm,
  	.release = generic_pipe_buf_release,
  	.steal = generic_pipe_buf_nosteal,
  	.get = generic_pipe_buf_get,
  };
  EXPORT_SYMBOL(nosteal_pipe_buf_ops);
6818173bd   Miklos Szeredi   splice: implement...
543
544
545
546
547
548
549
550
551
552
  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() */
793b80ef1   Christoph Hellwig   vfs: pass a flags...
553
  	res = vfs_readv(file, (const struct iovec __user *)vec, vlen, &pos, 0);
6818173bd   Miklos Szeredi   splice: implement...
554
555
556
557
  	set_fs(old_fs);
  
  	return res;
  }
7bb307e89   Al Viro   export kernel_wri...
558
  ssize_t kernel_write(struct file *file, const char *buf, size_t count,
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
559
  			    loff_t pos)
0b0a47f5c   Miklos Szeredi   splice: implement...
560
561
562
563
564
565
566
  {
  	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() */
7bb307e89   Al Viro   export kernel_wri...
567
  	res = vfs_write(file, (__force const char __user *)buf, count, &pos);
0b0a47f5c   Miklos Szeredi   splice: implement...
568
569
570
571
  	set_fs(old_fs);
  
  	return res;
  }
7bb307e89   Al Viro   export kernel_wri...
572
  EXPORT_SYMBOL(kernel_write);
0b0a47f5c   Miklos Szeredi   splice: implement...
573

6818173bd   Miklos Szeredi   splice: implement...
574
575
576
577
578
579
580
  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;
35f3d14db   Jens Axboe   pipe: add support...
581
582
583
  	struct page *pages[PIPE_DEF_BUFFERS];
  	struct partial_page partial[PIPE_DEF_BUFFERS];
  	struct iovec *vec, __vec[PIPE_DEF_BUFFERS];
6818173bd   Miklos Szeredi   splice: implement...
584
585
586
587
588
589
590
  	ssize_t res;
  	size_t this_len;
  	int error;
  	int i;
  	struct splice_pipe_desc spd = {
  		.pages = pages,
  		.partial = partial,
047fe3605   Eric Dumazet   splice: fix racy ...
591
  		.nr_pages_max = PIPE_DEF_BUFFERS,
6818173bd   Miklos Szeredi   splice: implement...
592
593
594
595
  		.flags = flags,
  		.ops = &default_pipe_buf_ops,
  		.spd_release = spd_release_page,
  	};
35f3d14db   Jens Axboe   pipe: add support...
596
597
598
599
600
  	if (splice_grow_spd(pipe, &spd))
  		return -ENOMEM;
  
  	res = -ENOMEM;
  	vec = __vec;
047fe3605   Eric Dumazet   splice: fix racy ...
601
602
  	if (spd.nr_pages_max > PIPE_DEF_BUFFERS) {
  		vec = kmalloc(spd.nr_pages_max * sizeof(struct iovec), GFP_KERNEL);
35f3d14db   Jens Axboe   pipe: add support...
603
604
605
  		if (!vec)
  			goto shrink_ret;
  	}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
606
607
  	offset = *ppos & ~PAGE_MASK;
  	nr_pages = (len + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
6818173bd   Miklos Szeredi   splice: implement...
608

047fe3605   Eric Dumazet   splice: fix racy ...
609
  	for (i = 0; i < nr_pages && i < spd.nr_pages_max && len; i++) {
6818173bd   Miklos Szeredi   splice: implement...
610
  		struct page *page;
4f2312285   Jens Axboe   splice: fix repea...
611
  		page = alloc_page(GFP_USER);
6818173bd   Miklos Szeredi   splice: implement...
612
613
614
  		error = -ENOMEM;
  		if (!page)
  			goto err;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
615
  		this_len = min_t(size_t, len, PAGE_SIZE - offset);
4f2312285   Jens Axboe   splice: fix repea...
616
  		vec[i].iov_base = (void __user *) page_address(page);
6818173bd   Miklos Szeredi   splice: implement...
617
  		vec[i].iov_len = this_len;
35f3d14db   Jens Axboe   pipe: add support...
618
  		spd.pages[i] = page;
6818173bd   Miklos Szeredi   splice: implement...
619
620
621
622
623
624
  		spd.nr_pages++;
  		len -= this_len;
  		offset = 0;
  	}
  
  	res = kernel_readv(in, vec, spd.nr_pages, *ppos);
77f6bf57b   Andrew Morton   splice: fix error...
625
626
  	if (res < 0) {
  		error = res;
6818173bd   Miklos Szeredi   splice: implement...
627
  		goto err;
77f6bf57b   Andrew Morton   splice: fix error...
628
  	}
6818173bd   Miklos Szeredi   splice: implement...
629
630
631
632
633
634
635
  
  	error = 0;
  	if (!res)
  		goto err;
  
  	nr_freed = 0;
  	for (i = 0; i < spd.nr_pages; i++) {
6818173bd   Miklos Szeredi   splice: implement...
636
  		this_len = min_t(size_t, vec[i].iov_len, res);
35f3d14db   Jens Axboe   pipe: add support...
637
638
  		spd.partial[i].offset = 0;
  		spd.partial[i].len = this_len;
6818173bd   Miklos Szeredi   splice: implement...
639
  		if (!this_len) {
35f3d14db   Jens Axboe   pipe: add support...
640
641
  			__free_page(spd.pages[i]);
  			spd.pages[i] = NULL;
6818173bd   Miklos Szeredi   splice: implement...
642
643
644
645
646
647
648
649
650
  			nr_freed++;
  		}
  		res -= this_len;
  	}
  	spd.nr_pages -= nr_freed;
  
  	res = splice_to_pipe(pipe, &spd);
  	if (res > 0)
  		*ppos += res;
35f3d14db   Jens Axboe   pipe: add support...
651
652
653
  shrink_ret:
  	if (vec != __vec)
  		kfree(vec);
047fe3605   Eric Dumazet   splice: fix racy ...
654
  	splice_shrink_spd(&spd);
6818173bd   Miklos Szeredi   splice: implement...
655
656
657
  	return res;
  
  err:
4f2312285   Jens Axboe   splice: fix repea...
658
  	for (i = 0; i < spd.nr_pages; i++)
35f3d14db   Jens Axboe   pipe: add support...
659
  		__free_page(spd.pages[i]);
4f2312285   Jens Axboe   splice: fix repea...
660

35f3d14db   Jens Axboe   pipe: add support...
661
662
  	res = error;
  	goto shrink_ret;
6818173bd   Miklos Szeredi   splice: implement...
663
664
  }
  EXPORT_SYMBOL(default_file_splice_read);
5274f052e   Jens Axboe   [PATCH] Introduce...
665
  /*
4f6f0bd2f   Jens Axboe   [PATCH] splice: i...
666
   * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
016b661e2   Jens Axboe   [PATCH] splice: f...
667
   * using sendpage(). Return the number of bytes sent.
5274f052e   Jens Axboe   [PATCH] Introduce...
668
   */
76ad4d111   Jens Axboe   [PATCH] splice: r...
669
  static int pipe_to_sendpage(struct pipe_inode_info *pipe,
5274f052e   Jens Axboe   [PATCH] Introduce...
670
671
  			    struct pipe_buffer *buf, struct splice_desc *sd)
  {
6a14b90bb   Jens Axboe   vmsplice: add vms...
672
  	struct file *file = sd->u.file;
5274f052e   Jens Axboe   [PATCH] Introduce...
673
  	loff_t pos = sd->pos;
a8adbe378   MichaÅ‚ MirosÅ‚aw   fs/splice: Pull b...
674
  	int more;
5274f052e   Jens Axboe   [PATCH] Introduce...
675

72c2d5319   Al Viro   file->f_op is nev...
676
  	if (!likely(file->f_op->sendpage))
a8adbe378   MichaÅ‚ MirosÅ‚aw   fs/splice: Pull b...
677
  		return -EINVAL;
35f9c09fe   Eric Dumazet   tcp: tcp_sendpage...
678
  	more = (sd->flags & SPLICE_F_MORE) ? MSG_MORE : 0;
ae62ca7b0   Eric Dumazet   tcp: fix MSG_SEND...
679
680
  
  	if (sd->len < sd->total_len && pipe->nrbufs > 1)
35f9c09fe   Eric Dumazet   tcp: tcp_sendpage...
681
  		more |= MSG_SENDPAGE_NOTLAST;
ae62ca7b0   Eric Dumazet   tcp: fix MSG_SEND...
682

a8adbe378   MichaÅ‚ MirosÅ‚aw   fs/splice: Pull b...
683
684
  	return file->f_op->sendpage(file, buf->page, buf->offset,
  				    sd->len, &pos, more);
5274f052e   Jens Axboe   [PATCH] Introduce...
685
  }
b3c2d2ddd   Miklos Szeredi   splice: split up ...
686
687
688
689
690
691
692
  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);
  }
932cc6d4f   Jens Axboe   splice: completel...
693
  /**
b3c2d2ddd   Miklos Szeredi   splice: split up ...
694
   * splice_from_pipe_feed - feed available data from a pipe to a file
932cc6d4f   Jens Axboe   splice: completel...
695
696
697
698
699
   * @pipe:	pipe to splice from
   * @sd:		information to @actor
   * @actor:	handler that splices the data
   *
   * Description:
b3c2d2ddd   Miklos Szeredi   splice: split up ...
700
701
702
703
704
705
706
   *    This function loops over the pipe and calls @actor to do the
   *    actual moving of a single struct pipe_buffer to the desired
   *    destination.  It returns when there's no more buffers left in
   *    the pipe or if the requested number of bytes (@sd->total_len)
   *    have been copied.  It returns a positive number (one) if the
   *    pipe needs to be filled with more data, zero if the required
   *    number of bytes have been copied and -errno on error.
932cc6d4f   Jens Axboe   splice: completel...
707
   *
b3c2d2ddd   Miklos Szeredi   splice: split up ...
708
709
710
711
   *    This, together with splice_from_pipe_{begin,end,next}, may be
   *    used to implement the functionality of __splice_from_pipe() when
   *    locking is required around copying the pipe buffers to the
   *    destination.
83f9135bd   Jens Axboe   [PATCH] splice: a...
712
   */
96f9bc8fb   Al Viro   fs/splice.c: remo...
713
  static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd,
b3c2d2ddd   Miklos Szeredi   splice: split up ...
714
  			  splice_actor *actor)
5274f052e   Jens Axboe   [PATCH] Introduce...
715
  {
b3c2d2ddd   Miklos Szeredi   splice: split up ...
716
  	int ret;
5274f052e   Jens Axboe   [PATCH] Introduce...
717

b3c2d2ddd   Miklos Szeredi   splice: split up ...
718
719
720
  	while (pipe->nrbufs) {
  		struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  		const struct pipe_buf_operations *ops = buf->ops;
5274f052e   Jens Axboe   [PATCH] Introduce...
721

b3c2d2ddd   Miklos Szeredi   splice: split up ...
722
723
724
  		sd->len = buf->len;
  		if (sd->len > sd->total_len)
  			sd->len = sd->total_len;
5274f052e   Jens Axboe   [PATCH] Introduce...
725

a8adbe378   MichaÅ‚ MirosÅ‚aw   fs/splice: Pull b...
726
727
  		ret = buf->ops->confirm(pipe, buf);
  		if (unlikely(ret)) {
b3c2d2ddd   Miklos Szeredi   splice: split up ...
728
729
730
731
  			if (ret == -ENODATA)
  				ret = 0;
  			return ret;
  		}
a8adbe378   MichaÅ‚ MirosÅ‚aw   fs/splice: Pull b...
732
733
734
735
  
  		ret = actor(pipe, buf, sd);
  		if (ret <= 0)
  			return ret;
b3c2d2ddd   Miklos Szeredi   splice: split up ...
736
737
738
739
740
741
742
743
744
745
746
  		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);
35f3d14db   Jens Axboe   pipe: add support...
747
  			pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
b3c2d2ddd   Miklos Szeredi   splice: split up ...
748
  			pipe->nrbufs--;
6447a3cf1   Al Viro   get rid of pipe->...
749
  			if (pipe->files)
b3c2d2ddd   Miklos Szeredi   splice: split up ...
750
751
  				sd->need_wakeup = true;
  		}
5274f052e   Jens Axboe   [PATCH] Introduce...
752

b3c2d2ddd   Miklos Szeredi   splice: split up ...
753
754
755
  		if (!sd->total_len)
  			return 0;
  	}
5274f052e   Jens Axboe   [PATCH] Introduce...
756

b3c2d2ddd   Miklos Szeredi   splice: split up ...
757
758
  	return 1;
  }
5274f052e   Jens Axboe   [PATCH] Introduce...
759

b3c2d2ddd   Miklos Szeredi   splice: split up ...
760
761
762
763
764
765
766
767
768
769
  /**
   * splice_from_pipe_next - wait for some data to splice from
   * @pipe:	pipe to splice from
   * @sd:		information about the splice operation
   *
   * Description:
   *    This function will wait for some data and return a positive
   *    value (one) if pipe buffers are available.  It will return zero
   *    or -errno if no more data needs to be spliced.
   */
96f9bc8fb   Al Viro   fs/splice.c: remo...
770
  static int splice_from_pipe_next(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2ddd   Miklos Szeredi   splice: split up ...
771
  {
c725bfce7   Jan Kara   vfs: Make sendfil...
772
773
774
775
776
777
  	/*
  	 * Check for signal early to make process killable when there are
  	 * always buffers available
  	 */
  	if (signal_pending(current))
  		return -ERESTARTSYS;
b3c2d2ddd   Miklos Szeredi   splice: split up ...
778
779
780
  	while (!pipe->nrbufs) {
  		if (!pipe->writers)
  			return 0;
016b661e2   Jens Axboe   [PATCH] splice: f...
781

b3c2d2ddd   Miklos Szeredi   splice: split up ...
782
783
  		if (!pipe->waiting_writers && sd->num_spliced)
  			return 0;
73d62d83e   Ingo Molnar   [PATCH] splice: c...
784

b3c2d2ddd   Miklos Szeredi   splice: split up ...
785
786
  		if (sd->flags & SPLICE_F_NONBLOCK)
  			return -EAGAIN;
5274f052e   Jens Axboe   [PATCH] Introduce...
787

b3c2d2ddd   Miklos Szeredi   splice: split up ...
788
789
  		if (signal_pending(current))
  			return -ERESTARTSYS;
5274f052e   Jens Axboe   [PATCH] Introduce...
790

b3c2d2ddd   Miklos Szeredi   splice: split up ...
791
792
793
  		if (sd->need_wakeup) {
  			wakeup_pipe_writers(pipe);
  			sd->need_wakeup = false;
5274f052e   Jens Axboe   [PATCH] Introduce...
794
  		}
b3c2d2ddd   Miklos Szeredi   splice: split up ...
795
796
  		pipe_wait(pipe);
  	}
29e350944   Linus Torvalds   splice: add SPLIC...
797

b3c2d2ddd   Miklos Szeredi   splice: split up ...
798
799
  	return 1;
  }
5274f052e   Jens Axboe   [PATCH] Introduce...
800

b3c2d2ddd   Miklos Szeredi   splice: split up ...
801
802
  /**
   * splice_from_pipe_begin - start splicing from pipe
b80901bbf   Randy Dunlap   splice: fix new k...
803
   * @sd:		information about the splice operation
b3c2d2ddd   Miklos Szeredi   splice: split up ...
804
805
806
807
808
809
   *
   * Description:
   *    This function should be called before a loop containing
   *    splice_from_pipe_next() and splice_from_pipe_feed() to
   *    initialize the necessary fields of @sd.
   */
96f9bc8fb   Al Viro   fs/splice.c: remo...
810
  static void splice_from_pipe_begin(struct splice_desc *sd)
b3c2d2ddd   Miklos Szeredi   splice: split up ...
811
812
813
814
  {
  	sd->num_spliced = 0;
  	sd->need_wakeup = false;
  }
5274f052e   Jens Axboe   [PATCH] Introduce...
815

b3c2d2ddd   Miklos Szeredi   splice: split up ...
816
817
818
819
820
821
822
823
824
825
  /**
   * splice_from_pipe_end - finish splicing from pipe
   * @pipe:	pipe to splice from
   * @sd:		information about the splice operation
   *
   * Description:
   *    This function will wake up pipe writers if necessary.  It should
   *    be called after a loop containing splice_from_pipe_next() and
   *    splice_from_pipe_feed().
   */
96f9bc8fb   Al Viro   fs/splice.c: remo...
826
  static void splice_from_pipe_end(struct pipe_inode_info *pipe, struct splice_desc *sd)
b3c2d2ddd   Miklos Szeredi   splice: split up ...
827
828
829
830
  {
  	if (sd->need_wakeup)
  		wakeup_pipe_writers(pipe);
  }
5274f052e   Jens Axboe   [PATCH] Introduce...
831

b3c2d2ddd   Miklos Szeredi   splice: split up ...
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
  /**
   * __splice_from_pipe - splice data from a pipe to given actor
   * @pipe:	pipe to splice from
   * @sd:		information to @actor
   * @actor:	handler that splices the data
   *
   * Description:
   *    This function does little more than loop over the pipe and call
   *    @actor to do the actual moving of a single struct pipe_buffer to
   *    the desired destination. See pipe_to_file, pipe_to_sendpage, or
   *    pipe_to_user.
   *
   */
  ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,
  			   splice_actor *actor)
  {
  	int ret;
5274f052e   Jens Axboe   [PATCH] Introduce...
849

b3c2d2ddd   Miklos Szeredi   splice: split up ...
850
851
  	splice_from_pipe_begin(sd);
  	do {
c2489e07c   Jan Kara   vfs: Avoid softlo...
852
  		cond_resched();
b3c2d2ddd   Miklos Szeredi   splice: split up ...
853
854
855
856
857
858
859
  		ret = splice_from_pipe_next(pipe, sd);
  		if (ret > 0)
  			ret = splice_from_pipe_feed(pipe, sd, actor);
  	} while (ret > 0);
  	splice_from_pipe_end(pipe, sd);
  
  	return sd->num_spliced ? sd->num_spliced : ret;
5274f052e   Jens Axboe   [PATCH] Introduce...
860
  }
40bee44ea   Mark Fasheh   Export __splice_f...
861
  EXPORT_SYMBOL(__splice_from_pipe);
5274f052e   Jens Axboe   [PATCH] Introduce...
862

932cc6d4f   Jens Axboe   splice: completel...
863
864
865
866
867
868
869
870
871
872
  /**
   * splice_from_pipe - splice data from a pipe to a file
   * @pipe:	pipe to splice from
   * @out:	file to splice to
   * @ppos:	position in @out
   * @len:	how many bytes to splice
   * @flags:	splice modifier flags
   * @actor:	handler that splices the data
   *
   * Description:
2933970b9   Miklos Szeredi   splice: remove i_...
873
   *    See __splice_from_pipe. This function locks the pipe inode,
932cc6d4f   Jens Axboe   splice: completel...
874
875
876
   *    otherwise it's identical to __splice_from_pipe().
   *
   */
6da618098   Mark Fasheh   [PATCH] Introduce...
877
878
879
880
881
  ssize_t splice_from_pipe(struct pipe_inode_info *pipe, struct file *out,
  			 loff_t *ppos, size_t len, unsigned int flags,
  			 splice_actor *actor)
  {
  	ssize_t ret;
c66ab6fa7   Jens Axboe   splice: abstract ...
882
883
884
885
  	struct splice_desc sd = {
  		.total_len = len,
  		.flags = flags,
  		.pos = *ppos,
6a14b90bb   Jens Axboe   vmsplice: add vms...
886
  		.u.file = out,
c66ab6fa7   Jens Axboe   splice: abstract ...
887
  	};
6da618098   Mark Fasheh   [PATCH] Introduce...
888

61e0d47c3   Miklos Szeredi   splice: add helpe...
889
  	pipe_lock(pipe);
c66ab6fa7   Jens Axboe   splice: abstract ...
890
  	ret = __splice_from_pipe(pipe, &sd, actor);
61e0d47c3   Miklos Szeredi   splice: add helpe...
891
  	pipe_unlock(pipe);
6da618098   Mark Fasheh   [PATCH] Introduce...
892
893
894
895
896
  
  	return ret;
  }
  
  /**
8d0207652   Al Viro   ->splice_write() ...
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
   * iter_file_splice_write - splice data from a pipe to a file
   * @pipe:	pipe info
   * @out:	file to write to
   * @ppos:	position in @out
   * @len:	number of bytes to splice
   * @flags:	splice modifier flags
   *
   * Description:
   *    Will either move or copy pages (determined by @flags options) from
   *    the given pipe inode to the given file.
   *    This one is ->write_iter-based.
   *
   */
  ssize_t
  iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
  			  loff_t *ppos, size_t len, unsigned int flags)
  {
  	struct splice_desc sd = {
  		.total_len = len,
  		.flags = flags,
  		.pos = *ppos,
  		.u.file = out,
  	};
  	int nbufs = pipe->buffers;
  	struct bio_vec *array = kcalloc(nbufs, sizeof(struct bio_vec),
  					GFP_KERNEL);
  	ssize_t ret;
  
  	if (unlikely(!array))
  		return -ENOMEM;
  
  	pipe_lock(pipe);
  
  	splice_from_pipe_begin(&sd);
  	while (sd.total_len) {
  		struct iov_iter from;
8d0207652   Al Viro   ->splice_write() ...
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  		size_t left;
  		int n, idx;
  
  		ret = splice_from_pipe_next(pipe, &sd);
  		if (ret <= 0)
  			break;
  
  		if (unlikely(nbufs < pipe->buffers)) {
  			kfree(array);
  			nbufs = pipe->buffers;
  			array = kcalloc(nbufs, sizeof(struct bio_vec),
  					GFP_KERNEL);
  			if (!array) {
  				ret = -ENOMEM;
  				break;
  			}
  		}
  
  		/* build the vector */
  		left = sd.total_len;
  		for (n = 0, idx = pipe->curbuf; left && n < pipe->nrbufs; n++, idx++) {
  			struct pipe_buffer *buf = pipe->bufs + idx;
  			size_t this_len = buf->len;
  
  			if (this_len > left)
  				this_len = left;
  
  			if (idx == pipe->buffers - 1)
  				idx = -1;
  
  			ret = buf->ops->confirm(pipe, buf);
  			if (unlikely(ret)) {
  				if (ret == -ENODATA)
  					ret = 0;
  				goto done;
  			}
  
  			array[n].bv_page = buf->page;
  			array[n].bv_len = this_len;
  			array[n].bv_offset = buf->offset;
  			left -= this_len;
  		}
05afcb77e   Al Viro   new helper: iov_i...
975
976
  		iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
  			      sd.total_len - left);
dbe4e192a   Christoph Hellwig   fs: add vfs_iter_...
977
  		ret = vfs_iter_write(out, &from, &sd.pos);
8d0207652   Al Viro   ->splice_write() ...
978
979
980
981
982
  		if (ret <= 0)
  			break;
  
  		sd.num_spliced += ret;
  		sd.total_len -= ret;
dbe4e192a   Christoph Hellwig   fs: add vfs_iter_...
983
  		*ppos = sd.pos;
8d0207652   Al Viro   ->splice_write() ...
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  
  		/* dismiss the fully eaten buffers, adjust the partial one */
  		while (ret) {
  			struct pipe_buffer *buf = pipe->bufs + pipe->curbuf;
  			if (ret >= buf->len) {
  				const struct pipe_buf_operations *ops = buf->ops;
  				ret -= buf->len;
  				buf->len = 0;
  				buf->ops = NULL;
  				ops->release(pipe, buf);
  				pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  				pipe->nrbufs--;
  				if (pipe->files)
  					sd.need_wakeup = true;
  			} else {
  				buf->offset += ret;
  				buf->len -= ret;
  				ret = 0;
  			}
  		}
  	}
  done:
  	kfree(array);
  	splice_from_pipe_end(pipe, &sd);
  
  	pipe_unlock(pipe);
  
  	if (sd.num_spliced)
  		ret = sd.num_spliced;
  
  	return ret;
  }
  
  EXPORT_SYMBOL(iter_file_splice_write);
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1018
1019
  static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  			  struct splice_desc *sd)
0b0a47f5c   Miklos Szeredi   splice: implement...
1020
  {
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1021
1022
  	int ret;
  	void *data;
06ae43f34   Al Viro   Don't bother with...
1023
  	loff_t tmp = sd->pos;
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1024

fbb32750a   Al Viro   pipe: kill ->map(...
1025
  	data = kmap(buf->page);
06ae43f34   Al Viro   Don't bother with...
1026
  	ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
fbb32750a   Al Viro   pipe: kill ->map(...
1027
  	kunmap(buf->page);
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1028
1029
  
  	return ret;
0b0a47f5c   Miklos Szeredi   splice: implement...
1030
1031
1032
1033
1034
1035
  }
  
  static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
  					 struct file *out, loff_t *ppos,
  					 size_t len, unsigned int flags)
  {
b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1036
  	ssize_t ret;
0b0a47f5c   Miklos Szeredi   splice: implement...
1037

b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1038
1039
1040
  	ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
  	if (ret > 0)
  		*ppos += ret;
0b0a47f5c   Miklos Szeredi   splice: implement...
1041

b2858d7d1   Miklos Szeredi   splice: fix kmaps...
1042
  	return ret;
0b0a47f5c   Miklos Szeredi   splice: implement...
1043
  }
83f9135bd   Jens Axboe   [PATCH] splice: a...
1044
1045
  /**
   * generic_splice_sendpage - splice data from a pipe to a socket
932cc6d4f   Jens Axboe   splice: completel...
1046
   * @pipe:	pipe to splice from
83f9135bd   Jens Axboe   [PATCH] splice: a...
1047
   * @out:	socket to write to
932cc6d4f   Jens Axboe   splice: completel...
1048
   * @ppos:	position in @out
83f9135bd   Jens Axboe   [PATCH] splice: a...
1049
1050
1051
   * @len:	number of bytes to splice
   * @flags:	splice modifier flags
   *
932cc6d4f   Jens Axboe   splice: completel...
1052
1053
1054
   * Description:
   *    Will send @len bytes from the pipe to a network socket. No data copying
   *    is involved.
83f9135bd   Jens Axboe   [PATCH] splice: a...
1055
1056
   *
   */
3a326a2ce   Ingo Molnar   [PATCH] introduce...
1057
  ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1058
  				loff_t *ppos, size_t len, unsigned int flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
1059
  {
00522fb41   Jens Axboe   [PATCH] splice: r...
1060
  	return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendpage);
5274f052e   Jens Axboe   [PATCH] Introduce...
1061
  }
059a8f373   Jens Axboe   [PATCH] splice: e...
1062
  EXPORT_SYMBOL(generic_splice_sendpage);
a0f067802   Jeff Garzik   [PATCH] splice ex...
1063

83f9135bd   Jens Axboe   [PATCH] splice: a...
1064
1065
1066
  /*
   * Attempt to initiate a splice from pipe to file.
   */
3a326a2ce   Ingo Molnar   [PATCH] introduce...
1067
  static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1068
  			   loff_t *ppos, size_t len, unsigned int flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
1069
  {
0b0a47f5c   Miklos Szeredi   splice: implement...
1070
1071
  	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  				loff_t *, size_t, unsigned int);
5274f052e   Jens Axboe   [PATCH] Introduce...
1072

72c2d5319   Al Viro   file->f_op is nev...
1073
  	if (out->f_op->splice_write)
cc56f7de7   Changli Gao   sendfile(): check...
1074
1075
  		splice_write = out->f_op->splice_write;
  	else
0b0a47f5c   Miklos Szeredi   splice: implement...
1076
  		splice_write = default_file_splice_write;
500368f7f   Al Viro   lift file_*_write...
1077
  	return splice_write(pipe, out, ppos, len, flags);
5274f052e   Jens Axboe   [PATCH] Introduce...
1078
  }
83f9135bd   Jens Axboe   [PATCH] splice: a...
1079
1080
1081
  /*
   * Attempt to initiate a splice from a file to a pipe.
   */
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1082
1083
1084
  static long do_splice_to(struct file *in, loff_t *ppos,
  			 struct pipe_inode_info *pipe, size_t len,
  			 unsigned int flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
1085
  {
6818173bd   Miklos Szeredi   splice: implement...
1086
1087
  	ssize_t (*splice_read)(struct file *, loff_t *,
  			       struct pipe_inode_info *, size_t, unsigned int);
5274f052e   Jens Axboe   [PATCH] Introduce...
1088
  	int ret;
49570e9b2   Jens Axboe   [PATCH] splice: u...
1089
  	if (unlikely(!(in->f_mode & FMODE_READ)))
5274f052e   Jens Axboe   [PATCH] Introduce...
1090
  		return -EBADF;
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1091
  	ret = rw_verify_area(READ, in, ppos, len);
5274f052e   Jens Axboe   [PATCH] Introduce...
1092
1093
  	if (unlikely(ret < 0))
  		return ret;
03cc0789a   Al Viro   do_splice_to(): c...
1094
1095
  	if (unlikely(len > MAX_RW_COUNT))
  		len = MAX_RW_COUNT;
72c2d5319   Al Viro   file->f_op is nev...
1096
  	if (in->f_op->splice_read)
cc56f7de7   Changli Gao   sendfile(): check...
1097
1098
  		splice_read = in->f_op->splice_read;
  	else
6818173bd   Miklos Szeredi   splice: implement...
1099
1100
1101
  		splice_read = default_file_splice_read;
  
  	return splice_read(in, ppos, pipe, len, flags);
5274f052e   Jens Axboe   [PATCH] Introduce...
1102
  }
932cc6d4f   Jens Axboe   splice: completel...
1103
1104
1105
1106
1107
1108
1109
1110
1111
  /**
   * splice_direct_to_actor - splices data directly between two non-pipes
   * @in:		file to splice from
   * @sd:		actor information on where to splice to
   * @actor:	handles the data splicing
   *
   * Description:
   *    This is a special case helper to splice directly between two
   *    points, without requiring an explicit pipe. Internally an allocated
79685b8de   Randy Dunlap   docbook: add pipe...
1112
   *    pipe is cached in the process, and reused during the lifetime of
932cc6d4f   Jens Axboe   splice: completel...
1113
1114
   *    that process.
   *
c66ab6fa7   Jens Axboe   splice: abstract ...
1115
1116
1117
   */
  ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
  			       splice_direct_actor *actor)
b92ce5589   Jens Axboe   [PATCH] splice: a...
1118
1119
1120
1121
  {
  	struct pipe_inode_info *pipe;
  	long ret, bytes;
  	umode_t i_mode;
c66ab6fa7   Jens Axboe   splice: abstract ...
1122
  	size_t len;
0ff28d9f4   Christophe Leroy   splice: sendfile(...
1123
  	int i, flags, more;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1124
1125
1126
1127
1128
1129
  
  	/*
  	 * We require the input being a regular file, as we don't want to
  	 * randomly drop data for eg socket -> socket splicing. Use the
  	 * piped splicing for that!
  	 */
496ad9aa8   Al Viro   new helper: file_...
1130
  	i_mode = file_inode(in)->i_mode;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1131
1132
1133
1134
1135
1136
1137
1138
  	if (unlikely(!S_ISREG(i_mode) && !S_ISBLK(i_mode)))
  		return -EINVAL;
  
  	/*
  	 * neither in nor out is a pipe, setup an internal pipe attached to
  	 * 'out' and transfer the wanted data from 'in' to 'out' through that
  	 */
  	pipe = current->splice_pipe;
49570e9b2   Jens Axboe   [PATCH] splice: u...
1139
  	if (unlikely(!pipe)) {
7bee130e2   Al Viro   get rid of alloc_...
1140
  		pipe = alloc_pipe_info();
b92ce5589   Jens Axboe   [PATCH] splice: a...
1141
1142
1143
1144
1145
  		if (!pipe)
  			return -ENOMEM;
  
  		/*
  		 * We don't have an immediate reader, but we'll read the stuff
00522fb41   Jens Axboe   [PATCH] splice: r...
1146
  		 * out of the pipe right after the splice_to_pipe(). So set
b92ce5589   Jens Axboe   [PATCH] splice: a...
1147
1148
1149
1150
1151
1152
1153
1154
  		 * PIPE_READERS appropriately.
  		 */
  		pipe->readers = 1;
  
  		current->splice_pipe = pipe;
  	}
  
  	/*
73d62d83e   Ingo Molnar   [PATCH] splice: c...
1155
  	 * Do the splice.
b92ce5589   Jens Axboe   [PATCH] splice: a...
1156
1157
1158
  	 */
  	ret = 0;
  	bytes = 0;
c66ab6fa7   Jens Axboe   splice: abstract ...
1159
1160
1161
1162
1163
1164
1165
  	len = sd->total_len;
  	flags = sd->flags;
  
  	/*
  	 * Don't block on output, we have to drain the direct pipe.
  	 */
  	sd->flags &= ~SPLICE_F_NONBLOCK;
0ff28d9f4   Christophe Leroy   splice: sendfile(...
1166
  	more = sd->flags & SPLICE_F_MORE;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1167
1168
  
  	while (len) {
51a92c0f6   Jens Axboe   splice: fix offse...
1169
  		size_t read_len;
a82c53a0e   Tom Zanussi   splice: fix sendf...
1170
  		loff_t pos = sd->pos, prev_pos = pos;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1171

bcd4f3acb   Jens Axboe   splice: direct sp...
1172
  		ret = do_splice_to(in, &pos, pipe, len, flags);
51a92c0f6   Jens Axboe   splice: fix offse...
1173
  		if (unlikely(ret <= 0))
b92ce5589   Jens Axboe   [PATCH] splice: a...
1174
1175
1176
  			goto out_release;
  
  		read_len = ret;
c66ab6fa7   Jens Axboe   splice: abstract ...
1177
  		sd->total_len = read_len;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1178
1179
  
  		/*
0ff28d9f4   Christophe Leroy   splice: sendfile(...
1180
1181
1182
1183
1184
1185
1186
1187
1188
  		 * If more data is pending, set SPLICE_F_MORE
  		 * If this is the last data and SPLICE_F_MORE was not set
  		 * initially, clears it.
  		 */
  		if (read_len < len)
  			sd->flags |= SPLICE_F_MORE;
  		else if (!more)
  			sd->flags &= ~SPLICE_F_MORE;
  		/*
b92ce5589   Jens Axboe   [PATCH] splice: a...
1189
1190
1191
1192
  		 * NOTE: nonblocking mode only applies to the input. We
  		 * must not do the output in nonblocking mode as then we
  		 * could get stuck data in the internal pipe:
  		 */
c66ab6fa7   Jens Axboe   splice: abstract ...
1193
  		ret = actor(pipe, sd);
a82c53a0e   Tom Zanussi   splice: fix sendf...
1194
1195
  		if (unlikely(ret <= 0)) {
  			sd->pos = prev_pos;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1196
  			goto out_release;
a82c53a0e   Tom Zanussi   splice: fix sendf...
1197
  		}
b92ce5589   Jens Axboe   [PATCH] splice: a...
1198
1199
1200
  
  		bytes += ret;
  		len -= ret;
bcd4f3acb   Jens Axboe   splice: direct sp...
1201
  		sd->pos = pos;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1202

a82c53a0e   Tom Zanussi   splice: fix sendf...
1203
1204
  		if (ret < read_len) {
  			sd->pos = prev_pos + ret;
51a92c0f6   Jens Axboe   splice: fix offse...
1205
  			goto out_release;
a82c53a0e   Tom Zanussi   splice: fix sendf...
1206
  		}
b92ce5589   Jens Axboe   [PATCH] splice: a...
1207
  	}
9e97198db   Jens Axboe   splice: fix probl...
1208
  done:
b92ce5589   Jens Axboe   [PATCH] splice: a...
1209
  	pipe->nrbufs = pipe->curbuf = 0;
808487085   Jens Axboe   splice: always up...
1210
  	file_accessed(in);
b92ce5589   Jens Axboe   [PATCH] splice: a...
1211
1212
1213
1214
1215
1216
1217
  	return bytes;
  
  out_release:
  	/*
  	 * If we did an incomplete transfer we must release
  	 * the pipe buffers in question:
  	 */
35f3d14db   Jens Axboe   pipe: add support...
1218
  	for (i = 0; i < pipe->buffers; i++) {
b92ce5589   Jens Axboe   [PATCH] splice: a...
1219
1220
1221
1222
1223
1224
1225
  		struct pipe_buffer *buf = pipe->bufs + i;
  
  		if (buf->ops) {
  			buf->ops->release(pipe, buf);
  			buf->ops = NULL;
  		}
  	}
b92ce5589   Jens Axboe   [PATCH] splice: a...
1226

9e97198db   Jens Axboe   splice: fix probl...
1227
1228
  	if (!bytes)
  		bytes = ret;
c66ab6fa7   Jens Axboe   splice: abstract ...
1229

9e97198db   Jens Axboe   splice: fix probl...
1230
  	goto done;
c66ab6fa7   Jens Axboe   splice: abstract ...
1231
1232
1233
1234
1235
1236
  }
  EXPORT_SYMBOL(splice_direct_to_actor);
  
  static int direct_splice_actor(struct pipe_inode_info *pipe,
  			       struct splice_desc *sd)
  {
6a14b90bb   Jens Axboe   vmsplice: add vms...
1237
  	struct file *file = sd->u.file;
c66ab6fa7   Jens Axboe   splice: abstract ...
1238

7995bd287   Al Viro   splice: don't pas...
1239
  	return do_splice_from(pipe, file, sd->opos, sd->total_len,
2cb4b05e7   Changli Gao   splice: direct_sp...
1240
  			      sd->flags);
c66ab6fa7   Jens Axboe   splice: abstract ...
1241
  }
932cc6d4f   Jens Axboe   splice: completel...
1242
1243
1244
1245
1246
  /**
   * do_splice_direct - splices data directly between two files
   * @in:		file to splice from
   * @ppos:	input file offset
   * @out:	file to splice to
acdb37c36   Randy Dunlap   fs: fix new splic...
1247
   * @opos:	output file offset
932cc6d4f   Jens Axboe   splice: completel...
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
   * @len:	number of bytes to splice
   * @flags:	splice modifier flags
   *
   * Description:
   *    For use by do_sendfile(). splice can easily emulate sendfile, but
   *    doing it in the application would incur an extra system call
   *    (splice in + splice out, as compared to just sendfile()). So this helper
   *    can splice directly through a process-private pipe.
   *
   */
c66ab6fa7   Jens Axboe   splice: abstract ...
1258
  long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
7995bd287   Al Viro   splice: don't pas...
1259
  		      loff_t *opos, size_t len, unsigned int flags)
c66ab6fa7   Jens Axboe   splice: abstract ...
1260
1261
1262
1263
1264
1265
  {
  	struct splice_desc sd = {
  		.len		= len,
  		.total_len	= len,
  		.flags		= flags,
  		.pos		= *ppos,
6a14b90bb   Jens Axboe   vmsplice: add vms...
1266
  		.u.file		= out,
7995bd287   Al Viro   splice: don't pas...
1267
  		.opos		= opos,
c66ab6fa7   Jens Axboe   splice: abstract ...
1268
  	};
51a92c0f6   Jens Axboe   splice: fix offse...
1269
  	long ret;
c66ab6fa7   Jens Axboe   splice: abstract ...
1270

18c67cb9f   Al Viro   splice: lift chec...
1271
1272
1273
1274
1275
1276
1277
1278
1279
  	if (unlikely(!(out->f_mode & FMODE_WRITE)))
  		return -EBADF;
  
  	if (unlikely(out->f_flags & O_APPEND))
  		return -EINVAL;
  
  	ret = rw_verify_area(WRITE, out, opos, len);
  	if (unlikely(ret < 0))
  		return ret;
c66ab6fa7   Jens Axboe   splice: abstract ...
1280
  	ret = splice_direct_to_actor(in, &sd, direct_splice_actor);
51a92c0f6   Jens Axboe   splice: fix offse...
1281
  	if (ret > 0)
a82c53a0e   Tom Zanussi   splice: fix sendf...
1282
  		*ppos = sd.pos;
51a92c0f6   Jens Axboe   splice: fix offse...
1283

c66ab6fa7   Jens Axboe   splice: abstract ...
1284
  	return ret;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1285
  }
1c118596a   Miklos Szeredi   vfs: export do_sp...
1286
  EXPORT_SYMBOL(do_splice_direct);
b92ce5589   Jens Axboe   [PATCH] splice: a...
1287

7c77f0b3f   Miklos Szeredi   splice: implement...
1288
1289
1290
  static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  			       struct pipe_inode_info *opipe,
  			       size_t len, unsigned int flags);
ddac0d39c   Jens Axboe   [PATCH] splice: f...
1291
1292
  
  /*
83f9135bd   Jens Axboe   [PATCH] splice: a...
1293
1294
   * Determine where to splice to/from.
   */
529565dcb   Ingo Molnar   [PATCH] splice: a...
1295
1296
1297
  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)
5274f052e   Jens Axboe   [PATCH] Introduce...
1298
  {
7c77f0b3f   Miklos Szeredi   splice: implement...
1299
1300
  	struct pipe_inode_info *ipipe;
  	struct pipe_inode_info *opipe;
7995bd287   Al Viro   splice: don't pas...
1301
  	loff_t offset;
a4514ebd8   Jens Axboe   [PATCH] splice: o...
1302
  	long ret;
5274f052e   Jens Axboe   [PATCH] Introduce...
1303

71993e62a   Linus Torvalds   Rename 'pipe_info...
1304
1305
  	ipipe = get_pipe_info(in);
  	opipe = get_pipe_info(out);
7c77f0b3f   Miklos Szeredi   splice: implement...
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  
  	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) {
529565dcb   Ingo Molnar   [PATCH] splice: a...
1325
1326
  		if (off_in)
  			return -ESPIPE;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1327
  		if (off_out) {
19c9a49b4   Changli Gao   splice: check f_m...
1328
  			if (!(out->f_mode & FMODE_PWRITE))
b92ce5589   Jens Axboe   [PATCH] splice: a...
1329
  				return -EINVAL;
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1330
  			if (copy_from_user(&offset, off_out, sizeof(loff_t)))
b92ce5589   Jens Axboe   [PATCH] splice: a...
1331
  				return -EFAULT;
7995bd287   Al Viro   splice: don't pas...
1332
1333
1334
  		} else {
  			offset = out->f_pos;
  		}
529565dcb   Ingo Molnar   [PATCH] splice: a...
1335

18c67cb9f   Al Viro   splice: lift chec...
1336
1337
1338
1339
1340
1341
1342
1343
1344
  		if (unlikely(!(out->f_mode & FMODE_WRITE)))
  			return -EBADF;
  
  		if (unlikely(out->f_flags & O_APPEND))
  			return -EINVAL;
  
  		ret = rw_verify_area(WRITE, out, &offset, len);
  		if (unlikely(ret < 0))
  			return ret;
500368f7f   Al Viro   lift file_*_write...
1345
  		file_start_write(out);
7995bd287   Al Viro   splice: don't pas...
1346
  		ret = do_splice_from(ipipe, out, &offset, len, flags);
500368f7f   Al Viro   lift file_*_write...
1347
  		file_end_write(out);
a4514ebd8   Jens Axboe   [PATCH] splice: o...
1348

7995bd287   Al Viro   splice: don't pas...
1349
1350
1351
  		if (!off_out)
  			out->f_pos = offset;
  		else if (copy_to_user(off_out, &offset, sizeof(loff_t)))
a4514ebd8   Jens Axboe   [PATCH] splice: o...
1352
1353
1354
  			ret = -EFAULT;
  
  		return ret;
529565dcb   Ingo Molnar   [PATCH] splice: a...
1355
  	}
5274f052e   Jens Axboe   [PATCH] Introduce...
1356

7c77f0b3f   Miklos Szeredi   splice: implement...
1357
  	if (opipe) {
529565dcb   Ingo Molnar   [PATCH] splice: a...
1358
1359
  		if (off_out)
  			return -ESPIPE;
b92ce5589   Jens Axboe   [PATCH] splice: a...
1360
  		if (off_in) {
19c9a49b4   Changli Gao   splice: check f_m...
1361
  			if (!(in->f_mode & FMODE_PREAD))
b92ce5589   Jens Axboe   [PATCH] splice: a...
1362
  				return -EINVAL;
cbb7e577e   Jens Axboe   [PATCH] splice: p...
1363
  			if (copy_from_user(&offset, off_in, sizeof(loff_t)))
b92ce5589   Jens Axboe   [PATCH] splice: a...
1364
  				return -EFAULT;
7995bd287   Al Viro   splice: don't pas...
1365
1366
1367
  		} else {
  			offset = in->f_pos;
  		}
529565dcb   Ingo Molnar   [PATCH] splice: a...
1368

7995bd287   Al Viro   splice: don't pas...
1369
  		ret = do_splice_to(in, &offset, opipe, len, flags);
a4514ebd8   Jens Axboe   [PATCH] splice: o...
1370

7995bd287   Al Viro   splice: don't pas...
1371
1372
1373
  		if (!off_in)
  			in->f_pos = offset;
  		else if (copy_to_user(off_in, &offset, sizeof(loff_t)))
a4514ebd8   Jens Axboe   [PATCH] splice: o...
1374
1375
1376
  			ret = -EFAULT;
  
  		return ret;
529565dcb   Ingo Molnar   [PATCH] splice: a...
1377
  	}
5274f052e   Jens Axboe   [PATCH] Introduce...
1378
1379
1380
  
  	return -EINVAL;
  }
912d35f86   Jens Axboe   [PATCH] Add suppo...
1381
1382
1383
1384
1385
1386
1387
1388
1389
  /*
   * 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,
bd1a68b59   Eric Dumazet   vmsplice: relax a...
1390
  				struct partial_page *partial, bool aligned,
35f3d14db   Jens Axboe   pipe: add support...
1391
  				unsigned int pipe_buffers)
912d35f86   Jens Axboe   [PATCH] Add suppo...
1392
1393
  {
  	int buffers = 0, error = 0;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1394
1395
  	while (nr_vecs) {
  		unsigned long off, npages;
757239576   Linus Torvalds   Fix possible spli...
1396
  		struct iovec entry;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1397
1398
1399
  		void __user *base;
  		size_t len;
  		int i;
757239576   Linus Torvalds   Fix possible spli...
1400
  		error = -EFAULT;
bc40d73c9   Nick Piggin   splice: use get_u...
1401
  		if (copy_from_user(&entry, iov, sizeof(entry)))
912d35f86   Jens Axboe   [PATCH] Add suppo...
1402
  			break;
757239576   Linus Torvalds   Fix possible spli...
1403
1404
  		base = entry.iov_base;
  		len = entry.iov_len;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1405
1406
1407
  		/*
  		 * Sanity check this iovec. 0 read succeeds.
  		 */
757239576   Linus Torvalds   Fix possible spli...
1408
  		error = 0;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1409
1410
1411
  		if (unlikely(!len))
  			break;
  		error = -EFAULT;
712a30e63   Bastian Blank   splice: fix user ...
1412
  		if (!access_ok(VERIFY_READ, base, len))
912d35f86   Jens Axboe   [PATCH] Add suppo...
1413
1414
1415
1416
1417
1418
1419
  			break;
  
  		/*
  		 * Get this base offset and number of pages, then map
  		 * in the user pages.
  		 */
  		off = (unsigned long) base & ~PAGE_MASK;
7afa6fd03   Jens Axboe   [PATCH] vmsplice:...
1420
1421
1422
1423
1424
1425
1426
1427
  
  		/*
  		 * 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;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1428
  		npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
35f3d14db   Jens Axboe   pipe: add support...
1429
1430
  		if (npages > pipe_buffers - buffers)
  			npages = pipe_buffers - buffers;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1431

bc40d73c9   Nick Piggin   splice: use get_u...
1432
1433
  		error = get_user_pages_fast((unsigned long)base, npages,
  					0, &pages[buffers]);
912d35f86   Jens Axboe   [PATCH] Add suppo...
1434
1435
1436
1437
1438
1439
1440
1441
  
  		if (unlikely(error <= 0))
  			break;
  
  		/*
  		 * Fill this contiguous range into the partial page map.
  		 */
  		for (i = 0; i < error; i++) {
7591489a8   Jens Axboe   [PATCH] vmsplice:...
1442
  			const int plen = min_t(size_t, len, PAGE_SIZE - off);
912d35f86   Jens Axboe   [PATCH] Add suppo...
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
  
  			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.
  		 */
35f3d14db   Jens Axboe   pipe: add support...
1465
  		if (error < npages || buffers == pipe_buffers)
912d35f86   Jens Axboe   [PATCH] Add suppo...
1466
1467
1468
1469
1470
  			break;
  
  		nr_vecs--;
  		iov++;
  	}
912d35f86   Jens Axboe   [PATCH] Add suppo...
1471
1472
1473
1474
1475
  	if (buffers)
  		return buffers;
  
  	return error;
  }
6a14b90bb   Jens Axboe   vmsplice: add vms...
1476
1477
1478
  static int pipe_to_user(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  			struct splice_desc *sd)
  {
6130f5315   Al Viro   switch vmsplice_t...
1479
1480
  	int n = copy_page_to_iter(buf->page, buf->offset, sd->len, sd->u.data);
  	return n == sd->len ? n : -EFAULT;
6a14b90bb   Jens Axboe   vmsplice: add vms...
1481
1482
1483
1484
1485
1486
  }
  
  /*
   * For lack of a better implementation, implement vmsplice() to userspace
   * as a simple copy of the pipes pages to the user iov.
   */
6130f5315   Al Viro   switch vmsplice_t...
1487
  static long vmsplice_to_user(struct file *file, const struct iovec __user *uiov,
6a14b90bb   Jens Axboe   vmsplice: add vms...
1488
1489
1490
1491
  			     unsigned long nr_segs, unsigned int flags)
  {
  	struct pipe_inode_info *pipe;
  	struct splice_desc sd;
6a14b90bb   Jens Axboe   vmsplice: add vms...
1492
  	long ret;
6130f5315   Al Viro   switch vmsplice_t...
1493
1494
1495
  	struct iovec iovstack[UIO_FASTIOV];
  	struct iovec *iov = iovstack;
  	struct iov_iter iter;
6a14b90bb   Jens Axboe   vmsplice: add vms...
1496

71993e62a   Linus Torvalds   Rename 'pipe_info...
1497
  	pipe = get_pipe_info(file);
6a14b90bb   Jens Axboe   vmsplice: add vms...
1498
1499
  	if (!pipe)
  		return -EBADF;
345995fa4   Al Viro   vmsplice_to_user(...
1500
1501
1502
1503
  	ret = import_iovec(READ, uiov, nr_segs,
  			   ARRAY_SIZE(iovstack), &iov, &iter);
  	if (ret < 0)
  		return ret;
6a14b90bb   Jens Axboe   vmsplice: add vms...
1504

345995fa4   Al Viro   vmsplice_to_user(...
1505
  	sd.total_len = iov_iter_count(&iter);
6130f5315   Al Viro   switch vmsplice_t...
1506
  	sd.len = 0;
6130f5315   Al Viro   switch vmsplice_t...
1507
1508
1509
  	sd.flags = flags;
  	sd.u.data = &iter;
  	sd.pos = 0;
6a14b90bb   Jens Axboe   vmsplice: add vms...
1510

345995fa4   Al Viro   vmsplice_to_user(...
1511
1512
1513
1514
1515
  	if (sd.total_len) {
  		pipe_lock(pipe);
  		ret = __splice_from_pipe(pipe, &sd, pipe_to_user);
  		pipe_unlock(pipe);
  	}
6a14b90bb   Jens Axboe   vmsplice: add vms...
1516

345995fa4   Al Viro   vmsplice_to_user(...
1517
  	kfree(iov);
6a14b90bb   Jens Axboe   vmsplice: add vms...
1518
1519
  	return ret;
  }
912d35f86   Jens Axboe   [PATCH] Add suppo...
1520
1521
1522
1523
  /*
   * vmsplice splices a user address range into a pipe. It can be thought of
   * as splice-from-memory, where the regular splice is splice-from-file (or
   * to file). In both cases the output is a pipe, naturally.
912d35f86   Jens Axboe   [PATCH] Add suppo...
1524
   */
6a14b90bb   Jens Axboe   vmsplice: add vms...
1525
1526
  static long vmsplice_to_pipe(struct file *file, const struct iovec __user *iov,
  			     unsigned long nr_segs, unsigned int flags)
912d35f86   Jens Axboe   [PATCH] Add suppo...
1527
  {
ddac0d39c   Jens Axboe   [PATCH] splice: f...
1528
  	struct pipe_inode_info *pipe;
35f3d14db   Jens Axboe   pipe: add support...
1529
1530
  	struct page *pages[PIPE_DEF_BUFFERS];
  	struct partial_page partial[PIPE_DEF_BUFFERS];
912d35f86   Jens Axboe   [PATCH] Add suppo...
1531
1532
1533
  	struct splice_pipe_desc spd = {
  		.pages = pages,
  		.partial = partial,
047fe3605   Eric Dumazet   splice: fix racy ...
1534
  		.nr_pages_max = PIPE_DEF_BUFFERS,
912d35f86   Jens Axboe   [PATCH] Add suppo...
1535
1536
  		.flags = flags,
  		.ops = &user_page_pipe_buf_ops,
bbdfc2f70   Jens Axboe   [SPLICE]: Don't a...
1537
  		.spd_release = spd_release_page,
912d35f86   Jens Axboe   [PATCH] Add suppo...
1538
  	};
35f3d14db   Jens Axboe   pipe: add support...
1539
  	long ret;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1540

71993e62a   Linus Torvalds   Rename 'pipe_info...
1541
  	pipe = get_pipe_info(file);
ddac0d39c   Jens Axboe   [PATCH] splice: f...
1542
  	if (!pipe)
912d35f86   Jens Axboe   [PATCH] Add suppo...
1543
  		return -EBADF;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1544

35f3d14db   Jens Axboe   pipe: add support...
1545
1546
1547
1548
  	if (splice_grow_spd(pipe, &spd))
  		return -ENOMEM;
  
  	spd.nr_pages = get_iovec_page_array(iov, nr_segs, spd.pages,
bd1a68b59   Eric Dumazet   vmsplice: relax a...
1549
  					    spd.partial, false,
047fe3605   Eric Dumazet   splice: fix racy ...
1550
  					    spd.nr_pages_max);
912d35f86   Jens Axboe   [PATCH] Add suppo...
1551
  	if (spd.nr_pages <= 0)
35f3d14db   Jens Axboe   pipe: add support...
1552
1553
1554
  		ret = spd.nr_pages;
  	else
  		ret = splice_to_pipe(pipe, &spd);
912d35f86   Jens Axboe   [PATCH] Add suppo...
1555

047fe3605   Eric Dumazet   splice: fix racy ...
1556
  	splice_shrink_spd(&spd);
35f3d14db   Jens Axboe   pipe: add support...
1557
  	return ret;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1558
  }
6a14b90bb   Jens Axboe   vmsplice: add vms...
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
  /*
   * 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().
   *
   */
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
1575
1576
  SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, iov,
  		unsigned long, nr_segs, unsigned int, flags)
912d35f86   Jens Axboe   [PATCH] Add suppo...
1577
  {
2903ff019   Al Viro   switch simple cas...
1578
  	struct fd f;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1579
  	long error;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1580

6a14b90bb   Jens Axboe   vmsplice: add vms...
1581
1582
1583
1584
  	if (unlikely(nr_segs > UIO_MAXIOV))
  		return -EINVAL;
  	else if (unlikely(!nr_segs))
  		return 0;
912d35f86   Jens Axboe   [PATCH] Add suppo...
1585
  	error = -EBADF;
2903ff019   Al Viro   switch simple cas...
1586
1587
1588
1589
1590
1591
1592
1593
  	f = fdget(fd);
  	if (f.file) {
  		if (f.file->f_mode & FMODE_WRITE)
  			error = vmsplice_to_pipe(f.file, iov, nr_segs, flags);
  		else if (f.file->f_mode & FMODE_READ)
  			error = vmsplice_to_user(f.file, iov, nr_segs, flags);
  
  		fdput(f);
912d35f86   Jens Axboe   [PATCH] Add suppo...
1594
1595
1596
1597
  	}
  
  	return error;
  }
76b021d05   Al Viro   convert vmsplice ...
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
  #ifdef CONFIG_COMPAT
  COMPAT_SYSCALL_DEFINE4(vmsplice, int, fd, const struct compat_iovec __user *, iov32,
  		    unsigned int, nr_segs, unsigned int, flags)
  {
  	unsigned i;
  	struct iovec __user *iov;
  	if (nr_segs > UIO_MAXIOV)
  		return -EINVAL;
  	iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  	for (i = 0; i < nr_segs; i++) {
  		struct compat_iovec v;
  		if (get_user(v.iov_base, &iov32[i].iov_base) ||
  		    get_user(v.iov_len, &iov32[i].iov_len) ||
  		    put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  		    put_user(v.iov_len, &iov[i].iov_len))
  			return -EFAULT;
  	}
  	return sys_vmsplice(fd, iov, nr_segs, flags);
  }
  #endif
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
1618
1619
1620
  SYSCALL_DEFINE6(splice, int, fd_in, loff_t __user *, off_in,
  		int, fd_out, loff_t __user *, off_out,
  		size_t, len, unsigned int, flags)
5274f052e   Jens Axboe   [PATCH] Introduce...
1621
  {
2903ff019   Al Viro   switch simple cas...
1622
  	struct fd in, out;
5274f052e   Jens Axboe   [PATCH] Introduce...
1623
  	long error;
5274f052e   Jens Axboe   [PATCH] Introduce...
1624
1625
1626
1627
1628
  
  	if (unlikely(!len))
  		return 0;
  
  	error = -EBADF;
2903ff019   Al Viro   switch simple cas...
1629
1630
1631
1632
1633
1634
1635
1636
  	in = fdget(fd_in);
  	if (in.file) {
  		if (in.file->f_mode & FMODE_READ) {
  			out = fdget(fd_out);
  			if (out.file) {
  				if (out.file->f_mode & FMODE_WRITE)
  					error = do_splice(in.file, off_in,
  							  out.file, off_out,
529565dcb   Ingo Molnar   [PATCH] splice: a...
1637
  							  len, flags);
2903ff019   Al Viro   switch simple cas...
1638
  				fdput(out);
5274f052e   Jens Axboe   [PATCH] Introduce...
1639
1640
  			}
  		}
2903ff019   Al Viro   switch simple cas...
1641
  		fdput(in);
5274f052e   Jens Axboe   [PATCH] Introduce...
1642
  	}
5274f052e   Jens Axboe   [PATCH] Introduce...
1643
1644
  	return error;
  }
70524490e   Jens Axboe   [PATCH] splice: a...
1645
1646
  
  /*
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1647
1648
1649
   * Make sure there's data to read. Wait for input if we can, otherwise
   * return an appropriate error.
   */
7c77f0b3f   Miklos Szeredi   splice: implement...
1650
  static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
  {
  	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;
61e0d47c3   Miklos Szeredi   splice: add helpe...
1662
  	pipe_lock(pipe);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
  
  	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);
  	}
61e0d47c3   Miklos Szeredi   splice: add helpe...
1679
  	pipe_unlock(pipe);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1680
1681
1682
1683
1684
1685
1686
  	return ret;
  }
  
  /*
   * Make sure there's writeable room. Wait for room if we can, otherwise
   * return an appropriate error.
   */
7c77f0b3f   Miklos Szeredi   splice: implement...
1687
  static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags)
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1688
1689
1690
1691
1692
1693
1694
  {
  	int ret;
  
  	/*
  	 * Check ->nrbufs without the inode lock first. This function
  	 * is speculative anyways, so missing one is ok.
  	 */
35f3d14db   Jens Axboe   pipe: add support...
1695
  	if (pipe->nrbufs < pipe->buffers)
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1696
1697
1698
  		return 0;
  
  	ret = 0;
61e0d47c3   Miklos Szeredi   splice: add helpe...
1699
  	pipe_lock(pipe);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1700

35f3d14db   Jens Axboe   pipe: add support...
1701
  	while (pipe->nrbufs >= pipe->buffers) {
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
  		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--;
  	}
61e0d47c3   Miklos Szeredi   splice: add helpe...
1719
  	pipe_unlock(pipe);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1720
1721
1722
1723
  	return ret;
  }
  
  /*
7c77f0b3f   Miklos Szeredi   splice: implement...
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
   * 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.
  		 */
35f3d14db   Jens Axboe   pipe: add support...
1766
  		if (!ipipe->nrbufs || opipe->nrbufs >= opipe->buffers) {
7c77f0b3f   Miklos Szeredi   splice: implement...
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
  			/* 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;
35f3d14db   Jens Axboe   pipe: add support...
1787
  		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
7c77f0b3f   Miklos Szeredi   splice: implement...
1788
1789
1790
1791
1792
1793
1794
1795
1796
  		obuf = opipe->bufs + nbuf;
  
  		if (len >= ibuf->len) {
  			/*
  			 * Simply move the whole buffer from ipipe to opipe
  			 */
  			*obuf = *ibuf;
  			ibuf->ops = NULL;
  			opipe->nrbufs++;
35f3d14db   Jens Axboe   pipe: add support...
1797
  			ipipe->curbuf = (ipipe->curbuf + 1) & (ipipe->buffers - 1);
7c77f0b3f   Miklos Szeredi   splice: implement...
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
  			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.
  	 */
825cdcb1a   Namhyung Kim   splice: add wakeu...
1829
1830
  	if (ret > 0)
  		wakeup_pipe_readers(opipe);
7c77f0b3f   Miklos Szeredi   splice: implement...
1831
1832
1833
1834
1835
1836
1837
  	if (input_wakeup)
  		wakeup_pipe_writers(ipipe);
  
  	return ret;
  }
  
  /*
70524490e   Jens Axboe   [PATCH] splice: a...
1838
1839
1840
1841
1842
1843
1844
   * 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;
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1845
  	int ret = 0, i = 0, nbuf;
70524490e   Jens Axboe   [PATCH] splice: a...
1846
1847
1848
  
  	/*
  	 * Potential ABBA deadlock, work around it by ordering lock
61e0d47c3   Miklos Szeredi   splice: add helpe...
1849
  	 * grabbing by pipe info address. Otherwise two different processes
70524490e   Jens Axboe   [PATCH] splice: a...
1850
1851
  	 * could deadlock (one doing tee from A -> B, the other from B -> A).
  	 */
61e0d47c3   Miklos Szeredi   splice: add helpe...
1852
  	pipe_double_lock(ipipe, opipe);
70524490e   Jens Axboe   [PATCH] splice: a...
1853

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1854
  	do {
70524490e   Jens Axboe   [PATCH] splice: a...
1855
1856
1857
1858
1859
1860
  		if (!opipe->readers) {
  			send_sig(SIGPIPE, current, 0);
  			if (!ret)
  				ret = -EPIPE;
  			break;
  		}
70524490e   Jens Axboe   [PATCH] splice: a...
1861

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1862
1863
1864
1865
  		/*
  		 * If we have iterated all input buffers or ran out of
  		 * output room, break.
  		 */
35f3d14db   Jens Axboe   pipe: add support...
1866
  		if (i >= ipipe->nrbufs || opipe->nrbufs >= opipe->buffers)
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1867
  			break;
70524490e   Jens Axboe   [PATCH] splice: a...
1868

35f3d14db   Jens Axboe   pipe: add support...
1869
1870
  		ibuf = ipipe->bufs + ((ipipe->curbuf + i) & (ipipe->buffers-1));
  		nbuf = (opipe->curbuf + opipe->nrbufs) & (opipe->buffers - 1);
70524490e   Jens Axboe   [PATCH] splice: a...
1871
1872
  
  		/*
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1873
1874
  		 * Get a reference to this pipe buffer,
  		 * so we can copy the contents over.
70524490e   Jens Axboe   [PATCH] splice: a...
1875
  		 */
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1876
1877
1878
1879
  		ibuf->ops->get(ipipe, ibuf);
  
  		obuf = opipe->bufs + nbuf;
  		*obuf = *ibuf;
2a27250e6   Jens Axboe   [PATCH] tee: link...
1880
  		/*
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1881
1882
  		 * Don't inherit the gift flag, we need to
  		 * prevent multiple steals of this page.
2a27250e6   Jens Axboe   [PATCH] tee: link...
1883
  		 */
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1884
  		obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
70524490e   Jens Axboe   [PATCH] splice: a...
1885

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1886
1887
  		if (obuf->len > len)
  			obuf->len = len;
70524490e   Jens Axboe   [PATCH] splice: a...
1888

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1889
1890
1891
1892
1893
  		opipe->nrbufs++;
  		ret += obuf->len;
  		len -= obuf->len;
  		i++;
  	} while (len);
70524490e   Jens Axboe   [PATCH] splice: a...
1894

02cf01aea   Jens Axboe   splice: only retu...
1895
1896
1897
1898
1899
1900
  	/*
  	 * 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;
61e0d47c3   Miklos Szeredi   splice: add helpe...
1901
1902
  	pipe_unlock(ipipe);
  	pipe_unlock(opipe);
70524490e   Jens Axboe   [PATCH] splice: a...
1903

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1904
1905
1906
  	/*
  	 * If we put data in the output pipe, wakeup any potential readers.
  	 */
825cdcb1a   Namhyung Kim   splice: add wakeu...
1907
1908
  	if (ret > 0)
  		wakeup_pipe_readers(opipe);
70524490e   Jens Axboe   [PATCH] splice: a...
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
  
  	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)
  {
71993e62a   Linus Torvalds   Rename 'pipe_info...
1922
1923
  	struct pipe_inode_info *ipipe = get_pipe_info(in);
  	struct pipe_inode_info *opipe = get_pipe_info(out);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1924
  	int ret = -EINVAL;
70524490e   Jens Axboe   [PATCH] splice: a...
1925
1926
  
  	/*
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1927
1928
  	 * Duplicate the contents of ipipe to opipe without actually
  	 * copying the data.
70524490e   Jens Axboe   [PATCH] splice: a...
1929
  	 */
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1930
1931
1932
1933
1934
  	if (ipipe && opipe && ipipe != opipe) {
  		/*
  		 * Keep going, unless we encounter an error. The ipipe/opipe
  		 * ordering doesn't really matter.
  		 */
7c77f0b3f   Miklos Szeredi   splice: implement...
1935
  		ret = ipipe_prep(ipipe, flags);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1936
  		if (!ret) {
7c77f0b3f   Miklos Szeredi   splice: implement...
1937
  			ret = opipe_prep(opipe, flags);
02cf01aea   Jens Axboe   splice: only retu...
1938
  			if (!ret)
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1939
  				ret = link_pipe(ipipe, opipe, len, flags);
aadd06e5c   Jens Axboe   [PATCH] splice: f...
1940
1941
  		}
  	}
70524490e   Jens Axboe   [PATCH] splice: a...
1942

aadd06e5c   Jens Axboe   [PATCH] splice: f...
1943
  	return ret;
70524490e   Jens Axboe   [PATCH] splice: a...
1944
  }
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
1945
  SYSCALL_DEFINE4(tee, int, fdin, int, fdout, size_t, len, unsigned int, flags)
70524490e   Jens Axboe   [PATCH] splice: a...
1946
  {
2903ff019   Al Viro   switch simple cas...
1947
1948
  	struct fd in;
  	int error;
70524490e   Jens Axboe   [PATCH] splice: a...
1949
1950
1951
1952
1953
  
  	if (unlikely(!len))
  		return 0;
  
  	error = -EBADF;
2903ff019   Al Viro   switch simple cas...
1954
1955
1956
1957
1958
1959
1960
1961
1962
  	in = fdget(fdin);
  	if (in.file) {
  		if (in.file->f_mode & FMODE_READ) {
  			struct fd out = fdget(fdout);
  			if (out.file) {
  				if (out.file->f_mode & FMODE_WRITE)
  					error = do_tee(in.file, out.file,
  							len, flags);
  				fdput(out);
70524490e   Jens Axboe   [PATCH] splice: a...
1963
1964
  			}
  		}
2903ff019   Al Viro   switch simple cas...
1965
   		fdput(in);
70524490e   Jens Axboe   [PATCH] splice: a...
1966
1967
1968
1969
   	}
  
  	return error;
  }