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;
103 * If nobody else uses this page, and we don't already have a
104 * temporary page, let's keep track of it as a one-deep
105 * allocation cache. (Otherwise just release our reference to it)
107 if (page_count(page) == 1 && !pipe->tmp_page)
108 pipe->tmp_page = page;
110 page_cache_release(page);
113 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
114 struct pipe_buffer *buf)
116 return kmap(buf->page);
119 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
120 struct pipe_buffer *buf)
125 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
126 struct pipe_buffer *buf)
128 struct page *page = buf->page;
130 if (page_count(page) == 1) {
138 void generic_pipe_buf_get(struct pipe_inode_info *info, struct pipe_buffer *buf)
140 page_cache_get(buf->page);
143 int generic_pipe_buf_pin(struct pipe_inode_info *info, struct pipe_buffer *buf)
148 static struct pipe_buf_operations anon_pipe_buf_ops = {
150 .map = generic_pipe_buf_map,
151 .unmap = generic_pipe_buf_unmap,
152 .pin = generic_pipe_buf_pin,
153 .release = anon_pipe_buf_release,
154 .steal = anon_pipe_buf_steal,
155 .get = generic_pipe_buf_get,
159 pipe_readv(struct file *filp, const struct iovec *_iov,
160 unsigned long nr_segs, loff_t *ppos)
162 struct inode *inode = filp->f_dentry->d_inode;
163 struct pipe_inode_info *pipe;
166 struct iovec *iov = (struct iovec *)_iov;
169 total_len = iov_length(iov, nr_segs);
170 /* Null read succeeds. */
171 if (unlikely(total_len == 0))
176 mutex_lock(&inode->i_mutex);
177 pipe = inode->i_pipe;
179 int bufs = pipe->nrbufs;
181 int curbuf = pipe->curbuf;
182 struct pipe_buffer *buf = pipe->bufs + curbuf;
183 struct pipe_buf_operations *ops = buf->ops;
185 size_t chars = buf->len;
188 if (chars > total_len)
191 error = ops->pin(pipe, buf);
198 addr = ops->map(pipe, buf);
199 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
200 ops->unmap(pipe, buf);
201 if (unlikely(error)) {
207 buf->offset += chars;
211 ops->release(pipe, buf);
212 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
213 pipe->curbuf = curbuf;
214 pipe->nrbufs = --bufs;
219 break; /* common path: read succeeded */
221 if (bufs) /* More to do? */
225 if (!pipe->waiting_writers) {
226 /* syscall merging: Usually we must not sleep
227 * if O_NONBLOCK is set, or if we got some data.
228 * But if a writer sleeps in kernel space, then
229 * we can wait for that data without violating POSIX.
233 if (filp->f_flags & O_NONBLOCK) {
238 if (signal_pending(current)) {
244 wake_up_interruptible_sync(&pipe->wait);
245 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
249 mutex_unlock(&inode->i_mutex);
251 /* Signal writers asynchronously that there is more room. */
253 wake_up_interruptible(&pipe->wait);
254 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
262 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
264 struct iovec iov = { .iov_base = buf, .iov_len = count };
266 return pipe_readv(filp, &iov, 1, ppos);
270 pipe_writev(struct file *filp, const struct iovec *_iov,
271 unsigned long nr_segs, loff_t *ppos)
273 struct inode *inode = filp->f_dentry->d_inode;
274 struct pipe_inode_info *pipe;
277 struct iovec *iov = (struct iovec *)_iov;
281 total_len = iov_length(iov, nr_segs);
282 /* Null write succeeds. */
283 if (unlikely(total_len == 0))
288 mutex_lock(&inode->i_mutex);
289 pipe = inode->i_pipe;
291 if (!pipe->readers) {
292 send_sig(SIGPIPE, current, 0);
297 /* We try to merge small writes */
298 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
299 if (pipe->nrbufs && chars != 0) {
300 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
302 struct pipe_buffer *buf = pipe->bufs + lastbuf;
303 struct pipe_buf_operations *ops = buf->ops;
304 int offset = buf->offset + buf->len;
306 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
310 error = ops->pin(pipe, buf);
314 addr = ops->map(pipe, buf);
315 error = pipe_iov_copy_from_user(offset + addr, iov,
317 ops->unmap(pipe, buf);
333 if (!pipe->readers) {
334 send_sig(SIGPIPE, current, 0);
340 if (bufs < PIPE_BUFFERS) {
341 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
342 struct pipe_buffer *buf = pipe->bufs + newbuf;
343 struct page *page = pipe->tmp_page;
347 page = alloc_page(GFP_HIGHUSER);
348 if (unlikely(!page)) {
349 ret = ret ? : -ENOMEM;
352 pipe->tmp_page = page;
354 /* Always wake up, even if the copy fails. Otherwise
355 * we lock up (O_NONBLOCK-)readers that sleep due to
357 * FIXME! Is this really true?
361 if (chars > total_len)
364 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
366 if (unlikely(error)) {
373 /* Insert it into the buffer array */
375 buf->ops = &anon_pipe_buf_ops;
378 pipe->nrbufs = ++bufs;
379 pipe->tmp_page = NULL;
385 if (bufs < PIPE_BUFFERS)
387 if (filp->f_flags & O_NONBLOCK) {
392 if (signal_pending(current)) {
398 wake_up_interruptible_sync(&pipe->wait);
399 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
402 pipe->waiting_writers++;
404 pipe->waiting_writers--;
407 mutex_unlock(&inode->i_mutex);
409 wake_up_interruptible(&pipe->wait);
410 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
413 file_update_time(filp);
418 pipe_write(struct file *filp, const char __user *buf,
419 size_t count, loff_t *ppos)
421 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
423 return pipe_writev(filp, &iov, 1, ppos);
427 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
433 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
440 pipe_ioctl(struct inode *pino, struct file *filp,
441 unsigned int cmd, unsigned long arg)
443 struct inode *inode = filp->f_dentry->d_inode;
444 struct pipe_inode_info *pipe;
445 int count, buf, nrbufs;
449 mutex_lock(&inode->i_mutex);
450 pipe = inode->i_pipe;
453 nrbufs = pipe->nrbufs;
454 while (--nrbufs >= 0) {
455 count += pipe->bufs[buf].len;
456 buf = (buf+1) & (PIPE_BUFFERS-1);
458 mutex_unlock(&inode->i_mutex);
460 return put_user(count, (int __user *)arg);
466 /* No kernel lock held - fine */
468 pipe_poll(struct file *filp, poll_table *wait)
471 struct inode *inode = filp->f_dentry->d_inode;
472 struct pipe_inode_info *pipe = inode->i_pipe;
475 poll_wait(filp, &pipe->wait, wait);
477 /* Reading only -- no need for acquiring the semaphore. */
478 nrbufs = pipe->nrbufs;
480 if (filp->f_mode & FMODE_READ) {
481 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
482 if (!pipe->writers && filp->f_version != pipe->w_counter)
486 if (filp->f_mode & FMODE_WRITE) {
487 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
489 * Most Unices do not set POLLERR for FIFOs but on Linux they
490 * behave exactly like pipes for poll().
500 pipe_release(struct inode *inode, int decr, int decw)
502 struct pipe_inode_info *pipe;
504 mutex_lock(&inode->i_mutex);
505 pipe = inode->i_pipe;
506 pipe->readers -= decr;
507 pipe->writers -= decw;
509 if (!pipe->readers && !pipe->writers) {
510 free_pipe_info(inode);
512 wake_up_interruptible(&pipe->wait);
513 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
514 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
516 mutex_unlock(&inode->i_mutex);
522 pipe_read_fasync(int fd, struct file *filp, int on)
524 struct inode *inode = filp->f_dentry->d_inode;
527 mutex_lock(&inode->i_mutex);
528 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
529 mutex_unlock(&inode->i_mutex);
539 pipe_write_fasync(int fd, struct file *filp, int on)
541 struct inode *inode = filp->f_dentry->d_inode;
544 mutex_lock(&inode->i_mutex);
545 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
546 mutex_unlock(&inode->i_mutex);
556 pipe_rdwr_fasync(int fd, struct file *filp, int on)
558 struct inode *inode = filp->f_dentry->d_inode;
559 struct pipe_inode_info *pipe = inode->i_pipe;
562 mutex_lock(&inode->i_mutex);
564 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
567 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
569 mutex_unlock(&inode->i_mutex);
579 pipe_read_release(struct inode *inode, struct file *filp)
581 pipe_read_fasync(-1, filp, 0);
582 return pipe_release(inode, 1, 0);
586 pipe_write_release(struct inode *inode, struct file *filp)
588 pipe_write_fasync(-1, filp, 0);
589 return pipe_release(inode, 0, 1);
593 pipe_rdwr_release(struct inode *inode, struct file *filp)
597 pipe_rdwr_fasync(-1, filp, 0);
598 decr = (filp->f_mode & FMODE_READ) != 0;
599 decw = (filp->f_mode & FMODE_WRITE) != 0;
600 return pipe_release(inode, decr, decw);
604 pipe_read_open(struct inode *inode, struct file *filp)
606 /* We could have perhaps used atomic_t, but this and friends
607 below are the only places. So it doesn't seem worthwhile. */
608 mutex_lock(&inode->i_mutex);
609 inode->i_pipe->readers++;
610 mutex_unlock(&inode->i_mutex);
616 pipe_write_open(struct inode *inode, struct file *filp)
618 mutex_lock(&inode->i_mutex);
619 inode->i_pipe->writers++;
620 mutex_unlock(&inode->i_mutex);
626 pipe_rdwr_open(struct inode *inode, struct file *filp)
628 mutex_lock(&inode->i_mutex);
629 if (filp->f_mode & FMODE_READ)
630 inode->i_pipe->readers++;
631 if (filp->f_mode & FMODE_WRITE)
632 inode->i_pipe->writers++;
633 mutex_unlock(&inode->i_mutex);
639 * The file_operations structs are not static because they
640 * are also used in linux/fs/fifo.c to do operations on FIFOs.
642 const struct file_operations read_fifo_fops = {
649 .open = pipe_read_open,
650 .release = pipe_read_release,
651 .fasync = pipe_read_fasync,
654 const struct file_operations write_fifo_fops = {
658 .writev = pipe_writev,
661 .open = pipe_write_open,
662 .release = pipe_write_release,
663 .fasync = pipe_write_fasync,
666 const struct file_operations rdwr_fifo_fops = {
671 .writev = pipe_writev,
674 .open = pipe_rdwr_open,
675 .release = pipe_rdwr_release,
676 .fasync = pipe_rdwr_fasync,
679 static struct file_operations read_pipe_fops = {
686 .open = pipe_read_open,
687 .release = pipe_read_release,
688 .fasync = pipe_read_fasync,
691 static struct file_operations write_pipe_fops = {
695 .writev = pipe_writev,
698 .open = pipe_write_open,
699 .release = pipe_write_release,
700 .fasync = pipe_write_fasync,
703 static struct file_operations rdwr_pipe_fops = {
708 .writev = pipe_writev,
711 .open = pipe_rdwr_open,
712 .release = pipe_rdwr_release,
713 .fasync = pipe_rdwr_fasync,
716 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
718 struct pipe_inode_info *pipe;
720 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
722 init_waitqueue_head(&pipe->wait);
723 pipe->r_counter = pipe->w_counter = 1;
730 void __free_pipe_info(struct pipe_inode_info *pipe)
734 for (i = 0; i < PIPE_BUFFERS; i++) {
735 struct pipe_buffer *buf = pipe->bufs + i;
737 buf->ops->release(pipe, buf);
740 __free_page(pipe->tmp_page);
744 void free_pipe_info(struct inode *inode)
746 __free_pipe_info(inode->i_pipe);
747 inode->i_pipe = NULL;
750 static struct vfsmount *pipe_mnt __read_mostly;
751 static int pipefs_delete_dentry(struct dentry *dentry)
756 static struct dentry_operations pipefs_dentry_operations = {
757 .d_delete = pipefs_delete_dentry,
760 static struct inode * get_pipe_inode(void)
762 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
763 struct pipe_inode_info *pipe;
768 pipe = alloc_pipe_info(inode);
771 inode->i_pipe = pipe;
773 pipe->readers = pipe->writers = 1;
774 inode->i_fop = &rdwr_pipe_fops;
777 * Mark the inode dirty from the very beginning,
778 * that way it will never be moved to the dirty
779 * list because "mark_inode_dirty()" will think
780 * that it already _is_ on the dirty list.
782 inode->i_state = I_DIRTY;
783 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
784 inode->i_uid = current->fsuid;
785 inode->i_gid = current->fsgid;
786 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
787 inode->i_blksize = PAGE_SIZE;
802 struct dentry *dentry;
803 struct inode * inode;
804 struct file *f1, *f2;
809 f1 = get_empty_filp();
813 f2 = get_empty_filp();
817 inode = get_pipe_inode();
821 error = get_unused_fd();
823 goto close_f12_inode;
826 error = get_unused_fd();
828 goto close_f12_inode_i;
832 sprintf(name, "[%lu]", inode->i_ino);
834 this.len = strlen(name);
835 this.hash = inode->i_ino; /* will go */
836 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
838 goto close_f12_inode_i_j;
840 dentry->d_op = &pipefs_dentry_operations;
841 d_add(dentry, inode);
842 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
843 f1->f_dentry = f2->f_dentry = dget(dentry);
844 f1->f_mapping = f2->f_mapping = inode->i_mapping;
847 f1->f_pos = f2->f_pos = 0;
848 f1->f_flags = O_RDONLY;
849 f1->f_op = &read_pipe_fops;
850 f1->f_mode = FMODE_READ;
854 f2->f_flags = O_WRONLY;
855 f2->f_op = &write_pipe_fops;
856 f2->f_mode = FMODE_WRITE;
871 free_pipe_info(inode);
882 * pipefs should _never_ be mounted by userland - too much of security hassle,
883 * no real gain from having the whole whorehouse mounted. So we don't need
884 * any operations on the root directory. However, we need a non-trivial
885 * d_name - pipe: will go nicely and kill the special-casing in procfs.
888 static struct super_block *
889 pipefs_get_sb(struct file_system_type *fs_type, int flags,
890 const char *dev_name, void *data)
892 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
895 static struct file_system_type pipe_fs_type = {
897 .get_sb = pipefs_get_sb,
898 .kill_sb = kill_anon_super,
901 static int __init init_pipe_fs(void)
903 int err = register_filesystem(&pipe_fs_type);
906 pipe_mnt = kern_mount(&pipe_fs_type);
907 if (IS_ERR(pipe_mnt)) {
908 err = PTR_ERR(pipe_mnt);
909 unregister_filesystem(&pipe_fs_type);
915 static void __exit exit_pipe_fs(void)
917 unregister_filesystem(&pipe_fs_type);
921 fs_initcall(init_pipe_fs);
922 module_exit(exit_pipe_fs);