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