Blame view

fs/pipe.c 26.8 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
  }
858119e15   Arjan van de Ven   [PATCH] Unlinline...
111
  static int
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
112
113
  pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
  			int atomic)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
119
120
  {
  	unsigned long copy;
  
  	while (len > 0) {
  		while (!iov->iov_len)
  			iov++;
  		copy = min_t(unsigned long, len, iov->iov_len);
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
121
122
123
124
125
126
127
  		if (atomic) {
  			if (__copy_from_user_inatomic(to, iov->iov_base, copy))
  				return -EFAULT;
  		} else {
  			if (copy_from_user(to, iov->iov_base, copy))
  				return -EFAULT;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
132
133
134
  		to += copy;
  		len -= copy;
  		iov->iov_base += copy;
  		iov->iov_len -= copy;
  	}
  	return 0;
  }
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  /*
   * Pre-fault in the user memory, so we can use atomic copies.
   */
  static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
  {
  	while (!iov->iov_len)
  		iov++;
  
  	while (len > 0) {
  		unsigned long this_len;
  
  		this_len = min_t(unsigned long, len, iov->iov_len);
  		fault_in_pages_readable(iov->iov_base, this_len);
  		len -= this_len;
  		iov++;
  	}
  }
341b446bc   Ingo Molnar   [PATCH] another r...
152
153
  static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
  				  struct pipe_buffer *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
  {
  	struct page *page = buf->page;
5274f052e   Jens Axboe   [PATCH] Introduce...
156
157
158
  	/*
  	 * 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...
159
  	 * allocation cache. (Otherwise just release our reference to it)
5274f052e   Jens Axboe   [PATCH] Introduce...
160
  	 */
341b446bc   Ingo Molnar   [PATCH] another r...
161
  	if (page_count(page) == 1 && !pipe->tmp_page)
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
162
  		pipe->tmp_page = page;
341b446bc   Ingo Molnar   [PATCH] another r...
163
164
  	else
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  }
0845718da   Jens Axboe   pipe: add documen...
166
  /**
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
167
   * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
0845718da   Jens Axboe   pipe: add documen...
168
169
170
171
   * @pipe:	the pipe that the buffer belongs to
   * @buf:	the buffer to attempt to steal
   *
   * Description:
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
172
   *	This function attempts to steal the &struct page attached to
0845718da   Jens Axboe   pipe: add documen...
173
174
   *	@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...
175
   *	he wishes; the typical use is insertion into a different file
0845718da   Jens Axboe   pipe: add documen...
176
177
   *	page cache.
   */
330ab7161   Jens Axboe   [PATCH] vmsplice:...
178
179
  int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
  			   struct pipe_buffer *buf)
