1 /* $NetBSD: spec_vnops.c,v 1.81 2005/06/21 14:01:13 ws 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.81 2005/06/21 14:01:13 ws 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 55 #include <miscfs/genfs/genfs.h> 56 #include <miscfs/specfs/specdev.h> 57 58 /* symbolic sleep message strings for devices */ 59 const char devopn[] = "devopn"; 60 const char devio[] = "devio"; 61 const char devwait[] = "devwait"; 62 const char devin[] = "devin"; 63 const char devout[] = "devout"; 64 const char devioc[] = "devioc"; 65 const char devcls[] = "devcls"; 66 67 struct vnode *speclisth[SPECHSZ]; 68 69 /* 70 * This vnode operations vector is used for two things only: 71 * - special device nodes created from whole cloth by the kernel. 72 * - as a temporary vnodeops replacement for vnodes which were found to 73 * be aliased by callers of checkalias(). 74 * For the ops vector for vnodes built from special devices found in a 75 * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the 76 * equivalent for other filesystems. 77 */ 78 79 int (**spec_vnodeop_p) __P((void *)); 80 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = { 81 { &vop_default_desc, vn_default_error }, 82 { &vop_lookup_desc, spec_lookup }, /* lookup */ 83 { &vop_create_desc, spec_create }, /* create */ 84 { &vop_mknod_desc, spec_mknod }, /* mknod */ 85 { &vop_open_desc, spec_open }, /* open */ 86 { &vop_close_desc, spec_close }, /* close */ 87 { &vop_access_desc, spec_access }, /* access */ 88 { &vop_getattr_desc, spec_getattr }, /* getattr */ 89 { &vop_setattr_desc, spec_setattr }, /* setattr */ 90 { &vop_read_desc, spec_read }, /* read */ 91 { &vop_write_desc, spec_write }, /* write */ 92 { &vop_lease_desc, spec_lease_check }, /* lease */ 93 { &vop_fcntl_desc, spec_fcntl }, /* fcntl */ 94 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ 95 { &vop_poll_desc, spec_poll }, /* poll */ 96 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */ 97 { &vop_revoke_desc, spec_revoke }, /* revoke */ 98 { &vop_mmap_desc, spec_mmap }, /* mmap */ 99 { &vop_fsync_desc, spec_fsync }, /* fsync */ 100 { &vop_seek_desc, spec_seek }, /* seek */ 101 { &vop_remove_desc, spec_remove }, /* remove */ 102 { &vop_link_desc, spec_link }, /* link */ 103 { &vop_rename_desc, spec_rename }, /* rename */ 104 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ 105 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ 106 { &vop_symlink_desc, spec_symlink }, /* symlink */ 107 { &vop_readdir_desc, spec_readdir }, /* readdir */ 108 { &vop_readlink_desc, spec_readlink }, /* readlink */ 109 { &vop_abortop_desc, spec_abortop }, /* abortop */ 110 { &vop_inactive_desc, spec_inactive }, /* inactive */ 111 { &vop_reclaim_desc, spec_reclaim }, /* reclaim */ 112 { &vop_lock_desc, spec_lock }, /* lock */ 113 { &vop_unlock_desc, spec_unlock }, /* unlock */ 114 { &vop_bmap_desc, spec_bmap }, /* bmap */ 115 { &vop_strategy_desc, spec_strategy }, /* strategy */ 116 { &vop_print_desc, spec_print }, /* print */ 117 { &vop_islocked_desc, spec_islocked }, /* islocked */ 118 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ 119 { &vop_advlock_desc, spec_advlock }, /* advlock */ 120 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */ 121 { &vop_valloc_desc, spec_valloc }, /* valloc */ 122 { &vop_vfree_desc, spec_vfree }, /* vfree */ 123 { &vop_truncate_desc, spec_truncate }, /* truncate */ 124 { &vop_update_desc, spec_update }, /* update */ 125 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */ 126 { &vop_getpages_desc, spec_getpages }, /* getpages */ 127 { &vop_putpages_desc, spec_putpages }, /* putpages */ 128 { NULL, NULL } 129 }; 130 const struct vnodeopv_desc spec_vnodeop_opv_desc = 131 { &spec_vnodeop_p, spec_vnodeop_entries }; 132 133 /* 134 * Trivial lookup routine that always fails. 135 */ 136 int 137 spec_lookup(v) 138 void *v; 139 { 140 struct vop_lookup_args /* { 141 struct vnode *a_dvp; 142 struct vnode **a_vpp; 143 struct componentname *a_cnp; 144 } */ *ap = v; 145 146 *ap->a_vpp = NULL; 147 return (ENOTDIR); 148 } 149 150 /* 151 * Returns true if dev is /dev/mem or /dev/kmem. 152 */ 153 static int 154 iskmemdev(dev_t dev) 155 { 156 /* mem_no is emitted by config(8) to generated devsw.c */ 157 extern const int mem_no; 158 159 /* minor 14 is /dev/io on i386 with COMPAT_10 */ 160 return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14)); 161 } 162 163 /* 164 * Open a special file. 165 */ 166 /* ARGSUSED */ 167 int 168 spec_open(v) 169 void *v; 170 { 171 struct vop_open_args /* { 172 struct vnode *a_vp; 173 int a_mode; 174 struct ucred *a_cred; 175 struct proc *a_p; 176 } */ *ap = v; 177 struct proc *p = ap->a_p; 178 struct vnode *bvp, *vp = ap->a_vp; 179 const struct bdevsw *bdev; 180 const struct cdevsw *cdev; 181 dev_t blkdev, dev = (dev_t)vp->v_rdev; 182 int error; 183 struct partinfo pi; 184 int (*d_ioctl)(dev_t, u_long, caddr_t, int, struct proc *); 185 186 /* 187 * Don't allow open if fs is mounted -nodev. 188 */ 189 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV)) 190 return (ENXIO); 191 192 switch (vp->v_type) { 193 194 case VCHR: 195 cdev = cdevsw_lookup(dev); 196 if (cdev == NULL) 197 return (ENXIO); 198 if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) { 199 /* 200 * When running in very secure mode, do not allow 201 * opens for writing of any disk character devices. 202 */ 203 if (securelevel >= 2 && cdev->d_type == D_DISK) 204 return (EPERM); 205 /* 206 * When running in secure mode, do not allow opens 207 * for writing of /dev/mem, /dev/kmem, or character 208 * devices whose corresponding block devices are 209 * currently mounted. 210 */ 211 if (securelevel >= 1) { 212 blkdev = devsw_chr2blk(dev); 213 if (blkdev != (dev_t)NODEV && 214 vfinddev(blkdev, VBLK, &bvp) && 215 (error = vfs_mountedon(bvp))) 216 return (error); 217 if (iskmemdev(dev)) 218 return (EPERM); 219 } 220 } 221 if (cdev->d_type == D_TTY) 222 vp->v_flag |= VISTTY; 223 VOP_UNLOCK(vp, 0); 224 error = (*cdev->d_open)(dev, ap->a_mode, S_IFCHR, p); 225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 226 if (cdev->d_type != D_DISK) 227 return error; 228 d_ioctl = cdev->d_ioctl; 229 break; 230 231 case VBLK: 232 bdev = bdevsw_lookup(dev); 233 if (bdev == NULL) 234 return (ENXIO); 235 /* 236 * When running in very secure mode, do not allow 237 * opens for writing of any disk block devices. 238 */ 239 if (securelevel >= 2 && ap->a_cred != FSCRED && 240 (ap->a_mode & FWRITE) && bdev->d_type == D_DISK) 241 return (EPERM); 242 /* 243 * Do not allow opens of block devices that are 244 * currently mounted. 245 */ 246 if ((error = vfs_mountedon(vp)) != 0) 247 return (error); 248 error = (*bdev->d_open)(dev, ap->a_mode, S_IFBLK, p); 249 d_ioctl = bdev->d_ioctl; 250 break; 251 252 case VNON: 253 case VLNK: 254 case VDIR: 255 case VREG: 256 case VBAD: 257 case VFIFO: 258 case VSOCK: 259 default: 260 return 0; 261 } 262 263 if (error) 264 return error; 265 if (!(*d_ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&pi, FREAD, curproc)) 266 vp->v_size = (voff_t)pi.disklab->d_secsize * pi.part->p_size; 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 void *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 (POLLERR); 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 vnode *a_vp; 582 struct buf *a_bp; 583 } */ *ap = v; 584 struct vnode *vp = ap->a_vp; 585 struct buf *bp = ap->a_bp; 586 int error, s; 587 struct spec_cow_entry *e; 588 589 error = 0; 590 bp->b_dev = vp->v_rdev; 591 if (!(bp->b_flags & B_READ) && 592 (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start) 593 (*bioops.io_start)(bp); 594 595 if (!(bp->b_flags & B_READ) && !SLIST_EMPTY(&vp->v_spec_cow_head)) { 596 SPEC_COW_LOCK(vp->v_specinfo, s); 597 while (vp->v_spec_cow_req > 0) 598 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0, 599 &vp->v_spec_cow_slock); 600 vp->v_spec_cow_count++; 601 SPEC_COW_UNLOCK(vp->v_specinfo, s); 602 603 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list) { 604 if ((error = (*e->ce_func)(e->ce_cookie, bp)) != 0) 605 break; 606 } 607 608 SPEC_COW_LOCK(vp->v_specinfo, s); 609 vp->v_spec_cow_count--; 610 if (vp->v_spec_cow_req && vp->v_spec_cow_count == 0) 611 wakeup(&vp->v_spec_cow_req); 612 SPEC_COW_UNLOCK(vp->v_specinfo, s); 613 } 614 615 if (error) { 616 bp->b_error = error; 617 bp->b_flags |= B_ERROR; 618 biodone(bp); 619 return (error); 620 } 621 622 DEV_STRATEGY(bp); 623 624 return (0); 625 } 626 627 int 628 spec_inactive(v) 629 void *v; 630 { 631 struct vop_inactive_args /* { 632 struct vnode *a_vp; 633 struct proc *a_p; 634 } */ *ap = v; 635 636 VOP_UNLOCK(ap->a_vp, 0); 637 return (0); 638 } 639 640 /* 641 * This is a noop, simply returning what one has been given. 642 */ 643 int 644 spec_bmap(v) 645 void *v; 646 { 647 struct vop_bmap_args /* { 648 struct vnode *a_vp; 649 daddr_t a_bn; 650 struct vnode **a_vpp; 651 daddr_t *a_bnp; 652 int *a_runp; 653 } */ *ap = v; 654 655 if (ap->a_vpp != NULL) 656 *ap->a_vpp = ap->a_vp; 657 if (ap->a_bnp != NULL) 658 *ap->a_bnp = ap->a_bn; 659 if (ap->a_runp != NULL) 660 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1; 661 return (0); 662 } 663 664 /* 665 * Device close routine 666 */ 667 /* ARGSUSED */ 668 int 669 spec_close(v) 670 void *v; 671 { 672 struct vop_close_args /* { 673 struct vnode *a_vp; 674 int a_fflag; 675 struct ucred *a_cred; 676 struct proc *a_p; 677 } */ *ap = v; 678 struct vnode *vp = ap->a_vp; 679 const struct bdevsw *bdev; 680 const struct cdevsw *cdev; 681 struct session *sess; 682 dev_t dev = vp->v_rdev; 683 int (*devclose) __P((dev_t, int, int, struct proc *)); 684 int mode, error, count, flags, flags1; 685 686 count = vcount(vp); 687 flags = vp->v_flag; 688 689 switch (vp->v_type) { 690 691 case VCHR: 692 /* 693 * Hack: a tty device that is a controlling terminal 694 * has a reference from the session structure. 695 * We cannot easily tell that a character device is 696 * a controlling terminal, unless it is the closing 697 * process' controlling terminal. In that case, 698 * if the reference count is 2 (this last descriptor 699 * plus the session), release the reference from the session. 700 * Also remove the link from the tty back to the session 701 * and pgrp - due to the way consoles are handled we cannot 702 * guarantee that the vrele() will do the final close on the 703 * actual tty device. 704 */ 705 if (count == 2 && ap->a_p && 706 vp == (sess = ap->a_p->p_session)->s_ttyvp) { 707 sess->s_ttyvp = NULL; 708 if (sess->s_ttyp->t_session != NULL) { 709 sess->s_ttyp->t_pgrp = NULL; 710 sess->s_ttyp->t_session = NULL; 711 SESSRELE(sess); 712 } else if (sess->s_ttyp->t_pgrp != NULL) 713 panic("spec_close: spurious pgrp ref"); 714 vrele(vp); 715 count--; 716 } 717 /* 718 * If the vnode is locked, then we are in the midst 719 * of forcably closing the device, otherwise we only 720 * close on last reference. 721 */ 722 if (count > 1 && (flags & VXLOCK) == 0) 723 return (0); 724 cdev = cdevsw_lookup(dev); 725 if (cdev != NULL) 726 devclose = cdev->d_close; 727 else 728 devclose = NULL; 729 mode = S_IFCHR; 730 break; 731 732 case VBLK: 733 /* 734 * On last close of a block device (that isn't mounted) 735 * we must invalidate any in core blocks, so that 736 * we can, for instance, change floppy disks. 737 */ 738 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0); 739 if (error) 740 return (error); 741 /* 742 * We do not want to really close the device if it 743 * is still in use unless we are trying to close it 744 * forcibly. Since every use (buffer, vnode, swap, cmap) 745 * holds a reference to the vnode, and because we mark 746 * any other vnodes that alias this device, when the 747 * sum of the reference counts on all the aliased 748 * vnodes descends to one, we are on last close. 749 */ 750 if (count > 1 && (flags & VXLOCK) == 0) 751 return (0); 752 bdev = bdevsw_lookup(dev); 753 if (bdev != NULL) 754 devclose = bdev->d_close; 755 else 756 devclose = NULL; 757 mode = S_IFBLK; 758 break; 759 760 default: 761 panic("spec_close: not special"); 762 } 763 764 flags1 = ap->a_fflag; 765 766 /* 767 * if VXLOCK is set, then we're going away soon, so make this 768 * non-blocking. Also ensures that we won't wedge in vn_lock below. 769 */ 770 if (flags & VXLOCK) 771 flags1 |= FNONBLOCK; 772 773 /* 774 * If we're able to block, release the vnode lock & reacquire. We 775 * might end up sleeping for someone else who wants our queues. They 776 * won't get them if we hold the vnode locked. Also, if VXLOCK is set, 777 * don't release the lock as we won't be able to regain it. 778 */ 779 if (!(flags1 & FNONBLOCK)) 780 VOP_UNLOCK(vp, 0); 781 782 if (devclose != NULL) 783 error = (*devclose)(dev, flags1, mode, ap->a_p); 784 else 785 error = ENXIO; 786 787 if (!(flags1 & FNONBLOCK)) 788 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 789 790 return (error); 791 } 792 793 /* 794 * Print out the contents of a special device vnode. 795 */ 796 int 797 spec_print(v) 798 void *v; 799 { 800 struct vop_print_args /* { 801 struct vnode *a_vp; 802 } */ *ap = v; 803 804 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev), 805 minor(ap->a_vp->v_rdev)); 806 return 0; 807 } 808 809 /* 810 * Return POSIX pathconf information applicable to special devices. 811 */ 812 int 813 spec_pathconf(v) 814 void *v; 815 { 816 struct vop_pathconf_args /* { 817 struct vnode *a_vp; 818 int a_name; 819 register_t *a_retval; 820 } */ *ap = v; 821 822 switch (ap->a_name) { 823 case _PC_LINK_MAX: 824 *ap->a_retval = LINK_MAX; 825 return (0); 826 case _PC_MAX_CANON: 827 *ap->a_retval = MAX_CANON; 828 return (0); 829 case _PC_MAX_INPUT: 830 *ap->a_retval = MAX_INPUT; 831 return (0); 832 case _PC_PIPE_BUF: 833 *ap->a_retval = PIPE_BUF; 834 return (0); 835 case _PC_CHOWN_RESTRICTED: 836 *ap->a_retval = 1; 837 return (0); 838 case _PC_VDISABLE: 839 *ap->a_retval = _POSIX_VDISABLE; 840 return (0); 841 case _PC_SYNC_IO: 842 *ap->a_retval = 1; 843 return (0); 844 default: 845 return (EINVAL); 846 } 847 /* NOTREACHED */ 848 } 849 850 /* 851 * Advisory record locking support. 852 */ 853 int 854 spec_advlock(v) 855 void *v; 856 { 857 struct vop_advlock_args /* { 858 struct vnode *a_vp; 859 void *a_id; 860 int a_op; 861 struct flock *a_fl; 862 int a_flags; 863 } */ *ap = v; 864 struct vnode *vp = ap->a_vp; 865 866 return lf_advlock(ap, &vp->v_speclockf, (off_t)0); 867 } 868