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