Blame view

fs/fifo.c 3.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   *  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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
  #include <linux/fs.h>
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
14
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/pipe_fs_i.h>
3a326a2ce   Ingo Molnar   [PATCH] introduce...
16
  static void wait_for_partner(struct inode* inode, unsigned int *cnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  {
  	int cur = *cnt;	
3a326a2ce   Ingo Molnar   [PATCH] introduce...
19
20
21
22
  
  	while (cur == *cnt) {
  		pipe_wait(inode->i_pipe);
  		if (signal_pending(current))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
28
  			break;
  	}
  }
  
  static void wake_up_partner(struct inode* inode)
  {
9aeedfc47   Ingo Molnar   [PATCH] get rid o...
29
  	wake_up_interruptible(&inode->i_pipe->wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
33
  }
  
  static int fifo_open(struct inode *inode, struct file *filp)
  {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
34
  	struct pipe_inode_info *pipe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
  	int ret;
9aeedfc47   Ingo Molnar   [PATCH] get rid o...
36
  	mutex_lock(&inode->i_mutex);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
37
38
  	pipe = inode->i_pipe;
  	if (!pipe) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  		ret = -ENOMEM;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
40
41
  		pipe = alloc_pipe_info(inode);
  		if (!pipe)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  			goto err_nocleanup;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
43
  		inode->i_pipe = pipe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
48
49
50
  	}
  	filp->f_version = 0;
  
  	/* We can only do regular read/write on fifos */
  	filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  
  	switch (filp->f_mode) {
aeb5d7270   Al Viro   [PATCH] introduce...
51
  	case FMODE_READ:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
  	/*
  	 *  O_RDONLY
  	 *  POSIX.1 says that O_NONBLOCK means return with the FIFO
  	 *  opened, even when there is no process writing the FIFO.
  	 */
d2d9648ec   Denys Vlasenko   [PATCH] reuse xxx...
57
  		filp->f_op = &read_pipefifo_fops;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
58
59
  		pipe->r_counter++;
  		if (pipe->readers++ == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  			wake_up_partner(inode);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
61
  		if (!pipe->writers) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
  			if ((filp->f_flags & O_NONBLOCK)) {
  				/* suppress POLLHUP until we have
  				 * seen a writer */
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
65
  				filp->f_version = pipe->w_counter;
ff38c083a   David Jenni   Filesystem: fifo:...
66
  			} else {
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
67
  				wait_for_partner(inode, &pipe->w_counter);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
  				if(signal_pending(current))
  					goto err_rd;
  			}
  		}
  		break;
  	
aeb5d7270   Al Viro   [PATCH] introduce...
74
  	case FMODE_WRITE:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
  	/*
  	 *  O_WRONLY
  	 *  POSIX.1 says that O_NONBLOCK means return -1 with
  	 *  errno=ENXIO when there is no process reading the FIFO.
  	 */
  		ret = -ENXIO;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
81
  		if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  			goto err;
d2d9648ec   Denys Vlasenko   [PATCH] reuse xxx...
83
  		filp->f_op = &write_pipefifo_fops;
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
84
85
  		pipe->w_counter++;
  		if (!pipe->writers++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  			wake_up_partner(inode);
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
87
88
  		if (!pipe->readers) {
  			wait_for_partner(inode, &pipe->r_counter);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
  			if (signal_pending(current))
  				goto err_wr;
  		}
  		break;
  	
aeb5d7270   Al Viro   [PATCH] introduce...
94
  	case FMODE_READ | FMODE_WRITE:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
  	/*
  	 *  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.
  	 */
d2d9648ec   Denys Vlasenko   [PATCH] reuse xxx...
101
  		filp->f_op = &rdwr_pipefifo_fops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102

923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
103
104
105
106
107
  		pipe->readers++;
  		pipe->writers++;
  		pipe->r_counter++;
  		pipe->w_counter++;
  		if (pipe->readers == 1 || pipe->writers == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
113
114
115
116
  			wake_up_partner(inode);
  		break;
  
  	default:
  		ret = -EINVAL;
  		goto err;
  	}
  
  	/* Ok! */
9aeedfc47   Ingo Molnar   [PATCH] get rid o...
117
  	mutex_unlock(&inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
  	return 0;
  
  err_rd:
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
121
122
  	if (!--pipe->readers)
  		wake_up_interruptible(&pipe->wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
  	ret = -ERESTARTSYS;
  	goto err;
  
  err_wr:
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
127
128
  	if (!--pipe->writers)
  		wake_up_interruptible(&pipe->wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
  	ret = -ERESTARTSYS;
  	goto err;
  
  err:
923f4f239   Ingo Molnar   [PATCH] pipe.c/fi...
133
  	if (!pipe->readers && !pipe->writers)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
  		free_pipe_info(inode);
  
  err_nocleanup:
9aeedfc47   Ingo Molnar   [PATCH] get rid o...
137
  	mutex_unlock(&inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
141
142
143
144
145
  	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...
   */
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
146
  const struct file_operations def_fifo_fops = {
d2d9648ec   Denys Vlasenko   [PATCH] reuse xxx...
147
  	.open		= fifo_open,	/* will set read_ or write_pipefifo_fops */
6038f373a   Arnd Bergmann   llseek: automatic...
148
  	.llseek		= noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  };