Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.106
      1 /*	$NetBSD: spec_vnops.c,v 1.106 2007/10/07 13:39:04 hannken 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.106 2007/10/07 13:39:04 hannken Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/proc.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/conf.h>
     42 #include <sys/buf.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/stat.h>
     47 #include <sys/errno.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/poll.h>
     50 #include <sys/file.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/lockf.h>
     53 #include <sys/tty.h>
     54 #include <sys/kauth.h>
     55 #include <sys/fstrans.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)(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_kqfilter_desc, spec_kqfilter },		/* kqfilter */
     99 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    100 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    101 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    102 	{ &vop_seek_desc, spec_seek },			/* seek */
    103 	{ &vop_remove_desc, spec_remove },		/* remove */
    104 	{ &vop_link_desc, spec_link },			/* link */
    105 	{ &vop_rename_desc, spec_rename },		/* rename */
    106 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    107 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    108 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    109 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    110 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    111 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    112 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    113 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
    114 	{ &vop_lock_desc, spec_lock },			/* lock */
    115 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
    116 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    117 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    118 	{ &vop_print_desc, spec_print },		/* print */
    119 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    120 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    121 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    122 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    123 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    124 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    125 	{ NULL, NULL }
    126 };
    127 const struct vnodeopv_desc spec_vnodeop_opv_desc =
    128 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    129 
    130 /*
    131  * Trivial lookup routine that always fails.
    132  */
    133 int
    134 spec_lookup(void *v)
    135 {
    136 	struct vop_lookup_args /* {
    137 		struct vnode *a_dvp;
    138 		struct vnode **a_vpp;
    139 		struct componentname *a_cnp;
    140 	} */ *ap = v;
    141 
    142 	*ap->a_vpp = NULL;
    143 	return (ENOTDIR);
    144 }
    145 
    146 /*
    147  * Returns true if dev is /dev/mem or /dev/kmem.
    148  */
    149 int
    150 iskmemdev(dev_t dev)
    151 {
    152 	/* mem_no is emitted by config(8) to generated devsw.c */
    153 	extern const int mem_no;
    154 
    155 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
    156 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
    157 }
    158 
    159 /*
    160  * Open a special file.
    161  */
    162 /* ARGSUSED */
    163 int
    164 spec_open(void *v)
    165 {
    166 	struct vop_open_args /* {
    167 		struct vnode *a_vp;
    168 		int  a_mode;
    169 		kauth_cred_t a_cred;
    170 		struct lwp *a_l;
    171 	} */ *ap = v;
    172 	struct lwp *l = ap->a_l;
    173 	struct vnode *vp = ap->a_vp;
    174 	dev_t dev = (dev_t)vp->v_rdev;
    175 	int error;
    176 	struct partinfo pi;
    177 	enum kauth_device_req req;
    178 
    179 	/*
    180 	 * Don't allow open if fs is mounted -nodev.
    181 	 */
    182 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    183 		return (ENXIO);
    184 
    185 #define M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
    186 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_RW : \
    187 		 (m) & FWRITE ? KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE : \
    188 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_READ)
    189 
    190 	switch (vp->v_type) {
    191 
    192 	case VCHR:
    193 		req = M2K(ap->a_mode);
    194 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    195 		if (error)
    196 			return (error);
    197 
    198 		if (cdev_type(dev) == D_TTY)
    199 			vp->v_flag |= VISTTY;
    200 		VOP_UNLOCK(vp, 0);
    201 		error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
    202 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    203 		if (cdev_type(dev) != D_DISK)
    204 			return error;
    205 		break;
    206 
    207 	case VBLK:
    208 		req = M2K(ap->a_mode);
    209 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    210 		if (error)
    211 			return (error);
    212 		error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
    213 		break;
    214 
    215 	case VNON:
    216 	case VLNK:
    217 	case VDIR:
    218 	case VREG:
    219 	case VBAD:
    220 	case VFIFO:
    221 	case VSOCK:
    222 	default:
    223 		return 0;
    224 	}
    225 
    226 #undef M2K
    227 
    228 	if (error)
    229 		return error;
    230 	if (vp->v_type == VCHR)
    231 		error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    232 	else
    233 		error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    234 	if (error == 0)
    235 		uvm_vnp_setsize(vp,
    236 		    (voff_t)pi.disklab->d_secsize * pi.part->p_size);
    237 	return 0;
    238 }
    239 
    240 /*
    241  * Vnode op for read
    242  */
    243 /* ARGSUSED */
    244 int
    245 spec_read(void *v)
    246 {
    247 	struct vop_read_args /* {
    248 		struct vnode *a_vp;
    249 		struct uio *a_uio;
    250 		int  a_ioflag;
    251 		kauth_cred_t a_cred;
    252 	} */ *ap = v;
    253 	struct vnode *vp = ap->a_vp;
    254 	struct uio *uio = ap->a_uio;
    255  	struct lwp *l = curlwp;
    256 	struct buf *bp;
    257 	daddr_t bn;
    258 	int bsize, bscale;
    259 	struct partinfo dpart;
    260 	int n, on;
    261 	int error = 0;
    262 
    263 #ifdef DIAGNOSTIC
    264 	if (uio->uio_rw != UIO_READ)
    265 		panic("spec_read mode");
    266 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    267 	    uio->uio_vmspace != curproc->p_vmspace)
    268 		panic("spec_read proc");
    269 #endif
    270 	if (uio->uio_resid == 0)
    271 		return (0);
    272 
    273 	switch (vp->v_type) {
    274 
    275 	case VCHR:
    276 		VOP_UNLOCK(vp, 0);
    277 		error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
    278 		vn_lock(vp, LK_SHARED | LK_RETRY);
    279 		return (error);
    280 
    281 	case VBLK:
    282 		if (uio->uio_offset < 0)
    283 			return (EINVAL);
    284 		bsize = BLKDEV_IOSIZE;
    285 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    286 			if (dpart.part->p_fstype == FS_BSDFFS &&
    287 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    288 				bsize = dpart.part->p_frag *
    289 				    dpart.part->p_fsize;
    290 		}
    291 		bscale = bsize >> DEV_BSHIFT;
    292 		do {
    293 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    294 			on = uio->uio_offset % bsize;
    295 			n = min((unsigned)(bsize - on), uio->uio_resid);
    296 			error = bread(vp, bn, bsize, NOCRED, &bp);
    297 			n = min(n, bsize - bp->b_resid);
    298 			if (error) {
    299 				brelse(bp);
    300 				return (error);
    301 			}
    302 			error = uiomove((char *)bp->b_data + on, n, uio);
    303 			brelse(bp);
    304 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    305 		return (error);
    306 
    307 	default:
    308 		panic("spec_read type");
    309 	}
    310 	/* NOTREACHED */
    311 }
    312 
    313 /*
    314  * Vnode op for write
    315  */
    316 /* ARGSUSED */
    317 int
    318 spec_write(void *v)
    319 {
    320 	struct vop_write_args /* {
    321 		struct vnode *a_vp;
    322 		struct uio *a_uio;
    323 		int  a_ioflag;
    324 		kauth_cred_t a_cred;
    325 	} */ *ap = v;
    326 	struct vnode *vp = ap->a_vp;
    327 	struct uio *uio = ap->a_uio;
    328 	struct lwp *l = curlwp;
    329 	struct buf *bp;
    330 	daddr_t bn;
    331 	int bsize, bscale;
    332 	struct partinfo dpart;
    333 	int n, on;
    334 	int error = 0;
    335 
    336 #ifdef DIAGNOSTIC
    337 	if (uio->uio_rw != UIO_WRITE)
    338 		panic("spec_write mode");
    339 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    340 	    uio->uio_vmspace != curproc->p_vmspace)
    341 		panic("spec_write proc");
    342 #endif
    343 
    344 	switch (vp->v_type) {
    345 
    346 	case VCHR:
    347 		VOP_UNLOCK(vp, 0);
    348 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
    349 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    350 		return (error);
    351 
    352 	case VBLK:
    353 		if (uio->uio_resid == 0)
    354 			return (0);
    355 		if (uio->uio_offset < 0)
    356 			return (EINVAL);
    357 		bsize = BLKDEV_IOSIZE;
    358 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    359 			if (dpart.part->p_fstype == FS_BSDFFS &&
    360 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    361 				bsize = dpart.part->p_frag *
    362 				    dpart.part->p_fsize;
    363 		}
    364 		bscale = bsize >> DEV_BSHIFT;
    365 		do {
    366 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    367 			on = uio->uio_offset % bsize;
    368 			n = min((unsigned)(bsize - on), uio->uio_resid);
    369 			if (n == bsize)
    370 				bp = getblk(vp, bn, bsize, 0, 0);
    371 			else
    372 				error = bread(vp, bn, bsize, NOCRED, &bp);
    373 			if (error) {
    374 				brelse(bp);
    375 				return (error);
    376 			}
    377 			n = min(n, bsize - bp->b_resid);
    378 			error = uiomove((char *)bp->b_data + on, n, uio);
    379 			if (error)
    380 				brelse(bp);
    381 			else {
    382 				if (n + on == bsize)
    383 					bawrite(bp);
    384 				else
    385 					bdwrite(bp);
    386 				if (bp->b_error != 0)
    387 					error = bp->b_error;
    388 			}
    389 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    390 		return (error);
    391 
    392 	default:
    393 		panic("spec_write type");
    394 	}
    395 	/* NOTREACHED */
    396 }
    397 
    398 /*
    399  * Device ioctl operation.
    400  */
    401 /* ARGSUSED */
    402 int
    403 spec_ioctl(void *v)
    404 {
    405 	struct vop_ioctl_args /* {
    406 		struct vnode *a_vp;
    407 		u_long a_command;
    408 		void  *a_data;
    409 		int  a_fflag;
    410 		kauth_cred_t a_cred;
    411 		struct lwp *a_l;
    412 	} */ *ap = v;
    413 	struct vnode *vp;
    414 	dev_t dev;
    415 
    416 	/*
    417 	 * Extract all the info we need from the vnode, taking care to
    418 	 * avoid a race with VOP_REVOKE().
    419 	 */
    420 
    421 	vp = ap->a_vp;
    422 	dev = NODEV;
    423 	simple_lock(&vp->v_interlock);
    424 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    425 		dev = vp->v_rdev;
    426 	}
    427 	simple_unlock(&vp->v_interlock);
    428 	if (dev == NODEV) {
    429 		return ENXIO;
    430 	}
    431 
    432 	switch (vp->v_type) {
    433 
    434 	case VCHR:
    435 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
    436 		    ap->a_fflag, ap->a_l);
    437 
    438 	case VBLK:
    439 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
    440 			if (bdev_type(dev) == D_TAPE)
    441 				return (0);
    442 			else
    443 				return (1);
    444 		}
    445 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
    446 		   ap->a_fflag, ap->a_l);
    447 
    448 	default:
    449 		panic("spec_ioctl");
    450 		/* NOTREACHED */
    451 	}
    452 }
    453 
    454 /* ARGSUSED */
    455 int
    456 spec_poll(void *v)
    457 {
    458 	struct vop_poll_args /* {
    459 		struct vnode *a_vp;
    460 		int a_events;
    461 		struct lwp *a_l;
    462 	} */ *ap = v;
    463 	struct vnode *vp;
    464 	dev_t dev;
    465 
    466 	/*
    467 	 * Extract all the info we need from the vnode, taking care to
    468 	 * avoid a race with VOP_REVOKE().
    469 	 */
    470 
    471 	vp = ap->a_vp;
    472 	dev = NODEV;
    473 	simple_lock(&vp->v_interlock);
    474 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    475 		dev = vp->v_rdev;
    476 	}
    477 	simple_unlock(&vp->v_interlock);
    478 	if (dev == NODEV) {
    479 		return POLLERR;
    480 	}
    481 
    482 	switch (vp->v_type) {
    483 
    484 	case VCHR:
    485 		return cdev_poll(dev, ap->a_events, ap->a_l);
    486 
    487 	default:
    488 		return (genfs_poll(v));
    489 	}
    490 }
    491 
    492 /* ARGSUSED */
    493 int
    494 spec_kqfilter(void *v)
    495 {
    496 	struct vop_kqfilter_args /* {
    497 		struct vnode	*a_vp;
    498 		struct proc	*a_kn;
    499 	} */ *ap = v;
    500 	dev_t dev;
    501 
    502 	switch (ap->a_vp->v_type) {
    503 
    504 	case VCHR:
    505 		dev = ap->a_vp->v_rdev;
    506 		return cdev_kqfilter(dev, ap->a_kn);
    507 	default:
    508 		/*
    509 		 * Block devices don't support kqfilter, and refuse it
    510 		 * for any other files (like those vflush()ed) too.
    511 		 */
    512 		return (EOPNOTSUPP);
    513 	}
    514 }
    515 
    516 /*
    517  * Allow mapping of only D_DISK.  This is called only for VBLK.
    518  */
    519 int
    520 spec_mmap(void *v)
    521 {
    522 	struct vop_mmap_args /* {
    523 		struct vnode *a_vp;
    524 		vm_prot_t a_prot;
    525 		kauth_cred_t a_cred;
    526 		struct lwp *a_l;
    527 	} */ *ap = v;
    528 	struct vnode *vp = ap->a_vp;
    529 
    530 	KASSERT(vp->v_type == VBLK);
    531 	if (bdev_type(vp->v_rdev) != D_DISK)
    532 		return EINVAL;
    533 
    534 	return 0;
    535 }
    536 
    537 /*
    538  * Synch buffers associated with a block device
    539  */
    540 /* ARGSUSED */
    541 int
    542 spec_fsync(void *v)
    543 {
    544 	struct vop_fsync_args /* {
    545 		struct vnode *a_vp;
    546 		kauth_cred_t a_cred;
    547 		int  a_flags;
    548 		off_t offlo;
    549 		off_t offhi;
    550 		struct lwp *a_l;
    551 	} */ *ap = v;
    552 	struct vnode *vp = ap->a_vp;
    553 
    554 	if (vp->v_type == VBLK)
    555 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    556 	return (0);
    557 }
    558 
    559 /*
    560  * Just call the device strategy routine
    561  */
    562 int
    563 spec_strategy(void *v)
    564 {
    565 	struct vop_strategy_args /* {
    566 		struct vnode *a_vp;
    567 		struct buf *a_bp;
    568 	} */ *ap = v;
    569 	struct vnode *vp = ap->a_vp;
    570 	struct buf *bp = ap->a_bp;
    571 	int error;
    572 
    573 	error = 0;
    574 	bp->b_dev = vp->v_rdev;
    575 	if (!(bp->b_flags & B_READ) &&
    576 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioopsp)
    577 		bioopsp->io_start(bp);
    578 
    579 	if (!(bp->b_flags & B_READ))
    580 		error = fscow_run(bp);
    581 
    582 	if (error) {
    583 		bp->b_error = error;
    584 		biodone(bp);
    585 		return (error);
    586 	}
    587 
    588 	bdev_strategy(bp);
    589 
    590 	return (0);
    591 }
    592 
    593 int
    594 spec_inactive(void *v)
    595 {
    596 	struct vop_inactive_args /* {
    597 		struct vnode *a_vp;
    598 		struct proc *a_l;
    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(void *v)
    610 {
    611 	struct vop_bmap_args /* {
    612 		struct vnode *a_vp;
    613 		daddr_t  a_bn;
    614 		struct vnode **a_vpp;
    615 		daddr_t *a_bnp;
    616 		int *a_runp;
    617 	} */ *ap = v;
    618 
    619 	if (ap->a_vpp != NULL)
    620 		*ap->a_vpp = ap->a_vp;
    621 	if (ap->a_bnp != NULL)
    622 		*ap->a_bnp = ap->a_bn;
    623 	if (ap->a_runp != NULL)
    624 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    625 	return (0);
    626 }
    627 
    628 /*
    629  * Device close routine
    630  */
    631 /* ARGSUSED */
    632 int
    633 spec_close(void *v)
    634 {
    635 	struct vop_close_args /* {
    636 		struct vnode *a_vp;
    637 		int  a_fflag;
    638 		kauth_cred_t a_cred;
    639 		struct lwp *a_l;
    640 	} */ *ap = v;
    641 	struct vnode *vp = ap->a_vp;
    642 	struct session *sess;
    643 	dev_t dev = vp->v_rdev;
    644 	int mode, error, count, flags, flags1;
    645 
    646 	count = vcount(vp);
    647 	flags = vp->v_flag;
    648 
    649 	switch (vp->v_type) {
    650 
    651 	case VCHR:
    652 		/*
    653 		 * Hack: a tty device that is a controlling terminal
    654 		 * has a reference from the session structure.
    655 		 * We cannot easily tell that a character device is
    656 		 * a controlling terminal, unless it is the closing
    657 		 * process' controlling terminal.  In that case,
    658 		 * if the reference count is 2 (this last descriptor
    659 		 * plus the session), release the reference from the session.
    660 		 * Also remove the link from the tty back to the session
    661 		 * and pgrp - due to the way consoles are handled we cannot
    662 		 * guarantee that the vrele() will do the final close on the
    663 		 * actual tty device.
    664 		 */
    665 		mutex_enter(&proclist_lock);
    666 		if (count == 2 && ap->a_l &&
    667 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
    668 			sess->s_ttyvp = NULL;
    669 			if (sess->s_ttyp->t_session != NULL) {
    670 				sess->s_ttyp->t_pgrp = NULL;
    671 				sess->s_ttyp->t_session = NULL;
    672 				mutex_exit(&proclist_lock);
    673 				SESSRELE(sess);
    674 			} else {
    675 				if (sess->s_ttyp->t_pgrp != NULL)
    676 					panic("spec_close: spurious pgrp ref");
    677 				mutex_exit(&proclist_lock);
    678 			}
    679 			vrele(vp);
    680 			count--;
    681 		} else
    682 			mutex_exit(&proclist_lock);
    683 
    684 		/*
    685 		 * If the vnode is locked, then we are in the midst
    686 		 * of forcably closing the device, otherwise we only
    687 		 * close on last reference.
    688 		 */
    689 		if (count > 1 && (flags & VXLOCK) == 0)
    690 			return (0);
    691 		mode = S_IFCHR;
    692 		break;
    693 
    694 	case VBLK:
    695 		/*
    696 		 * On last close of a block device (that isn't mounted)
    697 		 * we must invalidate any in core blocks, so that
    698 		 * we can, for instance, change floppy disks.
    699 		 */
    700 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
    701 		if (error)
    702 			return (error);
    703 		/*
    704 		 * We do not want to really close the device if it
    705 		 * is still in use unless we are trying to close it
    706 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    707 		 * holds a reference to the vnode, and because we mark
    708 		 * any other vnodes that alias this device, when the
    709 		 * sum of the reference counts on all the aliased
    710 		 * vnodes descends to one, we are on last close.
    711 		 */
    712 		if (count > 1 && (flags & VXLOCK) == 0)
    713 			return (0);
    714 		mode = S_IFBLK;
    715 		break;
    716 
    717 	default:
    718 		panic("spec_close: not special");
    719 	}
    720 
    721 	flags1 = ap->a_fflag;
    722 
    723 	/*
    724 	 * if VXLOCK is set, then we're going away soon, so make this
    725 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    726 	 */
    727 	if (flags & VXLOCK)
    728 		flags1 |= FNONBLOCK;
    729 
    730 	/*
    731 	 * If we're able to block, release the vnode lock & reacquire. We
    732 	 * might end up sleeping for someone else who wants our queues. They
    733 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is
    734 	 * set, don't release the lock as we won't be able to regain it.
    735 	 */
    736 	if (!(flags1 & FNONBLOCK))
    737 		VOP_UNLOCK(vp, 0);
    738 
    739 	if (vp->v_type == VBLK)
    740 		error = bdev_close(dev, flags1, mode, ap->a_l);
    741 	else
    742 		error = cdev_close(dev, flags1, mode, ap->a_l);
    743 
    744 	if (!(flags1 & FNONBLOCK))
    745 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    746 
    747 	return (error);
    748 }
    749 
    750 /*
    751  * Print out the contents of a special device vnode.
    752  */
    753 int
    754 spec_print(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(void *v)
    770 {
    771 	struct vop_pathconf_args /* {
    772 		struct vnode *a_vp;
    773 		int a_name;
    774 		register_t *a_retval;
    775 	} */ *ap = v;
    776 
    777 	switch (ap->a_name) {
    778 	case _PC_LINK_MAX:
    779 		*ap->a_retval = LINK_MAX;
    780 		return (0);
    781 	case _PC_MAX_CANON:
    782 		*ap->a_retval = MAX_CANON;
    783 		return (0);
    784 	case _PC_MAX_INPUT:
    785 		*ap->a_retval = MAX_INPUT;
    786 		return (0);
    787 	case _PC_PIPE_BUF:
    788 		*ap->a_retval = PIPE_BUF;
    789 		return (0);
    790 	case _PC_CHOWN_RESTRICTED:
    791 		*ap->a_retval = 1;
    792 		return (0);
    793 	case _PC_VDISABLE:
    794 		*ap->a_retval = _POSIX_VDISABLE;
    795 		return (0);
    796 	case _PC_SYNC_IO:
    797 		*ap->a_retval = 1;
    798 		return (0);
    799 	default:
    800 		return (EINVAL);
    801 	}
    802 	/* NOTREACHED */
    803 }
    804 
    805 /*
    806  * Advisory record locking support.
    807  */
    808 int
    809 spec_advlock(void *v)
    810 {
    811 	struct vop_advlock_args /* {
    812 		struct vnode *a_vp;
    813 		void *a_id;
    814 		int a_op;
    815 		struct flock *a_fl;
    816 		int a_flags;
    817 	} */ *ap = v;
    818 	struct vnode *vp = ap->a_vp;
    819 
    820 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    821 }
    822