1 /*- 2 * Copyright (c) 2000-2004 3 * Poul-Henning Kamp. All rights reserved. 4 * Copyright (c) 1989, 1992-1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. 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 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95 32 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43 33 * 34 * $FreeBSD$ 35 */ 36 37 /* 38 * TODO: 39 * remove empty directories 40 * mknod: hunt down DE_DELETED, compare name, reinstantiate. 41 * mkdir: want it ? 42 */ 43 44 #include <opt_devfs.h> 45 #include <opt_mac.h> 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/conf.h> 50 #include <sys/dirent.h> 51 #include <sys/fcntl.h> 52 #include <sys/file.h> 53 #include <sys/filedesc.h> 54 #include <sys/filio.h> 55 #include <sys/kernel.h> 56 #include <sys/lock.h> 57 #include <sys/mac.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/namei.h> 61 #include <sys/proc.h> 62 #include <sys/stat.h> 63 #include <sys/sx.h> 64 #include <sys/sysctl.h> 65 #include <sys/time.h> 66 #include <sys/ttycom.h> 67 #include <sys/unistd.h> 68 #include <sys/vnode.h> 69 70 #include <fs/devfs/devfs.h> 71 72 static fo_rdwr_t devfs_read_f; 73 static fo_rdwr_t devfs_write_f; 74 static fo_ioctl_t devfs_ioctl_f; 75 static fo_poll_t devfs_poll_f; 76 static fo_kqfilter_t devfs_kqfilter_f; 77 static fo_stat_t devfs_stat_f; 78 static fo_close_t devfs_close_f; 79 80 static struct fileops devfs_ops_f = { 81 .fo_read = devfs_read_f, 82 .fo_write = devfs_write_f, 83 .fo_ioctl = devfs_ioctl_f, 84 .fo_poll = devfs_poll_f, 85 .fo_kqfilter = devfs_kqfilter_f, 86 .fo_stat = devfs_stat_f, 87 .fo_close = devfs_close_f, 88 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 89 }; 90 91 static vop_access_t devfs_access; 92 static vop_advlock_t devfs_advlock; 93 static vop_close_t devfs_close; 94 static vop_fsync_t devfs_fsync; 95 static vop_getattr_t devfs_getattr; 96 static vop_lookup_t devfs_lookupx; 97 static vop_mknod_t devfs_mknod; 98 static vop_open_t devfs_open; 99 static vop_pathconf_t devfs_pathconf; 100 static vop_print_t devfs_print; 101 static vop_readdir_t devfs_readdir; 102 static vop_readlink_t devfs_readlink; 103 static vop_reclaim_t devfs_reclaim; 104 static vop_remove_t devfs_remove; 105 static vop_revoke_t devfs_revoke; 106 static vop_ioctl_t devfs_rioctl; 107 static vop_read_t devfs_rread; 108 static vop_setattr_t devfs_setattr; 109 #ifdef MAC 110 static vop_setlabel_t devfs_setlabel; 111 #endif 112 static vop_symlink_t devfs_symlink; 113 114 extern struct vop_vector devfs_vnodeops; 115 extern struct vop_vector devfs_specops; 116 117 static u_int 118 devfs_random(void) 119 { 120 static u_int devfs_seed; 121 122 while (devfs_seed == 0) { 123 /* 124 * Make sure people don't make stupid assumptions 125 * about device major/minor numbers in userspace. 126 * We do this late to get entropy and for the same 127 * reason we force a reseed, but it may not be 128 * late enough for entropy to be available. 129 */ 130 arc4rand(&devfs_seed, sizeof devfs_seed, 1); 131 devfs_seed &= 0xf0f; 132 } 133 return (devfs_seed); 134 } 135 136 static int 137 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp) 138 { 139 140 *devp = fp->f_vnode->v_rdev; 141 if (*devp != fp->f_data) 142 return (ENXIO); 143 KASSERT((*devp)->si_refcount > 0, 144 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp))); 145 *dswp = dev_refthread(*devp); 146 if (*dswp == NULL) 147 return (ENXIO); 148 return (0); 149 } 150 151 /* 152 * Construct the fully qualified path name relative to the mountpoint 153 */ 154 static char * 155 devfs_fqpn(char *buf, struct vnode *dvp, struct componentname *cnp) 156 { 157 int i; 158 struct devfs_dirent *de, *dd; 159 struct devfs_mount *dmp; 160 161 dmp = VFSTODEVFS(dvp->v_mount); 162 dd = dvp->v_data; 163 i = SPECNAMELEN; 164 buf[i] = '\0'; 165 i -= cnp->cn_namelen; 166 if (i < 0) 167 return (NULL); 168 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen); 169 de = dd; 170 while (de != dmp->dm_basedir) { 171 i--; 172 if (i < 0) 173 return (NULL); 174 buf[i] = '/'; 175 i -= de->de_dirent->d_namlen; 176 if (i < 0) 177 return (NULL); 178 bcopy(de->de_dirent->d_name, buf + i, 179 de->de_dirent->d_namlen); 180 de = TAILQ_FIRST(&de->de_dlist); /* "." */ 181 de = TAILQ_NEXT(de, de_list); /* ".." */ 182 de = de->de_dir; 183 } 184 return (buf + i); 185 } 186 187 int 188 devfs_allocv(struct devfs_dirent *de, struct mount *mp, struct vnode **vpp, struct thread *td) 189 { 190 int error; 191 struct vnode *vp; 192 struct cdev *dev; 193 194 KASSERT(td == curthread, ("devfs_allocv: td != curthread")); 195 loop: 196 vp = de->de_vnode; 197 if (vp != NULL) { 198 if (vget(vp, LK_EXCLUSIVE, td)) 199 goto loop; 200 *vpp = vp; 201 return (0); 202 } 203 if (de->de_dirent->d_type == DT_CHR) { 204 dev = *devfs_itod(de->de_inode); 205 if (dev == NULL) 206 return (ENOENT); 207 } else { 208 dev = NULL; 209 } 210 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp); 211 if (error != 0) { 212 printf("devfs_allocv: failed to allocate new vnode\n"); 213 return (error); 214 } 215 216 if (de->de_dirent->d_type == DT_CHR) { 217 vp->v_type = VCHR; 218 VI_LOCK(vp); 219 dev_lock(); 220 dev_refl(dev); 221 vp->v_rdev = dev; 222 LIST_INSERT_HEAD(&dev->si_alist, de, de_alias); 223 dev->si_usecount += vp->v_usecount; 224 dev_unlock(); 225 VI_UNLOCK(vp); 226 vp->v_op = &devfs_specops; 227 } else if (de->de_dirent->d_type == DT_DIR) { 228 vp->v_type = VDIR; 229 } else if (de->de_dirent->d_type == DT_LNK) { 230 vp->v_type = VLNK; 231 } else { 232 vp->v_type = VBAD; 233 } 234 vp->v_data = de; 235 de->de_vnode = vp; 236 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 237 #ifdef MAC 238 mac_associate_vnode_devfs(mp, de, vp); 239 #endif 240 *vpp = vp; 241 return (0); 242 } 243 244 static int 245 devfs_access(ap) 246 struct vop_access_args /* { 247 struct vnode *a_vp; 248 int a_mode; 249 struct ucred *a_cred; 250 struct thread *a_td; 251 } */ *ap; 252 { 253 struct vnode *vp = ap->a_vp; 254 struct devfs_dirent *de; 255 int error; 256 257 de = vp->v_data; 258 if (vp->v_type == VDIR) 259 de = de->de_dir; 260 261 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid, 262 ap->a_mode, ap->a_cred, NULL); 263 if (!error) 264 return (error); 265 if (error != EACCES) 266 return (error); 267 /* We do, however, allow access to the controlling terminal */ 268 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT)) 269 return (error); 270 if (ap->a_td->td_proc->p_session->s_ttyvp == de->de_vnode) 271 return (0); 272 return (error); 273 } 274 275 /* 276 * Special device advisory byte-level locks. 277 */ 278 /* ARGSUSED */ 279 static int 280 devfs_advlock(ap) 281 struct vop_advlock_args /* { 282 struct vnode *a_vp; 283 caddr_t a_id; 284 int a_op; 285 struct flock *a_fl; 286 int a_flags; 287 } */ *ap; 288 { 289 290 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 291 } 292 293 /* 294 * Device close routine 295 */ 296 /* ARGSUSED */ 297 static int 298 devfs_close(ap) 299 struct vop_close_args /* { 300 struct vnode *a_vp; 301 int a_fflag; 302 struct ucred *a_cred; 303 struct thread *a_td; 304 } */ *ap; 305 { 306 struct vnode *vp = ap->a_vp, *oldvp; 307 struct thread *td = ap->a_td; 308 struct cdev *dev = vp->v_rdev; 309 struct cdevsw *dsw; 310 int error; 311 312 /* 313 * Hack: a tty device that is a controlling terminal 314 * has a reference from the session structure. 315 * We cannot easily tell that a character device is 316 * a controlling terminal, unless it is the closing 317 * process' controlling terminal. In that case, 318 * if the reference count is 2 (this last descriptor 319 * plus the session), release the reference from the session. 320 */ 321 322 /* 323 * This needs to be rewritten to take the vp interlock into 324 * consideration. 325 */ 326 327 oldvp = NULL; 328 sx_xlock(&proctree_lock); 329 if (td && vp == td->td_proc->p_session->s_ttyvp) { 330 SESS_LOCK(td->td_proc->p_session); 331 VI_LOCK(vp); 332 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) { 333 td->td_proc->p_session->s_ttyvp = NULL; 334 oldvp = vp; 335 } 336 VI_UNLOCK(vp); 337 SESS_UNLOCK(td->td_proc->p_session); 338 } 339 sx_xunlock(&proctree_lock); 340 if (oldvp != NULL) 341 vrele(oldvp); 342 /* 343 * We do not want to really close the device if it 344 * is still in use unless we are trying to close it 345 * forcibly. Since every use (buffer, vnode, swap, cmap) 346 * holds a reference to the vnode, and because we mark 347 * any other vnodes that alias this device, when the 348 * sum of the reference counts on all the aliased 349 * vnodes descends to one, we are on last close. 350 */ 351 dsw = dev_refthread(dev); 352 if (dsw == NULL) 353 return (ENXIO); 354 VI_LOCK(vp); 355 if (vp->v_iflag & VI_DOOMED) { 356 /* Forced close. */ 357 } else if (dsw->d_flags & D_TRACKCLOSE) { 358 /* Keep device updated on status. */ 359 } else if (count_dev(dev) > 1) { 360 VI_UNLOCK(vp); 361 dev_relthread(dev); 362 return (0); 363 } 364 VI_UNLOCK(vp); 365 KASSERT(dev->si_refcount > 0, 366 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev))); 367 if (!(dsw->d_flags & D_NEEDGIANT)) { 368 DROP_GIANT(); 369 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td); 370 PICKUP_GIANT(); 371 } else { 372 mtx_lock(&Giant); 373 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td); 374 mtx_unlock(&Giant); 375 } 376 dev_relthread(dev); 377 return (error); 378 } 379 380 static int 381 devfs_close_f(struct file *fp, struct thread *td) 382 { 383 384 return (vnops.fo_close(fp, td)); 385 } 386 387 /* 388 * Synch buffers associated with a block device 389 */ 390 /* ARGSUSED */ 391 static int 392 devfs_fsync(ap) 393 struct vop_fsync_args /* { 394 struct vnode *a_vp; 395 struct ucred *a_cred; 396 int a_waitfor; 397 struct thread *a_td; 398 } */ *ap; 399 { 400 if (!vn_isdisk(ap->a_vp, NULL)) 401 return (0); 402 403 return (vop_stdfsync(ap)); 404 } 405 406 static int 407 devfs_getattr(ap) 408 struct vop_getattr_args /* { 409 struct vnode *a_vp; 410 struct vattr *a_vap; 411 struct ucred *a_cred; 412 struct thread *a_td; 413 } */ *ap; 414 { 415 struct vnode *vp = ap->a_vp; 416 struct vattr *vap = ap->a_vap; 417 int error = 0; 418 struct devfs_dirent *de; 419 struct cdev *dev; 420 421 de = vp->v_data; 422 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp)); 423 if (vp->v_type == VDIR) { 424 de = de->de_dir; 425 KASSERT(de != NULL, 426 ("Null dir dirent in devfs_getattr vp=%p", vp)); 427 } 428 bzero((caddr_t) vap, sizeof(*vap)); 429 vattr_null(vap); 430 vap->va_uid = de->de_uid; 431 vap->va_gid = de->de_gid; 432 vap->va_mode = de->de_mode; 433 if (vp->v_type == VLNK) 434 vap->va_size = strlen(de->de_symlink); 435 else if (vp->v_type == VDIR) 436 vap->va_size = vap->va_bytes = DEV_BSIZE; 437 else 438 vap->va_size = 0; 439 if (vp->v_type != VDIR) 440 vap->va_bytes = 0; 441 vap->va_blocksize = DEV_BSIZE; 442 vap->va_type = vp->v_type; 443 444 #define fix(aa) \ 445 do { \ 446 if ((aa).tv_sec == 0) { \ 447 (aa).tv_sec = boottime.tv_sec; \ 448 (aa).tv_nsec = boottime.tv_usec * 1000; \ 449 } \ 450 } while (0) 451 452 if (vp->v_type != VCHR) { 453 fix(de->de_atime); 454 vap->va_atime = de->de_atime; 455 fix(de->de_mtime); 456 vap->va_mtime = de->de_mtime; 457 fix(de->de_ctime); 458 vap->va_ctime = de->de_ctime; 459 } else { 460 dev = vp->v_rdev; 461 fix(dev->si_atime); 462 vap->va_atime = dev->si_atime; 463 fix(dev->si_mtime); 464 vap->va_mtime = dev->si_mtime; 465 fix(dev->si_ctime); 466 vap->va_ctime = dev->si_ctime; 467 468 vap->va_rdev = dev->si_inode ^ devfs_random(); 469 } 470 vap->va_gen = 0; 471 vap->va_flags = 0; 472 vap->va_nlink = de->de_links; 473 vap->va_fileid = de->de_inode; 474 475 return (error); 476 } 477 478 /* 479 * Device ioctl operation. 480 */ 481 /* ARGSUSED */ 482 static int 483 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) 484 { 485 struct cdev *dev; 486 struct cdevsw *dsw; 487 struct vnode *vp; 488 struct vnode *vpold; 489 int error, i; 490 const char *p; 491 struct fiodgname_arg *fgn; 492 493 error = devfs_fp_check(fp, &dev, &dsw); 494 if (error) 495 return (error); 496 497 if (com == FIODTYPE) { 498 *(int *)data = dsw->d_flags & D_TYPEMASK; 499 dev_relthread(dev); 500 return (0); 501 } else if (com == FIODGNAME) { 502 fgn = data; 503 p = devtoname(dev); 504 i = strlen(p) + 1; 505 if (i > fgn->len) 506 return (EINVAL); 507 return (copyout(p, fgn->buf, i)); 508 } 509 if (dsw->d_flags & D_NEEDGIANT) 510 mtx_lock(&Giant); 511 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td); 512 if (dsw->d_flags & D_NEEDGIANT) 513 mtx_unlock(&Giant); 514 dev_relthread(dev); 515 if (error == ENOIOCTL) 516 error = ENOTTY; 517 if (error == 0 && com == TIOCSCTTY) { 518 vp = fp->f_vnode; 519 520 /* Do nothing if reassigning same control tty */ 521 sx_slock(&proctree_lock); 522 if (td->td_proc->p_session->s_ttyvp == vp) { 523 sx_sunlock(&proctree_lock); 524 return (0); 525 } 526 527 mtx_lock(&Giant); 528 529 vpold = td->td_proc->p_session->s_ttyvp; 530 VREF(vp); 531 SESS_LOCK(td->td_proc->p_session); 532 td->td_proc->p_session->s_ttyvp = vp; 533 SESS_UNLOCK(td->td_proc->p_session); 534 535 sx_sunlock(&proctree_lock); 536 537 /* Get rid of reference to old control tty */ 538 if (vpold) 539 vrele(vpold); 540 mtx_unlock(&Giant); 541 } 542 return (error); 543 } 544 545 546 /* ARGSUSED */ 547 static int 548 devfs_kqfilter_f(struct file *fp, struct knote *kn) 549 { 550 struct cdev *dev; 551 struct cdevsw *dsw; 552 int error; 553 554 error = devfs_fp_check(fp, &dev, &dsw); 555 if (error) 556 return (error); 557 if (dsw->d_flags & D_NEEDGIANT) 558 mtx_lock(&Giant); 559 error = dsw->d_kqfilter(dev, kn); 560 if (dsw->d_flags & D_NEEDGIANT) 561 mtx_unlock(&Giant); 562 dev_relthread(dev); 563 return (error); 564 } 565 566 static int 567 devfs_lookupx(ap) 568 struct vop_lookup_args /* { 569 struct vnode * a_dvp; 570 struct vnode ** a_vpp; 571 struct componentname * a_cnp; 572 } */ *ap; 573 { 574 struct componentname *cnp; 575 struct vnode *dvp, **vpp; 576 struct thread *td; 577 struct devfs_dirent *de, *dd; 578 struct devfs_dirent **dde; 579 struct devfs_mount *dmp; 580 struct cdev *cdev; 581 int error, flags, nameiop; 582 char specname[SPECNAMELEN + 1], *pname; 583 584 cnp = ap->a_cnp; 585 vpp = ap->a_vpp; 586 dvp = ap->a_dvp; 587 pname = cnp->cn_nameptr; 588 td = cnp->cn_thread; 589 flags = cnp->cn_flags; 590 nameiop = cnp->cn_nameiop; 591 dmp = VFSTODEVFS(dvp->v_mount); 592 dd = dvp->v_data; 593 *vpp = NULLVP; 594 595 if ((flags & ISLASTCN) && nameiop == RENAME) 596 return (EOPNOTSUPP); 597 598 if (dvp->v_type != VDIR) 599 return (ENOTDIR); 600 601 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) 602 return (EIO); 603 604 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 605 if (error) 606 return (error); 607 608 if (cnp->cn_namelen == 1 && *pname == '.') { 609 if ((flags & ISLASTCN) && nameiop != LOOKUP) 610 return (EINVAL); 611 *vpp = dvp; 612 VREF(dvp); 613 return (0); 614 } 615 616 if (flags & ISDOTDOT) { 617 if ((flags & ISLASTCN) && nameiop != LOOKUP) 618 return (EINVAL); 619 VOP_UNLOCK(dvp, 0, td); 620 de = TAILQ_FIRST(&dd->de_dlist); /* "." */ 621 de = TAILQ_NEXT(de, de_list); /* ".." */ 622 de = de->de_dir; 623 error = devfs_allocv(de, dvp->v_mount, vpp, td); 624 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 625 return (error); 626 } 627 628 devfs_populate(dmp); 629 dd = dvp->v_data; 630 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 631 if (cnp->cn_namelen != de->de_dirent->d_namlen) 632 continue; 633 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 634 de->de_dirent->d_namlen) != 0) 635 continue; 636 if (de->de_flags & DE_WHITEOUT) 637 goto notfound; 638 goto found; 639 } 640 641 if (nameiop == DELETE) 642 goto notfound; 643 644 /* 645 * OK, we didn't have an entry for the name we were asked for 646 * so we try to see if anybody can create it on demand. 647 */ 648 pname = devfs_fqpn(specname, dvp, cnp); 649 if (pname == NULL) 650 goto notfound; 651 652 cdev = NULL; 653 EVENTHANDLER_INVOKE(dev_clone, pname, strlen(pname), &cdev); 654 if (cdev == NULL) 655 goto notfound; 656 657 devfs_populate(dmp); 658 659 dde = devfs_itode(dmp, cdev->si_inode); 660 dev_rel(cdev); 661 662 if (dde == NULL || *dde == NULL || *dde == DE_DELETED) 663 goto notfound; 664 665 if ((*dde)->de_flags & DE_WHITEOUT) 666 goto notfound; 667 668 de = *dde; 669 goto found; 670 671 notfound: 672 673 if ((nameiop == CREATE || nameiop == RENAME) && 674 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) { 675 cnp->cn_flags |= SAVENAME; 676 return (EJUSTRETURN); 677 } 678 return (ENOENT); 679 680 681 found: 682 683 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) { 684 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 685 if (error) 686 return (error); 687 if (*vpp == dvp) { 688 VREF(dvp); 689 *vpp = dvp; 690 return (0); 691 } 692 error = devfs_allocv(de, dvp->v_mount, vpp, td); 693 if (error) 694 return (error); 695 return (0); 696 } 697 error = devfs_allocv(de, dvp->v_mount, vpp, td); 698 return (error); 699 } 700 701 static int 702 devfs_lookup(struct vop_lookup_args *ap) 703 { 704 int j; 705 struct devfs_mount *dmp; 706 707 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 708 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 709 j = devfs_lookupx(ap); 710 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 711 return (j); 712 } 713 714 static int 715 devfs_mknod(struct vop_mknod_args *ap) 716 /* 717 struct vop_mknod_args { 718 struct vnodeop_desc *a_desc; 719 struct vnode *a_dvp; 720 struct vnode **a_vpp; 721 struct componentname *a_cnp; 722 struct vattr *a_vap; 723 }; */ 724 { 725 struct componentname *cnp; 726 struct vnode *dvp, **vpp; 727 struct thread *td; 728 struct devfs_dirent *dd, *de; 729 struct devfs_mount *dmp; 730 int error; 731 732 dvp = ap->a_dvp; 733 dmp = VFSTODEVFS(dvp->v_mount); 734 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread); 735 736 cnp = ap->a_cnp; 737 vpp = ap->a_vpp; 738 td = cnp->cn_thread; 739 dd = dvp->v_data; 740 741 error = ENOENT; 742 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 743 if (cnp->cn_namelen != de->de_dirent->d_namlen) 744 continue; 745 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 746 de->de_dirent->d_namlen) != 0) 747 continue; 748 if (de->de_flags & DE_WHITEOUT) 749 break; 750 goto notfound; 751 } 752 if (de == NULL) 753 goto notfound; 754 de->de_flags &= ~DE_WHITEOUT; 755 error = devfs_allocv(de, dvp->v_mount, vpp, td); 756 notfound: 757 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 758 return (error); 759 } 760 761 /* 762 * Open a special file. 763 */ 764 /* ARGSUSED */ 765 static int 766 devfs_open(ap) 767 struct vop_open_args /* { 768 struct vnode *a_vp; 769 int a_mode; 770 struct ucred *a_cred; 771 struct thread *a_td; 772 int a_fdidx; 773 } */ *ap; 774 { 775 struct thread *td = ap->a_td; 776 struct vnode *vp = ap->a_vp; 777 struct cdev *dev = vp->v_rdev; 778 struct file *fp; 779 int error; 780 struct cdevsw *dsw; 781 782 if (vp->v_type == VBLK) 783 return (ENXIO); 784 785 if (dev == NULL) 786 return (ENXIO); 787 788 /* Make this field valid before any I/O in d_open. */ 789 if (dev->si_iosize_max == 0) 790 dev->si_iosize_max = DFLTPHYS; 791 792 if (vn_isdisk(vp, NULL) && 793 ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) { 794 /* 795 * When running in very secure mode, do not allow 796 * opens for writing of any disks. 797 * XXX: should be in geom_dev.c, but we lack the cred there. 798 */ 799 error = securelevel_ge(td->td_ucred, 2); 800 if (error) 801 return (error); 802 } 803 804 dsw = dev_refthread(dev); 805 if (dsw == NULL) 806 return (ENXIO); 807 808 /* XXX: Special casing of ttys for deadfs. Probably redundant. */ 809 if (dsw->d_flags & D_TTY) 810 vp->v_vflag |= VV_ISTTY; 811 812 VOP_UNLOCK(vp, 0, td); 813 814 if(!(dsw->d_flags & D_NEEDGIANT)) { 815 DROP_GIANT(); 816 if (dsw->d_fdopen != NULL) 817 error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx); 818 else 819 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 820 PICKUP_GIANT(); 821 } else { 822 mtx_lock(&Giant); 823 if (dsw->d_fdopen != NULL) 824 error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx); 825 else 826 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 827 mtx_unlock(&Giant); 828 } 829 830 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 831 832 dev_relthread(dev); 833 834 if (error) 835 return (error); 836 837 #if 0 /* /dev/console */ 838 KASSERT(ap->a_fdidx >= 0, 839 ("Could not vnode bypass device on fd %d", ap->a_fdidx)); 840 #else 841 if(ap->a_fdidx < 0) 842 return (error); 843 #endif 844 /* 845 * This is a pretty disgustingly long chain, but I am not 846 * sure there is any better way. Passing the fdidx into 847 * VOP_OPEN() offers us more information than just passing 848 * the file *. 849 */ 850 fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx]; 851 KASSERT(fp->f_ops == &badfileops, 852 ("Could not vnode bypass device on fdops %p", fp->f_ops)); 853 fp->f_ops = &devfs_ops_f; 854 fp->f_data = dev; 855 return (error); 856 } 857 858 static int 859 devfs_pathconf(ap) 860 struct vop_pathconf_args /* { 861 struct vnode *a_vp; 862 int a_name; 863 int *a_retval; 864 } */ *ap; 865 { 866 867 switch (ap->a_name) { 868 case _PC_NAME_MAX: 869 *ap->a_retval = NAME_MAX; 870 return (0); 871 case _PC_PATH_MAX: 872 *ap->a_retval = PATH_MAX; 873 return (0); 874 case _PC_MAC_PRESENT: 875 #ifdef MAC 876 /* 877 * If MAC is enabled, devfs automatically supports 878 * trivial non-persistant label storage. 879 */ 880 *ap->a_retval = 1; 881 #else 882 *ap->a_retval = 0; 883 #endif 884 return (0); 885 default: 886 return (vop_stdpathconf(ap)); 887 } 888 /* NOTREACHED */ 889 } 890 891 /* ARGSUSED */ 892 static int 893 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 894 { 895 struct cdev *dev; 896 struct cdevsw *dsw; 897 int error; 898 899 error = devfs_fp_check(fp, &dev, &dsw); 900 if (error) 901 return (error); 902 if (dsw->d_flags & D_NEEDGIANT) 903 mtx_lock(&Giant); 904 error = dsw->d_poll(dev, events, td); 905 if (dsw->d_flags & D_NEEDGIANT) 906 mtx_unlock(&Giant); 907 dev_relthread(dev); 908 return(error); 909 } 910 911 /* 912 * Print out the contents of a special device vnode. 913 */ 914 static int 915 devfs_print(ap) 916 struct vop_print_args /* { 917 struct vnode *a_vp; 918 } */ *ap; 919 { 920 921 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev)); 922 return (0); 923 } 924 925 /* 926 * Vnode op for read 927 */ 928 /* ARGSUSED */ 929 static int 930 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 931 { 932 struct cdev *dev; 933 int ioflag, error, resid; 934 struct cdevsw *dsw; 935 936 error = devfs_fp_check(fp, &dev, &dsw); 937 if (error) 938 return (error); 939 resid = uio->uio_resid; 940 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT); 941 if (ioflag & O_DIRECT) 942 ioflag |= IO_DIRECT; 943 944 if ((flags & FOF_OFFSET) == 0) 945 uio->uio_offset = fp->f_offset; 946 947 if (dsw->d_flags & D_NEEDGIANT) 948 mtx_lock(&Giant); 949 error = dsw->d_read(dev, uio, ioflag); 950 if (dsw->d_flags & D_NEEDGIANT) 951 mtx_unlock(&Giant); 952 dev_relthread(dev); 953 if (uio->uio_resid != resid || (error == 0 && resid != 0)) 954 vfs_timestamp(&dev->si_atime); 955 956 if ((flags & FOF_OFFSET) == 0) 957 fp->f_offset = uio->uio_offset; 958 fp->f_nextoff = uio->uio_offset; 959 return (error); 960 } 961 962 static int 963 devfs_readdir(ap) 964 struct vop_readdir_args /* { 965 struct vnode *a_vp; 966 struct uio *a_uio; 967 struct ucred *a_cred; 968 int *a_eofflag; 969 int *a_ncookies; 970 u_long **a_cookies; 971 } */ *ap; 972 { 973 int error; 974 struct uio *uio; 975 struct dirent *dp; 976 struct devfs_dirent *dd; 977 struct devfs_dirent *de; 978 struct devfs_mount *dmp; 979 off_t off, oldoff; 980 int ncookies = 0; 981 u_long *cookiebuf, *cookiep; 982 struct dirent *dps, *dpe; 983 984 if (ap->a_vp->v_type != VDIR) 985 return (ENOTDIR); 986 987 uio = ap->a_uio; 988 if (uio->uio_offset < 0) 989 return (EINVAL); 990 991 dmp = VFSTODEVFS(ap->a_vp->v_mount); 992 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 993 devfs_populate(dmp); 994 error = 0; 995 de = ap->a_vp->v_data; 996 off = 0; 997 oldoff = uio->uio_offset; 998 TAILQ_FOREACH(dd, &de->de_dlist, de_list) { 999 if (dd->de_flags & DE_WHITEOUT) 1000 continue; 1001 if (dd->de_dirent->d_type == DT_DIR) 1002 de = dd->de_dir; 1003 else 1004 de = dd; 1005 dp = dd->de_dirent; 1006 if (dp->d_reclen > uio->uio_resid) 1007 break; 1008 dp->d_fileno = de->de_inode; 1009 if (off >= uio->uio_offset) { 1010 ncookies++; 1011 error = uiomove(dp, dp->d_reclen, uio); 1012 if (error) 1013 break; 1014 } 1015 off += dp->d_reclen; 1016 } 1017 if( !error && ap->a_ncookies != NULL && ap->a_cookies != NULL ) { 1018 MALLOC(cookiebuf, u_long *, ncookies * sizeof(u_long), 1019 M_TEMP, M_WAITOK); 1020 cookiep = cookiebuf; 1021 dps = (struct dirent *)((char *)uio->uio_iov->iov_base - 1022 (uio->uio_offset - oldoff)); 1023 dpe = (struct dirent *) uio->uio_iov->iov_base; 1024 for( dp = dps; 1025 dp < dpe; 1026 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) { 1027 oldoff += dp->d_reclen; 1028 *cookiep++ = (u_long) oldoff; 1029 } 1030 *ap->a_ncookies = ncookies; 1031 *ap->a_cookies = cookiebuf; 1032 } 1033 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1034 uio->uio_offset = off; 1035 return (error); 1036 } 1037 1038 static int 1039 devfs_readlink(ap) 1040 struct vop_readlink_args /* { 1041 struct vnode *a_vp; 1042 struct uio *a_uio; 1043 struct ucred *a_cead; 1044 } */ *ap; 1045 { 1046 int error; 1047 struct devfs_dirent *de; 1048 1049 de = ap->a_vp->v_data; 1050 error = uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio); 1051 return (error); 1052 } 1053 1054 static int 1055 devfs_reclaim(ap) 1056 struct vop_reclaim_args /* { 1057 struct vnode *a_vp; 1058 } */ *ap; 1059 { 1060 struct vnode *vp = ap->a_vp; 1061 struct devfs_dirent *de; 1062 struct cdev *dev; 1063 1064 de = vp->v_data; 1065 if (de != NULL) 1066 de->de_vnode = NULL; 1067 vp->v_data = NULL; 1068 vnode_destroy_vobject(vp); 1069 1070 dev = vp->v_rdev; 1071 vp->v_rdev = NULL; 1072 1073 if (dev == NULL) 1074 return (0); 1075 1076 dev_lock(); 1077 if (de != NULL) 1078 LIST_REMOVE(de, de_alias); 1079 dev->si_usecount -= vp->v_usecount; 1080 dev_unlock(); 1081 dev_rel(dev); 1082 return (0); 1083 } 1084 1085 static int 1086 devfs_remove(ap) 1087 struct vop_remove_args /* { 1088 struct vnode *a_dvp; 1089 struct vnode *a_vp; 1090 struct componentname *a_cnp; 1091 } */ *ap; 1092 { 1093 struct vnode *vp = ap->a_vp; 1094 struct devfs_dirent *dd; 1095 struct devfs_dirent *de; 1096 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount); 1097 1098 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread); 1099 dd = ap->a_dvp->v_data; 1100 de = vp->v_data; 1101 if (de->de_dirent->d_type == DT_LNK) { 1102 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 1103 if (de->de_vnode) 1104 de->de_vnode->v_data = NULL; 1105 #ifdef MAC 1106 mac_destroy_devfsdirent(de); 1107 #endif 1108 FREE(de, M_DEVFS); 1109 } else { 1110 de->de_flags |= DE_WHITEOUT; 1111 } 1112 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1113 return (0); 1114 } 1115 1116 /* 1117 * Revoke is called on a tty when a terminal session ends. The vnode 1118 * is orphaned by setting v_op to deadfs so we need to let go of it 1119 * as well so that we create a new one next time around. 1120 */ 1121 static int 1122 devfs_revoke(ap) 1123 struct vop_revoke_args /* { 1124 struct vnode *a_vp; 1125 int a_flags; 1126 } */ *ap; 1127 { 1128 struct vnode *vp = ap->a_vp; 1129 struct cdev *dev; 1130 struct devfs_dirent *de; 1131 1132 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL")); 1133 1134 dev = vp->v_rdev; 1135 for (;;) { 1136 dev_lock(); 1137 de = LIST_FIRST(&dev->si_alist); 1138 dev_unlock(); 1139 if (de == NULL) 1140 break; 1141 vgone(de->de_vnode); 1142 } 1143 return (0); 1144 } 1145 1146 static int 1147 devfs_rioctl(ap) 1148 struct vop_ioctl_args /* { 1149 struct vnode *a_vp; 1150 u_long a_command; 1151 caddr_t a_data; 1152 int a_fflag; 1153 struct ucred *a_cred; 1154 struct thread *a_td; 1155 } */ *ap; 1156 { 1157 int error; 1158 struct devfs_mount *dmp; 1159 1160 dmp = VFSTODEVFS(ap->a_vp->v_mount); 1161 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 1162 devfs_populate(dmp); 1163 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1164 error = devfs_rules_ioctl(ap->a_vp->v_mount, ap->a_command, ap->a_data, 1165 ap->a_td); 1166 return (error); 1167 } 1168 1169 static int 1170 devfs_rread(ap) 1171 struct vop_read_args /* { 1172 struct vnode *a_vp; 1173 struct uio *a_uio; 1174 int a_ioflag; 1175 struct ucred *a_cred; 1176 } */ *ap; 1177 { 1178 1179 if (ap->a_vp->v_type != VDIR) 1180 return (EINVAL); 1181 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL)); 1182 } 1183 1184 static int 1185 devfs_setattr(ap) 1186 struct vop_setattr_args /* { 1187 struct vnode *a_vp; 1188 struct vattr *a_vap; 1189 struct ucred *a_cred; 1190 struct proc *a_p; 1191 } */ *ap; 1192 { 1193 struct devfs_dirent *de; 1194 struct vattr *vap; 1195 struct vnode *vp; 1196 int c, error; 1197 uid_t uid; 1198 gid_t gid; 1199 1200 vap = ap->a_vap; 1201 vp = ap->a_vp; 1202 if ((vap->va_type != VNON) || 1203 (vap->va_nlink != VNOVAL) || 1204 (vap->va_fsid != VNOVAL) || 1205 (vap->va_fileid != VNOVAL) || 1206 (vap->va_blocksize != VNOVAL) || 1207 (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1208 (vap->va_rdev != VNOVAL) || 1209 ((int)vap->va_bytes != VNOVAL) || 1210 (vap->va_gen != VNOVAL)) { 1211 return (EINVAL); 1212 } 1213 1214 de = vp->v_data; 1215 if (vp->v_type == VDIR) 1216 de = de->de_dir; 1217 1218 error = c = 0; 1219 if (vap->va_uid == (uid_t)VNOVAL) 1220 uid = de->de_uid; 1221 else 1222 uid = vap->va_uid; 1223 if (vap->va_gid == (gid_t)VNOVAL) 1224 gid = de->de_gid; 1225 else 1226 gid = vap->va_gid; 1227 if (uid != de->de_uid || gid != de->de_gid) { 1228 if (((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid || 1229 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) && 1230 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)) != 0) 1231 return (error); 1232 de->de_uid = uid; 1233 de->de_gid = gid; 1234 c = 1; 1235 } 1236 1237 if (vap->va_mode != (mode_t)VNOVAL) { 1238 if ((ap->a_cred->cr_uid != de->de_uid) && 1239 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL))) 1240 return (error); 1241 de->de_mode = vap->va_mode; 1242 c = 1; 1243 } 1244 1245 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1246 /* See the comment in ufs_vnops::ufs_setattr(). */ 1247 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) && 1248 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 1249 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td)))) 1250 return (error); 1251 if (vap->va_atime.tv_sec != VNOVAL) { 1252 if (vp->v_type == VCHR) 1253 vp->v_rdev->si_atime = vap->va_atime; 1254 else 1255 de->de_atime = vap->va_atime; 1256 } 1257 if (vap->va_mtime.tv_sec != VNOVAL) { 1258 if (vp->v_type == VCHR) 1259 vp->v_rdev->si_mtime = vap->va_mtime; 1260 else 1261 de->de_mtime = vap->va_mtime; 1262 } 1263 c = 1; 1264 } 1265 1266 if (c) { 1267 if (vp->v_type == VCHR) 1268 vfs_timestamp(&vp->v_rdev->si_ctime); 1269 else 1270 vfs_timestamp(&de->de_mtime); 1271 } 1272 return (0); 1273 } 1274 1275 #ifdef MAC 1276 static int 1277 devfs_setlabel(ap) 1278 struct vop_setlabel_args /* { 1279 struct vnode *a_vp; 1280 struct mac *a_label; 1281 struct ucred *a_cred; 1282 struct thread *a_td; 1283 } */ *ap; 1284 { 1285 struct vnode *vp; 1286 struct devfs_dirent *de; 1287 1288 vp = ap->a_vp; 1289 de = vp->v_data; 1290 1291 mac_relabel_vnode(ap->a_cred, vp, ap->a_label); 1292 mac_update_devfsdirent(vp->v_mount, de, vp); 1293 1294 return (0); 1295 } 1296 #endif 1297 1298 static int 1299 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) 1300 { 1301 1302 return (vnops.fo_stat(fp, sb, cred, td)); 1303 } 1304 1305 static int 1306 devfs_symlink(ap) 1307 struct vop_symlink_args /* { 1308 struct vnode *a_dvp; 1309 struct vnode **a_vpp; 1310 struct componentname *a_cnp; 1311 struct vattr *a_vap; 1312 char *a_target; 1313 } */ *ap; 1314 { 1315 int i, error; 1316 struct devfs_dirent *dd; 1317 struct devfs_dirent *de; 1318 struct devfs_mount *dmp; 1319 struct thread *td; 1320 1321 td = ap->a_cnp->cn_thread; 1322 KASSERT(td == curthread, ("devfs_symlink: td != curthread")); 1323 error = suser(td); 1324 if (error) 1325 return(error); 1326 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1327 dd = ap->a_dvp->v_data; 1328 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen); 1329 de->de_uid = 0; 1330 de->de_gid = 0; 1331 de->de_mode = 0755; 1332 de->de_inode = dmp->dm_inode++; 1333 de->de_dirent->d_type = DT_LNK; 1334 i = strlen(ap->a_target) + 1; 1335 MALLOC(de->de_symlink, char *, i, M_DEVFS, M_WAITOK); 1336 bcopy(ap->a_target, de->de_symlink, i); 1337 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, td); 1338 #ifdef MAC 1339 mac_create_devfs_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de); 1340 #endif 1341 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 1342 devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, td); 1343 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, td); 1344 return (0); 1345 } 1346 1347 /* 1348 * Vnode op for write 1349 */ 1350 /* ARGSUSED */ 1351 static int 1352 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 1353 { 1354 struct cdev *dev; 1355 struct vnode *vp; 1356 int error, ioflag, resid; 1357 struct cdevsw *dsw; 1358 1359 error = devfs_fp_check(fp, &dev, &dsw); 1360 if (error) 1361 return (error); 1362 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td)); 1363 vp = fp->f_vnode; 1364 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC); 1365 if (ioflag & O_DIRECT) 1366 ioflag |= IO_DIRECT; 1367 if ((flags & FOF_OFFSET) == 0) 1368 uio->uio_offset = fp->f_offset; 1369 1370 resid = uio->uio_resid; 1371 1372 if (dsw->d_flags & D_NEEDGIANT) 1373 mtx_lock(&Giant); 1374 error = dsw->d_write(dev, uio, ioflag); 1375 if (dsw->d_flags & D_NEEDGIANT) 1376 mtx_unlock(&Giant); 1377 dev_relthread(dev); 1378 if (uio->uio_resid != resid || (error == 0 && resid != 0)) { 1379 vfs_timestamp(&dev->si_ctime); 1380 dev->si_mtime = dev->si_ctime; 1381 } 1382 1383 if ((flags & FOF_OFFSET) == 0) 1384 fp->f_offset = uio->uio_offset; 1385 fp->f_nextoff = uio->uio_offset; 1386 return (error); 1387 } 1388 1389 static struct vop_vector devfs_vnodeops = { 1390 .vop_default = &default_vnodeops, 1391 1392 .vop_access = devfs_access, 1393 .vop_getattr = devfs_getattr, 1394 .vop_ioctl = devfs_rioctl, 1395 .vop_lookup = devfs_lookup, 1396 .vop_mknod = devfs_mknod, 1397 .vop_pathconf = devfs_pathconf, 1398 .vop_read = devfs_rread, 1399 .vop_readdir = devfs_readdir, 1400 .vop_readlink = devfs_readlink, 1401 .vop_reclaim = devfs_reclaim, 1402 .vop_remove = devfs_remove, 1403 .vop_revoke = devfs_revoke, 1404 .vop_setattr = devfs_setattr, 1405 #ifdef MAC 1406 .vop_setlabel = devfs_setlabel, 1407 #endif 1408 .vop_symlink = devfs_symlink, 1409 }; 1410 1411 static struct vop_vector devfs_specops = { 1412 .vop_default = &default_vnodeops, 1413 1414 .vop_access = devfs_access, 1415 .vop_advlock = devfs_advlock, 1416 .vop_bmap = VOP_PANIC, 1417 .vop_close = devfs_close, 1418 .vop_create = VOP_PANIC, 1419 .vop_fsync = devfs_fsync, 1420 .vop_getattr = devfs_getattr, 1421 .vop_lease = VOP_NULL, 1422 .vop_link = VOP_PANIC, 1423 .vop_mkdir = VOP_PANIC, 1424 .vop_mknod = VOP_PANIC, 1425 .vop_open = devfs_open, 1426 .vop_pathconf = devfs_pathconf, 1427 .vop_print = devfs_print, 1428 .vop_read = VOP_PANIC, 1429 .vop_readdir = VOP_PANIC, 1430 .vop_readlink = VOP_PANIC, 1431 .vop_reallocblks = VOP_PANIC, 1432 .vop_reclaim = devfs_reclaim, 1433 .vop_remove = devfs_remove, 1434 .vop_rename = VOP_PANIC, 1435 .vop_revoke = devfs_revoke, 1436 .vop_rmdir = VOP_PANIC, 1437 .vop_setattr = devfs_setattr, 1438 #ifdef MAC 1439 .vop_setlabel = devfs_setlabel, 1440 #endif 1441 .vop_strategy = VOP_PANIC, 1442 .vop_symlink = VOP_PANIC, 1443 .vop_write = VOP_PANIC, 1444 }; 1445 1446 dev_t 1447 dev2udev(struct cdev *x) 1448 { 1449 if (x == NULL) 1450 return (NODEV); 1451 return (x->si_inode ^ devfs_random()); 1452 } 1453 1454 /* 1455 * Helper sysctl for devname(3). We're given a struct cdev * and return 1456 * the name, if any, registered by the device driver. 1457 */ 1458 static int 1459 sysctl_devname(SYSCTL_HANDLER_ARGS) 1460 { 1461 int error; 1462 dev_t ud; 1463 struct cdev *dev, **dp; 1464 1465 error = SYSCTL_IN(req, &ud, sizeof (ud)); 1466 if (error) 1467 return (error); 1468 if (ud == NODEV) 1469 return(EINVAL); 1470 dp = devfs_itod(ud ^ devfs_random()); 1471 if (dp == NULL) 1472 return(ENOENT); 1473 dev = *dp; 1474 if (dev == NULL) 1475 return(ENOENT); 1476 return(SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1)); 1477 return (error); 1478 } 1479 1480 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 1481 NULL, 0, sysctl_devname, "", "devname(3) handler"); 1482 1483 /* 1484 * Our calling convention to the device drivers used to be that we passed 1485 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 1486 * flags instead since that's what open(), close() and ioctl() takes and 1487 * we don't really want vnode.h in device drivers. 1488 * We solved the source compatibility by redefining some vnode flags to 1489 * be the same as the fcntl ones and by sending down the bitwise OR of 1490 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody 1491 * pulls the rug out under this. 1492 */ 1493 CTASSERT(O_NONBLOCK == IO_NDELAY); 1494 CTASSERT(O_FSYNC == IO_SYNC); 1495