xref: /netbsd-src/sys/miscfs/specfs/spec_vnops.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: spec_vnops.c,v 1.108 2007/10/10 20:42:30 ad 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 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.108 2007/10/10 20:42:30 ad Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/conf.h>
42 #include <sys/buf.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/stat.h>
47 #include <sys/errno.h>
48 #include <sys/ioctl.h>
49 #include <sys/poll.h>
50 #include <sys/file.h>
51 #include <sys/disklabel.h>
52 #include <sys/lockf.h>
53 #include <sys/tty.h>
54 #include <sys/kauth.h>
55 #include <sys/fstrans.h>
56 
57 #include <miscfs/genfs/genfs.h>
58 #include <miscfs/specfs/specdev.h>
59 
60 /* symbolic sleep message strings for devices */
61 const char	devopn[] = "devopn";
62 const char	devio[] = "devio";
63 const char	devwait[] = "devwait";
64 const char	devin[] = "devin";
65 const char	devout[] = "devout";
66 const char	devioc[] = "devioc";
67 const char	devcls[] = "devcls";
68 
69 struct vnode	*speclisth[SPECHSZ];
70 
71 /*
72  * This vnode operations vector is used for two things only:
73  * - special device nodes created from whole cloth by the kernel.
74  * - as a temporary vnodeops replacement for vnodes which were found to
75  *	be aliased by callers of checkalias().
76  * For the ops vector for vnodes built from special devices found in a
77  * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
78  * equivalent for other filesystems.
79  */
80 
81 int (**spec_vnodeop_p)(void *);
82 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
83 	{ &vop_default_desc, vn_default_error },
84 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
85 	{ &vop_create_desc, spec_create },		/* create */
86 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
87 	{ &vop_open_desc, spec_open },			/* open */
88 	{ &vop_close_desc, spec_close },		/* close */
89 	{ &vop_access_desc, spec_access },		/* access */
90 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
91 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
92 	{ &vop_read_desc, spec_read },			/* read */
93 	{ &vop_write_desc, spec_write },		/* write */
94 	{ &vop_lease_desc, spec_lease_check },		/* lease */
95 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
96 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
97 	{ &vop_poll_desc, spec_poll },			/* poll */
98 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
99 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
100 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
101 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
102 	{ &vop_seek_desc, spec_seek },			/* seek */
103 	{ &vop_remove_desc, spec_remove },		/* remove */
104 	{ &vop_link_desc, spec_link },			/* link */
105 	{ &vop_rename_desc, spec_rename },		/* rename */
106 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
107 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
108 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
109 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
110 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
111 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
112 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
113 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
114 	{ &vop_lock_desc, spec_lock },			/* lock */
115 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
116 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
117 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
118 	{ &vop_print_desc, spec_print },		/* print */
119 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
120 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
121 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
122 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
123 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
124 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
125 	{ NULL, NULL }
126 };
127 const struct vnodeopv_desc spec_vnodeop_opv_desc =
128 	{ &spec_vnodeop_p, spec_vnodeop_entries };
129 
130 /*
131  * Trivial lookup routine that always fails.
132  */
133 int
134 spec_lookup(void *v)
135 {
136 	struct vop_lookup_args /* {
137 		struct vnode *a_dvp;
138 		struct vnode **a_vpp;
139 		struct componentname *a_cnp;
140 	} */ *ap = v;
141 
142 	*ap->a_vpp = NULL;
143 	return (ENOTDIR);
144 }
145 
146 /*
147  * Returns true if dev is /dev/mem or /dev/kmem.
148  */
149 int
150 iskmemdev(dev_t dev)
151 {
152 	/* mem_no is emitted by config(8) to generated devsw.c */
153 	extern const int mem_no;
154 
155 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
156 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
157 }
158 
159 /*
160  * Open a special file.
161  */
162 /* ARGSUSED */
163 int
164 spec_open(void *v)
165 {
166 	struct vop_open_args /* {
167 		struct vnode *a_vp;
168 		int  a_mode;
169 		kauth_cred_t a_cred;
170 		struct lwp *a_l;
171 	} */ *ap = v;
172 	struct lwp *l = ap->a_l;
173 	struct vnode *vp = ap->a_vp;
174 	dev_t dev = (dev_t)vp->v_rdev;
175 	int error;
176 	struct partinfo pi;
177 	enum kauth_device_req req;
178 
179 	/*
180 	 * Don't allow open if fs is mounted -nodev.
181 	 */
182 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
183 		return (ENXIO);
184 
185 #define M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
186 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_RW : \
187 		 (m) & FWRITE ? KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE : \
188 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_READ)
189 
190 	switch (vp->v_type) {
191 
192 	case VCHR:
193 		req = M2K(ap->a_mode);
194 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
195 		if (error)
196 			return (error);
197 
198 		if (cdev_type(dev) == D_TTY)
199 			vp->v_vflag |= VV_ISTTY;
200 		VOP_UNLOCK(vp, 0);
201 		error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
202 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
203 		if (cdev_type(dev) != D_DISK)
204 			return error;
205 		break;
206 
207 	case VBLK:
208 		req = M2K(ap->a_mode);
209 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
210 		if (error)
211 			return (error);
212 		error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
213 		break;
214 
215 	case VNON:
216 	case VLNK:
217 	case VDIR:
218 	case VREG:
219 	case VBAD:
220 	case VFIFO:
221 	case VSOCK:
222 	default:
223 		return 0;
224 	}
225 
226 #undef M2K
227 
228 	if (error)
229 		return error;
230 	if (vp->v_type == VCHR)
231 		error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
232 	else
233 		error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
234 	if (error == 0)
235 		uvm_vnp_setsize(vp,
236 		    (voff_t)pi.disklab->d_secsize * pi.part->p_size);
237 	return 0;
238 }
239 
240 /*
241  * Vnode op for read
242  */
243 /* ARGSUSED */
244 int
245 spec_read(void *v)
246 {
247 	struct vop_read_args /* {
248 		struct vnode *a_vp;
249 		struct uio *a_uio;
250 		int  a_ioflag;
251 		kauth_cred_t a_cred;
252 	} */ *ap = v;
253 	struct vnode *vp = ap->a_vp;
254 	struct uio *uio = ap->a_uio;
255  	struct lwp *l = curlwp;
256 	struct buf *bp;
257 	daddr_t bn;
258 	int bsize, bscale;
259 	struct partinfo dpart;
260 	int n, on;
261 	int error = 0;
262 
263 #ifdef DIAGNOSTIC
264 	if (uio->uio_rw != UIO_READ)
265 		panic("spec_read mode");
266 	if (&uio->uio_vmspace->vm_map != kernel_map &&
267 	    uio->uio_vmspace != curproc->p_vmspace)
268 		panic("spec_read proc");
269 #endif
270 	if (uio->uio_resid == 0)
271 		return (0);
272 
273 	switch (vp->v_type) {
274 
275 	case VCHR:
276 		VOP_UNLOCK(vp, 0);
277 		error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
278 		vn_lock(vp, LK_SHARED | LK_RETRY);
279 		return (error);
280 
281 	case VBLK:
282 		if (uio->uio_offset < 0)
283 			return (EINVAL);
284 		bsize = BLKDEV_IOSIZE;
285 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
286 			if (dpart.part->p_fstype == FS_BSDFFS &&
287 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
288 				bsize = dpart.part->p_frag *
289 				    dpart.part->p_fsize;
290 		}
291 		bscale = bsize >> DEV_BSHIFT;
292 		do {
293 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
294 			on = uio->uio_offset % bsize;
295 			n = min((unsigned)(bsize - on), uio->uio_resid);
296 			error = bread(vp, bn, bsize, NOCRED, &bp);
297 			n = min(n, bsize - bp->b_resid);
298 			if (error) {
299 				brelse(bp, 0);
300 				return (error);
301 			}
302 			error = uiomove((char *)bp->b_data + on, n, uio);
303 			brelse(bp, 0);
304 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
305 		return (error);
306 
307 	default:
308 		panic("spec_read type");
309 	}
310 	/* NOTREACHED */
311 }
312 
313 /*
314  * Vnode op for write
315  */
316 /* ARGSUSED */
317 int
318 spec_write(void *v)
319 {
320 	struct vop_write_args /* {
321 		struct vnode *a_vp;
322 		struct uio *a_uio;
323 		int  a_ioflag;
324 		kauth_cred_t a_cred;
325 	} */ *ap = v;
326 	struct vnode *vp = ap->a_vp;
327 	struct uio *uio = ap->a_uio;
328 	struct lwp *l = curlwp;
329 	struct buf *bp;
330 	daddr_t bn;
331 	int bsize, bscale;
332 	struct partinfo dpart;
333 	int n, on;
334 	int error = 0;
335 
336 #ifdef DIAGNOSTIC
337 	if (uio->uio_rw != UIO_WRITE)
338 		panic("spec_write mode");
339 	if (&uio->uio_vmspace->vm_map != kernel_map &&
340 	    uio->uio_vmspace != curproc->p_vmspace)
341 		panic("spec_write proc");
342 #endif
343 
344 	switch (vp->v_type) {
345 
346 	case VCHR:
347 		VOP_UNLOCK(vp, 0);
348 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
349 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
350 		return (error);
351 
352 	case VBLK:
353 		if (uio->uio_resid == 0)
354 			return (0);
355 		if (uio->uio_offset < 0)
356 			return (EINVAL);
357 		bsize = BLKDEV_IOSIZE;
358 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
359 			if (dpart.part->p_fstype == FS_BSDFFS &&
360 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
361 				bsize = dpart.part->p_frag *
362 				    dpart.part->p_fsize;
363 		}
364 		bscale = bsize >> DEV_BSHIFT;
365 		do {
366 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
367 			on = uio->uio_offset % bsize;
368 			n = min((unsigned)(bsize - on), uio->uio_resid);
369 			if (n == bsize)
370 				bp = getblk(vp, bn, bsize, 0, 0);
371 			else
372 				error = bread(vp, bn, bsize, NOCRED, &bp);
373 			if (error) {
374 				brelse(bp, 0);
375 				return (error);
376 			}
377 			n = min(n, bsize - bp->b_resid);
378 			error = uiomove((char *)bp->b_data + on, n, uio);
379 			if (error)
380 				brelse(bp, 0);
381 			else {
382 				if (n + on == bsize)
383 					bawrite(bp);
384 				else
385 					bdwrite(bp);
386 				error = bp->b_error;
387 			}
388 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
389 		return (error);
390 
391 	default:
392 		panic("spec_write type");
393 	}
394 	/* NOTREACHED */
395 }
396 
397 /*
398  * Device ioctl operation.
399  */
400 /* ARGSUSED */
401 int
402 spec_ioctl(void *v)
403 {
404 	struct vop_ioctl_args /* {
405 		struct vnode *a_vp;
406 		u_long a_command;
407 		void  *a_data;
408 		int  a_fflag;
409 		kauth_cred_t a_cred;
410 		struct lwp *a_l;
411 	} */ *ap = v;
412 	struct vnode *vp;
413 	dev_t dev;
414 
415 	/*
416 	 * Extract all the info we need from the vnode, taking care to
417 	 * avoid a race with VOP_REVOKE().
418 	 */
419 
420 	vp = ap->a_vp;
421 	dev = NODEV;
422 	simple_lock(&vp->v_interlock);
423 	if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specinfo) {
424 		dev = vp->v_rdev;
425 	}
426 	simple_unlock(&vp->v_interlock);
427 	if (dev == NODEV) {
428 		return ENXIO;
429 	}
430 
431 	switch (vp->v_type) {
432 
433 	case VCHR:
434 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
435 		    ap->a_fflag, ap->a_l);
436 
437 	case VBLK:
438 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
439 		   ap->a_fflag, ap->a_l);
440 
441 	default:
442 		panic("spec_ioctl");
443 		/* NOTREACHED */
444 	}
445 }
446 
447 /* ARGSUSED */
448 int
449 spec_poll(void *v)
450 {
451 	struct vop_poll_args /* {
452 		struct vnode *a_vp;
453 		int a_events;
454 		struct lwp *a_l;
455 	} */ *ap = v;
456 	struct vnode *vp;
457 	dev_t dev;
458 
459 	/*
460 	 * Extract all the info we need from the vnode, taking care to
461 	 * avoid a race with VOP_REVOKE().
462 	 */
463 
464 	vp = ap->a_vp;
465 	dev = NODEV;
466 	simple_lock(&vp->v_interlock);
467 	if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specinfo) {
468 		dev = vp->v_rdev;
469 	}
470 	simple_unlock(&vp->v_interlock);
471 	if (dev == NODEV) {
472 		return POLLERR;
473 	}
474 
475 	switch (vp->v_type) {
476 
477 	case VCHR:
478 		return cdev_poll(dev, ap->a_events, ap->a_l);
479 
480 	default:
481 		return (genfs_poll(v));
482 	}
483 }
484 
485 /* ARGSUSED */
486 int
487 spec_kqfilter(void *v)
488 {
489 	struct vop_kqfilter_args /* {
490 		struct vnode	*a_vp;
491 		struct proc	*a_kn;
492 	} */ *ap = v;
493 	dev_t dev;
494 
495 	switch (ap->a_vp->v_type) {
496 
497 	case VCHR:
498 		dev = ap->a_vp->v_rdev;
499 		return cdev_kqfilter(dev, ap->a_kn);
500 	default:
501 		/*
502 		 * Block devices don't support kqfilter, and refuse it
503 		 * for any other files (like those vflush()ed) too.
504 		 */
505 		return (EOPNOTSUPP);
506 	}
507 }
508 
509 /*
510  * Allow mapping of only D_DISK.  This is called only for VBLK.
511  */
512 int
513 spec_mmap(void *v)
514 {
515 	struct vop_mmap_args /* {
516 		struct vnode *a_vp;
517 		vm_prot_t a_prot;
518 		kauth_cred_t a_cred;
519 		struct lwp *a_l;
520 	} */ *ap = v;
521 	struct vnode *vp = ap->a_vp;
522 
523 	KASSERT(vp->v_type == VBLK);
524 	if (bdev_type(vp->v_rdev) != D_DISK)
525 		return EINVAL;
526 
527 	return 0;
528 }
529 
530 /*
531  * Synch buffers associated with a block device
532  */
533 /* ARGSUSED */
534 int
535 spec_fsync(void *v)
536 {
537 	struct vop_fsync_args /* {
538 		struct vnode *a_vp;
539 		kauth_cred_t a_cred;
540 		int  a_flags;
541 		off_t offlo;
542 		off_t offhi;
543 		struct lwp *a_l;
544 	} */ *ap = v;
545 	struct vnode *vp = ap->a_vp;
546 
547 	if (vp->v_type == VBLK)
548 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
549 	return (0);
550 }
551 
552 /*
553  * Just call the device strategy routine
554  */
555 int
556 spec_strategy(void *v)
557 {
558 	struct vop_strategy_args /* {
559 		struct vnode *a_vp;
560 		struct buf *a_bp;
561 	} */ *ap = v;
562 	struct vnode *vp = ap->a_vp;
563 	struct buf *bp = ap->a_bp;
564 	int error;
565 
566 	error = 0;
567 	bp->b_dev = vp->v_rdev;
568 	if (!(bp->b_flags & B_READ) &&
569 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioopsp)
570 		bioopsp->io_start(bp);
571 
572 	if (!(bp->b_flags & B_READ))
573 		error = fscow_run(bp);
574 
575 	if (error) {
576 		bp->b_error = error;
577 		biodone(bp);
578 		return (error);
579 	}
580 
581 	bdev_strategy(bp);
582 
583 	return (0);
584 }
585 
586 int
587 spec_inactive(void *v)
588 {
589 	struct vop_inactive_args /* {
590 		struct vnode *a_vp;
591 		struct proc *a_l;
592 	} */ *ap = v;
593 
594 	VOP_UNLOCK(ap->a_vp, 0);
595 	return (0);
596 }
597 
598 /*
599  * This is a noop, simply returning what one has been given.
600  */
601 int
602 spec_bmap(void *v)
603 {
604 	struct vop_bmap_args /* {
605 		struct vnode *a_vp;
606 		daddr_t  a_bn;
607 		struct vnode **a_vpp;
608 		daddr_t *a_bnp;
609 		int *a_runp;
610 	} */ *ap = v;
611 
612 	if (ap->a_vpp != NULL)
613 		*ap->a_vpp = ap->a_vp;
614 	if (ap->a_bnp != NULL)
615 		*ap->a_bnp = ap->a_bn;
616 	if (ap->a_runp != NULL)
617 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
618 	return (0);
619 }
620 
621 /*
622  * Device close routine
623  */
624 /* ARGSUSED */
625 int
626 spec_close(void *v)
627 {
628 	struct vop_close_args /* {
629 		struct vnode *a_vp;
630 		int  a_fflag;
631 		kauth_cred_t a_cred;
632 		struct lwp *a_l;
633 	} */ *ap = v;
634 	struct vnode *vp = ap->a_vp;
635 	struct session *sess;
636 	dev_t dev = vp->v_rdev;
637 	int mode, error, count, flags, flags1;
638 
639 	count = vcount(vp);
640 	flags = vp->v_iflag;
641 
642 	switch (vp->v_type) {
643 
644 	case VCHR:
645 		/*
646 		 * Hack: a tty device that is a controlling terminal
647 		 * has a reference from the session structure.
648 		 * We cannot easily tell that a character device is
649 		 * a controlling terminal, unless it is the closing
650 		 * process' controlling terminal.  In that case,
651 		 * if the reference count is 2 (this last descriptor
652 		 * plus the session), release the reference from the session.
653 		 * Also remove the link from the tty back to the session
654 		 * and pgrp - due to the way consoles are handled we cannot
655 		 * guarantee that the vrele() will do the final close on the
656 		 * actual tty device.
657 		 */
658 		mutex_enter(&proclist_lock);
659 		if (count == 2 && ap->a_l &&
660 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
661 			sess->s_ttyvp = NULL;
662 			if (sess->s_ttyp->t_session != NULL) {
663 				sess->s_ttyp->t_pgrp = NULL;
664 				sess->s_ttyp->t_session = NULL;
665 				mutex_exit(&proclist_lock);
666 				SESSRELE(sess);
667 			} else {
668 				if (sess->s_ttyp->t_pgrp != NULL)
669 					panic("spec_close: spurious pgrp ref");
670 				mutex_exit(&proclist_lock);
671 			}
672 			vrele(vp);
673 			count--;
674 		} else
675 			mutex_exit(&proclist_lock);
676 
677 		/*
678 		 * If the vnode is locked, then we are in the midst
679 		 * of forcably closing the device, otherwise we only
680 		 * close on last reference.
681 		 */
682 		if (count > 1 && (flags & VI_XLOCK) == 0)
683 			return (0);
684 		mode = S_IFCHR;
685 		break;
686 
687 	case VBLK:
688 		/*
689 		 * On last close of a block device (that isn't mounted)
690 		 * we must invalidate any in core blocks, so that
691 		 * we can, for instance, change floppy disks.
692 		 */
693 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
694 		if (error)
695 			return (error);
696 		/*
697 		 * We do not want to really close the device if it
698 		 * is still in use unless we are trying to close it
699 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
700 		 * holds a reference to the vnode, and because we mark
701 		 * any other vnodes that alias this device, when the
702 		 * sum of the reference counts on all the aliased
703 		 * vnodes descends to one, we are on last close.
704 		 */
705 		if (count > 1 && (flags & VI_XLOCK) == 0)
706 			return (0);
707 		mode = S_IFBLK;
708 		break;
709 
710 	default:
711 		panic("spec_close: not special");
712 	}
713 
714 	flags1 = ap->a_fflag;
715 
716 	/*
717 	 * if VI_XLOCK is set, then we're going away soon, so make this
718 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
719 	 */
720 	if (flags & VI_XLOCK)
721 		flags1 |= FNONBLOCK;
722 
723 	/*
724 	 * If we're able to block, release the vnode lock & reacquire. We
725 	 * might end up sleeping for someone else who wants our queues. They
726 	 * won't get them if we hold the vnode locked. Also, if VI_XLOCK is
727 	 * set, don't release the lock as we won't be able to regain it.
728 	 */
729 	if (!(flags1 & FNONBLOCK))
730 		VOP_UNLOCK(vp, 0);
731 
732 	if (vp->v_type == VBLK)
733 		error = bdev_close(dev, flags1, mode, ap->a_l);
734 	else
735 		error = cdev_close(dev, flags1, mode, ap->a_l);
736 
737 	if (!(flags1 & FNONBLOCK))
738 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
739 
740 	return (error);
741 }
742 
743 /*
744  * Print out the contents of a special device vnode.
745  */
746 int
747 spec_print(void *v)
748 {
749 	struct vop_print_args /* {
750 		struct vnode *a_vp;
751 	} */ *ap = v;
752 
753 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
754 	    minor(ap->a_vp->v_rdev));
755 	return 0;
756 }
757 
758 /*
759  * Return POSIX pathconf information applicable to special devices.
760  */
761 int
762 spec_pathconf(void *v)
763 {
764 	struct vop_pathconf_args /* {
765 		struct vnode *a_vp;
766 		int a_name;
767 		register_t *a_retval;
768 	} */ *ap = v;
769 
770 	switch (ap->a_name) {
771 	case _PC_LINK_MAX:
772 		*ap->a_retval = LINK_MAX;
773 		return (0);
774 	case _PC_MAX_CANON:
775 		*ap->a_retval = MAX_CANON;
776 		return (0);
777 	case _PC_MAX_INPUT:
778 		*ap->a_retval = MAX_INPUT;
779 		return (0);
780 	case _PC_PIPE_BUF:
781 		*ap->a_retval = PIPE_BUF;
782 		return (0);
783 	case _PC_CHOWN_RESTRICTED:
784 		*ap->a_retval = 1;
785 		return (0);
786 	case _PC_VDISABLE:
787 		*ap->a_retval = _POSIX_VDISABLE;
788 		return (0);
789 	case _PC_SYNC_IO:
790 		*ap->a_retval = 1;
791 		return (0);
792 	default:
793 		return (EINVAL);
794 	}
795 	/* NOTREACHED */
796 }
797 
798 /*
799  * Advisory record locking support.
800  */
801 int
802 spec_advlock(void *v)
803 {
804 	struct vop_advlock_args /* {
805 		struct vnode *a_vp;
806 		void *a_id;
807 		int a_op;
808 		struct flock *a_fl;
809 		int a_flags;
810 	} */ *ap = v;
811 	struct vnode *vp = ap->a_vp;
812 
813 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
814 }
815