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;
419 * Most Unices do not set POLLERR for FIFOs but on Linux they
420 * behave exactly like pipes for poll().
422 if (!PIPE_READERS(*inode))
430 pipe_release(struct inode *inode, int decr, int decw)
432 down(PIPE_SEM(*inode));
433 PIPE_READERS(*inode) -= decr;
434 PIPE_WRITERS(*inode) -= decw;
435 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
436 free_pipe_info(inode);
438 wake_up_interruptible(PIPE_WAIT(*inode));
439 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
440 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
442 up(PIPE_SEM(*inode));
448 pipe_read_fasync(int fd, struct file *filp, int on)
450 struct inode *inode = filp->f_dentry->d_inode;
453 down(PIPE_SEM(*inode));
454 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
455 up(PIPE_SEM(*inode));
465 pipe_write_fasync(int fd, struct file *filp, int on)
467 struct inode *inode = filp->f_dentry->d_inode;
470 down(PIPE_SEM(*inode));
471 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
472 up(PIPE_SEM(*inode));
482 pipe_rdwr_fasync(int fd, struct file *filp, int on)
484 struct inode *inode = filp->f_dentry->d_inode;
487 down(PIPE_SEM(*inode));
489 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
492 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
494 up(PIPE_SEM(*inode));
504 pipe_read_release(struct inode *inode, struct file *filp)
506 pipe_read_fasync(-1, filp, 0);
507 return pipe_release(inode, 1, 0);
511 pipe_write_release(struct inode *inode, struct file *filp)
513 pipe_write_fasync(-1, filp, 0);
514 return pipe_release(inode, 0, 1);
518 pipe_rdwr_release(struct inode *inode, struct file *filp)
522 pipe_rdwr_fasync(-1, filp, 0);
523 decr = (filp->f_mode & FMODE_READ) != 0;
524 decw = (filp->f_mode & FMODE_WRITE) != 0;
525 return pipe_release(inode, decr, decw);
529 pipe_read_open(struct inode *inode, struct file *filp)
531 /* We could have perhaps used atomic_t, but this and friends
532 below are the only places. So it doesn't seem worthwhile. */
533 down(PIPE_SEM(*inode));
534 PIPE_READERS(*inode)++;
535 up(PIPE_SEM(*inode));
541 pipe_write_open(struct inode *inode, struct file *filp)
543 down(PIPE_SEM(*inode));
544 PIPE_WRITERS(*inode)++;
545 up(PIPE_SEM(*inode));
551 pipe_rdwr_open(struct inode *inode, struct file *filp)
553 down(PIPE_SEM(*inode));
554 if (filp->f_mode & FMODE_READ)
555 PIPE_READERS(*inode)++;
556 if (filp->f_mode & FMODE_WRITE)
557 PIPE_WRITERS(*inode)++;
558 up(PIPE_SEM(*inode));
564 * The file_operations structs are not static because they
565 * are also used in linux/fs/fifo.c to do operations on FIFOs.
567 struct file_operations read_fifo_fops = {
574 .open = pipe_read_open,
575 .release = pipe_read_release,
576 .fasync = pipe_read_fasync,
579 struct file_operations write_fifo_fops = {
583 .writev = pipe_writev,
586 .open = pipe_write_open,
587 .release = pipe_write_release,
588 .fasync = pipe_write_fasync,
591 struct file_operations rdwr_fifo_fops = {
596 .writev = pipe_writev,
599 .open = pipe_rdwr_open,
600 .release = pipe_rdwr_release,
601 .fasync = pipe_rdwr_fasync,
604 struct file_operations read_pipe_fops = {
611 .open = pipe_read_open,
612 .release = pipe_read_release,
613 .fasync = pipe_read_fasync,
616 struct file_operations write_pipe_fops = {
620 .writev = pipe_writev,
623 .open = pipe_write_open,
624 .release = pipe_write_release,
625 .fasync = pipe_write_fasync,
628 struct file_operations rdwr_pipe_fops = {
633 .writev = pipe_writev,
636 .open = pipe_rdwr_open,
637 .release = pipe_rdwr_release,
638 .fasync = pipe_rdwr_fasync,
641 void free_pipe_info(struct inode *inode)
644 struct pipe_inode_info *info = inode->i_pipe;
646 inode->i_pipe = NULL;
647 for (i = 0; i < PIPE_BUFFERS; i++) {
648 struct pipe_buffer *buf = info->bufs + i;
650 buf->ops->release(info, buf);
653 __free_page(info->tmp_page);
657 struct inode* pipe_new(struct inode* inode)
659 struct pipe_inode_info *info;
661 info = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
664 memset(info, 0, sizeof(*info));
665 inode->i_pipe = info;
667 init_waitqueue_head(PIPE_WAIT(*inode));
668 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
675 static struct vfsmount *pipe_mnt;
676 static int pipefs_delete_dentry(struct dentry *dentry)
680 static struct dentry_operations pipefs_dentry_operations = {
681 .d_delete = pipefs_delete_dentry,
684 static struct inode * get_pipe_inode(void)
686 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
693 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
694 inode->i_fop = &rdwr_pipe_fops;
697 * Mark the inode dirty from the very beginning,
698 * that way it will never be moved to the dirty
699 * list because "mark_inode_dirty()" will think
700 * that it already _is_ on the dirty list.
702 inode->i_state = I_DIRTY;
703 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
704 inode->i_uid = current->fsuid;
705 inode->i_gid = current->fsgid;
706 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
707 inode->i_blksize = PAGE_SIZE;
720 struct dentry *dentry;
721 struct inode * inode;
722 struct file *f1, *f2;
727 f1 = get_empty_filp();
731 f2 = get_empty_filp();
735 inode = get_pipe_inode();
739 error = get_unused_fd();
741 goto close_f12_inode;
744 error = get_unused_fd();
746 goto close_f12_inode_i;
750 sprintf(name, "[%lu]", inode->i_ino);
752 this.len = strlen(name);
753 this.hash = inode->i_ino; /* will go */
754 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
756 goto close_f12_inode_i_j;
757 dentry->d_op = &pipefs_dentry_operations;
758 d_add(dentry, inode);
759 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
760 f1->f_dentry = f2->f_dentry = dget(dentry);
761 f1->f_mapping = f2->f_mapping = inode->i_mapping;
764 f1->f_pos = f2->f_pos = 0;
765 f1->f_flags = O_RDONLY;
766 f1->f_op = &read_pipe_fops;
767 f1->f_mode = FMODE_READ;
771 f2->f_flags = O_WRONLY;
772 f2->f_op = &write_pipe_fops;
773 f2->f_mode = FMODE_WRITE;
787 free_pipe_info(inode);
798 * pipefs should _never_ be mounted by userland - too much of security hassle,
799 * no real gain from having the whole whorehouse mounted. So we don't need
800 * any operations on the root directory. However, we need a non-trivial
801 * d_name - pipe: will go nicely and kill the special-casing in procfs.
804 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
805 int flags, const char *dev_name, void *data)
807 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
810 static struct file_system_type pipe_fs_type = {
812 .get_sb = pipefs_get_sb,
813 .kill_sb = kill_anon_super,
816 static int __init init_pipe_fs(void)
818 int err = register_filesystem(&pipe_fs_type);
820 pipe_mnt = kern_mount(&pipe_fs_type);
821 if (IS_ERR(pipe_mnt)) {
822 err = PTR_ERR(pipe_mnt);
823 unregister_filesystem(&pipe_fs_type);
829 static void __exit exit_pipe_fs(void)
831 unregister_filesystem(&pipe_fs_type);
835 fs_initcall(init_pipe_fs);
836 module_exit(exit_pipe_fs);