xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*-
2  * Copyright (c) 1990, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2005 Robert N. M. Watson
5  * Copyright (c) 2012 Giovanni Trematerra
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
33  * $FreeBSD$
34  */
35 
36 #include <sys/param.h>
37 #include <sys/event.h>
38 #include <sys/file.h>
39 #include <sys/filedesc.h>
40 #include <sys/filio.h>
41 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/malloc.h>
46 #include <sys/selinfo.h>
47 #include <sys/pipe.h>
48 #include <sys/proc.h>
49 #include <sys/signalvar.h>
50 #include <sys/sx.h>
51 #include <sys/systm.h>
52 #include <sys/un.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 
56 /*
57  * This structure is associated with the FIFO vnode and stores
58  * the state associated with the FIFO.
59  * Notes about locking:
60  *   - fi_pipe is invariant since init time.
61  *   - fi_readers and fi_writers are protected by the vnode lock.
62  */
63 struct fifoinfo {
64 	struct pipe *fi_pipe;
65 	long	fi_readers;
66 	long	fi_writers;
67 	u_int	fi_rgen;
68 	u_int	fi_wgen;
69 };
70 
71 static vop_print_t	fifo_print;
72 static vop_open_t	fifo_open;
73 static vop_close_t	fifo_close;
74 static vop_pathconf_t	fifo_pathconf;
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_kqfilter =		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 =		fifo_pathconf,
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 
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(ap)
129 	struct vop_open_args /* {
130 		struct vnode *a_vp;
131 		int  a_mode;
132 		struct ucred *a_cred;
133 		struct thread *a_td;
134 		struct file *a_fp;
135 	} */ *ap;
136 {
137 	struct vnode *vp;
138 	struct file *fp;
139 	struct thread *td;
140 	struct fifoinfo *fip;
141 	struct pipe *fpipe;
142 	u_int gen;
143 	int error, stops_deferred;
144 
145 	vp = ap->a_vp;
146 	fp = ap->a_fp;
147 	td = ap->a_td;
148 	ASSERT_VOP_ELOCKED(vp, "fifo_open");
149 	if (fp == NULL || (ap->a_mode & FEXEC) != 0)
150 		return (EINVAL);
151 	if ((fip = vp->v_fifoinfo) == NULL) {
152 		pipe_named_ctor(&fpipe, td);
153 		fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
154 		fip->fi_pipe = fpipe;
155 		fpipe->pipe_wgen = fip->fi_readers = fip->fi_writers = 0;
156  		KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
157 		vp->v_fifoinfo = fip;
158 	}
159 	fpipe = fip->fi_pipe;
160  	KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
161 
162 	/*
163 	 * Use the pipe mutex here, in addition to the vnode lock,
164 	 * in order to allow vnode lock dropping before msleep() calls
165 	 * and still avoiding missed wakeups.
166 	 */
167 	PIPE_LOCK(fpipe);
168 	if (ap->a_mode & FREAD) {
169 		fip->fi_readers++;
170 		fip->fi_rgen++;
171 		if (fip->fi_readers == 1) {
172 			fpipe->pipe_state &= ~PIPE_EOF;
173 			if (fip->fi_writers > 0)
174 				wakeup(&fip->fi_writers);
175 		}
176 		fp->f_seqcount = fpipe->pipe_wgen - fip->fi_writers;
177 	}
178 	if (ap->a_mode & FWRITE) {
179 		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
180 			PIPE_UNLOCK(fpipe);
181 			if (fip->fi_writers == 0)
182 				fifo_cleanup(vp);
183 			return (ENXIO);
184 		}
185 		fip->fi_writers++;
186 		fip->fi_wgen++;
187 		if (fip->fi_writers == 1) {
188 			fpipe->pipe_state &= ~PIPE_EOF;
189 			if (fip->fi_readers > 0)
190 				wakeup(&fip->fi_readers);
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, 0);
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 					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, 0);
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 					PIPE_UNLOCK(fpipe);
238 					fifo_cleanup(vp);
239 				}
240 				return (error);
241 			}
242 			/*
243 			 * We must have got woken up because we had
244 			 * a reader.  That (and not still having one)
245 			 * is the condition that we must wait for.
246 			 */
247 			PIPE_LOCK(fpipe);
248 		}
249 	}
250 	PIPE_UNLOCK(fpipe);
251 	KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
252 	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
253 	finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
254 	return (0);
255 }
256 
257 /*
258  * Device close routine
259  */
260 /* ARGSUSED */
261 static int
262 fifo_close(ap)
263 	struct vop_close_args /* {
264 		struct vnode *a_vp;
265 		int  a_fflag;
266 		struct ucred *a_cred;
267 		struct thread *a_td;
268 	} */ *ap;
269 {
270 	struct vnode *vp;
271 	struct fifoinfo *fip;
272 	struct pipe *cpipe;
273 
274 	vp = ap->a_vp;
275 	fip = vp->v_fifoinfo;
276 	cpipe = fip->fi_pipe;
277 	ASSERT_VOP_ELOCKED(vp, "fifo_close");
278 	if (ap->a_fflag & FREAD) {
279 		fip->fi_readers--;
280 		if (fip->fi_readers == 0) {
281 			PIPE_LOCK(cpipe);
282 			cpipe->pipe_state |= PIPE_EOF;
283 			if ((cpipe->pipe_state & PIPE_WANTW)) {
284 				cpipe->pipe_state &= ~PIPE_WANTW;
285 				wakeup(cpipe);
286 			}
287 			pipeselwakeup(cpipe);
288 			PIPE_UNLOCK(cpipe);
289 		}
290 	}
291 	if (ap->a_fflag & FWRITE) {
292 		fip->fi_writers--;
293 		if (fip->fi_writers == 0) {
294 			PIPE_LOCK(cpipe);
295 			cpipe->pipe_state |= PIPE_EOF;
296 			if ((cpipe->pipe_state & PIPE_WANTR)) {
297 				cpipe->pipe_state &= ~PIPE_WANTR;
298 				wakeup(cpipe);
299 			}
300 			cpipe->pipe_wgen++;
301 			pipeselwakeup(cpipe);
302 			PIPE_UNLOCK(cpipe);
303 		}
304 	}
305 	fifo_cleanup(vp);
306 	return (0);
307 }
308 
309 /*
310  * Print out internal contents of a fifo vnode.
311  */
312 int
313 fifo_printinfo(vp)
314 	struct vnode *vp;
315 {
316 	register struct fifoinfo *fip = vp->v_fifoinfo;
317 
318 	if (fip == NULL){
319 		printf(", NULL v_fifoinfo");
320 		return (0);
321 	}
322 	printf(", fifo with %ld readers and %ld writers",
323 		fip->fi_readers, fip->fi_writers);
324 	return (0);
325 }
326 
327 /*
328  * Print out the contents of a fifo vnode.
329  */
330 static int
331 fifo_print(ap)
332 	struct vop_print_args /* {
333 		struct vnode *a_vp;
334 	} */ *ap;
335 {
336 	printf("    ");
337 	fifo_printinfo(ap->a_vp);
338 	printf("\n");
339 	return (0);
340 }
341 
342 /*
343  * Return POSIX pathconf information applicable to fifo's.
344  */
345 static int
346 fifo_pathconf(ap)
347 	struct vop_pathconf_args /* {
348 		struct vnode *a_vp;
349 		int a_name;
350 		int *a_retval;
351 	} */ *ap;
352 {
353 
354 	switch (ap->a_name) {
355 	case _PC_LINK_MAX:
356 		*ap->a_retval = LINK_MAX;
357 		return (0);
358 	case _PC_PIPE_BUF:
359 		*ap->a_retval = PIPE_BUF;
360 		return (0);
361 	case _PC_CHOWN_RESTRICTED:
362 		*ap->a_retval = 1;
363 		return (0);
364 	default:
365 		return (EINVAL);
366 	}
367 	/* NOTREACHED */
368 }
369 
370 /*
371  * Fifo advisory byte-level locks.
372  */
373 /* ARGSUSED */
374 static int
375 fifo_advlock(ap)
376 	struct vop_advlock_args /* {
377 		struct vnode *a_vp;
378 		caddr_t  a_id;
379 		int  a_op;
380 		struct flock *a_fl;
381 		int  a_flags;
382 	} */ *ap;
383 {
384 
385 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
386 }
387 
388