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)
65 copy = min_t(unsigned long, len, iov->iov_len);
67 if (copy_from_user(to, iov->iov_base, copy))
71 iov->iov_base += copy;
78 pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
85 copy = min_t(unsigned long, len, iov->iov_len);
87 if (copy_to_user(iov->iov_base, from, copy))
91 iov->iov_base += copy;
97 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
98 struct pipe_buffer *buf)
100 struct page *page = buf->page;
103 * If nobody else uses this page, and we don't already have a
104 * temporary page, let's keep track of it as a one-deep
105 * allocation cache. (Otherwise just release our reference to it)
107 if (page_count(page) == 1 && !pipe->tmp_page)
108 pipe->tmp_page = page;
110 page_cache_release(page);
113 static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe,
114 struct pipe_buffer *buf)
116 return kmap(buf->page);
119 static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe,
120 struct pipe_buffer *buf)
125 static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
126 struct pipe_buffer *buf)
128 struct page *page = buf->page;
130 if (page_count(page) == 1) {
138 static void anon_pipe_buf_get(struct pipe_inode_info *info,
139 struct pipe_buffer *buf)
141 page_cache_get(buf->page);
144 static struct pipe_buf_operations anon_pipe_buf_ops = {
146 .map = anon_pipe_buf_map,
147 .unmap = anon_pipe_buf_unmap,
148 .release = anon_pipe_buf_release,
149 .steal = anon_pipe_buf_steal,
150 .get = anon_pipe_buf_get,
154 pipe_readv(struct file *filp, const struct iovec *_iov,
155 unsigned long nr_segs, loff_t *ppos)
157 struct inode *inode = filp->f_dentry->d_inode;
158 struct pipe_inode_info *pipe;
161 struct iovec *iov = (struct iovec *)_iov;
164 total_len = iov_length(iov, nr_segs);
165 /* Null read succeeds. */
166 if (unlikely(total_len == 0))
171 mutex_lock(&inode->i_mutex);
172 pipe = inode->i_pipe;
174 int bufs = pipe->nrbufs;
176 int curbuf = pipe->curbuf;
177 struct pipe_buffer *buf = pipe->bufs + curbuf;
178 struct pipe_buf_operations *ops = buf->ops;
180 size_t chars = buf->len;
183 if (chars > total_len)
186 addr = ops->map(filp, pipe, buf);
192 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
193 ops->unmap(pipe, buf);
194 if (unlikely(error)) {
200 buf->offset += chars;
204 ops->release(pipe, buf);
205 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
206 pipe->curbuf = curbuf;
207 pipe->nrbufs = --bufs;
212 break; /* common path: read succeeded */
214 if (bufs) /* More to do? */
218 if (!pipe->waiting_writers) {
219 /* syscall merging: Usually we must not sleep
220 * if O_NONBLOCK is set, or if we got some data.
221 * But if a writer sleeps in kernel space, then
222 * we can wait for that data without violating POSIX.
226 if (filp->f_flags & O_NONBLOCK) {
231 if (signal_pending(current)) {
237 wake_up_interruptible_sync(&pipe->wait);
238 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
242 mutex_unlock(&inode->i_mutex);
244 /* Signal writers asynchronously that there is more room. */
246 wake_up_interruptible(&pipe->wait);
247 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
255 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
257 struct iovec iov = { .iov_base = buf, .iov_len = count };
259 return pipe_readv(filp, &iov, 1, ppos);
263 pipe_writev(struct file *filp, const struct iovec *_iov,
264 unsigned long nr_segs, loff_t *ppos)
266 struct inode *inode = filp->f_dentry->d_inode;
267 struct pipe_inode_info *pipe;
270 struct iovec *iov = (struct iovec *)_iov;
274 total_len = iov_length(iov, nr_segs);
275 /* Null write succeeds. */
276 if (unlikely(total_len == 0))
281 mutex_lock(&inode->i_mutex);
282 pipe = inode->i_pipe;
284 if (!pipe->readers) {
285 send_sig(SIGPIPE, current, 0);
290 /* We try to merge small writes */
291 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
292 if (pipe->nrbufs && chars != 0) {
293 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
295 struct pipe_buffer *buf = pipe->bufs + lastbuf;
296 struct pipe_buf_operations *ops = buf->ops;
297 int offset = buf->offset + buf->len;
299 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
303 addr = ops->map(filp, pipe, buf);
305 error = PTR_ERR(addr);
308 error = pipe_iov_copy_from_user(offset + addr, iov,
310 ops->unmap(pipe, buf);
326 if (!pipe->readers) {
327 send_sig(SIGPIPE, current, 0);
333 if (bufs < PIPE_BUFFERS) {
334 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
335 struct pipe_buffer *buf = pipe->bufs + newbuf;
336 struct page *page = pipe->tmp_page;
340 page = alloc_page(GFP_HIGHUSER);
341 if (unlikely(!page)) {
342 ret = ret ? : -ENOMEM;
345 pipe->tmp_page = page;
347 /* Always wake up, even if the copy fails. Otherwise
348 * we lock up (O_NONBLOCK-)readers that sleep due to
350 * FIXME! Is this really true?
354 if (chars > total_len)
357 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
359 if (unlikely(error)) {
366 /* Insert it into the buffer array */
368 buf->ops = &anon_pipe_buf_ops;
371 pipe->nrbufs = ++bufs;
372 pipe->tmp_page = NULL;
378 if (bufs < PIPE_BUFFERS)
380 if (filp->f_flags & O_NONBLOCK) {
385 if (signal_pending(current)) {
391 wake_up_interruptible_sync(&pipe->wait);
392 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
395 pipe->waiting_writers++;
397 pipe->waiting_writers--;
400 mutex_unlock(&inode->i_mutex);
402 wake_up_interruptible(&pipe->wait);
403 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
406 file_update_time(filp);
411 pipe_write(struct file *filp, const char __user *buf,
412 size_t count, loff_t *ppos)
414 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
416 return pipe_writev(filp, &iov, 1, ppos);
420 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
426 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
433 pipe_ioctl(struct inode *pino, struct file *filp,
434 unsigned int cmd, unsigned long arg)
436 struct inode *inode = filp->f_dentry->d_inode;
437 struct pipe_inode_info *pipe;
438 int count, buf, nrbufs;
442 mutex_lock(&inode->i_mutex);
443 pipe = inode->i_pipe;
446 nrbufs = pipe->nrbufs;
447 while (--nrbufs >= 0) {
448 count += pipe->bufs[buf].len;
449 buf = (buf+1) & (PIPE_BUFFERS-1);
451 mutex_unlock(&inode->i_mutex);
453 return put_user(count, (int __user *)arg);
459 /* No kernel lock held - fine */
461 pipe_poll(struct file *filp, poll_table *wait)
464 struct inode *inode = filp->f_dentry->d_inode;
465 struct pipe_inode_info *pipe = inode->i_pipe;
468 poll_wait(filp, &pipe->wait, wait);
470 /* Reading only -- no need for acquiring the semaphore. */
471 nrbufs = pipe->nrbufs;
473 if (filp->f_mode & FMODE_READ) {
474 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
475 if (!pipe->writers && filp->f_version != pipe->w_counter)
479 if (filp->f_mode & FMODE_WRITE) {
480 mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
482 * Most Unices do not set POLLERR for FIFOs but on Linux they
483 * behave exactly like pipes for poll().
493 pipe_release(struct inode *inode, int decr, int decw)
495 struct pipe_inode_info *pipe;
497 mutex_lock(&inode->i_mutex);
498 pipe = inode->i_pipe;
499 pipe->readers -= decr;
500 pipe->writers -= decw;
502 if (!pipe->readers && !pipe->writers) {
503 free_pipe_info(inode);
505 wake_up_interruptible(&pipe->wait);
506 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
507 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
509 mutex_unlock(&inode->i_mutex);
515 pipe_read_fasync(int fd, struct file *filp, int on)
517 struct inode *inode = filp->f_dentry->d_inode;
520 mutex_lock(&inode->i_mutex);
521 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
522 mutex_unlock(&inode->i_mutex);
532 pipe_write_fasync(int fd, struct file *filp, int on)
534 struct inode *inode = filp->f_dentry->d_inode;
537 mutex_lock(&inode->i_mutex);
538 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
539 mutex_unlock(&inode->i_mutex);
549 pipe_rdwr_fasync(int fd, struct file *filp, int on)
551 struct inode *inode = filp->f_dentry->d_inode;
552 struct pipe_inode_info *pipe = inode->i_pipe;
555 mutex_lock(&inode->i_mutex);
557 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
560 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
562 mutex_unlock(&inode->i_mutex);
572 pipe_read_release(struct inode *inode, struct file *filp)
574 pipe_read_fasync(-1, filp, 0);
575 return pipe_release(inode, 1, 0);
579 pipe_write_release(struct inode *inode, struct file *filp)
581 pipe_write_fasync(-1, filp, 0);
582 return pipe_release(inode, 0, 1);
586 pipe_rdwr_release(struct inode *inode, struct file *filp)
590 pipe_rdwr_fasync(-1, filp, 0);
591 decr = (filp->f_mode & FMODE_READ) != 0;
592 decw = (filp->f_mode & FMODE_WRITE) != 0;
593 return pipe_release(inode, decr, decw);
597 pipe_read_open(struct inode *inode, struct file *filp)
599 /* We could have perhaps used atomic_t, but this and friends
600 below are the only places. So it doesn't seem worthwhile. */
601 mutex_lock(&inode->i_mutex);
602 inode->i_pipe->readers++;
603 mutex_unlock(&inode->i_mutex);
609 pipe_write_open(struct inode *inode, struct file *filp)
611 mutex_lock(&inode->i_mutex);
612 inode->i_pipe->writers++;
613 mutex_unlock(&inode->i_mutex);
619 pipe_rdwr_open(struct inode *inode, struct file *filp)
621 mutex_lock(&inode->i_mutex);
622 if (filp->f_mode & FMODE_READ)
623 inode->i_pipe->readers++;
624 if (filp->f_mode & FMODE_WRITE)
625 inode->i_pipe->writers++;
626 mutex_unlock(&inode->i_mutex);
632 * The file_operations structs are not static because they
633 * are also used in linux/fs/fifo.c to do operations on FIFOs.
635 const struct file_operations read_fifo_fops = {
642 .open = pipe_read_open,
643 .release = pipe_read_release,
644 .fasync = pipe_read_fasync,
647 const struct file_operations write_fifo_fops = {
651 .writev = pipe_writev,
654 .open = pipe_write_open,
655 .release = pipe_write_release,
656 .fasync = pipe_write_fasync,
659 const struct file_operations rdwr_fifo_fops = {
664 .writev = pipe_writev,
667 .open = pipe_rdwr_open,
668 .release = pipe_rdwr_release,
669 .fasync = pipe_rdwr_fasync,
672 static struct file_operations read_pipe_fops = {
679 .open = pipe_read_open,
680 .release = pipe_read_release,
681 .fasync = pipe_read_fasync,
684 static struct file_operations write_pipe_fops = {
688 .writev = pipe_writev,
691 .open = pipe_write_open,
692 .release = pipe_write_release,
693 .fasync = pipe_write_fasync,
696 static struct file_operations rdwr_pipe_fops = {
701 .writev = pipe_writev,
704 .open = pipe_rdwr_open,
705 .release = pipe_rdwr_release,
706 .fasync = pipe_rdwr_fasync,
709 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
711 struct pipe_inode_info *pipe;
713 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
715 init_waitqueue_head(&pipe->wait);
716 pipe->r_counter = pipe->w_counter = 1;
723 void __free_pipe_info(struct pipe_inode_info *pipe)
727 for (i = 0; i < PIPE_BUFFERS; i++) {
728 struct pipe_buffer *buf = pipe->bufs + i;
730 buf->ops->release(pipe, buf);
733 __free_page(pipe->tmp_page);
737 void free_pipe_info(struct inode *inode)
739 __free_pipe_info(inode->i_pipe);
740 inode->i_pipe = NULL;
743 static struct vfsmount *pipe_mnt __read_mostly;
744 static int pipefs_delete_dentry(struct dentry *dentry)
749 static struct dentry_operations pipefs_dentry_operations = {
750 .d_delete = pipefs_delete_dentry,
753 static struct inode * get_pipe_inode(void)
755 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
756 struct pipe_inode_info *pipe;
761 pipe = alloc_pipe_info(inode);
764 inode->i_pipe = pipe;
766 pipe->readers = pipe->writers = 1;
767 inode->i_fop = &rdwr_pipe_fops;
770 * Mark the inode dirty from the very beginning,
771 * that way it will never be moved to the dirty
772 * list because "mark_inode_dirty()" will think
773 * that it already _is_ on the dirty list.
775 inode->i_state = I_DIRTY;
776 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
777 inode->i_uid = current->fsuid;
778 inode->i_gid = current->fsgid;
779 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
780 inode->i_blksize = PAGE_SIZE;
795 struct dentry *dentry;
796 struct inode * inode;
797 struct file *f1, *f2;
802 f1 = get_empty_filp();
806 f2 = get_empty_filp();
810 inode = get_pipe_inode();
814 error = get_unused_fd();
816 goto close_f12_inode;
819 error = get_unused_fd();
821 goto close_f12_inode_i;
825 sprintf(name, "[%lu]", inode->i_ino);
827 this.len = strlen(name);
828 this.hash = inode->i_ino; /* will go */
829 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
831 goto close_f12_inode_i_j;
833 dentry->d_op = &pipefs_dentry_operations;
834 d_add(dentry, inode);
835 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
836 f1->f_dentry = f2->f_dentry = dget(dentry);
837 f1->f_mapping = f2->f_mapping = inode->i_mapping;
840 f1->f_pos = f2->f_pos = 0;
841 f1->f_flags = O_RDONLY;
842 f1->f_op = &read_pipe_fops;
843 f1->f_mode = FMODE_READ;
847 f2->f_flags = O_WRONLY;
848 f2->f_op = &write_pipe_fops;
849 f2->f_mode = FMODE_WRITE;
864 free_pipe_info(inode);
875 * pipefs should _never_ be mounted by userland - too much of security hassle,
876 * no real gain from having the whole whorehouse mounted. So we don't need
877 * any operations on the root directory. However, we need a non-trivial
878 * d_name - pipe: will go nicely and kill the special-casing in procfs.
881 static struct super_block *
882 pipefs_get_sb(struct file_system_type *fs_type, int flags,
883 const char *dev_name, void *data)
885 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
888 static struct file_system_type pipe_fs_type = {
890 .get_sb = pipefs_get_sb,
891 .kill_sb = kill_anon_super,
894 static int __init init_pipe_fs(void)
896 int err = register_filesystem(&pipe_fs_type);
899 pipe_mnt = kern_mount(&pipe_fs_type);
900 if (IS_ERR(pipe_mnt)) {
901 err = PTR_ERR(pipe_mnt);
902 unregister_filesystem(&pipe_fs_type);
908 static void __exit exit_pipe_fs(void)
910 unregister_filesystem(&pipe_fs_type);
914 fs_initcall(init_pipe_fs);
915 module_exit(exit_pipe_fs);