2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2004
5 * Poul-Henning Kamp. All rights reserved.
6 * Copyright (c) 1989, 1992-1993, 1995
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software donated to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/eventhandler.h>
45 #include <sys/fcntl.h>
47 #include <sys/filedesc.h>
48 #include <sys/filio.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
53 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
61 #include <sys/sysctl.h>
63 #include <sys/ttycom.h>
64 #include <sys/unistd.h>
65 #include <sys/vnode.h>
67 static struct vop_vector devfs_vnodeops;
68 static struct vop_vector devfs_specops;
69 static struct fileops devfs_ops_f;
71 #include <fs/devfs/devfs.h>
72 #include <fs/devfs/devfs_int.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_object.h>
80 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
82 struct mtx devfs_de_interlock;
83 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
84 struct mtx cdevpriv_mtx;
85 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
87 SYSCTL_DECL(_vfs_devfs);
89 static int devfs_dotimes;
90 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
91 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
94 * Update devfs node timestamp. Note that updates are unlocked and
95 * stat(2) could see partially updated times.
98 devfs_timestamp(struct timespec *tsp)
106 if (tsp->tv_sec != ts) {
114 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
117 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
118 if (*dswp == NULL || *devp != fp->f_data) {
120 dev_relthread(*devp, *ref);
123 KASSERT((*devp)->si_refcount > 0,
124 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
127 curthread->td_fpop = fp;
132 devfs_get_cdevpriv(void **datap)
135 struct cdev_privdata *p;
138 fp = curthread->td_fpop;
144 *datap = p->cdpd_data;
151 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
154 struct cdev_priv *cdp;
155 struct cdev_privdata *p;
158 fp = curthread->td_fpop;
161 cdp = cdev2priv((struct cdev *)fp->f_data);
162 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
164 p->cdpd_dtr = priv_dtr;
166 mtx_lock(&cdevpriv_mtx);
167 if (fp->f_cdevpriv == NULL) {
168 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
170 mtx_unlock(&cdevpriv_mtx);
173 mtx_unlock(&cdevpriv_mtx);
174 free(p, M_CDEVPDATA);
181 devfs_foreach_cdevpriv(struct cdev *dev, int (*cb)(void *data, void *arg),
184 struct cdev_priv *cdp;
185 struct cdev_privdata *p;
188 cdp = cdev2priv(dev);
190 mtx_lock(&cdevpriv_mtx);
191 LIST_FOREACH(p, &cdp->cdp_fdpriv, cdpd_list) {
192 error = cb(p->cdpd_data, arg);
196 mtx_unlock(&cdevpriv_mtx);
201 devfs_destroy_cdevpriv(struct cdev_privdata *p)
204 mtx_assert(&cdevpriv_mtx, MA_OWNED);
205 KASSERT(p->cdpd_fp->f_cdevpriv == p,
206 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
207 p->cdpd_fp->f_cdevpriv = NULL;
208 LIST_REMOVE(p, cdpd_list);
209 mtx_unlock(&cdevpriv_mtx);
210 (p->cdpd_dtr)(p->cdpd_data);
211 free(p, M_CDEVPDATA);
215 devfs_fpdrop(struct file *fp)
217 struct cdev_privdata *p;
219 mtx_lock(&cdevpriv_mtx);
220 if ((p = fp->f_cdevpriv) == NULL) {
221 mtx_unlock(&cdevpriv_mtx);
224 devfs_destroy_cdevpriv(p);
228 devfs_clear_cdevpriv(void)
232 fp = curthread->td_fpop;
239 devfs_usecount_add(struct vnode *vp)
241 struct devfs_dirent *de;
244 mtx_lock(&devfs_de_interlock);
246 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
247 if (VN_IS_DOOMED(vp)) {
259 mtx_unlock(&devfs_de_interlock);
263 devfs_usecount_subl(struct vnode *vp)
265 struct devfs_dirent *de;
268 mtx_assert(&devfs_de_interlock, MA_OWNED);
269 ASSERT_VI_LOCKED(vp, __func__);
270 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
277 MPASS(de->de_usecount == 0);
280 if (dev->si_usecount < de->de_usecount)
281 panic("%s: si_usecount underflow for dev %p "
282 "(has %ld, dirent has %d)\n",
283 __func__, dev, dev->si_usecount, de->de_usecount);
284 if (VN_IS_DOOMED(vp)) {
285 dev->si_usecount -= de->de_usecount;
288 if (de->de_usecount == 0)
289 panic("%s: de_usecount underflow for dev %p\n",
297 devfs_usecount_sub(struct vnode *vp)
300 mtx_lock(&devfs_de_interlock);
302 devfs_usecount_subl(vp);
304 mtx_unlock(&devfs_de_interlock);
308 devfs_usecountl(struct vnode *vp)
311 VNPASS(vp->v_type == VCHR, vp);
312 mtx_assert(&devfs_de_interlock, MA_OWNED);
313 ASSERT_VI_LOCKED(vp, __func__);
314 return (vp->v_rdev->si_usecount);
318 devfs_usecount(struct vnode *vp)
322 VNPASS(vp->v_type == VCHR, vp);
323 mtx_lock(&devfs_de_interlock);
325 count = devfs_usecountl(vp);
327 mtx_unlock(&devfs_de_interlock);
332 devfs_ctty_ref(struct vnode *vp)
336 devfs_usecount_add(vp);
340 devfs_ctty_unref(struct vnode *vp)
343 devfs_usecount_sub(vp);
348 * On success devfs_populate_vp() returns with dmp->dm_lock held.
351 devfs_populate_vp(struct vnode *vp)
353 struct devfs_dirent *de;
354 struct devfs_mount *dmp;
357 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
359 dmp = VFSTODEVFS(vp->v_mount);
360 if (!devfs_populate_needed(dmp)) {
361 sx_xlock(&dmp->dm_lock);
365 locked = VOP_ISLOCKED(vp);
367 sx_xlock(&dmp->dm_lock);
370 /* Can't call devfs_populate() with the vnode lock held. */
374 sx_xunlock(&dmp->dm_lock);
375 vn_lock(vp, locked | LK_RETRY);
376 sx_xlock(&dmp->dm_lock);
377 if (DEVFS_DMP_DROP(dmp)) {
378 sx_xunlock(&dmp->dm_lock);
379 devfs_unmount_final(dmp);
383 if (VN_IS_DOOMED(vp)) {
384 sx_xunlock(&dmp->dm_lock);
389 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
390 if ((de->de_flags & DE_DOOMED) != 0) {
391 sx_xunlock(&dmp->dm_lock);
399 devfs_vptocnp(struct vop_vptocnp_args *ap)
401 struct vnode *vp = ap->a_vp;
402 struct vnode **dvp = ap->a_vpp;
403 struct devfs_mount *dmp;
404 char *buf = ap->a_buf;
405 size_t *buflen = ap->a_buflen;
406 struct devfs_dirent *dd, *de;
409 dmp = VFSTODEVFS(vp->v_mount);
411 error = devfs_populate_vp(vp);
415 if (vp->v_type != VCHR && vp->v_type != VDIR) {
421 if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
428 i -= dd->de_dirent->d_namlen;
433 bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
435 de = devfs_parent_dirent(dd);
440 mtx_lock(&devfs_de_interlock);
444 mtx_unlock(&devfs_de_interlock);
450 mtx_unlock(&devfs_de_interlock);
454 sx_xunlock(&dmp->dm_lock);
459 * Construct the fully qualified path name relative to the mountpoint.
460 * If a NULL cnp is provided, no '/' is appended to the resulting path.
463 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
464 struct componentname *cnp)
467 struct devfs_dirent *de;
469 sx_assert(&dmp->dm_lock, SA_LOCKED);
474 i -= cnp->cn_namelen;
478 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
480 while (de != dmp->dm_rootdir) {
481 if (cnp != NULL || i < SPECNAMELEN) {
487 i -= de->de_dirent->d_namlen;
490 bcopy(de->de_dirent->d_name, buf + i,
491 de->de_dirent->d_namlen);
492 de = devfs_parent_dirent(de);
500 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
501 struct devfs_dirent *de)
506 if (de->de_flags & DE_DOOMED)
508 if (DEVFS_DE_DROP(de)) {
509 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
510 devfs_dirent_free(de);
512 if (DEVFS_DMP_DROP(dmp)) {
513 KASSERT(not_found == 1,
514 ("DEVFS mount struct freed before dirent"));
516 sx_xunlock(&dmp->dm_lock);
517 devfs_unmount_final(dmp);
519 if (not_found == 1 || (drop_dm_lock && not_found != 2))
520 sx_unlock(&dmp->dm_lock);
525 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
529 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
535 struct devfs_mount *dmp;
539 dmp = VFSTODEVFS(mp);
540 if (de->de_flags & DE_DOOMED) {
541 sx_xunlock(&dmp->dm_lock);
547 mtx_lock(&devfs_de_interlock);
551 mtx_unlock(&devfs_de_interlock);
552 sx_xunlock(&dmp->dm_lock);
553 vget_finish(vp, lockmode | LK_RETRY, vs);
554 sx_xlock(&dmp->dm_lock);
555 if (devfs_allocv_drop_refs(0, dmp, de)) {
559 else if (VN_IS_DOOMED(vp)) {
560 mtx_lock(&devfs_de_interlock);
561 if (de->de_vnode == vp) {
565 mtx_unlock(&devfs_de_interlock);
569 sx_xunlock(&dmp->dm_lock);
573 mtx_unlock(&devfs_de_interlock);
574 if (de->de_dirent->d_type == DT_CHR) {
575 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
576 devfs_allocv_drop_refs(1, dmp, de);
579 dev = &de->de_cdp->cdp_c;
583 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
585 devfs_allocv_drop_refs(1, dmp, de);
586 printf("devfs_allocv: failed to allocate new vnode\n");
590 if (de->de_dirent->d_type == DT_CHR) {
595 /* XXX: v_rdev should be protect by vnode lock */
597 VNPASS(vp->v_usecount == 1, vp);
598 /* Special casing of ttys for deadfs. Probably redundant. */
600 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
601 vp->v_vflag |= VV_ISTTY;
604 if ((dev->si_flags & SI_ETERNAL) != 0)
605 vp->v_vflag |= VV_ETERNALDEV;
606 vp->v_op = &devfs_specops;
607 } else if (de->de_dirent->d_type == DT_DIR) {
609 } else if (de->de_dirent->d_type == DT_LNK) {
614 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
616 mtx_lock(&devfs_de_interlock);
619 mtx_unlock(&devfs_de_interlock);
620 error = insmntque1(vp, mp);
622 mtx_lock(&devfs_de_interlock);
625 mtx_unlock(&devfs_de_interlock);
628 (void) devfs_allocv_drop_refs(1, dmp, de);
631 if (devfs_allocv_drop_refs(0, dmp, de)) {
637 mac_devfs_vnode_associate(mp, de, vp);
639 sx_xunlock(&dmp->dm_lock);
640 vn_set_state(vp, VSTATE_CONSTRUCTED);
646 devfs_access(struct vop_access_args *ap)
648 struct vnode *vp = ap->a_vp;
649 struct devfs_dirent *de;
654 if (vp->v_type == VDIR)
657 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
658 ap->a_accmode, ap->a_cred);
663 p = ap->a_td->td_proc;
664 /* We do, however, allow access to the controlling terminal */
666 if (!(p->p_flag & P_CONTROLT)) {
670 if (p->p_session->s_ttydp == de->de_cdp)
676 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
677 "devfs-only flag reuse failed");
680 devfs_close(struct vop_close_args *ap)
682 struct vnode *vp = ap->a_vp, *oldvp;
683 struct thread *td = ap->a_td;
685 struct cdev *dev = vp->v_rdev;
687 struct devfs_dirent *de = vp->v_data;
688 int dflags, error, ref, vp_locked;
691 * XXX: Don't call d_close() if we were called because of
692 * XXX: insmntque() failure.
694 if (vp->v_data == NULL)
698 * Hack: a tty device that is a controlling terminal
699 * has a reference from the session structure.
700 * We cannot easily tell that a character device is
701 * a controlling terminal, unless it is the closing
702 * process' controlling terminal. In that case,
703 * if the reference count is 2 (this last descriptor
704 * plus the session), release the reference from the session.
706 if (de->de_usecount == 2 && td != NULL) {
709 if (vp == p->p_session->s_ttyvp) {
712 sx_xlock(&proctree_lock);
713 if (vp == p->p_session->s_ttyvp) {
714 SESS_LOCK(p->p_session);
715 mtx_lock(&devfs_de_interlock);
717 if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
718 p->p_session->s_ttyvp = NULL;
719 p->p_session->s_ttydp = NULL;
723 mtx_unlock(&devfs_de_interlock);
724 SESS_UNLOCK(p->p_session);
726 sx_xunlock(&proctree_lock);
728 devfs_ctty_unref(oldvp);
733 * We do not want to really close the device if it
734 * is still in use unless we are trying to close it
735 * forcibly. Since every use (buffer, vnode, swap, cmap)
736 * holds a reference to the vnode, and because we mark
737 * any other vnodes that alias this device, when the
738 * sum of the reference counts on all the aliased
739 * vnodes descends to one, we are on last close.
741 dsw = dev_refthread(dev, &ref);
745 mtx_lock(&devfs_de_interlock);
747 if (devfs_usecountl(vp) == 1)
748 dflags |= FLASTCLOSE;
749 devfs_usecount_subl(vp);
750 mtx_unlock(&devfs_de_interlock);
751 if (VN_IS_DOOMED(vp)) {
753 dflags |= FREVOKE | FNONBLOCK;
754 } else if (dsw->d_flags & D_TRACKCLOSE) {
755 /* Keep device updated on status. */
756 } else if ((dflags & FLASTCLOSE) == 0) {
758 dev_relthread(dev, ref);
763 vp_locked = VOP_ISLOCKED(vp);
765 KASSERT(dev->si_refcount > 0,
766 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
767 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
768 dev_relthread(dev, ref);
769 vn_lock(vp, vp_locked | LK_RETRY);
775 devfs_close_f(struct file *fp, struct thread *td)
781 * NB: td may be NULL if this descriptor is closed due to
782 * garbage collection from a closed UNIX domain socket.
784 fpop = curthread->td_fpop;
785 curthread->td_fpop = fp;
786 error = vnops.fo_close(fp, td);
787 curthread->td_fpop = fpop;
790 * The f_cdevpriv cannot be assigned non-NULL value while we
791 * are destroying the file.
793 if (fp->f_cdevpriv != NULL)
799 devfs_getattr(struct vop_getattr_args *ap)
801 struct vnode *vp = ap->a_vp;
802 struct vattr *vap = ap->a_vap;
803 struct devfs_dirent *de;
804 struct devfs_mount *dmp;
806 struct timeval boottime;
809 error = devfs_populate_vp(vp);
813 dmp = VFSTODEVFS(vp->v_mount);
814 sx_xunlock(&dmp->dm_lock);
817 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
818 if (vp->v_type == VDIR) {
821 ("Null dir dirent in devfs_getattr vp=%p", vp));
823 vap->va_uid = de->de_uid;
824 vap->va_gid = de->de_gid;
825 vap->va_mode = de->de_mode;
826 if (vp->v_type == VLNK)
827 vap->va_size = strlen(de->de_symlink);
828 else if (vp->v_type == VDIR)
829 vap->va_size = vap->va_bytes = DEV_BSIZE;
832 if (vp->v_type != VDIR)
834 vap->va_blocksize = DEV_BSIZE;
835 vap->va_type = vp->v_type;
837 getboottime(&boottime);
840 if ((aa).tv_sec <= 3600) { \
841 (aa).tv_sec = boottime.tv_sec; \
842 (aa).tv_nsec = boottime.tv_usec * 1000; \
846 if (vp->v_type != VCHR) {
848 vap->va_atime = de->de_atime;
850 vap->va_mtime = de->de_mtime;
852 vap->va_ctime = de->de_ctime;
856 vap->va_atime = dev->si_atime;
858 vap->va_mtime = dev->si_mtime;
860 vap->va_ctime = dev->si_ctime;
862 vap->va_rdev = cdev2priv(dev)->cdp_inode;
867 vap->va_nlink = de->de_links;
868 vap->va_fileid = de->de_inode;
875 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
882 error = vnops.fo_ioctl(fp, com, data, cred, td);
888 fiodgname_buf_get_ptr(void *fgnp, u_long com)
891 struct fiodgname_arg fgn;
892 #ifdef COMPAT_FREEBSD32
893 struct fiodgname_arg32 fgn32;
900 return (fgnup->fgn.buf);
901 #ifdef COMPAT_FREEBSD32
903 return ((void *)(uintptr_t)fgnup->fgn32.buf);
906 panic("Unhandled ioctl command %ld", com);
911 devfs_ioctl(struct vop_ioctl_args *ap)
913 struct fiodgname_arg *fgn;
914 struct vnode *vpold, *vp;
917 struct session *sess;
927 dsw = devvn_refthread(vp, &dev, &ref);
930 KASSERT(dev->si_refcount > 0,
931 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
935 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
939 #ifdef COMPAT_FREEBSD32
948 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
951 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
954 dev_relthread(dev, ref);
955 if (error == ENOIOCTL)
958 if (error == 0 && com == TIOCSCTTY) {
960 * Do nothing if reassigning same control tty, or if the
961 * control tty has already disappeared. If it disappeared,
962 * it's because we were racing with TIOCNOTTY. TIOCNOTTY
963 * already took care of releasing the old vnode and we have
964 * nothing left to do.
966 sx_slock(&proctree_lock);
967 sess = td->td_proc->p_session;
968 if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
969 sx_sunlock(&proctree_lock);
975 vpold = sess->s_ttyvp;
977 sess->s_ttydp = cdev2priv(dev);
980 sx_sunlock(&proctree_lock);
982 /* Get rid of reference to old control tty */
984 devfs_ctty_unref(vpold);
991 devfs_kqfilter_f(struct file *fp, struct knote *kn)
1001 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1004 error = dsw->d_kqfilter(dev, kn);
1006 dev_relthread(dev, ref);
1011 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
1013 struct cdev_priv *cdp;
1021 dcr = cdp->cdp_c.si_cred;
1025 error = prison_check(td->td_ucred, dcr);
1028 /* We do, however, allow access to the controlling terminal */
1031 if (!(p->p_flag & P_CONTROLT)) {
1035 if (p->p_session->s_ttydp == cdp)
1042 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1044 struct componentname *cnp;
1045 struct vnode *dvp, **vpp;
1047 struct devfs_dirent *de, *dd;
1048 struct devfs_dirent **dde;
1049 struct devfs_mount *dmp;
1052 int error, flags, nameiop, dvplocked;
1053 char specname[SPECNAMELEN + 1], *pname;
1059 pname = cnp->cn_nameptr;
1060 flags = cnp->cn_flags;
1061 nameiop = cnp->cn_nameiop;
1063 dmp = VFSTODEVFS(mp);
1067 if ((flags & ISLASTCN) && nameiop == RENAME)
1068 return (EOPNOTSUPP);
1070 if (dvp->v_type != VDIR)
1073 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1076 error = vn_dir_check_exec(dvp, cnp);
1080 if (cnp->cn_namelen == 1 && *pname == '.') {
1081 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1088 if (flags & ISDOTDOT) {
1089 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1091 de = devfs_parent_dirent(dd);
1094 dvplocked = VOP_ISLOCKED(dvp);
1096 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1099 vn_lock(dvp, dvplocked | LK_RETRY);
1104 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1105 while (de == NULL) { /* While(...) so we can use break */
1107 if (nameiop == DELETE)
1111 * OK, we didn't have an entry for the name we were asked for
1112 * so we try to see if anybody can create it on demand.
1114 pname = devfs_fqpn(specname, dmp, dd, cnp);
1119 DEVFS_DMP_HOLD(dmp);
1120 sx_xunlock(&dmp->dm_lock);
1121 EVENTHANDLER_INVOKE(dev_clone,
1122 td->td_ucred, pname, strlen(pname), &cdev);
1125 sx_xlock(&dmp->dm_lock);
1126 else if (devfs_populate_vp(dvp) != 0) {
1128 sx_xlock(&dmp->dm_lock);
1129 if (DEVFS_DMP_DROP(dmp)) {
1130 sx_xunlock(&dmp->dm_lock);
1131 devfs_unmount_final(dmp);
1133 sx_xunlock(&dmp->dm_lock);
1137 if (DEVFS_DMP_DROP(dmp)) {
1139 sx_xunlock(&dmp->dm_lock);
1140 devfs_unmount_final(dmp);
1150 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1151 if (dde != NULL && *dde != NULL)
1158 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1159 if ((nameiop == CREATE || nameiop == RENAME) &&
1160 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1161 return (EJUSTRETURN);
1166 if (devfs_prison_check(de, td))
1169 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1170 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1179 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1185 devfs_lookup(struct vop_lookup_args *ap)
1188 struct devfs_mount *dmp;
1191 if (devfs_populate_vp(ap->a_dvp) != 0)
1194 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1196 j = devfs_lookupx(ap, &dm_unlock);
1198 sx_xunlock(&dmp->dm_lock);
1203 devfs_mknod(struct vop_mknod_args *ap)
1205 struct componentname *cnp;
1206 struct vnode *dvp, **vpp;
1207 struct devfs_dirent *dd, *de;
1208 struct devfs_mount *dmp;
1212 * The only type of node we should be creating here is a
1213 * character device, for anything else return EOPNOTSUPP.
1215 if (ap->a_vap->va_type != VCHR)
1216 return (EOPNOTSUPP);
1218 dmp = VFSTODEVFS(dvp->v_mount);
1225 sx_xlock(&dmp->dm_lock);
1226 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1227 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1229 if (de->de_dirent->d_type == DT_CHR &&
1230 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1232 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1233 de->de_dirent->d_namlen) != 0)
1235 if (de->de_flags & DE_WHITEOUT)
1241 de->de_flags &= ~DE_WHITEOUT;
1242 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1245 sx_xunlock(&dmp->dm_lock);
1251 devfs_open(struct vop_open_args *ap)
1253 struct thread *td = ap->a_td;
1254 struct vnode *vp = ap->a_vp;
1255 struct cdev *dev = vp->v_rdev;
1256 struct file *fp = ap->a_fp;
1257 int error, ref, vlocked;
1261 if (vp->v_type == VBLK)
1267 /* Make this field valid before any I/O in d_open. */
1268 if (dev->si_iosize_max == 0)
1269 dev->si_iosize_max = DFLTPHYS;
1271 dsw = dev_refthread(dev, &ref);
1274 if (fp == NULL && dsw->d_fdopen != NULL) {
1275 dev_relthread(dev, ref);
1279 if (vp->v_type == VCHR)
1280 devfs_usecount_add(vp);
1282 vlocked = VOP_ISLOCKED(vp);
1291 if (dsw->d_fdopen != NULL)
1292 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1294 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1295 /* Clean up any cdevpriv upon error. */
1297 devfs_clear_cdevpriv();
1300 vn_lock(vp, vlocked | LK_RETRY);
1301 if (error != 0 && vp->v_type == VCHR)
1302 devfs_usecount_sub(vp);
1304 dev_relthread(dev, ref);
1306 if (error == ERESTART)
1311 #if 0 /* /dev/console */
1312 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1317 if (fp->f_ops == &badfileops)
1318 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1323 devfs_pathconf(struct vop_pathconf_args *ap)
1326 switch (ap->a_name) {
1327 case _PC_FILESIZEBITS:
1331 *ap->a_retval = NAME_MAX;
1334 *ap->a_retval = INT_MAX;
1336 case _PC_SYMLINK_MAX:
1337 *ap->a_retval = MAXPATHLEN;
1340 if (ap->a_vp->v_vflag & VV_ISTTY) {
1341 *ap->a_retval = MAX_CANON;
1346 if (ap->a_vp->v_vflag & VV_ISTTY) {
1347 *ap->a_retval = MAX_INPUT;
1352 if (ap->a_vp->v_vflag & VV_ISTTY) {
1353 *ap->a_retval = _POSIX_VDISABLE;
1357 case _PC_MAC_PRESENT:
1360 * If MAC is enabled, devfs automatically supports
1361 * trivial non-persistent label storage.
1368 case _PC_CHOWN_RESTRICTED:
1372 return (vop_stdpathconf(ap));
1379 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1387 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1389 error = vnops.fo_poll(fp, events, cred, td);
1392 error = dsw->d_poll(dev, events, td);
1394 dev_relthread(dev, ref);
1399 * Print out the contents of a special device vnode.
1402 devfs_print(struct vop_print_args *ap)
1405 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1410 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1411 int flags, struct thread *td)
1414 int ioflag, error, ref;
1419 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1422 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1424 error = vnops.fo_read(fp, uio, cred, flags, td);
1427 resid = uio->uio_resid;
1428 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1429 if (ioflag & O_DIRECT)
1430 ioflag |= IO_DIRECT;
1432 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1433 error = dsw->d_read(dev, uio, ioflag);
1434 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1435 devfs_timestamp(&dev->si_atime);
1437 dev_relthread(dev, ref);
1439 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1444 devfs_readdir(struct vop_readdir_args *ap)
1449 struct devfs_dirent *dd;
1450 struct devfs_dirent *de;
1451 struct devfs_mount *dmp;
1453 int *tmp_ncookies = NULL;
1455 if (ap->a_vp->v_type != VDIR)
1459 if (uio->uio_offset < 0)
1463 * XXX: This is a temporary hack to get around this filesystem not
1464 * supporting cookies. We store the location of the ncookies pointer
1465 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1466 * and set the number of cookies to 0. We then set the pointer to
1467 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1468 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1469 * pointer to its original location before returning to the caller.
1471 if (ap->a_ncookies != NULL) {
1472 tmp_ncookies = ap->a_ncookies;
1473 *ap->a_ncookies = 0;
1474 ap->a_ncookies = NULL;
1477 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1478 if (devfs_populate_vp(ap->a_vp) != 0) {
1479 if (tmp_ncookies != NULL)
1480 ap->a_ncookies = tmp_ncookies;
1484 de = ap->a_vp->v_data;
1486 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1487 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1488 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1490 if (devfs_prison_check(dd, uio->uio_td))
1492 if (dd->de_dirent->d_type == DT_DIR)
1497 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1498 if (dp->d_reclen > uio->uio_resid)
1500 dp->d_fileno = de->de_inode;
1501 /* NOTE: d_off is the offset for the *next* entry. */
1502 dp->d_off = off + dp->d_reclen;
1503 if (off >= uio->uio_offset) {
1504 error = vfs_read_dirent(ap, dp, off);
1508 off += dp->d_reclen;
1510 sx_xunlock(&dmp->dm_lock);
1511 uio->uio_offset = off;
1514 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1517 if (tmp_ncookies != NULL)
1518 ap->a_ncookies = tmp_ncookies;
1524 devfs_readlink(struct vop_readlink_args *ap)
1526 struct devfs_dirent *de;
1528 de = ap->a_vp->v_data;
1529 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1533 devfs_reclaiml(struct vnode *vp)
1535 struct devfs_dirent *de;
1537 mtx_assert(&devfs_de_interlock, MA_OWNED);
1540 MPASS(de->de_usecount == 0);
1541 de->de_vnode = NULL;
1547 devfs_reclaim(struct vop_reclaim_args *ap)
1552 mtx_lock(&devfs_de_interlock);
1554 mtx_unlock(&devfs_de_interlock);
1559 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1565 MPASS(vp->v_type == VCHR);
1567 mtx_lock(&devfs_de_interlock);
1569 devfs_usecount_subl(vp);
1571 mtx_unlock(&devfs_de_interlock);
1583 devfs_remove(struct vop_remove_args *ap)
1585 struct vnode *dvp = ap->a_dvp;
1586 struct vnode *vp = ap->a_vp;
1587 struct devfs_dirent *dd;
1588 struct devfs_dirent *de, *de_covered;
1589 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1591 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1592 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1594 sx_xlock(&dmp->dm_lock);
1595 dd = ap->a_dvp->v_data;
1597 if (de->de_cdp == NULL) {
1598 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1599 if (de->de_dirent->d_type == DT_LNK) {
1600 de_covered = devfs_find(dd, de->de_dirent->d_name,
1601 de->de_dirent->d_namlen, 0);
1602 if (de_covered != NULL)
1603 de_covered->de_flags &= ~DE_COVERED;
1605 /* We need to unlock dvp because devfs_delete() may lock it. */
1609 devfs_delete(dmp, de, 0);
1610 sx_xunlock(&dmp->dm_lock);
1612 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1615 de->de_flags |= DE_WHITEOUT;
1616 sx_xunlock(&dmp->dm_lock);
1622 * Revoke is called on a tty when a terminal session ends. The vnode
1623 * is orphaned by setting v_op to deadfs so we need to let go of it
1624 * as well so that we create a new one next time around.
1628 devfs_revoke(struct vop_revoke_args *ap)
1630 struct vnode *vp = ap->a_vp, *vp2;
1632 struct cdev_priv *cdp;
1633 struct devfs_dirent *de;
1637 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1640 cdp = cdev2priv(dev);
1653 mtx_lock(&devfs_de_interlock);
1656 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1657 de = cdp->cdp_dirents[i];
1664 vs = vget_prep(vp2);
1665 mtx_unlock(&devfs_de_interlock);
1666 if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1679 mtx_unlock(&devfs_de_interlock);
1684 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1685 KASSERT((cdp->cdp_flags & CDP_ON_ACTIVE_LIST) != 0,
1686 ("%s: cdp %p (%s) not on active list",
1687 __func__, cdp, dev->si_name));
1688 cdp->cdp_flags &= ~CDP_ON_ACTIVE_LIST;
1689 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1691 dev_rel(&cdp->cdp_c);
1695 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1700 devfs_rioctl(struct vop_ioctl_args *ap)
1703 struct devfs_mount *dmp;
1707 vn_lock(vp, LK_SHARED | LK_RETRY);
1708 if (VN_IS_DOOMED(vp)) {
1712 dmp = VFSTODEVFS(vp->v_mount);
1713 sx_xlock(&dmp->dm_lock);
1715 DEVFS_DMP_HOLD(dmp);
1716 devfs_populate(dmp);
1717 if (DEVFS_DMP_DROP(dmp)) {
1718 sx_xunlock(&dmp->dm_lock);
1719 devfs_unmount_final(dmp);
1722 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1723 sx_xunlock(&dmp->dm_lock);
1728 devfs_rread(struct vop_read_args *ap)
1731 if (ap->a_vp->v_type != VDIR)
1733 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1737 devfs_setattr(struct vop_setattr_args *ap)
1739 struct devfs_dirent *de;
1750 if ((vap->va_type != VNON) ||
1751 (vap->va_nlink != VNOVAL) ||
1752 (vap->va_fsid != VNOVAL) ||
1753 (vap->va_fileid != VNOVAL) ||
1754 (vap->va_blocksize != VNOVAL) ||
1755 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1756 (vap->va_rdev != VNOVAL) ||
1757 ((int)vap->va_bytes != VNOVAL) ||
1758 (vap->va_gen != VNOVAL)) {
1762 error = devfs_populate_vp(vp);
1767 if (vp->v_type == VDIR)
1771 if (vap->va_uid == (uid_t)VNOVAL)
1775 if (vap->va_gid == (gid_t)VNOVAL)
1779 if (uid != de->de_uid || gid != de->de_gid) {
1780 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1781 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1782 error = priv_check(td, PRIV_VFS_CHOWN);
1791 if (vap->va_mode != (mode_t)VNOVAL) {
1792 if (ap->a_cred->cr_uid != de->de_uid) {
1793 error = priv_check(td, PRIV_VFS_ADMIN);
1797 de->de_mode = vap->va_mode;
1801 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1802 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1805 if (vap->va_atime.tv_sec != VNOVAL) {
1806 if (vp->v_type == VCHR)
1807 vp->v_rdev->si_atime = vap->va_atime;
1809 de->de_atime = vap->va_atime;
1811 if (vap->va_mtime.tv_sec != VNOVAL) {
1812 if (vp->v_type == VCHR)
1813 vp->v_rdev->si_mtime = vap->va_mtime;
1815 de->de_mtime = vap->va_mtime;
1821 if (vp->v_type == VCHR)
1822 vfs_timestamp(&vp->v_rdev->si_ctime);
1824 vfs_timestamp(&de->de_mtime);
1828 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1834 devfs_setlabel(struct vop_setlabel_args *ap)
1837 struct devfs_dirent *de;
1842 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1843 mac_devfs_update(vp->v_mount, de, vp);
1850 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred)
1853 return (vnops.fo_stat(fp, sb, cred));
1857 devfs_symlink(struct vop_symlink_args *ap)
1860 struct devfs_dirent *dd;
1861 struct devfs_dirent *de, *de_covered, *de_dotdot;
1862 struct devfs_mount *dmp;
1864 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1867 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1868 if (devfs_populate_vp(ap->a_dvp) != 0)
1871 dd = ap->a_dvp->v_data;
1872 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1873 de->de_flags = DE_USER;
1877 de->de_inode = alloc_unr(devfs_inos);
1879 de->de_dirent->d_type = DT_LNK;
1880 i = strlen(ap->a_target) + 1;
1881 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1882 bcopy(ap->a_target, de->de_symlink, i);
1884 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1886 de_covered = devfs_find(dd, de->de_dirent->d_name,
1887 de->de_dirent->d_namlen, 0);
1888 if (de_covered != NULL) {
1889 if ((de_covered->de_flags & DE_USER) != 0) {
1890 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1891 sx_xunlock(&dmp->dm_lock);
1894 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1895 ("devfs_symlink: entry %p already covered", de_covered));
1896 de_covered->de_flags |= DE_COVERED;
1899 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1900 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1901 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1902 devfs_dir_ref_de(dmp, dd);
1903 devfs_rules_apply(dmp, de);
1905 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1909 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1912 return (vnops.fo_truncate(fp, length, cred, td));
1916 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1917 int flags, struct thread *td)
1920 int error, ioflag, ref;
1925 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1928 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1930 error = vnops.fo_write(fp, uio, cred, flags, td);
1933 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1934 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1935 if (ioflag & O_DIRECT)
1936 ioflag |= IO_DIRECT;
1937 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1939 resid = uio->uio_resid;
1941 error = dsw->d_write(dev, uio, ioflag);
1942 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1943 devfs_timestamp(&dev->si_ctime);
1944 dev->si_mtime = dev->si_ctime;
1947 dev_relthread(dev, ref);
1949 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1954 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1955 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1970 * Ensure that file and memory protections are
1974 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1975 maxprot = VM_PROT_NONE;
1976 if ((prot & VM_PROT_EXECUTE) != 0)
1979 maxprot = VM_PROT_EXECUTE;
1980 if ((fp->f_flag & FREAD) != 0)
1981 maxprot |= VM_PROT_READ;
1982 else if ((prot & VM_PROT_READ) != 0)
1986 * If we are sharing potential changes via MAP_SHARED and we
1987 * are trying to get write permission although we opened it
1988 * without asking for it, bail out.
1990 * Note that most character devices always share mappings.
1991 * The one exception is that D_MMAP_ANON devices
1992 * (i.e. /dev/zero) permit private writable mappings.
1994 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1995 * as well as updating maxprot to permit writing for
1996 * D_MMAP_ANON devices rather than doing that here.
1998 if ((flags & MAP_SHARED) != 0) {
1999 if ((fp->f_flag & FWRITE) != 0)
2000 maxprot |= VM_PROT_WRITE;
2001 else if ((prot & VM_PROT_WRITE) != 0)
2004 maxprot &= cap_maxprot;
2007 error = devfs_fp_check(fp, &dev, &dsw, &ref);
2011 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
2014 dev_relthread(dev, ref);
2018 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
2021 vm_object_deallocate(object);
2026 dev2udev(struct cdev *x)
2030 return (cdev2priv(x)->cdp_inode);
2034 devfs_cmp_f(struct file *fp1, struct file *fp2, struct thread *td)
2036 if (fp2->f_type != DTYPE_VNODE || fp2->f_ops != &devfs_ops_f)
2038 return (kcmp_cmp((uintptr_t)fp1->f_data, (uintptr_t)fp2->f_data));
2041 static struct fileops devfs_ops_f = {
2042 .fo_read = devfs_read_f,
2043 .fo_write = devfs_write_f,
2044 .fo_truncate = devfs_truncate_f,
2045 .fo_ioctl = devfs_ioctl_f,
2046 .fo_poll = devfs_poll_f,
2047 .fo_kqfilter = devfs_kqfilter_f,
2048 .fo_stat = devfs_stat_f,
2049 .fo_close = devfs_close_f,
2050 .fo_chmod = vn_chmod,
2051 .fo_chown = vn_chown,
2052 .fo_sendfile = vn_sendfile,
2054 .fo_fill_kinfo = vn_fill_kinfo,
2055 .fo_mmap = devfs_mmap_f,
2056 .fo_cmp = devfs_cmp_f,
2057 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
2060 /* Vops for non-CHR vnodes in /dev. */
2061 static struct vop_vector devfs_vnodeops = {
2062 .vop_default = &default_vnodeops,
2064 .vop_access = devfs_access,
2065 .vop_getattr = devfs_getattr,
2066 .vop_ioctl = devfs_rioctl,
2067 .vop_lookup = devfs_lookup,
2068 .vop_mknod = devfs_mknod,
2069 .vop_pathconf = devfs_pathconf,
2070 .vop_read = devfs_rread,
2071 .vop_readdir = devfs_readdir,
2072 .vop_readlink = devfs_readlink,
2073 .vop_reclaim = devfs_reclaim,
2074 .vop_remove = devfs_remove,
2075 .vop_revoke = devfs_revoke,
2076 .vop_setattr = devfs_setattr,
2078 .vop_setlabel = devfs_setlabel,
2080 .vop_symlink = devfs_symlink,
2081 .vop_vptocnp = devfs_vptocnp,
2082 .vop_lock1 = vop_lock,
2083 .vop_unlock = vop_unlock,
2084 .vop_islocked = vop_islocked,
2085 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2087 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2089 /* Vops for VCHR vnodes in /dev. */
2090 static struct vop_vector devfs_specops = {
2091 .vop_default = &default_vnodeops,
2093 .vop_access = devfs_access,
2094 .vop_bmap = VOP_PANIC,
2095 .vop_close = devfs_close,
2096 .vop_create = VOP_PANIC,
2097 .vop_fsync = vop_stdfsync,
2098 .vop_getattr = devfs_getattr,
2099 .vop_ioctl = devfs_ioctl,
2100 .vop_link = VOP_PANIC,
2101 .vop_mkdir = VOP_PANIC,
2102 .vop_mknod = VOP_PANIC,
2103 .vop_open = devfs_open,
2104 .vop_pathconf = devfs_pathconf,
2105 .vop_poll = dead_poll,
2106 .vop_print = devfs_print,
2107 .vop_read = dead_read,
2108 .vop_readdir = VOP_PANIC,
2109 .vop_readlink = VOP_PANIC,
2110 .vop_reallocblks = VOP_PANIC,
2111 .vop_reclaim = devfs_reclaim_vchr,
2112 .vop_remove = devfs_remove,
2113 .vop_rename = VOP_PANIC,
2114 .vop_revoke = devfs_revoke,
2115 .vop_rmdir = VOP_PANIC,
2116 .vop_setattr = devfs_setattr,
2118 .vop_setlabel = devfs_setlabel,
2120 .vop_strategy = VOP_PANIC,
2121 .vop_symlink = VOP_PANIC,
2122 .vop_vptocnp = devfs_vptocnp,
2123 .vop_write = dead_write,
2124 .vop_lock1 = vop_lock,
2125 .vop_unlock = vop_unlock,
2126 .vop_islocked = vop_islocked,
2127 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2129 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2132 * Our calling convention to the device drivers used to be that we passed
2133 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2134 * flags instead since that's what open(), close() and ioctl() takes and
2135 * we don't really want vnode.h in device drivers.
2136 * We solved the source compatibility by redefining some vnode flags to
2137 * be the same as the fcntl ones and by sending down the bitwise OR of
2138 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
2139 * pulls the rug out under this.
2141 CTASSERT(O_NONBLOCK == IO_NDELAY);
2142 CTASSERT(O_FSYNC == IO_SYNC);