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