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