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