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