spec_vnops.c revision 1.71 1 /* $NetBSD: spec_vnops.c,v 1.71 2003/11/06 08:58:04 dsl 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.71 2003/11/06 08:58:04 dsl 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 caddr_t 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 buf *a_bp;
581 } */ *ap = v;
582 struct buf *bp;
583 const struct bdevsw *bdev;
584
585 bp = ap->a_bp;
586 if (!(bp->b_flags & B_READ) &&
587 (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
588 (*bioops.io_start)(bp);
589 bdev = bdevsw_lookup(bp->b_dev);
590 if (bdev != NULL)
591 (*bdev->d_strategy)(bp);
592 return (0);
593 }
594
595 int
596 spec_inactive(v)
597 void *v;
598 {
599 struct vop_inactive_args /* {
600 struct vnode *a_vp;
601 struct proc *a_p;
602 } */ *ap = v;
603
604 VOP_UNLOCK(ap->a_vp, 0);
605 return (0);
606 }
607
608 /*
609 * This is a noop, simply returning what one has been given.
610 */
611 int
612 spec_bmap(v)
613 void *v;
614 {
615 struct vop_bmap_args /* {
616 struct vnode *a_vp;
617 daddr_t a_bn;
618 struct vnode **a_vpp;
619 daddr_t *a_bnp;
620 int *a_runp;
621 } */ *ap = v;
622
623 if (ap->a_vpp != NULL)
624 *ap->a_vpp = ap->a_vp;
625 if (ap->a_bnp != NULL)
626 *ap->a_bnp = ap->a_bn;
627 if (ap->a_runp != NULL)
628 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
629 return (0);
630 }
631
632 /*
633 * Device close routine
634 */
635 /* ARGSUSED */
636 int
637 spec_close(v)
638 void *v;
639 {
640 struct vop_close_args /* {
641 struct vnode *a_vp;
642 int a_fflag;
643 struct ucred *a_cred;
644 struct proc *a_p;
645 } */ *ap = v;
646 struct vnode *vp = ap->a_vp;
647 const struct bdevsw *bdev;
648 const struct cdevsw *cdev;
649 struct session *sess;
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 * Also remove the link from the tty back to the session
671 * and pgrp - due to the way consoles are handled we cannot
672 * guarantee that the vrele() will do the final close on the
673 * actual tty device.
674 */
675 if (count == 2 && ap->a_p &&
676 vp == (sess = ap->a_p->p_session)->s_ttyvp) {
677 sess->s_ttyvp = NULL;
678 sess->s_ttyp->t_pgrp = NULL;
679 sess->s_ttyp->t_session = NULL;
680 SESSRELE(sess);
681 vrele(vp);
682 count--;
683 }
684 /*
685 * If the vnode is locked, then we are in the midst
686 * of forcably closing the device, otherwise we only
687 * close on last reference.
688 */
689 if (count > 1 && (flags & VXLOCK) == 0)
690 return (0);
691 cdev = cdevsw_lookup(dev);
692 if (cdev != NULL)
693 devclose = cdev->d_close;
694 else
695 devclose = NULL;
696 mode = S_IFCHR;
697 break;
698
699 case VBLK:
700 /*
701 * On last close of a block device (that isn't mounted)
702 * we must invalidate any in core blocks, so that
703 * we can, for instance, change floppy disks.
704 */
705 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
706 if (error)
707 return (error);
708 /*
709 * We do not want to really close the device if it
710 * is still in use unless we are trying to close it
711 * forcibly. Since every use (buffer, vnode, swap, cmap)
712 * holds a reference to the vnode, and because we mark
713 * any other vnodes that alias this device, when the
714 * sum of the reference counts on all the aliased
715 * vnodes descends to one, we are on last close.
716 */
717 if (count > 1 && (flags & VXLOCK) == 0)
718 return (0);
719 bdev = bdevsw_lookup(dev);
720 if (bdev != NULL)
721 devclose = bdev->d_close;
722 else
723 devclose = NULL;
724 mode = S_IFBLK;
725 break;
726
727 default:
728 panic("spec_close: not special");
729 }
730
731 flags1 = ap->a_fflag;
732
733 /*
734 * if VXLOCK is set, then we're going away soon, so make this
735 * non-blocking. Also ensures that we won't wedge in vn_lock below.
736 */
737 if (flags & VXLOCK)
738 flags1 |= FNONBLOCK;
739
740 /*
741 * If we're able to block, release the vnode lock & reacquire. We
742 * might end up sleaping for someone else who wants our queues. They
743 * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
744 * don't release the lock as we won't be able to regain it.
745 */
746 if (!(flags1 & FNONBLOCK))
747 VOP_UNLOCK(vp, 0);
748
749 if (devclose != NULL)
750 error = (*devclose)(dev, flags1, mode, ap->a_p);
751 else
752 error = ENXIO;
753
754 if (!(flags1 & FNONBLOCK))
755 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
756
757 return (error);
758 }
759
760 /*
761 * Print out the contents of a special device vnode.
762 */
763 int
764 spec_print(v)
765 void *v;
766 {
767 struct vop_print_args /* {
768 struct vnode *a_vp;
769 } */ *ap = v;
770
771 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
772 minor(ap->a_vp->v_rdev));
773 return 0;
774 }
775
776 /*
777 * Return POSIX pathconf information applicable to special devices.
778 */
779 int
780 spec_pathconf(v)
781 void *v;
782 {
783 struct vop_pathconf_args /* {
784 struct vnode *a_vp;
785 int a_name;
786 register_t *a_retval;
787 } */ *ap = v;
788
789 switch (ap->a_name) {
790 case _PC_LINK_MAX:
791 *ap->a_retval = LINK_MAX;
792 return (0);
793 case _PC_MAX_CANON:
794 *ap->a_retval = MAX_CANON;
795 return (0);
796 case _PC_MAX_INPUT:
797 *ap->a_retval = MAX_INPUT;
798 return (0);
799 case _PC_PIPE_BUF:
800 *ap->a_retval = PIPE_BUF;
801 return (0);
802 case _PC_CHOWN_RESTRICTED:
803 *ap->a_retval = 1;
804 return (0);
805 case _PC_VDISABLE:
806 *ap->a_retval = _POSIX_VDISABLE;
807 return (0);
808 case _PC_SYNC_IO:
809 *ap->a_retval = 1;
810 return (0);
811 default:
812 return (EINVAL);
813 }
814 /* NOTREACHED */
815 }
816
817 /*
818 * Advisory record locking support.
819 */
820 int
821 spec_advlock(v)
822 void *v;
823 {
824 struct vop_advlock_args /* {
825 struct vnode *a_vp;
826 caddr_t a_id;
827 int a_op;
828 struct flock *a_fl;
829 int a_flags;
830 } */ *ap = v;
831 struct vnode *vp = ap->a_vp;
832
833 return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
834 }
835