Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.100
      1 /*	$NetBSD: spec_vnops.c,v 1.100 2007/07/09 21:10:59 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.100 2007/07/09 21:10:59 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_flags & B_ERROR)
    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  * Synch buffers associated with a block device
    524  */
    525 /* ARGSUSED */
    526 int
    527 spec_fsync(v)
    528 	void *v;
    529 {
    530 	struct vop_fsync_args /* {
    531 		struct vnode *a_vp;
    532 		kauth_cred_t a_cred;
    533 		int  a_flags;
    534 		off_t offlo;
    535 		off_t offhi;
    536 		struct lwp *a_l;
    537 	} */ *ap = v;
    538 	struct vnode *vp = ap->a_vp;
    539 
    540 	if (vp->v_type == VBLK)
    541 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    542 	return (0);
    543 }
    544 
    545 /*
    546  * Just call the device strategy routine
    547  */
    548 int
    549 spec_strategy(v)
    550 	void *v;
    551 {
    552 	struct vop_strategy_args /* {
    553 		struct vnode *a_vp;
    554 		struct buf *a_bp;
    555 	} */ *ap = v;
    556 	struct vnode *vp = ap->a_vp;
    557 	struct buf *bp = ap->a_bp;
    558 	int error, s;
    559 	struct spec_cow_entry *e;
    560 
    561 	error = 0;
    562 	bp->b_dev = vp->v_rdev;
    563 	if (!(bp->b_flags & B_READ) &&
    564 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
    565 		(*bioops.io_start)(bp);
    566 
    567 	if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) {
    568 		SPEC_COW_LOCK(vp->v_specinfo, s);
    569 		while (vp->v_spec_cow_req > 0)
    570 			ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    571 			    &vp->v_spec_cow_slock);
    572 		vp->v_spec_cow_count++;
    573 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    574 
    575 		SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) {
    576 			if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0)
    577 				break;
    578 		}
    579 
    580 		SPEC_COW_LOCK(vp->v_specinfo, s);
    581 		vp->v_spec_cow_count--;
    582 		if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0)
    583 			wakeup(&vp->v_spec_cow_req);
    584 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
    585 	}
    586 
    587 	if (error) {
    588 		bp->b_error = error;
    589 		bp->b_flags |= B_ERROR;
    590 		biodone(bp);
    591 		return (error);
    592 	}
    593 
    594 	bdev_strategy(bp);
    595 
    596 	return (0);
    597 }
    598 
    599 int
    600 spec_inactive(v)
    601 	void *v;
    602 {
    603 	struct vop_inactive_args /* {
    604 		struct vnode *a_vp;
    605 		struct proc *a_l;
    606 	} */ *ap = v;
    607 
    608 	VOP_UNLOCK(ap->a_vp, 0);
    609 	return (0);
    610 }
    611 
    612 /*
    613  * This is a noop, simply returning what one has been given.
    614  */
    615 int
    616 spec_bmap(v)
    617 	void *v;
    618 {
    619 	struct vop_bmap_args /* {
    620 		struct vnode *a_vp;
    621 		daddr_t  a_bn;
    622 		struct vnode **a_vpp;
    623 		daddr_t *a_bnp;
    624 		int *a_runp;
    625 	} */ *ap = v;
    626 
    627 	if (ap->a_vpp != NULL)
    628 		*ap->a_vpp = ap->a_vp;
    629 	if (ap->a_bnp != NULL)
    630 		*ap->a_bnp = ap->a_bn;
    631 	if (ap->a_runp != NULL)
    632 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    633 	return (0);
    634 }
    635 
    636 /*
    637  * Device close routine
    638  */
    639 /* ARGSUSED */
    640 int
    641 spec_close(v)
    642 	void *v;
    643 {
    644 	struct vop_close_args /* {
    645 		struct vnode *a_vp;
    646 		int  a_fflag;
    647 		kauth_cred_t a_cred;
    648 		struct lwp *a_l;
    649 	} */ *ap = v;
    650 	struct vnode *vp = ap->a_vp;
    651 	struct session *sess;
    652 	dev_t dev = vp->v_rdev;
    653 	int mode, error, count, flags, flags1;
    654 
    655 	count = vcount(vp);
    656 	flags = vp->v_flag;
    657 
    658 	switch (vp->v_type) {
    659 
    660 	case VCHR:
    661 		/*
    662 		 * Hack: a tty device that is a controlling terminal
    663 		 * has a reference from the session structure.
    664 		 * We cannot easily tell that a character device is
    665 		 * a controlling terminal, unless it is the closing
    666 		 * process' controlling terminal.  In that case,
    667 		 * if the reference count is 2 (this last descriptor
    668 		 * plus the session), release the reference from the session.
    669 		 * Also remove the link from the tty back to the session
    670 		 * and pgrp - due to the way consoles are handled we cannot
    671 		 * guarantee that the vrele() will do the final close on the
    672 		 * actual tty device.
    673 		 */
    674 		mutex_enter(&proclist_lock);
    675 		if (count == 2 && ap->a_l &&
    676 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
    677 			sess->s_ttyvp = NULL;
    678 			if (sess->s_ttyp->t_session != NULL) {
    679 				sess->s_ttyp->t_pgrp = NULL;
    680 				sess->s_ttyp->t_session = NULL;
    681 				mutex_exit(&proclist_lock);
    682 				SESSRELE(sess);
    683 			} else {
    684 				if (sess->s_ttyp->t_pgrp != NULL)
    685 					panic("spec_close: spurious pgrp ref");
    686 				mutex_exit(&proclist_lock);
    687 			}
    688 			vrele(vp);
    689 			count--;
    690 		} else
    691 			mutex_exit(&proclist_lock);
    692 
    693 		/*
    694 		 * If the vnode is locked, then we are in the midst
    695 		 * of forcably closing the device, otherwise we only
    696 		 * close on last reference.
    697 		 */
    698 		if (count > 1 && (flags & VXLOCK) == 0)
    699 			return (0);
    700 		mode = S_IFCHR;
    701 		break;
    702 
    703 	case VBLK:
    704 		/*
    705 		 * On last close of a block device (that isn't mounted)
    706 		 * we must invalidate any in core blocks, so that
    707 		 * we can, for instance, change floppy disks.
    708 		 */
    709 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
    710 		if (error)
    711 			return (error);
    712 		/*
    713 		 * We do not want to really close the device if it
    714 		 * is still in use unless we are trying to close it
    715 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    716 		 * holds a reference to the vnode, and because we mark
    717 		 * any other vnodes that alias this device, when the
    718 		 * sum of the reference counts on all the aliased
    719 		 * vnodes descends to one, we are on last close.
    720 		 */
    721 		if (count > 1 && (flags & VXLOCK) == 0)
    722 			return (0);
    723 		mode = S_IFBLK;
    724 		break;
    725 
    726 	default:
    727 		panic("spec_close: not special");
    728 	}
    729 
    730 	flags1 = ap->a_fflag;
    731 
    732 	/*
    733 	 * if VXLOCK is set, then we're going away soon, so make this
    734 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    735 	 */
    736 	if (flags & VXLOCK)
    737 		flags1 |= FNONBLOCK;
    738 
    739 	/*
    740 	 * If we're able to block, release the vnode lock & reacquire. We
    741 	 * might end up sleeping for someone else who wants our queues. They
    742 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is
    743 	 * set, don't release the lock as we won't be able to regain it.
    744 	 */
    745 	if (!(flags1 & FNONBLOCK))
    746 		VOP_UNLOCK(vp, 0);
    747 
    748 	if (vp->v_type == VBLK)
    749 		error = bdev_close(dev, flags1, mode, ap->a_l);
    750 	else
    751 		error = cdev_close(dev, flags1, mode, ap->a_l);
    752 
    753 	if (!(flags1 & FNONBLOCK))
    754 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    755 
    756 	return (error);
    757 }
    758 
    759 /*
    760  * Print out the contents of a special device vnode.
    761  */
    762 int
    763 spec_print(v)
    764 	void *v;
    765 {
    766 	struct vop_print_args /* {
    767 		struct vnode *a_vp;
    768 	} */ *ap = v;
    769 
    770 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    771 	    minor(ap->a_vp->v_rdev));
    772 	return 0;
    773 }
    774 
    775 /*
    776  * Return POSIX pathconf information applicable to special devices.
    777  */
    778 int
    779 spec_pathconf(v)
    780 	void *v;
    781 {
    782 	struct vop_pathconf_args /* {
    783 		struct vnode *a_vp;
    784 		int a_name;
    785 		register_t *a_retval;
    786 	} */ *ap = v;
    787 
    788 	switch (ap->a_name) {
    789 	case _PC_LINK_MAX:
    790 		*ap->a_retval = LINK_MAX;
    791 		return (0);
    792 	case _PC_MAX_CANON:
    793 		*ap->a_retval = MAX_CANON;
    794 		return (0);
    795 	case _PC_MAX_INPUT:
    796 		*ap->a_retval = MAX_INPUT;
    797 		return (0);
    798 	case _PC_PIPE_BUF:
    799 		*ap->a_retval = PIPE_BUF;
    800 		return (0);
    801 	case _PC_CHOWN_RESTRICTED:
    802 		*ap->a_retval = 1;
    803 		return (0);
    804 	case _PC_VDISABLE:
    805 		*ap->a_retval = _POSIX_VDISABLE;
    806 		return (0);
    807 	case _PC_SYNC_IO:
    808 		*ap->a_retval = 1;
    809 		return (0);
    810 	default:
    811 		return (EINVAL);
    812 	}
    813 	/* NOTREACHED */
    814 }
    815 
    816 /*
    817  * Advisory record locking support.
    818  */
    819 int
    820 spec_advlock(v)
    821 	void *v;
    822 {
    823 	struct vop_advlock_args /* {
    824 		struct vnode *a_vp;
    825 		void *a_id;
    826 		int a_op;
    827 		struct flock *a_fl;
    828 		int a_flags;
    829 	} */ *ap = v;
    830 	struct vnode *vp = ap->a_vp;
    831 
    832 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    833 }
    834