2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
33 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
34 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/dirent.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
52 #include <sys/filio.h>
54 #include <sys/kernel.h>
55 #include <sys/limits.h>
57 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
65 #include <sys/sysctl.h>
67 #include <sys/ttycom.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
71 static struct vop_vector devfs_vnodeops;
72 static struct vop_vector devfs_specops;
73 static struct fileops devfs_ops_f;
75 #include <fs/devfs/devfs.h>
76 #include <fs/devfs/devfs_int.h>
78 #include <security/mac/mac_framework.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_object.h>
84 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
86 struct mtx devfs_de_interlock;
87 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
88 struct sx clone_drain_lock;
89 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
90 struct mtx cdevpriv_mtx;
91 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
93 SYSCTL_DECL(_vfs_devfs);
95 static int devfs_dotimes;
96 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
97 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
100 * Update devfs node timestamp. Note that updates are unlocked and
101 * stat(2) could see partially updated times.
104 devfs_timestamp(struct timespec *tsp)
112 if (tsp->tv_sec != ts) {
120 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
124 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
125 if (*devp != fp->f_data) {
127 dev_relthread(*devp, *ref);
130 KASSERT((*devp)->si_refcount > 0,
131 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
134 curthread->td_fpop = fp;
139 devfs_get_cdevpriv(void **datap)
142 struct cdev_privdata *p;
145 fp = curthread->td_fpop;
151 *datap = p->cdpd_data;
158 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
161 struct cdev_priv *cdp;
162 struct cdev_privdata *p;
165 fp = curthread->td_fpop;
168 cdp = cdev2priv((struct cdev *)fp->f_data);
169 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
171 p->cdpd_dtr = priv_dtr;
173 mtx_lock(&cdevpriv_mtx);
174 if (fp->f_cdevpriv == NULL) {
175 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
177 mtx_unlock(&cdevpriv_mtx);
180 mtx_unlock(&cdevpriv_mtx);
181 free(p, M_CDEVPDATA);
188 devfs_destroy_cdevpriv(struct cdev_privdata *p)
191 mtx_assert(&cdevpriv_mtx, MA_OWNED);
192 KASSERT(p->cdpd_fp->f_cdevpriv == p,
193 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
194 p->cdpd_fp->f_cdevpriv = NULL;
195 LIST_REMOVE(p, cdpd_list);
196 mtx_unlock(&cdevpriv_mtx);
197 (p->cdpd_dtr)(p->cdpd_data);
198 free(p, M_CDEVPDATA);
202 devfs_fpdrop(struct file *fp)
204 struct cdev_privdata *p;
206 mtx_lock(&cdevpriv_mtx);
207 if ((p = fp->f_cdevpriv) == NULL) {
208 mtx_unlock(&cdevpriv_mtx);
211 devfs_destroy_cdevpriv(p);
215 devfs_clear_cdevpriv(void)
219 fp = curthread->td_fpop;
226 devfs_usecount_add(struct vnode *vp)
228 struct devfs_dirent *de;
231 mtx_lock(&devfs_de_interlock);
233 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
234 if (VN_IS_DOOMED(vp)) {
246 mtx_unlock(&devfs_de_interlock);
250 devfs_usecount_subl(struct vnode *vp)
252 struct devfs_dirent *de;
255 mtx_assert(&devfs_de_interlock, MA_OWNED);
256 ASSERT_VI_LOCKED(vp, __func__);
257 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
264 MPASS(de->de_usecount == 0);
267 if (dev->si_usecount < de->de_usecount)
268 panic("%s: si_usecount underflow for dev %p "
269 "(has %ld, dirent has %d)\n",
270 __func__, dev, dev->si_usecount, de->de_usecount);
271 if (VN_IS_DOOMED(vp)) {
272 dev->si_usecount -= de->de_usecount;
275 if (de->de_usecount == 0)
276 panic("%s: de_usecount underflow for dev %p\n",
284 devfs_usecount_sub(struct vnode *vp)
287 mtx_lock(&devfs_de_interlock);
289 devfs_usecount_subl(vp);
291 mtx_unlock(&devfs_de_interlock);
295 devfs_usecountl(struct vnode *vp)
298 VNPASS(vp->v_type == VCHR, vp);
299 mtx_assert(&devfs_de_interlock, MA_OWNED);
300 ASSERT_VI_LOCKED(vp, __func__);
301 return (vp->v_rdev->si_usecount);
305 devfs_usecount(struct vnode *vp)
309 VNPASS(vp->v_type == VCHR, vp);
310 mtx_lock(&devfs_de_interlock);
312 count = devfs_usecountl(vp);
314 mtx_unlock(&devfs_de_interlock);
319 devfs_ctty_ref(struct vnode *vp)
323 devfs_usecount_add(vp);
327 devfs_ctty_unref(struct vnode *vp)
330 devfs_usecount_sub(vp);
335 * On success devfs_populate_vp() returns with dmp->dm_lock held.
338 devfs_populate_vp(struct vnode *vp)
340 struct devfs_dirent *de;
341 struct devfs_mount *dmp;
344 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
346 dmp = VFSTODEVFS(vp->v_mount);
347 if (!devfs_populate_needed(dmp)) {
348 sx_xlock(&dmp->dm_lock);
352 locked = VOP_ISLOCKED(vp);
354 sx_xlock(&dmp->dm_lock);
357 /* Can't call devfs_populate() with the vnode lock held. */
361 sx_xunlock(&dmp->dm_lock);
362 vn_lock(vp, locked | LK_RETRY);
363 sx_xlock(&dmp->dm_lock);
364 if (DEVFS_DMP_DROP(dmp)) {
365 sx_xunlock(&dmp->dm_lock);
366 devfs_unmount_final(dmp);
370 if (VN_IS_DOOMED(vp)) {
371 sx_xunlock(&dmp->dm_lock);
376 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
377 if ((de->de_flags & DE_DOOMED) != 0) {
378 sx_xunlock(&dmp->dm_lock);
386 devfs_vptocnp(struct vop_vptocnp_args *ap)
388 struct vnode *vp = ap->a_vp;
389 struct vnode **dvp = ap->a_vpp;
390 struct devfs_mount *dmp;
391 char *buf = ap->a_buf;
392 size_t *buflen = ap->a_buflen;
393 struct devfs_dirent *dd, *de;
396 dmp = VFSTODEVFS(vp->v_mount);
398 error = devfs_populate_vp(vp);
402 if (vp->v_type != VCHR && vp->v_type != VDIR) {
408 if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
415 i -= dd->de_dirent->d_namlen;
420 bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
422 de = devfs_parent_dirent(dd);
427 mtx_lock(&devfs_de_interlock);
431 mtx_unlock(&devfs_de_interlock);
437 mtx_unlock(&devfs_de_interlock);
441 sx_xunlock(&dmp->dm_lock);
446 * Construct the fully qualified path name relative to the mountpoint.
447 * If a NULL cnp is provided, no '/' is appended to the resulting path.
450 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
451 struct componentname *cnp)
454 struct devfs_dirent *de;
456 sx_assert(&dmp->dm_lock, SA_LOCKED);
461 i -= cnp->cn_namelen;
465 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
467 while (de != dmp->dm_rootdir) {
468 if (cnp != NULL || i < SPECNAMELEN) {
474 i -= de->de_dirent->d_namlen;
477 bcopy(de->de_dirent->d_name, buf + i,
478 de->de_dirent->d_namlen);
479 de = devfs_parent_dirent(de);
487 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
488 struct devfs_dirent *de)
493 if (de->de_flags & DE_DOOMED)
495 if (DEVFS_DE_DROP(de)) {
496 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
497 devfs_dirent_free(de);
499 if (DEVFS_DMP_DROP(dmp)) {
500 KASSERT(not_found == 1,
501 ("DEVFS mount struct freed before dirent"));
503 sx_xunlock(&dmp->dm_lock);
504 devfs_unmount_final(dmp);
506 if (not_found == 1 || (drop_dm_lock && not_found != 2))
507 sx_unlock(&dmp->dm_lock);
512 devfs_insmntque_dtr(struct vnode *vp, void *arg)
514 struct devfs_dirent *de;
516 de = (struct devfs_dirent *)arg;
517 mtx_lock(&devfs_de_interlock);
520 mtx_unlock(&devfs_de_interlock);
526 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
530 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
536 struct devfs_mount *dmp;
540 dmp = VFSTODEVFS(mp);
541 if (de->de_flags & DE_DOOMED) {
542 sx_xunlock(&dmp->dm_lock);
548 mtx_lock(&devfs_de_interlock);
552 mtx_unlock(&devfs_de_interlock);
553 sx_xunlock(&dmp->dm_lock);
554 vget_finish(vp, lockmode | LK_RETRY, vs);
555 sx_xlock(&dmp->dm_lock);
556 if (devfs_allocv_drop_refs(0, dmp, de)) {
560 else if (VN_IS_DOOMED(vp)) {
561 mtx_lock(&devfs_de_interlock);
562 if (de->de_vnode == vp) {
566 mtx_unlock(&devfs_de_interlock);
570 sx_xunlock(&dmp->dm_lock);
574 mtx_unlock(&devfs_de_interlock);
575 if (de->de_dirent->d_type == DT_CHR) {
576 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
577 devfs_allocv_drop_refs(1, dmp, de);
580 dev = &de->de_cdp->cdp_c;
584 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
586 devfs_allocv_drop_refs(1, dmp, de);
587 printf("devfs_allocv: failed to allocate new vnode\n");
591 if (de->de_dirent->d_type == DT_CHR) {
596 /* XXX: v_rdev should be protect by vnode lock */
598 VNPASS(vp->v_usecount == 1, vp);
599 /* Special casing of ttys for deadfs. Probably redundant. */
601 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
602 vp->v_vflag |= VV_ISTTY;
605 if ((dev->si_flags & SI_ETERNAL) != 0)
606 vp->v_vflag |= VV_ETERNALDEV;
607 vp->v_op = &devfs_specops;
608 } else if (de->de_dirent->d_type == DT_DIR) {
610 } else if (de->de_dirent->d_type == DT_LNK) {
615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
617 mtx_lock(&devfs_de_interlock);
620 mtx_unlock(&devfs_de_interlock);
621 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
623 (void) devfs_allocv_drop_refs(1, dmp, de);
626 if (devfs_allocv_drop_refs(0, dmp, de)) {
631 mac_devfs_vnode_associate(mp, de, vp);
633 sx_xunlock(&dmp->dm_lock);
639 devfs_access(struct vop_access_args *ap)
641 struct vnode *vp = ap->a_vp;
642 struct devfs_dirent *de;
647 if (vp->v_type == VDIR)
650 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
651 ap->a_accmode, ap->a_cred);
656 p = ap->a_td->td_proc;
657 /* We do, however, allow access to the controlling terminal */
659 if (!(p->p_flag & P_CONTROLT)) {
663 if (p->p_session->s_ttydp == de->de_cdp)
669 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
670 "devfs-only flag reuse failed");
673 devfs_close(struct vop_close_args *ap)
675 struct vnode *vp = ap->a_vp, *oldvp;
676 struct thread *td = ap->a_td;
678 struct cdev *dev = vp->v_rdev;
680 struct devfs_dirent *de = vp->v_data;
681 int dflags, error, ref, vp_locked;
684 * XXX: Don't call d_close() if we were called because of
685 * XXX: insmntque1() failure.
687 if (vp->v_data == NULL)
691 * Hack: a tty device that is a controlling terminal
692 * has a reference from the session structure.
693 * We cannot easily tell that a character device is
694 * a controlling terminal, unless it is the closing
695 * process' controlling terminal. In that case,
696 * if the reference count is 2 (this last descriptor
697 * plus the session), release the reference from the session.
699 if (de->de_usecount == 2 && td != NULL) {
702 if (vp == p->p_session->s_ttyvp) {
705 sx_xlock(&proctree_lock);
706 if (vp == p->p_session->s_ttyvp) {
707 SESS_LOCK(p->p_session);
708 mtx_lock(&devfs_de_interlock);
710 if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
711 p->p_session->s_ttyvp = NULL;
712 p->p_session->s_ttydp = NULL;
716 mtx_unlock(&devfs_de_interlock);
717 SESS_UNLOCK(p->p_session);
719 sx_xunlock(&proctree_lock);
721 devfs_ctty_unref(oldvp);
726 * We do not want to really close the device if it
727 * is still in use unless we are trying to close it
728 * forcibly. Since every use (buffer, vnode, swap, cmap)
729 * holds a reference to the vnode, and because we mark
730 * any other vnodes that alias this device, when the
731 * sum of the reference counts on all the aliased
732 * vnodes descends to one, we are on last close.
734 dsw = dev_refthread(dev, &ref);
738 mtx_lock(&devfs_de_interlock);
740 if (devfs_usecountl(vp) == 1)
741 dflags |= FLASTCLOSE;
742 devfs_usecount_subl(vp);
743 mtx_unlock(&devfs_de_interlock);
744 if (VN_IS_DOOMED(vp)) {
746 dflags |= FREVOKE | FNONBLOCK;
747 } else if (dsw->d_flags & D_TRACKCLOSE) {
748 /* Keep device updated on status. */
749 } else if ((dflags & FLASTCLOSE) == 0) {
751 dev_relthread(dev, ref);
756 vp_locked = VOP_ISLOCKED(vp);
758 KASSERT(dev->si_refcount > 0,
759 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
760 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
761 dev_relthread(dev, ref);
762 vn_lock(vp, vp_locked | LK_RETRY);
768 devfs_close_f(struct file *fp, struct thread *td)
774 * NB: td may be NULL if this descriptor is closed due to
775 * garbage collection from a closed UNIX domain socket.
777 fpop = curthread->td_fpop;
778 curthread->td_fpop = fp;
779 error = vnops.fo_close(fp, td);
780 curthread->td_fpop = fpop;
783 * The f_cdevpriv cannot be assigned non-NULL value while we
784 * are destroying the file.
786 if (fp->f_cdevpriv != NULL)
792 devfs_getattr(struct vop_getattr_args *ap)
794 struct vnode *vp = ap->a_vp;
795 struct vattr *vap = ap->a_vap;
796 struct devfs_dirent *de;
797 struct devfs_mount *dmp;
799 struct timeval boottime;
802 error = devfs_populate_vp(vp);
806 dmp = VFSTODEVFS(vp->v_mount);
807 sx_xunlock(&dmp->dm_lock);
810 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
811 if (vp->v_type == VDIR) {
814 ("Null dir dirent in devfs_getattr vp=%p", vp));
816 vap->va_uid = de->de_uid;
817 vap->va_gid = de->de_gid;
818 vap->va_mode = de->de_mode;
819 if (vp->v_type == VLNK)
820 vap->va_size = strlen(de->de_symlink);
821 else if (vp->v_type == VDIR)
822 vap->va_size = vap->va_bytes = DEV_BSIZE;
825 if (vp->v_type != VDIR)
827 vap->va_blocksize = DEV_BSIZE;
828 vap->va_type = vp->v_type;
830 getboottime(&boottime);
833 if ((aa).tv_sec <= 3600) { \
834 (aa).tv_sec = boottime.tv_sec; \
835 (aa).tv_nsec = boottime.tv_usec * 1000; \
839 if (vp->v_type != VCHR) {
841 vap->va_atime = de->de_atime;
843 vap->va_mtime = de->de_mtime;
845 vap->va_ctime = de->de_ctime;
849 vap->va_atime = dev->si_atime;
851 vap->va_mtime = dev->si_mtime;
853 vap->va_ctime = dev->si_ctime;
855 vap->va_rdev = cdev2priv(dev)->cdp_inode;
860 vap->va_nlink = de->de_links;
861 vap->va_fileid = de->de_inode;
868 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
875 error = vnops.fo_ioctl(fp, com, data, cred, td);
881 fiodgname_buf_get_ptr(void *fgnp, u_long com)
884 struct fiodgname_arg fgn;
885 #ifdef COMPAT_FREEBSD32
886 struct fiodgname_arg32 fgn32;
893 return (fgnup->fgn.buf);
894 #ifdef COMPAT_FREEBSD32
896 return ((void *)(uintptr_t)fgnup->fgn32.buf);
899 panic("Unhandled ioctl command %ld", com);
904 devfs_ioctl(struct vop_ioctl_args *ap)
906 struct fiodgname_arg *fgn;
907 struct vnode *vpold, *vp;
910 struct session *sess;
920 dsw = devvn_refthread(vp, &dev, &ref);
923 KASSERT(dev->si_refcount > 0,
924 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
928 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
932 #ifdef COMPAT_FREEBSD32
941 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
944 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
947 dev_relthread(dev, ref);
948 if (error == ENOIOCTL)
951 if (error == 0 && com == TIOCSCTTY) {
953 * Do nothing if reassigning same control tty, or if the
954 * control tty has already disappeared. If it disappeared,
955 * it's because we were racing with TIOCNOTTY. TIOCNOTTY
956 * already took care of releasing the old vnode and we have
957 * nothing left to do.
959 sx_slock(&proctree_lock);
960 sess = td->td_proc->p_session;
961 if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
962 sx_sunlock(&proctree_lock);
968 vpold = sess->s_ttyvp;
970 sess->s_ttydp = cdev2priv(dev);
973 sx_sunlock(&proctree_lock);
975 /* Get rid of reference to old control tty */
977 devfs_ctty_unref(vpold);
984 devfs_kqfilter_f(struct file *fp, struct knote *kn)
994 error = devfs_fp_check(fp, &dev, &dsw, &ref);
997 error = dsw->d_kqfilter(dev, kn);
999 dev_relthread(dev, ref);
1004 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
1006 struct cdev_priv *cdp;
1014 dcr = cdp->cdp_c.si_cred;
1018 error = prison_check(td->td_ucred, dcr);
1021 /* We do, however, allow access to the controlling terminal */
1024 if (!(p->p_flag & P_CONTROLT)) {
1028 if (p->p_session->s_ttydp == cdp)
1035 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1037 struct componentname *cnp;
1038 struct vnode *dvp, **vpp;
1040 struct devfs_dirent *de, *dd;
1041 struct devfs_dirent **dde;
1042 struct devfs_mount *dmp;
1045 int error, flags, nameiop, dvplocked;
1046 char specname[SPECNAMELEN + 1], *pname;
1051 pname = cnp->cn_nameptr;
1052 td = cnp->cn_thread;
1053 flags = cnp->cn_flags;
1054 nameiop = cnp->cn_nameiop;
1056 dmp = VFSTODEVFS(mp);
1060 if ((flags & ISLASTCN) && nameiop == RENAME)
1061 return (EOPNOTSUPP);
1063 if (dvp->v_type != VDIR)
1066 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1069 error = vn_dir_check_exec(dvp, cnp);
1073 if (cnp->cn_namelen == 1 && *pname == '.') {
1074 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1081 if (flags & ISDOTDOT) {
1082 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1084 de = devfs_parent_dirent(dd);
1087 dvplocked = VOP_ISLOCKED(dvp);
1089 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1092 vn_lock(dvp, dvplocked | LK_RETRY);
1097 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1098 while (de == NULL) { /* While(...) so we can use break */
1100 if (nameiop == DELETE)
1104 * OK, we didn't have an entry for the name we were asked for
1105 * so we try to see if anybody can create it on demand.
1107 pname = devfs_fqpn(specname, dmp, dd, cnp);
1112 DEVFS_DMP_HOLD(dmp);
1113 sx_xunlock(&dmp->dm_lock);
1114 sx_slock(&clone_drain_lock);
1115 EVENTHANDLER_INVOKE(dev_clone,
1116 td->td_ucred, pname, strlen(pname), &cdev);
1117 sx_sunlock(&clone_drain_lock);
1120 sx_xlock(&dmp->dm_lock);
1121 else if (devfs_populate_vp(dvp) != 0) {
1123 sx_xlock(&dmp->dm_lock);
1124 if (DEVFS_DMP_DROP(dmp)) {
1125 sx_xunlock(&dmp->dm_lock);
1126 devfs_unmount_final(dmp);
1128 sx_xunlock(&dmp->dm_lock);
1132 if (DEVFS_DMP_DROP(dmp)) {
1134 sx_xunlock(&dmp->dm_lock);
1135 devfs_unmount_final(dmp);
1145 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1146 if (dde != NULL && *dde != NULL)
1153 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1154 if ((nameiop == CREATE || nameiop == RENAME) &&
1155 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1156 cnp->cn_flags |= SAVENAME;
1157 return (EJUSTRETURN);
1162 if (devfs_prison_check(de, td))
1165 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1166 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1175 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1181 devfs_lookup(struct vop_lookup_args *ap)
1184 struct devfs_mount *dmp;
1187 if (devfs_populate_vp(ap->a_dvp) != 0)
1190 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1192 j = devfs_lookupx(ap, &dm_unlock);
1194 sx_xunlock(&dmp->dm_lock);
1199 devfs_mknod(struct vop_mknod_args *ap)
1201 struct componentname *cnp;
1202 struct vnode *dvp, **vpp;
1203 struct devfs_dirent *dd, *de;
1204 struct devfs_mount *dmp;
1208 * The only type of node we should be creating here is a
1209 * character device, for anything else return EOPNOTSUPP.
1211 if (ap->a_vap->va_type != VCHR)
1212 return (EOPNOTSUPP);
1214 dmp = VFSTODEVFS(dvp->v_mount);
1221 sx_xlock(&dmp->dm_lock);
1222 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1223 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1225 if (de->de_dirent->d_type == DT_CHR &&
1226 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1228 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1229 de->de_dirent->d_namlen) != 0)
1231 if (de->de_flags & DE_WHITEOUT)
1237 de->de_flags &= ~DE_WHITEOUT;
1238 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1241 sx_xunlock(&dmp->dm_lock);
1247 devfs_open(struct vop_open_args *ap)
1249 struct thread *td = ap->a_td;
1250 struct vnode *vp = ap->a_vp;
1251 struct cdev *dev = vp->v_rdev;
1252 struct file *fp = ap->a_fp;
1253 int error, ref, vlocked;
1257 if (vp->v_type == VBLK)
1263 /* Make this field valid before any I/O in d_open. */
1264 if (dev->si_iosize_max == 0)
1265 dev->si_iosize_max = DFLTPHYS;
1267 dsw = dev_refthread(dev, &ref);
1270 if (fp == NULL && dsw->d_fdopen != NULL) {
1271 dev_relthread(dev, ref);
1275 if (vp->v_type == VCHR)
1276 devfs_usecount_add(vp);
1278 vlocked = VOP_ISLOCKED(vp);
1287 if (dsw->d_fdopen != NULL)
1288 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1290 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1291 /* Clean up any cdevpriv upon error. */
1293 devfs_clear_cdevpriv();
1296 vn_lock(vp, vlocked | LK_RETRY);
1297 if (error != 0 && vp->v_type == VCHR)
1298 devfs_usecount_sub(vp);
1300 dev_relthread(dev, ref);
1302 if (error == ERESTART)
1307 #if 0 /* /dev/console */
1308 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1313 if (fp->f_ops == &badfileops)
1314 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1319 devfs_pathconf(struct vop_pathconf_args *ap)
1322 switch (ap->a_name) {
1323 case _PC_FILESIZEBITS:
1327 *ap->a_retval = NAME_MAX;
1330 *ap->a_retval = INT_MAX;
1332 case _PC_SYMLINK_MAX:
1333 *ap->a_retval = MAXPATHLEN;
1336 if (ap->a_vp->v_vflag & VV_ISTTY) {
1337 *ap->a_retval = MAX_CANON;
1342 if (ap->a_vp->v_vflag & VV_ISTTY) {
1343 *ap->a_retval = MAX_INPUT;
1348 if (ap->a_vp->v_vflag & VV_ISTTY) {
1349 *ap->a_retval = _POSIX_VDISABLE;
1353 case _PC_MAC_PRESENT:
1356 * If MAC is enabled, devfs automatically supports
1357 * trivial non-persistant label storage.
1364 case _PC_CHOWN_RESTRICTED:
1368 return (vop_stdpathconf(ap));
1375 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1383 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1385 error = vnops.fo_poll(fp, events, cred, td);
1388 error = dsw->d_poll(dev, events, td);
1390 dev_relthread(dev, ref);
1395 * Print out the contents of a special device vnode.
1398 devfs_print(struct vop_print_args *ap)
1401 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1406 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1407 int flags, struct thread *td)
1410 int ioflag, error, ref;
1415 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1418 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1420 error = vnops.fo_read(fp, uio, cred, flags, td);
1423 resid = uio->uio_resid;
1424 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1425 if (ioflag & O_DIRECT)
1426 ioflag |= IO_DIRECT;
1428 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1429 error = dsw->d_read(dev, uio, ioflag);
1430 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1431 devfs_timestamp(&dev->si_atime);
1433 dev_relthread(dev, ref);
1435 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1440 devfs_readdir(struct vop_readdir_args *ap)
1445 struct devfs_dirent *dd;
1446 struct devfs_dirent *de;
1447 struct devfs_mount *dmp;
1449 int *tmp_ncookies = NULL;
1451 if (ap->a_vp->v_type != VDIR)
1455 if (uio->uio_offset < 0)
1459 * XXX: This is a temporary hack to get around this filesystem not
1460 * supporting cookies. We store the location of the ncookies pointer
1461 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1462 * and set the number of cookies to 0. We then set the pointer to
1463 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1464 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1465 * pointer to its original location before returning to the caller.
1467 if (ap->a_ncookies != NULL) {
1468 tmp_ncookies = ap->a_ncookies;
1469 *ap->a_ncookies = 0;
1470 ap->a_ncookies = NULL;
1473 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1474 if (devfs_populate_vp(ap->a_vp) != 0) {
1475 if (tmp_ncookies != NULL)
1476 ap->a_ncookies = tmp_ncookies;
1480 de = ap->a_vp->v_data;
1482 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1483 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1484 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1486 if (devfs_prison_check(dd, uio->uio_td))
1488 if (dd->de_dirent->d_type == DT_DIR)
1493 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1494 if (dp->d_reclen > uio->uio_resid)
1496 dp->d_fileno = de->de_inode;
1497 /* NOTE: d_off is the offset for the *next* entry. */
1498 dp->d_off = off + dp->d_reclen;
1499 if (off >= uio->uio_offset) {
1500 error = vfs_read_dirent(ap, dp, off);
1504 off += dp->d_reclen;
1506 sx_xunlock(&dmp->dm_lock);
1507 uio->uio_offset = off;
1510 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1513 if (tmp_ncookies != NULL)
1514 ap->a_ncookies = tmp_ncookies;
1520 devfs_readlink(struct vop_readlink_args *ap)
1522 struct devfs_dirent *de;
1524 de = ap->a_vp->v_data;
1525 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1529 devfs_reclaiml(struct vnode *vp)
1531 struct devfs_dirent *de;
1533 mtx_assert(&devfs_de_interlock, MA_OWNED);
1536 MPASS(de->de_usecount == 0);
1537 de->de_vnode = NULL;
1543 devfs_reclaim(struct vop_reclaim_args *ap)
1548 mtx_lock(&devfs_de_interlock);
1550 mtx_unlock(&devfs_de_interlock);
1555 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1561 MPASS(vp->v_type == VCHR);
1563 mtx_lock(&devfs_de_interlock);
1565 devfs_usecount_subl(vp);
1567 mtx_unlock(&devfs_de_interlock);
1579 devfs_remove(struct vop_remove_args *ap)
1581 struct vnode *dvp = ap->a_dvp;
1582 struct vnode *vp = ap->a_vp;
1583 struct devfs_dirent *dd;
1584 struct devfs_dirent *de, *de_covered;
1585 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1587 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1588 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1590 sx_xlock(&dmp->dm_lock);
1591 dd = ap->a_dvp->v_data;
1593 if (de->de_cdp == NULL) {
1594 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1595 if (de->de_dirent->d_type == DT_LNK) {
1596 de_covered = devfs_find(dd, de->de_dirent->d_name,
1597 de->de_dirent->d_namlen, 0);
1598 if (de_covered != NULL)
1599 de_covered->de_flags &= ~DE_COVERED;
1601 /* We need to unlock dvp because devfs_delete() may lock it. */
1605 devfs_delete(dmp, de, 0);
1606 sx_xunlock(&dmp->dm_lock);
1608 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1609 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1611 de->de_flags |= DE_WHITEOUT;
1612 sx_xunlock(&dmp->dm_lock);
1618 * Revoke is called on a tty when a terminal session ends. The vnode
1619 * is orphaned by setting v_op to deadfs so we need to let go of it
1620 * as well so that we create a new one next time around.
1624 devfs_revoke(struct vop_revoke_args *ap)
1626 struct vnode *vp = ap->a_vp, *vp2;
1628 struct cdev_priv *cdp;
1629 struct devfs_dirent *de;
1633 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1636 cdp = cdev2priv(dev);
1649 mtx_lock(&devfs_de_interlock);
1652 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1653 de = cdp->cdp_dirents[i];
1660 vs = vget_prep(vp2);
1661 mtx_unlock(&devfs_de_interlock);
1662 if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1675 mtx_unlock(&devfs_de_interlock);
1680 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1681 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1683 dev_rel(&cdp->cdp_c);
1687 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1692 devfs_rioctl(struct vop_ioctl_args *ap)
1695 struct devfs_mount *dmp;
1699 vn_lock(vp, LK_SHARED | LK_RETRY);
1700 if (VN_IS_DOOMED(vp)) {
1704 dmp = VFSTODEVFS(vp->v_mount);
1705 sx_xlock(&dmp->dm_lock);
1707 DEVFS_DMP_HOLD(dmp);
1708 devfs_populate(dmp);
1709 if (DEVFS_DMP_DROP(dmp)) {
1710 sx_xunlock(&dmp->dm_lock);
1711 devfs_unmount_final(dmp);
1714 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1715 sx_xunlock(&dmp->dm_lock);
1720 devfs_rread(struct vop_read_args *ap)
1723 if (ap->a_vp->v_type != VDIR)
1725 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1729 devfs_setattr(struct vop_setattr_args *ap)
1731 struct devfs_dirent *de;
1742 if ((vap->va_type != VNON) ||
1743 (vap->va_nlink != VNOVAL) ||
1744 (vap->va_fsid != VNOVAL) ||
1745 (vap->va_fileid != VNOVAL) ||
1746 (vap->va_blocksize != VNOVAL) ||
1747 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1748 (vap->va_rdev != VNOVAL) ||
1749 ((int)vap->va_bytes != VNOVAL) ||
1750 (vap->va_gen != VNOVAL)) {
1754 error = devfs_populate_vp(vp);
1759 if (vp->v_type == VDIR)
1763 if (vap->va_uid == (uid_t)VNOVAL)
1767 if (vap->va_gid == (gid_t)VNOVAL)
1771 if (uid != de->de_uid || gid != de->de_gid) {
1772 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1773 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1774 error = priv_check(td, PRIV_VFS_CHOWN);
1783 if (vap->va_mode != (mode_t)VNOVAL) {
1784 if (ap->a_cred->cr_uid != de->de_uid) {
1785 error = priv_check(td, PRIV_VFS_ADMIN);
1789 de->de_mode = vap->va_mode;
1793 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1794 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1797 if (vap->va_atime.tv_sec != VNOVAL) {
1798 if (vp->v_type == VCHR)
1799 vp->v_rdev->si_atime = vap->va_atime;
1801 de->de_atime = vap->va_atime;
1803 if (vap->va_mtime.tv_sec != VNOVAL) {
1804 if (vp->v_type == VCHR)
1805 vp->v_rdev->si_mtime = vap->va_mtime;
1807 de->de_mtime = vap->va_mtime;
1813 if (vp->v_type == VCHR)
1814 vfs_timestamp(&vp->v_rdev->si_ctime);
1816 vfs_timestamp(&de->de_mtime);
1820 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1826 devfs_setlabel(struct vop_setlabel_args *ap)
1829 struct devfs_dirent *de;
1834 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1835 mac_devfs_update(vp->v_mount, de, vp);
1842 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1845 return (vnops.fo_stat(fp, sb, cred, td));
1849 devfs_symlink(struct vop_symlink_args *ap)
1852 struct devfs_dirent *dd;
1853 struct devfs_dirent *de, *de_covered, *de_dotdot;
1854 struct devfs_mount *dmp;
1856 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1859 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1860 if (devfs_populate_vp(ap->a_dvp) != 0)
1863 dd = ap->a_dvp->v_data;
1864 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1865 de->de_flags = DE_USER;
1869 de->de_inode = alloc_unr(devfs_inos);
1871 de->de_dirent->d_type = DT_LNK;
1872 i = strlen(ap->a_target) + 1;
1873 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1874 bcopy(ap->a_target, de->de_symlink, i);
1876 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1878 de_covered = devfs_find(dd, de->de_dirent->d_name,
1879 de->de_dirent->d_namlen, 0);
1880 if (de_covered != NULL) {
1881 if ((de_covered->de_flags & DE_USER) != 0) {
1882 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1883 sx_xunlock(&dmp->dm_lock);
1886 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1887 ("devfs_symlink: entry %p already covered", de_covered));
1888 de_covered->de_flags |= DE_COVERED;
1891 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1892 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1893 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1894 devfs_dir_ref_de(dmp, dd);
1895 devfs_rules_apply(dmp, de);
1897 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1901 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1904 return (vnops.fo_truncate(fp, length, cred, td));
1908 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1909 int flags, struct thread *td)
1912 int error, ioflag, ref;
1917 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1920 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1922 error = vnops.fo_write(fp, uio, cred, flags, td);
1925 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1926 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1927 if (ioflag & O_DIRECT)
1928 ioflag |= IO_DIRECT;
1929 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1931 resid = uio->uio_resid;
1933 error = dsw->d_write(dev, uio, ioflag);
1934 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1935 devfs_timestamp(&dev->si_ctime);
1936 dev->si_mtime = dev->si_ctime;
1939 dev_relthread(dev, ref);
1941 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1946 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1947 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1962 * Ensure that file and memory protections are
1966 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1967 maxprot = VM_PROT_NONE;
1968 if ((prot & VM_PROT_EXECUTE) != 0)
1971 maxprot = VM_PROT_EXECUTE;
1972 if ((fp->f_flag & FREAD) != 0)
1973 maxprot |= VM_PROT_READ;
1974 else if ((prot & VM_PROT_READ) != 0)
1978 * If we are sharing potential changes via MAP_SHARED and we
1979 * are trying to get write permission although we opened it
1980 * without asking for it, bail out.
1982 * Note that most character devices always share mappings.
1983 * The one exception is that D_MMAP_ANON devices
1984 * (i.e. /dev/zero) permit private writable mappings.
1986 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1987 * as well as updating maxprot to permit writing for
1988 * D_MMAP_ANON devices rather than doing that here.
1990 if ((flags & MAP_SHARED) != 0) {
1991 if ((fp->f_flag & FWRITE) != 0)
1992 maxprot |= VM_PROT_WRITE;
1993 else if ((prot & VM_PROT_WRITE) != 0)
1996 maxprot &= cap_maxprot;
1999 error = devfs_fp_check(fp, &dev, &dsw, &ref);
2003 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
2006 dev_relthread(dev, ref);
2010 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
2013 vm_object_deallocate(object);
2018 dev2udev(struct cdev *x)
2022 return (cdev2priv(x)->cdp_inode);
2025 static struct fileops devfs_ops_f = {
2026 .fo_read = devfs_read_f,
2027 .fo_write = devfs_write_f,
2028 .fo_truncate = devfs_truncate_f,
2029 .fo_ioctl = devfs_ioctl_f,
2030 .fo_poll = devfs_poll_f,
2031 .fo_kqfilter = devfs_kqfilter_f,
2032 .fo_stat = devfs_stat_f,
2033 .fo_close = devfs_close_f,
2034 .fo_chmod = vn_chmod,
2035 .fo_chown = vn_chown,
2036 .fo_sendfile = vn_sendfile,
2038 .fo_fill_kinfo = vn_fill_kinfo,
2039 .fo_mmap = devfs_mmap_f,
2040 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
2043 /* Vops for non-CHR vnodes in /dev. */
2044 static struct vop_vector devfs_vnodeops = {
2045 .vop_default = &default_vnodeops,
2047 .vop_access = devfs_access,
2048 .vop_getattr = devfs_getattr,
2049 .vop_ioctl = devfs_rioctl,
2050 .vop_lookup = devfs_lookup,
2051 .vop_mknod = devfs_mknod,
2052 .vop_pathconf = devfs_pathconf,
2053 .vop_read = devfs_rread,
2054 .vop_readdir = devfs_readdir,
2055 .vop_readlink = devfs_readlink,
2056 .vop_reclaim = devfs_reclaim,
2057 .vop_remove = devfs_remove,
2058 .vop_revoke = devfs_revoke,
2059 .vop_setattr = devfs_setattr,
2061 .vop_setlabel = devfs_setlabel,
2063 .vop_symlink = devfs_symlink,
2064 .vop_vptocnp = devfs_vptocnp,
2065 .vop_lock1 = vop_lock,
2066 .vop_unlock = vop_unlock,
2067 .vop_islocked = vop_islocked,
2069 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2071 /* Vops for VCHR vnodes in /dev. */
2072 static struct vop_vector devfs_specops = {
2073 .vop_default = &default_vnodeops,
2075 .vop_access = devfs_access,
2076 .vop_bmap = VOP_PANIC,
2077 .vop_close = devfs_close,
2078 .vop_create = VOP_PANIC,
2079 .vop_fsync = vop_stdfsync,
2080 .vop_getattr = devfs_getattr,
2081 .vop_ioctl = devfs_ioctl,
2082 .vop_link = VOP_PANIC,
2083 .vop_mkdir = VOP_PANIC,
2084 .vop_mknod = VOP_PANIC,
2085 .vop_open = devfs_open,
2086 .vop_pathconf = devfs_pathconf,
2087 .vop_poll = dead_poll,
2088 .vop_print = devfs_print,
2089 .vop_read = dead_read,
2090 .vop_readdir = VOP_PANIC,
2091 .vop_readlink = VOP_PANIC,
2092 .vop_reallocblks = VOP_PANIC,
2093 .vop_reclaim = devfs_reclaim_vchr,
2094 .vop_remove = devfs_remove,
2095 .vop_rename = VOP_PANIC,
2096 .vop_revoke = devfs_revoke,
2097 .vop_rmdir = VOP_PANIC,
2098 .vop_setattr = devfs_setattr,
2100 .vop_setlabel = devfs_setlabel,
2102 .vop_strategy = VOP_PANIC,
2103 .vop_symlink = VOP_PANIC,
2104 .vop_vptocnp = devfs_vptocnp,
2105 .vop_write = dead_write,
2106 .vop_lock1 = vop_lock,
2107 .vop_unlock = vop_unlock,
2108 .vop_islocked = vop_islocked,
2110 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2113 * Our calling convention to the device drivers used to be that we passed
2114 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2115 * flags instead since that's what open(), close() and ioctl() takes and
2116 * we don't really want vnode.h in device drivers.
2117 * We solved the source compatibility by redefining some vnode flags to
2118 * be the same as the fcntl ones and by sending down the bitwise OR of
2119 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
2120 * pulls the rug out under this.
2122 CTASSERT(O_NONBLOCK == IO_NDELAY);
2123 CTASSERT(O_FSYNC == IO_SYNC);