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