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