xref: /netbsd-src/sys/miscfs/specfs/spec_vnops.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: spec_vnops.c,v 1.91 2006/09/21 09:28:37 jld Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
32  */
33 
34 #if defined(_KERNEL_OPT)
35 #include "veriexec.h"
36 #endif
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.91 2006/09/21 09:28:37 jld Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/conf.h>
46 #include <sys/buf.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/stat.h>
51 #include <sys/errno.h>
52 #include <sys/ioctl.h>
53 #include <sys/poll.h>
54 #include <sys/file.h>
55 #include <sys/disklabel.h>
56 #include <sys/lockf.h>
57 #include <sys/tty.h>
58 #include <sys/kauth.h>
59 
60 #include <miscfs/genfs/genfs.h>
61 #include <miscfs/specfs/specdev.h>
62 
63 #if NVERIEXEC > 0
64 #include <sys/verified_exec.h>
65 #endif /* NVERIEXEC > 0 */
66 
67 /* symbolic sleep message strings for devices */
68 const char	devopn[] = "devopn";
69 const char	devio[] = "devio";
70 const char	devwait[] = "devwait";
71 const char	devin[] = "devin";
72 const char	devout[] = "devout";
73 const char	devioc[] = "devioc";
74 const char	devcls[] = "devcls";
75 
76 struct vnode	*speclisth[SPECHSZ];
77 
78 /*
79  * This vnode operations vector is used for two things only:
80  * - special device nodes created from whole cloth by the kernel.
81  * - as a temporary vnodeops replacement for vnodes which were found to
82  *	be aliased by callers of checkalias().
83  * For the ops vector for vnodes built from special devices found in a
84  * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
85  * equivalent for other filesystems.
86  */
87 
88 int (**spec_vnodeop_p)(void *);
89 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
90 	{ &vop_default_desc, vn_default_error },
91 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
92 	{ &vop_create_desc, spec_create },		/* create */
93 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
94 	{ &vop_open_desc, spec_open },			/* open */
95 	{ &vop_close_desc, spec_close },		/* close */
96 	{ &vop_access_desc, spec_access },		/* access */
97 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
98 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
99 	{ &vop_read_desc, spec_read },			/* read */
100 	{ &vop_write_desc, spec_write },		/* write */
101 	{ &vop_lease_desc, spec_lease_check },		/* lease */
102 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
103 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
104 	{ &vop_poll_desc, spec_poll },			/* poll */
105 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
106 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
107 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
108 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
109 	{ &vop_seek_desc, spec_seek },			/* seek */
110 	{ &vop_remove_desc, spec_remove },		/* remove */
111 	{ &vop_link_desc, spec_link },			/* link */
112 	{ &vop_rename_desc, spec_rename },		/* rename */
113 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
114 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
115 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
116 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
117 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
118 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
119 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
120 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
121 	{ &vop_lock_desc, spec_lock },			/* lock */
122 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
123 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
124 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
125 	{ &vop_print_desc, spec_print },		/* print */
126 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
127 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
128 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
129 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
130 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
131 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
132 	{ NULL, NULL }
133 };
134 const struct vnodeopv_desc spec_vnodeop_opv_desc =
135 	{ &spec_vnodeop_p, spec_vnodeop_entries };
136 
137 /*
138  * Trivial lookup routine that always fails.
139  */
140 int
141 spec_lookup(v)
142 	void *v;
143 {
144 	struct vop_lookup_args /* {
145 		struct vnode *a_dvp;
146 		struct vnode **a_vpp;
147 		struct componentname *a_cnp;
148 	} */ *ap = v;
149 
150 	*ap->a_vpp = NULL;
151 	return (ENOTDIR);
152 }
153 
154 /*
155  * Returns true if dev is /dev/mem or /dev/kmem.
156  */
157 static int
158 iskmemdev(dev_t dev)
159 {
160 	/* mem_no is emitted by config(8) to generated devsw.c */
161 	extern const int mem_no;
162 
163 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
164 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
165 }
166 
167 /*
168  * Open a special file.
169  */
170 /* ARGSUSED */
171 int
172 spec_open(v)
173 	void *v;
174 {
175 	struct vop_open_args /* {
176 		struct vnode *a_vp;
177 		int  a_mode;
178 		kauth_cred_t a_cred;
179 		struct lwp *a_l;
180 	} */ *ap = v;
181 	struct lwp *l = ap->a_l;
182 	struct vnode *bvp, *vp = ap->a_vp;
183 	const struct bdevsw *bdev;
184 	const struct cdevsw *cdev;
185 	dev_t blkdev, dev = (dev_t)vp->v_rdev;
186 	int error;
187 	struct partinfo pi;
188 	int (*d_ioctl)(dev_t, u_long, caddr_t, int, struct lwp *);
189 
190 	/*
191 	 * Don't allow open if fs is mounted -nodev.
192 	 */
193 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
194 		return (ENXIO);
195 
196 #define M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? KAUTH_REQ_SYSTEM_RAWIO_RW : \
197 		 (m) & FWRITE ? KAUTH_REQ_SYSTEM_RAWIO_WRITE : \
198 		 KAUTH_REQ_SYSTEM_RAWIO_READ)
199 
200 	switch (vp->v_type) {
201 
202 	case VCHR:
203 		cdev = cdevsw_lookup(dev);
204 		if (cdev == NULL)
205 			return (ENXIO);
206 
207 		if (ap->a_cred != FSCRED) {
208 			u_long rw;
209 
210 			rw = M2K(ap->a_mode);
211 			error = 0;
212 			bvp = NULL;
213 
214 			/* XXX we're holding a vnode lock here */
215 			if (iskmemdev(dev)) {
216 				error = kauth_authorize_system(ap->a_cred,
217 				    KAUTH_SYSTEM_RAWIO,
218 				    KAUTH_REQ_SYSTEM_RAWIO_MEMORY,
219 				    (void *)rw, NULL, NULL);
220 			} else {
221 				blkdev = devsw_chr2blk(dev);
222 				if (blkdev != (dev_t)NODEV) {
223 					vfinddev(blkdev, VBLK, &bvp);
224 					error = kauth_authorize_system(ap->a_cred,
225 					    KAUTH_SYSTEM_RAWIO,
226 					    KAUTH_REQ_SYSTEM_RAWIO_DISK,
227 					    (void *)rw, vp, (void *)(u_long)dev);
228 					if (error) printf("nope.\n");
229 				}
230 			}
231 
232 			if (error)
233 				return (error);
234 
235 #if NVERIEXEC > 0
236 			if (veriexec_strict >= VERIEXEC_IPS && iskmemdev(dev))
237 				return (error);
238 			error = veriexec_rawchk(bvp);
239 			if (error)
240 				return (error);
241 #endif /* NVERIEXEC > 0 */
242 		}
243 
244 		if (cdev->d_type == D_TTY)
245 			vp->v_flag |= VISTTY;
246 		VOP_UNLOCK(vp, 0);
247 		error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, l);
248 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
249 		if (cdev->d_type != D_DISK)
250 			return error;
251 		d_ioctl = cdev->d_ioctl;
252 		break;
253 
254 	case VBLK:
255 		bdev = bdevsw_lookup(dev);
256 		if (bdev == NULL)
257 			return (ENXIO);
258 
259 		/*
260 		 * When running in very secure mode, do not allow
261 		 * opens for writing of any disk block devices.
262 		 */
263 		if (ap->a_cred != FSCRED) {
264 			u_long rw;
265 
266 			if ((error = vfs_mountedon(vp)) != 0)
267 				return (error);
268 
269 			rw = M2K(ap->a_mode);
270 
271 			error = kauth_authorize_system(ap->a_cred,
272 			    KAUTH_SYSTEM_RAWIO,
273 			    KAUTH_REQ_SYSTEM_RAWIO_DISK,
274 			    (void *)rw, vp, (void *)(u_long)dev);
275 			if (error)
276 				return (error);
277 		}
278 
279 #if NVERIEXEC > 0
280 		error = veriexec_rawchk(vp);
281 		if (error)
282 			return (error);
283 #endif /* NVERIEXEC > 0 */
284 
285 		error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, l);
286 		d_ioctl = bdev->d_ioctl;
287 		break;
288 
289 	case VNON:
290 	case VLNK:
291 	case VDIR:
292 	case VREG:
293 	case VBAD:
294 	case VFIFO:
295 	case VSOCK:
296 	default:
297 		return 0;
298 	}
299 
300 #undef M2K
301 
302 	if (error)
303 		return error;
304 	if (!(*d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&pi, FREAD, curlwp))
305 		vp->v_size = (voff_t)pi.disklab->d_secsize * pi.part->p_size;
306 	return 0;
307 }
308 
309 /*
310  * Vnode op for read
311  */
312 /* ARGSUSED */
313 int
314 spec_read(v)
315 	void *v;
316 {
317 	struct vop_read_args /* {
318 		struct vnode *a_vp;
319 		struct uio *a_uio;
320 		int  a_ioflag;
321 		kauth_cred_t a_cred;
322 	} */ *ap = v;
323 	struct vnode *vp = ap->a_vp;
324 	struct uio *uio = ap->a_uio;
325  	struct lwp *l = curlwp;
326 	struct buf *bp;
327 	const struct bdevsw *bdev;
328 	const struct cdevsw *cdev;
329 	daddr_t bn;
330 	int bsize, bscale;
331 	struct partinfo dpart;
332 	int n, on;
333 	int error = 0;
334 
335 #ifdef DIAGNOSTIC
336 	if (uio->uio_rw != UIO_READ)
337 		panic("spec_read mode");
338 	if (&uio->uio_vmspace->vm_map != kernel_map &&
339 	    uio->uio_vmspace != curproc->p_vmspace)
340 		panic("spec_read proc");
341 #endif
342 	if (uio->uio_resid == 0)
343 		return (0);
344 
345 	switch (vp->v_type) {
346 
347 	case VCHR:
348 		VOP_UNLOCK(vp, 0);
349 		cdev = cdevsw_lookup(vp->v_rdev);
350 		if (cdev != NULL)
351 			error = (*cdev->d_read)(vp->v_rdev, uio, ap->a_ioflag);
352 		else
353 			error = ENXIO;
354 		vn_lock(vp, LK_SHARED | LK_RETRY);
355 		return (error);
356 
357 	case VBLK:
358 		if (uio->uio_offset < 0)
359 			return (EINVAL);
360 		bsize = BLKDEV_IOSIZE;
361 		bdev = bdevsw_lookup(vp->v_rdev);
362 		if (bdev != NULL &&
363 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
364 				     FREAD, l) == 0) {
365 			if (dpart.part->p_fstype == FS_BSDFFS &&
366 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
367 				bsize = dpart.part->p_frag *
368 				    dpart.part->p_fsize;
369 		}
370 		bscale = bsize >> DEV_BSHIFT;
371 		do {
372 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
373 			on = uio->uio_offset % bsize;
374 			n = min((unsigned)(bsize - on), uio->uio_resid);
375 			error = bread(vp, bn, bsize, NOCRED, &bp);
376 			n = min(n, bsize - bp->b_resid);
377 			if (error) {
378 				brelse(bp);
379 				return (error);
380 			}
381 			error = uiomove((char *)bp->b_data + on, n, uio);
382 			brelse(bp);
383 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
384 		return (error);
385 
386 	default:
387 		panic("spec_read type");
388 	}
389 	/* NOTREACHED */
390 }
391 
392 /*
393  * Vnode op for write
394  */
395 /* ARGSUSED */
396 int
397 spec_write(v)
398 	void *v;
399 {
400 	struct vop_write_args /* {
401 		struct vnode *a_vp;
402 		struct uio *a_uio;
403 		int  a_ioflag;
404 		kauth_cred_t a_cred;
405 	} */ *ap = v;
406 	struct vnode *vp = ap->a_vp;
407 	struct uio *uio = ap->a_uio;
408 	struct lwp *l = curlwp;
409 	struct buf *bp;
410 	const struct bdevsw *bdev;
411 	const struct cdevsw *cdev;
412 	daddr_t bn;
413 	int bsize, bscale;
414 	struct partinfo dpart;
415 	int n, on;
416 	int error = 0;
417 
418 #ifdef DIAGNOSTIC
419 	if (uio->uio_rw != UIO_WRITE)
420 		panic("spec_write mode");
421 	if (&uio->uio_vmspace->vm_map != kernel_map &&
422 	    uio->uio_vmspace != curproc->p_vmspace)
423 		panic("spec_write proc");
424 #endif
425 
426 	switch (vp->v_type) {
427 
428 	case VCHR:
429 		VOP_UNLOCK(vp, 0);
430 		cdev = cdevsw_lookup(vp->v_rdev);
431 		if (cdev != NULL)
432 			error = (*cdev->d_write)(vp->v_rdev, uio, ap->a_ioflag);
433 		else
434 			error = ENXIO;
435 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
436 		return (error);
437 
438 	case VBLK:
439 		if (uio->uio_resid == 0)
440 			return (0);
441 		if (uio->uio_offset < 0)
442 			return (EINVAL);
443 		bsize = BLKDEV_IOSIZE;
444 		bdev = bdevsw_lookup(vp->v_rdev);
445 		if (bdev != NULL &&
446 		    (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
447 				    FREAD, l) == 0) {
448 			if (dpart.part->p_fstype == FS_BSDFFS &&
449 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
450 				bsize = dpart.part->p_frag *
451 				    dpart.part->p_fsize;
452 		}
453 		bscale = bsize >> DEV_BSHIFT;
454 		do {
455 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
456 			on = uio->uio_offset % bsize;
457 			n = min((unsigned)(bsize - on), uio->uio_resid);
458 			if (n == bsize)
459 				bp = getblk(vp, bn, bsize, 0, 0);
460 			else
461 				error = bread(vp, bn, bsize, NOCRED, &bp);
462 			if (error) {
463 				brelse(bp);
464 				return (error);
465 			}
466 			n = min(n, bsize - bp->b_resid);
467 			error = uiomove((char *)bp->b_data + on, n, uio);
468 			if (error)
469 				brelse(bp);
470 			else {
471 				if (n + on == bsize)
472 					bawrite(bp);
473 				else
474 					bdwrite(bp);
475 				if (bp->b_flags & B_ERROR)
476 					error = bp->b_error;
477 			}
478 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
479 		return (error);
480 
481 	default:
482 		panic("spec_write type");
483 	}
484 	/* NOTREACHED */
485 }
486 
487 /*
488  * Device ioctl operation.
489  */
490 /* ARGSUSED */
491 int
492 spec_ioctl(v)
493 	void *v;
494 {
495 	struct vop_ioctl_args /* {
496 		struct vnode *a_vp;
497 		u_long a_command;
498 		void  *a_data;
499 		int  a_fflag;
500 		kauth_cred_t a_cred;
501 		struct lwp *a_l;
502 	} */ *ap = v;
503 	const struct bdevsw *bdev;
504 	const struct cdevsw *cdev;
505 	struct vnode *vp;
506 	dev_t dev;
507 
508 	/*
509 	 * Extract all the info we need from the vnode, taking care to
510 	 * avoid a race with VOP_REVOKE().
511 	 */
512 
513 	vp = ap->a_vp;
514 	dev = NODEV;
515 	simple_lock(&vp->v_interlock);
516 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
517 		dev = vp->v_rdev;
518 	}
519 	simple_unlock(&vp->v_interlock);
520 	if (dev == NODEV) {
521 		return ENXIO;
522 	}
523 
524 	switch (vp->v_type) {
525 
526 	case VCHR:
527 		cdev = cdevsw_lookup(dev);
528 		if (cdev == NULL)
529 			return (ENXIO);
530 		return ((*cdev->d_ioctl)(dev, ap->a_command, ap->a_data,
531 		    ap->a_fflag, ap->a_l));
532 
533 	case VBLK:
534 		bdev = bdevsw_lookup(dev);
535 		if (bdev == NULL)
536 			return (ENXIO);
537 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
538 			if (bdev->d_type == D_TAPE)
539 				return (0);
540 			else
541 				return (1);
542 		}
543 		return ((*bdev->d_ioctl)(dev, ap->a_command, ap->a_data,
544 		   ap->a_fflag, ap->a_l));
545 
546 	default:
547 		panic("spec_ioctl");
548 		/* NOTREACHED */
549 	}
550 }
551 
552 /* ARGSUSED */
553 int
554 spec_poll(v)
555 	void *v;
556 {
557 	struct vop_poll_args /* {
558 		struct vnode *a_vp;
559 		int a_events;
560 		struct lwp *a_l;
561 	} */ *ap = v;
562 	const struct cdevsw *cdev;
563 	struct vnode *vp;
564 	dev_t dev;
565 
566 	/*
567 	 * Extract all the info we need from the vnode, taking care to
568 	 * avoid a race with VOP_REVOKE().
569 	 */
570 
571 	vp = ap->a_vp;
572 	dev = NODEV;
573 	simple_lock(&vp->v_interlock);
574 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
575 		dev = vp->v_rdev;
576 	}
577 	simple_unlock(&vp->v_interlock);
578 	if (dev == NODEV) {
579 		return ENXIO;
580 	}
581 
582 	switch (vp->v_type) {
583 
584 	case VCHR:
585 		cdev = cdevsw_lookup(dev);
586 		if (cdev == NULL)
587 			return (POLLERR);
588 		return (*cdev->d_poll)(dev, ap->a_events, ap->a_l);
589 
590 	default:
591 		return (genfs_poll(v));
592 	}
593 }
594 
595 /* ARGSUSED */
596 int
597 spec_kqfilter(v)
598 	void *v;
599 {
600 	struct vop_kqfilter_args /* {
601 		struct vnode	*a_vp;
602 		struct proc	*a_kn;
603 	} */ *ap = v;
604 	const struct cdevsw *cdev;
605 	dev_t dev;
606 
607 	switch (ap->a_vp->v_type) {
608 
609 	case VCHR:
610 		dev = ap->a_vp->v_rdev;
611 		cdev = cdevsw_lookup(dev);
612 		if (cdev == NULL)
613 			return (ENXIO);
614 		return (*cdev->d_kqfilter)(dev, ap->a_kn);
615 	default:
616 		/*
617 		 * Block devices don't support kqfilter, and refuse it
618 		 * for any other files (like those vflush()ed) too.
619 		 */
620 		return (EOPNOTSUPP);
621 	}
622 }
623 
624 /*
625  * Synch buffers associated with a block device
626  */
627 /* ARGSUSED */
628 int
629 spec_fsync(v)
630 	void *v;
631 {
632 	struct vop_fsync_args /* {
633 		struct vnode *a_vp;
634 		kauth_cred_t a_cred;
635 		int  a_flags;
636 		off_t offlo;
637 		off_t offhi;
638 		struct lwp *a_l;
639 	} */ *ap = v;
640 	struct vnode *vp = ap->a_vp;
641 
642 	if (vp->v_type == VBLK)
643 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
644 	return (0);
645 }
646 
647 /*
648  * Just call the device strategy routine
649  */
650 int
651 spec_strategy(v)
652 	void *v;
653 {
654 	struct vop_strategy_args /* {
655 		struct vnode *a_vp;
656 		struct buf *a_bp;
657 	} */ *ap = v;
658 	struct vnode *vp = ap->a_vp;
659 	struct buf *bp = ap->a_bp;
660 	int error, s;
661 	struct spec_cow_entry *e;
662 
663 	error = 0;
664 	bp->b_dev = vp->v_rdev;
665 	if (!(bp->b_flags & B_READ) &&
666 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
667 		(*bioops.io_start)(bp);
668 
669 	if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) {
670 		SPEC_COW_LOCK(vp->v_specinfo, s);
671 		while (vp->v_spec_cow_req > 0)
672 			ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
673 			    &vp->v_spec_cow_slock);
674 		vp->v_spec_cow_count++;
675 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
676 
677 		SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) {
678 			if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0)
679 				break;
680 		}
681 
682 		SPEC_COW_LOCK(vp->v_specinfo, s);
683 		vp->v_spec_cow_count--;
684 		if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0)
685 			wakeup(&vp->v_spec_cow_req);
686 		SPEC_COW_UNLOCK(vp->v_specinfo, s);
687 	}
688 
689 	if (error) {
690 		bp->b_error = error;
691 		bp->b_flags |= B_ERROR;
692 		biodone(bp);
693 		return (error);
694 	}
695 
696 	DEV_STRATEGY(bp);
697 
698 	return (0);
699 }
700 
701 int
702 spec_inactive(v)
703 	void *v;
704 {
705 	struct vop_inactive_args /* {
706 		struct vnode *a_vp;
707 		struct proc *a_l;
708 	} */ *ap = v;
709 
710 	VOP_UNLOCK(ap->a_vp, 0);
711 	return (0);
712 }
713 
714 /*
715  * This is a noop, simply returning what one has been given.
716  */
717 int
718 spec_bmap(v)
719 	void *v;
720 {
721 	struct vop_bmap_args /* {
722 		struct vnode *a_vp;
723 		daddr_t  a_bn;
724 		struct vnode **a_vpp;
725 		daddr_t *a_bnp;
726 		int *a_runp;
727 	} */ *ap = v;
728 
729 	if (ap->a_vpp != NULL)
730 		*ap->a_vpp = ap->a_vp;
731 	if (ap->a_bnp != NULL)
732 		*ap->a_bnp = ap->a_bn;
733 	if (ap->a_runp != NULL)
734 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
735 	return (0);
736 }
737 
738 /*
739  * Device close routine
740  */
741 /* ARGSUSED */
742 int
743 spec_close(v)
744 	void *v;
745 {
746 	struct vop_close_args /* {
747 		struct vnode *a_vp;
748 		int  a_fflag;
749 		kauth_cred_t a_cred;
750 		struct lwp *a_l;
751 	} */ *ap = v;
752 	struct vnode *vp = ap->a_vp;
753 	const struct bdevsw *bdev;
754 	const struct cdevsw *cdev;
755 	struct session *sess;
756 	dev_t dev = vp->v_rdev;
757 	int (*devclose)(dev_t, int, int, struct lwp *);
758 	int mode, error, count, flags, flags1;
759 
760 	count = vcount(vp);
761 	flags = vp->v_flag;
762 
763 	switch (vp->v_type) {
764 
765 	case VCHR:
766 		/*
767 		 * Hack: a tty device that is a controlling terminal
768 		 * has a reference from the session structure.
769 		 * We cannot easily tell that a character device is
770 		 * a controlling terminal, unless it is the closing
771 		 * process' controlling terminal.  In that case,
772 		 * if the reference count is 2 (this last descriptor
773 		 * plus the session), release the reference from the session.
774 		 * Also remove the link from the tty back to the session
775 		 * and pgrp - due to the way consoles are handled we cannot
776 		 * guarantee that the vrele() will do the final close on the
777 		 * actual tty device.
778 		 */
779 		if (count == 2 && ap->a_l &&
780 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
781 			sess->s_ttyvp = NULL;
782 			if (sess->s_ttyp->t_session != NULL) {
783 				sess->s_ttyp->t_pgrp = NULL;
784 				sess->s_ttyp->t_session = NULL;
785 				SESSRELE(sess);
786 			} else if (sess->s_ttyp->t_pgrp != NULL)
787 				panic("spec_close: spurious pgrp ref");
788 			vrele(vp);
789 			count--;
790 		}
791 		/*
792 		 * If the vnode is locked, then we are in the midst
793 		 * of forcably closing the device, otherwise we only
794 		 * close on last reference.
795 		 */
796 		if (count > 1 && (flags & VXLOCK) == 0)
797 			return (0);
798 		cdev = cdevsw_lookup(dev);
799 		if (cdev != NULL)
800 			devclose = cdev->d_close;
801 		else
802 			devclose = NULL;
803 		mode = S_IFCHR;
804 		break;
805 
806 	case VBLK:
807 		/*
808 		 * On last close of a block device (that isn't mounted)
809 		 * we must invalidate any in core blocks, so that
810 		 * we can, for instance, change floppy disks.
811 		 */
812 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
813 		if (error)
814 			return (error);
815 		/*
816 		 * We do not want to really close the device if it
817 		 * is still in use unless we are trying to close it
818 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
819 		 * holds a reference to the vnode, and because we mark
820 		 * any other vnodes that alias this device, when the
821 		 * sum of the reference counts on all the aliased
822 		 * vnodes descends to one, we are on last close.
823 		 */
824 		if (count > 1 && (flags & VXLOCK) == 0)
825 			return (0);
826 		bdev = bdevsw_lookup(dev);
827 		if (bdev != NULL)
828 			devclose = bdev->d_close;
829 		else
830 			devclose = NULL;
831 		mode = S_IFBLK;
832 		break;
833 
834 	default:
835 		panic("spec_close: not special");
836 	}
837 
838 	flags1 = ap->a_fflag;
839 
840 	/*
841 	 * if VXLOCK is set, then we're going away soon, so make this
842 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
843 	 */
844 	if (flags & VXLOCK)
845 		flags1 |= FNONBLOCK;
846 
847 	/*
848 	 * If we're able to block, release the vnode lock & reacquire. We
849 	 * might end up sleeping for someone else who wants our queues. They
850 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
851 	 * don't release the lock as we won't be able to regain it.
852 	 */
853 	if (!(flags1 & FNONBLOCK))
854 		VOP_UNLOCK(vp, 0);
855 
856 	if (devclose != NULL)
857 		error = (*devclose)(dev, flags1, mode, ap->a_l);
858 	else
859 		error = ENXIO;
860 
861 	if (!(flags1 & FNONBLOCK))
862 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
863 
864 	return (error);
865 }
866 
867 /*
868  * Print out the contents of a special device vnode.
869  */
870 int
871 spec_print(v)
872 	void *v;
873 {
874 	struct vop_print_args /* {
875 		struct vnode *a_vp;
876 	} */ *ap = v;
877 
878 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
879 	    minor(ap->a_vp->v_rdev));
880 	return 0;
881 }
882 
883 /*
884  * Return POSIX pathconf information applicable to special devices.
885  */
886 int
887 spec_pathconf(v)
888 	void *v;
889 {
890 	struct vop_pathconf_args /* {
891 		struct vnode *a_vp;
892 		int a_name;
893 		register_t *a_retval;
894 	} */ *ap = v;
895 
896 	switch (ap->a_name) {
897 	case _PC_LINK_MAX:
898 		*ap->a_retval = LINK_MAX;
899 		return (0);
900 	case _PC_MAX_CANON:
901 		*ap->a_retval = MAX_CANON;
902 		return (0);
903 	case _PC_MAX_INPUT:
904 		*ap->a_retval = MAX_INPUT;
905 		return (0);
906 	case _PC_PIPE_BUF:
907 		*ap->a_retval = PIPE_BUF;
908 		return (0);
909 	case _PC_CHOWN_RESTRICTED:
910 		*ap->a_retval = 1;
911 		return (0);
912 	case _PC_VDISABLE:
913 		*ap->a_retval = _POSIX_VDISABLE;
914 		return (0);
915 	case _PC_SYNC_IO:
916 		*ap->a_retval = 1;
917 		return (0);
918 	default:
919 		return (EINVAL);
920 	}
921 	/* NOTREACHED */
922 }
923 
924 /*
925  * Advisory record locking support.
926  */
927 int
928 spec_advlock(v)
929 	void *v;
930 {
931 	struct vop_advlock_args /* {
932 		struct vnode *a_vp;
933 		void *a_id;
934 		int a_op;
935 		struct flock *a_fl;
936 		int a_flags;
937 	} */ *ap = v;
938 	struct vnode *vp = ap->a_vp;
939 
940 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
941 }
942