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