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