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