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