xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 #include <sys/event.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/filio.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/selinfo.h>
49 #include <sys/pipe.h>
50 #include <sys/proc.h>
51 #include <sys/signalvar.h>
52 #include <sys/sx.h>
53 #include <sys/systm.h>
54 #include <sys/un.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
57 
58 /*
59  * This structure is associated with the FIFO vnode and stores
60  * the state associated with the FIFO.
61  * Notes about locking:
62  *   - fi_pipe is invariant since init time.
63  *   - fi_readers and fi_writers are protected by the vnode lock.
64  */
65 struct fifoinfo {
66 	struct pipe *fi_pipe;
67 	long	fi_readers;
68 	long	fi_writers;
69 	u_int	fi_rgen;
70 	u_int	fi_wgen;
71 };
72 
73 static vop_print_t	fifo_print;
74 static vop_open_t	fifo_open;
75 static vop_close_t	fifo_close;
76 static vop_advlock_t	fifo_advlock;
77 
78 struct vop_vector fifo_specops = {
79 	.vop_default =		&default_vnodeops,
80 
81 	.vop_advlock =		fifo_advlock,
82 	.vop_close =		fifo_close,
83 	.vop_create =		VOP_PANIC,
84 	.vop_getattr =		VOP_EBADF,
85 	.vop_ioctl =		VOP_PANIC,
86 	.vop_link =		VOP_PANIC,
87 	.vop_mkdir =		VOP_PANIC,
88 	.vop_mknod =		VOP_PANIC,
89 	.vop_open =		fifo_open,
90 	.vop_pathconf =		VOP_PANIC,
91 	.vop_print =		fifo_print,
92 	.vop_read =		VOP_PANIC,
93 	.vop_readdir =		VOP_PANIC,
94 	.vop_readlink =		VOP_PANIC,
95 	.vop_reallocblks =	VOP_PANIC,
96 	.vop_reclaim =		VOP_NULL,
97 	.vop_remove =		VOP_PANIC,
98 	.vop_rename =		VOP_PANIC,
99 	.vop_rmdir =		VOP_PANIC,
100 	.vop_setattr =		VOP_EBADF,
101 	.vop_symlink =		VOP_PANIC,
102 	.vop_write =		VOP_PANIC,
103 };
104 VFS_VOP_VECTOR_REGISTER(fifo_specops);
105 
106 /*
107  * Dispose of fifo resources.
108  */
109 static void
110 fifo_cleanup(struct vnode *vp)
111 {
112 	struct fifoinfo *fip;
113 
114 	ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
115 	fip = vp->v_fifoinfo;
116 	if (fip->fi_readers == 0 && fip->fi_writers == 0) {
117 		vp->v_fifoinfo = NULL;
118 		pipe_dtor(fip->fi_pipe);
119 		free(fip, M_VNODE);
120 	}
121 }
122 
123 /*
124  * Open called to set up a new instance of a fifo or
125  * to find an active instance of a fifo.
126  */
127 /* ARGSUSED */
128 static int
129 fifo_open(struct vop_open_args *ap)
130 {
131 	struct vnode *vp;
132 	struct file *fp;
133 	struct thread *td;
134 	struct fifoinfo *fip;
135 	struct pipe *fpipe;
136 	u_int gen;
137 	int error, stops_deferred;
138 
139 	vp = ap->a_vp;
140 	fp = ap->a_fp;
141 	td = ap->a_td;
142 	ASSERT_VOP_ELOCKED(vp, "fifo_open");
143 	if (fp == NULL || (ap->a_mode & FEXEC) != 0)
144 		return (EINVAL);
145 	if ((fip = vp->v_fifoinfo) == NULL) {
146 		error = pipe_named_ctor(&fpipe, td);
147 		if (error != 0)
148 			return (error);
149 		fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK | M_ZERO);
150 		fip->fi_pipe = fpipe;
151 		fpipe->pipe_wgen = 0;
152  		KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
153 		vp->v_fifoinfo = fip;
154 	}
155 	fpipe = fip->fi_pipe;
156  	KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
157 
158 	/*
159 	 * Use the pipe mutex here, in addition to the vnode lock,
160 	 * in order to allow vnode lock dropping before msleep() calls
161 	 * and still avoiding missed wakeups.
162 	 */
163 	PIPE_LOCK(fpipe);
164 	if (ap->a_mode & FREAD) {
165 		fip->fi_readers++;
166 		fip->fi_rgen++;
167 		if (fip->fi_readers == 1) {
168 			fpipe->pipe_state &= ~PIPE_EOF;
169 			if (fip->fi_writers > 0) {
170 				wakeup(&fip->fi_writers);
171 				pipeselwakeup(fpipe);
172 			}
173 		}
174 		fp->f_pipegen = fpipe->pipe_wgen - fip->fi_writers;
175 	}
176 	if (ap->a_mode & FWRITE) {
177 		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
178 			PIPE_UNLOCK(fpipe);
179 			if (fip->fi_writers == 0)
180 				fifo_cleanup(vp);
181 			return (ENXIO);
182 		}
183 		fip->fi_writers++;
184 		fip->fi_wgen++;
185 		if (fip->fi_writers == 1) {
186 			fpipe->pipe_state &= ~PIPE_EOF;
187 			if (fip->fi_readers > 0) {
188 				wakeup(&fip->fi_readers);
189 				pipeselwakeup(fpipe);
190 			}
191 		}
192 	}
193 	if ((ap->a_mode & O_NONBLOCK) == 0) {
194 		if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
195 			gen = fip->fi_wgen;
196 			VOP_UNLOCK(vp);
197 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
198 			error = msleep(&fip->fi_readers, PIPE_MTX(fpipe),
199 			    PDROP | PCATCH | PSOCK, "fifoor", 0);
200 			sigallowstop(stops_deferred);
201 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
202 			if (error != 0 && gen == fip->fi_wgen) {
203 				fip->fi_readers--;
204 				if (fip->fi_readers == 0) {
205 					PIPE_LOCK(fpipe);
206 					fpipe->pipe_state |= PIPE_EOF;
207 					if (fpipe->pipe_state & PIPE_WANTW)
208 						wakeup(fpipe);
209 					pipeselwakeup(fpipe);
210 					PIPE_UNLOCK(fpipe);
211 					fifo_cleanup(vp);
212 				}
213 				return (error);
214 			}
215 			PIPE_LOCK(fpipe);
216 			/*
217 			 * We must have got woken up because we had a writer.
218 			 * That (and not still having one) is the condition
219 			 * that we must wait for.
220 			 */
221 		}
222 		if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
223 			gen = fip->fi_rgen;
224 			VOP_UNLOCK(vp);
225 			stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
226 			error = msleep(&fip->fi_writers, PIPE_MTX(fpipe),
227 			    PDROP | PCATCH | PSOCK, "fifoow", 0);
228 			sigallowstop(stops_deferred);
229 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
230 			if (error != 0 && gen == fip->fi_rgen) {
231 				fip->fi_writers--;
232 				if (fip->fi_writers == 0) {
233 					PIPE_LOCK(fpipe);
234 					fpipe->pipe_state |= PIPE_EOF;
235 					if (fpipe->pipe_state & PIPE_WANTR)
236 						wakeup(fpipe);
237 					fpipe->pipe_wgen++;
238 					pipeselwakeup(fpipe);
239 					PIPE_UNLOCK(fpipe);
240 					fifo_cleanup(vp);
241 				}
242 				return (error);
243 			}
244 			/*
245 			 * We must have got woken up because we had
246 			 * a reader.  That (and not still having one)
247 			 * is the condition that we must wait for.
248 			 */
249 			PIPE_LOCK(fpipe);
250 		}
251 	}
252 	PIPE_UNLOCK(fpipe);
253 	KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
254 	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
255 	finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
256 	return (0);
257 }
258 
259 /*
260  * Device close routine
261  */
262 /* ARGSUSED */
263 static int
264 fifo_close(struct vop_close_args *ap)
265 {
266 	struct vnode *vp;
267 	struct fifoinfo *fip;
268 	struct pipe *cpipe;
269 
270 	vp = ap->a_vp;
271 	ASSERT_VOP_ELOCKED(vp, "fifo_close");
272 	fip = vp->v_fifoinfo;
273 
274 	/*
275 	 * During open, it is possible that the fifo vnode is relocked
276 	 * after the vnode is instantiated but before VOP_OPEN() is
277 	 * done.  For instance, vn_open_vnode() might need to upgrade
278 	 * vnode lock, or ffs_vput_pair() needs to unlock vp to sync
279 	 * dvp.  In this case, reclaim can observe us with v_fifoinfo
280 	 * equal to NULL.
281 	 */
282 	if (fip == NULL)
283 		return (0);
284 
285 	cpipe = fip->fi_pipe;
286 	if (ap->a_fflag & FREAD) {
287 		fip->fi_readers--;
288 		if (fip->fi_readers == 0) {
289 			PIPE_LOCK(cpipe);
290 			cpipe->pipe_state |= PIPE_EOF;
291 			if ((cpipe->pipe_state & PIPE_WANTW)) {
292 				cpipe->pipe_state &= ~PIPE_WANTW;
293 				wakeup(cpipe);
294 			}
295 			pipeselwakeup(cpipe);
296 			PIPE_UNLOCK(cpipe);
297 		}
298 	}
299 	if (ap->a_fflag & FWRITE) {
300 		fip->fi_writers--;
301 		if (fip->fi_writers == 0) {
302 			PIPE_LOCK(cpipe);
303 			cpipe->pipe_state |= PIPE_EOF;
304 			if ((cpipe->pipe_state & PIPE_WANTR)) {
305 				cpipe->pipe_state &= ~PIPE_WANTR;
306 				wakeup(cpipe);
307 			}
308 			cpipe->pipe_wgen++;
309 			pipeselwakeup(cpipe);
310 			PIPE_UNLOCK(cpipe);
311 		}
312 	}
313 	fifo_cleanup(vp);
314 	return (0);
315 }
316 
317 /*
318  * Print out internal contents of a fifo vnode.
319  */
320 int
321 fifo_printinfo(struct vnode *vp)
322 {
323 	struct fifoinfo *fip = vp->v_fifoinfo;
324 
325 	if (fip == NULL){
326 		printf(", NULL v_fifoinfo");
327 		return (0);
328 	}
329 	printf(", fifo with %ld readers and %ld writers",
330 		fip->fi_readers, fip->fi_writers);
331 	return (0);
332 }
333 
334 /*
335  * Print out the contents of a fifo vnode.
336  */
337 static int
338 fifo_print(struct vop_print_args *ap)
339 {
340 	printf("    ");
341 	fifo_printinfo(ap->a_vp);
342 	printf("\n");
343 	return (0);
344 }
345 
346 /*
347  * Fifo advisory byte-level locks.
348  */
349 /* ARGSUSED */
350 static int
351 fifo_advlock(struct vop_advlock_args *ap)
352 {
353 
354 	if ((ap->a_flags & F_FLOCK) == 0)
355 		return (EINVAL);
356 	return (vop_stdadvlock(ap));
357 }
358