xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision 78704ef45793e56c8e064611c05c9bb8a0067e9f)
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((caddr_t)&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((caddr_t)&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((caddr_t)&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 				error = tsleep((caddr_t)&fip->fi_writers,
248 				    PCATCH | PSOCK, "fifoow", 0);
249 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
250 				if (error)
251 					goto bad;
252 			}
253 		}
254 	}
255 	return (0);
256 bad:
257 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, td);
258 	return (error);
259 }
260 
261 /*
262  * Vnode op for read
263  */
264 /* ARGSUSED */
265 static int
266 fifo_read(ap)
267 	struct vop_read_args /* {
268 		struct vnode *a_vp;
269 		struct uio *a_uio;
270 		int  a_ioflag;
271 		struct ucred *a_cred;
272 	} */ *ap;
273 {
274 	struct uio *uio = ap->a_uio;
275 	struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
276 	struct thread *td = uio->uio_td;
277 	int error, startresid;
278 
279 #ifdef DIAGNOSTIC
280 	if (uio->uio_rw != UIO_READ)
281 		panic("fifo_read mode");
282 #endif
283 	if (uio->uio_resid == 0)
284 		return (0);
285 	if (ap->a_ioflag & IO_NDELAY)
286 		rso->so_state |= SS_NBIO;
287 	startresid = uio->uio_resid;
288 	VOP_UNLOCK(ap->a_vp, 0, td);
289 	error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
290 	    (struct mbuf **)0, (int *)0);
291 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
292 	if (ap->a_ioflag & IO_NDELAY)
293 		rso->so_state &= ~SS_NBIO;
294 	return (error);
295 }
296 
297 /*
298  * Vnode op for write
299  */
300 /* ARGSUSED */
301 static int
302 fifo_write(ap)
303 	struct vop_write_args /* {
304 		struct vnode *a_vp;
305 		struct uio *a_uio;
306 		int  a_ioflag;
307 		struct ucred *a_cred;
308 	} */ *ap;
309 {
310 	struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
311 	struct thread *td = ap->a_uio->uio_td;
312 	int error;
313 
314 #ifdef DIAGNOSTIC
315 	if (ap->a_uio->uio_rw != UIO_WRITE)
316 		panic("fifo_write mode");
317 #endif
318 	if (ap->a_ioflag & IO_NDELAY)
319 		wso->so_state |= SS_NBIO;
320 	VOP_UNLOCK(ap->a_vp, 0, td);
321 	error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
322 		       (struct mbuf *)0, 0, td);
323 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
324 	if (ap->a_ioflag & IO_NDELAY)
325 		wso->so_state &= ~SS_NBIO;
326 	return (error);
327 }
328 
329 /*
330  * Device ioctl operation.
331  */
332 /* ARGSUSED */
333 static int
334 fifo_ioctl(ap)
335 	struct vop_ioctl_args /* {
336 		struct vnode *a_vp;
337 		u_long  a_command;
338 		caddr_t  a_data;
339 		int  a_fflag;
340 		struct ucred *a_cred;
341 		struct thread *a_td;
342 	} */ *ap;
343 {
344 	struct file filetmp;	/* Local, so need not be locked. */
345 	int error;
346 
347 	if (ap->a_command == FIONBIO)
348 		return (0);
349 	if (ap->a_fflag & FREAD) {
350 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
351 		filetmp.f_cred = ap->a_cred;
352 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
353 		    ap->a_td->td_ucred, ap->a_td);
354 		if (error)
355 			return (error);
356 	}
357 	if (ap->a_fflag & FWRITE) {
358 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
359 		filetmp.f_cred = ap->a_cred;
360 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
361 		    ap->a_td->td_ucred, ap->a_td);
362 		if (error)
363 			return (error);
364 	}
365 	return (0);
366 }
367 
368 /* ARGSUSED */
369 static int
370 fifo_kqfilter(ap)
371 	struct vop_kqfilter_args /* {
372 		struct vnode *a_vp;
373 		struct knote *a_kn;
374 	} */ *ap;
375 {
376 	struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
377 	struct socket *so;
378 	struct sockbuf *sb;
379 
380 	switch (ap->a_kn->kn_filter) {
381 	case EVFILT_READ:
382 		ap->a_kn->kn_fop = &fiforead_filtops;
383 		so = fi->fi_readsock;
384 		sb = &so->so_rcv;
385 		break;
386 	case EVFILT_WRITE:
387 		ap->a_kn->kn_fop = &fifowrite_filtops;
388 		so = fi->fi_writesock;
389 		sb = &so->so_snd;
390 		break;
391 	default:
392 		return (1);
393 	}
394 
395 	ap->a_kn->kn_hook = (caddr_t)so;
396 
397 	SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
398 	sb->sb_flags |= SB_KNOTE;
399 
400 	return (0);
401 }
402 
403 static void
404 filt_fifordetach(struct knote *kn)
405 {
406 	struct socket *so = (struct socket *)kn->kn_hook;
407 
408 	SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
409 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
410 		so->so_rcv.sb_flags &= ~SB_KNOTE;
411 }
412 
413 static int
414 filt_fiforead(struct knote *kn, long hint)
415 {
416 	struct socket *so = (struct socket *)kn->kn_hook;
417 
418 	kn->kn_data = so->so_rcv.sb_cc;
419 	if (so->so_state & SS_CANTRCVMORE) {
420 		kn->kn_flags |= EV_EOF;
421 		return (1);
422 	}
423 	kn->kn_flags &= ~EV_EOF;
424 	return (kn->kn_data > 0);
425 }
426 
427 static void
428 filt_fifowdetach(struct knote *kn)
429 {
430 	struct socket *so = (struct socket *)kn->kn_hook;
431 
432 	SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
433 	if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
434 		so->so_snd.sb_flags &= ~SB_KNOTE;
435 }
436 
437 static int
438 filt_fifowrite(struct knote *kn, long hint)
439 {
440 	struct socket *so = (struct socket *)kn->kn_hook;
441 
442 	kn->kn_data = sbspace(&so->so_snd);
443 	if (so->so_state & SS_CANTSENDMORE) {
444 		kn->kn_flags |= EV_EOF;
445 		return (1);
446 	}
447 	kn->kn_flags &= ~EV_EOF;
448 	return (kn->kn_data >= so->so_snd.sb_lowat);
449 }
450 
451 /* ARGSUSED */
452 static int
453 fifo_poll(ap)
454 	struct vop_poll_args /* {
455 		struct vnode *a_vp;
456 		int  a_events;
457 		struct ucred *a_cred;
458 		struct thread *a_td;
459 	} */ *ap;
460 {
461 	struct file filetmp;
462 	int events, revents = 0;
463 
464 	events = ap->a_events &
465 	    (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
466 	if (events) {
467 		/*
468 		 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
469 		 * not, then convert the first two to the last one.  This
470 		 * tells the socket poll function to ignore EOF so that we
471 		 * block if there is no writer (and no data).  Callers can
472 		 * set POLLINIGNEOF to get non-blocking behavior.
473 		 */
474 		if (events & (POLLIN | POLLRDNORM) &&
475 		    !(events & POLLINIGNEOF)) {
476 			events &= ~(POLLIN | POLLRDNORM);
477 			events |= POLLINIGNEOF;
478 		}
479 
480 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
481 		filetmp.f_cred = ap->a_cred;
482 		if (filetmp.f_data)
483 			revents |= soo_poll(&filetmp, events,
484 			    ap->a_td->td_ucred, ap->a_td);
485 
486 		/* Reverse the above conversion. */
487 		if ((revents & POLLINIGNEOF) &&
488 		    !(ap->a_events & POLLINIGNEOF)) {
489 			revents |= (ap->a_events & (POLLIN | POLLRDNORM));
490 			revents &= ~POLLINIGNEOF;
491 		}
492 	}
493 	events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND);
494 	if (events) {
495 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
496 		filetmp.f_cred = ap->a_cred;
497 		if (filetmp.f_data)
498 			revents |= soo_poll(&filetmp, events,
499 			    ap->a_td->td_ucred, ap->a_td);
500 	}
501 	return (revents);
502 }
503 
504 /*
505  * Device close routine
506  */
507 /* ARGSUSED */
508 static int
509 fifo_close(ap)
510 	struct vop_close_args /* {
511 		struct vnode *a_vp;
512 		int  a_fflag;
513 		struct ucred *a_cred;
514 		struct thread *a_td;
515 	} */ *ap;
516 {
517 	register struct vnode *vp = ap->a_vp;
518 	register struct fifoinfo *fip = vp->v_fifoinfo;
519 	int error1, error2;
520 
521 	if (ap->a_fflag & FREAD) {
522 		fip->fi_readers--;
523 		if (fip->fi_readers == 0)
524 			socantsendmore(fip->fi_writesock);
525 	}
526 	if (ap->a_fflag & FWRITE) {
527 		fip->fi_writers--;
528 		if (fip->fi_writers == 0)
529 			socantrcvmore(fip->fi_readsock);
530 	}
531 	if (vrefcnt(vp) > 1)
532 		return (0);
533 	error1 = soclose(fip->fi_readsock);
534 	error2 = soclose(fip->fi_writesock);
535 	FREE(fip, M_VNODE);
536 	vp->v_fifoinfo = NULL;
537 	if (error1)
538 		return (error1);
539 	return (error2);
540 }
541 
542 
543 /*
544  * Print out internal contents of a fifo vnode.
545  */
546 int
547 fifo_printinfo(vp)
548 	struct vnode *vp;
549 {
550 	register struct fifoinfo *fip = vp->v_fifoinfo;
551 
552 	printf(", fifo with %ld readers and %ld writers",
553 		fip->fi_readers, fip->fi_writers);
554 	return (0);
555 }
556 
557 /*
558  * Print out the contents of a fifo vnode.
559  */
560 static int
561 fifo_print(ap)
562 	struct vop_print_args /* {
563 		struct vnode *a_vp;
564 	} */ *ap;
565 {
566 	fifo_printinfo(ap->a_vp);
567 	printf("\n");
568 	return (0);
569 }
570 
571 /*
572  * Return POSIX pathconf information applicable to fifo's.
573  */
574 static int
575 fifo_pathconf(ap)
576 	struct vop_pathconf_args /* {
577 		struct vnode *a_vp;
578 		int a_name;
579 		int *a_retval;
580 	} */ *ap;
581 {
582 
583 	switch (ap->a_name) {
584 	case _PC_LINK_MAX:
585 		*ap->a_retval = LINK_MAX;
586 		return (0);
587 	case _PC_PIPE_BUF:
588 		*ap->a_retval = PIPE_BUF;
589 		return (0);
590 	case _PC_CHOWN_RESTRICTED:
591 		*ap->a_retval = 1;
592 		return (0);
593 	default:
594 		return (EINVAL);
595 	}
596 	/* NOTREACHED */
597 }
598 
599 /*
600  * Fifo advisory byte-level locks.
601  */
602 /* ARGSUSED */
603 static int
604 fifo_advlock(ap)
605 	struct vop_advlock_args /* {
606 		struct vnode *a_vp;
607 		caddr_t  a_id;
608 		int  a_op;
609 		struct flock *a_fl;
610 		int  a_flags;
611 	} */ *ap;
612 {
613 
614 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
615 }
616