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, cdevpriv_dtr_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)
562 devfs_close(struct vop_close_args *ap)
564 struct vnode *vp = ap->a_vp, *oldvp;
565 struct thread *td = ap->a_td;
567 struct cdev *dev = vp->v_rdev;
569 int vp_locked, error, ref;
572 * XXX: Don't call d_close() if we were called because of
573 * XXX: insmntque1() failure.
575 if (vp->v_data == NULL)
579 * Hack: a tty device that is a controlling terminal
580 * has a reference from the session structure.
581 * We cannot easily tell that a character device is
582 * a controlling terminal, unless it is the closing
583 * process' controlling terminal. In that case,
584 * if the reference count is 2 (this last descriptor
585 * plus the session), release the reference from the session.
590 if (vp == p->p_session->s_ttyvp) {
593 sx_xlock(&proctree_lock);
594 if (vp == p->p_session->s_ttyvp) {
595 SESS_LOCK(p->p_session);
597 if (count_dev(dev) == 2 &&
598 (vp->v_iflag & VI_DOOMED) == 0) {
599 p->p_session->s_ttyvp = NULL;
600 p->p_session->s_ttydp = NULL;
604 SESS_UNLOCK(p->p_session);
606 sx_xunlock(&proctree_lock);
613 * We do not want to really close the device if it
614 * is still in use unless we are trying to close it
615 * forcibly. Since every use (buffer, vnode, swap, cmap)
616 * holds a reference to the vnode, and because we mark
617 * any other vnodes that alias this device, when the
618 * sum of the reference counts on all the aliased
619 * vnodes descends to one, we are on last close.
621 dsw = dev_refthread(dev, &ref);
625 if (vp->v_iflag & VI_DOOMED) {
627 } else if (dsw->d_flags & D_TRACKCLOSE) {
628 /* Keep device updated on status. */
629 } else if (count_dev(dev) > 1) {
631 dev_relthread(dev, ref);
636 vp_locked = VOP_ISLOCKED(vp);
638 KASSERT(dev->si_refcount > 0,
639 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
640 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
641 dev_relthread(dev, ref);
642 vn_lock(vp, vp_locked | LK_RETRY);
648 devfs_close_f(struct file *fp, struct thread *td)
654 * NB: td may be NULL if this descriptor is closed due to
655 * garbage collection from a closed UNIX domain socket.
657 fpop = curthread->td_fpop;
658 curthread->td_fpop = fp;
659 error = vnops.fo_close(fp, td);
660 curthread->td_fpop = fpop;
663 * The f_cdevpriv cannot be assigned non-NULL value while we
664 * are destroying the file.
666 if (fp->f_cdevpriv != NULL)
672 devfs_fsync(struct vop_fsync_args *ap)
676 struct devfs_dirent *de;
678 if (!vn_isdisk(ap->a_vp, &error)) {
679 bo = &ap->a_vp->v_bufobj;
680 de = ap->a_vp->v_data;
681 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
682 printf("Device %s went missing before all of the data "
683 "could be written to it; expect data loss.\n",
684 de->de_dirent->d_name);
686 error = vop_stdfsync(ap);
687 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
688 panic("devfs_fsync: vop_stdfsync failed.");
694 return (vop_stdfsync(ap));
698 devfs_getattr(struct vop_getattr_args *ap)
700 struct vnode *vp = ap->a_vp;
701 struct vattr *vap = ap->a_vap;
703 struct devfs_dirent *de;
704 struct devfs_mount *dmp;
707 error = devfs_populate_vp(vp);
711 dmp = VFSTODEVFS(vp->v_mount);
712 sx_xunlock(&dmp->dm_lock);
715 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
716 if (vp->v_type == VDIR) {
719 ("Null dir dirent in devfs_getattr vp=%p", vp));
721 vap->va_uid = de->de_uid;
722 vap->va_gid = de->de_gid;
723 vap->va_mode = de->de_mode;
724 if (vp->v_type == VLNK)
725 vap->va_size = strlen(de->de_symlink);
726 else if (vp->v_type == VDIR)
727 vap->va_size = vap->va_bytes = DEV_BSIZE;
730 if (vp->v_type != VDIR)
732 vap->va_blocksize = DEV_BSIZE;
733 vap->va_type = vp->v_type;
737 if ((aa).tv_sec <= 3600) { \
738 (aa).tv_sec = boottime.tv_sec; \
739 (aa).tv_nsec = boottime.tv_usec * 1000; \
743 if (vp->v_type != VCHR) {
745 vap->va_atime = de->de_atime;
747 vap->va_mtime = de->de_mtime;
749 vap->va_ctime = de->de_ctime;
753 vap->va_atime = dev->si_atime;
755 vap->va_mtime = dev->si_mtime;
757 vap->va_ctime = dev->si_ctime;
759 vap->va_rdev = cdev2priv(dev)->cdp_inode;
764 vap->va_nlink = de->de_links;
765 vap->va_fileid = de->de_inode;
772 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
780 struct fiodgname_arg *fgn;
784 error = devfs_fp_check(fp, &dev, &dsw, &ref);
786 error = vnops.fo_ioctl(fp, com, data, cred, td);
790 if (com == FIODTYPE) {
791 *(int *)data = dsw->d_flags & D_TYPEMASK;
793 dev_relthread(dev, ref);
795 } else if (com == FIODGNAME) {
802 error = copyout(p, fgn->buf, i);
804 dev_relthread(dev, ref);
807 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
809 dev_relthread(dev, ref);
810 if (error == ENOIOCTL)
812 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;
900 int error, flags, nameiop, dvplocked;
901 char specname[SPECNAMELEN + 1], *pname;
906 pname = cnp->cn_nameptr;
908 flags = cnp->cn_flags;
909 nameiop = cnp->cn_nameiop;
910 dmp = VFSTODEVFS(dvp->v_mount);
914 if ((flags & ISLASTCN) && nameiop == RENAME)
917 if (dvp->v_type != VDIR)
920 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
923 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
927 if (cnp->cn_namelen == 1 && *pname == '.') {
928 if ((flags & ISLASTCN) && nameiop != LOOKUP)
935 if (flags & ISDOTDOT) {
936 if ((flags & ISLASTCN) && nameiop != LOOKUP)
938 de = devfs_parent_dirent(dd);
941 dvplocked = VOP_ISLOCKED(dvp);
943 error = devfs_allocv(de, dvp->v_mount,
944 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
946 vn_lock(dvp, dvplocked | LK_RETRY);
951 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
952 while (de == NULL) { /* While(...) so we can use break */
954 if (nameiop == DELETE)
958 * OK, we didn't have an entry for the name we were asked for
959 * so we try to see if anybody can create it on demand.
961 pname = devfs_fqpn(specname, dmp, dd, cnp);
967 sx_xunlock(&dmp->dm_lock);
968 sx_slock(&clone_drain_lock);
969 EVENTHANDLER_INVOKE(dev_clone,
970 td->td_ucred, pname, strlen(pname), &cdev);
971 sx_sunlock(&clone_drain_lock);
974 sx_xlock(&dmp->dm_lock);
975 else if (devfs_populate_vp(dvp) != 0) {
977 sx_xlock(&dmp->dm_lock);
978 if (DEVFS_DMP_DROP(dmp)) {
979 sx_xunlock(&dmp->dm_lock);
980 devfs_unmount_final(dmp);
982 sx_xunlock(&dmp->dm_lock);
986 if (DEVFS_DMP_DROP(dmp)) {
988 sx_xunlock(&dmp->dm_lock);
989 devfs_unmount_final(dmp);
999 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1000 if (dde != NULL && *dde != NULL)
1007 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1008 if ((nameiop == CREATE || nameiop == RENAME) &&
1009 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1010 cnp->cn_flags |= SAVENAME;
1011 return (EJUSTRETURN);
1016 if (devfs_prison_check(de, td))
1019 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1020 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1029 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
1036 devfs_lookup(struct vop_lookup_args *ap)
1039 struct devfs_mount *dmp;
1042 if (devfs_populate_vp(ap->a_dvp) != 0)
1045 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1047 j = devfs_lookupx(ap, &dm_unlock);
1049 sx_xunlock(&dmp->dm_lock);
1054 devfs_mknod(struct vop_mknod_args *ap)
1056 struct componentname *cnp;
1057 struct vnode *dvp, **vpp;
1058 struct devfs_dirent *dd, *de;
1059 struct devfs_mount *dmp;
1063 * The only type of node we should be creating here is a
1064 * character device, for anything else return EOPNOTSUPP.
1066 if (ap->a_vap->va_type != VCHR)
1067 return (EOPNOTSUPP);
1069 dmp = VFSTODEVFS(dvp->v_mount);
1076 sx_xlock(&dmp->dm_lock);
1077 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1078 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1080 if (de->de_dirent->d_type == DT_CHR &&
1081 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1083 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1084 de->de_dirent->d_namlen) != 0)
1086 if (de->de_flags & DE_WHITEOUT)
1092 de->de_flags &= ~DE_WHITEOUT;
1093 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1096 sx_xunlock(&dmp->dm_lock);
1102 devfs_open(struct vop_open_args *ap)
1104 struct thread *td = ap->a_td;
1105 struct vnode *vp = ap->a_vp;
1106 struct cdev *dev = vp->v_rdev;
1107 struct file *fp = ap->a_fp;
1108 int error, ref, vlocked;
1113 if (vp->v_type == VBLK)
1119 /* Make this field valid before any I/O in d_open. */
1120 if (dev->si_iosize_max == 0)
1121 dev->si_iosize_max = DFLTPHYS;
1123 dsw = dev_refthread(dev, &ref);
1126 if (fp == NULL && dsw->d_fdopen != NULL) {
1127 dev_relthread(dev, ref);
1131 vlocked = VOP_ISLOCKED(vp);
1140 if (dsw->d_fdopen != NULL)
1141 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1143 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1144 /* cleanup any cdevpriv upon error */
1146 devfs_clear_cdevpriv();
1149 vn_lock(vp, vlocked | LK_RETRY);
1150 dev_relthread(dev, ref);
1152 if (error == ERESTART)
1157 #if 0 /* /dev/console */
1158 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1163 if (fp->f_ops == &badfileops)
1164 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1165 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1168 * Hint to the dofilewrite() to not force the buffer draining
1169 * on the writer to the file. Most likely, the write would
1170 * not need normal buffers.
1173 fp->f_vnread_flags |= FDEVFS_VNODE;
1179 devfs_pathconf(struct vop_pathconf_args *ap)
1182 switch (ap->a_name) {
1183 case _PC_MAC_PRESENT:
1186 * If MAC is enabled, devfs automatically supports
1187 * trivial non-persistant label storage.
1195 return (vop_stdpathconf(ap));
1202 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1210 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1212 error = vnops.fo_poll(fp, events, cred, td);
1215 error = dsw->d_poll(dev, events, td);
1217 dev_relthread(dev, ref);
1222 * Print out the contents of a special device vnode.
1225 devfs_print(struct vop_print_args *ap)
1228 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1233 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1234 int flags, struct thread *td)
1237 int ioflag, error, ref;
1242 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1245 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1247 error = vnops.fo_read(fp, uio, cred, flags, td);
1250 resid = uio->uio_resid;
1251 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1252 if (ioflag & O_DIRECT)
1253 ioflag |= IO_DIRECT;
1255 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1256 error = dsw->d_read(dev, uio, ioflag);
1257 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1258 devfs_timestamp(&dev->si_atime);
1260 dev_relthread(dev, ref);
1262 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1267 devfs_readdir(struct vop_readdir_args *ap)
1272 struct devfs_dirent *dd;
1273 struct devfs_dirent *de;
1274 struct devfs_mount *dmp;
1276 int *tmp_ncookies = NULL;
1278 if (ap->a_vp->v_type != VDIR)
1282 if (uio->uio_offset < 0)
1286 * XXX: This is a temporary hack to get around this filesystem not
1287 * supporting cookies. We store the location of the ncookies pointer
1288 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1289 * and set the number of cookies to 0. We then set the pointer to
1290 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1291 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1292 * pointer to its original location before returning to the caller.
1294 if (ap->a_ncookies != NULL) {
1295 tmp_ncookies = ap->a_ncookies;
1296 *ap->a_ncookies = 0;
1297 ap->a_ncookies = NULL;
1300 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1301 if (devfs_populate_vp(ap->a_vp) != 0) {
1302 if (tmp_ncookies != NULL)
1303 ap->a_ncookies = tmp_ncookies;
1307 de = ap->a_vp->v_data;
1309 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1310 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1311 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1313 if (devfs_prison_check(dd, uio->uio_td))
1315 if (dd->de_dirent->d_type == DT_DIR)
1320 if (dp->d_reclen > uio->uio_resid)
1322 dp->d_fileno = de->de_inode;
1323 if (off >= uio->uio_offset) {
1324 error = vfs_read_dirent(ap, dp, off);
1328 off += dp->d_reclen;
1330 sx_xunlock(&dmp->dm_lock);
1331 uio->uio_offset = off;
1334 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1337 if (tmp_ncookies != NULL)
1338 ap->a_ncookies = tmp_ncookies;
1344 devfs_readlink(struct vop_readlink_args *ap)
1346 struct devfs_dirent *de;
1348 de = ap->a_vp->v_data;
1349 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1353 devfs_reclaim(struct vop_reclaim_args *ap)
1355 struct vnode *vp = ap->a_vp;
1356 struct devfs_dirent *de;
1359 mtx_lock(&devfs_de_interlock);
1362 de->de_vnode = NULL;
1365 mtx_unlock(&devfs_de_interlock);
1367 vnode_destroy_vobject(vp);
1380 dev->si_usecount -= vp->v_usecount;
1388 devfs_remove(struct vop_remove_args *ap)
1390 struct vnode *dvp = ap->a_dvp;
1391 struct vnode *vp = ap->a_vp;
1392 struct devfs_dirent *dd;
1393 struct devfs_dirent *de, *de_covered;
1394 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1396 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1397 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1399 sx_xlock(&dmp->dm_lock);
1400 dd = ap->a_dvp->v_data;
1402 if (de->de_cdp == NULL) {
1403 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1404 if (de->de_dirent->d_type == DT_LNK) {
1405 de_covered = devfs_find(dd, de->de_dirent->d_name,
1406 de->de_dirent->d_namlen, 0);
1407 if (de_covered != NULL)
1408 de_covered->de_flags &= ~DE_COVERED;
1410 /* We need to unlock dvp because devfs_delete() may lock it. */
1414 devfs_delete(dmp, de, 0);
1415 sx_xunlock(&dmp->dm_lock);
1417 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1418 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1420 de->de_flags |= DE_WHITEOUT;
1421 sx_xunlock(&dmp->dm_lock);
1427 * Revoke is called on a tty when a terminal session ends. The vnode
1428 * is orphaned by setting v_op to deadfs so we need to let go of it
1429 * as well so that we create a new one next time around.
1433 devfs_revoke(struct vop_revoke_args *ap)
1435 struct vnode *vp = ap->a_vp, *vp2;
1437 struct cdev_priv *cdp;
1438 struct devfs_dirent *de;
1441 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1444 cdp = cdev2priv(dev);
1457 mtx_lock(&devfs_de_interlock);
1460 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1461 de = cdp->cdp_dirents[i];
1469 mtx_unlock(&devfs_de_interlock);
1470 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1484 mtx_unlock(&devfs_de_interlock);
1489 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1490 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1492 dev_rel(&cdp->cdp_c);
1496 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1501 devfs_rioctl(struct vop_ioctl_args *ap)
1504 struct devfs_mount *dmp;
1508 vn_lock(vp, LK_SHARED | LK_RETRY);
1509 if (vp->v_iflag & VI_DOOMED) {
1513 dmp = VFSTODEVFS(vp->v_mount);
1514 sx_xlock(&dmp->dm_lock);
1516 DEVFS_DMP_HOLD(dmp);
1517 devfs_populate(dmp);
1518 if (DEVFS_DMP_DROP(dmp)) {
1519 sx_xunlock(&dmp->dm_lock);
1520 devfs_unmount_final(dmp);
1523 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1524 sx_xunlock(&dmp->dm_lock);
1529 devfs_rread(struct vop_read_args *ap)
1532 if (ap->a_vp->v_type != VDIR)
1534 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1538 devfs_setattr(struct vop_setattr_args *ap)
1540 struct devfs_dirent *de;
1551 if ((vap->va_type != VNON) ||
1552 (vap->va_nlink != VNOVAL) ||
1553 (vap->va_fsid != VNOVAL) ||
1554 (vap->va_fileid != VNOVAL) ||
1555 (vap->va_blocksize != VNOVAL) ||
1556 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1557 (vap->va_rdev != VNOVAL) ||
1558 ((int)vap->va_bytes != VNOVAL) ||
1559 (vap->va_gen != VNOVAL)) {
1564 if (vp->v_type == VDIR)
1568 if (vap->va_uid == (uid_t)VNOVAL)
1572 if (vap->va_gid == (gid_t)VNOVAL)
1576 if (uid != de->de_uid || gid != de->de_gid) {
1577 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1578 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1579 error = priv_check(td, PRIV_VFS_CHOWN);
1588 if (vap->va_mode != (mode_t)VNOVAL) {
1589 if (ap->a_cred->cr_uid != de->de_uid) {
1590 error = priv_check(td, PRIV_VFS_ADMIN);
1594 de->de_mode = vap->va_mode;
1598 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1599 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1602 if (vap->va_atime.tv_sec != VNOVAL) {
1603 if (vp->v_type == VCHR)
1604 vp->v_rdev->si_atime = vap->va_atime;
1606 de->de_atime = vap->va_atime;
1608 if (vap->va_mtime.tv_sec != VNOVAL) {
1609 if (vp->v_type == VCHR)
1610 vp->v_rdev->si_mtime = vap->va_mtime;
1612 de->de_mtime = vap->va_mtime;
1618 if (vp->v_type == VCHR)
1619 vfs_timestamp(&vp->v_rdev->si_ctime);
1621 vfs_timestamp(&de->de_mtime);
1628 devfs_setlabel(struct vop_setlabel_args *ap)
1631 struct devfs_dirent *de;
1636 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1637 mac_devfs_update(vp->v_mount, de, vp);
1644 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1647 return (vnops.fo_stat(fp, sb, cred, td));
1651 devfs_symlink(struct vop_symlink_args *ap)
1654 struct devfs_dirent *dd;
1655 struct devfs_dirent *de, *de_covered, *de_dotdot;
1656 struct devfs_mount *dmp;
1658 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1661 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1662 if (devfs_populate_vp(ap->a_dvp) != 0)
1665 dd = ap->a_dvp->v_data;
1666 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1667 de->de_flags = DE_USER;
1671 de->de_inode = alloc_unr(devfs_inos);
1673 de->de_dirent->d_type = DT_LNK;
1674 i = strlen(ap->a_target) + 1;
1675 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1676 bcopy(ap->a_target, de->de_symlink, i);
1678 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1680 de_covered = devfs_find(dd, de->de_dirent->d_name,
1681 de->de_dirent->d_namlen, 0);
1682 if (de_covered != NULL) {
1683 if ((de_covered->de_flags & DE_USER) != 0) {
1684 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1685 sx_xunlock(&dmp->dm_lock);
1688 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1689 ("devfs_symlink: entry %p already covered", de_covered));
1690 de_covered->de_flags |= DE_COVERED;
1693 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1694 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1695 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1696 devfs_dir_ref_de(dmp, dd);
1697 devfs_rules_apply(dmp, de);
1699 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1703 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1706 return (vnops.fo_truncate(fp, length, cred, td));
1710 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1711 int flags, struct thread *td)
1714 int error, ioflag, ref;
1719 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1722 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1724 error = vnops.fo_write(fp, uio, cred, flags, td);
1727 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1728 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1729 if (ioflag & O_DIRECT)
1730 ioflag |= IO_DIRECT;
1731 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1733 resid = uio->uio_resid;
1735 error = dsw->d_write(dev, uio, ioflag);
1736 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1737 devfs_timestamp(&dev->si_ctime);
1738 dev->si_mtime = dev->si_ctime;
1741 dev_relthread(dev, ref);
1743 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1748 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1749 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1764 * Ensure that file and memory protections are
1768 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0)
1769 maxprot = VM_PROT_NONE;
1771 maxprot = VM_PROT_EXECUTE;
1772 if ((fp->f_flag & FREAD) != 0)
1773 maxprot |= VM_PROT_READ;
1774 else if ((prot & VM_PROT_READ) != 0)
1778 * If we are sharing potential changes via MAP_SHARED and we
1779 * are trying to get write permission although we opened it
1780 * without asking for it, bail out.
1782 * Note that most character devices always share mappings.
1783 * The one exception is that D_MMAP_ANON devices
1784 * (i.e. /dev/zero) permit private writable mappings.
1786 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1787 * as well as updating maxprot to permit writing for
1788 * D_MMAP_ANON devices rather than doing that here.
1790 if ((flags & MAP_SHARED) != 0) {
1791 if ((fp->f_flag & FWRITE) != 0)
1792 maxprot |= VM_PROT_WRITE;
1793 else if ((prot & VM_PROT_WRITE) != 0)
1796 maxprot &= cap_maxprot;
1799 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1803 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1806 dev_relthread(dev, ref);
1810 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1813 vm_object_deallocate(object);
1818 dev2udev(struct cdev *x)
1822 return (cdev2priv(x)->cdp_inode);
1825 static struct fileops devfs_ops_f = {
1826 .fo_read = devfs_read_f,
1827 .fo_write = devfs_write_f,
1828 .fo_truncate = devfs_truncate_f,
1829 .fo_ioctl = devfs_ioctl_f,
1830 .fo_poll = devfs_poll_f,
1831 .fo_kqfilter = devfs_kqfilter_f,
1832 .fo_stat = devfs_stat_f,
1833 .fo_close = devfs_close_f,
1834 .fo_chmod = vn_chmod,
1835 .fo_chown = vn_chown,
1836 .fo_sendfile = vn_sendfile,
1838 .fo_fill_kinfo = vn_fill_kinfo,
1839 .fo_mmap = devfs_mmap_f,
1840 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1843 static struct vop_vector devfs_vnodeops = {
1844 .vop_default = &default_vnodeops,
1846 .vop_access = devfs_access,
1847 .vop_getattr = devfs_getattr,
1848 .vop_ioctl = devfs_rioctl,
1849 .vop_lookup = devfs_lookup,
1850 .vop_mknod = devfs_mknod,
1851 .vop_pathconf = devfs_pathconf,
1852 .vop_read = devfs_rread,
1853 .vop_readdir = devfs_readdir,
1854 .vop_readlink = devfs_readlink,
1855 .vop_reclaim = devfs_reclaim,
1856 .vop_remove = devfs_remove,
1857 .vop_revoke = devfs_revoke,
1858 .vop_setattr = devfs_setattr,
1860 .vop_setlabel = devfs_setlabel,
1862 .vop_symlink = devfs_symlink,
1863 .vop_vptocnp = devfs_vptocnp,
1866 static struct vop_vector devfs_specops = {
1867 .vop_default = &default_vnodeops,
1869 .vop_access = devfs_access,
1870 .vop_bmap = VOP_PANIC,
1871 .vop_close = devfs_close,
1872 .vop_create = VOP_PANIC,
1873 .vop_fsync = devfs_fsync,
1874 .vop_getattr = devfs_getattr,
1875 .vop_link = VOP_PANIC,
1876 .vop_mkdir = VOP_PANIC,
1877 .vop_mknod = VOP_PANIC,
1878 .vop_open = devfs_open,
1879 .vop_pathconf = devfs_pathconf,
1880 .vop_poll = dead_poll,
1881 .vop_print = devfs_print,
1882 .vop_read = dead_read,
1883 .vop_readdir = VOP_PANIC,
1884 .vop_readlink = VOP_PANIC,
1885 .vop_reallocblks = VOP_PANIC,
1886 .vop_reclaim = devfs_reclaim,
1887 .vop_remove = devfs_remove,
1888 .vop_rename = VOP_PANIC,
1889 .vop_revoke = devfs_revoke,
1890 .vop_rmdir = VOP_PANIC,
1891 .vop_setattr = devfs_setattr,
1893 .vop_setlabel = devfs_setlabel,
1895 .vop_strategy = VOP_PANIC,
1896 .vop_symlink = VOP_PANIC,
1897 .vop_vptocnp = devfs_vptocnp,
1898 .vop_write = dead_write,
1902 * Our calling convention to the device drivers used to be that we passed
1903 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1904 * flags instead since that's what open(), close() and ioctl() takes and
1905 * we don't really want vnode.h in device drivers.
1906 * We solved the source compatibility by redefining some vnode flags to
1907 * be the same as the fcntl ones and by sending down the bitwise OR of
1908 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1909 * pulls the rug out under this.
1911 CTASSERT(O_NONBLOCK == IO_NDELAY);
1912 CTASSERT(O_FSYNC == IO_SYNC);