spec_vnops.c revision 1.88 1 /* $NetBSD: spec_vnops.c,v 1.88 2006/08/11 19:17:47 christos 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.88 2006/08/11 19:17:47 christos 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 switch (vp->v_type) {
197
198 case VCHR:
199 cdev = cdevsw_lookup(dev);
200 if (cdev == NULL)
201 return (ENXIO);
202 if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
203 /*
204 * When running in very secure mode, do not allow
205 * opens for writing of any disk character devices.
206 */
207 if (securelevel >= 2 && cdev->d_type == D_DISK)
208 return (EPERM);
209 /*
210 * When running in secure mode, do not allow opens
211 * for writing of /dev/mem, /dev/kmem, or character
212 * devices whose corresponding block devices are
213 * currently mounted.
214 */
215 bvp = NULL;
216 if (securelevel >= 1) {
217 blkdev = devsw_chr2blk(dev);
218 if (blkdev != (dev_t)NODEV &&
219 vfinddev(blkdev, VBLK, &bvp) &&
220 (error = vfs_mountedon(bvp)))
221 return (error);
222 if (iskmemdev(dev))
223 return (EPERM);
224 }
225
226 #if NVERIEXEC > 0
227 if (veriexec_strict >= VERIEXEC_IPS && iskmemdev(dev))
228 return (error);
229 error = veriexec_rawchk(bvp);
230 if (error)
231 return (error);
232 #endif /* NVERIEXEC > 0 */
233 }
234 if (cdev->d_type == D_TTY)
235 vp->v_flag |= VISTTY;
236 VOP_UNLOCK(vp, 0);
237 error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, l);
238 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
239 if (cdev->d_type != D_DISK)
240 return error;
241 d_ioctl = cdev->d_ioctl;
242 break;
243
244 case VBLK:
245 bdev = bdevsw_lookup(dev);
246 if (bdev == NULL)
247 return (ENXIO);
248 /*
249 * When running in very secure mode, do not allow
250 * opens for writing of any disk block devices.
251 */
252 if (securelevel >= 2 && ap->a_cred != FSCRED &&
253 (ap->a_mode & FWRITE) && bdev->d_type == D_DISK)
254 return (EPERM);
255 /*
256 * Do not allow opens of block devices that are
257 * currently mounted.
258 */
259 if ((error = vfs_mountedon(vp)) != 0)
260 return (error);
261
262 #if NVERIEXEC > 0
263 error = veriexec_rawchk(vp);
264 if (error)
265 return (error);
266 #endif /* NVERIEXEC > 0 */
267
268 error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, l);
269 d_ioctl = bdev->d_ioctl;
270 break;
271
272 case VNON:
273 case VLNK:
274 case VDIR:
275 case VREG:
276 case VBAD:
277 case VFIFO:
278 case VSOCK:
279 default:
280 return 0;
281 }
282
283 if (error)
284 return error;
285 if (!(*d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&pi, FREAD, curlwp))
286 vp->v_size = (voff_t)pi.disklab->d_secsize * pi.part->p_size;
287 return 0;
288 }
289
290 /*
291 * Vnode op for read
292 */
293 /* ARGSUSED */
294 int
295 spec_read(v)
296 void *v;
297 {
298 struct vop_read_args /* {
299 struct vnode *a_vp;
300 struct uio *a_uio;
301 int a_ioflag;
302 kauth_cred_t a_cred;
303 } */ *ap = v;
304 struct vnode *vp = ap->a_vp;
305 struct uio *uio = ap->a_uio;
306 struct lwp *l = curlwp;
307 struct buf *bp;
308 const struct bdevsw *bdev;
309 const struct cdevsw *cdev;
310 daddr_t bn;
311 int bsize, bscale;
312 struct partinfo dpart;
313 int n, on;
314 int error = 0;
315
316 #ifdef DIAGNOSTIC
317 if (uio->uio_rw != UIO_READ)
318 panic("spec_read mode");
319 if (&uio->uio_vmspace->vm_map != kernel_map &&
320 uio->uio_vmspace != curproc->p_vmspace)
321 panic("spec_read proc");
322 #endif
323 if (uio->uio_resid == 0)
324 return (0);
325
326 switch (vp->v_type) {
327
328 case VCHR:
329 VOP_UNLOCK(vp, 0);
330 cdev = cdevsw_lookup(vp->v_rdev);
331 if (cdev != NULL)
332 error = (*cdev->d_read)(vp->v_rdev, uio, ap->a_ioflag);
333 else
334 error = ENXIO;
335 vn_lock(vp, LK_SHARED | LK_RETRY);
336 return (error);
337
338 case VBLK:
339 if (uio->uio_offset < 0)
340 return (EINVAL);
341 bsize = BLKDEV_IOSIZE;
342 bdev = bdevsw_lookup(vp->v_rdev);
343 if (bdev != NULL &&
344 (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
345 FREAD, l) == 0) {
346 if (dpart.part->p_fstype == FS_BSDFFS &&
347 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
348 bsize = dpart.part->p_frag *
349 dpart.part->p_fsize;
350 }
351 bscale = bsize >> DEV_BSHIFT;
352 do {
353 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
354 on = uio->uio_offset % bsize;
355 n = min((unsigned)(bsize - on), uio->uio_resid);
356 error = bread(vp, bn, bsize, NOCRED, &bp);
357 n = min(n, bsize - bp->b_resid);
358 if (error) {
359 brelse(bp);
360 return (error);
361 }
362 error = uiomove((char *)bp->b_data + on, n, uio);
363 brelse(bp);
364 } while (error == 0 && uio->uio_resid > 0 && n != 0);
365 return (error);
366
367 default:
368 panic("spec_read type");
369 }
370 /* NOTREACHED */
371 }
372
373 /*
374 * Vnode op for write
375 */
376 /* ARGSUSED */
377 int
378 spec_write(v)
379 void *v;
380 {
381 struct vop_write_args /* {
382 struct vnode *a_vp;
383 struct uio *a_uio;
384 int a_ioflag;
385 kauth_cred_t a_cred;
386 } */ *ap = v;
387 struct vnode *vp = ap->a_vp;
388 struct uio *uio = ap->a_uio;
389 struct lwp *l = curlwp;
390 struct buf *bp;
391 const struct bdevsw *bdev;
392 const struct cdevsw *cdev;
393 daddr_t bn;
394 int bsize, bscale;
395 struct partinfo dpart;
396 int n, on;
397 int error = 0;
398
399 #ifdef DIAGNOSTIC
400 if (uio->uio_rw != UIO_WRITE)
401 panic("spec_write mode");
402 if (&uio->uio_vmspace->vm_map != kernel_map &&
403 uio->uio_vmspace != curproc->p_vmspace)
404 panic("spec_write proc");
405 #endif
406
407 switch (vp->v_type) {
408
409 case VCHR:
410 VOP_UNLOCK(vp, 0);
411 cdev = cdevsw_lookup(vp->v_rdev);
412 if (cdev != NULL)
413 error = (*cdev->d_write)(vp->v_rdev, uio, ap->a_ioflag);
414 else
415 error = ENXIO;
416 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
417 return (error);
418
419 case VBLK:
420 if (uio->uio_resid == 0)
421 return (0);
422 if (uio->uio_offset < 0)
423 return (EINVAL);
424 bsize = BLKDEV_IOSIZE;
425 bdev = bdevsw_lookup(vp->v_rdev);
426 if (bdev != NULL &&
427 (*bdev->d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart,
428 FREAD, l) == 0) {
429 if (dpart.part->p_fstype == FS_BSDFFS &&
430 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
431 bsize = dpart.part->p_frag *
432 dpart.part->p_fsize;
433 }
434 bscale = bsize >> DEV_BSHIFT;
435 do {
436 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
437 on = uio->uio_offset % bsize;
438 n = min((unsigned)(bsize - on), uio->uio_resid);
439 if (n == bsize)
440 bp = getblk(vp, bn, bsize, 0, 0);
441 else
442 error = bread(vp, bn, bsize, NOCRED, &bp);
443 if (error) {
444 brelse(bp);
445 return (error);
446 }
447 n = min(n, bsize - bp->b_resid);
448 error = uiomove((char *)bp->b_data + on, n, uio);
449 if (error)
450 brelse(bp);
451 else {
452 if (n + on == bsize)
453 bawrite(bp);
454 else
455 bdwrite(bp);
456 if (bp->b_flags & B_ERROR)
457 error = bp->b_error;
458 }
459 } while (error == 0 && uio->uio_resid > 0 && n != 0);
460 return (error);
461
462 default:
463 panic("spec_write type");
464 }
465 /* NOTREACHED */
466 }
467
468 /*
469 * Device ioctl operation.
470 */
471 /* ARGSUSED */
472 int
473 spec_ioctl(v)
474 void *v;
475 {
476 struct vop_ioctl_args /* {
477 struct vnode *a_vp;
478 u_long a_command;
479 void *a_data;
480 int a_fflag;
481 kauth_cred_t a_cred;
482 struct lwp *a_l;
483 } */ *ap = v;
484 const struct bdevsw *bdev;
485 const struct cdevsw *cdev;
486 struct vnode *vp;
487 dev_t dev;
488
489 /*
490 * Extract all the info we need from the vnode, taking care to
491 * avoid a race with VOP_REVOKE().
492 */
493
494 vp = ap->a_vp;
495 dev = NODEV;
496 simple_lock(&vp->v_interlock);
497 if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
498 dev = vp->v_rdev;
499 }
500 simple_unlock(&vp->v_interlock);
501 if (dev == NODEV) {
502 return ENXIO;
503 }
504
505 switch (vp->v_type) {
506
507 case VCHR:
508 cdev = cdevsw_lookup(dev);
509 if (cdev == NULL)
510 return (ENXIO);
511 return ((*cdev->d_ioctl)(dev, ap->a_command, ap->a_data,
512 ap->a_fflag, ap->a_l));
513
514 case VBLK:
515 bdev = bdevsw_lookup(dev);
516 if (bdev == NULL)
517 return (ENXIO);
518 if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
519 if (bdev->d_type == D_TAPE)
520 return (0);
521 else
522 return (1);
523 }
524 return ((*bdev->d_ioctl)(dev, ap->a_command, ap->a_data,
525 ap->a_fflag, ap->a_l));
526
527 default:
528 panic("spec_ioctl");
529 /* NOTREACHED */
530 }
531 }
532
533 /* ARGSUSED */
534 int
535 spec_poll(v)
536 void *v;
537 {
538 struct vop_poll_args /* {
539 struct vnode *a_vp;
540 int a_events;
541 struct lwp *a_l;
542 } */ *ap = v;
543 const struct cdevsw *cdev;
544 dev_t dev;
545
546 switch (ap->a_vp->v_type) {
547
548 case VCHR:
549 dev = ap->a_vp->v_rdev;
550 cdev = cdevsw_lookup(dev);
551 if (cdev == NULL)
552 return (POLLERR);
553 return (*cdev->d_poll)(dev, ap->a_events, ap->a_l);
554
555 default:
556 return (genfs_poll(v));
557 }
558 }
559
560 /* ARGSUSED */
561 int
562 spec_kqfilter(v)
563 void *v;
564 {
565 struct vop_kqfilter_args /* {
566 struct vnode *a_vp;
567 struct proc *a_kn;
568 } */ *ap = v;
569 const struct cdevsw *cdev;
570 dev_t dev;
571
572 switch (ap->a_vp->v_type) {
573
574 case VCHR:
575 dev = ap->a_vp->v_rdev;
576 cdev = cdevsw_lookup(dev);
577 if (cdev == NULL)
578 return (ENXIO);
579 return (*cdev->d_kqfilter)(dev, ap->a_kn);
580 default:
581 /*
582 * Block devices don't support kqfilter, and refuse it
583 * for any other files (like those vflush()ed) too.
584 */
585 return (EOPNOTSUPP);
586 }
587 }
588
589 /*
590 * Synch buffers associated with a block device
591 */
592 /* ARGSUSED */
593 int
594 spec_fsync(v)
595 void *v;
596 {
597 struct vop_fsync_args /* {
598 struct vnode *a_vp;
599 kauth_cred_t a_cred;
600 int a_flags;
601 off_t offlo;
602 off_t offhi;
603 struct lwp *a_l;
604 } */ *ap = v;
605 struct vnode *vp = ap->a_vp;
606
607 if (vp->v_type == VBLK)
608 vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
609 return (0);
610 }
611
612 /*
613 * Just call the device strategy routine
614 */
615 int
616 spec_strategy(v)
617 void *v;
618 {
619 struct vop_strategy_args /* {
620 struct vnode *a_vp;
621 struct buf *a_bp;
622 } */ *ap = v;
623 struct vnode *vp = ap->a_vp;
624 struct buf *bp = ap->a_bp;
625 int error, s;
626 struct spec_cow_entry *e;
627
628 error = 0;
629 bp->b_dev = vp->v_rdev;
630 if (!(bp->b_flags & B_READ) &&
631 (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
632 (*bioops.io_start)(bp);
633
634 if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) {
635 SPEC_COW_LOCK(vp->v_specinfo, s);
636 while (vp->v_spec_cow_req > 0)
637 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
638 &vp->v_spec_cow_slock);
639 vp->v_spec_cow_count++;
640 SPEC_COW_UNLOCK(vp->v_specinfo, s);
641
642 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) {
643 if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0)
644 break;
645 }
646
647 SPEC_COW_LOCK(vp->v_specinfo, s);
648 vp->v_spec_cow_count--;
649 if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0)
650 wakeup(&vp->v_spec_cow_req);
651 SPEC_COW_UNLOCK(vp->v_specinfo, s);
652 }
653
654 if (error) {
655 bp->b_error = error;
656 bp->b_flags |= B_ERROR;
657 biodone(bp);
658 return (error);
659 }
660
661 DEV_STRATEGY(bp);
662
663 return (0);
664 }
665
666 int
667 spec_inactive(v)
668 void *v;
669 {
670 struct vop_inactive_args /* {
671 struct vnode *a_vp;
672 struct proc *a_l;
673 } */ *ap = v;
674
675 VOP_UNLOCK(ap->a_vp, 0);
676 return (0);
677 }
678
679 /*
680 * This is a noop, simply returning what one has been given.
681 */
682 int
683 spec_bmap(v)
684 void *v;
685 {
686 struct vop_bmap_args /* {
687 struct vnode *a_vp;
688 daddr_t a_bn;
689 struct vnode **a_vpp;
690 daddr_t *a_bnp;
691 int *a_runp;
692 } */ *ap = v;
693
694 if (ap->a_vpp != NULL)
695 *ap->a_vpp = ap->a_vp;
696 if (ap->a_bnp != NULL)
697 *ap->a_bnp = ap->a_bn;
698 if (ap->a_runp != NULL)
699 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
700 return (0);
701 }
702
703 /*
704 * Device close routine
705 */
706 /* ARGSUSED */
707 int
708 spec_close(v)
709 void *v;
710 {
711 struct vop_close_args /* {
712 struct vnode *a_vp;
713 int a_fflag;
714 kauth_cred_t a_cred;
715 struct lwp *a_l;
716 } */ *ap = v;
717 struct vnode *vp = ap->a_vp;
718 const struct bdevsw *bdev;
719 const struct cdevsw *cdev;
720 struct session *sess;
721 dev_t dev = vp->v_rdev;
722 int (*devclose)(dev_t, int, int, struct lwp *);
723 int mode, error, count, flags, flags1;
724
725 count = vcount(vp);
726 flags = vp->v_flag;
727
728 switch (vp->v_type) {
729
730 case VCHR:
731 /*
732 * Hack: a tty device that is a controlling terminal
733 * has a reference from the session structure.
734 * We cannot easily tell that a character device is
735 * a controlling terminal, unless it is the closing
736 * process' controlling terminal. In that case,
737 * if the reference count is 2 (this last descriptor
738 * plus the session), release the reference from the session.
739 * Also remove the link from the tty back to the session
740 * and pgrp - due to the way consoles are handled we cannot
741 * guarantee that the vrele() will do the final close on the
742 * actual tty device.
743 */
744 if (count == 2 && ap->a_l &&
745 vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
746 sess->s_ttyvp = NULL;
747 if (sess->s_ttyp->t_session != NULL) {
748 sess->s_ttyp->t_pgrp = NULL;
749 sess->s_ttyp->t_session = NULL;
750 SESSRELE(sess);
751 } else if (sess->s_ttyp->t_pgrp != NULL)
752 panic("spec_close: spurious pgrp ref");
753 vrele(vp);
754 count--;
755 }
756 /*
757 * If the vnode is locked, then we are in the midst
758 * of forcably closing the device, otherwise we only
759 * close on last reference.
760 */
761 if (count > 1 && (flags & VXLOCK) == 0)
762 return (0);
763 cdev = cdevsw_lookup(dev);
764 if (cdev != NULL)
765 devclose = cdev->d_close;
766 else
767 devclose = NULL;
768 mode = S_IFCHR;
769 break;
770
771 case VBLK:
772 /*
773 * On last close of a block device (that isn't mounted)
774 * we must invalidate any in core blocks, so that
775 * we can, for instance, change floppy disks.
776 */
777 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
778 if (error)
779 return (error);
780 /*
781 * We do not want to really close the device if it
782 * is still in use unless we are trying to close it
783 * forcibly. Since every use (buffer, vnode, swap, cmap)
784 * holds a reference to the vnode, and because we mark
785 * any other vnodes that alias this device, when the
786 * sum of the reference counts on all the aliased
787 * vnodes descends to one, we are on last close.
788 */
789 if (count > 1 && (flags & VXLOCK) == 0)
790 return (0);
791 bdev = bdevsw_lookup(dev);
792 if (bdev != NULL)
793 devclose = bdev->d_close;
794 else
795 devclose = NULL;
796 mode = S_IFBLK;
797 break;
798
799 default:
800 panic("spec_close: not special");
801 }
802
803 flags1 = ap->a_fflag;
804
805 /*
806 * if VXLOCK is set, then we're going away soon, so make this
807 * non-blocking. Also ensures that we won't wedge in vn_lock below.
808 */
809 if (flags & VXLOCK)
810 flags1 |= FNONBLOCK;
811
812 /*
813 * If we're able to block, release the vnode lock & reacquire. We
814 * might end up sleeping for someone else who wants our queues. They
815 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
816 * don't release the lock as we won't be able to regain it.
817 */
818 if (!(flags1 & FNONBLOCK))
819 VOP_UNLOCK(vp, 0);
820
821 if (devclose != NULL)
822 error = (*devclose)(dev, flags1, mode, ap->a_l);
823 else
824 error = ENXIO;
825
826 if (!(flags1 & FNONBLOCK))
827 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
828
829 return (error);
830 }
831
832 /*
833 * Print out the contents of a special device vnode.
834 */
835 int
836 spec_print(v)
837 void *v;
838 {
839 struct vop_print_args /* {
840 struct vnode *a_vp;
841 } */ *ap = v;
842
843 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
844 minor(ap->a_vp->v_rdev));
845 return 0;
846 }
847
848 /*
849 * Return POSIX pathconf information applicable to special devices.
850 */
851 int
852 spec_pathconf(v)
853 void *v;
854 {
855 struct vop_pathconf_args /* {
856 struct vnode *a_vp;
857 int a_name;
858 register_t *a_retval;
859 } */ *ap = v;
860
861 switch (ap->a_name) {
862 case _PC_LINK_MAX:
863 *ap->a_retval = LINK_MAX;
864 return (0);
865 case _PC_MAX_CANON:
866 *ap->a_retval = MAX_CANON;
867 return (0);
868 case _PC_MAX_INPUT:
869 *ap->a_retval = MAX_INPUT;
870 return (0);
871 case _PC_PIPE_BUF:
872 *ap->a_retval = PIPE_BUF;
873 return (0);
874 case _PC_CHOWN_RESTRICTED:
875 *ap->a_retval = 1;
876 return (0);
877 case _PC_VDISABLE:
878 *ap->a_retval = _POSIX_VDISABLE;
879 return (0);
880 case _PC_SYNC_IO:
881 *ap->a_retval = 1;
882 return (0);
883 default:
884 return (EINVAL);
885 }
886 /* NOTREACHED */
887 }
888
889 /*
890 * Advisory record locking support.
891 */
892 int
893 spec_advlock(v)
894 void *v;
895 {
896 struct vop_advlock_args /* {
897 struct vnode *a_vp;
898 void *a_id;
899 int a_op;
900 struct flock *a_fl;
901 int a_flags;
902 } */ *ap = v;
903 struct vnode *vp = ap->a_vp;
904
905 return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
906 }
907