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 int error; 503 504 de = vp->v_data; 505 if (vp->v_type == VDIR) 506 de = de->de_dir; 507 508 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid, 509 ap->a_accmode, ap->a_cred, NULL); 510 if (error == 0) 511 return (0); 512 if (error != EACCES) 513 return (error); 514 /* We do, however, allow access to the controlling terminal */ 515 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT)) 516 return (error); 517 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp) 518 return (0); 519 return (error); 520 } 521 522 /* ARGSUSED */ 523 static int 524 devfs_close(struct vop_close_args *ap) 525 { 526 struct vnode *vp = ap->a_vp, *oldvp; 527 struct thread *td = ap->a_td; 528 struct cdev *dev = vp->v_rdev; 529 struct cdevsw *dsw; 530 int vp_locked, error, ref; 531 532 /* 533 * XXX: Don't call d_close() if we were called because of 534 * XXX: insmntque1() failure. 535 */ 536 if (vp->v_data == NULL) 537 return (0); 538 539 /* 540 * Hack: a tty device that is a controlling terminal 541 * has a reference from the session structure. 542 * We cannot easily tell that a character device is 543 * a controlling terminal, unless it is the closing 544 * process' controlling terminal. In that case, 545 * if the reference count is 2 (this last descriptor 546 * plus the session), release the reference from the session. 547 */ 548 if (td && vp == td->td_proc->p_session->s_ttyvp) { 549 oldvp = NULL; 550 sx_xlock(&proctree_lock); 551 if (vp == td->td_proc->p_session->s_ttyvp) { 552 SESS_LOCK(td->td_proc->p_session); 553 VI_LOCK(vp); 554 if (count_dev(dev) == 2 && 555 (vp->v_iflag & VI_DOOMED) == 0) { 556 td->td_proc->p_session->s_ttyvp = NULL; 557 td->td_proc->p_session->s_ttydp = NULL; 558 oldvp = vp; 559 } 560 VI_UNLOCK(vp); 561 SESS_UNLOCK(td->td_proc->p_session); 562 } 563 sx_xunlock(&proctree_lock); 564 if (oldvp != NULL) 565 vrele(oldvp); 566 } 567 /* 568 * We do not want to really close the device if it 569 * is still in use unless we are trying to close it 570 * forcibly. Since every use (buffer, vnode, swap, cmap) 571 * holds a reference to the vnode, and because we mark 572 * any other vnodes that alias this device, when the 573 * sum of the reference counts on all the aliased 574 * vnodes descends to one, we are on last close. 575 */ 576 dsw = dev_refthread(dev, &ref); 577 if (dsw == NULL) 578 return (ENXIO); 579 VI_LOCK(vp); 580 if (vp->v_iflag & VI_DOOMED) { 581 /* Forced close. */ 582 } else if (dsw->d_flags & D_TRACKCLOSE) { 583 /* Keep device updated on status. */ 584 } else if (count_dev(dev) > 1) { 585 VI_UNLOCK(vp); 586 dev_relthread(dev, ref); 587 return (0); 588 } 589 vholdl(vp); 590 VI_UNLOCK(vp); 591 vp_locked = VOP_ISLOCKED(vp); 592 VOP_UNLOCK(vp, 0); 593 KASSERT(dev->si_refcount > 0, 594 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev))); 595 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td); 596 dev_relthread(dev, ref); 597 vn_lock(vp, vp_locked | LK_RETRY); 598 vdrop(vp); 599 return (error); 600 } 601 602 static int 603 devfs_close_f(struct file *fp, struct thread *td) 604 { 605 int error; 606 struct file *fpop; 607 608 /* 609 * NB: td may be NULL if this descriptor is closed due to 610 * garbage collection from a closed UNIX domain socket. 611 */ 612 fpop = curthread->td_fpop; 613 curthread->td_fpop = fp; 614 error = vnops.fo_close(fp, td); 615 curthread->td_fpop = fpop; 616 617 /* 618 * The f_cdevpriv cannot be assigned non-NULL value while we 619 * are destroying the file. 620 */ 621 if (fp->f_cdevpriv != NULL) 622 devfs_fpdrop(fp); 623 return (error); 624 } 625 626 static int 627 devfs_fsync(struct vop_fsync_args *ap) 628 { 629 int error; 630 struct bufobj *bo; 631 struct devfs_dirent *de; 632 633 if (!vn_isdisk(ap->a_vp, &error)) { 634 bo = &ap->a_vp->v_bufobj; 635 de = ap->a_vp->v_data; 636 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) { 637 printf("Device %s went missing before all of the data " 638 "could be written to it; expect data loss.\n", 639 de->de_dirent->d_name); 640 641 error = vop_stdfsync(ap); 642 if (bo->bo_dirty.bv_cnt != 0 || error != 0) 643 panic("devfs_fsync: vop_stdfsync failed."); 644 } 645 646 return (0); 647 } 648 649 return (vop_stdfsync(ap)); 650 } 651 652 static int 653 devfs_getattr(struct vop_getattr_args *ap) 654 { 655 struct vnode *vp = ap->a_vp; 656 struct vattr *vap = ap->a_vap; 657 int error; 658 struct devfs_dirent *de; 659 struct devfs_mount *dmp; 660 struct cdev *dev; 661 662 error = devfs_populate_vp(vp); 663 if (error != 0) 664 return (error); 665 666 dmp = VFSTODEVFS(vp->v_mount); 667 sx_xunlock(&dmp->dm_lock); 668 669 de = vp->v_data; 670 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp)); 671 if (vp->v_type == VDIR) { 672 de = de->de_dir; 673 KASSERT(de != NULL, 674 ("Null dir dirent in devfs_getattr vp=%p", vp)); 675 } 676 vap->va_uid = de->de_uid; 677 vap->va_gid = de->de_gid; 678 vap->va_mode = de->de_mode; 679 if (vp->v_type == VLNK) 680 vap->va_size = strlen(de->de_symlink); 681 else if (vp->v_type == VDIR) 682 vap->va_size = vap->va_bytes = DEV_BSIZE; 683 else 684 vap->va_size = 0; 685 if (vp->v_type != VDIR) 686 vap->va_bytes = 0; 687 vap->va_blocksize = DEV_BSIZE; 688 vap->va_type = vp->v_type; 689 690 #define fix(aa) \ 691 do { \ 692 if ((aa).tv_sec <= 3600) { \ 693 (aa).tv_sec = boottime.tv_sec; \ 694 (aa).tv_nsec = boottime.tv_usec * 1000; \ 695 } \ 696 } while (0) 697 698 if (vp->v_type != VCHR) { 699 fix(de->de_atime); 700 vap->va_atime = de->de_atime; 701 fix(de->de_mtime); 702 vap->va_mtime = de->de_mtime; 703 fix(de->de_ctime); 704 vap->va_ctime = de->de_ctime; 705 } else { 706 dev = vp->v_rdev; 707 fix(dev->si_atime); 708 vap->va_atime = dev->si_atime; 709 fix(dev->si_mtime); 710 vap->va_mtime = dev->si_mtime; 711 fix(dev->si_ctime); 712 vap->va_ctime = dev->si_ctime; 713 714 vap->va_rdev = cdev2priv(dev)->cdp_inode; 715 } 716 vap->va_gen = 0; 717 vap->va_flags = 0; 718 vap->va_filerev = 0; 719 vap->va_nlink = de->de_links; 720 vap->va_fileid = de->de_inode; 721 722 return (error); 723 } 724 725 /* ARGSUSED */ 726 static int 727 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td) 728 { 729 struct cdev *dev; 730 struct cdevsw *dsw; 731 struct vnode *vp; 732 struct vnode *vpold; 733 int error, i, ref; 734 const char *p; 735 struct fiodgname_arg *fgn; 736 struct file *fpop; 737 738 fpop = td->td_fpop; 739 error = devfs_fp_check(fp, &dev, &dsw, &ref); 740 if (error) 741 return (error); 742 743 if (com == FIODTYPE) { 744 *(int *)data = dsw->d_flags & D_TYPEMASK; 745 td->td_fpop = fpop; 746 dev_relthread(dev, ref); 747 return (0); 748 } else if (com == FIODGNAME) { 749 fgn = data; 750 p = devtoname(dev); 751 i = strlen(p) + 1; 752 if (i > fgn->len) 753 error = EINVAL; 754 else 755 error = copyout(p, fgn->buf, i); 756 td->td_fpop = fpop; 757 dev_relthread(dev, ref); 758 return (error); 759 } 760 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td); 761 td->td_fpop = NULL; 762 dev_relthread(dev, ref); 763 if (error == ENOIOCTL) 764 error = ENOTTY; 765 if (error == 0 && com == TIOCSCTTY) { 766 vp = fp->f_vnode; 767 768 /* Do nothing if reassigning same control tty */ 769 sx_slock(&proctree_lock); 770 if (td->td_proc->p_session->s_ttyvp == vp) { 771 sx_sunlock(&proctree_lock); 772 return (0); 773 } 774 775 vpold = td->td_proc->p_session->s_ttyvp; 776 VREF(vp); 777 SESS_LOCK(td->td_proc->p_session); 778 td->td_proc->p_session->s_ttyvp = vp; 779 td->td_proc->p_session->s_ttydp = cdev2priv(dev); 780 SESS_UNLOCK(td->td_proc->p_session); 781 782 sx_sunlock(&proctree_lock); 783 784 /* Get rid of reference to old control tty */ 785 if (vpold) 786 vrele(vpold); 787 } 788 return (error); 789 } 790 791 /* ARGSUSED */ 792 static int 793 devfs_kqfilter_f(struct file *fp, struct knote *kn) 794 { 795 struct cdev *dev; 796 struct cdevsw *dsw; 797 int error, ref; 798 struct file *fpop; 799 struct thread *td; 800 801 td = curthread; 802 fpop = td->td_fpop; 803 error = devfs_fp_check(fp, &dev, &dsw, &ref); 804 if (error) 805 return (error); 806 error = dsw->d_kqfilter(dev, kn); 807 td->td_fpop = fpop; 808 dev_relthread(dev, ref); 809 return (error); 810 } 811 812 static inline int 813 devfs_prison_check(struct devfs_dirent *de, struct thread *td) 814 { 815 struct cdev_priv *cdp; 816 struct ucred *dcr; 817 int error; 818 819 cdp = de->de_cdp; 820 if (cdp == NULL) 821 return (0); 822 dcr = cdp->cdp_c.si_cred; 823 if (dcr == NULL) 824 return (0); 825 826 error = prison_check(td->td_ucred, dcr); 827 if (error == 0) 828 return (0); 829 /* We do, however, allow access to the controlling terminal */ 830 if (!(td->td_proc->p_flag & P_CONTROLT)) 831 return (error); 832 if (td->td_proc->p_session->s_ttydp == cdp) 833 return (0); 834 return (error); 835 } 836 837 static int 838 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock) 839 { 840 struct componentname *cnp; 841 struct vnode *dvp, **vpp; 842 struct thread *td; 843 struct devfs_dirent *de, *dd; 844 struct devfs_dirent **dde; 845 struct devfs_mount *dmp; 846 struct cdev *cdev; 847 int error, flags, nameiop, dvplocked; 848 char specname[SPECNAMELEN + 1], *pname; 849 850 cnp = ap->a_cnp; 851 vpp = ap->a_vpp; 852 dvp = ap->a_dvp; 853 pname = cnp->cn_nameptr; 854 td = cnp->cn_thread; 855 flags = cnp->cn_flags; 856 nameiop = cnp->cn_nameiop; 857 dmp = VFSTODEVFS(dvp->v_mount); 858 dd = dvp->v_data; 859 *vpp = NULLVP; 860 861 if ((flags & ISLASTCN) && nameiop == RENAME) 862 return (EOPNOTSUPP); 863 864 if (dvp->v_type != VDIR) 865 return (ENOTDIR); 866 867 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) 868 return (EIO); 869 870 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 871 if (error) 872 return (error); 873 874 if (cnp->cn_namelen == 1 && *pname == '.') { 875 if ((flags & ISLASTCN) && nameiop != LOOKUP) 876 return (EINVAL); 877 *vpp = dvp; 878 VREF(dvp); 879 return (0); 880 } 881 882 if (flags & ISDOTDOT) { 883 if ((flags & ISLASTCN) && nameiop != LOOKUP) 884 return (EINVAL); 885 de = devfs_parent_dirent(dd); 886 if (de == NULL) 887 return (ENOENT); 888 dvplocked = VOP_ISLOCKED(dvp); 889 VOP_UNLOCK(dvp, 0); 890 error = devfs_allocv(de, dvp->v_mount, 891 cnp->cn_lkflags & LK_TYPE_MASK, vpp); 892 *dm_unlock = 0; 893 vn_lock(dvp, dvplocked | LK_RETRY); 894 return (error); 895 } 896 897 dd = dvp->v_data; 898 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0); 899 while (de == NULL) { /* While(...) so we can use break */ 900 901 if (nameiop == DELETE) 902 return (ENOENT); 903 904 /* 905 * OK, we didn't have an entry for the name we were asked for 906 * so we try to see if anybody can create it on demand. 907 */ 908 pname = devfs_fqpn(specname, dmp, dd, cnp); 909 if (pname == NULL) 910 break; 911 912 cdev = NULL; 913 DEVFS_DMP_HOLD(dmp); 914 sx_xunlock(&dmp->dm_lock); 915 sx_slock(&clone_drain_lock); 916 EVENTHANDLER_INVOKE(dev_clone, 917 td->td_ucred, pname, strlen(pname), &cdev); 918 sx_sunlock(&clone_drain_lock); 919 920 if (cdev == NULL) 921 sx_xlock(&dmp->dm_lock); 922 else if (devfs_populate_vp(dvp) != 0) { 923 *dm_unlock = 0; 924 sx_xlock(&dmp->dm_lock); 925 if (DEVFS_DMP_DROP(dmp)) { 926 sx_xunlock(&dmp->dm_lock); 927 devfs_unmount_final(dmp); 928 } else 929 sx_xunlock(&dmp->dm_lock); 930 dev_rel(cdev); 931 return (ENOENT); 932 } 933 if (DEVFS_DMP_DROP(dmp)) { 934 *dm_unlock = 0; 935 sx_xunlock(&dmp->dm_lock); 936 devfs_unmount_final(dmp); 937 if (cdev != NULL) 938 dev_rel(cdev); 939 return (ENOENT); 940 } 941 942 if (cdev == NULL) 943 break; 944 945 dev_lock(); 946 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx]; 947 if (dde != NULL && *dde != NULL) 948 de = *dde; 949 dev_unlock(); 950 dev_rel(cdev); 951 break; 952 } 953 954 if (de == NULL || de->de_flags & DE_WHITEOUT) { 955 if ((nameiop == CREATE || nameiop == RENAME) && 956 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) { 957 cnp->cn_flags |= SAVENAME; 958 return (EJUSTRETURN); 959 } 960 return (ENOENT); 961 } 962 963 if (devfs_prison_check(de, td)) 964 return (ENOENT); 965 966 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) { 967 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 968 if (error) 969 return (error); 970 if (*vpp == dvp) { 971 VREF(dvp); 972 *vpp = dvp; 973 return (0); 974 } 975 } 976 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK, 977 vpp); 978 *dm_unlock = 0; 979 return (error); 980 } 981 982 static int 983 devfs_lookup(struct vop_lookup_args *ap) 984 { 985 int j; 986 struct devfs_mount *dmp; 987 int dm_unlock; 988 989 if (devfs_populate_vp(ap->a_dvp) != 0) 990 return (ENOTDIR); 991 992 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 993 dm_unlock = 1; 994 j = devfs_lookupx(ap, &dm_unlock); 995 if (dm_unlock == 1) 996 sx_xunlock(&dmp->dm_lock); 997 return (j); 998 } 999 1000 static int 1001 devfs_mknod(struct vop_mknod_args *ap) 1002 { 1003 struct componentname *cnp; 1004 struct vnode *dvp, **vpp; 1005 struct devfs_dirent *dd, *de; 1006 struct devfs_mount *dmp; 1007 int error; 1008 1009 /* 1010 * The only type of node we should be creating here is a 1011 * character device, for anything else return EOPNOTSUPP. 1012 */ 1013 if (ap->a_vap->va_type != VCHR) 1014 return (EOPNOTSUPP); 1015 dvp = ap->a_dvp; 1016 dmp = VFSTODEVFS(dvp->v_mount); 1017 1018 cnp = ap->a_cnp; 1019 vpp = ap->a_vpp; 1020 dd = dvp->v_data; 1021 1022 error = ENOENT; 1023 sx_xlock(&dmp->dm_lock); 1024 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 1025 if (cnp->cn_namelen != de->de_dirent->d_namlen) 1026 continue; 1027 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name, 1028 de->de_dirent->d_namlen) != 0) 1029 continue; 1030 if (de->de_flags & DE_WHITEOUT) 1031 break; 1032 goto notfound; 1033 } 1034 if (de == NULL) 1035 goto notfound; 1036 de->de_flags &= ~DE_WHITEOUT; 1037 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp); 1038 return (error); 1039 notfound: 1040 sx_xunlock(&dmp->dm_lock); 1041 return (error); 1042 } 1043 1044 /* ARGSUSED */ 1045 static int 1046 devfs_open(struct vop_open_args *ap) 1047 { 1048 struct thread *td = ap->a_td; 1049 struct vnode *vp = ap->a_vp; 1050 struct cdev *dev = vp->v_rdev; 1051 struct file *fp = ap->a_fp; 1052 int error, ref, vlocked; 1053 struct cdevsw *dsw; 1054 struct file *fpop; 1055 struct mtx *mtxp; 1056 1057 if (vp->v_type == VBLK) 1058 return (ENXIO); 1059 1060 if (dev == NULL) 1061 return (ENXIO); 1062 1063 /* Make this field valid before any I/O in d_open. */ 1064 if (dev->si_iosize_max == 0) 1065 dev->si_iosize_max = DFLTPHYS; 1066 1067 dsw = dev_refthread(dev, &ref); 1068 if (dsw == NULL) 1069 return (ENXIO); 1070 if (fp == NULL && dsw->d_fdopen != NULL) { 1071 dev_relthread(dev, ref); 1072 return (ENXIO); 1073 } 1074 1075 vlocked = VOP_ISLOCKED(vp); 1076 VOP_UNLOCK(vp, 0); 1077 1078 fpop = td->td_fpop; 1079 td->td_fpop = fp; 1080 if (fp != NULL) { 1081 fp->f_data = dev; 1082 fp->f_vnode = vp; 1083 } 1084 if (dsw->d_fdopen != NULL) 1085 error = dsw->d_fdopen(dev, ap->a_mode, td, fp); 1086 else 1087 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td); 1088 /* cleanup any cdevpriv upon error */ 1089 if (error != 0) 1090 devfs_clear_cdevpriv(); 1091 td->td_fpop = fpop; 1092 1093 vn_lock(vp, vlocked | LK_RETRY); 1094 dev_relthread(dev, ref); 1095 if (error != 0) { 1096 if (error == ERESTART) 1097 error = EINTR; 1098 return (error); 1099 } 1100 1101 #if 0 /* /dev/console */ 1102 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp")); 1103 #else 1104 if (fp == NULL) 1105 return (error); 1106 #endif 1107 if (fp->f_ops == &badfileops) 1108 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f); 1109 mtxp = mtx_pool_find(mtxpool_sleep, fp); 1110 1111 /* 1112 * Hint to the dofilewrite() to not force the buffer draining 1113 * on the writer to the file. Most likely, the write would 1114 * not need normal buffers. 1115 */ 1116 mtx_lock(mtxp); 1117 fp->f_vnread_flags |= FDEVFS_VNODE; 1118 mtx_unlock(mtxp); 1119 return (error); 1120 } 1121 1122 static int 1123 devfs_pathconf(struct vop_pathconf_args *ap) 1124 { 1125 1126 switch (ap->a_name) { 1127 case _PC_MAC_PRESENT: 1128 #ifdef MAC 1129 /* 1130 * If MAC is enabled, devfs automatically supports 1131 * trivial non-persistant label storage. 1132 */ 1133 *ap->a_retval = 1; 1134 #else 1135 *ap->a_retval = 0; 1136 #endif 1137 return (0); 1138 default: 1139 return (vop_stdpathconf(ap)); 1140 } 1141 /* NOTREACHED */ 1142 } 1143 1144 /* ARGSUSED */ 1145 static int 1146 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 1147 { 1148 struct cdev *dev; 1149 struct cdevsw *dsw; 1150 int error, ref; 1151 struct file *fpop; 1152 1153 fpop = td->td_fpop; 1154 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1155 if (error) 1156 return (poll_no_poll(events)); 1157 error = dsw->d_poll(dev, events, td); 1158 td->td_fpop = fpop; 1159 dev_relthread(dev, ref); 1160 return(error); 1161 } 1162 1163 /* 1164 * Print out the contents of a special device vnode. 1165 */ 1166 static int 1167 devfs_print(struct vop_print_args *ap) 1168 { 1169 1170 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev)); 1171 return (0); 1172 } 1173 1174 static int 1175 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, 1176 int flags, struct thread *td) 1177 { 1178 struct cdev *dev; 1179 int ioflag, error, ref; 1180 ssize_t resid; 1181 struct cdevsw *dsw; 1182 struct file *fpop; 1183 1184 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1185 return (EINVAL); 1186 fpop = td->td_fpop; 1187 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1188 if (error) 1189 return (error); 1190 resid = uio->uio_resid; 1191 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT); 1192 if (ioflag & O_DIRECT) 1193 ioflag |= IO_DIRECT; 1194 1195 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1196 error = dsw->d_read(dev, uio, ioflag); 1197 if (uio->uio_resid != resid || (error == 0 && resid != 0)) 1198 vfs_timestamp(&dev->si_atime); 1199 td->td_fpop = fpop; 1200 dev_relthread(dev, ref); 1201 1202 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF); 1203 return (error); 1204 } 1205 1206 static int 1207 devfs_readdir(struct vop_readdir_args *ap) 1208 { 1209 int error; 1210 struct uio *uio; 1211 struct dirent *dp; 1212 struct devfs_dirent *dd; 1213 struct devfs_dirent *de; 1214 struct devfs_mount *dmp; 1215 off_t off; 1216 int *tmp_ncookies = NULL; 1217 1218 if (ap->a_vp->v_type != VDIR) 1219 return (ENOTDIR); 1220 1221 uio = ap->a_uio; 1222 if (uio->uio_offset < 0) 1223 return (EINVAL); 1224 1225 /* 1226 * XXX: This is a temporary hack to get around this filesystem not 1227 * supporting cookies. We store the location of the ncookies pointer 1228 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent() 1229 * and set the number of cookies to 0. We then set the pointer to 1230 * NULL so that vfs_read_dirent doesn't try to call realloc() on 1231 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies 1232 * pointer to its original location before returning to the caller. 1233 */ 1234 if (ap->a_ncookies != NULL) { 1235 tmp_ncookies = ap->a_ncookies; 1236 *ap->a_ncookies = 0; 1237 ap->a_ncookies = NULL; 1238 } 1239 1240 dmp = VFSTODEVFS(ap->a_vp->v_mount); 1241 if (devfs_populate_vp(ap->a_vp) != 0) { 1242 if (tmp_ncookies != NULL) 1243 ap->a_ncookies = tmp_ncookies; 1244 return (EIO); 1245 } 1246 error = 0; 1247 de = ap->a_vp->v_data; 1248 off = 0; 1249 TAILQ_FOREACH(dd, &de->de_dlist, de_list) { 1250 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__)); 1251 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT)) 1252 continue; 1253 if (devfs_prison_check(dd, uio->uio_td)) 1254 continue; 1255 if (dd->de_dirent->d_type == DT_DIR) 1256 de = dd->de_dir; 1257 else 1258 de = dd; 1259 dp = dd->de_dirent; 1260 if (dp->d_reclen > uio->uio_resid) 1261 break; 1262 dp->d_fileno = de->de_inode; 1263 if (off >= uio->uio_offset) { 1264 error = vfs_read_dirent(ap, dp, off); 1265 if (error) 1266 break; 1267 } 1268 off += dp->d_reclen; 1269 } 1270 sx_xunlock(&dmp->dm_lock); 1271 uio->uio_offset = off; 1272 1273 /* 1274 * Restore ap->a_ncookies if it wasn't originally NULL in the first 1275 * place. 1276 */ 1277 if (tmp_ncookies != NULL) 1278 ap->a_ncookies = tmp_ncookies; 1279 1280 return (error); 1281 } 1282 1283 static int 1284 devfs_readlink(struct vop_readlink_args *ap) 1285 { 1286 struct devfs_dirent *de; 1287 1288 de = ap->a_vp->v_data; 1289 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio)); 1290 } 1291 1292 static int 1293 devfs_reclaim(struct vop_reclaim_args *ap) 1294 { 1295 struct vnode *vp = ap->a_vp; 1296 struct devfs_dirent *de; 1297 struct cdev *dev; 1298 1299 mtx_lock(&devfs_de_interlock); 1300 de = vp->v_data; 1301 if (de != NULL) { 1302 de->de_vnode = NULL; 1303 vp->v_data = NULL; 1304 } 1305 mtx_unlock(&devfs_de_interlock); 1306 1307 vnode_destroy_vobject(vp); 1308 1309 VI_LOCK(vp); 1310 dev_lock(); 1311 dev = vp->v_rdev; 1312 vp->v_rdev = NULL; 1313 1314 if (dev == NULL) { 1315 dev_unlock(); 1316 VI_UNLOCK(vp); 1317 return (0); 1318 } 1319 1320 dev->si_usecount -= vp->v_usecount; 1321 dev_unlock(); 1322 VI_UNLOCK(vp); 1323 dev_rel(dev); 1324 return (0); 1325 } 1326 1327 static int 1328 devfs_remove(struct vop_remove_args *ap) 1329 { 1330 struct vnode *dvp = ap->a_dvp; 1331 struct vnode *vp = ap->a_vp; 1332 struct devfs_dirent *dd; 1333 struct devfs_dirent *de, *de_covered; 1334 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount); 1335 1336 ASSERT_VOP_ELOCKED(dvp, "devfs_remove"); 1337 ASSERT_VOP_ELOCKED(vp, "devfs_remove"); 1338 1339 sx_xlock(&dmp->dm_lock); 1340 dd = ap->a_dvp->v_data; 1341 de = vp->v_data; 1342 if (de->de_cdp == NULL) { 1343 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 1344 if (de->de_dirent->d_type == DT_LNK) { 1345 de_covered = devfs_find(dd, de->de_dirent->d_name, 1346 de->de_dirent->d_namlen, 0); 1347 if (de_covered != NULL) 1348 de_covered->de_flags &= ~DE_COVERED; 1349 } 1350 /* We need to unlock dvp because devfs_delete() may lock it. */ 1351 VOP_UNLOCK(vp, 0); 1352 if (dvp != vp) 1353 VOP_UNLOCK(dvp, 0); 1354 devfs_delete(dmp, de, 0); 1355 sx_xunlock(&dmp->dm_lock); 1356 if (dvp != vp) 1357 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1358 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1359 } else { 1360 de->de_flags |= DE_WHITEOUT; 1361 sx_xunlock(&dmp->dm_lock); 1362 } 1363 return (0); 1364 } 1365 1366 /* 1367 * Revoke is called on a tty when a terminal session ends. The vnode 1368 * is orphaned by setting v_op to deadfs so we need to let go of it 1369 * as well so that we create a new one next time around. 1370 * 1371 */ 1372 static int 1373 devfs_revoke(struct vop_revoke_args *ap) 1374 { 1375 struct vnode *vp = ap->a_vp, *vp2; 1376 struct cdev *dev; 1377 struct cdev_priv *cdp; 1378 struct devfs_dirent *de; 1379 int i; 1380 1381 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL")); 1382 1383 dev = vp->v_rdev; 1384 cdp = cdev2priv(dev); 1385 1386 dev_lock(); 1387 cdp->cdp_inuse++; 1388 dev_unlock(); 1389 1390 vhold(vp); 1391 vgone(vp); 1392 vdrop(vp); 1393 1394 VOP_UNLOCK(vp,0); 1395 loop: 1396 for (;;) { 1397 mtx_lock(&devfs_de_interlock); 1398 dev_lock(); 1399 vp2 = NULL; 1400 for (i = 0; i <= cdp->cdp_maxdirent; i++) { 1401 de = cdp->cdp_dirents[i]; 1402 if (de == NULL) 1403 continue; 1404 1405 vp2 = de->de_vnode; 1406 if (vp2 != NULL) { 1407 dev_unlock(); 1408 VI_LOCK(vp2); 1409 mtx_unlock(&devfs_de_interlock); 1410 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, 1411 curthread)) 1412 goto loop; 1413 vhold(vp2); 1414 vgone(vp2); 1415 vdrop(vp2); 1416 vput(vp2); 1417 break; 1418 } 1419 } 1420 if (vp2 != NULL) { 1421 continue; 1422 } 1423 dev_unlock(); 1424 mtx_unlock(&devfs_de_interlock); 1425 break; 1426 } 1427 dev_lock(); 1428 cdp->cdp_inuse--; 1429 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) { 1430 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list); 1431 dev_unlock(); 1432 dev_rel(&cdp->cdp_c); 1433 } else 1434 dev_unlock(); 1435 1436 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1437 return (0); 1438 } 1439 1440 static int 1441 devfs_rioctl(struct vop_ioctl_args *ap) 1442 { 1443 struct vnode *vp; 1444 struct devfs_mount *dmp; 1445 int error; 1446 1447 vp = ap->a_vp; 1448 vn_lock(vp, LK_SHARED | LK_RETRY); 1449 if (vp->v_iflag & VI_DOOMED) { 1450 VOP_UNLOCK(vp, 0); 1451 return (EBADF); 1452 } 1453 dmp = VFSTODEVFS(vp->v_mount); 1454 sx_xlock(&dmp->dm_lock); 1455 VOP_UNLOCK(vp, 0); 1456 DEVFS_DMP_HOLD(dmp); 1457 devfs_populate(dmp); 1458 if (DEVFS_DMP_DROP(dmp)) { 1459 sx_xunlock(&dmp->dm_lock); 1460 devfs_unmount_final(dmp); 1461 return (ENOENT); 1462 } 1463 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td); 1464 sx_xunlock(&dmp->dm_lock); 1465 return (error); 1466 } 1467 1468 static int 1469 devfs_rread(struct vop_read_args *ap) 1470 { 1471 1472 if (ap->a_vp->v_type != VDIR) 1473 return (EINVAL); 1474 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL)); 1475 } 1476 1477 static int 1478 devfs_setattr(struct vop_setattr_args *ap) 1479 { 1480 struct devfs_dirent *de; 1481 struct vattr *vap; 1482 struct vnode *vp; 1483 struct thread *td; 1484 int c, error; 1485 uid_t uid; 1486 gid_t gid; 1487 1488 vap = ap->a_vap; 1489 vp = ap->a_vp; 1490 td = curthread; 1491 if ((vap->va_type != VNON) || 1492 (vap->va_nlink != VNOVAL) || 1493 (vap->va_fsid != VNOVAL) || 1494 (vap->va_fileid != VNOVAL) || 1495 (vap->va_blocksize != VNOVAL) || 1496 (vap->va_flags != VNOVAL && vap->va_flags != 0) || 1497 (vap->va_rdev != VNOVAL) || 1498 ((int)vap->va_bytes != VNOVAL) || 1499 (vap->va_gen != VNOVAL)) { 1500 return (EINVAL); 1501 } 1502 1503 de = vp->v_data; 1504 if (vp->v_type == VDIR) 1505 de = de->de_dir; 1506 1507 error = c = 0; 1508 if (vap->va_uid == (uid_t)VNOVAL) 1509 uid = de->de_uid; 1510 else 1511 uid = vap->va_uid; 1512 if (vap->va_gid == (gid_t)VNOVAL) 1513 gid = de->de_gid; 1514 else 1515 gid = vap->va_gid; 1516 if (uid != de->de_uid || gid != de->de_gid) { 1517 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid || 1518 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) { 1519 error = priv_check(td, PRIV_VFS_CHOWN); 1520 if (error) 1521 return (error); 1522 } 1523 de->de_uid = uid; 1524 de->de_gid = gid; 1525 c = 1; 1526 } 1527 1528 if (vap->va_mode != (mode_t)VNOVAL) { 1529 if (ap->a_cred->cr_uid != de->de_uid) { 1530 error = priv_check(td, PRIV_VFS_ADMIN); 1531 if (error) 1532 return (error); 1533 } 1534 de->de_mode = vap->va_mode; 1535 c = 1; 1536 } 1537 1538 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 1539 error = vn_utimes_perm(vp, vap, ap->a_cred, td); 1540 if (error != 0) 1541 return (error); 1542 if (vap->va_atime.tv_sec != VNOVAL) { 1543 if (vp->v_type == VCHR) 1544 vp->v_rdev->si_atime = vap->va_atime; 1545 else 1546 de->de_atime = vap->va_atime; 1547 } 1548 if (vap->va_mtime.tv_sec != VNOVAL) { 1549 if (vp->v_type == VCHR) 1550 vp->v_rdev->si_mtime = vap->va_mtime; 1551 else 1552 de->de_mtime = vap->va_mtime; 1553 } 1554 c = 1; 1555 } 1556 1557 if (c) { 1558 if (vp->v_type == VCHR) 1559 vfs_timestamp(&vp->v_rdev->si_ctime); 1560 else 1561 vfs_timestamp(&de->de_mtime); 1562 } 1563 return (0); 1564 } 1565 1566 #ifdef MAC 1567 static int 1568 devfs_setlabel(struct vop_setlabel_args *ap) 1569 { 1570 struct vnode *vp; 1571 struct devfs_dirent *de; 1572 1573 vp = ap->a_vp; 1574 de = vp->v_data; 1575 1576 mac_vnode_relabel(ap->a_cred, vp, ap->a_label); 1577 mac_devfs_update(vp->v_mount, de, vp); 1578 1579 return (0); 1580 } 1581 #endif 1582 1583 static int 1584 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) 1585 { 1586 1587 return (vnops.fo_stat(fp, sb, cred, td)); 1588 } 1589 1590 static int 1591 devfs_symlink(struct vop_symlink_args *ap) 1592 { 1593 int i, error; 1594 struct devfs_dirent *dd; 1595 struct devfs_dirent *de, *de_covered, *de_dotdot; 1596 struct devfs_mount *dmp; 1597 1598 error = priv_check(curthread, PRIV_DEVFS_SYMLINK); 1599 if (error) 1600 return(error); 1601 dmp = VFSTODEVFS(ap->a_dvp->v_mount); 1602 if (devfs_populate_vp(ap->a_dvp) != 0) 1603 return (ENOENT); 1604 1605 dd = ap->a_dvp->v_data; 1606 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen); 1607 de->de_flags = DE_USER; 1608 de->de_uid = 0; 1609 de->de_gid = 0; 1610 de->de_mode = 0755; 1611 de->de_inode = alloc_unr(devfs_inos); 1612 de->de_dir = dd; 1613 de->de_dirent->d_type = DT_LNK; 1614 i = strlen(ap->a_target) + 1; 1615 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK); 1616 bcopy(ap->a_target, de->de_symlink, i); 1617 #ifdef MAC 1618 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de); 1619 #endif 1620 de_covered = devfs_find(dd, de->de_dirent->d_name, 1621 de->de_dirent->d_namlen, 0); 1622 if (de_covered != NULL) { 1623 if ((de_covered->de_flags & DE_USER) != 0) { 1624 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE); 1625 sx_xunlock(&dmp->dm_lock); 1626 return (EEXIST); 1627 } 1628 KASSERT((de_covered->de_flags & DE_COVERED) == 0, 1629 ("devfs_symlink: entry %p already covered", de_covered)); 1630 de_covered->de_flags |= DE_COVERED; 1631 } 1632 1633 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */ 1634 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */ 1635 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list); 1636 devfs_dir_ref_de(dmp, dd); 1637 devfs_rules_apply(dmp, de); 1638 1639 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp)); 1640 } 1641 1642 static int 1643 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td) 1644 { 1645 1646 return (vnops.fo_truncate(fp, length, cred, td)); 1647 } 1648 1649 static int 1650 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, 1651 int flags, struct thread *td) 1652 { 1653 struct cdev *dev; 1654 int error, ioflag, ref; 1655 ssize_t resid; 1656 struct cdevsw *dsw; 1657 struct file *fpop; 1658 1659 if (uio->uio_resid > DEVFS_IOSIZE_MAX) 1660 return (EINVAL); 1661 fpop = td->td_fpop; 1662 error = devfs_fp_check(fp, &dev, &dsw, &ref); 1663 if (error) 1664 return (error); 1665 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td)); 1666 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC); 1667 if (ioflag & O_DIRECT) 1668 ioflag |= IO_DIRECT; 1669 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK); 1670 1671 resid = uio->uio_resid; 1672 1673 error = dsw->d_write(dev, uio, ioflag); 1674 if (uio->uio_resid != resid || (error == 0 && resid != 0)) { 1675 vfs_timestamp(&dev->si_ctime); 1676 dev->si_mtime = dev->si_ctime; 1677 } 1678 td->td_fpop = fpop; 1679 dev_relthread(dev, ref); 1680 1681 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF); 1682 return (error); 1683 } 1684 1685 dev_t 1686 dev2udev(struct cdev *x) 1687 { 1688 if (x == NULL) 1689 return (NODEV); 1690 return (cdev2priv(x)->cdp_inode); 1691 } 1692 1693 static struct fileops devfs_ops_f = { 1694 .fo_read = devfs_read_f, 1695 .fo_write = devfs_write_f, 1696 .fo_truncate = devfs_truncate_f, 1697 .fo_ioctl = devfs_ioctl_f, 1698 .fo_poll = devfs_poll_f, 1699 .fo_kqfilter = devfs_kqfilter_f, 1700 .fo_stat = devfs_stat_f, 1701 .fo_close = devfs_close_f, 1702 .fo_chmod = vn_chmod, 1703 .fo_chown = vn_chown, 1704 .fo_sendfile = vn_sendfile, 1705 .fo_seek = vn_seek, 1706 .fo_fill_kinfo = vn_fill_kinfo, 1707 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 1708 }; 1709 1710 static struct vop_vector devfs_vnodeops = { 1711 .vop_default = &default_vnodeops, 1712 1713 .vop_access = devfs_access, 1714 .vop_getattr = devfs_getattr, 1715 .vop_ioctl = devfs_rioctl, 1716 .vop_lookup = devfs_lookup, 1717 .vop_mknod = devfs_mknod, 1718 .vop_pathconf = devfs_pathconf, 1719 .vop_read = devfs_rread, 1720 .vop_readdir = devfs_readdir, 1721 .vop_readlink = devfs_readlink, 1722 .vop_reclaim = devfs_reclaim, 1723 .vop_remove = devfs_remove, 1724 .vop_revoke = devfs_revoke, 1725 .vop_setattr = devfs_setattr, 1726 #ifdef MAC 1727 .vop_setlabel = devfs_setlabel, 1728 #endif 1729 .vop_symlink = devfs_symlink, 1730 .vop_vptocnp = devfs_vptocnp, 1731 }; 1732 1733 static struct vop_vector devfs_specops = { 1734 .vop_default = &default_vnodeops, 1735 1736 .vop_access = devfs_access, 1737 .vop_bmap = VOP_PANIC, 1738 .vop_close = devfs_close, 1739 .vop_create = VOP_PANIC, 1740 .vop_fsync = devfs_fsync, 1741 .vop_getattr = devfs_getattr, 1742 .vop_link = VOP_PANIC, 1743 .vop_mkdir = VOP_PANIC, 1744 .vop_mknod = VOP_PANIC, 1745 .vop_open = devfs_open, 1746 .vop_pathconf = devfs_pathconf, 1747 .vop_print = devfs_print, 1748 .vop_read = VOP_PANIC, 1749 .vop_readdir = VOP_PANIC, 1750 .vop_readlink = VOP_PANIC, 1751 .vop_reallocblks = VOP_PANIC, 1752 .vop_reclaim = devfs_reclaim, 1753 .vop_remove = devfs_remove, 1754 .vop_rename = VOP_PANIC, 1755 .vop_revoke = devfs_revoke, 1756 .vop_rmdir = VOP_PANIC, 1757 .vop_setattr = devfs_setattr, 1758 #ifdef MAC 1759 .vop_setlabel = devfs_setlabel, 1760 #endif 1761 .vop_strategy = VOP_PANIC, 1762 .vop_symlink = VOP_PANIC, 1763 .vop_vptocnp = devfs_vptocnp, 1764 .vop_write = VOP_PANIC, 1765 }; 1766 1767 /* 1768 * Our calling convention to the device drivers used to be that we passed 1769 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 1770 * flags instead since that's what open(), close() and ioctl() takes and 1771 * we don't really want vnode.h in device drivers. 1772 * We solved the source compatibility by redefining some vnode flags to 1773 * be the same as the fcntl ones and by sending down the bitwise OR of 1774 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody 1775 * pulls the rug out under this. 1776 */ 1777 CTASSERT(O_NONBLOCK == IO_NDELAY); 1778 CTASSERT(O_FSYNC == IO_SYNC); 1779