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/log2.h>
15 #include <linux/mount.h>
16 #include <linux/pipe_fs_i.h>
17 #include <linux/uio.h>
18 #include <linux/highmem.h>
19 #include <linux/pagemap.h>
20 #include <linux/audit.h>
21 #include <linux/syscalls.h>
22 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
25 #include <asm/ioctls.h>
28 * The max size that a non-root user is allowed to grow the pipe. Can
29 * be set by root in /proc/sys/fs/pipe-max-size
31 unsigned int pipe_max_size = 1048576;
34 * Minimum pipe size, as required by POSIX
36 unsigned int pipe_min_size = PAGE_SIZE;
39 * We use a start+len construction, which provides full use of the
41 * -- Florian Coosmann (FGC)
43 * Reads with count = 0 should always return 0.
44 * -- Julian Bradfield 1999-06-07.
46 * FIFOs and Pipes now generate SIGIO for both readers and writers.
47 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
49 * pipe_read & write cleanup
50 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
53 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
56 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
59 void pipe_lock(struct pipe_inode_info *pipe)
62 * pipe_lock() nests non-pipe inode locks (for writing to a file)
64 pipe_lock_nested(pipe, I_MUTEX_PARENT);
66 EXPORT_SYMBOL(pipe_lock);
68 void pipe_unlock(struct pipe_inode_info *pipe)
71 mutex_unlock(&pipe->inode->i_mutex);
73 EXPORT_SYMBOL(pipe_unlock);
75 void pipe_double_lock(struct pipe_inode_info *pipe1,
76 struct pipe_inode_info *pipe2)
78 BUG_ON(pipe1 == pipe2);
81 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
82 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
84 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
85 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
89 /* Drop the inode semaphore and wait for a pipe event, atomically */
90 void pipe_wait(struct pipe_inode_info *pipe)
95 * Pipes are system-local resources, so sleeping on them
96 * is considered a noninteractive wait:
98 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
101 finish_wait(&pipe->wait, &wait);
106 pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
107 size_t *remaining, int atomic)
111 while (*remaining > 0) {
112 while (!iov->iov_len)
114 copy = min_t(unsigned long, *remaining, iov->iov_len);
117 if (__copy_from_user_inatomic(addr + *offset,
118 iov->iov_base, copy))
121 if (copy_from_user(addr + *offset,
122 iov->iov_base, copy))
127 iov->iov_base += copy;
128 iov->iov_len -= copy;
134 pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
135 size_t *remaining, int atomic)
139 while (*remaining > 0) {
140 while (!iov->iov_len)
142 copy = min_t(unsigned long, *remaining, iov->iov_len);
145 if (__copy_to_user_inatomic(iov->iov_base,
146 addr + *offset, copy))
149 if (copy_to_user(iov->iov_base,
150 addr + *offset, copy))
155 iov->iov_base += copy;
156 iov->iov_len -= copy;
162 * Attempt to pre-fault in the user memory, so we can use atomic copies.
163 * Returns the number of bytes not faulted in.
165 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
167 while (!iov->iov_len)
171 unsigned long this_len;
173 this_len = min_t(unsigned long, len, iov->iov_len);
174 if (fault_in_pages_writeable(iov->iov_base, this_len))
185 * Pre-fault in the user memory, so we can use atomic copies.
187 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
189 while (!iov->iov_len)
193 unsigned long this_len;
195 this_len = min_t(unsigned long, len, iov->iov_len);
196 fault_in_pages_readable(iov->iov_base, this_len);
202 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
203 struct pipe_buffer *buf)
205 struct page *page = buf->page;
208 * If nobody else uses this page, and we don't already have a
209 * temporary page, let's keep track of it as a one-deep
210 * allocation cache. (Otherwise just release our reference to it)
212 if (page_count(page) == 1 && !pipe->tmp_page)
213 pipe->tmp_page = page;
215 page_cache_release(page);
219 * generic_pipe_buf_map - virtually map a pipe buffer
220 * @pipe: the pipe that the buffer belongs to
221 * @buf: the buffer that should be mapped
222 * @atomic: whether to use an atomic map
225 * This function returns a kernel virtual address mapping for the
226 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
227 * and the caller has to be careful not to fault before calling
228 * the unmap function.
230 * Note that this function occupies KM_USER0 if @atomic != 0.
232 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
233 struct pipe_buffer *buf, int atomic)
236 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
237 return kmap_atomic(buf->page, KM_USER0);
240 return kmap(buf->page);
242 EXPORT_SYMBOL(generic_pipe_buf_map);
245 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
246 * @pipe: the pipe that the buffer belongs to
247 * @buf: the buffer that should be unmapped
248 * @map_data: the data that the mapping function returned
251 * This function undoes the mapping that ->map() provided.
253 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
254 struct pipe_buffer *buf, void *map_data)
256 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
257 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
258 kunmap_atomic(map_data, KM_USER0);
262 EXPORT_SYMBOL(generic_pipe_buf_unmap);
265 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
266 * @pipe: the pipe that the buffer belongs to
267 * @buf: the buffer to attempt to steal
270 * This function attempts to steal the &struct page attached to
271 * @buf. If successful, this function returns 0 and returns with
272 * the page locked. The caller may then reuse the page for whatever
273 * he wishes; the typical use is insertion into a different file
276 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
277 struct pipe_buffer *buf)
279 struct page *page = buf->page;
282 * A reference of one is golden, that means that the owner of this
283 * page is the only one holding a reference to it. lock the page
286 if (page_count(page) == 1) {
293 EXPORT_SYMBOL(generic_pipe_buf_steal);
296 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
297 * @pipe: the pipe that the buffer belongs to
298 * @buf: the buffer to get a reference to
301 * This function grabs an extra reference to @buf. It's used in
302 * in the tee() system call, when we duplicate the buffers in one
305 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
307 page_cache_get(buf->page);
309 EXPORT_SYMBOL(generic_pipe_buf_get);
312 * generic_pipe_buf_confirm - verify contents of the pipe buffer
313 * @info: the pipe that the buffer belongs to
314 * @buf: the buffer to confirm
317 * This function does nothing, because the generic pipe code uses
318 * pages that are always good when inserted into the pipe.
320 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
321 struct pipe_buffer *buf)
325 EXPORT_SYMBOL(generic_pipe_buf_confirm);
328 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
329 * @pipe: the pipe that the buffer belongs to
330 * @buf: the buffer to put a reference to
333 * This function releases a reference to @buf.
335 void generic_pipe_buf_release(struct pipe_inode_info *pipe,
336 struct pipe_buffer *buf)
338 page_cache_release(buf->page);
340 EXPORT_SYMBOL(generic_pipe_buf_release);
342 static const struct pipe_buf_operations anon_pipe_buf_ops = {
344 .map = generic_pipe_buf_map,
345 .unmap = generic_pipe_buf_unmap,
346 .confirm = generic_pipe_buf_confirm,
347 .release = anon_pipe_buf_release,
348 .steal = generic_pipe_buf_steal,
349 .get = generic_pipe_buf_get,
352 static const struct pipe_buf_operations packet_pipe_buf_ops = {
354 .map = generic_pipe_buf_map,
355 .unmap = generic_pipe_buf_unmap,
356 .confirm = generic_pipe_buf_confirm,
357 .release = anon_pipe_buf_release,
358 .steal = generic_pipe_buf_steal,
359 .get = generic_pipe_buf_get,
363 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
364 unsigned long nr_segs, loff_t pos)
366 struct file *filp = iocb->ki_filp;
367 struct inode *inode = filp->f_path.dentry->d_inode;
368 struct pipe_inode_info *pipe;
371 struct iovec *iov = (struct iovec *)_iov;
374 total_len = iov_length(iov, nr_segs);
375 /* Null read succeeds. */
376 if (unlikely(total_len == 0))
381 mutex_lock(&inode->i_mutex);
382 pipe = inode->i_pipe;
384 int bufs = pipe->nrbufs;
386 int curbuf = pipe->curbuf;
387 struct pipe_buffer *buf = pipe->bufs + curbuf;
388 const struct pipe_buf_operations *ops = buf->ops;
390 size_t chars = buf->len, remaining;
393 if (chars > total_len)
396 error = ops->confirm(pipe, buf);
403 atomic = !iov_fault_in_pages_write(iov, chars);
406 addr = ops->map(pipe, buf, atomic);
407 error = pipe_iov_copy_to_user(iov, addr, &buf->offset,
409 ops->unmap(pipe, buf, addr);
410 if (unlikely(error)) {
412 * Just retry with the slow path if we failed.
425 /* Was it a packet buffer? Clean up and exit */
426 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
433 ops->release(pipe, buf);
434 curbuf = (curbuf + 1) & (pipe->buffers - 1);
435 pipe->curbuf = curbuf;
436 pipe->nrbufs = --bufs;
441 break; /* common path: read succeeded */
443 if (bufs) /* More to do? */
447 if (!pipe->waiting_writers) {
448 /* syscall merging: Usually we must not sleep
449 * if O_NONBLOCK is set, or if we got some data.
450 * But if a writer sleeps in kernel space, then
451 * we can wait for that data without violating POSIX.
455 if (filp->f_flags & O_NONBLOCK) {
460 if (signal_pending(current)) {
466 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
467 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
471 mutex_unlock(&inode->i_mutex);
473 /* Signal writers asynchronously that there is more room. */
475 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
476 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
483 static inline int is_packetized(struct file *file)
485 return (file->f_flags & O_DIRECT) != 0;
489 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
490 unsigned long nr_segs, loff_t ppos)
492 struct file *filp = iocb->ki_filp;
493 struct inode *inode = filp->f_path.dentry->d_inode;
494 struct pipe_inode_info *pipe;
497 struct iovec *iov = (struct iovec *)_iov;
501 total_len = iov_length(iov, nr_segs);
502 /* Null write succeeds. */
503 if (unlikely(total_len == 0))
508 mutex_lock(&inode->i_mutex);
509 pipe = inode->i_pipe;
511 if (!pipe->readers) {
512 send_sig(SIGPIPE, current, 0);
517 /* We try to merge small writes */
518 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
519 if (pipe->nrbufs && chars != 0) {
520 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
522 struct pipe_buffer *buf = pipe->bufs + lastbuf;
523 const struct pipe_buf_operations *ops = buf->ops;
524 int offset = buf->offset + buf->len;
526 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
527 int error, atomic = 1;
529 size_t remaining = chars;
531 error = ops->confirm(pipe, buf);
535 iov_fault_in_pages_read(iov, chars);
537 addr = ops->map(pipe, buf, atomic);
538 error = pipe_iov_copy_from_user(addr, &offset, iov,
540 ops->unmap(pipe, buf, addr);
561 if (!pipe->readers) {
562 send_sig(SIGPIPE, current, 0);
568 if (bufs < pipe->buffers) {
569 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
570 struct pipe_buffer *buf = pipe->bufs + newbuf;
571 struct page *page = pipe->tmp_page;
573 int error, atomic = 1;
578 page = alloc_page(GFP_HIGHUSER);
579 if (unlikely(!page)) {
580 ret = ret ? : -ENOMEM;
583 pipe->tmp_page = page;
585 /* Always wake up, even if the copy fails. Otherwise
586 * we lock up (O_NONBLOCK-)readers that sleep due to
588 * FIXME! Is this really true?
592 if (chars > total_len)
595 iov_fault_in_pages_read(iov, chars);
599 src = kmap_atomic(page, KM_USER0);
603 error = pipe_iov_copy_from_user(src, &offset, iov,
606 kunmap_atomic(src, KM_USER0);
610 if (unlikely(error)) {
621 /* Insert it into the buffer array */
623 buf->ops = &anon_pipe_buf_ops;
627 if (is_packetized(filp)) {
628 buf->ops = &packet_pipe_buf_ops;
629 buf->flags = PIPE_BUF_FLAG_PACKET;
631 pipe->nrbufs = ++bufs;
632 pipe->tmp_page = NULL;
638 if (bufs < pipe->buffers)
640 if (filp->f_flags & O_NONBLOCK) {
645 if (signal_pending(current)) {
651 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
652 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
655 pipe->waiting_writers++;
657 pipe->waiting_writers--;
660 mutex_unlock(&inode->i_mutex);
662 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
663 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
666 file_update_time(filp);
671 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
677 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
683 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
685 struct inode *inode = filp->f_path.dentry->d_inode;
686 struct pipe_inode_info *pipe;
687 int count, buf, nrbufs;
691 mutex_lock(&inode->i_mutex);
692 pipe = inode->i_pipe;
695 nrbufs = pipe->nrbufs;
696 while (--nrbufs >= 0) {
697 count += pipe->bufs[buf].len;
698 buf = (buf+1) & (pipe->buffers - 1);
700 mutex_unlock(&inode->i_mutex);
702 return put_user(count, (int __user *)arg);
708 /* No kernel lock held - fine */
710 pipe_poll(struct file *filp, poll_table *wait)
713 struct inode *inode = filp->f_path.dentry->d_inode;
714 struct pipe_inode_info *pipe = inode->i_pipe;
717 poll_wait(filp, &pipe->wait, wait);
719 /* Reading only -- no need for acquiring the semaphore. */
720 nrbufs = pipe->nrbufs;
722 if (filp->f_mode & FMODE_READ) {
723 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
724 if (!pipe->writers && filp->f_version != pipe->w_counter)
728 if (filp->f_mode & FMODE_WRITE) {
729 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
731 * Most Unices do not set POLLERR for FIFOs but on Linux they
732 * behave exactly like pipes for poll().
742 pipe_release(struct inode *inode, int decr, int decw)
744 struct pipe_inode_info *pipe;
746 mutex_lock(&inode->i_mutex);
747 pipe = inode->i_pipe;
748 pipe->readers -= decr;
749 pipe->writers -= decw;
751 if (!pipe->readers && !pipe->writers) {
752 free_pipe_info(inode);
754 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
755 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
756 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
758 mutex_unlock(&inode->i_mutex);
764 pipe_read_fasync(int fd, struct file *filp, int on)
766 struct inode *inode = filp->f_path.dentry->d_inode;
769 mutex_lock(&inode->i_mutex);
770 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
771 mutex_unlock(&inode->i_mutex);
778 pipe_write_fasync(int fd, struct file *filp, int on)
780 struct inode *inode = filp->f_path.dentry->d_inode;
783 mutex_lock(&inode->i_mutex);
784 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
785 mutex_unlock(&inode->i_mutex);
792 pipe_rdwr_fasync(int fd, struct file *filp, int on)
794 struct inode *inode = filp->f_path.dentry->d_inode;
795 struct pipe_inode_info *pipe = inode->i_pipe;
798 mutex_lock(&inode->i_mutex);
799 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
801 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
802 if (retval < 0) /* this can happen only if on == T */
803 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
805 mutex_unlock(&inode->i_mutex);
811 pipe_read_release(struct inode *inode, struct file *filp)
813 return pipe_release(inode, 1, 0);
817 pipe_write_release(struct inode *inode, struct file *filp)
819 return pipe_release(inode, 0, 1);
823 pipe_rdwr_release(struct inode *inode, struct file *filp)
827 decr = (filp->f_mode & FMODE_READ) != 0;
828 decw = (filp->f_mode & FMODE_WRITE) != 0;
829 return pipe_release(inode, decr, decw);
833 pipe_read_open(struct inode *inode, struct file *filp)
837 mutex_lock(&inode->i_mutex);
841 inode->i_pipe->readers++;
844 mutex_unlock(&inode->i_mutex);
850 pipe_write_open(struct inode *inode, struct file *filp)
854 mutex_lock(&inode->i_mutex);
858 inode->i_pipe->writers++;
861 mutex_unlock(&inode->i_mutex);
867 pipe_rdwr_open(struct inode *inode, struct file *filp)
871 if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
874 mutex_lock(&inode->i_mutex);
878 if (filp->f_mode & FMODE_READ)
879 inode->i_pipe->readers++;
880 if (filp->f_mode & FMODE_WRITE)
881 inode->i_pipe->writers++;
884 mutex_unlock(&inode->i_mutex);
890 * The file_operations structs are not static because they
891 * are also used in linux/fs/fifo.c to do operations on FIFOs.
893 * Pipes reuse fifos' file_operations structs.
895 const struct file_operations read_pipefifo_fops = {
897 .read = do_sync_read,
898 .aio_read = pipe_read,
901 .unlocked_ioctl = pipe_ioctl,
902 .open = pipe_read_open,
903 .release = pipe_read_release,
904 .fasync = pipe_read_fasync,
907 const struct file_operations write_pipefifo_fops = {
910 .write = do_sync_write,
911 .aio_write = pipe_write,
913 .unlocked_ioctl = pipe_ioctl,
914 .open = pipe_write_open,
915 .release = pipe_write_release,
916 .fasync = pipe_write_fasync,
919 const struct file_operations rdwr_pipefifo_fops = {
921 .read = do_sync_read,
922 .aio_read = pipe_read,
923 .write = do_sync_write,
924 .aio_write = pipe_write,
926 .unlocked_ioctl = pipe_ioctl,
927 .open = pipe_rdwr_open,
928 .release = pipe_rdwr_release,
929 .fasync = pipe_rdwr_fasync,
932 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
934 struct pipe_inode_info *pipe;
936 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
938 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
940 init_waitqueue_head(&pipe->wait);
941 pipe->r_counter = pipe->w_counter = 1;
943 pipe->buffers = PIPE_DEF_BUFFERS;
952 void __free_pipe_info(struct pipe_inode_info *pipe)
956 for (i = 0; i < pipe->buffers; i++) {
957 struct pipe_buffer *buf = pipe->bufs + i;
959 buf->ops->release(pipe, buf);
962 __free_page(pipe->tmp_page);
967 void free_pipe_info(struct inode *inode)
969 __free_pipe_info(inode->i_pipe);
970 inode->i_pipe = NULL;
973 static struct vfsmount *pipe_mnt __read_mostly;
976 * pipefs_dname() is called from d_path().
978 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
980 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
981 dentry->d_inode->i_ino);
984 static const struct dentry_operations pipefs_dentry_operations = {
985 .d_dname = pipefs_dname,
988 static struct inode * get_pipe_inode(void)
990 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
991 struct pipe_inode_info *pipe;
996 inode->i_ino = get_next_ino();
998 pipe = alloc_pipe_info(inode);
1001 inode->i_pipe = pipe;
1003 pipe->readers = pipe->writers = 1;
1004 inode->i_fop = &rdwr_pipefifo_fops;
1007 * Mark the inode dirty from the very beginning,
1008 * that way it will never be moved to the dirty
1009 * list because "mark_inode_dirty()" will think
1010 * that it already _is_ on the dirty list.
1012 inode->i_state = I_DIRTY;
1013 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1014 inode->i_uid = current_fsuid();
1015 inode->i_gid = current_fsgid();
1016 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1027 struct file *create_write_pipe(int flags)
1030 struct inode *inode;
1033 struct qstr name = { .name = "" };
1036 inode = get_pipe_inode();
1041 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
1044 path.mnt = mntget(pipe_mnt);
1046 d_instantiate(path.dentry, inode);
1049 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
1052 f->f_mapping = inode->i_mapping;
1054 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
1060 free_pipe_info(inode);
1062 return ERR_PTR(err);
1065 free_pipe_info(inode);
1068 return ERR_PTR(err);
1071 void free_write_pipe(struct file *f)
1073 free_pipe_info(f->f_dentry->d_inode);
1074 path_put(&f->f_path);
1078 struct file *create_read_pipe(struct file *wrf, int flags)
1080 /* Grab pipe from the writer */
1081 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1082 &read_pipefifo_fops);
1084 return ERR_PTR(-ENFILE);
1086 path_get(&wrf->f_path);
1087 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
1092 int do_pipe_flags(int *fd, int flags)
1094 struct file *fw, *fr;
1098 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
1101 fw = create_write_pipe(flags);
1104 fr = create_read_pipe(fw, flags);
1105 error = PTR_ERR(fr);
1107 goto err_write_pipe;
1109 error = get_unused_fd_flags(flags);
1114 error = get_unused_fd_flags(flags);
1119 audit_fd_pair(fdr, fdw);
1120 fd_install(fdr, fr);
1121 fd_install(fdw, fw);
1130 path_put(&fr->f_path);
1133 free_write_pipe(fw);
1138 * sys_pipe() is the normal C calling standard for creating
1139 * a pipe. It's not the way Unix traditionally does this, though.
1141 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
1146 error = do_pipe_flags(fd, flags);
1148 if (copy_to_user(fildes, fd, sizeof(fd))) {
1157 SYSCALL_DEFINE1(pipe, int __user *, fildes)
1159 return sys_pipe2(fildes, 0);
1163 * Allocate a new array of pipe buffers and copy the info over. Returns the
1164 * pipe size if successful, or return -ERROR on error.
1166 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
1168 struct pipe_buffer *bufs;
1171 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1172 * expect a lot of shrink+grow operations, just free and allocate
1173 * again like we would do for growing. If the pipe currently
1174 * contains more buffers than arg, then return busy.
1176 if (nr_pages < pipe->nrbufs)
1179 bufs = kcalloc(nr_pages, sizeof(struct pipe_buffer), GFP_KERNEL);
1180 if (unlikely(!bufs))
1184 * The pipe array wraps around, so just start the new one at zero
1185 * and adjust the indexes.
1191 tail = pipe->curbuf + pipe->nrbufs;
1192 if (tail < pipe->buffers)
1195 tail &= (pipe->buffers - 1);
1197 head = pipe->nrbufs - tail;
1199 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1201 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
1207 pipe->buffers = nr_pages;
1208 return nr_pages * PAGE_SIZE;
1212 * Currently we rely on the pipe array holding a power-of-2 number
1215 static inline unsigned int round_pipe_size(unsigned int size)
1217 unsigned long nr_pages;
1219 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1220 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1224 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1225 * will return an error.
1227 int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1228 size_t *lenp, loff_t *ppos)
1232 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1233 if (ret < 0 || !write)
1236 pipe_max_size = round_pipe_size(pipe_max_size);
1241 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1242 * location, so checking ->i_pipe is not enough to verify that this is a
1245 struct pipe_inode_info *get_pipe_info(struct file *file)
1247 struct inode *i = file->f_path.dentry->d_inode;
1249 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1252 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1254 struct pipe_inode_info *pipe;
1257 pipe = get_pipe_info(file);
1261 mutex_lock(&pipe->inode->i_mutex);
1264 case F_SETPIPE_SZ: {
1265 unsigned int size, nr_pages;
1267 size = round_pipe_size(arg);
1268 nr_pages = size >> PAGE_SHIFT;
1274 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
1278 ret = pipe_set_size(pipe, nr_pages);
1282 ret = pipe->buffers * PAGE_SIZE;
1290 mutex_unlock(&pipe->inode->i_mutex);
1294 static const struct super_operations pipefs_ops = {
1295 .destroy_inode = free_inode_nonrcu,
1296 .statfs = simple_statfs,
1300 * pipefs should _never_ be mounted by userland - too much of security hassle,
1301 * no real gain from having the whole whorehouse mounted. So we don't need
1302 * any operations on the root directory. However, we need a non-trivial
1303 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1305 static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1306 int flags, const char *dev_name, void *data)
1308 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1309 &pipefs_dentry_operations, PIPEFS_MAGIC);
1312 static struct file_system_type pipe_fs_type = {
1314 .mount = pipefs_mount,
1315 .kill_sb = kill_anon_super,
1318 static int __init init_pipe_fs(void)
1320 int err = register_filesystem(&pipe_fs_type);
1323 pipe_mnt = kern_mount(&pipe_fs_type);
1324 if (IS_ERR(pipe_mnt)) {
1325 err = PTR_ERR(pipe_mnt);
1326 unregister_filesystem(&pipe_fs_type);
1332 static void __exit exit_pipe_fs(void)
1334 kern_unmount(pipe_mnt);
1335 unregister_filesystem(&pipe_fs_type);
1338 fs_initcall(init_pipe_fs);
1339 module_exit(exit_pipe_fs);