2 * Copyright (c) 2000-2004
3 * Poul-Henning Kamp. All rights reserved.
4 * Copyright (c) 1989, 1992-1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
32 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/dirent.h>
46 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
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 sx clone_drain_lock;
85 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
86 struct mtx cdevpriv_mtx;
87 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
89 SYSCTL_DECL(_vfs_devfs);
91 static int devfs_dotimes;
92 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
93 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
96 * Update devfs node timestamp. Note that updates are unlocked and
97 * stat(2) could see partially updated times.
100 devfs_timestamp(struct timespec *tsp)
108 if (tsp->tv_sec != ts) {
116 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
120 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
121 if (*devp != fp->f_data) {
123 dev_relthread(*devp, *ref);
126 KASSERT((*devp)->si_refcount > 0,
127 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
130 curthread->td_fpop = fp;
135 devfs_get_cdevpriv(void **datap)
138 struct cdev_privdata *p;
141 fp = curthread->td_fpop;
147 *datap = p->cdpd_data;
154 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
157 struct cdev_priv *cdp;
158 struct cdev_privdata *p;
161 fp = curthread->td_fpop;
164 cdp = cdev2priv((struct cdev *)fp->f_data);
165 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
167 p->cdpd_dtr = priv_dtr;
169 mtx_lock(&cdevpriv_mtx);
170 if (fp->f_cdevpriv == NULL) {
171 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
173 mtx_unlock(&cdevpriv_mtx);
176 mtx_unlock(&cdevpriv_mtx);
177 free(p, M_CDEVPDATA);
184 devfs_destroy_cdevpriv(struct cdev_privdata *p)
187 mtx_assert(&cdevpriv_mtx, MA_OWNED);
188 p->cdpd_fp->f_cdevpriv = NULL;
189 LIST_REMOVE(p, cdpd_list);
190 mtx_unlock(&cdevpriv_mtx);
191 (p->cdpd_dtr)(p->cdpd_data);
192 free(p, M_CDEVPDATA);
196 devfs_fpdrop(struct file *fp)
198 struct cdev_privdata *p;
200 mtx_lock(&cdevpriv_mtx);
201 if ((p = fp->f_cdevpriv) == NULL) {
202 mtx_unlock(&cdevpriv_mtx);
205 devfs_destroy_cdevpriv(p);
209 devfs_clear_cdevpriv(void)
213 fp = curthread->td_fpop;
220 * On success devfs_populate_vp() returns with dmp->dm_lock held.
223 devfs_populate_vp(struct vnode *vp)
225 struct devfs_dirent *de;
226 struct devfs_mount *dmp;
229 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
231 dmp = VFSTODEVFS(vp->v_mount);
232 locked = VOP_ISLOCKED(vp);
234 sx_xlock(&dmp->dm_lock);
237 /* Can't call devfs_populate() with the vnode lock held. */
241 sx_xunlock(&dmp->dm_lock);
242 vn_lock(vp, locked | LK_RETRY);
243 sx_xlock(&dmp->dm_lock);
244 if (DEVFS_DMP_DROP(dmp)) {
245 sx_xunlock(&dmp->dm_lock);
246 devfs_unmount_final(dmp);
249 if ((vp->v_iflag & VI_DOOMED) != 0) {
250 sx_xunlock(&dmp->dm_lock);
255 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
256 if ((de->de_flags & DE_DOOMED) != 0) {
257 sx_xunlock(&dmp->dm_lock);
265 devfs_vptocnp(struct vop_vptocnp_args *ap)
267 struct vnode *vp = ap->a_vp;
268 struct vnode **dvp = ap->a_vpp;
269 struct devfs_mount *dmp;
270 char *buf = ap->a_buf;
271 int *buflen = ap->a_buflen;
272 struct devfs_dirent *dd, *de;
275 dmp = VFSTODEVFS(vp->v_mount);
277 error = devfs_populate_vp(vp);
284 if (vp->v_type == VCHR) {
285 i -= strlen(dd->de_cdp->cdp_c.si_name);
290 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
291 strlen(dd->de_cdp->cdp_c.si_name));
293 } else if (vp->v_type == VDIR) {
294 if (dd == dmp->dm_rootdir) {
299 i -= dd->de_dirent->d_namlen;
304 bcopy(dd->de_dirent->d_name, buf + i,
305 dd->de_dirent->d_namlen);
312 de = devfs_parent_dirent(de);
317 mtx_lock(&devfs_de_interlock);
321 mtx_unlock(&devfs_de_interlock);
327 mtx_unlock(&devfs_de_interlock);
331 sx_xunlock(&dmp->dm_lock);
336 * Construct the fully qualified path name relative to the mountpoint.
337 * If a NULL cnp is provided, no '/' is appended to the resulting path.
340 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
341 struct componentname *cnp)
344 struct devfs_dirent *de;
346 sx_assert(&dmp->dm_lock, SA_LOCKED);
351 i -= cnp->cn_namelen;
355 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
357 while (de != dmp->dm_rootdir) {
358 if (cnp != NULL || i < SPECNAMELEN) {
364 i -= de->de_dirent->d_namlen;
367 bcopy(de->de_dirent->d_name, buf + i,
368 de->de_dirent->d_namlen);
369 de = devfs_parent_dirent(de);
377 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
378 struct devfs_dirent *de)
383 if (de->de_flags & DE_DOOMED)
385 if (DEVFS_DE_DROP(de)) {
386 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
387 devfs_dirent_free(de);
389 if (DEVFS_DMP_DROP(dmp)) {
390 KASSERT(not_found == 1,
391 ("DEVFS mount struct freed before dirent"));
393 sx_xunlock(&dmp->dm_lock);
394 devfs_unmount_final(dmp);
396 if (not_found == 1 || (drop_dm_lock && not_found != 2))
397 sx_unlock(&dmp->dm_lock);
402 devfs_insmntque_dtr(struct vnode *vp, void *arg)
404 struct devfs_dirent *de;
406 de = (struct devfs_dirent *)arg;
407 mtx_lock(&devfs_de_interlock);
410 mtx_unlock(&devfs_de_interlock);
416 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
420 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
426 struct devfs_mount *dmp;
429 dmp = VFSTODEVFS(mp);
430 if (de->de_flags & DE_DOOMED) {
431 sx_xunlock(&dmp->dm_lock);
437 mtx_lock(&devfs_de_interlock);
441 mtx_unlock(&devfs_de_interlock);
442 sx_xunlock(&dmp->dm_lock);
443 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
444 sx_xlock(&dmp->dm_lock);
445 if (devfs_allocv_drop_refs(0, dmp, de)) {
449 else if ((vp->v_iflag & VI_DOOMED) != 0) {
450 mtx_lock(&devfs_de_interlock);
451 if (de->de_vnode == vp) {
455 mtx_unlock(&devfs_de_interlock);
459 sx_xunlock(&dmp->dm_lock);
463 mtx_unlock(&devfs_de_interlock);
464 if (de->de_dirent->d_type == DT_CHR) {
465 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
466 devfs_allocv_drop_refs(1, dmp, de);
469 dev = &de->de_cdp->cdp_c;
473 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
475 devfs_allocv_drop_refs(1, dmp, de);
476 printf("devfs_allocv: failed to allocate new vnode\n");
480 if (de->de_dirent->d_type == DT_CHR) {
485 /* XXX: v_rdev should be protect by vnode lock */
487 KASSERT(vp->v_usecount == 1,
488 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
489 dev->si_usecount += vp->v_usecount;
490 /* Special casing of ttys for deadfs. Probably redundant. */
492 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
493 vp->v_vflag |= VV_ISTTY;
496 if ((dev->si_flags & SI_ETERNAL) != 0)
497 vp->v_vflag |= VV_ETERNALDEV;
498 vp->v_op = &devfs_specops;
499 } else if (de->de_dirent->d_type == DT_DIR) {
501 } else if (de->de_dirent->d_type == DT_LNK) {
506 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
508 mtx_lock(&devfs_de_interlock);
511 mtx_unlock(&devfs_de_interlock);
512 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
514 (void) devfs_allocv_drop_refs(1, dmp, de);
517 if (devfs_allocv_drop_refs(0, dmp, de)) {
522 mac_devfs_vnode_associate(mp, de, vp);
524 sx_xunlock(&dmp->dm_lock);
530 devfs_access(struct vop_access_args *ap)
532 struct vnode *vp = ap->a_vp;
533 struct devfs_dirent *de;
538 if (vp->v_type == VDIR)
541 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
542 ap->a_accmode, ap->a_cred, NULL);
547 p = ap->a_td->td_proc;
548 /* We do, however, allow access to the controlling terminal */
550 if (!(p->p_flag & P_CONTROLT)) {
554 if (p->p_session->s_ttydp == de->de_cdp)
560 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
561 "devfs-only flag reuse failed");
564 devfs_close(struct vop_close_args *ap)
566 struct vnode *vp = ap->a_vp, *oldvp;
567 struct thread *td = ap->a_td;
569 struct cdev *dev = vp->v_rdev;
571 int dflags, error, ref, vp_locked;
574 * XXX: Don't call d_close() if we were called because of
575 * XXX: insmntque1() failure.
577 if (vp->v_data == NULL)
581 * Hack: a tty device that is a controlling terminal
582 * has a reference from the session structure.
583 * We cannot easily tell that a character device is
584 * a controlling terminal, unless it is the closing
585 * process' controlling terminal. In that case,
586 * if the reference count is 2 (this last descriptor
587 * plus the session), release the reference from the session.
592 if (vp == p->p_session->s_ttyvp) {
595 sx_xlock(&proctree_lock);
596 if (vp == p->p_session->s_ttyvp) {
597 SESS_LOCK(p->p_session);
599 if (count_dev(dev) == 2 &&
600 (vp->v_iflag & VI_DOOMED) == 0) {
601 p->p_session->s_ttyvp = NULL;
602 p->p_session->s_ttydp = NULL;
606 SESS_UNLOCK(p->p_session);
608 sx_xunlock(&proctree_lock);
615 * We do not want to really close the device if it
616 * is still in use unless we are trying to close it
617 * forcibly. Since every use (buffer, vnode, swap, cmap)
618 * holds a reference to the vnode, and because we mark
619 * any other vnodes that alias this device, when the
620 * sum of the reference counts on all the aliased
621 * vnodes descends to one, we are on last close.
623 dsw = dev_refthread(dev, &ref);
628 if (vp->v_iflag & VI_DOOMED) {
630 dflags |= FREVOKE | FNONBLOCK;
631 } else if (dsw->d_flags & D_TRACKCLOSE) {
632 /* Keep device updated on status. */
633 } else if (count_dev(dev) > 1) {
635 dev_relthread(dev, ref);
638 if (count_dev(dev) == 1)
639 dflags |= FLASTCLOSE;
642 vp_locked = VOP_ISLOCKED(vp);
644 KASSERT(dev->si_refcount > 0,
645 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
646 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
647 dev_relthread(dev, ref);
648 vn_lock(vp, vp_locked | LK_RETRY);
654 devfs_close_f(struct file *fp, struct thread *td)
660 * NB: td may be NULL if this descriptor is closed due to
661 * garbage collection from a closed UNIX domain socket.
663 fpop = curthread->td_fpop;
664 curthread->td_fpop = fp;
665 error = vnops.fo_close(fp, td);
666 curthread->td_fpop = fpop;
669 * The f_cdevpriv cannot be assigned non-NULL value while we
670 * are destroying the file.
672 if (fp->f_cdevpriv != NULL)
678 devfs_fsync(struct vop_fsync_args *ap)
682 struct devfs_dirent *de;
684 if (!vn_isdisk(ap->a_vp, &error)) {
685 bo = &ap->a_vp->v_bufobj;
686 de = ap->a_vp->v_data;
687 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
688 printf("Device %s went missing before all of the data "
689 "could be written to it; expect data loss.\n",
690 de->de_dirent->d_name);
692 error = vop_stdfsync(ap);
693 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
694 panic("devfs_fsync: vop_stdfsync failed.");
700 return (vop_stdfsync(ap));
704 devfs_getattr(struct vop_getattr_args *ap)
706 struct vnode *vp = ap->a_vp;
707 struct vattr *vap = ap->a_vap;
709 struct devfs_dirent *de;
710 struct devfs_mount *dmp;
713 error = devfs_populate_vp(vp);
717 dmp = VFSTODEVFS(vp->v_mount);
718 sx_xunlock(&dmp->dm_lock);
721 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
722 if (vp->v_type == VDIR) {
725 ("Null dir dirent in devfs_getattr vp=%p", vp));
727 vap->va_uid = de->de_uid;
728 vap->va_gid = de->de_gid;
729 vap->va_mode = de->de_mode;
730 if (vp->v_type == VLNK)
731 vap->va_size = strlen(de->de_symlink);
732 else if (vp->v_type == VDIR)
733 vap->va_size = vap->va_bytes = DEV_BSIZE;
736 if (vp->v_type != VDIR)
738 vap->va_blocksize = DEV_BSIZE;
739 vap->va_type = vp->v_type;
743 if ((aa).tv_sec <= 3600) { \
744 (aa).tv_sec = boottime.tv_sec; \
745 (aa).tv_nsec = boottime.tv_usec * 1000; \
749 if (vp->v_type != VCHR) {
751 vap->va_atime = de->de_atime;
753 vap->va_mtime = de->de_mtime;
755 vap->va_ctime = de->de_ctime;
759 vap->va_atime = dev->si_atime;
761 vap->va_mtime = dev->si_mtime;
763 vap->va_ctime = dev->si_ctime;
765 vap->va_rdev = cdev2priv(dev)->cdp_inode;
770 vap->va_nlink = de->de_links;
771 vap->va_fileid = de->de_inode;
778 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
786 struct fiodgname_arg *fgn;
790 error = devfs_fp_check(fp, &dev, &dsw, &ref);
792 error = vnops.fo_ioctl(fp, com, data, cred, td);
796 if (com == FIODTYPE) {
797 *(int *)data = dsw->d_flags & D_TYPEMASK;
799 dev_relthread(dev, ref);
801 } else if (com == FIODGNAME) {
808 error = copyout(p, fgn->buf, i);
810 dev_relthread(dev, ref);
813 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
815 dev_relthread(dev, ref);
816 if (error == ENOIOCTL)
818 if (error == 0 && com == TIOCSCTTY) {
821 /* Do nothing if reassigning same control tty */
822 sx_slock(&proctree_lock);
823 if (td->td_proc->p_session->s_ttyvp == vp) {
824 sx_sunlock(&proctree_lock);
828 vpold = td->td_proc->p_session->s_ttyvp;
830 SESS_LOCK(td->td_proc->p_session);
831 td->td_proc->p_session->s_ttyvp = vp;
832 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
833 SESS_UNLOCK(td->td_proc->p_session);
835 sx_sunlock(&proctree_lock);
837 /* Get rid of reference to old control tty */
846 devfs_kqfilter_f(struct file *fp, struct knote *kn)
856 error = devfs_fp_check(fp, &dev, &dsw, &ref);
859 error = dsw->d_kqfilter(dev, kn);
861 dev_relthread(dev, ref);
866 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
868 struct cdev_priv *cdp;
876 dcr = cdp->cdp_c.si_cred;
880 error = prison_check(td->td_ucred, dcr);
883 /* We do, however, allow access to the controlling terminal */
886 if (!(p->p_flag & P_CONTROLT)) {
890 if (p->p_session->s_ttydp == cdp)
897 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
899 struct componentname *cnp;
900 struct vnode *dvp, **vpp;
902 struct devfs_dirent *de, *dd;
903 struct devfs_dirent **dde;
904 struct devfs_mount *dmp;
906 int error, flags, nameiop, dvplocked;
907 char specname[SPECNAMELEN + 1], *pname;
912 pname = cnp->cn_nameptr;
914 flags = cnp->cn_flags;
915 nameiop = cnp->cn_nameiop;
916 dmp = VFSTODEVFS(dvp->v_mount);
920 if ((flags & ISLASTCN) && nameiop == RENAME)
923 if (dvp->v_type != VDIR)
926 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
929 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
933 if (cnp->cn_namelen == 1 && *pname == '.') {
934 if ((flags & ISLASTCN) && nameiop != LOOKUP)
941 if (flags & ISDOTDOT) {
942 if ((flags & ISLASTCN) && nameiop != LOOKUP)
944 de = devfs_parent_dirent(dd);
947 dvplocked = VOP_ISLOCKED(dvp);
949 error = devfs_allocv(de, dvp->v_mount,
950 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
952 vn_lock(dvp, dvplocked | LK_RETRY);
957 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
958 while (de == NULL) { /* While(...) so we can use break */
960 if (nameiop == DELETE)
964 * OK, we didn't have an entry for the name we were asked for
965 * so we try to see if anybody can create it on demand.
967 pname = devfs_fqpn(specname, dmp, dd, cnp);
973 sx_xunlock(&dmp->dm_lock);
974 sx_slock(&clone_drain_lock);
975 EVENTHANDLER_INVOKE(dev_clone,
976 td->td_ucred, pname, strlen(pname), &cdev);
977 sx_sunlock(&clone_drain_lock);
980 sx_xlock(&dmp->dm_lock);
981 else if (devfs_populate_vp(dvp) != 0) {
983 sx_xlock(&dmp->dm_lock);
984 if (DEVFS_DMP_DROP(dmp)) {
985 sx_xunlock(&dmp->dm_lock);
986 devfs_unmount_final(dmp);
988 sx_xunlock(&dmp->dm_lock);
992 if (DEVFS_DMP_DROP(dmp)) {
994 sx_xunlock(&dmp->dm_lock);
995 devfs_unmount_final(dmp);
1005 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1006 if (dde != NULL && *dde != NULL)
1013 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1014 if ((nameiop == CREATE || nameiop == RENAME) &&
1015 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1016 cnp->cn_flags |= SAVENAME;
1017 return (EJUSTRETURN);
1022 if (devfs_prison_check(de, td))
1025 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1026 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1035 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
1042 devfs_lookup(struct vop_lookup_args *ap)
1045 struct devfs_mount *dmp;
1048 if (devfs_populate_vp(ap->a_dvp) != 0)
1051 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1053 j = devfs_lookupx(ap, &dm_unlock);
1055 sx_xunlock(&dmp->dm_lock);
1060 devfs_mknod(struct vop_mknod_args *ap)
1062 struct componentname *cnp;
1063 struct vnode *dvp, **vpp;
1064 struct devfs_dirent *dd, *de;
1065 struct devfs_mount *dmp;
1069 * The only type of node we should be creating here is a
1070 * character device, for anything else return EOPNOTSUPP.
1072 if (ap->a_vap->va_type != VCHR)
1073 return (EOPNOTSUPP);
1075 dmp = VFSTODEVFS(dvp->v_mount);
1082 sx_xlock(&dmp->dm_lock);
1083 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1084 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1086 if (de->de_dirent->d_type == DT_CHR &&
1087 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1089 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1090 de->de_dirent->d_namlen) != 0)
1092 if (de->de_flags & DE_WHITEOUT)
1098 de->de_flags &= ~DE_WHITEOUT;
1099 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1102 sx_xunlock(&dmp->dm_lock);
1108 devfs_open(struct vop_open_args *ap)
1110 struct thread *td = ap->a_td;
1111 struct vnode *vp = ap->a_vp;
1112 struct cdev *dev = vp->v_rdev;
1113 struct file *fp = ap->a_fp;
1114 int error, ref, vlocked;
1119 if (vp->v_type == VBLK)
1125 /* Make this field valid before any I/O in d_open. */
1126 if (dev->si_iosize_max == 0)
1127 dev->si_iosize_max = DFLTPHYS;
1129 dsw = dev_refthread(dev, &ref);
1132 if (fp == NULL && dsw->d_fdopen != NULL) {
1133 dev_relthread(dev, ref);
1137 vlocked = VOP_ISLOCKED(vp);
1146 if (dsw->d_fdopen != NULL)
1147 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1149 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1150 /* cleanup any cdevpriv upon error */
1152 devfs_clear_cdevpriv();
1155 vn_lock(vp, vlocked | LK_RETRY);
1156 dev_relthread(dev, ref);
1158 if (error == ERESTART)
1163 #if 0 /* /dev/console */
1164 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1169 if (fp->f_ops == &badfileops)
1170 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1171 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1174 * Hint to the dofilewrite() to not force the buffer draining
1175 * on the writer to the file. Most likely, the write would
1176 * not need normal buffers.
1179 fp->f_vnread_flags |= FDEVFS_VNODE;
1185 devfs_pathconf(struct vop_pathconf_args *ap)
1188 switch (ap->a_name) {
1189 case _PC_MAC_PRESENT:
1192 * If MAC is enabled, devfs automatically supports
1193 * trivial non-persistant label storage.
1201 return (vop_stdpathconf(ap));
1208 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1216 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1218 error = vnops.fo_poll(fp, events, cred, td);
1221 error = dsw->d_poll(dev, events, td);
1223 dev_relthread(dev, ref);
1228 * Print out the contents of a special device vnode.
1231 devfs_print(struct vop_print_args *ap)
1234 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1239 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1240 int flags, struct thread *td)
1243 int ioflag, error, ref;
1248 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1251 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1253 error = vnops.fo_read(fp, uio, cred, flags, td);
1256 resid = uio->uio_resid;
1257 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1258 if (ioflag & O_DIRECT)
1259 ioflag |= IO_DIRECT;
1261 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1262 error = dsw->d_read(dev, uio, ioflag);
1263 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1264 devfs_timestamp(&dev->si_atime);
1266 dev_relthread(dev, ref);
1268 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1273 devfs_readdir(struct vop_readdir_args *ap)
1278 struct devfs_dirent *dd;
1279 struct devfs_dirent *de;
1280 struct devfs_mount *dmp;
1282 int *tmp_ncookies = NULL;
1284 if (ap->a_vp->v_type != VDIR)
1288 if (uio->uio_offset < 0)
1292 * XXX: This is a temporary hack to get around this filesystem not
1293 * supporting cookies. We store the location of the ncookies pointer
1294 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1295 * and set the number of cookies to 0. We then set the pointer to
1296 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1297 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1298 * pointer to its original location before returning to the caller.
1300 if (ap->a_ncookies != NULL) {
1301 tmp_ncookies = ap->a_ncookies;
1302 *ap->a_ncookies = 0;
1303 ap->a_ncookies = NULL;
1306 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1307 if (devfs_populate_vp(ap->a_vp) != 0) {
1308 if (tmp_ncookies != NULL)
1309 ap->a_ncookies = tmp_ncookies;
1313 de = ap->a_vp->v_data;
1315 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1316 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1317 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1319 if (devfs_prison_check(dd, uio->uio_td))
1321 if (dd->de_dirent->d_type == DT_DIR)
1326 if (dp->d_reclen > uio->uio_resid)
1328 dp->d_fileno = de->de_inode;
1329 if (off >= uio->uio_offset) {
1330 error = vfs_read_dirent(ap, dp, off);
1334 off += dp->d_reclen;
1336 sx_xunlock(&dmp->dm_lock);
1337 uio->uio_offset = off;
1340 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1343 if (tmp_ncookies != NULL)
1344 ap->a_ncookies = tmp_ncookies;
1350 devfs_readlink(struct vop_readlink_args *ap)
1352 struct devfs_dirent *de;
1354 de = ap->a_vp->v_data;
1355 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1359 devfs_reclaim(struct vop_reclaim_args *ap)
1361 struct vnode *vp = ap->a_vp;
1362 struct devfs_dirent *de;
1365 mtx_lock(&devfs_de_interlock);
1368 de->de_vnode = NULL;
1371 mtx_unlock(&devfs_de_interlock);
1373 vnode_destroy_vobject(vp);
1386 dev->si_usecount -= vp->v_usecount;
1394 devfs_remove(struct vop_remove_args *ap)
1396 struct vnode *dvp = ap->a_dvp;
1397 struct vnode *vp = ap->a_vp;
1398 struct devfs_dirent *dd;
1399 struct devfs_dirent *de, *de_covered;
1400 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1402 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1403 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1405 sx_xlock(&dmp->dm_lock);
1406 dd = ap->a_dvp->v_data;
1408 if (de->de_cdp == NULL) {
1409 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1410 if (de->de_dirent->d_type == DT_LNK) {
1411 de_covered = devfs_find(dd, de->de_dirent->d_name,
1412 de->de_dirent->d_namlen, 0);
1413 if (de_covered != NULL)
1414 de_covered->de_flags &= ~DE_COVERED;
1416 /* We need to unlock dvp because devfs_delete() may lock it. */
1420 devfs_delete(dmp, de, 0);
1421 sx_xunlock(&dmp->dm_lock);
1423 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1424 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1426 de->de_flags |= DE_WHITEOUT;
1427 sx_xunlock(&dmp->dm_lock);
1433 * Revoke is called on a tty when a terminal session ends. The vnode
1434 * is orphaned by setting v_op to deadfs so we need to let go of it
1435 * as well so that we create a new one next time around.
1439 devfs_revoke(struct vop_revoke_args *ap)
1441 struct vnode *vp = ap->a_vp, *vp2;
1443 struct cdev_priv *cdp;
1444 struct devfs_dirent *de;
1447 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1450 cdp = cdev2priv(dev);
1463 mtx_lock(&devfs_de_interlock);
1466 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1467 de = cdp->cdp_dirents[i];
1475 mtx_unlock(&devfs_de_interlock);
1476 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1490 mtx_unlock(&devfs_de_interlock);
1495 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1496 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1498 dev_rel(&cdp->cdp_c);
1502 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1507 devfs_rioctl(struct vop_ioctl_args *ap)
1510 struct devfs_mount *dmp;
1514 vn_lock(vp, LK_SHARED | LK_RETRY);
1515 if (vp->v_iflag & VI_DOOMED) {
1519 dmp = VFSTODEVFS(vp->v_mount);
1520 sx_xlock(&dmp->dm_lock);
1522 DEVFS_DMP_HOLD(dmp);
1523 devfs_populate(dmp);
1524 if (DEVFS_DMP_DROP(dmp)) {
1525 sx_xunlock(&dmp->dm_lock);
1526 devfs_unmount_final(dmp);
1529 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1530 sx_xunlock(&dmp->dm_lock);
1535 devfs_rread(struct vop_read_args *ap)
1538 if (ap->a_vp->v_type != VDIR)
1540 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1544 devfs_setattr(struct vop_setattr_args *ap)
1546 struct devfs_dirent *de;
1557 if ((vap->va_type != VNON) ||
1558 (vap->va_nlink != VNOVAL) ||
1559 (vap->va_fsid != VNOVAL) ||
1560 (vap->va_fileid != VNOVAL) ||
1561 (vap->va_blocksize != VNOVAL) ||
1562 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1563 (vap->va_rdev != VNOVAL) ||
1564 ((int)vap->va_bytes != VNOVAL) ||
1565 (vap->va_gen != VNOVAL)) {
1569 error = devfs_populate_vp(vp);
1574 if (vp->v_type == VDIR)
1578 if (vap->va_uid == (uid_t)VNOVAL)
1582 if (vap->va_gid == (gid_t)VNOVAL)
1586 if (uid != de->de_uid || gid != de->de_gid) {
1587 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1588 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1589 error = priv_check(td, PRIV_VFS_CHOWN);
1598 if (vap->va_mode != (mode_t)VNOVAL) {
1599 if (ap->a_cred->cr_uid != de->de_uid) {
1600 error = priv_check(td, PRIV_VFS_ADMIN);
1604 de->de_mode = vap->va_mode;
1608 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1609 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1612 if (vap->va_atime.tv_sec != VNOVAL) {
1613 if (vp->v_type == VCHR)
1614 vp->v_rdev->si_atime = vap->va_atime;
1616 de->de_atime = vap->va_atime;
1618 if (vap->va_mtime.tv_sec != VNOVAL) {
1619 if (vp->v_type == VCHR)
1620 vp->v_rdev->si_mtime = vap->va_mtime;
1622 de->de_mtime = vap->va_mtime;
1628 if (vp->v_type == VCHR)
1629 vfs_timestamp(&vp->v_rdev->si_ctime);
1631 vfs_timestamp(&de->de_mtime);
1635 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1641 devfs_setlabel(struct vop_setlabel_args *ap)
1644 struct devfs_dirent *de;
1649 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1650 mac_devfs_update(vp->v_mount, de, vp);
1657 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1660 return (vnops.fo_stat(fp, sb, cred, td));
1664 devfs_symlink(struct vop_symlink_args *ap)
1667 struct devfs_dirent *dd;
1668 struct devfs_dirent *de, *de_covered, *de_dotdot;
1669 struct devfs_mount *dmp;
1671 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1674 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1675 if (devfs_populate_vp(ap->a_dvp) != 0)
1678 dd = ap->a_dvp->v_data;
1679 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1680 de->de_flags = DE_USER;
1684 de->de_inode = alloc_unr(devfs_inos);
1686 de->de_dirent->d_type = DT_LNK;
1687 i = strlen(ap->a_target) + 1;
1688 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1689 bcopy(ap->a_target, de->de_symlink, i);
1691 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1693 de_covered = devfs_find(dd, de->de_dirent->d_name,
1694 de->de_dirent->d_namlen, 0);
1695 if (de_covered != NULL) {
1696 if ((de_covered->de_flags & DE_USER) != 0) {
1697 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1698 sx_xunlock(&dmp->dm_lock);
1701 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1702 ("devfs_symlink: entry %p already covered", de_covered));
1703 de_covered->de_flags |= DE_COVERED;
1706 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1707 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1708 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1709 devfs_dir_ref_de(dmp, dd);
1710 devfs_rules_apply(dmp, de);
1712 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1716 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1719 return (vnops.fo_truncate(fp, length, cred, td));
1723 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1724 int flags, struct thread *td)
1727 int error, ioflag, ref;
1732 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1735 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1737 error = vnops.fo_write(fp, uio, cred, flags, td);
1740 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1741 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1742 if (ioflag & O_DIRECT)
1743 ioflag |= IO_DIRECT;
1744 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1746 resid = uio->uio_resid;
1748 error = dsw->d_write(dev, uio, ioflag);
1749 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1750 devfs_timestamp(&dev->si_ctime);
1751 dev->si_mtime = dev->si_ctime;
1754 dev_relthread(dev, ref);
1756 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1761 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1762 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1777 * Ensure that file and memory protections are
1781 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0)
1782 maxprot = VM_PROT_NONE;
1784 maxprot = VM_PROT_EXECUTE;
1785 if ((fp->f_flag & FREAD) != 0)
1786 maxprot |= VM_PROT_READ;
1787 else if ((prot & VM_PROT_READ) != 0)
1791 * If we are sharing potential changes via MAP_SHARED and we
1792 * are trying to get write permission although we opened it
1793 * without asking for it, bail out.
1795 * Note that most character devices always share mappings.
1796 * The one exception is that D_MMAP_ANON devices
1797 * (i.e. /dev/zero) permit private writable mappings.
1799 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1800 * as well as updating maxprot to permit writing for
1801 * D_MMAP_ANON devices rather than doing that here.
1803 if ((flags & MAP_SHARED) != 0) {
1804 if ((fp->f_flag & FWRITE) != 0)
1805 maxprot |= VM_PROT_WRITE;
1806 else if ((prot & VM_PROT_WRITE) != 0)
1809 maxprot &= cap_maxprot;
1812 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1816 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1819 dev_relthread(dev, ref);
1823 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1826 vm_object_deallocate(object);
1831 dev2udev(struct cdev *x)
1835 return (cdev2priv(x)->cdp_inode);
1838 static struct fileops devfs_ops_f = {
1839 .fo_read = devfs_read_f,
1840 .fo_write = devfs_write_f,
1841 .fo_truncate = devfs_truncate_f,
1842 .fo_ioctl = devfs_ioctl_f,
1843 .fo_poll = devfs_poll_f,
1844 .fo_kqfilter = devfs_kqfilter_f,
1845 .fo_stat = devfs_stat_f,
1846 .fo_close = devfs_close_f,
1847 .fo_chmod = vn_chmod,
1848 .fo_chown = vn_chown,
1849 .fo_sendfile = vn_sendfile,
1851 .fo_fill_kinfo = vn_fill_kinfo,
1852 .fo_mmap = devfs_mmap_f,
1853 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1856 static struct vop_vector devfs_vnodeops = {
1857 .vop_default = &default_vnodeops,
1859 .vop_access = devfs_access,
1860 .vop_getattr = devfs_getattr,
1861 .vop_ioctl = devfs_rioctl,
1862 .vop_lookup = devfs_lookup,
1863 .vop_mknod = devfs_mknod,
1864 .vop_pathconf = devfs_pathconf,
1865 .vop_read = devfs_rread,
1866 .vop_readdir = devfs_readdir,
1867 .vop_readlink = devfs_readlink,
1868 .vop_reclaim = devfs_reclaim,
1869 .vop_remove = devfs_remove,
1870 .vop_revoke = devfs_revoke,
1871 .vop_setattr = devfs_setattr,
1873 .vop_setlabel = devfs_setlabel,
1875 .vop_symlink = devfs_symlink,
1876 .vop_vptocnp = devfs_vptocnp,
1879 static struct vop_vector devfs_specops = {
1880 .vop_default = &default_vnodeops,
1882 .vop_access = devfs_access,
1883 .vop_bmap = VOP_PANIC,
1884 .vop_close = devfs_close,
1885 .vop_create = VOP_PANIC,
1886 .vop_fsync = devfs_fsync,
1887 .vop_getattr = devfs_getattr,
1888 .vop_link = VOP_PANIC,
1889 .vop_mkdir = VOP_PANIC,
1890 .vop_mknod = VOP_PANIC,
1891 .vop_open = devfs_open,
1892 .vop_pathconf = devfs_pathconf,
1893 .vop_poll = dead_poll,
1894 .vop_print = devfs_print,
1895 .vop_read = dead_read,
1896 .vop_readdir = VOP_PANIC,
1897 .vop_readlink = VOP_PANIC,
1898 .vop_reallocblks = VOP_PANIC,
1899 .vop_reclaim = devfs_reclaim,
1900 .vop_remove = devfs_remove,
1901 .vop_rename = VOP_PANIC,
1902 .vop_revoke = devfs_revoke,
1903 .vop_rmdir = VOP_PANIC,
1904 .vop_setattr = devfs_setattr,
1906 .vop_setlabel = devfs_setlabel,
1908 .vop_strategy = VOP_PANIC,
1909 .vop_symlink = VOP_PANIC,
1910 .vop_vptocnp = devfs_vptocnp,
1911 .vop_write = dead_write,
1915 * Our calling convention to the device drivers used to be that we passed
1916 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1917 * flags instead since that's what open(), close() and ioctl() takes and
1918 * we don't really want vnode.h in device drivers.
1919 * We solved the source compatibility by redefining some vnode flags to
1920 * be the same as the fcntl ones and by sending down the bitwise OR of
1921 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1922 * pulls the rug out under this.
1924 CTASSERT(O_NONBLOCK == IO_NDELAY);
1925 CTASSERT(O_FSYNC == IO_SYNC);