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