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>
19 #include <linux/audit.h>
21 #include <asm/uaccess.h>
22 #include <asm/ioctls.h>
25 * We use a start+len construction, which provides full use of the
27 * -- Florian Coosmann (FGC)
29 * Reads with count = 0 should always return 0.
30 * -- Julian Bradfield 1999-06-07.
32 * FIFOs and Pipes now generate SIGIO for both readers and writers.
33 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
35 * pipe_read & write cleanup
36 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
39 /* Drop the inode semaphore and wait for a pipe event, atomically */
40 void pipe_wait(struct pipe_inode_info *pipe)
45 * Pipes are system-local resources, so sleeping on them
46 * is considered a noninteractive wait:
48 prepare_to_wait(&pipe->wait, &wait,
49 TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
51 mutex_unlock(&pipe->inode->i_mutex);
53 finish_wait(&pipe->wait, &wait);
55 mutex_lock(&pipe->inode->i_mutex);
59 pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len,
67 copy = min_t(unsigned long, len, iov->iov_len);
70 if (__copy_from_user_inatomic(to, iov->iov_base, copy))
73 if (copy_from_user(to, iov->iov_base, copy))
78 iov->iov_base += copy;
85 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len,
93 copy = min_t(unsigned long, len, iov->iov_len);
96 if (__copy_to_user_inatomic(iov->iov_base, from, copy))
99 if (copy_to_user(iov->iov_base, from, copy))
104 iov->iov_base += copy;
105 iov->iov_len -= copy;
111 * Attempt to pre-fault in the user memory, so we can use atomic copies.
112 * Returns the number of bytes not faulted in.
114 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
116 while (!iov->iov_len)
120 unsigned long this_len;
122 this_len = min_t(unsigned long, len, iov->iov_len);
123 if (fault_in_pages_writeable(iov->iov_base, this_len))
134 * Pre-fault in the user memory, so we can use atomic copies.
136 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
138 while (!iov->iov_len)
142 unsigned long this_len;
144 this_len = min_t(unsigned long, len, iov->iov_len);
145 fault_in_pages_readable(iov->iov_base, this_len);
151 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
152 struct pipe_buffer *buf)
154 struct page *page = buf->page;
157 * If nobody else uses this page, and we don't already have a
158 * temporary page, let's keep track of it as a one-deep
159 * allocation cache. (Otherwise just release our reference to it)
161 if (page_count(page) == 1 && !pipe->tmp_page)
162 pipe->tmp_page = page;
164 page_cache_release(page);
168 * generic_pipe_buf_map - virtually map a pipe buffer
169 * @pipe: the pipe that the buffer belongs to
170 * @buf: the buffer that should be mapped
171 * @atomic: whether to use an atomic map
174 * This function returns a kernel virtual address mapping for the
175 * passed in @pipe_buffer. If @atomic is set, an atomic map is provided
176 * and the caller has to be careful not to fault before calling
177 * the unmap function.
179 * Note that this function occupies KM_USER0 if @atomic != 0.
181 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
182 struct pipe_buffer *buf, int atomic)
185 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
186 return kmap_atomic(buf->page, KM_USER0);
189 return kmap(buf->page);
193 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
194 * @pipe: the pipe that the buffer belongs to
195 * @buf: the buffer that should be unmapped
196 * @map_data: the data that the mapping function returned
199 * This function undoes the mapping that ->map() provided.
201 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
202 struct pipe_buffer *buf, void *map_data)
204 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
205 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
206 kunmap_atomic(map_data, KM_USER0);
212 * generic_pipe_buf_steal - attempt to take ownership of a @pipe_buffer
213 * @pipe: the pipe that the buffer belongs to
214 * @buf: the buffer to attempt to steal
217 * This function attempts to steal the @struct page attached to
218 * @buf. If successful, this function returns 0 and returns with
219 * the page locked. The caller may then reuse the page for whatever
220 * he wishes, the typical use is insertion into a different file
223 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
224 struct pipe_buffer *buf)
226 struct page *page = buf->page;
229 * A reference of one is golden, that means that the owner of this
230 * page is the only one holding a reference to it. lock the page
233 if (page_count(page) == 1) {
242 * generic_pipe_buf_get - get a reference to a @struct pipe_buffer
243 * @pipe: the pipe that the buffer belongs to
244 * @buf: the buffer to get a reference to
247 * This function grabs an extra reference to @buf. It's used in
248 * in the tee() system call, when we duplicate the buffers in one
251 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
253 page_cache_get(buf->page);
257 * generic_pipe_buf_confirm - verify contents of the pipe buffer
258 * @pipe: the pipe that the buffer belongs to
259 * @buf: the buffer to confirm
262 * This function does nothing, because the generic pipe code uses
263 * pages that are always good when inserted into the pipe.
265 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
266 struct pipe_buffer *buf)
271 static const struct pipe_buf_operations anon_pipe_buf_ops = {
273 .map = generic_pipe_buf_map,
274 .unmap = generic_pipe_buf_unmap,
275 .confirm = generic_pipe_buf_confirm,
276 .release = anon_pipe_buf_release,
277 .steal = generic_pipe_buf_steal,
278 .get = generic_pipe_buf_get,
282 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
283 unsigned long nr_segs, loff_t pos)
285 struct file *filp = iocb->ki_filp;
286 struct inode *inode = filp->f_path.dentry->d_inode;
287 struct pipe_inode_info *pipe;
290 struct iovec *iov = (struct iovec *)_iov;
293 total_len = iov_length(iov, nr_segs);
294 /* Null read succeeds. */
295 if (unlikely(total_len == 0))
300 mutex_lock(&inode->i_mutex);
301 pipe = inode->i_pipe;
303 int bufs = pipe->nrbufs;
305 int curbuf = pipe->curbuf;
306 struct pipe_buffer *buf = pipe->bufs + curbuf;
307 const struct pipe_buf_operations *ops = buf->ops;
309 size_t chars = buf->len;
312 if (chars > total_len)
315 error = ops->confirm(pipe, buf);
322 atomic = !iov_fault_in_pages_write(iov, chars);
324 addr = ops->map(pipe, buf, atomic);
325 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
326 ops->unmap(pipe, buf, addr);
327 if (unlikely(error)) {
329 * Just retry with the slow path if we failed.
340 buf->offset += chars;
344 ops->release(pipe, buf);
345 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
346 pipe->curbuf = curbuf;
347 pipe->nrbufs = --bufs;
352 break; /* common path: read succeeded */
354 if (bufs) /* More to do? */
358 if (!pipe->waiting_writers) {
359 /* syscall merging: Usually we must not sleep
360 * if O_NONBLOCK is set, or if we got some data.
361 * But if a writer sleeps in kernel space, then
362 * we can wait for that data without violating POSIX.
366 if (filp->f_flags & O_NONBLOCK) {
371 if (signal_pending(current)) {
377 wake_up_interruptible_sync(&pipe->wait);
378 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
382 mutex_unlock(&inode->i_mutex);
384 /* Signal writers asynchronously that there is more room. */
386 wake_up_interruptible(&pipe->wait);
387 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
395 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
396 unsigned long nr_segs, loff_t ppos)
398 struct file *filp = iocb->ki_filp;
399 struct inode *inode = filp->f_path.dentry->d_inode;
400 struct pipe_inode_info *pipe;
403 struct iovec *iov = (struct iovec *)_iov;
407 total_len = iov_length(iov, nr_segs);
408 /* Null write succeeds. */
409 if (unlikely(total_len == 0))
414 mutex_lock(&inode->i_mutex);
415 pipe = inode->i_pipe;
417 if (!pipe->readers) {
418 send_sig(SIGPIPE, current, 0);
423 /* We try to merge small writes */
424 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
425 if (pipe->nrbufs && chars != 0) {
426 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
428 struct pipe_buffer *buf = pipe->bufs + lastbuf;
429 const struct pipe_buf_operations *ops = buf->ops;
430 int offset = buf->offset + buf->len;
432 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
433 int error, atomic = 1;
436 error = ops->confirm(pipe, buf);
440 iov_fault_in_pages_read(iov, chars);
442 addr = ops->map(pipe, buf, atomic);
443 error = pipe_iov_copy_from_user(offset + addr, iov,
445 ops->unmap(pipe, buf, addr);
466 if (!pipe->readers) {
467 send_sig(SIGPIPE, current, 0);
473 if (bufs < PIPE_BUFFERS) {
474 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
475 struct pipe_buffer *buf = pipe->bufs + newbuf;
476 struct page *page = pipe->tmp_page;
478 int error, atomic = 1;
481 page = alloc_page(GFP_HIGHUSER);
482 if (unlikely(!page)) {
483 ret = ret ? : -ENOMEM;
486 pipe->tmp_page = page;
488 /* Always wake up, even if the copy fails. Otherwise
489 * we lock up (O_NONBLOCK-)readers that sleep due to
491 * FIXME! Is this really true?
495 if (chars > total_len)
498 iov_fault_in_pages_read(iov, chars);
501 src = kmap_atomic(page, KM_USER0);
505 error = pipe_iov_copy_from_user(src, iov, chars,
508 kunmap_atomic(src, KM_USER0);
512 if (unlikely(error)) {
523 /* Insert it into the buffer array */
525 buf->ops = &anon_pipe_buf_ops;
528 pipe->nrbufs = ++bufs;
529 pipe->tmp_page = NULL;
535 if (bufs < PIPE_BUFFERS)
537 if (filp->f_flags & O_NONBLOCK) {
542 if (signal_pending(current)) {
548 wake_up_interruptible_sync(&pipe->wait);
549 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
552 pipe->waiting_writers++;
554 pipe->waiting_writers--;
557 mutex_unlock(&inode->i_mutex);
559 wake_up_interruptible(&pipe->wait);
560 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
563 file_update_time(filp);
568 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
574 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
581 pipe_ioctl(struct inode *pino, struct file *filp,
582 unsigned int cmd, unsigned long arg)
584 struct inode *inode = filp->f_path.dentry->d_inode;
585 struct pipe_inode_info *pipe;
586 int count, buf, nrbufs;
590 mutex_lock(&inode->i_mutex);
591 pipe = inode->i_pipe;
594 nrbufs = pipe->nrbufs;
595 while (--nrbufs >= 0) {
596 count += pipe->bufs[buf].len;
597 buf = (buf+1) & (PIPE_BUFFERS-1);
599 mutex_unlock(&inode->i_mutex);
601 return put_user(count, (int __user *)arg);
607 /* No kernel lock held - fine */
609 pipe_poll(struct file *filp, poll_table *wait)
612 struct inode *inode = filp->f_path.dentry->d_inode;
613 struct pipe_inode_info *pipe = inode->i_pipe;
616 poll_wait(filp, &pipe->wait, wait);
618 /* Reading only -- no need for acquiring the semaphore. */
619 nrbufs = pipe->nrbufs;
621 if (filp->f_mode & FMODE_READ) {
622 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
623 if (!pipe->writers && filp->f_version != pipe->w_counter)
627 if (filp->f_mode & FMODE_WRITE) {
628 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
630 * Most Unices do not set POLLERR for FIFOs but on Linux they
631 * behave exactly like pipes for poll().
641 pipe_release(struct inode *inode, int decr, int decw)
643 struct pipe_inode_info *pipe;
645 mutex_lock(&inode->i_mutex);
646 pipe = inode->i_pipe;
647 pipe->readers -= decr;
648 pipe->writers -= decw;
650 if (!pipe->readers && !pipe->writers) {
651 free_pipe_info(inode);
653 wake_up_interruptible(&pipe->wait);
654 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
655 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
657 mutex_unlock(&inode->i_mutex);
663 pipe_read_fasync(int fd, struct file *filp, int on)
665 struct inode *inode = filp->f_path.dentry->d_inode;
668 mutex_lock(&inode->i_mutex);
669 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
670 mutex_unlock(&inode->i_mutex);
680 pipe_write_fasync(int fd, struct file *filp, int on)
682 struct inode *inode = filp->f_path.dentry->d_inode;
685 mutex_lock(&inode->i_mutex);
686 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
687 mutex_unlock(&inode->i_mutex);
697 pipe_rdwr_fasync(int fd, struct file *filp, int on)
699 struct inode *inode = filp->f_path.dentry->d_inode;
700 struct pipe_inode_info *pipe = inode->i_pipe;
703 mutex_lock(&inode->i_mutex);
705 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
708 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
710 mutex_unlock(&inode->i_mutex);
720 pipe_read_release(struct inode *inode, struct file *filp)
722 pipe_read_fasync(-1, filp, 0);
723 return pipe_release(inode, 1, 0);
727 pipe_write_release(struct inode *inode, struct file *filp)
729 pipe_write_fasync(-1, filp, 0);
730 return pipe_release(inode, 0, 1);
734 pipe_rdwr_release(struct inode *inode, struct file *filp)
738 pipe_rdwr_fasync(-1, filp, 0);
739 decr = (filp->f_mode & FMODE_READ) != 0;
740 decw = (filp->f_mode & FMODE_WRITE) != 0;
741 return pipe_release(inode, decr, decw);
745 pipe_read_open(struct inode *inode, struct file *filp)
747 /* We could have perhaps used atomic_t, but this and friends
748 below are the only places. So it doesn't seem worthwhile. */
749 mutex_lock(&inode->i_mutex);
750 inode->i_pipe->readers++;
751 mutex_unlock(&inode->i_mutex);
757 pipe_write_open(struct inode *inode, struct file *filp)
759 mutex_lock(&inode->i_mutex);
760 inode->i_pipe->writers++;
761 mutex_unlock(&inode->i_mutex);
767 pipe_rdwr_open(struct inode *inode, struct file *filp)
769 mutex_lock(&inode->i_mutex);
770 if (filp->f_mode & FMODE_READ)
771 inode->i_pipe->readers++;
772 if (filp->f_mode & FMODE_WRITE)
773 inode->i_pipe->writers++;
774 mutex_unlock(&inode->i_mutex);
780 * The file_operations structs are not static because they
781 * are also used in linux/fs/fifo.c to do operations on FIFOs.
783 const struct file_operations read_fifo_fops = {
785 .read = do_sync_read,
786 .aio_read = pipe_read,
790 .open = pipe_read_open,
791 .release = pipe_read_release,
792 .fasync = pipe_read_fasync,
795 const struct file_operations write_fifo_fops = {
798 .write = do_sync_write,
799 .aio_write = pipe_write,
802 .open = pipe_write_open,
803 .release = pipe_write_release,
804 .fasync = pipe_write_fasync,
807 const struct file_operations rdwr_fifo_fops = {
809 .read = do_sync_read,
810 .aio_read = pipe_read,
811 .write = do_sync_write,
812 .aio_write = pipe_write,
815 .open = pipe_rdwr_open,
816 .release = pipe_rdwr_release,
817 .fasync = pipe_rdwr_fasync,
820 static const struct file_operations read_pipe_fops = {
822 .read = do_sync_read,
823 .aio_read = pipe_read,
827 .open = pipe_read_open,
828 .release = pipe_read_release,
829 .fasync = pipe_read_fasync,
832 static const struct file_operations write_pipe_fops = {
835 .write = do_sync_write,
836 .aio_write = pipe_write,
839 .open = pipe_write_open,
840 .release = pipe_write_release,
841 .fasync = pipe_write_fasync,
844 static const struct file_operations rdwr_pipe_fops = {
846 .read = do_sync_read,
847 .aio_read = pipe_read,
848 .write = do_sync_write,
849 .aio_write = pipe_write,
852 .open = pipe_rdwr_open,
853 .release = pipe_rdwr_release,
854 .fasync = pipe_rdwr_fasync,
857 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
859 struct pipe_inode_info *pipe;
861 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
863 init_waitqueue_head(&pipe->wait);
864 pipe->r_counter = pipe->w_counter = 1;
871 void __free_pipe_info(struct pipe_inode_info *pipe)
875 for (i = 0; i < PIPE_BUFFERS; i++) {
876 struct pipe_buffer *buf = pipe->bufs + i;
878 buf->ops->release(pipe, buf);
881 __free_page(pipe->tmp_page);
885 void free_pipe_info(struct inode *inode)
887 __free_pipe_info(inode->i_pipe);
888 inode->i_pipe = NULL;
891 static struct vfsmount *pipe_mnt __read_mostly;
892 static int pipefs_delete_dentry(struct dentry *dentry)
895 * At creation time, we pretended this dentry was hashed
896 * (by clearing DCACHE_UNHASHED bit in d_flags)
897 * At delete time, we restore the truth : not hashed.
898 * (so that dput() can proceed correctly)
900 dentry->d_flags |= DCACHE_UNHASHED;
905 * pipefs_dname() is called from d_path().
907 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
909 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
910 dentry->d_inode->i_ino);
913 static struct dentry_operations pipefs_dentry_operations = {
914 .d_delete = pipefs_delete_dentry,
915 .d_dname = pipefs_dname,
918 static struct inode * get_pipe_inode(void)
920 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
921 struct pipe_inode_info *pipe;
926 pipe = alloc_pipe_info(inode);
929 inode->i_pipe = pipe;
931 pipe->readers = pipe->writers = 1;
932 inode->i_fop = &rdwr_pipe_fops;
935 * Mark the inode dirty from the very beginning,
936 * that way it will never be moved to the dirty
937 * list because "mark_inode_dirty()" will think
938 * that it already _is_ on the dirty list.
940 inode->i_state = I_DIRTY;
941 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
942 inode->i_uid = current->fsuid;
943 inode->i_gid = current->fsgid;
944 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
955 struct file *create_write_pipe(void)
960 struct dentry *dentry;
961 struct qstr name = { .name = "" };
963 f = get_empty_filp();
965 return ERR_PTR(-ENFILE);
967 inode = get_pipe_inode();
972 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
976 dentry->d_op = &pipefs_dentry_operations;
978 * We dont want to publish this dentry into global dentry hash table.
979 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
980 * This permits a working /proc/$pid/fd/XXX on pipes
982 dentry->d_flags &= ~DCACHE_UNHASHED;
983 d_instantiate(dentry, inode);
984 f->f_path.mnt = mntget(pipe_mnt);
985 f->f_path.dentry = dentry;
986 f->f_mapping = inode->i_mapping;
988 f->f_flags = O_WRONLY;
989 f->f_op = &write_pipe_fops;
990 f->f_mode = FMODE_WRITE;
996 free_pipe_info(inode);
1000 return ERR_PTR(err);
1003 void free_write_pipe(struct file *f)
1005 free_pipe_info(f->f_dentry->d_inode);
1006 dput(f->f_path.dentry);
1007 mntput(f->f_path.mnt);
1011 struct file *create_read_pipe(struct file *wrf)
1013 struct file *f = get_empty_filp();
1015 return ERR_PTR(-ENFILE);
1017 /* Grab pipe from the writer */
1018 f->f_path.mnt = mntget(wrf->f_path.mnt);
1019 f->f_path.dentry = dget(wrf->f_path.dentry);
1020 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
1023 f->f_flags = O_RDONLY;
1024 f->f_op = &read_pipe_fops;
1025 f->f_mode = FMODE_READ;
1031 int do_pipe(int *fd)
1033 struct file *fw, *fr;
1037 fw = create_write_pipe();
1040 fr = create_read_pipe(fw);
1041 error = PTR_ERR(fr);
1043 goto err_write_pipe;
1045 error = get_unused_fd();
1050 error = get_unused_fd();
1055 error = audit_fd_pair(fdr, fdw);
1059 fd_install(fdr, fr);
1060 fd_install(fdw, fw);
1072 mntput(fr->f_vfsmnt);
1075 free_write_pipe(fw);
1080 * pipefs should _never_ be mounted by userland - too much of security hassle,
1081 * no real gain from having the whole whorehouse mounted. So we don't need
1082 * any operations on the root directory. However, we need a non-trivial
1083 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1085 static int pipefs_get_sb(struct file_system_type *fs_type,
1086 int flags, const char *dev_name, void *data,
1087 struct vfsmount *mnt)
1089 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
1092 static struct file_system_type pipe_fs_type = {
1094 .get_sb = pipefs_get_sb,
1095 .kill_sb = kill_anon_super,
1098 static int __init init_pipe_fs(void)
1100 int err = register_filesystem(&pipe_fs_type);
1103 pipe_mnt = kern_mount(&pipe_fs_type);
1104 if (IS_ERR(pipe_mnt)) {
1105 err = PTR_ERR(pipe_mnt);
1106 unregister_filesystem(&pipe_fs_type);
1112 static void __exit exit_pipe_fs(void)
1114 unregister_filesystem(&pipe_fs_type);
1118 fs_initcall(init_pipe_fs);
1119 module_exit(exit_pipe_fs);