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;
229 total_len = iov_length(iov, nr_segs);
230 /* Null write succeeds. */
231 if (unlikely(total_len == 0))
236 down(PIPE_SEM(*inode));
237 info = inode->i_pipe;
239 if (!PIPE_READERS(*inode)) {
240 send_sig(SIGPIPE, current, 0);
245 /* We try to merge small writes */
246 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
247 if (info->nrbufs && chars != 0) {
248 int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1);
249 struct pipe_buffer *buf = info->bufs + lastbuf;
250 struct pipe_buf_operations *ops = buf->ops;
251 int offset = buf->offset + buf->len;
252 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
253 void *addr = ops->map(filp, info, buf);
254 int error = pipe_iov_copy_from_user(offset + addr, iov, chars);
255 ops->unmap(info, buf);
270 if (!PIPE_READERS(*inode)) {
271 send_sig(SIGPIPE, current, 0);
272 if (!ret) ret = -EPIPE;
276 if (bufs < PIPE_BUFFERS) {
277 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1);
278 struct pipe_buffer *buf = info->bufs + newbuf;
279 struct page *page = info->tmp_page;
283 page = alloc_page(GFP_HIGHUSER);
284 if (unlikely(!page)) {
285 ret = ret ? : -ENOMEM;
288 info->tmp_page = page;
290 /* Always wakeup, even if the copy fails. Otherwise
291 * we lock up (O_NONBLOCK-)readers that sleep due to
293 * FIXME! Is this really true?
297 if (chars > total_len)
300 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
302 if (unlikely(error)) {
303 if (!ret) ret = -EFAULT;
308 /* Insert it into the buffer array */
310 buf->ops = &anon_pipe_buf_ops;
313 info->nrbufs = ++bufs;
314 info->tmp_page = NULL;
320 if (bufs < PIPE_BUFFERS)
322 if (filp->f_flags & O_NONBLOCK) {
323 if (!ret) ret = -EAGAIN;
326 if (signal_pending(current)) {
327 if (!ret) ret = -ERESTARTSYS;
331 wake_up_interruptible_sync(PIPE_WAIT(*inode));
332 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
335 PIPE_WAITING_WRITERS(*inode)++;
337 PIPE_WAITING_WRITERS(*inode)--;
340 up(PIPE_SEM(*inode));
342 wake_up_interruptible(PIPE_WAIT(*inode));
343 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
346 inode_update_time(inode, 1); /* mtime and ctime */
351 pipe_write(struct file *filp, const char __user *buf,
352 size_t count, loff_t *ppos)
354 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
355 return pipe_writev(filp, &iov, 1, ppos);
359 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
365 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
371 pipe_ioctl(struct inode *pino, struct file *filp,
372 unsigned int cmd, unsigned long arg)
374 struct inode *inode = filp->f_dentry->d_inode;
375 struct pipe_inode_info *info;
376 int count, buf, nrbufs;
380 down(PIPE_SEM(*inode));
381 info = inode->i_pipe;
384 nrbufs = info->nrbufs;
385 while (--nrbufs >= 0) {
386 count += info->bufs[buf].len;
387 buf = (buf+1) & (PIPE_BUFFERS-1);
389 up(PIPE_SEM(*inode));
390 return put_user(count, (int __user *)arg);
396 /* No kernel lock held - fine */
398 pipe_poll(struct file *filp, poll_table *wait)
401 struct inode *inode = filp->f_dentry->d_inode;
402 struct pipe_inode_info *info = inode->i_pipe;
405 poll_wait(filp, PIPE_WAIT(*inode), wait);
407 /* Reading only -- no need for acquiring the semaphore. */
408 nrbufs = info->nrbufs;
410 if (filp->f_mode & FMODE_READ) {
411 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
412 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
416 if (filp->f_mode & FMODE_WRITE) {
417 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
418 if (!PIPE_READERS(*inode))
425 /* FIXME: most Unices do not set POLLERR for fifos */
426 #define fifo_poll pipe_poll
429 pipe_release(struct inode *inode, int decr, int decw)
431 down(PIPE_SEM(*inode));
432 PIPE_READERS(*inode) -= decr;
433 PIPE_WRITERS(*inode) -= decw;
434 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
435 free_pipe_info(inode);
437 wake_up_interruptible(PIPE_WAIT(*inode));
438 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
439 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
441 up(PIPE_SEM(*inode));
447 pipe_read_fasync(int fd, struct file *filp, int on)
449 struct inode *inode = filp->f_dentry->d_inode;
452 down(PIPE_SEM(*inode));
453 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
454 up(PIPE_SEM(*inode));
464 pipe_write_fasync(int fd, struct file *filp, int on)
466 struct inode *inode = filp->f_dentry->d_inode;
469 down(PIPE_SEM(*inode));
470 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
471 up(PIPE_SEM(*inode));
481 pipe_rdwr_fasync(int fd, struct file *filp, int on)
483 struct inode *inode = filp->f_dentry->d_inode;
486 down(PIPE_SEM(*inode));
488 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
491 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
493 up(PIPE_SEM(*inode));
503 pipe_read_release(struct inode *inode, struct file *filp)
505 pipe_read_fasync(-1, filp, 0);
506 return pipe_release(inode, 1, 0);
510 pipe_write_release(struct inode *inode, struct file *filp)
512 pipe_write_fasync(-1, filp, 0);
513 return pipe_release(inode, 0, 1);
517 pipe_rdwr_release(struct inode *inode, struct file *filp)
521 pipe_rdwr_fasync(-1, filp, 0);
522 decr = (filp->f_mode & FMODE_READ) != 0;
523 decw = (filp->f_mode & FMODE_WRITE) != 0;
524 return pipe_release(inode, decr, decw);
528 pipe_read_open(struct inode *inode, struct file *filp)
530 /* We could have perhaps used atomic_t, but this and friends
531 below are the only places. So it doesn't seem worthwhile. */
532 down(PIPE_SEM(*inode));
533 PIPE_READERS(*inode)++;
534 up(PIPE_SEM(*inode));
540 pipe_write_open(struct inode *inode, struct file *filp)
542 down(PIPE_SEM(*inode));
543 PIPE_WRITERS(*inode)++;
544 up(PIPE_SEM(*inode));
550 pipe_rdwr_open(struct inode *inode, struct file *filp)
552 down(PIPE_SEM(*inode));
553 if (filp->f_mode & FMODE_READ)
554 PIPE_READERS(*inode)++;
555 if (filp->f_mode & FMODE_WRITE)
556 PIPE_WRITERS(*inode)++;
557 up(PIPE_SEM(*inode));
563 * The file_operations structs are not static because they
564 * are also used in linux/fs/fifo.c to do operations on FIFOs.
566 struct file_operations read_fifo_fops = {
573 .open = pipe_read_open,
574 .release = pipe_read_release,
575 .fasync = pipe_read_fasync,
578 struct file_operations write_fifo_fops = {
582 .writev = pipe_writev,
585 .open = pipe_write_open,
586 .release = pipe_write_release,
587 .fasync = pipe_write_fasync,
590 struct file_operations rdwr_fifo_fops = {
595 .writev = pipe_writev,
598 .open = pipe_rdwr_open,
599 .release = pipe_rdwr_release,
600 .fasync = pipe_rdwr_fasync,
603 struct file_operations read_pipe_fops = {
610 .open = pipe_read_open,
611 .release = pipe_read_release,
612 .fasync = pipe_read_fasync,
615 struct file_operations write_pipe_fops = {
619 .writev = pipe_writev,
622 .open = pipe_write_open,
623 .release = pipe_write_release,
624 .fasync = pipe_write_fasync,
627 struct file_operations rdwr_pipe_fops = {
632 .writev = pipe_writev,
635 .open = pipe_rdwr_open,
636 .release = pipe_rdwr_release,
637 .fasync = pipe_rdwr_fasync,
640 void free_pipe_info(struct inode *inode)
643 struct pipe_inode_info *info = inode->i_pipe;
645 inode->i_pipe = NULL;
646 for (i = 0; i < PIPE_BUFFERS; i++) {
647 struct pipe_buffer *buf = info->bufs + i;
649 buf->ops->release(info, buf);
652 __free_page(info->tmp_page);
656 struct inode* pipe_new(struct inode* inode)
658 struct pipe_inode_info *info;
660 info = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
663 memset(info, 0, sizeof(*info));
664 inode->i_pipe = info;
666 init_waitqueue_head(PIPE_WAIT(*inode));
667 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
674 static struct vfsmount *pipe_mnt;
675 static int pipefs_delete_dentry(struct dentry *dentry)
679 static struct dentry_operations pipefs_dentry_operations = {
680 .d_delete = pipefs_delete_dentry,
683 static struct inode * get_pipe_inode(void)
685 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
692 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
693 inode->i_fop = &rdwr_pipe_fops;
696 * Mark the inode dirty from the very beginning,
697 * that way it will never be moved to the dirty
698 * list because "mark_inode_dirty()" will think
699 * that it already _is_ on the dirty list.
701 inode->i_state = I_DIRTY;
702 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
703 inode->i_uid = current->fsuid;
704 inode->i_gid = current->fsgid;
705 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
706 inode->i_blksize = PAGE_SIZE;
719 struct dentry *dentry;
720 struct inode * inode;
721 struct file *f1, *f2;
726 f1 = get_empty_filp();
730 f2 = get_empty_filp();
734 inode = get_pipe_inode();
738 error = get_unused_fd();
740 goto close_f12_inode;
743 error = get_unused_fd();
745 goto close_f12_inode_i;
749 sprintf(name, "[%lu]", inode->i_ino);
751 this.len = strlen(name);
752 this.hash = inode->i_ino; /* will go */
753 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
755 goto close_f12_inode_i_j;
756 dentry->d_op = &pipefs_dentry_operations;
757 d_add(dentry, inode);
758 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
759 f1->f_dentry = f2->f_dentry = dget(dentry);
760 f1->f_mapping = f2->f_mapping = inode->i_mapping;
763 f1->f_pos = f2->f_pos = 0;
764 f1->f_flags = O_RDONLY;
765 f1->f_op = &read_pipe_fops;
766 f1->f_mode = FMODE_READ;
770 f2->f_flags = O_WRONLY;
771 f2->f_op = &write_pipe_fops;
772 f2->f_mode = FMODE_WRITE;
786 free_pipe_info(inode);
797 * pipefs should _never_ be mounted by userland - too much of security hassle,
798 * no real gain from having the whole whorehouse mounted. So we don't need
799 * any operations on the root directory. However, we need a non-trivial
800 * d_name - pipe: will go nicely and kill the special-casing in procfs.
803 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
804 int flags, const char *dev_name, void *data)
806 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
809 static struct file_system_type pipe_fs_type = {
811 .get_sb = pipefs_get_sb,
812 .kill_sb = kill_anon_super,
815 static int __init init_pipe_fs(void)
817 int err = register_filesystem(&pipe_fs_type);
819 pipe_mnt = kern_mount(&pipe_fs_type);
820 if (IS_ERR(pipe_mnt)) {
821 err = PTR_ERR(pipe_mnt);
822 unregister_filesystem(&pipe_fs_type);
828 static void __exit exit_pipe_fs(void)
830 unregister_filesystem(&pipe_fs_type);
834 fs_initcall(init_pipe_fs);
835 module_exit(exit_pipe_fs);