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);
167 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
168 struct pipe_buffer *buf, int atomic)
171 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
172 return kmap_atomic(buf->page, KM_USER0);
175 return kmap(buf->page);
178 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
179 struct pipe_buffer *buf, void *map_data)
181 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
182 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
183 kunmap_atomic(map_data, KM_USER0);
188 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
189 struct pipe_buffer *buf)
191 struct page *page = buf->page;
193 if (page_count(page) == 1) {
201 void generic_pipe_buf_get(struct pipe_inode_info *info, struct pipe_buffer *buf)
203 page_cache_get(buf->page);
206 int generic_pipe_buf_pin(struct pipe_inode_info *info, struct pipe_buffer *buf)
211 static const struct pipe_buf_operations anon_pipe_buf_ops = {
213 .map = generic_pipe_buf_map,
214 .unmap = generic_pipe_buf_unmap,
215 .pin = generic_pipe_buf_pin,
216 .release = anon_pipe_buf_release,
217 .steal = generic_pipe_buf_steal,
218 .get = generic_pipe_buf_get,
222 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
223 unsigned long nr_segs, loff_t pos)
225 struct file *filp = iocb->ki_filp;
226 struct inode *inode = filp->f_path.dentry->d_inode;
227 struct pipe_inode_info *pipe;
230 struct iovec *iov = (struct iovec *)_iov;
233 total_len = iov_length(iov, nr_segs);
234 /* Null read succeeds. */
235 if (unlikely(total_len == 0))
240 mutex_lock(&inode->i_mutex);
241 pipe = inode->i_pipe;
243 int bufs = pipe->nrbufs;
245 int curbuf = pipe->curbuf;
246 struct pipe_buffer *buf = pipe->bufs + curbuf;
247 const struct pipe_buf_operations *ops = buf->ops;
249 size_t chars = buf->len;
252 if (chars > total_len)
255 error = ops->pin(pipe, buf);
262 atomic = !iov_fault_in_pages_write(iov, chars);
264 addr = ops->map(pipe, buf, atomic);
265 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars, atomic);
266 ops->unmap(pipe, buf, addr);
267 if (unlikely(error)) {
269 * Just retry with the slow path if we failed.
280 buf->offset += chars;
284 ops->release(pipe, buf);
285 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
286 pipe->curbuf = curbuf;
287 pipe->nrbufs = --bufs;
292 break; /* common path: read succeeded */
294 if (bufs) /* More to do? */
298 if (!pipe->waiting_writers) {
299 /* syscall merging: Usually we must not sleep
300 * if O_NONBLOCK is set, or if we got some data.
301 * But if a writer sleeps in kernel space, then
302 * we can wait for that data without violating POSIX.
306 if (filp->f_flags & O_NONBLOCK) {
311 if (signal_pending(current)) {
317 wake_up_interruptible_sync(&pipe->wait);
318 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
322 mutex_unlock(&inode->i_mutex);
324 /* Signal writers asynchronously that there is more room. */
326 wake_up_interruptible(&pipe->wait);
327 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
335 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
336 unsigned long nr_segs, loff_t ppos)
338 struct file *filp = iocb->ki_filp;
339 struct inode *inode = filp->f_path.dentry->d_inode;
340 struct pipe_inode_info *pipe;
343 struct iovec *iov = (struct iovec *)_iov;
347 total_len = iov_length(iov, nr_segs);
348 /* Null write succeeds. */
349 if (unlikely(total_len == 0))
354 mutex_lock(&inode->i_mutex);
355 pipe = inode->i_pipe;
357 if (!pipe->readers) {
358 send_sig(SIGPIPE, current, 0);
363 /* We try to merge small writes */
364 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
365 if (pipe->nrbufs && chars != 0) {
366 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
368 struct pipe_buffer *buf = pipe->bufs + lastbuf;
369 const struct pipe_buf_operations *ops = buf->ops;
370 int offset = buf->offset + buf->len;
372 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
373 int error, atomic = 1;
376 error = ops->pin(pipe, buf);
380 iov_fault_in_pages_read(iov, chars);
382 addr = ops->map(pipe, buf, atomic);
383 error = pipe_iov_copy_from_user(offset + addr, iov,
385 ops->unmap(pipe, buf, addr);
406 if (!pipe->readers) {
407 send_sig(SIGPIPE, current, 0);
413 if (bufs < PIPE_BUFFERS) {
414 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
415 struct pipe_buffer *buf = pipe->bufs + newbuf;
416 struct page *page = pipe->tmp_page;
418 int error, atomic = 1;
421 page = alloc_page(GFP_HIGHUSER);
422 if (unlikely(!page)) {
423 ret = ret ? : -ENOMEM;
426 pipe->tmp_page = page;
428 /* Always wake up, even if the copy fails. Otherwise
429 * we lock up (O_NONBLOCK-)readers that sleep due to
431 * FIXME! Is this really true?
435 if (chars > total_len)
438 iov_fault_in_pages_read(iov, chars);
441 src = kmap_atomic(page, KM_USER0);
445 error = pipe_iov_copy_from_user(src, iov, chars,
448 kunmap_atomic(src, KM_USER0);
452 if (unlikely(error)) {
463 /* Insert it into the buffer array */
465 buf->ops = &anon_pipe_buf_ops;
468 pipe->nrbufs = ++bufs;
469 pipe->tmp_page = NULL;
475 if (bufs < PIPE_BUFFERS)
477 if (filp->f_flags & O_NONBLOCK) {
482 if (signal_pending(current)) {
488 wake_up_interruptible_sync(&pipe->wait);
489 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
492 pipe->waiting_writers++;
494 pipe->waiting_writers--;
497 mutex_unlock(&inode->i_mutex);
499 wake_up_interruptible(&pipe->wait);
500 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
503 file_update_time(filp);
508 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
514 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
521 pipe_ioctl(struct inode *pino, struct file *filp,
522 unsigned int cmd, unsigned long arg)
524 struct inode *inode = filp->f_path.dentry->d_inode;
525 struct pipe_inode_info *pipe;
526 int count, buf, nrbufs;
530 mutex_lock(&inode->i_mutex);
531 pipe = inode->i_pipe;
534 nrbufs = pipe->nrbufs;
535 while (--nrbufs >= 0) {
536 count += pipe->bufs[buf].len;
537 buf = (buf+1) & (PIPE_BUFFERS-1);
539 mutex_unlock(&inode->i_mutex);
541 return put_user(count, (int __user *)arg);
547 /* No kernel lock held - fine */
549 pipe_poll(struct file *filp, poll_table *wait)
552 struct inode *inode = filp->f_path.dentry->d_inode;
553 struct pipe_inode_info *pipe = inode->i_pipe;
556 poll_wait(filp, &pipe->wait, wait);
558 /* Reading only -- no need for acquiring the semaphore. */
559 nrbufs = pipe->nrbufs;
561 if (filp->f_mode & FMODE_READ) {
562 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
563 if (!pipe->writers && filp->f_version != pipe->w_counter)
567 if (filp->f_mode & FMODE_WRITE) {
568 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
570 * Most Unices do not set POLLERR for FIFOs but on Linux they
571 * behave exactly like pipes for poll().
581 pipe_release(struct inode *inode, int decr, int decw)
583 struct pipe_inode_info *pipe;
585 mutex_lock(&inode->i_mutex);
586 pipe = inode->i_pipe;
587 pipe->readers -= decr;
588 pipe->writers -= decw;
590 if (!pipe->readers && !pipe->writers) {
591 free_pipe_info(inode);
593 wake_up_interruptible(&pipe->wait);
594 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
595 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
597 mutex_unlock(&inode->i_mutex);
603 pipe_read_fasync(int fd, struct file *filp, int on)
605 struct inode *inode = filp->f_path.dentry->d_inode;
608 mutex_lock(&inode->i_mutex);
609 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
610 mutex_unlock(&inode->i_mutex);
620 pipe_write_fasync(int fd, struct file *filp, int on)
622 struct inode *inode = filp->f_path.dentry->d_inode;
625 mutex_lock(&inode->i_mutex);
626 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
627 mutex_unlock(&inode->i_mutex);
637 pipe_rdwr_fasync(int fd, struct file *filp, int on)
639 struct inode *inode = filp->f_path.dentry->d_inode;
640 struct pipe_inode_info *pipe = inode->i_pipe;
643 mutex_lock(&inode->i_mutex);
645 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
648 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
650 mutex_unlock(&inode->i_mutex);
660 pipe_read_release(struct inode *inode, struct file *filp)
662 pipe_read_fasync(-1, filp, 0);
663 return pipe_release(inode, 1, 0);
667 pipe_write_release(struct inode *inode, struct file *filp)
669 pipe_write_fasync(-1, filp, 0);
670 return pipe_release(inode, 0, 1);
674 pipe_rdwr_release(struct inode *inode, struct file *filp)
678 pipe_rdwr_fasync(-1, filp, 0);
679 decr = (filp->f_mode & FMODE_READ) != 0;
680 decw = (filp->f_mode & FMODE_WRITE) != 0;
681 return pipe_release(inode, decr, decw);
685 pipe_read_open(struct inode *inode, struct file *filp)
687 /* We could have perhaps used atomic_t, but this and friends
688 below are the only places. So it doesn't seem worthwhile. */
689 mutex_lock(&inode->i_mutex);
690 inode->i_pipe->readers++;
691 mutex_unlock(&inode->i_mutex);
697 pipe_write_open(struct inode *inode, struct file *filp)
699 mutex_lock(&inode->i_mutex);
700 inode->i_pipe->writers++;
701 mutex_unlock(&inode->i_mutex);
707 pipe_rdwr_open(struct inode *inode, struct file *filp)
709 mutex_lock(&inode->i_mutex);
710 if (filp->f_mode & FMODE_READ)
711 inode->i_pipe->readers++;
712 if (filp->f_mode & FMODE_WRITE)
713 inode->i_pipe->writers++;
714 mutex_unlock(&inode->i_mutex);
720 * The file_operations structs are not static because they
721 * are also used in linux/fs/fifo.c to do operations on FIFOs.
723 const struct file_operations read_fifo_fops = {
725 .read = do_sync_read,
726 .aio_read = pipe_read,
730 .open = pipe_read_open,
731 .release = pipe_read_release,
732 .fasync = pipe_read_fasync,
735 const struct file_operations write_fifo_fops = {
738 .write = do_sync_write,
739 .aio_write = pipe_write,
742 .open = pipe_write_open,
743 .release = pipe_write_release,
744 .fasync = pipe_write_fasync,
747 const struct file_operations rdwr_fifo_fops = {
749 .read = do_sync_read,
750 .aio_read = pipe_read,
751 .write = do_sync_write,
752 .aio_write = pipe_write,
755 .open = pipe_rdwr_open,
756 .release = pipe_rdwr_release,
757 .fasync = pipe_rdwr_fasync,
760 static const struct file_operations read_pipe_fops = {
762 .read = do_sync_read,
763 .aio_read = pipe_read,
767 .open = pipe_read_open,
768 .release = pipe_read_release,
769 .fasync = pipe_read_fasync,
772 static const struct file_operations write_pipe_fops = {
775 .write = do_sync_write,
776 .aio_write = pipe_write,
779 .open = pipe_write_open,
780 .release = pipe_write_release,
781 .fasync = pipe_write_fasync,
784 static const struct file_operations rdwr_pipe_fops = {
786 .read = do_sync_read,
787 .aio_read = pipe_read,
788 .write = do_sync_write,
789 .aio_write = pipe_write,
792 .open = pipe_rdwr_open,
793 .release = pipe_rdwr_release,
794 .fasync = pipe_rdwr_fasync,
797 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
799 struct pipe_inode_info *pipe;
801 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
803 init_waitqueue_head(&pipe->wait);
804 pipe->r_counter = pipe->w_counter = 1;
811 void __free_pipe_info(struct pipe_inode_info *pipe)
815 for (i = 0; i < PIPE_BUFFERS; i++) {
816 struct pipe_buffer *buf = pipe->bufs + i;
818 buf->ops->release(pipe, buf);
821 __free_page(pipe->tmp_page);
825 void free_pipe_info(struct inode *inode)
827 __free_pipe_info(inode->i_pipe);
828 inode->i_pipe = NULL;
831 static struct vfsmount *pipe_mnt __read_mostly;
832 static int pipefs_delete_dentry(struct dentry *dentry)
835 * At creation time, we pretended this dentry was hashed
836 * (by clearing DCACHE_UNHASHED bit in d_flags)
837 * At delete time, we restore the truth : not hashed.
838 * (so that dput() can proceed correctly)
840 dentry->d_flags |= DCACHE_UNHASHED;
844 static struct dentry_operations pipefs_dentry_operations = {
845 .d_delete = pipefs_delete_dentry,
848 static struct inode * get_pipe_inode(void)
850 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
851 struct pipe_inode_info *pipe;
856 pipe = alloc_pipe_info(inode);
859 inode->i_pipe = pipe;
861 pipe->readers = pipe->writers = 1;
862 inode->i_fop = &rdwr_pipe_fops;
865 * Mark the inode dirty from the very beginning,
866 * that way it will never be moved to the dirty
867 * list because "mark_inode_dirty()" will think
868 * that it already _is_ on the dirty list.
870 inode->i_state = I_DIRTY;
871 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
872 inode->i_uid = current->fsuid;
873 inode->i_gid = current->fsgid;
874 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
885 struct file *create_write_pipe(void)
890 struct dentry *dentry;
894 f = get_empty_filp();
896 return ERR_PTR(-ENFILE);
898 inode = get_pipe_inode();
902 this.len = sprintf(name, "[%lu]", inode->i_ino);
906 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
910 dentry->d_op = &pipefs_dentry_operations;
912 * We dont want to publish this dentry into global dentry hash table.
913 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
914 * This permits a working /proc/$pid/fd/XXX on pipes
916 dentry->d_flags &= ~DCACHE_UNHASHED;
917 d_instantiate(dentry, inode);
918 f->f_path.mnt = mntget(pipe_mnt);
919 f->f_path.dentry = dentry;
920 f->f_mapping = inode->i_mapping;
922 f->f_flags = O_WRONLY;
923 f->f_op = &write_pipe_fops;
924 f->f_mode = FMODE_WRITE;
930 free_pipe_info(inode);
937 void free_write_pipe(struct file *f)
939 free_pipe_info(f->f_dentry->d_inode);
940 dput(f->f_path.dentry);
941 mntput(f->f_path.mnt);
945 struct file *create_read_pipe(struct file *wrf)
947 struct file *f = get_empty_filp();
949 return ERR_PTR(-ENFILE);
951 /* Grab pipe from the writer */
952 f->f_path.mnt = mntget(wrf->f_path.mnt);
953 f->f_path.dentry = dget(wrf->f_path.dentry);
954 f->f_mapping = wrf->f_path.dentry->d_inode->i_mapping;
957 f->f_flags = O_RDONLY;
958 f->f_op = &read_pipe_fops;
959 f->f_mode = FMODE_READ;
967 struct file *fw, *fr;
971 fw = create_write_pipe();
974 fr = create_read_pipe(fw);
979 error = get_unused_fd();
984 error = get_unused_fd();
989 error = audit_fd_pair(fdr, fdw);
1006 mntput(fr->f_vfsmnt);
1009 free_write_pipe(fw);
1014 * pipefs should _never_ be mounted by userland - too much of security hassle,
1015 * no real gain from having the whole whorehouse mounted. So we don't need
1016 * any operations on the root directory. However, we need a non-trivial
1017 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1019 static int pipefs_get_sb(struct file_system_type *fs_type,
1020 int flags, const char *dev_name, void *data,
1021 struct vfsmount *mnt)
1023 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC, mnt);
1026 static struct file_system_type pipe_fs_type = {
1028 .get_sb = pipefs_get_sb,
1029 .kill_sb = kill_anon_super,
1032 static int __init init_pipe_fs(void)
1034 int err = register_filesystem(&pipe_fs_type);
1037 pipe_mnt = kern_mount(&pipe_fs_type);
1038 if (IS_ERR(pipe_mnt)) {
1039 err = PTR_ERR(pipe_mnt);
1040 unregister_filesystem(&pipe_fs_type);
1046 static void __exit exit_pipe_fs(void)
1048 unregister_filesystem(&pipe_fs_type);
1052 fs_initcall(init_pipe_fs);
1053 module_exit(exit_pipe_fs);