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>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
60 #include <sys/sysctl.h>
62 #include <sys/ttycom.h>
63 #include <sys/unistd.h>
64 #include <sys/vnode.h>
66 static struct vop_vector devfs_vnodeops;
67 static struct vop_vector devfs_specops;
68 static struct fileops devfs_ops_f;
70 #include <fs/devfs/devfs.h>
71 #include <fs/devfs/devfs_int.h>
73 #include <security/mac/mac_framework.h>
75 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
77 struct mtx devfs_de_interlock;
78 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
79 struct sx clone_drain_lock;
80 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
81 struct mtx cdevpriv_mtx;
82 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
84 SYSCTL_DECL(_vfs_devfs);
86 static int devfs_dotimes;
87 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
88 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
91 * Update devfs node timestamp. Note that updates are unlocked and
92 * stat(2) could see partially updated times.
95 devfs_timestamp(struct timespec *tsp)
103 if (tsp->tv_sec != ts) {
111 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
115 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
116 if (*devp != fp->f_data) {
118 dev_relthread(*devp, *ref);
121 KASSERT((*devp)->si_refcount > 0,
122 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
125 curthread->td_fpop = fp;
130 devfs_get_cdevpriv(void **datap)
133 struct cdev_privdata *p;
136 fp = curthread->td_fpop;
142 *datap = p->cdpd_data;
149 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
152 struct cdev_priv *cdp;
153 struct cdev_privdata *p;
156 fp = curthread->td_fpop;
159 cdp = cdev2priv((struct cdev *)fp->f_data);
160 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
162 p->cdpd_dtr = priv_dtr;
164 mtx_lock(&cdevpriv_mtx);
165 if (fp->f_cdevpriv == NULL) {
166 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
168 mtx_unlock(&cdevpriv_mtx);
171 mtx_unlock(&cdevpriv_mtx);
172 free(p, M_CDEVPDATA);
179 devfs_destroy_cdevpriv(struct cdev_privdata *p)
182 mtx_assert(&cdevpriv_mtx, MA_OWNED);
183 p->cdpd_fp->f_cdevpriv = NULL;
184 LIST_REMOVE(p, cdpd_list);
185 mtx_unlock(&cdevpriv_mtx);
186 (p->cdpd_dtr)(p->cdpd_data);
187 free(p, M_CDEVPDATA);
191 devfs_fpdrop(struct file *fp)
193 struct cdev_privdata *p;
195 mtx_lock(&cdevpriv_mtx);
196 if ((p = fp->f_cdevpriv) == NULL) {
197 mtx_unlock(&cdevpriv_mtx);
200 devfs_destroy_cdevpriv(p);
204 devfs_clear_cdevpriv(void)
208 fp = curthread->td_fpop;
215 * On success devfs_populate_vp() returns with dmp->dm_lock held.
218 devfs_populate_vp(struct vnode *vp)
220 struct devfs_dirent *de;
221 struct devfs_mount *dmp;
224 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
226 dmp = VFSTODEVFS(vp->v_mount);
227 locked = VOP_ISLOCKED(vp);
229 sx_xlock(&dmp->dm_lock);
232 /* Can't call devfs_populate() with the vnode lock held. */
236 sx_xunlock(&dmp->dm_lock);
237 vn_lock(vp, locked | LK_RETRY);
238 sx_xlock(&dmp->dm_lock);
239 if (DEVFS_DMP_DROP(dmp)) {
240 sx_xunlock(&dmp->dm_lock);
241 devfs_unmount_final(dmp);
244 if ((vp->v_iflag & VI_DOOMED) != 0) {
245 sx_xunlock(&dmp->dm_lock);
250 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
251 if ((de->de_flags & DE_DOOMED) != 0) {
252 sx_xunlock(&dmp->dm_lock);
260 devfs_vptocnp(struct vop_vptocnp_args *ap)
262 struct vnode *vp = ap->a_vp;
263 struct vnode **dvp = ap->a_vpp;
264 struct devfs_mount *dmp;
265 char *buf = ap->a_buf;
266 int *buflen = ap->a_buflen;
267 struct devfs_dirent *dd, *de;
270 dmp = VFSTODEVFS(vp->v_mount);
272 error = devfs_populate_vp(vp);
279 if (vp->v_type == VCHR) {
280 i -= strlen(dd->de_cdp->cdp_c.si_name);
285 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
286 strlen(dd->de_cdp->cdp_c.si_name));
288 } else if (vp->v_type == VDIR) {
289 if (dd == dmp->dm_rootdir) {
294 i -= dd->de_dirent->d_namlen;
299 bcopy(dd->de_dirent->d_name, buf + i,
300 dd->de_dirent->d_namlen);
307 de = devfs_parent_dirent(de);
312 mtx_lock(&devfs_de_interlock);
316 mtx_unlock(&devfs_de_interlock);
322 mtx_unlock(&devfs_de_interlock);
326 sx_xunlock(&dmp->dm_lock);
331 * Construct the fully qualified path name relative to the mountpoint.
332 * If a NULL cnp is provided, no '/' is appended to the resulting path.
335 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
336 struct componentname *cnp)
339 struct devfs_dirent *de;
341 sx_assert(&dmp->dm_lock, SA_LOCKED);
346 i -= cnp->cn_namelen;
350 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
352 while (de != dmp->dm_rootdir) {
353 if (cnp != NULL || i < SPECNAMELEN) {
359 i -= de->de_dirent->d_namlen;
362 bcopy(de->de_dirent->d_name, buf + i,
363 de->de_dirent->d_namlen);
364 de = devfs_parent_dirent(de);
372 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
373 struct devfs_dirent *de)
378 if (de->de_flags & DE_DOOMED)
380 if (DEVFS_DE_DROP(de)) {
381 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
382 devfs_dirent_free(de);
384 if (DEVFS_DMP_DROP(dmp)) {
385 KASSERT(not_found == 1,
386 ("DEVFS mount struct freed before dirent"));
388 sx_xunlock(&dmp->dm_lock);
389 devfs_unmount_final(dmp);
391 if (not_found == 1 || (drop_dm_lock && not_found != 2))
392 sx_unlock(&dmp->dm_lock);
397 devfs_insmntque_dtr(struct vnode *vp, void *arg)
399 struct devfs_dirent *de;
401 de = (struct devfs_dirent *)arg;
402 mtx_lock(&devfs_de_interlock);
405 mtx_unlock(&devfs_de_interlock);
411 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
415 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
421 struct devfs_mount *dmp;
424 dmp = VFSTODEVFS(mp);
425 if (de->de_flags & DE_DOOMED) {
426 sx_xunlock(&dmp->dm_lock);
432 mtx_lock(&devfs_de_interlock);
436 mtx_unlock(&devfs_de_interlock);
437 sx_xunlock(&dmp->dm_lock);
438 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
439 sx_xlock(&dmp->dm_lock);
440 if (devfs_allocv_drop_refs(0, dmp, de)) {
444 else if ((vp->v_iflag & VI_DOOMED) != 0) {
445 mtx_lock(&devfs_de_interlock);
446 if (de->de_vnode == vp) {
450 mtx_unlock(&devfs_de_interlock);
454 sx_xunlock(&dmp->dm_lock);
458 mtx_unlock(&devfs_de_interlock);
459 if (de->de_dirent->d_type == DT_CHR) {
460 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
461 devfs_allocv_drop_refs(1, dmp, de);
464 dev = &de->de_cdp->cdp_c;
468 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
470 devfs_allocv_drop_refs(1, dmp, de);
471 printf("devfs_allocv: failed to allocate new vnode\n");
475 if (de->de_dirent->d_type == DT_CHR) {
480 /* XXX: v_rdev should be protect by vnode lock */
482 KASSERT(vp->v_usecount == 1,
483 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
484 dev->si_usecount += vp->v_usecount;
485 /* Special casing of ttys for deadfs. Probably redundant. */
487 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
488 vp->v_vflag |= VV_ISTTY;
491 if ((dev->si_flags & SI_ETERNAL) != 0)
492 vp->v_vflag |= VV_ETERNALDEV;
493 vp->v_op = &devfs_specops;
494 } else if (de->de_dirent->d_type == DT_DIR) {
496 } else if (de->de_dirent->d_type == DT_LNK) {
501 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
503 mtx_lock(&devfs_de_interlock);
506 mtx_unlock(&devfs_de_interlock);
507 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
509 (void) devfs_allocv_drop_refs(1, dmp, de);
512 if (devfs_allocv_drop_refs(0, dmp, de)) {
517 mac_devfs_vnode_associate(mp, de, vp);
519 sx_xunlock(&dmp->dm_lock);
525 devfs_access(struct vop_access_args *ap)
527 struct vnode *vp = ap->a_vp;
528 struct devfs_dirent *de;
532 if (vp->v_type == VDIR)
535 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
536 ap->a_accmode, ap->a_cred, NULL);
541 /* We do, however, allow access to the controlling terminal */
542 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
544 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
551 devfs_close(struct vop_close_args *ap)
553 struct vnode *vp = ap->a_vp, *oldvp;
554 struct thread *td = ap->a_td;
555 struct cdev *dev = vp->v_rdev;
557 int vp_locked, error, ref;
560 * XXX: Don't call d_close() if we were called because of
561 * XXX: insmntque1() failure.
563 if (vp->v_data == NULL)
567 * Hack: a tty device that is a controlling terminal
568 * has a reference from the session structure.
569 * We cannot easily tell that a character device is
570 * a controlling terminal, unless it is the closing
571 * process' controlling terminal. In that case,
572 * if the reference count is 2 (this last descriptor
573 * plus the session), release the reference from the session.
576 sx_xlock(&proctree_lock);
577 if (td && vp == td->td_proc->p_session->s_ttyvp) {
578 SESS_LOCK(td->td_proc->p_session);
580 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
581 td->td_proc->p_session->s_ttyvp = NULL;
582 td->td_proc->p_session->s_ttydp = NULL;
586 SESS_UNLOCK(td->td_proc->p_session);
588 sx_xunlock(&proctree_lock);
592 * We do not want to really close the device if it
593 * is still in use unless we are trying to close it
594 * forcibly. Since every use (buffer, vnode, swap, cmap)
595 * holds a reference to the vnode, and because we mark
596 * any other vnodes that alias this device, when the
597 * sum of the reference counts on all the aliased
598 * vnodes descends to one, we are on last close.
600 dsw = dev_refthread(dev, &ref);
604 if (vp->v_iflag & VI_DOOMED) {
606 } else if (dsw->d_flags & D_TRACKCLOSE) {
607 /* Keep device updated on status. */
608 } else if (count_dev(dev) > 1) {
610 dev_relthread(dev, ref);
615 vp_locked = VOP_ISLOCKED(vp);
617 KASSERT(dev->si_refcount > 0,
618 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
619 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
620 dev_relthread(dev, ref);
621 vn_lock(vp, vp_locked | LK_RETRY);
627 devfs_close_f(struct file *fp, struct thread *td)
633 * NB: td may be NULL if this descriptor is closed due to
634 * garbage collection from a closed UNIX domain socket.
636 fpop = curthread->td_fpop;
637 curthread->td_fpop = fp;
638 error = vnops.fo_close(fp, td);
639 curthread->td_fpop = fpop;
642 * The f_cdevpriv cannot be assigned non-NULL value while we
643 * are destroying the file.
645 if (fp->f_cdevpriv != NULL)
651 devfs_fsync(struct vop_fsync_args *ap)
655 struct devfs_dirent *de;
657 if (!vn_isdisk(ap->a_vp, &error)) {
658 bo = &ap->a_vp->v_bufobj;
659 de = ap->a_vp->v_data;
660 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
661 printf("Device %s went missing before all of the data "
662 "could be written to it; expect data loss.\n",
663 de->de_dirent->d_name);
665 error = vop_stdfsync(ap);
666 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
667 panic("devfs_fsync: vop_stdfsync failed.");
673 return (vop_stdfsync(ap));
677 devfs_getattr(struct vop_getattr_args *ap)
679 struct vnode *vp = ap->a_vp;
680 struct vattr *vap = ap->a_vap;
682 struct devfs_dirent *de;
683 struct devfs_mount *dmp;
686 error = devfs_populate_vp(vp);
690 dmp = VFSTODEVFS(vp->v_mount);
691 sx_xunlock(&dmp->dm_lock);
694 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
695 if (vp->v_type == VDIR) {
698 ("Null dir dirent in devfs_getattr vp=%p", vp));
700 vap->va_uid = de->de_uid;
701 vap->va_gid = de->de_gid;
702 vap->va_mode = de->de_mode;
703 if (vp->v_type == VLNK)
704 vap->va_size = strlen(de->de_symlink);
705 else if (vp->v_type == VDIR)
706 vap->va_size = vap->va_bytes = DEV_BSIZE;
709 if (vp->v_type != VDIR)
711 vap->va_blocksize = DEV_BSIZE;
712 vap->va_type = vp->v_type;
716 if ((aa).tv_sec <= 3600) { \
717 (aa).tv_sec = boottime.tv_sec; \
718 (aa).tv_nsec = boottime.tv_usec * 1000; \
722 if (vp->v_type != VCHR) {
724 vap->va_atime = de->de_atime;
726 vap->va_mtime = de->de_mtime;
728 vap->va_ctime = de->de_ctime;
732 vap->va_atime = dev->si_atime;
734 vap->va_mtime = dev->si_mtime;
736 vap->va_ctime = dev->si_ctime;
738 vap->va_rdev = cdev2priv(dev)->cdp_inode;
743 vap->va_nlink = de->de_links;
744 vap->va_fileid = de->de_inode;
751 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
759 struct fiodgname_arg *fgn;
763 error = devfs_fp_check(fp, &dev, &dsw, &ref);
765 error = vnops.fo_ioctl(fp, com, data, cred, td);
769 if (com == FIODTYPE) {
770 *(int *)data = dsw->d_flags & D_TYPEMASK;
772 dev_relthread(dev, ref);
774 } else if (com == FIODGNAME) {
781 error = copyout(p, fgn->buf, i);
783 dev_relthread(dev, ref);
786 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
788 dev_relthread(dev, ref);
789 if (error == ENOIOCTL)
791 if (error == 0 && com == TIOCSCTTY) {
794 /* Do nothing if reassigning same control tty */
795 sx_slock(&proctree_lock);
796 if (td->td_proc->p_session->s_ttyvp == vp) {
797 sx_sunlock(&proctree_lock);
801 vpold = td->td_proc->p_session->s_ttyvp;
803 SESS_LOCK(td->td_proc->p_session);
804 td->td_proc->p_session->s_ttyvp = vp;
805 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
806 SESS_UNLOCK(td->td_proc->p_session);
808 sx_sunlock(&proctree_lock);
810 /* Get rid of reference to old control tty */
819 devfs_kqfilter_f(struct file *fp, struct knote *kn)
829 error = devfs_fp_check(fp, &dev, &dsw, &ref);
832 error = dsw->d_kqfilter(dev, kn);
834 dev_relthread(dev, ref);
839 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
841 struct cdev_priv *cdp;
848 dcr = cdp->cdp_c.si_cred;
852 error = prison_check(td->td_ucred, dcr);
855 /* We do, however, allow access to the controlling terminal */
856 if (!(td->td_proc->p_flag & P_CONTROLT))
858 if (td->td_proc->p_session->s_ttydp == cdp)
864 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
866 struct componentname *cnp;
867 struct vnode *dvp, **vpp;
869 struct devfs_dirent *de, *dd;
870 struct devfs_dirent **dde;
871 struct devfs_mount *dmp;
873 int error, flags, nameiop, dvplocked;
874 char specname[SPECNAMELEN + 1], *pname;
879 pname = cnp->cn_nameptr;
881 flags = cnp->cn_flags;
882 nameiop = cnp->cn_nameiop;
883 dmp = VFSTODEVFS(dvp->v_mount);
887 if ((flags & ISLASTCN) && nameiop == RENAME)
890 if (dvp->v_type != VDIR)
893 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
896 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
900 if (cnp->cn_namelen == 1 && *pname == '.') {
901 if ((flags & ISLASTCN) && nameiop != LOOKUP)
908 if (flags & ISDOTDOT) {
909 if ((flags & ISLASTCN) && nameiop != LOOKUP)
911 de = devfs_parent_dirent(dd);
914 dvplocked = VOP_ISLOCKED(dvp);
916 error = devfs_allocv(de, dvp->v_mount,
917 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
919 vn_lock(dvp, dvplocked | LK_RETRY);
924 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
925 while (de == NULL) { /* While(...) so we can use break */
927 if (nameiop == DELETE)
931 * OK, we didn't have an entry for the name we were asked for
932 * so we try to see if anybody can create it on demand.
934 pname = devfs_fqpn(specname, dmp, dd, cnp);
940 sx_xunlock(&dmp->dm_lock);
941 sx_slock(&clone_drain_lock);
942 EVENTHANDLER_INVOKE(dev_clone,
943 td->td_ucred, pname, strlen(pname), &cdev);
944 sx_sunlock(&clone_drain_lock);
947 sx_xlock(&dmp->dm_lock);
948 else if (devfs_populate_vp(dvp) != 0) {
950 sx_xlock(&dmp->dm_lock);
951 if (DEVFS_DMP_DROP(dmp)) {
952 sx_xunlock(&dmp->dm_lock);
953 devfs_unmount_final(dmp);
955 sx_xunlock(&dmp->dm_lock);
959 if (DEVFS_DMP_DROP(dmp)) {
961 sx_xunlock(&dmp->dm_lock);
962 devfs_unmount_final(dmp);
972 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
973 if (dde != NULL && *dde != NULL)
980 if (de == NULL || de->de_flags & DE_WHITEOUT) {
981 if ((nameiop == CREATE || nameiop == RENAME) &&
982 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
983 cnp->cn_flags |= SAVENAME;
984 return (EJUSTRETURN);
989 if (devfs_prison_check(de, td))
992 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
993 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1002 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
1009 devfs_lookup(struct vop_lookup_args *ap)
1012 struct devfs_mount *dmp;
1015 if (devfs_populate_vp(ap->a_dvp) != 0)
1018 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1020 j = devfs_lookupx(ap, &dm_unlock);
1022 sx_xunlock(&dmp->dm_lock);
1027 devfs_mknod(struct vop_mknod_args *ap)
1029 struct componentname *cnp;
1030 struct vnode *dvp, **vpp;
1031 struct devfs_dirent *dd, *de;
1032 struct devfs_mount *dmp;
1036 * The only type of node we should be creating here is a
1037 * character device, for anything else return EOPNOTSUPP.
1039 if (ap->a_vap->va_type != VCHR)
1040 return (EOPNOTSUPP);
1042 dmp = VFSTODEVFS(dvp->v_mount);
1049 sx_xlock(&dmp->dm_lock);
1050 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1051 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1053 if (de->de_dirent->d_type == DT_CHR &&
1054 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1056 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1057 de->de_dirent->d_namlen) != 0)
1059 if (de->de_flags & DE_WHITEOUT)
1065 de->de_flags &= ~DE_WHITEOUT;
1066 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1069 sx_xunlock(&dmp->dm_lock);
1075 devfs_open(struct vop_open_args *ap)
1077 struct thread *td = ap->a_td;
1078 struct vnode *vp = ap->a_vp;
1079 struct cdev *dev = vp->v_rdev;
1080 struct file *fp = ap->a_fp;
1081 int error, ref, vlocked;
1086 if (vp->v_type == VBLK)
1092 /* Make this field valid before any I/O in d_open. */
1093 if (dev->si_iosize_max == 0)
1094 dev->si_iosize_max = DFLTPHYS;
1096 dsw = dev_refthread(dev, &ref);
1099 if (fp == NULL && dsw->d_fdopen != NULL) {
1100 dev_relthread(dev, ref);
1104 vlocked = VOP_ISLOCKED(vp);
1113 if (dsw->d_fdopen != NULL)
1114 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1116 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1117 /* Clean up any cdevpriv upon error. */
1119 devfs_clear_cdevpriv();
1122 vn_lock(vp, vlocked | LK_RETRY);
1123 dev_relthread(dev, ref);
1125 if (error == ERESTART)
1130 #if 0 /* /dev/console */
1131 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1136 if (fp->f_ops == &badfileops)
1137 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1138 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1141 * Hint to the dofilewrite() to not force the buffer draining
1142 * on the writer to the file. Most likely, the write would
1143 * not need normal buffers.
1146 fp->f_vnread_flags |= FDEVFS_VNODE;
1152 devfs_pathconf(struct vop_pathconf_args *ap)
1155 switch (ap->a_name) {
1156 case _PC_MAC_PRESENT:
1159 * If MAC is enabled, devfs automatically supports
1160 * trivial non-persistant label storage.
1168 return (vop_stdpathconf(ap));
1175 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1183 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1185 error = vnops.fo_poll(fp, events, cred, td);
1188 error = dsw->d_poll(dev, events, td);
1190 dev_relthread(dev, ref);
1195 * Print out the contents of a special device vnode.
1198 devfs_print(struct vop_print_args *ap)
1201 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1206 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1207 int flags, struct thread *td)
1210 int ioflag, error, ref;
1215 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1218 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1220 error = vnops.fo_read(fp, uio, cred, flags, td);
1223 resid = uio->uio_resid;
1224 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1225 if (ioflag & O_DIRECT)
1226 ioflag |= IO_DIRECT;
1228 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1229 error = dsw->d_read(dev, uio, ioflag);
1230 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1231 devfs_timestamp(&dev->si_atime);
1233 dev_relthread(dev, ref);
1235 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1240 devfs_readdir(struct vop_readdir_args *ap)
1245 struct devfs_dirent *dd;
1246 struct devfs_dirent *de;
1247 struct devfs_mount *dmp;
1249 int *tmp_ncookies = NULL;
1251 if (ap->a_vp->v_type != VDIR)
1255 if (uio->uio_offset < 0)
1259 * XXX: This is a temporary hack to get around this filesystem not
1260 * supporting cookies. We store the location of the ncookies pointer
1261 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1262 * and set the number of cookies to 0. We then set the pointer to
1263 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1264 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1265 * pointer to its original location before returning to the caller.
1267 if (ap->a_ncookies != NULL) {
1268 tmp_ncookies = ap->a_ncookies;
1269 *ap->a_ncookies = 0;
1270 ap->a_ncookies = NULL;
1273 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1274 if (devfs_populate_vp(ap->a_vp) != 0) {
1275 if (tmp_ncookies != NULL)
1276 ap->a_ncookies = tmp_ncookies;
1280 de = ap->a_vp->v_data;
1282 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1283 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1284 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1286 if (devfs_prison_check(dd, uio->uio_td))
1288 if (dd->de_dirent->d_type == DT_DIR)
1293 if (dp->d_reclen > uio->uio_resid)
1295 dp->d_fileno = de->de_inode;
1296 if (off >= uio->uio_offset) {
1297 error = vfs_read_dirent(ap, dp, off);
1301 off += dp->d_reclen;
1303 sx_xunlock(&dmp->dm_lock);
1304 uio->uio_offset = off;
1307 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1310 if (tmp_ncookies != NULL)
1311 ap->a_ncookies = tmp_ncookies;
1317 devfs_readlink(struct vop_readlink_args *ap)
1319 struct devfs_dirent *de;
1321 de = ap->a_vp->v_data;
1322 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1326 devfs_reclaim(struct vop_reclaim_args *ap)
1328 struct vnode *vp = ap->a_vp;
1329 struct devfs_dirent *de;
1332 mtx_lock(&devfs_de_interlock);
1335 de->de_vnode = NULL;
1338 mtx_unlock(&devfs_de_interlock);
1340 vnode_destroy_vobject(vp);
1353 dev->si_usecount -= vp->v_usecount;
1361 devfs_remove(struct vop_remove_args *ap)
1363 struct vnode *dvp = ap->a_dvp;
1364 struct vnode *vp = ap->a_vp;
1365 struct devfs_dirent *dd;
1366 struct devfs_dirent *de, *de_covered;
1367 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1369 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1370 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1372 sx_xlock(&dmp->dm_lock);
1373 dd = ap->a_dvp->v_data;
1375 if (de->de_cdp == NULL) {
1376 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1377 if (de->de_dirent->d_type == DT_LNK) {
1378 de_covered = devfs_find(dd, de->de_dirent->d_name,
1379 de->de_dirent->d_namlen, 0);
1380 if (de_covered != NULL)
1381 de_covered->de_flags &= ~DE_COVERED;
1383 /* We need to unlock dvp because devfs_delete() may lock it. */
1387 devfs_delete(dmp, de, 0);
1388 sx_xunlock(&dmp->dm_lock);
1390 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1391 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1393 de->de_flags |= DE_WHITEOUT;
1394 sx_xunlock(&dmp->dm_lock);
1400 * Revoke is called on a tty when a terminal session ends. The vnode
1401 * is orphaned by setting v_op to deadfs so we need to let go of it
1402 * as well so that we create a new one next time around.
1406 devfs_revoke(struct vop_revoke_args *ap)
1408 struct vnode *vp = ap->a_vp, *vp2;
1410 struct cdev_priv *cdp;
1411 struct devfs_dirent *de;
1414 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1417 cdp = cdev2priv(dev);
1430 mtx_lock(&devfs_de_interlock);
1433 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1434 de = cdp->cdp_dirents[i];
1442 mtx_unlock(&devfs_de_interlock);
1443 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1457 mtx_unlock(&devfs_de_interlock);
1462 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1463 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1465 dev_rel(&cdp->cdp_c);
1469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1474 devfs_rioctl(struct vop_ioctl_args *ap)
1477 struct devfs_mount *dmp;
1481 vn_lock(vp, LK_SHARED | LK_RETRY);
1482 if (vp->v_iflag & VI_DOOMED) {
1486 dmp = VFSTODEVFS(vp->v_mount);
1487 sx_xlock(&dmp->dm_lock);
1489 DEVFS_DMP_HOLD(dmp);
1490 devfs_populate(dmp);
1491 if (DEVFS_DMP_DROP(dmp)) {
1492 sx_xunlock(&dmp->dm_lock);
1493 devfs_unmount_final(dmp);
1496 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1497 sx_xunlock(&dmp->dm_lock);
1502 devfs_rread(struct vop_read_args *ap)
1505 if (ap->a_vp->v_type != VDIR)
1507 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1511 devfs_setattr(struct vop_setattr_args *ap)
1513 struct devfs_dirent *de;
1524 if ((vap->va_type != VNON) ||
1525 (vap->va_nlink != VNOVAL) ||
1526 (vap->va_fsid != VNOVAL) ||
1527 (vap->va_fileid != VNOVAL) ||
1528 (vap->va_blocksize != VNOVAL) ||
1529 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1530 (vap->va_rdev != VNOVAL) ||
1531 ((int)vap->va_bytes != VNOVAL) ||
1532 (vap->va_gen != VNOVAL)) {
1536 error = devfs_populate_vp(vp);
1541 if (vp->v_type == VDIR)
1545 if (vap->va_uid == (uid_t)VNOVAL)
1549 if (vap->va_gid == (gid_t)VNOVAL)
1553 if (uid != de->de_uid || gid != de->de_gid) {
1554 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1555 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1556 error = priv_check(td, PRIV_VFS_CHOWN);
1565 if (vap->va_mode != (mode_t)VNOVAL) {
1566 if (ap->a_cred->cr_uid != de->de_uid) {
1567 error = priv_check(td, PRIV_VFS_ADMIN);
1571 de->de_mode = vap->va_mode;
1575 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1576 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1579 if (vap->va_atime.tv_sec != VNOVAL) {
1580 if (vp->v_type == VCHR)
1581 vp->v_rdev->si_atime = vap->va_atime;
1583 de->de_atime = vap->va_atime;
1585 if (vap->va_mtime.tv_sec != VNOVAL) {
1586 if (vp->v_type == VCHR)
1587 vp->v_rdev->si_mtime = vap->va_mtime;
1589 de->de_mtime = vap->va_mtime;
1595 if (vp->v_type == VCHR)
1596 vfs_timestamp(&vp->v_rdev->si_ctime);
1598 vfs_timestamp(&de->de_mtime);
1602 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1608 devfs_setlabel(struct vop_setlabel_args *ap)
1611 struct devfs_dirent *de;
1616 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1617 mac_devfs_update(vp->v_mount, de, vp);
1624 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1627 return (vnops.fo_stat(fp, sb, cred, td));
1631 devfs_symlink(struct vop_symlink_args *ap)
1634 struct devfs_dirent *dd;
1635 struct devfs_dirent *de, *de_covered, *de_dotdot;
1636 struct devfs_mount *dmp;
1638 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1641 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1642 if (devfs_populate_vp(ap->a_dvp) != 0)
1645 dd = ap->a_dvp->v_data;
1646 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1647 de->de_flags = DE_USER;
1651 de->de_inode = alloc_unr(devfs_inos);
1653 de->de_dirent->d_type = DT_LNK;
1654 i = strlen(ap->a_target) + 1;
1655 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1656 bcopy(ap->a_target, de->de_symlink, i);
1658 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1660 de_covered = devfs_find(dd, de->de_dirent->d_name,
1661 de->de_dirent->d_namlen, 0);
1662 if (de_covered != NULL) {
1663 if ((de_covered->de_flags & DE_USER) != 0) {
1664 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1665 sx_xunlock(&dmp->dm_lock);
1668 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1669 ("devfs_symlink: entry %p already covered", de_covered));
1670 de_covered->de_flags |= DE_COVERED;
1673 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1674 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1675 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1676 devfs_dir_ref_de(dmp, dd);
1677 devfs_rules_apply(dmp, de);
1679 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1683 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1686 return (vnops.fo_truncate(fp, length, cred, td));
1690 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1691 int flags, struct thread *td)
1694 int error, ioflag, ref;
1699 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1702 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1704 error = vnops.fo_write(fp, uio, cred, flags, td);
1707 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1708 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1709 if (ioflag & O_DIRECT)
1710 ioflag |= IO_DIRECT;
1711 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1713 resid = uio->uio_resid;
1715 error = dsw->d_write(dev, uio, ioflag);
1716 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1717 devfs_timestamp(&dev->si_ctime);
1718 dev->si_mtime = dev->si_ctime;
1721 dev_relthread(dev, ref);
1723 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1728 dev2udev(struct cdev *x)
1732 return (cdev2priv(x)->cdp_inode);
1735 static struct fileops devfs_ops_f = {
1736 .fo_read = devfs_read_f,
1737 .fo_write = devfs_write_f,
1738 .fo_truncate = devfs_truncate_f,
1739 .fo_ioctl = devfs_ioctl_f,
1740 .fo_poll = devfs_poll_f,
1741 .fo_kqfilter = devfs_kqfilter_f,
1742 .fo_stat = devfs_stat_f,
1743 .fo_close = devfs_close_f,
1744 .fo_chmod = vn_chmod,
1745 .fo_chown = vn_chown,
1746 .fo_sendfile = vn_sendfile,
1748 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1751 static struct vop_vector devfs_vnodeops = {
1752 .vop_default = &default_vnodeops,
1754 .vop_access = devfs_access,
1755 .vop_getattr = devfs_getattr,
1756 .vop_ioctl = devfs_rioctl,
1757 .vop_lookup = devfs_lookup,
1758 .vop_mknod = devfs_mknod,
1759 .vop_pathconf = devfs_pathconf,
1760 .vop_read = devfs_rread,
1761 .vop_readdir = devfs_readdir,
1762 .vop_readlink = devfs_readlink,
1763 .vop_reclaim = devfs_reclaim,
1764 .vop_remove = devfs_remove,
1765 .vop_revoke = devfs_revoke,
1766 .vop_setattr = devfs_setattr,
1768 .vop_setlabel = devfs_setlabel,
1770 .vop_symlink = devfs_symlink,
1771 .vop_vptocnp = devfs_vptocnp,
1774 static struct vop_vector devfs_specops = {
1775 .vop_default = &default_vnodeops,
1777 .vop_access = devfs_access,
1778 .vop_bmap = VOP_PANIC,
1779 .vop_close = devfs_close,
1780 .vop_create = VOP_PANIC,
1781 .vop_fsync = devfs_fsync,
1782 .vop_getattr = devfs_getattr,
1783 .vop_link = VOP_PANIC,
1784 .vop_mkdir = VOP_PANIC,
1785 .vop_mknod = VOP_PANIC,
1786 .vop_open = devfs_open,
1787 .vop_pathconf = devfs_pathconf,
1788 .vop_poll = dead_poll,
1789 .vop_print = devfs_print,
1790 .vop_read = dead_read,
1791 .vop_readdir = VOP_PANIC,
1792 .vop_readlink = VOP_PANIC,
1793 .vop_reallocblks = VOP_PANIC,
1794 .vop_reclaim = devfs_reclaim,
1795 .vop_remove = devfs_remove,
1796 .vop_rename = VOP_PANIC,
1797 .vop_revoke = devfs_revoke,
1798 .vop_rmdir = VOP_PANIC,
1799 .vop_setattr = devfs_setattr,
1801 .vop_setlabel = devfs_setlabel,
1803 .vop_strategy = VOP_PANIC,
1804 .vop_symlink = VOP_PANIC,
1805 .vop_vptocnp = devfs_vptocnp,
1806 .vop_write = dead_write,
1810 * Our calling convention to the device drivers used to be that we passed
1811 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1812 * flags instead since that's what open(), close() and ioctl() takes and
1813 * we don't really want vnode.h in device drivers.
1814 * We solved the source compatibility by redefining some vnode flags to
1815 * be the same as the fcntl ones and by sending down the bitwise OR of
1816 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1817 * pulls the rug out under this.
1819 CTASSERT(O_NONBLOCK == IO_NDELAY);
1820 CTASSERT(O_FSYNC == IO_SYNC);