Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.62
      1 /*	$NetBSD: spec_vnops.c,v 1.62 2002/07/10 23:16:33 wiz 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/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.62 2002/07/10 23:16:33 wiz 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/file.h>
     54 #include <sys/disklabel.h>
     55 #include <sys/lockf.h>
     56 
     57 #include <miscfs/genfs/genfs.h>
     58 #include <miscfs/specfs/specdev.h>
     59 
     60 /* symbolic sleep message strings for devices */
     61 const char	devopn[] = "devopn";
     62 const char	devio[] = "devio";
     63 const char	devwait[] = "devwait";
     64 const char	devin[] = "devin";
     65 const char	devout[] = "devout";
     66 const char	devioc[] = "devioc";
     67 const char	devcls[] = "devcls";
     68 
     69 struct vnode	*speclisth[SPECHSZ];
     70 
     71 /*
     72  * This vnode operations vector is used for two things only:
     73  * - special device nodes created from whole cloth by the kernel.
     74  * - as a temporary vnodeops replacement for vnodes which were found to
     75  *	be aliased by callers of checkalias().
     76  * For the ops vector for vnodes built from special devices found in a
     77  * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
     78  * equivalent for other filesystems.
     79  */
     80 
     81 int (**spec_vnodeop_p) __P((void *));
     82 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
     83 	{ &vop_default_desc, vn_default_error },
     84 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
     85 	{ &vop_create_desc, spec_create },		/* create */
     86 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
     87 	{ &vop_open_desc, spec_open },			/* open */
     88 	{ &vop_close_desc, spec_close },		/* close */
     89 	{ &vop_access_desc, spec_access },		/* access */
     90 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
     91 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
     92 	{ &vop_read_desc, spec_read },			/* read */
     93 	{ &vop_write_desc, spec_write },		/* write */
     94 	{ &vop_lease_desc, spec_lease_check },		/* lease */
     95 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
     96 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
     97 	{ &vop_poll_desc, spec_poll },			/* poll */
     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_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    122 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    123 	{ &vop_vfree_desc, spec_vfree },		/* vfree */
    124 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    125 	{ &vop_update_desc, spec_update },		/* update */
    126 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    127 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    128 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    129 	{ NULL, NULL }
    130 };
    131 const struct vnodeopv_desc spec_vnodeop_opv_desc =
    132 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    133 
    134 /*
    135  * Trivial lookup routine that always fails.
    136  */
    137 int
    138 spec_lookup(v)
    139 	void *v;
    140 {
    141 	struct vop_lookup_args /* {
    142 		struct vnode *a_dvp;
    143 		struct vnode **a_vpp;
    144 		struct componentname *a_cnp;
    145 	} */ *ap = v;
    146 
    147 	*ap->a_vpp = NULL;
    148 	return (ENOTDIR);
    149 }
    150 
    151 /*
    152  * Open a special file.
    153  */
    154 /* ARGSUSED */
    155 int
    156 spec_open(v)
    157 	void *v;
    158 {
    159 	struct vop_open_args /* {
    160 		struct vnode *a_vp;
    161 		int  a_mode;
    162 		struct ucred *a_cred;
    163 		struct proc *a_p;
    164 	} */ *ap = v;
    165 	struct proc *p = ap->a_p;
    166 	struct vnode *bvp, *vp = ap->a_vp;
    167 	dev_t bdev, dev = (dev_t)vp->v_rdev;
    168 	int maj = major(dev);
    169 	int error;
    170 	struct partinfo pi;
    171 
    172 	/*
    173 	 * Don't allow open if fs is mounted -nodev.
    174 	 */
    175 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    176 		return (ENXIO);
    177 
    178 	switch (vp->v_type) {
    179 
    180 	case VCHR:
    181 		if ((u_int)maj >= nchrdev)
    182 			return (ENXIO);
    183 		if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
    184 			/*
    185 			 * When running in very secure mode, do not allow
    186 			 * opens for writing of any disk character devices.
    187 			 */
    188 			if (securelevel >= 2 && cdevsw[maj].d_type == D_DISK)
    189 				return (EPERM);
    190 			/*
    191 			 * When running in secure mode, do not allow opens
    192 			 * for writing of /dev/mem, /dev/kmem, or character
    193 			 * devices whose corresponding block devices are
    194 			 * currently mounted.
    195 			 */
    196 			if (securelevel >= 1) {
    197 				if ((bdev = chrtoblk(dev)) != (dev_t)NODEV &&
    198 				    vfinddev(bdev, VBLK, &bvp) &&
    199 				    (error = vfs_mountedon(bvp)))
    200 					return (error);
    201 				if (iskmemdev(dev))
    202 					return (EPERM);
    203 			}
    204 		}
    205 		if (cdevsw[maj].d_type == D_TTY)
    206 			vp->v_flag |= VISTTY;
    207 		VOP_UNLOCK(vp, 0);
    208 		error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
    209 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    210 		return (error);
    211 
    212 	case VBLK:
    213 		if ((u_int)maj >= nblkdev)
    214 			return (ENXIO);
    215 		/*
    216 		 * When running in very secure mode, do not allow
    217 		 * opens for writing of any disk block devices.
    218 		 */
    219 		if (securelevel >= 2 && ap->a_cred != FSCRED &&
    220 		    (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
    221 			return (EPERM);
    222 		/*
    223 		 * Do not allow opens of block devices that are
    224 		 * currently mounted.
    225 		 */
    226 		if ((error = vfs_mountedon(vp)) != 0)
    227 			return (error);
    228 		error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
    229 		if (error) {
    230 			return error;
    231 		}
    232 		error = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
    233 		    DIOCGPART, (caddr_t)&pi, FREAD, curproc);
    234 		if (error == 0) {
    235 			vp->v_size = (voff_t)pi.disklab->d_secsize *
    236 			    pi.part->p_size;
    237 		}
    238 		return 0;
    239 
    240 	case VNON:
    241 	case VLNK:
    242 	case VDIR:
    243 	case VREG:
    244 	case VBAD:
    245 	case VFIFO:
    246 	case VSOCK:
    247 		break;
    248 	}
    249 	return (0);
    250 }
    251 
    252 /*
    253  * Vnode op for read
    254  */
    255 /* ARGSUSED */
    256 int
    257 spec_read(v)
    258 	void *v;
    259 {
    260 	struct vop_read_args /* {
    261 		struct vnode *a_vp;
    262 		struct uio *a_uio;
    263 		int  a_ioflag;
    264 		struct ucred *a_cred;
    265 	} */ *ap = v;
    266 	struct vnode *vp = ap->a_vp;
    267 	struct uio *uio = ap->a_uio;
    268  	struct proc *p = uio->uio_procp;
    269 	struct buf *bp;
    270 	daddr_t bn;
    271 	int bsize, bscale;
    272 	struct partinfo dpart;
    273 	int n, on, majordev;
    274 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    275 	int error = 0;
    276 
    277 #ifdef DIAGNOSTIC
    278 	if (uio->uio_rw != UIO_READ)
    279 		panic("spec_read mode");
    280 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    281 		panic("spec_read proc");
    282 #endif
    283 	if (uio->uio_resid == 0)
    284 		return (0);
    285 
    286 	switch (vp->v_type) {
    287 
    288 	case VCHR:
    289 		VOP_UNLOCK(vp, 0);
    290 		error = (*cdevsw[major(vp->v_rdev)].d_read)
    291 			(vp->v_rdev, uio, ap->a_ioflag);
    292 		vn_lock(vp, LK_SHARED | LK_RETRY);
    293 		return (error);
    294 
    295 	case VBLK:
    296 		if (uio->uio_offset < 0)
    297 			return (EINVAL);
    298 		bsize = BLKDEV_IOSIZE;
    299 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    300 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    301 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    302 			if (dpart.part->p_fstype == FS_BSDFFS &&
    303 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    304 				bsize = dpart.part->p_frag *
    305 				    dpart.part->p_fsize;
    306 		}
    307 		bscale = bsize >> DEV_BSHIFT;
    308 		do {
    309 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    310 			on = uio->uio_offset % bsize;
    311 			n = min((unsigned)(bsize - on), uio->uio_resid);
    312 			error = bread(vp, bn, bsize, NOCRED, &bp);
    313 			n = min(n, bsize - bp->b_resid);
    314 			if (error) {
    315 				brelse(bp);
    316 				return (error);
    317 			}
    318 			error = uiomove((char *)bp->b_data + on, n, uio);
    319 			brelse(bp);
    320 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    321 		return (error);
    322 
    323 	default:
    324 		panic("spec_read type");
    325 	}
    326 	/* NOTREACHED */
    327 }
    328 
    329 /*
    330  * Vnode op for write
    331  */
    332 /* ARGSUSED */
    333 int
    334 spec_write(v)
    335 	void *v;
    336 {
    337 	struct vop_write_args /* {
    338 		struct vnode *a_vp;
    339 		struct uio *a_uio;
    340 		int  a_ioflag;
    341 		struct ucred *a_cred;
    342 	} */ *ap = v;
    343 	struct vnode *vp = ap->a_vp;
    344 	struct uio *uio = ap->a_uio;
    345 	struct proc *p = uio->uio_procp;
    346 	struct buf *bp;
    347 	daddr_t bn;
    348 	int bsize, bscale;
    349 	struct partinfo dpart;
    350 	int n, on, majordev;
    351 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    352 	int error = 0;
    353 
    354 #ifdef DIAGNOSTIC
    355 	if (uio->uio_rw != UIO_WRITE)
    356 		panic("spec_write mode");
    357 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    358 		panic("spec_write proc");
    359 #endif
    360 
    361 	switch (vp->v_type) {
    362 
    363 	case VCHR:
    364 		VOP_UNLOCK(vp, 0);
    365 		error = (*cdevsw[major(vp->v_rdev)].d_write)
    366 			(vp->v_rdev, uio, ap->a_ioflag);
    367 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    368 		return (error);
    369 
    370 	case VBLK:
    371 		if (uio->uio_resid == 0)
    372 			return (0);
    373 		if (uio->uio_offset < 0)
    374 			return (EINVAL);
    375 		bsize = BLKDEV_IOSIZE;
    376 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    377 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    378 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    379 			if (dpart.part->p_fstype == FS_BSDFFS &&
    380 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    381 				bsize = dpart.part->p_frag *
    382 				    dpart.part->p_fsize;
    383 		}
    384 		bscale = bsize >> DEV_BSHIFT;
    385 		do {
    386 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    387 			on = uio->uio_offset % bsize;
    388 			n = min((unsigned)(bsize - on), uio->uio_resid);
    389 			if (n == bsize)
    390 				bp = getblk(vp, bn, bsize, 0, 0);
    391 			else
    392 				error = bread(vp, bn, bsize, NOCRED, &bp);
    393 			if (error) {
    394 				brelse(bp);
    395 				return (error);
    396 			}
    397 			n = min(n, bsize - bp->b_resid);
    398 			error = uiomove((char *)bp->b_data + on, n, uio);
    399 			if (error)
    400 				brelse(bp);
    401 			else {
    402 				if (n + on == bsize)
    403 					bawrite(bp);
    404 				else
    405 					bdwrite(bp);
    406 				if (bp->b_flags & B_ERROR)
    407 					error = bp->b_error;
    408 			}
    409 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    410 		return (error);
    411 
    412 	default:
    413 		panic("spec_write type");
    414 	}
    415 	/* NOTREACHED */
    416 }
    417 
    418 /*
    419  * Device ioctl operation.
    420  */
    421 /* ARGSUSED */
    422 int
    423 spec_ioctl(v)
    424 	void *v;
    425 {
    426 	struct vop_ioctl_args /* {
    427 		struct vnode *a_vp;
    428 		u_long a_command;
    429 		caddr_t  a_data;
    430 		int  a_fflag;
    431 		struct ucred *a_cred;
    432 		struct proc *a_p;
    433 	} */ *ap = v;
    434 	dev_t dev = ap->a_vp->v_rdev;
    435 	int maj = major(dev);
    436 
    437 	switch (ap->a_vp->v_type) {
    438 
    439 	case VCHR:
    440 		return ((*cdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    441 		    ap->a_fflag, ap->a_p));
    442 
    443 	case VBLK:
    444 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
    445 			if (bdevsw[maj].d_type == D_TAPE)
    446 				return (0);
    447 			else
    448 				return (1);
    449 		}
    450 		return ((*bdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    451 		   ap->a_fflag, ap->a_p));
    452 
    453 	default:
    454 		panic("spec_ioctl");
    455 		/* NOTREACHED */
    456 	}
    457 }
    458 
    459 /* ARGSUSED */
    460 int
    461 spec_poll(v)
    462 	void *v;
    463 {
    464 	struct vop_poll_args /* {
    465 		struct vnode *a_vp;
    466 		int a_events;
    467 		struct proc *a_p;
    468 	} */ *ap = v;
    469 	dev_t dev;
    470 
    471 	switch (ap->a_vp->v_type) {
    472 
    473 	case VCHR:
    474 		dev = ap->a_vp->v_rdev;
    475 		return (*cdevsw[major(dev)].d_poll)(dev, ap->a_events, ap->a_p);
    476 
    477 	default:
    478 		return (genfs_poll(v));
    479 	}
    480 }
    481 /*
    482  * Synch buffers associated with a block device
    483  */
    484 /* ARGSUSED */
    485 int
    486 spec_fsync(v)
    487 	void *v;
    488 {
    489 	struct vop_fsync_args /* {
    490 		struct vnode *a_vp;
    491 		struct ucred *a_cred;
    492 		int  a_flags;
    493 		off_t offlo;
    494 		off_t offhi;
    495 		struct proc *a_p;
    496 	} */ *ap = v;
    497 	struct vnode *vp = ap->a_vp;
    498 
    499 	if (vp->v_type == VBLK)
    500 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    501 	return (0);
    502 }
    503 
    504 /*
    505  * Just call the device strategy routine
    506  */
    507 int
    508 spec_strategy(v)
    509 	void *v;
    510 {
    511 	struct vop_strategy_args /* {
    512 		struct buf *a_bp;
    513 	} */ *ap = v;
    514 	struct buf *bp;
    515 
    516 	bp = ap->a_bp;
    517 	if (!(bp->b_flags & B_READ) &&
    518 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
    519 		(*bioops.io_start)(bp);
    520 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
    521 	return (0);
    522 }
    523 
    524 int
    525 spec_inactive(v)
    526 	void *v;
    527 {
    528 	struct vop_inactive_args /* {
    529 		struct vnode *a_vp;
    530 		struct proc *a_p;
    531 	} */ *ap = v;
    532 
    533 	VOP_UNLOCK(ap->a_vp, 0);
    534 	return (0);
    535 }
    536 
    537 /*
    538  * This is a noop, simply returning what one has been given.
    539  */
    540 int
    541 spec_bmap(v)
    542 	void *v;
    543 {
    544 	struct vop_bmap_args /* {
    545 		struct vnode *a_vp;
    546 		daddr_t  a_bn;
    547 		struct vnode **a_vpp;
    548 		daddr_t *a_bnp;
    549 		int *a_runp;
    550 	} */ *ap = v;
    551 
    552 	if (ap->a_vpp != NULL)
    553 		*ap->a_vpp = ap->a_vp;
    554 	if (ap->a_bnp != NULL)
    555 		*ap->a_bnp = ap->a_bn;
    556 	if (ap->a_runp != NULL)
    557 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    558 	return (0);
    559 }
    560 
    561 /*
    562  * Device close routine
    563  */
    564 /* ARGSUSED */
    565 int
    566 spec_close(v)
    567 	void *v;
    568 {
    569 	struct vop_close_args /* {
    570 		struct vnode *a_vp;
    571 		int  a_fflag;
    572 		struct ucred *a_cred;
    573 		struct proc *a_p;
    574 	} */ *ap = v;
    575 	struct vnode *vp = ap->a_vp;
    576 	dev_t dev = vp->v_rdev;
    577 	int (*devclose) __P((dev_t, int, int, struct proc *));
    578 	int mode, error, count, flags, flags1;
    579 
    580 	count = vcount(vp);
    581 	simple_lock(&vp->v_interlock);
    582 	flags = vp->v_flag;
    583 	simple_unlock(&vp->v_interlock);
    584 
    585 	switch (vp->v_type) {
    586 
    587 	case VCHR:
    588 		/*
    589 		 * Hack: a tty device that is a controlling terminal
    590 		 * has a reference from the session structure.
    591 		 * We cannot easily tell that a character device is
    592 		 * a controlling terminal, unless it is the closing
    593 		 * process' controlling terminal.  In that case,
    594 		 * if the reference count is 2 (this last descriptor
    595 		 * plus the session), release the reference from the session.
    596 		 */
    597 		if (count == 2 && ap->a_p &&
    598 		    vp == ap->a_p->p_session->s_ttyvp) {
    599 			vrele(vp);
    600 			count--;
    601 			ap->a_p->p_session->s_ttyvp = NULL;
    602 		}
    603 		/*
    604 		 * If the vnode is locked, then we are in the midst
    605 		 * of forcably closing the device, otherwise we only
    606 		 * close on last reference.
    607 		 */
    608 		if (count > 1 && (flags & VXLOCK) == 0)
    609 			return (0);
    610 		devclose = cdevsw[major(dev)].d_close;
    611 		mode = S_IFCHR;
    612 		break;
    613 
    614 	case VBLK:
    615 		/*
    616 		 * On last close of a block device (that isn't mounted)
    617 		 * we must invalidate any in core blocks, so that
    618 		 * we can, for instance, change floppy disks.
    619 		 */
    620 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
    621 		if (error)
    622 			return (error);
    623 		/*
    624 		 * We do not want to really close the device if it
    625 		 * is still in use unless we are trying to close it
    626 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    627 		 * holds a reference to the vnode, and because we mark
    628 		 * any other vnodes that alias this device, when the
    629 		 * sum of the reference counts on all the aliased
    630 		 * vnodes descends to one, we are on last close.
    631 		 */
    632 		if (count > 1 && (flags & VXLOCK) == 0)
    633 			return (0);
    634 		devclose = bdevsw[major(dev)].d_close;
    635 		mode = S_IFBLK;
    636 		break;
    637 
    638 	default:
    639 		panic("spec_close: not special");
    640 	}
    641 
    642 	flags1 = ap->a_fflag;
    643 
    644 	/*
    645 	 * if VXLOCK is set, then we're going away soon, so make this
    646 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    647 	 */
    648 	if (flags & VXLOCK)
    649 		flags1 |= FNONBLOCK;
    650 
    651 	/*
    652 	 * If we're able to block, release the vnode lock & reacquire. We
    653 	 * might end up sleaping for someone else who wants our queues. They
    654 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
    655 	 * don't release the lock as we won't be able to regain it.
    656 	 */
    657 	if (!(flags1 & FNONBLOCK))
    658 		VOP_UNLOCK(vp, 0);
    659 
    660 	error =  (*devclose)(dev, flags1, mode, ap->a_p);
    661 
    662 	if (!(flags1 & FNONBLOCK))
    663 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    664 
    665 	return (error);
    666 }
    667 
    668 /*
    669  * Print out the contents of a special device vnode.
    670  */
    671 int
    672 spec_print(v)
    673 	void *v;
    674 {
    675 	struct vop_print_args /* {
    676 		struct vnode *a_vp;
    677 	} */ *ap = v;
    678 
    679 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    680 	    minor(ap->a_vp->v_rdev));
    681 	return 0;
    682 }
    683 
    684 /*
    685  * Return POSIX pathconf information applicable to special devices.
    686  */
    687 int
    688 spec_pathconf(v)
    689 	void *v;
    690 {
    691 	struct vop_pathconf_args /* {
    692 		struct vnode *a_vp;
    693 		int a_name;
    694 		register_t *a_retval;
    695 	} */ *ap = v;
    696 
    697 	switch (ap->a_name) {
    698 	case _PC_LINK_MAX:
    699 		*ap->a_retval = LINK_MAX;
    700 		return (0);
    701 	case _PC_MAX_CANON:
    702 		*ap->a_retval = MAX_CANON;
    703 		return (0);
    704 	case _PC_MAX_INPUT:
    705 		*ap->a_retval = MAX_INPUT;
    706 		return (0);
    707 	case _PC_PIPE_BUF:
    708 		*ap->a_retval = PIPE_BUF;
    709 		return (0);
    710 	case _PC_CHOWN_RESTRICTED:
    711 		*ap->a_retval = 1;
    712 		return (0);
    713 	case _PC_VDISABLE:
    714 		*ap->a_retval = _POSIX_VDISABLE;
    715 		return (0);
    716 	case _PC_SYNC_IO:
    717 		*ap->a_retval = 1;
    718 		return (0);
    719 	default:
    720 		return (EINVAL);
    721 	}
    722 	/* NOTREACHED */
    723 }
    724 
    725 /*
    726  * Advisory record locking support.
    727  */
    728 int
    729 spec_advlock(v)
    730 	void *v;
    731 {
    732 	struct vop_advlock_args /* {
    733 		struct vnode *a_vp;
    734 		caddr_t a_id;
    735 		int a_op;
    736 		struct flock *a_fl;
    737 		int a_flags;
    738 	} */ *ap = v;
    739 	struct vnode *vp = ap->a_vp;
    740 
    741 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    742 }
    743