xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1995
5  *	The Regents of the University of California.
6  * Copyright (c) 2005 Robert N. M. Watson
7  * Copyright (c) 2012 Giovanni Trematerra
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
35  */
36 
37 #include <sys/param.h>
38 #include <sys/event.h>
39 #include <sys/file.h>
40 #include <sys/filedesc.h>
41 #include <sys/filio.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/selinfo.h>
48 #include <sys/pipe.h>
49 #include <sys/proc.h>
50 #include <sys/signalvar.h>
51 #include <sys/sx.h>
52 #include <sys/systm.h>
53 #include <sys/un.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
56 
57 /*
58  * This structure is associated with the FIFO vnode and stores
59  * the state associated with the FIFO.
60  * Notes about locking:
61  *   - fi_pipe is invariant since init time.
62  *   - fi_readers and fi_writers are protected by the vnode lock.
63  */
64 struct fifoinfo {
65 	struct pipe *fi_pipe;
66 	long	fi_readers;
67 	long	fi_writers;
68 	u_int	fi_rgen;
69 	u_int	fi_wgen;
70 };
71 
72 static vop_print_t	fifo_print;
73 static vop_open_t	fifo_open;
74 static vop_close_t	fifo_close;
75 static vop_advlock_t	fifo_advlock;
76 
77 struct vop_vector fifo_specops = {
78 	.vop_default =		&default_vnodeops,
79 
80 	.vop_advlock =		fifo_advlock,
81 	.vop_close =		fifo_close,
82 	.vop_create =		VOP_PANIC,
83 	.vop_getattr =		VOP_EBADF,
84 	.vop_ioctl =		VOP_PANIC,
85 	.vop_link =		VOP_PANIC,
86 	.vop_mkdir =		VOP_PANIC,
87 	.vop_mknod =		VOP_PANIC,
88 	.vop_open =		fifo_open,
89 	.vop_pathconf =		VOP_PANIC,
90 	.vop_print =		fifo_print,
91 	.vop_read =		VOP_PANIC,
92 	.vop_readdir =		VOP_PANIC,
93 	.vop_readlink =		VOP_PANIC,
94 	.vop_reallocblks =	VOP_PANIC,
95 	.vop_reclaim =		VOP_NULL,
96 	.vop_remove =		VOP_PANIC,
97 	.vop_rename =		VOP_PANIC,
98 	.vop_rmdir =		VOP_PANIC,
99 	.vop_setattr =		VOP_EBADF,
100 	.vop_symlink =		VOP_PANIC,
101 	.vop_write =		VOP_PANIC,
102 };
103 VFS_VOP_VECTOR_REGISTER(fifo_specops);
104 
105 /*
106  * Dispose of fifo resources.
107  */
108 static void
109 fifo_cleanup(struct vnode *vp)
110 {
111 	struct fifoinfo *fip;
112 
113 	ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
114 	fip = vp->v_fifoinfo;
115 	if (fip->fi_readers == 0 && fip->fi_writers == 0) {
116 		vp->v_fifoinfo = NULL;
117 		pipe_dtor(fip->fi_pipe);
118 		free(fip, M_VNODE);
119 	}
120 }
121 
122 /*
123  * Open called to set up a new instance of a fifo or
124  * to find an active instance of a fifo.
125  */
126 /* ARGSUSED */
127 static int
128 fifo_open(struct vop_open_args *ap)
129 {
130 	struct vnode *vp;
131 	struct file *fp;
132 	struct thread *td;
133 	struct fifoinfo *fip;
134 	struct pipe *fpipe;
135 	u_int gen;
136 	int error, stops_deferred;
137 
138 	vp = ap->a_vp;
139 	fp = ap->a_fp;
140 	td = ap->a_td;
141 	ASSERT_VOP_ELOCKED(vp, "fifo_open");
142 	if (fp == NULL || (ap->a_mode & FEXEC) != 0)
143 		return (EINVAL);
144 	if ((fip = vp->v_fifoinfo) == NULL) {
145 		error = pipe_named_ctor(&fpipe, td);
146 		if (error != 0)
147 			return (error);
148 		fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK | M_ZERO);
149 		fip->fi_pipe = fpipe;
150 		fpipe->pipe_wgen = 0;
151  		KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
152 		vp->v_fifoinfo = fip;
153 	}
154 	fpipe = fip->fi_pipe;
155  	KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
156 
157 	/*
158 	 * Use the pipe mutex here, in addition to the vnode lock,
159 	 * in order to allow vnode lock dropping before msleep() calls
160 	 * and still avoiding missed wakeups.
161 	 */
162 	PIPE_LOCK(fpipe);
163 	if (ap->a_mode & FREAD) {
164 		fip->fi_readers++;
165 		fip->fi_rgen++;
166 		if (fip->fi_readers == 1) {
167 			fpipe->pipe_state &= ~PIPE_EOF;
168 			if (fip->fi_writers > 0) {
169 				wakeup(&fip->fi_writers);
170 				pipeselwakeup(fpipe);
171 			}
172 		}
173 		fp->f_pipegen = fpipe->pipe_wgen - fip->fi_writers;
174 	}
175 	if (ap->a_mode & FWRITE) {
176 		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
177 			PIPE_UNLOCK(fpipe);
178 			if (fip->fi_writers == 0)
179 				fifo_cleanup(vp);
180 			return (ENXIO);
181 		}
182 		fip->fi_writers++;
183 		fip->fi_wgen++;
184 		if (fip->fi_writers == 1) {
185 			fpipe->pipe_state &= ~PIPE_EOF;
186 			if (fip->fi_readers > 0) {
187 				wakeup(&fip->fi_readers);
188 				pipeselwakeup(fpipe);
189 			}
190 		}
191 	}
192 	if ((ap->a_mode & O_NONBLOCK) == 0) {
193 		if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
194 			gen = fip->fi_wgen;
195 			VOP_UNLOCK(vp);
196 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
197 			error = msleep(&fip->fi_readers, PIPE_MTX(fpipe),
198 			    PDROP | PCATCH | PSOCK, "fifoor", 0);
199 			sigallowstop(stops_deferred);
200 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
201 			if (error != 0 && gen == fip->fi_wgen) {
202 				fip->fi_readers--;
203 				if (fip->fi_readers == 0) {
204 					PIPE_LOCK(fpipe);
205 					fpipe->pipe_state |= PIPE_EOF;
206 					if (fpipe->pipe_state & PIPE_WANTW)
207 						wakeup(fpipe);
208 					pipeselwakeup(fpipe);
209 					PIPE_UNLOCK(fpipe);
210 					fifo_cleanup(vp);
211 				}
212 				return (error);
213 			}
214 			PIPE_LOCK(fpipe);
215 			/*
216 			 * We must have got woken up because we had a writer.
217 			 * That (and not still having one) is the condition
218 			 * that we must wait for.
219 			 */
220 		}
221 		if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
222 			gen = fip->fi_rgen;
223 			VOP_UNLOCK(vp);
224 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
225 			error = msleep(&fip->fi_writers, PIPE_MTX(fpipe),
226 			    PDROP | PCATCH | PSOCK, "fifoow", 0);
227 			sigallowstop(stops_deferred);
228 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 			if (error != 0 && gen == fip->fi_rgen) {
230 				fip->fi_writers--;
231 				if (fip->fi_writers == 0) {
232 					PIPE_LOCK(fpipe);
233 					fpipe->pipe_state |= PIPE_EOF;
234 					if (fpipe->pipe_state & PIPE_WANTR)
235 						wakeup(fpipe);
236 					fpipe->pipe_wgen++;
237 					pipeselwakeup(fpipe);
238 					PIPE_UNLOCK(fpipe);
239 					fifo_cleanup(vp);
240 				}
241 				return (error);
242 			}
243 			/*
244 			 * We must have got woken up because we had
245 			 * a reader.  That (and not still having one)
246 			 * is the condition that we must wait for.
247 			 */
248 			PIPE_LOCK(fpipe);
249 		}
250 	}
251 	PIPE_UNLOCK(fpipe);
252 	KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
253 	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
254 	finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
255 	return (0);
256 }
257 
258 /*
259  * Device close routine
260  */
261 /* ARGSUSED */
262 static int
263 fifo_close(struct vop_close_args *ap)
264 {
265 	struct vnode *vp;
266 	struct fifoinfo *fip;
267 	struct pipe *cpipe;
268 
269 	vp = ap->a_vp;
270 	ASSERT_VOP_ELOCKED(vp, "fifo_close");
271 	fip = vp->v_fifoinfo;
272 
273 	/*
274 	 * During open, it is possible that the fifo vnode is relocked
275 	 * after the vnode is instantiated but before VOP_OPEN() is
276 	 * done.  For instance, vn_open_vnode() might need to upgrade
277 	 * vnode lock, or ffs_vput_pair() needs to unlock vp to sync
278 	 * dvp.  In this case, reclaim can observe us with v_fifoinfo
279 	 * equal to NULL.
280 	 */
281 	if (fip == NULL)
282 		return (0);
283 
284 	cpipe = fip->fi_pipe;
285 	if (ap->a_fflag & FREAD) {
286 		fip->fi_readers--;
287 		if (fip->fi_readers == 0) {
288 			PIPE_LOCK(cpipe);
289 			cpipe->pipe_state |= PIPE_EOF;
290 			if ((cpipe->pipe_state & PIPE_WANTW)) {
291 				cpipe->pipe_state &= ~PIPE_WANTW;
292 				wakeup(cpipe);
293 			}
294 			pipeselwakeup(cpipe);
295 			PIPE_UNLOCK(cpipe);
296 		}
297 	}
298 	if (ap->a_fflag & FWRITE) {
299 		fip->fi_writers--;
300 		if (fip->fi_writers == 0) {
301 			PIPE_LOCK(cpipe);
302 			cpipe->pipe_state |= PIPE_EOF;
303 			if ((cpipe->pipe_state & PIPE_WANTR)) {
304 				cpipe->pipe_state &= ~PIPE_WANTR;
305 				wakeup(cpipe);
306 			}
307 			cpipe->pipe_wgen++;
308 			pipeselwakeup(cpipe);
309 			PIPE_UNLOCK(cpipe);
310 		}
311 	}
312 	fifo_cleanup(vp);
313 	return (0);
314 }
315 
316 /*
317  * Print out internal contents of a fifo vnode.
318  */
319 int
320 fifo_printinfo(struct vnode *vp)
321 {
322 	struct fifoinfo *fip = vp->v_fifoinfo;
323 
324 	if (fip == NULL){
325 		printf(", NULL v_fifoinfo");
326 		return (0);
327 	}
328 	printf(", fifo with %ld readers and %ld writers",
329 		fip->fi_readers, fip->fi_writers);
330 	return (0);
331 }
332 
333 /*
334  * Print out the contents of a fifo vnode.
335  */
336 static int
337 fifo_print(struct vop_print_args *ap)
338 {
339 	printf("    ");
340 	fifo_printinfo(ap->a_vp);
341 	printf("\n");
342 	return (0);
343 }
344 
345 /*
346  * Fifo advisory byte-level locks.
347  */
348 /* ARGSUSED */
349 static int
350 fifo_advlock(struct vop_advlock_args *ap)
351 {
352 
353 	if ((ap->a_flags & F_FLOCK) == 0)
354 		return (EINVAL);
355 	return (vop_stdadvlock(ap));
356 }
357