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