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/mount.h>
15 #include <linux/pipe_fs_i.h>
16 #include <linux/uio.h>
17 #include <linux/highmem.h>
19 #include <asm/uaccess.h>
20 #include <asm/ioctls.h>
23 * We use a start+len construction, which provides full use of the
25 * -- Florian Coosmann (FGC)
27 * Reads with count = 0 should always return 0.
28 * -- Julian Bradfield 1999-06-07.
30 * FIFOs and Pipes now generate SIGIO for both readers and writers.
31 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
33 * pipe_read & write cleanup
34 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
37 /* Drop the inode semaphore and wait for a pipe event, atomically */
38 void pipe_wait(struct inode
* inode
)
43 * Pipes are system-local resources, so sleeping on them
44 * is considered a noninteractive wait:
46 prepare_to_wait(PIPE_WAIT(*inode
), &wait
, TASK_INTERRUPTIBLE
|TASK_NONINTERACTIVE
);
47 mutex_unlock(PIPE_MUTEX(*inode
));
49 finish_wait(PIPE_WAIT(*inode
), &wait
);
50 mutex_lock(PIPE_MUTEX(*inode
));
54 pipe_iov_copy_from_user(void *to
, struct iovec
*iov
, unsigned long len
)
61 copy
= min_t(unsigned long, len
, iov
->iov_len
);
63 if (copy_from_user(to
, iov
->iov_base
, copy
))
67 iov
->iov_base
+= copy
;
74 pipe_iov_copy_to_user(struct iovec
*iov
, const void *from
, unsigned long len
)
81 copy
= min_t(unsigned long, len
, iov
->iov_len
);
83 if (copy_to_user(iov
->iov_base
, from
, copy
))
87 iov
->iov_base
+= copy
;
93 static void anon_pipe_buf_release(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
)
95 struct page
*page
= buf
->page
;
101 info
->tmp_page
= page
;
104 static void *anon_pipe_buf_map(struct file
*file
, struct pipe_inode_info
*info
, struct pipe_buffer
*buf
)
106 return kmap(buf
->page
);
109 static void anon_pipe_buf_unmap(struct pipe_inode_info
*info
, struct pipe_buffer
*buf
)
114 static struct pipe_buf_operations anon_pipe_buf_ops
= {
116 .map
= anon_pipe_buf_map
,
117 .unmap
= anon_pipe_buf_unmap
,
118 .release
= anon_pipe_buf_release
,
122 pipe_readv(struct file
*filp
, const struct iovec
*_iov
,
123 unsigned long nr_segs
, loff_t
*ppos
)
125 struct inode
*inode
= filp
->f_dentry
->d_inode
;
126 struct pipe_inode_info
*info
;
129 struct iovec
*iov
= (struct iovec
*)_iov
;
132 total_len
= iov_length(iov
, nr_segs
);
133 /* Null read succeeds. */
134 if (unlikely(total_len
== 0))
139 mutex_lock(PIPE_MUTEX(*inode
));
140 info
= inode
->i_pipe
;
142 int bufs
= info
->nrbufs
;
144 int curbuf
= info
->curbuf
;
145 struct pipe_buffer
*buf
= info
->bufs
+ curbuf
;
146 struct pipe_buf_operations
*ops
= buf
->ops
;
148 size_t chars
= buf
->len
;
151 if (chars
> total_len
)
154 addr
= ops
->map(filp
, info
, buf
);
155 error
= pipe_iov_copy_to_user(iov
, addr
+ buf
->offset
, chars
);
156 ops
->unmap(info
, buf
);
157 if (unlikely(error
)) {
158 if (!ret
) ret
= -EFAULT
;
162 buf
->offset
+= chars
;
166 ops
->release(info
, buf
);
167 curbuf
= (curbuf
+ 1) & (PIPE_BUFFERS
-1);
168 info
->curbuf
= curbuf
;
169 info
->nrbufs
= --bufs
;
174 break; /* common path: read succeeded */
176 if (bufs
) /* More to do? */
178 if (!PIPE_WRITERS(*inode
))
180 if (!PIPE_WAITING_WRITERS(*inode
)) {
181 /* syscall merging: Usually we must not sleep
182 * if O_NONBLOCK is set, or if we got some data.
183 * But if a writer sleeps in kernel space, then
184 * we can wait for that data without violating POSIX.
188 if (filp
->f_flags
& O_NONBLOCK
) {
193 if (signal_pending(current
)) {
194 if (!ret
) ret
= -ERESTARTSYS
;
198 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
199 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
203 mutex_unlock(PIPE_MUTEX(*inode
));
204 /* Signal writers asynchronously that there is more room. */
206 wake_up_interruptible(PIPE_WAIT(*inode
));
207 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
215 pipe_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
217 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
218 return pipe_readv(filp
, &iov
, 1, ppos
);
222 pipe_writev(struct file
*filp
, const struct iovec
*_iov
,
223 unsigned long nr_segs
, loff_t
*ppos
)
225 struct inode
*inode
= filp
->f_dentry
->d_inode
;
226 struct pipe_inode_info
*info
;
229 struct iovec
*iov
= (struct iovec
*)_iov
;
233 total_len
= iov_length(iov
, nr_segs
);
234 /* Null write succeeds. */
235 if (unlikely(total_len
== 0))
240 mutex_lock(PIPE_MUTEX(*inode
));
241 info
= inode
->i_pipe
;
243 if (!PIPE_READERS(*inode
)) {
244 send_sig(SIGPIPE
, current
, 0);
249 /* We try to merge small writes */
250 chars
= total_len
& (PAGE_SIZE
-1); /* size of the last buffer */
251 if (info
->nrbufs
&& chars
!= 0) {
252 int lastbuf
= (info
->curbuf
+ info
->nrbufs
- 1) & (PIPE_BUFFERS
-1);
253 struct pipe_buffer
*buf
= info
->bufs
+ lastbuf
;
254 struct pipe_buf_operations
*ops
= buf
->ops
;
255 int offset
= buf
->offset
+ buf
->len
;
256 if (ops
->can_merge
&& offset
+ chars
<= PAGE_SIZE
) {
257 void *addr
= ops
->map(filp
, info
, buf
);
258 int error
= pipe_iov_copy_from_user(offset
+ addr
, iov
, chars
);
259 ops
->unmap(info
, buf
);
274 if (!PIPE_READERS(*inode
)) {
275 send_sig(SIGPIPE
, current
, 0);
276 if (!ret
) ret
= -EPIPE
;
280 if (bufs
< PIPE_BUFFERS
) {
281 int newbuf
= (info
->curbuf
+ bufs
) & (PIPE_BUFFERS
-1);
282 struct pipe_buffer
*buf
= info
->bufs
+ newbuf
;
283 struct page
*page
= info
->tmp_page
;
287 page
= alloc_page(GFP_HIGHUSER
);
288 if (unlikely(!page
)) {
289 ret
= ret
? : -ENOMEM
;
292 info
->tmp_page
= page
;
294 /* Always wakeup, even if the copy fails. Otherwise
295 * we lock up (O_NONBLOCK-)readers that sleep due to
297 * FIXME! Is this really true?
301 if (chars
> total_len
)
304 error
= pipe_iov_copy_from_user(kmap(page
), iov
, chars
);
306 if (unlikely(error
)) {
307 if (!ret
) ret
= -EFAULT
;
312 /* Insert it into the buffer array */
314 buf
->ops
= &anon_pipe_buf_ops
;
317 info
->nrbufs
= ++bufs
;
318 info
->tmp_page
= NULL
;
324 if (bufs
< PIPE_BUFFERS
)
326 if (filp
->f_flags
& O_NONBLOCK
) {
327 if (!ret
) ret
= -EAGAIN
;
330 if (signal_pending(current
)) {
331 if (!ret
) ret
= -ERESTARTSYS
;
335 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
336 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
339 PIPE_WAITING_WRITERS(*inode
)++;
341 PIPE_WAITING_WRITERS(*inode
)--;
344 mutex_unlock(PIPE_MUTEX(*inode
));
346 wake_up_interruptible(PIPE_WAIT(*inode
));
347 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
350 file_update_time(filp
);
355 pipe_write(struct file
*filp
, const char __user
*buf
,
356 size_t count
, loff_t
*ppos
)
358 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
359 return pipe_writev(filp
, &iov
, 1, ppos
);
363 bad_pipe_r(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
369 bad_pipe_w(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
375 pipe_ioctl(struct inode
*pino
, struct file
*filp
,
376 unsigned int cmd
, unsigned long arg
)
378 struct inode
*inode
= filp
->f_dentry
->d_inode
;
379 struct pipe_inode_info
*info
;
380 int count
, buf
, nrbufs
;
384 mutex_lock(PIPE_MUTEX(*inode
));
385 info
= inode
->i_pipe
;
388 nrbufs
= info
->nrbufs
;
389 while (--nrbufs
>= 0) {
390 count
+= info
->bufs
[buf
].len
;
391 buf
= (buf
+1) & (PIPE_BUFFERS
-1);
393 mutex_unlock(PIPE_MUTEX(*inode
));
394 return put_user(count
, (int __user
*)arg
);
400 /* No kernel lock held - fine */
402 pipe_poll(struct file
*filp
, poll_table
*wait
)
405 struct inode
*inode
= filp
->f_dentry
->d_inode
;
406 struct pipe_inode_info
*info
= inode
->i_pipe
;
409 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
411 /* Reading only -- no need for acquiring the semaphore. */
412 nrbufs
= info
->nrbufs
;
414 if (filp
->f_mode
& FMODE_READ
) {
415 mask
= (nrbufs
> 0) ? POLLIN
| POLLRDNORM
: 0;
416 if (!PIPE_WRITERS(*inode
) && filp
->f_version
!= PIPE_WCOUNTER(*inode
))
420 if (filp
->f_mode
& FMODE_WRITE
) {
421 mask
|= (nrbufs
< PIPE_BUFFERS
) ? POLLOUT
| POLLWRNORM
: 0;
423 * Most Unices do not set POLLERR for FIFOs but on Linux they
424 * behave exactly like pipes for poll().
426 if (!PIPE_READERS(*inode
))
434 pipe_release(struct inode
*inode
, int decr
, int decw
)
436 mutex_lock(PIPE_MUTEX(*inode
));
437 PIPE_READERS(*inode
) -= decr
;
438 PIPE_WRITERS(*inode
) -= decw
;
439 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
440 free_pipe_info(inode
);
442 wake_up_interruptible(PIPE_WAIT(*inode
));
443 kill_fasync(PIPE_FASYNC_READERS(*inode
), SIGIO
, POLL_IN
);
444 kill_fasync(PIPE_FASYNC_WRITERS(*inode
), SIGIO
, POLL_OUT
);
446 mutex_unlock(PIPE_MUTEX(*inode
));
452 pipe_read_fasync(int fd
, struct file
*filp
, int on
)
454 struct inode
*inode
= filp
->f_dentry
->d_inode
;
457 mutex_lock(PIPE_MUTEX(*inode
));
458 retval
= fasync_helper(fd
, filp
, on
, PIPE_FASYNC_READERS(*inode
));
459 mutex_unlock(PIPE_MUTEX(*inode
));
469 pipe_write_fasync(int fd
, struct file
*filp
, int on
)
471 struct inode
*inode
= filp
->f_dentry
->d_inode
;
474 mutex_lock(PIPE_MUTEX(*inode
));
475 retval
= fasync_helper(fd
, filp
, on
, PIPE_FASYNC_WRITERS(*inode
));
476 mutex_unlock(PIPE_MUTEX(*inode
));
486 pipe_rdwr_fasync(int fd
, struct file
*filp
, int on
)
488 struct inode
*inode
= filp
->f_dentry
->d_inode
;
491 mutex_lock(PIPE_MUTEX(*inode
));
493 retval
= fasync_helper(fd
, filp
, on
, PIPE_FASYNC_READERS(*inode
));
496 retval
= fasync_helper(fd
, filp
, on
, PIPE_FASYNC_WRITERS(*inode
));
498 mutex_unlock(PIPE_MUTEX(*inode
));
508 pipe_read_release(struct inode
*inode
, struct file
*filp
)
510 pipe_read_fasync(-1, filp
, 0);
511 return pipe_release(inode
, 1, 0);
515 pipe_write_release(struct inode
*inode
, struct file
*filp
)
517 pipe_write_fasync(-1, filp
, 0);
518 return pipe_release(inode
, 0, 1);
522 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
526 pipe_rdwr_fasync(-1, filp
, 0);
527 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
528 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
529 return pipe_release(inode
, decr
, decw
);
533 pipe_read_open(struct inode
*inode
, struct file
*filp
)
535 /* We could have perhaps used atomic_t, but this and friends
536 below are the only places. So it doesn't seem worthwhile. */
537 mutex_lock(PIPE_MUTEX(*inode
));
538 PIPE_READERS(*inode
)++;
539 mutex_unlock(PIPE_MUTEX(*inode
));
545 pipe_write_open(struct inode
*inode
, struct file
*filp
)
547 mutex_lock(PIPE_MUTEX(*inode
));
548 PIPE_WRITERS(*inode
)++;
549 mutex_unlock(PIPE_MUTEX(*inode
));
555 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
557 mutex_lock(PIPE_MUTEX(*inode
));
558 if (filp
->f_mode
& FMODE_READ
)
559 PIPE_READERS(*inode
)++;
560 if (filp
->f_mode
& FMODE_WRITE
)
561 PIPE_WRITERS(*inode
)++;
562 mutex_unlock(PIPE_MUTEX(*inode
));
568 * The file_operations structs are not static because they
569 * are also used in linux/fs/fifo.c to do operations on FIFOs.
571 const struct file_operations read_fifo_fops
= {
578 .open
= pipe_read_open
,
579 .release
= pipe_read_release
,
580 .fasync
= pipe_read_fasync
,
583 const struct file_operations write_fifo_fops
= {
587 .writev
= pipe_writev
,
590 .open
= pipe_write_open
,
591 .release
= pipe_write_release
,
592 .fasync
= pipe_write_fasync
,
595 const struct file_operations rdwr_fifo_fops
= {
600 .writev
= pipe_writev
,
603 .open
= pipe_rdwr_open
,
604 .release
= pipe_rdwr_release
,
605 .fasync
= pipe_rdwr_fasync
,
608 static struct file_operations read_pipe_fops
= {
615 .open
= pipe_read_open
,
616 .release
= pipe_read_release
,
617 .fasync
= pipe_read_fasync
,
620 static struct file_operations write_pipe_fops
= {
624 .writev
= pipe_writev
,
627 .open
= pipe_write_open
,
628 .release
= pipe_write_release
,
629 .fasync
= pipe_write_fasync
,
632 static struct file_operations rdwr_pipe_fops
= {
637 .writev
= pipe_writev
,
640 .open
= pipe_rdwr_open
,
641 .release
= pipe_rdwr_release
,
642 .fasync
= pipe_rdwr_fasync
,
645 void free_pipe_info(struct inode
*inode
)
648 struct pipe_inode_info
*info
= inode
->i_pipe
;
650 inode
->i_pipe
= NULL
;
651 for (i
= 0; i
< PIPE_BUFFERS
; i
++) {
652 struct pipe_buffer
*buf
= info
->bufs
+ i
;
654 buf
->ops
->release(info
, buf
);
657 __free_page(info
->tmp_page
);
661 struct inode
* pipe_new(struct inode
* inode
)
663 struct pipe_inode_info
*info
;
665 info
= kzalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
668 inode
->i_pipe
= info
;
670 init_waitqueue_head(PIPE_WAIT(*inode
));
671 PIPE_RCOUNTER(*inode
) = PIPE_WCOUNTER(*inode
) = 1;
678 static struct vfsmount
*pipe_mnt __read_mostly
;
679 static int pipefs_delete_dentry(struct dentry
*dentry
)
683 static struct dentry_operations pipefs_dentry_operations
= {
684 .d_delete
= pipefs_delete_dentry
,
687 static struct inode
* get_pipe_inode(void)
689 struct inode
*inode
= new_inode(pipe_mnt
->mnt_sb
);
696 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
697 inode
->i_fop
= &rdwr_pipe_fops
;
700 * Mark the inode dirty from the very beginning,
701 * that way it will never be moved to the dirty
702 * list because "mark_inode_dirty()" will think
703 * that it already _is_ on the dirty list.
705 inode
->i_state
= I_DIRTY
;
706 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
707 inode
->i_uid
= current
->fsuid
;
708 inode
->i_gid
= current
->fsgid
;
709 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
710 inode
->i_blksize
= PAGE_SIZE
;
723 struct dentry
*dentry
;
724 struct inode
* inode
;
725 struct file
*f1
, *f2
;
730 f1
= get_empty_filp();
734 f2
= get_empty_filp();
738 inode
= get_pipe_inode();
742 error
= get_unused_fd();
744 goto close_f12_inode
;
747 error
= get_unused_fd();
749 goto close_f12_inode_i
;
753 sprintf(name
, "[%lu]", inode
->i_ino
);
755 this.len
= strlen(name
);
756 this.hash
= inode
->i_ino
; /* will go */
757 dentry
= d_alloc(pipe_mnt
->mnt_sb
->s_root
, &this);
759 goto close_f12_inode_i_j
;
760 dentry
->d_op
= &pipefs_dentry_operations
;
761 d_add(dentry
, inode
);
762 f1
->f_vfsmnt
= f2
->f_vfsmnt
= mntget(mntget(pipe_mnt
));
763 f1
->f_dentry
= f2
->f_dentry
= dget(dentry
);
764 f1
->f_mapping
= f2
->f_mapping
= inode
->i_mapping
;
767 f1
->f_pos
= f2
->f_pos
= 0;
768 f1
->f_flags
= O_RDONLY
;
769 f1
->f_op
= &read_pipe_fops
;
770 f1
->f_mode
= FMODE_READ
;
774 f2
->f_flags
= O_WRONLY
;
775 f2
->f_op
= &write_pipe_fops
;
776 f2
->f_mode
= FMODE_WRITE
;
790 free_pipe_info(inode
);
801 * pipefs should _never_ be mounted by userland - too much of security hassle,
802 * no real gain from having the whole whorehouse mounted. So we don't need
803 * any operations on the root directory. However, we need a non-trivial
804 * d_name - pipe: will go nicely and kill the special-casing in procfs.
807 static struct super_block
*pipefs_get_sb(struct file_system_type
*fs_type
,
808 int flags
, const char *dev_name
, void *data
)
810 return get_sb_pseudo(fs_type
, "pipe:", NULL
, PIPEFS_MAGIC
);
813 static struct file_system_type pipe_fs_type
= {
815 .get_sb
= pipefs_get_sb
,
816 .kill_sb
= kill_anon_super
,
819 static int __init
init_pipe_fs(void)
821 int err
= register_filesystem(&pipe_fs_type
);
823 pipe_mnt
= kern_mount(&pipe_fs_type
);
824 if (IS_ERR(pipe_mnt
)) {
825 err
= PTR_ERR(pipe_mnt
);
826 unregister_filesystem(&pipe_fs_type
);
832 static void __exit
exit_pipe_fs(void)
834 unregister_filesystem(&pipe_fs_type
);
838 fs_initcall(init_pipe_fs
);
839 module_exit(exit_pipe_fs
);