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