spec_vnops.c revision 1.66 1 /* $NetBSD: spec_vnops.c,v 1.66 2002/10/26 13:50:17 jdolecek 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.66 2002/10/26 13:50:17 jdolecek 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/file.h>
54 #include <sys/disklabel.h>
55 #include <sys/lockf.h>
56
57 #include <miscfs/genfs/genfs.h>
58 #include <miscfs/specfs/specdev.h>
59
60 /* symbolic sleep message strings for devices */
61 const char devopn[] = "devopn";
62 const char devio[] = "devio";
63 const char devwait[] = "devwait";
64 const char devin[] = "devin";
65 const char devout[] = "devout";
66 const char devioc[] = "devioc";
67 const char devcls[] = "devcls";
68
69 struct vnode *speclisth[SPECHSZ];
70
71 /*
72 * This vnode operations vector is used for two things only:
73 * - special device nodes created from whole cloth by the kernel.
74 * - as a temporary vnodeops replacement for vnodes which were found to
75 * be aliased by callers of checkalias().
76 * For the ops vector for vnodes built from special devices found in a
77 * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
78 * equivalent for other filesystems.
79 */
80
81 int (**spec_vnodeop_p) __P((void *));
82 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
83 { &vop_default_desc, vn_default_error },
84 { &vop_lookup_desc, spec_lookup }, /* lookup */
85 { &vop_create_desc, spec_create }, /* create */
86 { &vop_mknod_desc, spec_mknod }, /* mknod */
87 { &vop_open_desc, spec_open }, /* open */
88 { &vop_close_desc, spec_close }, /* close */
89 { &vop_access_desc, spec_access }, /* access */
90 { &vop_getattr_desc, spec_getattr }, /* getattr */
91 { &vop_setattr_desc, spec_setattr }, /* setattr */
92 { &vop_read_desc, spec_read }, /* read */
93 { &vop_write_desc, spec_write }, /* write */
94 { &vop_lease_desc, spec_lease_check }, /* lease */
95 { &vop_fcntl_desc, spec_fcntl }, /* fcntl */
96 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
97 { &vop_poll_desc, spec_poll }, /* poll */
98 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
99 { &vop_revoke_desc, spec_revoke }, /* revoke */
100 { &vop_mmap_desc, spec_mmap }, /* mmap */
101 { &vop_fsync_desc, spec_fsync }, /* fsync */
102 { &vop_seek_desc, spec_seek }, /* seek */
103 { &vop_remove_desc, spec_remove }, /* remove */
104 { &vop_link_desc, spec_link }, /* link */
105 { &vop_rename_desc, spec_rename }, /* rename */
106 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
107 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
108 { &vop_symlink_desc, spec_symlink }, /* symlink */
109 { &vop_readdir_desc, spec_readdir }, /* readdir */
110 { &vop_readlink_desc, spec_readlink }, /* readlink */
111 { &vop_abortop_desc, spec_abortop }, /* abortop */
112 { &vop_inactive_desc, spec_inactive }, /* inactive */
113 { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
114 { &vop_lock_desc, spec_lock }, /* lock */
115 { &vop_unlock_desc, spec_unlock }, /* unlock */
116 { &vop_bmap_desc, spec_bmap }, /* bmap */
117 { &vop_strategy_desc, spec_strategy }, /* strategy */
118 { &vop_print_desc, spec_print }, /* print */
119 { &vop_islocked_desc, spec_islocked }, /* islocked */
120 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
121 { &vop_advlock_desc, spec_advlock }, /* advlock */
122 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
123 { &vop_valloc_desc, spec_valloc }, /* valloc */
124 { &vop_vfree_desc, spec_vfree }, /* vfree */
125 { &vop_truncate_desc, spec_truncate }, /* truncate */
126 { &vop_update_desc, spec_update }, /* update */
127 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
128 { &vop_getpages_desc, spec_getpages }, /* getpages */
129 { &vop_putpages_desc, spec_putpages }, /* putpages */
130 { NULL, NULL }
131 };
132 const struct vnodeopv_desc spec_vnodeop_opv_desc =
133 { &spec_vnodeop_p, spec_vnodeop_entries };
134
135 /*
136 * Trivial lookup routine that always fails.
137 */
138 int
139 spec_lookup(v)
140 void *v;
141 {
142 struct vop_lookup_args /* {
143 struct vnode *a_dvp;
144 struct vnode **a_vpp;
145 struct componentname *a_cnp;
146 } */ *ap = v;
147
148 *ap->a_vpp = NULL;
149 return (ENOTDIR);
150 }
151
152 /*
153 * Returns true if dev is /dev/mem or /dev/kmem.
154 */
155 static int
156 iskmemdev(dev_t dev)
157 {
158 /* mem_no is emitted by config(8) to generated devsw.c */
159 extern const int mem_no;
160
161 /* minor 14 is /dev/io on i386 with COMPAT_10 */
162 return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
163 }
164
165 /*
166 * Open a special file.
167 */
168 /* ARGSUSED */
169 int
170 spec_open(v)
171 void *v;
172 {
173 struct vop_open_args /* {
174 struct vnode *a_vp;
175 int a_mode;
176 struct ucred *a_cred;
177 struct proc *a_p;
178 } */ *ap = v;
179 struct proc *p = ap->a_p;
180 struct vnode *bvp, *vp = ap->a_vp;
181 const struct bdevsw *bdev;
182 const struct cdevsw *cdev;
183 dev_t blkdev, dev = (dev_t)vp->v_rdev;
184 int error;
185 struct partinfo pi;
186
187 /*
188 * Don't allow open if fs is mounted -nodev.
189 */
190 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
191 return (ENXIO);
192
193 switch (vp->v_type) {
194
195 case VCHR:
196 cdev = cdevsw_lookup(dev);
197 if (cdev == NULL)
198 return (ENXIO);
199 if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
200 /*
201 * When running in very secure mode, do not allow
202 * opens for writing of any disk character devices.
203 */
204 if (securelevel >= 2 && cdev->d_type == D_DISK)
205 return (EPERM);
206 /*
207 * When running in secure mode, do not allow opens
208 * for writing of /dev/mem, /dev/kmem, or character
209 * devices whose corresponding block devices are
210 * currently mounted.
211 */
212 if (securelevel >= 1) {
213 blkdev = devsw_chr2blk(dev);
214 if (blkdev != (dev_t)NODEV &&
215 vfinddev(blkdev, VBLK, &bvp) &&
216 (error = vfs_mountedon(bvp)))
217 return (error);
218 if (iskmemdev(dev))
219 return (EPERM);
220 }
221 }
222 if (cdev->d_type == D_TTY)
223 vp->v_flag |= VISTTY;
224 VOP_UNLOCK(vp, 0);
225 error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, p);
226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
227 return (error);
228
229 case VBLK:
230 bdev = bdevsw_lookup(dev);
231 if (bdev == NULL)
232 return (ENXIO);
233 /*
234 * When running in very secure mode, do not allow
235 * opens for writing of any disk block devices.
236 */
237 if (securelevel >= 2 && ap->a_cred != FSCRED &&
238 (ap->a_mode & FWRITE) && bdev->d_type == D_DISK)
239 return (EPERM);
240 /*
241 * Do not allow opens of block devices that are
242 * currently mounted.
243 */
244 if ((error = vfs_mountedon(vp)) != 0)
245 return (error);
246 error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, p);
247 if (error) {
248 return error;
249 }
250 error = (*bdev->d_ioctl)(vp->v_rdev,
251 DIOCGPART, (caddr_t)&pi, FREAD, curproc);
252 if (error == 0) {
253 vp->v_size = (voff_t)pi.disklab->d_secsize *
254 pi.part->p_size;
255 }
256 return 0;
257
258 case VNON:
259 case VLNK:
260 case VDIR:
261 case VREG:
262 case VBAD:
263 case VFIFO:
264 case VSOCK:
265 break;
266 }
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 caddr_t 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 dev_t dev = ap->a_vp->v_rdev;
465
466 switch (ap->a_vp->v_type) {
467
468 case VCHR:
469 cdev = cdevsw_lookup(dev);
470 if (cdev == NULL)
471 return (ENXIO);
472 return ((*cdev->d_ioctl)(dev, ap->a_command, ap->a_data,
473 ap->a_fflag, ap->a_p));
474
475 case VBLK:
476 bdev = bdevsw_lookup(dev);
477 if (bdev == NULL)
478 return (ENXIO);
479 if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
480 if (bdev->d_type == D_TAPE)
481 return (0);
482 else
483 return (1);
484 }
485 return ((*bdev->d_ioctl)(dev, ap->a_command, ap->a_data,
486 ap->a_fflag, ap->a_p));
487
488 default:
489 panic("spec_ioctl");
490 /* NOTREACHED */
491 }
492 }
493
494 /* ARGSUSED */
495 int
496 spec_poll(v)
497 void *v;
498 {
499 struct vop_poll_args /* {
500 struct vnode *a_vp;
501 int a_events;
502 struct proc *a_p;
503 } */ *ap = v;
504 const struct cdevsw *cdev;
505 dev_t dev;
506
507 switch (ap->a_vp->v_type) {
508
509 case VCHR:
510 dev = ap->a_vp->v_rdev;
511 cdev = cdevsw_lookup(dev);
512 if (cdev == NULL)
513 return (ENXIO);
514 return (*cdev->d_poll)(dev, ap->a_events, ap->a_p);
515
516 default:
517 return (genfs_poll(v));
518 }
519 }
520
521 /* ARGSUSED */
522 int
523 spec_kqfilter(v)
524 void *v;
525 {
526 struct vop_kqfilter_args /* {
527 struct vnode *a_vp;
528 struct proc *a_kn;
529 } */ *ap = v;
530 const struct cdevsw *cdev;
531 dev_t dev;
532
533 switch (ap->a_vp->v_type) {
534
535 case VCHR:
536 dev = ap->a_vp->v_rdev;
537 cdev = cdevsw_lookup(dev);
538 if (cdev == NULL)
539 return (ENXIO);
540 return (*cdev->d_kqfilter)(dev, ap->a_kn);
541 default:
542 /*
543 * Block devices don't support kqfilter, and refuse it
544 * for any other files (like those vflush()ed) too.
545 */
546 return (EOPNOTSUPP);
547 }
548 }
549
550 /*
551 * Synch buffers associated with a block device
552 */
553 /* ARGSUSED */
554 int
555 spec_fsync(v)
556 void *v;
557 {
558 struct vop_fsync_args /* {
559 struct vnode *a_vp;
560 struct ucred *a_cred;
561 int a_flags;
562 off_t offlo;
563 off_t offhi;
564 struct proc *a_p;
565 } */ *ap = v;
566 struct vnode *vp = ap->a_vp;
567
568 if (vp->v_type == VBLK)
569 vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
570 return (0);
571 }
572
573 /*
574 * Just call the device strategy routine
575 */
576 int
577 spec_strategy(v)
578 void *v;
579 {
580 struct vop_strategy_args /* {
581 struct buf *a_bp;
582 } */ *ap = v;
583 struct buf *bp;
584 const struct bdevsw *bdev;
585
586 bp = ap->a_bp;
587 if (!(bp->b_flags & B_READ) &&
588 (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
589 (*bioops.io_start)(bp);
590 bdev = bdevsw_lookup(bp->b_dev);
591 if (bdev != NULL)
592 (*bdev->d_strategy)(bp);
593 return (0);
594 }
595
596 int
597 spec_inactive(v)
598 void *v;
599 {
600 struct vop_inactive_args /* {
601 struct vnode *a_vp;
602 struct proc *a_p;
603 } */ *ap = v;
604
605 VOP_UNLOCK(ap->a_vp, 0);
606 return (0);
607 }
608
609 /*
610 * This is a noop, simply returning what one has been given.
611 */
612 int
613 spec_bmap(v)
614 void *v;
615 {
616 struct vop_bmap_args /* {
617 struct vnode *a_vp;
618 daddr_t a_bn;
619 struct vnode **a_vpp;
620 daddr_t *a_bnp;
621 int *a_runp;
622 } */ *ap = v;
623
624 if (ap->a_vpp != NULL)
625 *ap->a_vpp = ap->a_vp;
626 if (ap->a_bnp != NULL)
627 *ap->a_bnp = ap->a_bn;
628 if (ap->a_runp != NULL)
629 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
630 return (0);
631 }
632
633 /*
634 * Device close routine
635 */
636 /* ARGSUSED */
637 int
638 spec_close(v)
639 void *v;
640 {
641 struct vop_close_args /* {
642 struct vnode *a_vp;
643 int a_fflag;
644 struct ucred *a_cred;
645 struct proc *a_p;
646 } */ *ap = v;
647 struct vnode *vp = ap->a_vp;
648 const struct bdevsw *bdev;
649 const struct cdevsw *cdev;
650 dev_t dev = vp->v_rdev;
651 int (*devclose) __P((dev_t, int, int, struct proc *));
652 int mode, error, count, flags, flags1;
653
654 count = vcount(vp);
655 simple_lock(&vp->v_interlock);
656 flags = vp->v_flag;
657 simple_unlock(&vp->v_interlock);
658
659 switch (vp->v_type) {
660
661 case VCHR:
662 /*
663 * Hack: a tty device that is a controlling terminal
664 * has a reference from the session structure.
665 * We cannot easily tell that a character device is
666 * a controlling terminal, unless it is the closing
667 * process' controlling terminal. In that case,
668 * if the reference count is 2 (this last descriptor
669 * plus the session), release the reference from the session.
670 */
671 if (count == 2 && ap->a_p &&
672 vp == ap->a_p->p_session->s_ttyvp) {
673 vrele(vp);
674 count--;
675 ap->a_p->p_session->s_ttyvp = NULL;
676 }
677 /*
678 * If the vnode is locked, then we are in the midst
679 * of forcably closing the device, otherwise we only
680 * close on last reference.
681 */
682 if (count > 1 && (flags & VXLOCK) == 0)
683 return (0);
684 cdev = cdevsw_lookup(dev);
685 if (cdev != NULL)
686 devclose = cdev->d_close;
687 else
688 devclose = NULL;
689 mode = S_IFCHR;
690 break;
691
692 case VBLK:
693 /*
694 * On last close of a block device (that isn't mounted)
695 * we must invalidate any in core blocks, so that
696 * we can, for instance, change floppy disks.
697 */
698 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
699 if (error)
700 return (error);
701 /*
702 * We do not want to really close the device if it
703 * is still in use unless we are trying to close it
704 * forcibly. Since every use (buffer, vnode, swap, cmap)
705 * holds a reference to the vnode, and because we mark
706 * any other vnodes that alias this device, when the
707 * sum of the reference counts on all the aliased
708 * vnodes descends to one, we are on last close.
709 */
710 if (count > 1 && (flags & VXLOCK) == 0)
711 return (0);
712 bdev = bdevsw_lookup(dev);
713 if (bdev != NULL)
714 devclose = bdev->d_close;
715 else
716 devclose = NULL;
717 mode = S_IFBLK;
718 break;
719
720 default:
721 panic("spec_close: not special");
722 }
723
724 flags1 = ap->a_fflag;
725
726 /*
727 * if VXLOCK is set, then we're going away soon, so make this
728 * non-blocking. Also ensures that we won't wedge in vn_lock below.
729 */
730 if (flags & VXLOCK)
731 flags1 |= FNONBLOCK;
732
733 /*
734 * If we're able to block, release the vnode lock & reacquire. We
735 * might end up sleaping for someone else who wants our queues. They
736 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
737 * don't release the lock as we won't be able to regain it.
738 */
739 if (!(flags1 & FNONBLOCK))
740 VOP_UNLOCK(vp, 0);
741
742 if (devclose != NULL)
743 error = (*devclose)(dev, flags1, mode, ap->a_p);
744 else
745 error = ENXIO;
746
747 if (!(flags1 & FNONBLOCK))
748 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
749
750 return (error);
751 }
752
753 /*
754 * Print out the contents of a special device vnode.
755 */
756 int
757 spec_print(v)
758 void *v;
759 {
760 struct vop_print_args /* {
761 struct vnode *a_vp;
762 } */ *ap = v;
763
764 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
765 minor(ap->a_vp->v_rdev));
766 return 0;
767 }
768
769 /*
770 * Return POSIX pathconf information applicable to special devices.
771 */
772 int
773 spec_pathconf(v)
774 void *v;
775 {
776 struct vop_pathconf_args /* {
777 struct vnode *a_vp;
778 int a_name;
779 register_t *a_retval;
780 } */ *ap = v;
781
782 switch (ap->a_name) {
783 case _PC_LINK_MAX:
784 *ap->a_retval = LINK_MAX;
785 return (0);
786 case _PC_MAX_CANON:
787 *ap->a_retval = MAX_CANON;
788 return (0);
789 case _PC_MAX_INPUT:
790 *ap->a_retval = MAX_INPUT;
791 return (0);
792 case _PC_PIPE_BUF:
793 *ap->a_retval = PIPE_BUF;
794 return (0);
795 case _PC_CHOWN_RESTRICTED:
796 *ap->a_retval = 1;
797 return (0);
798 case _PC_VDISABLE:
799 *ap->a_retval = _POSIX_VDISABLE;
800 return (0);
801 case _PC_SYNC_IO:
802 *ap->a_retval = 1;
803 return (0);
804 default:
805 return (EINVAL);
806 }
807 /* NOTREACHED */
808 }
809
810 /*
811 * Advisory record locking support.
812 */
813 int
814 spec_advlock(v)
815 void *v;
816 {
817 struct vop_advlock_args /* {
818 struct vnode *a_vp;
819 caddr_t a_id;
820 int a_op;
821 struct flock *a_fl;
822 int a_flags;
823 } */ *ap = v;
824 struct vnode *vp = ap->a_vp;
825
826 return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
827 }
828