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