Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.176
      1 /*	$NetBSD: spec_vnops.c,v 1.176 2019/09/22 22:59:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright (c) 1989, 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  * 3. Neither the name of the University nor the names of its contributors
     42  *    may be used to endorse or promote products derived from this software
     43  *    without specific prior written permission.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  *
     57  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
     58  */
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.176 2019/09/22 22:59:39 christos Exp $");
     62 
     63 #include <sys/param.h>
     64 #include <sys/proc.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/conf.h>
     68 #include <sys/buf.h>
     69 #include <sys/mount.h>
     70 #include <sys/namei.h>
     71 #include <sys/vnode_impl.h>
     72 #include <sys/stat.h>
     73 #include <sys/errno.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/poll.h>
     76 #include <sys/file.h>
     77 #include <sys/disklabel.h>
     78 #include <sys/disk.h>
     79 #include <sys/lockf.h>
     80 #include <sys/tty.h>
     81 #include <sys/kauth.h>
     82 #include <sys/fstrans.h>
     83 #include <sys/module.h>
     84 
     85 #include <miscfs/genfs/genfs.h>
     86 #include <miscfs/specfs/specdev.h>
     87 
     88 /* symbolic sleep message strings for devices */
     89 const char	devopn[] = "devopn";
     90 const char	devio[] = "devio";
     91 const char	devwait[] = "devwait";
     92 const char	devin[] = "devin";
     93 const char	devout[] = "devout";
     94 const char	devioc[] = "devioc";
     95 const char	devcls[] = "devcls";
     96 
     97 #define	SPECHSZ	64
     98 #if	((SPECHSZ&(SPECHSZ-1)) == 0)
     99 #define	SPECHASH(rdev)	(((rdev>>5)+(rdev))&(SPECHSZ-1))
    100 #else
    101 #define	SPECHASH(rdev)	(((unsigned)((rdev>>5)+(rdev)))%SPECHSZ)
    102 #endif
    103 
    104 static vnode_t	*specfs_hash[SPECHSZ];
    105 extern struct mount *dead_rootmount;
    106 
    107 /*
    108  * This vnode operations vector is used for special device nodes
    109  * created from whole cloth by the kernel.  For the ops vector for
    110  * vnodes built from special devices found in a filesystem, see (e.g)
    111  * ffs_specop_entries[] in ffs_vnops.c or the equivalent for other
    112  * filesystems.
    113  */
    114 
    115 int (**spec_vnodeop_p)(void *);
    116 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
    117 	{ &vop_default_desc, vn_default_error },
    118 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    119 	{ &vop_create_desc, spec_create },		/* create */
    120 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    121 	{ &vop_open_desc, spec_open },			/* open */
    122 	{ &vop_close_desc, spec_close },		/* close */
    123 	{ &vop_access_desc, spec_access },		/* access */
    124 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
    125 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
    126 	{ &vop_read_desc, spec_read },			/* read */
    127 	{ &vop_write_desc, spec_write },		/* write */
    128 	{ &vop_fallocate_desc, spec_fallocate },	/* fallocate */
    129 	{ &vop_fdiscard_desc, spec_fdiscard },		/* fdiscard */
    130 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
    131 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    132 	{ &vop_poll_desc, spec_poll },			/* poll */
    133 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    134 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    135 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    136 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    137 	{ &vop_seek_desc, spec_seek },			/* seek */
    138 	{ &vop_remove_desc, spec_remove },		/* remove */
    139 	{ &vop_link_desc, spec_link },			/* link */
    140 	{ &vop_rename_desc, spec_rename },		/* rename */
    141 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    142 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    143 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    144 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    145 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    146 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    147 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    148 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
    149 	{ &vop_lock_desc, spec_lock },			/* lock */
    150 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
    151 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    152 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    153 	{ &vop_print_desc, spec_print },		/* print */
    154 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    155 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    156 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    157 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    158 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    159 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    160 	{ NULL, NULL }
    161 };
    162 const struct vnodeopv_desc spec_vnodeop_opv_desc =
    163 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    164 
    165 static kauth_listener_t rawio_listener;
    166 
    167 /* Returns true if vnode is /dev/mem or /dev/kmem. */
    168 bool
    169 iskmemvp(struct vnode *vp)
    170 {
    171 	return ((vp->v_type == VCHR) && iskmemdev(vp->v_rdev));
    172 }
    173 
    174 /*
    175  * Returns true if dev is /dev/mem or /dev/kmem.
    176  */
    177 int
    178 iskmemdev(dev_t dev)
    179 {
    180 	/* mem_no is emitted by config(8) to generated devsw.c */
    181 	extern const int mem_no;
    182 
    183 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
    184 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
    185 }
    186 
    187 static int
    188 rawio_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    189     void *arg0, void *arg1, void *arg2, void *arg3)
    190 {
    191 	int result;
    192 
    193 	result = KAUTH_RESULT_DEFER;
    194 
    195 	if ((action != KAUTH_DEVICE_RAWIO_SPEC) &&
    196 	    (action != KAUTH_DEVICE_RAWIO_PASSTHRU))
    197 		return result;
    198 
    199 	/* Access is mandated by permissions. */
    200 	result = KAUTH_RESULT_ALLOW;
    201 
    202 	return result;
    203 }
    204 
    205 void
    206 spec_init(void)
    207 {
    208 
    209 	rawio_listener = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
    210 	    rawio_listener_cb, NULL);
    211 }
    212 
    213 /*
    214  * Initialize a vnode that represents a device.
    215  */
    216 void
    217 spec_node_init(vnode_t *vp, dev_t rdev)
    218 {
    219 	specnode_t *sn;
    220 	specdev_t *sd;
    221 	vnode_t *vp2;
    222 	vnode_t **vpp;
    223 
    224 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    225 	KASSERT(vp->v_specnode == NULL);
    226 
    227 	/*
    228 	 * Search the hash table for this device.  If known, add a
    229 	 * reference to the device structure.  If not known, create
    230 	 * a new entry to represent the device.  In all cases add
    231 	 * the vnode to the hash table.
    232 	 */
    233 	sn = kmem_alloc(sizeof(*sn), KM_SLEEP);
    234 	sd = kmem_alloc(sizeof(*sd), KM_SLEEP);
    235 	mutex_enter(&device_lock);
    236 	vpp = &specfs_hash[SPECHASH(rdev)];
    237 	for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) {
    238 		KASSERT(vp2->v_specnode != NULL);
    239 		if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) {
    240 			break;
    241 		}
    242 	}
    243 	if (vp2 == NULL) {
    244 		/* No existing record, create a new one. */
    245 		sd->sd_rdev = rdev;
    246 		sd->sd_mountpoint = NULL;
    247 		sd->sd_lockf = NULL;
    248 		sd->sd_refcnt = 1;
    249 		sd->sd_opencnt = 0;
    250 		sd->sd_bdevvp = NULL;
    251 		sn->sn_dev = sd;
    252 		sd = NULL;
    253 	} else {
    254 		/* Use the existing record. */
    255 		sn->sn_dev = vp2->v_specnode->sn_dev;
    256 		sn->sn_dev->sd_refcnt++;
    257 	}
    258 	/* Insert vnode into the hash chain. */
    259 	sn->sn_opencnt = 0;
    260 	sn->sn_rdev = rdev;
    261 	sn->sn_gone = false;
    262 	vp->v_specnode = sn;
    263 	vp->v_specnext = *vpp;
    264 	*vpp = vp;
    265 	mutex_exit(&device_lock);
    266 
    267 	/* Free the record we allocated if unused. */
    268 	if (sd != NULL) {
    269 		kmem_free(sd, sizeof(*sd));
    270 	}
    271 }
    272 
    273 /*
    274  * Lookup a vnode by device number and return it referenced.
    275  */
    276 int
    277 spec_node_lookup_by_dev(enum vtype type, dev_t dev, vnode_t **vpp)
    278 {
    279 	int error;
    280 	vnode_t *vp;
    281 
    282 	mutex_enter(&device_lock);
    283 	for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
    284 		if (type == vp->v_type && dev == vp->v_rdev) {
    285 			mutex_enter(vp->v_interlock);
    286 			/* If clean or being cleaned, then ignore it. */
    287 			if (vdead_check(vp, VDEAD_NOWAIT) == 0)
    288 				break;
    289 			mutex_exit(vp->v_interlock);
    290 		}
    291 	}
    292 	KASSERT(vp == NULL || mutex_owned(vp->v_interlock));
    293 	if (vp == NULL) {
    294 		mutex_exit(&device_lock);
    295 		return ENOENT;
    296 	}
    297 	/*
    298 	 * If it is an opened block device return the opened vnode.
    299 	 */
    300 	if (type == VBLK && vp->v_specnode->sn_dev->sd_bdevvp != NULL) {
    301 		mutex_exit(vp->v_interlock);
    302 		vp = vp->v_specnode->sn_dev->sd_bdevvp;
    303 		mutex_enter(vp->v_interlock);
    304 	}
    305 	mutex_exit(&device_lock);
    306 	error = vcache_vget(vp);
    307 	if (error != 0)
    308 		return error;
    309 	*vpp = vp;
    310 
    311 	return 0;
    312 }
    313 
    314 /*
    315  * Lookup a vnode by file system mounted on and return it referenced.
    316  */
    317 int
    318 spec_node_lookup_by_mount(struct mount *mp, vnode_t **vpp)
    319 {
    320 	int i, error;
    321 	vnode_t *vp, *vq;
    322 
    323 	mutex_enter(&device_lock);
    324 	for (i = 0, vq = NULL; i < SPECHSZ && vq == NULL; i++) {
    325 		for (vp = specfs_hash[i]; vp; vp = vp->v_specnext) {
    326 			if (vp->v_type != VBLK)
    327 				continue;
    328 			vq = vp->v_specnode->sn_dev->sd_bdevvp;
    329 			if (vq != NULL &&
    330 			    vq->v_specnode->sn_dev->sd_mountpoint == mp)
    331 				break;
    332 			vq = NULL;
    333 		}
    334 	}
    335 	if (vq == NULL) {
    336 		mutex_exit(&device_lock);
    337 		return ENOENT;
    338 	}
    339 	mutex_enter(vq->v_interlock);
    340 	mutex_exit(&device_lock);
    341 	error = vcache_vget(vq);
    342 	if (error != 0)
    343 		return error;
    344 	*vpp = vq;
    345 
    346 	return 0;
    347 
    348 }
    349 
    350 /*
    351  * Get the file system mounted on this block device.
    352  */
    353 struct mount *
    354 spec_node_getmountedfs(vnode_t *devvp)
    355 {
    356 	struct mount *mp;
    357 
    358 	KASSERT(devvp->v_type == VBLK);
    359 	mp = devvp->v_specnode->sn_dev->sd_mountpoint;
    360 
    361 	return mp;
    362 }
    363 
    364 /*
    365  * Set the file system mounted on this block device.
    366  */
    367 void
    368 spec_node_setmountedfs(vnode_t *devvp, struct mount *mp)
    369 {
    370 	struct dkwedge_info dkw;
    371 
    372 	KASSERT(devvp->v_type == VBLK);
    373 	KASSERT(devvp->v_specnode->sn_dev->sd_mountpoint == NULL || mp == NULL);
    374 	devvp->v_specnode->sn_dev->sd_mountpoint = mp;
    375 	if (mp == NULL)
    376 		return;
    377 
    378 	if (bdev_ioctl(devvp->v_rdev, DIOCGWEDGEINFO, &dkw, FREAD, curlwp) != 0)
    379 		return;
    380 
    381 	strlcpy(mp->mnt_stat.f_mntfromlabel, dkw.dkw_wname,
    382 	    sizeof(mp->mnt_stat.f_mntfromlabel));
    383 }
    384 
    385 /*
    386  * A vnode representing a special device is going away.  Close
    387  * the device if the vnode holds it open.
    388  */
    389 void
    390 spec_node_revoke(vnode_t *vp)
    391 {
    392 	specnode_t *sn;
    393 	specdev_t *sd;
    394 
    395 	sn = vp->v_specnode;
    396 	sd = sn->sn_dev;
    397 
    398 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    399 	KASSERT(vp->v_specnode != NULL);
    400 	KASSERT(sn->sn_gone == false);
    401 
    402 	mutex_enter(&device_lock);
    403 	KASSERT(sn->sn_opencnt <= sd->sd_opencnt);
    404 	if (sn->sn_opencnt != 0) {
    405 		sd->sd_opencnt -= (sn->sn_opencnt - 1);
    406 		sn->sn_opencnt = 1;
    407 		sn->sn_gone = true;
    408 		mutex_exit(&device_lock);
    409 
    410 		VOP_CLOSE(vp, FNONBLOCK, NOCRED);
    411 
    412 		mutex_enter(&device_lock);
    413 		KASSERT(sn->sn_opencnt == 0);
    414 	}
    415 	mutex_exit(&device_lock);
    416 }
    417 
    418 /*
    419  * A vnode representing a special device is being recycled.
    420  * Destroy the specfs component.
    421  */
    422 void
    423 spec_node_destroy(vnode_t *vp)
    424 {
    425 	specnode_t *sn;
    426 	specdev_t *sd;
    427 	vnode_t **vpp, *vp2;
    428 	int refcnt;
    429 
    430 	sn = vp->v_specnode;
    431 	sd = sn->sn_dev;
    432 
    433 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    434 	KASSERT(vp->v_specnode != NULL);
    435 	KASSERT(sn->sn_opencnt == 0);
    436 
    437 	mutex_enter(&device_lock);
    438 	/* Remove from the hash and destroy the node. */
    439 	vpp = &specfs_hash[SPECHASH(vp->v_rdev)];
    440 	for (vp2 = *vpp;; vp2 = vp2->v_specnext) {
    441 		if (vp2 == NULL) {
    442 			panic("spec_node_destroy: corrupt hash");
    443 		}
    444 		if (vp2 == vp) {
    445 			KASSERT(vp == *vpp);
    446 			*vpp = vp->v_specnext;
    447 			break;
    448 		}
    449 		if (vp2->v_specnext == vp) {
    450 			vp2->v_specnext = vp->v_specnext;
    451 			break;
    452 		}
    453 	}
    454 	sn = vp->v_specnode;
    455 	vp->v_specnode = NULL;
    456 	refcnt = sd->sd_refcnt--;
    457 	KASSERT(refcnt > 0);
    458 	mutex_exit(&device_lock);
    459 
    460 	/* If the device is no longer in use, destroy our record. */
    461 	if (refcnt == 1) {
    462 		KASSERT(sd->sd_opencnt == 0);
    463 		KASSERT(sd->sd_bdevvp == NULL);
    464 		kmem_free(sd, sizeof(*sd));
    465 	}
    466 	kmem_free(sn, sizeof(*sn));
    467 }
    468 
    469 /*
    470  * Trivial lookup routine that always fails.
    471  */
    472 int
    473 spec_lookup(void *v)
    474 {
    475 	struct vop_lookup_v2_args /* {
    476 		struct vnode *a_dvp;
    477 		struct vnode **a_vpp;
    478 		struct componentname *a_cnp;
    479 	} */ *ap = v;
    480 
    481 	*ap->a_vpp = NULL;
    482 	return (ENOTDIR);
    483 }
    484 
    485 typedef int (*spec_ioctl_t)(dev_t, u_long, void *, int, struct lwp *);
    486 
    487 /*
    488  * Open a special file.
    489  */
    490 /* ARGSUSED */
    491 int
    492 spec_open(void *v)
    493 {
    494 	struct vop_open_args /* {
    495 		struct vnode *a_vp;
    496 		int  a_mode;
    497 		kauth_cred_t a_cred;
    498 	} */ *ap = v;
    499 	struct lwp *l;
    500 	struct vnode *vp;
    501 	dev_t dev;
    502 	int error;
    503 	enum kauth_device_req req;
    504 	specnode_t *sn;
    505 	specdev_t *sd;
    506 	spec_ioctl_t ioctl;
    507 	u_int gen;
    508 	const char *name;
    509 	struct partinfo pi;
    510 
    511 	l = curlwp;
    512 	vp = ap->a_vp;
    513 	dev = vp->v_rdev;
    514 	sn = vp->v_specnode;
    515 	sd = sn->sn_dev;
    516 	name = NULL;
    517 	gen = 0;
    518 
    519 	/*
    520 	 * Don't allow open if fs is mounted -nodev.
    521 	 */
    522 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    523 		return (ENXIO);
    524 
    525 	switch (ap->a_mode & (FREAD | FWRITE)) {
    526 	case FREAD | FWRITE:
    527 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
    528 		break;
    529 	case FWRITE:
    530 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
    531 		break;
    532 	default:
    533 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
    534 		break;
    535 	}
    536 
    537 	switch (vp->v_type) {
    538 	case VCHR:
    539 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    540 		if (error != 0)
    541 			return (error);
    542 
    543 		/*
    544 		 * Character devices can accept opens from multiple
    545 		 * vnodes.
    546 		 */
    547 		mutex_enter(&device_lock);
    548 		if (sn->sn_gone) {
    549 			mutex_exit(&device_lock);
    550 			return (EBADF);
    551 		}
    552 		sd->sd_opencnt++;
    553 		sn->sn_opencnt++;
    554 		mutex_exit(&device_lock);
    555 		if (cdev_type(dev) == D_TTY)
    556 			vp->v_vflag |= VV_ISTTY;
    557 		VOP_UNLOCK(vp);
    558 		do {
    559 			const struct cdevsw *cdev;
    560 
    561 			gen = module_gen;
    562 			error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
    563 			if (error != ENXIO)
    564 				break;
    565 
    566 			/* Check if we already have a valid driver */
    567 			mutex_enter(&device_lock);
    568 			cdev = cdevsw_lookup(dev);
    569 			mutex_exit(&device_lock);
    570 			if (cdev != NULL)
    571 				break;
    572 
    573 			/* Get device name from devsw_conv array */
    574 			if ((name = cdevsw_getname(major(dev))) == NULL)
    575 				break;
    576 
    577 			/* Try to autoload device module */
    578 			(void) module_autoload(name, MODULE_CLASS_DRIVER);
    579 		} while (gen != module_gen);
    580 
    581 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    582 		break;
    583 
    584 	case VBLK:
    585 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    586 		if (error != 0)
    587 			return (error);
    588 
    589 		/*
    590 		 * For block devices, permit only one open.  The buffer
    591 		 * cache cannot remain self-consistent with multiple
    592 		 * vnodes holding a block device open.
    593 		 *
    594 		 * Treat zero opencnt with non-NULL mountpoint as open.
    595 		 * This may happen after forced detach of a mounted device.
    596 		 */
    597 		mutex_enter(&device_lock);
    598 		if (sn->sn_gone) {
    599 			mutex_exit(&device_lock);
    600 			return (EBADF);
    601 		}
    602 		if (sd->sd_opencnt != 0 || sd->sd_mountpoint != NULL) {
    603 			mutex_exit(&device_lock);
    604 			return EBUSY;
    605 		}
    606 		sn->sn_opencnt = 1;
    607 		sd->sd_opencnt = 1;
    608 		sd->sd_bdevvp = vp;
    609 		mutex_exit(&device_lock);
    610 		do {
    611 			const struct bdevsw *bdev;
    612 
    613 			gen = module_gen;
    614 			error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
    615 			if (error != ENXIO)
    616 				break;
    617 
    618 			/* Check if we already have a valid driver */
    619 			mutex_enter(&device_lock);
    620 			bdev = bdevsw_lookup(dev);
    621 			mutex_exit(&device_lock);
    622 			if (bdev != NULL)
    623 				break;
    624 
    625 			/* Get device name from devsw_conv array */
    626 			if ((name = bdevsw_getname(major(dev))) == NULL)
    627 				break;
    628 
    629 			VOP_UNLOCK(vp);
    630 
    631                         /* Try to autoload device module */
    632 			(void) module_autoload(name, MODULE_CLASS_DRIVER);
    633 
    634 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    635 		} while (gen != module_gen);
    636 
    637 		break;
    638 
    639 	case VNON:
    640 	case VLNK:
    641 	case VDIR:
    642 	case VREG:
    643 	case VBAD:
    644 	case VFIFO:
    645 	case VSOCK:
    646 	default:
    647 		return 0;
    648 	}
    649 
    650 	mutex_enter(&device_lock);
    651 	if (sn->sn_gone) {
    652 		if (error == 0)
    653 			error = EBADF;
    654 	} else if (error != 0) {
    655 		sd->sd_opencnt--;
    656 		sn->sn_opencnt--;
    657 		if (vp->v_type == VBLK)
    658 			sd->sd_bdevvp = NULL;
    659 
    660 	}
    661 	mutex_exit(&device_lock);
    662 
    663 	if (cdev_type(dev) != D_DISK || error != 0)
    664 		return error;
    665 
    666 
    667 	ioctl = vp->v_type == VCHR ? cdev_ioctl : bdev_ioctl;
    668 	error = (*ioctl)(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, curlwp);
    669 	if (error == 0)
    670 		uvm_vnp_setsize(vp, (voff_t)pi.pi_secsize * pi.pi_size);
    671 
    672 	return 0;
    673 }
    674 
    675 /*
    676  * Vnode op for read
    677  */
    678 /* ARGSUSED */
    679 int
    680 spec_read(void *v)
    681 {
    682 	struct vop_read_args /* {
    683 		struct vnode *a_vp;
    684 		struct uio *a_uio;
    685 		int  a_ioflag;
    686 		kauth_cred_t a_cred;
    687 	} */ *ap = v;
    688 	struct vnode *vp = ap->a_vp;
    689 	struct uio *uio = ap->a_uio;
    690  	struct lwp *l = curlwp;
    691 	struct buf *bp;
    692 	daddr_t bn;
    693 	int bsize, bscale;
    694 	struct partinfo pi;
    695 	int n, on;
    696 	int error = 0;
    697 
    698 	KASSERT(uio->uio_rw == UIO_READ);
    699 	KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
    700 		   uio->uio_vmspace == curproc->p_vmspace,
    701 		"vmspace belongs to neither kernel nor curproc");
    702 
    703 	if (uio->uio_resid == 0)
    704 		return (0);
    705 
    706 	switch (vp->v_type) {
    707 
    708 	case VCHR:
    709 		VOP_UNLOCK(vp);
    710 		error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
    711 		vn_lock(vp, LK_SHARED | LK_RETRY);
    712 		return (error);
    713 
    714 	case VBLK:
    715 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    716 		if (uio->uio_offset < 0)
    717 			return (EINVAL);
    718 
    719 		if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0)
    720 			bsize = pi.pi_bsize;
    721 		else
    722 			bsize = BLKDEV_IOSIZE;
    723 
    724 		bscale = bsize >> DEV_BSHIFT;
    725 		do {
    726 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    727 			on = uio->uio_offset % bsize;
    728 			n = uimin((unsigned)(bsize - on), uio->uio_resid);
    729 			error = bread(vp, bn, bsize, 0, &bp);
    730 			if (error) {
    731 				return (error);
    732 			}
    733 			n = uimin(n, bsize - bp->b_resid);
    734 			error = uiomove((char *)bp->b_data + on, n, uio);
    735 			brelse(bp, 0);
    736 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    737 		return (error);
    738 
    739 	default:
    740 		panic("spec_read type");
    741 	}
    742 	/* NOTREACHED */
    743 }
    744 
    745 /*
    746  * Vnode op for write
    747  */
    748 /* ARGSUSED */
    749 int
    750 spec_write(void *v)
    751 {
    752 	struct vop_write_args /* {
    753 		struct vnode *a_vp;
    754 		struct uio *a_uio;
    755 		int  a_ioflag;
    756 		kauth_cred_t a_cred;
    757 	} */ *ap = v;
    758 	struct vnode *vp = ap->a_vp;
    759 	struct uio *uio = ap->a_uio;
    760 	struct lwp *l = curlwp;
    761 	struct buf *bp;
    762 	daddr_t bn;
    763 	int bsize, bscale;
    764 	struct partinfo pi;
    765 	int n, on;
    766 	int error = 0;
    767 
    768 	KASSERT(uio->uio_rw == UIO_WRITE);
    769 	KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
    770 		   uio->uio_vmspace == curproc->p_vmspace,
    771 		"vmspace belongs to neither kernel nor curproc");
    772 
    773 	switch (vp->v_type) {
    774 
    775 	case VCHR:
    776 		VOP_UNLOCK(vp);
    777 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
    778 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    779 		return (error);
    780 
    781 	case VBLK:
    782 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    783 		if (uio->uio_resid == 0)
    784 			return (0);
    785 		if (uio->uio_offset < 0)
    786 			return (EINVAL);
    787 
    788 		if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0)
    789 			bsize = pi.pi_bsize;
    790 		else
    791 			bsize = BLKDEV_IOSIZE;
    792 
    793 		bscale = bsize >> DEV_BSHIFT;
    794 		do {
    795 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    796 			on = uio->uio_offset % bsize;
    797 			n = uimin((unsigned)(bsize - on), uio->uio_resid);
    798 			if (n == bsize)
    799 				bp = getblk(vp, bn, bsize, 0, 0);
    800 			else
    801 				error = bread(vp, bn, bsize, B_MODIFY, &bp);
    802 			if (error) {
    803 				return (error);
    804 			}
    805 			n = uimin(n, bsize - bp->b_resid);
    806 			error = uiomove((char *)bp->b_data + on, n, uio);
    807 			if (error)
    808 				brelse(bp, 0);
    809 			else {
    810 				if (n + on == bsize)
    811 					bawrite(bp);
    812 				else
    813 					bdwrite(bp);
    814 				error = bp->b_error;
    815 			}
    816 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    817 		return (error);
    818 
    819 	default:
    820 		panic("spec_write type");
    821 	}
    822 	/* NOTREACHED */
    823 }
    824 
    825 /*
    826  * fdiscard, which on disk devices becomes TRIM.
    827  */
    828 int
    829 spec_fdiscard(void *v)
    830 {
    831 	struct vop_fdiscard_args /* {
    832 		struct vnode *a_vp;
    833 		off_t a_pos;
    834 		off_t a_len;
    835 	} */ *ap = v;
    836 	struct vnode *vp;
    837 	dev_t dev;
    838 
    839 	vp = ap->a_vp;
    840 	dev = NODEV;
    841 
    842 	mutex_enter(vp->v_interlock);
    843 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) {
    844 		dev = vp->v_rdev;
    845 	}
    846 	mutex_exit(vp->v_interlock);
    847 
    848 	if (dev == NODEV) {
    849 		return ENXIO;
    850 	}
    851 
    852 	switch (vp->v_type) {
    853 	    case VCHR:
    854 		// this is not stored for character devices
    855 		//KASSERT(vp == vp->v_specnode->sn_dev->sd_cdevvp);
    856 		return cdev_discard(dev, ap->a_pos, ap->a_len);
    857 	    case VBLK:
    858 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    859 		return bdev_discard(dev, ap->a_pos, ap->a_len);
    860 	    default:
    861 		panic("spec_fdiscard: not a device\n");
    862 	}
    863 }
    864 
    865 /*
    866  * Device ioctl operation.
    867  */
    868 /* ARGSUSED */
    869 int
    870 spec_ioctl(void *v)
    871 {
    872 	struct vop_ioctl_args /* {
    873 		struct vnode *a_vp;
    874 		u_long a_command;
    875 		void  *a_data;
    876 		int  a_fflag;
    877 		kauth_cred_t a_cred;
    878 	} */ *ap = v;
    879 	struct vnode *vp;
    880 	dev_t dev;
    881 
    882 	/*
    883 	 * Extract all the info we need from the vnode, taking care to
    884 	 * avoid a race with VOP_REVOKE().
    885 	 */
    886 
    887 	vp = ap->a_vp;
    888 	dev = NODEV;
    889 	mutex_enter(vp->v_interlock);
    890 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
    891 		dev = vp->v_rdev;
    892 	}
    893 	mutex_exit(vp->v_interlock);
    894 	if (dev == NODEV) {
    895 		return ENXIO;
    896 	}
    897 
    898 	switch (vp->v_type) {
    899 
    900 	case VCHR:
    901 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
    902 		    ap->a_fflag, curlwp);
    903 
    904 	case VBLK:
    905 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    906 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
    907 		   ap->a_fflag, curlwp);
    908 
    909 	default:
    910 		panic("spec_ioctl");
    911 		/* NOTREACHED */
    912 	}
    913 }
    914 
    915 /* ARGSUSED */
    916 int
    917 spec_poll(void *v)
    918 {
    919 	struct vop_poll_args /* {
    920 		struct vnode *a_vp;
    921 		int a_events;
    922 	} */ *ap = v;
    923 	struct vnode *vp;
    924 	dev_t dev;
    925 
    926 	/*
    927 	 * Extract all the info we need from the vnode, taking care to
    928 	 * avoid a race with VOP_REVOKE().
    929 	 */
    930 
    931 	vp = ap->a_vp;
    932 	dev = NODEV;
    933 	mutex_enter(vp->v_interlock);
    934 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
    935 		dev = vp->v_rdev;
    936 	}
    937 	mutex_exit(vp->v_interlock);
    938 	if (dev == NODEV) {
    939 		return POLLERR;
    940 	}
    941 
    942 	switch (vp->v_type) {
    943 
    944 	case VCHR:
    945 		return cdev_poll(dev, ap->a_events, curlwp);
    946 
    947 	default:
    948 		return (genfs_poll(v));
    949 	}
    950 }
    951 
    952 /* ARGSUSED */
    953 int
    954 spec_kqfilter(void *v)
    955 {
    956 	struct vop_kqfilter_args /* {
    957 		struct vnode	*a_vp;
    958 		struct proc	*a_kn;
    959 	} */ *ap = v;
    960 	dev_t dev;
    961 
    962 	switch (ap->a_vp->v_type) {
    963 
    964 	case VCHR:
    965 		dev = ap->a_vp->v_rdev;
    966 		return cdev_kqfilter(dev, ap->a_kn);
    967 	default:
    968 		/*
    969 		 * Block devices don't support kqfilter, and refuse it
    970 		 * for any other files (like those vflush()ed) too.
    971 		 */
    972 		return (EOPNOTSUPP);
    973 	}
    974 }
    975 
    976 /*
    977  * Allow mapping of only D_DISK.  This is called only for VBLK.
    978  */
    979 int
    980 spec_mmap(void *v)
    981 {
    982 	struct vop_mmap_args /* {
    983 		struct vnode *a_vp;
    984 		vm_prot_t a_prot;
    985 		kauth_cred_t a_cred;
    986 	} */ *ap = v;
    987 	struct vnode *vp = ap->a_vp;
    988 
    989 	KASSERT(vp->v_type == VBLK);
    990 	if (bdev_type(vp->v_rdev) != D_DISK)
    991 		return EINVAL;
    992 
    993 	return 0;
    994 }
    995 
    996 /*
    997  * Synch buffers associated with a block device
    998  */
    999 /* ARGSUSED */
   1000 int
   1001 spec_fsync(void *v)
   1002 {
   1003 	struct vop_fsync_args /* {
   1004 		struct vnode *a_vp;
   1005 		kauth_cred_t a_cred;
   1006 		int  a_flags;
   1007 		off_t offlo;
   1008 		off_t offhi;
   1009 	} */ *ap = v;
   1010 	struct vnode *vp = ap->a_vp;
   1011 	struct mount *mp;
   1012 	int error;
   1013 
   1014 	if (vp->v_type == VBLK) {
   1015 		if ((mp = spec_node_getmountedfs(vp)) != NULL) {
   1016 			error = VFS_FSYNC(mp, vp, ap->a_flags);
   1017 			if (error != EOPNOTSUPP)
   1018 				return error;
   1019 		}
   1020 		return vflushbuf(vp, ap->a_flags);
   1021 	}
   1022 	return (0);
   1023 }
   1024 
   1025 /*
   1026  * Just call the device strategy routine
   1027  */
   1028 int
   1029 spec_strategy(void *v)
   1030 {
   1031 	struct vop_strategy_args /* {
   1032 		struct vnode *a_vp;
   1033 		struct buf *a_bp;
   1034 	} */ *ap = v;
   1035 	struct vnode *vp = ap->a_vp;
   1036 	struct buf *bp = ap->a_bp;
   1037 	dev_t dev;
   1038 	int error;
   1039 
   1040 	dev = NODEV;
   1041 
   1042 	/*
   1043 	 * Extract all the info we need from the vnode, taking care to
   1044 	 * avoid a race with VOP_REVOKE().
   1045 	 */
   1046 
   1047 	mutex_enter(vp->v_interlock);
   1048 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) {
   1049 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
   1050 		dev = vp->v_rdev;
   1051 	}
   1052 	mutex_exit(vp->v_interlock);
   1053 
   1054 	if (dev == NODEV) {
   1055 		error = ENXIO;
   1056 		goto out;
   1057 	}
   1058 	bp->b_dev = dev;
   1059 
   1060 	if (!(bp->b_flags & B_READ)) {
   1061 #ifdef DIAGNOSTIC
   1062 		if (bp->b_vp && bp->b_vp->v_type == VBLK) {
   1063 			struct mount *mp = spec_node_getmountedfs(bp->b_vp);
   1064 
   1065 			if (mp && (mp->mnt_flag & MNT_RDONLY)) {
   1066 				printf("%s blk %"PRId64" written while ro!\n",
   1067 				    mp->mnt_stat.f_mntonname, bp->b_blkno);
   1068 			}
   1069 		}
   1070 #endif /* DIAGNOSTIC */
   1071 		error = fscow_run(bp, false);
   1072 		if (error)
   1073 			goto out;
   1074 	}
   1075 	bdev_strategy(bp);
   1076 
   1077 	return 0;
   1078 
   1079 out:
   1080 	bp->b_error = error;
   1081 	bp->b_resid = bp->b_bcount;
   1082 	biodone(bp);
   1083 
   1084 	return error;
   1085 }
   1086 
   1087 int
   1088 spec_inactive(void *v)
   1089 {
   1090 	struct vop_inactive_v2_args /* {
   1091 		struct vnode *a_vp;
   1092 		struct bool *a_recycle;
   1093 	} */ *ap = v;
   1094 
   1095 	KASSERT(ap->a_vp->v_mount == dead_rootmount);
   1096 	*ap->a_recycle = true;
   1097 
   1098 	return 0;
   1099 }
   1100 
   1101 int
   1102 spec_reclaim(void *v)
   1103 {
   1104 	struct vop_reclaim_v2_args /* {
   1105 		struct vnode *a_vp;
   1106 	} */ *ap = v;
   1107 	struct vnode *vp = ap->a_vp;
   1108 
   1109 	VOP_UNLOCK(vp);
   1110 
   1111 	KASSERT(vp->v_mount == dead_rootmount);
   1112 	return 0;
   1113 }
   1114 
   1115 /*
   1116  * This is a noop, simply returning what one has been given.
   1117  */
   1118 int
   1119 spec_bmap(void *v)
   1120 {
   1121 	struct vop_bmap_args /* {
   1122 		struct vnode *a_vp;
   1123 		daddr_t  a_bn;
   1124 		struct vnode **a_vpp;
   1125 		daddr_t *a_bnp;
   1126 		int *a_runp;
   1127 	} */ *ap = v;
   1128 
   1129 	if (ap->a_vpp != NULL)
   1130 		*ap->a_vpp = ap->a_vp;
   1131 	if (ap->a_bnp != NULL)
   1132 		*ap->a_bnp = ap->a_bn;
   1133 	if (ap->a_runp != NULL)
   1134 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
   1135 	return (0);
   1136 }
   1137 
   1138 /*
   1139  * Device close routine
   1140  */
   1141 /* ARGSUSED */
   1142 int
   1143 spec_close(void *v)
   1144 {
   1145 	struct vop_close_args /* {
   1146 		struct vnode *a_vp;
   1147 		int  a_fflag;
   1148 		kauth_cred_t a_cred;
   1149 	} */ *ap = v;
   1150 	struct vnode *vp = ap->a_vp;
   1151 	struct session *sess;
   1152 	dev_t dev = vp->v_rdev;
   1153 	int flags = ap->a_fflag;
   1154 	int mode, error, count;
   1155 	specnode_t *sn;
   1156 	specdev_t *sd;
   1157 
   1158 	mutex_enter(vp->v_interlock);
   1159 	sn = vp->v_specnode;
   1160 	sd = sn->sn_dev;
   1161 	/*
   1162 	 * If we're going away soon, make this non-blocking.
   1163 	 * Also ensures that we won't wedge in vn_lock below.
   1164 	 */
   1165 	if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   1166 		flags |= FNONBLOCK;
   1167 	mutex_exit(vp->v_interlock);
   1168 
   1169 	switch (vp->v_type) {
   1170 
   1171 	case VCHR:
   1172 		/*
   1173 		 * Hack: a tty device that is a controlling terminal
   1174 		 * has a reference from the session structure.  We
   1175 		 * cannot easily tell that a character device is a
   1176 		 * controlling terminal, unless it is the closing
   1177 		 * process' controlling terminal.  In that case, if the
   1178 		 * open count is 1 release the reference from the
   1179 		 * session.  Also, remove the link from the tty back to
   1180 		 * the session and pgrp.
   1181 		 *
   1182 		 * XXX V. fishy.
   1183 		 */
   1184 		mutex_enter(proc_lock);
   1185 		sess = curlwp->l_proc->p_session;
   1186 		if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
   1187 			mutex_spin_enter(&tty_lock);
   1188 			sess->s_ttyvp = NULL;
   1189 			if (sess->s_ttyp->t_session != NULL) {
   1190 				sess->s_ttyp->t_pgrp = NULL;
   1191 				sess->s_ttyp->t_session = NULL;
   1192 				mutex_spin_exit(&tty_lock);
   1193 				/* Releases proc_lock. */
   1194 				proc_sessrele(sess);
   1195 			} else {
   1196 				mutex_spin_exit(&tty_lock);
   1197 				if (sess->s_ttyp->t_pgrp != NULL)
   1198 					panic("spec_close: spurious pgrp ref");
   1199 				mutex_exit(proc_lock);
   1200 			}
   1201 			vrele(vp);
   1202 		} else
   1203 			mutex_exit(proc_lock);
   1204 
   1205 		/*
   1206 		 * If the vnode is locked, then we are in the midst
   1207 		 * of forcably closing the device, otherwise we only
   1208 		 * close on last reference.
   1209 		 */
   1210 		mode = S_IFCHR;
   1211 		break;
   1212 
   1213 	case VBLK:
   1214 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
   1215 		/*
   1216 		 * On last close of a block device (that isn't mounted)
   1217 		 * we must invalidate any in core blocks, so that
   1218 		 * we can, for instance, change floppy disks.
   1219 		 */
   1220 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
   1221 		if (error)
   1222 			return (error);
   1223 		/*
   1224 		 * We do not want to really close the device if it
   1225 		 * is still in use unless we are trying to close it
   1226 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
   1227 		 * holds a reference to the vnode, and because we mark
   1228 		 * any other vnodes that alias this device, when the
   1229 		 * sum of the reference counts on all the aliased
   1230 		 * vnodes descends to one, we are on last close.
   1231 		 */
   1232 		mode = S_IFBLK;
   1233 		break;
   1234 
   1235 	default:
   1236 		panic("spec_close: not special");
   1237 	}
   1238 
   1239 	mutex_enter(&device_lock);
   1240 	sn->sn_opencnt--;
   1241 	count = --sd->sd_opencnt;
   1242 	if (vp->v_type == VBLK)
   1243 		sd->sd_bdevvp = NULL;
   1244 	mutex_exit(&device_lock);
   1245 
   1246 	if (count != 0 && (vp->v_type != VCHR || !(cdev_flags(dev) & D_MCLOSE)))
   1247 		return 0;
   1248 
   1249 	/*
   1250 	 * If we're able to block, release the vnode lock & reacquire. We
   1251 	 * might end up sleeping for someone else who wants our queues. They
   1252 	 * won't get them if we hold the vnode locked.
   1253 	 */
   1254 	if (!(flags & FNONBLOCK))
   1255 		VOP_UNLOCK(vp);
   1256 
   1257 	if (vp->v_type == VBLK)
   1258 		error = bdev_close(dev, flags, mode, curlwp);
   1259 	else
   1260 		error = cdev_close(dev, flags, mode, curlwp);
   1261 
   1262 	if (!(flags & FNONBLOCK))
   1263 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1264 
   1265 	return (error);
   1266 }
   1267 
   1268 /*
   1269  * Print out the contents of a special device vnode.
   1270  */
   1271 int
   1272 spec_print(void *v)
   1273 {
   1274 	struct vop_print_args /* {
   1275 		struct vnode *a_vp;
   1276 	} */ *ap = v;
   1277 
   1278 	printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev),
   1279 	    (unsigned long long)minor(ap->a_vp->v_rdev));
   1280 	return 0;
   1281 }
   1282 
   1283 /*
   1284  * Return POSIX pathconf information applicable to special devices.
   1285  */
   1286 int
   1287 spec_pathconf(void *v)
   1288 {
   1289 	struct vop_pathconf_args /* {
   1290 		struct vnode *a_vp;
   1291 		int a_name;
   1292 		register_t *a_retval;
   1293 	} */ *ap = v;
   1294 
   1295 	switch (ap->a_name) {
   1296 	case _PC_LINK_MAX:
   1297 		*ap->a_retval = LINK_MAX;
   1298 		return (0);
   1299 	case _PC_MAX_CANON:
   1300 		*ap->a_retval = MAX_CANON;
   1301 		return (0);
   1302 	case _PC_MAX_INPUT:
   1303 		*ap->a_retval = MAX_INPUT;
   1304 		return (0);
   1305 	case _PC_PIPE_BUF:
   1306 		*ap->a_retval = PIPE_BUF;
   1307 		return (0);
   1308 	case _PC_CHOWN_RESTRICTED:
   1309 		*ap->a_retval = 1;
   1310 		return (0);
   1311 	case _PC_VDISABLE:
   1312 		*ap->a_retval = _POSIX_VDISABLE;
   1313 		return (0);
   1314 	case _PC_SYNC_IO:
   1315 		*ap->a_retval = 1;
   1316 		return (0);
   1317 	default:
   1318 		return (EINVAL);
   1319 	}
   1320 	/* NOTREACHED */
   1321 }
   1322 
   1323 /*
   1324  * Advisory record locking support.
   1325  */
   1326 int
   1327 spec_advlock(void *v)
   1328 {
   1329 	struct vop_advlock_args /* {
   1330 		struct vnode *a_vp;
   1331 		void *a_id;
   1332 		int a_op;
   1333 		struct flock *a_fl;
   1334 		int a_flags;
   1335 	} */ *ap = v;
   1336 	struct vnode *vp = ap->a_vp;
   1337 
   1338 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
   1339 }
   1340