Home | History | Annotate | Line # | Download | only in fifofs
fifo_vnops.c revision 1.30
      1 /*	$NetBSD: fifo_vnops.c,v 1.30 2001/02/27 19:52:21 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990, 1993, 1995
      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.10 (Berkeley) 5/27/95
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/proc.h>
     41 #include <sys/time.h>
     42 #include <sys/namei.h>
     43 #include <sys/vnode.h>
     44 #include <sys/socket.h>
     45 #include <sys/socketvar.h>
     46 #include <sys/stat.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 const 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_revoke_desc, fifo_revoke },		/* revoke */
     85 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
     86 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
     87 	{ &vop_seek_desc, fifo_seek },			/* seek */
     88 	{ &vop_remove_desc, fifo_remove },		/* remove */
     89 	{ &vop_link_desc, fifo_link },			/* link */
     90 	{ &vop_rename_desc, fifo_rename },		/* rename */
     91 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
     92 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
     93 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
     94 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
     95 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
     96 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
     97 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
     98 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
     99 	{ &vop_lock_desc, fifo_lock },			/* lock */
    100 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
    101 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    102 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    103 	{ &vop_print_desc, fifo_print },		/* print */
    104 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
    105 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    106 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    107 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
    108 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
    109 	{ &vop_vfree_desc, fifo_vfree },		/* vfree */
    110 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
    111 	{ &vop_update_desc, fifo_update },		/* update */
    112 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
    113 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    114 };
    115 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
    116 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
    117 
    118 /*
    119  * Trivial lookup routine that always fails.
    120  */
    121 /* ARGSUSED */
    122 int
    123 fifo_lookup(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(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 	struct vnode	*vp;
    150 	struct fifoinfo	*fip;
    151 	struct proc	*p;
    152 	struct socket	*rso, *wso;
    153 	int		error;
    154 	static const char openstr[] = "fifo";
    155 
    156 	vp = ap->a_vp;
    157 	p = ap->a_p;
    158 
    159 	if ((fip = vp->v_fifoinfo) == NULL) {
    160 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
    161 		vp->v_fifoinfo = fip;
    162 		if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
    163 			free(fip, M_VNODE);
    164 			vp->v_fifoinfo = NULL;
    165 			return (error);
    166 		}
    167 		fip->fi_readsock = rso;
    168 		if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
    169 			(void)soclose(rso);
    170 			free(fip, M_VNODE);
    171 			vp->v_fifoinfo = NULL;
    172 			return (error);
    173 		}
    174 		fip->fi_writesock = wso;
    175 		if ((error = unp_connect2(wso, rso)) != 0) {
    176 			(void)soclose(wso);
    177 			(void)soclose(rso);
    178 			free(fip, M_VNODE);
    179 			vp->v_fifoinfo = NULL;
    180 			return (error);
    181 		}
    182 		fip->fi_readers = fip->fi_writers = 0;
    183 		wso->so_state |= SS_CANTRCVMORE;
    184 		rso->so_state |= SS_CANTSENDMORE;
    185 	}
    186 	if (ap->a_mode & FREAD) {
    187 		if (fip->fi_readers++ == 0) {
    188 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
    189 			if (fip->fi_writers > 0)
    190 				wakeup((caddr_t)&fip->fi_writers);
    191 		}
    192 	}
    193 	if (ap->a_mode & FWRITE) {
    194 		if (fip->fi_writers++ == 0) {
    195 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
    196 			if (fip->fi_readers > 0)
    197 				wakeup((caddr_t)&fip->fi_readers);
    198 		}
    199 	}
    200 	if (ap->a_mode & FREAD) {
    201 		if (ap->a_mode & O_NONBLOCK) {
    202 		} else {
    203 			while (fip->fi_writers == 0) {
    204 				VOP_UNLOCK(vp, 0);
    205 				error = tsleep((caddr_t)&fip->fi_readers,
    206 				    PCATCH | PSOCK, openstr, 0);
    207 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    208 				if (error)
    209 					goto bad;
    210 			}
    211 		}
    212 	}
    213 	if (ap->a_mode & FWRITE) {
    214 		if (ap->a_mode & O_NONBLOCK) {
    215 			if (fip->fi_readers == 0) {
    216 				error = ENXIO;
    217 				goto bad;
    218 			}
    219 		} else {
    220 			while (fip->fi_readers == 0) {
    221 				VOP_UNLOCK(vp, 0);
    222 				error = tsleep((caddr_t)&fip->fi_writers,
    223 				    PCATCH | PSOCK, openstr, 0);
    224 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    225 				if (error)
    226 					goto bad;
    227 			}
    228 		}
    229 	}
    230 	return (0);
    231  bad:
    232 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
    233 	return (error);
    234 }
    235 
    236 /*
    237  * Vnode op for read
    238  */
    239 /* ARGSUSED */
    240 int
    241 fifo_read(void *v)
    242 {
    243 	struct vop_read_args /* {
    244 		struct vnode	*a_vp;
    245 		struct uio	*a_uio;
    246 		int		a_ioflag;
    247 		struct ucred	*a_cred;
    248 	} */ *ap = v;
    249 	struct uio	*uio;
    250 	struct socket	*rso;
    251 	int		error, startresid;
    252 
    253 	uio = ap->a_uio;
    254 	rso = ap->a_vp->v_fifoinfo->fi_readsock;
    255 #ifdef DIAGNOSTIC
    256 	if (uio->uio_rw != UIO_READ)
    257 		panic("fifo_read mode");
    258 #endif
    259 	if (uio->uio_resid == 0)
    260 		return (0);
    261 	if (ap->a_ioflag & IO_NDELAY)
    262 		rso->so_state |= SS_NBIO;
    263 	startresid = uio->uio_resid;
    264 	VOP_UNLOCK(ap->a_vp, 0);
    265 	error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
    266 	    (struct mbuf **)0, (struct mbuf **)0, (int *)0);
    267 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    268 	/*
    269 	 * Clear EOF indication after first such return.
    270 	 */
    271 	if (uio->uio_resid == startresid)
    272 		rso->so_state &= ~SS_CANTRCVMORE;
    273 	if (ap->a_ioflag & IO_NDELAY) {
    274 		rso->so_state &= ~SS_NBIO;
    275 		if (error == EWOULDBLOCK &&
    276 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
    277 			error = 0;
    278 	}
    279 	return (error);
    280 }
    281 
    282 /*
    283  * Vnode op for write
    284  */
    285 /* ARGSUSED */
    286 int
    287 fifo_write(void *v)
    288 {
    289 	struct vop_write_args /* {
    290 		struct vnode	*a_vp;
    291 		struct uio	*a_uio;
    292 		int		a_ioflag;
    293 		struct ucred	*a_cred;
    294 	} */ *ap = v;
    295 	struct socket	*wso;
    296 	int		error;
    297 
    298 	wso = ap->a_vp->v_fifoinfo->fi_writesock;
    299 #ifdef DIAGNOSTIC
    300 	if (ap->a_uio->uio_rw != UIO_WRITE)
    301 		panic("fifo_write mode");
    302 #endif
    303 	if (ap->a_ioflag & IO_NDELAY)
    304 		wso->so_state |= SS_NBIO;
    305 	VOP_UNLOCK(ap->a_vp, 0);
    306 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
    307 	    (struct mbuf *)0, 0);
    308 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    309 	if (ap->a_ioflag & IO_NDELAY)
    310 		wso->so_state &= ~SS_NBIO;
    311 	return (error);
    312 }
    313 
    314 /*
    315  * Device ioctl operation.
    316  */
    317 /* ARGSUSED */
    318 int
    319 fifo_ioctl(void *v)
    320 {
    321 	struct vop_ioctl_args /* {
    322 		struct vnode	*a_vp;
    323 		u_long		a_command;
    324 		caddr_t		a_data;
    325 		int		a_fflag;
    326 		struct ucred	*a_cred;
    327 		struct proc	*a_p;
    328 	} */ *ap = v;
    329 	struct file	filetmp;
    330 	int		error;
    331 
    332 	if (ap->a_command == FIONBIO)
    333 		return (0);
    334 	if (ap->a_fflag & FREAD) {
    335 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
    336 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
    337 		if (error)
    338 			return (error);
    339 	}
    340 	if (ap->a_fflag & FWRITE) {
    341 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
    342 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
    343 		if (error)
    344 			return (error);
    345 	}
    346 	return (0);
    347 }
    348 
    349 /* ARGSUSED */
    350 int
    351 fifo_poll(void *v)
    352 {
    353 	struct vop_poll_args /* {
    354 		struct vnode	*a_vp;
    355 		int		a_events;
    356 		struct proc	*a_p;
    357 	} */ *ap = v;
    358 	struct file	filetmp;
    359 	int		revents;
    360 
    361 	revents = 0;
    362 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    363 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
    364 		if (filetmp.f_data)
    365 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
    366 	}
    367 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
    368 		filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
    369 		if (filetmp.f_data)
    370 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
    371 	}
    372 
    373 	return (revents);
    374 }
    375 
    376 int
    377 fifo_inactive(void *v)
    378 {
    379 	struct vop_inactive_args /* {
    380 		struct vnode	*a_vp;
    381 		struct proc	*a_p;
    382 	} */ *ap = v;
    383 
    384 	VOP_UNLOCK(ap->a_vp, 0);
    385 	return (0);
    386 }
    387 
    388 /*
    389  * This is a noop, simply returning what one has been given.
    390  */
    391 int
    392 fifo_bmap(void *v)
    393 {
    394 	struct vop_bmap_args /* {
    395 		struct vnode	*a_vp;
    396 		daddr_t		a_bn;
    397 		struct vnode	**a_vpp;
    398 		daddr_t		*a_bnp;
    399 		int		*a_runp;
    400 	} */ *ap = v;
    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 	return (0);
    409 }
    410 
    411 /*
    412  * Device close routine
    413  */
    414 /* ARGSUSED */
    415 int
    416 fifo_close(void *v)
    417 {
    418 	struct vop_close_args /* {
    419 		struct vnode	*a_vp;
    420 		int		a_fflag;
    421 		struct ucred	*a_cred;
    422 		struct proc	*a_p;
    423 	} */ *ap = v;
    424 	struct vnode	*vp;
    425 	struct fifoinfo	*fip;
    426 	int		error1, error2;
    427 
    428 	vp = ap->a_vp;
    429 	fip = vp->v_fifoinfo;
    430 	if (ap->a_fflag & FREAD) {
    431 		if (--fip->fi_readers == 0)
    432 			socantsendmore(fip->fi_writesock);
    433 	}
    434 	if (ap->a_fflag & FWRITE) {
    435 		if (--fip->fi_writers == 0)
    436 			socantrcvmore(fip->fi_readsock);
    437 	}
    438 	if (vp->v_usecount > 1)
    439 		return (0);
    440 	error1 = soclose(fip->fi_readsock);
    441 	error2 = soclose(fip->fi_writesock);
    442 	FREE(fip, M_VNODE);
    443 	vp->v_fifoinfo = NULL;
    444 	if (error1)
    445 		return (error1);
    446 	return (error2);
    447 }
    448 
    449 /*
    450  * Print out the contents of a fifo vnode.
    451  */
    452 int
    453 fifo_print(void *v)
    454 {
    455 	struct vop_print_args /* {
    456 		struct vnode	*a_vp;
    457 	} */ *ap = v;
    458 
    459 	printf("tag VT_NON");
    460 	fifo_printinfo(ap->a_vp);
    461 	printf("\n");
    462 	return 0;
    463 }
    464 
    465 /*
    466  * Print out internal contents of a fifo vnode.
    467  */
    468 void
    469 fifo_printinfo(struct vnode *vp)
    470 {
    471 	struct fifoinfo	*fip;
    472 
    473 	fip = vp->v_fifoinfo;
    474 	printf(", fifo with %ld readers and %ld writers",
    475 	    fip->fi_readers, fip->fi_writers);
    476 }
    477 
    478 /*
    479  * Return POSIX pathconf information applicable to fifo's.
    480  */
    481 int
    482 fifo_pathconf(void *v)
    483 {
    484 	struct vop_pathconf_args /* {
    485 		struct vnode	*a_vp;
    486 		int		a_name;
    487 		register_t	*a_retval;
    488 	} */ *ap = v;
    489 
    490 	switch (ap->a_name) {
    491 	case _PC_LINK_MAX:
    492 		*ap->a_retval = LINK_MAX;
    493 		return (0);
    494 	case _PC_PIPE_BUF:
    495 		*ap->a_retval = PIPE_BUF;
    496 		return (0);
    497 	case _PC_CHOWN_RESTRICTED:
    498 		*ap->a_retval = 1;
    499 		return (0);
    500 	case _PC_SYNC_IO:
    501 		*ap->a_retval = 1;
    502 		return (0);
    503 	default:
    504 		return (EINVAL);
    505 	}
    506 	/* NOTREACHED */
    507 }
    508