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)) {
558 } else if (VN_IS_DOOMED(vp)) {
559 mtx_lock(&devfs_de_interlock);
560 if (de->de_vnode == vp) {
564 mtx_unlock(&devfs_de_interlock);
568 sx_xunlock(&dmp->dm_lock);
572 mtx_unlock(&devfs_de_interlock);
573 if (de->de_dirent->d_type == DT_CHR) {
574 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
575 devfs_allocv_drop_refs(1, dmp, de);
578 dev = &de->de_cdp->cdp_c;
582 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
584 devfs_allocv_drop_refs(1, dmp, de);
585 printf("devfs_allocv: failed to allocate new vnode\n");
589 if (de->de_dirent->d_type == DT_CHR) {
594 /* XXX: v_rdev should be protect by vnode lock */
596 VNPASS(vp->v_usecount == 1, vp);
597 /* Special casing of ttys for deadfs. Probably redundant. */
599 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
600 vp->v_vflag |= VV_ISTTY;
603 if ((dev->si_flags & SI_ETERNAL) != 0)
604 vp->v_vflag |= VV_ETERNALDEV;
605 vp->v_op = &devfs_specops;
606 } else if (de->de_dirent->d_type == DT_DIR) {
608 } else if (de->de_dirent->d_type == DT_LNK) {
613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
615 mtx_lock(&devfs_de_interlock);
618 mtx_unlock(&devfs_de_interlock);
619 error = insmntque1(vp, mp);
621 mtx_lock(&devfs_de_interlock);
624 mtx_unlock(&devfs_de_interlock);
627 (void) devfs_allocv_drop_refs(1, dmp, de);
630 if (devfs_allocv_drop_refs(0, dmp, de)) {
636 mac_devfs_vnode_associate(mp, de, vp);
638 sx_xunlock(&dmp->dm_lock);
639 vn_set_state(vp, VSTATE_CONSTRUCTED);
645 devfs_access(struct vop_access_args *ap)
647 struct vnode *vp = ap->a_vp;
648 struct devfs_dirent *de;
653 if (vp->v_type == VDIR)
656 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
657 ap->a_accmode, ap->a_cred);
662 p = ap->a_td->td_proc;
663 /* We do, however, allow access to the controlling terminal */
665 if (!(p->p_flag & P_CONTROLT)) {
669 if (p->p_session->s_ttydp == de->de_cdp)
675 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
676 "devfs-only flag reuse failed");
679 devfs_close(struct vop_close_args *ap)
681 struct vnode *vp = ap->a_vp, *oldvp;
682 struct thread *td = ap->a_td;
684 struct cdev *dev = vp->v_rdev;
686 struct devfs_dirent *de = vp->v_data;
687 int dflags, error, ref, vp_locked;
690 * XXX: Don't call d_close() if we were called because of
691 * XXX: insmntque() failure.
693 if (vp->v_data == NULL)
697 * Hack: a tty device that is a controlling terminal
698 * has a reference from the session structure.
699 * We cannot easily tell that a character device is
700 * a controlling terminal, unless it is the closing
701 * process' controlling terminal. In that case,
702 * if the reference count is 2 (this last descriptor
703 * plus the session), release the reference from the session.
705 if (de->de_usecount == 2 && td != NULL) {
708 if (vp == p->p_session->s_ttyvp) {
711 sx_xlock(&proctree_lock);
712 if (vp == p->p_session->s_ttyvp) {
713 SESS_LOCK(p->p_session);
714 mtx_lock(&devfs_de_interlock);
716 if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
717 p->p_session->s_ttyvp = NULL;
718 p->p_session->s_ttydp = NULL;
722 mtx_unlock(&devfs_de_interlock);
723 SESS_UNLOCK(p->p_session);
725 sx_xunlock(&proctree_lock);
727 devfs_ctty_unref(oldvp);
732 * We do not want to really close the device if it
733 * is still in use unless we are trying to close it
734 * forcibly. Since every use (buffer, vnode, swap, cmap)
735 * holds a reference to the vnode, and because we mark
736 * any other vnodes that alias this device, when the
737 * sum of the reference counts on all the aliased
738 * vnodes descends to one, we are on last close.
740 dsw = dev_refthread(dev, &ref);
744 mtx_lock(&devfs_de_interlock);
746 if (devfs_usecountl(vp) == 1)
747 dflags |= FLASTCLOSE;
748 devfs_usecount_subl(vp);
749 mtx_unlock(&devfs_de_interlock);
750 if (VN_IS_DOOMED(vp)) {
752 dflags |= FREVOKE | FNONBLOCK;
753 } else if (dsw->d_flags & D_TRACKCLOSE) {
754 /* Keep device updated on status. */
755 } else if ((dflags & FLASTCLOSE) == 0) {
757 dev_relthread(dev, ref);
762 vp_locked = VOP_ISLOCKED(vp);
764 KASSERT(dev->si_refcount > 0,
765 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
766 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
767 dev_relthread(dev, ref);
768 vn_lock(vp, vp_locked | LK_RETRY);
774 devfs_close_f(struct file *fp, struct thread *td)
780 * NB: td may be NULL if this descriptor is closed due to
781 * garbage collection from a closed UNIX domain socket.
783 fpop = curthread->td_fpop;
784 curthread->td_fpop = fp;
785 error = vnops.fo_close(fp, td);
786 curthread->td_fpop = fpop;
789 * The f_cdevpriv cannot be assigned non-NULL value while we
790 * are destroying the file.
792 if (fp->f_cdevpriv != NULL)
798 devfs_getattr(struct vop_getattr_args *ap)
800 struct vnode *vp = ap->a_vp;
801 struct vattr *vap = ap->a_vap;
802 struct devfs_dirent *de;
803 struct devfs_mount *dmp;
805 struct timeval boottime;
808 error = devfs_populate_vp(vp);
812 dmp = VFSTODEVFS(vp->v_mount);
813 sx_xunlock(&dmp->dm_lock);
816 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
817 if (vp->v_type == VDIR) {
820 ("Null dir dirent in devfs_getattr vp=%p", vp));
822 vap->va_uid = de->de_uid;
823 vap->va_gid = de->de_gid;
824 vap->va_mode = de->de_mode;
825 if (vp->v_type == VLNK)
826 vap->va_size = strlen(de->de_symlink);
827 else if (vp->v_type == VDIR)
828 vap->va_size = vap->va_bytes = DEV_BSIZE;
831 if (vp->v_type != VDIR)
833 vap->va_blocksize = DEV_BSIZE;
834 vap->va_type = vp->v_type;
836 getboottime(&boottime);
839 if ((aa).tv_sec <= 3600) { \
840 (aa).tv_sec = boottime.tv_sec; \
841 (aa).tv_nsec = boottime.tv_usec * 1000; \
845 if (vp->v_type != VCHR) {
847 vap->va_atime = de->de_atime;
849 vap->va_mtime = de->de_mtime;
851 vap->va_ctime = de->de_ctime;
855 vap->va_atime = dev->si_atime;
857 vap->va_mtime = dev->si_mtime;
859 vap->va_ctime = dev->si_ctime;
861 vap->va_rdev = cdev2priv(dev)->cdp_inode;
866 vap->va_nlink = de->de_links;
867 vap->va_fileid = de->de_inode;
874 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
881 error = vnops.fo_ioctl(fp, com, data, cred, td);
887 fiodgname_buf_get_ptr(void *fgnp, u_long com)
890 struct fiodgname_arg fgn;
891 #ifdef COMPAT_FREEBSD32
892 struct fiodgname_arg32 fgn32;
899 return (fgnup->fgn.buf);
900 #ifdef COMPAT_FREEBSD32
902 return ((void *)(uintptr_t)fgnup->fgn32.buf);
905 panic("Unhandled ioctl command %ld", com);
910 devfs_ioctl(struct vop_ioctl_args *ap)
912 struct fiodgname_arg *fgn;
913 struct vnode *vpold, *vp;
916 struct session *sess;
926 dsw = devvn_refthread(vp, &dev, &ref);
929 KASSERT(dev->si_refcount > 0,
930 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
934 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
938 #ifdef COMPAT_FREEBSD32
947 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
950 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
953 dev_relthread(dev, ref);
954 if (error == ENOIOCTL)
957 if (error == 0 && com == TIOCSCTTY) {
959 * Do nothing if reassigning same control tty, or if the
960 * control tty has already disappeared. If it disappeared,
961 * it's because we were racing with TIOCNOTTY. TIOCNOTTY
962 * already took care of releasing the old vnode and we have
963 * nothing left to do.
965 sx_slock(&proctree_lock);
966 sess = td->td_proc->p_session;
967 if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
968 sx_sunlock(&proctree_lock);
974 vpold = sess->s_ttyvp;
976 sess->s_ttydp = cdev2priv(dev);
979 sx_sunlock(&proctree_lock);
981 /* Get rid of reference to old control tty */
983 devfs_ctty_unref(vpold);
990 devfs_kqfilter_f(struct file *fp, struct knote *kn)
1000 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1003 error = dsw->d_kqfilter(dev, kn);
1005 dev_relthread(dev, ref);
1010 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
1012 struct cdev_priv *cdp;
1020 dcr = cdp->cdp_c.si_cred;
1024 error = prison_check(td->td_ucred, dcr);
1027 /* We do, however, allow access to the controlling terminal */
1030 if (!(p->p_flag & P_CONTROLT)) {
1034 if (p->p_session->s_ttydp == cdp)
1041 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1043 struct componentname *cnp;
1044 struct vnode *dvp, **vpp;
1046 struct devfs_dirent *de, *dd;
1047 struct devfs_dirent **dde;
1048 struct devfs_mount *dmp;
1051 int error, flags, nameiop, dvplocked;
1052 char specname[SPECNAMELEN + 1], *pname;
1058 pname = cnp->cn_nameptr;
1059 flags = cnp->cn_flags;
1060 nameiop = cnp->cn_nameiop;
1062 dmp = VFSTODEVFS(mp);
1066 if ((flags & ISLASTCN) && nameiop == RENAME)
1067 return (EOPNOTSUPP);
1069 if (dvp->v_type != VDIR)
1072 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1075 error = vn_dir_check_exec(dvp, cnp);
1079 if (cnp->cn_namelen == 1 && *pname == '.') {
1080 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1087 if (flags & ISDOTDOT) {
1088 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1090 de = devfs_parent_dirent(dd);
1093 dvplocked = VOP_ISLOCKED(dvp);
1095 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1098 vn_lock(dvp, dvplocked | LK_RETRY);
1103 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1104 while (de == NULL) { /* While(...) so we can use break */
1106 if (nameiop == DELETE)
1110 * OK, we didn't have an entry for the name we were asked for
1111 * so we try to see if anybody can create it on demand.
1113 pname = devfs_fqpn(specname, dmp, dd, cnp);
1118 DEVFS_DMP_HOLD(dmp);
1119 sx_xunlock(&dmp->dm_lock);
1120 EVENTHANDLER_INVOKE(dev_clone,
1121 td->td_ucred, pname, strlen(pname), &cdev);
1124 sx_xlock(&dmp->dm_lock);
1125 else if (devfs_populate_vp(dvp) != 0) {
1127 sx_xlock(&dmp->dm_lock);
1128 if (DEVFS_DMP_DROP(dmp)) {
1129 sx_xunlock(&dmp->dm_lock);
1130 devfs_unmount_final(dmp);
1132 sx_xunlock(&dmp->dm_lock);
1136 if (DEVFS_DMP_DROP(dmp)) {
1138 sx_xunlock(&dmp->dm_lock);
1139 devfs_unmount_final(dmp);
1149 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1150 if (dde != NULL && *dde != NULL)
1157 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1158 if ((nameiop == CREATE || nameiop == RENAME) &&
1159 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1160 return (EJUSTRETURN);
1165 if (devfs_prison_check(de, td))
1168 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1169 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1178 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1184 devfs_lookup(struct vop_lookup_args *ap)
1187 struct devfs_mount *dmp;
1190 if (devfs_populate_vp(ap->a_dvp) != 0)
1193 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1195 j = devfs_lookupx(ap, &dm_unlock);
1197 sx_xunlock(&dmp->dm_lock);
1202 devfs_mknod(struct vop_mknod_args *ap)
1204 struct componentname *cnp;
1205 struct vnode *dvp, **vpp;
1206 struct devfs_dirent *dd, *de;
1207 struct devfs_mount *dmp;
1211 * The only type of node we should be creating here is a
1212 * character device, for anything else return EOPNOTSUPP.
1214 if (ap->a_vap->va_type != VCHR)
1215 return (EOPNOTSUPP);
1217 dmp = VFSTODEVFS(dvp->v_mount);
1224 sx_xlock(&dmp->dm_lock);
1225 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1226 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1228 if (de->de_dirent->d_type == DT_CHR &&
1229 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1231 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1232 de->de_dirent->d_namlen) != 0)
1234 if (de->de_flags & DE_WHITEOUT)
1240 de->de_flags &= ~DE_WHITEOUT;
1241 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1244 sx_xunlock(&dmp->dm_lock);
1250 devfs_open(struct vop_open_args *ap)
1252 struct thread *td = ap->a_td;
1253 struct vnode *vp = ap->a_vp;
1254 struct cdev *dev = vp->v_rdev;
1255 struct file *fp = ap->a_fp;
1256 int error, ref, vlocked;
1260 if (vp->v_type == VBLK)
1266 /* Make this field valid before any I/O in d_open. */
1267 if (dev->si_iosize_max == 0)
1268 dev->si_iosize_max = DFLTPHYS;
1270 dsw = dev_refthread(dev, &ref);
1273 if (fp == NULL && dsw->d_fdopen != NULL) {
1274 dev_relthread(dev, ref);
1278 if (vp->v_type == VCHR)
1279 devfs_usecount_add(vp);
1281 vlocked = VOP_ISLOCKED(vp);
1290 if (dsw->d_fdopen != NULL)
1291 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1293 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1294 /* Clean up any cdevpriv upon error. */
1296 devfs_clear_cdevpriv();
1299 vn_lock(vp, vlocked | LK_RETRY);
1300 if (error != 0 && vp->v_type == VCHR)
1301 devfs_usecount_sub(vp);
1303 dev_relthread(dev, ref);
1305 if (error == ERESTART)
1310 #if 0 /* /dev/console */
1311 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1316 if (fp->f_ops == &badfileops)
1317 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1322 devfs_pathconf(struct vop_pathconf_args *ap)
1325 switch (ap->a_name) {
1326 case _PC_FILESIZEBITS:
1330 *ap->a_retval = NAME_MAX;
1333 *ap->a_retval = INT_MAX;
1335 case _PC_SYMLINK_MAX:
1336 *ap->a_retval = MAXPATHLEN;
1339 if (ap->a_vp->v_vflag & VV_ISTTY) {
1340 *ap->a_retval = MAX_CANON;
1345 if (ap->a_vp->v_vflag & VV_ISTTY) {
1346 *ap->a_retval = MAX_INPUT;
1351 if (ap->a_vp->v_vflag & VV_ISTTY) {
1352 *ap->a_retval = _POSIX_VDISABLE;
1356 case _PC_MAC_PRESENT:
1359 * If MAC is enabled, devfs automatically supports
1360 * trivial non-persistent label storage.
1367 case _PC_CHOWN_RESTRICTED:
1371 return (vop_stdpathconf(ap));
1378 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1386 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1388 error = vnops.fo_poll(fp, events, cred, td);
1391 error = dsw->d_poll(dev, events, td);
1393 dev_relthread(dev, ref);
1398 * Print out the contents of a special device vnode.
1401 devfs_print(struct vop_print_args *ap)
1404 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1409 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1410 int flags, struct thread *td)
1413 int ioflag, error, ref;
1418 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1421 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1423 error = vnops.fo_read(fp, uio, cred, flags, td);
1426 resid = uio->uio_resid;
1427 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1428 if (ioflag & O_DIRECT)
1429 ioflag |= IO_DIRECT;
1431 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1432 error = dsw->d_read(dev, uio, ioflag);
1433 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1434 devfs_timestamp(&dev->si_atime);
1436 dev_relthread(dev, ref);
1438 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1443 devfs_readdir(struct vop_readdir_args *ap)
1448 struct devfs_dirent *dd;
1449 struct devfs_dirent *de;
1450 struct devfs_mount *dmp;
1452 int *tmp_ncookies = NULL;
1454 if (ap->a_vp->v_type != VDIR)
1458 if (uio->uio_offset < 0)
1462 * XXX: This is a temporary hack to get around this filesystem not
1463 * supporting cookies. We store the location of the ncookies pointer
1464 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1465 * and set the number of cookies to 0. We then set the pointer to
1466 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1467 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1468 * pointer to its original location before returning to the caller.
1470 if (ap->a_ncookies != NULL) {
1471 tmp_ncookies = ap->a_ncookies;
1472 *ap->a_ncookies = 0;
1473 ap->a_ncookies = NULL;
1476 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1477 if (devfs_populate_vp(ap->a_vp) != 0) {
1478 if (tmp_ncookies != NULL)
1479 ap->a_ncookies = tmp_ncookies;
1483 de = ap->a_vp->v_data;
1485 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1486 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1487 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1489 if (devfs_prison_check(dd, uio->uio_td))
1491 if (dd->de_dirent->d_type == DT_DIR)
1496 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1497 if (dp->d_reclen > uio->uio_resid)
1499 dp->d_fileno = de->de_inode;
1500 /* NOTE: d_off is the offset for the *next* entry. */
1501 dp->d_off = off + dp->d_reclen;
1502 if (off >= uio->uio_offset) {
1503 error = vfs_read_dirent(ap, dp, off);
1507 off += dp->d_reclen;
1509 sx_xunlock(&dmp->dm_lock);
1510 uio->uio_offset = off;
1513 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1516 if (tmp_ncookies != NULL)
1517 ap->a_ncookies = tmp_ncookies;
1523 devfs_readlink(struct vop_readlink_args *ap)
1525 struct devfs_dirent *de;
1527 de = ap->a_vp->v_data;
1528 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1532 devfs_reclaiml(struct vnode *vp)
1534 struct devfs_dirent *de;
1536 mtx_assert(&devfs_de_interlock, MA_OWNED);
1539 MPASS(de->de_usecount == 0);
1540 de->de_vnode = NULL;
1546 devfs_reclaim(struct vop_reclaim_args *ap)
1551 mtx_lock(&devfs_de_interlock);
1553 mtx_unlock(&devfs_de_interlock);
1558 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1564 MPASS(vp->v_type == VCHR);
1566 mtx_lock(&devfs_de_interlock);
1568 devfs_usecount_subl(vp);
1570 mtx_unlock(&devfs_de_interlock);
1582 devfs_remove(struct vop_remove_args *ap)
1584 struct vnode *dvp = ap->a_dvp;
1585 struct vnode *vp = ap->a_vp;
1586 struct devfs_dirent *dd;
1587 struct devfs_dirent *de, *de_covered;
1588 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1590 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1591 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1593 sx_xlock(&dmp->dm_lock);
1594 dd = ap->a_dvp->v_data;
1596 if (de->de_cdp == NULL) {
1597 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1598 if (de->de_dirent->d_type == DT_LNK) {
1599 de_covered = devfs_find(dd, de->de_dirent->d_name,
1600 de->de_dirent->d_namlen, 0);
1601 if (de_covered != NULL)
1602 de_covered->de_flags &= ~DE_COVERED;
1604 /* We need to unlock dvp because devfs_delete() may lock it. */
1608 devfs_delete(dmp, de, 0);
1609 sx_xunlock(&dmp->dm_lock);
1611 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1614 de->de_flags |= DE_WHITEOUT;
1615 sx_xunlock(&dmp->dm_lock);
1621 * Revoke is called on a tty when a terminal session ends. The vnode
1622 * is orphaned by setting v_op to deadfs so we need to let go of it
1623 * as well so that we create a new one next time around.
1627 devfs_revoke(struct vop_revoke_args *ap)
1629 struct vnode *vp = ap->a_vp, *vp2;
1631 struct cdev_priv *cdp;
1632 struct devfs_dirent *de;
1636 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1639 cdp = cdev2priv(dev);
1652 mtx_lock(&devfs_de_interlock);
1655 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1656 de = cdp->cdp_dirents[i];
1663 vs = vget_prep(vp2);
1664 mtx_unlock(&devfs_de_interlock);
1665 if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1678 mtx_unlock(&devfs_de_interlock);
1683 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1684 KASSERT((cdp->cdp_flags & CDP_ON_ACTIVE_LIST) != 0,
1685 ("%s: cdp %p (%s) not on active list",
1686 __func__, cdp, dev->si_name));
1687 cdp->cdp_flags &= ~CDP_ON_ACTIVE_LIST;
1688 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1690 dev_rel(&cdp->cdp_c);
1694 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1699 devfs_rioctl(struct vop_ioctl_args *ap)
1702 struct devfs_mount *dmp;
1706 vn_lock(vp, LK_SHARED | LK_RETRY);
1707 if (VN_IS_DOOMED(vp)) {
1711 dmp = VFSTODEVFS(vp->v_mount);
1712 sx_xlock(&dmp->dm_lock);
1714 DEVFS_DMP_HOLD(dmp);
1715 devfs_populate(dmp);
1716 if (DEVFS_DMP_DROP(dmp)) {
1717 sx_xunlock(&dmp->dm_lock);
1718 devfs_unmount_final(dmp);
1721 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1722 sx_xunlock(&dmp->dm_lock);
1727 devfs_rread(struct vop_read_args *ap)
1730 if (ap->a_vp->v_type != VDIR)
1732 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1736 devfs_setattr(struct vop_setattr_args *ap)
1738 struct devfs_dirent *de;
1749 if ((vap->va_type != VNON) ||
1750 (vap->va_nlink != VNOVAL) ||
1751 (vap->va_fsid != VNOVAL) ||
1752 (vap->va_fileid != VNOVAL) ||
1753 (vap->va_blocksize != VNOVAL) ||
1754 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1755 (vap->va_rdev != VNOVAL) ||
1756 ((int)vap->va_bytes != VNOVAL) ||
1757 (vap->va_gen != VNOVAL)) {
1761 error = devfs_populate_vp(vp);
1766 if (vp->v_type == VDIR)
1770 if (vap->va_uid == (uid_t)VNOVAL)
1774 if (vap->va_gid == (gid_t)VNOVAL)
1778 if (uid != de->de_uid || gid != de->de_gid) {
1779 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1780 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1781 error = priv_check(td, PRIV_VFS_CHOWN);
1790 if (vap->va_mode != (mode_t)VNOVAL) {
1791 if (ap->a_cred->cr_uid != de->de_uid) {
1792 error = priv_check(td, PRIV_VFS_ADMIN);
1796 de->de_mode = vap->va_mode;
1800 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1801 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1804 if (vap->va_atime.tv_sec != VNOVAL) {
1805 if (vp->v_type == VCHR)
1806 vp->v_rdev->si_atime = vap->va_atime;
1808 de->de_atime = vap->va_atime;
1810 if (vap->va_mtime.tv_sec != VNOVAL) {
1811 if (vp->v_type == VCHR)
1812 vp->v_rdev->si_mtime = vap->va_mtime;
1814 de->de_mtime = vap->va_mtime;
1820 if (vp->v_type == VCHR)
1821 vfs_timestamp(&vp->v_rdev->si_ctime);
1823 vfs_timestamp(&de->de_mtime);
1827 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1833 devfs_setlabel(struct vop_setlabel_args *ap)
1836 struct devfs_dirent *de;
1841 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1842 mac_devfs_update(vp->v_mount, de, vp);
1849 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred)
1852 return (vnops.fo_stat(fp, sb, cred));
1856 devfs_symlink(struct vop_symlink_args *ap)
1859 struct devfs_dirent *dd;
1860 struct devfs_dirent *de, *de_covered, *de_dotdot;
1861 struct devfs_mount *dmp;
1863 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1866 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1867 if (devfs_populate_vp(ap->a_dvp) != 0)
1870 dd = ap->a_dvp->v_data;
1871 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1872 de->de_flags = DE_USER;
1876 de->de_inode = alloc_unr(devfs_inos);
1878 de->de_dirent->d_type = DT_LNK;
1879 i = strlen(ap->a_target) + 1;
1880 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1881 bcopy(ap->a_target, de->de_symlink, i);
1883 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1885 de_covered = devfs_find(dd, de->de_dirent->d_name,
1886 de->de_dirent->d_namlen, 0);
1887 if (de_covered != NULL) {
1888 if ((de_covered->de_flags & DE_USER) != 0) {
1889 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1890 sx_xunlock(&dmp->dm_lock);
1893 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1894 ("devfs_symlink: entry %p already covered", de_covered));
1895 de_covered->de_flags |= DE_COVERED;
1898 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1899 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1900 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1901 devfs_dir_ref_de(dmp, dd);
1902 devfs_rules_apply(dmp, de);
1904 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1908 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1911 return (vnops.fo_truncate(fp, length, cred, td));
1915 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1916 int flags, struct thread *td)
1919 int error, ioflag, ref;
1924 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1927 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1929 error = vnops.fo_write(fp, uio, cred, flags, td);
1932 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1933 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1934 if (ioflag & O_DIRECT)
1935 ioflag |= IO_DIRECT;
1936 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1938 resid = uio->uio_resid;
1940 error = dsw->d_write(dev, uio, ioflag);
1941 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1942 devfs_timestamp(&dev->si_ctime);
1943 dev->si_mtime = dev->si_ctime;
1946 dev_relthread(dev, ref);
1948 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1953 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1954 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1969 * Ensure that file and memory protections are
1973 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1974 maxprot = VM_PROT_NONE;
1975 if ((prot & VM_PROT_EXECUTE) != 0)
1978 maxprot = VM_PROT_EXECUTE;
1979 if ((fp->f_flag & FREAD) != 0)
1980 maxprot |= VM_PROT_READ;
1981 else if ((prot & VM_PROT_READ) != 0)
1985 * If we are sharing potential changes via MAP_SHARED and we
1986 * are trying to get write permission although we opened it
1987 * without asking for it, bail out.
1989 * Note that most character devices always share mappings.
1990 * The one exception is that D_MMAP_ANON devices
1991 * (i.e. /dev/zero) permit private writable mappings.
1993 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1994 * as well as updating maxprot to permit writing for
1995 * D_MMAP_ANON devices rather than doing that here.
1997 if ((flags & MAP_SHARED) != 0) {
1998 if ((fp->f_flag & FWRITE) != 0)
1999 maxprot |= VM_PROT_WRITE;
2000 else if ((prot & VM_PROT_WRITE) != 0)
2003 maxprot &= cap_maxprot;
2006 error = devfs_fp_check(fp, &dev, &dsw, &ref);
2010 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
2013 dev_relthread(dev, ref);
2017 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
2020 vm_object_deallocate(object);
2025 dev2udev(struct cdev *x)
2029 return (cdev2priv(x)->cdp_inode);
2033 devfs_cmp_f(struct file *fp1, struct file *fp2, struct thread *td)
2035 if (fp2->f_type != DTYPE_VNODE || fp2->f_ops != &devfs_ops_f)
2037 return (kcmp_cmp((uintptr_t)fp1->f_data, (uintptr_t)fp2->f_data));
2040 static struct fileops devfs_ops_f = {
2041 .fo_read = devfs_read_f,
2042 .fo_write = devfs_write_f,
2043 .fo_truncate = devfs_truncate_f,
2044 .fo_ioctl = devfs_ioctl_f,
2045 .fo_poll = devfs_poll_f,
2046 .fo_kqfilter = devfs_kqfilter_f,
2047 .fo_stat = devfs_stat_f,
2048 .fo_close = devfs_close_f,
2049 .fo_chmod = vn_chmod,
2050 .fo_chown = vn_chown,
2051 .fo_sendfile = vn_sendfile,
2053 .fo_fill_kinfo = vn_fill_kinfo,
2054 .fo_mmap = devfs_mmap_f,
2055 .fo_cmp = devfs_cmp_f,
2056 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
2059 /* Vops for non-CHR vnodes in /dev. */
2060 static struct vop_vector devfs_vnodeops = {
2061 .vop_default = &default_vnodeops,
2063 .vop_access = devfs_access,
2064 .vop_getattr = devfs_getattr,
2065 .vop_ioctl = devfs_rioctl,
2066 .vop_lookup = devfs_lookup,
2067 .vop_mknod = devfs_mknod,
2068 .vop_pathconf = devfs_pathconf,
2069 .vop_read = devfs_rread,
2070 .vop_readdir = devfs_readdir,
2071 .vop_readlink = devfs_readlink,
2072 .vop_reclaim = devfs_reclaim,
2073 .vop_remove = devfs_remove,
2074 .vop_revoke = devfs_revoke,
2075 .vop_setattr = devfs_setattr,
2077 .vop_setlabel = devfs_setlabel,
2079 .vop_symlink = devfs_symlink,
2080 .vop_vptocnp = devfs_vptocnp,
2081 .vop_lock1 = vop_lock,
2082 .vop_unlock = vop_unlock,
2083 .vop_islocked = vop_islocked,
2084 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2086 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2088 /* Vops for VCHR vnodes in /dev. */
2089 static struct vop_vector devfs_specops = {
2090 .vop_default = &default_vnodeops,
2092 .vop_access = devfs_access,
2093 .vop_bmap = VOP_PANIC,
2094 .vop_close = devfs_close,
2095 .vop_create = VOP_PANIC,
2096 .vop_fsync = vop_stdfsync,
2097 .vop_getattr = devfs_getattr,
2098 .vop_ioctl = devfs_ioctl,
2099 .vop_link = VOP_PANIC,
2100 .vop_mkdir = VOP_PANIC,
2101 .vop_mknod = VOP_PANIC,
2102 .vop_open = devfs_open,
2103 .vop_pathconf = devfs_pathconf,
2104 .vop_poll = dead_poll,
2105 .vop_print = devfs_print,
2106 .vop_read = dead_read,
2107 .vop_readdir = VOP_PANIC,
2108 .vop_readlink = VOP_PANIC,
2109 .vop_reallocblks = VOP_PANIC,
2110 .vop_reclaim = devfs_reclaim_vchr,
2111 .vop_remove = devfs_remove,
2112 .vop_rename = VOP_PANIC,
2113 .vop_revoke = devfs_revoke,
2114 .vop_rmdir = VOP_PANIC,
2115 .vop_setattr = devfs_setattr,
2117 .vop_setlabel = devfs_setlabel,
2119 .vop_strategy = VOP_PANIC,
2120 .vop_symlink = VOP_PANIC,
2121 .vop_vptocnp = devfs_vptocnp,
2122 .vop_write = dead_write,
2123 .vop_lock1 = vop_lock,
2124 .vop_unlock = vop_unlock,
2125 .vop_islocked = vop_islocked,
2126 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2128 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2131 * Our calling convention to the device drivers used to be that we passed
2132 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2133 * flags instead since that's what open(), close() and ioctl() takes and
2134 * we don't really want vnode.h in device drivers.
2135 * We solved the source compatibility by redefining some vnode flags to
2136 * be the same as the fcntl ones and by sending down the bitwise OR of
2137 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
2138 * pulls the rug out under this.
2140 CTASSERT(O_NONBLOCK == IO_NDELAY);
2141 CTASSERT(O_FSYNC == IO_SYNC);