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