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