Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.87
      1 /*	$NetBSD: spec_vnops.c,v 1.87 2006/05/14 21:32:21 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.87 2006/05/14 21:32:21 elad Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/proc.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/conf.h>
     42 #include <sys/buf.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/stat.h>
     47 #include <sys/errno.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/poll.h>
     50 #include <sys/file.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/lockf.h>
     53 #include <sys/tty.h>
     54 #include <sys/kauth.h>
     55 
     56 #include <miscfs/genfs/genfs.h>
     57 #include <miscfs/specfs/specdev.h>
     58 
     59 /* symbolic sleep message strings for devices */
     60 const char	devopn[] = "devopn";
     61 const char	devio[] = "devio";
     62 const char	devwait[] = "devwait";
     63 const char	devin[] = "devin";
     64 const char	devout[] = "devout";
     65 const char	devioc[] = "devioc";
     66 const char	devcls[] = "devcls";
     67 
     68 struct vnode	*speclisth[SPECHSZ];
     69 
     70 /*
     71  * This vnode operations vector is used for two things only:
     72  * - special device nodes created from whole cloth by the kernel.
     73  * - as a temporary vnodeops replacement for vnodes which were found to
     74  *	be aliased by callers of checkalias().
     75  * For the ops vector for vnodes built from special devices found in a
     76  * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
     77  * equivalent for other filesystems.
     78  */
     79 
     80 int (**spec_vnodeop_p)(void *);
     81 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
     82 	{ &vop_default_desc, vn_default_error },
     83 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
     84 	{ &vop_create_desc, spec_create },		/* create */
     85 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
     86 	{ &vop_open_desc, spec_open },			/* open */
     87 	{ &vop_close_desc, spec_close },		/* close */
     88 	{ &vop_access_desc, spec_access },		/* access */
     89 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
     90 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
     91 	{ &vop_read_desc, spec_read },			/* read */
     92 	{ &vop_write_desc, spec_write },		/* write */
     93 	{ &vop_lease_desc, spec_lease_check },		/* lease */
     94 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
     95 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
     96 	{ &vop_poll_desc, spec_poll },			/* poll */
     97 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
     98 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
     99 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    100 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    101 	{ &vop_seek_desc, spec_seek },			/* seek */
    102 	{ &vop_remove_desc, spec_remove },		/* remove */
    103 	{ &vop_link_desc, spec_link },			/* link */
    104 	{ &vop_rename_desc, spec_rename },		/* rename */
    105 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    106 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    107 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    108 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    109 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    110 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    111 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    112 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
    113 	{ &vop_lock_desc, spec_lock },			/* lock */
    114 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
    115 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    116 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    117 	{ &vop_print_desc, spec_print },		/* print */
    118 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    119 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    120 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    121 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    122 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    123 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    124 	{ NULL, NULL }
    125 };
    126 const struct vnodeopv_desc spec_vnodeop_opv_desc =
    127 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    128 
    129 /*
    130  * Trivial lookup routine that always fails.
    131  */
    132 int
    133 spec_lookup(v)
    134 	void *v;
    135 {
    136 	struct vop_lookup_args /* {
    137 		struct vnode *a_dvp;
    138 		struct vnode **a_vpp;
    139 		struct componentname *a_cnp;
    140 	} */ *ap = v;
    141 
    142 	*ap->a_vpp = NULL;
    143 	return (ENOTDIR);
    144 }
    145 
    146 /*
    147  * Returns true if dev is /dev/mem or /dev/kmem.
    148  */
    149 static int
    150 iskmemdev(dev_t dev)
    151 {
    152 	/* mem_no is emitted by config(8) to generated devsw.c */
    153 	extern const int mem_no;
    154 
    155 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
    156 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
    157 }
    158 
    159 /*
    160  * Open a special file.
    161  */
    162 /* ARGSUSED */
    163 int
    164 spec_open(v)
    165 	void *v;
    166 {
    167 	struct vop_open_args /* {
    168 		struct vnode *a_vp;
    169 		int  a_mode;
    170 		kauth_cred_t a_cred;
    171 		struct lwp *a_l;
    172 	} */ *ap = v;
    173 	struct lwp *l = ap->a_l;
    174 	struct vnode *bvp, *vp = ap->a_vp;
    175 	const struct bdevsw *bdev;
    176 	const struct cdevsw *cdev;
    177 	dev_t blkdev, dev = (dev_t)vp->v_rdev;
    178 	int error;
    179 	struct partinfo pi;
    180 	int (*d_ioctl)(dev_t, u_long, caddr_t, int, struct lwp *);
    181 
    182 	/*
    183 	 * Don't allow open if fs is mounted -nodev.
    184 	 */
    185 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    186 		return (ENXIO);
    187 
    188 	switch (vp->v_type) {
    189 
    190 	case VCHR:
    191 		cdev = cdevsw_lookup(dev);
    192 		if (cdev == NULL)
    193 			return (ENXIO);
    194 		if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
    195 			/*
    196 			 * When running in very secure mode, do not allow
    197 			 * opens for writing of any disk character devices.
    198 			 */
    199 			if (securelevel >= 2 && cdev->d_type == D_DISK)
    200 				return (EPERM);
    201 			/*
    202 			 * When running in secure mode, do not allow opens
    203 			 * for writing of /dev/mem, /dev/kmem, or character
    204 			 * devices whose corresponding block devices are
    205 			 * currently mounted.
    206 			 */
    207 			if (securelevel >= 1) {
    208 				blkdev = devsw_chr2blk(dev);
    209 				if (blkdev != (dev_t)NODEV &&
    210 				    vfinddev(blkdev, VBLK, &bvp) &&
    211 				    (error = vfs_mountedon(bvp)))
    212 					return (error);
    213 				if (iskmemdev(dev))
    214 					return (EPERM);
    215 			}
    216 		}
    217 		if (cdev->d_type == D_TTY)
    218 			vp->v_flag |= VISTTY;
    219 		VOP_UNLOCK(vp, 0);
    220 		error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, l);
    221 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    222 		if (cdev->d_type != D_DISK)
    223 			return error;
    224 		d_ioctl = cdev->d_ioctl;
    225 		break;
    226 
    227 	case VBLK:
    228 		bdev = bdevsw_lookup(dev);
    229 		if (bdev == NULL)
    230 			return (ENXIO);
    231 		/*
    232 		 * When running in very secure mode, do not allow
    233 		 * opens for writing of any disk block devices.
    234 		 */
    235 		if (securelevel >= 2 && ap->a_cred != FSCRED &&
    236 		    (ap->a_mode & FWRITE) && bdev->d_type == D_DISK)
    237 			return (EPERM);
    238 		/*
    239 		 * Do not allow opens of block devices that are
    240 		 * currently mounted.
    241 		 */
    242 		if ((error = vfs_mountedon(vp)) != 0)
    243 			return (error);
    244 		error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, l);
    245 		d_ioctl = bdev->d_ioctl;
    246 		break;
    247 
    248 	case VNON:
    249 	case VLNK:
    250 	case VDIR:
    251 	case VREG:
    252 	case VBAD:
    253 	case VFIFO:
    254 	case VSOCK:
    255 	default:
    256 		return 0;
    257 	}
    258 
    259 	if (error)
    260 		return error;
    261 	if (!(*d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&pi, FREAD, curlwp))
    262 		vp->v_size = (voff_t)pi.disklab->d_secsize * pi.part->p_size;
    263 	return 0;
    264 }
    265 
    266 /*
    267  * Vnode op for read
    268  */
    269 /* ARGSUSED */
    270 int
    271 spec_read(v)
    272 	void *v;
    273 {
    274 	struct vop_read_args /* {
    275 		struct vnode *a_vp;
    276 		struct uio *a_uio;
    277 		int  a_ioflag;
    278 		kauth_cred_t a_cred;
    279 	} */ *ap = v;
    280 	struct vnode *vp = ap->a_vp;
    281 	struct uio *uio = ap->a_uio;
    282  	struct lwp *l = curlwp;
    283 	struct buf *bp;
    284 	const struct bdevsw *bdev;
    285 	const struct cdevsw *cdev;
    286 	daddr_t bn;
    287 	int bsize, bscale;
    288 	struct partinfo dpart;
    289 	int n, on;
    290 	int error = 0;
    291 
    292 #ifdef DIAGNOSTIC
    293 	if (uio->uio_rw != UIO_READ)
    294 		panic("spec_read mode");
    295 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    296 	    uio->uio_vmspace != curproc->p_vmspace)
    297 		panic("spec_read proc");
    298 #endif
    299 	if (uio->uio_resid == 0)
    300 		return (0);
    301 
    302 	switch (vp->v_type) {
    303 
    304 	case VCHR:
    305 		VOP_UNLOCK(vp, 0);
    306 		cdev = cdevsw_lookup(vp->v_rdev);
    307 		if (cdev != NULL)
    308 			error = (*cdev->d_read)(vp->v_rdev, uio, ap->a_ioflag);
    309 		else
    310 			error = ENXIO;
    311 		vn_lock(vp, LK_SHARED | LK_RETRY);
    312 		return (error);
    313 
    314 	case VBLK:
    315 		if (uio->uio_offset < 0)
    316 			return (EINVAL);
    317 		bsize = BLKDEV_IOSIZE;
    318 		bdev = bdevsw_lookup(vp->v_rdev);
    319 		if (bdev != NULL &&
    320 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
    321 				     FREAD, l) == 0) {
    322 			if (dpart.part->p_fstype == FS_BSDFFS &&
    323 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    324 				bsize = dpart.part->p_frag *
    325 				    dpart.part->p_fsize;
    326 		}
    327 		bscale = bsize >> DEV_BSHIFT;
    328 		do {
    329 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    330 			on = uio->uio_offset % bsize;
    331 			n = min((unsigned)(bsize - on), uio->uio_resid);
    332 			error = bread(vp, bn, bsize, NOCRED, &bp);
    333 			n = min(n, bsize - bp->b_resid);
    334 			if (error) {
    335 				brelse(bp);
    336 				return (error);
    337 			}
    338 			error = uiomove((char *)bp->b_data + on, n, uio);
    339 			brelse(bp);
    340 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    341 		return (error);
    342 
    343 	default:
    344 		panic("spec_read type");
    345 	}
    346 	/* NOTREACHED */
    347 }
    348 
    349 /*
    350  * Vnode op for write
    351  */
    352 /* ARGSUSED */
    353 int
    354 spec_write(v)
    355 	void *v;
    356 {
    357 	struct vop_write_args /* {
    358 		struct vnode *a_vp;
    359 		struct uio *a_uio;
    360 		int  a_ioflag;
    361 		kauth_cred_t a_cred;
    362 	} */ *ap = v;
    363 	struct vnode *vp = ap->a_vp;
    364 	struct uio *uio = ap->a_uio;
    365 	struct lwp *l = curlwp;
    366 	struct buf *bp;
    367 	const struct bdevsw *bdev;
    368 	const struct cdevsw *cdev;
    369 	daddr_t bn;
    370 	int bsize, bscale;
    371 	struct partinfo dpart;
    372 	int n, on;
    373 	int error = 0;
    374 
    375 #ifdef DIAGNOSTIC
    376 	if (uio->uio_rw != UIO_WRITE)
    377 		panic("spec_write mode");
    378 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    379 	    uio->uio_vmspace != curproc->p_vmspace)
    380 		panic("spec_write proc");
    381 #endif
    382 
    383 	switch (vp->v_type) {
    384 
    385 	case VCHR:
    386 		VOP_UNLOCK(vp, 0);
    387 		cdev = cdevsw_lookup(vp->v_rdev);
    388 		if (cdev != NULL)
    389 			error = (*cdev->d_write)(vp->v_rdev, uio, ap->a_ioflag);
    390 		else
    391 			error = ENXIO;
    392 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    393 		return (error);
    394 
    395 	case VBLK:
    396 		if (uio->uio_resid == 0)
    397 			return (0);
    398 		if (uio->uio_offset < 0)
    399 			return (EINVAL);
    400 		bsize = BLKDEV_IOSIZE;
    401 		bdev = bdevsw_lookup(vp->v_rdev);
    402 		if (bdev != NULL &&
    403 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
    404 				    FREAD, l) == 0) {
    405 			if (dpart.part->p_fstype == FS_BSDFFS &&
    406 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    407 				bsize = dpart.part->p_frag *
    408 				    dpart.part->p_fsize;
    409 		}
    410 		bscale = bsize >> DEV_BSHIFT;
    411 		do {
    412 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    413 			on = uio->uio_offset % bsize;
    414 			n = min((unsigned)(bsize - on), uio->uio_resid);
    415 			if (n == bsize)
    416 				bp = getblk(vp, bn, bsize, 0, 0);
    417 			else
    418 				error = bread(vp, bn, bsize, NOCRED, &bp);
    419 			if (error) {
    420 				brelse(bp);
    421 				return (error);
    422 			}
    423 			n = min(n, bsize - bp->b_resid);
    424 			error = uiomove((char *)bp->b_data + on, n, uio);
    425 			if (error)
    426 				brelse(bp);
    427 			else {
    428 				if (n + on == bsize)
    429 					bawrite(bp);
    430 				else
    431 					bdwrite(bp);
    432 				if (bp->b_flags & B_ERROR)
    433 					error = bp->b_error;
    434 			}
    435 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    436 		return (error);
    437 
    438 	default:
    439 		panic("spec_write type");
    440 	}
    441 	/* NOTREACHED */
    442 }
    443 
    444 /*
    445  * Device ioctl operation.
    446  */
    447 /* ARGSUSED */
    448 int
    449 spec_ioctl(v)
    450 	void *v;
    451 {
    452 	struct vop_ioctl_args /* {
    453 		struct vnode *a_vp;
    454 		u_long a_command;
    455 		void  *a_data;
    456 		int  a_fflag;
    457 		kauth_cred_t a_cred;
    458 		struct lwp *a_l;
    459 	} */ *ap = v;
    460 	const struct bdevsw *bdev;
    461 	const struct cdevsw *cdev;
    462 	struct vnode *vp;
    463 	dev_t dev;
    464 
    465 	/*
    466 	 * Extract all the info we need from the vnode, taking care to
    467 	 * avoid a race with VOP_REVOKE().
    468 	 */
    469 
    470 	vp = ap->a_vp;
    471 	dev = NODEV;
    472 	simple_lock(&vp->v_interlock);
    473 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    474 		dev = vp->v_rdev;
    475 	}
    476 	simple_unlock(&vp->v_interlock);
    477 	if (dev == NODEV) {
    478 		return ENXIO;
    479 	}
    480 
    481 	switch (vp->v_type) {
    482 
    483 	case VCHR:
    484 		cdev = cdevsw_lookup(dev);
    485 		if (cdev == NULL)
    486 			return (ENXIO);
    487 		return ((*cdev->d_ioctl)(dev, ap->a_command, ap->a_data,
    488 		    ap->a_fflag, ap->a_l));
    489 
    490 	case VBLK:
    491 		bdev = bdevsw_lookup(dev);
    492 		if (bdev == NULL)
    493 			return (ENXIO);
    494 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
    495 			if (bdev->d_type == D_TAPE)
    496 				return (0);
    497 			else
    498 				return (1);
    499 		}
    500 		return ((*bdev->d_ioctl)(dev, ap->a_command, ap->a_data,
    501 		   ap->a_fflag, ap->a_l));
    502 
    503 	default:
    504 		panic("spec_ioctl");
    505 		/* NOTREACHED */
    506 	}
    507 }
    508 
    509 /* ARGSUSED */
    510 int
    511 spec_poll(v)
    512 	void *v;
    513 {
    514 	struct vop_poll_args /* {
    515 		struct vnode *a_vp;
    516 		int a_events;
    517 		struct lwp *a_l;
    518 	} */ *ap = v;
    519 	const struct cdevsw *cdev;
    520 	dev_t dev;
    521 
    522 	switch (ap->a_vp->v_type) {
    523 
    524 	case VCHR:
    525 		dev = ap->a_vp->v_rdev;
    526 		cdev = cdevsw_lookup(dev);
    527 		if (cdev == NULL)
    528 			return (POLLERR);
    529 		return (*cdev->d_poll)(dev, ap->a_events, ap->a_l);
    530 
    531 	default:
    532 		return (genfs_poll(v));
    533 	}
    534 }
    535 
    536 /* ARGSUSED */
    537 int
    538 spec_kqfilter(v)
    539 	void *v;
    540 {
    541 	struct vop_kqfilter_args /* {
    542 		struct vnode	*a_vp;
    543 		struct proc	*a_kn;
    544 	} */ *ap = v;
    545 	const struct cdevsw *cdev;
    546 	dev_t dev;
    547 
    548 	switch (ap->a_vp->v_type) {
    549 
    550 	case VCHR:
    551 		dev = ap->a_vp->v_rdev;
    552 		cdev = cdevsw_lookup(dev);
    553 		if (cdev == NULL)
    554 			return (ENXIO);
    555 		return (*cdev->d_kqfilter)(dev, ap->a_kn);
    556 	default:
    557 		/*
    558 		 * Block devices don't support kqfilter, and refuse it
    559 		 * for any other files (like those vflush()ed) too.
    560 		 */
    561 		return (EOPNOTSUPP);
    562 	}
    563 }
    564 
    565 /*
    566  * Synch buffers associated with a block device
    567  */
    568 /* ARGSUSED */
    569 int
    570 spec_fsync(v)
    571 	void *v;
    572 {
    573 	struct vop_fsync_args /* {
    574 		struct vnode *a_vp;
    575 		kauth_cred_t a_cred;
    576 		int  a_flags;
    577 		off_t offlo;
    578 		off_t offhi;
    579 		struct lwp *a_l;
    580 	} */ *ap = v;
    581 	struct vnode *vp = ap->a_vp;
    582 
    583 	if (vp->v_type == VBLK)
    584 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    585 	return (0);
    586 }
    587 
    588 /*
    589  * Just call the device strategy routine
    590  */
    591 int
    592 spec_strategy(v)
    593 	void *v;
    594 {
    595 	struct vop_strategy_args /* {
    596 		struct vnode *a_vp;
    597 		struct buf *a_bp;
    598 	} */ *ap = v;
    599 	struct vnode *vp = ap->a_vp;
    600 	struct buf *bp = ap->a_bp;
    601 	int error, s;
    602 	struct spec_cow_entry *e;
    603 
    604 	error = 0;
    605 	bp->b_dev = vp->v_rdev;
    606 	if (!(bp->b_flags & B_READ) &&
    607 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
    608 		(*bioops.io_start)(bp);
    609 
    610 	if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) {
    611 		SPEC_COW_LOCK(vp->v_specinfo, s);
    612 		while (vp->v_spec_cow_req > 0)
    613 			ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    614 			    &vp->v_spec_cow_slock);
    615 		vp->v_spec_cow_count++;
    616 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    617 
    618 		SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) {
    619 			if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0)
    620 				break;
    621 		}
    622 
    623 		SPEC_COW_LOCK(vp->v_specinfo, s);
    624 		vp->v_spec_cow_count--;
    625 		if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0)
    626 			wakeup(&vp->v_spec_cow_req);
    627 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    628 	}
    629 
    630 	if (error) {
    631 		bp->b_error = error;
    632 		bp->b_flags |= B_ERROR;
    633 		biodone(bp);
    634 		return (error);
    635 	}
    636 
    637 	DEV_STRATEGY(bp);
    638 
    639 	return (0);
    640 }
    641 
    642 int
    643 spec_inactive(v)
    644 	void *v;
    645 {
    646 	struct vop_inactive_args /* {
    647 		struct vnode *a_vp;
    648 		struct proc *a_l;
    649 	} */ *ap = v;
    650 
    651 	VOP_UNLOCK(ap->a_vp, 0);
    652 	return (0);
    653 }
    654 
    655 /*
    656  * This is a noop, simply returning what one has been given.
    657  */
    658 int
    659 spec_bmap(v)
    660 	void *v;
    661 {
    662 	struct vop_bmap_args /* {
    663 		struct vnode *a_vp;
    664 		daddr_t  a_bn;
    665 		struct vnode **a_vpp;
    666 		daddr_t *a_bnp;
    667 		int *a_runp;
    668 	} */ *ap = v;
    669 
    670 	if (ap->a_vpp != NULL)
    671 		*ap->a_vpp = ap->a_vp;
    672 	if (ap->a_bnp != NULL)
    673 		*ap->a_bnp = ap->a_bn;
    674 	if (ap->a_runp != NULL)
    675 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    676 	return (0);
    677 }
    678 
    679 /*
    680  * Device close routine
    681  */
    682 /* ARGSUSED */
    683 int
    684 spec_close(v)
    685 	void *v;
    686 {
    687 	struct vop_close_args /* {
    688 		struct vnode *a_vp;
    689 		int  a_fflag;
    690 		kauth_cred_t a_cred;
    691 		struct lwp *a_l;
    692 	} */ *ap = v;
    693 	struct vnode *vp = ap->a_vp;
    694 	const struct bdevsw *bdev;
    695 	const struct cdevsw *cdev;
    696 	struct session *sess;
    697 	dev_t dev = vp->v_rdev;
    698 	int (*devclose)(dev_t, int, int, struct lwp *);
    699 	int mode, error, count, flags, flags1;
    700 
    701 	count = vcount(vp);
    702 	flags = vp->v_flag;
    703 
    704 	switch (vp->v_type) {
    705 
    706 	case VCHR:
    707 		/*
    708 		 * Hack: a tty device that is a controlling terminal
    709 		 * has a reference from the session structure.
    710 		 * We cannot easily tell that a character device is
    711 		 * a controlling terminal, unless it is the closing
    712 		 * process' controlling terminal.  In that case,
    713 		 * if the reference count is 2 (this last descriptor
    714 		 * plus the session), release the reference from the session.
    715 		 * Also remove the link from the tty back to the session
    716 		 * and pgrp - due to the way consoles are handled we cannot
    717 		 * guarantee that the vrele() will do the final close on the
    718 		 * actual tty device.
    719 		 */
    720 		if (count == 2 && ap->a_l &&
    721 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
    722 			sess->s_ttyvp = NULL;
    723 			if (sess->s_ttyp->t_session != NULL) {
    724 				sess->s_ttyp->t_pgrp = NULL;
    725 				sess->s_ttyp->t_session = NULL;
    726 				SESSRELE(sess);
    727 			} else if (sess->s_ttyp->t_pgrp != NULL)
    728 				panic("spec_close: spurious pgrp ref");
    729 			vrele(vp);
    730 			count--;
    731 		}
    732 		/*
    733 		 * If the vnode is locked, then we are in the midst
    734 		 * of forcably closing the device, otherwise we only
    735 		 * close on last reference.
    736 		 */
    737 		if (count > 1 && (flags & VXLOCK) == 0)
    738 			return (0);
    739 		cdev = cdevsw_lookup(dev);
    740 		if (cdev != NULL)
    741 			devclose = cdev->d_close;
    742 		else
    743 			devclose = NULL;
    744 		mode = S_IFCHR;
    745 		break;
    746 
    747 	case VBLK:
    748 		/*
    749 		 * On last close of a block device (that isn't mounted)
    750 		 * we must invalidate any in core blocks, so that
    751 		 * we can, for instance, change floppy disks.
    752 		 */
    753 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
    754 		if (error)
    755 			return (error);
    756 		/*
    757 		 * We do not want to really close the device if it
    758 		 * is still in use unless we are trying to close it
    759 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    760 		 * holds a reference to the vnode, and because we mark
    761 		 * any other vnodes that alias this device, when the
    762 		 * sum of the reference counts on all the aliased
    763 		 * vnodes descends to one, we are on last close.
    764 		 */
    765 		if (count > 1 && (flags & VXLOCK) == 0)
    766 			return (0);
    767 		bdev = bdevsw_lookup(dev);
    768 		if (bdev != NULL)
    769 			devclose = bdev->d_close;
    770 		else
    771 			devclose = NULL;
    772 		mode = S_IFBLK;
    773 		break;
    774 
    775 	default:
    776 		panic("spec_close: not special");
    777 	}
    778 
    779 	flags1 = ap->a_fflag;
    780 
    781 	/*
    782 	 * if VXLOCK is set, then we're going away soon, so make this
    783 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    784 	 */
    785 	if (flags & VXLOCK)
    786 		flags1 |= FNONBLOCK;
    787 
    788 	/*
    789 	 * If we're able to block, release the vnode lock & reacquire. We
    790 	 * might end up sleeping for someone else who wants our queues. They
    791 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
    792 	 * don't release the lock as we won't be able to regain it.
    793 	 */
    794 	if (!(flags1 & FNONBLOCK))
    795 		VOP_UNLOCK(vp, 0);
    796 
    797 	if (devclose != NULL)
    798 		error = (*devclose)(dev, flags1, mode, ap->a_l);
    799 	else
    800 		error = ENXIO;
    801 
    802 	if (!(flags1 & FNONBLOCK))
    803 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    804 
    805 	return (error);
    806 }
    807 
    808 /*
    809  * Print out the contents of a special device vnode.
    810  */
    811 int
    812 spec_print(v)
    813 	void *v;
    814 {
    815 	struct vop_print_args /* {
    816 		struct vnode *a_vp;
    817 	} */ *ap = v;
    818 
    819 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    820 	    minor(ap->a_vp->v_rdev));
    821 	return 0;
    822 }
    823 
    824 /*
    825  * Return POSIX pathconf information applicable to special devices.
    826  */
    827 int
    828 spec_pathconf(v)
    829 	void *v;
    830 {
    831 	struct vop_pathconf_args /* {
    832 		struct vnode *a_vp;
    833 		int a_name;
    834 		register_t *a_retval;
    835 	} */ *ap = v;
    836 
    837 	switch (ap->a_name) {
    838 	case _PC_LINK_MAX:
    839 		*ap->a_retval = LINK_MAX;
    840 		return (0);
    841 	case _PC_MAX_CANON:
    842 		*ap->a_retval = MAX_CANON;
    843 		return (0);
    844 	case _PC_MAX_INPUT:
    845 		*ap->a_retval = MAX_INPUT;
    846 		return (0);
    847 	case _PC_PIPE_BUF:
    848 		*ap->a_retval = PIPE_BUF;
    849 		return (0);
    850 	case _PC_CHOWN_RESTRICTED:
    851 		*ap->a_retval = 1;
    852 		return (0);
    853 	case _PC_VDISABLE:
    854 		*ap->a_retval = _POSIX_VDISABLE;
    855 		return (0);
    856 	case _PC_SYNC_IO:
    857 		*ap->a_retval = 1;
    858 		return (0);
    859 	default:
    860 		return (EINVAL);
    861 	}
    862 	/* NOTREACHED */
    863 }
    864 
    865 /*
    866  * Advisory record locking support.
    867  */
    868 int
    869 spec_advlock(v)
    870 	void *v;
    871 {
    872 	struct vop_advlock_args /* {
    873 		struct vnode *a_vp;
    874 		void *a_id;
    875 		int a_op;
    876 		struct flock *a_fl;
    877 		int a_flags;
    878 	} */ *ap = v;
    879 	struct vnode *vp = ap->a_vp;
    880 
    881 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    882 }
    883