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>
20 #include <linux/syscalls.h>
22 #include <asm/uaccess.h>
23 #include <asm/ioctls.h>
26 * We use a start+len construction, which provides full use of the
28 * -- Florian Coosmann (FGC)
30 * Reads with count = 0 should always return 0.
31 * -- Julian Bradfield 1999-06-07.
33 * FIFOs and Pipes now generate SIGIO for both readers and writers.
34 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
36 * pipe_read & write cleanup
37 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
40 static void pipe_lock_nested(struct pipe_inode_info
*pipe
, int subclass
)
43 mutex_lock_nested(&pipe
->inode
->i_mutex
, subclass
);
46 void pipe_lock(struct pipe_inode_info
*pipe
)
49 * pipe_lock() nests non-pipe inode locks (for writing to a file)
51 pipe_lock_nested(pipe
, I_MUTEX_PARENT
);
53 EXPORT_SYMBOL(pipe_lock
);
55 void pipe_unlock(struct pipe_inode_info
*pipe
)
58 mutex_unlock(&pipe
->inode
->i_mutex
);
60 EXPORT_SYMBOL(pipe_unlock
);
62 void pipe_double_lock(struct pipe_inode_info
*pipe1
,
63 struct pipe_inode_info
*pipe2
)
65 BUG_ON(pipe1
== pipe2
);
68 pipe_lock_nested(pipe1
, I_MUTEX_PARENT
);
69 pipe_lock_nested(pipe2
, I_MUTEX_CHILD
);
71 pipe_lock_nested(pipe2
, I_MUTEX_CHILD
);
72 pipe_lock_nested(pipe1
, I_MUTEX_PARENT
);
76 /* Drop the inode semaphore and wait for a pipe event, atomically */
77 void pipe_wait(struct pipe_inode_info
*pipe
)
82 * Pipes are system-local resources, so sleeping on them
83 * is considered a noninteractive wait:
85 prepare_to_wait(&pipe
->wait
, &wait
, TASK_INTERRUPTIBLE
);
88 finish_wait(&pipe
->wait
, &wait
);
93 pipe_iov_copy_from_user(void *to
, struct iovec
*iov
, unsigned long len
,
101 copy
= min_t(unsigned long, len
, iov
->iov_len
);
104 if (__copy_from_user_inatomic(to
, iov
->iov_base
, copy
))
107 if (copy_from_user(to
, iov
->iov_base
, copy
))
112 iov
->iov_base
+= copy
;
113 iov
->iov_len
-= copy
;
119 pipe_iov_copy_to_user(struct iovec
*iov
, const void *from
, unsigned long len
,
125 while (!iov
->iov_len
)
127 copy
= min_t(unsigned long, len
, iov
->iov_len
);
130 if (__copy_to_user_inatomic(iov
->iov_base
, from
, copy
))
133 if (copy_to_user(iov
->iov_base
, from
, copy
))
138 iov
->iov_base
+= copy
;
139 iov
->iov_len
-= copy
;
145 * Attempt to pre-fault in the user memory, so we can use atomic copies.
146 * Returns the number of bytes not faulted in.
148 static int iov_fault_in_pages_write(struct iovec
*iov
, unsigned long len
)
150 while (!iov
->iov_len
)
154 unsigned long this_len
;
156 this_len
= min_t(unsigned long, len
, iov
->iov_len
);
157 if (fault_in_pages_writeable(iov
->iov_base
, this_len
))
168 * Pre-fault in the user memory, so we can use atomic copies.
170 static void iov_fault_in_pages_read(struct iovec
*iov
, unsigned long len
)
172 while (!iov
->iov_len
)
176 unsigned long this_len
;
178 this_len
= min_t(unsigned long, len
, iov
->iov_len
);
179 fault_in_pages_readable(iov
->iov_base
, this_len
);
185 static void anon_pipe_buf_release(struct pipe_inode_info
*pipe
,
186 struct pipe_buffer
*buf
)
188 struct page
*page
= buf
->page
;
191 * If nobody else uses this page, and we don't already have a
192 * temporary page, let's keep track of it as a one-deep
193 * allocation cache. (Otherwise just release our reference to it)
195 if (page_count(page
) == 1 && !pipe
->tmp_page
)
196 pipe
->tmp_page
= page
;
198 page_cache_release(page
);
202 * generic_pipe_buf_map - virtually map a pipe buffer
203 * @pipe: the pipe that the buffer belongs to
204 * @buf: the buffer that should be mapped
205 * @atomic: whether to use an atomic map
208 * This function returns a kernel virtual address mapping for the
209 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
210 * and the caller has to be careful not to fault before calling
211 * the unmap function.
213 * Note that this function occupies KM_USER0 if @atomic != 0.
215 void *generic_pipe_buf_map(struct pipe_inode_info
*pipe
,
216 struct pipe_buffer
*buf
, int atomic
)
219 buf
->flags
|= PIPE_BUF_FLAG_ATOMIC
;
220 return kmap_atomic(buf
->page
, KM_USER0
);
223 return kmap(buf
->page
);
227 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
228 * @pipe: the pipe that the buffer belongs to
229 * @buf: the buffer that should be unmapped
230 * @map_data: the data that the mapping function returned
233 * This function undoes the mapping that ->map() provided.
235 void generic_pipe_buf_unmap(struct pipe_inode_info
*pipe
,
236 struct pipe_buffer
*buf
, void *map_data
)
238 if (buf
->flags
& PIPE_BUF_FLAG_ATOMIC
) {
239 buf
->flags
&= ~PIPE_BUF_FLAG_ATOMIC
;
240 kunmap_atomic(map_data
, KM_USER0
);
246 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
247 * @pipe: the pipe that the buffer belongs to
248 * @buf: the buffer to attempt to steal
251 * This function attempts to steal the &struct page attached to
252 * @buf. If successful, this function returns 0 and returns with
253 * the page locked. The caller may then reuse the page for whatever
254 * he wishes; the typical use is insertion into a different file
257 int generic_pipe_buf_steal(struct pipe_inode_info
*pipe
,
258 struct pipe_buffer
*buf
)
260 struct page
*page
= buf
->page
;
263 * A reference of one is golden, that means that the owner of this
264 * page is the only one holding a reference to it. lock the page
267 if (page_count(page
) == 1) {
276 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
277 * @pipe: the pipe that the buffer belongs to
278 * @buf: the buffer to get a reference to
281 * This function grabs an extra reference to @buf. It's used in
282 * in the tee() system call, when we duplicate the buffers in one
285 void generic_pipe_buf_get(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
)
287 page_cache_get(buf
->page
);
291 * generic_pipe_buf_confirm - verify contents of the pipe buffer
292 * @info: the pipe that the buffer belongs to
293 * @buf: the buffer to confirm
296 * This function does nothing, because the generic pipe code uses
297 * pages that are always good when inserted into the pipe.
299 int generic_pipe_buf_confirm(struct pipe_inode_info
*info
,
300 struct pipe_buffer
*buf
)
305 static const struct pipe_buf_operations anon_pipe_buf_ops
= {
307 .map
= generic_pipe_buf_map
,
308 .unmap
= generic_pipe_buf_unmap
,
309 .confirm
= generic_pipe_buf_confirm
,
310 .release
= anon_pipe_buf_release
,
311 .steal
= generic_pipe_buf_steal
,
312 .get
= generic_pipe_buf_get
,
316 pipe_read(struct kiocb
*iocb
, const struct iovec
*_iov
,
317 unsigned long nr_segs
, loff_t pos
)
319 struct file
*filp
= iocb
->ki_filp
;
320 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
321 struct pipe_inode_info
*pipe
;
324 struct iovec
*iov
= (struct iovec
*)_iov
;
327 total_len
= iov_length(iov
, nr_segs
);
328 /* Null read succeeds. */
329 if (unlikely(total_len
== 0))
334 mutex_lock(&inode
->i_mutex
);
335 pipe
= inode
->i_pipe
;
337 int bufs
= pipe
->nrbufs
;
339 int curbuf
= pipe
->curbuf
;
340 struct pipe_buffer
*buf
= pipe
->bufs
+ curbuf
;
341 const struct pipe_buf_operations
*ops
= buf
->ops
;
343 size_t chars
= buf
->len
;
346 if (chars
> total_len
)
349 error
= ops
->confirm(pipe
, buf
);
356 atomic
= !iov_fault_in_pages_write(iov
, chars
);
358 addr
= ops
->map(pipe
, buf
, atomic
);
359 error
= pipe_iov_copy_to_user(iov
, addr
+ buf
->offset
, chars
, atomic
);
360 ops
->unmap(pipe
, buf
, addr
);
361 if (unlikely(error
)) {
363 * Just retry with the slow path if we failed.
374 buf
->offset
+= chars
;
378 ops
->release(pipe
, buf
);
379 curbuf
= (curbuf
+ 1) & (PIPE_BUFFERS
-1);
380 pipe
->curbuf
= curbuf
;
381 pipe
->nrbufs
= --bufs
;
386 break; /* common path: read succeeded */
388 if (bufs
) /* More to do? */
392 if (!pipe
->waiting_writers
) {
393 /* syscall merging: Usually we must not sleep
394 * if O_NONBLOCK is set, or if we got some data.
395 * But if a writer sleeps in kernel space, then
396 * we can wait for that data without violating POSIX.
400 if (filp
->f_flags
& O_NONBLOCK
) {
405 if (signal_pending(current
)) {
411 wake_up_interruptible_sync(&pipe
->wait
);
412 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
416 mutex_unlock(&inode
->i_mutex
);
418 /* Signal writers asynchronously that there is more room. */
420 wake_up_interruptible_sync(&pipe
->wait
);
421 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
429 pipe_write(struct kiocb
*iocb
, const struct iovec
*_iov
,
430 unsigned long nr_segs
, loff_t ppos
)
432 struct file
*filp
= iocb
->ki_filp
;
433 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
434 struct pipe_inode_info
*pipe
;
437 struct iovec
*iov
= (struct iovec
*)_iov
;
441 total_len
= iov_length(iov
, nr_segs
);
442 /* Null write succeeds. */
443 if (unlikely(total_len
== 0))
448 mutex_lock(&inode
->i_mutex
);
449 pipe
= inode
->i_pipe
;
451 if (!pipe
->readers
) {
452 send_sig(SIGPIPE
, current
, 0);
457 /* We try to merge small writes */
458 chars
= total_len
& (PAGE_SIZE
-1); /* size of the last buffer */
459 if (pipe
->nrbufs
&& chars
!= 0) {
460 int lastbuf
= (pipe
->curbuf
+ pipe
->nrbufs
- 1) &
462 struct pipe_buffer
*buf
= pipe
->bufs
+ lastbuf
;
463 const struct pipe_buf_operations
*ops
= buf
->ops
;
464 int offset
= buf
->offset
+ buf
->len
;
466 if (ops
->can_merge
&& offset
+ chars
<= PAGE_SIZE
) {
467 int error
, atomic
= 1;
470 error
= ops
->confirm(pipe
, buf
);
474 iov_fault_in_pages_read(iov
, chars
);
476 addr
= ops
->map(pipe
, buf
, atomic
);
477 error
= pipe_iov_copy_from_user(offset
+ addr
, iov
,
479 ops
->unmap(pipe
, buf
, addr
);
500 if (!pipe
->readers
) {
501 send_sig(SIGPIPE
, current
, 0);
507 if (bufs
< PIPE_BUFFERS
) {
508 int newbuf
= (pipe
->curbuf
+ bufs
) & (PIPE_BUFFERS
-1);
509 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
510 struct page
*page
= pipe
->tmp_page
;
512 int error
, atomic
= 1;
515 page
= alloc_page(GFP_HIGHUSER
);
516 if (unlikely(!page
)) {
517 ret
= ret
? : -ENOMEM
;
520 pipe
->tmp_page
= page
;
522 /* Always wake up, even if the copy fails. Otherwise
523 * we lock up (O_NONBLOCK-)readers that sleep due to
525 * FIXME! Is this really true?
529 if (chars
> total_len
)
532 iov_fault_in_pages_read(iov
, chars
);
535 src
= kmap_atomic(page
, KM_USER0
);
539 error
= pipe_iov_copy_from_user(src
, iov
, chars
,
542 kunmap_atomic(src
, KM_USER0
);
546 if (unlikely(error
)) {
557 /* Insert it into the buffer array */
559 buf
->ops
= &anon_pipe_buf_ops
;
562 pipe
->nrbufs
= ++bufs
;
563 pipe
->tmp_page
= NULL
;
569 if (bufs
< PIPE_BUFFERS
)
571 if (filp
->f_flags
& O_NONBLOCK
) {
576 if (signal_pending(current
)) {
582 wake_up_interruptible_sync(&pipe
->wait
);
583 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
586 pipe
->waiting_writers
++;
588 pipe
->waiting_writers
--;
591 mutex_unlock(&inode
->i_mutex
);
593 wake_up_interruptible_sync(&pipe
->wait
);
594 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
597 file_update_time(filp
);
602 bad_pipe_r(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
608 bad_pipe_w(struct file
*filp
, const char __user
*buf
, size_t count
,
614 static long pipe_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
616 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
617 struct pipe_inode_info
*pipe
;
618 int count
, buf
, nrbufs
;
622 mutex_lock(&inode
->i_mutex
);
623 pipe
= inode
->i_pipe
;
626 nrbufs
= pipe
->nrbufs
;
627 while (--nrbufs
>= 0) {
628 count
+= pipe
->bufs
[buf
].len
;
629 buf
= (buf
+1) & (PIPE_BUFFERS
-1);
631 mutex_unlock(&inode
->i_mutex
);
633 return put_user(count
, (int __user
*)arg
);
639 /* No kernel lock held - fine */
641 pipe_poll(struct file
*filp
, poll_table
*wait
)
644 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
645 struct pipe_inode_info
*pipe
= inode
->i_pipe
;
648 poll_wait(filp
, &pipe
->wait
, wait
);
650 /* Reading only -- no need for acquiring the semaphore. */
651 nrbufs
= pipe
->nrbufs
;
653 if (filp
->f_mode
& FMODE_READ
) {
654 mask
= (nrbufs
> 0) ? POLLIN
| POLLRDNORM
: 0;
655 if (!pipe
->writers
&& filp
->f_version
!= pipe
->w_counter
)
659 if (filp
->f_mode
& FMODE_WRITE
) {
660 mask
|= (nrbufs
< PIPE_BUFFERS
) ? POLLOUT
| POLLWRNORM
: 0;
662 * Most Unices do not set POLLERR for FIFOs but on Linux they
663 * behave exactly like pipes for poll().
673 pipe_release(struct inode
*inode
, int decr
, int decw
)
675 struct pipe_inode_info
*pipe
;
677 mutex_lock(&inode
->i_mutex
);
678 pipe
= inode
->i_pipe
;
679 pipe
->readers
-= decr
;
680 pipe
->writers
-= decw
;
682 if (!pipe
->readers
&& !pipe
->writers
) {
683 free_pipe_info(inode
);
685 wake_up_interruptible_sync(&pipe
->wait
);
686 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
687 kill_fasync(&pipe
->fasync_writers
, SIGIO
, POLL_OUT
);
689 mutex_unlock(&inode
->i_mutex
);
695 pipe_read_fasync(int fd
, struct file
*filp
, int on
)
697 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
700 mutex_lock(&inode
->i_mutex
);
701 retval
= fasync_helper(fd
, filp
, on
, &inode
->i_pipe
->fasync_readers
);
702 mutex_unlock(&inode
->i_mutex
);
709 pipe_write_fasync(int fd
, struct file
*filp
, int on
)
711 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
714 mutex_lock(&inode
->i_mutex
);
715 retval
= fasync_helper(fd
, filp
, on
, &inode
->i_pipe
->fasync_writers
);
716 mutex_unlock(&inode
->i_mutex
);
723 pipe_rdwr_fasync(int fd
, struct file
*filp
, int on
)
725 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
726 struct pipe_inode_info
*pipe
= inode
->i_pipe
;
729 mutex_lock(&inode
->i_mutex
);
730 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_readers
);
732 retval
= fasync_helper(fd
, filp
, on
, &pipe
->fasync_writers
);
733 if (retval
< 0) /* this can happen only if on == T */
734 fasync_helper(-1, filp
, 0, &pipe
->fasync_readers
);
736 mutex_unlock(&inode
->i_mutex
);
742 pipe_read_release(struct inode
*inode
, struct file
*filp
)
744 return pipe_release(inode
, 1, 0);
748 pipe_write_release(struct inode
*inode
, struct file
*filp
)
750 return pipe_release(inode
, 0, 1);
754 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
758 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
759 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
760 return pipe_release(inode
, decr
, decw
);
764 pipe_read_open(struct inode
*inode
, struct file
*filp
)
766 /* We could have perhaps used atomic_t, but this and friends
767 below are the only places. So it doesn't seem worthwhile. */
768 mutex_lock(&inode
->i_mutex
);
769 inode
->i_pipe
->readers
++;
770 mutex_unlock(&inode
->i_mutex
);
776 pipe_write_open(struct inode
*inode
, struct file
*filp
)
778 mutex_lock(&inode
->i_mutex
);
779 inode
->i_pipe
->writers
++;
780 mutex_unlock(&inode
->i_mutex
);
786 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
788 mutex_lock(&inode
->i_mutex
);
789 if (filp
->f_mode
& FMODE_READ
)
790 inode
->i_pipe
->readers
++;
791 if (filp
->f_mode
& FMODE_WRITE
)
792 inode
->i_pipe
->writers
++;
793 mutex_unlock(&inode
->i_mutex
);
799 * The file_operations structs are not static because they
800 * are also used in linux/fs/fifo.c to do operations on FIFOs.
802 * Pipes reuse fifos' file_operations structs.
804 const struct file_operations read_pipefifo_fops
= {
806 .read
= do_sync_read
,
807 .aio_read
= pipe_read
,
810 .unlocked_ioctl
= pipe_ioctl
,
811 .open
= pipe_read_open
,
812 .release
= pipe_read_release
,
813 .fasync
= pipe_read_fasync
,
816 const struct file_operations write_pipefifo_fops
= {
819 .write
= do_sync_write
,
820 .aio_write
= pipe_write
,
822 .unlocked_ioctl
= pipe_ioctl
,
823 .open
= pipe_write_open
,
824 .release
= pipe_write_release
,
825 .fasync
= pipe_write_fasync
,
828 const struct file_operations rdwr_pipefifo_fops
= {
830 .read
= do_sync_read
,
831 .aio_read
= pipe_read
,
832 .write
= do_sync_write
,
833 .aio_write
= pipe_write
,
835 .unlocked_ioctl
= pipe_ioctl
,
836 .open
= pipe_rdwr_open
,
837 .release
= pipe_rdwr_release
,
838 .fasync
= pipe_rdwr_fasync
,
841 struct pipe_inode_info
* alloc_pipe_info(struct inode
*inode
)
843 struct pipe_inode_info
*pipe
;
845 pipe
= kzalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
847 init_waitqueue_head(&pipe
->wait
);
848 pipe
->r_counter
= pipe
->w_counter
= 1;
855 void __free_pipe_info(struct pipe_inode_info
*pipe
)
859 for (i
= 0; i
< PIPE_BUFFERS
; i
++) {
860 struct pipe_buffer
*buf
= pipe
->bufs
+ i
;
862 buf
->ops
->release(pipe
, buf
);
865 __free_page(pipe
->tmp_page
);
869 void free_pipe_info(struct inode
*inode
)
871 __free_pipe_info(inode
->i_pipe
);
872 inode
->i_pipe
= NULL
;
875 static struct vfsmount
*pipe_mnt __read_mostly
;
876 static int pipefs_delete_dentry(struct dentry
*dentry
)
879 * At creation time, we pretended this dentry was hashed
880 * (by clearing DCACHE_UNHASHED bit in d_flags)
881 * At delete time, we restore the truth : not hashed.
882 * (so that dput() can proceed correctly)
884 dentry
->d_flags
|= DCACHE_UNHASHED
;
889 * pipefs_dname() is called from d_path().
891 static char *pipefs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
893 return dynamic_dname(dentry
, buffer
, buflen
, "pipe:[%lu]",
894 dentry
->d_inode
->i_ino
);
897 static const struct dentry_operations pipefs_dentry_operations
= {
898 .d_delete
= pipefs_delete_dentry
,
899 .d_dname
= pipefs_dname
,
902 static struct inode
* get_pipe_inode(void)
904 struct inode
*inode
= new_inode(pipe_mnt
->mnt_sb
);
905 struct pipe_inode_info
*pipe
;
910 pipe
= alloc_pipe_info(inode
);
913 inode
->i_pipe
= pipe
;
915 pipe
->readers
= pipe
->writers
= 1;
916 inode
->i_fop
= &rdwr_pipefifo_fops
;
919 * Mark the inode dirty from the very beginning,
920 * that way it will never be moved to the dirty
921 * list because "mark_inode_dirty()" will think
922 * that it already _is_ on the dirty list.
924 inode
->i_state
= I_DIRTY
;
925 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
926 inode
->i_uid
= current_fsuid();
927 inode
->i_gid
= current_fsgid();
928 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
939 struct file
*create_write_pipe(int flags
)
944 struct dentry
*dentry
;
945 struct qstr name
= { .name
= "" };
948 inode
= get_pipe_inode();
953 dentry
= d_alloc(pipe_mnt
->mnt_sb
->s_root
, &name
);
957 dentry
->d_op
= &pipefs_dentry_operations
;
959 * We dont want to publish this dentry into global dentry hash table.
960 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
961 * This permits a working /proc/$pid/fd/XXX on pipes
963 dentry
->d_flags
&= ~DCACHE_UNHASHED
;
964 d_instantiate(dentry
, inode
);
967 f
= alloc_file(pipe_mnt
, dentry
, FMODE_WRITE
, &write_pipefifo_fops
);
970 f
->f_mapping
= inode
->i_mapping
;
972 f
->f_flags
= O_WRONLY
| (flags
& O_NONBLOCK
);
978 free_pipe_info(inode
);
983 free_pipe_info(inode
);
989 void free_write_pipe(struct file
*f
)
991 free_pipe_info(f
->f_dentry
->d_inode
);
992 path_put(&f
->f_path
);
996 struct file
*create_read_pipe(struct file
*wrf
, int flags
)
998 struct file
*f
= get_empty_filp();
1000 return ERR_PTR(-ENFILE
);
1002 /* Grab pipe from the writer */
1003 f
->f_path
= wrf
->f_path
;
1004 path_get(&wrf
->f_path
);
1005 f
->f_mapping
= wrf
->f_path
.dentry
->d_inode
->i_mapping
;
1008 f
->f_flags
= O_RDONLY
| (flags
& O_NONBLOCK
);
1009 f
->f_op
= &read_pipefifo_fops
;
1010 f
->f_mode
= FMODE_READ
;
1016 int do_pipe_flags(int *fd
, int flags
)
1018 struct file
*fw
, *fr
;
1022 if (flags
& ~(O_CLOEXEC
| O_NONBLOCK
))
1025 fw
= create_write_pipe(flags
);
1028 fr
= create_read_pipe(fw
, flags
);
1029 error
= PTR_ERR(fr
);
1031 goto err_write_pipe
;
1033 error
= get_unused_fd_flags(flags
);
1038 error
= get_unused_fd_flags(flags
);
1043 audit_fd_pair(fdr
, fdw
);
1044 fd_install(fdr
, fr
);
1045 fd_install(fdw
, fw
);
1054 path_put(&fr
->f_path
);
1057 free_write_pipe(fw
);
1062 * sys_pipe() is the normal C calling standard for creating
1063 * a pipe. It's not the way Unix traditionally does this, though.
1065 SYSCALL_DEFINE2(pipe2
, int __user
*, fildes
, int, flags
)
1070 error
= do_pipe_flags(fd
, flags
);
1072 if (copy_to_user(fildes
, fd
, sizeof(fd
))) {
1081 SYSCALL_DEFINE1(pipe
, int __user
*, fildes
)
1083 return sys_pipe2(fildes
, 0);
1087 * pipefs should _never_ be mounted by userland - too much of security hassle,
1088 * no real gain from having the whole whorehouse mounted. So we don't need
1089 * any operations on the root directory. However, we need a non-trivial
1090 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1092 static int pipefs_get_sb(struct file_system_type
*fs_type
,
1093 int flags
, const char *dev_name
, void *data
,
1094 struct vfsmount
*mnt
)
1096 return get_sb_pseudo(fs_type
, "pipe:", NULL
, PIPEFS_MAGIC
, mnt
);
1099 static struct file_system_type pipe_fs_type
= {
1101 .get_sb
= pipefs_get_sb
,
1102 .kill_sb
= kill_anon_super
,
1105 static int __init
init_pipe_fs(void)
1107 int err
= register_filesystem(&pipe_fs_type
);
1110 pipe_mnt
= kern_mount(&pipe_fs_type
);
1111 if (IS_ERR(pipe_mnt
)) {
1112 err
= PTR_ERR(pipe_mnt
);
1113 unregister_filesystem(&pipe_fs_type
);
1119 static void __exit
exit_pipe_fs(void)
1121 unregister_filesystem(&pipe_fs_type
);
1125 fs_initcall(init_pipe_fs
);
1126 module_exit(exit_pipe_fs
);