Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.99
      1 /*	$NetBSD: spec_vnops.c,v 1.99 2007/06/05 12:31:32 yamt 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.99 2007/06/05 12:31:32 yamt 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 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 *vp = ap->a_vp;
    175 	const struct bdevsw *bdev;
    176 	const struct cdevsw *cdev;
    177 	dev_t dev = (dev_t)vp->v_rdev;
    178 	int error;
    179 	struct partinfo pi;
    180 	int (*d_ioctl)(dev_t, u_long, void *, int, struct lwp *);
    181 	enum kauth_device_req req;
    182 
    183 	/*
    184 	 * Don't allow open if fs is mounted -nodev.
    185 	 */
    186 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    187 		return (ENXIO);
    188 
    189 #define M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
    190 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_RW : \
    191 		 (m) & FWRITE ? KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE : \
    192 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_READ)
    193 
    194 	switch (vp->v_type) {
    195 
    196 	case VCHR:
    197 		cdev = cdevsw_lookup(dev);
    198 		if (cdev == NULL)
    199 			return (ENXIO);
    200 
    201 		req = M2K(ap->a_mode);
    202 
    203 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    204 		if (error)
    205 			return (error);
    206 
    207 		if (cdev->d_type == D_TTY)
    208 			vp->v_flag |= VISTTY;
    209 		VOP_UNLOCK(vp, 0);
    210 		error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, l);
    211 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    212 		if (cdev->d_type != D_DISK)
    213 			return error;
    214 		d_ioctl = cdev->d_ioctl;
    215 		break;
    216 
    217 	case VBLK:
    218 		bdev = bdevsw_lookup(dev);
    219 		if (bdev == NULL)
    220 			return (ENXIO);
    221 
    222 		req = M2K(ap->a_mode);
    223 
    224 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    225 		if (error)
    226 			return (error);
    227 
    228 		error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, l);
    229 		d_ioctl = bdev->d_ioctl;
    230 		break;
    231 
    232 	case VNON:
    233 	case VLNK:
    234 	case VDIR:
    235 	case VREG:
    236 	case VBAD:
    237 	case VFIFO:
    238 	case VSOCK:
    239 	default:
    240 		return 0;
    241 	}
    242 
    243 #undef M2K
    244 
    245 	if (error)
    246 		return error;
    247 	if (!(*d_ioctl)(vp->v_rdev, DIOCGPART, (void *)&pi, FREAD, curlwp))
    248 		uvm_vnp_setsize(vp,
    249 		    (voff_t)pi.disklab->d_secsize * pi.part->p_size);
    250 	return 0;
    251 }
    252 
    253 /*
    254  * Vnode op for read
    255  */
    256 /* ARGSUSED */
    257 int
    258 spec_read(v)
    259 	void *v;
    260 {
    261 	struct vop_read_args /* {
    262 		struct vnode *a_vp;
    263 		struct uio *a_uio;
    264 		int  a_ioflag;
    265 		kauth_cred_t a_cred;
    266 	} */ *ap = v;
    267 	struct vnode *vp = ap->a_vp;
    268 	struct uio *uio = ap->a_uio;
    269  	struct lwp *l = curlwp;
    270 	struct buf *bp;
    271 	const struct bdevsw *bdev;
    272 	const struct cdevsw *cdev;
    273 	daddr_t bn;
    274 	int bsize, bscale;
    275 	struct partinfo dpart;
    276 	int n, on;
    277 	int error = 0;
    278 
    279 #ifdef DIAGNOSTIC
    280 	if (uio->uio_rw != UIO_READ)
    281 		panic("spec_read mode");
    282 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    283 	    uio->uio_vmspace != curproc->p_vmspace)
    284 		panic("spec_read proc");
    285 #endif
    286 	if (uio->uio_resid == 0)
    287 		return (0);
    288 
    289 	switch (vp->v_type) {
    290 
    291 	case VCHR:
    292 		VOP_UNLOCK(vp, 0);
    293 		cdev = cdevsw_lookup(vp->v_rdev);
    294 		if (cdev != NULL)
    295 			error = (*cdev->d_read)(vp->v_rdev, uio, ap->a_ioflag);
    296 		else
    297 			error = ENXIO;
    298 		vn_lock(vp, LK_SHARED | LK_RETRY);
    299 		return (error);
    300 
    301 	case VBLK:
    302 		if (uio->uio_offset < 0)
    303 			return (EINVAL);
    304 		bsize = BLKDEV_IOSIZE;
    305 		bdev = bdevsw_lookup(vp->v_rdev);
    306 		if (bdev != NULL &&
    307 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (void *)&dpart,
    308 				     FREAD, l) == 0) {
    309 			if (dpart.part->p_fstype == FS_BSDFFS &&
    310 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    311 				bsize = dpart.part->p_frag *
    312 				    dpart.part->p_fsize;
    313 		}
    314 		bscale = bsize >> DEV_BSHIFT;
    315 		do {
    316 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    317 			on = uio->uio_offset % bsize;
    318 			n = min((unsigned)(bsize - on), uio->uio_resid);
    319 			error = bread(vp, bn, bsize, NOCRED, &bp);
    320 			n = min(n, bsize - bp->b_resid);
    321 			if (error) {
    322 				brelse(bp);
    323 				return (error);
    324 			}
    325 			error = uiomove((char *)bp->b_data + on, n, uio);
    326 			brelse(bp);
    327 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    328 		return (error);
    329 
    330 	default:
    331 		panic("spec_read type");
    332 	}
    333 	/* NOTREACHED */
    334 }
    335 
    336 /*
    337  * Vnode op for write
    338  */
    339 /* ARGSUSED */
    340 int
    341 spec_write(v)
    342 	void *v;
    343 {
    344 	struct vop_write_args /* {
    345 		struct vnode *a_vp;
    346 		struct uio *a_uio;
    347 		int  a_ioflag;
    348 		kauth_cred_t a_cred;
    349 	} */ *ap = v;
    350 	struct vnode *vp = ap->a_vp;
    351 	struct uio *uio = ap->a_uio;
    352 	struct lwp *l = curlwp;
    353 	struct buf *bp;
    354 	const struct bdevsw *bdev;
    355 	const struct cdevsw *cdev;
    356 	daddr_t bn;
    357 	int bsize, bscale;
    358 	struct partinfo dpart;
    359 	int n, on;
    360 	int error = 0;
    361 
    362 #ifdef DIAGNOSTIC
    363 	if (uio->uio_rw != UIO_WRITE)
    364 		panic("spec_write mode");
    365 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    366 	    uio->uio_vmspace != curproc->p_vmspace)
    367 		panic("spec_write proc");
    368 #endif
    369 
    370 	switch (vp->v_type) {
    371 
    372 	case VCHR:
    373 		VOP_UNLOCK(vp, 0);
    374 		cdev = cdevsw_lookup(vp->v_rdev);
    375 		if (cdev != NULL)
    376 			error = (*cdev->d_write)(vp->v_rdev, uio, ap->a_ioflag);
    377 		else
    378 			error = ENXIO;
    379 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    380 		return (error);
    381 
    382 	case VBLK:
    383 		if (uio->uio_resid == 0)
    384 			return (0);
    385 		if (uio->uio_offset < 0)
    386 			return (EINVAL);
    387 		bsize = BLKDEV_IOSIZE;
    388 		bdev = bdevsw_lookup(vp->v_rdev);
    389 		if (bdev != NULL &&
    390 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (void *)&dpart,
    391 				    FREAD, l) == 0) {
    392 			if (dpart.part->p_fstype == FS_BSDFFS &&
    393 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    394 				bsize = dpart.part->p_frag *
    395 				    dpart.part->p_fsize;
    396 		}
    397 		bscale = bsize >> DEV_BSHIFT;
    398 		do {
    399 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    400 			on = uio->uio_offset % bsize;
    401 			n = min((unsigned)(bsize - on), uio->uio_resid);
    402 			if (n == bsize)
    403 				bp = getblk(vp, bn, bsize, 0, 0);
    404 			else
    405 				error = bread(vp, bn, bsize, NOCRED, &bp);
    406 			if (error) {
    407 				brelse(bp);
    408 				return (error);
    409 			}
    410 			n = min(n, bsize - bp->b_resid);
    411 			error = uiomove((char *)bp->b_data + on, n, uio);
    412 			if (error)
    413 				brelse(bp);
    414 			else {
    415 				if (n + on == bsize)
    416 					bawrite(bp);
    417 				else
    418 					bdwrite(bp);
    419 				if (bp->b_flags & B_ERROR)
    420 					error = bp->b_error;
    421 			}
    422 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    423 		return (error);
    424 
    425 	default:
    426 		panic("spec_write type");
    427 	}
    428 	/* NOTREACHED */
    429 }
    430 
    431 /*
    432  * Device ioctl operation.
    433  */
    434 /* ARGSUSED */
    435 int
    436 spec_ioctl(v)
    437 	void *v;
    438 {
    439 	struct vop_ioctl_args /* {
    440 		struct vnode *a_vp;
    441 		u_long a_command;
    442 		void  *a_data;
    443 		int  a_fflag;
    444 		kauth_cred_t a_cred;
    445 		struct lwp *a_l;
    446 	} */ *ap = v;
    447 	const struct bdevsw *bdev;
    448 	const struct cdevsw *cdev;
    449 	struct vnode *vp;
    450 	dev_t dev;
    451 
    452 	/*
    453 	 * Extract all the info we need from the vnode, taking care to
    454 	 * avoid a race with VOP_REVOKE().
    455 	 */
    456 
    457 	vp = ap->a_vp;
    458 	dev = NODEV;
    459 	simple_lock(&vp->v_interlock);
    460 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    461 		dev = vp->v_rdev;
    462 	}
    463 	simple_unlock(&vp->v_interlock);
    464 	if (dev == NODEV) {
    465 		return ENXIO;
    466 	}
    467 
    468 	switch (vp->v_type) {
    469 
    470 	case VCHR:
    471 		cdev = cdevsw_lookup(dev);
    472 		if (cdev == NULL)
    473 			return (ENXIO);
    474 		return ((*cdev->d_ioctl)(dev, ap->a_command, ap->a_data,
    475 		    ap->a_fflag, ap->a_l));
    476 
    477 	case VBLK:
    478 		bdev = bdevsw_lookup(dev);
    479 		if (bdev == NULL)
    480 			return (ENXIO);
    481 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
    482 			if (bdev->d_type == D_TAPE)
    483 				return (0);
    484 			else
    485 				return (1);
    486 		}
    487 		return ((*bdev->d_ioctl)(dev, ap->a_command, ap->a_data,
    488 		   ap->a_fflag, ap->a_l));
    489 
    490 	default:
    491 		panic("spec_ioctl");
    492 		/* NOTREACHED */
    493 	}
    494 }
    495 
    496 /* ARGSUSED */
    497 int
    498 spec_poll(v)
    499 	void *v;
    500 {
    501 	struct vop_poll_args /* {
    502 		struct vnode *a_vp;
    503 		int a_events;
    504 		struct lwp *a_l;
    505 	} */ *ap = v;
    506 	const struct cdevsw *cdev;
    507 	struct vnode *vp;
    508 	dev_t dev;
    509 
    510 	/*
    511 	 * Extract all the info we need from the vnode, taking care to
    512 	 * avoid a race with VOP_REVOKE().
    513 	 */
    514 
    515 	vp = ap->a_vp;
    516 	dev = NODEV;
    517 	simple_lock(&vp->v_interlock);
    518 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    519 		dev = vp->v_rdev;
    520 	}
    521 	simple_unlock(&vp->v_interlock);
    522 	if (dev == NODEV) {
    523 		return POLLERR;
    524 	}
    525 
    526 	switch (vp->v_type) {
    527 
    528 	case VCHR:
    529 		cdev = cdevsw_lookup(dev);
    530 		if (cdev == NULL)
    531 			return (POLLERR);
    532 		return (*cdev->d_poll)(dev, ap->a_events, ap->a_l);
    533 
    534 	default:
    535 		return (genfs_poll(v));
    536 	}
    537 }
    538 
    539 /* ARGSUSED */
    540 int
    541 spec_kqfilter(v)
    542 	void *v;
    543 {
    544 	struct vop_kqfilter_args /* {
    545 		struct vnode	*a_vp;
    546 		struct proc	*a_kn;
    547 	} */ *ap = v;
    548 	const struct cdevsw *cdev;
    549 	dev_t dev;
    550 
    551 	switch (ap->a_vp->v_type) {
    552 
    553 	case VCHR:
    554 		dev = ap->a_vp->v_rdev;
    555 		cdev = cdevsw_lookup(dev);
    556 		if (cdev == NULL)
    557 			return (ENXIO);
    558 		return (*cdev->d_kqfilter)(dev, ap->a_kn);
    559 	default:
    560 		/*
    561 		 * Block devices don't support kqfilter, and refuse it
    562 		 * for any other files (like those vflush()ed) too.
    563 		 */
    564 		return (EOPNOTSUPP);
    565 	}
    566 }
    567 
    568 /*
    569  * Synch buffers associated with a block device
    570  */
    571 /* ARGSUSED */
    572 int
    573 spec_fsync(v)
    574 	void *v;
    575 {
    576 	struct vop_fsync_args /* {
    577 		struct vnode *a_vp;
    578 		kauth_cred_t a_cred;
    579 		int  a_flags;
    580 		off_t offlo;
    581 		off_t offhi;
    582 		struct lwp *a_l;
    583 	} */ *ap = v;
    584 	struct vnode *vp = ap->a_vp;
    585 
    586 	if (vp->v_type == VBLK)
    587 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    588 	return (0);
    589 }
    590 
    591 /*
    592  * Just call the device strategy routine
    593  */
    594 int
    595 spec_strategy(v)
    596 	void *v;
    597 {
    598 	struct vop_strategy_args /* {
    599 		struct vnode *a_vp;
    600 		struct buf *a_bp;
    601 	} */ *ap = v;
    602 	struct vnode *vp = ap->a_vp;
    603 	struct buf *bp = ap->a_bp;
    604 	int error, s;
    605 	struct spec_cow_entry *e;
    606 
    607 	error = 0;
    608 	bp->b_dev = vp->v_rdev;
    609 	if (!(bp->b_flags & B_READ) &&
    610 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
    611 		(*bioops.io_start)(bp);
    612 
    613 	if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) {
    614 		SPEC_COW_LOCK(vp->v_specinfo, s);
    615 		while (vp->v_spec_cow_req > 0)
    616 			ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    617 			    &vp->v_spec_cow_slock);
    618 		vp->v_spec_cow_count++;
    619 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    620 
    621 		SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) {
    622 			if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0)
    623 				break;
    624 		}
    625 
    626 		SPEC_COW_LOCK(vp->v_specinfo, s);
    627 		vp->v_spec_cow_count--;
    628 		if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0)
    629 			wakeup(&vp->v_spec_cow_req);
    630 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    631 	}
    632 
    633 	if (error) {
    634 		bp->b_error = error;
    635 		bp->b_flags |= B_ERROR;
    636 		biodone(bp);
    637 		return (error);
    638 	}
    639 
    640 	DEV_STRATEGY(bp);
    641 
    642 	return (0);
    643 }
    644 
    645 int
    646 spec_inactive(v)
    647 	void *v;
    648 {
    649 	struct vop_inactive_args /* {
    650 		struct vnode *a_vp;
    651 		struct proc *a_l;
    652 	} */ *ap = v;
    653 
    654 	VOP_UNLOCK(ap->a_vp, 0);
    655 	return (0);
    656 }
    657 
    658 /*
    659  * This is a noop, simply returning what one has been given.
    660  */
    661 int
    662 spec_bmap(v)
    663 	void *v;
    664 {
    665 	struct vop_bmap_args /* {
    666 		struct vnode *a_vp;
    667 		daddr_t  a_bn;
    668 		struct vnode **a_vpp;
    669 		daddr_t *a_bnp;
    670 		int *a_runp;
    671 	} */ *ap = v;
    672 
    673 	if (ap->a_vpp != NULL)
    674 		*ap->a_vpp = ap->a_vp;
    675 	if (ap->a_bnp != NULL)
    676 		*ap->a_bnp = ap->a_bn;
    677 	if (ap->a_runp != NULL)
    678 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    679 	return (0);
    680 }
    681 
    682 /*
    683  * Device close routine
    684  */
    685 /* ARGSUSED */
    686 int
    687 spec_close(v)
    688 	void *v;
    689 {
    690 	struct vop_close_args /* {
    691 		struct vnode *a_vp;
    692 		int  a_fflag;
    693 		kauth_cred_t a_cred;
    694 		struct lwp *a_l;
    695 	} */ *ap = v;
    696 	struct vnode *vp = ap->a_vp;
    697 	const struct bdevsw *bdev;
    698 	const struct cdevsw *cdev;
    699 	struct session *sess;
    700 	dev_t dev = vp->v_rdev;
    701 	int (*devclose)(dev_t, int, int, struct lwp *);
    702 	int mode, error, count, flags, flags1;
    703 
    704 	count = vcount(vp);
    705 	flags = vp->v_flag;
    706 
    707 	switch (vp->v_type) {
    708 
    709 	case VCHR:
    710 		/*
    711 		 * Hack: a tty device that is a controlling terminal
    712 		 * has a reference from the session structure.
    713 		 * We cannot easily tell that a character device is
    714 		 * a controlling terminal, unless it is the closing
    715 		 * process' controlling terminal.  In that case,
    716 		 * if the reference count is 2 (this last descriptor
    717 		 * plus the session), release the reference from the session.
    718 		 * Also remove the link from the tty back to the session
    719 		 * and pgrp - due to the way consoles are handled we cannot
    720 		 * guarantee that the vrele() will do the final close on the
    721 		 * actual tty device.
    722 		 */
    723 		if (count == 2 && ap->a_l &&
    724 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
    725 			sess->s_ttyvp = NULL;
    726 			if (sess->s_ttyp->t_session != NULL) {
    727 				sess->s_ttyp->t_pgrp = NULL;
    728 				sess->s_ttyp->t_session = NULL;
    729 				SESSRELE(sess);
    730 			} else if (sess->s_ttyp->t_pgrp != NULL)
    731 				panic("spec_close: spurious pgrp ref");
    732 			vrele(vp);
    733 			count--;
    734 		}
    735 		/*
    736 		 * If the vnode is locked, then we are in the midst
    737 		 * of forcably closing the device, otherwise we only
    738 		 * close on last reference.
    739 		 */
    740 		if (count > 1 && (flags & VXLOCK) == 0)
    741 			return (0);
    742 		cdev = cdevsw_lookup(dev);
    743 		if (cdev != NULL)
    744 			devclose = cdev->d_close;
    745 		else
    746 			devclose = NULL;
    747 		mode = S_IFCHR;
    748 		break;
    749 
    750 	case VBLK:
    751 		/*
    752 		 * On last close of a block device (that isn't mounted)
    753 		 * we must invalidate any in core blocks, so that
    754 		 * we can, for instance, change floppy disks.
    755 		 */
    756 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
    757 		if (error)
    758 			return (error);
    759 		/*
    760 		 * We do not want to really close the device if it
    761 		 * is still in use unless we are trying to close it
    762 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    763 		 * holds a reference to the vnode, and because we mark
    764 		 * any other vnodes that alias this device, when the
    765 		 * sum of the reference counts on all the aliased
    766 		 * vnodes descends to one, we are on last close.
    767 		 */
    768 		if (count > 1 && (flags & VXLOCK) == 0)
    769 			return (0);
    770 		bdev = bdevsw_lookup(dev);
    771 		if (bdev != NULL)
    772 			devclose = bdev->d_close;
    773 		else
    774 			devclose = NULL;
    775 		mode = S_IFBLK;
    776 		break;
    777 
    778 	default:
    779 		panic("spec_close: not special");
    780 	}
    781 
    782 	flags1 = ap->a_fflag;
    783 
    784 	/*
    785 	 * if VXLOCK is set, then we're going away soon, so make this
    786 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    787 	 */
    788 	if (flags & VXLOCK)
    789 		flags1 |= FNONBLOCK;
    790 
    791 	/*
    792 	 * If we're able to block, release the vnode lock & reacquire. We
    793 	 * might end up sleeping for someone else who wants our queues. They
    794 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
    795 	 * don't release the lock as we won't be able to regain it.
    796 	 */
    797 	if (!(flags1 & FNONBLOCK))
    798 		VOP_UNLOCK(vp, 0);
    799 
    800 	if (devclose != NULL)
    801 		error = (*devclose)(dev, flags1, mode, ap->a_l);
    802 	else
    803 		error = ENXIO;
    804 
    805 	if (!(flags1 & FNONBLOCK))
    806 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    807 
    808 	return (error);
    809 }
    810 
    811 /*
    812  * Print out the contents of a special device vnode.
    813  */
    814 int
    815 spec_print(v)
    816 	void *v;
    817 {
    818 	struct vop_print_args /* {
    819 		struct vnode *a_vp;
    820 	} */ *ap = v;
    821 
    822 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    823 	    minor(ap->a_vp->v_rdev));
    824 	return 0;
    825 }
    826 
    827 /*
    828  * Return POSIX pathconf information applicable to special devices.
    829  */
    830 int
    831 spec_pathconf(v)
    832 	void *v;
    833 {
    834 	struct vop_pathconf_args /* {
    835 		struct vnode *a_vp;
    836 		int a_name;
    837 		register_t *a_retval;
    838 	} */ *ap = v;
    839 
    840 	switch (ap->a_name) {
    841 	case _PC_LINK_MAX:
    842 		*ap->a_retval = LINK_MAX;
    843 		return (0);
    844 	case _PC_MAX_CANON:
    845 		*ap->a_retval = MAX_CANON;
    846 		return (0);
    847 	case _PC_MAX_INPUT:
    848 		*ap->a_retval = MAX_INPUT;
    849 		return (0);
    850 	case _PC_PIPE_BUF:
    851 		*ap->a_retval = PIPE_BUF;
    852 		return (0);
    853 	case _PC_CHOWN_RESTRICTED:
    854 		*ap->a_retval = 1;
    855 		return (0);
    856 	case _PC_VDISABLE:
    857 		*ap->a_retval = _POSIX_VDISABLE;
    858 		return (0);
    859 	case _PC_SYNC_IO:
    860 		*ap->a_retval = 1;
    861 		return (0);
    862 	default:
    863 		return (EINVAL);
    864 	}
    865 	/* NOTREACHED */
    866 }
    867 
    868 /*
    869  * Advisory record locking support.
    870  */
    871 int
    872 spec_advlock(v)
    873 	void *v;
    874 {
    875 	struct vop_advlock_args /* {
    876 		struct vnode *a_vp;
    877 		void *a_id;
    878 		int a_op;
    879 		struct flock *a_fl;
    880 		int a_flags;
    881 	} */ *ap = v;
    882 	struct vnode *vp = ap->a_vp;
    883 
    884 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    885 }
    886