xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*
2  * Copyright (c) 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/event.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/malloc.h>
46 #include <sys/poll.h>
47 #include <sys/proc.h> /* XXXKSE */
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.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 #include <fs/fifofs/fifo.h>
57 
58 /*
59  * This structure is associated with the FIFO vnode and stores
60  * the state associated with the FIFO.
61  */
62 struct fifoinfo {
63 	struct socket	*fi_readsock;
64 	struct socket	*fi_writesock;
65 	long		fi_readers;
66 	long		fi_writers;
67 };
68 
69 static int	fifo_print(struct vop_print_args *);
70 static int	fifo_lookup(struct vop_lookup_args *);
71 static int	fifo_open(struct vop_open_args *);
72 static int	fifo_close(struct vop_close_args *);
73 static int	fifo_read(struct vop_read_args *);
74 static int	fifo_write(struct vop_write_args *);
75 static int	fifo_ioctl(struct vop_ioctl_args *);
76 static int	fifo_poll(struct vop_poll_args *);
77 static int	fifo_kqfilter(struct vop_kqfilter_args *);
78 static int	fifo_pathconf(struct vop_pathconf_args *);
79 static int	fifo_advlock(struct vop_advlock_args *);
80 
81 static void	filt_fifordetach(struct knote *kn);
82 static int	filt_fiforead(struct knote *kn, long hint);
83 static void	filt_fifowdetach(struct knote *kn);
84 static int	filt_fifowrite(struct knote *kn, long hint);
85 
86 static struct filterops fiforead_filtops =
87 	{ 1, NULL, filt_fifordetach, filt_fiforead };
88 static struct filterops fifowrite_filtops =
89 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
90 
91 vop_t **fifo_vnodeop_p;
92 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
93 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
94 	{ &vop_access_desc,		(vop_t *) vop_ebadf },
95 	{ &vop_advlock_desc,		(vop_t *) fifo_advlock },
96 	{ &vop_close_desc,		(vop_t *) fifo_close },
97 	{ &vop_create_desc,		(vop_t *) vop_panic },
98 	{ &vop_getattr_desc,		(vop_t *) vop_ebadf },
99 	{ &vop_getwritemount_desc, 	(vop_t *) vop_stdgetwritemount },
100 	{ &vop_ioctl_desc,		(vop_t *) fifo_ioctl },
101 	{ &vop_kqfilter_desc,		(vop_t *) fifo_kqfilter },
102 	{ &vop_lease_desc,		(vop_t *) vop_null },
103 	{ &vop_link_desc,		(vop_t *) vop_panic },
104 	{ &vop_lookup_desc,		(vop_t *) fifo_lookup },
105 	{ &vop_mkdir_desc,		(vop_t *) vop_panic },
106 	{ &vop_mknod_desc,		(vop_t *) vop_panic },
107 	{ &vop_open_desc,		(vop_t *) fifo_open },
108 	{ &vop_pathconf_desc,		(vop_t *) fifo_pathconf },
109 	{ &vop_poll_desc,		(vop_t *) fifo_poll },
110 	{ &vop_print_desc,		(vop_t *) fifo_print },
111 	{ &vop_read_desc,		(vop_t *) fifo_read },
112 	{ &vop_readdir_desc,		(vop_t *) vop_panic },
113 	{ &vop_readlink_desc,		(vop_t *) vop_panic },
114 	{ &vop_reallocblks_desc,	(vop_t *) vop_panic },
115 	{ &vop_reclaim_desc,		(vop_t *) vop_null },
116 	{ &vop_remove_desc,		(vop_t *) vop_panic },
117 	{ &vop_rename_desc,		(vop_t *) vop_panic },
118 	{ &vop_rmdir_desc,		(vop_t *) vop_panic },
119 	{ &vop_setattr_desc,		(vop_t *) vop_ebadf },
120 	{ &vop_symlink_desc,		(vop_t *) vop_panic },
121 	{ &vop_write_desc,		(vop_t *) fifo_write },
122 	{ NULL, NULL }
123 };
124 static struct vnodeopv_desc fifo_vnodeop_opv_desc =
125 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
126 
127 VNODEOP_SET(fifo_vnodeop_opv_desc);
128 
129 int
130 fifo_vnoperate(ap)
131 	struct vop_generic_args /* {
132 		struct vnodeop_desc *a_desc;
133 		<other random data follows, presumably>
134 	} */ *ap;
135 {
136 	return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap));
137 }
138 
139 /*
140  * Trivial lookup routine that always fails.
141  */
142 /* ARGSUSED */
143 static int
144 fifo_lookup(ap)
145 	struct vop_lookup_args /* {
146 		struct vnode * a_dvp;
147 		struct vnode ** a_vpp;
148 		struct componentname * a_cnp;
149 	} */ *ap;
150 {
151 
152 	*ap->a_vpp = NULL;
153 	return (ENOTDIR);
154 }
155 
156 /*
157  * Open called to set up a new instance of a fifo or
158  * to find an active instance of a fifo.
159  */
160 /* ARGSUSED */
161 static int
162 fifo_open(ap)
163 	struct vop_open_args /* {
164 		struct vnode *a_vp;
165 		int  a_mode;
166 		struct ucred *a_cred;
167 		struct thread *a_td;
168 	} */ *ap;
169 {
170 	struct vnode *vp = ap->a_vp;
171 	struct fifoinfo *fip;
172 	struct thread *td = ap->a_td;
173 	struct socket *rso, *wso;
174 	int error;
175 
176 	if ((fip = vp->v_fifoinfo) == NULL) {
177 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
178 		vp->v_fifoinfo = fip;
179 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0,
180 		    ap->a_td->td_ucred, ap->a_td);
181 		if (error) {
182 			free(fip, M_VNODE);
183 			vp->v_fifoinfo = NULL;
184 			return (error);
185 		}
186 		fip->fi_readsock = rso;
187 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0,
188 		    ap->a_td->td_ucred, ap->a_td);
189 		if (error) {
190 			(void)soclose(rso);
191 			free(fip, M_VNODE);
192 			vp->v_fifoinfo = NULL;
193 			return (error);
194 		}
195 		fip->fi_writesock = wso;
196 		error = unp_connect2(wso, rso);
197 		if (error) {
198 			(void)soclose(wso);
199 			(void)soclose(rso);
200 			free(fip, M_VNODE);
201 			vp->v_fifoinfo = NULL;
202 			return (error);
203 		}
204 		fip->fi_readers = fip->fi_writers = 0;
205 		wso->so_snd.sb_lowat = PIPE_BUF;
206 		rso->so_state |= SS_CANTRCVMORE;
207 	}
208 	if (ap->a_mode & FREAD) {
209 		fip->fi_readers++;
210 		if (fip->fi_readers == 1) {
211 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
212 			if (fip->fi_writers > 0) {
213 				wakeup(&fip->fi_writers);
214 				sowwakeup(fip->fi_writesock);
215 			}
216 		}
217 	}
218 	if (ap->a_mode & FWRITE) {
219 		fip->fi_writers++;
220 		if (fip->fi_writers == 1) {
221 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
222 			if (fip->fi_readers > 0) {
223 				wakeup(&fip->fi_readers);
224 				sorwakeup(fip->fi_writesock);
225 			}
226 		}
227 	}
228 	if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
229 		while (fip->fi_writers == 0) {
230 			VOP_UNLOCK(vp, 0, td);
231 			error = tsleep(&fip->fi_readers,
232 			    PCATCH | PSOCK, "fifoor", 0);
233 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
234 			if (error)
235 				goto bad;
236 		}
237 	}
238 	if (ap->a_mode & FWRITE) {
239 		if (ap->a_mode & O_NONBLOCK) {
240 			if (fip->fi_readers == 0) {
241 				error = ENXIO;
242 				goto bad;
243 			}
244 		} else {
245 			while (fip->fi_readers == 0) {
246 				VOP_UNLOCK(vp, 0, td);
247 				/*
248 				 * XXX: Some race I havn't located is solved
249 				 * by timing out after a sec.  Race seen when
250 				 * sendmail hangs here during boot /phk
251 				 */
252 				error = tsleep(&fip->fi_writers,
253 				    PCATCH | PSOCK, "fifoow", hz);
254 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
255 				if (error)
256 					goto bad;
257 			}
258 		}
259 	}
260 	return (0);
261 bad:
262 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, td);
263 	return (error);
264 }
265 
266 /*
267  * Vnode op for read
268  */
269 /* ARGSUSED */
270 static int
271 fifo_read(ap)
272 	struct vop_read_args /* {
273 		struct vnode *a_vp;
274 		struct uio *a_uio;
275 		int  a_ioflag;
276 		struct ucred *a_cred;
277 	} */ *ap;
278 {
279 	struct uio *uio = ap->a_uio;
280 	struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
281 	struct thread *td = uio->uio_td;
282 	int error, startresid;
283 
284 #ifdef DIAGNOSTIC
285 	if (uio->uio_rw != UIO_READ)
286 		panic("fifo_read mode");
287 #endif
288 	if (uio->uio_resid == 0)
289 		return (0);
290 	if (ap->a_ioflag & IO_NDELAY)
291 		rso->so_state |= SS_NBIO;
292 	startresid = uio->uio_resid;
293 	VOP_UNLOCK(ap->a_vp, 0, td);
294 	error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
295 	    (struct mbuf **)0, (int *)0);
296 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
297 	if (ap->a_ioflag & IO_NDELAY)
298 		rso->so_state &= ~SS_NBIO;
299 	return (error);
300 }
301 
302 /*
303  * Vnode op for write
304  */
305 /* ARGSUSED */
306 static int
307 fifo_write(ap)
308 	struct vop_write_args /* {
309 		struct vnode *a_vp;
310 		struct uio *a_uio;
311 		int  a_ioflag;
312 		struct ucred *a_cred;
313 	} */ *ap;
314 {
315 	struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
316 	struct thread *td = ap->a_uio->uio_td;
317 	int error;
318 
319 #ifdef DIAGNOSTIC
320 	if (ap->a_uio->uio_rw != UIO_WRITE)
321 		panic("fifo_write mode");
322 #endif
323 	if (ap->a_ioflag & IO_NDELAY)
324 		wso->so_state |= SS_NBIO;
325 	VOP_UNLOCK(ap->a_vp, 0, td);
326 	error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
327 		       (struct mbuf *)0, 0, td);
328 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
329 	if (ap->a_ioflag & IO_NDELAY)
330 		wso->so_state &= ~SS_NBIO;
331 	return (error);
332 }
333 
334 /*
335  * Device ioctl operation.
336  */
337 /* ARGSUSED */
338 static int
339 fifo_ioctl(ap)
340 	struct vop_ioctl_args /* {
341 		struct vnode *a_vp;
342 		u_long  a_command;
343 		caddr_t  a_data;
344 		int  a_fflag;
345 		struct ucred *a_cred;
346 		struct thread *a_td;
347 	} */ *ap;
348 {
349 	struct file filetmp;	/* Local, so need not be locked. */
350 	int error;
351 
352 	if (ap->a_command == FIONBIO)
353 		return (0);
354 	if (ap->a_fflag & FREAD) {
355 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
356 		filetmp.f_cred = ap->a_cred;
357 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
358 		    ap->a_td->td_ucred, ap->a_td);
359 		if (error)
360 			return (error);
361 	}
362 	if (ap->a_fflag & FWRITE) {
363 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
364 		filetmp.f_cred = ap->a_cred;
365 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
366 		    ap->a_td->td_ucred, ap->a_td);
367 		if (error)
368 			return (error);
369 	}
370 	return (0);
371 }
372 
373 /* ARGSUSED */
374 static int
375 fifo_kqfilter(ap)
376 	struct vop_kqfilter_args /* {
377 		struct vnode *a_vp;
378 		struct knote *a_kn;
379 	} */ *ap;
380 {
381 	struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
382 	struct socket *so;
383 	struct sockbuf *sb;
384 
385 	switch (ap->a_kn->kn_filter) {
386 	case EVFILT_READ:
387 		ap->a_kn->kn_fop = &fiforead_filtops;
388 		so = fi->fi_readsock;
389 		sb = &so->so_rcv;
390 		break;
391 	case EVFILT_WRITE:
392 		ap->a_kn->kn_fop = &fifowrite_filtops;
393 		so = fi->fi_writesock;
394 		sb = &so->so_snd;
395 		break;
396 	default:
397 		return (1);
398 	}
399 
400 	ap->a_kn->kn_hook = (caddr_t)so;
401 
402 	SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
403 	sb->sb_flags |= SB_KNOTE;
404 
405 	return (0);
406 }
407 
408 static void
409 filt_fifordetach(struct knote *kn)
410 {
411 	struct socket *so = (struct socket *)kn->kn_hook;
412 
413 	SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
414 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
415 		so->so_rcv.sb_flags &= ~SB_KNOTE;
416 }
417 
418 static int
419 filt_fiforead(struct knote *kn, long hint)
420 {
421 	struct socket *so = (struct socket *)kn->kn_hook;
422 
423 	kn->kn_data = so->so_rcv.sb_cc;
424 	if (so->so_state & SS_CANTRCVMORE) {
425 		kn->kn_flags |= EV_EOF;
426 		return (1);
427 	}
428 	kn->kn_flags &= ~EV_EOF;
429 	return (kn->kn_data > 0);
430 }
431 
432 static void
433 filt_fifowdetach(struct knote *kn)
434 {
435 	struct socket *so = (struct socket *)kn->kn_hook;
436 
437 	SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
438 	if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
439 		so->so_snd.sb_flags &= ~SB_KNOTE;
440 }
441 
442 static int
443 filt_fifowrite(struct knote *kn, long hint)
444 {
445 	struct socket *so = (struct socket *)kn->kn_hook;
446 
447 	kn->kn_data = sbspace(&so->so_snd);
448 	if (so->so_state & SS_CANTSENDMORE) {
449 		kn->kn_flags |= EV_EOF;
450 		return (1);
451 	}
452 	kn->kn_flags &= ~EV_EOF;
453 	return (kn->kn_data >= so->so_snd.sb_lowat);
454 }
455 
456 /* ARGSUSED */
457 static int
458 fifo_poll(ap)
459 	struct vop_poll_args /* {
460 		struct vnode *a_vp;
461 		int  a_events;
462 		struct ucred *a_cred;
463 		struct thread *a_td;
464 	} */ *ap;
465 {
466 	struct file filetmp;
467 	int events, revents = 0;
468 
469 	events = ap->a_events &
470 	    (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
471 	if (events) {
472 		/*
473 		 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
474 		 * not, then convert the first two to the last one.  This
475 		 * tells the socket poll function to ignore EOF so that we
476 		 * block if there is no writer (and no data).  Callers can
477 		 * set POLLINIGNEOF to get non-blocking behavior.
478 		 */
479 		if (events & (POLLIN | POLLRDNORM) &&
480 		    !(events & POLLINIGNEOF)) {
481 			events &= ~(POLLIN | POLLRDNORM);
482 			events |= POLLINIGNEOF;
483 		}
484 
485 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
486 		filetmp.f_cred = ap->a_cred;
487 		if (filetmp.f_data)
488 			revents |= soo_poll(&filetmp, events,
489 			    ap->a_td->td_ucred, ap->a_td);
490 
491 		/* Reverse the above conversion. */
492 		if ((revents & POLLINIGNEOF) &&
493 		    !(ap->a_events & POLLINIGNEOF)) {
494 			revents |= (ap->a_events & (POLLIN | POLLRDNORM));
495 			revents &= ~POLLINIGNEOF;
496 		}
497 	}
498 	events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND);
499 	if (events) {
500 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
501 		filetmp.f_cred = ap->a_cred;
502 		if (filetmp.f_data) {
503 			revents |= soo_poll(&filetmp, events,
504 			    ap->a_td->td_ucred, ap->a_td);
505 		}
506 	}
507 	return (revents);
508 }
509 
510 /*
511  * Device close routine
512  */
513 /* ARGSUSED */
514 static int
515 fifo_close(ap)
516 	struct vop_close_args /* {
517 		struct vnode *a_vp;
518 		int  a_fflag;
519 		struct ucred *a_cred;
520 		struct thread *a_td;
521 	} */ *ap;
522 {
523 	register struct vnode *vp = ap->a_vp;
524 	register struct fifoinfo *fip = vp->v_fifoinfo;
525 	int error1, error2;
526 
527 	if (ap->a_fflag & FREAD) {
528 		fip->fi_readers--;
529 		if (fip->fi_readers == 0)
530 			socantsendmore(fip->fi_writesock);
531 	}
532 	if (ap->a_fflag & FWRITE) {
533 		fip->fi_writers--;
534 		if (fip->fi_writers == 0)
535 			socantrcvmore(fip->fi_readsock);
536 	}
537 	if (vrefcnt(vp) > 1)
538 		return (0);
539 	error1 = soclose(fip->fi_readsock);
540 	error2 = soclose(fip->fi_writesock);
541 	FREE(fip, M_VNODE);
542 	vp->v_fifoinfo = NULL;
543 	if (error1)
544 		return (error1);
545 	return (error2);
546 }
547 
548 
549 /*
550  * Print out internal contents of a fifo vnode.
551  */
552 int
553 fifo_printinfo(vp)
554 	struct vnode *vp;
555 {
556 	register struct fifoinfo *fip = vp->v_fifoinfo;
557 
558 	printf(", fifo with %ld readers and %ld writers",
559 		fip->fi_readers, fip->fi_writers);
560 	return (0);
561 }
562 
563 /*
564  * Print out the contents of a fifo vnode.
565  */
566 static int
567 fifo_print(ap)
568 	struct vop_print_args /* {
569 		struct vnode *a_vp;
570 	} */ *ap;
571 {
572 	fifo_printinfo(ap->a_vp);
573 	printf("\n");
574 	return (0);
575 }
576 
577 /*
578  * Return POSIX pathconf information applicable to fifo's.
579  */
580 static int
581 fifo_pathconf(ap)
582 	struct vop_pathconf_args /* {
583 		struct vnode *a_vp;
584 		int a_name;
585 		int *a_retval;
586 	} */ *ap;
587 {
588 
589 	switch (ap->a_name) {
590 	case _PC_LINK_MAX:
591 		*ap->a_retval = LINK_MAX;
592 		return (0);
593 	case _PC_PIPE_BUF:
594 		*ap->a_retval = PIPE_BUF;
595 		return (0);
596 	case _PC_CHOWN_RESTRICTED:
597 		*ap->a_retval = 1;
598 		return (0);
599 	default:
600 		return (EINVAL);
601 	}
602 	/* NOTREACHED */
603 }
604 
605 /*
606  * Fifo advisory byte-level locks.
607  */
608 /* ARGSUSED */
609 static int
610 fifo_advlock(ap)
611 	struct vop_advlock_args /* {
612 		struct vnode *a_vp;
613 		caddr_t  a_id;
614 		int  a_op;
615 		struct flock *a_fl;
616 		int  a_flags;
617 	} */ *ap;
618 {
619 
620 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
621 }
622