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)
42 prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE);
45 finish_wait(PIPE_WAIT(*inode), &wait);
46 down(PIPE_SEM(*inode));
50 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
57 copy = min_t(unsigned long, len, iov->iov_len);
59 if (copy_from_user(to, iov->iov_base, copy))
63 iov->iov_base += copy;
70 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
77 copy = min_t(unsigned long, len, iov->iov_len);
79 if (copy_to_user(iov->iov_base, from, copy))
83 iov->iov_base += copy;
89 static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf)
91 struct page *page = buf->page;
97 info->tmp_page = page;
100 static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf)
102 return kmap(buf->page);
105 static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf)
110 static struct pipe_buf_operations anon_pipe_buf_ops = {
112 .map = anon_pipe_buf_map,
113 .unmap = anon_pipe_buf_unmap,
114 .release = anon_pipe_buf_release,
118 pipe_readv(struct file *filp, const struct iovec *_iov,
119 unsigned long nr_segs, loff_t *ppos)
121 struct inode *inode = filp->f_dentry->d_inode;
122 struct pipe_inode_info *info;
125 struct iovec *iov = (struct iovec *)_iov;
128 total_len = iov_length(iov, nr_segs);
129 /* Null read succeeds. */
130 if (unlikely(total_len == 0))
135 down(PIPE_SEM(*inode));
136 info = inode->i_pipe;
138 int bufs = info->nrbufs;
140 int curbuf = info->curbuf;
141 struct pipe_buffer *buf = info->bufs + curbuf;
142 struct pipe_buf_operations *ops = buf->ops;
144 size_t chars = buf->len;
147 if (chars > total_len)
150 addr = ops->map(filp, info, buf);
151 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
152 ops->unmap(info, buf);
153 if (unlikely(error)) {
154 if (!ret) ret = -EFAULT;
158 buf->offset += chars;
162 ops->release(info, buf);
163 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
164 info->curbuf = curbuf;
165 info->nrbufs = --bufs;
170 break; /* common path: read succeeded */
172 if (bufs) /* More to do? */
174 if (!PIPE_WRITERS(*inode))
176 if (!PIPE_WAITING_WRITERS(*inode)) {
177 /* syscall merging: Usually we must not sleep
178 * if O_NONBLOCK is set, or if we got some data.
179 * But if a writer sleeps in kernel space, then
180 * we can wait for that data without violating POSIX.
184 if (filp->f_flags & O_NONBLOCK) {
189 if (signal_pending(current)) {
190 if (!ret) ret = -ERESTARTSYS;
194 wake_up_interruptible_sync(PIPE_WAIT(*inode));
195 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
199 up(PIPE_SEM(*inode));
200 /* Signal writers asynchronously that there is more room. */
202 wake_up_interruptible(PIPE_WAIT(*inode));
203 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
211 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
213 struct iovec iov = { .iov_base = buf, .iov_len = count };
214 return pipe_readv(filp, &iov, 1, ppos);
218 pipe_writev(struct file *filp, const struct iovec *_iov,
219 unsigned long nr_segs, loff_t *ppos)
221 struct inode *inode = filp->f_dentry->d_inode;
222 struct pipe_inode_info *info;
225 struct iovec *iov = (struct iovec *)_iov;
228 total_len = iov_length(iov, nr_segs);
229 /* Null write succeeds. */
230 if (unlikely(total_len == 0))
235 down(PIPE_SEM(*inode));
236 info = inode->i_pipe;
238 if (!PIPE_READERS(*inode)) {
239 send_sig(SIGPIPE, current, 0);
244 /* We try to merge small writes */
245 if (info->nrbufs && total_len < PAGE_SIZE) {
246 int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1);
247 struct pipe_buffer *buf = info->bufs + lastbuf;
248 struct pipe_buf_operations *ops = buf->ops;
249 int offset = buf->offset + buf->len;
250 if (ops->can_merge && offset + total_len <= PAGE_SIZE) {
251 void *addr = ops->map(filp, info, buf);
252 int error = pipe_iov_copy_from_user(offset + addr, iov, total_len);
253 ops->unmap(info, buf);
258 buf->len += total_len;
267 if (!PIPE_READERS(*inode)) {
268 send_sig(SIGPIPE, current, 0);
269 if (!ret) ret = -EPIPE;
273 if (bufs < PIPE_BUFFERS) {
275 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1);
276 struct pipe_buffer *buf = info->bufs + newbuf;
277 struct page *page = info->tmp_page;
281 page = alloc_page(GFP_HIGHUSER);
282 if (unlikely(!page)) {
283 ret = ret ? : -ENOMEM;
286 info->tmp_page = page;
288 /* Always wakeup, even if the copy fails. Otherwise
289 * we lock up (O_NONBLOCK-)readers that sleep due to
291 * FIXME! Is this really true?
295 if (chars > total_len)
298 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
300 if (unlikely(error)) {
301 if (!ret) ret = -EFAULT;
306 /* Insert it into the buffer array */
308 buf->ops = &anon_pipe_buf_ops;
311 info->nrbufs = ++bufs;
312 info->tmp_page = NULL;
318 if (bufs < PIPE_BUFFERS)
320 if (filp->f_flags & O_NONBLOCK) {
321 if (!ret) ret = -EAGAIN;
324 if (signal_pending(current)) {
325 if (!ret) ret = -ERESTARTSYS;
329 wake_up_interruptible_sync(PIPE_WAIT(*inode));
330 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
333 PIPE_WAITING_WRITERS(*inode)++;
335 PIPE_WAITING_WRITERS(*inode)--;
338 up(PIPE_SEM(*inode));
340 wake_up_interruptible(PIPE_WAIT(*inode));
341 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
344 inode_update_time(inode, 1); /* mtime and ctime */
349 pipe_write(struct file *filp, const char __user *buf,
350 size_t count, loff_t *ppos)
352 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
353 return pipe_writev(filp, &iov, 1, ppos);
357 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
363 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
369 pipe_ioctl(struct inode *pino, struct file *filp,
370 unsigned int cmd, unsigned long arg)
372 struct inode *inode = filp->f_dentry->d_inode;
373 struct pipe_inode_info *info;
374 int count, buf, nrbufs;
378 down(PIPE_SEM(*inode));
379 info = inode->i_pipe;
382 nrbufs = info->nrbufs;
383 while (--nrbufs >= 0) {
384 count += info->bufs[buf].len;
385 buf = (buf+1) & (PIPE_BUFFERS-1);
387 up(PIPE_SEM(*inode));
388 return put_user(count, (int __user *)arg);
394 /* No kernel lock held - fine */
396 pipe_poll(struct file *filp, poll_table *wait)
399 struct inode *inode = filp->f_dentry->d_inode;
400 struct pipe_inode_info *info = inode->i_pipe;
403 poll_wait(filp, PIPE_WAIT(*inode), wait);
405 /* Reading only -- no need for acquiring the semaphore. */
406 nrbufs = info->nrbufs;
408 if (filp->f_mode & FMODE_READ) {
409 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
410 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
414 if (filp->f_mode & FMODE_WRITE) {
415 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
416 if (!PIPE_READERS(*inode))
423 /* FIXME: most Unices do not set POLLERR for fifos */
424 #define fifo_poll pipe_poll
427 pipe_release(struct inode *inode, int decr, int decw)
429 down(PIPE_SEM(*inode));
430 PIPE_READERS(*inode) -= decr;
431 PIPE_WRITERS(*inode) -= decw;
432 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
433 free_pipe_info(inode);
435 wake_up_interruptible(PIPE_WAIT(*inode));
436 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
437 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
439 up(PIPE_SEM(*inode));
445 pipe_read_fasync(int fd, struct file *filp, int on)
447 struct inode *inode = filp->f_dentry->d_inode;
450 down(PIPE_SEM(*inode));
451 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
452 up(PIPE_SEM(*inode));
462 pipe_write_fasync(int fd, struct file *filp, int on)
464 struct inode *inode = filp->f_dentry->d_inode;
467 down(PIPE_SEM(*inode));
468 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
469 up(PIPE_SEM(*inode));
479 pipe_rdwr_fasync(int fd, struct file *filp, int on)
481 struct inode *inode = filp->f_dentry->d_inode;
484 down(PIPE_SEM(*inode));
486 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
489 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
491 up(PIPE_SEM(*inode));
501 pipe_read_release(struct inode *inode, struct file *filp)
503 pipe_read_fasync(-1, filp, 0);
504 return pipe_release(inode, 1, 0);
508 pipe_write_release(struct inode *inode, struct file *filp)
510 pipe_write_fasync(-1, filp, 0);
511 return pipe_release(inode, 0, 1);
515 pipe_rdwr_release(struct inode *inode, struct file *filp)
519 pipe_rdwr_fasync(-1, filp, 0);
520 decr = (filp->f_mode & FMODE_READ) != 0;
521 decw = (filp->f_mode & FMODE_WRITE) != 0;
522 return pipe_release(inode, decr, decw);
526 pipe_read_open(struct inode *inode, struct file *filp)
528 /* We could have perhaps used atomic_t, but this and friends
529 below are the only places. So it doesn't seem worthwhile. */
530 down(PIPE_SEM(*inode));
531 PIPE_READERS(*inode)++;
532 up(PIPE_SEM(*inode));
538 pipe_write_open(struct inode *inode, struct file *filp)
540 down(PIPE_SEM(*inode));
541 PIPE_WRITERS(*inode)++;
542 up(PIPE_SEM(*inode));
548 pipe_rdwr_open(struct inode *inode, struct file *filp)
550 down(PIPE_SEM(*inode));
551 if (filp->f_mode & FMODE_READ)
552 PIPE_READERS(*inode)++;
553 if (filp->f_mode & FMODE_WRITE)
554 PIPE_WRITERS(*inode)++;
555 up(PIPE_SEM(*inode));
561 * The file_operations structs are not static because they
562 * are also used in linux/fs/fifo.c to do operations on FIFOs.
564 struct file_operations read_fifo_fops = {
571 .open = pipe_read_open,
572 .release = pipe_read_release,
573 .fasync = pipe_read_fasync,
576 struct file_operations write_fifo_fops = {
580 .writev = pipe_writev,
583 .open = pipe_write_open,
584 .release = pipe_write_release,
585 .fasync = pipe_write_fasync,
588 struct file_operations rdwr_fifo_fops = {
593 .writev = pipe_writev,
596 .open = pipe_rdwr_open,
597 .release = pipe_rdwr_release,
598 .fasync = pipe_rdwr_fasync,
601 struct file_operations read_pipe_fops = {
608 .open = pipe_read_open,
609 .release = pipe_read_release,
610 .fasync = pipe_read_fasync,
613 struct file_operations write_pipe_fops = {
617 .writev = pipe_writev,
620 .open = pipe_write_open,
621 .release = pipe_write_release,
622 .fasync = pipe_write_fasync,
625 struct file_operations rdwr_pipe_fops = {
630 .writev = pipe_writev,
633 .open = pipe_rdwr_open,
634 .release = pipe_rdwr_release,
635 .fasync = pipe_rdwr_fasync,
638 void free_pipe_info(struct inode *inode)
641 struct pipe_inode_info *info = inode->i_pipe;
643 inode->i_pipe = NULL;
644 for (i = 0; i < PIPE_BUFFERS; i++) {
645 struct pipe_buffer *buf = info->bufs + i;
647 buf->ops->release(info, buf);
650 __free_page(info->tmp_page);
654 struct inode* pipe_new(struct inode* inode)
656 struct pipe_inode_info *info;
658 info = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
661 memset(info, 0, sizeof(*info));
662 inode->i_pipe = info;
664 init_waitqueue_head(PIPE_WAIT(*inode));
665 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
672 static struct vfsmount *pipe_mnt;
673 static int pipefs_delete_dentry(struct dentry *dentry)
677 static struct dentry_operations pipefs_dentry_operations = {
678 .d_delete = pipefs_delete_dentry,
681 static struct inode * get_pipe_inode(void)
683 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
690 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
691 inode->i_fop = &rdwr_pipe_fops;
694 * Mark the inode dirty from the very beginning,
695 * that way it will never be moved to the dirty
696 * list because "mark_inode_dirty()" will think
697 * that it already _is_ on the dirty list.
699 inode->i_state = I_DIRTY;
700 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
701 inode->i_uid = current->fsuid;
702 inode->i_gid = current->fsgid;
703 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
704 inode->i_blksize = PAGE_SIZE;
717 struct dentry *dentry;
718 struct inode * inode;
719 struct file *f1, *f2;
724 f1 = get_empty_filp();
728 f2 = get_empty_filp();
732 inode = get_pipe_inode();
736 error = get_unused_fd();
738 goto close_f12_inode;
741 error = get_unused_fd();
743 goto close_f12_inode_i;
747 sprintf(name, "[%lu]", inode->i_ino);
749 this.len = strlen(name);
750 this.hash = inode->i_ino; /* will go */
751 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
753 goto close_f12_inode_i_j;
754 dentry->d_op = &pipefs_dentry_operations;
755 d_add(dentry, inode);
756 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
757 f1->f_dentry = f2->f_dentry = dget(dentry);
758 f1->f_mapping = f2->f_mapping = inode->i_mapping;
761 f1->f_pos = f2->f_pos = 0;
762 f1->f_flags = O_RDONLY;
763 f1->f_op = &read_pipe_fops;
764 f1->f_mode = FMODE_READ;
768 f2->f_flags = O_WRONLY;
769 f2->f_op = &write_pipe_fops;
770 f2->f_mode = FMODE_WRITE;
784 free_pipe_info(inode);
795 * pipefs should _never_ be mounted by userland - too much of security hassle,
796 * no real gain from having the whole whorehouse mounted. So we don't need
797 * any operations on the root directory. However, we need a non-trivial
798 * d_name - pipe: will go nicely and kill the special-casing in procfs.
801 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
802 int flags, const char *dev_name, void *data)
804 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
807 static struct file_system_type pipe_fs_type = {
809 .get_sb = pipefs_get_sb,
810 .kill_sb = kill_anon_super,
813 static int __init init_pipe_fs(void)
815 int err = register_filesystem(&pipe_fs_type);
817 pipe_mnt = kern_mount(&pipe_fs_type);
818 if (IS_ERR(pipe_mnt)) {
819 err = PTR_ERR(pipe_mnt);
820 unregister_filesystem(&pipe_fs_type);
826 static void __exit exit_pipe_fs(void)
828 unregister_filesystem(&pipe_fs_type);
832 fs_initcall(init_pipe_fs);
833 module_exit(exit_pipe_fs);