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/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/filio.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/namei.h>
63 #include <sys/sysctl.h>
65 #include <sys/ttycom.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
69 static struct vop_vector devfs_vnodeops;
70 static struct vop_vector devfs_specops;
71 static struct fileops devfs_ops_f;
73 #include <fs/devfs/devfs.h>
74 #include <fs/devfs/devfs_int.h>
76 #include <security/mac/mac_framework.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_object.h>
82 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
84 struct mtx devfs_de_interlock;
85 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
86 struct sx clone_drain_lock;
87 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
88 struct mtx cdevpriv_mtx;
89 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
91 SYSCTL_DECL(_vfs_devfs);
93 static int devfs_dotimes;
94 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
95 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
98 * Update devfs node timestamp. Note that updates are unlocked and
99 * stat(2) could see partially updated times.
102 devfs_timestamp(struct timespec *tsp)
110 if (tsp->tv_sec != ts) {
118 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
122 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
123 if (*devp != fp->f_data) {
125 dev_relthread(*devp, *ref);
128 KASSERT((*devp)->si_refcount > 0,
129 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
132 curthread->td_fpop = fp;
137 devfs_get_cdevpriv(void **datap)
140 struct cdev_privdata *p;
143 fp = curthread->td_fpop;
149 *datap = p->cdpd_data;
156 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
159 struct cdev_priv *cdp;
160 struct cdev_privdata *p;
163 fp = curthread->td_fpop;
166 cdp = cdev2priv((struct cdev *)fp->f_data);
167 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
169 p->cdpd_dtr = priv_dtr;
171 mtx_lock(&cdevpriv_mtx);
172 if (fp->f_cdevpriv == NULL) {
173 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
175 mtx_unlock(&cdevpriv_mtx);
178 mtx_unlock(&cdevpriv_mtx);
179 free(p, M_CDEVPDATA);
186 devfs_destroy_cdevpriv(struct cdev_privdata *p)
189 mtx_assert(&cdevpriv_mtx, MA_OWNED);
190 KASSERT(p->cdpd_fp->f_cdevpriv == p,
191 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
192 p->cdpd_fp->f_cdevpriv = NULL;
193 LIST_REMOVE(p, cdpd_list);
194 mtx_unlock(&cdevpriv_mtx);
195 (p->cdpd_dtr)(p->cdpd_data);
196 free(p, M_CDEVPDATA);
200 devfs_fpdrop(struct file *fp)
202 struct cdev_privdata *p;
204 mtx_lock(&cdevpriv_mtx);
205 if ((p = fp->f_cdevpriv) == NULL) {
206 mtx_unlock(&cdevpriv_mtx);
209 devfs_destroy_cdevpriv(p);
213 devfs_clear_cdevpriv(void)
217 fp = curthread->td_fpop;
224 * On success devfs_populate_vp() returns with dmp->dm_lock held.
227 devfs_populate_vp(struct vnode *vp)
229 struct devfs_dirent *de;
230 struct devfs_mount *dmp;
233 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
235 dmp = VFSTODEVFS(vp->v_mount);
236 locked = VOP_ISLOCKED(vp);
238 sx_xlock(&dmp->dm_lock);
241 /* Can't call devfs_populate() with the vnode lock held. */
245 sx_xunlock(&dmp->dm_lock);
246 vn_lock(vp, locked | LK_RETRY);
247 sx_xlock(&dmp->dm_lock);
248 if (DEVFS_DMP_DROP(dmp)) {
249 sx_xunlock(&dmp->dm_lock);
250 devfs_unmount_final(dmp);
253 if ((vp->v_iflag & VI_DOOMED) != 0) {
254 sx_xunlock(&dmp->dm_lock);
259 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
260 if ((de->de_flags & DE_DOOMED) != 0) {
261 sx_xunlock(&dmp->dm_lock);
269 devfs_vptocnp(struct vop_vptocnp_args *ap)
271 struct vnode *vp = ap->a_vp;
272 struct vnode **dvp = ap->a_vpp;
273 struct devfs_mount *dmp;
274 char *buf = ap->a_buf;
275 int *buflen = ap->a_buflen;
276 struct devfs_dirent *dd, *de;
279 dmp = VFSTODEVFS(vp->v_mount);
281 error = devfs_populate_vp(vp);
288 if (vp->v_type == VCHR) {
289 i -= strlen(dd->de_cdp->cdp_c.si_name);
294 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
295 strlen(dd->de_cdp->cdp_c.si_name));
297 } else if (vp->v_type == VDIR) {
298 if (dd == dmp->dm_rootdir) {
303 i -= dd->de_dirent->d_namlen;
308 bcopy(dd->de_dirent->d_name, buf + i,
309 dd->de_dirent->d_namlen);
316 de = devfs_parent_dirent(de);
321 mtx_lock(&devfs_de_interlock);
325 mtx_unlock(&devfs_de_interlock);
331 mtx_unlock(&devfs_de_interlock);
335 sx_xunlock(&dmp->dm_lock);
340 * Construct the fully qualified path name relative to the mountpoint.
341 * If a NULL cnp is provided, no '/' is appended to the resulting path.
344 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
345 struct componentname *cnp)
348 struct devfs_dirent *de;
350 sx_assert(&dmp->dm_lock, SA_LOCKED);
355 i -= cnp->cn_namelen;
359 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
361 while (de != dmp->dm_rootdir) {
362 if (cnp != NULL || i < SPECNAMELEN) {
368 i -= de->de_dirent->d_namlen;
371 bcopy(de->de_dirent->d_name, buf + i,
372 de->de_dirent->d_namlen);
373 de = devfs_parent_dirent(de);
381 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
382 struct devfs_dirent *de)
387 if (de->de_flags & DE_DOOMED)
389 if (DEVFS_DE_DROP(de)) {
390 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
391 devfs_dirent_free(de);
393 if (DEVFS_DMP_DROP(dmp)) {
394 KASSERT(not_found == 1,
395 ("DEVFS mount struct freed before dirent"));
397 sx_xunlock(&dmp->dm_lock);
398 devfs_unmount_final(dmp);
400 if (not_found == 1 || (drop_dm_lock && not_found != 2))
401 sx_unlock(&dmp->dm_lock);
406 devfs_insmntque_dtr(struct vnode *vp, void *arg)
408 struct devfs_dirent *de;
410 de = (struct devfs_dirent *)arg;
411 mtx_lock(&devfs_de_interlock);
414 mtx_unlock(&devfs_de_interlock);
420 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
424 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
430 struct devfs_mount *dmp;
433 dmp = VFSTODEVFS(mp);
434 if (de->de_flags & DE_DOOMED) {
435 sx_xunlock(&dmp->dm_lock);
441 mtx_lock(&devfs_de_interlock);
445 mtx_unlock(&devfs_de_interlock);
446 sx_xunlock(&dmp->dm_lock);
447 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
448 sx_xlock(&dmp->dm_lock);
449 if (devfs_allocv_drop_refs(0, dmp, de)) {
453 else if ((vp->v_iflag & VI_DOOMED) != 0) {
454 mtx_lock(&devfs_de_interlock);
455 if (de->de_vnode == vp) {
459 mtx_unlock(&devfs_de_interlock);
463 sx_xunlock(&dmp->dm_lock);
467 mtx_unlock(&devfs_de_interlock);
468 if (de->de_dirent->d_type == DT_CHR) {
469 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
470 devfs_allocv_drop_refs(1, dmp, de);
473 dev = &de->de_cdp->cdp_c;
477 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
479 devfs_allocv_drop_refs(1, dmp, de);
480 printf("devfs_allocv: failed to allocate new vnode\n");
484 if (de->de_dirent->d_type == DT_CHR) {
489 /* XXX: v_rdev should be protect by vnode lock */
491 KASSERT(vp->v_usecount == 1,
492 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
493 dev->si_usecount += vp->v_usecount;
494 /* Special casing of ttys for deadfs. Probably redundant. */
496 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
497 vp->v_vflag |= VV_ISTTY;
500 if ((dev->si_flags & SI_ETERNAL) != 0)
501 vp->v_vflag |= VV_ETERNALDEV;
502 vp->v_op = &devfs_specops;
503 } else if (de->de_dirent->d_type == DT_DIR) {
505 } else if (de->de_dirent->d_type == DT_LNK) {
510 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
512 mtx_lock(&devfs_de_interlock);
515 mtx_unlock(&devfs_de_interlock);
516 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
518 (void) devfs_allocv_drop_refs(1, dmp, de);
521 if (devfs_allocv_drop_refs(0, dmp, de)) {
526 mac_devfs_vnode_associate(mp, de, vp);
528 sx_xunlock(&dmp->dm_lock);
534 devfs_access(struct vop_access_args *ap)
536 struct vnode *vp = ap->a_vp;
537 struct devfs_dirent *de;
542 if (vp->v_type == VDIR)
545 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
546 ap->a_accmode, ap->a_cred, NULL);
551 p = ap->a_td->td_proc;
552 /* We do, however, allow access to the controlling terminal */
554 if (!(p->p_flag & P_CONTROLT)) {
558 if (p->p_session->s_ttydp == de->de_cdp)
564 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
565 "devfs-only flag reuse failed");
568 devfs_close(struct vop_close_args *ap)
570 struct vnode *vp = ap->a_vp, *oldvp;
571 struct thread *td = ap->a_td;
573 struct cdev *dev = vp->v_rdev;
575 int dflags, error, ref, vp_locked;
578 * XXX: Don't call d_close() if we were called because of
579 * XXX: insmntque1() failure.
581 if (vp->v_data == NULL)
585 * Hack: a tty device that is a controlling terminal
586 * has a reference from the session structure.
587 * We cannot easily tell that a character device is
588 * a controlling terminal, unless it is the closing
589 * process' controlling terminal. In that case,
590 * if the reference count is 2 (this last descriptor
591 * plus the session), release the reference from the session.
596 if (vp == p->p_session->s_ttyvp) {
599 sx_xlock(&proctree_lock);
600 if (vp == p->p_session->s_ttyvp) {
601 SESS_LOCK(p->p_session);
603 if (count_dev(dev) == 2 &&
604 (vp->v_iflag & VI_DOOMED) == 0) {
605 p->p_session->s_ttyvp = NULL;
606 p->p_session->s_ttydp = NULL;
610 SESS_UNLOCK(p->p_session);
612 sx_xunlock(&proctree_lock);
619 * We do not want to really close the device if it
620 * is still in use unless we are trying to close it
621 * forcibly. Since every use (buffer, vnode, swap, cmap)
622 * holds a reference to the vnode, and because we mark
623 * any other vnodes that alias this device, when the
624 * sum of the reference counts on all the aliased
625 * vnodes descends to one, we are on last close.
627 dsw = dev_refthread(dev, &ref);
632 if (vp->v_iflag & VI_DOOMED) {
634 dflags |= FREVOKE | FNONBLOCK;
635 } else if (dsw->d_flags & D_TRACKCLOSE) {
636 /* Keep device updated on status. */
637 } else if (count_dev(dev) > 1) {
639 dev_relthread(dev, ref);
642 if (count_dev(dev) == 1)
643 dflags |= FLASTCLOSE;
646 vp_locked = VOP_ISLOCKED(vp);
648 KASSERT(dev->si_refcount > 0,
649 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
650 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
651 dev_relthread(dev, ref);
652 vn_lock(vp, vp_locked | LK_RETRY);
658 devfs_close_f(struct file *fp, struct thread *td)
664 * NB: td may be NULL if this descriptor is closed due to
665 * garbage collection from a closed UNIX domain socket.
667 fpop = curthread->td_fpop;
668 curthread->td_fpop = fp;
669 error = vnops.fo_close(fp, td);
670 curthread->td_fpop = fpop;
673 * The f_cdevpriv cannot be assigned non-NULL value while we
674 * are destroying the file.
676 if (fp->f_cdevpriv != NULL)
682 devfs_getattr(struct vop_getattr_args *ap)
684 struct vnode *vp = ap->a_vp;
685 struct vattr *vap = ap->a_vap;
686 struct devfs_dirent *de;
687 struct devfs_mount *dmp;
689 struct timeval boottime;
692 error = devfs_populate_vp(vp);
696 dmp = VFSTODEVFS(vp->v_mount);
697 sx_xunlock(&dmp->dm_lock);
700 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
701 if (vp->v_type == VDIR) {
704 ("Null dir dirent in devfs_getattr vp=%p", vp));
706 vap->va_uid = de->de_uid;
707 vap->va_gid = de->de_gid;
708 vap->va_mode = de->de_mode;
709 if (vp->v_type == VLNK)
710 vap->va_size = strlen(de->de_symlink);
711 else if (vp->v_type == VDIR)
712 vap->va_size = vap->va_bytes = DEV_BSIZE;
715 if (vp->v_type != VDIR)
717 vap->va_blocksize = DEV_BSIZE;
718 vap->va_type = vp->v_type;
720 getboottime(&boottime);
723 if ((aa).tv_sec <= 3600) { \
724 (aa).tv_sec = boottime.tv_sec; \
725 (aa).tv_nsec = boottime.tv_usec * 1000; \
729 if (vp->v_type != VCHR) {
731 vap->va_atime = de->de_atime;
733 vap->va_mtime = de->de_mtime;
735 vap->va_ctime = de->de_ctime;
739 vap->va_atime = dev->si_atime;
741 vap->va_mtime = dev->si_mtime;
743 vap->va_ctime = dev->si_ctime;
745 vap->va_rdev = cdev2priv(dev)->cdp_inode;
750 vap->va_nlink = de->de_links;
751 vap->va_fileid = de->de_inode;
758 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
765 error = vnops.fo_ioctl(fp, com, data, cred, td);
771 devfs_ioctl(struct vop_ioctl_args *ap)
773 struct fiodgname_arg *fgn;
774 struct vnode *vpold, *vp;
786 dsw = devvn_refthread(vp, &dev, &ref);
789 KASSERT(dev->si_refcount > 0,
790 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
792 if (com == FIODTYPE) {
793 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
796 } else if (com == FIODGNAME) {
803 error = copyout(p, fgn->buf, i);
807 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
810 dev_relthread(dev, ref);
811 if (error == ENOIOCTL)
814 if (error == 0 && com == TIOCSCTTY) {
815 /* Do nothing if reassigning same control tty */
816 sx_slock(&proctree_lock);
817 if (td->td_proc->p_session->s_ttyvp == vp) {
818 sx_sunlock(&proctree_lock);
822 vpold = td->td_proc->p_session->s_ttyvp;
824 SESS_LOCK(td->td_proc->p_session);
825 td->td_proc->p_session->s_ttyvp = vp;
826 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
827 SESS_UNLOCK(td->td_proc->p_session);
829 sx_sunlock(&proctree_lock);
831 /* Get rid of reference to old control tty */
840 devfs_kqfilter_f(struct file *fp, struct knote *kn)
850 error = devfs_fp_check(fp, &dev, &dsw, &ref);
853 error = dsw->d_kqfilter(dev, kn);
855 dev_relthread(dev, ref);
860 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
862 struct cdev_priv *cdp;
870 dcr = cdp->cdp_c.si_cred;
874 error = prison_check(td->td_ucred, dcr);
877 /* We do, however, allow access to the controlling terminal */
880 if (!(p->p_flag & P_CONTROLT)) {
884 if (p->p_session->s_ttydp == cdp)
891 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
893 struct componentname *cnp;
894 struct vnode *dvp, **vpp;
896 struct devfs_dirent *de, *dd;
897 struct devfs_dirent **dde;
898 struct devfs_mount *dmp;
901 int error, flags, nameiop, dvplocked;
902 char specname[SPECNAMELEN + 1], *pname;
907 pname = cnp->cn_nameptr;
909 flags = cnp->cn_flags;
910 nameiop = cnp->cn_nameiop;
912 dmp = VFSTODEVFS(mp);
916 if ((flags & ISLASTCN) && nameiop == RENAME)
919 if (dvp->v_type != VDIR)
922 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
925 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
929 if (cnp->cn_namelen == 1 && *pname == '.') {
930 if ((flags & ISLASTCN) && nameiop != LOOKUP)
937 if (flags & ISDOTDOT) {
938 if ((flags & ISLASTCN) && nameiop != LOOKUP)
940 de = devfs_parent_dirent(dd);
943 dvplocked = VOP_ISLOCKED(dvp);
945 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
948 vn_lock(dvp, dvplocked | LK_RETRY);
953 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
954 while (de == NULL) { /* While(...) so we can use break */
956 if (nameiop == DELETE)
960 * OK, we didn't have an entry for the name we were asked for
961 * so we try to see if anybody can create it on demand.
963 pname = devfs_fqpn(specname, dmp, dd, cnp);
969 sx_xunlock(&dmp->dm_lock);
970 sx_slock(&clone_drain_lock);
971 EVENTHANDLER_INVOKE(dev_clone,
972 td->td_ucred, pname, strlen(pname), &cdev);
973 sx_sunlock(&clone_drain_lock);
976 sx_xlock(&dmp->dm_lock);
977 else if (devfs_populate_vp(dvp) != 0) {
979 sx_xlock(&dmp->dm_lock);
980 if (DEVFS_DMP_DROP(dmp)) {
981 sx_xunlock(&dmp->dm_lock);
982 devfs_unmount_final(dmp);
984 sx_xunlock(&dmp->dm_lock);
988 if (DEVFS_DMP_DROP(dmp)) {
990 sx_xunlock(&dmp->dm_lock);
991 devfs_unmount_final(dmp);
1001 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1002 if (dde != NULL && *dde != NULL)
1009 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1010 if ((nameiop == CREATE || nameiop == RENAME) &&
1011 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1012 cnp->cn_flags |= SAVENAME;
1013 return (EJUSTRETURN);
1018 if (devfs_prison_check(de, td))
1021 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1022 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1031 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1037 devfs_lookup(struct vop_lookup_args *ap)
1040 struct devfs_mount *dmp;
1043 if (devfs_populate_vp(ap->a_dvp) != 0)
1046 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1048 j = devfs_lookupx(ap, &dm_unlock);
1050 sx_xunlock(&dmp->dm_lock);
1055 devfs_mknod(struct vop_mknod_args *ap)
1057 struct componentname *cnp;
1058 struct vnode *dvp, **vpp;
1059 struct devfs_dirent *dd, *de;
1060 struct devfs_mount *dmp;
1064 * The only type of node we should be creating here is a
1065 * character device, for anything else return EOPNOTSUPP.
1067 if (ap->a_vap->va_type != VCHR)
1068 return (EOPNOTSUPP);
1070 dmp = VFSTODEVFS(dvp->v_mount);
1077 sx_xlock(&dmp->dm_lock);
1078 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1079 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1081 if (de->de_dirent->d_type == DT_CHR &&
1082 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1084 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1085 de->de_dirent->d_namlen) != 0)
1087 if (de->de_flags & DE_WHITEOUT)
1093 de->de_flags &= ~DE_WHITEOUT;
1094 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1097 sx_xunlock(&dmp->dm_lock);
1103 devfs_open(struct vop_open_args *ap)
1105 struct thread *td = ap->a_td;
1106 struct vnode *vp = ap->a_vp;
1107 struct cdev *dev = vp->v_rdev;
1108 struct file *fp = ap->a_fp;
1109 int error, ref, vlocked;
1114 if (vp->v_type == VBLK)
1120 /* Make this field valid before any I/O in d_open. */
1121 if (dev->si_iosize_max == 0)
1122 dev->si_iosize_max = DFLTPHYS;
1124 dsw = dev_refthread(dev, &ref);
1127 if (fp == NULL && dsw->d_fdopen != NULL) {
1128 dev_relthread(dev, ref);
1132 vlocked = VOP_ISLOCKED(vp);
1141 if (dsw->d_fdopen != NULL)
1142 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1144 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1145 /* Clean up any cdevpriv upon error. */
1147 devfs_clear_cdevpriv();
1150 vn_lock(vp, vlocked | LK_RETRY);
1151 dev_relthread(dev, ref);
1153 if (error == ERESTART)
1158 #if 0 /* /dev/console */
1159 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1164 if (fp->f_ops == &badfileops)
1165 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1166 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1169 * Hint to the dofilewrite() to not force the buffer draining
1170 * on the writer to the file. Most likely, the write would
1171 * not need normal buffers.
1174 fp->f_vnread_flags |= FDEVFS_VNODE;
1180 devfs_pathconf(struct vop_pathconf_args *ap)
1183 switch (ap->a_name) {
1184 case _PC_FILESIZEBITS:
1188 *ap->a_retval = NAME_MAX;
1191 *ap->a_retval = INT_MAX;
1193 case _PC_SYMLINK_MAX:
1194 *ap->a_retval = MAXPATHLEN;
1197 if (ap->a_vp->v_vflag & VV_ISTTY) {
1198 *ap->a_retval = MAX_CANON;
1203 if (ap->a_vp->v_vflag & VV_ISTTY) {
1204 *ap->a_retval = MAX_INPUT;
1209 if (ap->a_vp->v_vflag & VV_ISTTY) {
1210 *ap->a_retval = _POSIX_VDISABLE;
1214 case _PC_MAC_PRESENT:
1217 * If MAC is enabled, devfs automatically supports
1218 * trivial non-persistant label storage.
1225 case _PC_CHOWN_RESTRICTED:
1229 return (vop_stdpathconf(ap));
1236 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1244 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1246 error = vnops.fo_poll(fp, events, cred, td);
1249 error = dsw->d_poll(dev, events, td);
1251 dev_relthread(dev, ref);
1256 * Print out the contents of a special device vnode.
1259 devfs_print(struct vop_print_args *ap)
1262 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1267 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1268 int flags, struct thread *td)
1271 int ioflag, error, ref;
1276 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1279 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1281 error = vnops.fo_read(fp, uio, cred, flags, td);
1284 resid = uio->uio_resid;
1285 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1286 if (ioflag & O_DIRECT)
1287 ioflag |= IO_DIRECT;
1289 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1290 error = dsw->d_read(dev, uio, ioflag);
1291 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1292 devfs_timestamp(&dev->si_atime);
1294 dev_relthread(dev, ref);
1296 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1301 devfs_readdir(struct vop_readdir_args *ap)
1306 struct devfs_dirent *dd;
1307 struct devfs_dirent *de;
1308 struct devfs_mount *dmp;
1310 int *tmp_ncookies = NULL;
1312 if (ap->a_vp->v_type != VDIR)
1316 if (uio->uio_offset < 0)
1320 * XXX: This is a temporary hack to get around this filesystem not
1321 * supporting cookies. We store the location of the ncookies pointer
1322 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1323 * and set the number of cookies to 0. We then set the pointer to
1324 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1325 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1326 * pointer to its original location before returning to the caller.
1328 if (ap->a_ncookies != NULL) {
1329 tmp_ncookies = ap->a_ncookies;
1330 *ap->a_ncookies = 0;
1331 ap->a_ncookies = NULL;
1334 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1335 if (devfs_populate_vp(ap->a_vp) != 0) {
1336 if (tmp_ncookies != NULL)
1337 ap->a_ncookies = tmp_ncookies;
1341 de = ap->a_vp->v_data;
1343 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1344 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1345 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1347 if (devfs_prison_check(dd, uio->uio_td))
1349 if (dd->de_dirent->d_type == DT_DIR)
1354 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1355 if (dp->d_reclen > uio->uio_resid)
1357 dp->d_fileno = de->de_inode;
1358 if (off >= uio->uio_offset) {
1359 error = vfs_read_dirent(ap, dp, off);
1363 off += dp->d_reclen;
1365 sx_xunlock(&dmp->dm_lock);
1366 uio->uio_offset = off;
1369 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1372 if (tmp_ncookies != NULL)
1373 ap->a_ncookies = tmp_ncookies;
1379 devfs_readlink(struct vop_readlink_args *ap)
1381 struct devfs_dirent *de;
1383 de = ap->a_vp->v_data;
1384 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1388 devfs_reclaim(struct vop_reclaim_args *ap)
1391 struct devfs_dirent *de;
1394 mtx_lock(&devfs_de_interlock);
1397 de->de_vnode = NULL;
1400 mtx_unlock(&devfs_de_interlock);
1401 vnode_destroy_vobject(vp);
1406 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1412 MPASS(vp->v_type == VCHR);
1421 dev->si_usecount -= vp->v_usecount;
1430 devfs_remove(struct vop_remove_args *ap)
1432 struct vnode *dvp = ap->a_dvp;
1433 struct vnode *vp = ap->a_vp;
1434 struct devfs_dirent *dd;
1435 struct devfs_dirent *de, *de_covered;
1436 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1438 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1439 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1441 sx_xlock(&dmp->dm_lock);
1442 dd = ap->a_dvp->v_data;
1444 if (de->de_cdp == NULL) {
1445 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1446 if (de->de_dirent->d_type == DT_LNK) {
1447 de_covered = devfs_find(dd, de->de_dirent->d_name,
1448 de->de_dirent->d_namlen, 0);
1449 if (de_covered != NULL)
1450 de_covered->de_flags &= ~DE_COVERED;
1452 /* We need to unlock dvp because devfs_delete() may lock it. */
1456 devfs_delete(dmp, de, 0);
1457 sx_xunlock(&dmp->dm_lock);
1459 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1462 de->de_flags |= DE_WHITEOUT;
1463 sx_xunlock(&dmp->dm_lock);
1469 * Revoke is called on a tty when a terminal session ends. The vnode
1470 * is orphaned by setting v_op to deadfs so we need to let go of it
1471 * as well so that we create a new one next time around.
1475 devfs_revoke(struct vop_revoke_args *ap)
1477 struct vnode *vp = ap->a_vp, *vp2;
1479 struct cdev_priv *cdp;
1480 struct devfs_dirent *de;
1483 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1486 cdp = cdev2priv(dev);
1499 mtx_lock(&devfs_de_interlock);
1502 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1503 de = cdp->cdp_dirents[i];
1511 mtx_unlock(&devfs_de_interlock);
1512 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1526 mtx_unlock(&devfs_de_interlock);
1531 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1532 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1534 dev_rel(&cdp->cdp_c);
1538 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1543 devfs_rioctl(struct vop_ioctl_args *ap)
1546 struct devfs_mount *dmp;
1550 vn_lock(vp, LK_SHARED | LK_RETRY);
1551 if (vp->v_iflag & VI_DOOMED) {
1555 dmp = VFSTODEVFS(vp->v_mount);
1556 sx_xlock(&dmp->dm_lock);
1558 DEVFS_DMP_HOLD(dmp);
1559 devfs_populate(dmp);
1560 if (DEVFS_DMP_DROP(dmp)) {
1561 sx_xunlock(&dmp->dm_lock);
1562 devfs_unmount_final(dmp);
1565 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1566 sx_xunlock(&dmp->dm_lock);
1571 devfs_rread(struct vop_read_args *ap)
1574 if (ap->a_vp->v_type != VDIR)
1576 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1580 devfs_setattr(struct vop_setattr_args *ap)
1582 struct devfs_dirent *de;
1593 if ((vap->va_type != VNON) ||
1594 (vap->va_nlink != VNOVAL) ||
1595 (vap->va_fsid != VNOVAL) ||
1596 (vap->va_fileid != VNOVAL) ||
1597 (vap->va_blocksize != VNOVAL) ||
1598 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1599 (vap->va_rdev != VNOVAL) ||
1600 ((int)vap->va_bytes != VNOVAL) ||
1601 (vap->va_gen != VNOVAL)) {
1605 error = devfs_populate_vp(vp);
1610 if (vp->v_type == VDIR)
1614 if (vap->va_uid == (uid_t)VNOVAL)
1618 if (vap->va_gid == (gid_t)VNOVAL)
1622 if (uid != de->de_uid || gid != de->de_gid) {
1623 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1624 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1625 error = priv_check(td, PRIV_VFS_CHOWN);
1634 if (vap->va_mode != (mode_t)VNOVAL) {
1635 if (ap->a_cred->cr_uid != de->de_uid) {
1636 error = priv_check(td, PRIV_VFS_ADMIN);
1640 de->de_mode = vap->va_mode;
1644 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1645 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1648 if (vap->va_atime.tv_sec != VNOVAL) {
1649 if (vp->v_type == VCHR)
1650 vp->v_rdev->si_atime = vap->va_atime;
1652 de->de_atime = vap->va_atime;
1654 if (vap->va_mtime.tv_sec != VNOVAL) {
1655 if (vp->v_type == VCHR)
1656 vp->v_rdev->si_mtime = vap->va_mtime;
1658 de->de_mtime = vap->va_mtime;
1664 if (vp->v_type == VCHR)
1665 vfs_timestamp(&vp->v_rdev->si_ctime);
1667 vfs_timestamp(&de->de_mtime);
1671 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1677 devfs_setlabel(struct vop_setlabel_args *ap)
1680 struct devfs_dirent *de;
1685 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1686 mac_devfs_update(vp->v_mount, de, vp);
1693 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1696 return (vnops.fo_stat(fp, sb, cred, td));
1700 devfs_symlink(struct vop_symlink_args *ap)
1703 struct devfs_dirent *dd;
1704 struct devfs_dirent *de, *de_covered, *de_dotdot;
1705 struct devfs_mount *dmp;
1707 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1710 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1711 if (devfs_populate_vp(ap->a_dvp) != 0)
1714 dd = ap->a_dvp->v_data;
1715 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1716 de->de_flags = DE_USER;
1720 de->de_inode = alloc_unr(devfs_inos);
1722 de->de_dirent->d_type = DT_LNK;
1723 i = strlen(ap->a_target) + 1;
1724 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1725 bcopy(ap->a_target, de->de_symlink, i);
1727 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1729 de_covered = devfs_find(dd, de->de_dirent->d_name,
1730 de->de_dirent->d_namlen, 0);
1731 if (de_covered != NULL) {
1732 if ((de_covered->de_flags & DE_USER) != 0) {
1733 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1734 sx_xunlock(&dmp->dm_lock);
1737 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1738 ("devfs_symlink: entry %p already covered", de_covered));
1739 de_covered->de_flags |= DE_COVERED;
1742 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1743 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1744 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1745 devfs_dir_ref_de(dmp, dd);
1746 devfs_rules_apply(dmp, de);
1748 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1752 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1755 return (vnops.fo_truncate(fp, length, cred, td));
1759 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1760 int flags, struct thread *td)
1763 int error, ioflag, ref;
1768 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1771 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1773 error = vnops.fo_write(fp, uio, cred, flags, td);
1776 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1777 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1778 if (ioflag & O_DIRECT)
1779 ioflag |= IO_DIRECT;
1780 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1782 resid = uio->uio_resid;
1784 error = dsw->d_write(dev, uio, ioflag);
1785 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1786 devfs_timestamp(&dev->si_ctime);
1787 dev->si_mtime = dev->si_ctime;
1790 dev_relthread(dev, ref);
1792 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1797 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1798 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1813 * Ensure that file and memory protections are
1817 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1818 maxprot = VM_PROT_NONE;
1819 if ((prot & VM_PROT_EXECUTE) != 0)
1822 maxprot = VM_PROT_EXECUTE;
1823 if ((fp->f_flag & FREAD) != 0)
1824 maxprot |= VM_PROT_READ;
1825 else if ((prot & VM_PROT_READ) != 0)
1829 * If we are sharing potential changes via MAP_SHARED and we
1830 * are trying to get write permission although we opened it
1831 * without asking for it, bail out.
1833 * Note that most character devices always share mappings.
1834 * The one exception is that D_MMAP_ANON devices
1835 * (i.e. /dev/zero) permit private writable mappings.
1837 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1838 * as well as updating maxprot to permit writing for
1839 * D_MMAP_ANON devices rather than doing that here.
1841 if ((flags & MAP_SHARED) != 0) {
1842 if ((fp->f_flag & FWRITE) != 0)
1843 maxprot |= VM_PROT_WRITE;
1844 else if ((prot & VM_PROT_WRITE) != 0)
1847 maxprot &= cap_maxprot;
1850 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1854 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1857 dev_relthread(dev, ref);
1861 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1864 vm_object_deallocate(object);
1869 dev2udev(struct cdev *x)
1873 return (cdev2priv(x)->cdp_inode);
1876 static struct fileops devfs_ops_f = {
1877 .fo_read = devfs_read_f,
1878 .fo_write = devfs_write_f,
1879 .fo_truncate = devfs_truncate_f,
1880 .fo_ioctl = devfs_ioctl_f,
1881 .fo_poll = devfs_poll_f,
1882 .fo_kqfilter = devfs_kqfilter_f,
1883 .fo_stat = devfs_stat_f,
1884 .fo_close = devfs_close_f,
1885 .fo_chmod = vn_chmod,
1886 .fo_chown = vn_chown,
1887 .fo_sendfile = vn_sendfile,
1889 .fo_fill_kinfo = vn_fill_kinfo,
1890 .fo_mmap = devfs_mmap_f,
1891 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1894 /* Vops for non-CHR vnodes in /dev. */
1895 static struct vop_vector devfs_vnodeops = {
1896 .vop_default = &default_vnodeops,
1898 .vop_access = devfs_access,
1899 .vop_getattr = devfs_getattr,
1900 .vop_ioctl = devfs_rioctl,
1901 .vop_lookup = devfs_lookup,
1902 .vop_mknod = devfs_mknod,
1903 .vop_pathconf = devfs_pathconf,
1904 .vop_read = devfs_rread,
1905 .vop_readdir = devfs_readdir,
1906 .vop_readlink = devfs_readlink,
1907 .vop_reclaim = devfs_reclaim,
1908 .vop_remove = devfs_remove,
1909 .vop_revoke = devfs_revoke,
1910 .vop_setattr = devfs_setattr,
1912 .vop_setlabel = devfs_setlabel,
1914 .vop_symlink = devfs_symlink,
1915 .vop_vptocnp = devfs_vptocnp,
1918 /* Vops for VCHR vnodes in /dev. */
1919 static struct vop_vector devfs_specops = {
1920 .vop_default = &default_vnodeops,
1922 .vop_access = devfs_access,
1923 .vop_bmap = VOP_PANIC,
1924 .vop_close = devfs_close,
1925 .vop_create = VOP_PANIC,
1926 .vop_fsync = vop_stdfsync,
1927 .vop_getattr = devfs_getattr,
1928 .vop_ioctl = devfs_ioctl,
1929 .vop_link = VOP_PANIC,
1930 .vop_mkdir = VOP_PANIC,
1931 .vop_mknod = VOP_PANIC,
1932 .vop_open = devfs_open,
1933 .vop_pathconf = devfs_pathconf,
1934 .vop_poll = dead_poll,
1935 .vop_print = devfs_print,
1936 .vop_read = dead_read,
1937 .vop_readdir = VOP_PANIC,
1938 .vop_readlink = VOP_PANIC,
1939 .vop_reallocblks = VOP_PANIC,
1940 .vop_reclaim = devfs_reclaim_vchr,
1941 .vop_remove = devfs_remove,
1942 .vop_rename = VOP_PANIC,
1943 .vop_revoke = devfs_revoke,
1944 .vop_rmdir = VOP_PANIC,
1945 .vop_setattr = devfs_setattr,
1947 .vop_setlabel = devfs_setlabel,
1949 .vop_strategy = VOP_PANIC,
1950 .vop_symlink = VOP_PANIC,
1951 .vop_vptocnp = devfs_vptocnp,
1952 .vop_write = dead_write,
1956 * Our calling convention to the device drivers used to be that we passed
1957 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1958 * flags instead since that's what open(), close() and ioctl() takes and
1959 * we don't really want vnode.h in device drivers.
1960 * We solved the source compatibility by redefining some vnode flags to
1961 * be the same as the fcntl ones and by sending down the bitwise OR of
1962 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1963 * pulls the rug out under this.
1965 CTASSERT(O_NONBLOCK == IO_NDELAY);
1966 CTASSERT(O_FSYNC == IO_SYNC);