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, TASK_INTERRUPTIBLE);
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);
167 * generic_pipe_buf_map - virtually map a pipe buffer
168 * @pipe: the pipe that the buffer belongs to
169 * @buf: the buffer that should be mapped
170 * @atomic: whether to use an atomic map
173 * This function returns a kernel virtual address mapping for the
174 * passed in @pipe_buffer. If @atomic is set, an atomic map is provided
175 * and the caller has to be careful not to fault before calling
176 * the unmap function.
178 * Note that this function occupies KM_USER0 if @atomic != 0.
180 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
181 struct pipe_buffer *buf, int atomic)
184 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
185 return kmap_atomic(buf->page, KM_USER0);
188 return kmap(buf->page);
192 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
193 * @pipe: the pipe that the buffer belongs to
194 * @buf: the buffer that should be unmapped
195 * @map_data: the data that the mapping function returned
198 * This function undoes the mapping that ->map() provided.
200 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
201 struct pipe_buffer *buf, void *map_data)
203 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
204 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
205 kunmap_atomic(map_data, KM_USER0);
211 * generic_pipe_buf_steal - attempt to take ownership of a @pipe_buffer
212 * @pipe: the pipe that the buffer belongs to
213 * @buf: the buffer to attempt to steal
216 * This function attempts to steal the @struct page attached to
217 * @buf. If successful, this function returns 0 and returns with
218 * the page locked. The caller may then reuse the page for whatever
219 * he wishes, the typical use is insertion into a different file
222 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
223 struct pipe_buffer *buf)
225 struct page *page = buf->page;
228 * A reference of one is golden, that means that the owner of this
229 * page is the only one holding a reference to it. lock the page
232 if (page_count(page) == 1) {
241 * generic_pipe_buf_get - get a reference to a @struct pipe_buffer
242 * @pipe: the pipe that the buffer belongs to
243 * @buf: the buffer to get a reference to
246 * This function grabs an extra reference to @buf. It's used in
247 * in the tee() system call, when we duplicate the buffers in one
250 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
252 page_cache_get(buf->page);
256 * generic_pipe_buf_confirm - verify contents of the pipe buffer
257 * @info: the pipe that the buffer belongs to
258 * @buf: the buffer to confirm
261 * This function does nothing, because the generic pipe code uses
262 * pages that are always good when inserted into the pipe.
264 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
265 struct pipe_buffer *buf)
270 static const struct pipe_buf_operations anon_pipe_buf_ops = {
272 .map = generic_pipe_buf_map,
273 .unmap = generic_pipe_buf_unmap,
274 .confirm = generic_pipe_buf_confirm,
275 .release = anon_pipe_buf_release,
276 .steal = generic_pipe_buf_steal,
277 .get = generic_pipe_buf_get,
281 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
282 unsigned long nr_segs, loff_t pos)
284 struct file *filp = iocb->ki_filp;
285 struct inode *inode = filp->f_path.dentry->d_inode;
286 struct pipe_inode_info *pipe;
289 struct iovec *iov = (struct iovec *)_iov;
292 total_len = iov_length(iov, nr_segs);
293 /* Null read succeeds. */
294 if (unlikely(total_len == 0))
299 mutex_lock(&inode->i_mutex);
300 pipe = inode->i_pipe;
302 int bufs = pipe->nrbufs;
304 int curbuf = pipe->curbuf;
305 struct pipe_buffer *buf = pipe->bufs + curbuf;
306 const struct pipe_buf_operations *ops = buf->ops;
308 size_t chars = buf->len;
311 if (chars > total_len)
314 error = ops->confirm(pipe, buf);
321 atomic = !iov_fault_in_pages_write(iov, chars);
323 addr = ops->map(pipe, buf, atomic);
324 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
325 ops->unmap(pipe, buf, addr);
326 if (unlikely(error)) {
328 * Just retry with the slow path if we failed.
339 buf->offset += chars;
343 ops->release(pipe, buf);
344 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
345 pipe->curbuf = curbuf;
346 pipe->nrbufs = --bufs;
351 break; /* common path: read succeeded */
353 if (bufs) /* More to do? */
357 if (!pipe->waiting_writers) {
358 /* syscall merging: Usually we must not sleep
359 * if O_NONBLOCK is set, or if we got some data.
360 * But if a writer sleeps in kernel space, then
361 * we can wait for that data without violating POSIX.
365 if (filp->f_flags & O_NONBLOCK) {
370 if (signal_pending(current)) {
376 wake_up_interruptible_sync(&pipe->wait);
377 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
381 mutex_unlock(&inode->i_mutex);
383 /* Signal writers asynchronously that there is more room. */
385 wake_up_interruptible_sync(&pipe->wait);
386 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
394 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
395 unsigned long nr_segs, loff_t ppos)
397 struct file *filp = iocb->ki_filp;
398 struct inode *inode = filp->f_path.dentry->d_inode;
399 struct pipe_inode_info *pipe;
402 struct iovec *iov = (struct iovec *)_iov;
406 total_len = iov_length(iov, nr_segs);
407 /* Null write succeeds. */
408 if (unlikely(total_len == 0))
413 mutex_lock(&inode->i_mutex);
414 pipe = inode->i_pipe;
416 if (!pipe->readers) {
417 send_sig(SIGPIPE, current, 0);
422 /* We try to merge small writes */
423 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
424 if (pipe->nrbufs && chars != 0) {
425 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
427 struct pipe_buffer *buf = pipe->bufs + lastbuf;
428 const struct pipe_buf_operations *ops = buf->ops;
429 int offset = buf->offset + buf->len;
431 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
432 int error, atomic = 1;
435 error = ops->confirm(pipe, buf);
439 iov_fault_in_pages_read(iov, chars);
441 addr = ops->map(pipe, buf, atomic);
442 error = pipe_iov_copy_from_user(offset + addr, iov,
444 ops->unmap(pipe, buf, addr);
465 if (!pipe->readers) {
466 send_sig(SIGPIPE, current, 0);
472 if (bufs < PIPE_BUFFERS) {
473 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
474 struct pipe_buffer *buf = pipe->bufs + newbuf;
475 struct page *page = pipe->tmp_page;
477 int error, atomic = 1;
480 page = alloc_page(GFP_HIGHUSER);
481 if (unlikely(!page)) {
482 ret = ret ? : -ENOMEM;
485 pipe->tmp_page = page;
487 /* Always wake up, even if the copy fails. Otherwise
488 * we lock up (O_NONBLOCK-)readers that sleep due to
490 * FIXME! Is this really true?
494 if (chars > total_len)
497 iov_fault_in_pages_read(iov, chars);
500 src = kmap_atomic(page, KM_USER0);
504 error = pipe_iov_copy_from_user(src, iov, chars,
507 kunmap_atomic(src, KM_USER0);
511 if (unlikely(error)) {
522 /* Insert it into the buffer array */
524 buf->ops = &anon_pipe_buf_ops;
527 pipe->nrbufs = ++bufs;
528 pipe->tmp_page = NULL;
534 if (bufs < PIPE_BUFFERS)
536 if (filp->f_flags & O_NONBLOCK) {
541 if (signal_pending(current)) {
547 wake_up_interruptible_sync(&pipe->wait);
548 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
551 pipe->waiting_writers++;
553 pipe->waiting_writers--;
556 mutex_unlock(&inode->i_mutex);
558 wake_up_interruptible_sync(&pipe->wait);
559 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
562 file_update_time(filp);
567 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
573 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
580 pipe_ioctl(struct inode *pino, struct file *filp,
581 unsigned int cmd, unsigned long arg)
583 struct inode *inode = filp->f_path.dentry->d_inode;
584 struct pipe_inode_info *pipe;
585 int count, buf, nrbufs;
589 mutex_lock(&inode->i_mutex);
590 pipe = inode->i_pipe;
593 nrbufs = pipe->nrbufs;
594 while (--nrbufs >= 0) {
595 count += pipe->bufs[buf].len;
596 buf = (buf+1) & (PIPE_BUFFERS-1);
598 mutex_unlock(&inode->i_mutex);
600 return put_user(count, (int __user *)arg);
606 /* No kernel lock held - fine */
608 pipe_poll(struct file *filp, poll_table *wait)
611 struct inode *inode = filp->f_path.dentry->d_inode;
612 struct pipe_inode_info *pipe = inode->i_pipe;
615 poll_wait(filp, &pipe->wait, wait);
617 /* Reading only -- no need for acquiring the semaphore. */
618 nrbufs = pipe->nrbufs;
620 if (filp->f_mode & FMODE_READ) {
621 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
622 if (!pipe->writers && filp->f_version != pipe->w_counter)
626 if (filp->f_mode & FMODE_WRITE) {
627 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
629 * Most Unices do not set POLLERR for FIFOs but on Linux they
630 * behave exactly like pipes for poll().
640 pipe_release(struct inode *inode, int decr, int decw)
642 struct pipe_inode_info *pipe;
644 mutex_lock(&inode->i_mutex);
645 pipe = inode->i_pipe;
646 pipe->readers -= decr;
647 pipe->writers -= decw;
649 if (!pipe->readers && !pipe->writers) {
650 free_pipe_info(inode);
652 wake_up_interruptible_sync(&pipe->wait);
653 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
654 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
656 mutex_unlock(&inode->i_mutex);
662 pipe_read_fasync(int fd, struct file *filp, int on)
664 struct inode *inode = filp->f_path.dentry->d_inode;
667 mutex_lock(&inode->i_mutex);
668 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
669 mutex_unlock(&inode->i_mutex);
679 pipe_write_fasync(int fd, struct file *filp, int on)
681 struct inode *inode = filp->f_path.dentry->d_inode;
684 mutex_lock(&inode->i_mutex);
685 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
686 mutex_unlock(&inode->i_mutex);
696 pipe_rdwr_fasync(int fd, struct file *filp, int on)
698 struct inode *inode = filp->f_path.dentry->d_inode;
699 struct pipe_inode_info *pipe = inode->i_pipe;
702 mutex_lock(&inode->i_mutex);
704 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
707 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
709 mutex_unlock(&inode->i_mutex);
719 pipe_read_release(struct inode *inode, struct file *filp)
721 pipe_read_fasync(-1, filp, 0);
722 return pipe_release(inode, 1, 0);
726 pipe_write_release(struct inode *inode, struct file *filp)
728 pipe_write_fasync(-1, filp, 0);
729 return pipe_release(inode, 0, 1);
733 pipe_rdwr_release(struct inode *inode, struct file *filp)
737 pipe_rdwr_fasync(-1, filp, 0);
738 decr = (filp->f_mode & FMODE_READ) != 0;
739 decw = (filp->f_mode & FMODE_WRITE) != 0;
740 return pipe_release(inode, decr, decw);
744 pipe_read_open(struct inode *inode, struct file *filp)
746 /* We could have perhaps used atomic_t, but this and friends
747 below are the only places. So it doesn't seem worthwhile. */
748 mutex_lock(&inode->i_mutex);
749 inode->i_pipe->readers++;
750 mutex_unlock(&inode->i_mutex);
756 pipe_write_open(struct inode *inode, struct file *filp)
758 mutex_lock(&inode->i_mutex);
759 inode->i_pipe->writers++;
760 mutex_unlock(&inode->i_mutex);
766 pipe_rdwr_open(struct inode *inode, struct file *filp)
768 mutex_lock(&inode->i_mutex);
769 if (filp->f_mode & FMODE_READ)
770 inode->i_pipe->readers++;
771 if (filp->f_mode & FMODE_WRITE)
772 inode->i_pipe->writers++;
773 mutex_unlock(&inode->i_mutex);
779 * The file_operations structs are not static because they
780 * are also used in linux/fs/fifo.c to do operations on FIFOs.
782 const struct file_operations read_fifo_fops = {
784 .read = do_sync_read,
785 .aio_read = pipe_read,
789 .open = pipe_read_open,
790 .release = pipe_read_release,
791 .fasync = pipe_read_fasync,
794 const struct file_operations write_fifo_fops = {
797 .write = do_sync_write,
798 .aio_write = pipe_write,
801 .open = pipe_write_open,
802 .release = pipe_write_release,
803 .fasync = pipe_write_fasync,
806 const struct file_operations rdwr_fifo_fops = {
808 .read = do_sync_read,
809 .aio_read = pipe_read,
810 .write = do_sync_write,
811 .aio_write = pipe_write,
814 .open = pipe_rdwr_open,
815 .release = pipe_rdwr_release,
816 .fasync = pipe_rdwr_fasync,
819 static const struct file_operations read_pipe_fops = {
821 .read = do_sync_read,
822 .aio_read = pipe_read,
826 .open = pipe_read_open,
827 .release = pipe_read_release,
828 .fasync = pipe_read_fasync,
831 static const struct file_operations write_pipe_fops = {
834 .write = do_sync_write,
835 .aio_write = pipe_write,
838 .open = pipe_write_open,
839 .release = pipe_write_release,
840 .fasync = pipe_write_fasync,
843 static const struct file_operations rdwr_pipe_fops = {
845 .read = do_sync_read,
846 .aio_read = pipe_read,
847 .write = do_sync_write,
848 .aio_write = pipe_write,
851 .open = pipe_rdwr_open,
852 .release = pipe_rdwr_release,
853 .fasync = pipe_rdwr_fasync,
856 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
858 struct pipe_inode_info *pipe;
860 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
862 init_waitqueue_head(&pipe->wait);
863 pipe->r_counter = pipe->w_counter = 1;
870 void __free_pipe_info(struct pipe_inode_info *pipe)
874 for (i = 0; i < PIPE_BUFFERS; i++) {
875 struct pipe_buffer *buf = pipe->bufs + i;
877 buf->ops->release(pipe, buf);
880 __free_page(pipe->tmp_page);
884 void free_pipe_info(struct inode *inode)
886 __free_pipe_info(inode->i_pipe);
887 inode->i_pipe = NULL;
890 static struct vfsmount *pipe_mnt __read_mostly;
891 static int pipefs_delete_dentry(struct dentry *dentry)
894 * At creation time, we pretended this dentry was hashed
895 * (by clearing DCACHE_UNHASHED bit in d_flags)
896 * At delete time, we restore the truth : not hashed.
897 * (so that dput() can proceed correctly)
899 dentry->d_flags |= DCACHE_UNHASHED;
904 * pipefs_dname() is called from d_path().
906 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
908 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
909 dentry->d_inode->i_ino);
912 static struct dentry_operations pipefs_dentry_operations = {
913 .d_delete = pipefs_delete_dentry,
914 .d_dname = pipefs_dname,
917 static struct inode * get_pipe_inode(void)
919 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
920 struct pipe_inode_info *pipe;
925 pipe = alloc_pipe_info(inode);
928 inode->i_pipe = pipe;
930 pipe->readers = pipe->writers = 1;
931 inode->i_fop = &rdwr_pipe_fops;
934 * Mark the inode dirty from the very beginning,
935 * that way it will never be moved to the dirty
936 * list because "mark_inode_dirty()" will think
937 * that it already _is_ on the dirty list.
939 inode->i_state = I_DIRTY;
940 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
941 inode->i_uid = current->fsuid;
942 inode->i_gid = current->fsgid;
943 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
954 struct file *create_write_pipe(void)
959 struct dentry *dentry;
960 struct qstr name = { .name = "" };
962 f = get_empty_filp();
964 return ERR_PTR(-ENFILE);
966 inode = get_pipe_inode();
971 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name);
975 dentry->d_op = &pipefs_dentry_operations;
977 * We dont want to publish this dentry into global dentry hash table.
978 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
979 * This permits a working /proc/$pid/fd/XXX on pipes
981 dentry->d_flags &= ~DCACHE_UNHASHED;
982 d_instantiate(dentry, inode);
983 f->f_path.mnt = mntget(pipe_mnt);
984 f->f_path.dentry = dentry;
985 f->f_mapping = inode->i_mapping;
987 f->f_flags = O_WRONLY;
988 f->f_op = &write_pipe_fops;
989 f->f_mode = FMODE_WRITE;
995 free_pipe_info(inode);
1002 void free_write_pipe(struct file *f)
1004 free_pipe_info(f->f_dentry->d_inode);
1005 dput(f->f_path.dentry);
1006 mntput(f->f_path.mnt);
1010 struct file *create_read_pipe(struct file *wrf)
1012 struct file *f = get_empty_filp();
1014 return ERR_PTR(-ENFILE);
1016 /* Grab pipe from the writer */
1017 f->f_path.mnt = mntget(wrf->f_path.mnt);
1018 f->f_path.dentry = dget(wrf->f_path.dentry);
1019 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
1022 f->f_flags = O_RDONLY;
1023 f->f_op = &read_pipe_fops;
1024 f->f_mode = FMODE_READ;
1030 int do_pipe(int *fd)
1032 struct file *fw, *fr;
1036 fw = create_write_pipe();
1039 fr = create_read_pipe(fw);
1040 error = PTR_ERR(fr);
1042 goto err_write_pipe;
1044 error = get_unused_fd();
1049 error = get_unused_fd();
1054 error = audit_fd_pair(fdr, fdw);
1058 fd_install(fdr, fr);
1059 fd_install(fdw, fw);
1071 mntput(fr->f_vfsmnt);
1074 free_write_pipe(fw);
1079 * pipefs should _never_ be mounted by userland - too much of security hassle,
1080 * no real gain from having the whole whorehouse mounted. So we don't need
1081 * any operations on the root directory. However, we need a non-trivial
1082 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1084 static int pipefs_get_sb(struct file_system_type *fs_type,
1085 int flags, const char *dev_name, void *data,
1086 struct vfsmount *mnt)
1088 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
1091 static struct file_system_type pipe_fs_type = {
1093 .get_sb = pipefs_get_sb,
1094 .kill_sb = kill_anon_super,
1097 static int __init init_pipe_fs(void)
1099 int err = register_filesystem(&pipe_fs_type);
1102 pipe_mnt = kern_mount(&pipe_fs_type);
1103 if (IS_ERR(pipe_mnt)) {
1104 err = PTR_ERR(pipe_mnt);
1105 unregister_filesystem(&pipe_fs_type);
1111 static void __exit exit_pipe_fs(void)
1113 unregister_filesystem(&pipe_fs_type);
1117 fs_initcall(init_pipe_fs);
1118 module_exit(exit_pipe_fs);