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 <asm/uaccess.h>
15 #include <asm/ioctls.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));
138 pipe_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
140 struct inode *inode = filp->f_dentry->d_inode;
141 ssize_t free, written, ret;
143 /* Seeks are not allowed on pipes. */
146 if (ppos != &filp->f_pos)
149 /* Null write succeeds. */
155 if (down_interruptible(PIPE_SEM(*inode)))
158 /* No readers yields SIGPIPE. */
159 if (!PIPE_READERS(*inode))
162 /* If count <= PIPE_BUF, we have to make it atomic. */
163 free = (count <= PIPE_BUF ? count : 1);
165 /* Wait, or check for, available space. */
166 if (filp->f_flags & O_NONBLOCK) {
168 if (PIPE_FREE(*inode) < free)
171 while (PIPE_FREE(*inode) < free) {
172 PIPE_WAITING_WRITERS(*inode)++;
174 PIPE_WAITING_WRITERS(*inode)--;
176 if (signal_pending(current))
179 if (!PIPE_READERS(*inode))
184 /* Copy into available space. */
188 char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode);
189 ssize_t chars = PIPE_MAX_WCHUNK(*inode);
191 if ((space = PIPE_FREE(*inode)) != 0) {
197 if (copy_from_user(pipebuf, buf, chars))
201 PIPE_LEN(*inode) += chars;
204 space = PIPE_FREE(*inode);
209 if (filp->f_flags & O_NONBLOCK)
214 * Synchronous wake-up: it knows that this process
215 * is going to give up this CPU, so it doesn't have
216 * to do idle reschedules.
218 wake_up_interruptible_sync(PIPE_WAIT(*inode));
219 PIPE_WAITING_WRITERS(*inode)++;
221 PIPE_WAITING_WRITERS(*inode)--;
222 if (signal_pending(current))
224 if (!PIPE_READERS(*inode))
226 } while (!PIPE_FREE(*inode));
230 /* Signal readers asynchronously that there is more data. */
231 wake_up_interruptible(PIPE_WAIT(*inode));
233 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
234 mark_inode_dirty(inode);
237 up(PIPE_SEM(*inode));
246 up(PIPE_SEM(*inode));
247 send_sig(SIGPIPE, current, 0);
252 bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
258 bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
264 pipe_ioctl(struct inode *pino, struct file *filp,
265 unsigned int cmd, unsigned long arg)
269 return put_user(PIPE_LEN(*pino), (int *)arg);
275 /* No kernel lock held - fine */
277 pipe_poll(struct file *filp, poll_table *wait)
280 struct inode *inode = filp->f_dentry->d_inode;
282 poll_wait(filp, PIPE_WAIT(*inode), wait);
284 /* Reading only -- no need for acquiring the semaphore. */
285 mask = POLLIN | POLLRDNORM;
286 if (PIPE_EMPTY(*inode))
287 mask = POLLOUT | POLLWRNORM;
288 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
290 if (!PIPE_READERS(*inode))
296 /* FIXME: most Unices do not set POLLERR for fifos */
297 #define fifo_poll pipe_poll
300 pipe_release(struct inode *inode, int decr, int decw)
302 down(PIPE_SEM(*inode));
303 PIPE_READERS(*inode) -= decr;
304 PIPE_WRITERS(*inode) -= decw;
305 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
306 struct pipe_inode_info *info = inode->i_pipe;
307 inode->i_pipe = NULL;
308 free_page((unsigned long) info->base);
311 wake_up_interruptible(PIPE_WAIT(*inode));
313 up(PIPE_SEM(*inode));
319 pipe_read_release(struct inode *inode, struct file *filp)
321 return pipe_release(inode, 1, 0);
325 pipe_write_release(struct inode *inode, struct file *filp)
327 return pipe_release(inode, 0, 1);
331 pipe_rdwr_release(struct inode *inode, struct file *filp)
335 decr = (filp->f_mode & FMODE_READ) != 0;
336 decw = (filp->f_mode & FMODE_WRITE) != 0;
337 return pipe_release(inode, decr, decw);
341 pipe_read_open(struct inode *inode, struct file *filp)
343 /* We could have perhaps used atomic_t, but this and friends
344 below are the only places. So it doesn't seem worthwhile. */
345 down(PIPE_SEM(*inode));
346 PIPE_READERS(*inode)++;
347 up(PIPE_SEM(*inode));
353 pipe_write_open(struct inode *inode, struct file *filp)
355 down(PIPE_SEM(*inode));
356 PIPE_WRITERS(*inode)++;
357 up(PIPE_SEM(*inode));
363 pipe_rdwr_open(struct inode *inode, struct file *filp)
365 down(PIPE_SEM(*inode));
366 if (filp->f_mode & FMODE_READ)
367 PIPE_READERS(*inode)++;
368 if (filp->f_mode & FMODE_WRITE)
369 PIPE_WRITERS(*inode)++;
370 up(PIPE_SEM(*inode));
376 * The file_operations structs are not static because they
377 * are also used in linux/fs/fifo.c to do operations on FIFOs.
379 struct file_operations read_fifo_fops = {
385 open: pipe_read_open,
386 release: pipe_read_release,
389 struct file_operations write_fifo_fops = {
395 open: pipe_write_open,
396 release: pipe_write_release,
399 struct file_operations rdwr_fifo_fops = {
405 open: pipe_rdwr_open,
406 release: pipe_rdwr_release,
409 struct file_operations read_pipe_fops = {
415 open: pipe_read_open,
416 release: pipe_read_release,
419 struct file_operations write_pipe_fops = {
425 open: pipe_write_open,
426 release: pipe_write_release,
429 struct file_operations rdwr_pipe_fops = {
435 open: pipe_rdwr_open,
436 release: pipe_rdwr_release,
439 struct inode* pipe_new(struct inode* inode)
443 page = __get_free_page(GFP_USER);
447 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
451 init_waitqueue_head(PIPE_WAIT(*inode));
452 PIPE_BASE(*inode) = (char*) page;
453 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
454 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
455 PIPE_WAITING_READERS(*inode) = PIPE_WAITING_WRITERS(*inode) = 0;
456 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
464 static struct vfsmount *pipe_mnt;
465 static int pipefs_delete_dentry(struct dentry *dentry)
469 static struct dentry_operations pipefs_dentry_operations = {
470 d_delete: pipefs_delete_dentry,
473 static struct inode * get_pipe_inode(void)
475 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
482 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
483 inode->i_fop = &rdwr_pipe_fops;
486 * Mark the inode dirty from the very beginning,
487 * that way it will never be moved to the dirty
488 * list because "mark_inode_dirty()" will think
489 * that it already _is_ on the dirty list.
491 inode->i_state = I_DIRTY;
492 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
493 inode->i_uid = current->fsuid;
494 inode->i_gid = current->fsgid;
495 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
496 inode->i_blksize = PAGE_SIZE;
509 struct dentry *dentry;
510 struct inode * inode;
511 struct file *f1, *f2;
516 f1 = get_empty_filp();
520 f2 = get_empty_filp();
524 inode = get_pipe_inode();
528 error = get_unused_fd();
530 goto close_f12_inode;
533 error = get_unused_fd();
535 goto close_f12_inode_i;
539 sprintf(name, "[%lu]", inode->i_ino);
541 this.len = strlen(name);
542 this.hash = inode->i_ino; /* will go */
543 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
545 goto close_f12_inode_i_j;
546 dentry->d_op = &pipefs_dentry_operations;
547 d_add(dentry, inode);
548 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
549 f1->f_dentry = f2->f_dentry = dget(dentry);
552 f1->f_pos = f2->f_pos = 0;
553 f1->f_flags = O_RDONLY;
554 f1->f_op = &read_pipe_fops;
559 f2->f_flags = O_WRONLY;
560 f2->f_op = &write_pipe_fops;
575 free_page((unsigned long) PIPE_BASE(*inode));
576 kfree(inode->i_pipe);
577 inode->i_pipe = NULL;
588 * pipefs should _never_ be mounted by userland - too much of security hassle,
589 * no real gain from having the whole whorehouse mounted. So we don't need
590 * any operations on the root directory. However, we need a non-trivial
591 * d_name - pipe: will go nicely and kill the special-casing in procfs.
593 static int pipefs_statfs(struct super_block *sb, struct statfs *buf)
595 buf->f_type = PIPEFS_MAGIC;
597 buf->f_namelen = 255;
601 static struct super_operations pipefs_ops = {
602 statfs: pipefs_statfs,
605 static struct super_block * pipefs_read_super(struct super_block *sb, void *data, int silent)
607 struct inode *root = new_inode(sb);
610 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
611 root->i_uid = root->i_gid = 0;
612 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
613 sb->s_blocksize = 1024;
614 sb->s_blocksize_bits = 10;
615 sb->s_magic = PIPEFS_MAGIC;
616 sb->s_op = &pipefs_ops;
617 sb->s_root = d_alloc(NULL, &(const struct qstr) { "pipe:", 5, 0 });
622 sb->s_root->d_sb = sb;
623 sb->s_root->d_parent = sb->s_root;
624 d_instantiate(sb->s_root, root);
628 static DECLARE_FSTYPE(pipe_fs_type, "pipefs", pipefs_read_super, FS_NOMOUNT);
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))
637 unregister_filesystem(&pipe_fs_type);
644 static void __exit exit_pipe_fs(void)
646 unregister_filesystem(&pipe_fs_type);
650 module_init(init_pipe_fs)
651 module_exit(exit_pipe_fs)