xref: /netbsd-src/sys/miscfs/fifofs/fifo_vnops.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: fifo_vnops.c,v 1.22 1996/10/13 02:21:29 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
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.4 (Berkeley) 8/10/94
36  */
37 
38 #include <sys/param.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/socketvar.h>
45 #include <sys/stat.h>
46 #include <sys/systm.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 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_mmap_desc, fifo_mmap },			/* mmap */
85 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
86 	{ &vop_seek_desc, fifo_seek },			/* seek */
87 	{ &vop_remove_desc, fifo_remove },		/* remove */
88 	{ &vop_link_desc, fifo_link },			/* link */
89 	{ &vop_rename_desc, fifo_rename },		/* rename */
90 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
91 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
92 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
93 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
94 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
95 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
96 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
97 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
98 	{ &vop_lock_desc, fifo_lock },			/* lock */
99 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
100 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
101 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
102 	{ &vop_print_desc, fifo_print },		/* print */
103 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
104 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
105 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
106 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
107 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
108 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
109 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
110 	{ &vop_update_desc, fifo_update },		/* update */
111 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
112 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
113 };
114 struct vnodeopv_desc fifo_vnodeop_opv_desc =
115 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
116 
117 /*
118  * Trivial lookup routine that always fails.
119  */
120 /* ARGSUSED */
121 int
122 fifo_lookup(v)
123 	void *v;
124 {
125 	struct vop_lookup_args /* {
126 		struct vnode * a_dvp;
127 		struct vnode ** a_vpp;
128 		struct componentname * a_cnp;
129 	} */ *ap = v;
130 
131 	*ap->a_vpp = NULL;
132 	return (ENOTDIR);
133 }
134 
135 /*
136  * Open called to set up a new instance of a fifo or
137  * to find an active instance of a fifo.
138  */
139 /* ARGSUSED */
140 int
141 fifo_open(v)
142 	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 	register struct vnode *vp = ap->a_vp;
151 	register struct fifoinfo *fip;
152 	struct socket *rso, *wso;
153 	int error;
154 	static char openstr[] = "fifo";
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 		if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
160 			free(fip, M_VNODE);
161 			vp->v_fifoinfo = NULL;
162 			return (error);
163 		}
164 		fip->fi_readsock = rso;
165 		if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
166 			(void)soclose(rso);
167 			free(fip, M_VNODE);
168 			vp->v_fifoinfo = NULL;
169 			return (error);
170 		}
171 		fip->fi_writesock = wso;
172 		if ((error = unp_connect2(wso, rso)) != 0) {
173 			(void)soclose(wso);
174 			(void)soclose(rso);
175 			free(fip, M_VNODE);
176 			vp->v_fifoinfo = NULL;
177 			return (error);
178 		}
179 		fip->fi_readers = fip->fi_writers = 0;
180 		wso->so_state |= SS_CANTRCVMORE;
181 		rso->so_state |= SS_CANTSENDMORE;
182 	}
183 	if (ap->a_mode & FREAD) {
184 		if (fip->fi_readers++ == 0) {
185 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
186 			if (fip->fi_writers > 0)
187 				wakeup((caddr_t)&fip->fi_writers);
188 		}
189 	}
190 	if (ap->a_mode & FWRITE) {
191 		if (fip->fi_writers++ == 0) {
192 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
193 			if (fip->fi_readers > 0)
194 				wakeup((caddr_t)&fip->fi_readers);
195 		}
196 	}
197 	if (ap->a_mode & FREAD) {
198 		if (ap->a_mode & O_NONBLOCK) {
199 		} else {
200 			while (fip->fi_writers == 0) {
201 				VOP_UNLOCK(vp);
202 				error = tsleep((caddr_t)&fip->fi_readers,
203 				    PCATCH | PSOCK, openstr, 0);
204 				VOP_LOCK(vp);
205 				if (error)
206 					goto bad;
207 			}
208 		}
209 	}
210 	if (ap->a_mode & FWRITE) {
211 		if (ap->a_mode & O_NONBLOCK) {
212 			if (fip->fi_readers == 0) {
213 				error = ENXIO;
214 				goto bad;
215 			}
216 		} else {
217 			while (fip->fi_readers == 0) {
218 				VOP_UNLOCK(vp);
219 				error = tsleep((caddr_t)&fip->fi_writers,
220 				    PCATCH | PSOCK, openstr, 0);
221 				VOP_LOCK(vp);
222 				if (error)
223 					goto bad;
224 			}
225 		}
226 	}
227 	return (0);
228 bad:
229 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p);
230 	return (error);
231 }
232 
233 /*
234  * Vnode op for read
235  */
236 /* ARGSUSED */
237 int
238 fifo_read(v)
239 	void *v;
240 {
241 	struct vop_read_args /* {
242 		struct vnode *a_vp;
243 		struct uio *a_uio;
244 		int  a_ioflag;
245 		struct ucred *a_cred;
246 	} */ *ap = v;
247 	register struct uio *uio = ap->a_uio;
248 	register struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
249 	int error, startresid;
250 
251 #ifdef DIAGNOSTIC
252 	if (uio->uio_rw != UIO_READ)
253 		panic("fifo_read mode");
254 #endif
255 	if (uio->uio_resid == 0)
256 		return (0);
257 	if (ap->a_ioflag & IO_NDELAY)
258 		rso->so_state |= SS_NBIO;
259 	startresid = uio->uio_resid;
260 	VOP_UNLOCK(ap->a_vp);
261 	error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
262 	    (struct mbuf **)0, (int *)0);
263 	VOP_LOCK(ap->a_vp);
264 	/*
265 	 * Clear EOF indication after first such return.
266 	 */
267 	if (uio->uio_resid == startresid)
268 		rso->so_state &= ~SS_CANTRCVMORE;
269 	if (ap->a_ioflag & IO_NDELAY)
270 		rso->so_state &= ~SS_NBIO;
271 	return (error);
272 }
273 
274 /*
275  * Vnode op for write
276  */
277 /* ARGSUSED */
278 int
279 fifo_write(v)
280 	void *v;
281 {
282 	struct vop_write_args /* {
283 		struct vnode *a_vp;
284 		struct uio *a_uio;
285 		int  a_ioflag;
286 		struct ucred *a_cred;
287 	} */ *ap = v;
288 	struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
289 	int error;
290 
291 #ifdef DIAGNOSTIC
292 	if (ap->a_uio->uio_rw != UIO_WRITE)
293 		panic("fifo_write mode");
294 #endif
295 	if (ap->a_ioflag & IO_NDELAY)
296 		wso->so_state |= SS_NBIO;
297 	VOP_UNLOCK(ap->a_vp);
298 	error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
299 	VOP_LOCK(ap->a_vp);
300 	if (ap->a_ioflag & IO_NDELAY)
301 		wso->so_state &= ~SS_NBIO;
302 	return (error);
303 }
304 
305 /*
306  * Device ioctl operation.
307  */
308 /* ARGSUSED */
309 int
310 fifo_ioctl(v)
311 	void *v;
312 {
313 	struct vop_ioctl_args /* {
314 		struct vnode *a_vp;
315 		u_long a_command;
316 		caddr_t  a_data;
317 		int  a_fflag;
318 		struct ucred *a_cred;
319 		struct proc *a_p;
320 	} */ *ap = v;
321 	struct file filetmp;
322 	int error;
323 
324 	if (ap->a_command == FIONBIO)
325 		return (0);
326 	if (ap->a_fflag & FREAD) {
327 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
328 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
329 		if (error)
330 			return (error);
331 	}
332 	if (ap->a_fflag & FWRITE) {
333 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
334 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
335 		if (error)
336 			return (error);
337 	}
338 	return (0);
339 }
340 
341 /* ARGSUSED */
342 int
343 fifo_poll(v)
344 	void *v;
345 {
346 	struct vop_poll_args /* {
347 		struct vnode *a_vp;
348 		int a_events;
349 		struct proc *a_p;
350 	} */ *ap = v;
351 	struct file filetmp;
352 	int revents = 0;
353 
354 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
355 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
356 		if (filetmp.f_data)
357 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
358 	}
359 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
360 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
361 		if (filetmp.f_data)
362 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
363 	}
364 
365 	return (revents);
366 }
367 
368 /*
369  * This is a noop, simply returning what one has been given.
370  */
371 int
372 fifo_bmap(v)
373 	void *v;
374 {
375 	struct vop_bmap_args /* {
376 		struct vnode *a_vp;
377 		daddr_t  a_bn;
378 		struct vnode **a_vpp;
379 		daddr_t *a_bnp;
380 	} */ *ap = v;
381 
382 	if (ap->a_vpp != NULL)
383 		*ap->a_vpp = ap->a_vp;
384 	if (ap->a_bnp != NULL)
385 		*ap->a_bnp = ap->a_bn;
386 	return (0);
387 }
388 
389 /*
390  * At the moment we do not do any locking.
391  */
392 /* ARGSUSED */
393 int
394 fifo_lock(v)
395 	void *v;
396 {
397 	return (0);
398 }
399 
400 /* ARGSUSED */
401 int
402 fifo_unlock(v)
403 	void *v;
404 {
405 
406 	return (0);
407 }
408 
409 /*
410  * Device close routine
411  */
412 /* ARGSUSED */
413 int
414 fifo_close(v)
415 	void *v;
416 {
417 	struct vop_close_args /* {
418 		struct vnode *a_vp;
419 		int  a_fflag;
420 		struct ucred *a_cred;
421 		struct proc *a_p;
422 	} */ *ap = v;
423 	register struct vnode *vp = ap->a_vp;
424 	register struct fifoinfo *fip = vp->v_fifoinfo;
425 	int error1, error2;
426 
427 	if (ap->a_fflag & FREAD) {
428 		if (--fip->fi_readers == 0)
429 			socantsendmore(fip->fi_writesock);
430 	}
431 	if (ap->a_fflag & FWRITE) {
432 		if (--fip->fi_writers == 0)
433 			socantrcvmore(fip->fi_readsock);
434 	}
435 	if (vp->v_usecount > 1)
436 		return (0);
437 	error1 = soclose(fip->fi_readsock);
438 	error2 = soclose(fip->fi_writesock);
439 	FREE(fip, M_VNODE);
440 	vp->v_fifoinfo = NULL;
441 	if (error1)
442 		return (error1);
443 	return (error2);
444 }
445 
446 /*
447  * Print out the contents of a fifo vnode.
448  */
449 int
450 fifo_print(v)
451 	void *v;
452 {
453 	struct vop_print_args /* {
454 		struct vnode *a_vp;
455 	} */ *ap = v;
456 
457 	printf("tag VT_NON");
458 	fifo_printinfo(ap->a_vp);
459 	printf("\n");
460 	return 0;
461 }
462 
463 /*
464  * Print out internal contents of a fifo vnode.
465  */
466 void
467 fifo_printinfo(vp)
468 	struct vnode *vp;
469 {
470 	register struct fifoinfo *fip = vp->v_fifoinfo;
471 
472 	printf(", fifo with %ld readers and %ld writers",
473 	    fip->fi_readers, fip->fi_writers);
474 }
475 
476 /*
477  * Return POSIX pathconf information applicable to fifo's.
478  */
479 int
480 fifo_pathconf(v)
481 	void *v;
482 {
483 	struct vop_pathconf_args /* {
484 		struct vnode *a_vp;
485 		int a_name;
486 		register_t *a_retval;
487 	} */ *ap = v;
488 
489 	switch (ap->a_name) {
490 	case _PC_LINK_MAX:
491 		*ap->a_retval = LINK_MAX;
492 		return (0);
493 	case _PC_PIPE_BUF:
494 		*ap->a_retval = PIPE_BUF;
495 		return (0);
496 	case _PC_CHOWN_RESTRICTED:
497 		*ap->a_retval = 1;
498 		return (0);
499 	default:
500 		return (EINVAL);
501 	}
502 	/* NOTREACHED */
503 }
504