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