fifo.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  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/smp_lock.h>
#include <linux/fs.h>
#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 36 37
}

static int fifo_open(struct inode *inode, struct file *filp)
{
	int ret;

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

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

	switch (filp->f_mode) {
	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;
58 59
		inode->i_pipe->r_counter++;
		if (inode->i_pipe->readers++ == 0)
L
Linus Torvalds 已提交
60 61
			wake_up_partner(inode);

62
		if (!inode->i_pipe->writers) {
L
Linus Torvalds 已提交
63 64 65
			if ((filp->f_flags & O_NONBLOCK)) {
				/* suppress POLLHUP until we have
				 * seen a writer */
66
				filp->f_version = inode->i_pipe->w_counter;
L
Linus Torvalds 已提交
67 68
			} else 
			{
69
				wait_for_partner(inode, &inode->i_pipe->w_counter);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
				if(signal_pending(current))
					goto err_rd;
			}
		}
		break;
	
	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;
83
		if ((filp->f_flags & O_NONBLOCK) && !inode->i_pipe->readers)
L
Linus Torvalds 已提交
84 85 86
			goto err;

		filp->f_op = &write_fifo_fops;
87 88
		inode->i_pipe->w_counter++;
		if (!inode->i_pipe->writers++)
L
Linus Torvalds 已提交
89 90
			wake_up_partner(inode);

91 92
		if (!inode->i_pipe->readers) {
			wait_for_partner(inode, &inode->i_pipe->r_counter);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
			if (signal_pending(current))
				goto err_wr;
		}
		break;
	
	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;

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

	default:
		ret = -EINVAL;
		goto err;
	}

	/* Ok! */
121
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
122 123 124
	return 0;

err_rd:
125 126
	if (!--inode->i_pipe->readers)
		wake_up_interruptible(&inode->i_pipe->wait);
L
Linus Torvalds 已提交
127 128 129 130
	ret = -ERESTARTSYS;
	goto err;

err_wr:
131 132
	if (!--inode->i_pipe->writers)
		wake_up_interruptible(&inode->i_pipe->wait);
L
Linus Torvalds 已提交
133 134 135 136
	ret = -ERESTARTSYS;
	goto err;

err:
137
	if (!inode->i_pipe->readers && !inode->i_pipe->writers)
L
Linus Torvalds 已提交
138 139 140
		free_pipe_info(inode);

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