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.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
32 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/dirent.h>
46 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
61 #include <sys/ttycom.h>
62 #include <sys/unistd.h>
63 #include <sys/vnode.h>
65 static struct vop_vector devfs_vnodeops;
66 static struct vop_vector devfs_specops;
67 static struct fileops devfs_ops_f;
69 #include <fs/devfs/devfs.h>
70 #include <fs/devfs/devfs_int.h>
72 #include <security/mac/mac_framework.h>
74 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
76 struct mtx devfs_de_interlock;
77 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
78 struct sx clone_drain_lock;
79 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
80 struct mtx cdevpriv_mtx;
81 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
84 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
88 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
89 if (*devp != fp->f_data) {
91 dev_relthread(*devp, *ref);
94 KASSERT((*devp)->si_refcount > 0,
95 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
98 curthread->td_fpop = fp;
103 devfs_get_cdevpriv(void **datap)
106 struct cdev_privdata *p;
109 fp = curthread->td_fpop;
115 *datap = p->cdpd_data;
122 devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t priv_dtr)
125 struct cdev_priv *cdp;
126 struct cdev_privdata *p;
129 fp = curthread->td_fpop;
132 cdp = cdev2priv((struct cdev *)fp->f_data);
133 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
135 p->cdpd_dtr = priv_dtr;
137 mtx_lock(&cdevpriv_mtx);
138 if (fp->f_cdevpriv == NULL) {
139 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
141 mtx_unlock(&cdevpriv_mtx);
144 mtx_unlock(&cdevpriv_mtx);
145 free(p, M_CDEVPDATA);
152 devfs_destroy_cdevpriv(struct cdev_privdata *p)
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);
164 devfs_fpdrop(struct file *fp)
166 struct cdev_privdata *p;
168 mtx_lock(&cdevpriv_mtx);
169 if ((p = fp->f_cdevpriv) == NULL) {
170 mtx_unlock(&cdevpriv_mtx);
173 devfs_destroy_cdevpriv(p);
177 devfs_clear_cdevpriv(void)
181 fp = curthread->td_fpop;
188 * On success devfs_populate_vp() returns with dmp->dm_lock held.
191 devfs_populate_vp(struct vnode *vp)
193 struct devfs_dirent *de;
194 struct devfs_mount *dmp;
197 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
199 dmp = VFSTODEVFS(vp->v_mount);
200 locked = VOP_ISLOCKED(vp);
202 sx_xlock(&dmp->dm_lock);
205 /* Can't call devfs_populate() with the vnode lock held. */
209 sx_xunlock(&dmp->dm_lock);
210 vn_lock(vp, locked | LK_RETRY);
211 sx_xlock(&dmp->dm_lock);
212 if (DEVFS_DMP_DROP(dmp)) {
213 sx_xunlock(&dmp->dm_lock);
214 devfs_unmount_final(dmp);
217 if ((vp->v_iflag & VI_DOOMED) != 0) {
218 sx_xunlock(&dmp->dm_lock);
223 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
224 if ((de->de_flags & DE_DOOMED) != 0) {
225 sx_xunlock(&dmp->dm_lock);
233 devfs_vptocnp(struct vop_vptocnp_args *ap)
235 struct vnode *vp = ap->a_vp;
236 struct vnode **dvp = ap->a_vpp;
237 struct devfs_mount *dmp;
238 char *buf = ap->a_buf;
239 int *buflen = ap->a_buflen;
240 struct devfs_dirent *dd, *de;
243 dmp = VFSTODEVFS(vp->v_mount);
245 error = devfs_populate_vp(vp);
252 if (vp->v_type == VCHR) {
253 i -= strlen(dd->de_cdp->cdp_c.si_name);
258 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
259 strlen(dd->de_cdp->cdp_c.si_name));
261 } else if (vp->v_type == VDIR) {
262 if (dd == dmp->dm_rootdir) {
267 i -= dd->de_dirent->d_namlen;
272 bcopy(dd->de_dirent->d_name, buf + i,
273 dd->de_dirent->d_namlen);
280 de = devfs_parent_dirent(de);
285 mtx_lock(&devfs_de_interlock);
289 mtx_unlock(&devfs_de_interlock);
295 mtx_unlock(&devfs_de_interlock);
299 sx_xunlock(&dmp->dm_lock);
304 * Construct the fully qualified path name relative to the mountpoint.
305 * If a NULL cnp is provided, no '/' is appended to the resulting path.
308 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
309 struct componentname *cnp)
312 struct devfs_dirent *de;
314 sx_assert(&dmp->dm_lock, SA_LOCKED);
319 i -= cnp->cn_namelen;
323 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
325 while (de != dmp->dm_rootdir) {
326 if (cnp != NULL || i < SPECNAMELEN) {
332 i -= de->de_dirent->d_namlen;
335 bcopy(de->de_dirent->d_name, buf + i,
336 de->de_dirent->d_namlen);
337 de = devfs_parent_dirent(de);
345 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
346 struct devfs_dirent *de)
351 if (de->de_flags & DE_DOOMED)
353 if (DEVFS_DE_DROP(de)) {
354 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
355 devfs_dirent_free(de);
357 if (DEVFS_DMP_DROP(dmp)) {
358 KASSERT(not_found == 1,
359 ("DEVFS mount struct freed before dirent"));
361 sx_xunlock(&dmp->dm_lock);
362 devfs_unmount_final(dmp);
364 if (not_found == 1 || (drop_dm_lock && not_found != 2))
365 sx_unlock(&dmp->dm_lock);
370 devfs_insmntque_dtr(struct vnode *vp, void *arg)
372 struct devfs_dirent *de;
374 de = (struct devfs_dirent *)arg;
375 mtx_lock(&devfs_de_interlock);
378 mtx_unlock(&devfs_de_interlock);
384 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
388 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
394 struct devfs_mount *dmp;
397 dmp = VFSTODEVFS(mp);
398 if (de->de_flags & DE_DOOMED) {
399 sx_xunlock(&dmp->dm_lock);
405 mtx_lock(&devfs_de_interlock);
409 mtx_unlock(&devfs_de_interlock);
410 sx_xunlock(&dmp->dm_lock);
411 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
412 sx_xlock(&dmp->dm_lock);
413 if (devfs_allocv_drop_refs(0, dmp, de)) {
417 else if ((vp->v_iflag & VI_DOOMED) != 0) {
418 mtx_lock(&devfs_de_interlock);
419 if (de->de_vnode == vp) {
423 mtx_unlock(&devfs_de_interlock);
427 sx_xunlock(&dmp->dm_lock);
431 mtx_unlock(&devfs_de_interlock);
432 if (de->de_dirent->d_type == DT_CHR) {
433 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
434 devfs_allocv_drop_refs(1, dmp, de);
437 dev = &de->de_cdp->cdp_c;
441 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
443 devfs_allocv_drop_refs(1, dmp, de);
444 printf("devfs_allocv: failed to allocate new vnode\n");
448 if (de->de_dirent->d_type == DT_CHR) {
453 /* XXX: v_rdev should be protect by vnode lock */
455 KASSERT(vp->v_usecount == 1,
456 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
457 dev->si_usecount += vp->v_usecount;
458 /* Special casing of ttys for deadfs. Probably redundant. */
460 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
461 vp->v_vflag |= VV_ISTTY;
464 if ((dev->si_flags & SI_ETERNAL) != 0)
465 vp->v_vflag |= VV_ETERNALDEV;
466 vp->v_op = &devfs_specops;
467 } else if (de->de_dirent->d_type == DT_DIR) {
469 } else if (de->de_dirent->d_type == DT_LNK) {
474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
476 mtx_lock(&devfs_de_interlock);
479 mtx_unlock(&devfs_de_interlock);
480 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
482 (void) devfs_allocv_drop_refs(1, dmp, de);
485 if (devfs_allocv_drop_refs(0, dmp, de)) {
490 mac_devfs_vnode_associate(mp, de, vp);
492 sx_xunlock(&dmp->dm_lock);
498 devfs_access(struct vop_access_args *ap)
500 struct vnode *vp = ap->a_vp;
501 struct devfs_dirent *de;
505 if (vp->v_type == VDIR)
508 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
509 ap->a_accmode, ap->a_cred, NULL);
514 /* We do, however, allow access to the controlling terminal */
515 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
517 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
524 devfs_close(struct vop_close_args *ap)
526 struct vnode *vp = ap->a_vp, *oldvp;
527 struct thread *td = ap->a_td;
528 struct cdev *dev = vp->v_rdev;
530 int vp_locked, error, ref;
533 * XXX: Don't call d_close() if we were called because of
534 * XXX: insmntque1() failure.
536 if (vp->v_data == NULL)
540 * Hack: a tty device that is a controlling terminal
541 * has a reference from the session structure.
542 * We cannot easily tell that a character device is
543 * a controlling terminal, unless it is the closing
544 * process' controlling terminal. In that case,
545 * if the reference count is 2 (this last descriptor
546 * plus the session), release the reference from the session.
549 sx_xlock(&proctree_lock);
550 if (td && vp == td->td_proc->p_session->s_ttyvp) {
551 SESS_LOCK(td->td_proc->p_session);
553 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
554 td->td_proc->p_session->s_ttyvp = NULL;
555 td->td_proc->p_session->s_ttydp = NULL;
559 SESS_UNLOCK(td->td_proc->p_session);
561 sx_xunlock(&proctree_lock);
565 * We do not want to really close the device if it
566 * is still in use unless we are trying to close it
567 * forcibly. Since every use (buffer, vnode, swap, cmap)
568 * holds a reference to the vnode, and because we mark
569 * any other vnodes that alias this device, when the
570 * sum of the reference counts on all the aliased
571 * vnodes descends to one, we are on last close.
573 dsw = dev_refthread(dev, &ref);
577 if (vp->v_iflag & VI_DOOMED) {
579 } else if (dsw->d_flags & D_TRACKCLOSE) {
580 /* Keep device updated on status. */
581 } else if (count_dev(dev) > 1) {
583 dev_relthread(dev, ref);
588 vp_locked = VOP_ISLOCKED(vp);
590 KASSERT(dev->si_refcount > 0,
591 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
592 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
593 dev_relthread(dev, ref);
594 vn_lock(vp, vp_locked | LK_RETRY);
600 devfs_close_f(struct file *fp, struct thread *td)
606 * NB: td may be NULL if this descriptor is closed due to
607 * garbage collection from a closed UNIX domain socket.
609 fpop = curthread->td_fpop;
610 curthread->td_fpop = fp;
611 error = vnops.fo_close(fp, td);
612 curthread->td_fpop = fpop;
615 * The f_cdevpriv cannot be assigned non-NULL value while we
616 * are destroying the file.
618 if (fp->f_cdevpriv != NULL)
624 devfs_fsync(struct vop_fsync_args *ap)
628 struct devfs_dirent *de;
630 if (!vn_isdisk(ap->a_vp, &error)) {
631 bo = &ap->a_vp->v_bufobj;
632 de = ap->a_vp->v_data;
633 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
634 printf("Device %s went missing before all of the data "
635 "could be written to it; expect data loss.\n",
636 de->de_dirent->d_name);
638 error = vop_stdfsync(ap);
639 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
640 panic("devfs_fsync: vop_stdfsync failed.");
646 return (vop_stdfsync(ap));
650 devfs_getattr(struct vop_getattr_args *ap)
652 struct vnode *vp = ap->a_vp;
653 struct vattr *vap = ap->a_vap;
655 struct devfs_dirent *de;
656 struct devfs_mount *dmp;
659 error = devfs_populate_vp(vp);
663 dmp = VFSTODEVFS(vp->v_mount);
664 sx_xunlock(&dmp->dm_lock);
667 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
668 if (vp->v_type == VDIR) {
671 ("Null dir dirent in devfs_getattr vp=%p", vp));
673 vap->va_uid = de->de_uid;
674 vap->va_gid = de->de_gid;
675 vap->va_mode = de->de_mode;
676 if (vp->v_type == VLNK)
677 vap->va_size = strlen(de->de_symlink);
678 else if (vp->v_type == VDIR)
679 vap->va_size = vap->va_bytes = DEV_BSIZE;
682 if (vp->v_type != VDIR)
684 vap->va_blocksize = DEV_BSIZE;
685 vap->va_type = vp->v_type;
689 if ((aa).tv_sec <= 3600) { \
690 (aa).tv_sec = boottime.tv_sec; \
691 (aa).tv_nsec = boottime.tv_usec * 1000; \
695 if (vp->v_type != VCHR) {
697 vap->va_atime = de->de_atime;
699 vap->va_mtime = de->de_mtime;
701 vap->va_ctime = de->de_ctime;
705 vap->va_atime = dev->si_atime;
707 vap->va_mtime = dev->si_mtime;
709 vap->va_ctime = dev->si_ctime;
711 vap->va_rdev = cdev2priv(dev)->cdp_inode;
716 vap->va_nlink = de->de_links;
717 vap->va_fileid = de->de_inode;
724 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
732 struct fiodgname_arg *fgn;
736 error = devfs_fp_check(fp, &dev, &dsw, &ref);
740 if (com == FIODTYPE) {
741 *(int *)data = dsw->d_flags & D_TYPEMASK;
743 dev_relthread(dev, ref);
745 } else if (com == FIODGNAME) {
752 error = copyout(p, fgn->buf, i);
754 dev_relthread(dev, ref);
757 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
759 dev_relthread(dev, ref);
760 if (error == ENOIOCTL)
762 if (error == 0 && com == TIOCSCTTY) {
765 /* Do nothing if reassigning same control tty */
766 sx_slock(&proctree_lock);
767 if (td->td_proc->p_session->s_ttyvp == vp) {
768 sx_sunlock(&proctree_lock);
772 vpold = td->td_proc->p_session->s_ttyvp;
774 SESS_LOCK(td->td_proc->p_session);
775 td->td_proc->p_session->s_ttyvp = vp;
776 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
777 SESS_UNLOCK(td->td_proc->p_session);
779 sx_sunlock(&proctree_lock);
781 /* Get rid of reference to old control tty */
790 devfs_kqfilter_f(struct file *fp, struct knote *kn)
800 error = devfs_fp_check(fp, &dev, &dsw, &ref);
803 error = dsw->d_kqfilter(dev, kn);
805 dev_relthread(dev, ref);
810 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
812 struct cdev_priv *cdp;
819 dcr = cdp->cdp_c.si_cred;
823 error = prison_check(td->td_ucred, dcr);
826 /* We do, however, allow access to the controlling terminal */
827 if (!(td->td_proc->p_flag & P_CONTROLT))
829 if (td->td_proc->p_session->s_ttydp == cdp)
835 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
837 struct componentname *cnp;
838 struct vnode *dvp, **vpp;
840 struct devfs_dirent *de, *dd;
841 struct devfs_dirent **dde;
842 struct devfs_mount *dmp;
844 int error, flags, nameiop, dvplocked;
845 char specname[SPECNAMELEN + 1], *pname;
850 pname = cnp->cn_nameptr;
852 flags = cnp->cn_flags;
853 nameiop = cnp->cn_nameiop;
854 dmp = VFSTODEVFS(dvp->v_mount);
858 if ((flags & ISLASTCN) && nameiop == RENAME)
861 if (dvp->v_type != VDIR)
864 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
867 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
871 if (cnp->cn_namelen == 1 && *pname == '.') {
872 if ((flags & ISLASTCN) && nameiop != LOOKUP)
879 if (flags & ISDOTDOT) {
880 if ((flags & ISLASTCN) && nameiop != LOOKUP)
882 de = devfs_parent_dirent(dd);
885 dvplocked = VOP_ISLOCKED(dvp);
887 error = devfs_allocv(de, dvp->v_mount,
888 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
890 vn_lock(dvp, dvplocked | LK_RETRY);
895 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
896 while (de == NULL) { /* While(...) so we can use break */
898 if (nameiop == DELETE)
902 * OK, we didn't have an entry for the name we were asked for
903 * so we try to see if anybody can create it on demand.
905 pname = devfs_fqpn(specname, dmp, dd, cnp);
911 sx_xunlock(&dmp->dm_lock);
912 sx_slock(&clone_drain_lock);
913 EVENTHANDLER_INVOKE(dev_clone,
914 td->td_ucred, pname, strlen(pname), &cdev);
915 sx_sunlock(&clone_drain_lock);
918 sx_xlock(&dmp->dm_lock);
919 else if (devfs_populate_vp(dvp) != 0) {
921 sx_xlock(&dmp->dm_lock);
922 if (DEVFS_DMP_DROP(dmp)) {
923 sx_xunlock(&dmp->dm_lock);
924 devfs_unmount_final(dmp);
926 sx_xunlock(&dmp->dm_lock);
930 if (DEVFS_DMP_DROP(dmp)) {
932 sx_xunlock(&dmp->dm_lock);
933 devfs_unmount_final(dmp);
943 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
944 if (dde != NULL && *dde != NULL)
951 if (de == NULL || de->de_flags & DE_WHITEOUT) {
952 if ((nameiop == CREATE || nameiop == RENAME) &&
953 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
954 cnp->cn_flags |= SAVENAME;
955 return (EJUSTRETURN);
960 if (devfs_prison_check(de, td))
963 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
964 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
973 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
980 devfs_lookup(struct vop_lookup_args *ap)
983 struct devfs_mount *dmp;
986 if (devfs_populate_vp(ap->a_dvp) != 0)
989 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
991 j = devfs_lookupx(ap, &dm_unlock);
993 sx_xunlock(&dmp->dm_lock);
998 devfs_mknod(struct vop_mknod_args *ap)
1000 struct componentname *cnp;
1001 struct vnode *dvp, **vpp;
1002 struct devfs_dirent *dd, *de;
1003 struct devfs_mount *dmp;
1007 * The only type of node we should be creating here is a
1008 * character device, for anything else return EOPNOTSUPP.
1010 if (ap->a_vap->va_type != VCHR)
1011 return (EOPNOTSUPP);
1013 dmp = VFSTODEVFS(dvp->v_mount);
1020 sx_xlock(&dmp->dm_lock);
1021 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1022 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1024 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1025 de->de_dirent->d_namlen) != 0)
1027 if (de->de_flags & DE_WHITEOUT)
1033 de->de_flags &= ~DE_WHITEOUT;
1034 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1037 sx_xunlock(&dmp->dm_lock);
1043 devfs_open(struct vop_open_args *ap)
1045 struct thread *td = ap->a_td;
1046 struct vnode *vp = ap->a_vp;
1047 struct cdev *dev = vp->v_rdev;
1048 struct file *fp = ap->a_fp;
1049 int error, ref, vlocked;
1054 if (vp->v_type == VBLK)
1060 /* Make this field valid before any I/O in d_open. */
1061 if (dev->si_iosize_max == 0)
1062 dev->si_iosize_max = DFLTPHYS;
1064 dsw = dev_refthread(dev, &ref);
1067 if (fp == NULL && dsw->d_fdopen != NULL) {
1068 dev_relthread(dev, ref);
1072 vlocked = VOP_ISLOCKED(vp);
1081 if (dsw->d_fdopen != NULL)
1082 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1084 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1085 /* cleanup any cdevpriv upon error */
1087 devfs_clear_cdevpriv();
1090 vn_lock(vp, vlocked | LK_RETRY);
1091 dev_relthread(dev, ref);
1093 if (error == ERESTART)
1098 #if 0 /* /dev/console */
1099 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1104 if (fp->f_ops == &badfileops)
1105 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1106 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1109 * Hint to the dofilewrite() to not force the buffer draining
1110 * on the writer to the file. Most likely, the write would
1111 * not need normal buffers.
1114 fp->f_vnread_flags |= FDEVFS_VNODE;
1120 devfs_pathconf(struct vop_pathconf_args *ap)
1123 switch (ap->a_name) {
1124 case _PC_MAC_PRESENT:
1127 * If MAC is enabled, devfs automatically supports
1128 * trivial non-persistant label storage.
1136 return (vop_stdpathconf(ap));
1143 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1151 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1153 return (poll_no_poll(events));
1154 error = dsw->d_poll(dev, events, td);
1156 dev_relthread(dev, ref);
1161 * Print out the contents of a special device vnode.
1164 devfs_print(struct vop_print_args *ap)
1167 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1172 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1173 int flags, struct thread *td)
1176 int ioflag, error, ref;
1181 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1184 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1187 resid = uio->uio_resid;
1188 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1189 if (ioflag & O_DIRECT)
1190 ioflag |= IO_DIRECT;
1192 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1193 error = dsw->d_read(dev, uio, ioflag);
1194 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1195 vfs_timestamp(&dev->si_atime);
1197 dev_relthread(dev, ref);
1199 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1204 devfs_readdir(struct vop_readdir_args *ap)
1209 struct devfs_dirent *dd;
1210 struct devfs_dirent *de;
1211 struct devfs_mount *dmp;
1213 int *tmp_ncookies = NULL;
1215 if (ap->a_vp->v_type != VDIR)
1219 if (uio->uio_offset < 0)
1223 * XXX: This is a temporary hack to get around this filesystem not
1224 * supporting cookies. We store the location of the ncookies pointer
1225 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1226 * and set the number of cookies to 0. We then set the pointer to
1227 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1228 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1229 * pointer to its original location before returning to the caller.
1231 if (ap->a_ncookies != NULL) {
1232 tmp_ncookies = ap->a_ncookies;
1233 *ap->a_ncookies = 0;
1234 ap->a_ncookies = NULL;
1237 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1238 if (devfs_populate_vp(ap->a_vp) != 0) {
1239 if (tmp_ncookies != NULL)
1240 ap->a_ncookies = tmp_ncookies;
1244 de = ap->a_vp->v_data;
1246 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1247 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1248 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1250 if (devfs_prison_check(dd, uio->uio_td))
1252 if (dd->de_dirent->d_type == DT_DIR)
1257 if (dp->d_reclen > uio->uio_resid)
1259 dp->d_fileno = de->de_inode;
1260 if (off >= uio->uio_offset) {
1261 error = vfs_read_dirent(ap, dp, off);
1265 off += dp->d_reclen;
1267 sx_xunlock(&dmp->dm_lock);
1268 uio->uio_offset = off;
1271 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1274 if (tmp_ncookies != NULL)
1275 ap->a_ncookies = tmp_ncookies;
1281 devfs_readlink(struct vop_readlink_args *ap)
1283 struct devfs_dirent *de;
1285 de = ap->a_vp->v_data;
1286 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1290 devfs_reclaim(struct vop_reclaim_args *ap)
1292 struct vnode *vp = ap->a_vp;
1293 struct devfs_dirent *de;
1296 mtx_lock(&devfs_de_interlock);
1299 de->de_vnode = NULL;
1302 mtx_unlock(&devfs_de_interlock);
1304 vnode_destroy_vobject(vp);
1317 dev->si_usecount -= vp->v_usecount;
1325 devfs_remove(struct vop_remove_args *ap)
1327 struct vnode *dvp = ap->a_dvp;
1328 struct vnode *vp = ap->a_vp;
1329 struct devfs_dirent *dd;
1330 struct devfs_dirent *de, *de_covered;
1331 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1333 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1334 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1336 sx_xlock(&dmp->dm_lock);
1337 dd = ap->a_dvp->v_data;
1339 if (de->de_cdp == NULL) {
1340 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1341 if (de->de_dirent->d_type == DT_LNK) {
1342 de_covered = devfs_find(dd, de->de_dirent->d_name,
1343 de->de_dirent->d_namlen, 0);
1344 if (de_covered != NULL)
1345 de_covered->de_flags &= ~DE_COVERED;
1347 /* We need to unlock dvp because devfs_delete() may lock it. */
1351 devfs_delete(dmp, de, 0);
1352 sx_xunlock(&dmp->dm_lock);
1354 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1357 de->de_flags |= DE_WHITEOUT;
1358 sx_xunlock(&dmp->dm_lock);
1364 * Revoke is called on a tty when a terminal session ends. The vnode
1365 * is orphaned by setting v_op to deadfs so we need to let go of it
1366 * as well so that we create a new one next time around.
1370 devfs_revoke(struct vop_revoke_args *ap)
1372 struct vnode *vp = ap->a_vp, *vp2;
1374 struct cdev_priv *cdp;
1375 struct devfs_dirent *de;
1378 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1381 cdp = cdev2priv(dev);
1394 mtx_lock(&devfs_de_interlock);
1397 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1398 de = cdp->cdp_dirents[i];
1406 mtx_unlock(&devfs_de_interlock);
1407 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1421 mtx_unlock(&devfs_de_interlock);
1426 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1427 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1429 dev_rel(&cdp->cdp_c);
1433 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1438 devfs_rioctl(struct vop_ioctl_args *ap)
1441 struct devfs_mount *dmp;
1445 vn_lock(vp, LK_SHARED | LK_RETRY);
1446 if (vp->v_iflag & VI_DOOMED) {
1450 dmp = VFSTODEVFS(vp->v_mount);
1451 sx_xlock(&dmp->dm_lock);
1453 DEVFS_DMP_HOLD(dmp);
1454 devfs_populate(dmp);
1455 if (DEVFS_DMP_DROP(dmp)) {
1456 sx_xunlock(&dmp->dm_lock);
1457 devfs_unmount_final(dmp);
1460 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1461 sx_xunlock(&dmp->dm_lock);
1466 devfs_rread(struct vop_read_args *ap)
1469 if (ap->a_vp->v_type != VDIR)
1471 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1475 devfs_setattr(struct vop_setattr_args *ap)
1477 struct devfs_dirent *de;
1488 if ((vap->va_type != VNON) ||
1489 (vap->va_nlink != VNOVAL) ||
1490 (vap->va_fsid != VNOVAL) ||
1491 (vap->va_fileid != VNOVAL) ||
1492 (vap->va_blocksize != VNOVAL) ||
1493 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1494 (vap->va_rdev != VNOVAL) ||
1495 ((int)vap->va_bytes != VNOVAL) ||
1496 (vap->va_gen != VNOVAL)) {
1501 if (vp->v_type == VDIR)
1505 if (vap->va_uid == (uid_t)VNOVAL)
1509 if (vap->va_gid == (gid_t)VNOVAL)
1513 if (uid != de->de_uid || gid != de->de_gid) {
1514 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1515 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1516 error = priv_check(td, PRIV_VFS_CHOWN);
1525 if (vap->va_mode != (mode_t)VNOVAL) {
1526 if (ap->a_cred->cr_uid != de->de_uid) {
1527 error = priv_check(td, PRIV_VFS_ADMIN);
1531 de->de_mode = vap->va_mode;
1535 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1536 /* See the comment in ufs_vnops::ufs_setattr(). */
1537 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1538 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1539 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1541 if (vap->va_atime.tv_sec != VNOVAL) {
1542 if (vp->v_type == VCHR)
1543 vp->v_rdev->si_atime = vap->va_atime;
1545 de->de_atime = vap->va_atime;
1547 if (vap->va_mtime.tv_sec != VNOVAL) {
1548 if (vp->v_type == VCHR)
1549 vp->v_rdev->si_mtime = vap->va_mtime;
1551 de->de_mtime = vap->va_mtime;
1557 if (vp->v_type == VCHR)
1558 vfs_timestamp(&vp->v_rdev->si_ctime);
1560 vfs_timestamp(&de->de_mtime);
1567 devfs_setlabel(struct vop_setlabel_args *ap)
1570 struct devfs_dirent *de;
1575 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1576 mac_devfs_update(vp->v_mount, de, vp);
1583 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1586 return (vnops.fo_stat(fp, sb, cred, td));
1590 devfs_symlink(struct vop_symlink_args *ap)
1593 struct devfs_dirent *dd;
1594 struct devfs_dirent *de, *de_covered, *de_dotdot;
1595 struct devfs_mount *dmp;
1597 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1600 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1601 if (devfs_populate_vp(ap->a_dvp) != 0)
1604 dd = ap->a_dvp->v_data;
1605 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1606 de->de_flags = DE_USER;
1610 de->de_inode = alloc_unr(devfs_inos);
1612 de->de_dirent->d_type = DT_LNK;
1613 i = strlen(ap->a_target) + 1;
1614 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1615 bcopy(ap->a_target, de->de_symlink, i);
1617 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1619 de_covered = devfs_find(dd, de->de_dirent->d_name,
1620 de->de_dirent->d_namlen, 0);
1621 if (de_covered != NULL) {
1622 if ((de_covered->de_flags & DE_USER) != 0) {
1623 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1624 sx_xunlock(&dmp->dm_lock);
1627 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1628 ("devfs_symlink: entry %p already covered", de_covered));
1629 de_covered->de_flags |= DE_COVERED;
1632 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1633 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1634 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1635 devfs_dir_ref_de(dmp, dd);
1636 devfs_rules_apply(dmp, de);
1638 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1642 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1645 return (vnops.fo_truncate(fp, length, cred, td));
1649 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1650 int flags, struct thread *td)
1653 int error, ioflag, ref;
1658 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1661 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1664 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1665 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1666 if (ioflag & O_DIRECT)
1667 ioflag |= IO_DIRECT;
1668 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1670 resid = uio->uio_resid;
1672 error = dsw->d_write(dev, uio, ioflag);
1673 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1674 vfs_timestamp(&dev->si_ctime);
1675 dev->si_mtime = dev->si_ctime;
1678 dev_relthread(dev, ref);
1680 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1685 dev2udev(struct cdev *x)
1689 return (cdev2priv(x)->cdp_inode);
1692 static struct fileops devfs_ops_f = {
1693 .fo_read = devfs_read_f,
1694 .fo_write = devfs_write_f,
1695 .fo_truncate = devfs_truncate_f,
1696 .fo_ioctl = devfs_ioctl_f,
1697 .fo_poll = devfs_poll_f,
1698 .fo_kqfilter = devfs_kqfilter_f,
1699 .fo_stat = devfs_stat_f,
1700 .fo_close = devfs_close_f,
1701 .fo_chmod = vn_chmod,
1702 .fo_chown = vn_chown,
1703 .fo_sendfile = vn_sendfile,
1705 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1708 static struct vop_vector devfs_vnodeops = {
1709 .vop_default = &default_vnodeops,
1711 .vop_access = devfs_access,
1712 .vop_getattr = devfs_getattr,
1713 .vop_ioctl = devfs_rioctl,
1714 .vop_lookup = devfs_lookup,
1715 .vop_mknod = devfs_mknod,
1716 .vop_pathconf = devfs_pathconf,
1717 .vop_read = devfs_rread,
1718 .vop_readdir = devfs_readdir,
1719 .vop_readlink = devfs_readlink,
1720 .vop_reclaim = devfs_reclaim,
1721 .vop_remove = devfs_remove,
1722 .vop_revoke = devfs_revoke,
1723 .vop_setattr = devfs_setattr,
1725 .vop_setlabel = devfs_setlabel,
1727 .vop_symlink = devfs_symlink,
1728 .vop_vptocnp = devfs_vptocnp,
1731 static struct vop_vector devfs_specops = {
1732 .vop_default = &default_vnodeops,
1734 .vop_access = devfs_access,
1735 .vop_bmap = VOP_PANIC,
1736 .vop_close = devfs_close,
1737 .vop_create = VOP_PANIC,
1738 .vop_fsync = devfs_fsync,
1739 .vop_getattr = devfs_getattr,
1740 .vop_link = VOP_PANIC,
1741 .vop_mkdir = VOP_PANIC,
1742 .vop_mknod = VOP_PANIC,
1743 .vop_open = devfs_open,
1744 .vop_pathconf = devfs_pathconf,
1745 .vop_print = devfs_print,
1746 .vop_read = VOP_PANIC,
1747 .vop_readdir = VOP_PANIC,
1748 .vop_readlink = VOP_PANIC,
1749 .vop_reallocblks = VOP_PANIC,
1750 .vop_reclaim = devfs_reclaim,
1751 .vop_remove = devfs_remove,
1752 .vop_rename = VOP_PANIC,
1753 .vop_revoke = devfs_revoke,
1754 .vop_rmdir = VOP_PANIC,
1755 .vop_setattr = devfs_setattr,
1757 .vop_setlabel = devfs_setlabel,
1759 .vop_strategy = VOP_PANIC,
1760 .vop_symlink = VOP_PANIC,
1761 .vop_vptocnp = devfs_vptocnp,
1762 .vop_write = VOP_PANIC,
1766 * Our calling convention to the device drivers used to be that we passed
1767 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1768 * flags instead since that's what open(), close() and ioctl() takes and
1769 * we don't really want vnode.h in device drivers.
1770 * We solved the source compatibility by redefining some vnode flags to
1771 * be the same as the fcntl ones and by sending down the bitwise OR of
1772 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1773 * pulls the rug out under this.
1775 CTASSERT(O_NONBLOCK == IO_NDELAY);
1776 CTASSERT(O_FSYNC == IO_SYNC);