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 pipe_inode_info *pipe)
44 * Pipes are system-local resources, so sleeping on them
45 * is considered a noninteractive wait:
47 prepare_to_wait(&pipe->wait, &wait,
48 TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
50 mutex_unlock(&pipe->inode->i_mutex);
52 finish_wait(&pipe->wait, &wait);
54 mutex_lock(&pipe->inode->i_mutex);
58 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
65 copy = min_t(unsigned long, len, iov->iov_len);
67 if (copy_from_user(to, iov->iov_base, copy))
71 iov->iov_base += copy;
78 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
85 copy = min_t(unsigned long, len, iov->iov_len);
87 if (copy_to_user(iov->iov_base, from, copy))
91 iov->iov_base += copy;
97 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
98 struct pipe_buffer *buf)
100 struct page *page = buf->page;
102 buf->flags &= ~PIPE_BUF_FLAG_STOLEN;
105 * If nobody else uses this page, and we don't already have a
106 * temporary page, let's keep track of it as a one-deep
107 * allocation cache. (Otherwise just release our reference to it)
109 if (page_count(page) == 1 && !pipe->tmp_page)
110 pipe->tmp_page = page;
112 page_cache_release(page);
115 static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe,
116 struct pipe_buffer *buf)
118 return kmap(buf->page);
121 static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe,
122 struct pipe_buffer *buf)
127 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
128 struct pipe_buffer *buf)
130 struct page *page = buf->page;
132 if (page_count(page) == 1) {
133 buf->flags |= PIPE_BUF_FLAG_STOLEN;
141 static void anon_pipe_buf_get(struct pipe_inode_info *info,
142 struct pipe_buffer *buf)
144 page_cache_get(buf->page);
147 static struct pipe_buf_operations anon_pipe_buf_ops = {
149 .map = anon_pipe_buf_map,
150 .unmap = anon_pipe_buf_unmap,
151 .release = anon_pipe_buf_release,
152 .steal = anon_pipe_buf_steal,
153 .get = anon_pipe_buf_get,
157 pipe_readv(struct file *filp, const struct iovec *_iov,
158 unsigned long nr_segs, loff_t *ppos)
160 struct inode *inode = filp->f_dentry->d_inode;
161 struct pipe_inode_info *pipe;
164 struct iovec *iov = (struct iovec *)_iov;
167 total_len = iov_length(iov, nr_segs);
168 /* Null read succeeds. */
169 if (unlikely(total_len == 0))
174 mutex_lock(&inode->i_mutex);
175 pipe = inode->i_pipe;
177 int bufs = pipe->nrbufs;
179 int curbuf = pipe->curbuf;
180 struct pipe_buffer *buf = pipe->bufs + curbuf;
181 struct pipe_buf_operations *ops = buf->ops;
183 size_t chars = buf->len;
186 if (chars > total_len)
189 addr = ops->map(filp, pipe, buf);
195 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
196 ops->unmap(pipe, buf);
197 if (unlikely(error)) {
203 buf->offset += chars;
207 ops->release(pipe, buf);
208 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
209 pipe->curbuf = curbuf;
210 pipe->nrbufs = --bufs;
215 break; /* common path: read succeeded */
217 if (bufs) /* More to do? */
221 if (!pipe->waiting_writers) {
222 /* syscall merging: Usually we must not sleep
223 * if O_NONBLOCK is set, or if we got some data.
224 * But if a writer sleeps in kernel space, then
225 * we can wait for that data without violating POSIX.
229 if (filp->f_flags & O_NONBLOCK) {
234 if (signal_pending(current)) {
240 wake_up_interruptible_sync(&pipe->wait);
241 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
245 mutex_unlock(&inode->i_mutex);
247 /* Signal writers asynchronously that there is more room. */
249 wake_up_interruptible(&pipe->wait);
250 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
258 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
260 struct iovec iov = { .iov_base = buf, .iov_len = count };
262 return pipe_readv(filp, &iov, 1, ppos);
266 pipe_writev(struct file *filp, const struct iovec *_iov,
267 unsigned long nr_segs, loff_t *ppos)
269 struct inode *inode = filp->f_dentry->d_inode;
270 struct pipe_inode_info *pipe;
273 struct iovec *iov = (struct iovec *)_iov;
277 total_len = iov_length(iov, nr_segs);
278 /* Null write succeeds. */
279 if (unlikely(total_len == 0))
284 mutex_lock(&inode->i_mutex);
285 pipe = inode->i_pipe;
287 if (!pipe->readers) {
288 send_sig(SIGPIPE, current, 0);
293 /* We try to merge small writes */
294 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
295 if (pipe->nrbufs && chars != 0) {
296 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
298 struct pipe_buffer *buf = pipe->bufs + lastbuf;
299 struct pipe_buf_operations *ops = buf->ops;
300 int offset = buf->offset + buf->len;
302 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
306 addr = ops->map(filp, pipe, buf);
308 error = PTR_ERR(addr);
311 error = pipe_iov_copy_from_user(offset + addr, iov,
313 ops->unmap(pipe, buf);
329 if (!pipe->readers) {
330 send_sig(SIGPIPE, current, 0);
336 if (bufs < PIPE_BUFFERS) {
337 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
338 struct pipe_buffer *buf = pipe->bufs + newbuf;
339 struct page *page = pipe->tmp_page;
343 page = alloc_page(GFP_HIGHUSER);
344 if (unlikely(!page)) {
345 ret = ret ? : -ENOMEM;
348 pipe->tmp_page = page;
350 /* Always wake up, even if the copy fails. Otherwise
351 * we lock up (O_NONBLOCK-)readers that sleep due to
353 * FIXME! Is this really true?
357 if (chars > total_len)
360 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
362 if (unlikely(error)) {
369 /* Insert it into the buffer array */
371 buf->ops = &anon_pipe_buf_ops;
374 pipe->nrbufs = ++bufs;
375 pipe->tmp_page = NULL;
381 if (bufs < PIPE_BUFFERS)
383 if (filp->f_flags & O_NONBLOCK) {
388 if (signal_pending(current)) {
394 wake_up_interruptible_sync(&pipe->wait);
395 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
398 pipe->waiting_writers++;
400 pipe->waiting_writers--;
403 mutex_unlock(&inode->i_mutex);
405 wake_up_interruptible(&pipe->wait);
406 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
409 file_update_time(filp);
414 pipe_write(struct file *filp, const char __user *buf,
415 size_t count, loff_t *ppos)
417 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
419 return pipe_writev(filp, &iov, 1, ppos);
423 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
429 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
436 pipe_ioctl(struct inode *pino, struct file *filp,
437 unsigned int cmd, unsigned long arg)
439 struct inode *inode = filp->f_dentry->d_inode;
440 struct pipe_inode_info *pipe;
441 int count, buf, nrbufs;
445 mutex_lock(&inode->i_mutex);
446 pipe = inode->i_pipe;
449 nrbufs = pipe->nrbufs;
450 while (--nrbufs >= 0) {
451 count += pipe->bufs[buf].len;
452 buf = (buf+1) & (PIPE_BUFFERS-1);
454 mutex_unlock(&inode->i_mutex);
456 return put_user(count, (int __user *)arg);
462 /* No kernel lock held - fine */
464 pipe_poll(struct file *filp, poll_table *wait)
467 struct inode *inode = filp->f_dentry->d_inode;
468 struct pipe_inode_info *pipe = inode->i_pipe;
471 poll_wait(filp, &pipe->wait, wait);
473 /* Reading only -- no need for acquiring the semaphore. */
474 nrbufs = pipe->nrbufs;
476 if (filp->f_mode & FMODE_READ) {
477 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
478 if (!pipe->writers && filp->f_version != pipe->w_counter)
482 if (filp->f_mode & FMODE_WRITE) {
483 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
485 * Most Unices do not set POLLERR for FIFOs but on Linux they
486 * behave exactly like pipes for poll().
496 pipe_release(struct inode *inode, int decr, int decw)
498 struct pipe_inode_info *pipe;
500 mutex_lock(&inode->i_mutex);
501 pipe = inode->i_pipe;
502 pipe->readers -= decr;
503 pipe->writers -= decw;
505 if (!pipe->readers && !pipe->writers) {
506 free_pipe_info(inode);
508 wake_up_interruptible(&pipe->wait);
509 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
510 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
512 mutex_unlock(&inode->i_mutex);
518 pipe_read_fasync(int fd, struct file *filp, int on)
520 struct inode *inode = filp->f_dentry->d_inode;
523 mutex_lock(&inode->i_mutex);
524 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
525 mutex_unlock(&inode->i_mutex);
535 pipe_write_fasync(int fd, struct file *filp, int on)
537 struct inode *inode = filp->f_dentry->d_inode;
540 mutex_lock(&inode->i_mutex);
541 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
542 mutex_unlock(&inode->i_mutex);
552 pipe_rdwr_fasync(int fd, struct file *filp, int on)
554 struct inode *inode = filp->f_dentry->d_inode;
555 struct pipe_inode_info *pipe = inode->i_pipe;
558 mutex_lock(&inode->i_mutex);
560 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
563 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
565 mutex_unlock(&inode->i_mutex);
575 pipe_read_release(struct inode *inode, struct file *filp)
577 pipe_read_fasync(-1, filp, 0);
578 return pipe_release(inode, 1, 0);
582 pipe_write_release(struct inode *inode, struct file *filp)
584 pipe_write_fasync(-1, filp, 0);
585 return pipe_release(inode, 0, 1);
589 pipe_rdwr_release(struct inode *inode, struct file *filp)
593 pipe_rdwr_fasync(-1, filp, 0);
594 decr = (filp->f_mode & FMODE_READ) != 0;
595 decw = (filp->f_mode & FMODE_WRITE) != 0;
596 return pipe_release(inode, decr, decw);
600 pipe_read_open(struct inode *inode, struct file *filp)
602 /* We could have perhaps used atomic_t, but this and friends
603 below are the only places. So it doesn't seem worthwhile. */
604 mutex_lock(&inode->i_mutex);
605 inode->i_pipe->readers++;
606 mutex_unlock(&inode->i_mutex);
612 pipe_write_open(struct inode *inode, struct file *filp)
614 mutex_lock(&inode->i_mutex);
615 inode->i_pipe->writers++;
616 mutex_unlock(&inode->i_mutex);
622 pipe_rdwr_open(struct inode *inode, struct file *filp)
624 mutex_lock(&inode->i_mutex);
625 if (filp->f_mode & FMODE_READ)
626 inode->i_pipe->readers++;
627 if (filp->f_mode & FMODE_WRITE)
628 inode->i_pipe->writers++;
629 mutex_unlock(&inode->i_mutex);
635 * The file_operations structs are not static because they
636 * are also used in linux/fs/fifo.c to do operations on FIFOs.
638 const struct file_operations read_fifo_fops = {
645 .open = pipe_read_open,
646 .release = pipe_read_release,
647 .fasync = pipe_read_fasync,
650 const struct file_operations write_fifo_fops = {
654 .writev = pipe_writev,
657 .open = pipe_write_open,
658 .release = pipe_write_release,
659 .fasync = pipe_write_fasync,
662 const struct file_operations rdwr_fifo_fops = {
667 .writev = pipe_writev,
670 .open = pipe_rdwr_open,
671 .release = pipe_rdwr_release,
672 .fasync = pipe_rdwr_fasync,
675 static struct file_operations read_pipe_fops = {
682 .open = pipe_read_open,
683 .release = pipe_read_release,
684 .fasync = pipe_read_fasync,
687 static struct file_operations write_pipe_fops = {
691 .writev = pipe_writev,
694 .open = pipe_write_open,
695 .release = pipe_write_release,
696 .fasync = pipe_write_fasync,
699 static struct file_operations rdwr_pipe_fops = {
704 .writev = pipe_writev,
707 .open = pipe_rdwr_open,
708 .release = pipe_rdwr_release,
709 .fasync = pipe_rdwr_fasync,
712 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
714 struct pipe_inode_info *pipe;
716 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
718 init_waitqueue_head(&pipe->wait);
719 pipe->r_counter = pipe->w_counter = 1;
726 void __free_pipe_info(struct pipe_inode_info *pipe)
730 for (i = 0; i < PIPE_BUFFERS; i++) {
731 struct pipe_buffer *buf = pipe->bufs + i;
733 buf->ops->release(pipe, buf);
736 __free_page(pipe->tmp_page);
740 void free_pipe_info(struct inode *inode)
742 __free_pipe_info(inode->i_pipe);
743 inode->i_pipe = NULL;
746 static struct vfsmount *pipe_mnt __read_mostly;
747 static int pipefs_delete_dentry(struct dentry *dentry)
752 static struct dentry_operations pipefs_dentry_operations = {
753 .d_delete = pipefs_delete_dentry,
756 static struct inode * get_pipe_inode(void)
758 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
759 struct pipe_inode_info *pipe;
764 pipe = alloc_pipe_info(inode);
767 inode->i_pipe = pipe;
769 pipe->readers = pipe->writers = 1;
770 inode->i_fop = &rdwr_pipe_fops;
773 * Mark the inode dirty from the very beginning,
774 * that way it will never be moved to the dirty
775 * list because "mark_inode_dirty()" will think
776 * that it already _is_ on the dirty list.
778 inode->i_state = I_DIRTY;
779 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
780 inode->i_uid = current->fsuid;
781 inode->i_gid = current->fsgid;
782 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
783 inode->i_blksize = PAGE_SIZE;
798 struct dentry *dentry;
799 struct inode * inode;
800 struct file *f1, *f2;
805 f1 = get_empty_filp();
809 f2 = get_empty_filp();
813 inode = get_pipe_inode();
817 error = get_unused_fd();
819 goto close_f12_inode;
822 error = get_unused_fd();
824 goto close_f12_inode_i;
828 sprintf(name, "[%lu]", inode->i_ino);
830 this.len = strlen(name);
831 this.hash = inode->i_ino; /* will go */
832 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
834 goto close_f12_inode_i_j;
836 dentry->d_op = &pipefs_dentry_operations;
837 d_add(dentry, inode);
838 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
839 f1->f_dentry = f2->f_dentry = dget(dentry);
840 f1->f_mapping = f2->f_mapping = inode->i_mapping;
843 f1->f_pos = f2->f_pos = 0;
844 f1->f_flags = O_RDONLY;
845 f1->f_op = &read_pipe_fops;
846 f1->f_mode = FMODE_READ;
850 f2->f_flags = O_WRONLY;
851 f2->f_op = &write_pipe_fops;
852 f2->f_mode = FMODE_WRITE;
867 free_pipe_info(inode);
878 * pipefs should _never_ be mounted by userland - too much of security hassle,
879 * no real gain from having the whole whorehouse mounted. So we don't need
880 * any operations on the root directory. However, we need a non-trivial
881 * d_name - pipe: will go nicely and kill the special-casing in procfs.
884 static struct super_block *
885 pipefs_get_sb(struct file_system_type *fs_type, int flags,
886 const char *dev_name, void *data)
888 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
891 static struct file_system_type pipe_fs_type = {
893 .get_sb = pipefs_get_sb,
894 .kill_sb = kill_anon_super,
897 static int __init init_pipe_fs(void)
899 int err = register_filesystem(&pipe_fs_type);
902 pipe_mnt = kern_mount(&pipe_fs_type);
903 if (IS_ERR(pipe_mnt)) {
904 err = PTR_ERR(pipe_mnt);
905 unregister_filesystem(&pipe_fs_type);
911 static void __exit exit_pipe_fs(void)
913 unregister_filesystem(&pipe_fs_type);
917 fs_initcall(init_pipe_fs);
918 module_exit(exit_pipe_fs);