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