Blame view

fs/pipe.c 25 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   *  linux/fs/pipe.c
   *
   *  Copyright (C) 1991, 1992, 1999  Linus Torvalds
   */
  
  #include <linux/mm.h>
  #include <linux/file.h>
  #include <linux/poll.h>
  #include <linux/slab.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/fs.h>
35f3d14db   Jens Axboe   pipe: add support...
14
  #include <linux/log2.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/mount.h>
b502bd115   Muthu Kumar   magic.h: move som...
16
  #include <linux/magic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
19
  #include <linux/pipe_fs_i.h>
  #include <linux/uio.h>
  #include <linux/highmem.h>
5274f052e   Jens Axboe   [PATCH] Introduce...
20
  #include <linux/pagemap.h>
db3495099   Al Viro   [PATCH] AUDIT_FD_...
21
  #include <linux/audit.h>
ba719baea   Ulrich Drepper   sys_pipe(): fix f...
22
  #include <linux/syscalls.h>
b492e95be   Jens Axboe   pipe: set lower a...
23
  #include <linux/fcntl.h>
a27bb332c   Kent Overstreet   aio: don't includ...
24
  #include <linux/aio.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
  
  #include <asm/uaccess.h>
  #include <asm/ioctls.h>
599a0ac14   Al Viro   pipe: fold file_o...
28
  #include "internal.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  /*
b492e95be   Jens Axboe   pipe: set lower a...
30
   * The max size that a non-root user is allowed to grow the pipe. Can
ff9da691c   Jens Axboe   pipe: change /pro...
31
   * be set by root in /proc/sys/fs/pipe-max-size
b492e95be   Jens Axboe   pipe: set lower a...
32
   */
ff9da691c   Jens Axboe   pipe: change /pro...
33
34
35
36
37
38
  unsigned int pipe_max_size = 1048576;
  
  /*
   * Minimum pipe size, as required by POSIX
   */
  unsigned int pipe_min_size = PAGE_SIZE;
b492e95be   Jens Axboe   pipe: set lower a...
39
40
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
47
48
49
50
51
52
53
   * We use a start+len construction, which provides full use of the 
   * allocated memory.
   * -- Florian Coosmann (FGC)
   * 
   * Reads with count = 0 should always return 0.
   * -- Julian Bradfield 1999-06-07.
   *
   * FIFOs and Pipes now generate SIGIO for both readers and writers.
   * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
   *
   * pipe_read & write cleanup
   * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
   */
61e0d47c3   Miklos Szeredi   splice: add helpe...
54
55
  static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
  {
6447a3cf1   Al Viro   get rid of pipe->...
56
  	if (pipe->files)
72b0d9aac   Al Viro   pipe: don't use -...
57
  		mutex_lock_nested(&pipe->mutex, subclass);
61e0d47c3   Miklos Szeredi   splice: add helpe...
58
59
60
61
62
63
64
65
66
67
68
69
70
  }
  
  void pipe_lock(struct pipe_inode_info *pipe)
  {
  	/*
  	 * pipe_lock() nests non-pipe inode locks (for writing to a file)
  	 */
  	pipe_lock_nested(pipe, I_MUTEX_PARENT);
  }
  EXPORT_SYMBOL(pipe_lock);
  
  void pipe_unlock(struct pipe_inode_info *pipe)
  {
6447a3cf1   Al Viro   get rid of pipe->...
71
  	if (pipe->files)
72b0d9aac   Al Viro   pipe: don't use -...
72
  		mutex_unlock(&pipe->mutex);
61e0d47c3   Miklos Szeredi   splice: add helpe...
73
74
  }
  EXPORT_SYMBOL(pipe_unlock);
ebec73f47   Al Viro   introduce variant...
75
76
77
78
79
80
81
82
83
  static inline void __pipe_lock(struct pipe_inode_info *pipe)
  {
  	mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
  }
  
  static inline void __pipe_unlock(struct pipe_inode_info *pipe)
  {
  	mutex_unlock(&pipe->mutex);
  }
