/*
* linux/fs/fifo.c
*
* written by Paul H. Hargrove
*
* Fixes:
* 10-06-1999, AV: fixed OOM handling in fifo_open(), moved
* initialization there, switched to external
* allocation of pipe_inode_info.
*/
#include <linux/mm.h>
#include <linux/malloc.h>
#include <linux/smp_lock.h>
16 static void wait_for_partner(struct inode* inode, unsigned int* cnt)
{
int cur = *cnt;
19 while(cur == *cnt) {
pipe_wait(inode);
21 if(signal_pending(current))
22 break;
}
}
26 static void wake_up_partner(struct inode* inode)
{
wake_up_interruptible(PIPE_WAIT(*inode));
}
31 static int fifo_open(struct inode *inode, struct file *filp)
{
int ret;
ret = -ERESTARTSYS;
36 lock_kernel();
37 if (down_interruptible(PIPE_SEM(*inode)))
38 goto err_nolock_nocleanup;
40 if (!inode->i_pipe) {
ret = -ENOMEM;
42 if(!pipe_new(inode))
43 goto err_nocleanup;
}
filp->f_version = 0;
47 switch (filp->f_mode) {
48 case 1:
/*
* O_RDONLY
* POSIX.1 says that O_NONBLOCK means return with the FIFO
* opened, even when there is no process writing the FIFO.
*/
filp->f_op = &read_fifo_fops;
PIPE_RCOUNTER(*inode)++;
56 if (PIPE_READERS(*inode)++ == 0)
wake_up_partner(inode);
59 if (!PIPE_WRITERS(*inode)) {
60 if ((filp->f_flags & O_NONBLOCK)) {
/* suppress POLLHUP until we have
* seen a writer */
filp->f_version = PIPE_WCOUNTER(*inode);
64 } else
{
wait_for_partner(inode, &PIPE_WCOUNTER(*inode));
67 if(signal_pending(current))
68 goto err_rd;
}
}
71 break;
73 case 2:
/*
* O_WRONLY
* POSIX.1 says that O_NONBLOCK means return -1 with
* errno=ENXIO when there is no process reading the FIFO.
*/
ret = -ENXIO;
80 if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode))
81 goto err;
filp->f_op = &write_fifo_fops;
PIPE_WCOUNTER(*inode)++;
85 if (!PIPE_WRITERS(*inode)++)
wake_up_partner(inode);
88 if (!PIPE_READERS(*inode)) {
wait_for_partner(inode, &PIPE_RCOUNTER(*inode));
90 if (signal_pending(current))
91 goto err_wr;
}
93 break;
95 case 3:
/*
* O_RDWR
* POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
* This implementation will NEVER block on a O_RDWR open, since
* the process can at least talk to itself.
*/
filp->f_op = &rdwr_fifo_fops;
PIPE_READERS(*inode)++;
PIPE_WRITERS(*inode)++;
PIPE_RCOUNTER(*inode)++;
PIPE_WCOUNTER(*inode)++;
108 if (PIPE_READERS(*inode) == 1 || PIPE_WRITERS(*inode) == 1)
wake_up_partner(inode);
110 break;
112 default:
ret = -EINVAL;
114 goto err;
}
/* Ok! */
up(PIPE_SEM(*inode));
119 unlock_kernel();
120 return 0;
err_rd:
123 if (!--PIPE_READERS(*inode))
wake_up_interruptible(PIPE_WAIT(*inode));
ret = -ERESTARTSYS;
126 goto err;
err_wr:
129 if (!--PIPE_WRITERS(*inode))
wake_up_interruptible(PIPE_WAIT(*inode));
ret = -ERESTARTSYS;
132 goto err;
err:
135 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
struct pipe_inode_info *info = inode->i_pipe;
inode->i_pipe = NULL;
free_page((unsigned long)info->base);
kfree(info);
}
err_nocleanup:
up(PIPE_SEM(*inode));
err_nolock_nocleanup:
146 unlock_kernel();
147 return ret;
}
/*
* Dummy default file-operations: the only thing this does
* is contain the open that then fills in the correct operations
* depending on the access mode of the file...
*/
struct file_operations def_fifo_fops = {
open: fifo_open, /* will set read or write pipe_fops */
};