5abc97aa2   Jens Axboe   [PATCH] splice: a...
180
  {
46e678c96   Jens Axboe   [PATCH] splice: f...
181
  	struct page *page = buf->page;
0845718da   Jens Axboe   pipe: add documen...
182
183
184
185
186
  	/*
  	 * 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...
187
  	if (page_count(page) == 1) {
46e678c96   Jens Axboe   [PATCH] splice: f...
188
189
190
191
192
  		lock_page(page);
  		return 0;
  	}
  
  	return 1;
5abc97aa2   Jens Axboe   [PATCH] splice: a...
193
  }
51921cb74   Miklos Szeredi   mm: export generi...
194
  EXPORT_SYMBOL(generic_pipe_buf_steal);
5abc97aa2   Jens Axboe   [PATCH] splice: a...
195

0845718da   Jens Axboe   pipe: add documen...
196
  /**
b51d63c6d   Randy Dunlap   kernel-doc: fix f...
197
   * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
0845718da   Jens Axboe   pipe: add documen...
198
199
200
201
202
203
204
205
206
   * @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...
207
208
209
  {
  	page_cache_get(buf->page);
  }
51921cb74   Miklos Szeredi   mm: export generi...
210
  EXPORT_SYMBOL(generic_pipe_buf_get);
70524490e   Jens Axboe   [PATCH] splice: a...
211

0845718da   Jens Axboe   pipe: add documen...
212
213
  /**
   * generic_pipe_buf_confirm - verify contents of the pipe buffer
79685b8de   Randy Dunlap   docbook: add pipe...
214
   * @info:	the pipe that the buffer belongs to
0845718da   Jens Axboe   pipe: add documen...
215
216
217
218
219
220
   * @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 ...
221
222
  int generic_pipe_buf_confirm(struct pipe_inode_info *info,
  			     struct pipe_buffer *buf)
f84d75199   Jens Axboe   [PATCH] pipe: int...
223
224
225
  {
  	return 0;
  }
51921cb74   Miklos Szeredi   mm: export generi...
226
  EXPORT_SYMBOL(generic_pipe_buf_confirm);
f84d75199   Jens Axboe   [PATCH] pipe: int...
227

6818173bd   Miklos Szeredi   splice: implement...
228
229
230
231
232
233
234
235
236
237
238
239
240
  /**
   * 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...
241
  EXPORT_SYMBOL(generic_pipe_buf_release);
6818173bd   Miklos Szeredi   splice: implement...
242

d4c3cca94   Eric Dumazet   [PATCH] constify ...
243
  static const struct pipe_buf_operations anon_pipe_buf_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
  	.can_merge = 1,
cac36bb06   Jens Axboe   pipe: change the ...
245
  	.confirm = generic_pipe_buf_confirm,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  	.release = anon_pipe_buf_release,
330ab7161   Jens Axboe   [PATCH] vmsplice:...
247
  	.steal = generic_pipe_buf_steal,
f84d75199   Jens Axboe   [PATCH] pipe: int...
248
  	.get = generic_pipe_buf_get,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
  };
9883035ae   Linus Torvalds   pipes: add a "pac...
250
251
  static const struct pipe_buf_operations packet_pipe_buf_ops = {
  	.can_merge = 0,
9883035ae   Linus Torvalds   pipes: add a "pac...
252
253
254
255
256
  	.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
257
  static ssize_t
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
258
259
  pipe_read(struct kiocb *iocb, const struct iovec *_iov,
  	   unsigned long nr_segs, loff_t pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
261
  	struct file *filp = iocb->ki_filp;
de32ec4cf   Al Viro   pipe: set file->p...
262
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
264
265
266
  	int do_wakeup;
  	ssize_t ret;
  	struct iovec *iov = (struct iovec *)_iov;
  	size_t total_len;
637b58c28   Al Viro   switch pipe_read(...
267
  	struct iov_iter iter;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
269
270
271
272
  
  	total_len = iov_length(iov, nr_segs);
  	/* Null read succeeds. */
  	if (unlikely(total_len == 0))
  		return 0;
637b58c28   Al Viro   switch pipe_read(...
273
  	iov_iter_init(&iter, iov, nr_segs, total_len, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
  	do_wakeup = 0;
  	ret = 0;
ebec73f47   Al Viro   introduce variant...
276
  	__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
  	for (;;) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
278
  		int bufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
  		if (bufs) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
280
281
  			int curbuf = pipe->curbuf;
  			struct pipe_buffer *buf = pipe->bufs + curbuf;
d4c3cca94   Eric Dumazet   [PATCH] constify ...
282
  			const struct pipe_buf_operations *ops = buf->ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  			size_t chars = buf->len;
637b58c28   Al Viro   switch pipe_read(...
284
285
  			size_t written;
  			int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
288
  
  			if (chars > total_len)
  				chars = total_len;
cac36bb06   Jens Axboe   pipe: change the ...
289
  			error = ops->confirm(pipe, buf);
f84d75199   Jens Axboe   [PATCH] pipe: int...
290
  			if (error) {
5274f052e   Jens Axboe   [PATCH] Introduce...
291
  				if (!ret)
e5953cbdf   Nicolas Kaiser   pipe: fix failure...
292
  					ret = error;
5274f052e   Jens Axboe   [PATCH] Introduce...
293
294
  				break;
  			}
f84d75199   Jens Axboe   [PATCH] pipe: int...
295

637b58c28   Al Viro   switch pipe_read(...
296
297
  			written = copy_page_to_iter(buf->page, buf->offset, chars, &iter);
  			if (unlikely(written < chars)) {
341b446bc   Ingo Molnar   [PATCH] another r...
298
  				if (!ret)
637b58c28   Al Viro   switch pipe_read(...
299
  					ret = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
301
302
303
304
  				break;
  			}
  			ret += chars;
  			buf->offset += chars;
  			buf->len -= chars;
9883035ae   Linus Torvalds   pipes: add a "pac...
305
306
307
308
309
310
  
  			/* 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
311
312
  			if (!buf->len) {
  				buf->ops = NULL;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
313
  				ops->release(pipe, buf);
35f3d14db   Jens Axboe   pipe: add support...
314
  				curbuf = (curbuf + 1) & (pipe->buffers - 1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
315
316
  				pipe->curbuf = curbuf;
  				pipe->nrbufs = --bufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
323
324
  				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...
325
  		if (!pipe->writers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  			break;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
327
  		if (!pipe->waiting_writers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
334
335
336
337
338
339
340
  			/* 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...
341
342
  			if (!ret)
  				ret = -ERESTARTSYS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
  			break;
  		}
  		if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
346
  			wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
347
   			kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
349
  		pipe_wait(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
  	}
ebec73f47   Al Viro   introduce variant...
351
  	__pipe_unlock(pipe);
341b446bc   Ingo Molnar   [PATCH] another r...
352
353
  
  	/* Signal writers asynchronously that there is more room. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
  	if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
355
  		wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
356
  		kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
361
  	}
  	if (ret > 0)
  		file_accessed(filp);
  	return ret;
  }
9883035ae   Linus Torvalds   pipes: add a "pac...
362
363
364
365
  static inline int is_packetized(struct file *file)
  {
  	return (file->f_flags & O_DIRECT) != 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
  static ssize_t
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
367
368
  pipe_write(struct kiocb *iocb, const struct iovec *_iov,
  	    unsigned long nr_segs, loff_t ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  {
ee0b3e671   Badari Pulavarty   [PATCH] Remove re...
370
  	struct file *filp = iocb->ki_filp;
de32ec4cf   Al Viro   pipe: set file->p...
371
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
375
376
377
378
379
380
381
382
383
384
  	ssize_t ret;
  	int do_wakeup;
  	struct iovec *iov = (struct iovec *)_iov;
  	size_t total_len;
  	ssize_t chars;
  
  	total_len = iov_length(iov, nr_segs);
  	/* Null write succeeds. */
  	if (unlikely(total_len == 0))
  		return 0;
  
  	do_wakeup = 0;
  	ret = 0;
ebec73f47   Al Viro   introduce variant...
385
  	__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
387
  	if (!pipe->readers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
393
394
  		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...
395
  	if (pipe->nrbufs && chars != 0) {
341b446bc   Ingo Molnar   [PATCH] another r...
396
  		int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
35f3d14db   Jens Axboe   pipe: add support...
397
  							(pipe->buffers - 1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
398
  		struct pipe_buffer *buf = pipe->bufs + lastbuf;
d4c3cca94   Eric Dumazet   [PATCH] constify ...
399
  		const struct pipe_buf_operations *ops = buf->ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  		int offset = buf->offset + buf->len;
341b446bc   Ingo Molnar   [PATCH] another r...
401

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  		if (ops->can_merge && offset + chars <= PAGE_SIZE) {
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
403
  			int error, atomic = 1;
5274f052e   Jens Axboe   [PATCH] Introduce...
404
  			void *addr;
5274f052e   Jens Axboe   [PATCH] Introduce...
405

cac36bb06   Jens Axboe   pipe: change the ...
406
  			error = ops->confirm(pipe, buf);
f84d75199   Jens Axboe   [PATCH] pipe: int...
407
  			if (error)
5274f052e   Jens Axboe   [PATCH] Introduce...
408
  				goto out;
f84d75199   Jens Axboe   [PATCH] pipe: int...
409

f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
410
411
  			iov_fault_in_pages_read(iov, chars);
  redo1:
fbb32750a   Al Viro   pipe: kill ->map(...
412
413
414
415
  			if (atomic)
  				addr = kmap_atomic(buf->page);
  			else
  				addr = kmap(buf->page);
5274f052e   Jens Axboe   [PATCH] Introduce...
416
  			error = pipe_iov_copy_from_user(offset + addr, iov,
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
417
  							chars, atomic);
fbb32750a   Al Viro   pipe: kill ->map(...
418
419
420
421
  			if (atomic)
  				kunmap_atomic(addr);
  			else
  				kunmap(buf->page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
  			ret = error;
  			do_wakeup = 1;
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
424
425
426
427
428
  			if (error) {
  				if (atomic) {
  					atomic = 0;
  					goto redo1;
  				}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
  				goto out;
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
430
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
432
433
434
435
436
437
438
439
440
  			buf->len += chars;
  			total_len -= chars;
  			ret = chars;
  			if (!total_len)
  				goto out;
  		}
  	}
  
  	for (;;) {
  		int bufs;
341b446bc   Ingo Molnar   [PATCH] another r...
441

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
442
  		if (!pipe->readers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  			send_sig(SIGPIPE, current, 0);
341b446bc   Ingo Molnar   [PATCH] another r...
444
445
  			if (!ret)
  				ret = -EPIPE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
447
  			break;
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
448
  		bufs = pipe->nrbufs;
35f3d14db   Jens Axboe   pipe: add support...
449
450
  		if (bufs < pipe->buffers) {
  			int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
451
452
  			struct pipe_buffer *buf = pipe->bufs + newbuf;
  			struct page *page = pipe->tmp_page;
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
453
454
  			char *src;
  			int error, atomic = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
456
457
458
459
460
461
  
  			if (!page) {
  				page = alloc_page(GFP_HIGHUSER);
  				if (unlikely(!page)) {
  					ret = ret ? : -ENOMEM;
  					break;
  				}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
462
  				pipe->tmp_page = page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  			}
341b446bc   Ingo Molnar   [PATCH] another r...
464
  			/* Always wake up, even if the copy fails. Otherwise
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
468
469
470
471
472
  			 * we lock up (O_NONBLOCK-)readers that sleep due to
  			 * syscall merging.
  			 * FIXME! Is this really true?
  			 */
  			do_wakeup = 1;
  			chars = PAGE_SIZE;
  			if (chars > total_len)
  				chars = total_len;
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
473
474
475
  			iov_fault_in_pages_read(iov, chars);
  redo2:
  			if (atomic)
e8e3c3d66   Cong Wang   fs: remove the se...
476
  				src = kmap_atomic(page);
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
477
478
479
480
481
482
  			else
  				src = kmap(page);
  
  			error = pipe_iov_copy_from_user(src, iov, chars,
  							atomic);
  			if (atomic)
e8e3c3d66   Cong Wang   fs: remove the se...
483
  				kunmap_atomic(src);
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
484
485
  			else
  				kunmap(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
  			if (unlikely(error)) {
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
487
488
489
490
  				if (atomic) {
  					atomic = 0;
  					goto redo2;
  				}
341b446bc   Ingo Molnar   [PATCH] another r...
491
  				if (!ret)
f6762b7ad   Jens Axboe   [PATCH] pipe: ena...
492
  					ret = error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
494
495
496
497
498
499
500
501
  				break;
  			}
  			ret += chars;
  
  			/* Insert it into the buffer array */
  			buf->page = page;
  			buf->ops = &anon_pipe_buf_ops;
  			buf->offset = 0;
  			buf->len = chars;
9883035ae   Linus Torvalds   pipes: add a "pac...
502
503
504
505
506
  			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...
507
508
  			pipe->nrbufs = ++bufs;
  			pipe->tmp_page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
511
512
513
  
  			total_len -= chars;
  			if (!total_len)
  				break;
  		}
35f3d14db   Jens Axboe   pipe: add support...
514
  		if (bufs < pipe->buffers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
  			continue;
  		if (filp->f_flags & O_NONBLOCK) {
341b446bc   Ingo Molnar   [PATCH] another r...
517
518
  			if (!ret)
  				ret = -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
  			break;
  		}
  		if (signal_pending(current)) {
341b446bc   Ingo Molnar   [PATCH] another r...
522
523
  			if (!ret)
  				ret = -ERESTARTSYS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
  			break;
  		}
  		if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
527
  			wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
528
  			kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
  			do_wakeup = 0;
  		}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
531
532
533
  		pipe->waiting_writers++;
  		pipe_wait(pipe);
  		pipe->waiting_writers--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534
535
  	}
  out:
ebec73f47   Al Viro   introduce variant...
536
  	__pipe_unlock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  	if (do_wakeup) {
28e58ee8c   Linus Torvalds   Fix broken "pipe:...
538
  		wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
539
  		kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
  	}
7e775f46a   Dmitry Monakhov   fs/pipe.c: skip f...
541
  	if (ret > 0 && sb_start_write_trylock(file_inode(filp)->i_sb)) {
c3b2da314   Josef Bacik   fs: introduce ino...
542
543
544
  		int err = file_update_time(filp);
  		if (err)
  			ret = err;
7e775f46a   Dmitry Monakhov   fs/pipe.c: skip f...
545
  		sb_end_write(file_inode(filp)->i_sb);
c3b2da314   Josef Bacik   fs: introduce ino...
546
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
  	return ret;
  }
d59d0b1b8   Andi Kleen   BKL-Removal: conv...
549
  static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
  {
de32ec4cf   Al Viro   pipe: set file->p...
551
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
  	int count, buf, nrbufs;
  
  	switch (cmd) {
  		case FIONREAD:
ebec73f47   Al Viro   introduce variant...
556
  			__pipe_lock(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
  			count = 0;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
558
559
  			buf = pipe->curbuf;
  			nrbufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
  			while (--nrbufs >= 0) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
561
  				count += pipe->bufs[buf].len;
35f3d14db   Jens Axboe   pipe: add support...
562
  				buf = (buf+1) & (pipe->buffers - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
  			}
ebec73f47   Al Viro   introduce variant...
564
  			__pipe_unlock(pipe);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
565

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
  			return put_user(count, (int __user *)arg);
  		default:
46ce341b2   Will Deacon   pipe: return -ENO...
568
  			return -ENOIOCTLCMD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
574
575
576
  	}
  }
  
  /* 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...
577
  	struct pipe_inode_info *pipe = filp->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
  	int nrbufs;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
579
  	poll_wait(filp, &pipe->wait, wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
581
  
  	/* Reading only -- no need for acquiring the semaphore.  */
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
582
  	nrbufs = pipe->nrbufs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
583
584
585
  	mask = 0;
  	if (filp->f_mode & FMODE_READ) {
  		mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
586
  		if (!pipe->writers && filp->f_version != pipe->w_counter)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587
588
589
590
  			mask |= POLLHUP;
  	}
  
  	if (filp->f_mode & FMODE_WRITE) {
35f3d14db   Jens Axboe   pipe: add support...
591
  		mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
5e5d7a222   Pekka Enberg   [PATCH] pipe: rem...
592
593
594
595
  		/*
  		 * 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...
596
  		if (!pipe->readers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
599
600
601
  			mask |= POLLERR;
  	}
  
  	return mask;
  }
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  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
616
  static int
599a0ac14   Al Viro   pipe: fold file_o...
617
  pipe_release(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
  {
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
619
  	struct pipe_inode_info *pipe = file->private_data;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
620

ebec73f47   Al Viro   introduce variant...
621
  	__pipe_lock(pipe);
599a0ac14   Al Viro   pipe: fold file_o...
622
623
624
625
  	if (file->f_mode & FMODE_READ)
  		pipe->readers--;
  	if (file->f_mode & FMODE_WRITE)
  		pipe->writers--;
341b446bc   Ingo Molnar   [PATCH] another r...
626

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

b0d8d2292   Linus Torvalds   vfs: fix subtle u...
634
  	put_pipe_info(inode, pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
637
638
  	return 0;
  }
  
  static int
599a0ac14   Al Viro   pipe: fold file_o...
639
  pipe_fasync(int fd, struct file *filp, int on)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
  {
de32ec4cf   Al Viro   pipe: set file->p...
641
  	struct pipe_inode_info *pipe = filp->private_data;
599a0ac14   Al Viro   pipe: fold file_o...
642
  	int retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643

ebec73f47   Al Viro   introduce variant...
644
  	__pipe_lock(pipe);
599a0ac14   Al Viro   pipe: fold file_o...
645
646
647
  	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...
648
  		retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
599a0ac14   Al Viro   pipe: fold file_o...
649
650
  		if (retval < 0 && (filp->f_mode & FMODE_READ))
  			/* this can happen only if on == T */
e5bc49ba7   Oleg Nesterov   pipe_rdwr_fasync:...
651
652
  			fasync_helper(-1, filp, 0, &pipe->fasync_readers);
  	}
ebec73f47   Al Viro   introduce variant...
653
  	__pipe_unlock(pipe);
60aa49243   Jonathan Corbet   Rationalize fasyn...
654
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
  }
7bee130e2   Al Viro   get rid of alloc_...
656
  struct pipe_inode_info *alloc_pipe_info(void)
3a326a2ce   Ingo Molnar   [PATCH] introduce...
657
  {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
658
  	struct pipe_inode_info *pipe;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
659

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
660
661
  	pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
  	if (pipe) {
35f3d14db   Jens Axboe   pipe: add support...
662
663
664
665
  		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...
666
  			pipe->buffers = PIPE_DEF_BUFFERS;
72b0d9aac   Al Viro   pipe: don't use -...
667
  			mutex_init(&pipe->mutex);
35f3d14db   Jens Axboe   pipe: add support...
668
669
670
  			return pipe;
  		}
  		kfree(pipe);
3a326a2ce   Ingo Molnar   [PATCH] introduce...
671
  	}
35f3d14db   Jens Axboe   pipe: add support...
672
  	return NULL;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
673
  }
4b8a8f1e4   Al Viro   get rid of the la...
674
  void free_pipe_info(struct pipe_inode_info *pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
  {
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677

35f3d14db   Jens Axboe   pipe: add support...
678
  	for (i = 0; i < pipe->buffers; i++) {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
679
  		struct pipe_buffer *buf = pipe->bufs + i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
  		if (buf->ops)
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
681
  			buf->ops->release(pipe, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
  	}
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
683
684
  	if (pipe->tmp_page)
  		__free_page(pipe->tmp_page);
35f3d14db   Jens Axboe   pipe: add support...
685
  	kfree(pipe->bufs);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
686
  	kfree(pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
  }
fa3536cc1   Eric Dumazet   [PATCH] Use __rea...
688
  static struct vfsmount *pipe_mnt __read_mostly;
341b446bc   Ingo Molnar   [PATCH] another r...
689

c23fbb6bc   Eric Dumazet   VFS: delay the de...
690
691
692
693
694
695
696
697
  /*
   * 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...
698
  static const struct dentry_operations pipefs_dentry_operations = {
c23fbb6bc   Eric Dumazet   VFS: delay the de...
699
  	.d_dname	= pipefs_dname,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
700
701
702
703
  };
  
  static struct inode * get_pipe_inode(void)
  {
a209dfc7b   Eric Dumazet   vfs: dont chain p...
704
  	struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
705
  	struct pipe_inode_info *pipe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
708
  
  	if (!inode)
  		goto fail_inode;
85fe4025c   Christoph Hellwig   fs: do not assign...
709
  	inode->i_ino = get_next_ino();
7bee130e2   Al Viro   get rid of alloc_...
710
  	pipe = alloc_pipe_info();
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
711
  	if (!pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
  		goto fail_iput;
3a326a2ce   Ingo Molnar   [PATCH] introduce...
713

ba5bb1473   Al Viro   pipe: take alloca...
714
715
  	inode->i_pipe = pipe;
  	pipe->files = 2;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
716
  	pipe->readers = pipe->writers = 1;
599a0ac14   Al Viro   pipe: fold file_o...
717
  	inode->i_fop = &pipefifo_fops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
719
720
721
722
723
724
725
726
  
  	/*
  	 * 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...
727
728
  	inode->i_uid = current_fsuid();
  	inode->i_gid = current_fsgid();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
730

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
732
733
734
  	return inode;
  
  fail_iput:
  	iput(inode);
341b446bc   Ingo Molnar   [PATCH] another r...
735

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
  fail_inode:
  	return NULL;
  }
e4fad8e5d   Al Viro   consolidate pipe ...
739
  int create_pipe_files(struct file **res, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
  {
d6cbd281d   Andi Kleen   [PATCH] Some clea...
741
  	int err;
e4fad8e5d   Al Viro   consolidate pipe ...
742
  	struct inode *inode = get_pipe_inode();
d6cbd281d   Andi Kleen   [PATCH] Some clea...
743
  	struct file *f;
2c48b9c45   Al Viro   switch alloc_file...
744
  	struct path path;
e4fad8e5d   Al Viro   consolidate pipe ...
745
  	static struct qstr name = { .name = "" };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
  	if (!inode)
e4fad8e5d   Al Viro   consolidate pipe ...
748
  		return -ENFILE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749

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

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

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

599a0ac14   Al Viro   pipe: fold file_o...
766
  	res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
39b652527   Anatol Pomozov   fs: Preserve erro...
767
  	if (IS_ERR(res[0]))
e4fad8e5d   Al Viro   consolidate pipe ...
768
769
770
  		goto err_file;
  
  	path_get(&path);
de32ec4cf   Al Viro   pipe: set file->p...
771
  	res[0]->private_data = inode->i_pipe;
e4fad8e5d   Al Viro   consolidate pipe ...
772
773
774
  	res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  	res[1] = f;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775

e4fad8e5d   Al Viro   consolidate pipe ...
776
777
778
  err_file:
  	put_filp(f);
  err_dentry:
4b8a8f1e4   Al Viro   get rid of the la...
779
  	free_pipe_info(inode->i_pipe);
2c48b9c45   Al Viro   switch alloc_file...
780
  	path_put(&path);
e4fad8e5d   Al Viro   consolidate pipe ...
781
  	return err;
ed1524371   Al Viro   [PATCH] double-fr...
782

e4fad8e5d   Al Viro   consolidate pipe ...
783
  err_inode:
4b8a8f1e4   Al Viro   get rid of the la...
784
  	free_pipe_info(inode->i_pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
  	iput(inode);
e4fad8e5d   Al Viro   consolidate pipe ...
786
  	return err;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
787
  }
5b249b1b0   Al Viro   pipe(2) - race-fr...
788
  static int __do_pipe_flags(int *fd, struct file **files, int flags)
d6cbd281d   Andi Kleen   [PATCH] Some clea...
789
  {
d6cbd281d   Andi Kleen   [PATCH] Some clea...
790
791
  	int error;
  	int fdw, fdr;
9883035ae   Linus Torvalds   pipes: add a "pac...
792
  	if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
ed8cae8ba   Ulrich Drepper   flag parameters: ...
793
  		return -EINVAL;
e4fad8e5d   Al Viro   consolidate pipe ...
794
795
796
  	error = create_pipe_files(files, flags);
  	if (error)
  		return error;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
797

ed8cae8ba   Ulrich Drepper   flag parameters: ...
798
  	error = get_unused_fd_flags(flags);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
799
800
801
  	if (error < 0)
  		goto err_read_pipe;
  	fdr = error;
ed8cae8ba   Ulrich Drepper   flag parameters: ...
802
  	error = get_unused_fd_flags(flags);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
803
804
805
  	if (error < 0)
  		goto err_fdr;
  	fdw = error;
157cf649a   Al Viro   sanitize audit_fd...
806
  	audit_fd_pair(fdr, fdw);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
807
808
  	fd[0] = fdr;
  	fd[1] = fdw;
d6cbd281d   Andi Kleen   [PATCH] Some clea...
809
810
811
812
813
  	return 0;
  
   err_fdr:
  	put_unused_fd(fdr);
   err_read_pipe:
e4fad8e5d   Al Viro   consolidate pipe ...
814
815
  	fput(files[0]);
  	fput(files[1]);
d6cbd281d   Andi Kleen   [PATCH] Some clea...
816
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
817
  }
5b249b1b0   Al Viro   pipe(2) - race-fr...
818
819
820
821
822
823
824
825
826
827
  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
828
  /*
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
829
830
831
   * 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...
832
  SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
833
  {
5b249b1b0   Al Viro   pipe(2) - race-fr...
834
  	struct file *files[2];
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
835
836
  	int fd[2];
  	int error;
5b249b1b0   Al Viro   pipe(2) - race-fr...
837
  	error = __do_pipe_flags(fd, files, flags);
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
838
  	if (!error) {
5b249b1b0   Al Viro   pipe(2) - race-fr...
839
840
841
842
843
  		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...
844
  			error = -EFAULT;
5b249b1b0   Al Viro   pipe(2) - race-fr...
845
846
847
  		} else {
  			fd_install(fd[0], files[0]);
  			fd_install(fd[1], files[1]);
ba719baea   Ulrich Drepper   sys_pipe(): fix f...
848
  		}
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
849
850
851
  	}
  	return error;
  }
2b6642199   Heiko Carstens   [CVE-2009-0029] S...
852
  SYSCALL_DEFINE1(pipe, int __user *, fildes)
ed8cae8ba   Ulrich Drepper   flag parameters: ...
853
854
855
  {
  	return sys_pipe2(fildes, 0);
  }
fc7478a2b   Al Viro   pipe: switch wait...
856
  static int wait_for_partner(struct pipe_inode_info *pipe, unsigned int *cnt)
f776c7388   Al Viro   fold fifo.c into ...
857
858
859
860
  {
  	int cur = *cnt;	
  
  	while (cur == *cnt) {
fc7478a2b   Al Viro   pipe: switch wait...
861
  		pipe_wait(pipe);
f776c7388   Al Viro   fold fifo.c into ...
862
863
864
865
866
  		if (signal_pending(current))
  			break;
  	}
  	return cur == *cnt ? -ERESTARTSYS : 0;
  }
fc7478a2b   Al Viro   pipe: switch wait...
867
  static void wake_up_partner(struct pipe_inode_info *pipe)
f776c7388   Al Viro   fold fifo.c into ...
868
  {
fc7478a2b   Al Viro   pipe: switch wait...
869
  	wake_up_interruptible(&pipe->wait);
f776c7388   Al Viro   fold fifo.c into ...
870
871
872
873
874
  }
  
  static int fifo_open(struct inode *inode, struct file *filp)
  {
  	struct pipe_inode_info *pipe;
599a0ac14   Al Viro   pipe: fold file_o...
875
  	bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
f776c7388   Al Viro   fold fifo.c into ...
876
  	int ret;
ba5bb1473   Al Viro   pipe: take alloca...
877
878
879
880
881
882
883
884
885
  	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_...
886
  		pipe = alloc_pipe_info();
f776c7388   Al Viro   fold fifo.c into ...
887
  		if (!pipe)
ba5bb1473   Al Viro   pipe: take alloca...
888
889
890
891
892
893
  			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...
894
  			free_pipe_info(pipe);
ba5bb1473   Al Viro   pipe: take alloca...
895
896
897
898
899
  			pipe = inode->i_pipe;
  		} else {
  			inode->i_pipe = pipe;
  			spin_unlock(&inode->i_lock);
  		}
f776c7388   Al Viro   fold fifo.c into ...
900
  	}
de32ec4cf   Al Viro   pipe: set file->p...
901
  	filp->private_data = pipe;
ba5bb1473   Al Viro   pipe: take alloca...
902
  	/* OK, we have a pipe and it's pinned down */
ebec73f47   Al Viro   introduce variant...
903
  	__pipe_lock(pipe);
f776c7388   Al Viro   fold fifo.c into ...
904
905
906
907
908
909
910
911
912
913
914
  
  	/* 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 ...
915
916
  		pipe->r_counter++;
  		if (pipe->readers++ == 0)
fc7478a2b   Al Viro   pipe: switch wait...
917
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
918

599a0ac14   Al Viro   pipe: fold file_o...
919
  		if (!is_pipe && !pipe->writers) {
f776c7388   Al Viro   fold fifo.c into ...
920
921
922
923
924
  			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...
925
  				if (wait_for_partner(pipe, &pipe->w_counter))
f776c7388   Al Viro   fold fifo.c into ...
926
927
928
929
930
931
932
933
934
935
936
937
  					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...
938
  		if (!is_pipe && (filp->f_flags & O_NONBLOCK) && !pipe->readers)
f776c7388   Al Viro   fold fifo.c into ...
939
  			goto err;
f776c7388   Al Viro   fold fifo.c into ...
940
941
  		pipe->w_counter++;
  		if (!pipe->writers++)
fc7478a2b   Al Viro   pipe: switch wait...
942
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
943

599a0ac14   Al Viro   pipe: fold file_o...
944
  		if (!is_pipe && !pipe->readers) {
fc7478a2b   Al Viro   pipe: switch wait...
945
  			if (wait_for_partner(pipe, &pipe->r_counter))
f776c7388   Al Viro   fold fifo.c into ...
946
947
948
949
950
951
952
953
954
955
956
  				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 ...
957
958
959
960
961
962
  
  		pipe->readers++;
  		pipe->writers++;
  		pipe->r_counter++;
  		pipe->w_counter++;
  		if (pipe->readers == 1 || pipe->writers == 1)
fc7478a2b   Al Viro   pipe: switch wait...
963
  			wake_up_partner(pipe);
f776c7388   Al Viro   fold fifo.c into ...
964
965
966
967
968
969
970
971
  		break;
  
  	default:
  		ret = -EINVAL;
  		goto err;
  	}
  
  	/* Ok! */
ebec73f47   Al Viro   introduce variant...
972
  	__pipe_unlock(pipe);
f776c7388   Al Viro   fold fifo.c into ...
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  	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...
988
  	__pipe_unlock(pipe);
b0d8d2292   Linus Torvalds   vfs: fix subtle u...
989
990
  
  	put_pipe_info(inode, pipe);
f776c7388   Al Viro   fold fifo.c into ...
991
992
  	return ret;
  }
599a0ac14   Al Viro   pipe: fold file_o...
993
994
995
996
997
998
999
1000
1001
1002
1003
  const struct file_operations pipefifo_fops = {
  	.open		= fifo_open,
  	.llseek		= no_llseek,
  	.read		= do_sync_read,
  	.aio_read	= pipe_read,
  	.write		= do_sync_write,
  	.aio_write	= pipe_write,
  	.poll		= pipe_poll,
  	.unlocked_ioctl	= pipe_ioctl,
  	.release	= pipe_release,
  	.fasync		= pipe_fasync,
f776c7388   Al Viro   fold fifo.c into ...
1004
  };
d35c7b0e5   Ulrich Drepper   unified (weak) sy...
1005
  /*
35f3d14db   Jens Axboe   pipe: add support...
1006
1007
1008
   * 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...
1009
  static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
35f3d14db   Jens Axboe   pipe: add support...
1010
1011
1012
1013
  {
  	struct pipe_buffer *bufs;
  
  	/*
35f3d14db   Jens Axboe   pipe: add support...
1014
1015
1016
1017
1018
  	 * 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...
1019
  	if (nr_pages < pipe->nrbufs)
35f3d14db   Jens Axboe   pipe: add support...
1020
  		return -EBUSY;
2ccd4f4d4   Sasha Levin   pipe: fail cleanl...
1021
  	bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
35f3d14db   Jens Axboe   pipe: add support...
1022
1023
1024
1025
1026
1027
1028
1029
  	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...
1030
1031
  		unsigned int tail;
  		unsigned int head;
35f3d14db   Jens Axboe   pipe: add support...
1032

1d862f412   Miklos Szeredi   pipe: fix pipe bu...
1033
1034
1035
1036
1037
1038
1039
  		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...
1040
1041
1042
  		if (head)
  			memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
  		if (tail)
1d862f412   Miklos Szeredi   pipe: fix pipe bu...
1043
  			memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
35f3d14db   Jens Axboe   pipe: add support...
1044
1045
1046
1047
1048
  	}
  
  	pipe->curbuf = 0;
  	kfree(pipe->bufs);
  	pipe->bufs = bufs;
b9598db34   Jens Axboe   pipe: make F_{GET...
1049
1050
  	pipe->buffers = nr_pages;
  	return nr_pages * PAGE_SIZE;
35f3d14db   Jens Axboe   pipe: add support...
1051
  }
ff9da691c   Jens Axboe   pipe: change /pro...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
  /*
   * 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...
1080
1081
1082
1083
1084
1085
1086
  /*
   * 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...
1087
  	return file->f_op == &pipefifo_fops ? file->private_data : NULL;
720836465   Linus Torvalds   Un-inline get_pip...
1088
  }
35f3d14db   Jens Axboe   pipe: add support...
1089
1090
1091
1092
  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_...
1093
  	pipe = get_pipe_info(file);
35f3d14db   Jens Axboe   pipe: add support...
1094
1095
  	if (!pipe)
  		return -EBADF;
ebec73f47   Al Viro   introduce variant...
1096
  	__pipe_lock(pipe);
35f3d14db   Jens Axboe   pipe: add support...
1097
1098
  
  	switch (cmd) {
b9598db34   Jens Axboe   pipe: make F_{GET...
1099
  	case F_SETPIPE_SZ: {
ff9da691c   Jens Axboe   pipe: change /pro...
1100
  		unsigned int size, nr_pages;
b9598db34   Jens Axboe   pipe: make F_{GET...
1101

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

6db40cf04   Miklos Szeredi   pipe: fix check i...
1105
1106
1107
  		ret = -EINVAL;
  		if (!nr_pages)
  			goto out;
ff9da691c   Jens Axboe   pipe: change /pro...
1108
  		if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
b4ca76157   Jens Axboe   Merge branch 'mas...
1109
  			ret = -EPERM;
cc967be54   Julia Lawall   fs: Add missing m...
1110
  			goto out;
cc967be54   Julia Lawall   fs: Add missing m...
1111
  		}
ff9da691c   Jens Axboe   pipe: change /pro...
1112
  		ret = pipe_set_size(pipe, nr_pages);
35f3d14db   Jens Axboe   pipe: add support...
1113
  		break;
b9598db34   Jens Axboe   pipe: make F_{GET...
1114
  		}
35f3d14db   Jens Axboe   pipe: add support...
1115
  	case F_GETPIPE_SZ:
b9598db34   Jens Axboe   pipe: make F_{GET...
1116
  		ret = pipe->buffers * PAGE_SIZE;
35f3d14db   Jens Axboe   pipe: add support...
1117
1118
1119
1120
1121
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
cc967be54   Julia Lawall   fs: Add missing m...
1122
  out:
ebec73f47   Al Viro   introduce variant...
1123
  	__pipe_unlock(pipe);
35f3d14db   Jens Axboe   pipe: add support...
1124
1125
  	return ret;
  }
ff0c7d15f   Nick Piggin   fs: avoid inode R...
1126
1127
  static const struct super_operations pipefs_ops = {
  	.destroy_inode = free_inode_nonrcu,
d70ef97ba   Pavel Emelyanov   fs/pipe.c: add ->...
1128
  	.statfs = simple_statfs,
ff0c7d15f   Nick Piggin   fs: avoid inode R...
1129
  };
35f3d14db   Jens Axboe   pipe: add support...
1130
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
1133
1134
1135
   * 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...
1136
1137
  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
1138
  {
c74a1cbb3   Al Viro   pass default dent...
1139
1140
  	return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
  			&pipefs_dentry_operations, PIPEFS_MAGIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1141
1142
1143
1144
  }
  
  static struct file_system_type pipe_fs_type = {
  	.name		= "pipefs",
51139adac   Al Viro   convert get_sb_ps...
1145
  	.mount		= pipefs_mount,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
1147
1148
1149
1150
1151
  	.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...
1152

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
1154
1155
1156
1157
1158
1159
1160
1161
  	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
1162
  fs_initcall(init_pipe_fs);