xref: /netbsd-src/sys/miscfs/fifofs/fifo_vnops.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: fifo_vnops.c,v 1.59 2007/12/05 17:19:59 pooka 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.59 2007/12/05 17:19:59 pooka 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_readers;
66 	long		fi_writers;
67 };
68 
69 int (**fifo_vnodeop_p)(void *);
70 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
71 	{ &vop_default_desc, vn_default_error },
72 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
73 	{ &vop_create_desc, fifo_create },		/* create */
74 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
75 	{ &vop_open_desc, fifo_open },			/* open */
76 	{ &vop_close_desc, fifo_close },		/* close */
77 	{ &vop_access_desc, fifo_access },		/* access */
78 	{ &vop_getattr_desc, fifo_getattr },		/* getattr */
79 	{ &vop_setattr_desc, fifo_setattr },		/* setattr */
80 	{ &vop_read_desc, fifo_read },			/* read */
81 	{ &vop_write_desc, fifo_write },		/* write */
82 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
83 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
84 	{ &vop_poll_desc, fifo_poll },			/* poll */
85 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
86 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
87 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
88 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
89 	{ &vop_seek_desc, fifo_seek },			/* seek */
90 	{ &vop_remove_desc, fifo_remove },		/* remove */
91 	{ &vop_link_desc, fifo_link },			/* link */
92 	{ &vop_rename_desc, fifo_rename },		/* rename */
93 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
94 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
95 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
96 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
97 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
98 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
99 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
100 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
101 	{ &vop_lock_desc, fifo_lock },			/* lock */
102 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
103 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
104 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
105 	{ &vop_print_desc, fifo_print },		/* print */
106 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
107 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
108 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
109 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
110 	{ &vop_putpages_desc, fifo_putpages }, 		/* putpages */
111 	{ (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
112 };
113 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
114 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
115 
116 /*
117  * Trivial lookup routine that always fails.
118  */
119 /* ARGSUSED */
120 int
121 fifo_lookup(void *v)
122 {
123 	struct vop_lookup_args /* {
124 		struct vnode		*a_dvp;
125 		struct vnode		**a_vpp;
126 		struct componentname	*a_cnp;
127 	} */ *ap = v;
128 
129 	*ap->a_vpp = NULL;
130 	return (ENOTDIR);
131 }
132 
133 /*
134  * Open called to set up a new instance of a fifo or
135  * to find an active instance of a fifo.
136  */
137 /* ARGSUSED */
138 int
139 fifo_open(void *v)
140 {
141 	struct vop_open_args /* {
142 		struct vnode	*a_vp;
143 		int		a_mode;
144 		kauth_cred_t	a_cred;
145 	} */ *ap = v;
146 	struct lwp	*l = curlwp;
147 	struct vnode	*vp;
148 	struct fifoinfo	*fip;
149 	struct proc	*p;
150 	struct socket	*rso, *wso;
151 	int		error;
152 
153 	vp = ap->a_vp;
154 	p = l->l_proc;
155 
156 	if ((fip = vp->v_fifoinfo) == NULL) {
157 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
158 		vp->v_fifoinfo = fip;
159 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
160 		if (error != 0) {
161 			free(fip, M_VNODE);
162 			vp->v_fifoinfo = NULL;
163 			return (error);
164 		}
165 		fip->fi_readsock = rso;
166 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
167 		if (error != 0) {
168 			(void)soclose(rso);
169 			free(fip, M_VNODE);
170 			vp->v_fifoinfo = NULL;
171 			return (error);
172 		}
173 		fip->fi_writesock = wso;
174 		if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
175 			(void)soclose(wso);
176 			(void)soclose(rso);
177 			free(fip, M_VNODE);
178 			vp->v_fifoinfo = NULL;
179 			return (error);
180 		}
181 		fip->fi_readers = fip->fi_writers = 0;
182 		wso->so_state |= SS_CANTRCVMORE;
183 		rso->so_state |= SS_CANTSENDMORE;
184 	}
185 	if (ap->a_mode & FREAD) {
186 		if (fip->fi_readers++ == 0) {
187 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
188 			if (fip->fi_writers > 0)
189 				wakeup(&fip->fi_writers);
190 		}
191 	}
192 	if (ap->a_mode & FWRITE) {
193 		if (fip->fi_writers++ == 0) {
194 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
195 			if (fip->fi_readers > 0)
196 				wakeup(&fip->fi_readers);
197 		}
198 	}
199 	if (ap->a_mode & FREAD) {
200 		if (ap->a_mode & O_NONBLOCK) {
201 		} else {
202 			while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
203 				VOP_UNLOCK(vp, 0);
204 				error = tsleep(&fip->fi_readers,
205 				    PCATCH | PSOCK, "fifor", 0);
206 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
207 				if (error)
208 					goto bad;
209 			}
210 		}
211 	}
212 	if (ap->a_mode & FWRITE) {
213 		if (ap->a_mode & O_NONBLOCK) {
214 			if (fip->fi_readers == 0) {
215 				error = ENXIO;
216 				goto bad;
217 			}
218 		} else {
219 			while (fip->fi_readers == 0) {
220 				VOP_UNLOCK(vp, 0);
221 				error = tsleep(&fip->fi_writers,
222 				    PCATCH | PSOCK, "fifow", 0);
223 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
224 				if (error)
225 					goto bad;
226 			}
227 		}
228 	}
229 	return (0);
230  bad:
231 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred);
232 	return (error);
233 }
234 
235 /*
236  * Vnode op for read
237  */
238 /* ARGSUSED */
239 int
240 fifo_read(void *v)
241 {
242 	struct vop_read_args /* {
243 		struct vnode	*a_vp;
244 		struct uio	*a_uio;
245 		int		a_ioflag;
246 		kauth_cred_t	a_cred;
247 	} */ *ap = v;
248 	struct uio	*uio;
249 	struct socket	*rso;
250 	int		error;
251 	size_t		startresid;
252 
253 	uio = ap->a_uio;
254 	rso = ap->a_vp->v_fifoinfo->fi_readsock;
255 #ifdef DIAGNOSTIC
256 	if (uio->uio_rw != UIO_READ)
257 		panic("fifo_read mode");
258 #endif
259 	if (uio->uio_resid == 0)
260 		return (0);
261 	if (ap->a_ioflag & IO_NDELAY)
262 		rso->so_state |= SS_NBIO;
263 	startresid = uio->uio_resid;
264 	VOP_UNLOCK(ap->a_vp, 0);
265 	error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
266 	    (struct mbuf **)0, (struct mbuf **)0, (int *)0);
267 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
268 	/*
269 	 * Clear EOF indication after first such return.
270 	 */
271 	if (uio->uio_resid == startresid)
272 		rso->so_state &= ~SS_CANTRCVMORE;
273 	if (ap->a_ioflag & IO_NDELAY) {
274 		rso->so_state &= ~SS_NBIO;
275 		if (error == EWOULDBLOCK &&
276 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
277 			error = 0;
278 	}
279 	return (error);
280 }
281 
282 /*
283  * Vnode op for write
284  */
285 /* ARGSUSED */
286 int
287 fifo_write(void *v)
288 {
289 	struct vop_write_args /* {
290 		struct vnode	*a_vp;
291 		struct uio	*a_uio;
292 		int		a_ioflag;
293 		kauth_cred_t	a_cred;
294 	} */ *ap = v;
295 	struct socket	*wso;
296 	int		error;
297 
298 	wso = ap->a_vp->v_fifoinfo->fi_writesock;
299 #ifdef DIAGNOSTIC
300 	if (ap->a_uio->uio_rw != UIO_WRITE)
301 		panic("fifo_write mode");
302 #endif
303 	if (ap->a_ioflag & IO_NDELAY)
304 		wso->so_state |= SS_NBIO;
305 	VOP_UNLOCK(ap->a_vp, 0);
306 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
307 	    (struct mbuf *)0, 0, curlwp /*XXX*/);
308 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
309 	if (ap->a_ioflag & IO_NDELAY)
310 		wso->so_state &= ~SS_NBIO;
311 	return (error);
312 }
313 
314 /*
315  * Device ioctl operation.
316  */
317 /* ARGSUSED */
318 int
319 fifo_ioctl(void *v)
320 {
321 	struct vop_ioctl_args /* {
322 		struct vnode	*a_vp;
323 		u_long		a_command;
324 		void		*a_data;
325 		int		a_fflag;
326 		kauth_cred_t	a_cred;
327 		struct lwp	*a_l;
328 	} */ *ap = v;
329 	struct file	filetmp;
330 	int		error;
331 
332 	if (ap->a_command == FIONBIO)
333 		return (0);
334 	if (ap->a_fflag & FREAD) {
335 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
336 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
337 		if (error)
338 			return (error);
339 	}
340 	if (ap->a_fflag & FWRITE) {
341 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
342 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
343 		if (error)
344 			return (error);
345 	}
346 	return (0);
347 }
348 
349 /* ARGSUSED */
350 int
351 fifo_poll(void *v)
352 {
353 	struct vop_poll_args /* {
354 		struct vnode	*a_vp;
355 		int		a_events;
356 		struct lwp	*a_l;
357 	} */ *ap = v;
358 	struct file	filetmp;
359 	int		revents;
360 
361 	revents = 0;
362 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
363 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
364 		if (filetmp.f_data)
365 			revents |= soo_poll(&filetmp, ap->a_events, curlwp);
366 	}
367 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
368 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
369 		if (filetmp.f_data)
370 			revents |= soo_poll(&filetmp, ap->a_events, curlwp);
371 	}
372 
373 	return (revents);
374 }
375 
376 int
377 fifo_inactive(void *v)
378 {
379 	struct vop_inactive_args /* {
380 		struct vnode	*a_vp;
381 		struct lwp	*a_l;
382 	} */ *ap = v;
383 
384 	VOP_UNLOCK(ap->a_vp, 0);
385 	return (0);
386 }
387 
388 /*
389  * This is a noop, simply returning what one has been given.
390  */
391 int
392 fifo_bmap(void *v)
393 {
394 	struct vop_bmap_args /* {
395 		struct vnode	*a_vp;
396 		daddr_t		a_bn;
397 		struct vnode	**a_vpp;
398 		daddr_t		*a_bnp;
399 		int		*a_runp;
400 	} */ *ap = v;
401 
402 	if (ap->a_vpp != NULL)
403 		*ap->a_vpp = ap->a_vp;
404 	if (ap->a_bnp != NULL)
405 		*ap->a_bnp = ap->a_bn;
406 	if (ap->a_runp != NULL)
407 		*ap->a_runp = 0;
408 	return (0);
409 }
410 
411 /*
412  * Device close routine
413  */
414 /* ARGSUSED */
415 int
416 fifo_close(void *v)
417 {
418 	struct vop_close_args /* {
419 		struct vnode	*a_vp;
420 		int		a_fflag;
421 		kauth_cred_t	a_cred;
422 		struct lwp	*a_l;
423 	} */ *ap = v;
424 	struct vnode	*vp;
425 	struct fifoinfo	*fip;
426 	int isrevoke;
427 
428 	vp = ap->a_vp;
429 	fip = vp->v_fifoinfo;
430 	isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
431 	if (isrevoke) {
432 		if (fip->fi_readers != 0) {
433 			fip->fi_readers = 0;
434 			socantsendmore(fip->fi_writesock);
435 		}
436 		if (fip->fi_writers != 0) {
437 			fip->fi_writers = 0;
438 			socantrcvmore(fip->fi_readsock);
439 		}
440 	} else {
441 		if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
442 			socantsendmore(fip->fi_writesock);
443 		if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
444 			socantrcvmore(fip->fi_readsock);
445 	}
446 	/* Shut down if all readers and writers are gone. */
447 	if ((fip->fi_readers + fip->fi_writers) == 0) {
448 		(void) soclose(fip->fi_readsock);
449 		(void) soclose(fip->fi_writesock);
450 		FREE(fip, M_VNODE);
451 		vp->v_fifoinfo = NULL;
452 	}
453 	return (0);
454 }
455 
456 /*
457  * Print out the contents of a fifo vnode.
458  */
459 int
460 fifo_print(void *v)
461 {
462 	struct vop_print_args /* {
463 		struct vnode	*a_vp;
464 	} */ *ap = v;
465 
466 	printf("tag VT_NON");
467 	fifo_printinfo(ap->a_vp);
468 	printf("\n");
469 	return 0;
470 }
471 
472 /*
473  * Print out internal contents of a fifo vnode.
474  */
475 void
476 fifo_printinfo(struct vnode *vp)
477 {
478 	struct fifoinfo	*fip;
479 
480 	fip = vp->v_fifoinfo;
481 	printf(", fifo with %ld readers and %ld writers",
482 	    fip->fi_readers, fip->fi_writers);
483 }
484 
485 /*
486  * Return POSIX pathconf information applicable to fifo's.
487  */
488 int
489 fifo_pathconf(void *v)
490 {
491 	struct vop_pathconf_args /* {
492 		struct vnode	*a_vp;
493 		int		a_name;
494 		register_t	*a_retval;
495 	} */ *ap = v;
496 
497 	switch (ap->a_name) {
498 	case _PC_LINK_MAX:
499 		*ap->a_retval = LINK_MAX;
500 		return (0);
501 	case _PC_PIPE_BUF:
502 		*ap->a_retval = PIPE_BUF;
503 		return (0);
504 	case _PC_CHOWN_RESTRICTED:
505 		*ap->a_retval = 1;
506 		return (0);
507 	case _PC_SYNC_IO:
508 		*ap->a_retval = 1;
509 		return (0);
510 	default:
511 		return (EINVAL);
512 	}
513 	/* NOTREACHED */
514 }
515 
516 static void
517 filt_fifordetach(struct knote *kn)
518 {
519 	struct socket *so;
520 
521 	so = (struct socket *)kn->kn_hook;
522 	SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
523 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
524 		so->so_rcv.sb_flags &= ~SB_KNOTE;
525 }
526 
527 static int
528 filt_fiforead(struct knote *kn, long hint)
529 {
530 	struct socket *so;
531 
532 	so = (struct socket *)kn->kn_hook;
533 	kn->kn_data = so->so_rcv.sb_cc;
534 	if (so->so_state & SS_CANTRCVMORE) {
535 		kn->kn_flags |= EV_EOF;
536 		return (1);
537 	}
538 	kn->kn_flags &= ~EV_EOF;
539 	return (kn->kn_data > 0);
540 }
541 
542 static void
543 filt_fifowdetach(struct knote *kn)
544 {
545 	struct socket *so;
546 
547 	so = (struct socket *)kn->kn_hook;
548 	SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
549 	if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
550 		so->so_snd.sb_flags &= ~SB_KNOTE;
551 }
552 
553 static int
554 filt_fifowrite(struct knote *kn, long hint)
555 {
556 	struct socket *so;
557 
558 	so = (struct socket *)kn->kn_hook;
559 	kn->kn_data = sbspace(&so->so_snd);
560 	if (so->so_state & SS_CANTSENDMORE) {
561 		kn->kn_flags |= EV_EOF;
562 		return (1);
563 	}
564 	kn->kn_flags &= ~EV_EOF;
565 	return (kn->kn_data >= so->so_snd.sb_lowat);
566 }
567 
568 static const struct filterops fiforead_filtops =
569 	{ 1, NULL, filt_fifordetach, filt_fiforead };
570 static const struct filterops fifowrite_filtops =
571 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
572 
573 /* ARGSUSED */
574 int
575 fifo_kqfilter(void *v)
576 {
577 	struct vop_kqfilter_args /* {
578 		struct vnode *a_vp;
579 		struct knote *a_kn;
580 	} */ *ap = v;
581 	struct socket	*so;
582 	struct sockbuf	*sb;
583 
584 	so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
585 	switch (ap->a_kn->kn_filter) {
586 	case EVFILT_READ:
587 		ap->a_kn->kn_fop = &fiforead_filtops;
588 		sb = &so->so_rcv;
589 		break;
590 	case EVFILT_WRITE:
591 		ap->a_kn->kn_fop = &fifowrite_filtops;
592 		sb = &so->so_snd;
593 		break;
594 	default:
595 		return (EINVAL);
596 	}
597 
598 	ap->a_kn->kn_hook = so;
599 
600 	SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
601 	sb->sb_flags |= SB_KNOTE;
602 	return (0);
603 }
604