4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/malloc.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 #include <asm/uaccess.h>
17 * We use a start+len construction, which provides full use of the
19 * -- Florian Coosmann (FGC)
21 * Reads with count = 0 should always return 0.
22 * -- Julian Bradfield 1999-06-07.
25 /* Drop the inode semaphore and wait for a pipe event, atomically */
26 void pipe_wait(struct inode
* inode
)
28 DECLARE_WAITQUEUE(wait
, current
);
29 current
->state
= TASK_INTERRUPTIBLE
;
30 add_wait_queue(PIPE_WAIT(*inode
), &wait
);
33 remove_wait_queue(PIPE_WAIT(*inode
), &wait
);
34 current
->state
= TASK_RUNNING
;
35 down(PIPE_SEM(*inode
));
39 pipe_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
41 struct inode
*inode
= filp
->f_dentry
->d_inode
;
42 ssize_t size
, read
, ret
;
44 /* Seeks are not allowed on pipes. */
47 if (ppos
!= &filp
->f_pos
)
50 /* Always return 0 on null read. */
55 /* Get the pipe semaphore */
57 if (down_interruptible(PIPE_SEM(*inode
)))
60 if (PIPE_EMPTY(*inode
)) {
63 if (!PIPE_WRITERS(*inode
))
67 if (filp
->f_flags
& O_NONBLOCK
)
71 PIPE_WAITING_READERS(*inode
)++;
73 PIPE_WAITING_READERS(*inode
)--;
75 if (signal_pending(current
))
78 if (!PIPE_EMPTY(*inode
))
80 if (!PIPE_WRITERS(*inode
))
85 /* Read what data is available. */
87 while (count
> 0 && (size
= PIPE_LEN(*inode
))) {
88 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_START(*inode
);
89 ssize_t chars
= PIPE_MAX_RCHUNK(*inode
);
96 if (copy_to_user(buf
, pipebuf
, chars
))
100 PIPE_START(*inode
) += chars
;
101 PIPE_START(*inode
) &= (PIPE_SIZE
- 1);
102 PIPE_LEN(*inode
) -= chars
;
107 /* Cache behaviour optimization */
108 if (!PIPE_LEN(*inode
))
109 PIPE_START(*inode
) = 0;
111 if (count
&& PIPE_WAITING_WRITERS(*inode
) && !(filp
->f_flags
& O_NONBLOCK
)) {
113 * We know that we are going to sleep: signal
114 * writers synchronously that there is more
117 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
118 if (!PIPE_EMPTY(*inode
))
122 /* Signal writers asynchronously that there is more room. */
123 wake_up_interruptible(PIPE_WAIT(*inode
));
127 up(PIPE_SEM(*inode
));
135 pipe_write(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
137 struct inode
*inode
= filp
->f_dentry
->d_inode
;
138 ssize_t free
, written
, ret
;
140 /* Seeks are not allowed on pipes. */
143 if (ppos
!= &filp
->f_pos
)
146 /* Null write succeeds. */
152 if (down_interruptible(PIPE_SEM(*inode
)))
155 /* No readers yields SIGPIPE. */
156 if (!PIPE_READERS(*inode
))
159 /* If count <= PIPE_BUF, we have to make it atomic. */
160 free
= (count
<= PIPE_BUF
? count
: 1);
162 /* Wait, or check for, available space. */
163 if (filp
->f_flags
& O_NONBLOCK
) {
165 if (PIPE_FREE(*inode
) < free
)
168 while (PIPE_FREE(*inode
) < free
) {
169 PIPE_WAITING_WRITERS(*inode
)++;
171 PIPE_WAITING_WRITERS(*inode
)--;
173 if (signal_pending(current
))
176 if (!PIPE_READERS(*inode
))
181 /* Copy into available space. */
185 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_END(*inode
);
186 ssize_t chars
= PIPE_MAX_WCHUNK(*inode
);
188 if ((space
= PIPE_FREE(*inode
)) != 0) {
194 if (copy_from_user(pipebuf
, buf
, chars
))
198 PIPE_LEN(*inode
) += chars
;
201 space
= PIPE_FREE(*inode
);
206 if (filp
->f_flags
& O_NONBLOCK
)
211 * Synchronous wake-up: it knows that this process
212 * is going to give up this CPU, so it doesnt have
213 * to do idle reschedules.
215 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
216 PIPE_WAITING_WRITERS(*inode
)++;
218 PIPE_WAITING_WRITERS(*inode
)--;
219 if (signal_pending(current
))
221 if (!PIPE_READERS(*inode
))
223 } while (!PIPE_FREE(*inode
));
227 /* Signal readers asynchronously that there is more data. */
228 wake_up_interruptible(PIPE_WAIT(*inode
));
230 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
231 mark_inode_dirty(inode
);
234 up(PIPE_SEM(*inode
));
243 up(PIPE_SEM(*inode
));
244 send_sig(SIGPIPE
, current
, 0);
249 pipe_lseek(struct file
*file
, loff_t offset
, int orig
)
255 bad_pipe_r(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
261 bad_pipe_w(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
267 pipe_ioctl(struct inode
*pino
, struct file
*filp
,
268 unsigned int cmd
, unsigned long arg
)
272 return put_user(PIPE_LEN(*pino
), (int *)arg
);
278 /* No kernel lock held - fine */
280 pipe_poll(struct file
*filp
, poll_table
*wait
)
283 struct inode
*inode
= filp
->f_dentry
->d_inode
;
285 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
287 /* Reading only -- no need for aquiring the semaphore. */
288 mask
= POLLIN
| POLLRDNORM
;
289 if (PIPE_EMPTY(*inode
))
290 mask
= POLLOUT
| POLLWRNORM
;
291 if (!PIPE_WRITERS(*inode
) && filp
->f_version
!= PIPE_WCOUNTER(*inode
))
293 if (!PIPE_READERS(*inode
))
299 /* FIXME: most Unices do not set POLLERR for fifos */
300 #define fifo_poll pipe_poll
303 pipe_release(struct inode
*inode
, int decr
, int decw
)
305 down(PIPE_SEM(*inode
));
306 PIPE_READERS(*inode
) -= decr
;
307 PIPE_WRITERS(*inode
) -= decw
;
308 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
309 struct pipe_inode_info
*info
= inode
->i_pipe
;
310 inode
->i_pipe
= NULL
;
311 free_page((unsigned long) info
->base
);
314 wake_up_interruptible(PIPE_WAIT(*inode
));
316 up(PIPE_SEM(*inode
));
322 pipe_read_release(struct inode
*inode
, struct file
*filp
)
324 return pipe_release(inode
, 1, 0);
328 pipe_write_release(struct inode
*inode
, struct file
*filp
)
330 return pipe_release(inode
, 0, 1);
334 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
338 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
339 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
340 return pipe_release(inode
, decr
, decw
);
344 pipe_read_open(struct inode
*inode
, struct file
*filp
)
346 /* We could have perhaps used atomic_t, but this and friends
347 below are the only places. So it doesn't seem worthwhile. */
348 down(PIPE_SEM(*inode
));
349 PIPE_READERS(*inode
)++;
350 up(PIPE_SEM(*inode
));
356 pipe_write_open(struct inode
*inode
, struct file
*filp
)
358 down(PIPE_SEM(*inode
));
359 PIPE_WRITERS(*inode
)++;
360 up(PIPE_SEM(*inode
));
366 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
368 down(PIPE_SEM(*inode
));
369 if (filp
->f_mode
& FMODE_READ
)
370 PIPE_READERS(*inode
)++;
371 if (filp
->f_mode
& FMODE_WRITE
)
372 PIPE_WRITERS(*inode
)++;
373 up(PIPE_SEM(*inode
));
379 * The file_operations structs are not static because they
380 * are also used in linux/fs/fifo.c to do operations on FIFOs.
382 struct file_operations read_fifo_fops
= {
388 open
: pipe_read_open
,
389 release
: pipe_read_release
,
392 struct file_operations write_fifo_fops
= {
398 open
: pipe_write_open
,
399 release
: pipe_write_release
,
402 struct file_operations rdwr_fifo_fops
= {
408 open
: pipe_rdwr_open
,
409 release
: pipe_rdwr_release
,
412 struct file_operations read_pipe_fops
= {
418 open
: pipe_read_open
,
419 release
: pipe_read_release
,
422 struct file_operations write_pipe_fops
= {
428 open
: pipe_write_open
,
429 release
: pipe_write_release
,
432 struct file_operations rdwr_pipe_fops
= {
438 open
: pipe_rdwr_open
,
439 release
: pipe_rdwr_release
,
442 struct inode
* pipe_new(struct inode
* inode
)
446 page
= __get_free_page(GFP_USER
);
450 inode
->i_pipe
= kmalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
454 init_waitqueue_head(PIPE_WAIT(*inode
));
455 PIPE_BASE(*inode
) = (char*) page
;
456 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
457 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 0;
458 PIPE_WAITING_READERS(*inode
) = PIPE_WAITING_WRITERS(*inode
) = 0;
459 PIPE_RCOUNTER(*inode
) = PIPE_WCOUNTER(*inode
) = 1;
467 static struct vfsmount
*pipe_mnt
;
468 static int pipefs_delete_dentry(struct dentry
*dentry
)
472 static struct dentry_operations pipefs_dentry_operations
= {
473 d_delete
: pipefs_delete_dentry
,
476 static struct inode
* get_pipe_inode(void)
478 struct inode
*inode
= get_empty_inode();
485 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
486 inode
->i_fop
= &rdwr_pipe_fops
;
487 inode
->i_sb
= pipe_mnt
->mnt_sb
;
490 * Mark the inode dirty from the very beginning,
491 * that way it will never be moved to the dirty
492 * list because "mark_inode_dirty()" will think
493 * that it already _is_ on the dirty list.
495 inode
->i_state
= I_DIRTY
;
496 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
497 inode
->i_uid
= current
->fsuid
;
498 inode
->i_gid
= current
->fsgid
;
499 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
500 inode
->i_blksize
= PAGE_SIZE
;
513 struct dentry
*dentry
;
514 struct inode
* inode
;
515 struct file
*f1
, *f2
;
520 f1
= get_empty_filp();
524 f2
= get_empty_filp();
528 inode
= get_pipe_inode();
532 error
= get_unused_fd();
534 goto close_f12_inode
;
537 error
= get_unused_fd();
539 goto close_f12_inode_i
;
543 sprintf(name
, "[%lu]", inode
->i_ino
);
545 this.len
= strlen(name
);
546 this.hash
= inode
->i_ino
; /* will go */
547 dentry
= d_alloc(pipe_mnt
->mnt_sb
->s_root
, &this);
548 dentry
->d_op
= &pipefs_dentry_operations
;
550 goto close_f12_inode_i_j
;
551 d_add(dentry
, inode
);
552 f1
->f_vfsmnt
= f2
->f_vfsmnt
= mntget(mntget(pipe_mnt
));
553 f1
->f_dentry
= f2
->f_dentry
= dget(dentry
);
556 f1
->f_pos
= f2
->f_pos
= 0;
557 f1
->f_flags
= O_RDONLY
;
558 f1
->f_op
= &read_pipe_fops
;
563 f2
->f_flags
= O_WRONLY
;
564 f2
->f_op
= &write_pipe_fops
;
579 free_page((unsigned long) PIPE_BASE(*inode
));
580 kfree(inode
->i_pipe
);
581 inode
->i_pipe
= NULL
;
592 * pipefs should _never_ be mounted by userland - too much of security hassle,
593 * no real gain from having the whole whorehouse mounted. So we don't need
594 * any operations on the root directory. However, we need a non-trivial
595 * d_name - pipe: will go nicely and kill the special-casing in procfs.
597 static int pipefs_statfs(struct super_block
*sb
, struct statfs
*buf
)
599 buf
->f_type
= PIPEFS_MAGIC
;
601 buf
->f_namelen
= 255;
605 static struct super_operations pipefs_ops
= {
606 statfs
: pipefs_statfs
,
609 static struct super_block
* pipefs_read_super(struct super_block
*sb
, void *data
, int silent
)
611 struct inode
*root
= get_empty_inode();
614 root
->i_mode
= S_IFDIR
| S_IRUSR
| S_IWUSR
;
615 root
->i_uid
= root
->i_gid
= 0;
616 root
->i_atime
= root
->i_mtime
= root
->i_ctime
= CURRENT_TIME
;
618 root
->i_dev
= sb
->s_dev
;
619 sb
->s_blocksize
= 1024;
620 sb
->s_blocksize_bits
= 10;
621 sb
->s_magic
= PIPEFS_MAGIC
;
622 sb
->s_op
= &pipefs_ops
;
623 sb
->s_root
= d_alloc(NULL
, &(const struct qstr
) { "pipe:", 5, 0 });
628 sb
->s_root
->d_sb
= sb
;
629 sb
->s_root
->d_parent
= sb
->s_root
;
630 d_instantiate(sb
->s_root
, root
);
634 static DECLARE_FSTYPE(pipe_fs_type
, "pipefs", pipefs_read_super
,
635 FS_NOMOUNT
|FS_SINGLE
);
637 static int __init
init_pipe_fs(void)
639 int err
= register_filesystem(&pipe_fs_type
);
641 pipe_mnt
= kern_mount(&pipe_fs_type
);
642 err
= PTR_ERR(pipe_mnt
);
643 if (IS_ERR(pipe_mnt
))
644 unregister_filesystem(&pipe_fs_type
);
651 static void __exit
exit_pipe_fs(void)
653 unregister_filesystem(&pipe_fs_type
);
654 kern_umount(pipe_mnt
);
657 module_init(init_pipe_fs
)
658 module_exit(exit_pipe_fs
)