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