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;
38 /* Maximum allocatable pages per user. Hard limit is unset by default, soft
39 * matches default values.
41 unsigned long pipe_user_pages_hard;
42 unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR;
45 * We use a start+len construction, which provides full use of the
47 * -- Florian Coosmann (FGC)
49 * Reads with count = 0 should always return 0.
50 * -- Julian Bradfield 1999-06-07.
52 * FIFOs and Pipes now generate SIGIO for both readers and writers.
53 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
55 * pipe_read & write cleanup
56 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
59 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
62 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
65 void pipe_lock(struct pipe_inode_info *pipe)
68 * pipe_lock() nests non-pipe inode locks (for writing to a file)
70 pipe_lock_nested(pipe, I_MUTEX_PARENT);
72 EXPORT_SYMBOL(pipe_lock);
74 void pipe_unlock(struct pipe_inode_info *pipe)
77 mutex_unlock(&pipe->inode->i_mutex);
79 EXPORT_SYMBOL(pipe_unlock);
81 void pipe_double_lock(struct pipe_inode_info *pipe1,
82 struct pipe_inode_info *pipe2)
84 BUG_ON(pipe1 == pipe2);
87 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
88 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
90 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
91 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
95 /* Drop the inode semaphore and wait for a pipe event, atomically */
96 void pipe_wait(struct pipe_inode_info *pipe)
101 * Pipes are system-local resources, so sleeping on them
102 * is considered a noninteractive wait:
104 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
107 finish_wait(&pipe->wait, &wait);
112 pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
113 size_t *remaining, int atomic)
117 while (*remaining > 0) {
118 while (!iov->iov_len)
120 copy = min_t(unsigned long, *remaining, iov->iov_len);
123 if (__copy_from_user_inatomic(addr + *offset,
124 iov->iov_base, copy))
127 if (copy_from_user(addr + *offset,
128 iov->iov_base, copy))
133 iov->iov_base += copy;
134 iov->iov_len -= copy;
140 pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
141 size_t *remaining, int atomic)
145 while (*remaining > 0) {
146 while (!iov->iov_len)
148 copy = min_t(unsigned long, *remaining, iov->iov_len);
151 if (__copy_to_user_inatomic(iov->iov_base,
152 addr + *offset, copy))
155 if (copy_to_user(iov->iov_base,
156 addr + *offset, copy))
161 iov->iov_base += copy;
162 iov->iov_len -= copy;
168 * Attempt to pre-fault in the user memory, so we can use atomic copies.
169 * Returns the number of bytes not faulted in.
171 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
173 while (!iov->iov_len)
177 unsigned long this_len;
179 this_len = min_t(unsigned long, len, iov->iov_len);
180 if (fault_in_pages_writeable(iov->iov_base, this_len))
191 * Pre-fault in the user memory, so we can use atomic copies.
193 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
195 while (!iov->iov_len)
199 unsigned long this_len;
201 this_len = min_t(unsigned long, len, iov->iov_len);
202 fault_in_pages_readable(iov->iov_base, this_len);
208 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
209 struct pipe_buffer *buf)
211 struct page *page = buf->page;
214 * If nobody else uses this page, and we don't already have a
215 * temporary page, let's keep track of it as a one-deep
216 * allocation cache. (Otherwise just release our reference to it)
218 if (page_count(page) == 1 && !pipe->tmp_page)
219 pipe->tmp_page = page;
221 page_cache_release(page);
225 * generic_pipe_buf_map - virtually map a pipe buffer
226 * @pipe: the pipe that the buffer belongs to
227 * @buf: the buffer that should be mapped
228 * @atomic: whether to use an atomic map
231 * This function returns a kernel virtual address mapping for the
232 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
233 * and the caller has to be careful not to fault before calling
234 * the unmap function.
236 * Note that this function occupies KM_USER0 if @atomic != 0.
238 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
239 struct pipe_buffer *buf, int atomic)
242 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
243 return kmap_atomic(buf->page, KM_USER0);
246 return kmap(buf->page);
248 EXPORT_SYMBOL(generic_pipe_buf_map);
251 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
252 * @pipe: the pipe that the buffer belongs to
253 * @buf: the buffer that should be unmapped
254 * @map_data: the data that the mapping function returned
257 * This function undoes the mapping that ->map() provided.
259 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
260 struct pipe_buffer *buf, void *map_data)
262 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
263 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
264 kunmap_atomic(map_data, KM_USER0);
268 EXPORT_SYMBOL(generic_pipe_buf_unmap);
271 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
272 * @pipe: the pipe that the buffer belongs to
273 * @buf: the buffer to attempt to steal
276 * This function attempts to steal the &struct page attached to
277 * @buf. If successful, this function returns 0 and returns with
278 * the page locked. The caller may then reuse the page for whatever
279 * he wishes; the typical use is insertion into a different file
282 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
283 struct pipe_buffer *buf)
285 struct page *page = buf->page;
288 * A reference of one is golden, that means that the owner of this
289 * page is the only one holding a reference to it. lock the page
292 if (page_count(page) == 1) {
299 EXPORT_SYMBOL(generic_pipe_buf_steal);
302 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
303 * @pipe: the pipe that the buffer belongs to
304 * @buf: the buffer to get a reference to
307 * This function grabs an extra reference to @buf. It's used in
308 * in the tee() system call, when we duplicate the buffers in one
311 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
313 page_cache_get(buf->page);
315 EXPORT_SYMBOL(generic_pipe_buf_get);
318 * generic_pipe_buf_confirm - verify contents of the pipe buffer
319 * @info: the pipe that the buffer belongs to
320 * @buf: the buffer to confirm
323 * This function does nothing, because the generic pipe code uses
324 * pages that are always good when inserted into the pipe.
326 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
327 struct pipe_buffer *buf)
331 EXPORT_SYMBOL(generic_pipe_buf_confirm);
334 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
335 * @pipe: the pipe that the buffer belongs to
336 * @buf: the buffer to put a reference to
339 * This function releases a reference to @buf.
341 void generic_pipe_buf_release(struct pipe_inode_info *pipe,
342 struct pipe_buffer *buf)
344 page_cache_release(buf->page);
346 EXPORT_SYMBOL(generic_pipe_buf_release);
348 static const struct pipe_buf_operations anon_pipe_buf_ops = {
350 .map = generic_pipe_buf_map,
351 .unmap = generic_pipe_buf_unmap,
352 .confirm = generic_pipe_buf_confirm,
353 .release = anon_pipe_buf_release,
354 .steal = generic_pipe_buf_steal,
355 .get = generic_pipe_buf_get,
358 static const struct pipe_buf_operations packet_pipe_buf_ops = {
360 .map = generic_pipe_buf_map,
361 .unmap = generic_pipe_buf_unmap,
362 .confirm = generic_pipe_buf_confirm,
363 .release = anon_pipe_buf_release,
364 .steal = generic_pipe_buf_steal,
365 .get = generic_pipe_buf_get,
369 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
370 unsigned long nr_segs, loff_t pos)
372 struct file *filp = iocb->ki_filp;
373 struct inode *inode = filp->f_path.dentry->d_inode;
374 struct pipe_inode_info *pipe;
377 struct iovec *iov = (struct iovec *)_iov;
380 total_len = iov_length(iov, nr_segs);
381 /* Null read succeeds. */
382 if (unlikely(total_len == 0))
387 mutex_lock(&inode->i_mutex);
388 pipe = inode->i_pipe;
390 int bufs = pipe->nrbufs;
392 int curbuf = pipe->curbuf;
393 struct pipe_buffer *buf = pipe->bufs + curbuf;
394 const struct pipe_buf_operations *ops = buf->ops;
396 size_t chars = buf->len, remaining;
399 if (chars > total_len)
402 error = ops->confirm(pipe, buf);
409 atomic = !iov_fault_in_pages_write(iov, chars);
412 addr = ops->map(pipe, buf, atomic);
413 error = pipe_iov_copy_to_user(iov, addr, &buf->offset,
415 ops->unmap(pipe, buf, addr);
416 if (unlikely(error)) {
418 * Just retry with the slow path if we failed.
431 /* Was it a packet buffer? Clean up and exit */
432 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
439 ops->release(pipe, buf);
440 curbuf = (curbuf + 1) & (pipe->buffers - 1);
441 pipe->curbuf = curbuf;
442 pipe->nrbufs = --bufs;
447 break; /* common path: read succeeded */
449 if (bufs) /* More to do? */
453 if (!pipe->waiting_writers) {
454 /* syscall merging: Usually we must not sleep
455 * if O_NONBLOCK is set, or if we got some data.
456 * But if a writer sleeps in kernel space, then
457 * we can wait for that data without violating POSIX.
461 if (filp->f_flags & O_NONBLOCK) {
466 if (signal_pending(current)) {
472 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
473 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
477 mutex_unlock(&inode->i_mutex);
479 /* Signal writers asynchronously that there is more room. */
481 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
482 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
489 static inline int is_packetized(struct file *file)
491 return (file->f_flags & O_DIRECT) != 0;
495 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
496 unsigned long nr_segs, loff_t ppos)
498 struct file *filp = iocb->ki_filp;
499 struct inode *inode = filp->f_path.dentry->d_inode;
500 struct pipe_inode_info *pipe;
503 struct iovec *iov = (struct iovec *)_iov;
507 total_len = iov_length(iov, nr_segs);
508 /* Null write succeeds. */
509 if (unlikely(total_len == 0))
514 mutex_lock(&inode->i_mutex);
515 pipe = inode->i_pipe;
517 if (!pipe->readers) {
518 send_sig(SIGPIPE, current, 0);
523 /* We try to merge small writes */
524 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
525 if (pipe->nrbufs && chars != 0) {
526 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
528 struct pipe_buffer *buf = pipe->bufs + lastbuf;
529 const struct pipe_buf_operations *ops = buf->ops;
530 int offset = buf->offset + buf->len;
532 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
533 int error, atomic = 1;
535 size_t remaining = chars;
537 error = ops->confirm(pipe, buf);
541 iov_fault_in_pages_read(iov, chars);
543 addr = ops->map(pipe, buf, atomic);
544 error = pipe_iov_copy_from_user(addr, &offset, iov,
546 ops->unmap(pipe, buf, addr);
567 if (!pipe->readers) {
568 send_sig(SIGPIPE, current, 0);
574 if (bufs < pipe->buffers) {
575 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
576 struct pipe_buffer *buf = pipe->bufs + newbuf;
577 struct page *page = pipe->tmp_page;
579 int error, atomic = 1;
584 page = alloc_page(GFP_HIGHUSER);
585 if (unlikely(!page)) {
586 ret = ret ? : -ENOMEM;
589 pipe->tmp_page = page;
591 /* Always wake up, even if the copy fails. Otherwise
592 * we lock up (O_NONBLOCK-)readers that sleep due to
594 * FIXME! Is this really true?
598 if (chars > total_len)
601 iov_fault_in_pages_read(iov, chars);
605 src = kmap_atomic(page, KM_USER0);
609 error = pipe_iov_copy_from_user(src, &offset, iov,
612 kunmap_atomic(src, KM_USER0);
616 if (unlikely(error)) {
627 /* Insert it into the buffer array */
629 buf->ops = &anon_pipe_buf_ops;
633 if (is_packetized(filp)) {
634 buf->ops = &packet_pipe_buf_ops;
635 buf->flags = PIPE_BUF_FLAG_PACKET;
637 pipe->nrbufs = ++bufs;
638 pipe->tmp_page = NULL;
644 if (bufs < pipe->buffers)
646 if (filp->f_flags & O_NONBLOCK) {
651 if (signal_pending(current)) {
657 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
658 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
661 pipe->waiting_writers++;
663 pipe->waiting_writers--;
666 mutex_unlock(&inode->i_mutex);
668 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
669 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
672 file_update_time(filp);
677 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
683 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
689 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
691 struct inode *inode = filp->f_path.dentry->d_inode;
692 struct pipe_inode_info *pipe;
693 int count, buf, nrbufs;
697 mutex_lock(&inode->i_mutex);
698 pipe = inode->i_pipe;
701 nrbufs = pipe->nrbufs;
702 while (--nrbufs >= 0) {
703 count += pipe->bufs[buf].len;
704 buf = (buf+1) & (pipe->buffers - 1);
706 mutex_unlock(&inode->i_mutex);
708 return put_user(count, (int __user *)arg);
714 /* No kernel lock held - fine */
716 pipe_poll(struct file *filp, poll_table *wait)
719 struct inode *inode = filp->f_path.dentry->d_inode;
720 struct pipe_inode_info *pipe = inode->i_pipe;
723 poll_wait(filp, &pipe->wait, wait);
725 /* Reading only -- no need for acquiring the semaphore. */
726 nrbufs = pipe->nrbufs;
728 if (filp->f_mode & FMODE_READ) {
729 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
730 if (!pipe->writers && filp->f_version != pipe->w_counter)
734 if (filp->f_mode & FMODE_WRITE) {
735 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
737 * Most Unices do not set POLLERR for FIFOs but on Linux they
738 * behave exactly like pipes for poll().
748 pipe_release(struct inode *inode, int decr, int decw)
750 struct pipe_inode_info *pipe;
752 mutex_lock(&inode->i_mutex);
753 pipe = inode->i_pipe;
754 pipe->readers -= decr;
755 pipe->writers -= decw;
757 if (!pipe->readers && !pipe->writers) {
758 free_pipe_info(inode);
760 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
761 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
762 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
764 mutex_unlock(&inode->i_mutex);
770 pipe_read_fasync(int fd, struct file *filp, int on)
772 struct inode *inode = filp->f_path.dentry->d_inode;
775 mutex_lock(&inode->i_mutex);
776 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
777 mutex_unlock(&inode->i_mutex);
784 pipe_write_fasync(int fd, struct file *filp, int on)
786 struct inode *inode = filp->f_path.dentry->d_inode;
789 mutex_lock(&inode->i_mutex);
790 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
791 mutex_unlock(&inode->i_mutex);
798 pipe_rdwr_fasync(int fd, struct file *filp, int on)
800 struct inode *inode = filp->f_path.dentry->d_inode;
801 struct pipe_inode_info *pipe = inode->i_pipe;
804 mutex_lock(&inode->i_mutex);
805 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
807 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
808 if (retval < 0) /* this can happen only if on == T */
809 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
811 mutex_unlock(&inode->i_mutex);
817 pipe_read_release(struct inode *inode, struct file *filp)
819 return pipe_release(inode, 1, 0);
823 pipe_write_release(struct inode *inode, struct file *filp)
825 return pipe_release(inode, 0, 1);
829 pipe_rdwr_release(struct inode *inode, struct file *filp)
833 decr = (filp->f_mode & FMODE_READ) != 0;
834 decw = (filp->f_mode & FMODE_WRITE) != 0;
835 return pipe_release(inode, decr, decw);
839 pipe_read_open(struct inode *inode, struct file *filp)
843 mutex_lock(&inode->i_mutex);
847 inode->i_pipe->readers++;
850 mutex_unlock(&inode->i_mutex);
856 pipe_write_open(struct inode *inode, struct file *filp)
860 mutex_lock(&inode->i_mutex);
864 inode->i_pipe->writers++;
867 mutex_unlock(&inode->i_mutex);
873 pipe_rdwr_open(struct inode *inode, struct file *filp)
877 if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
880 mutex_lock(&inode->i_mutex);
884 if (filp->f_mode & FMODE_READ)
885 inode->i_pipe->readers++;
886 if (filp->f_mode & FMODE_WRITE)
887 inode->i_pipe->writers++;
890 mutex_unlock(&inode->i_mutex);
896 * The file_operations structs are not static because they
897 * are also used in linux/fs/fifo.c to do operations on FIFOs.
899 * Pipes reuse fifos' file_operations structs.
901 const struct file_operations read_pipefifo_fops = {
903 .read = do_sync_read,
904 .aio_read = pipe_read,
907 .unlocked_ioctl = pipe_ioctl,
908 .open = pipe_read_open,
909 .release = pipe_read_release,
910 .fasync = pipe_read_fasync,
913 const struct file_operations write_pipefifo_fops = {
916 .write = do_sync_write,
917 .aio_write = pipe_write,
919 .unlocked_ioctl = pipe_ioctl,
920 .open = pipe_write_open,
921 .release = pipe_write_release,
922 .fasync = pipe_write_fasync,
925 const struct file_operations rdwr_pipefifo_fops = {
927 .read = do_sync_read,
928 .aio_read = pipe_read,
929 .write = do_sync_write,
930 .aio_write = pipe_write,
932 .unlocked_ioctl = pipe_ioctl,
933 .open = pipe_rdwr_open,
934 .release = pipe_rdwr_release,
935 .fasync = pipe_rdwr_fasync,
938 static void account_pipe_buffers(struct pipe_inode_info *pipe,
939 unsigned long old, unsigned long new)
941 atomic_long_add(new - old, &pipe->user->pipe_bufs);
944 static bool too_many_pipe_buffers_soft(struct user_struct *user)
946 return pipe_user_pages_soft &&
947 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
950 static bool too_many_pipe_buffers_hard(struct user_struct *user)
952 return pipe_user_pages_hard &&
953 atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
956 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
958 struct pipe_inode_info *pipe;
960 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
962 unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
963 struct user_struct *user = get_current_user();
965 if (!too_many_pipe_buffers_hard(user)) {
966 if (too_many_pipe_buffers_soft(user))
968 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * pipe_bufs, GFP_KERNEL);
972 init_waitqueue_head(&pipe->wait);
973 pipe->r_counter = pipe->w_counter = 1;
975 pipe->buffers = pipe_bufs;
977 account_pipe_buffers(pipe, 0, pipe_bufs);
987 void __free_pipe_info(struct pipe_inode_info *pipe)
991 account_pipe_buffers(pipe, pipe->buffers, 0);
992 free_uid(pipe->user);
993 for (i = 0; i < pipe->buffers; i++) {
994 struct pipe_buffer *buf = pipe->bufs + i;
996 buf->ops->release(pipe, buf);
999 __free_page(pipe->tmp_page);
1004 void free_pipe_info(struct inode *inode)
1006 __free_pipe_info(inode->i_pipe);
1007 inode->i_pipe = NULL;
1010 static struct vfsmount *pipe_mnt __read_mostly;
1013 * pipefs_dname() is called from d_path().
1015 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
1017 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
1018 dentry->d_inode->i_ino);
1021 static const struct dentry_operations pipefs_dentry_operations = {
1022 .d_dname = pipefs_dname,
1025 static struct inode * get_pipe_inode(void)
1027 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
1028 struct pipe_inode_info *pipe;
1033 inode->i_ino = get_next_ino();
1035 pipe = alloc_pipe_info(inode);
1038 inode->i_pipe = pipe;
1040 pipe->readers = pipe->writers = 1;
1041 inode->i_fop = &rdwr_pipefifo_fops;
1044 * Mark the inode dirty from the very beginning,
1045 * that way it will never be moved to the dirty
1046 * list because "mark_inode_dirty()" will think
1047 * that it already _is_ on the dirty list.
1049 inode->i_state = I_DIRTY;
1050 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1051 inode->i_uid = current_fsuid();
1052 inode->i_gid = current_fsgid();
1053 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1064 struct file *create_write_pipe(int flags)
1067 struct inode *inode;
1070 struct qstr name = { .name = "" };
1073 inode = get_pipe_inode();
1078 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
1081 path.mnt = mntget(pipe_mnt);
1083 d_instantiate(path.dentry, inode);
1086 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
1089 f->f_mapping = inode->i_mapping;
1091 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
1097 free_pipe_info(inode);
1099 return ERR_PTR(err);
1102 free_pipe_info(inode);
1105 return ERR_PTR(err);
1108 void free_write_pipe(struct file *f)
1110 free_pipe_info(f->f_dentry->d_inode);
1111 path_put(&f->f_path);
1115 struct file *create_read_pipe(struct file *wrf, int flags)
1117 /* Grab pipe from the writer */
1118 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1119 &read_pipefifo_fops);
1121 return ERR_PTR(-ENFILE);
1123 path_get(&wrf->f_path);
1124 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
1129 int do_pipe_flags(int *fd, int flags)
1131 struct file *fw, *fr;
1135 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
1138 fw = create_write_pipe(flags);
1141 fr = create_read_pipe(fw, flags);
1142 error = PTR_ERR(fr);
1144 goto err_write_pipe;
1146 error = get_unused_fd_flags(flags);
1151 error = get_unused_fd_flags(flags);
1156 audit_fd_pair(fdr, fdw);
1157 fd_install(fdr, fr);
1158 fd_install(fdw, fw);
1167 path_put(&fr->f_path);
1170 free_write_pipe(fw);
1175 * sys_pipe() is the normal C calling standard for creating
1176 * a pipe. It's not the way Unix traditionally does this, though.
1178 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
1183 error = do_pipe_flags(fd, flags);
1185 if (copy_to_user(fildes, fd, sizeof(fd))) {
1194 SYSCALL_DEFINE1(pipe, int __user *, fildes)
1196 return sys_pipe2(fildes, 0);
1200 * Allocate a new array of pipe buffers and copy the info over. Returns the
1201 * pipe size if successful, or return -ERROR on error.
1203 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
1205 struct pipe_buffer *bufs;
1208 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1209 * expect a lot of shrink+grow operations, just free and allocate
1210 * again like we would do for growing. If the pipe currently
1211 * contains more buffers than arg, then return busy.
1213 if (nr_pages < pipe->nrbufs)
1216 bufs = kcalloc(nr_pages, sizeof(struct pipe_buffer), GFP_KERNEL);
1217 if (unlikely(!bufs))
1221 * The pipe array wraps around, so just start the new one at zero
1222 * and adjust the indexes.
1228 tail = pipe->curbuf + pipe->nrbufs;
1229 if (tail < pipe->buffers)
1232 tail &= (pipe->buffers - 1);
1234 head = pipe->nrbufs - tail;
1236 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1238 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
1241 account_pipe_buffers(pipe, pipe->buffers, nr_pages);
1245 pipe->buffers = nr_pages;
1246 return nr_pages * PAGE_SIZE;
1250 * Currently we rely on the pipe array holding a power-of-2 number
1253 static inline unsigned int round_pipe_size(unsigned int size)
1255 unsigned long nr_pages;
1257 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1258 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1262 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1263 * will return an error.
1265 int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1266 size_t *lenp, loff_t *ppos)
1270 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1271 if (ret < 0 || !write)
1274 pipe_max_size = round_pipe_size(pipe_max_size);
1279 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1280 * location, so checking ->i_pipe is not enough to verify that this is a
1283 struct pipe_inode_info *get_pipe_info(struct file *file)
1285 struct inode *i = file->f_path.dentry->d_inode;
1287 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1290 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1292 struct pipe_inode_info *pipe;
1295 pipe = get_pipe_info(file);
1299 mutex_lock(&pipe->inode->i_mutex);
1302 case F_SETPIPE_SZ: {
1303 unsigned int size, nr_pages;
1305 size = round_pipe_size(arg);
1306 nr_pages = size >> PAGE_SHIFT;
1312 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
1315 } else if ((too_many_pipe_buffers_hard(pipe->user) ||
1316 too_many_pipe_buffers_soft(pipe->user)) &&
1317 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
1321 ret = pipe_set_size(pipe, nr_pages);
1325 ret = pipe->buffers * PAGE_SIZE;
1333 mutex_unlock(&pipe->inode->i_mutex);
1337 static const struct super_operations pipefs_ops = {
1338 .destroy_inode = free_inode_nonrcu,
1339 .statfs = simple_statfs,
1343 * pipefs should _never_ be mounted by userland - too much of security hassle,
1344 * no real gain from having the whole whorehouse mounted. So we don't need
1345 * any operations on the root directory. However, we need a non-trivial
1346 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1348 static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1349 int flags, const char *dev_name, void *data)
1351 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1352 &pipefs_dentry_operations, PIPEFS_MAGIC);
1355 static struct file_system_type pipe_fs_type = {
1357 .mount = pipefs_mount,
1358 .kill_sb = kill_anon_super,
1361 static int __init init_pipe_fs(void)
1363 int err = register_filesystem(&pipe_fs_type);
1366 pipe_mnt = kern_mount(&pipe_fs_type);
1367 if (IS_ERR(pipe_mnt)) {
1368 err = PTR_ERR(pipe_mnt);
1369 unregister_filesystem(&pipe_fs_type);
1375 static void __exit exit_pipe_fs(void)
1377 kern_unmount(pipe_mnt);
1378 unregister_filesystem(&pipe_fs_type);
1381 fs_initcall(init_pipe_fs);
1382 module_exit(exit_pipe_fs);