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 inode * inode)
44 * Pipes are system-local resources, so sleeping on them
45 * is considered a noninteractive wait:
47 prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
48 mutex_unlock(PIPE_MUTEX(*inode));
50 finish_wait(PIPE_WAIT(*inode), &wait);
51 mutex_lock(PIPE_MUTEX(*inode));
55 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
62 copy = min_t(unsigned long, len, iov->iov_len);
64 if (copy_from_user(to, iov->iov_base, copy))
68 iov->iov_base += copy;
75 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
82 copy = min_t(unsigned long, len, iov->iov_len);
84 if (copy_to_user(iov->iov_base, from, copy))
88 iov->iov_base += copy;
94 static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf)
96 struct page *page = buf->page;
99 * If nobody else uses this page, and we don't already have a
100 * temporary page, let's keep track of it as a one-deep
103 if (page_count(page) == 1 && !info->tmp_page) {
104 info->tmp_page = page;
109 * Otherwise just release our reference to it
111 page_cache_release(page);
114 static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf)
116 return kmap(buf->page);
119 static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf)
124 static int anon_pipe_buf_steal(struct pipe_inode_info *info,
125 struct pipe_buffer *buf)
130 static struct pipe_buf_operations anon_pipe_buf_ops = {
132 .map = anon_pipe_buf_map,
133 .unmap = anon_pipe_buf_unmap,
134 .release = anon_pipe_buf_release,
135 .steal = anon_pipe_buf_steal,
139 pipe_readv(struct file *filp, const struct iovec *_iov,
140 unsigned long nr_segs, loff_t *ppos)
142 struct inode *inode = filp->f_dentry->d_inode;
143 struct pipe_inode_info *info;
146 struct iovec *iov = (struct iovec *)_iov;
149 total_len = iov_length(iov, nr_segs);
150 /* Null read succeeds. */
151 if (unlikely(total_len == 0))
156 mutex_lock(PIPE_MUTEX(*inode));
157 info = inode->i_pipe;
159 int bufs = info->nrbufs;
161 int curbuf = info->curbuf;
162 struct pipe_buffer *buf = info->bufs + curbuf;
163 struct pipe_buf_operations *ops = buf->ops;
165 size_t chars = buf->len;
168 if (chars > total_len)
171 addr = ops->map(filp, info, buf);
177 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
178 ops->unmap(info, buf);
179 if (unlikely(error)) {
180 if (!ret) ret = -EFAULT;
184 buf->offset += chars;
188 ops->release(info, buf);
189 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
190 info->curbuf = curbuf;
191 info->nrbufs = --bufs;
196 break; /* common path: read succeeded */
198 if (bufs) /* More to do? */
200 if (!PIPE_WRITERS(*inode))
202 if (!PIPE_WAITING_WRITERS(*inode)) {
203 /* syscall merging: Usually we must not sleep
204 * if O_NONBLOCK is set, or if we got some data.
205 * But if a writer sleeps in kernel space, then
206 * we can wait for that data without violating POSIX.
210 if (filp->f_flags & O_NONBLOCK) {
215 if (signal_pending(current)) {
216 if (!ret) ret = -ERESTARTSYS;
220 wake_up_interruptible_sync(PIPE_WAIT(*inode));
221 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
225 mutex_unlock(PIPE_MUTEX(*inode));
226 /* Signal writers asynchronously that there is more room. */
228 wake_up_interruptible(PIPE_WAIT(*inode));
229 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
237 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
239 struct iovec iov = { .iov_base = buf, .iov_len = count };
240 return pipe_readv(filp, &iov, 1, ppos);
244 pipe_writev(struct file *filp, const struct iovec *_iov,
245 unsigned long nr_segs, loff_t *ppos)
247 struct inode *inode = filp->f_dentry->d_inode;
248 struct pipe_inode_info *info;
251 struct iovec *iov = (struct iovec *)_iov;
255 total_len = iov_length(iov, nr_segs);
256 /* Null write succeeds. */
257 if (unlikely(total_len == 0))
262 mutex_lock(PIPE_MUTEX(*inode));
263 info = inode->i_pipe;
265 if (!PIPE_READERS(*inode)) {
266 send_sig(SIGPIPE, current, 0);
271 /* We try to merge small writes */
272 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
273 if (info->nrbufs && chars != 0) {
274 int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1);
275 struct pipe_buffer *buf = info->bufs + lastbuf;
276 struct pipe_buf_operations *ops = buf->ops;
277 int offset = buf->offset + buf->len;
278 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
282 addr = ops->map(filp, info, buf);
284 error = PTR_ERR(addr);
287 error = pipe_iov_copy_from_user(offset + addr, iov,
289 ops->unmap(info, buf);
304 if (!PIPE_READERS(*inode)) {
305 send_sig(SIGPIPE, current, 0);
306 if (!ret) ret = -EPIPE;
310 if (bufs < PIPE_BUFFERS) {
311 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1);
312 struct pipe_buffer *buf = info->bufs + newbuf;
313 struct page *page = info->tmp_page;
317 page = alloc_page(GFP_HIGHUSER);
318 if (unlikely(!page)) {
319 ret = ret ? : -ENOMEM;
322 info->tmp_page = page;
324 /* Always wakeup, even if the copy fails. Otherwise
325 * we lock up (O_NONBLOCK-)readers that sleep due to
327 * FIXME! Is this really true?
331 if (chars > total_len)
334 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
336 if (unlikely(error)) {
337 if (!ret) ret = -EFAULT;
342 /* Insert it into the buffer array */
344 buf->ops = &anon_pipe_buf_ops;
347 info->nrbufs = ++bufs;
348 info->tmp_page = NULL;
354 if (bufs < PIPE_BUFFERS)
356 if (filp->f_flags & O_NONBLOCK) {
357 if (!ret) ret = -EAGAIN;
360 if (signal_pending(current)) {
361 if (!ret) ret = -ERESTARTSYS;
365 wake_up_interruptible_sync(PIPE_WAIT(*inode));
366 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
369 PIPE_WAITING_WRITERS(*inode)++;
371 PIPE_WAITING_WRITERS(*inode)--;
374 mutex_unlock(PIPE_MUTEX(*inode));
376 wake_up_interruptible(PIPE_WAIT(*inode));
377 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
380 file_update_time(filp);
385 pipe_write(struct file *filp, const char __user *buf,
386 size_t count, loff_t *ppos)
388 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
389 return pipe_writev(filp, &iov, 1, ppos);
393 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
399 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
405 pipe_ioctl(struct inode *pino, struct file *filp,
406 unsigned int cmd, unsigned long arg)
408 struct inode *inode = filp->f_dentry->d_inode;
409 struct pipe_inode_info *info;
410 int count, buf, nrbufs;
414 mutex_lock(PIPE_MUTEX(*inode));
415 info = inode->i_pipe;
418 nrbufs = info->nrbufs;
419 while (--nrbufs >= 0) {
420 count += info->bufs[buf].len;
421 buf = (buf+1) & (PIPE_BUFFERS-1);
423 mutex_unlock(PIPE_MUTEX(*inode));
424 return put_user(count, (int __user *)arg);
430 /* No kernel lock held - fine */
432 pipe_poll(struct file *filp, poll_table *wait)
435 struct inode *inode = filp->f_dentry->d_inode;
436 struct pipe_inode_info *info = inode->i_pipe;
439 poll_wait(filp, PIPE_WAIT(*inode), wait);
441 /* Reading only -- no need for acquiring the semaphore. */
442 nrbufs = info->nrbufs;
444 if (filp->f_mode & FMODE_READ) {
445 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
446 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
450 if (filp->f_mode & FMODE_WRITE) {
451 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
453 * Most Unices do not set POLLERR for FIFOs but on Linux they
454 * behave exactly like pipes for poll().
456 if (!PIPE_READERS(*inode))
464 pipe_release(struct inode *inode, int decr, int decw)
466 mutex_lock(PIPE_MUTEX(*inode));
467 PIPE_READERS(*inode) -= decr;
468 PIPE_WRITERS(*inode) -= decw;
469 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
470 free_pipe_info(inode);
472 wake_up_interruptible(PIPE_WAIT(*inode));
473 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
474 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
476 mutex_unlock(PIPE_MUTEX(*inode));
482 pipe_read_fasync(int fd, struct file *filp, int on)
484 struct inode *inode = filp->f_dentry->d_inode;
487 mutex_lock(PIPE_MUTEX(*inode));
488 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
489 mutex_unlock(PIPE_MUTEX(*inode));
499 pipe_write_fasync(int fd, struct file *filp, int on)
501 struct inode *inode = filp->f_dentry->d_inode;
504 mutex_lock(PIPE_MUTEX(*inode));
505 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
506 mutex_unlock(PIPE_MUTEX(*inode));
516 pipe_rdwr_fasync(int fd, struct file *filp, int on)
518 struct inode *inode = filp->f_dentry->d_inode;
521 mutex_lock(PIPE_MUTEX(*inode));
523 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
526 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
528 mutex_unlock(PIPE_MUTEX(*inode));
538 pipe_read_release(struct inode *inode, struct file *filp)
540 pipe_read_fasync(-1, filp, 0);
541 return pipe_release(inode, 1, 0);
545 pipe_write_release(struct inode *inode, struct file *filp)
547 pipe_write_fasync(-1, filp, 0);
548 return pipe_release(inode, 0, 1);
552 pipe_rdwr_release(struct inode *inode, struct file *filp)
556 pipe_rdwr_fasync(-1, filp, 0);
557 decr = (filp->f_mode & FMODE_READ) != 0;
558 decw = (filp->f_mode & FMODE_WRITE) != 0;
559 return pipe_release(inode, decr, decw);
563 pipe_read_open(struct inode *inode, struct file *filp)
565 /* We could have perhaps used atomic_t, but this and friends
566 below are the only places. So it doesn't seem worthwhile. */
567 mutex_lock(PIPE_MUTEX(*inode));
568 PIPE_READERS(*inode)++;
569 mutex_unlock(PIPE_MUTEX(*inode));
575 pipe_write_open(struct inode *inode, struct file *filp)
577 mutex_lock(PIPE_MUTEX(*inode));
578 PIPE_WRITERS(*inode)++;
579 mutex_unlock(PIPE_MUTEX(*inode));
585 pipe_rdwr_open(struct inode *inode, struct file *filp)
587 mutex_lock(PIPE_MUTEX(*inode));
588 if (filp->f_mode & FMODE_READ)
589 PIPE_READERS(*inode)++;
590 if (filp->f_mode & FMODE_WRITE)
591 PIPE_WRITERS(*inode)++;
592 mutex_unlock(PIPE_MUTEX(*inode));
598 * The file_operations structs are not static because they
599 * are also used in linux/fs/fifo.c to do operations on FIFOs.
601 const struct file_operations read_fifo_fops = {
608 .open = pipe_read_open,
609 .release = pipe_read_release,
610 .fasync = pipe_read_fasync,
613 const struct file_operations write_fifo_fops = {
617 .writev = pipe_writev,
620 .open = pipe_write_open,
621 .release = pipe_write_release,
622 .fasync = pipe_write_fasync,
625 const struct file_operations rdwr_fifo_fops = {
630 .writev = pipe_writev,
633 .open = pipe_rdwr_open,
634 .release = pipe_rdwr_release,
635 .fasync = pipe_rdwr_fasync,
638 static struct file_operations read_pipe_fops = {
645 .open = pipe_read_open,
646 .release = pipe_read_release,
647 .fasync = pipe_read_fasync,
650 static struct file_operations write_pipe_fops = {
654 .writev = pipe_writev,
657 .open = pipe_write_open,
658 .release = pipe_write_release,
659 .fasync = pipe_write_fasync,
662 static struct file_operations rdwr_pipe_fops = {
667 .writev = pipe_writev,
670 .open = pipe_rdwr_open,
671 .release = pipe_rdwr_release,
672 .fasync = pipe_rdwr_fasync,
675 void free_pipe_info(struct inode *inode)
678 struct pipe_inode_info *info = inode->i_pipe;
680 inode->i_pipe = NULL;
681 for (i = 0; i < PIPE_BUFFERS; i++) {
682 struct pipe_buffer *buf = info->bufs + i;
684 buf->ops->release(info, buf);
687 __free_page(info->tmp_page);
691 struct inode* pipe_new(struct inode* inode)
693 struct pipe_inode_info *info;
695 info = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
698 inode->i_pipe = info;
700 init_waitqueue_head(PIPE_WAIT(*inode));
701 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
708 static struct vfsmount *pipe_mnt __read_mostly;
709 static int pipefs_delete_dentry(struct dentry *dentry)
713 static struct dentry_operations pipefs_dentry_operations = {
714 .d_delete = pipefs_delete_dentry,
717 static struct inode * get_pipe_inode(void)
719 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
726 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
727 inode->i_fop = &rdwr_pipe_fops;
730 * Mark the inode dirty from the very beginning,
731 * that way it will never be moved to the dirty
732 * list because "mark_inode_dirty()" will think
733 * that it already _is_ on the dirty list.
735 inode->i_state = I_DIRTY;
736 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
737 inode->i_uid = current->fsuid;
738 inode->i_gid = current->fsgid;
739 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
740 inode->i_blksize = PAGE_SIZE;
753 struct dentry *dentry;
754 struct inode * inode;
755 struct file *f1, *f2;
760 f1 = get_empty_filp();
764 f2 = get_empty_filp();
768 inode = get_pipe_inode();
772 error = get_unused_fd();
774 goto close_f12_inode;
777 error = get_unused_fd();
779 goto close_f12_inode_i;
783 sprintf(name, "[%lu]", inode->i_ino);
785 this.len = strlen(name);
786 this.hash = inode->i_ino; /* will go */
787 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
789 goto close_f12_inode_i_j;
790 dentry->d_op = &pipefs_dentry_operations;
791 d_add(dentry, inode);
792 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
793 f1->f_dentry = f2->f_dentry = dget(dentry);
794 f1->f_mapping = f2->f_mapping = inode->i_mapping;
797 f1->f_pos = f2->f_pos = 0;
798 f1->f_flags = O_RDONLY;
799 f1->f_op = &read_pipe_fops;
800 f1->f_mode = FMODE_READ;
804 f2->f_flags = O_WRONLY;
805 f2->f_op = &write_pipe_fops;
806 f2->f_mode = FMODE_WRITE;
820 free_pipe_info(inode);
831 * pipefs should _never_ be mounted by userland - too much of security hassle,
832 * no real gain from having the whole whorehouse mounted. So we don't need
833 * any operations on the root directory. However, we need a non-trivial
834 * d_name - pipe: will go nicely and kill the special-casing in procfs.
837 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
838 int flags, const char *dev_name, void *data)
840 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
843 static struct file_system_type pipe_fs_type = {
845 .get_sb = pipefs_get_sb,
846 .kill_sb = kill_anon_super,
849 static int __init init_pipe_fs(void)
851 int err = register_filesystem(&pipe_fs_type);
853 pipe_mnt = kern_mount(&pipe_fs_type);
854 if (IS_ERR(pipe_mnt)) {
855 err = PTR_ERR(pipe_mnt);
856 unregister_filesystem(&pipe_fs_type);
862 static void __exit exit_pipe_fs(void)
864 unregister_filesystem(&pipe_fs_type);
868 fs_initcall(init_pipe_fs);
869 module_exit(exit_pipe_fs);