4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 #include <linux/log2.h>
15 #include <linux/mount.h>
16 #include <linux/magic.h>
17 #include <linux/pipe_fs_i.h>
18 #include <linux/uio.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/audit.h>
22 #include <linux/syscalls.h>
23 #include <linux/fcntl.h>
25 #include <asm/uaccess.h>
26 #include <asm/ioctls.h>
31 * The max size that a non-root user is allowed to grow the pipe. Can
32 * be set by root in /proc/sys/fs/pipe-max-size
34 unsigned int pipe_max_size
= 1048576;
37 * Minimum pipe size, as required by POSIX
39 unsigned int pipe_min_size
= PAGE_SIZE
;
42 * We use a start+len construction, which provides full use of the
44 * -- Florian Coosmann (FGC)
46 * Reads with count = 0 should always return 0.
47 * -- Julian Bradfield 1999-06-07.
49 * FIFOs and Pipes now generate SIGIO for both readers and writers.
50 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
52 * pipe_read & write cleanup
53 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
56 static void pipe_lock_nested(struct pipe_inode_info
*pipe
, int subclass
)
59 mutex_lock_nested(&pipe
->mutex
, subclass
);
62 void pipe_lock(struct pipe_inode_info
*pipe
)
65 * pipe_lock() nests non-pipe inode locks (for writing to a file)
67 pipe_lock_nested(pipe
, I_MUTEX_PARENT
);
69 EXPORT_SYMBOL(pipe_lock
);
71 void pipe_unlock(struct pipe_inode_info
*pipe
)
74 mutex_unlock(&pipe
->mutex
);
76 EXPORT_SYMBOL(pipe_unlock
);
78 void pipe_double_lock(struct pipe_inode_info
*pipe1
,
79 struct pipe_inode_info
*pipe2
)
81 BUG_ON(pipe1
== pipe2
);
84 pipe_lock_nested(pipe1
, I_MUTEX_PARENT
);
85 pipe_lock_nested(pipe2
, I_MUTEX_CHILD
);
87 pipe_lock_nested(pipe2
, I_MUTEX_PARENT
);
88 pipe_lock_nested(pipe1
, I_MUTEX_CHILD
);
92 /* Drop the inode semaphore and wait for a pipe event, atomically */
93 void pipe_wait(struct pipe_inode_info
*pipe
)
98 * Pipes are system-local resources, so sleeping on them
99 * is considered a noninteractive wait:
101 prepare_to_wait(&pipe
->wait
, &wait
, TASK_INTERRUPTIBLE
);
104 finish_wait(&pipe
->wait
, &wait
);
109 pipe_iov_copy_from_user(void *to
, struct iovec
*iov
, unsigned long len
,
115 while (!iov
->iov_len
)
117 copy
= min_t(unsigned long, len
, iov
->iov_len
);
120 if (__copy_from_user_inatomic(to
, iov
->iov_base
, copy
))
123 if (copy_from_user(to
, iov
->iov_base
, copy
))
128 iov
->iov_base
+= copy
;
129 iov
->iov_len
-= copy
;
135 pipe_iov_copy_to_user(struct iovec
*iov
, const void *from
, unsigned long len
,
141 while (!iov
->iov_len
)
143 copy
= min_t(unsigned long, len
, iov
->iov_len
);
146 if (__copy_to_user_inatomic(iov
->iov_base
, from
, copy
))
149 if (copy_to_user(iov
->iov_base
, from
, copy
))
154 iov
->iov_base
+= copy
;
155 iov
->iov_len
-= copy
;
161 * Attempt to pre-fault in the user memory, so we can use atomic copies.
162 * Returns the number of bytes not faulted in.
164 static int iov_fault_in_pages_write(struct iovec
*iov
, unsigned long len
)
166 while (!iov
->iov_len
)
170 unsigned long this_len
;
172 this_len
= min_t(unsigned long, len
, iov
->iov_len
);
173 if (fault_in_pages_writeable(iov
->iov_base
, this_len
))
184 * Pre-fault in the user memory, so we can use atomic copies.
186 static void iov_fault_in_pages_read(struct iovec
*iov
, unsigned long len
)
188 while (!iov
->iov_len
)
192 unsigned long this_len
;
194 this_len
= min_t(unsigned long, len
, iov
->iov_len
);
195 fault_in_pages_readable(iov
->iov_base
, this_len
);
201 static void anon_pipe_buf_release(struct pipe_inode_info
*pipe
,
202 struct pipe_buffer
*buf
)
204 struct page
*page
= buf
->page
;
207 * If nobody else uses this page, and we don't already have a
208 * temporary page, let's keep track of it as a one-deep
209 * allocation cache. (Otherwise just release our reference to it)
211 if (page_count(page
) == 1 && !pipe
->tmp_page
)
212 pipe
->tmp_page
= page
;
214 page_cache_release(page
);
218 * generic_pipe_buf_map - virtually map a pipe buffer
219 * @pipe: the pipe that the buffer belongs to
220 * @buf: the buffer that should be mapped
221 * @atomic: whether to use an atomic map
224 * This function returns a kernel virtual address mapping for the
225 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
226 * and the caller has to be careful not to fault before calling
227 * the unmap function.
229 * Note that this function calls kmap_atomic() if @atomic != 0.
231 void *generic_pipe_buf_map(struct pipe_inode_info
*pipe
,
232 struct pipe_buffer
*buf
, int atomic
)
235 buf
->flags
|= PIPE_BUF_FLAG_ATOMIC
;
236 return kmap_atomic(buf
->page
);
239 return kmap(buf
->page
);
241 EXPORT_SYMBOL(generic_pipe_buf_map
);
244 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
245 * @pipe: the pipe that the buffer belongs to
246 * @buf: the buffer that should be unmapped
247 * @map_data: the data that the mapping function returned
250 * This function undoes the mapping that ->map() provided.
252 void generic_pipe_buf_unmap(struct pipe_inode_info
*pipe
,
253 struct pipe_buffer
*buf
, void *map_data
)
255 if (buf
->flags
& PIPE_BUF_FLAG_ATOMIC
) {
256 buf
->flags
&= ~PIPE_BUF_FLAG_ATOMIC
;
257 kunmap_atomic(map_data
);
261 EXPORT_SYMBOL(generic_pipe_buf_unmap
);
264 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
265 * @pipe: the pipe that the buffer belongs to
266 * @buf: the buffer to attempt to steal
269 * This function attempts to steal the &struct page attached to
270 * @buf. If successful, this function returns 0 and returns with
271 * the page locked. The caller may then reuse the page for whatever
272 * he wishes; the typical use is insertion into a different file
275 int generic_pipe_buf_steal(struct pipe_inode_info
*pipe
,
276 struct pipe_buffer
*buf
)
278 struct page
*page
= buf
->page
;
281 * A reference of one is golden, that means that the owner of this
282 * page is the only one holding a reference to it. lock the page
285 if (page_count(page
) == 1) {
292 EXPORT_SYMBOL(generic_pipe_buf_steal
);
295 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
296 * @pipe: the pipe that the buffer belongs to
297 * @buf: the buffer to get a reference to
300 * This function grabs an extra reference to @buf. It's used in
301 * in the tee() system call, when we duplicate the buffers in one
304 void generic_pipe_buf_get(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
)
306 page_cache_get(buf
->page
);
308 EXPORT_SYMBOL(generic_pipe_buf_get
);
311 * generic_pipe_buf_confirm - verify contents of the pipe buffer
312 * @info: the pipe that the buffer belongs to
313 * @buf: the buffer to confirm
316 * This function does nothing, because the generic pipe code uses
317 * pages that are always good when inserted into the pipe.
319 int generic_pipe_buf_confirm(struct pipe_inode_info
*info
,
320 struct pipe_buffer
*buf
)
324 EXPORT_SYMBOL(generic_pipe_buf_confirm
);
327 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
328 * @pipe: the pipe that the buffer belongs to
329 * @buf: the buffer to put a reference to
332 * This function releases a reference to @buf.
334 void generic_pipe_buf_release(struct pipe_inode_info
*pipe
,
335 struct pipe_buffer
*buf
)
337 page_cache_release(buf
->page
);
339 EXPORT_SYMBOL(generic_pipe_buf_release
);
341 static const struct pipe_buf_operations anon_pipe_buf_ops
= {
343 .map
= generic_pipe_buf_map
,
344 .unmap
= generic_pipe_buf_unmap
,
345 .confirm
= generic_pipe_buf_confirm
,
346 .release
= anon_pipe_buf_release
,
347 .steal
= generic_pipe_buf_steal
,
348 .get
= generic_pipe_buf_get
,
351 static const struct pipe_buf_operations packet_pipe_buf_ops
= {
353 .map
= generic_pipe_buf_map
,
354 .unmap
= generic_pipe_buf_unmap
,
355 .confirm
= generic_pipe_buf_confirm
,
356 .release
= anon_pipe_buf_release
,
357 .steal
= generic_pipe_buf_steal
,
358 .get
= generic_pipe_buf_get
,
362 pipe_read(struct kiocb
*iocb
, const struct iovec
*_iov
,
363 unsigned long nr_segs
, loff_t pos
)
365 struct file
*filp
= iocb
->ki_filp
;
366 struct pipe_inode_info
*pipe
= filp
->private_data
;
369 struct iovec
*iov
= (struct iovec
*)_iov
;
372 total_len
= iov_length(iov
, nr_segs
);
373 /* Null read succeeds. */
374 if (unlikely(total_len
== 0))
381 int bufs
= pipe
->nrbufs
;
383 int curbuf
= pipe
->curbuf
;
384 struct pipe_buffer
*buf
= pipe
->bufs
+ curbuf
;
385 const struct pipe_buf_operations
*ops
= buf
->ops
;
387 size_t chars
= buf
->len
;
390 if (chars
> total_len
)
393 error
= ops
->confirm(pipe
, buf
);
400 atomic
= !iov_fault_in_pages_write(iov
, chars
);
402 addr
= ops
->map(pipe
, buf
, atomic
);
403 error
= pipe_iov_copy_to_user(iov
, addr
+ buf
->offset
, chars
, atomic
);
404 ops
->unmap(pipe
, buf
, addr
);
405 if (unlikely(error
)) {
407 * Just retry with the slow path if we failed.
418 buf
->offset
+= chars
;
421 /* Was it a packet buffer? Clean up and exit */
422 if (buf
->flags
& PIPE_BUF_FLAG_PACKET
) {
429 ops
->release(pipe
, buf
);
430 curbuf
= (curbuf
+ 1) & (pipe
->buffers
- 1);
431 pipe
->curbuf
= curbuf
;
432 pipe
->nrbufs
= --bufs
;
437 break; /* common path: read succeeded */
439 if (bufs
) /* More to do? */
443 if (!pipe
->waiting_writers
) {
444 /* syscall merging: Usually we must not sleep
445 * if O_NONBLOCK is set, or if we got some data.
446 * But if a writer sleeps in kernel space, then
447 * we can wait for that data without violating POSIX.
451 if (filp
->f_flags
& O_NONBLOCK
) {
456 if (signal_pending(current
)) {
462 wake_up_interruptible_sync_poll(&pipe
->wait
, POLLOUT
| POLLWRNORM
);
463 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
469 /* Signal writers asynchronously that there is more room. */
471 wake_up_interruptible_sync_poll(&pipe
->wait
, POLLOUT
| POLLWRNORM
);
472 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
479 static inline int is_packetized(struct file
*file
)
481 return (file
->f_flags
& O_DIRECT
) != 0;
485 pipe_write(struct kiocb
*iocb
, const struct iovec
*_iov
,
486 unsigned long nr_segs
, loff_t ppos
)
488 struct file
*filp
= iocb
->ki_filp
;
489 struct pipe_inode_info
*pipe
= filp
->private_data
;
492 struct iovec
*iov
= (struct iovec
*)_iov
;
496 total_len
= iov_length(iov
, nr_segs
);
497 /* Null write succeeds. */
498 if (unlikely(total_len
== 0))
505 if (!pipe
->readers
) {
506 send_sig(SIGPIPE
, current
, 0);
511 /* We try to merge small writes */
512 chars
= total_len
& (PAGE_SIZE
-1); /* size of the last buffer */
513 if (pipe
->nrbufs
&& chars
!= 0) {
514 int lastbuf
= (pipe
->curbuf
+ pipe
->nrbufs
- 1) &
516 struct pipe_buffer
*buf
= pipe
->bufs
+ lastbuf
;
517 const struct pipe_buf_operations
*ops
= buf
->ops
;
518 int offset
= buf
->offset
+ buf
->len
;
520 if (ops
->can_merge
&& offset
+ chars
<= PAGE_SIZE
) {
521 int error
, atomic
= 1;
524 error
= ops
->confirm(pipe
, buf
);
528 iov_fault_in_pages_read(iov
, chars
);
530 addr
= ops
->map(pipe
, buf
, atomic
);
531 error
= pipe_iov_copy_from_user(offset
+ addr
, iov
,
533 ops
->unmap(pipe
, buf
, addr
);
554 if (!pipe
->readers
) {
555 send_sig(SIGPIPE
, current
, 0);
561 if (bufs
< pipe
->buffers
) {
562 int newbuf
= (pipe
->curbuf
+ bufs
) & (pipe
->buffers
-1);
563 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
564 struct page
*page
= pipe
->tmp_page
;
566 int error
, atomic
= 1;
569 page
= alloc_page(GFP_HIGHUSER
);
570 if (unlikely(!page
)) {
571 ret
= ret
? : -ENOMEM
;
574 pipe
->tmp_page
= page
;
576 /* Always wake up, even if the copy fails. Otherwise
577 * we lock up (O_NONBLOCK-)readers that sleep due to
579 * FIXME! Is this really true?
583 if (chars
> total_len
)
586 iov_fault_in_pages_read(iov
, chars
);
589 src
= kmap_atomic(page
);
593 error
= pipe_iov_copy_from_user(src
, iov
, chars
,
600 if (unlikely(error
)) {
611 /* Insert it into the buffer array */
613 buf
->ops
= &anon_pipe_buf_ops
;
617 if (is_packetized(filp
)) {
618 buf
->ops
= &packet_pipe_buf_ops
;
619 buf
->flags
= PIPE_BUF_FLAG_PACKET
;
621 pipe
->nrbufs
= ++bufs
;
622 pipe
->tmp_page
= NULL
;
628 if (bufs
< pipe
->buffers
)
630 if (filp
->f_flags
& O_NONBLOCK
) {
635 if (signal_pending(current
)) {
641 wake_up_interruptible_sync_poll(&pipe
->wait
, POLLIN
| POLLRDNORM
);
642 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
645 pipe
->waiting_writers
++;
647 pipe
->waiting_writers
--;
652 wake_up_interruptible_sync_poll(&pipe
->wait
, POLLIN
| POLLRDNORM
);
653 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
656 int err
= file_update_time(filp
);
663 static long pipe_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
665 struct pipe_inode_info
*pipe
= filp
->private_data
;
666 int count
, buf
, nrbufs
;
673 nrbufs
= pipe
->nrbufs
;
674 while (--nrbufs
>= 0) {
675 count
+= pipe
->bufs
[buf
].len
;
676 buf
= (buf
+1) & (pipe
->buffers
- 1);
680 return put_user(count
, (int __user
*)arg
);
686 /* No kernel lock held - fine */
688 pipe_poll(struct file
*filp
, poll_table
*wait
)
691 struct pipe_inode_info
*pipe
= filp
->private_data
;
694 poll_wait(filp
, &pipe
->wait
, wait
);
696 /* Reading only -- no need for acquiring the semaphore. */
697 nrbufs
= pipe
->nrbufs
;
699 if (filp
->f_mode
& FMODE_READ
) {
700 mask
= (nrbufs
> 0) ? POLLIN
| POLLRDNORM
: 0;
701 if (!pipe
->writers
&& filp
->f_version
!= pipe
->w_counter
)
705 if (filp
->f_mode
& FMODE_WRITE
) {
706 mask
|= (nrbufs
< pipe
->buffers
) ? POLLOUT
| POLLWRNORM
: 0;
708 * Most Unices do not set POLLERR for FIFOs but on Linux they
709 * behave exactly like pipes for poll().
719 pipe_release(struct inode
*inode
, struct file
*file
)
721 struct pipe_inode_info
*pipe
= inode
->i_pipe
;
725 if (file
->f_mode
& FMODE_READ
)
727 if (file
->f_mode
& FMODE_WRITE
)
730 if (pipe
->readers
|| pipe
->writers
) {
731 wake_up_interruptible_sync_poll(&pipe
->wait
, POLLIN
| POLLOUT
| POLLRDNORM
| POLLWRNORM
| POLLERR
| POLLHUP
);
732 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
733 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
735 spin_lock(&inode
->i_lock
);
736 if (!--pipe
->files
) {
737 inode
->i_pipe
= NULL
;
740 spin_unlock(&inode
->i_lock
);
744 __free_pipe_info(pipe
);
750 pipe_fasync(int fd
, struct file
*filp
, int on
)
752 struct pipe_inode_info
*pipe
= filp
->private_data
;
756 if (filp
->f_mode
& FMODE_READ
)
757 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_readers
);
758 if ((filp
->f_mode
& FMODE_WRITE
) && retval
>= 0) {
759 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_writers
);
760 if (retval
< 0 && (filp
->f_mode
& FMODE_READ
))
761 /* this can happen only if on == T */
762 fasync_helper(-1, filp
, 0, &pipe
->fasync_readers
);
768 struct pipe_inode_info
* alloc_pipe_info(struct inode
*inode
)
770 struct pipe_inode_info
*pipe
;
772 pipe
= kzalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
774 pipe
->bufs
= kzalloc(sizeof(struct pipe_buffer
) * PIPE_DEF_BUFFERS
, GFP_KERNEL
);
776 init_waitqueue_head(&pipe
->wait
);
777 pipe
->r_counter
= pipe
->w_counter
= 1;
779 pipe
->buffers
= PIPE_DEF_BUFFERS
;
780 mutex_init(&pipe
->mutex
);
789 void __free_pipe_info(struct pipe_inode_info
*pipe
)
793 for (i
= 0; i
< pipe
->buffers
; i
++) {
794 struct pipe_buffer
*buf
= pipe
->bufs
+ i
;
796 buf
->ops
->release(pipe
, buf
);
799 __free_page(pipe
->tmp_page
);
804 void free_pipe_info(struct inode
*inode
)
806 __free_pipe_info(inode
->i_pipe
);
807 inode
->i_pipe
= NULL
;
810 static struct vfsmount
*pipe_mnt __read_mostly
;
813 * pipefs_dname() is called from d_path().
815 static char *pipefs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
817 return dynamic_dname(dentry
, buffer
, buflen
, "pipe:[%lu]",
818 dentry
->d_inode
->i_ino
);
821 static const struct dentry_operations pipefs_dentry_operations
= {
822 .d_dname
= pipefs_dname
,
825 static struct inode
* get_pipe_inode(void)
827 struct inode
*inode
= new_inode_pseudo(pipe_mnt
->mnt_sb
);
828 struct pipe_inode_info
*pipe
;
833 inode
->i_ino
= get_next_ino();
835 pipe
= alloc_pipe_info(inode
);
839 inode
->i_pipe
= pipe
;
841 pipe
->readers
= pipe
->writers
= 1;
842 inode
->i_fop
= &pipefifo_fops
;
845 * Mark the inode dirty from the very beginning,
846 * that way it will never be moved to the dirty
847 * list because "mark_inode_dirty()" will think
848 * that it already _is_ on the dirty list.
850 inode
->i_state
= I_DIRTY
;
851 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
852 inode
->i_uid
= current_fsuid();
853 inode
->i_gid
= current_fsgid();
854 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
865 int create_pipe_files(struct file
**res
, int flags
)
868 struct inode
*inode
= get_pipe_inode();
871 static struct qstr name
= { .name
= "" };
877 path
.dentry
= d_alloc_pseudo(pipe_mnt
->mnt_sb
, &name
);
880 path
.mnt
= mntget(pipe_mnt
);
882 d_instantiate(path
.dentry
, inode
);
885 f
= alloc_file(&path
, FMODE_WRITE
, &pipefifo_fops
);
889 f
->f_flags
= O_WRONLY
| (flags
& (O_NONBLOCK
| O_DIRECT
));
890 f
->private_data
= inode
->i_pipe
;
892 res
[0] = alloc_file(&path
, FMODE_READ
, &pipefifo_fops
);
897 res
[0]->private_data
= inode
->i_pipe
;
898 res
[0]->f_flags
= O_RDONLY
| (flags
& O_NONBLOCK
);
905 free_pipe_info(inode
);
910 free_pipe_info(inode
);
915 static int __do_pipe_flags(int *fd
, struct file
**files
, int flags
)
920 if (flags
& ~(O_CLOEXEC
| O_NONBLOCK
| O_DIRECT
))
923 error
= create_pipe_files(files
, flags
);
927 error
= get_unused_fd_flags(flags
);
932 error
= get_unused_fd_flags(flags
);
937 audit_fd_pair(fdr
, fdw
);
950 int do_pipe_flags(int *fd
, int flags
)
952 struct file
*files
[2];
953 int error
= __do_pipe_flags(fd
, files
, flags
);
955 fd_install(fd
[0], files
[0]);
956 fd_install(fd
[1], files
[1]);
962 * sys_pipe() is the normal C calling standard for creating
963 * a pipe. It's not the way Unix traditionally does this, though.
965 SYSCALL_DEFINE2(pipe2
, int __user
*, fildes
, int, flags
)
967 struct file
*files
[2];
971 error
= __do_pipe_flags(fd
, files
, flags
);
973 if (unlikely(copy_to_user(fildes
, fd
, sizeof(fd
)))) {
976 put_unused_fd(fd
[0]);
977 put_unused_fd(fd
[1]);
980 fd_install(fd
[0], files
[0]);
981 fd_install(fd
[1], files
[1]);
987 SYSCALL_DEFINE1(pipe
, int __user
*, fildes
)
989 return sys_pipe2(fildes
, 0);
992 static int wait_for_partner(struct pipe_inode_info
*pipe
, unsigned int *cnt
)
996 while (cur
== *cnt
) {
998 if (signal_pending(current
))
1001 return cur
== *cnt
? -ERESTARTSYS
: 0;
1004 static void wake_up_partner(struct pipe_inode_info
*pipe
)
1006 wake_up_interruptible(&pipe
->wait
);
1009 static int fifo_open(struct inode
*inode
, struct file
*filp
)
1011 struct pipe_inode_info
*pipe
;
1012 bool is_pipe
= inode
->i_sb
->s_magic
== PIPEFS_MAGIC
;
1016 filp
->f_version
= 0;
1018 spin_lock(&inode
->i_lock
);
1019 if (inode
->i_pipe
) {
1020 pipe
= inode
->i_pipe
;
1022 spin_unlock(&inode
->i_lock
);
1024 spin_unlock(&inode
->i_lock
);
1025 pipe
= alloc_pipe_info(inode
);
1029 spin_lock(&inode
->i_lock
);
1030 if (unlikely(inode
->i_pipe
)) {
1031 inode
->i_pipe
->files
++;
1032 spin_unlock(&inode
->i_lock
);
1033 __free_pipe_info(pipe
);
1034 pipe
= inode
->i_pipe
;
1036 inode
->i_pipe
= pipe
;
1037 spin_unlock(&inode
->i_lock
);
1040 filp
->private_data
= pipe
;
1041 /* OK, we have a pipe and it's pinned down */
1045 /* We can only do regular read/write on fifos */
1046 filp
->f_mode
&= (FMODE_READ
| FMODE_WRITE
);
1048 switch (filp
->f_mode
) {
1052 * POSIX.1 says that O_NONBLOCK means return with the FIFO
1053 * opened, even when there is no process writing the FIFO.
1056 if (pipe
->readers
++ == 0)
1057 wake_up_partner(pipe
);
1059 if (!is_pipe
&& !pipe
->writers
) {
1060 if ((filp
->f_flags
& O_NONBLOCK
)) {
1061 /* suppress POLLHUP until we have
1063 filp
->f_version
= pipe
->w_counter
;
1065 if (wait_for_partner(pipe
, &pipe
->w_counter
))
1074 * POSIX.1 says that O_NONBLOCK means return -1 with
1075 * errno=ENXIO when there is no process reading the FIFO.
1078 if (!is_pipe
&& (filp
->f_flags
& O_NONBLOCK
) && !pipe
->readers
)
1082 if (!pipe
->writers
++)
1083 wake_up_partner(pipe
);
1085 if (!is_pipe
&& !pipe
->readers
) {
1086 if (wait_for_partner(pipe
, &pipe
->r_counter
))
1091 case FMODE_READ
| FMODE_WRITE
:
1094 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
1095 * This implementation will NEVER block on a O_RDWR open, since
1096 * the process can at least talk to itself.
1103 if (pipe
->readers
== 1 || pipe
->writers
== 1)
1104 wake_up_partner(pipe
);
1117 if (!--pipe
->readers
)
1118 wake_up_interruptible(&pipe
->wait
);
1123 if (!--pipe
->writers
)
1124 wake_up_interruptible(&pipe
->wait
);
1129 spin_lock(&inode
->i_lock
);
1130 if (!--pipe
->files
) {
1131 inode
->i_pipe
= NULL
;
1134 spin_unlock(&inode
->i_lock
);
1137 __free_pipe_info(pipe
);
1141 const struct file_operations pipefifo_fops
= {
1143 .llseek
= no_llseek
,
1144 .read
= do_sync_read
,
1145 .aio_read
= pipe_read
,
1146 .write
= do_sync_write
,
1147 .aio_write
= pipe_write
,
1149 .unlocked_ioctl
= pipe_ioctl
,
1150 .release
= pipe_release
,
1151 .fasync
= pipe_fasync
,
1155 * Allocate a new array of pipe buffers and copy the info over. Returns the
1156 * pipe size if successful, or return -ERROR on error.
1158 static long pipe_set_size(struct pipe_inode_info
*pipe
, unsigned long nr_pages
)
1160 struct pipe_buffer
*bufs
;
1163 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1164 * expect a lot of shrink+grow operations, just free and allocate
1165 * again like we would do for growing. If the pipe currently
1166 * contains more buffers than arg, then return busy.
1168 if (nr_pages
< pipe
->nrbufs
)
1171 bufs
= kcalloc(nr_pages
, sizeof(*bufs
), GFP_KERNEL
| __GFP_NOWARN
);
1172 if (unlikely(!bufs
))
1176 * The pipe array wraps around, so just start the new one at zero
1177 * and adjust the indexes.
1183 tail
= pipe
->curbuf
+ pipe
->nrbufs
;
1184 if (tail
< pipe
->buffers
)
1187 tail
&= (pipe
->buffers
- 1);
1189 head
= pipe
->nrbufs
- tail
;
1191 memcpy(bufs
, pipe
->bufs
+ pipe
->curbuf
, head
* sizeof(struct pipe_buffer
));
1193 memcpy(bufs
+ head
, pipe
->bufs
, tail
* sizeof(struct pipe_buffer
));
1199 pipe
->buffers
= nr_pages
;
1200 return nr_pages
* PAGE_SIZE
;
1204 * Currently we rely on the pipe array holding a power-of-2 number
1207 static inline unsigned int round_pipe_size(unsigned int size
)
1209 unsigned long nr_pages
;
1211 nr_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1212 return roundup_pow_of_two(nr_pages
) << PAGE_SHIFT
;
1216 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1217 * will return an error.
1219 int pipe_proc_fn(struct ctl_table
*table
, int write
, void __user
*buf
,
1220 size_t *lenp
, loff_t
*ppos
)
1224 ret
= proc_dointvec_minmax(table
, write
, buf
, lenp
, ppos
);
1225 if (ret
< 0 || !write
)
1228 pipe_max_size
= round_pipe_size(pipe_max_size
);
1233 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1234 * location, so checking ->i_pipe is not enough to verify that this is a
1237 struct pipe_inode_info
*get_pipe_info(struct file
*file
)
1239 return file
->f_op
== &pipefifo_fops
? file
->private_data
: NULL
;
1242 long pipe_fcntl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1244 struct pipe_inode_info
*pipe
;
1247 pipe
= get_pipe_info(file
);
1254 case F_SETPIPE_SZ
: {
1255 unsigned int size
, nr_pages
;
1257 size
= round_pipe_size(arg
);
1258 nr_pages
= size
>> PAGE_SHIFT
;
1264 if (!capable(CAP_SYS_RESOURCE
) && size
> pipe_max_size
) {
1268 ret
= pipe_set_size(pipe
, nr_pages
);
1272 ret
= pipe
->buffers
* PAGE_SIZE
;
1284 static const struct super_operations pipefs_ops
= {
1285 .destroy_inode
= free_inode_nonrcu
,
1286 .statfs
= simple_statfs
,
1290 * pipefs should _never_ be mounted by userland - too much of security hassle,
1291 * no real gain from having the whole whorehouse mounted. So we don't need
1292 * any operations on the root directory. However, we need a non-trivial
1293 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1295 static struct dentry
*pipefs_mount(struct file_system_type
*fs_type
,
1296 int flags
, const char *dev_name
, void *data
)
1298 return mount_pseudo(fs_type
, "pipe:", &pipefs_ops
,
1299 &pipefs_dentry_operations
, PIPEFS_MAGIC
);
1302 static struct file_system_type pipe_fs_type
= {
1304 .mount
= pipefs_mount
,
1305 .kill_sb
= kill_anon_super
,
1308 static int __init
init_pipe_fs(void)
1310 int err
= register_filesystem(&pipe_fs_type
);
1313 pipe_mnt
= kern_mount(&pipe_fs_type
);
1314 if (IS_ERR(pipe_mnt
)) {
1315 err
= PTR_ERR(pipe_mnt
);
1316 unregister_filesystem(&pipe_fs_type
);
1322 fs_initcall(init_pipe_fs
);