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 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td); 373 dev_relthread(dev); 374 return (error); 375 } 376 377 static int 378 devfs_close_f(struct file *fp, struct thread *td) 379 { 380 381 return (vnops.fo_close(fp, td)); 382 } 383 384 /* 385 * Synch buffers associated with a block device 386 */ 387 /* ARGSUSED */ 388 static int 389 devfs_fsync(ap) 390 struct vop_fsync_args /* { 391 struct vnode *a_vp; 392 struct ucred *a_cred; 393 int a_waitfor; 394 struct thread *a_td; 395 } */ *ap; 396 { 397 if (!vn_isdisk(ap->a_vp, NULL)) 398 return (0); 399 400 return (vop_stdfsync(ap)); 401 } 402 403 static int 404 devfs_getattr(ap) 405 struct vop_getattr_args /* { 406 struct vnode *a_vp; 407 struct vattr *a_vap; 408 struct ucred *a_cred; 409 struct thread *a_td; 410 } */ *ap; 411 { 412 struct vnode *vp = ap->a_vp; 413 struct vattr *vap = ap->a_vap; 414 int error = 0; 415 struct devfs_dirent *de; 416 struct cdev *dev; 417 418 de = vp->v_data; 419 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp)); 420 if (vp->v_type == VDIR) { 421 de = de->de_dir; 422 KASSERT(de != NULL, 423 ("Null dir dirent in devfs_getattr vp=%p", vp)); 424 } 425 bzero((caddr_t) vap, sizeof(*vap)); 426 vattr_null(vap); 427 vap->va_uid = de->de_uid; 428 vap->va_gid = de->de_gid; 429 vap->va_mode = de->de_mode; 430 if (vp->v_type == VLNK) 431 vap->va_size = strlen(de->de_symlink); 432 else if (vp->v_type == VDIR) 433 vap->va_size = vap->va_bytes = DEV_BSIZE; 434 else 435 vap->va_size = 0; 436 if (vp->v_type != VDIR) 437 vap->va_bytes = 0; 438 vap->va_blocksize = DEV_BSIZE; 439 vap->va_type = vp->v_type; 440 441 #define fix(aa) \ 442 do { \ 443 if ((aa).tv_sec == 0) { \ 444 (aa).tv_sec = boottime.tv_sec; \ 445 (aa).tv_nsec = boottime.tv_usec * 1000; \ 446 } \ 447 } while (0) 448 449 if (vp->v_type != VCHR) { 450 fix(de->de_atime); 451 vap->va_atime = de->de_atime; 452 fix(de->de_mtime); 453 vap->va_mtime = de->de_mtime; 454 fix(de->de_ctime); 455 vap->va_ctime = de->de_ctime; 456 } else { 457 dev = vp->v_rdev; 458 fix(dev->si_atime); 459 vap->va_atime = dev->si_atime; 460 fix(dev->si_mtime); 461 vap->va_mtime = dev->si_mtime; 462 fix(dev->si_ctime); 463 vap->va_ctime = dev->si_ctime; 464 465 vap->va_rdev = dev->si_inode ^ devfs_random(); 466 } 467 vap->va_gen = 0; 468 vap->va_flags = 0; 469 vap->va_nlink = de->de_links; 470 vap->va_fileid = de->de_inode; 471 472 return (error); 473 } 474 475 /* 476 * Device ioctl operation. 477 */ 478 /* ARGSUSED */ 479 static int 480 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) 481 { 482 struct cdev *dev; 483 struct cdevsw *dsw; 484 struct vnode *vp; 485 struct vnode *vpold; 486 int error, i; 487 const char *p; 488 struct fiodgname_arg *fgn; 489 490 error = devfs_fp_check(fp, &dev, &dsw); 491 if (error) 492 return (error); 493 494 if (com == FIODTYPE) { 495 *(int *)data = dsw->d_flags & D_TYPEMASK; 496 dev_relthread(dev); 497 return (0); 498 } else if (com == FIODGNAME) { 499 fgn = data; 500 p = devtoname(dev); 501 i = strlen(p) + 1; 502 if (i > fgn->len) 503 return (EINVAL); 504 return (copyout(p, fgn->buf, i)); 505 } 506 if (dsw->d_flags & D_NEEDGIANT) 507 mtx_lock(&Giant); 508 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td); 509 if (dsw->d_flags & D_NEEDGIANT) 510 mtx_unlock(&Giant); 511 dev_relthread(dev); 512 if (error == ENOIOCTL) 513 error = ENOTTY; 514 if (error == 0 && com == TIOCSCTTY) { 515 vp = fp->f_vnode; 516 517 /* Do nothing if reassigning same control tty */ 518 sx_slock(&proctree_lock); 519 if (td->td_proc->p_session->s_ttyvp == vp) { 520 sx_sunlock(&proctree_lock); 521 return (0); 522 } 523 524 mtx_lock(&Giant); 525 526 vpold = td->td_proc->p_session->s_ttyvp; 527 VREF(vp); 528 SESS_LOCK(td->td_proc->p_session); 529 td->td_proc->p_session->s_ttyvp = vp; 530 SESS_UNLOCK(td->td_proc->p_session); 531 532 sx_sunlock(&proctree_lock); 533 534 /* Get rid of reference to old control tty */ 535 if (vpold) 536 vrele(vpold); 537 mtx_unlock(&Giant); 538 } 539 return (error); 540 } 541 542 543 /* ARGSUSED */ 544 static int 545 devfs_kqfilter_f(struct file *fp, struct knote *kn) 546 { 547 struct cdev *dev; 548 struct cdevsw *dsw; 549 int error; 550 551 error = devfs_fp_check(fp, &dev, &dsw); 552 if (error) 553 return (error); 554 if (dsw->d_flags & D_NEEDGIANT) 555 mtx_lock(&Giant); 556 error = dsw->d_kqfilter(dev, kn); 557 if (dsw->d_flags & D_NEEDGIANT) 558 mtx_unlock(&Giant); 559 dev_relthread(dev); 560 return (error); 561 } 562 563 static int 564 devfs_lookupx(ap) 565 struct vop_lookup_args /* { 566 struct vnode * a_dvp; 567 struct vnode ** a_vpp; 568 struct componentname * a_cnp; 569 } */ *ap; 570 { 571 struct componentname *cnp; 572 struct vnode *dvp, **vpp; 573 struct thread *td; 574 struct devfs_dirent *de, *dd; 575 struct devfs_dirent **dde; 576 struct devfs_mount *dmp; 577 struct cdev *cdev; 578 int error, flags, nameiop; 579 char specname[SPECNAMELEN + 1], *pname; 580 581 cnp = ap->a_cnp; 582 vpp = ap->a_vpp; 583 dvp = ap->a_dvp; 584 pname = cnp->cn_nameptr; 585 td = cnp->cn_thread; 586 flags = cnp->cn_flags; 587 nameiop = cnp->cn_nameiop; 588 dmp = VFSTODEVFS(dvp->v_mount); 589 dd = dvp->v_data; 590 *vpp = NULLVP; 591 592 if ((flags & ISLASTCN) && nameiop == RENAME) 593 return (EOPNOTSUPP); 594 595 if (dvp->v_type != VDIR) 596 return (ENOTDIR); 597 598 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) 599 return (EIO); 600 601 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 602 if (error) 603 return (error); 604 605 if (cnp->cn_namelen == 1 && *pname == '.') { 606 if ((flags & ISLASTCN) && nameiop != LOOKUP) 607 return (EINVAL); 608 *vpp = dvp; 609 VREF(dvp); 610 return (0); 611 } 612 613 if (flags & ISDOTDOT) { 614 if ((flags & ISLASTCN) && nameiop != LOOKUP) 615 return (EINVAL); 616 VOP_UNLOCK(dvp, 0, td); 617 de = TAILQ_FIRST(&dd->de_dlist); /* "." */ 618 de = TAILQ_NEXT(de, de_list); /* ".." */ 619 de = de->de_dir; 620 error = devfs_allocv(de, dvp->v_mount, vpp, td); 621 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 622 return (error); 623 } 624 625 devfs_populate(dmp); 626 dd = dvp->v_data; 627 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 628 if (cnp->cn_namelen != de->de_dirent->d_namlen) 629 continue; 630 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 631 de->de_dirent->d_namlen) != 0) 632 continue; 633 if (de->de_flags & DE_WHITEOUT) 634 goto notfound; 635 goto found; 636 } 637 638 if (nameiop == DELETE) 639 goto notfound; 640 641 /* 642 * OK, we didn't have an entry for the name we were asked for 643 * so we try to see if anybody can create it on demand. 644 */ 645 pname = devfs_fqpn(specname, dvp, cnp); 646 if (pname == NULL) 647 goto notfound; 648 649 cdev = NULL; 650 EVENTHANDLER_INVOKE(dev_clone, pname, strlen(pname), &cdev); 651 if (cdev == NULL) 652 goto notfound; 653 654 devfs_populate(dmp); 655 656 dde = devfs_itode(dmp, cdev->si_inode); 657 dev_rel(cdev); 658 659 if (dde == NULL || *dde == NULL || *dde == DE_DELETED) 660 goto notfound; 661 662 if ((*dde)->de_flags & DE_WHITEOUT) 663 goto notfound; 664 665 de = *dde; 666 goto found; 667 668 notfound: 669 670 if ((nameiop == CREATE || nameiop == RENAME) && 671 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) { 672 cnp->cn_flags |= SAVENAME; 673 return (EJUSTRETURN); 674 } 675 return (ENOENT); 676 677 678 found: 679 680 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) { 681 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 682 if (error) 683 return (error); 684 if (*vpp == dvp) { 685 VREF(dvp); 686 *vpp = dvp; 687 return (0); 688 } 689 error = devfs_allocv(de, dvp->v_mount, vpp, td); 690 if (error) 691 return (error); 692 return (0); 693 } 694 error = devfs_allocv(de, dvp->v_mount, vpp, td); 695 return (error); 696 } 697 698 static int 699 devfs_lookup(struct vop_lookup_args *ap) 700 { 701 int j; 702 struct devfs_mount *dmp; 703 704 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 705 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 706 j = devfs_lookupx(ap); 707 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 708 return (j); 709 } 710 711 static int 712 devfs_mknod(struct vop_mknod_args *ap) 713 /* 714 struct vop_mknod_args { 715 struct vnodeop_desc *a_desc; 716 struct vnode *a_dvp; 717 struct vnode **a_vpp; 718 struct componentname *a_cnp; 719 struct vattr *a_vap; 720 }; */ 721 { 722 struct componentname *cnp; 723 struct vnode *dvp, **vpp; 724 struct thread *td; 725 struct devfs_dirent *dd, *de; 726 struct devfs_mount *dmp; 727 int error; 728 729 dvp = ap->a_dvp; 730 dmp = VFSTODEVFS(dvp->v_mount); 731 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread); 732 733 cnp = ap->a_cnp; 734 vpp = ap->a_vpp; 735 td = cnp->cn_thread; 736 dd = dvp->v_data; 737 738 error = ENOENT; 739 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 740 if (cnp->cn_namelen != de->de_dirent->d_namlen) 741 continue; 742 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 743 de->de_dirent->d_namlen) != 0) 744 continue; 745 if (de->de_flags & DE_WHITEOUT) 746 break; 747 goto notfound; 748 } 749 if (de == NULL) 750 goto notfound; 751 de->de_flags &= ~DE_WHITEOUT; 752 error = devfs_allocv(de, dvp->v_mount, vpp, td); 753 notfound: 754 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 755 return (error); 756 } 757 758 /* 759 * Open a special file. 760 */ 761 /* ARGSUSED */ 762 static int 763 devfs_open(ap) 764 struct vop_open_args /* { 765 struct vnode *a_vp; 766 int a_mode; 767 struct ucred *a_cred; 768 struct thread *a_td; 769 int a_fdidx; 770 } */ *ap; 771 { 772 struct thread *td = ap->a_td; 773 struct vnode *vp = ap->a_vp; 774 struct cdev *dev = vp->v_rdev; 775 struct file *fp; 776 int error; 777 struct cdevsw *dsw; 778 779 if (vp->v_type == VBLK) 780 return (ENXIO); 781 782 if (dev == NULL) 783 return (ENXIO); 784 785 /* Make this field valid before any I/O in d_open. */ 786 if (dev->si_iosize_max == 0) 787 dev->si_iosize_max = DFLTPHYS; 788 789 if (vn_isdisk(vp, NULL) && 790 ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) { 791 /* 792 * When running in very secure mode, do not allow 793 * opens for writing of any disks. 794 * XXX: should be in geom_dev.c, but we lack the cred there. 795 */ 796 error = securelevel_ge(td->td_ucred, 2); 797 if (error) 798 return (error); 799 } 800 801 dsw = dev_refthread(dev); 802 if (dsw == NULL) 803 return (ENXIO); 804 805 /* XXX: Special casing of ttys for deadfs. Probably redundant. */ 806 if (dsw->d_flags & D_TTY) 807 vp->v_vflag |= VV_ISTTY; 808 809 VOP_UNLOCK(vp, 0, td); 810 811 if(!(dsw->d_flags & D_NEEDGIANT)) { 812 DROP_GIANT(); 813 if (dsw->d_fdopen != NULL) 814 error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx); 815 else 816 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 817 PICKUP_GIANT(); 818 } else if (dsw->d_fdopen != NULL) 819 error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx); 820 else 821 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 822 823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 824 825 dev_relthread(dev); 826 827 if (error) 828 return (error); 829 830 #if 0 /* /dev/console */ 831 KASSERT(ap->a_fdidx >= 0, 832 ("Could not vnode bypass device on fd %d", ap->a_fdidx)); 833 #else 834 if(ap->a_fdidx < 0) 835 return (error); 836 #endif 837 /* 838 * This is a pretty disgustingly long chain, but I am not 839 * sure there is any better way. Passing the fdidx into 840 * VOP_OPEN() offers us more information than just passing 841 * the file *. 842 */ 843 fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx]; 844 KASSERT(fp->f_ops == &badfileops, 845 ("Could not vnode bypass device on fdops %p", fp->f_ops)); 846 fp->f_ops = &devfs_ops_f; 847 fp->f_data = dev; 848 return (error); 849 } 850 851 static int 852 devfs_pathconf(ap) 853 struct vop_pathconf_args /* { 854 struct vnode *a_vp; 855 int a_name; 856 int *a_retval; 857 } */ *ap; 858 { 859 860 switch (ap->a_name) { 861 case _PC_NAME_MAX: 862 *ap->a_retval = NAME_MAX; 863 return (0); 864 case _PC_PATH_MAX: 865 *ap->a_retval = PATH_MAX; 866 return (0); 867 case _PC_MAC_PRESENT: 868 #ifdef MAC 869 /* 870 * If MAC is enabled, devfs automatically supports 871 * trivial non-persistant label storage. 872 */ 873 *ap->a_retval = 1; 874 #else 875 *ap->a_retval = 0; 876 #endif 877 return (0); 878 default: 879 return (vop_stdpathconf(ap)); 880 } 881 /* NOTREACHED */ 882 } 883 884 /* ARGSUSED */ 885 static int 886 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 887 { 888 struct cdev *dev; 889 struct cdevsw *dsw; 890 int error; 891 892 error = devfs_fp_check(fp, &dev, &dsw); 893 if (error) 894 return (error); 895 if (dsw->d_flags & D_NEEDGIANT) 896 mtx_lock(&Giant); 897 error = dsw->d_poll(dev, events, td); 898 if (dsw->d_flags & D_NEEDGIANT) 899 mtx_unlock(&Giant); 900 dev_relthread(dev); 901 return(error); 902 } 903 904 /* 905 * Print out the contents of a special device vnode. 906 */ 907 static int 908 devfs_print(ap) 909 struct vop_print_args /* { 910 struct vnode *a_vp; 911 } */ *ap; 912 { 913 914 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev)); 915 return (0); 916 } 917 918 /* 919 * Vnode op for read 920 */ 921 /* ARGSUSED */ 922 static int 923 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 924 { 925 struct cdev *dev; 926 int ioflag, error, resid; 927 struct cdevsw *dsw; 928 929 error = devfs_fp_check(fp, &dev, &dsw); 930 if (error) 931 return (error); 932 resid = uio->uio_resid; 933 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT); 934 if (ioflag & O_DIRECT) 935 ioflag |= IO_DIRECT; 936 937 if ((flags & FOF_OFFSET) == 0) 938 uio->uio_offset = fp->f_offset; 939 940 if (dsw->d_flags & D_NEEDGIANT) 941 mtx_lock(&Giant); 942 error = dsw->d_read(dev, uio, ioflag); 943 if (dsw->d_flags & D_NEEDGIANT) 944 mtx_unlock(&Giant); 945 dev_relthread(dev); 946 if (uio->uio_resid != resid || (error == 0 && resid != 0)) 947 vfs_timestamp(&dev->si_atime); 948 949 if ((flags & FOF_OFFSET) == 0) 950 fp->f_offset = uio->uio_offset; 951 fp->f_nextoff = uio->uio_offset; 952 return (error); 953 } 954 955 static int 956 devfs_readdir(ap) 957 struct vop_readdir_args /* { 958 struct vnode *a_vp; 959 struct uio *a_uio; 960 struct ucred *a_cred; 961 int *a_eofflag; 962 int *a_ncookies; 963 u_long **a_cookies; 964 } */ *ap; 965 { 966 int error; 967 struct uio *uio; 968 struct dirent *dp; 969 struct devfs_dirent *dd; 970 struct devfs_dirent *de; 971 struct devfs_mount *dmp; 972 off_t off, oldoff; 973 int ncookies = 0; 974 u_long *cookiebuf, *cookiep; 975 struct dirent *dps, *dpe; 976 977 if (ap->a_vp->v_type != VDIR) 978 return (ENOTDIR); 979 980 uio = ap->a_uio; 981 if (uio->uio_offset < 0) 982 return (EINVAL); 983 984 dmp = VFSTODEVFS(ap->a_vp->v_mount); 985 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 986 devfs_populate(dmp); 987 error = 0; 988 de = ap->a_vp->v_data; 989 off = 0; 990 oldoff = uio->uio_offset; 991 TAILQ_FOREACH(dd, &de->de_dlist, de_list) { 992 if (dd->de_flags & DE_WHITEOUT) 993 continue; 994 if (dd->de_dirent->d_type == DT_DIR) 995 de = dd->de_dir; 996 else 997 de = dd; 998 dp = dd->de_dirent; 999 if (dp->d_reclen > uio->uio_resid) 1000 break; 1001 dp->d_fileno = de->de_inode; 1002 if (off >= uio->uio_offset) { 1003 ncookies++; 1004 error = uiomove(dp, dp->d_reclen, uio); 1005 if (error) 1006 break; 1007 } 1008 off += dp->d_reclen; 1009 } 1010 if( !error && ap->a_ncookies != NULL && ap->a_cookies != NULL ) { 1011 MALLOC(cookiebuf, u_long *, ncookies * sizeof(u_long), 1012 M_TEMP, M_WAITOK); 1013 cookiep = cookiebuf; 1014 dps = (struct dirent *)((char *)uio->uio_iov->iov_base - 1015 (uio->uio_offset - oldoff)); 1016 dpe = (struct dirent *) uio->uio_iov->iov_base; 1017 for( dp = dps; 1018 dp < dpe; 1019 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) { 1020 oldoff += dp->d_reclen; 1021 *cookiep++ = (u_long) oldoff; 1022 } 1023 *ap->a_ncookies = ncookies; 1024 *ap->a_cookies = cookiebuf; 1025 } 1026 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1027 uio->uio_offset = off; 1028 return (error); 1029 } 1030 1031 static int 1032 devfs_readlink(ap) 1033 struct vop_readlink_args /* { 1034 struct vnode *a_vp; 1035 struct uio *a_uio; 1036 struct ucred *a_cead; 1037 } */ *ap; 1038 { 1039 int error; 1040 struct devfs_dirent *de; 1041 1042 de = ap->a_vp->v_data; 1043 error = uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio); 1044 return (error); 1045 } 1046 1047 static int 1048 devfs_reclaim(ap) 1049 struct vop_reclaim_args /* { 1050 struct vnode *a_vp; 1051 } */ *ap; 1052 { 1053 struct vnode *vp = ap->a_vp; 1054 struct devfs_dirent *de; 1055 struct cdev *dev; 1056 1057 de = vp->v_data; 1058 if (de != NULL) 1059 de->de_vnode = NULL; 1060 vp->v_data = NULL; 1061 vnode_destroy_vobject(vp); 1062 1063 dev = vp->v_rdev; 1064 vp->v_rdev = NULL; 1065 1066 if (dev == NULL) 1067 return (0); 1068 1069 dev_lock(); 1070 if (de != NULL) 1071 LIST_REMOVE(de, de_alias); 1072 dev->si_usecount -= vp->v_usecount; 1073 dev_unlock(); 1074 dev_rel(dev); 1075 return (0); 1076 } 1077 1078 static int 1079 devfs_remove(ap) 1080 struct vop_remove_args /* { 1081 struct vnode *a_dvp; 1082 struct vnode *a_vp; 1083 struct componentname *a_cnp; 1084 } */ *ap; 1085 { 1086 struct vnode *vp = ap->a_vp; 1087 struct devfs_dirent *dd; 1088 struct devfs_dirent *de; 1089 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount); 1090 1091 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread); 1092 dd = ap->a_dvp->v_data; 1093 de = vp->v_data; 1094 if (de->de_dirent->d_type == DT_LNK) { 1095 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 1096 if (de->de_vnode) 1097 de->de_vnode->v_data = NULL; 1098 #ifdef MAC 1099 mac_destroy_devfsdirent(de); 1100 #endif 1101 FREE(de, M_DEVFS); 1102 } else { 1103 de->de_flags |= DE_WHITEOUT; 1104 } 1105 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1106 return (0); 1107 } 1108 1109 /* 1110 * Revoke is called on a tty when a terminal session ends. The vnode 1111 * is orphaned by setting v_op to deadfs so we need to let go of it 1112 * as well so that we create a new one next time around. 1113 */ 1114 static int 1115 devfs_revoke(ap) 1116 struct vop_revoke_args /* { 1117 struct vnode *a_vp; 1118 int a_flags; 1119 } */ *ap; 1120 { 1121 struct vnode *vp = ap->a_vp; 1122 struct cdev *dev; 1123 struct devfs_dirent *de; 1124 1125 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL")); 1126 1127 dev = vp->v_rdev; 1128 for (;;) { 1129 dev_lock(); 1130 de = LIST_FIRST(&dev->si_alist); 1131 dev_unlock(); 1132 if (de == NULL) 1133 break; 1134 vgone(de->de_vnode); 1135 } 1136 return (0); 1137 } 1138 1139 static int 1140 devfs_rioctl(ap) 1141 struct vop_ioctl_args /* { 1142 struct vnode *a_vp; 1143 u_long a_command; 1144 caddr_t a_data; 1145 int a_fflag; 1146 struct ucred *a_cred; 1147 struct thread *a_td; 1148 } */ *ap; 1149 { 1150 int error; 1151 struct devfs_mount *dmp; 1152 1153 dmp = VFSTODEVFS(ap->a_vp->v_mount); 1154 lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread); 1155 devfs_populate(dmp); 1156 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread); 1157 error = devfs_rules_ioctl(ap->a_vp->v_mount, ap->a_command, ap->a_data, 1158 ap->a_td); 1159 return (error); 1160 } 1161 1162 static int 1163 devfs_rread(ap) 1164 struct vop_read_args /* { 1165 struct vnode *a_vp; 1166 struct uio *a_uio; 1167 int a_ioflag; 1168 struct ucred *a_cred; 1169 } */ *ap; 1170 { 1171 1172 if (ap->a_vp->v_type != VDIR) 1173 return (EINVAL); 1174 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL)); 1175 } 1176 1177 static int 1178 devfs_setattr(ap) 1179 struct vop_setattr_args /* { 1180 struct vnode *a_vp; 1181 struct vattr *a_vap; 1182 struct ucred *a_cred; 1183 struct proc *a_p; 1184 } */ *ap; 1185 { 1186 struct devfs_dirent *de; 1187 struct vattr *vap; 1188 struct vnode *vp; 1189 int c, error; 1190 uid_t uid; 1191 gid_t gid; 1192 1193 vap = ap->a_vap; 1194 vp = ap->a_vp; 1195 if ((vap->va_type != VNON) || 1196 (vap->va_nlink != VNOVAL) || 1197 (vap->va_fsid != VNOVAL) || 1198 (vap->va_fileid != VNOVAL) || 1199 (vap->va_blocksize != VNOVAL) || 1200 (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1201 (vap->va_rdev != VNOVAL) || 1202 ((int)vap->va_bytes != VNOVAL) || 1203 (vap->va_gen != VNOVAL)) { 1204 return (EINVAL); 1205 } 1206 1207 de = vp->v_data; 1208 if (vp->v_type == VDIR) 1209 de = de->de_dir; 1210 1211 error = c = 0; 1212 if (vap->va_uid == (uid_t)VNOVAL) 1213 uid = de->de_uid; 1214 else 1215 uid = vap->va_uid; 1216 if (vap->va_gid == (gid_t)VNOVAL) 1217 gid = de->de_gid; 1218 else 1219 gid = vap->va_gid; 1220 if (uid != de->de_uid || gid != de->de_gid) { 1221 if (((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid || 1222 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) && 1223 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)) != 0) 1224 return (error); 1225 de->de_uid = uid; 1226 de->de_gid = gid; 1227 c = 1; 1228 } 1229 1230 if (vap->va_mode != (mode_t)VNOVAL) { 1231 if ((ap->a_cred->cr_uid != de->de_uid) && 1232 (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL))) 1233 return (error); 1234 de->de_mode = vap->va_mode; 1235 c = 1; 1236 } 1237 1238 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1239 /* See the comment in ufs_vnops::ufs_setattr(). */ 1240 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) && 1241 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 1242 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td)))) 1243 return (error); 1244 if (vap->va_atime.tv_sec != VNOVAL) { 1245 if (vp->v_type == VCHR) 1246 vp->v_rdev->si_atime = vap->va_atime; 1247 else 1248 de->de_atime = vap->va_atime; 1249 } 1250 if (vap->va_mtime.tv_sec != VNOVAL) { 1251 if (vp->v_type == VCHR) 1252 vp->v_rdev->si_mtime = vap->va_mtime; 1253 else 1254 de->de_mtime = vap->va_mtime; 1255 } 1256 c = 1; 1257 } 1258 1259 if (c) { 1260 if (vp->v_type == VCHR) 1261 vfs_timestamp(&vp->v_rdev->si_ctime); 1262 else 1263 vfs_timestamp(&de->de_mtime); 1264 } 1265 return (0); 1266 } 1267 1268 #ifdef MAC 1269 static int 1270 devfs_setlabel(ap) 1271 struct vop_setlabel_args /* { 1272 struct vnode *a_vp; 1273 struct mac *a_label; 1274 struct ucred *a_cred; 1275 struct thread *a_td; 1276 } */ *ap; 1277 { 1278 struct vnode *vp; 1279 struct devfs_dirent *de; 1280 1281 vp = ap->a_vp; 1282 de = vp->v_data; 1283 1284 mac_relabel_vnode(ap->a_cred, vp, ap->a_label); 1285 mac_update_devfsdirent(vp->v_mount, de, vp); 1286 1287 return (0); 1288 } 1289 #endif 1290 1291 static int 1292 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) 1293 { 1294 1295 return (vnops.fo_stat(fp, sb, cred, td)); 1296 } 1297 1298 static int 1299 devfs_symlink(ap) 1300 struct vop_symlink_args /* { 1301 struct vnode *a_dvp; 1302 struct vnode **a_vpp; 1303 struct componentname *a_cnp; 1304 struct vattr *a_vap; 1305 char *a_target; 1306 } */ *ap; 1307 { 1308 int i, error; 1309 struct devfs_dirent *dd; 1310 struct devfs_dirent *de; 1311 struct devfs_mount *dmp; 1312 struct thread *td; 1313 1314 td = ap->a_cnp->cn_thread; 1315 KASSERT(td == curthread, ("devfs_symlink: td != curthread")); 1316 error = suser(td); 1317 if (error) 1318 return(error); 1319 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1320 dd = ap->a_dvp->v_data; 1321 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen); 1322 de->de_uid = 0; 1323 de->de_gid = 0; 1324 de->de_mode = 0755; 1325 de->de_inode = dmp->dm_inode++; 1326 de->de_dirent->d_type = DT_LNK; 1327 i = strlen(ap->a_target) + 1; 1328 MALLOC(de->de_symlink, char *, i, M_DEVFS, M_WAITOK); 1329 bcopy(ap->a_target, de->de_symlink, i); 1330 lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, td); 1331 #ifdef MAC 1332 mac_create_devfs_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de); 1333 #endif 1334 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 1335 devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, td); 1336 lockmgr(&dmp->dm_lock, LK_RELEASE, 0, td); 1337 return (0); 1338 } 1339 1340 /* 1341 * Vnode op for write 1342 */ 1343 /* ARGSUSED */ 1344 static int 1345 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 1346 { 1347 struct cdev *dev; 1348 struct vnode *vp; 1349 int error, ioflag, resid; 1350 struct cdevsw *dsw; 1351 1352 error = devfs_fp_check(fp, &dev, &dsw); 1353 if (error) 1354 return (error); 1355 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td)); 1356 vp = fp->f_vnode; 1357 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC); 1358 if (ioflag & O_DIRECT) 1359 ioflag |= IO_DIRECT; 1360 if ((flags & FOF_OFFSET) == 0) 1361 uio->uio_offset = fp->f_offset; 1362 1363 resid = uio->uio_resid; 1364 1365 if (dsw->d_flags & D_NEEDGIANT) 1366 mtx_lock(&Giant); 1367 error = dsw->d_write(dev, uio, ioflag); 1368 if (dsw->d_flags & D_NEEDGIANT) 1369 mtx_unlock(&Giant); 1370 dev_relthread(dev); 1371 if (uio->uio_resid != resid || (error == 0 && resid != 0)) { 1372 vfs_timestamp(&dev->si_ctime); 1373 dev->si_mtime = dev->si_ctime; 1374 } 1375 1376 if ((flags & FOF_OFFSET) == 0) 1377 fp->f_offset = uio->uio_offset; 1378 fp->f_nextoff = uio->uio_offset; 1379 return (error); 1380 } 1381 1382 static struct vop_vector devfs_vnodeops = { 1383 .vop_default = &default_vnodeops, 1384 1385 .vop_access = devfs_access, 1386 .vop_getattr = devfs_getattr, 1387 .vop_ioctl = devfs_rioctl, 1388 .vop_lookup = devfs_lookup, 1389 .vop_mknod = devfs_mknod, 1390 .vop_pathconf = devfs_pathconf, 1391 .vop_read = devfs_rread, 1392 .vop_readdir = devfs_readdir, 1393 .vop_readlink = devfs_readlink, 1394 .vop_reclaim = devfs_reclaim, 1395 .vop_remove = devfs_remove, 1396 .vop_revoke = devfs_revoke, 1397 .vop_setattr = devfs_setattr, 1398 #ifdef MAC 1399 .vop_setlabel = devfs_setlabel, 1400 #endif 1401 .vop_symlink = devfs_symlink, 1402 }; 1403 1404 static struct vop_vector devfs_specops = { 1405 .vop_default = &default_vnodeops, 1406 1407 .vop_access = devfs_access, 1408 .vop_advlock = devfs_advlock, 1409 .vop_bmap = VOP_PANIC, 1410 .vop_close = devfs_close, 1411 .vop_create = VOP_PANIC, 1412 .vop_fsync = devfs_fsync, 1413 .vop_getattr = devfs_getattr, 1414 .vop_lease = VOP_NULL, 1415 .vop_link = VOP_PANIC, 1416 .vop_mkdir = VOP_PANIC, 1417 .vop_mknod = VOP_PANIC, 1418 .vop_open = devfs_open, 1419 .vop_pathconf = devfs_pathconf, 1420 .vop_print = devfs_print, 1421 .vop_read = VOP_PANIC, 1422 .vop_readdir = VOP_PANIC, 1423 .vop_readlink = VOP_PANIC, 1424 .vop_reallocblks = VOP_PANIC, 1425 .vop_reclaim = devfs_reclaim, 1426 .vop_remove = devfs_remove, 1427 .vop_rename = VOP_PANIC, 1428 .vop_revoke = devfs_revoke, 1429 .vop_rmdir = VOP_PANIC, 1430 .vop_setattr = devfs_setattr, 1431 #ifdef MAC 1432 .vop_setlabel = devfs_setlabel, 1433 #endif 1434 .vop_strategy = VOP_PANIC, 1435 .vop_symlink = VOP_PANIC, 1436 .vop_write = VOP_PANIC, 1437 }; 1438 1439 dev_t 1440 dev2udev(struct cdev *x) 1441 { 1442 if (x == NULL) 1443 return (NODEV); 1444 return (x->si_inode ^ devfs_random()); 1445 } 1446 1447 /* 1448 * Helper sysctl for devname(3). We're given a struct cdev * and return 1449 * the name, if any, registered by the device driver. 1450 */ 1451 static int 1452 sysctl_devname(SYSCTL_HANDLER_ARGS) 1453 { 1454 int error; 1455 dev_t ud; 1456 struct cdev *dev, **dp; 1457 1458 error = SYSCTL_IN(req, &ud, sizeof (ud)); 1459 if (error) 1460 return (error); 1461 if (ud == NODEV) 1462 return(EINVAL); 1463 dp = devfs_itod(ud ^ devfs_random()); 1464 if (dp == NULL) 1465 return(ENOENT); 1466 dev = *dp; 1467 if (dev == NULL) 1468 return(ENOENT); 1469 return(SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1)); 1470 return (error); 1471 } 1472 1473 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 1474 NULL, 0, sysctl_devname, "", "devname(3) handler"); 1475 1476 /* 1477 * Our calling convention to the device drivers used to be that we passed 1478 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 1479 * flags instead since that's what open(), close() and ioctl() takes and 1480 * we don't really want vnode.h in device drivers. 1481 * We solved the source compatibility by redefining some vnode flags to 1482 * be the same as the fcntl ones and by sending down the bitwise OR of 1483 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody 1484 * pulls the rug out under this. 1485 */ 1486 CTASSERT(O_NONBLOCK == IO_NDELAY); 1487 CTASSERT(O_FSYNC == IO_SYNC); 1488