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>
18 #include <linux/pagemap.h>
20 #include <asm/uaccess.h>
21 #include <asm/ioctls.h>
24 * We use a start+len construction, which provides full use of the
26 * -- Florian Coosmann (FGC)
28 * Reads with count = 0 should always return 0.
29 * -- Julian Bradfield 1999-06-07.
31 * FIFOs and Pipes now generate SIGIO for both readers and writers.
32 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
34 * pipe_read & write cleanup
35 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
38 /* Drop the inode semaphore and wait for a pipe event, atomically */
39 void pipe_wait(struct inode * inode)
44 * Pipes are system-local resources, so sleeping on them
45 * is considered a noninteractive wait:
47 prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
48 mutex_unlock(PIPE_MUTEX(*inode));
50 finish_wait(PIPE_WAIT(*inode), &wait);
51 mutex_lock(PIPE_MUTEX(*inode));
55 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
62 copy = min_t(unsigned long, len, iov->iov_len);
64 if (copy_from_user(to, iov->iov_base, copy))
68 iov->iov_base += copy;
75 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
82 copy = min_t(unsigned long, len, iov->iov_len);
84 if (copy_to_user(iov->iov_base, from, copy))
88 iov->iov_base += copy;
94 static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf)
96 struct page *page = buf->page;
99 * If nobody else uses this page, and we don't already have a
100 * temporary page, let's keep track of it as a one-deep
103 if (page_count(page) == 1 && !info->tmp_page) {
104 info->tmp_page = page;
109 * Otherwise just release our reference to it
111 page_cache_release(page);
114 static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf)
116 return kmap(buf->page);
119 static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf)
124 static struct pipe_buf_operations anon_pipe_buf_ops = {
126 .map = anon_pipe_buf_map,
127 .unmap = anon_pipe_buf_unmap,
128 .release = anon_pipe_buf_release,
132 pipe_readv(struct file *filp, const struct iovec *_iov,
133 unsigned long nr_segs, loff_t *ppos)
135 struct inode *inode = filp->f_dentry->d_inode;
136 struct pipe_inode_info *info;
139 struct iovec *iov = (struct iovec *)_iov;
142 total_len = iov_length(iov, nr_segs);
143 /* Null read succeeds. */
144 if (unlikely(total_len == 0))
149 mutex_lock(PIPE_MUTEX(*inode));
150 info = inode->i_pipe;
152 int bufs = info->nrbufs;
154 int curbuf = info->curbuf;
155 struct pipe_buffer *buf = info->bufs + curbuf;
156 struct pipe_buf_operations *ops = buf->ops;
158 size_t chars = buf->len;
161 if (chars > total_len)
164 addr = ops->map(filp, info, buf);
170 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
171 ops->unmap(info, buf);
172 if (unlikely(error)) {
173 if (!ret) ret = -EFAULT;
177 buf->offset += chars;
181 ops->release(info, buf);
182 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
183 info->curbuf = curbuf;
184 info->nrbufs = --bufs;
189 break; /* common path: read succeeded */
191 if (bufs) /* More to do? */
193 if (!PIPE_WRITERS(*inode))
195 if (!PIPE_WAITING_WRITERS(*inode)) {
196 /* syscall merging: Usually we must not sleep
197 * if O_NONBLOCK is set, or if we got some data.
198 * But if a writer sleeps in kernel space, then
199 * we can wait for that data without violating POSIX.
203 if (filp->f_flags & O_NONBLOCK) {
208 if (signal_pending(current)) {
209 if (!ret) ret = -ERESTARTSYS;
213 wake_up_interruptible_sync(PIPE_WAIT(*inode));
214 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
218 mutex_unlock(PIPE_MUTEX(*inode));
219 /* Signal writers asynchronously that there is more room. */
221 wake_up_interruptible(PIPE_WAIT(*inode));
222 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
230 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
232 struct iovec iov = { .iov_base = buf, .iov_len = count };
233 return pipe_readv(filp, &iov, 1, ppos);
237 pipe_writev(struct file *filp, const struct iovec *_iov,
238 unsigned long nr_segs, loff_t *ppos)
240 struct inode *inode = filp->f_dentry->d_inode;
241 struct pipe_inode_info *info;
244 struct iovec *iov = (struct iovec *)_iov;
248 total_len = iov_length(iov, nr_segs);
249 /* Null write succeeds. */
250 if (unlikely(total_len == 0))
255 mutex_lock(PIPE_MUTEX(*inode));
256 info = inode->i_pipe;
258 if (!PIPE_READERS(*inode)) {
259 send_sig(SIGPIPE, current, 0);
264 /* We try to merge small writes */
265 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
266 if (info->nrbufs && chars != 0) {
267 int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1);
268 struct pipe_buffer *buf = info->bufs + lastbuf;
269 struct pipe_buf_operations *ops = buf->ops;
270 int offset = buf->offset + buf->len;
271 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
275 addr = ops->map(filp, info, buf);
277 error = PTR_ERR(addr);
280 error = pipe_iov_copy_from_user(offset + addr, iov,
282 ops->unmap(info, buf);
297 if (!PIPE_READERS(*inode)) {
298 send_sig(SIGPIPE, current, 0);
299 if (!ret) ret = -EPIPE;
303 if (bufs < PIPE_BUFFERS) {
304 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1);
305 struct pipe_buffer *buf = info->bufs + newbuf;
306 struct page *page = info->tmp_page;
310 page = alloc_page(GFP_HIGHUSER);
311 if (unlikely(!page)) {
312 ret = ret ? : -ENOMEM;
315 info->tmp_page = page;
317 /* Always wakeup, even if the copy fails. Otherwise
318 * we lock up (O_NONBLOCK-)readers that sleep due to
320 * FIXME! Is this really true?
324 if (chars > total_len)
327 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
329 if (unlikely(error)) {
330 if (!ret) ret = -EFAULT;
335 /* Insert it into the buffer array */
337 buf->ops = &anon_pipe_buf_ops;
340 info->nrbufs = ++bufs;
341 info->tmp_page = NULL;
347 if (bufs < PIPE_BUFFERS)
349 if (filp->f_flags & O_NONBLOCK) {
350 if (!ret) ret = -EAGAIN;
353 if (signal_pending(current)) {
354 if (!ret) ret = -ERESTARTSYS;
358 wake_up_interruptible_sync(PIPE_WAIT(*inode));
359 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
362 PIPE_WAITING_WRITERS(*inode)++;
364 PIPE_WAITING_WRITERS(*inode)--;
367 mutex_unlock(PIPE_MUTEX(*inode));
369 wake_up_interruptible(PIPE_WAIT(*inode));
370 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
373 file_update_time(filp);
378 pipe_write(struct file *filp, const char __user *buf,
379 size_t count, loff_t *ppos)
381 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
382 return pipe_writev(filp, &iov, 1, ppos);
386 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
392 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
398 pipe_ioctl(struct inode *pino, struct file *filp,
399 unsigned int cmd, unsigned long arg)
401 struct inode *inode = filp->f_dentry->d_inode;
402 struct pipe_inode_info *info;
403 int count, buf, nrbufs;
407 mutex_lock(PIPE_MUTEX(*inode));
408 info = inode->i_pipe;
411 nrbufs = info->nrbufs;
412 while (--nrbufs >= 0) {
413 count += info->bufs[buf].len;
414 buf = (buf+1) & (PIPE_BUFFERS-1);
416 mutex_unlock(PIPE_MUTEX(*inode));
417 return put_user(count, (int __user *)arg);
423 /* No kernel lock held - fine */
425 pipe_poll(struct file *filp, poll_table *wait)
428 struct inode *inode = filp->f_dentry->d_inode;
429 struct pipe_inode_info *info = inode->i_pipe;
432 poll_wait(filp, PIPE_WAIT(*inode), wait);
434 /* Reading only -- no need for acquiring the semaphore. */
435 nrbufs = info->nrbufs;
437 if (filp->f_mode & FMODE_READ) {
438 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
439 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
443 if (filp->f_mode & FMODE_WRITE) {
444 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
446 * Most Unices do not set POLLERR for FIFOs but on Linux they
447 * behave exactly like pipes for poll().
449 if (!PIPE_READERS(*inode))
457 pipe_release(struct inode *inode, int decr, int decw)
459 mutex_lock(PIPE_MUTEX(*inode));
460 PIPE_READERS(*inode) -= decr;
461 PIPE_WRITERS(*inode) -= decw;
462 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
463 free_pipe_info(inode);
465 wake_up_interruptible(PIPE_WAIT(*inode));
466 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
467 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
469 mutex_unlock(PIPE_MUTEX(*inode));
475 pipe_read_fasync(int fd, struct file *filp, int on)
477 struct inode *inode = filp->f_dentry->d_inode;
480 mutex_lock(PIPE_MUTEX(*inode));
481 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
482 mutex_unlock(PIPE_MUTEX(*inode));
492 pipe_write_fasync(int fd, struct file *filp, int on)
494 struct inode *inode = filp->f_dentry->d_inode;
497 mutex_lock(PIPE_MUTEX(*inode));
498 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
499 mutex_unlock(PIPE_MUTEX(*inode));
509 pipe_rdwr_fasync(int fd, struct file *filp, int on)
511 struct inode *inode = filp->f_dentry->d_inode;
514 mutex_lock(PIPE_MUTEX(*inode));
516 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
519 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
521 mutex_unlock(PIPE_MUTEX(*inode));
531 pipe_read_release(struct inode *inode, struct file *filp)
533 pipe_read_fasync(-1, filp, 0);
534 return pipe_release(inode, 1, 0);
538 pipe_write_release(struct inode *inode, struct file *filp)
540 pipe_write_fasync(-1, filp, 0);
541 return pipe_release(inode, 0, 1);
545 pipe_rdwr_release(struct inode *inode, struct file *filp)
549 pipe_rdwr_fasync(-1, filp, 0);
550 decr = (filp->f_mode & FMODE_READ) != 0;
551 decw = (filp->f_mode & FMODE_WRITE) != 0;
552 return pipe_release(inode, decr, decw);
556 pipe_read_open(struct inode *inode, struct file *filp)
558 /* We could have perhaps used atomic_t, but this and friends
559 below are the only places. So it doesn't seem worthwhile. */
560 mutex_lock(PIPE_MUTEX(*inode));
561 PIPE_READERS(*inode)++;
562 mutex_unlock(PIPE_MUTEX(*inode));
568 pipe_write_open(struct inode *inode, struct file *filp)
570 mutex_lock(PIPE_MUTEX(*inode));
571 PIPE_WRITERS(*inode)++;
572 mutex_unlock(PIPE_MUTEX(*inode));
578 pipe_rdwr_open(struct inode *inode, struct file *filp)
580 mutex_lock(PIPE_MUTEX(*inode));
581 if (filp->f_mode & FMODE_READ)
582 PIPE_READERS(*inode)++;
583 if (filp->f_mode & FMODE_WRITE)
584 PIPE_WRITERS(*inode)++;
585 mutex_unlock(PIPE_MUTEX(*inode));
591 * The file_operations structs are not static because they
592 * are also used in linux/fs/fifo.c to do operations on FIFOs.
594 const struct file_operations read_fifo_fops = {
601 .open = pipe_read_open,
602 .release = pipe_read_release,
603 .fasync = pipe_read_fasync,
606 const struct file_operations write_fifo_fops = {
610 .writev = pipe_writev,
613 .open = pipe_write_open,
614 .release = pipe_write_release,
615 .fasync = pipe_write_fasync,
618 const struct file_operations rdwr_fifo_fops = {
623 .writev = pipe_writev,
626 .open = pipe_rdwr_open,
627 .release = pipe_rdwr_release,
628 .fasync = pipe_rdwr_fasync,
631 static struct file_operations read_pipe_fops = {
638 .open = pipe_read_open,
639 .release = pipe_read_release,
640 .fasync = pipe_read_fasync,
643 static struct file_operations write_pipe_fops = {
647 .writev = pipe_writev,
650 .open = pipe_write_open,
651 .release = pipe_write_release,
652 .fasync = pipe_write_fasync,
655 static struct file_operations rdwr_pipe_fops = {
660 .writev = pipe_writev,
663 .open = pipe_rdwr_open,
664 .release = pipe_rdwr_release,
665 .fasync = pipe_rdwr_fasync,
668 void free_pipe_info(struct inode *inode)
671 struct pipe_inode_info *info = inode->i_pipe;
673 inode->i_pipe = NULL;
674 for (i = 0; i < PIPE_BUFFERS; i++) {
675 struct pipe_buffer *buf = info->bufs + i;
677 buf->ops->release(info, buf);
680 __free_page(info->tmp_page);
684 struct inode* pipe_new(struct inode* inode)
686 struct pipe_inode_info *info;
688 info = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
691 inode->i_pipe = info;
693 init_waitqueue_head(PIPE_WAIT(*inode));
694 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
701 static struct vfsmount *pipe_mnt __read_mostly;
702 static int pipefs_delete_dentry(struct dentry *dentry)
706 static struct dentry_operations pipefs_dentry_operations = {
707 .d_delete = pipefs_delete_dentry,
710 static struct inode * get_pipe_inode(void)
712 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
719 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
720 inode->i_fop = &rdwr_pipe_fops;
723 * Mark the inode dirty from the very beginning,
724 * that way it will never be moved to the dirty
725 * list because "mark_inode_dirty()" will think
726 * that it already _is_ on the dirty list.
728 inode->i_state = I_DIRTY;
729 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
730 inode->i_uid = current->fsuid;
731 inode->i_gid = current->fsgid;
732 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
733 inode->i_blksize = PAGE_SIZE;
746 struct dentry *dentry;
747 struct inode * inode;
748 struct file *f1, *f2;
753 f1 = get_empty_filp();
757 f2 = get_empty_filp();
761 inode = get_pipe_inode();
765 error = get_unused_fd();
767 goto close_f12_inode;
770 error = get_unused_fd();
772 goto close_f12_inode_i;
776 sprintf(name, "[%lu]", inode->i_ino);
778 this.len = strlen(name);
779 this.hash = inode->i_ino; /* will go */
780 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
782 goto close_f12_inode_i_j;
783 dentry->d_op = &pipefs_dentry_operations;
784 d_add(dentry, inode);
785 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
786 f1->f_dentry = f2->f_dentry = dget(dentry);
787 f1->f_mapping = f2->f_mapping = inode->i_mapping;
790 f1->f_pos = f2->f_pos = 0;
791 f1->f_flags = O_RDONLY;
792 f1->f_op = &read_pipe_fops;
793 f1->f_mode = FMODE_READ;
797 f2->f_flags = O_WRONLY;
798 f2->f_op = &write_pipe_fops;
799 f2->f_mode = FMODE_WRITE;
813 free_pipe_info(inode);
824 * pipefs should _never_ be mounted by userland - too much of security hassle,
825 * no real gain from having the whole whorehouse mounted. So we don't need
826 * any operations on the root directory. However, we need a non-trivial
827 * d_name - pipe: will go nicely and kill the special-casing in procfs.
830 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
831 int flags, const char *dev_name, void *data)
833 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
836 static struct file_system_type pipe_fs_type = {
838 .get_sb = pipefs_get_sb,
839 .kill_sb = kill_anon_super,
842 static int __init init_pipe_fs(void)
844 int err = register_filesystem(&pipe_fs_type);
846 pipe_mnt = kern_mount(&pipe_fs_type);
847 if (IS_ERR(pipe_mnt)) {
848 err = PTR_ERR(pipe_mnt);
849 unregister_filesystem(&pipe_fs_type);
855 static void __exit exit_pipe_fs(void)
857 unregister_filesystem(&pipe_fs_type);
861 fs_initcall(init_pipe_fs);
862 module_exit(exit_pipe_fs);