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