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