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>
23 #include <asm/uaccess.h>
24 #include <asm/ioctls.h>
27 * We use a start+len construction, which provides full use of the
29 * -- Florian Coosmann (FGC)
31 * Reads with count = 0 should always return 0.
32 * -- Julian Bradfield 1999-06-07.
34 * FIFOs and Pipes now generate SIGIO for both readers and writers.
35 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
37 * pipe_read & write cleanup
38 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
41 static void pipe_lock_nested(struct pipe_inode_info
*pipe
, int subclass
)
44 mutex_lock_nested(&pipe
->inode
->i_mutex
, subclass
);
47 void pipe_lock(struct pipe_inode_info
*pipe
)
50 * pipe_lock() nests non-pipe inode locks (for writing to a file)
52 pipe_lock_nested(pipe
, I_MUTEX_PARENT
);
54 EXPORT_SYMBOL(pipe_lock
);
56 void pipe_unlock(struct pipe_inode_info
*pipe
)
59 mutex_unlock(&pipe
->inode
->i_mutex
);
61 EXPORT_SYMBOL(pipe_unlock
);
63 void pipe_double_lock(struct pipe_inode_info
*pipe1
,
64 struct pipe_inode_info
*pipe2
)
66 BUG_ON(pipe1
== pipe2
);
69 pipe_lock_nested(pipe1
, I_MUTEX_PARENT
);
70 pipe_lock_nested(pipe2
, I_MUTEX_CHILD
);
72 pipe_lock_nested(pipe2
, I_MUTEX_PARENT
);
73 pipe_lock_nested(pipe1
, I_MUTEX_CHILD
);
77 /* Drop the inode semaphore and wait for a pipe event, atomically */
78 void pipe_wait(struct pipe_inode_info
*pipe
)
83 * Pipes are system-local resources, so sleeping on them
84 * is considered a noninteractive wait:
86 prepare_to_wait(&pipe
->wait
, &wait
, TASK_INTERRUPTIBLE
);
89 finish_wait(&pipe
->wait
, &wait
);
94 pipe_iov_copy_from_user(void *to
, struct iovec
*iov
, unsigned long len
,
100 while (!iov
->iov_len
)
102 copy
= min_t(unsigned long, len
, iov
->iov_len
);
105 if (__copy_from_user_inatomic(to
, iov
->iov_base
, copy
))
108 if (copy_from_user(to
, iov
->iov_base
, copy
))
113 iov
->iov_base
+= copy
;
114 iov
->iov_len
-= copy
;
120 pipe_iov_copy_to_user(struct iovec
*iov
, const void *from
, unsigned long len
,
126 while (!iov
->iov_len
)
128 copy
= min_t(unsigned long, len
, iov
->iov_len
);
131 if (__copy_to_user_inatomic(iov
->iov_base
, from
, copy
))
134 if (copy_to_user(iov
->iov_base
, from
, copy
))
139 iov
->iov_base
+= copy
;
140 iov
->iov_len
-= copy
;
146 * Attempt to pre-fault in the user memory, so we can use atomic copies.
147 * Returns the number of bytes not faulted in.
149 static int iov_fault_in_pages_write(struct iovec
*iov
, unsigned long len
)
151 while (!iov
->iov_len
)
155 unsigned long this_len
;
157 this_len
= min_t(unsigned long, len
, iov
->iov_len
);
158 if (fault_in_pages_writeable(iov
->iov_base
, this_len
))
169 * Pre-fault in the user memory, so we can use atomic copies.
171 static void iov_fault_in_pages_read(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 fault_in_pages_readable(iov
->iov_base
, this_len
);
186 static void anon_pipe_buf_release(struct pipe_inode_info
*pipe
,
187 struct pipe_buffer
*buf
)
189 struct page
*page
= buf
->page
;
192 * If nobody else uses this page, and we don't already have a
193 * temporary page, let's keep track of it as a one-deep
194 * allocation cache. (Otherwise just release our reference to it)
196 if (page_count(page
) == 1 && !pipe
->tmp_page
)
197 pipe
->tmp_page
= page
;
199 page_cache_release(page
);
203 * generic_pipe_buf_map - virtually map a pipe buffer
204 * @pipe: the pipe that the buffer belongs to
205 * @buf: the buffer that should be mapped
206 * @atomic: whether to use an atomic map
209 * This function returns a kernel virtual address mapping for the
210 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
211 * and the caller has to be careful not to fault before calling
212 * the unmap function.
214 * Note that this function occupies KM_USER0 if @atomic != 0.
216 void *generic_pipe_buf_map(struct pipe_inode_info
*pipe
,
217 struct pipe_buffer
*buf
, int atomic
)
220 buf
->flags
|= PIPE_BUF_FLAG_ATOMIC
;
221 return kmap_atomic(buf
->page
, KM_USER0
);
224 return kmap(buf
->page
);
228 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
229 * @pipe: the pipe that the buffer belongs to
230 * @buf: the buffer that should be unmapped
231 * @map_data: the data that the mapping function returned
234 * This function undoes the mapping that ->map() provided.
236 void generic_pipe_buf_unmap(struct pipe_inode_info
*pipe
,
237 struct pipe_buffer
*buf
, void *map_data
)
239 if (buf
->flags
& PIPE_BUF_FLAG_ATOMIC
) {
240 buf
->flags
&= ~PIPE_BUF_FLAG_ATOMIC
;
241 kunmap_atomic(map_data
, KM_USER0
);
247 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
248 * @pipe: the pipe that the buffer belongs to
249 * @buf: the buffer to attempt to steal
252 * This function attempts to steal the &struct page attached to
253 * @buf. If successful, this function returns 0 and returns with
254 * the page locked. The caller may then reuse the page for whatever
255 * he wishes; the typical use is insertion into a different file
258 int generic_pipe_buf_steal(struct pipe_inode_info
*pipe
,
259 struct pipe_buffer
*buf
)
261 struct page
*page
= buf
->page
;
264 * A reference of one is golden, that means that the owner of this
265 * page is the only one holding a reference to it. lock the page
268 if (page_count(page
) == 1) {
277 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
278 * @pipe: the pipe that the buffer belongs to
279 * @buf: the buffer to get a reference to
282 * This function grabs an extra reference to @buf. It's used in
283 * in the tee() system call, when we duplicate the buffers in one
286 void generic_pipe_buf_get(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
)
288 page_cache_get(buf
->page
);
292 * generic_pipe_buf_confirm - verify contents of the pipe buffer
293 * @info: the pipe that the buffer belongs to
294 * @buf: the buffer to confirm
297 * This function does nothing, because the generic pipe code uses
298 * pages that are always good when inserted into the pipe.
300 int generic_pipe_buf_confirm(struct pipe_inode_info
*info
,
301 struct pipe_buffer
*buf
)
307 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
308 * @pipe: the pipe that the buffer belongs to
309 * @buf: the buffer to put a reference to
312 * This function releases a reference to @buf.
314 void generic_pipe_buf_release(struct pipe_inode_info
*pipe
,
315 struct pipe_buffer
*buf
)
317 page_cache_release(buf
->page
);
320 static const struct pipe_buf_operations anon_pipe_buf_ops
= {
322 .map
= generic_pipe_buf_map
,
323 .unmap
= generic_pipe_buf_unmap
,
324 .confirm
= generic_pipe_buf_confirm
,
325 .release
= anon_pipe_buf_release
,
326 .steal
= generic_pipe_buf_steal
,
327 .get
= generic_pipe_buf_get
,
331 pipe_read(struct kiocb
*iocb
, const struct iovec
*_iov
,
332 unsigned long nr_segs
, loff_t pos
)
334 struct file
*filp
= iocb
->ki_filp
;
335 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
336 struct pipe_inode_info
*pipe
;
339 struct iovec
*iov
= (struct iovec
*)_iov
;
342 total_len
= iov_length(iov
, nr_segs
);
343 /* Null read succeeds. */
344 if (unlikely(total_len
== 0))
349 mutex_lock(&inode
->i_mutex
);
350 pipe
= inode
->i_pipe
;
352 int bufs
= pipe
->nrbufs
;
354 int curbuf
= pipe
->curbuf
;
355 struct pipe_buffer
*buf
= pipe
->bufs
+ curbuf
;
356 const struct pipe_buf_operations
*ops
= buf
->ops
;
358 size_t chars
= buf
->len
;
361 if (chars
> total_len
)
364 error
= ops
->confirm(pipe
, buf
);
371 atomic
= !iov_fault_in_pages_write(iov
, chars
);
373 addr
= ops
->map(pipe
, buf
, atomic
);
374 error
= pipe_iov_copy_to_user(iov
, addr
+ buf
->offset
, chars
, atomic
);
375 ops
->unmap(pipe
, buf
, addr
);
376 if (unlikely(error
)) {
378 * Just retry with the slow path if we failed.
389 buf
->offset
+= chars
;
393 ops
->release(pipe
, buf
);
394 curbuf
= (curbuf
+ 1) & (pipe
->buffers
- 1);
395 pipe
->curbuf
= curbuf
;
396 pipe
->nrbufs
= --bufs
;
401 break; /* common path: read succeeded */
403 if (bufs
) /* More to do? */
407 if (!pipe
->waiting_writers
) {
408 /* syscall merging: Usually we must not sleep
409 * if O_NONBLOCK is set, or if we got some data.
410 * But if a writer sleeps in kernel space, then
411 * we can wait for that data without violating POSIX.
415 if (filp
->f_flags
& O_NONBLOCK
) {
420 if (signal_pending(current
)) {
426 wake_up_interruptible_sync(&pipe
->wait
);
427 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
431 mutex_unlock(&inode
->i_mutex
);
433 /* Signal writers asynchronously that there is more room. */
435 wake_up_interruptible_sync(&pipe
->wait
);
436 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
444 pipe_write(struct kiocb
*iocb
, const struct iovec
*_iov
,
445 unsigned long nr_segs
, loff_t ppos
)
447 struct file
*filp
= iocb
->ki_filp
;
448 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
449 struct pipe_inode_info
*pipe
;
452 struct iovec
*iov
= (struct iovec
*)_iov
;
456 total_len
= iov_length(iov
, nr_segs
);
457 /* Null write succeeds. */
458 if (unlikely(total_len
== 0))
463 mutex_lock(&inode
->i_mutex
);
464 pipe
= inode
->i_pipe
;
466 if (!pipe
->readers
) {
467 send_sig(SIGPIPE
, current
, 0);
472 /* We try to merge small writes */
473 chars
= total_len
& (PAGE_SIZE
-1); /* size of the last buffer */
474 if (pipe
->nrbufs
&& chars
!= 0) {
475 int lastbuf
= (pipe
->curbuf
+ pipe
->nrbufs
- 1) &
477 struct pipe_buffer
*buf
= pipe
->bufs
+ lastbuf
;
478 const struct pipe_buf_operations
*ops
= buf
->ops
;
479 int offset
= buf
->offset
+ buf
->len
;
481 if (ops
->can_merge
&& offset
+ chars
<= PAGE_SIZE
) {
482 int error
, atomic
= 1;
485 error
= ops
->confirm(pipe
, buf
);
489 iov_fault_in_pages_read(iov
, chars
);
491 addr
= ops
->map(pipe
, buf
, atomic
);
492 error
= pipe_iov_copy_from_user(offset
+ addr
, iov
,
494 ops
->unmap(pipe
, buf
, addr
);
515 if (!pipe
->readers
) {
516 send_sig(SIGPIPE
, current
, 0);
522 if (bufs
< pipe
->buffers
) {
523 int newbuf
= (pipe
->curbuf
+ bufs
) & (pipe
->buffers
-1);
524 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
525 struct page
*page
= pipe
->tmp_page
;
527 int error
, atomic
= 1;
530 page
= alloc_page(GFP_HIGHUSER
);
531 if (unlikely(!page
)) {
532 ret
= ret
? : -ENOMEM
;
535 pipe
->tmp_page
= page
;
537 /* Always wake up, even if the copy fails. Otherwise
538 * we lock up (O_NONBLOCK-)readers that sleep due to
540 * FIXME! Is this really true?
544 if (chars
> total_len
)
547 iov_fault_in_pages_read(iov
, chars
);
550 src
= kmap_atomic(page
, KM_USER0
);
554 error
= pipe_iov_copy_from_user(src
, iov
, chars
,
557 kunmap_atomic(src
, KM_USER0
);
561 if (unlikely(error
)) {
572 /* Insert it into the buffer array */
574 buf
->ops
= &anon_pipe_buf_ops
;
577 pipe
->nrbufs
= ++bufs
;
578 pipe
->tmp_page
= NULL
;
584 if (bufs
< pipe
->buffers
)
586 if (filp
->f_flags
& O_NONBLOCK
) {
591 if (signal_pending(current
)) {
597 wake_up_interruptible_sync(&pipe
->wait
);
598 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
601 pipe
->waiting_writers
++;
603 pipe
->waiting_writers
--;
606 mutex_unlock(&inode
->i_mutex
);
608 wake_up_interruptible_sync(&pipe
->wait
);
609 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
612 file_update_time(filp
);
617 bad_pipe_r(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
623 bad_pipe_w(struct file
*filp
, const char __user
*buf
, size_t count
,
629 static long pipe_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
631 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
632 struct pipe_inode_info
*pipe
;
633 int count
, buf
, nrbufs
;
637 mutex_lock(&inode
->i_mutex
);
638 pipe
= inode
->i_pipe
;
641 nrbufs
= pipe
->nrbufs
;
642 while (--nrbufs
>= 0) {
643 count
+= pipe
->bufs
[buf
].len
;
644 buf
= (buf
+1) & (pipe
->buffers
- 1);
646 mutex_unlock(&inode
->i_mutex
);
648 return put_user(count
, (int __user
*)arg
);
654 /* No kernel lock held - fine */
656 pipe_poll(struct file
*filp
, poll_table
*wait
)
659 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
660 struct pipe_inode_info
*pipe
= inode
->i_pipe
;
663 poll_wait(filp
, &pipe
->wait
, wait
);
665 /* Reading only -- no need for acquiring the semaphore. */
666 nrbufs
= pipe
->nrbufs
;
668 if (filp
->f_mode
& FMODE_READ
) {
669 mask
= (nrbufs
> 0) ? POLLIN
| POLLRDNORM
: 0;
670 if (!pipe
->writers
&& filp
->f_version
!= pipe
->w_counter
)
674 if (filp
->f_mode
& FMODE_WRITE
) {
675 mask
|= (nrbufs
< pipe
->buffers
) ? POLLOUT
| POLLWRNORM
: 0;
677 * Most Unices do not set POLLERR for FIFOs but on Linux they
678 * behave exactly like pipes for poll().
688 pipe_release(struct inode
*inode
, int decr
, int decw
)
690 struct pipe_inode_info
*pipe
;
692 mutex_lock(&inode
->i_mutex
);
693 pipe
= inode
->i_pipe
;
694 pipe
->readers
-= decr
;
695 pipe
->writers
-= decw
;
697 if (!pipe
->readers
&& !pipe
->writers
) {
698 free_pipe_info(inode
);
700 wake_up_interruptible_sync(&pipe
->wait
);
701 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
702 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
704 mutex_unlock(&inode
->i_mutex
);
710 pipe_read_fasync(int fd
, struct file
*filp
, int on
)
712 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
715 mutex_lock(&inode
->i_mutex
);
716 retval
= fasync_helper(fd
, filp
, on
, &inode
->i_pipe
->fasync_readers
);
717 mutex_unlock(&inode
->i_mutex
);
724 pipe_write_fasync(int fd
, struct file
*filp
, int on
)
726 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
729 mutex_lock(&inode
->i_mutex
);
730 retval
= fasync_helper(fd
, filp
, on
, &inode
->i_pipe
->fasync_writers
);
731 mutex_unlock(&inode
->i_mutex
);
738 pipe_rdwr_fasync(int fd
, struct file
*filp
, int on
)
740 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
741 struct pipe_inode_info
*pipe
= inode
->i_pipe
;
744 mutex_lock(&inode
->i_mutex
);
745 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_readers
);
747 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_writers
);
748 if (retval
< 0) /* this can happen only if on == T */
749 fasync_helper(-1, filp
, 0, &pipe
->fasync_readers
);
751 mutex_unlock(&inode
->i_mutex
);
757 pipe_read_release(struct inode
*inode
, struct file
*filp
)
759 return pipe_release(inode
, 1, 0);
763 pipe_write_release(struct inode
*inode
, struct file
*filp
)
765 return pipe_release(inode
, 0, 1);
769 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
773 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
774 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
775 return pipe_release(inode
, decr
, decw
);
779 pipe_read_open(struct inode
*inode
, struct file
*filp
)
783 mutex_lock(&inode
->i_mutex
);
787 inode
->i_pipe
->readers
++;
790 mutex_unlock(&inode
->i_mutex
);
796 pipe_write_open(struct inode
*inode
, struct file
*filp
)
800 mutex_lock(&inode
->i_mutex
);
804 inode
->i_pipe
->writers
++;
807 mutex_unlock(&inode
->i_mutex
);
813 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
817 mutex_lock(&inode
->i_mutex
);
821 if (filp
->f_mode
& FMODE_READ
)
822 inode
->i_pipe
->readers
++;
823 if (filp
->f_mode
& FMODE_WRITE
)
824 inode
->i_pipe
->writers
++;
827 mutex_unlock(&inode
->i_mutex
);
833 * The file_operations structs are not static because they
834 * are also used in linux/fs/fifo.c to do operations on FIFOs.
836 * Pipes reuse fifos' file_operations structs.
838 const struct file_operations read_pipefifo_fops
= {
840 .read
= do_sync_read
,
841 .aio_read
= pipe_read
,
844 .unlocked_ioctl
= pipe_ioctl
,
845 .open
= pipe_read_open
,
846 .release
= pipe_read_release
,
847 .fasync
= pipe_read_fasync
,
850 const struct file_operations write_pipefifo_fops
= {
853 .write
= do_sync_write
,
854 .aio_write
= pipe_write
,
856 .unlocked_ioctl
= pipe_ioctl
,
857 .open
= pipe_write_open
,
858 .release
= pipe_write_release
,
859 .fasync
= pipe_write_fasync
,
862 const struct file_operations rdwr_pipefifo_fops
= {
864 .read
= do_sync_read
,
865 .aio_read
= pipe_read
,
866 .write
= do_sync_write
,
867 .aio_write
= pipe_write
,
869 .unlocked_ioctl
= pipe_ioctl
,
870 .open
= pipe_rdwr_open
,
871 .release
= pipe_rdwr_release
,
872 .fasync
= pipe_rdwr_fasync
,
875 struct pipe_inode_info
* alloc_pipe_info(struct inode
*inode
)
877 struct pipe_inode_info
*pipe
;
879 pipe
= kzalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
881 pipe
->bufs
= kzalloc(sizeof(struct pipe_buffer
) * PIPE_DEF_BUFFERS
, GFP_KERNEL
);
883 init_waitqueue_head(&pipe
->wait
);
884 pipe
->r_counter
= pipe
->w_counter
= 1;
886 pipe
->buffers
= PIPE_DEF_BUFFERS
;
895 void __free_pipe_info(struct pipe_inode_info
*pipe
)
899 for (i
= 0; i
< pipe
->buffers
; i
++) {
900 struct pipe_buffer
*buf
= pipe
->bufs
+ i
;
902 buf
->ops
->release(pipe
, buf
);
905 __free_page(pipe
->tmp_page
);
910 void free_pipe_info(struct inode
*inode
)
912 __free_pipe_info(inode
->i_pipe
);
913 inode
->i_pipe
= NULL
;
916 static struct vfsmount
*pipe_mnt __read_mostly
;
919 * pipefs_dname() is called from d_path().
921 static char *pipefs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
923 return dynamic_dname(dentry
, buffer
, buflen
, "pipe:[%lu]",
924 dentry
->d_inode
->i_ino
);
927 static const struct dentry_operations pipefs_dentry_operations
= {
928 .d_dname
= pipefs_dname
,
931 static struct inode
* get_pipe_inode(void)
933 struct inode
*inode
= new_inode(pipe_mnt
->mnt_sb
);
934 struct pipe_inode_info
*pipe
;
939 pipe
= alloc_pipe_info(inode
);
942 inode
->i_pipe
= pipe
;
944 pipe
->readers
= pipe
->writers
= 1;
945 inode
->i_fop
= &rdwr_pipefifo_fops
;
948 * Mark the inode dirty from the very beginning,
949 * that way it will never be moved to the dirty
950 * list because "mark_inode_dirty()" will think
951 * that it already _is_ on the dirty list.
953 inode
->i_state
= I_DIRTY
;
954 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
955 inode
->i_uid
= current_fsuid();
956 inode
->i_gid
= current_fsgid();
957 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
968 struct file
*create_write_pipe(int flags
)
974 struct qstr name
= { .name
= "" };
977 inode
= get_pipe_inode();
982 path
.dentry
= d_alloc(pipe_mnt
->mnt_sb
->s_root
, &name
);
985 path
.mnt
= mntget(pipe_mnt
);
987 path
.dentry
->d_op
= &pipefs_dentry_operations
;
988 d_instantiate(path
.dentry
, inode
);
991 f
= alloc_file(&path
, FMODE_WRITE
, &write_pipefifo_fops
);
994 f
->f_mapping
= inode
->i_mapping
;
996 f
->f_flags
= O_WRONLY
| (flags
& O_NONBLOCK
);
1002 free_pipe_info(inode
);
1004 return ERR_PTR(err
);
1007 free_pipe_info(inode
);
1010 return ERR_PTR(err
);
1013 void free_write_pipe(struct file
*f
)
1015 free_pipe_info(f
->f_dentry
->d_inode
);
1016 path_put(&f
->f_path
);
1020 struct file
*create_read_pipe(struct file
*wrf
, int flags
)
1022 /* Grab pipe from the writer */
1023 struct file
*f
= alloc_file(&wrf
->f_path
, FMODE_READ
,
1024 &read_pipefifo_fops
);
1026 return ERR_PTR(-ENFILE
);
1028 path_get(&wrf
->f_path
);
1029 f
->f_flags
= O_RDONLY
| (flags
& O_NONBLOCK
);
1034 int do_pipe_flags(int *fd
, int flags
)
1036 struct file
*fw
, *fr
;
1040 if (flags
& ~(O_CLOEXEC
| O_NONBLOCK
))
1043 fw
= create_write_pipe(flags
);
1046 fr
= create_read_pipe(fw
, flags
);
1047 error
= PTR_ERR(fr
);
1049 goto err_write_pipe
;
1051 error
= get_unused_fd_flags(flags
);
1056 error
= get_unused_fd_flags(flags
);
1061 audit_fd_pair(fdr
, fdw
);
1062 fd_install(fdr
, fr
);
1063 fd_install(fdw
, fw
);
1072 path_put(&fr
->f_path
);
1075 free_write_pipe(fw
);
1080 * sys_pipe() is the normal C calling standard for creating
1081 * a pipe. It's not the way Unix traditionally does this, though.
1083 SYSCALL_DEFINE2(pipe2
, int __user
*, fildes
, int, flags
)
1088 error
= do_pipe_flags(fd
, flags
);
1090 if (copy_to_user(fildes
, fd
, sizeof(fd
))) {
1099 SYSCALL_DEFINE1(pipe
, int __user
*, fildes
)
1101 return sys_pipe2(fildes
, 0);
1105 * Allocate a new array of pipe buffers and copy the info over. Returns the
1106 * pipe size if successful, or return -ERROR on error.
1108 static long pipe_set_size(struct pipe_inode_info
*pipe
, unsigned long arg
)
1110 struct pipe_buffer
*bufs
;
1113 * Must be a power-of-2 currently
1115 if (!is_power_of_2(arg
))
1119 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1120 * expect a lot of shrink+grow operations, just free and allocate
1121 * again like we would do for growing. If the pipe currently
1122 * contains more buffers than arg, then return busy.
1124 if (arg
< pipe
->nrbufs
)
1127 bufs
= kcalloc(arg
, sizeof(struct pipe_buffer
), GFP_KERNEL
);
1128 if (unlikely(!bufs
))
1132 * The pipe array wraps around, so just start the new one at zero
1133 * and adjust the indexes.
1136 const unsigned int tail
= pipe
->nrbufs
& (pipe
->buffers
- 1);
1137 const unsigned int head
= pipe
->nrbufs
- tail
;
1140 memcpy(bufs
, pipe
->bufs
+ pipe
->curbuf
, head
* sizeof(struct pipe_buffer
));
1142 memcpy(bufs
+ head
, pipe
->bufs
+ pipe
->curbuf
, tail
* sizeof(struct pipe_buffer
));
1148 pipe
->buffers
= arg
;
1152 long pipe_fcntl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1154 struct pipe_inode_info
*pipe
;
1157 pipe
= file
->f_path
.dentry
->d_inode
->i_pipe
;
1161 mutex_lock(&pipe
->inode
->i_mutex
);
1165 ret
= pipe_set_size(pipe
, arg
);
1168 ret
= pipe
->buffers
;
1175 mutex_unlock(&pipe
->inode
->i_mutex
);
1180 * pipefs should _never_ be mounted by userland - too much of security hassle,
1181 * no real gain from having the whole whorehouse mounted. So we don't need
1182 * any operations on the root directory. However, we need a non-trivial
1183 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1185 static int pipefs_get_sb(struct file_system_type
*fs_type
,
1186 int flags
, const char *dev_name
, void *data
,
1187 struct vfsmount
*mnt
)
1189 return get_sb_pseudo(fs_type
, "pipe:", NULL
, PIPEFS_MAGIC
, mnt
);
1192 static struct file_system_type pipe_fs_type
= {
1194 .get_sb
= pipefs_get_sb
,
1195 .kill_sb
= kill_anon_super
,
1198 static int __init
init_pipe_fs(void)
1200 int err
= register_filesystem(&pipe_fs_type
);
1203 pipe_mnt
= kern_mount(&pipe_fs_type
);
1204 if (IS_ERR(pipe_mnt
)) {
1205 err
= PTR_ERR(pipe_mnt
);
1206 unregister_filesystem(&pipe_fs_type
);
1212 static void __exit
exit_pipe_fs(void)
1214 unregister_filesystem(&pipe_fs_type
);
1218 fs_initcall(init_pipe_fs
);
1219 module_exit(exit_pipe_fs
);