xref: /netbsd-src/sys/miscfs/fifofs/fifo_vnops.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: fifo_vnops.c,v 1.49 2004/05/22 22:52:14 jonathan Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.49 2004/05/22 22:52:14 jonathan Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/time.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/socket.h>
44 #include <sys/protosw.h>
45 #include <sys/socketvar.h>
46 #include <sys/stat.h>
47 #include <sys/ioctl.h>
48 #include <sys/file.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 #include <sys/un.h>
52 #include <sys/poll.h>
53 #include <sys/event.h>
54 
55 #include <miscfs/fifofs/fifo.h>
56 #include <miscfs/genfs/genfs.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_opencount;
66 	long		fi_readers;
67 	long		fi_writers;
68 };
69 
70 int (**fifo_vnodeop_p) __P((void *));
71 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
72 	{ &vop_default_desc, vn_default_error },
73 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
74 	{ &vop_create_desc, fifo_create },		/* create */
75 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
76 	{ &vop_open_desc, fifo_open },			/* open */
77 	{ &vop_close_desc, fifo_close },		/* close */
78 	{ &vop_access_desc, fifo_access },		/* access */
79 	{ &vop_getattr_desc, fifo_getattr },		/* getattr */
80 	{ &vop_setattr_desc, fifo_setattr },		/* setattr */
81 	{ &vop_read_desc, fifo_read },			/* read */
82 	{ &vop_write_desc, fifo_write },		/* write */
83 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
84 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
85 	{ &vop_poll_desc, fifo_poll },			/* poll */
86 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
87 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
88 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
89 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
90 	{ &vop_seek_desc, fifo_seek },			/* seek */
91 	{ &vop_remove_desc, fifo_remove },		/* remove */
92 	{ &vop_link_desc, fifo_link },			/* link */
93 	{ &vop_rename_desc, fifo_rename },		/* rename */
94 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
95 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
96 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
97 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
98 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
99 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
100 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
101 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
102 	{ &vop_lock_desc, fifo_lock },			/* lock */
103 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
104 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
105 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
106 	{ &vop_print_desc, fifo_print },		/* print */
107 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
108 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
109 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
110 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
111 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
112 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
113 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
114 	{ &vop_update_desc, fifo_update },		/* update */
115 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
116 	{ &vop_putpages_desc, fifo_putpages }, 		/* putpages */
117 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
118 };
119 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
120 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
121 
122 /*
123  * Trivial lookup routine that always fails.
124  */
125 /* ARGSUSED */
126 int
127 fifo_lookup(void *v)
128 {
129 	struct vop_lookup_args /* {
130 		struct vnode		*a_dvp;
131 		struct vnode		**a_vpp;
132 		struct componentname	*a_cnp;
133 	} */ *ap = v;
134 
135 	*ap->a_vpp = NULL;
136 	return (ENOTDIR);
137 }
138 
139 /*
140  * Open called to set up a new instance of a fifo or
141  * to find an active instance of a fifo.
142  */
143 /* ARGSUSED */
144 int
145 fifo_open(void *v)
146 {
147 	struct vop_open_args /* {
148 		struct vnode	*a_vp;
149 		int		a_mode;
150 		struct ucred	*a_cred;
151 		struct proc	*a_p;
152 	} */ *ap = v;
153 	struct vnode	*vp;
154 	struct fifoinfo	*fip;
155 	struct proc	*p;
156 	struct socket	*rso, *wso;
157 	int		error;
158 
159 	vp = ap->a_vp;
160 	p = ap->a_p;
161 
162 	if ((fip = vp->v_fifoinfo) == NULL) {
163 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
164 		vp->v_fifoinfo = fip;
165 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, p);
166 		if (error != 0) {
167 			free(fip, M_VNODE);
168 			vp->v_fifoinfo = NULL;
169 			return (error);
170 		}
171 		fip->fi_readsock = rso;
172 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, p);
173 		if (error != 0) {
174 			(void)soclose(rso);
175 			free(fip, M_VNODE);
176 			vp->v_fifoinfo = NULL;
177 			return (error);
178 		}
179 		fip->fi_writesock = wso;
180 		if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
181 			(void)soclose(wso);
182 			(void)soclose(rso);
183 			free(fip, M_VNODE);
184 			vp->v_fifoinfo = NULL;
185 			return (error);
186 		}
187 		fip->fi_readers = fip->fi_writers = 0;
188 		fip->fi_opencount = 0;
189 		wso->so_state |= SS_CANTRCVMORE;
190 		rso->so_state |= SS_CANTSENDMORE;
191 	}
192 	fip->fi_opencount++;
193 	if (ap->a_mode & FREAD) {
194 		if (fip->fi_readers++ == 0) {
195 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
196 			if (fip->fi_writers > 0)
197 				wakeup(&fip->fi_writers);
198 		}
199 	}
200 	if (ap->a_mode & FWRITE) {
201 		if (fip->fi_writers++ == 0) {
202 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
203 			if (fip->fi_readers > 0)
204 				wakeup(&fip->fi_readers);
205 		}
206 	}
207 	if (ap->a_mode & FREAD) {
208 		if (ap->a_mode & O_NONBLOCK) {
209 		} else {
210 			while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
211 				VOP_UNLOCK(vp, 0);
212 				error = tsleep(&fip->fi_readers,
213 				    PCATCH | PSOCK, "fifor", 0);
214 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
215 				if (error)
216 					goto bad;
217 			}
218 		}
219 	}
220 	if (ap->a_mode & FWRITE) {
221 		if (ap->a_mode & O_NONBLOCK) {
222 			if (fip->fi_readers == 0) {
223 				error = ENXIO;
224 				goto bad;
225 			}
226 		} else {
227 			while (fip->fi_readers == 0) {
228 				VOP_UNLOCK(vp, 0);
229 				error = tsleep(&fip->fi_writers,
230 				    PCATCH | PSOCK, "fifow", 0);
231 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
232 				if (error)
233 					goto bad;
234 			}
235 		}
236 	}
237 	return (0);
238  bad:
239 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
240 	return (error);
241 }
242 
243 /*
244  * Vnode op for read
245  */
246 /* ARGSUSED */
247 int
248 fifo_read(void *v)
249 {
250 	struct vop_read_args /* {
251 		struct vnode	*a_vp;
252 		struct uio	*a_uio;
253 		int		a_ioflag;
254 		struct ucred	*a_cred;
255 	} */ *ap = v;
256 	struct uio	*uio;
257 	struct socket	*rso;
258 	int		error;
259 	size_t		startresid;
260 
261 	uio = ap->a_uio;
262 	rso = ap->a_vp->v_fifoinfo->fi_readsock;
263 #ifdef DIAGNOSTIC
264 	if (uio->uio_rw != UIO_READ)
265 		panic("fifo_read mode");
266 #endif
267 	if (uio->uio_resid == 0)
268 		return (0);
269 	if (ap->a_ioflag & IO_NDELAY)
270 		rso->so_state |= SS_NBIO;
271 	startresid = uio->uio_resid;
272 	VOP_UNLOCK(ap->a_vp, 0);
273 	error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
274 	    (struct mbuf **)0, (struct mbuf **)0, (int *)0);
275 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
276 	/*
277 	 * Clear EOF indication after first such return.
278 	 */
279 	if (uio->uio_resid == startresid)
280 		rso->so_state &= ~SS_CANTRCVMORE;
281 	if (ap->a_ioflag & IO_NDELAY) {
282 		rso->so_state &= ~SS_NBIO;
283 		if (error == EWOULDBLOCK &&
284 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
285 			error = 0;
286 	}
287 	return (error);
288 }
289 
290 /*
291  * Vnode op for write
292  */
293 /* ARGSUSED */
294 int
295 fifo_write(void *v)
296 {
297 	struct vop_write_args /* {
298 		struct vnode	*a_vp;
299 		struct uio	*a_uio;
300 		int		a_ioflag;
301 		struct ucred	*a_cred;
302 	} */ *ap = v;
303 	struct socket	*wso;
304 	int		error;
305 
306 	wso = ap->a_vp->v_fifoinfo->fi_writesock;
307 #ifdef DIAGNOSTIC
308 	if (ap->a_uio->uio_rw != UIO_WRITE)
309 		panic("fifo_write mode");
310 #endif
311 	if (ap->a_ioflag & IO_NDELAY)
312 		wso->so_state |= SS_NBIO;
313 	VOP_UNLOCK(ap->a_vp, 0);
314 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
315 	    (struct mbuf *)0, 0, curproc /*XXX*/);
316 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
317 	if (ap->a_ioflag & IO_NDELAY)
318 		wso->so_state &= ~SS_NBIO;
319 	return (error);
320 }
321 
322 /*
323  * Device ioctl operation.
324  */
325 /* ARGSUSED */
326 int
327 fifo_ioctl(void *v)
328 {
329 	struct vop_ioctl_args /* {
330 		struct vnode	*a_vp;
331 		u_long		a_command;
332 		void		*a_data;
333 		int		a_fflag;
334 		struct ucred	*a_cred;
335 		struct proc	*a_p;
336 	} */ *ap = v;
337 	struct file	filetmp;
338 	int		error;
339 
340 	if (ap->a_command == FIONBIO)
341 		return (0);
342 	if (ap->a_fflag & FREAD) {
343 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
344 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
345 		if (error)
346 			return (error);
347 	}
348 	if (ap->a_fflag & FWRITE) {
349 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
350 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
351 		if (error)
352 			return (error);
353 	}
354 	return (0);
355 }
356 
357 /* ARGSUSED */
358 int
359 fifo_poll(void *v)
360 {
361 	struct vop_poll_args /* {
362 		struct vnode	*a_vp;
363 		int		a_events;
364 		struct proc	*a_p;
365 	} */ *ap = v;
366 	struct file	filetmp;
367 	int		revents;
368 
369 	revents = 0;
370 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
371 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
372 		if (filetmp.f_data)
373 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
374 	}
375 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
376 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
377 		if (filetmp.f_data)
378 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
379 	}
380 
381 	return (revents);
382 }
383 
384 int
385 fifo_inactive(void *v)
386 {
387 	struct vop_inactive_args /* {
388 		struct vnode	*a_vp;
389 		struct proc	*a_p;
390 	} */ *ap = v;
391 
392 	VOP_UNLOCK(ap->a_vp, 0);
393 	return (0);
394 }
395 
396 /*
397  * This is a noop, simply returning what one has been given.
398  */
399 int
400 fifo_bmap(void *v)
401 {
402 	struct vop_bmap_args /* {
403 		struct vnode	*a_vp;
404 		daddr_t		a_bn;
405 		struct vnode	**a_vpp;
406 		daddr_t		*a_bnp;
407 		int		*a_runp;
408 	} */ *ap = v;
409 
410 	if (ap->a_vpp != NULL)
411 		*ap->a_vpp = ap->a_vp;
412 	if (ap->a_bnp != NULL)
413 		*ap->a_bnp = ap->a_bn;
414 	if (ap->a_runp != NULL)
415 		*ap->a_runp = 0;
416 	return (0);
417 }
418 
419 /*
420  * Device close routine
421  */
422 /* ARGSUSED */
423 int
424 fifo_close(void *v)
425 {
426 	struct vop_close_args /* {
427 		struct vnode	*a_vp;
428 		int		a_fflag;
429 		struct ucred	*a_cred;
430 		struct proc	*a_p;
431 	} */ *ap = v;
432 	struct vnode	*vp;
433 	struct fifoinfo	*fip;
434 
435 	vp = ap->a_vp;
436 	fip = vp->v_fifoinfo;
437 	if (ap->a_fflag & FREAD) {
438 		if (--fip->fi_readers == 0)
439 			socantsendmore(fip->fi_writesock);
440 	}
441 	if (ap->a_fflag & FWRITE) {
442 		if (--fip->fi_writers == 0)
443 			socantrcvmore(fip->fi_readsock);
444 	}
445 	/*
446 	 * shut down if either last close, or if close called from
447 	 * vclean()
448 	 */
449 	if ((--fip->fi_opencount == 0)
450 	    || ((ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK)) {
451 		(void) soclose(fip->fi_readsock);
452 		(void) soclose(fip->fi_writesock);
453 		FREE(fip, M_VNODE);
454 		vp->v_fifoinfo = NULL;
455 	}
456 	return (0);
457 }
458 
459 /*
460  * Print out the contents of a fifo vnode.
461  */
462 int
463 fifo_print(void *v)
464 {
465 	struct vop_print_args /* {
466 		struct vnode	*a_vp;
467 	} */ *ap = v;
468 
469 	printf("tag VT_NON");
470 	fifo_printinfo(ap->a_vp);
471 	printf("\n");
472 	return 0;
473 }
474 
475 /*
476  * Print out internal contents of a fifo vnode.
477  */
478 void
479 fifo_printinfo(struct vnode *vp)
480 {
481 	struct fifoinfo	*fip;
482 
483 	fip = vp->v_fifoinfo;
484 	printf(", fifo with %ld readers and %ld writers",
485 	    fip->fi_readers, fip->fi_writers);
486 }
487 
488 /*
489  * Return POSIX pathconf information applicable to fifo's.
490  */
491 int
492 fifo_pathconf(void *v)
493 {
494 	struct vop_pathconf_args /* {
495 		struct vnode	*a_vp;
496 		int		a_name;
497 		register_t	*a_retval;
498 	} */ *ap = v;
499 
500 	switch (ap->a_name) {
501 	case _PC_LINK_MAX:
502 		*ap->a_retval = LINK_MAX;
503 		return (0);
504 	case _PC_PIPE_BUF:
505 		*ap->a_retval = PIPE_BUF;
506 		return (0);
507 	case _PC_CHOWN_RESTRICTED:
508 		*ap->a_retval = 1;
509 		return (0);
510 	case _PC_SYNC_IO:
511 		*ap->a_retval = 1;
512 		return (0);
513 	default:
514 		return (EINVAL);
515 	}
516 	/* NOTREACHED */
517 }
518 
519 static void
520 filt_fifordetach(struct knote *kn)
521 {
522 	struct socket *so;
523 
524 	so = (struct socket *)kn->kn_hook;
525 	SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
526 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
527 		so->so_rcv.sb_flags &= ~SB_KNOTE;
528 }
529 
530 static int
531 filt_fiforead(struct knote *kn, long hint)
532 {
533 	struct socket *so;
534 
535 	so = (struct socket *)kn->kn_hook;
536 	kn->kn_data = so->so_rcv.sb_cc;
537 	if (so->so_state & SS_CANTRCVMORE) {
538 		kn->kn_flags |= EV_EOF;
539 		return (1);
540 	}
541 	kn->kn_flags &= ~EV_EOF;
542 	return (kn->kn_data > 0);
543 }
544 
545 static void
546 filt_fifowdetach(struct knote *kn)
547 {
548 	struct socket *so;
549 
550 	so = (struct socket *)kn->kn_hook;
551 	SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
552 	if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
553 		so->so_snd.sb_flags &= ~SB_KNOTE;
554 }
555 
556 static int
557 filt_fifowrite(struct knote *kn, long hint)
558 {
559 	struct socket *so;
560 
561 	so = (struct socket *)kn->kn_hook;
562 	kn->kn_data = sbspace(&so->so_snd);
563 	if (so->so_state & SS_CANTSENDMORE) {
564 		kn->kn_flags |= EV_EOF;
565 		return (1);
566 	}
567 	kn->kn_flags &= ~EV_EOF;
568 	return (kn->kn_data >= so->so_snd.sb_lowat);
569 }
570 
571 static const struct filterops fiforead_filtops =
572 	{ 1, NULL, filt_fifordetach, filt_fiforead };
573 static const struct filterops fifowrite_filtops =
574 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
575 
576 /* ARGSUSED */
577 int
578 fifo_kqfilter(void *v)
579 {
580 	struct vop_kqfilter_args /* {
581 		struct vnode *a_vp;
582 		struct knote *a_kn;
583 	} */ *ap = v;
584 	struct socket	*so;
585 	struct sockbuf	*sb;
586 
587 	so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
588 	switch (ap->a_kn->kn_filter) {
589 	case EVFILT_READ:
590 		ap->a_kn->kn_fop = &fiforead_filtops;
591 		sb = &so->so_rcv;
592 		break;
593 	case EVFILT_WRITE:
594 		ap->a_kn->kn_fop = &fifowrite_filtops;
595 		sb = &so->so_snd;
596 		break;
597 	default:
598 		return (1);
599 	}
600 
601 	ap->a_kn->kn_hook = so;
602 
603 	SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
604 	sb->sb_flags |= SB_KNOTE;
605 	return (0);
606 }
607