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