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