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/smp_lock.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <asm/uaccess.h>
18 * We use a start+len construction, which provides full use of the
20 * -- Florian Coosmann (FGC)
22 * Reads with count = 0 should always return 0.
23 * -- Julian Bradfield 1999-06-07.
26 /* Drop the inode semaphore and wait for a pipe event, atomically */
27 void pipe_wait(struct inode
* inode
)
29 DECLARE_WAITQUEUE(wait
, current
);
30 current
->state
= TASK_INTERRUPTIBLE
;
31 add_wait_queue(PIPE_WAIT(*inode
), &wait
);
34 remove_wait_queue(PIPE_WAIT(*inode
), &wait
);
35 current
->state
= TASK_RUNNING
;
36 down(PIPE_SEM(*inode
));
40 pipe_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
42 struct inode
*inode
= filp
->f_dentry
->d_inode
;
43 ssize_t size
, read
, ret
;
45 /* Seeks are not allowed on pipes. */
48 if (ppos
!= &filp
->f_pos
)
51 /* Always return 0 on null read. */
56 /* Get the pipe semaphore */
58 if (down_interruptible(PIPE_SEM(*inode
)))
61 if (PIPE_EMPTY(*inode
)) {
64 if (!PIPE_WRITERS(*inode
))
68 if (filp
->f_flags
& O_NONBLOCK
)
72 PIPE_WAITING_READERS(*inode
)++;
74 PIPE_WAITING_READERS(*inode
)--;
76 if (signal_pending(current
))
79 if (!PIPE_EMPTY(*inode
))
81 if (!PIPE_WRITERS(*inode
))
86 /* Read what data is available. */
88 while (count
> 0 && (size
= PIPE_LEN(*inode
))) {
89 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_START(*inode
);
90 ssize_t chars
= PIPE_MAX_RCHUNK(*inode
);
97 if (copy_to_user(buf
, pipebuf
, chars
))
101 PIPE_START(*inode
) += chars
;
102 PIPE_START(*inode
) &= (PIPE_SIZE
- 1);
103 PIPE_LEN(*inode
) -= chars
;
108 /* Cache behaviour optimization */
109 if (!PIPE_LEN(*inode
))
110 PIPE_START(*inode
) = 0;
112 if (count
&& PIPE_WAITING_WRITERS(*inode
) && !(filp
->f_flags
& O_NONBLOCK
)) {
114 * We know that we are going to sleep: signal
115 * writers synchronously that there is more
118 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
119 if (!PIPE_EMPTY(*inode
))
123 /* Signal writers asynchronously that there is more room. */
124 wake_up_interruptible(PIPE_WAIT(*inode
));
128 up(PIPE_SEM(*inode
));
136 pipe_write(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
138 struct inode
*inode
= filp
->f_dentry
->d_inode
;
139 ssize_t free
, written
, ret
;
141 /* Seeks are not allowed on pipes. */
144 if (ppos
!= &filp
->f_pos
)
147 /* Null write succeeds. */
153 if (down_interruptible(PIPE_SEM(*inode
)))
156 /* No readers yields SIGPIPE. */
157 if (!PIPE_READERS(*inode
))
160 /* If count <= PIPE_BUF, we have to make it atomic. */
161 free
= (count
<= PIPE_BUF
? count
: 1);
163 /* Wait, or check for, available space. */
164 if (filp
->f_flags
& O_NONBLOCK
) {
166 if (PIPE_FREE(*inode
) < free
)
169 while (PIPE_FREE(*inode
) < free
) {
170 PIPE_WAITING_WRITERS(*inode
)++;
172 PIPE_WAITING_WRITERS(*inode
)--;
174 if (signal_pending(current
))
177 if (!PIPE_READERS(*inode
))
182 /* Copy into available space. */
186 char *pipebuf
= PIPE_BASE(*inode
) + PIPE_END(*inode
);
187 ssize_t chars
= PIPE_MAX_WCHUNK(*inode
);
189 if ((space
= PIPE_FREE(*inode
)) != 0) {
195 if (copy_from_user(pipebuf
, buf
, chars
))
199 PIPE_LEN(*inode
) += chars
;
202 space
= PIPE_FREE(*inode
);
207 if (filp
->f_flags
& O_NONBLOCK
)
212 * Synchronous wake-up: it knows that this process
213 * is going to give up this CPU, so it doesnt have
214 * to do idle reschedules.
216 wake_up_interruptible_sync(PIPE_WAIT(*inode
));
217 PIPE_WAITING_WRITERS(*inode
)++;
219 PIPE_WAITING_WRITERS(*inode
)--;
220 if (signal_pending(current
))
222 if (!PIPE_READERS(*inode
))
224 } while (!PIPE_FREE(*inode
));
228 /* Signal readers asynchronously that there is more data. */
229 wake_up_interruptible(PIPE_WAIT(*inode
));
231 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
232 mark_inode_dirty(inode
);
235 up(PIPE_SEM(*inode
));
244 up(PIPE_SEM(*inode
));
245 send_sig(SIGPIPE
, current
, 0);
250 pipe_lseek(struct file
*file
, loff_t offset
, int orig
)
256 bad_pipe_r(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
262 bad_pipe_w(struct file
*filp
, const char *buf
, size_t count
, loff_t
*ppos
)
268 pipe_ioctl(struct inode
*pino
, struct file
*filp
,
269 unsigned int cmd
, unsigned long arg
)
273 return put_user(PIPE_LEN(*pino
), (int *)arg
);
279 /* No kernel lock held - fine */
281 pipe_poll(struct file
*filp
, poll_table
*wait
)
284 struct inode
*inode
= filp
->f_dentry
->d_inode
;
286 poll_wait(filp
, PIPE_WAIT(*inode
), wait
);
288 /* Reading only -- no need for aquiring the semaphore. */
289 mask
= POLLIN
| POLLRDNORM
;
290 if (PIPE_EMPTY(*inode
))
291 mask
= POLLOUT
| POLLWRNORM
;
292 if (!PIPE_WRITERS(*inode
) && filp
->f_version
!= PIPE_WCOUNTER(*inode
))
294 if (!PIPE_READERS(*inode
))
300 /* FIXME: most Unices do not set POLLERR for fifos */
301 #define fifo_poll pipe_poll
304 pipe_release(struct inode
*inode
, int decr
, int decw
)
306 down(PIPE_SEM(*inode
));
307 PIPE_READERS(*inode
) -= decr
;
308 PIPE_WRITERS(*inode
) -= decw
;
309 if (!PIPE_READERS(*inode
) && !PIPE_WRITERS(*inode
)) {
310 struct pipe_inode_info
*info
= inode
->i_pipe
;
311 inode
->i_pipe
= NULL
;
312 free_page((unsigned long) info
->base
);
315 wake_up_interruptible(PIPE_WAIT(*inode
));
317 up(PIPE_SEM(*inode
));
323 pipe_read_release(struct inode
*inode
, struct file
*filp
)
325 return pipe_release(inode
, 1, 0);
329 pipe_write_release(struct inode
*inode
, struct file
*filp
)
331 return pipe_release(inode
, 0, 1);
335 pipe_rdwr_release(struct inode
*inode
, struct file
*filp
)
339 decr
= (filp
->f_mode
& FMODE_READ
) != 0;
340 decw
= (filp
->f_mode
& FMODE_WRITE
) != 0;
341 return pipe_release(inode
, decr
, decw
);
345 pipe_read_open(struct inode
*inode
, struct file
*filp
)
347 /* We could have perhaps used atomic_t, but this and friends
348 below are the only places. So it doesn't seem worthwhile. */
349 down(PIPE_SEM(*inode
));
350 PIPE_READERS(*inode
)++;
351 up(PIPE_SEM(*inode
));
357 pipe_write_open(struct inode
*inode
, struct file
*filp
)
359 down(PIPE_SEM(*inode
));
360 PIPE_WRITERS(*inode
)++;
361 up(PIPE_SEM(*inode
));
367 pipe_rdwr_open(struct inode
*inode
, struct file
*filp
)
369 down(PIPE_SEM(*inode
));
370 if (filp
->f_mode
& FMODE_READ
)
371 PIPE_READERS(*inode
)++;
372 if (filp
->f_mode
& FMODE_WRITE
)
373 PIPE_WRITERS(*inode
)++;
374 up(PIPE_SEM(*inode
));
380 * The file_operations structs are not static because they
381 * are also used in linux/fs/fifo.c to do operations on FIFOs.
383 struct file_operations read_fifo_fops
= {
389 open
: pipe_read_open
,
390 release
: pipe_read_release
,
393 struct file_operations write_fifo_fops
= {
399 open
: pipe_write_open
,
400 release
: pipe_write_release
,
403 struct file_operations rdwr_fifo_fops
= {
409 open
: pipe_rdwr_open
,
410 release
: pipe_rdwr_release
,
413 struct file_operations read_pipe_fops
= {
419 open
: pipe_read_open
,
420 release
: pipe_read_release
,
423 struct file_operations write_pipe_fops
= {
429 open
: pipe_write_open
,
430 release
: pipe_write_release
,
433 struct file_operations rdwr_pipe_fops
= {
439 open
: pipe_rdwr_open
,
440 release
: pipe_rdwr_release
,
443 struct inode
* pipe_new(struct inode
* inode
)
447 page
= __get_free_page(GFP_USER
);
451 inode
->i_pipe
= kmalloc(sizeof(struct pipe_inode_info
), GFP_KERNEL
);
455 init_waitqueue_head(PIPE_WAIT(*inode
));
456 PIPE_BASE(*inode
) = (char*) page
;
457 PIPE_START(*inode
) = PIPE_LEN(*inode
) = 0;
458 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 0;
459 PIPE_WAITING_READERS(*inode
) = PIPE_WAITING_WRITERS(*inode
) = 0;
460 PIPE_RCOUNTER(*inode
) = PIPE_WCOUNTER(*inode
) = 1;
468 static struct vfsmount
*pipe_mnt
;
470 static struct inode
* get_pipe_inode(void)
472 struct inode
*inode
= get_empty_inode();
479 PIPE_READERS(*inode
) = PIPE_WRITERS(*inode
) = 1;
480 inode
->i_fop
= &rdwr_pipe_fops
;
481 inode
->i_sb
= pipe_mnt
->mnt_sb
;
484 * Mark the inode dirty from the very beginning,
485 * that way it will never be moved to the dirty
486 * list because "mark_inode_dirty()" will think
487 * that it already _is_ on the dirty list.
489 inode
->i_state
= I_DIRTY
;
490 inode
->i_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
491 inode
->i_uid
= current
->fsuid
;
492 inode
->i_gid
= current
->fsgid
;
493 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
494 inode
->i_blksize
= PAGE_SIZE
;
507 struct dentry
*dentry
;
508 struct inode
* inode
;
509 struct file
*f1
, *f2
;
514 f1
= get_empty_filp();
518 f2
= get_empty_filp();
522 inode
= get_pipe_inode();
526 error
= get_unused_fd();
528 goto close_f12_inode
;
531 error
= get_unused_fd();
533 goto close_f12_inode_i
;
537 sprintf(name
, "%lu", inode
->i_ino
);
539 this.len
= strlen(name
);
540 /* We don't care for hash - it will never be looked up */
541 dentry
= d_alloc(pipe_mnt
->mnt_sb
->s_root
, &this);
543 goto close_f12_inode_i_j
;
544 d_instantiate(dentry
, inode
);
545 f1
->f_vfsmnt
= f2
->f_vfsmnt
= mntget(mntget(pipe_mnt
));
546 f1
->f_dentry
= f2
->f_dentry
= dget(dentry
);
549 f1
->f_pos
= f2
->f_pos
= 0;
550 f1
->f_flags
= O_RDONLY
;
551 f1
->f_op
= &read_pipe_fops
;
556 f2
->f_flags
= O_WRONLY
;
557 f2
->f_op
= &write_pipe_fops
;
572 free_page((unsigned long) PIPE_BASE(*inode
));
573 kfree(inode
->i_pipe
);
574 inode
->i_pipe
= NULL
;
585 * pipefs should _never_ be mounted by userland - too much of security hassle,
586 * no real gain from having the whole whorehouse mounted. So we don't need
587 * any operations on the root directory. However, we need a non-trivial
588 * d_name - pipe: will go nicely and kill the special-casing in procfs.
590 static int pipefs_statfs(struct super_block
*sb
, struct statfs
*buf
)
592 buf
->f_type
= PIPEFS_MAGIC
;
594 buf
->f_namelen
= 255;
598 static struct super_operations pipefs_ops
= {
599 statfs
: pipefs_statfs
,
602 static struct super_block
* pipefs_read_super(struct super_block
*sb
, void *data
, int silent
)
604 struct inode
*root
= get_empty_inode();
607 root
->i_mode
= S_IFDIR
| S_IRUSR
| S_IWUSR
;
608 root
->i_uid
= root
->i_gid
= 0;
609 root
->i_atime
= root
->i_mtime
= root
->i_ctime
= CURRENT_TIME
;
611 root
->i_dev
= sb
->s_dev
;
612 sb
->s_blocksize
= 1024;
613 sb
->s_blocksize_bits
= 10;
614 sb
->s_magic
= PIPEFS_MAGIC
;
615 sb
->s_op
= &pipefs_ops
;
616 sb
->s_root
= d_alloc(NULL
, &(const struct qstr
) { "pipe:", 5, 0 });
621 sb
->s_root
->d_sb
= sb
;
622 sb
->s_root
->d_parent
= sb
->s_root
;
623 d_instantiate(sb
->s_root
, root
);
627 static DECLARE_FSTYPE(pipe_fs_type
, "pipefs", pipefs_read_super
,
628 FS_NOMOUNT
|FS_SINGLE
);
630 static int __init
init_pipe_fs(void)
632 int err
= register_filesystem(&pipe_fs_type
);
634 pipe_mnt
= kern_mount(&pipe_fs_type
);
635 err
= PTR_ERR(pipe_mnt
);
636 if (!IS_ERR(pipe_mnt
))
642 static void __exit
exit_pipe_fs(void)
644 unregister_filesystem(&pipe_fs_type
);
645 kern_umount(pipe_mnt
);
648 module_init(init_pipe_fs
)
649 module_exit(exit_pipe_fs
)