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