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 * mkdir: want it ? 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/dirent.h> 46 #include <sys/fcntl.h> 47 #include <sys/file.h> 48 #include <sys/filedesc.h> 49 #include <sys/filio.h> 50 #include <sys/jail.h> 51 #include <sys/kernel.h> 52 #include <sys/lock.h> 53 #include <sys/malloc.h> 54 #include <sys/mount.h> 55 #include <sys/namei.h> 56 #include <sys/priv.h> 57 #include <sys/proc.h> 58 #include <sys/stat.h> 59 #include <sys/sx.h> 60 #include <sys/time.h> 61 #include <sys/ttycom.h> 62 #include <sys/unistd.h> 63 #include <sys/vnode.h> 64 65 static struct vop_vector devfs_vnodeops; 66 static struct vop_vector devfs_specops; 67 static struct fileops devfs_ops_f; 68 69 #include <fs/devfs/devfs.h> 70 #include <fs/devfs/devfs_int.h> 71 72 #include <security/mac/mac_framework.h> 73 74 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data"); 75 76 struct mtx devfs_de_interlock; 77 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF); 78 struct sx clone_drain_lock; 79 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock"); 80 struct mtx cdevpriv_mtx; 81 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF); 82 83 static int 84 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp, 85 int *ref) 86 { 87 88 *dswp = devvn_refthread(fp->f_vnode, devp, ref); 89 if (*devp != fp->f_data) { 90 if (*dswp != NULL) 91 dev_relthread(*devp, *ref); 92 return (ENXIO); 93 } 94 KASSERT((*devp)->si_refcount > 0, 95 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp))); 96 if (*dswp == NULL) 97 return (ENXIO); 98 curthread->td_fpop = fp; 99 return (0); 100 } 101 102 int 103 devfs_get_cdevpriv(void **datap) 104 { 105 struct file *fp; 106 struct cdev_privdata *p; 107 int error; 108 109 fp = curthread->td_fpop; 110 if (fp == NULL) 111 return (EBADF); 112 p = fp->f_cdevpriv; 113 if (p != NULL) { 114 error = 0; 115 *datap = p->cdpd_data; 116 } else 117 error = ENOENT; 118 return (error); 119 } 120 121 int 122 devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t priv_dtr) 123 { 124 struct file *fp; 125 struct cdev_priv *cdp; 126 struct cdev_privdata *p; 127 int error; 128 129 fp = curthread->td_fpop; 130 if (fp == NULL) 131 return (ENOENT); 132 cdp = cdev2priv((struct cdev *)fp->f_data); 133 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK); 134 p->cdpd_data = priv; 135 p->cdpd_dtr = priv_dtr; 136 p->cdpd_fp = fp; 137 mtx_lock(&cdevpriv_mtx); 138 if (fp->f_cdevpriv == NULL) { 139 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list); 140 fp->f_cdevpriv = p; 141 mtx_unlock(&cdevpriv_mtx); 142 error = 0; 143 } else { 144 mtx_unlock(&cdevpriv_mtx); 145 free(p, M_CDEVPDATA); 146 error = EBUSY; 147 } 148 return (error); 149 } 150 151 void 152 devfs_destroy_cdevpriv(struct cdev_privdata *p) 153 { 154 155 mtx_assert(&cdevpriv_mtx, MA_OWNED); 156 p->cdpd_fp->f_cdevpriv = NULL; 157 LIST_REMOVE(p, cdpd_list); 158 mtx_unlock(&cdevpriv_mtx); 159 (p->cdpd_dtr)(p->cdpd_data); 160 free(p, M_CDEVPDATA); 161 } 162 163 void 164 devfs_fpdrop(struct file *fp) 165 { 166 struct cdev_privdata *p; 167 168 mtx_lock(&cdevpriv_mtx); 169 if ((p = fp->f_cdevpriv) == NULL) { 170 mtx_unlock(&cdevpriv_mtx); 171 return; 172 } 173 devfs_destroy_cdevpriv(p); 174 } 175 176 void 177 devfs_clear_cdevpriv(void) 178 { 179 struct file *fp; 180 181 fp = curthread->td_fpop; 182 if (fp == NULL) 183 return; 184 devfs_fpdrop(fp); 185 } 186 187 /* 188 * On success devfs_populate_vp() returns with dmp->dm_lock held. 189 */ 190 static int 191 devfs_populate_vp(struct vnode *vp) 192 { 193 struct devfs_dirent *de; 194 struct devfs_mount *dmp; 195 int locked; 196 197 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp"); 198 199 dmp = VFSTODEVFS(vp->v_mount); 200 locked = VOP_ISLOCKED(vp); 201 202 sx_xlock(&dmp->dm_lock); 203 DEVFS_DMP_HOLD(dmp); 204 205 /* Can't call devfs_populate() with the vnode lock held. */ 206 VOP_UNLOCK(vp, 0); 207 devfs_populate(dmp); 208 209 sx_xunlock(&dmp->dm_lock); 210 vn_lock(vp, locked | LK_RETRY); 211 sx_xlock(&dmp->dm_lock); 212 if (DEVFS_DMP_DROP(dmp)) { 213 sx_xunlock(&dmp->dm_lock); 214 devfs_unmount_final(dmp); 215 return (EBADF); 216 } 217 if ((vp->v_iflag & VI_DOOMED) != 0) { 218 sx_xunlock(&dmp->dm_lock); 219 return (EBADF); 220 } 221 de = vp->v_data; 222 KASSERT(de != NULL, 223 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed")); 224 if ((de->de_flags & DE_DOOMED) != 0) { 225 sx_xunlock(&dmp->dm_lock); 226 return (EBADF); 227 } 228 229 return (0); 230 } 231 232 static int 233 devfs_vptocnp(struct vop_vptocnp_args *ap) 234 { 235 struct vnode *vp = ap->a_vp; 236 struct vnode **dvp = ap->a_vpp; 237 struct devfs_mount *dmp; 238 char *buf = ap->a_buf; 239 int *buflen = ap->a_buflen; 240 struct devfs_dirent *dd, *de; 241 int i, error; 242 243 dmp = VFSTODEVFS(vp->v_mount); 244 245 error = devfs_populate_vp(vp); 246 if (error != 0) 247 return (error); 248 249 i = *buflen; 250 dd = vp->v_data; 251 252 if (vp->v_type == VCHR) { 253 i -= strlen(dd->de_cdp->cdp_c.si_name); 254 if (i < 0) { 255 error = ENOMEM; 256 goto finished; 257 } 258 bcopy(dd->de_cdp->cdp_c.si_name, buf + i, 259 strlen(dd->de_cdp->cdp_c.si_name)); 260 de = dd->de_dir; 261 } else if (vp->v_type == VDIR) { 262 if (dd == dmp->dm_rootdir) { 263 *dvp = vp; 264 vref(*dvp); 265 goto finished; 266 } 267 i -= dd->de_dirent->d_namlen; 268 if (i < 0) { 269 error = ENOMEM; 270 goto finished; 271 } 272 bcopy(dd->de_dirent->d_name, buf + i, 273 dd->de_dirent->d_namlen); 274 de = dd; 275 } else { 276 error = ENOENT; 277 goto finished; 278 } 279 *buflen = i; 280 de = devfs_parent_dirent(de); 281 if (de == NULL) { 282 error = ENOENT; 283 goto finished; 284 } 285 mtx_lock(&devfs_de_interlock); 286 *dvp = de->de_vnode; 287 if (*dvp != NULL) { 288 VI_LOCK(*dvp); 289 mtx_unlock(&devfs_de_interlock); 290 vholdl(*dvp); 291 VI_UNLOCK(*dvp); 292 vref(*dvp); 293 vdrop(*dvp); 294 } else { 295 mtx_unlock(&devfs_de_interlock); 296 error = ENOENT; 297 } 298 finished: 299 sx_xunlock(&dmp->dm_lock); 300 return (error); 301 } 302 303 /* 304 * Construct the fully qualified path name relative to the mountpoint. 305 * If a NULL cnp is provided, no '/' is appended to the resulting path. 306 */ 307 char * 308 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd, 309 struct componentname *cnp) 310 { 311 int i; 312 struct devfs_dirent *de; 313 314 sx_assert(&dmp->dm_lock, SA_LOCKED); 315 316 i = SPECNAMELEN; 317 buf[i] = '\0'; 318 if (cnp != NULL) 319 i -= cnp->cn_namelen; 320 if (i < 0) 321 return (NULL); 322 if (cnp != NULL) 323 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen); 324 de = dd; 325 while (de != dmp->dm_rootdir) { 326 if (cnp != NULL || i < SPECNAMELEN) { 327 i--; 328 if (i < 0) 329 return (NULL); 330 buf[i] = '/'; 331 } 332 i -= de->de_dirent->d_namlen; 333 if (i < 0) 334 return (NULL); 335 bcopy(de->de_dirent->d_name, buf + i, 336 de->de_dirent->d_namlen); 337 de = devfs_parent_dirent(de); 338 if (de == NULL) 339 return (NULL); 340 } 341 return (buf + i); 342 } 343 344 static int 345 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp, 346 struct devfs_dirent *de) 347 { 348 int not_found; 349 350 not_found = 0; 351 if (de->de_flags & DE_DOOMED) 352 not_found = 1; 353 if (DEVFS_DE_DROP(de)) { 354 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed")); 355 devfs_dirent_free(de); 356 } 357 if (DEVFS_DMP_DROP(dmp)) { 358 KASSERT(not_found == 1, 359 ("DEVFS mount struct freed before dirent")); 360 not_found = 2; 361 sx_xunlock(&dmp->dm_lock); 362 devfs_unmount_final(dmp); 363 } 364 if (not_found == 1 || (drop_dm_lock && not_found != 2)) 365 sx_unlock(&dmp->dm_lock); 366 return (not_found); 367 } 368 369 static void 370 devfs_insmntque_dtr(struct vnode *vp, void *arg) 371 { 372 struct devfs_dirent *de; 373 374 de = (struct devfs_dirent *)arg; 375 mtx_lock(&devfs_de_interlock); 376 vp->v_data = NULL; 377 de->de_vnode = NULL; 378 mtx_unlock(&devfs_de_interlock); 379 vgone(vp); 380 vput(vp); 381 } 382 383 /* 384 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops 385 * it on return. 386 */ 387 int 388 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode, 389 struct vnode **vpp) 390 { 391 int error; 392 struct vnode *vp; 393 struct cdev *dev; 394 struct devfs_mount *dmp; 395 struct cdevsw *dsw; 396 397 dmp = VFSTODEVFS(mp); 398 if (de->de_flags & DE_DOOMED) { 399 sx_xunlock(&dmp->dm_lock); 400 return (ENOENT); 401 } 402 loop: 403 DEVFS_DE_HOLD(de); 404 DEVFS_DMP_HOLD(dmp); 405 mtx_lock(&devfs_de_interlock); 406 vp = de->de_vnode; 407 if (vp != NULL) { 408 VI_LOCK(vp); 409 mtx_unlock(&devfs_de_interlock); 410 sx_xunlock(&dmp->dm_lock); 411 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread); 412 sx_xlock(&dmp->dm_lock); 413 if (devfs_allocv_drop_refs(0, dmp, de)) { 414 vput(vp); 415 return (ENOENT); 416 } 417 else if ((vp->v_iflag & VI_DOOMED) != 0) { 418 mtx_lock(&devfs_de_interlock); 419 if (de->de_vnode == vp) { 420 de->de_vnode = NULL; 421 vp->v_data = NULL; 422 } 423 mtx_unlock(&devfs_de_interlock); 424 vput(vp); 425 goto loop; 426 } 427 sx_xunlock(&dmp->dm_lock); 428 *vpp = vp; 429 return (0); 430 } 431 mtx_unlock(&devfs_de_interlock); 432 if (de->de_dirent->d_type == DT_CHR) { 433 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) { 434 devfs_allocv_drop_refs(1, dmp, de); 435 return (ENOENT); 436 } 437 dev = &de->de_cdp->cdp_c; 438 } else { 439 dev = NULL; 440 } 441 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp); 442 if (error != 0) { 443 devfs_allocv_drop_refs(1, dmp, de); 444 printf("devfs_allocv: failed to allocate new vnode\n"); 445 return (error); 446 } 447 448 if (de->de_dirent->d_type == DT_CHR) { 449 vp->v_type = VCHR; 450 VI_LOCK(vp); 451 dev_lock(); 452 dev_refl(dev); 453 /* XXX: v_rdev should be protect by vnode lock */ 454 vp->v_rdev = dev; 455 KASSERT(vp->v_usecount == 1, 456 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount)); 457 dev->si_usecount += vp->v_usecount; 458 /* Special casing of ttys for deadfs. Probably redundant. */ 459 dsw = dev->si_devsw; 460 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0) 461 vp->v_vflag |= VV_ISTTY; 462 dev_unlock(); 463 VI_UNLOCK(vp); 464 if ((dev->si_flags & SI_ETERNAL) != 0) 465 vp->v_vflag |= VV_ETERNALDEV; 466 vp->v_op = &devfs_specops; 467 } else if (de->de_dirent->d_type == DT_DIR) { 468 vp->v_type = VDIR; 469 } else if (de->de_dirent->d_type == DT_LNK) { 470 vp->v_type = VLNK; 471 } else { 472 vp->v_type = VBAD; 473 } 474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS); 475 VN_LOCK_ASHARE(vp); 476 mtx_lock(&devfs_de_interlock); 477 vp->v_data = de; 478 de->de_vnode = vp; 479 mtx_unlock(&devfs_de_interlock); 480 error = insmntque1(vp, mp, devfs_insmntque_dtr, de); 481 if (error != 0) { 482 (void) devfs_allocv_drop_refs(1, dmp, de); 483 return (error); 484 } 485 if (devfs_allocv_drop_refs(0, dmp, de)) { 486 vput(vp); 487 return (ENOENT); 488 } 489 #ifdef MAC 490 mac_devfs_vnode_associate(mp, de, vp); 491 #endif 492 sx_xunlock(&dmp->dm_lock); 493 *vpp = vp; 494 return (0); 495 } 496 497 static int 498 devfs_access(struct vop_access_args *ap) 499 { 500 struct vnode *vp = ap->a_vp; 501 struct devfs_dirent *de; 502 struct proc *p; 503 int error; 504 505 de = vp->v_data; 506 if (vp->v_type == VDIR) 507 de = de->de_dir; 508 509 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid, 510 ap->a_accmode, ap->a_cred, NULL); 511 if (error == 0) 512 return (0); 513 if (error != EACCES) 514 return (error); 515 p = ap->a_td->td_proc; 516 /* We do, however, allow access to the controlling terminal */ 517 PROC_LOCK(p); 518 if (!(p->p_flag & P_CONTROLT)) { 519 PROC_UNLOCK(p); 520 return (error); 521 } 522 if (p->p_session->s_ttydp == de->de_cdp) 523 error = 0; 524 PROC_UNLOCK(p); 525 return (error); 526 } 527 528 /* ARGSUSED */ 529 static int 530 devfs_close(struct vop_close_args *ap) 531 { 532 struct vnode *vp = ap->a_vp, *oldvp; 533 struct thread *td = ap->a_td; 534 struct proc *p; 535 struct cdev *dev = vp->v_rdev; 536 struct cdevsw *dsw; 537 int vp_locked, error, ref; 538 539 /* 540 * XXX: Don't call d_close() if we were called because of 541 * XXX: insmntque1() failure. 542 */ 543 if (vp->v_data == NULL) 544 return (0); 545 546 /* 547 * Hack: a tty device that is a controlling terminal 548 * has a reference from the session structure. 549 * We cannot easily tell that a character device is 550 * a controlling terminal, unless it is the closing 551 * process' controlling terminal. In that case, 552 * if the reference count is 2 (this last descriptor 553 * plus the session), release the reference from the session. 554 */ 555 if (td != NULL) { 556 p = td->td_proc; 557 PROC_LOCK(p); 558 if (vp == p->p_session->s_ttyvp) { 559 PROC_UNLOCK(p); 560 oldvp = NULL; 561 sx_xlock(&proctree_lock); 562 if (vp == p->p_session->s_ttyvp) { 563 SESS_LOCK(p->p_session); 564 VI_LOCK(vp); 565 if (count_dev(dev) == 2 && 566 (vp->v_iflag & VI_DOOMED) == 0) { 567 p->p_session->s_ttyvp = NULL; 568 p->p_session->s_ttydp = NULL; 569 oldvp = vp; 570 } 571 VI_UNLOCK(vp); 572 SESS_UNLOCK(p->p_session); 573 } 574 sx_xunlock(&proctree_lock); 575 if (oldvp != NULL) 576 vrele(oldvp); 577 } else 578 PROC_UNLOCK(p); 579 } 580 /* 581 * We do not want to really close the device if it 582 * is still in use unless we are trying to close it 583 * forcibly. Since every use (buffer, vnode, swap, cmap) 584 * holds a reference to the vnode, and because we mark 585 * any other vnodes that alias this device, when the 586 * sum of the reference counts on all the aliased 587 * vnodes descends to one, we are on last close. 588 */ 589 dsw = dev_refthread(dev, &ref); 590 if (dsw == NULL) 591 return (ENXIO); 592 VI_LOCK(vp); 593 if (vp->v_iflag & VI_DOOMED) { 594 /* Forced close. */ 595 } else if (dsw->d_flags & D_TRACKCLOSE) { 596 /* Keep device updated on status. */ 597 } else if (count_dev(dev) > 1) { 598 VI_UNLOCK(vp); 599 dev_relthread(dev, ref); 600 return (0); 601 } 602 vholdl(vp); 603 VI_UNLOCK(vp); 604 vp_locked = VOP_ISLOCKED(vp); 605 VOP_UNLOCK(vp, 0); 606 KASSERT(dev->si_refcount > 0, 607 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev))); 608 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td); 609 dev_relthread(dev, ref); 610 vn_lock(vp, vp_locked | LK_RETRY); 611 vdrop(vp); 612 return (error); 613 } 614 615 static int 616 devfs_close_f(struct file *fp, struct thread *td) 617 { 618 int error; 619 struct file *fpop; 620 621 /* 622 * NB: td may be NULL if this descriptor is closed due to 623 * garbage collection from a closed UNIX domain socket. 624 */ 625 fpop = curthread->td_fpop; 626 curthread->td_fpop = fp; 627 error = vnops.fo_close(fp, td); 628 curthread->td_fpop = fpop; 629 630 /* 631 * The f_cdevpriv cannot be assigned non-NULL value while we 632 * are destroying the file. 633 */ 634 if (fp->f_cdevpriv != NULL) 635 devfs_fpdrop(fp); 636 return (error); 637 } 638 639 static int 640 devfs_fsync(struct vop_fsync_args *ap) 641 { 642 int error; 643 struct bufobj *bo; 644 struct devfs_dirent *de; 645 646 if (!vn_isdisk(ap->a_vp, &error)) { 647 bo = &ap->a_vp->v_bufobj; 648 de = ap->a_vp->v_data; 649 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) { 650 printf("Device %s went missing before all of the data " 651 "could be written to it; expect data loss.\n", 652 de->de_dirent->d_name); 653 654 error = vop_stdfsync(ap); 655 if (bo->bo_dirty.bv_cnt != 0 || error != 0) 656 panic("devfs_fsync: vop_stdfsync failed."); 657 } 658 659 return (0); 660 } 661 662 return (vop_stdfsync(ap)); 663 } 664 665 static int 666 devfs_getattr(struct vop_getattr_args *ap) 667 { 668 struct vnode *vp = ap->a_vp; 669 struct vattr *vap = ap->a_vap; 670 int error; 671 struct devfs_dirent *de; 672 struct devfs_mount *dmp; 673 struct cdev *dev; 674 675 error = devfs_populate_vp(vp); 676 if (error != 0) 677 return (error); 678 679 dmp = VFSTODEVFS(vp->v_mount); 680 sx_xunlock(&dmp->dm_lock); 681 682 de = vp->v_data; 683 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp)); 684 if (vp->v_type == VDIR) { 685 de = de->de_dir; 686 KASSERT(de != NULL, 687 ("Null dir dirent in devfs_getattr vp=%p", vp)); 688 } 689 vap->va_uid = de->de_uid; 690 vap->va_gid = de->de_gid; 691 vap->va_mode = de->de_mode; 692 if (vp->v_type == VLNK) 693 vap->va_size = strlen(de->de_symlink); 694 else if (vp->v_type == VDIR) 695 vap->va_size = vap->va_bytes = DEV_BSIZE; 696 else 697 vap->va_size = 0; 698 if (vp->v_type != VDIR) 699 vap->va_bytes = 0; 700 vap->va_blocksize = DEV_BSIZE; 701 vap->va_type = vp->v_type; 702 703 #define fix(aa) \ 704 do { \ 705 if ((aa).tv_sec <= 3600) { \ 706 (aa).tv_sec = boottime.tv_sec; \ 707 (aa).tv_nsec = boottime.tv_usec * 1000; \ 708 } \ 709 } while (0) 710 711 if (vp->v_type != VCHR) { 712 fix(de->de_atime); 713 vap->va_atime = de->de_atime; 714 fix(de->de_mtime); 715 vap->va_mtime = de->de_mtime; 716 fix(de->de_ctime); 717 vap->va_ctime = de->de_ctime; 718 } else { 719 dev = vp->v_rdev; 720 fix(dev->si_atime); 721 vap->va_atime = dev->si_atime; 722 fix(dev->si_mtime); 723 vap->va_mtime = dev->si_mtime; 724 fix(dev->si_ctime); 725 vap->va_ctime = dev->si_ctime; 726 727 vap->va_rdev = cdev2priv(dev)->cdp_inode; 728 } 729 vap->va_gen = 0; 730 vap->va_flags = 0; 731 vap->va_filerev = 0; 732 vap->va_nlink = de->de_links; 733 vap->va_fileid = de->de_inode; 734 735 return (error); 736 } 737 738 /* ARGSUSED */ 739 static int 740 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) 741 { 742 struct cdev *dev; 743 struct cdevsw *dsw; 744 struct vnode *vp; 745 struct vnode *vpold; 746 int error, i, ref; 747 const char *p; 748 struct fiodgname_arg *fgn; 749 struct file *fpop; 750 751 fpop = td->td_fpop; 752 error = devfs_fp_check(fp, &dev, &dsw, &ref); 753 if (error != 0) { 754 error = vnops.fo_ioctl(fp, com, data, cred, td); 755 return (error); 756 } 757 758 if (com == FIODTYPE) { 759 *(int *)data = dsw->d_flags & D_TYPEMASK; 760 td->td_fpop = fpop; 761 dev_relthread(dev, ref); 762 return (0); 763 } else if (com == FIODGNAME) { 764 fgn = data; 765 p = devtoname(dev); 766 i = strlen(p) + 1; 767 if (i > fgn->len) 768 error = EINVAL; 769 else 770 error = copyout(p, fgn->buf, i); 771 td->td_fpop = fpop; 772 dev_relthread(dev, ref); 773 return (error); 774 } 775 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td); 776 td->td_fpop = NULL; 777 dev_relthread(dev, ref); 778 if (error == ENOIOCTL) 779 error = ENOTTY; 780 if (error == 0 && com == TIOCSCTTY) { 781 vp = fp->f_vnode; 782 783 /* Do nothing if reassigning same control tty */ 784 sx_slock(&proctree_lock); 785 if (td->td_proc->p_session->s_ttyvp == vp) { 786 sx_sunlock(&proctree_lock); 787 return (0); 788 } 789 790 vpold = td->td_proc->p_session->s_ttyvp; 791 VREF(vp); 792 SESS_LOCK(td->td_proc->p_session); 793 td->td_proc->p_session->s_ttyvp = vp; 794 td->td_proc->p_session->s_ttydp = cdev2priv(dev); 795 SESS_UNLOCK(td->td_proc->p_session); 796 797 sx_sunlock(&proctree_lock); 798 799 /* Get rid of reference to old control tty */ 800 if (vpold) 801 vrele(vpold); 802 } 803 return (error); 804 } 805 806 /* ARGSUSED */ 807 static int 808 devfs_kqfilter_f(struct file *fp, struct knote *kn) 809 { 810 struct cdev *dev; 811 struct cdevsw *dsw; 812 int error, ref; 813 struct file *fpop; 814 struct thread *td; 815 816 td = curthread; 817 fpop = td->td_fpop; 818 error = devfs_fp_check(fp, &dev, &dsw, &ref); 819 if (error) 820 return (error); 821 error = dsw->d_kqfilter(dev, kn); 822 td->td_fpop = fpop; 823 dev_relthread(dev, ref); 824 return (error); 825 } 826 827 static inline int 828 devfs_prison_check(struct devfs_dirent *de, struct thread *td) 829 { 830 struct cdev_priv *cdp; 831 struct ucred *dcr; 832 struct proc *p; 833 int error; 834 835 cdp = de->de_cdp; 836 if (cdp == NULL) 837 return (0); 838 dcr = cdp->cdp_c.si_cred; 839 if (dcr == NULL) 840 return (0); 841 842 error = prison_check(td->td_ucred, dcr); 843 if (error == 0) 844 return (0); 845 /* We do, however, allow access to the controlling terminal */ 846 p = td->td_proc; 847 PROC_LOCK(p); 848 if (!(p->p_flag & P_CONTROLT)) { 849 PROC_UNLOCK(p); 850 return (error); 851 } 852 if (p->p_session->s_ttydp == cdp) 853 error = 0; 854 PROC_UNLOCK(p); 855 return (error); 856 } 857 858 static int 859 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock) 860 { 861 struct componentname *cnp; 862 struct vnode *dvp, **vpp; 863 struct thread *td; 864 struct devfs_dirent *de, *dd; 865 struct devfs_dirent **dde; 866 struct devfs_mount *dmp; 867 struct cdev *cdev; 868 int error, flags, nameiop, dvplocked; 869 char specname[SPECNAMELEN + 1], *pname; 870 871 cnp = ap->a_cnp; 872 vpp = ap->a_vpp; 873 dvp = ap->a_dvp; 874 pname = cnp->cn_nameptr; 875 td = cnp->cn_thread; 876 flags = cnp->cn_flags; 877 nameiop = cnp->cn_nameiop; 878 dmp = VFSTODEVFS(dvp->v_mount); 879 dd = dvp->v_data; 880 *vpp = NULLVP; 881 882 if ((flags & ISLASTCN) && nameiop == RENAME) 883 return (EOPNOTSUPP); 884 885 if (dvp->v_type != VDIR) 886 return (ENOTDIR); 887 888 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) 889 return (EIO); 890 891 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 892 if (error) 893 return (error); 894 895 if (cnp->cn_namelen == 1 && *pname == '.') { 896 if ((flags & ISLASTCN) && nameiop != LOOKUP) 897 return (EINVAL); 898 *vpp = dvp; 899 VREF(dvp); 900 return (0); 901 } 902 903 if (flags & ISDOTDOT) { 904 if ((flags & ISLASTCN) && nameiop != LOOKUP) 905 return (EINVAL); 906 de = devfs_parent_dirent(dd); 907 if (de == NULL) 908 return (ENOENT); 909 dvplocked = VOP_ISLOCKED(dvp); 910 VOP_UNLOCK(dvp, 0); 911 error = devfs_allocv(de, dvp->v_mount, 912 cnp->cn_lkflags & LK_TYPE_MASK, vpp); 913 *dm_unlock = 0; 914 vn_lock(dvp, dvplocked | LK_RETRY); 915 return (error); 916 } 917 918 dd = dvp->v_data; 919 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0); 920 while (de == NULL) { /* While(...) so we can use break */ 921 922 if (nameiop == DELETE) 923 return (ENOENT); 924 925 /* 926 * OK, we didn't have an entry for the name we were asked for 927 * so we try to see if anybody can create it on demand. 928 */ 929 pname = devfs_fqpn(specname, dmp, dd, cnp); 930 if (pname == NULL) 931 break; 932 933 cdev = NULL; 934 DEVFS_DMP_HOLD(dmp); 935 sx_xunlock(&dmp->dm_lock); 936 sx_slock(&clone_drain_lock); 937 EVENTHANDLER_INVOKE(dev_clone, 938 td->td_ucred, pname, strlen(pname), &cdev); 939 sx_sunlock(&clone_drain_lock); 940 941 if (cdev == NULL) 942 sx_xlock(&dmp->dm_lock); 943 else if (devfs_populate_vp(dvp) != 0) { 944 *dm_unlock = 0; 945 sx_xlock(&dmp->dm_lock); 946 if (DEVFS_DMP_DROP(dmp)) { 947 sx_xunlock(&dmp->dm_lock); 948 devfs_unmount_final(dmp); 949 } else 950 sx_xunlock(&dmp->dm_lock); 951 dev_rel(cdev); 952 return (ENOENT); 953 } 954 if (DEVFS_DMP_DROP(dmp)) { 955 *dm_unlock = 0; 956 sx_xunlock(&dmp->dm_lock); 957 devfs_unmount_final(dmp); 958 if (cdev != NULL) 959 dev_rel(cdev); 960 return (ENOENT); 961 } 962 963 if (cdev == NULL) 964 break; 965 966 dev_lock(); 967 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx]; 968 if (dde != NULL && *dde != NULL) 969 de = *dde; 970 dev_unlock(); 971 dev_rel(cdev); 972 break; 973 } 974 975 if (de == NULL || de->de_flags & DE_WHITEOUT) { 976 if ((nameiop == CREATE || nameiop == RENAME) && 977 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) { 978 cnp->cn_flags |= SAVENAME; 979 return (EJUSTRETURN); 980 } 981 return (ENOENT); 982 } 983 984 if (devfs_prison_check(de, td)) 985 return (ENOENT); 986 987 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) { 988 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 989 if (error) 990 return (error); 991 if (*vpp == dvp) { 992 VREF(dvp); 993 *vpp = dvp; 994 return (0); 995 } 996 } 997 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK, 998 vpp); 999 *dm_unlock = 0; 1000 return (error); 1001 } 1002 1003 static int 1004 devfs_lookup(struct vop_lookup_args *ap) 1005 { 1006 int j; 1007 struct devfs_mount *dmp; 1008 int dm_unlock; 1009 1010 if (devfs_populate_vp(ap->a_dvp) != 0) 1011 return (ENOTDIR); 1012 1013 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1014 dm_unlock = 1; 1015 j = devfs_lookupx(ap, &dm_unlock); 1016 if (dm_unlock == 1) 1017 sx_xunlock(&dmp->dm_lock); 1018 return (j); 1019 } 1020 1021 static int 1022 devfs_mknod(struct vop_mknod_args *ap) 1023 { 1024 struct componentname *cnp; 1025 struct vnode *dvp, **vpp; 1026 struct devfs_dirent *dd, *de; 1027 struct devfs_mount *dmp; 1028 int error; 1029 1030 /* 1031 * The only type of node we should be creating here is a 1032 * character device, for anything else return EOPNOTSUPP. 1033 */ 1034 if (ap->a_vap->va_type != VCHR) 1035 return (EOPNOTSUPP); 1036 dvp = ap->a_dvp; 1037 dmp = VFSTODEVFS(dvp->v_mount); 1038 1039 cnp = ap->a_cnp; 1040 vpp = ap->a_vpp; 1041 dd = dvp->v_data; 1042 1043 error = ENOENT; 1044 sx_xlock(&dmp->dm_lock); 1045 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 1046 if (cnp->cn_namelen != de->de_dirent->d_namlen) 1047 continue; 1048 if (de->de_dirent->d_type == DT_CHR && 1049 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0) 1050 continue; 1051 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 1052 de->de_dirent->d_namlen) != 0) 1053 continue; 1054 if (de->de_flags & DE_WHITEOUT) 1055 break; 1056 goto notfound; 1057 } 1058 if (de == NULL) 1059 goto notfound; 1060 de->de_flags &= ~DE_WHITEOUT; 1061 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp); 1062 return (error); 1063 notfound: 1064 sx_xunlock(&dmp->dm_lock); 1065 return (error); 1066 } 1067 1068 /* ARGSUSED */ 1069 static int 1070 devfs_open(struct vop_open_args *ap) 1071 { 1072 struct thread *td = ap->a_td; 1073 struct vnode *vp = ap->a_vp; 1074 struct cdev *dev = vp->v_rdev; 1075 struct file *fp = ap->a_fp; 1076 int error, ref, vlocked; 1077 struct cdevsw *dsw; 1078 struct file *fpop; 1079 struct mtx *mtxp; 1080 1081 if (vp->v_type == VBLK) 1082 return (ENXIO); 1083 1084 if (dev == NULL) 1085 return (ENXIO); 1086 1087 /* Make this field valid before any I/O in d_open. */ 1088 if (dev->si_iosize_max == 0) 1089 dev->si_iosize_max = DFLTPHYS; 1090 1091 dsw = dev_refthread(dev, &ref); 1092 if (dsw == NULL) 1093 return (ENXIO); 1094 if (fp == NULL && dsw->d_fdopen != NULL) { 1095 dev_relthread(dev, ref); 1096 return (ENXIO); 1097 } 1098 1099 vlocked = VOP_ISLOCKED(vp); 1100 VOP_UNLOCK(vp, 0); 1101 1102 fpop = td->td_fpop; 1103 td->td_fpop = fp; 1104 if (fp != NULL) { 1105 fp->f_data = dev; 1106 fp->f_vnode = vp; 1107 } 1108 if (dsw->d_fdopen != NULL) 1109 error = dsw->d_fdopen(dev, ap->a_mode, td, fp); 1110 else 1111 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 1112 /* cleanup any cdevpriv upon error */ 1113 if (error != 0) 1114 devfs_clear_cdevpriv(); 1115 td->td_fpop = fpop; 1116 1117 vn_lock(vp, vlocked | LK_RETRY); 1118 dev_relthread(dev, ref); 1119 if (error != 0) { 1120 if (error == ERESTART) 1121 error = EINTR; 1122 return (error); 1123 } 1124 1125 #if 0 /* /dev/console */ 1126 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp")); 1127 #else 1128 if (fp == NULL) 1129 return (error); 1130 #endif 1131 if (fp->f_ops == &badfileops) 1132 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f); 1133 mtxp = mtx_pool_find(mtxpool_sleep, fp); 1134 1135 /* 1136 * Hint to the dofilewrite() to not force the buffer draining 1137 * on the writer to the file. Most likely, the write would 1138 * not need normal buffers. 1139 */ 1140 mtx_lock(mtxp); 1141 fp->f_vnread_flags |= FDEVFS_VNODE; 1142 mtx_unlock(mtxp); 1143 return (error); 1144 } 1145 1146 static int 1147 devfs_pathconf(struct vop_pathconf_args *ap) 1148 { 1149 1150 switch (ap->a_name) { 1151 case _PC_MAC_PRESENT: 1152 #ifdef MAC 1153 /* 1154 * If MAC is enabled, devfs automatically supports 1155 * trivial non-persistant label storage. 1156 */ 1157 *ap->a_retval = 1; 1158 #else 1159 *ap->a_retval = 0; 1160 #endif 1161 return (0); 1162 default: 1163 return (vop_stdpathconf(ap)); 1164 } 1165 /* NOTREACHED */ 1166 } 1167 1168 /* ARGSUSED */ 1169 static int 1170 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 1171 { 1172 struct cdev *dev; 1173 struct cdevsw *dsw; 1174 int error, ref; 1175 struct file *fpop; 1176 1177 fpop = td->td_fpop; 1178 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1179 if (error != 0) { 1180 error = vnops.fo_poll(fp, events, cred, td); 1181 return (error); 1182 } 1183 error = dsw->d_poll(dev, events, td); 1184 td->td_fpop = fpop; 1185 dev_relthread(dev, ref); 1186 return(error); 1187 } 1188 1189 /* 1190 * Print out the contents of a special device vnode. 1191 */ 1192 static int 1193 devfs_print(struct vop_print_args *ap) 1194 { 1195 1196 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev)); 1197 return (0); 1198 } 1199 1200 static int 1201 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, 1202 int flags, struct thread *td) 1203 { 1204 struct cdev *dev; 1205 int ioflag, error, ref; 1206 ssize_t resid; 1207 struct cdevsw *dsw; 1208 struct file *fpop; 1209 1210 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1211 return (EINVAL); 1212 fpop = td->td_fpop; 1213 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1214 if (error != 0) { 1215 error = vnops.fo_read(fp, uio, cred, flags, td); 1216 return (error); 1217 } 1218 resid = uio->uio_resid; 1219 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT); 1220 if (ioflag & O_DIRECT) 1221 ioflag |= IO_DIRECT; 1222 1223 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1224 error = dsw->d_read(dev, uio, ioflag); 1225 if (uio->uio_resid != resid || (error == 0 && resid != 0)) 1226 vfs_timestamp(&dev->si_atime); 1227 td->td_fpop = fpop; 1228 dev_relthread(dev, ref); 1229 1230 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF); 1231 return (error); 1232 } 1233 1234 static int 1235 devfs_readdir(struct vop_readdir_args *ap) 1236 { 1237 int error; 1238 struct uio *uio; 1239 struct dirent *dp; 1240 struct devfs_dirent *dd; 1241 struct devfs_dirent *de; 1242 struct devfs_mount *dmp; 1243 off_t off; 1244 int *tmp_ncookies = NULL; 1245 1246 if (ap->a_vp->v_type != VDIR) 1247 return (ENOTDIR); 1248 1249 uio = ap->a_uio; 1250 if (uio->uio_offset < 0) 1251 return (EINVAL); 1252 1253 /* 1254 * XXX: This is a temporary hack to get around this filesystem not 1255 * supporting cookies. We store the location of the ncookies pointer 1256 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent() 1257 * and set the number of cookies to 0. We then set the pointer to 1258 * NULL so that vfs_read_dirent doesn't try to call realloc() on 1259 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies 1260 * pointer to its original location before returning to the caller. 1261 */ 1262 if (ap->a_ncookies != NULL) { 1263 tmp_ncookies = ap->a_ncookies; 1264 *ap->a_ncookies = 0; 1265 ap->a_ncookies = NULL; 1266 } 1267 1268 dmp = VFSTODEVFS(ap->a_vp->v_mount); 1269 if (devfs_populate_vp(ap->a_vp) != 0) { 1270 if (tmp_ncookies != NULL) 1271 ap->a_ncookies = tmp_ncookies; 1272 return (EIO); 1273 } 1274 error = 0; 1275 de = ap->a_vp->v_data; 1276 off = 0; 1277 TAILQ_FOREACH(dd, &de->de_dlist, de_list) { 1278 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__)); 1279 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT)) 1280 continue; 1281 if (devfs_prison_check(dd, uio->uio_td)) 1282 continue; 1283 if (dd->de_dirent->d_type == DT_DIR) 1284 de = dd->de_dir; 1285 else 1286 de = dd; 1287 dp = dd->de_dirent; 1288 if (dp->d_reclen > uio->uio_resid) 1289 break; 1290 dp->d_fileno = de->de_inode; 1291 if (off >= uio->uio_offset) { 1292 error = vfs_read_dirent(ap, dp, off); 1293 if (error) 1294 break; 1295 } 1296 off += dp->d_reclen; 1297 } 1298 sx_xunlock(&dmp->dm_lock); 1299 uio->uio_offset = off; 1300 1301 /* 1302 * Restore ap->a_ncookies if it wasn't originally NULL in the first 1303 * place. 1304 */ 1305 if (tmp_ncookies != NULL) 1306 ap->a_ncookies = tmp_ncookies; 1307 1308 return (error); 1309 } 1310 1311 static int 1312 devfs_readlink(struct vop_readlink_args *ap) 1313 { 1314 struct devfs_dirent *de; 1315 1316 de = ap->a_vp->v_data; 1317 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio)); 1318 } 1319 1320 static int 1321 devfs_reclaim(struct vop_reclaim_args *ap) 1322 { 1323 struct vnode *vp = ap->a_vp; 1324 struct devfs_dirent *de; 1325 struct cdev *dev; 1326 1327 mtx_lock(&devfs_de_interlock); 1328 de = vp->v_data; 1329 if (de != NULL) { 1330 de->de_vnode = NULL; 1331 vp->v_data = NULL; 1332 } 1333 mtx_unlock(&devfs_de_interlock); 1334 1335 vnode_destroy_vobject(vp); 1336 1337 VI_LOCK(vp); 1338 dev_lock(); 1339 dev = vp->v_rdev; 1340 vp->v_rdev = NULL; 1341 1342 if (dev == NULL) { 1343 dev_unlock(); 1344 VI_UNLOCK(vp); 1345 return (0); 1346 } 1347 1348 dev->si_usecount -= vp->v_usecount; 1349 dev_unlock(); 1350 VI_UNLOCK(vp); 1351 dev_rel(dev); 1352 return (0); 1353 } 1354 1355 static int 1356 devfs_remove(struct vop_remove_args *ap) 1357 { 1358 struct vnode *dvp = ap->a_dvp; 1359 struct vnode *vp = ap->a_vp; 1360 struct devfs_dirent *dd; 1361 struct devfs_dirent *de, *de_covered; 1362 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount); 1363 1364 ASSERT_VOP_ELOCKED(dvp, "devfs_remove"); 1365 ASSERT_VOP_ELOCKED(vp, "devfs_remove"); 1366 1367 sx_xlock(&dmp->dm_lock); 1368 dd = ap->a_dvp->v_data; 1369 de = vp->v_data; 1370 if (de->de_cdp == NULL) { 1371 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 1372 if (de->de_dirent->d_type == DT_LNK) { 1373 de_covered = devfs_find(dd, de->de_dirent->d_name, 1374 de->de_dirent->d_namlen, 0); 1375 if (de_covered != NULL) 1376 de_covered->de_flags &= ~DE_COVERED; 1377 } 1378 /* We need to unlock dvp because devfs_delete() may lock it. */ 1379 VOP_UNLOCK(vp, 0); 1380 if (dvp != vp) 1381 VOP_UNLOCK(dvp, 0); 1382 devfs_delete(dmp, de, 0); 1383 sx_xunlock(&dmp->dm_lock); 1384 if (dvp != vp) 1385 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1387 } else { 1388 de->de_flags |= DE_WHITEOUT; 1389 sx_xunlock(&dmp->dm_lock); 1390 } 1391 return (0); 1392 } 1393 1394 /* 1395 * Revoke is called on a tty when a terminal session ends. The vnode 1396 * is orphaned by setting v_op to deadfs so we need to let go of it 1397 * as well so that we create a new one next time around. 1398 * 1399 */ 1400 static int 1401 devfs_revoke(struct vop_revoke_args *ap) 1402 { 1403 struct vnode *vp = ap->a_vp, *vp2; 1404 struct cdev *dev; 1405 struct cdev_priv *cdp; 1406 struct devfs_dirent *de; 1407 int i; 1408 1409 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL")); 1410 1411 dev = vp->v_rdev; 1412 cdp = cdev2priv(dev); 1413 1414 dev_lock(); 1415 cdp->cdp_inuse++; 1416 dev_unlock(); 1417 1418 vhold(vp); 1419 vgone(vp); 1420 vdrop(vp); 1421 1422 VOP_UNLOCK(vp,0); 1423 loop: 1424 for (;;) { 1425 mtx_lock(&devfs_de_interlock); 1426 dev_lock(); 1427 vp2 = NULL; 1428 for (i = 0; i <= cdp->cdp_maxdirent; i++) { 1429 de = cdp->cdp_dirents[i]; 1430 if (de == NULL) 1431 continue; 1432 1433 vp2 = de->de_vnode; 1434 if (vp2 != NULL) { 1435 dev_unlock(); 1436 VI_LOCK(vp2); 1437 mtx_unlock(&devfs_de_interlock); 1438 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, 1439 curthread)) 1440 goto loop; 1441 vhold(vp2); 1442 vgone(vp2); 1443 vdrop(vp2); 1444 vput(vp2); 1445 break; 1446 } 1447 } 1448 if (vp2 != NULL) { 1449 continue; 1450 } 1451 dev_unlock(); 1452 mtx_unlock(&devfs_de_interlock); 1453 break; 1454 } 1455 dev_lock(); 1456 cdp->cdp_inuse--; 1457 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) { 1458 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list); 1459 dev_unlock(); 1460 dev_rel(&cdp->cdp_c); 1461 } else 1462 dev_unlock(); 1463 1464 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1465 return (0); 1466 } 1467 1468 static int 1469 devfs_rioctl(struct vop_ioctl_args *ap) 1470 { 1471 struct vnode *vp; 1472 struct devfs_mount *dmp; 1473 int error; 1474 1475 vp = ap->a_vp; 1476 vn_lock(vp, LK_SHARED | LK_RETRY); 1477 if (vp->v_iflag & VI_DOOMED) { 1478 VOP_UNLOCK(vp, 0); 1479 return (EBADF); 1480 } 1481 dmp = VFSTODEVFS(vp->v_mount); 1482 sx_xlock(&dmp->dm_lock); 1483 VOP_UNLOCK(vp, 0); 1484 DEVFS_DMP_HOLD(dmp); 1485 devfs_populate(dmp); 1486 if (DEVFS_DMP_DROP(dmp)) { 1487 sx_xunlock(&dmp->dm_lock); 1488 devfs_unmount_final(dmp); 1489 return (ENOENT); 1490 } 1491 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td); 1492 sx_xunlock(&dmp->dm_lock); 1493 return (error); 1494 } 1495 1496 static int 1497 devfs_rread(struct vop_read_args *ap) 1498 { 1499 1500 if (ap->a_vp->v_type != VDIR) 1501 return (EINVAL); 1502 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL)); 1503 } 1504 1505 static int 1506 devfs_setattr(struct vop_setattr_args *ap) 1507 { 1508 struct devfs_dirent *de; 1509 struct vattr *vap; 1510 struct vnode *vp; 1511 struct thread *td; 1512 int c, error; 1513 uid_t uid; 1514 gid_t gid; 1515 1516 vap = ap->a_vap; 1517 vp = ap->a_vp; 1518 td = curthread; 1519 if ((vap->va_type != VNON) || 1520 (vap->va_nlink != VNOVAL) || 1521 (vap->va_fsid != VNOVAL) || 1522 (vap->va_fileid != VNOVAL) || 1523 (vap->va_blocksize != VNOVAL) || 1524 (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1525 (vap->va_rdev != VNOVAL) || 1526 ((int)vap->va_bytes != VNOVAL) || 1527 (vap->va_gen != VNOVAL)) { 1528 return (EINVAL); 1529 } 1530 1531 de = vp->v_data; 1532 if (vp->v_type == VDIR) 1533 de = de->de_dir; 1534 1535 error = c = 0; 1536 if (vap->va_uid == (uid_t)VNOVAL) 1537 uid = de->de_uid; 1538 else 1539 uid = vap->va_uid; 1540 if (vap->va_gid == (gid_t)VNOVAL) 1541 gid = de->de_gid; 1542 else 1543 gid = vap->va_gid; 1544 if (uid != de->de_uid || gid != de->de_gid) { 1545 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid || 1546 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) { 1547 error = priv_check(td, PRIV_VFS_CHOWN); 1548 if (error) 1549 return (error); 1550 } 1551 de->de_uid = uid; 1552 de->de_gid = gid; 1553 c = 1; 1554 } 1555 1556 if (vap->va_mode != (mode_t)VNOVAL) { 1557 if (ap->a_cred->cr_uid != de->de_uid) { 1558 error = priv_check(td, PRIV_VFS_ADMIN); 1559 if (error) 1560 return (error); 1561 } 1562 de->de_mode = vap->va_mode; 1563 c = 1; 1564 } 1565 1566 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1567 error = vn_utimes_perm(vp, vap, ap->a_cred, td); 1568 if (error != 0) 1569 return (error); 1570 if (vap->va_atime.tv_sec != VNOVAL) { 1571 if (vp->v_type == VCHR) 1572 vp->v_rdev->si_atime = vap->va_atime; 1573 else 1574 de->de_atime = vap->va_atime; 1575 } 1576 if (vap->va_mtime.tv_sec != VNOVAL) { 1577 if (vp->v_type == VCHR) 1578 vp->v_rdev->si_mtime = vap->va_mtime; 1579 else 1580 de->de_mtime = vap->va_mtime; 1581 } 1582 c = 1; 1583 } 1584 1585 if (c) { 1586 if (vp->v_type == VCHR) 1587 vfs_timestamp(&vp->v_rdev->si_ctime); 1588 else 1589 vfs_timestamp(&de->de_mtime); 1590 } 1591 return (0); 1592 } 1593 1594 #ifdef MAC 1595 static int 1596 devfs_setlabel(struct vop_setlabel_args *ap) 1597 { 1598 struct vnode *vp; 1599 struct devfs_dirent *de; 1600 1601 vp = ap->a_vp; 1602 de = vp->v_data; 1603 1604 mac_vnode_relabel(ap->a_cred, vp, ap->a_label); 1605 mac_devfs_update(vp->v_mount, de, vp); 1606 1607 return (0); 1608 } 1609 #endif 1610 1611 static int 1612 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) 1613 { 1614 1615 return (vnops.fo_stat(fp, sb, cred, td)); 1616 } 1617 1618 static int 1619 devfs_symlink(struct vop_symlink_args *ap) 1620 { 1621 int i, error; 1622 struct devfs_dirent *dd; 1623 struct devfs_dirent *de, *de_covered, *de_dotdot; 1624 struct devfs_mount *dmp; 1625 1626 error = priv_check(curthread, PRIV_DEVFS_SYMLINK); 1627 if (error) 1628 return(error); 1629 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1630 if (devfs_populate_vp(ap->a_dvp) != 0) 1631 return (ENOENT); 1632 1633 dd = ap->a_dvp->v_data; 1634 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen); 1635 de->de_flags = DE_USER; 1636 de->de_uid = 0; 1637 de->de_gid = 0; 1638 de->de_mode = 0755; 1639 de->de_inode = alloc_unr(devfs_inos); 1640 de->de_dir = dd; 1641 de->de_dirent->d_type = DT_LNK; 1642 i = strlen(ap->a_target) + 1; 1643 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK); 1644 bcopy(ap->a_target, de->de_symlink, i); 1645 #ifdef MAC 1646 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de); 1647 #endif 1648 de_covered = devfs_find(dd, de->de_dirent->d_name, 1649 de->de_dirent->d_namlen, 0); 1650 if (de_covered != NULL) { 1651 if ((de_covered->de_flags & DE_USER) != 0) { 1652 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE); 1653 sx_xunlock(&dmp->dm_lock); 1654 return (EEXIST); 1655 } 1656 KASSERT((de_covered->de_flags & DE_COVERED) == 0, 1657 ("devfs_symlink: entry %p already covered", de_covered)); 1658 de_covered->de_flags |= DE_COVERED; 1659 } 1660 1661 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */ 1662 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */ 1663 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list); 1664 devfs_dir_ref_de(dmp, dd); 1665 devfs_rules_apply(dmp, de); 1666 1667 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp)); 1668 } 1669 1670 static int 1671 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td) 1672 { 1673 1674 return (vnops.fo_truncate(fp, length, cred, td)); 1675 } 1676 1677 static int 1678 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, 1679 int flags, struct thread *td) 1680 { 1681 struct cdev *dev; 1682 int error, ioflag, ref; 1683 ssize_t resid; 1684 struct cdevsw *dsw; 1685 struct file *fpop; 1686 1687 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1688 return (EINVAL); 1689 fpop = td->td_fpop; 1690 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1691 if (error != 0) { 1692 error = vnops.fo_write(fp, uio, cred, flags, td); 1693 return (error); 1694 } 1695 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td)); 1696 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC); 1697 if (ioflag & O_DIRECT) 1698 ioflag |= IO_DIRECT; 1699 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1700 1701 resid = uio->uio_resid; 1702 1703 error = dsw->d_write(dev, uio, ioflag); 1704 if (uio->uio_resid != resid || (error == 0 && resid != 0)) { 1705 vfs_timestamp(&dev->si_ctime); 1706 dev->si_mtime = dev->si_ctime; 1707 } 1708 td->td_fpop = fpop; 1709 dev_relthread(dev, ref); 1710 1711 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF); 1712 return (error); 1713 } 1714 1715 dev_t 1716 dev2udev(struct cdev *x) 1717 { 1718 if (x == NULL) 1719 return (NODEV); 1720 return (cdev2priv(x)->cdp_inode); 1721 } 1722 1723 static struct fileops devfs_ops_f = { 1724 .fo_read = devfs_read_f, 1725 .fo_write = devfs_write_f, 1726 .fo_truncate = devfs_truncate_f, 1727 .fo_ioctl = devfs_ioctl_f, 1728 .fo_poll = devfs_poll_f, 1729 .fo_kqfilter = devfs_kqfilter_f, 1730 .fo_stat = devfs_stat_f, 1731 .fo_close = devfs_close_f, 1732 .fo_chmod = vn_chmod, 1733 .fo_chown = vn_chown, 1734 .fo_sendfile = vn_sendfile, 1735 .fo_seek = vn_seek, 1736 .fo_fill_kinfo = vn_fill_kinfo, 1737 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 1738 }; 1739 1740 static struct vop_vector devfs_vnodeops = { 1741 .vop_default = &default_vnodeops, 1742 1743 .vop_access = devfs_access, 1744 .vop_getattr = devfs_getattr, 1745 .vop_ioctl = devfs_rioctl, 1746 .vop_lookup = devfs_lookup, 1747 .vop_mknod = devfs_mknod, 1748 .vop_pathconf = devfs_pathconf, 1749 .vop_read = devfs_rread, 1750 .vop_readdir = devfs_readdir, 1751 .vop_readlink = devfs_readlink, 1752 .vop_reclaim = devfs_reclaim, 1753 .vop_remove = devfs_remove, 1754 .vop_revoke = devfs_revoke, 1755 .vop_setattr = devfs_setattr, 1756 #ifdef MAC 1757 .vop_setlabel = devfs_setlabel, 1758 #endif 1759 .vop_symlink = devfs_symlink, 1760 .vop_vptocnp = devfs_vptocnp, 1761 }; 1762 1763 static struct vop_vector devfs_specops = { 1764 .vop_default = &default_vnodeops, 1765 1766 .vop_access = devfs_access, 1767 .vop_bmap = VOP_PANIC, 1768 .vop_close = devfs_close, 1769 .vop_create = VOP_PANIC, 1770 .vop_fsync = devfs_fsync, 1771 .vop_getattr = devfs_getattr, 1772 .vop_link = VOP_PANIC, 1773 .vop_mkdir = VOP_PANIC, 1774 .vop_mknod = VOP_PANIC, 1775 .vop_open = devfs_open, 1776 .vop_pathconf = devfs_pathconf, 1777 .vop_poll = dead_poll, 1778 .vop_print = devfs_print, 1779 .vop_read = dead_read, 1780 .vop_readdir = VOP_PANIC, 1781 .vop_readlink = VOP_PANIC, 1782 .vop_reallocblks = VOP_PANIC, 1783 .vop_reclaim = devfs_reclaim, 1784 .vop_remove = devfs_remove, 1785 .vop_rename = VOP_PANIC, 1786 .vop_revoke = devfs_revoke, 1787 .vop_rmdir = VOP_PANIC, 1788 .vop_setattr = devfs_setattr, 1789 #ifdef MAC 1790 .vop_setlabel = devfs_setlabel, 1791 #endif 1792 .vop_strategy = VOP_PANIC, 1793 .vop_symlink = VOP_PANIC, 1794 .vop_vptocnp = devfs_vptocnp, 1795 .vop_write = dead_write, 1796 }; 1797 1798 /* 1799 * Our calling convention to the device drivers used to be that we passed 1800 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 1801 * flags instead since that's what open(), close() and ioctl() takes and 1802 * we don't really want vnode.h in device drivers. 1803 * We solved the source compatibility by redefining some vnode flags to 1804 * be the same as the fcntl ones and by sending down the bitwise OR of 1805 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody 1806 * pulls the rug out under this. 1807 */ 1808 CTASSERT(O_NONBLOCK == IO_NDELAY); 1809 CTASSERT(O_FSYNC == IO_SYNC); 1810