Home | History | Annotate | Line # | Download | only in fifofs
fifo_vnops.c revision 1.23
      1 /*	$NetBSD: fifo_vnops.c,v 1.23 1997/05/18 12:19:29 kleink 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 		if (error == EWOULDBLOCK &&
    272 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
    273 			error = 0;
    274 	}
    275 	return (error);
    276 }
    277 
    278 /*
    279  * Vnode op for write
    280  */
    281 /* ARGSUSED */
    282 int
    283 fifo_write(v)
    284 	void *v;
    285 {
    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 = v;
    292 	struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
    293 	int error;
    294 
    295 #ifdef DIAGNOSTIC
    296 	if (ap->a_uio->uio_rw != UIO_WRITE)
    297 		panic("fifo_write mode");
    298 #endif
    299 	if (ap->a_ioflag & IO_NDELAY)
    300 		wso->so_state |= SS_NBIO;
    301 	VOP_UNLOCK(ap->a_vp);
    302 	error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
    303 	VOP_LOCK(ap->a_vp);
    304 	if (ap->a_ioflag & IO_NDELAY)
    305 		wso->so_state &= ~SS_NBIO;
    306 	return (error);
    307 }
    308 
    309 /*
    310  * Device ioctl operation.
    311  */
    312 /* ARGSUSED */
    313 int
    314 fifo_ioctl(v)
    315 	void *v;
    316 {
    317 	struct vop_ioctl_args /* {
    318 		struct vnode *a_vp;
    319 		u_long a_command;
    320 		caddr_t  a_data;
    321 		int  a_fflag;
    322 		struct ucred *a_cred;
    323 		struct proc *a_p;
    324 	} */ *ap = v;
    325 	struct file filetmp;
    326 	int error;
    327 
    328 	if (ap->a_command == FIONBIO)
    329 		return (0);
    330 	if (ap->a_fflag & FREAD) {
    331 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
    332 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
    333 		if (error)
    334 			return (error);
    335 	}
    336 	if (ap->a_fflag & FWRITE) {
    337 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
    338 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
    339 		if (error)
    340 			return (error);
    341 	}
    342 	return (0);
    343 }
    344 
    345 /* ARGSUSED */
    346 int
    347 fifo_poll(v)
    348 	void *v;
    349 {
    350 	struct vop_poll_args /* {
    351 		struct vnode *a_vp;
    352 		int a_events;
    353 		struct proc *a_p;
    354 	} */ *ap = v;
    355 	struct file filetmp;
    356 	int revents = 0;
    357 
    358 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    359 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
    360 		if (filetmp.f_data)
    361 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
    362 	}
    363 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
    364 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
    365 		if (filetmp.f_data)
    366 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
    367 	}
    368 
    369 	return (revents);
    370 }
    371 
    372 /*
    373  * This is a noop, simply returning what one has been given.
    374  */
    375 int
    376 fifo_bmap(v)
    377 	void *v;
    378 {
    379 	struct vop_bmap_args /* {
    380 		struct vnode *a_vp;
    381 		daddr_t  a_bn;
    382 		struct vnode **a_vpp;
    383 		daddr_t *a_bnp;
    384 	} */ *ap = v;
    385 
    386 	if (ap->a_vpp != NULL)
    387 		*ap->a_vpp = ap->a_vp;
    388 	if (ap->a_bnp != NULL)
    389 		*ap->a_bnp = ap->a_bn;
    390 	return (0);
    391 }
    392 
    393 /*
    394  * At the moment we do not do any locking.
    395  */
    396 /* ARGSUSED */
    397 int
    398 fifo_lock(v)
    399 	void *v;
    400 {
    401 	return (0);
    402 }
    403 
    404 /* ARGSUSED */
    405 int
    406 fifo_unlock(v)
    407 	void *v;
    408 {
    409 
    410 	return (0);
    411 }
    412 
    413 /*
    414  * Device close routine
    415  */
    416 /* ARGSUSED */
    417 int
    418 fifo_close(v)
    419 	void *v;
    420 {
    421 	struct vop_close_args /* {
    422 		struct vnode *a_vp;
    423 		int  a_fflag;
    424 		struct ucred *a_cred;
    425 		struct proc *a_p;
    426 	} */ *ap = v;
    427 	register struct vnode *vp = ap->a_vp;
    428 	register struct fifoinfo *fip = vp->v_fifoinfo;
    429 	int error1, error2;
    430 
    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(v)
    455 	void *v;
    456 {
    457 	struct vop_print_args /* {
    458 		struct vnode *a_vp;
    459 	} */ *ap = v;
    460 
    461 	printf("tag VT_NON");
    462 	fifo_printinfo(ap->a_vp);
    463 	printf("\n");
    464 	return 0;
    465 }
    466 
    467 /*
    468  * Print out internal contents of a fifo vnode.
    469  */
    470 void
    471 fifo_printinfo(vp)
    472 	struct vnode *vp;
    473 {
    474 	register struct fifoinfo *fip = vp->v_fifoinfo;
    475 
    476 	printf(", fifo with %ld readers and %ld writers",
    477 	    fip->fi_readers, fip->fi_writers);
    478 }
    479 
    480 /*
    481  * Return POSIX pathconf information applicable to fifo's.
    482  */
    483 int
    484 fifo_pathconf(v)
    485 	void *v;
    486 {
    487 	struct vop_pathconf_args /* {
    488 		struct vnode *a_vp;
    489 		int a_name;
    490 		register_t *a_retval;
    491 	} */ *ap = v;
    492 
    493 	switch (ap->a_name) {
    494 	case _PC_LINK_MAX:
    495 		*ap->a_retval = LINK_MAX;
    496 		return (0);
    497 	case _PC_PIPE_BUF:
    498 		*ap->a_retval = PIPE_BUF;
    499 		return (0);
    500 	case _PC_CHOWN_RESTRICTED:
    501 		*ap->a_retval = 1;
    502 		return (0);
    503 	default:
    504 		return (EINVAL);
    505 	}
    506 	/* NOTREACHED */
    507 }
    508