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,
66 copy = min_t(unsigned long, len, iov->iov_len);
69 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
72 if (copy_from_user(to, iov->iov_base, copy))
77 iov->iov_base += copy;
84 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
92 copy = min_t(unsigned long, len, iov->iov_len);
95 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
98 if (copy_to_user(iov->iov_base, from, copy))
103 iov->iov_base += copy;
104 iov->iov_len -= copy;
110 * Attempt to pre-fault in the user memory, so we can use atomic copies.
111 * Returns the number of bytes not faulted in.
113 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
115 while (!iov->iov_len)
119 unsigned long this_len;
121 this_len = min_t(unsigned long, len, iov->iov_len);
122 if (fault_in_pages_writeable(iov->iov_base, this_len))
133 * Pre-fault in the user memory, so we can use atomic copies.
135 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
137 while (!iov->iov_len)
141 unsigned long this_len;
143 this_len = min_t(unsigned long, len, iov->iov_len);
144 fault_in_pages_readable(iov->iov_base, this_len);
150 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
151 struct pipe_buffer *buf)
153 struct page *page = buf->page;
156 * If nobody else uses this page, and we don't already have a
157 * temporary page, let's keep track of it as a one-deep
158 * allocation cache. (Otherwise just release our reference to it)
160 if (page_count(page) == 1 && !pipe->tmp_page)
161 pipe->tmp_page = page;
163 page_cache_release(page);
166 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
167 struct pipe_buffer *buf, int atomic)
170 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
171 return kmap_atomic(buf->page, KM_USER0);
174 return kmap(buf->page);
177 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
178 struct pipe_buffer *buf, void *map_data)
180 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
181 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
182 kunmap_atomic(map_data, KM_USER0);
187 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
188 struct pipe_buffer *buf)
190 struct page *page = buf->page;
192 if (page_count(page) == 1) {
200 void generic_pipe_buf_get(struct pipe_inode_info *info, struct pipe_buffer *buf)
202 page_cache_get(buf->page);
205 int generic_pipe_buf_pin(struct pipe_inode_info *info, struct pipe_buffer *buf)
210 static struct pipe_buf_operations anon_pipe_buf_ops = {
212 .map = generic_pipe_buf_map,
213 .unmap = generic_pipe_buf_unmap,
214 .pin = generic_pipe_buf_pin,
215 .release = anon_pipe_buf_release,
216 .steal = generic_pipe_buf_steal,
217 .get = generic_pipe_buf_get,
221 pipe_readv(struct file *filp, const struct iovec *_iov,
222 unsigned long nr_segs, loff_t *ppos)
224 struct inode *inode = filp->f_dentry->d_inode;
225 struct pipe_inode_info *pipe;
228 struct iovec *iov = (struct iovec *)_iov;
231 total_len = iov_length(iov, nr_segs);
232 /* Null read succeeds. */
233 if (unlikely(total_len == 0))
238 mutex_lock(&inode->i_mutex);
239 pipe = inode->i_pipe;
241 int bufs = pipe->nrbufs;
243 int curbuf = pipe->curbuf;
244 struct pipe_buffer *buf = pipe->bufs + curbuf;
245 struct pipe_buf_operations *ops = buf->ops;
247 size_t chars = buf->len;
250 if (chars > total_len)
253 error = ops->pin(pipe, buf);
260 atomic = !iov_fault_in_pages_write(iov, chars);
262 addr = ops->map(pipe, buf, atomic);
263 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
264 ops->unmap(pipe, buf, addr);
265 if (unlikely(error)) {
267 * Just retry with the slow path if we failed.
278 buf->offset += chars;
282 ops->release(pipe, buf);
283 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
284 pipe->curbuf = curbuf;
285 pipe->nrbufs = --bufs;
290 break; /* common path: read succeeded */
292 if (bufs) /* More to do? */
296 if (!pipe->waiting_writers) {
297 /* syscall merging: Usually we must not sleep
298 * if O_NONBLOCK is set, or if we got some data.
299 * But if a writer sleeps in kernel space, then
300 * we can wait for that data without violating POSIX.
304 if (filp->f_flags & O_NONBLOCK) {
309 if (signal_pending(current)) {
315 wake_up_interruptible_sync(&pipe->wait);
316 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
320 mutex_unlock(&inode->i_mutex);
322 /* Signal writers asynchronously that there is more room. */
324 wake_up_interruptible(&pipe->wait);
325 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
333 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
335 struct iovec iov = { .iov_base = buf, .iov_len = count };
337 return pipe_readv(filp, &iov, 1, ppos);
341 pipe_writev(struct file *filp, const struct iovec *_iov,
342 unsigned long nr_segs, loff_t *ppos)
344 struct inode *inode = filp->f_dentry->d_inode;
345 struct pipe_inode_info *pipe;
348 struct iovec *iov = (struct iovec *)_iov;
352 total_len = iov_length(iov, nr_segs);
353 /* Null write succeeds. */
354 if (unlikely(total_len == 0))
359 mutex_lock(&inode->i_mutex);
360 pipe = inode->i_pipe;
362 if (!pipe->readers) {
363 send_sig(SIGPIPE, current, 0);
368 /* We try to merge small writes */
369 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
370 if (pipe->nrbufs && chars != 0) {
371 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
373 struct pipe_buffer *buf = pipe->bufs + lastbuf;
374 struct pipe_buf_operations *ops = buf->ops;
375 int offset = buf->offset + buf->len;
377 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
378 int error, atomic = 1;
381 error = ops->pin(pipe, buf);
385 iov_fault_in_pages_read(iov, chars);
387 addr = ops->map(pipe, buf, atomic);
388 error = pipe_iov_copy_from_user(offset + addr, iov,
390 ops->unmap(pipe, buf, addr);
411 if (!pipe->readers) {
412 send_sig(SIGPIPE, current, 0);
418 if (bufs < PIPE_BUFFERS) {
419 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
420 struct pipe_buffer *buf = pipe->bufs + newbuf;
421 struct page *page = pipe->tmp_page;
423 int error, atomic = 1;
426 page = alloc_page(GFP_HIGHUSER);
427 if (unlikely(!page)) {
428 ret = ret ? : -ENOMEM;
431 pipe->tmp_page = page;
433 /* Always wake up, even if the copy fails. Otherwise
434 * we lock up (O_NONBLOCK-)readers that sleep due to
436 * FIXME! Is this really true?
440 if (chars > total_len)
443 iov_fault_in_pages_read(iov, chars);
446 src = kmap_atomic(page, KM_USER0);
450 error = pipe_iov_copy_from_user(src, iov, chars,
453 kunmap_atomic(src, KM_USER0);
457 if (unlikely(error)) {
468 /* Insert it into the buffer array */
470 buf->ops = &anon_pipe_buf_ops;
473 pipe->nrbufs = ++bufs;
474 pipe->tmp_page = NULL;
480 if (bufs < PIPE_BUFFERS)
482 if (filp->f_flags & O_NONBLOCK) {
487 if (signal_pending(current)) {
493 wake_up_interruptible_sync(&pipe->wait);
494 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
497 pipe->waiting_writers++;
499 pipe->waiting_writers--;
502 mutex_unlock(&inode->i_mutex);
504 wake_up_interruptible(&pipe->wait);
505 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
508 file_update_time(filp);
513 pipe_write(struct file *filp, const char __user *buf,
514 size_t count, loff_t *ppos)
516 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
518 return pipe_writev(filp, &iov, 1, ppos);
522 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
528 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
535 pipe_ioctl(struct inode *pino, struct file *filp,
536 unsigned int cmd, unsigned long arg)
538 struct inode *inode = filp->f_dentry->d_inode;
539 struct pipe_inode_info *pipe;
540 int count, buf, nrbufs;
544 mutex_lock(&inode->i_mutex);
545 pipe = inode->i_pipe;
548 nrbufs = pipe->nrbufs;
549 while (--nrbufs >= 0) {
550 count += pipe->bufs[buf].len;
551 buf = (buf+1) & (PIPE_BUFFERS-1);
553 mutex_unlock(&inode->i_mutex);
555 return put_user(count, (int __user *)arg);
561 /* No kernel lock held - fine */
563 pipe_poll(struct file *filp, poll_table *wait)
566 struct inode *inode = filp->f_dentry->d_inode;
567 struct pipe_inode_info *pipe = inode->i_pipe;
570 poll_wait(filp, &pipe->wait, wait);
572 /* Reading only -- no need for acquiring the semaphore. */
573 nrbufs = pipe->nrbufs;
575 if (filp->f_mode & FMODE_READ) {
576 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
577 if (!pipe->writers && filp->f_version != pipe->w_counter)
581 if (filp->f_mode & FMODE_WRITE) {
582 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
584 * Most Unices do not set POLLERR for FIFOs but on Linux they
585 * behave exactly like pipes for poll().
595 pipe_release(struct inode *inode, int decr, int decw)
597 struct pipe_inode_info *pipe;
599 mutex_lock(&inode->i_mutex);
600 pipe = inode->i_pipe;
601 pipe->readers -= decr;
602 pipe->writers -= decw;
604 if (!pipe->readers && !pipe->writers) {
605 free_pipe_info(inode);
607 wake_up_interruptible(&pipe->wait);
608 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
609 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
611 mutex_unlock(&inode->i_mutex);
617 pipe_read_fasync(int fd, struct file *filp, int on)
619 struct inode *inode = filp->f_dentry->d_inode;
622 mutex_lock(&inode->i_mutex);
623 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
624 mutex_unlock(&inode->i_mutex);
634 pipe_write_fasync(int fd, struct file *filp, int on)
636 struct inode *inode = filp->f_dentry->d_inode;
639 mutex_lock(&inode->i_mutex);
640 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
641 mutex_unlock(&inode->i_mutex);
651 pipe_rdwr_fasync(int fd, struct file *filp, int on)
653 struct inode *inode = filp->f_dentry->d_inode;
654 struct pipe_inode_info *pipe = inode->i_pipe;
657 mutex_lock(&inode->i_mutex);
659 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
662 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
664 mutex_unlock(&inode->i_mutex);
674 pipe_read_release(struct inode *inode, struct file *filp)
676 pipe_read_fasync(-1, filp, 0);
677 return pipe_release(inode, 1, 0);
681 pipe_write_release(struct inode *inode, struct file *filp)
683 pipe_write_fasync(-1, filp, 0);
684 return pipe_release(inode, 0, 1);
688 pipe_rdwr_release(struct inode *inode, struct file *filp)
692 pipe_rdwr_fasync(-1, filp, 0);
693 decr = (filp->f_mode & FMODE_READ) != 0;
694 decw = (filp->f_mode & FMODE_WRITE) != 0;
695 return pipe_release(inode, decr, decw);
699 pipe_read_open(struct inode *inode, struct file *filp)
701 /* We could have perhaps used atomic_t, but this and friends
702 below are the only places. So it doesn't seem worthwhile. */
703 mutex_lock(&inode->i_mutex);
704 inode->i_pipe->readers++;
705 mutex_unlock(&inode->i_mutex);
711 pipe_write_open(struct inode *inode, struct file *filp)
713 mutex_lock(&inode->i_mutex);
714 inode->i_pipe->writers++;
715 mutex_unlock(&inode->i_mutex);
721 pipe_rdwr_open(struct inode *inode, struct file *filp)
723 mutex_lock(&inode->i_mutex);
724 if (filp->f_mode & FMODE_READ)
725 inode->i_pipe->readers++;
726 if (filp->f_mode & FMODE_WRITE)
727 inode->i_pipe->writers++;
728 mutex_unlock(&inode->i_mutex);
734 * The file_operations structs are not static because they
735 * are also used in linux/fs/fifo.c to do operations on FIFOs.
737 const struct file_operations read_fifo_fops = {
744 .open = pipe_read_open,
745 .release = pipe_read_release,
746 .fasync = pipe_read_fasync,
749 const struct file_operations write_fifo_fops = {
753 .writev = pipe_writev,
756 .open = pipe_write_open,
757 .release = pipe_write_release,
758 .fasync = pipe_write_fasync,
761 const struct file_operations rdwr_fifo_fops = {
766 .writev = pipe_writev,
769 .open = pipe_rdwr_open,
770 .release = pipe_rdwr_release,
771 .fasync = pipe_rdwr_fasync,
774 static struct file_operations read_pipe_fops = {
781 .open = pipe_read_open,
782 .release = pipe_read_release,
783 .fasync = pipe_read_fasync,
786 static struct file_operations write_pipe_fops = {
790 .writev = pipe_writev,
793 .open = pipe_write_open,
794 .release = pipe_write_release,
795 .fasync = pipe_write_fasync,
798 static struct file_operations rdwr_pipe_fops = {
803 .writev = pipe_writev,
806 .open = pipe_rdwr_open,
807 .release = pipe_rdwr_release,
808 .fasync = pipe_rdwr_fasync,
811 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
813 struct pipe_inode_info *pipe;
815 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
817 init_waitqueue_head(&pipe->wait);
818 pipe->r_counter = pipe->w_counter = 1;
825 void __free_pipe_info(struct pipe_inode_info *pipe)
829 for (i = 0; i < PIPE_BUFFERS; i++) {
830 struct pipe_buffer *buf = pipe->bufs + i;
832 buf->ops->release(pipe, buf);
835 __free_page(pipe->tmp_page);
839 void free_pipe_info(struct inode *inode)
841 __free_pipe_info(inode->i_pipe);
842 inode->i_pipe = NULL;
845 static struct vfsmount *pipe_mnt __read_mostly;
846 static int pipefs_delete_dentry(struct dentry *dentry)
851 static struct dentry_operations pipefs_dentry_operations = {
852 .d_delete = pipefs_delete_dentry,
855 static struct inode * get_pipe_inode(void)
857 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
858 struct pipe_inode_info *pipe;
863 pipe = alloc_pipe_info(inode);
866 inode->i_pipe = pipe;
868 pipe->readers = pipe->writers = 1;
869 inode->i_fop = &rdwr_pipe_fops;
872 * Mark the inode dirty from the very beginning,
873 * that way it will never be moved to the dirty
874 * list because "mark_inode_dirty()" will think
875 * that it already _is_ on the dirty list.
877 inode->i_state = I_DIRTY;
878 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
879 inode->i_uid = current->fsuid;
880 inode->i_gid = current->fsgid;
881 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
882 inode->i_blksize = PAGE_SIZE;
897 struct dentry *dentry;
898 struct inode * inode;
899 struct file *f1, *f2;
904 f1 = get_empty_filp();
908 f2 = get_empty_filp();
912 inode = get_pipe_inode();
916 error = get_unused_fd();
918 goto close_f12_inode;
921 error = get_unused_fd();
923 goto close_f12_inode_i;
927 sprintf(name, "[%lu]", inode->i_ino);
929 this.len = strlen(name);
930 this.hash = inode->i_ino; /* will go */
931 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
933 goto close_f12_inode_i_j;
935 dentry->d_op = &pipefs_dentry_operations;
936 d_add(dentry, inode);
937 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
938 f1->f_dentry = f2->f_dentry = dget(dentry);
939 f1->f_mapping = f2->f_mapping = inode->i_mapping;
942 f1->f_pos = f2->f_pos = 0;
943 f1->f_flags = O_RDONLY;
944 f1->f_op = &read_pipe_fops;
945 f1->f_mode = FMODE_READ;
949 f2->f_flags = O_WRONLY;
950 f2->f_op = &write_pipe_fops;
951 f2->f_mode = FMODE_WRITE;
966 free_pipe_info(inode);
977 * pipefs should _never_ be mounted by userland - too much of security hassle,
978 * no real gain from having the whole whorehouse mounted. So we don't need
979 * any operations on the root directory. However, we need a non-trivial
980 * d_name - pipe: will go nicely and kill the special-casing in procfs.
982 static int pipefs_get_sb(struct file_system_type *fs_type,
983 int flags, const char *dev_name, void *data,
984 struct vfsmount *mnt)
986 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
989 static struct file_system_type pipe_fs_type = {
991 .get_sb = pipefs_get_sb,
992 .kill_sb = kill_anon_super,
995 static int __init init_pipe_fs(void)
997 int err = register_filesystem(&pipe_fs_type);
1000 pipe_mnt = kern_mount(&pipe_fs_type);
1001 if (IS_ERR(pipe_mnt)) {
1002 err = PTR_ERR(pipe_mnt);
1003 unregister_filesystem(&pipe_fs_type);
1009 static void __exit exit_pipe_fs(void)
1011 unregister_filesystem(&pipe_fs_type);
1015 fs_initcall(init_pipe_fs);
1016 module_exit(exit_pipe_fs);