| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/fs/fifo.c | 
|  | 3 | * | 
|  | 4 | *  written by Paul H. Hargrove | 
|  | 5 | * | 
|  | 6 | *  Fixes: | 
|  | 7 | *	10-06-1999, AV: fixed OOM handling in fifo_open(), moved | 
|  | 8 | *			initialization there, switched to external | 
|  | 9 | *			allocation of pipe_inode_info. | 
|  | 10 | */ | 
|  | 11 |  | 
|  | 12 | #include <linux/mm.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/fs.h> | 
| Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 14 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/pipe_fs_i.h> | 
|  | 16 |  | 
| Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 17 | static void wait_for_partner(struct inode* inode, unsigned int *cnt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { | 
|  | 19 | int cur = *cnt; | 
| Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 20 |  | 
|  | 21 | while (cur == *cnt) { | 
|  | 22 | pipe_wait(inode->i_pipe); | 
|  | 23 | if (signal_pending(current)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | break; | 
|  | 25 | } | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | static void wake_up_partner(struct inode* inode) | 
|  | 29 | { | 
| Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 30 | wake_up_interruptible(&inode->i_pipe->wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } | 
|  | 32 |  | 
|  | 33 | static int fifo_open(struct inode *inode, struct file *filp) | 
|  | 34 | { | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 35 | struct pipe_inode_info *pipe; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int ret; | 
|  | 37 |  | 
| Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 38 | mutex_lock(&inode->i_mutex); | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 39 | pipe = inode->i_pipe; | 
|  | 40 | if (!pipe) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | ret = -ENOMEM; | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 42 | pipe = alloc_pipe_info(inode); | 
|  | 43 | if (!pipe) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | goto err_nocleanup; | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 45 | inode->i_pipe = pipe; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } | 
|  | 47 | filp->f_version = 0; | 
|  | 48 |  | 
|  | 49 | /* We can only do regular read/write on fifos */ | 
|  | 50 | filp->f_mode &= (FMODE_READ | FMODE_WRITE); | 
|  | 51 |  | 
|  | 52 | switch (filp->f_mode) { | 
| Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 53 | case FMODE_READ: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* | 
|  | 55 | *  O_RDONLY | 
|  | 56 | *  POSIX.1 says that O_NONBLOCK means return with the FIFO | 
|  | 57 | *  opened, even when there is no process writing the FIFO. | 
|  | 58 | */ | 
| Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 59 | filp->f_op = &read_pipefifo_fops; | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 60 | pipe->r_counter++; | 
|  | 61 | if (pipe->readers++ == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | wake_up_partner(inode); | 
|  | 63 |  | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 64 | if (!pipe->writers) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if ((filp->f_flags & O_NONBLOCK)) { | 
|  | 66 | /* suppress POLLHUP until we have | 
|  | 67 | * seen a writer */ | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 68 | filp->f_version = pipe->w_counter; | 
| David Jenni | ff38c08 | 2011-02-23 16:51:05 +0100 | [diff] [blame] | 69 | } else { | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 70 | wait_for_partner(inode, &pipe->w_counter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if(signal_pending(current)) | 
|  | 72 | goto err_rd; | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 | break; | 
|  | 76 |  | 
| Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 77 | case FMODE_WRITE: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* | 
|  | 79 | *  O_WRONLY | 
|  | 80 | *  POSIX.1 says that O_NONBLOCK means return -1 with | 
|  | 81 | *  errno=ENXIO when there is no process reading the FIFO. | 
|  | 82 | */ | 
|  | 83 | ret = -ENXIO; | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 84 | if ((filp->f_flags & O_NONBLOCK) && !pipe->readers) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | goto err; | 
|  | 86 |  | 
| Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 87 | filp->f_op = &write_pipefifo_fops; | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 88 | pipe->w_counter++; | 
|  | 89 | if (!pipe->writers++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | wake_up_partner(inode); | 
|  | 91 |  | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 92 | if (!pipe->readers) { | 
|  | 93 | wait_for_partner(inode, &pipe->r_counter); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (signal_pending(current)) | 
|  | 95 | goto err_wr; | 
|  | 96 | } | 
|  | 97 | break; | 
|  | 98 |  | 
| Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 99 | case FMODE_READ | FMODE_WRITE: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* | 
|  | 101 | *  O_RDWR | 
|  | 102 | *  POSIX.1 leaves this case "undefined" when O_NONBLOCK is set. | 
|  | 103 | *  This implementation will NEVER block on a O_RDWR open, since | 
|  | 104 | *  the process can at least talk to itself. | 
|  | 105 | */ | 
| Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 106 | filp->f_op = &rdwr_pipefifo_fops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 |  | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 108 | pipe->readers++; | 
|  | 109 | pipe->writers++; | 
|  | 110 | pipe->r_counter++; | 
|  | 111 | pipe->w_counter++; | 
|  | 112 | if (pipe->readers == 1 || pipe->writers == 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | wake_up_partner(inode); | 
|  | 114 | break; | 
|  | 115 |  | 
|  | 116 | default: | 
|  | 117 | ret = -EINVAL; | 
|  | 118 | goto err; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /* Ok! */ | 
| Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 122 | mutex_unlock(&inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | return 0; | 
|  | 124 |  | 
|  | 125 | err_rd: | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 126 | if (!--pipe->readers) | 
|  | 127 | wake_up_interruptible(&pipe->wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | ret = -ERESTARTSYS; | 
|  | 129 | goto err; | 
|  | 130 |  | 
|  | 131 | err_wr: | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 132 | if (!--pipe->writers) | 
|  | 133 | wake_up_interruptible(&pipe->wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | ret = -ERESTARTSYS; | 
|  | 135 | goto err; | 
|  | 136 |  | 
|  | 137 | err: | 
| Ingo Molnar | 923f4f2 | 2006-04-11 13:53:33 +0200 | [diff] [blame] | 138 | if (!pipe->readers && !pipe->writers) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | free_pipe_info(inode); | 
|  | 140 |  | 
|  | 141 | err_nocleanup: | 
| Ingo Molnar | 9aeedfc | 2006-04-11 13:53:10 +0200 | [diff] [blame] | 142 | mutex_unlock(&inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return ret; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | /* | 
|  | 147 | * Dummy default file-operations: the only thing this does | 
|  | 148 | * is contain the open that then fills in the correct operations | 
|  | 149 | * depending on the access mode of the file... | 
|  | 150 | */ | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 151 | const struct file_operations def_fifo_fops = { | 
| Denys Vlasenko | d2d9648 | 2008-07-01 14:16:09 +0200 | [diff] [blame] | 152 | .open		= fifo_open,	/* will set read_ or write_pipefifo_fops */ | 
| Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 153 | .llseek		= noop_llseek, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |