xref: /netbsd-src/sys/miscfs/specfs/spec_vnops.c (revision 4d12bfcd155352508213ace5ccc59ce930ea2974)
1 /*	$NetBSD: spec_vnops.c,v 1.140 2013/07/20 23:00:08 dholland 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.140 2013/07/20 23:00:08 dholland 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 
687 		/*
688 		 * dholland 20130616: XXX this logic should not be
689 		 * here. It is here because the old buffer cache
690 		 * demands that all accesses to the same blocks need
691 		 * to be the same size; but it only works for FFS and
692 		 * nowadays I think it'll fail silently if the size
693 		 * info in the disklabel is wrong. (Or missing.) The
694 		 * buffer cache needs to be smarter; or failing that
695 		 * we need a reliable way here to get the right block
696 		 * size; or a reliable way to guarantee that (a) the
697 		 * fs is not mounted when we get here and (b) any
698 		 * buffers generated here will get purged when the fs
699 		 * does get mounted.
700 		 */
701 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
702 			if (dpart.part->p_fstype == FS_BSDFFS &&
703 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
704 				bsize = dpart.part->p_frag *
705 				    dpart.part->p_fsize;
706 		}
707 
708 		bscale = bsize >> DEV_BSHIFT;
709 		do {
710 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
711 			on = uio->uio_offset % bsize;
712 			n = min((unsigned)(bsize - on), uio->uio_resid);
713 			error = bread(vp, bn, bsize, NOCRED, 0, &bp);
714 			if (error) {
715 				return (error);
716 			}
717 			n = min(n, bsize - bp->b_resid);
718 			error = uiomove((char *)bp->b_data + on, n, uio);
719 			brelse(bp, 0);
720 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
721 		return (error);
722 
723 	default:
724 		panic("spec_read type");
725 	}
726 	/* NOTREACHED */
727 }
728 
729 /*
730  * Vnode op for write
731  */
732 /* ARGSUSED */
733 int
734 spec_write(void *v)
735 {
736 	struct vop_write_args /* {
737 		struct vnode *a_vp;
738 		struct uio *a_uio;
739 		int  a_ioflag;
740 		kauth_cred_t a_cred;
741 	} */ *ap = v;
742 	struct vnode *vp = ap->a_vp;
743 	struct uio *uio = ap->a_uio;
744 	struct lwp *l = curlwp;
745 	struct buf *bp;
746 	daddr_t bn;
747 	int bsize, bscale;
748 	struct partinfo dpart;
749 	int n, on;
750 	int error = 0;
751 
752 #ifdef DIAGNOSTIC
753 	if (uio->uio_rw != UIO_WRITE)
754 		panic("spec_write mode");
755 	if (&uio->uio_vmspace->vm_map != kernel_map &&
756 	    uio->uio_vmspace != curproc->p_vmspace)
757 		panic("spec_write proc");
758 #endif
759 
760 	switch (vp->v_type) {
761 
762 	case VCHR:
763 		VOP_UNLOCK(vp);
764 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
765 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
766 		return (error);
767 
768 	case VBLK:
769 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
770 		if (uio->uio_resid == 0)
771 			return (0);
772 		if (uio->uio_offset < 0)
773 			return (EINVAL);
774 		bsize = BLKDEV_IOSIZE;
775 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
776 			if (dpart.part->p_fstype == FS_BSDFFS &&
777 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
778 				bsize = dpart.part->p_frag *
779 				    dpart.part->p_fsize;
780 		}
781 		bscale = bsize >> DEV_BSHIFT;
782 		do {
783 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
784 			on = uio->uio_offset % bsize;
785 			n = min((unsigned)(bsize - on), uio->uio_resid);
786 			if (n == bsize)
787 				bp = getblk(vp, bn, bsize, 0, 0);
788 			else
789 				error = bread(vp, bn, bsize, NOCRED,
790 				    B_MODIFY, &bp);
791 			if (error) {
792 				return (error);
793 			}
794 			n = min(n, bsize - bp->b_resid);
795 			error = uiomove((char *)bp->b_data + on, n, uio);
796 			if (error)
797 				brelse(bp, 0);
798 			else {
799 				if (n + on == bsize)
800 					bawrite(bp);
801 				else
802 					bdwrite(bp);
803 				error = bp->b_error;
804 			}
805 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
806 		return (error);
807 
808 	default:
809 		panic("spec_write type");
810 	}
811 	/* NOTREACHED */
812 }
813 
814 /*
815  * Device ioctl operation.
816  */
817 /* ARGSUSED */
818 int
819 spec_ioctl(void *v)
820 {
821 	struct vop_ioctl_args /* {
822 		struct vnode *a_vp;
823 		u_long a_command;
824 		void  *a_data;
825 		int  a_fflag;
826 		kauth_cred_t a_cred;
827 	} */ *ap = v;
828 	struct vnode *vp;
829 	dev_t dev;
830 
831 	/*
832 	 * Extract all the info we need from the vnode, taking care to
833 	 * avoid a race with VOP_REVOKE().
834 	 */
835 
836 	vp = ap->a_vp;
837 	dev = NODEV;
838 	mutex_enter(vp->v_interlock);
839 	if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specnode) {
840 		dev = vp->v_rdev;
841 	}
842 	mutex_exit(vp->v_interlock);
843 	if (dev == NODEV) {
844 		return ENXIO;
845 	}
846 
847 	switch (vp->v_type) {
848 
849 	case VCHR:
850 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
851 		    ap->a_fflag, curlwp);
852 
853 	case VBLK:
854 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
855 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
856 		   ap->a_fflag, curlwp);
857 
858 	default:
859 		panic("spec_ioctl");
860 		/* NOTREACHED */
861 	}
862 }
863 
864 /* ARGSUSED */
865 int
866 spec_poll(void *v)
867 {
868 	struct vop_poll_args /* {
869 		struct vnode *a_vp;
870 		int a_events;
871 	} */ *ap = v;
872 	struct vnode *vp;
873 	dev_t dev;
874 
875 	/*
876 	 * Extract all the info we need from the vnode, taking care to
877 	 * avoid a race with VOP_REVOKE().
878 	 */
879 
880 	vp = ap->a_vp;
881 	dev = NODEV;
882 	mutex_enter(vp->v_interlock);
883 	if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specnode) {
884 		dev = vp->v_rdev;
885 	}
886 	mutex_exit(vp->v_interlock);
887 	if (dev == NODEV) {
888 		return POLLERR;
889 	}
890 
891 	switch (vp->v_type) {
892 
893 	case VCHR:
894 		return cdev_poll(dev, ap->a_events, curlwp);
895 
896 	default:
897 		return (genfs_poll(v));
898 	}
899 }
900 
901 /* ARGSUSED */
902 int
903 spec_kqfilter(void *v)
904 {
905 	struct vop_kqfilter_args /* {
906 		struct vnode	*a_vp;
907 		struct proc	*a_kn;
908 	} */ *ap = v;
909 	dev_t dev;
910 
911 	switch (ap->a_vp->v_type) {
912 
913 	case VCHR:
914 		dev = ap->a_vp->v_rdev;
915 		return cdev_kqfilter(dev, ap->a_kn);
916 	default:
917 		/*
918 		 * Block devices don't support kqfilter, and refuse it
919 		 * for any other files (like those vflush()ed) too.
920 		 */
921 		return (EOPNOTSUPP);
922 	}
923 }
924 
925 /*
926  * Allow mapping of only D_DISK.  This is called only for VBLK.
927  */
928 int
929 spec_mmap(void *v)
930 {
931 	struct vop_mmap_args /* {
932 		struct vnode *a_vp;
933 		vm_prot_t a_prot;
934 		kauth_cred_t a_cred;
935 	} */ *ap = v;
936 	struct vnode *vp = ap->a_vp;
937 
938 	KASSERT(vp->v_type == VBLK);
939 	if (bdev_type(vp->v_rdev) != D_DISK)
940 		return EINVAL;
941 
942 	return 0;
943 }
944 
945 /*
946  * Synch buffers associated with a block device
947  */
948 /* ARGSUSED */
949 int
950 spec_fsync(void *v)
951 {
952 	struct vop_fsync_args /* {
953 		struct vnode *a_vp;
954 		kauth_cred_t a_cred;
955 		int  a_flags;
956 		off_t offlo;
957 		off_t offhi;
958 	} */ *ap = v;
959 	struct vnode *vp = ap->a_vp;
960 	struct mount *mp;
961 	int error;
962 
963 	if (vp->v_type == VBLK) {
964 		if ((mp = vp->v_specmountpoint) != NULL) {
965 			error = VFS_FSYNC(mp, vp, ap->a_flags);
966 			if (error != EOPNOTSUPP)
967 				return error;
968 		}
969 		return vflushbuf(vp, ap->a_flags);
970 	}
971 	return (0);
972 }
973 
974 /*
975  * Just call the device strategy routine
976  */
977 int
978 spec_strategy(void *v)
979 {
980 	struct vop_strategy_args /* {
981 		struct vnode *a_vp;
982 		struct buf *a_bp;
983 	} */ *ap = v;
984 	struct vnode *vp = ap->a_vp;
985 	struct buf *bp = ap->a_bp;
986 	int error;
987 
988 	KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
989 
990 	error = 0;
991 	bp->b_dev = vp->v_rdev;
992 
993 	if (!(bp->b_flags & B_READ))
994 		error = fscow_run(bp, false);
995 
996 	if (error) {
997 		bp->b_error = error;
998 		bp->b_resid = bp->b_bcount;
999 		biodone(bp);
1000 		return (error);
1001 	}
1002 
1003 	bdev_strategy(bp);
1004 
1005 	return (0);
1006 }
1007 
1008 int
1009 spec_inactive(void *v)
1010 {
1011 	struct vop_inactive_args /* {
1012 		struct vnode *a_vp;
1013 		struct proc *a_l;
1014 	} */ *ap = v;
1015 
1016 	VOP_UNLOCK(ap->a_vp);
1017 	return (0);
1018 }
1019 
1020 /*
1021  * This is a noop, simply returning what one has been given.
1022  */
1023 int
1024 spec_bmap(void *v)
1025 {
1026 	struct vop_bmap_args /* {
1027 		struct vnode *a_vp;
1028 		daddr_t  a_bn;
1029 		struct vnode **a_vpp;
1030 		daddr_t *a_bnp;
1031 		int *a_runp;
1032 	} */ *ap = v;
1033 
1034 	if (ap->a_vpp != NULL)
1035 		*ap->a_vpp = ap->a_vp;
1036 	if (ap->a_bnp != NULL)
1037 		*ap->a_bnp = ap->a_bn;
1038 	if (ap->a_runp != NULL)
1039 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
1040 	return (0);
1041 }
1042 
1043 /*
1044  * Device close routine
1045  */
1046 /* ARGSUSED */
1047 int
1048 spec_close(void *v)
1049 {
1050 	struct vop_close_args /* {
1051 		struct vnode *a_vp;
1052 		int  a_fflag;
1053 		kauth_cred_t a_cred;
1054 	} */ *ap = v;
1055 	struct vnode *vp = ap->a_vp;
1056 	struct session *sess;
1057 	dev_t dev = vp->v_rdev;
1058 	int mode, error, flags, flags1, count;
1059 	specnode_t *sn;
1060 	specdev_t *sd;
1061 
1062 	flags = vp->v_iflag;
1063 	sn = vp->v_specnode;
1064 	sd = sn->sn_dev;
1065 
1066 	switch (vp->v_type) {
1067 
1068 	case VCHR:
1069 		/*
1070 		 * Hack: a tty device that is a controlling terminal
1071 		 * has a reference from the session structure.  We
1072 		 * cannot easily tell that a character device is a
1073 		 * controlling terminal, unless it is the closing
1074 		 * process' controlling terminal.  In that case, if the
1075 		 * open count is 1 release the reference from the
1076 		 * session.  Also, remove the link from the tty back to
1077 		 * the session and pgrp.
1078 		 *
1079 		 * XXX V. fishy.
1080 		 */
1081 		mutex_enter(proc_lock);
1082 		sess = curlwp->l_proc->p_session;
1083 		if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
1084 			mutex_spin_enter(&tty_lock);
1085 			sess->s_ttyvp = NULL;
1086 			if (sess->s_ttyp->t_session != NULL) {
1087 				sess->s_ttyp->t_pgrp = NULL;
1088 				sess->s_ttyp->t_session = NULL;
1089 				mutex_spin_exit(&tty_lock);
1090 				/* Releases proc_lock. */
1091 				proc_sessrele(sess);
1092 			} else {
1093 				mutex_spin_exit(&tty_lock);
1094 				if (sess->s_ttyp->t_pgrp != NULL)
1095 					panic("spec_close: spurious pgrp ref");
1096 				mutex_exit(proc_lock);
1097 			}
1098 			vrele(vp);
1099 		} else
1100 			mutex_exit(proc_lock);
1101 
1102 		/*
1103 		 * If the vnode is locked, then we are in the midst
1104 		 * of forcably closing the device, otherwise we only
1105 		 * close on last reference.
1106 		 */
1107 		mode = S_IFCHR;
1108 		break;
1109 
1110 	case VBLK:
1111 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
1112 		/*
1113 		 * On last close of a block device (that isn't mounted)
1114 		 * we must invalidate any in core blocks, so that
1115 		 * we can, for instance, change floppy disks.
1116 		 */
1117 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
1118 		if (error)
1119 			return (error);
1120 		/*
1121 		 * We do not want to really close the device if it
1122 		 * is still in use unless we are trying to close it
1123 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
1124 		 * holds a reference to the vnode, and because we mark
1125 		 * any other vnodes that alias this device, when the
1126 		 * sum of the reference counts on all the aliased
1127 		 * vnodes descends to one, we are on last close.
1128 		 */
1129 		mode = S_IFBLK;
1130 		break;
1131 
1132 	default:
1133 		panic("spec_close: not special");
1134 	}
1135 
1136 	mutex_enter(&device_lock);
1137 	sn->sn_opencnt--;
1138 	count = --sd->sd_opencnt;
1139 	if (vp->v_type == VBLK)
1140 		sd->sd_bdevvp = NULL;
1141 	mutex_exit(&device_lock);
1142 
1143 	if (count != 0)
1144 		return 0;
1145 
1146 	flags1 = ap->a_fflag;
1147 
1148 	/*
1149 	 * if VI_XLOCK is set, then we're going away soon, so make this
1150 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
1151 	 */
1152 	if (flags & VI_XLOCK)
1153 		flags1 |= FNONBLOCK;
1154 
1155 	/*
1156 	 * If we're able to block, release the vnode lock & reacquire. We
1157 	 * might end up sleeping for someone else who wants our queues. They
1158 	 * won't get them if we hold the vnode locked. Also, if VI_XLOCK is
1159 	 * set, don't release the lock as we won't be able to regain it.
1160 	 */
1161 	if (!(flags1 & FNONBLOCK))
1162 		VOP_UNLOCK(vp);
1163 
1164 	if (vp->v_type == VBLK)
1165 		error = bdev_close(dev, flags1, mode, curlwp);
1166 	else
1167 		error = cdev_close(dev, flags1, mode, curlwp);
1168 
1169 	if (!(flags1 & FNONBLOCK))
1170 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1171 
1172 	return (error);
1173 }
1174 
1175 /*
1176  * Print out the contents of a special device vnode.
1177  */
1178 int
1179 spec_print(void *v)
1180 {
1181 	struct vop_print_args /* {
1182 		struct vnode *a_vp;
1183 	} */ *ap = v;
1184 
1185 	printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev),
1186 	    (unsigned long long)minor(ap->a_vp->v_rdev));
1187 	return 0;
1188 }
1189 
1190 /*
1191  * Return POSIX pathconf information applicable to special devices.
1192  */
1193 int
1194 spec_pathconf(void *v)
1195 {
1196 	struct vop_pathconf_args /* {
1197 		struct vnode *a_vp;
1198 		int a_name;
1199 		register_t *a_retval;
1200 	} */ *ap = v;
1201 
1202 	switch (ap->a_name) {
1203 	case _PC_LINK_MAX:
1204 		*ap->a_retval = LINK_MAX;
1205 		return (0);
1206 	case _PC_MAX_CANON:
1207 		*ap->a_retval = MAX_CANON;
1208 		return (0);
1209 	case _PC_MAX_INPUT:
1210 		*ap->a_retval = MAX_INPUT;
1211 		return (0);
1212 	case _PC_PIPE_BUF:
1213 		*ap->a_retval = PIPE_BUF;
1214 		return (0);
1215 	case _PC_CHOWN_RESTRICTED:
1216 		*ap->a_retval = 1;
1217 		return (0);
1218 	case _PC_VDISABLE:
1219 		*ap->a_retval = _POSIX_VDISABLE;
1220 		return (0);
1221 	case _PC_SYNC_IO:
1222 		*ap->a_retval = 1;
1223 		return (0);
1224 	default:
1225 		return (EINVAL);
1226 	}
1227 	/* NOTREACHED */
1228 }
1229 
1230 /*
1231  * Advisory record locking support.
1232  */
1233 int
1234 spec_advlock(void *v)
1235 {
1236 	struct vop_advlock_args /* {
1237 		struct vnode *a_vp;
1238 		void *a_id;
1239 		int a_op;
1240 		struct flock *a_fl;
1241 		int a_flags;
1242 	} */ *ap = v;
1243 	struct vnode *vp = ap->a_vp;
1244 
1245 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
1246 }
1247