fifo.c 3.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  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/slab.h>
#include <linux/fs.h>
A
Alexey Dobriyan 已提交
15
#include <linux/sched.h>
L
Linus Torvalds 已提交
16 17
#include <linux/pipe_fs_i.h>

18
static void wait_for_partner(struct inode* inode, unsigned int *cnt)
L
Linus Torvalds 已提交
19 20
{
	int cur = *cnt;	
21 22 23 24

	while (cur == *cnt) {
		pipe_wait(inode->i_pipe);
		if (signal_pending(current))
L
Linus Torvalds 已提交
25 26 27 28 29 30
			break;
	}
}

static void wake_up_partner(struct inode* inode)
{
31
	wake_up_interruptible(&inode->i_pipe->wait);
L
Linus Torvalds 已提交
32 33 34 35
}

static int fifo_open(struct inode *inode, struct file *filp)
{
36
	struct pipe_inode_info *pipe;
L
Linus Torvalds 已提交
37 38
	int ret;

39
	mutex_lock(&inode->i_mutex);
40 41
	pipe = inode->i_pipe;
	if (!pipe) {
L
Linus Torvalds 已提交
42
		ret = -ENOMEM;
43 44
		pipe = alloc_pipe_info(inode);
		if (!pipe)
L
Linus Torvalds 已提交
45
			goto err_nocleanup;
46
		inode->i_pipe = pipe;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53
	}
	filp->f_version = 0;

	/* We can only do regular read/write on fifos */
	filp->f_mode &= (FMODE_READ | FMODE_WRITE);

	switch (filp->f_mode) {
54
	case FMODE_READ:
L
Linus Torvalds 已提交
55 56 57 58 59
	/*
	 *  O_RDONLY
	 *  POSIX.1 says that O_NONBLOCK means return with the FIFO
	 *  opened, even when there is no process writing the FIFO.
	 */
60
		filp->f_op = &read_pipefifo_fops;
61 62
		pipe->r_counter++;
		if (pipe->readers++ == 0)
L
Linus Torvalds 已提交
63 64
			wake_up_partner(inode);

65
		if (!pipe->writers) {
L
Linus Torvalds 已提交
66 67 68
			if ((filp->f_flags & O_NONBLOCK)) {
				/* suppress POLLHUP until we have
				 * seen a writer */
69
				filp->f_version = pipe->w_counter;
L
Linus Torvalds 已提交
70 71
			} else 
			{
72
				wait_for_partner(inode, &pipe->w_counter);
L
Linus Torvalds 已提交
73 74 75 76 77 78
				if(signal_pending(current))
					goto err_rd;
			}
		}
		break;
	
79
	case FMODE_WRITE:
L
Linus Torvalds 已提交
80 81 82 83 84 85
	/*
	 *  O_WRONLY
	 *  POSIX.1 says that O_NONBLOCK means return -1 with
	 *  errno=ENXIO when there is no process reading the FIFO.
	 */
		ret = -ENXIO;
86
		if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
L
Linus Torvalds 已提交
87 88
			goto err;

89
		filp->f_op = &write_pipefifo_fops;
90 91
		pipe->w_counter++;
		if (!pipe->writers++)
L
Linus Torvalds 已提交
92 93
			wake_up_partner(inode);

94 95
		if (!pipe->readers) {
			wait_for_partner(inode, &pipe->r_counter);
L
Linus Torvalds 已提交
96 97 98 99 100
			if (signal_pending(current))
				goto err_wr;
		}
		break;
	
101
	case FMODE_READ | FMODE_WRITE:
L
Linus Torvalds 已提交
102 103 104 105 106 107
	/*
	 *  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.
	 */
108
		filp->f_op = &rdwr_pipefifo_fops;
L
Linus Torvalds 已提交
109

110 111 112 113 114
		pipe->readers++;
		pipe->writers++;
		pipe->r_counter++;
		pipe->w_counter++;
		if (pipe->readers == 1 || pipe->writers == 1)
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123
			wake_up_partner(inode);
		break;

	default:
		ret = -EINVAL;
		goto err;
	}

	/* Ok! */
124
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
125 126 127
	return 0;

err_rd:
128 129
	if (!--pipe->readers)
		wake_up_interruptible(&pipe->wait);
L
Linus Torvalds 已提交
130 131 132 133
	ret = -ERESTARTSYS;
	goto err;

err_wr:
134 135
	if (!--pipe->writers)
		wake_up_interruptible(&pipe->wait);
L
Linus Torvalds 已提交
136 137 138 139
	ret = -ERESTARTSYS;
	goto err;

err:
140
	if (!pipe->readers && !pipe->writers)
L
Linus Torvalds 已提交
141 142 143
		free_pipe_info(inode);

err_nocleanup:
144
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
	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...
 */
153
const struct file_operations def_fifo_fops = {
154
	.open		= fifo_open,	/* will set read_ or write_pipefifo_fops */
L
Linus Torvalds 已提交
155
};