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