61e0d47c3   Miklos Szeredi   splice: add helpe...
84
85
86
87
88
89
90
91
92
  void pipe_double_lock(struct pipe_inode_info *pipe1,
  		      struct pipe_inode_info *pipe2)
  {
  	BUG_ON(pipe1 == pipe2);
  
  	if (pipe1 < pipe2) {
  		pipe_lock_nested(pipe1, I_MUTEX_PARENT);
  		pipe_lock_nested(pipe2, I_MUTEX_CHILD);
  	} else {
023d43c7b   Peter Zijlstra   lockdep: Fix lock...
93
94
  		pipe_lock_nested(pipe2, I_MUTEX_PARENT);
  		pipe_lock_nested(pipe1, I_MUTEX_CHILD);
61e0d47c3   Miklos Szeredi   splice: add helpe...
95
96
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  /* Drop the inode semaphore and wait for a pipe event, atomically */
3a326a2ce   Ingo Molnar   [PATCH] introduce...
98
  void pipe_wait(struct pipe_inode_info *pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
  {
  	DEFINE_WAIT(wait);
d79fc0fc6   Ingo Molnar   [PATCH] sched: TA...
101
102
103
104
  	/*
  	 * Pipes are system-local resources, so sleeping on them
  	 * is considered a noninteractive wait:
  	 */
af9272326   Mike Galbraith   sched: cleanup, r...
105
  	prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
61e0d47c3   Miklos Szeredi   splice: add helpe...
106
  	pipe_unlock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  	schedule();
3a326a2ce   Ingo Molnar   [PATCH] introduce...
108
  	finish_wait(&pipe->wait, &wait);
61e0d47c3   Miklos Szeredi   splice: add helpe...
109
  	pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  }
341b446bc   Ingo Molnar   [PATCH] another r...
111
112
  static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  				  struct pipe_buffer *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
  {
  	struct page *page = buf->page;
5274f052e   Jens Axboe   [PATCH] Introduce...
115
116
117
  	/*
  	 * If nobody else uses this page, and we don't already have a
  	 * temporary page, let's keep track of it as a one-deep
341b446bc   Ingo Molnar   [PATCH] another r...
118
  	 * allocation cache. (Otherwise just release our reference to it)
5274f052e   Jens Axboe   [PATCH] Introduce...
119
  	 */
341b446bc   Ingo Molnar   [PATCH] another r...
120
  	if (page_count(page) == 1 && !pipe->tmp_page)
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
121
  		pipe->tmp_page = page;
341b446bc   Ingo Molnar   [PATCH] another r...
122
123
  	else
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  }
0845718da   Jens Axboe   pipe: add documen...
125
  /**
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
126
   * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
0845718da   Jens Axboe   pipe: add documen...
127
128
129
130
   * @pipe:	the pipe that the buffer belongs to
   * @buf:	the buffer to attempt to steal
   *
   * Description:
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
131
   *	This function attempts to steal the &struct page attached to
0845718da   Jens Axboe   pipe: add documen...
132
133
   *	@buf. If successful, this function returns 0 and returns with
   *	the page locked. The caller may then reuse the page for whatever
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
134
   *	he wishes; the typical use is insertion into a different file
0845718da   Jens Axboe   pipe: add documen...
135
136
   *	page cache.
   */
330ab7161   Jens Axboe   [PATCH] vmsplice:...
137
138
  int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
  			   struct pipe_buffer *buf)
5abc97aa2   Jens Axboe   [PATCH] splice: a...
139
  {
46e678c96   Jens Axboe   [PATCH] splice: f...
140
  	struct page *page = buf->page;
0845718da   Jens Axboe   pipe: add documen...
141
142
143
144
145
  	/*
  	 * A reference of one is golden, that means that the owner of this
  	 * page is the only one holding a reference to it. lock the page
  	 * and return OK.
  	 */
46e678c96   Jens Axboe   [PATCH] splice: f...
146
  	if (page_count(page) == 1) {
46e678c96   Jens Axboe   [PATCH] splice: f...
147
148
149
150
151
  		lock_page(page);
  		return 0;
  	}
  
  	return 1;
5abc97aa2   Jens Axboe   [PATCH] splice: a...
152
  }
51921cb74   Miklos Szeredi   mm: export generi...
153
  EXPORT_SYMBOL(generic_pipe_buf_steal);
5abc97aa2   Jens Axboe   [PATCH] splice: a...
154

0845718da   Jens Axboe   pipe: add documen...
155
  /**
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
156
   * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
0845718da   Jens Axboe   pipe: add documen...
157
158
159
160
161
162
163
164
165
   * @pipe:	the pipe that the buffer belongs to
   * @buf:	the buffer to get a reference to
   *
   * Description:
   *	This function grabs an extra reference to @buf. It's used in
   *	in the tee() system call, when we duplicate the buffers in one
   *	pipe into another.
   */
  void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
70524490e   Jens Axboe   [PATCH] splice: a...
166
167
168
  {
  	page_cache_get(buf->page);
  }
51921cb74   Miklos Szeredi   mm: export generi...
169
  EXPORT_SYMBOL(generic_pipe_buf_get);
70524490e   Jens Axboe   [PATCH] splice: a...
170

0845718da   Jens Axboe   pipe: add documen...
171
172
  /**
   * generic_pipe_buf_confirm - verify contents of the pipe buffer
79685b8de   Randy Dunlap   docbook: add pipe...
173
   * @info:	the pipe that the buffer belongs to
0845718da   Jens Axboe   pipe: add documen...
174
175
176
177
178
179
   * @buf:	the buffer to confirm
   *
   * Description:
   *	This function does nothing, because the generic pipe code uses
   *	pages that are always good when inserted into the pipe.
   */
cac36bb06   Jens Axboe   pipe: change the ...
180
181
  int generic_pipe_buf_confirm(struct pipe_inode_info *info,
  			     struct pipe_buffer *buf)
f84d75199   Jens Axboe   [PATCH] pipe: int...
182
183
184
  {
  	return 0;
  }
51921cb74   Miklos Szeredi   mm: export generi...
185
  EXPORT_SYMBOL(generic_pipe_buf_confirm);
f84d75199   Jens Axboe   [PATCH] pipe: int...
186

6818173bd   Miklos Szeredi   splice: implement...
187
188
189
190
191
192
193
194
195
196
197
198
199
  /**
   * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
   * @pipe:	the pipe that the buffer belongs to
   * @buf:	the buffer to put a reference to
   *
   * Description:
   *	This function releases a reference to @buf.
   */
  void generic_pipe_buf_release(struct pipe_inode_info *pipe,
  			      struct pipe_buffer *buf)
  {
  	page_cache_release(buf->page);
  }
51921cb74   Miklos Szeredi   mm: export generi...
200
  EXPORT_SYMBOL(generic_pipe_buf_release);
6818173bd   Miklos Szeredi   splice: implement...
201

d4c3cca94   Eric Dumazet   [PATCH] constify ...
202
  static const struct pipe_buf_operations anon_pipe_buf_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  	.can_merge = 1,
cac36bb06   Jens Axboe   pipe: change the ...
204
  	.confirm = generic_pipe_buf_confirm,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  	.release = anon_pipe_buf_release,
330ab7161   Jens Axboe   [PATCH] vmsplice:...
206
  	.steal = generic_pipe_buf_steal,
f84d75199   Jens Axboe   [PATCH] pipe: int...
207
  	.get = generic_pipe_buf_get,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  };
9883035ae   Linus Torvalds   pipes: add a "pac...
209
210
  static const struct pipe_buf_operations packet_pipe_buf_ops = {
  	.can_merge = 0,
9883035ae   Linus Torvalds   pipes: add a "pac...
211
212
213
214
215
  	.confirm = generic_pipe_buf_confirm,
  	.release = anon_pipe_buf_release,
  	.steal = generic_pipe_buf_steal,
  	.get = generic_pipe_buf_get,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  static ssize_t
fb9096a34   Al Viro   pipe: switch to -...
217
  pipe_read(struct kiocb *iocb, struct iov_iter *to)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  {
fb9096a34   Al Viro   pipe: switch to -...
219
  	size_t total_len = iov_iter_count(to);
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
220
  	struct file *filp = iocb->ki_filp;
de32ec4cf   Al Viro   pipe: set file->p...
221
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
  	int do_wakeup;
  	ssize_t ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
229
230
  	/* Null read succeeds. */
  	if (unlikely(total_len == 0))
  		return 0;
  
  	do_wakeup = 0;
  	ret = 0;
ebec73f47   Al Viro   introduce variant...
231
  	__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
  	for (;;) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
233
  		int bufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  		if (bufs) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
235
236
  			int curbuf = pipe->curbuf;
  			struct pipe_buffer *buf = pipe->bufs + curbuf;
d4c3cca94   Eric Dumazet   [PATCH] constify ...
237
  			const struct pipe_buf_operations *ops = buf->ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  			size_t chars = buf->len;
637b58c28   Al Viro   switch pipe_read(...
239
240
  			size_t written;
  			int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
  
  			if (chars > total_len)
  				chars = total_len;
cac36bb06   Jens Axboe   pipe: change the ...
244
  			error = ops->confirm(pipe, buf);
f84d75199   Jens Axboe   [PATCH] pipe: int...
245
  			if (error) {
5274f052e   Jens Axboe   [PATCH] Introduce...
246
  				if (!ret)
e5953cbdf   Nicolas Kaiser   pipe: fix failure...
247
  					ret = error;
5274f052e   Jens Axboe   [PATCH] Introduce...
248
249
  				break;
  			}
f84d75199   Jens Axboe   [PATCH] pipe: int...
250

fb9096a34   Al Viro   pipe: switch to -...
251
  			written = copy_page_to_iter(buf->page, buf->offset, chars, to);
637b58c28   Al Viro   switch pipe_read(...
252
  			if (unlikely(written < chars)) {
341b446bc   Ingo Molnar   [PATCH] another r...
253
  				if (!ret)
637b58c28   Al Viro   switch pipe_read(...
254
  					ret = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
258
259
  				break;
  			}
  			ret += chars;
  			buf->offset += chars;
  			buf->len -= chars;
9883035ae   Linus Torvalds   pipes: add a "pac...
260
261
262
263
264
265
  
  			/* Was it a packet buffer? Clean up and exit */
  			if (buf->flags & PIPE_BUF_FLAG_PACKET) {
  				total_len = chars;
  				buf->len = 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
  			if (!buf->len) {
  				buf->ops = NULL;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
268
  				ops->release(pipe, buf);
35f3d14db   Jens Axboe   pipe: add support...
269
  				curbuf = (curbuf + 1) & (pipe->buffers - 1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
270
271
  				pipe->curbuf = curbuf;
  				pipe->nrbufs = --bufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
276
277
278
279
  				do_wakeup = 1;
  			}
  			total_len -= chars;
  			if (!total_len)
  				break;	/* common path: read succeeded */
  		}
  		if (bufs)	/* More to do? */
  			continue;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
280
  		if (!pipe->writers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  			break;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
282
  		if (!pipe->waiting_writers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
288
289
290
291
292
293
294
295
  			/* syscall merging: Usually we must not sleep
  			 * if O_NONBLOCK is set, or if we got some data.
  			 * But if a writer sleeps in kernel space, then
  			 * we can wait for that data without violating POSIX.
  			 */
  			if (ret)
  				break;
  			if (filp->f_flags & O_NONBLOCK) {
  				ret = -EAGAIN;
  				break;
  			}
  		}
  		if (signal_pending(current)) {
341b446bc   Ingo Molnar   [PATCH] another r...
296
297
  			if (!ret)
  				ret = -ERESTARTSYS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
  			break;
  		}
  		if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
301
  			wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
302
   			kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
304
  		pipe_wait(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  	}
ebec73f47   Al Viro   introduce variant...
306
  	__pipe_unlock(pipe);
341b446bc   Ingo Molnar   [PATCH] another r...
307
308
  
  	/* Signal writers asynchronously that there is more room. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
  	if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
310
  		wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
311
  		kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
316
  	}
  	if (ret > 0)
  		file_accessed(filp);
  	return ret;
  }
9883035ae   Linus Torvalds   pipes: add a "pac...
317
318
319
320
  static inline int is_packetized(struct file *file)
  {
  	return (file->f_flags & O_DIRECT) != 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
  static ssize_t
f0d1bec9d   Al Viro   new helper: copy_...
322
  pipe_write(struct kiocb *iocb, struct iov_iter *from)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
  {
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
324
  	struct file *filp = iocb->ki_filp;
de32ec4cf   Al Viro   pipe: set file->p...
325
  	struct pipe_inode_info *pipe = filp->private_data;
f0d1bec9d   Al Viro   new helper: copy_...
326
327
328
  	ssize_t ret = 0;
  	int do_wakeup = 0;
  	size_t total_len = iov_iter_count(from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
  	ssize_t chars;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
  	/* Null write succeeds. */
  	if (unlikely(total_len == 0))
  		return 0;
ebec73f47   Al Viro   introduce variant...
333
  	__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
335
  	if (!pipe->readers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
342
  		send_sig(SIGPIPE, current, 0);
  		ret = -EPIPE;
  		goto out;
  	}
  
  	/* We try to merge small writes */
  	chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
343
  	if (pipe->nrbufs && chars != 0) {
341b446bc   Ingo Molnar   [PATCH] another r...
344
  		int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
35f3d14db   Jens Axboe   pipe: add support...
345
  							(pipe->buffers - 1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
346
  		struct pipe_buffer *buf = pipe->bufs + lastbuf;
d4c3cca94   Eric Dumazet   [PATCH] constify ...
347
  		const struct pipe_buf_operations *ops = buf->ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  		int offset = buf->offset + buf->len;
341b446bc   Ingo Molnar   [PATCH] another r...
349

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
  		if (ops->can_merge && offset + chars <= PAGE_SIZE) {
f0d1bec9d   Al Viro   new helper: copy_...
351
  			int error = ops->confirm(pipe, buf);
f84d75199   Jens Axboe   [PATCH] pipe: int...
352
  			if (error)
5274f052e   Jens Axboe   [PATCH] Introduce...
353
  				goto out;
f84d75199   Jens Axboe   [PATCH] pipe: int...
354

f0d1bec9d   Al Viro   new helper: copy_...
355
356
357
  			ret = copy_page_from_iter(buf->page, offset, chars, from);
  			if (unlikely(ret < chars)) {
  				error = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
  				goto out;
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
359
  			}
f0d1bec9d   Al Viro   new helper: copy_...
360
  			do_wakeup = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
  			buf->len += chars;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
  			ret = chars;
f0d1bec9d   Al Viro   new helper: copy_...
363
  			if (!iov_iter_count(from))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
368
369
  				goto out;
  		}
  	}
  
  	for (;;) {
  		int bufs;
341b446bc   Ingo Molnar   [PATCH] another r...
370

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
371
  		if (!pipe->readers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  			send_sig(SIGPIPE, current, 0);
341b446bc   Ingo Molnar   [PATCH] another r...
373
374
  			if (!ret)
  				ret = -EPIPE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
  			break;
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
377
  		bufs = pipe->nrbufs;
35f3d14db   Jens Axboe   pipe: add support...
378
379
  		if (bufs < pipe->buffers) {
  			int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
380
381
  			struct pipe_buffer *buf = pipe->bufs + newbuf;
  			struct page *page = pipe->tmp_page;
f0d1bec9d   Al Viro   new helper: copy_...
382
  			int copied;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
385
386
387
388
389
  
  			if (!page) {
  				page = alloc_page(GFP_HIGHUSER);
  				if (unlikely(!page)) {
  					ret = ret ? : -ENOMEM;
  					break;
  				}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
390
  				pipe->tmp_page = page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
  			}
341b446bc   Ingo Molnar   [PATCH] another r...
392
  			/* Always wake up, even if the copy fails. Otherwise
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
397
  			 * we lock up (O_NONBLOCK-)readers that sleep due to
  			 * syscall merging.
  			 * FIXME! Is this really true?
  			 */
  			do_wakeup = 1;
f0d1bec9d   Al Viro   new helper: copy_...
398
399
  			copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
  			if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
341b446bc   Ingo Molnar   [PATCH] another r...
400
  				if (!ret)
f0d1bec9d   Al Viro   new helper: copy_...
401
  					ret = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
  				break;
  			}
f0d1bec9d   Al Viro   new helper: copy_...
404
  			ret += copied;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
406
407
408
409
  
  			/* Insert it into the buffer array */
  			buf->page = page;
  			buf->ops = &anon_pipe_buf_ops;
  			buf->offset = 0;
f0d1bec9d   Al Viro   new helper: copy_...
410
  			buf->len = copied;
9883035ae   Linus Torvalds   pipes: add a "pac...
411
412
413
414
415
  			buf->flags = 0;
  			if (is_packetized(filp)) {
  				buf->ops = &packet_pipe_buf_ops;
  				buf->flags = PIPE_BUF_FLAG_PACKET;
  			}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
416
417
  			pipe->nrbufs = ++bufs;
  			pipe->tmp_page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418

f0d1bec9d   Al Viro   new helper: copy_...
419
  			if (!iov_iter_count(from))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
  				break;
  		}
35f3d14db   Jens Axboe   pipe: add support...
422
  		if (bufs < pipe->buffers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
424
  			continue;
  		if (filp->f_flags & O_NONBLOCK) {
341b446bc   Ingo Molnar   [PATCH] another r...
425
426
  			if (!ret)
  				ret = -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
  			break;
  		}
  		if (signal_pending(current)) {
341b446bc   Ingo Molnar   [PATCH] another r...
430
431
  			if (!ret)
  				ret = -ERESTARTSYS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
  			break;
  		}
  		if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
435
  			wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
436
  			kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
  			do_wakeup = 0;
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
439
440
441
  		pipe->waiting_writers++;
  		pipe_wait(pipe);
  		pipe->waiting_writers--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
  	}
  out:
ebec73f47   Al Viro   introduce variant...
444
  	__pipe_unlock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
  	if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
446
  		wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
447
  		kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
  	}
7e775f46a   Dmitry Monakhov   fs/pipe.c: skip f...
449
  	if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
c3b2da314   Josef Bacik   fs: introduce ino...
450
451
452
  		int err = file_update_time(filp);
  		if (err)
  			ret = err;
7e775f46a   Dmitry Monakhov   fs/pipe.c: skip f...
453
  		sb_end_write(file_inode(filp)->i_sb);
c3b2da314   Josef Bacik   fs: introduce ino...
454
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
456
  	return ret;
  }
d59d0b1b8   Andi Kleen   BKL-Removal: conv...
457
  static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
  {
de32ec4cf   Al Viro   pipe: set file->p...
459
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
463
  	int count, buf, nrbufs;
  
  	switch (cmd) {
  		case FIONREAD:
ebec73f47   Al Viro   introduce variant...
464
  			__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
  			count = 0;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
466
467
  			buf = pipe->curbuf;
  			nrbufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
  			while (--nrbufs >= 0) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
469
  				count += pipe->bufs[buf].len;
35f3d14db   Jens Axboe   pipe: add support...
470
  				buf = (buf+1) & (pipe->buffers - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  			}
ebec73f47   Al Viro   introduce variant...
472
  			__pipe_unlock(pipe);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
473

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
  			return put_user(count, (int __user *)arg);
  		default:
46ce341b2   Will Deacon   pipe: return -ENO...
476
  			return -ENOIOCTLCMD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
478
479
480
481
482
483
484
  	}
  }
  
  /* No kernel lock held - fine */
  static unsigned int
  pipe_poll(struct file *filp, poll_table *wait)
  {
  	unsigned int mask;
de32ec4cf   Al Viro   pipe: set file->p...
485
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
  	int nrbufs;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
487
  	poll_wait(filp, &pipe->wait, wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
489
  
  	/* Reading only -- no need for acquiring the semaphore.  */
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
490
  	nrbufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
  	mask = 0;
  	if (filp->f_mode & FMODE_READ) {
  		mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
494
  		if (!pipe->writers && filp->f_version != pipe->w_counter)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
  			mask |= POLLHUP;
  	}
  
  	if (filp->f_mode & FMODE_WRITE) {
35f3d14db   Jens Axboe   pipe: add support...
499
  		mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
5e5d7a222   Pekka Enberg   [PATCH] pipe: rem...
500
501
502
503
  		/*
  		 * Most Unices do not set POLLERR for FIFOs but on Linux they
  		 * behave exactly like pipes for poll().
  		 */
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
504
  		if (!pipe->readers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
507
508
509
  			mask |= POLLERR;
  	}
  
  	return mask;
  }
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
  {
  	int kill = 0;
  
  	spin_lock(&inode->i_lock);
  	if (!--pipe->files) {
  		inode->i_pipe = NULL;
  		kill = 1;
  	}
  	spin_unlock(&inode->i_lock);
  
  	if (kill)
  		free_pipe_info(pipe);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
  static int
599a0ac14   Al Viro   pipe: fold file_o...
525
  pipe_release(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  {
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
527
  	struct pipe_inode_info *pipe = file->private_data;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
528

ebec73f47   Al Viro   introduce variant...
529
  	__pipe_lock(pipe);
599a0ac14   Al Viro   pipe: fold file_o...
530
531
532
533
  	if (file->f_mode & FMODE_READ)
  		pipe->readers--;
  	if (file->f_mode & FMODE_WRITE)
  		pipe->writers--;
341b446bc   Ingo Molnar   [PATCH] another r...
534

ba5bb1473   Al Viro   pipe: take alloca...
535
  	if (pipe->readers || pipe->writers) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
536
  		wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
537
538
  		kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  		kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  	}
ebec73f47   Al Viro   introduce variant...
540
  	__pipe_unlock(pipe);
ba5bb1473   Al Viro   pipe: take alloca...
541

b0d8d2292   Linus Torvalds   vfs: fix subtle u...
542
  	put_pipe_info(inode, pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
544
545
546
  	return 0;
  }
  
  static int
599a0ac14   Al Viro   pipe: fold file_o...
547
  pipe_fasync(int fd, struct file *filp, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
  {
de32ec4cf   Al Viro   pipe: set file->p...
549
  	struct pipe_inode_info *pipe = filp->private_data;
599a0ac14   Al Viro   pipe: fold file_o...
550
  	int retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551

ebec73f47   Al Viro   introduce variant...
552
  	__pipe_lock(pipe);
599a0ac14   Al Viro   pipe: fold file_o...
553
554
555
  	if (filp->f_mode & FMODE_READ)
  		retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
  	if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
341b446bc   Ingo Molnar   [PATCH] another r...
556
  		retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
599a0ac14   Al Viro   pipe: fold file_o...
557
558
  		if (retval < 0 && (filp->f_mode & FMODE_READ))
  			/* this can happen only if on == T */
e5bc49ba7   Oleg Nesterov   pipe_rdwr_fasync:...
559
560
  			fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  	}
ebec73f47   Al Viro   introduce variant...
561
  	__pipe_unlock(pipe);
60aa49243   Jonathan Corbet   Rationalize fasyn...
562
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
  }
7bee130e2   Al Viro   get rid of alloc_...
564
  struct pipe_inode_info *alloc_pipe_info(void)
3a326a2ce   Ingo Molnar   [PATCH] introduce...
565
  {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
566
  	struct pipe_inode_info *pipe;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
567

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
568
569
  	pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  	if (pipe) {
35f3d14db   Jens Axboe   pipe: add support...
570
571
572
573
  		pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
  		if (pipe->bufs) {
  			init_waitqueue_head(&pipe->wait);
  			pipe->r_counter = pipe->w_counter = 1;
35f3d14db   Jens Axboe   pipe: add support...
574
  			pipe->buffers = PIPE_DEF_BUFFERS;
72b0d9aac   Al Viro   pipe: don't use -...
575
  			mutex_init(&pipe->mutex);
35f3d14db   Jens Axboe   pipe: add support...
576
577
578
  			return pipe;
  		}
  		kfree(pipe);
3a326a2ce   Ingo Molnar   [PATCH] introduce...
579
  	}
35f3d14db   Jens Axboe   pipe: add support...
580
  	return NULL;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
581
  }
4b8a8f1e4   Al Viro   get rid of the la...
582
  void free_pipe_info(struct pipe_inode_info *pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583
584
  {
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585

35f3d14db   Jens Axboe   pipe: add support...
586
  	for (i = 0; i < pipe->buffers; i++) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
587
  		struct pipe_buffer *buf = pipe->bufs + i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
  		if (buf->ops)
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
589
  			buf->ops->release(pipe, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
  	}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
591
592
  	if (pipe->tmp_page)
  		__free_page(pipe->tmp_page);
35f3d14db   Jens Axboe   pipe: add support...
593
  	kfree(pipe->bufs);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
594
  	kfree(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
  }
fa3536cc1   Eric Dumazet   [PATCH] Use __rea...
596
  static struct vfsmount *pipe_mnt __read_mostly;
341b446bc   Ingo Molnar   [PATCH] another r...
597

c23fbb6bc   Eric Dumazet   VFS: delay the de...
598
599
600
601
602
603
604
605
  /*
   * pipefs_dname() is called from d_path().
   */
  static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
  {
  	return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  				dentry->d_inode->i_ino);
  }
3ba13d179   Al Viro   constify dentry_o...
606
  static const struct dentry_operations pipefs_dentry_operations = {
c23fbb6bc   Eric Dumazet   VFS: delay the de...
607
  	.d_dname	= pipefs_dname,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
609
610
611
  };
  
  static struct inode * get_pipe_inode(void)
  {
a209dfc7b   Eric Dumazet   vfs: dont chain p...
612
  	struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
613
  	struct pipe_inode_info *pipe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
615
616
  
  	if (!inode)
  		goto fail_inode;
85fe4025c   Christoph Hellwig   fs: do not assign...
617
  	inode->i_ino = get_next_ino();
7bee130e2   Al Viro   get rid of alloc_...
618
  	pipe = alloc_pipe_info();
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
619
  	if (!pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
  		goto fail_iput;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
621

ba5bb1473   Al Viro   pipe: take alloca...
622
623
  	inode->i_pipe = pipe;
  	pipe->files = 2;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
624
  	pipe->readers = pipe->writers = 1;
599a0ac14   Al Viro   pipe: fold file_o...
625
  	inode->i_fop = &pipefifo_fops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
629
630
631
632
633
634
  
  	/*
  	 * Mark the inode dirty from the very beginning,
  	 * that way it will never be moved to the dirty
  	 * list because "mark_inode_dirty()" will think
  	 * that it already _is_ on the dirty list.
  	 */
  	inode->i_state = I_DIRTY;
  	inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
da9592ede   David Howells   CRED: Wrap task c...
635
636
  	inode->i_uid = current_fsuid();
  	inode->i_gid = current_fsgid();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637
  	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
638

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
639
640
641
642
  	return inode;
  
  fail_iput:
  	iput(inode);
341b446bc   Ingo Molnar   [PATCH] another r...
643

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
645
646
  fail_inode:
  	return NULL;
  }
e4fad8e5d   Al Viro   consolidate pipe ...
647
  int create_pipe_files(struct file **res, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
  {
d6cbd281d   Andi Kleen   [PATCH] Some clea...
649
  	int err;
e4fad8e5d   Al Viro   consolidate pipe ...
650
  	struct inode *inode = get_pipe_inode();
d6cbd281d   Andi Kleen   [PATCH] Some clea...
651
  	struct file *f;
2c48b9c45   Al Viro   switch alloc_file...
652
  	struct path path;
e4fad8e5d   Al Viro   consolidate pipe ...
653
  	static struct qstr name = { .name = "" };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
  	if (!inode)
e4fad8e5d   Al Viro   consolidate pipe ...
656
  		return -ENFILE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657

d6cbd281d   Andi Kleen   [PATCH] Some clea...
658
  	err = -ENOMEM;
4b936885a   Nick Piggin   fs: improve scala...
659
  	path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
2c48b9c45   Al Viro   switch alloc_file...
660
  	if (!path.dentry)
d6cbd281d   Andi Kleen   [PATCH] Some clea...
661
  		goto err_inode;
2c48b9c45   Al Viro   switch alloc_file...
662
  	path.mnt = mntget(pipe_mnt);
341b446bc   Ingo Molnar   [PATCH] another r...
663

2c48b9c45   Al Viro   switch alloc_file...
664
  	d_instantiate(path.dentry, inode);
430e285e0   Dave Hansen   [PATCH] fix up ne...
665
666
  
  	err = -ENFILE;
599a0ac14   Al Viro   pipe: fold file_o...
667
  	f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
39b652527   Anatol Pomozov   fs: Preserve erro...
668
  	if (IS_ERR(f))
430e285e0   Dave Hansen   [PATCH] fix up ne...
669
  		goto err_dentry;
341b446bc   Ingo Molnar   [PATCH] another r...
670

9883035ae   Linus Torvalds   pipes: add a "pac...
671
  	f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
de32ec4cf   Al Viro   pipe: set file->p...
672
  	f->private_data = inode->i_pipe;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
673

599a0ac14   Al Viro   pipe: fold file_o...
674
  	res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
39b652527   Anatol Pomozov   fs: Preserve erro...
675
  	if (IS_ERR(res[0]))
e4fad8e5d   Al Viro   consolidate pipe ...
676
677
678
  		goto err_file;
  
  	path_get(&path);
de32ec4cf   Al Viro   pipe: set file->p...
679
  	res[0]->private_data = inode->i_pipe;
e4fad8e5d   Al Viro   consolidate pipe ...
680
681
682
  	res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  	res[1] = f;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683

e4fad8e5d   Al Viro   consolidate pipe ...
684
685
686
  err_file:
  	put_filp(f);
  err_dentry:
4b8a8f1e4   Al Viro   get rid of the la...
687
  	free_pipe_info(inode->i_pipe);
2c48b9c45   Al Viro   switch alloc_file...
688
  	path_put(&path);
e4fad8e5d   Al Viro   consolidate pipe ...
689
  	return err;
ed1524371   Al Viro   [PATCH] double-fr...
690

e4fad8e5d   Al Viro   consolidate pipe ...
691
  err_inode:
4b8a8f1e4   Al Viro   get rid of the la...
692
  	free_pipe_info(inode->i_pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
  	iput(inode);
e4fad8e5d   Al Viro   consolidate pipe ...
694
  	return err;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
695
  }
5b249b1b0   Al Viro   pipe(2) - race-fr...
696
  static int __do_pipe_flags(int *fd, struct file **files, int flags)
d6cbd281d   Andi Kleen   [PATCH] Some clea...
697
  {
d6cbd281d   Andi Kleen   [PATCH] Some clea...
698
699
  	int error;
  	int fdw, fdr;
9883035ae   Linus Torvalds   pipes: add a "pac...
700
  	if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
ed8cae8ba   Ulrich Drepper   flag parameters: ...
701
  		return -EINVAL;
e4fad8e5d   Al Viro   consolidate pipe ...
702
703
704
  	error = create_pipe_files(files, flags);
  	if (error)
  		return error;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
705

ed8cae8ba   Ulrich Drepper   flag parameters: ...
706
  	error = get_unused_fd_flags(flags);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
707
708
709
  	if (error < 0)
  		goto err_read_pipe;
  	fdr = error;
ed8cae8ba   Ulrich Drepper   flag parameters: ...
710
  	error = get_unused_fd_flags(flags);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
711
712
713
  	if (error < 0)
  		goto err_fdr;
  	fdw = error;
157cf649a   Al Viro   sanitize audit_fd...
714
  	audit_fd_pair(fdr, fdw);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
715
716
  	fd[0] = fdr;
  	fd[1] = fdw;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
717
718
719
720
721
  	return 0;
  
   err_fdr:
  	put_unused_fd(fdr);
   err_read_pipe:
e4fad8e5d   Al Viro   consolidate pipe ...
722
723
  	fput(files[0]);
  	fput(files[1]);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
724
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
  }
5b249b1b0   Al Viro   pipe(2) - race-fr...
726
727
728
729
730
731
732
733
734
735
  int do_pipe_flags(int *fd, int flags)
  {
  	struct file *files[2];
  	int error = __do_pipe_flags(fd, files, flags);
  	if (!error) {
  		fd_install(fd[0], files[0]);
  		fd_install(fd[1], files[1]);
  	}
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
  /*
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
737
738
739
   * sys_pipe() is the normal C calling standard for creating
   * a pipe. It's not the way Unix traditionally does this, though.
   */
d4e82042c   Heiko Carstens   [CVE-2009-0029] S...
740
  SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
741
  {
5b249b1b0   Al Viro   pipe(2) - race-fr...
742
  	struct file *files[2];
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
743
744
  	int fd[2];
  	int error;
5b249b1b0   Al Viro   pipe(2) - race-fr...
745
  	error = __do_pipe_flags(fd, files, flags);
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
746
  	if (!error) {
5b249b1b0   Al Viro   pipe(2) - race-fr...
747
748
749
750
751
  		if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) {
  			fput(files[0]);
  			fput(files[1]);
  			put_unused_fd(fd[0]);
  			put_unused_fd(fd[1]);
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
752
  			error = -EFAULT;
5b249b1b0   Al Viro   pipe(2) - race-fr...
753
754
755
  		} else {
  			fd_install(fd[0], files[0]);
  			fd_install(fd[1], files[1]);
ba719baea   Ulrich Drepper   sys_pipe(): fix f...
756
  		}
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
757
758
759
  	}
  	return error;
  }
2b6642199   Heiko Carstens   [CVE-2009-0029] S...
760
  SYSCALL_DEFINE1(pipe, int __user *, fildes)
ed8cae8ba   Ulrich Drepper   flag parameters: ...
761
762
763
  {
  	return sys_pipe2(fildes, 0);
  }
fc7478a2b   Al Viro   pipe: switch wait...
764
  static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
f776c7388   Al Viro   fold fifo.c into ...
765
766
767
768
  {
  	int cur = *cnt;	
  
  	while (cur == *cnt) {
fc7478a2b   Al Viro   pipe: switch wait...
769
  		pipe_wait(pipe);
f776c7388   Al Viro   fold fifo.c into ...
770
771
772
773
774
  		if (signal_pending(current))
  			break;
  	}
  	return cur == *cnt ? -ERESTARTSYS : 0;
  }
fc7478a2b   Al Viro   pipe: switch wait...
775
  static void wake_up_partner(struct pipe_inode_info *pipe)
f776c7388   Al Viro   fold fifo.c into ...
776
  {
fc7478a2b   Al Viro   pipe: switch wait...
777
  	wake_up_interruptible(&pipe->wait);
f776c7388   Al Viro   fold fifo.c into ...
778
779
780
781
782
  }
  
  static int fifo_open(struct inode *inode, struct file *filp)
  {
  	struct pipe_inode_info *pipe;
599a0ac14   Al Viro   pipe: fold file_o...
783
  	bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
f776c7388   Al Viro   fold fifo.c into ...
784
  	int ret;
ba5bb1473   Al Viro   pipe: take alloca...
785
786
787
788
789
790
791
792
793
  	filp->f_version = 0;
  
  	spin_lock(&inode->i_lock);
  	if (inode->i_pipe) {
  		pipe = inode->i_pipe;
  		pipe->files++;
  		spin_unlock(&inode->i_lock);
  	} else {
  		spin_unlock(&inode->i_lock);
7bee130e2   Al Viro   get rid of alloc_...
794
  		pipe = alloc_pipe_info();
f776c7388   Al Viro   fold fifo.c into ...
795
  		if (!pipe)
ba5bb1473   Al Viro   pipe: take alloca...
796
797
798
799
800
801
  			return -ENOMEM;
  		pipe->files = 1;
  		spin_lock(&inode->i_lock);
  		if (unlikely(inode->i_pipe)) {
  			inode->i_pipe->files++;
  			spin_unlock(&inode->i_lock);
4b8a8f1e4   Al Viro   get rid of the la...
802
  			free_pipe_info(pipe);
ba5bb1473   Al Viro   pipe: take alloca...
803
804
805
806
807
  			pipe = inode->i_pipe;
  		} else {
  			inode->i_pipe = pipe;
  			spin_unlock(&inode->i_lock);
  		}
f776c7388   Al Viro   fold fifo.c into ...
808
  	}
de32ec4cf   Al Viro   pipe: set file->p...
809
  	filp->private_data = pipe;
ba5bb1473   Al Viro   pipe: take alloca...
810
  	/* OK, we have a pipe and it's pinned down */
ebec73f47   Al Viro   introduce variant...
811
  	__pipe_lock(pipe);
f776c7388   Al Viro   fold fifo.c into ...
812
813
814
815
816
817
818
819
820
821
822
  
  	/* We can only do regular read/write on fifos */
  	filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  
  	switch (filp->f_mode) {
  	case FMODE_READ:
  	/*
  	 *  O_RDONLY
  	 *  POSIX.1 says that O_NONBLOCK means return with the FIFO
  	 *  opened, even when there is no process writing the FIFO.
  	 */
f776c7388   Al Viro   fold fifo.c into ...
823
824
  		pipe->r_counter++;
  		if (pipe->readers++ == 0)
fc7478a2b   Al Viro   pipe: switch wait...
825
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
826

599a0ac14   Al Viro   pipe: fold file_o...
827
  		if (!is_pipe && !pipe->writers) {
f776c7388   Al Viro   fold fifo.c into ...
828
829
830
831
832
  			if ((filp->f_flags & O_NONBLOCK)) {
  				/* suppress POLLHUP until we have
  				 * seen a writer */
  				filp->f_version = pipe->w_counter;
  			} else {
fc7478a2b   Al Viro   pipe: switch wait...
833
  				if (wait_for_partner(pipe, &pipe->w_counter))
f776c7388   Al Viro   fold fifo.c into ...
834
835
836
837
838
839
840
841
842
843
844
845
  					goto err_rd;
  			}
  		}
  		break;
  	
  	case FMODE_WRITE:
  	/*
  	 *  O_WRONLY
  	 *  POSIX.1 says that O_NONBLOCK means return -1 with
  	 *  errno=ENXIO when there is no process reading the FIFO.
  	 */
  		ret = -ENXIO;
599a0ac14   Al Viro   pipe: fold file_o...
846
  		if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
f776c7388   Al Viro   fold fifo.c into ...
847
  			goto err;
f776c7388   Al Viro   fold fifo.c into ...
848
849
  		pipe->w_counter++;
  		if (!pipe->writers++)
fc7478a2b   Al Viro   pipe: switch wait...
850
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
851

599a0ac14   Al Viro   pipe: fold file_o...
852
  		if (!is_pipe && !pipe->readers) {
fc7478a2b   Al Viro   pipe: switch wait...
853
  			if (wait_for_partner(pipe, &pipe->r_counter))
f776c7388   Al Viro   fold fifo.c into ...
854
855
856
857
858
859
860
861
862
863
864
  				goto err_wr;
  		}
  		break;
  	
  	case FMODE_READ | FMODE_WRITE:
  	/*
  	 *  O_RDWR
  	 *  POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  	 *  This implementation will NEVER block on a O_RDWR open, since
  	 *  the process can at least talk to itself.
  	 */
f776c7388   Al Viro   fold fifo.c into ...
865
866
867
868
869
870
  
  		pipe->readers++;
  		pipe->writers++;
  		pipe->r_counter++;
  		pipe->w_counter++;
  		if (pipe->readers == 1 || pipe->writers == 1)
fc7478a2b   Al Viro   pipe: switch wait...
871
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
872
873
874
875
876
877
878
879
  		break;
  
  	default:
  		ret = -EINVAL;
  		goto err;
  	}
  
  	/* Ok! */
ebec73f47   Al Viro   introduce variant...
880
  	__pipe_unlock(pipe);
f776c7388   Al Viro   fold fifo.c into ...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
  	return 0;
  
  err_rd:
  	if (!--pipe->readers)
  		wake_up_interruptible(&pipe->wait);
  	ret = -ERESTARTSYS;
  	goto err;
  
  err_wr:
  	if (!--pipe->writers)
  		wake_up_interruptible(&pipe->wait);
  	ret = -ERESTARTSYS;
  	goto err;
  
  err:
ebec73f47   Al Viro   introduce variant...
896
  	__pipe_unlock(pipe);
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
897
898
  
  	put_pipe_info(inode, pipe);
f776c7388   Al Viro   fold fifo.c into ...
899
900
  	return ret;
  }
599a0ac14   Al Viro   pipe: fold file_o...
901
902
903
  const struct file_operations pipefifo_fops = {
  	.open		= fifo_open,
  	.llseek		= no_llseek,
fb9096a34   Al Viro   pipe: switch to -...
904
905
  	.read		= new_sync_read,
  	.read_iter	= pipe_read,
f0d1bec9d   Al Viro   new helper: copy_...
906
907
  	.write		= new_sync_write,
  	.write_iter	= pipe_write,
599a0ac14   Al Viro   pipe: fold file_o...
908
909
910
911
  	.poll		= pipe_poll,
  	.unlocked_ioctl	= pipe_ioctl,
  	.release	= pipe_release,
  	.fasync		= pipe_fasync,
f776c7388   Al Viro   fold fifo.c into ...
912
  };
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
913
  /*
35f3d14db   Jens Axboe   pipe: add support...
914
915
916
   * Allocate a new array of pipe buffers and copy the info over. Returns the
   * pipe size if successful, or return -ERROR on error.
   */
b9598db34   Jens Axboe   pipe: make F_{GET...
917
  static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
35f3d14db   Jens Axboe   pipe: add support...
918
919
920
921
  {
  	struct pipe_buffer *bufs;
  
  	/*
35f3d14db   Jens Axboe   pipe: add support...
922
923
924
925
926
  	 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
  	 * expect a lot of shrink+grow operations, just free and allocate
  	 * again like we would do for growing. If the pipe currently
  	 * contains more buffers than arg, then return busy.
  	 */
b9598db34   Jens Axboe   pipe: make F_{GET...
927
  	if (nr_pages < pipe->nrbufs)
35f3d14db   Jens Axboe   pipe: add support...
928
  		return -EBUSY;
2ccd4f4d4   Sasha Levin   pipe: fail cleanl...
929
  	bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
35f3d14db   Jens Axboe   pipe: add support...
930
931
932
933
934
935
936
937
  	if (unlikely(!bufs))
  		return -ENOMEM;
  
  	/*
  	 * The pipe array wraps around, so just start the new one at zero
  	 * and adjust the indexes.
  	 */
  	if (pipe->nrbufs) {
1d862f412   Miklos Szeredi   pipe: fix pipe bu...
938
939
  		unsigned int tail;
  		unsigned int head;
35f3d14db   Jens Axboe   pipe: add support...
940

1d862f412   Miklos Szeredi   pipe: fix pipe bu...
941
942
943
944
945
946
947
  		tail = pipe->curbuf + pipe->nrbufs;
  		if (tail < pipe->buffers)
  			tail = 0;
  		else
  			tail &= (pipe->buffers - 1);
  
  		head = pipe->nrbufs - tail;
35f3d14db   Jens Axboe   pipe: add support...
948
949
950
  		if (head)
  			memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  		if (tail)
1d862f412   Miklos Szeredi   pipe: fix pipe bu...
951
  			memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
35f3d14db   Jens Axboe   pipe: add support...
952
953
954
955
956
  	}
  
  	pipe->curbuf = 0;
  	kfree(pipe->bufs);
  	pipe->bufs = bufs;
b9598db34   Jens Axboe   pipe: make F_{GET...
957
958
  	pipe->buffers = nr_pages;
  	return nr_pages * PAGE_SIZE;
35f3d14db   Jens Axboe   pipe: add support...
959
  }
ff9da691c   Jens Axboe   pipe: change /pro...
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  /*
   * Currently we rely on the pipe array holding a power-of-2 number
   * of pages.
   */
  static inline unsigned int round_pipe_size(unsigned int size)
  {
  	unsigned long nr_pages;
  
  	nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  	return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
  }
  
  /*
   * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
   * will return an error.
   */
  int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
  		 size_t *lenp, loff_t *ppos)
  {
  	int ret;
  
  	ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
  	if (ret < 0 || !write)
  		return ret;
  
  	pipe_max_size = round_pipe_size(pipe_max_size);
  	return ret;
  }
720836465   Linus Torvalds   Un-inline get_pip...
988
989
990
991
992
993
994
  /*
   * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
   * location, so checking ->i_pipe is not enough to verify that this is a
   * pipe.
   */
  struct pipe_inode_info *get_pipe_info(struct file *file)
  {
de32ec4cf   Al Viro   pipe: set file->p...
995
  	return file->f_op == &pipefifo_fops ? file->private_data : NULL;
720836465   Linus Torvalds   Un-inline get_pip...
996
  }
35f3d14db   Jens Axboe   pipe: add support...
997
998
999
1000
  long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  {
  	struct pipe_inode_info *pipe;
  	long ret;
c66fb3479   Linus Torvalds   Export 'get_pipe_...
1001
  	pipe = get_pipe_info(file);
35f3d14db   Jens Axboe   pipe: add support...
1002
1003
  	if (!pipe)
  		return -EBADF;
ebec73f47   Al Viro   introduce variant...
1004
  	__pipe_lock(pipe);
35f3d14db   Jens Axboe   pipe: add support...
1005
1006
  
  	switch (cmd) {
b9598db34   Jens Axboe   pipe: make F_{GET...
1007
  	case F_SETPIPE_SZ: {
ff9da691c   Jens Axboe   pipe: change /pro...
1008
  		unsigned int size, nr_pages;
b9598db34   Jens Axboe   pipe: make F_{GET...
1009

ff9da691c   Jens Axboe   pipe: change /pro...
1010
1011
  		size = round_pipe_size(arg);
  		nr_pages = size >> PAGE_SHIFT;
b9598db34   Jens Axboe   pipe: make F_{GET...
1012

6db40cf04   Miklos Szeredi   pipe: fix check i...
1013
1014
1015
  		ret = -EINVAL;
  		if (!nr_pages)
  			goto out;
ff9da691c   Jens Axboe   pipe: change /pro...
1016
  		if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
b4ca76157   Jens Axboe   Merge branch 'mas...
1017
  			ret = -EPERM;
cc967be54   Julia Lawall   fs: Add missing m...
1018
  			goto out;
cc967be54   Julia Lawall   fs: Add missing m...
1019
  		}
ff9da691c   Jens Axboe   pipe: change /pro...
1020
  		ret = pipe_set_size(pipe, nr_pages);
35f3d14db   Jens Axboe   pipe: add support...
1021
  		break;
b9598db34   Jens Axboe   pipe: make F_{GET...
1022
  		}
35f3d14db   Jens Axboe   pipe: add support...
1023
  	case F_GETPIPE_SZ:
b9598db34   Jens Axboe   pipe: make F_{GET...
1024
  		ret = pipe->buffers * PAGE_SIZE;
35f3d14db   Jens Axboe   pipe: add support...
1025
1026
1027
1028
1029
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
cc967be54   Julia Lawall   fs: Add missing m...
1030
  out:
ebec73f47   Al Viro   introduce variant...
1031
  	__pipe_unlock(pipe);
35f3d14db   Jens Axboe   pipe: add support...
1032
1033
  	return ret;
  }
ff0c7d15f   Nick Piggin   fs: avoid inode R...
1034
1035
  static const struct super_operations pipefs_ops = {
  	.destroy_inode = free_inode_nonrcu,
d70ef97ba   Pavel Emelyanov   fs/pipe.c: add ->...
1036
  	.statfs = simple_statfs,
ff0c7d15f   Nick Piggin   fs: avoid inode R...
1037
  };
35f3d14db   Jens Axboe   pipe: add support...
1038
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1039
1040
1041
1042
1043
   * pipefs should _never_ be mounted by userland - too much of security hassle,
   * no real gain from having the whole whorehouse mounted. So we don't need
   * any operations on the root directory. However, we need a non-trivial
   * d_name - pipe: will go nicely and kill the special-casing in procfs.
   */
51139adac   Al Viro   convert get_sb_ps...
1044
1045
  static struct dentry *pipefs_mount(struct file_system_type *fs_type,
  			 int flags, const char *dev_name, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
  {
c74a1cbb3   Al Viro   pass default dent...
1047
1048
  	return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  			&pipefs_dentry_operations, PIPEFS_MAGIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1049
1050
1051
1052
  }
  
  static struct file_system_type pipe_fs_type = {
  	.name		= "pipefs",
51139adac   Al Viro   convert get_sb_ps...
1053
  	.mount		= pipefs_mount,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
1056
1057
1058
1059
  	.kill_sb	= kill_anon_super,
  };
  
  static int __init init_pipe_fs(void)
  {
  	int err = register_filesystem(&pipe_fs_type);
341b446bc   Ingo Molnar   [PATCH] another r...
1060

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
1062
1063
1064
1065
1066
1067
1068
1069
  	if (!err) {
  		pipe_mnt = kern_mount(&pipe_fs_type);
  		if (IS_ERR(pipe_mnt)) {
  			err = PTR_ERR(pipe_mnt);
  			unregister_filesystem(&pipe_fs_type);
  		}
  	}
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
  fs_initcall(init_pipe_fs);