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 update_mctime(inode);
236 up(PIPE_SEM(*inode));
245 up(PIPE_SEM(*inode));
246 send_sig(SIGPIPE, current, 0);
251 bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
257 bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
263 pipe_ioctl(struct inode *pino, struct file *filp,
264 unsigned int cmd, unsigned long arg)
268 return put_user(PIPE_LEN(*pino), (int *)arg);
274 /* No kernel lock held - fine */
276 pipe_poll(struct file *filp, poll_table *wait)
279 struct inode *inode = filp->f_dentry->d_inode;
281 poll_wait(filp, PIPE_WAIT(*inode), wait);
283 /* Reading only -- no need for acquiring the semaphore. */
284 mask = POLLIN | POLLRDNORM;
285 if (PIPE_EMPTY(*inode))
286 mask = POLLOUT | POLLWRNORM;
287 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
289 if (!PIPE_READERS(*inode))
295 /* FIXME: most Unices do not set POLLERR for fifos */
296 #define fifo_poll pipe_poll
299 pipe_release(struct inode *inode, int decr, int decw)
301 down(PIPE_SEM(*inode));
302 PIPE_READERS(*inode) -= decr;
303 PIPE_WRITERS(*inode) -= decw;
304 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
305 struct pipe_inode_info *info = inode->i_pipe;
306 inode->i_pipe = NULL;
307 free_page((unsigned long) info->base);
310 wake_up_interruptible(PIPE_WAIT(*inode));
312 up(PIPE_SEM(*inode));
318 pipe_read_release(struct inode *inode, struct file *filp)
320 return pipe_release(inode, 1, 0);
324 pipe_write_release(struct inode *inode, struct file *filp)
326 return pipe_release(inode, 0, 1);
330 pipe_rdwr_release(struct inode *inode, struct file *filp)
334 decr = (filp->f_mode & FMODE_READ) != 0;
335 decw = (filp->f_mode & FMODE_WRITE) != 0;
336 return pipe_release(inode, decr, decw);
340 pipe_read_open(struct inode *inode, struct file *filp)
342 /* We could have perhaps used atomic_t, but this and friends
343 below are the only places. So it doesn't seem worthwhile. */
344 down(PIPE_SEM(*inode));
345 PIPE_READERS(*inode)++;
346 up(PIPE_SEM(*inode));
352 pipe_write_open(struct inode *inode, struct file *filp)
354 down(PIPE_SEM(*inode));
355 PIPE_WRITERS(*inode)++;
356 up(PIPE_SEM(*inode));
362 pipe_rdwr_open(struct inode *inode, struct file *filp)
364 down(PIPE_SEM(*inode));
365 if (filp->f_mode & FMODE_READ)
366 PIPE_READERS(*inode)++;
367 if (filp->f_mode & FMODE_WRITE)
368 PIPE_WRITERS(*inode)++;
369 up(PIPE_SEM(*inode));
375 * The file_operations structs are not static because they
376 * are also used in linux/fs/fifo.c to do operations on FIFOs.
378 struct file_operations read_fifo_fops = {
384 open: pipe_read_open,
385 release: pipe_read_release,
388 struct file_operations write_fifo_fops = {
394 open: pipe_write_open,
395 release: pipe_write_release,
398 struct file_operations rdwr_fifo_fops = {
404 open: pipe_rdwr_open,
405 release: pipe_rdwr_release,
408 struct file_operations read_pipe_fops = {
414 open: pipe_read_open,
415 release: pipe_read_release,
418 struct file_operations write_pipe_fops = {
424 open: pipe_write_open,
425 release: pipe_write_release,
428 struct file_operations rdwr_pipe_fops = {
434 open: pipe_rdwr_open,
435 release: pipe_rdwr_release,
438 struct inode* pipe_new(struct inode* inode)
442 page = __get_free_page(GFP_USER);
446 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
450 init_waitqueue_head(PIPE_WAIT(*inode));
451 PIPE_BASE(*inode) = (char*) page;
452 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
453 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
454 PIPE_WAITING_READERS(*inode) = PIPE_WAITING_WRITERS(*inode) = 0;
455 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
463 static struct vfsmount *pipe_mnt;
464 static int pipefs_delete_dentry(struct dentry *dentry)
468 static struct dentry_operations pipefs_dentry_operations = {
469 d_delete: pipefs_delete_dentry,
472 static struct inode * get_pipe_inode(void)
474 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
481 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
482 inode->i_fop = &rdwr_pipe_fops;
485 * Mark the inode dirty from the very beginning,
486 * that way it will never be moved to the dirty
487 * list because "mark_inode_dirty()" will think
488 * that it already _is_ on the dirty list.
490 inode->i_state = I_DIRTY;
491 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
492 inode->i_uid = current->fsuid;
493 inode->i_gid = current->fsgid;
494 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
495 inode->i_blksize = PAGE_SIZE;
508 struct dentry *dentry;
509 struct inode * inode;
510 struct file *f1, *f2;
515 f1 = get_empty_filp();
519 f2 = get_empty_filp();
523 inode = get_pipe_inode();
527 error = get_unused_fd();
529 goto close_f12_inode;
532 error = get_unused_fd();
534 goto close_f12_inode_i;
538 sprintf(name, "[%lu]", inode->i_ino);
540 this.len = strlen(name);
541 this.hash = inode->i_ino; /* will go */
542 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
544 goto close_f12_inode_i_j;
545 dentry->d_op = &pipefs_dentry_operations;
546 d_add(dentry, inode);
547 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
548 f1->f_dentry = f2->f_dentry = dget(dentry);
551 f1->f_pos = f2->f_pos = 0;
552 f1->f_flags = O_RDONLY;
553 f1->f_op = &read_pipe_fops;
558 f2->f_flags = O_WRONLY;
559 f2->f_op = &write_pipe_fops;
574 free_page((unsigned long) PIPE_BASE(*inode));
575 kfree(inode->i_pipe);
576 inode->i_pipe = NULL;
587 * pipefs should _never_ be mounted by userland - too much of security hassle,
588 * no real gain from having the whole whorehouse mounted. So we don't need
589 * any operations on the root directory. However, we need a non-trivial
590 * d_name - pipe: will go nicely and kill the special-casing in procfs.
592 static int pipefs_statfs(struct super_block *sb, struct statfs *buf)
594 buf->f_type = PIPEFS_MAGIC;
596 buf->f_namelen = 255;
600 static struct super_operations pipefs_ops = {
601 statfs: pipefs_statfs,
604 static struct super_block * pipefs_read_super(struct super_block *sb, void *data, int silent)
606 struct inode *root = new_inode(sb);
609 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
610 root->i_uid = root->i_gid = 0;
611 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
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, FS_NOMOUNT);
629 static int __init init_pipe_fs(void)
631 int err = register_filesystem(&pipe_fs_type);
633 pipe_mnt = kern_mount(&pipe_fs_type);
634 err = PTR_ERR(pipe_mnt);
635 if (IS_ERR(pipe_mnt))
636 unregister_filesystem(&pipe_fs_type);
643 static void __exit exit_pipe_fs(void)
645 unregister_filesystem(&pipe_fs_type);
649 module_init(init_pipe_fs)
650 module_exit(exit_pipe_fs)