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