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 fileops devfs_ops_f;
69 #include <fs/devfs/devfs.h>
70 #include <fs/devfs/devfs_int.h>
72 #include <security/mac/mac_framework.h>
74 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
76 struct mtx devfs_de_interlock;
77 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
78 struct sx clone_drain_lock;
79 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
80 struct mtx cdevpriv_mtx;
81 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
83 SYSCTL_DECL(_vfs_devfs);
85 static int devfs_dotimes;
86 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
87 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
90 * Update devfs node timestamp. Note that updates are unlocked and
91 * stat(2) could see partially updated times.
94 devfs_timestamp(struct timespec *tsp)
102 if (tsp->tv_sec != ts) {
110 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
114 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
115 if (*devp != fp->f_data) {
117 dev_relthread(*devp, *ref);
120 KASSERT((*devp)->si_refcount > 0,
121 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
124 curthread->td_fpop = fp;
129 devfs_get_cdevpriv(void **datap)
132 struct cdev_privdata *p;
135 fp = curthread->td_fpop;
141 *datap = p->cdpd_data;
148 devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t priv_dtr)
151 struct cdev_priv *cdp;
152 struct cdev_privdata *p;
155 fp = curthread->td_fpop;
158 cdp = cdev2priv((struct cdev *)fp->f_data);
159 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
161 p->cdpd_dtr = priv_dtr;
163 mtx_lock(&cdevpriv_mtx);
164 if (fp->f_cdevpriv == NULL) {
165 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
167 mtx_unlock(&cdevpriv_mtx);
170 mtx_unlock(&cdevpriv_mtx);
171 free(p, M_CDEVPDATA);
178 devfs_destroy_cdevpriv(struct cdev_privdata *p)
181 mtx_assert(&cdevpriv_mtx, MA_OWNED);
182 p->cdpd_fp->f_cdevpriv = NULL;
183 LIST_REMOVE(p, cdpd_list);
184 mtx_unlock(&cdevpriv_mtx);
185 (p->cdpd_dtr)(p->cdpd_data);
186 free(p, M_CDEVPDATA);
190 devfs_fpdrop(struct file *fp)
192 struct cdev_privdata *p;
194 mtx_lock(&cdevpriv_mtx);
195 if ((p = fp->f_cdevpriv) == NULL) {
196 mtx_unlock(&cdevpriv_mtx);
199 devfs_destroy_cdevpriv(p);
203 devfs_clear_cdevpriv(void)
207 fp = curthread->td_fpop;
214 * On success devfs_populate_vp() returns with dmp->dm_lock held.
217 devfs_populate_vp(struct vnode *vp)
219 struct devfs_dirent *de;
220 struct devfs_mount *dmp;
223 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
225 dmp = VFSTODEVFS(vp->v_mount);
226 locked = VOP_ISLOCKED(vp);
228 sx_xlock(&dmp->dm_lock);
231 /* Can't call devfs_populate() with the vnode lock held. */
235 sx_xunlock(&dmp->dm_lock);
236 vn_lock(vp, locked | LK_RETRY);
237 sx_xlock(&dmp->dm_lock);
238 if (DEVFS_DMP_DROP(dmp)) {
239 sx_xunlock(&dmp->dm_lock);
240 devfs_unmount_final(dmp);
243 if ((vp->v_iflag & VI_DOOMED) != 0) {
244 sx_xunlock(&dmp->dm_lock);
249 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
250 if ((de->de_flags & DE_DOOMED) != 0) {
251 sx_xunlock(&dmp->dm_lock);
259 devfs_vptocnp(struct vop_vptocnp_args *ap)
261 struct vnode *vp = ap->a_vp;
262 struct vnode **dvp = ap->a_vpp;
263 struct devfs_mount *dmp;
264 char *buf = ap->a_buf;
265 int *buflen = ap->a_buflen;
266 struct devfs_dirent *dd, *de;
269 dmp = VFSTODEVFS(vp->v_mount);
271 error = devfs_populate_vp(vp);
278 if (vp->v_type == VCHR) {
279 i -= strlen(dd->de_cdp->cdp_c.si_name);
284 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
285 strlen(dd->de_cdp->cdp_c.si_name));
287 } else if (vp->v_type == VDIR) {
288 if (dd == dmp->dm_rootdir) {
293 i -= dd->de_dirent->d_namlen;
298 bcopy(dd->de_dirent->d_name, buf + i,
299 dd->de_dirent->d_namlen);
306 de = devfs_parent_dirent(de);
311 mtx_lock(&devfs_de_interlock);
315 mtx_unlock(&devfs_de_interlock);
321 mtx_unlock(&devfs_de_interlock);
325 sx_xunlock(&dmp->dm_lock);
330 * Construct the fully qualified path name relative to the mountpoint.
331 * If a NULL cnp is provided, no '/' is appended to the resulting path.
334 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
335 struct componentname *cnp)
338 struct devfs_dirent *de;
340 sx_assert(&dmp->dm_lock, SA_LOCKED);
345 i -= cnp->cn_namelen;
349 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
351 while (de != dmp->dm_rootdir) {
352 if (cnp != NULL || i < SPECNAMELEN) {
358 i -= de->de_dirent->d_namlen;
361 bcopy(de->de_dirent->d_name, buf + i,
362 de->de_dirent->d_namlen);
363 de = devfs_parent_dirent(de);
371 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
372 struct devfs_dirent *de)
377 if (de->de_flags & DE_DOOMED)
379 if (DEVFS_DE_DROP(de)) {
380 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
381 devfs_dirent_free(de);
383 if (DEVFS_DMP_DROP(dmp)) {
384 KASSERT(not_found == 1,
385 ("DEVFS mount struct freed before dirent"));
387 sx_xunlock(&dmp->dm_lock);
388 devfs_unmount_final(dmp);
390 if (not_found == 1 || (drop_dm_lock && not_found != 2))
391 sx_unlock(&dmp->dm_lock);
396 devfs_insmntque_dtr(struct vnode *vp, void *arg)
398 struct devfs_dirent *de;
400 de = (struct devfs_dirent *)arg;
401 mtx_lock(&devfs_de_interlock);
404 mtx_unlock(&devfs_de_interlock);
410 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
414 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
420 struct devfs_mount *dmp;
423 dmp = VFSTODEVFS(mp);
424 if (de->de_flags & DE_DOOMED) {
425 sx_xunlock(&dmp->dm_lock);
431 mtx_lock(&devfs_de_interlock);
435 mtx_unlock(&devfs_de_interlock);
436 sx_xunlock(&dmp->dm_lock);
437 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
438 sx_xlock(&dmp->dm_lock);
439 if (devfs_allocv_drop_refs(0, dmp, de)) {
443 else if ((vp->v_iflag & VI_DOOMED) != 0) {
444 mtx_lock(&devfs_de_interlock);
445 if (de->de_vnode == vp) {
449 mtx_unlock(&devfs_de_interlock);
453 sx_xunlock(&dmp->dm_lock);
457 mtx_unlock(&devfs_de_interlock);
458 if (de->de_dirent->d_type == DT_CHR) {
459 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
460 devfs_allocv_drop_refs(1, dmp, de);
463 dev = &de->de_cdp->cdp_c;
467 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
469 devfs_allocv_drop_refs(1, dmp, de);
470 printf("devfs_allocv: failed to allocate new vnode\n");
474 if (de->de_dirent->d_type == DT_CHR) {
479 /* XXX: v_rdev should be protect by vnode lock */
481 KASSERT(vp->v_usecount == 1,
482 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
483 dev->si_usecount += vp->v_usecount;
484 /* Special casing of ttys for deadfs. Probably redundant. */
486 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
487 vp->v_vflag |= VV_ISTTY;
490 if ((dev->si_flags & SI_ETERNAL) != 0)
491 vp->v_vflag |= VV_ETERNALDEV;
492 vp->v_op = &devfs_specops;
493 } else if (de->de_dirent->d_type == DT_DIR) {
495 } else if (de->de_dirent->d_type == DT_LNK) {
500 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
502 mtx_lock(&devfs_de_interlock);
505 mtx_unlock(&devfs_de_interlock);
506 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
508 (void) devfs_allocv_drop_refs(1, dmp, de);
511 if (devfs_allocv_drop_refs(0, dmp, de)) {
516 mac_devfs_vnode_associate(mp, de, vp);
518 sx_xunlock(&dmp->dm_lock);
524 devfs_access(struct vop_access_args *ap)
526 struct vnode *vp = ap->a_vp;
527 struct devfs_dirent *de;
531 if (vp->v_type == VDIR)
534 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
535 ap->a_accmode, ap->a_cred, NULL);
540 /* We do, however, allow access to the controlling terminal */
541 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
543 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
550 devfs_close(struct vop_close_args *ap)
552 struct vnode *vp = ap->a_vp, *oldvp;
553 struct thread *td = ap->a_td;
554 struct cdev *dev = vp->v_rdev;
556 int vp_locked, error, ref;
559 * XXX: Don't call d_close() if we were called because of
560 * XXX: insmntque1() failure.
562 if (vp->v_data == NULL)
566 * Hack: a tty device that is a controlling terminal
567 * has a reference from the session structure.
568 * We cannot easily tell that a character device is
569 * a controlling terminal, unless it is the closing
570 * process' controlling terminal. In that case,
571 * if the reference count is 2 (this last descriptor
572 * plus the session), release the reference from the session.
575 sx_xlock(&proctree_lock);
576 if (td && vp == td->td_proc->p_session->s_ttyvp) {
577 SESS_LOCK(td->td_proc->p_session);
579 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
580 td->td_proc->p_session->s_ttyvp = NULL;
581 td->td_proc->p_session->s_ttydp = NULL;
585 SESS_UNLOCK(td->td_proc->p_session);
587 sx_xunlock(&proctree_lock);
591 * We do not want to really close the device if it
592 * is still in use unless we are trying to close it
593 * forcibly. Since every use (buffer, vnode, swap, cmap)
594 * holds a reference to the vnode, and because we mark
595 * any other vnodes that alias this device, when the
596 * sum of the reference counts on all the aliased
597 * vnodes descends to one, we are on last close.
599 dsw = dev_refthread(dev, &ref);
603 if (vp->v_iflag & VI_DOOMED) {
605 } else if (dsw->d_flags & D_TRACKCLOSE) {
606 /* Keep device updated on status. */
607 } else if (count_dev(dev) > 1) {
609 dev_relthread(dev, ref);
614 vp_locked = VOP_ISLOCKED(vp);
616 KASSERT(dev->si_refcount > 0,
617 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
618 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
619 dev_relthread(dev, ref);
620 vn_lock(vp, vp_locked | LK_RETRY);
626 devfs_close_f(struct file *fp, struct thread *td)
632 * NB: td may be NULL if this descriptor is closed due to
633 * garbage collection from a closed UNIX domain socket.
635 fpop = curthread->td_fpop;
636 curthread->td_fpop = fp;
637 error = vnops.fo_close(fp, td);
638 curthread->td_fpop = fpop;
641 * The f_cdevpriv cannot be assigned non-NULL value while we
642 * are destroying the file.
644 if (fp->f_cdevpriv != NULL)
650 devfs_fsync(struct vop_fsync_args *ap)
654 struct devfs_dirent *de;
656 if (!vn_isdisk(ap->a_vp, &error)) {
657 bo = &ap->a_vp->v_bufobj;
658 de = ap->a_vp->v_data;
659 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
660 printf("Device %s went missing before all of the data "
661 "could be written to it; expect data loss.\n",
662 de->de_dirent->d_name);
664 error = vop_stdfsync(ap);
665 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
666 panic("devfs_fsync: vop_stdfsync failed.");
672 return (vop_stdfsync(ap));
676 devfs_getattr(struct vop_getattr_args *ap)
678 struct vnode *vp = ap->a_vp;
679 struct vattr *vap = ap->a_vap;
681 struct devfs_dirent *de;
682 struct devfs_mount *dmp;
685 error = devfs_populate_vp(vp);
689 dmp = VFSTODEVFS(vp->v_mount);
690 sx_xunlock(&dmp->dm_lock);
693 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
694 if (vp->v_type == VDIR) {
697 ("Null dir dirent in devfs_getattr vp=%p", vp));
699 vap->va_uid = de->de_uid;
700 vap->va_gid = de->de_gid;
701 vap->va_mode = de->de_mode;
702 if (vp->v_type == VLNK)
703 vap->va_size = strlen(de->de_symlink);
704 else if (vp->v_type == VDIR)
705 vap->va_size = vap->va_bytes = DEV_BSIZE;
708 if (vp->v_type != VDIR)
710 vap->va_blocksize = DEV_BSIZE;
711 vap->va_type = vp->v_type;
715 if ((aa).tv_sec <= 3600) { \
716 (aa).tv_sec = boottime.tv_sec; \
717 (aa).tv_nsec = boottime.tv_usec * 1000; \
721 if (vp->v_type != VCHR) {
723 vap->va_atime = de->de_atime;
725 vap->va_mtime = de->de_mtime;
727 vap->va_ctime = de->de_ctime;
731 vap->va_atime = dev->si_atime;
733 vap->va_mtime = dev->si_mtime;
735 vap->va_ctime = dev->si_ctime;
737 vap->va_rdev = cdev2priv(dev)->cdp_inode;
742 vap->va_nlink = de->de_links;
743 vap->va_fileid = de->de_inode;
750 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
758 struct fiodgname_arg *fgn;
762 error = devfs_fp_check(fp, &dev, &dsw, &ref);
764 error = vnops.fo_ioctl(fp, com, data, cred, td);
768 if (com == FIODTYPE) {
769 *(int *)data = dsw->d_flags & D_TYPEMASK;
771 dev_relthread(dev, ref);
773 } else if (com == FIODGNAME) {
780 error = copyout(p, fgn->buf, i);
782 dev_relthread(dev, ref);
785 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
787 dev_relthread(dev, ref);
788 if (error == ENOIOCTL)
790 if (error == 0 && com == TIOCSCTTY) {
793 /* Do nothing if reassigning same control tty */
794 sx_slock(&proctree_lock);
795 if (td->td_proc->p_session->s_ttyvp == vp) {
796 sx_sunlock(&proctree_lock);
800 vpold = td->td_proc->p_session->s_ttyvp;
802 SESS_LOCK(td->td_proc->p_session);
803 td->td_proc->p_session->s_ttyvp = vp;
804 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
805 SESS_UNLOCK(td->td_proc->p_session);
807 sx_sunlock(&proctree_lock);
809 /* Get rid of reference to old control tty */
818 devfs_kqfilter_f(struct file *fp, struct knote *kn)
828 error = devfs_fp_check(fp, &dev, &dsw, &ref);
831 error = dsw->d_kqfilter(dev, kn);
833 dev_relthread(dev, ref);
838 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
840 struct cdev_priv *cdp;
847 dcr = cdp->cdp_c.si_cred;
851 error = prison_check(td->td_ucred, dcr);
854 /* We do, however, allow access to the controlling terminal */
855 if (!(td->td_proc->p_flag & P_CONTROLT))
857 if (td->td_proc->p_session->s_ttydp == cdp)
863 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
865 struct componentname *cnp;
866 struct vnode *dvp, **vpp;
868 struct devfs_dirent *de, *dd;
869 struct devfs_dirent **dde;
870 struct devfs_mount *dmp;
872 int error, flags, nameiop, dvplocked;
873 char specname[SPECNAMELEN + 1], *pname;
878 pname = cnp->cn_nameptr;
880 flags = cnp->cn_flags;
881 nameiop = cnp->cn_nameiop;
882 dmp = VFSTODEVFS(dvp->v_mount);
886 if ((flags & ISLASTCN) && nameiop == RENAME)
889 if (dvp->v_type != VDIR)
892 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
895 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
899 if (cnp->cn_namelen == 1 && *pname == '.') {
900 if ((flags & ISLASTCN) && nameiop != LOOKUP)
907 if (flags & ISDOTDOT) {
908 if ((flags & ISLASTCN) && nameiop != LOOKUP)
910 de = devfs_parent_dirent(dd);
913 dvplocked = VOP_ISLOCKED(dvp);
915 error = devfs_allocv(de, dvp->v_mount,
916 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
918 vn_lock(dvp, dvplocked | LK_RETRY);
923 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
924 while (de == NULL) { /* While(...) so we can use break */
926 if (nameiop == DELETE)
930 * OK, we didn't have an entry for the name we were asked for
931 * so we try to see if anybody can create it on demand.
933 pname = devfs_fqpn(specname, dmp, dd, cnp);
939 sx_xunlock(&dmp->dm_lock);
940 sx_slock(&clone_drain_lock);
941 EVENTHANDLER_INVOKE(dev_clone,
942 td->td_ucred, pname, strlen(pname), &cdev);
943 sx_sunlock(&clone_drain_lock);
946 sx_xlock(&dmp->dm_lock);
947 else if (devfs_populate_vp(dvp) != 0) {
949 sx_xlock(&dmp->dm_lock);
950 if (DEVFS_DMP_DROP(dmp)) {
951 sx_xunlock(&dmp->dm_lock);
952 devfs_unmount_final(dmp);
954 sx_xunlock(&dmp->dm_lock);
958 if (DEVFS_DMP_DROP(dmp)) {
960 sx_xunlock(&dmp->dm_lock);
961 devfs_unmount_final(dmp);
971 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
972 if (dde != NULL && *dde != NULL)
979 if (de == NULL || de->de_flags & DE_WHITEOUT) {
980 if ((nameiop == CREATE || nameiop == RENAME) &&
981 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
982 cnp->cn_flags |= SAVENAME;
983 return (EJUSTRETURN);
988 if (devfs_prison_check(de, td))
991 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
992 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1001 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
1008 devfs_lookup(struct vop_lookup_args *ap)
1011 struct devfs_mount *dmp;
1014 if (devfs_populate_vp(ap->a_dvp) != 0)
1017 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1019 j = devfs_lookupx(ap, &dm_unlock);
1021 sx_xunlock(&dmp->dm_lock);
1026 devfs_mknod(struct vop_mknod_args *ap)
1028 struct componentname *cnp;
1029 struct vnode *dvp, **vpp;
1030 struct devfs_dirent *dd, *de;
1031 struct devfs_mount *dmp;
1035 * The only type of node we should be creating here is a
1036 * character device, for anything else return EOPNOTSUPP.
1038 if (ap->a_vap->va_type != VCHR)
1039 return (EOPNOTSUPP);
1041 dmp = VFSTODEVFS(dvp->v_mount);
1048 sx_xlock(&dmp->dm_lock);
1049 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1050 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1052 if (de->de_dirent->d_type == DT_CHR &&
1053 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1055 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1056 de->de_dirent->d_namlen) != 0)
1058 if (de->de_flags & DE_WHITEOUT)
1064 de->de_flags &= ~DE_WHITEOUT;
1065 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1068 sx_xunlock(&dmp->dm_lock);
1074 devfs_open(struct vop_open_args *ap)
1076 struct thread *td = ap->a_td;
1077 struct vnode *vp = ap->a_vp;
1078 struct cdev *dev = vp->v_rdev;
1079 struct file *fp = ap->a_fp;
1080 int error, ref, vlocked;
1085 if (vp->v_type == VBLK)
1091 /* Make this field valid before any I/O in d_open. */
1092 if (dev->si_iosize_max == 0)
1093 dev->si_iosize_max = DFLTPHYS;
1095 dsw = dev_refthread(dev, &ref);
1098 if (fp == NULL && dsw->d_fdopen != NULL) {
1099 dev_relthread(dev, ref);
1103 vlocked = VOP_ISLOCKED(vp);
1112 if (dsw->d_fdopen != NULL)
1113 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1115 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1116 /* cleanup any cdevpriv upon error */
1118 devfs_clear_cdevpriv();
1121 vn_lock(vp, vlocked | LK_RETRY);
1122 dev_relthread(dev, ref);
1124 if (error == ERESTART)
1129 #if 0 /* /dev/console */
1130 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1135 if (fp->f_ops == &badfileops)
1136 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1137 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1140 * Hint to the dofilewrite() to not force the buffer draining
1141 * on the writer to the file. Most likely, the write would
1142 * not need normal buffers.
1145 fp->f_vnread_flags |= FDEVFS_VNODE;
1151 devfs_pathconf(struct vop_pathconf_args *ap)
1154 switch (ap->a_name) {
1155 case _PC_MAC_PRESENT:
1158 * If MAC is enabled, devfs automatically supports
1159 * trivial non-persistant label storage.
1167 return (vop_stdpathconf(ap));
1174 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1182 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1184 error = vnops.fo_poll(fp, events, cred, td);
1187 error = dsw->d_poll(dev, events, td);
1189 dev_relthread(dev, ref);
1194 * Print out the contents of a special device vnode.
1197 devfs_print(struct vop_print_args *ap)
1200 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1205 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1206 int flags, struct thread *td)
1209 int ioflag, error, ref;
1214 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1217 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1219 error = vnops.fo_read(fp, uio, cred, flags, td);
1222 resid = uio->uio_resid;
1223 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1224 if (ioflag & O_DIRECT)
1225 ioflag |= IO_DIRECT;
1227 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1228 error = dsw->d_read(dev, uio, ioflag);
1229 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1230 devfs_timestamp(&dev->si_atime);
1232 dev_relthread(dev, ref);
1234 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1239 devfs_readdir(struct vop_readdir_args *ap)
1244 struct devfs_dirent *dd;
1245 struct devfs_dirent *de;
1246 struct devfs_mount *dmp;
1248 int *tmp_ncookies = NULL;
1250 if (ap->a_vp->v_type != VDIR)
1254 if (uio->uio_offset < 0)
1258 * XXX: This is a temporary hack to get around this filesystem not
1259 * supporting cookies. We store the location of the ncookies pointer
1260 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1261 * and set the number of cookies to 0. We then set the pointer to
1262 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1263 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1264 * pointer to its original location before returning to the caller.
1266 if (ap->a_ncookies != NULL) {
1267 tmp_ncookies = ap->a_ncookies;
1268 *ap->a_ncookies = 0;
1269 ap->a_ncookies = NULL;
1272 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1273 if (devfs_populate_vp(ap->a_vp) != 0) {
1274 if (tmp_ncookies != NULL)
1275 ap->a_ncookies = tmp_ncookies;
1279 de = ap->a_vp->v_data;
1281 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1282 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1283 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1285 if (devfs_prison_check(dd, uio->uio_td))
1287 if (dd->de_dirent->d_type == DT_DIR)
1292 if (dp->d_reclen > uio->uio_resid)
1294 dp->d_fileno = de->de_inode;
1295 if (off >= uio->uio_offset) {
1296 error = vfs_read_dirent(ap, dp, off);
1300 off += dp->d_reclen;
1302 sx_xunlock(&dmp->dm_lock);
1303 uio->uio_offset = off;
1306 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1309 if (tmp_ncookies != NULL)
1310 ap->a_ncookies = tmp_ncookies;
1316 devfs_readlink(struct vop_readlink_args *ap)
1318 struct devfs_dirent *de;
1320 de = ap->a_vp->v_data;
1321 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1325 devfs_reclaim(struct vop_reclaim_args *ap)
1327 struct vnode *vp = ap->a_vp;
1328 struct devfs_dirent *de;
1331 mtx_lock(&devfs_de_interlock);
1334 de->de_vnode = NULL;
1337 mtx_unlock(&devfs_de_interlock);
1339 vnode_destroy_vobject(vp);
1352 dev->si_usecount -= vp->v_usecount;
1360 devfs_remove(struct vop_remove_args *ap)
1362 struct vnode *dvp = ap->a_dvp;
1363 struct vnode *vp = ap->a_vp;
1364 struct devfs_dirent *dd;
1365 struct devfs_dirent *de, *de_covered;
1366 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1368 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1369 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1371 sx_xlock(&dmp->dm_lock);
1372 dd = ap->a_dvp->v_data;
1374 if (de->de_cdp == NULL) {
1375 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1376 if (de->de_dirent->d_type == DT_LNK) {
1377 de_covered = devfs_find(dd, de->de_dirent->d_name,
1378 de->de_dirent->d_namlen, 0);
1379 if (de_covered != NULL)
1380 de_covered->de_flags &= ~DE_COVERED;
1382 /* We need to unlock dvp because devfs_delete() may lock it. */
1386 devfs_delete(dmp, de, 0);
1387 sx_xunlock(&dmp->dm_lock);
1389 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1390 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1392 de->de_flags |= DE_WHITEOUT;
1393 sx_xunlock(&dmp->dm_lock);
1399 * Revoke is called on a tty when a terminal session ends. The vnode
1400 * is orphaned by setting v_op to deadfs so we need to let go of it
1401 * as well so that we create a new one next time around.
1405 devfs_revoke(struct vop_revoke_args *ap)
1407 struct vnode *vp = ap->a_vp, *vp2;
1409 struct cdev_priv *cdp;
1410 struct devfs_dirent *de;
1413 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1416 cdp = cdev2priv(dev);
1429 mtx_lock(&devfs_de_interlock);
1432 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1433 de = cdp->cdp_dirents[i];
1441 mtx_unlock(&devfs_de_interlock);
1442 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1456 mtx_unlock(&devfs_de_interlock);
1461 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1462 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1464 dev_rel(&cdp->cdp_c);
1468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1473 devfs_rioctl(struct vop_ioctl_args *ap)
1476 struct devfs_mount *dmp;
1480 vn_lock(vp, LK_SHARED | LK_RETRY);
1481 if (vp->v_iflag & VI_DOOMED) {
1485 dmp = VFSTODEVFS(vp->v_mount);
1486 sx_xlock(&dmp->dm_lock);
1488 DEVFS_DMP_HOLD(dmp);
1489 devfs_populate(dmp);
1490 if (DEVFS_DMP_DROP(dmp)) {
1491 sx_xunlock(&dmp->dm_lock);
1492 devfs_unmount_final(dmp);
1495 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1496 sx_xunlock(&dmp->dm_lock);
1501 devfs_rread(struct vop_read_args *ap)
1504 if (ap->a_vp->v_type != VDIR)
1506 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1510 devfs_setattr(struct vop_setattr_args *ap)
1512 struct devfs_dirent *de;
1523 if ((vap->va_type != VNON) ||
1524 (vap->va_nlink != VNOVAL) ||
1525 (vap->va_fsid != VNOVAL) ||
1526 (vap->va_fileid != VNOVAL) ||
1527 (vap->va_blocksize != VNOVAL) ||
1528 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1529 (vap->va_rdev != VNOVAL) ||
1530 ((int)vap->va_bytes != VNOVAL) ||
1531 (vap->va_gen != VNOVAL)) {
1536 if (vp->v_type == VDIR)
1540 if (vap->va_uid == (uid_t)VNOVAL)
1544 if (vap->va_gid == (gid_t)VNOVAL)
1548 if (uid != de->de_uid || gid != de->de_gid) {
1549 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1550 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1551 error = priv_check(td, PRIV_VFS_CHOWN);
1560 if (vap->va_mode != (mode_t)VNOVAL) {
1561 if (ap->a_cred->cr_uid != de->de_uid) {
1562 error = priv_check(td, PRIV_VFS_ADMIN);
1566 de->de_mode = vap->va_mode;
1570 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1571 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1574 if (vap->va_atime.tv_sec != VNOVAL) {
1575 if (vp->v_type == VCHR)
1576 vp->v_rdev->si_atime = vap->va_atime;
1578 de->de_atime = vap->va_atime;
1580 if (vap->va_mtime.tv_sec != VNOVAL) {
1581 if (vp->v_type == VCHR)
1582 vp->v_rdev->si_mtime = vap->va_mtime;
1584 de->de_mtime = vap->va_mtime;
1590 if (vp->v_type == VCHR)
1591 vfs_timestamp(&vp->v_rdev->si_ctime);
1593 vfs_timestamp(&de->de_mtime);
1600 devfs_setlabel(struct vop_setlabel_args *ap)
1603 struct devfs_dirent *de;
1608 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1609 mac_devfs_update(vp->v_mount, de, vp);
1616 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1619 return (vnops.fo_stat(fp, sb, cred, td));
1623 devfs_symlink(struct vop_symlink_args *ap)
1626 struct devfs_dirent *dd;
1627 struct devfs_dirent *de, *de_covered, *de_dotdot;
1628 struct devfs_mount *dmp;
1630 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1633 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1634 if (devfs_populate_vp(ap->a_dvp) != 0)
1637 dd = ap->a_dvp->v_data;
1638 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1639 de->de_flags = DE_USER;
1643 de->de_inode = alloc_unr(devfs_inos);
1645 de->de_dirent->d_type = DT_LNK;
1646 i = strlen(ap->a_target) + 1;
1647 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1648 bcopy(ap->a_target, de->de_symlink, i);
1650 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1652 de_covered = devfs_find(dd, de->de_dirent->d_name,
1653 de->de_dirent->d_namlen, 0);
1654 if (de_covered != NULL) {
1655 if ((de_covered->de_flags & DE_USER) != 0) {
1656 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1657 sx_xunlock(&dmp->dm_lock);
1660 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1661 ("devfs_symlink: entry %p already covered", de_covered));
1662 de_covered->de_flags |= DE_COVERED;
1665 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1666 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1667 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1668 devfs_dir_ref_de(dmp, dd);
1669 devfs_rules_apply(dmp, de);
1671 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1675 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1678 return (vnops.fo_truncate(fp, length, cred, td));
1682 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1683 int flags, struct thread *td)
1686 int error, ioflag, ref;
1691 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1694 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1696 error = vnops.fo_write(fp, uio, cred, flags, td);
1699 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1700 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1701 if (ioflag & O_DIRECT)
1702 ioflag |= IO_DIRECT;
1703 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1705 resid = uio->uio_resid;
1707 error = dsw->d_write(dev, uio, ioflag);
1708 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1709 devfs_timestamp(&dev->si_ctime);
1710 dev->si_mtime = dev->si_ctime;
1713 dev_relthread(dev, ref);
1715 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1720 dev2udev(struct cdev *x)
1724 return (cdev2priv(x)->cdp_inode);
1727 static struct fileops devfs_ops_f = {
1728 .fo_read = devfs_read_f,
1729 .fo_write = devfs_write_f,
1730 .fo_truncate = devfs_truncate_f,
1731 .fo_ioctl = devfs_ioctl_f,
1732 .fo_poll = devfs_poll_f,
1733 .fo_kqfilter = devfs_kqfilter_f,
1734 .fo_stat = devfs_stat_f,
1735 .fo_close = devfs_close_f,
1736 .fo_chmod = vn_chmod,
1737 .fo_chown = vn_chown,
1738 .fo_sendfile = vn_sendfile,
1740 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1743 static struct vop_vector devfs_vnodeops = {
1744 .vop_default = &default_vnodeops,
1746 .vop_access = devfs_access,
1747 .vop_getattr = devfs_getattr,
1748 .vop_ioctl = devfs_rioctl,
1749 .vop_lookup = devfs_lookup,
1750 .vop_mknod = devfs_mknod,
1751 .vop_pathconf = devfs_pathconf,
1752 .vop_read = devfs_rread,
1753 .vop_readdir = devfs_readdir,
1754 .vop_readlink = devfs_readlink,
1755 .vop_reclaim = devfs_reclaim,
1756 .vop_remove = devfs_remove,
1757 .vop_revoke = devfs_revoke,
1758 .vop_setattr = devfs_setattr,
1760 .vop_setlabel = devfs_setlabel,
1762 .vop_symlink = devfs_symlink,
1763 .vop_vptocnp = devfs_vptocnp,
1766 struct vop_vector devfs_specops = {
1767 .vop_default = &default_vnodeops,
1769 .vop_access = devfs_access,
1770 .vop_bmap = VOP_PANIC,
1771 .vop_close = devfs_close,
1772 .vop_create = VOP_PANIC,
1773 .vop_fsync = devfs_fsync,
1774 .vop_getattr = devfs_getattr,
1775 .vop_link = VOP_PANIC,
1776 .vop_mkdir = VOP_PANIC,
1777 .vop_mknod = VOP_PANIC,
1778 .vop_open = devfs_open,
1779 .vop_pathconf = devfs_pathconf,
1780 .vop_poll = dead_poll,
1781 .vop_print = devfs_print,
1782 .vop_read = dead_read,
1783 .vop_readdir = VOP_PANIC,
1784 .vop_readlink = VOP_PANIC,
1785 .vop_reallocblks = VOP_PANIC,
1786 .vop_reclaim = devfs_reclaim,
1787 .vop_remove = devfs_remove,
1788 .vop_rename = VOP_PANIC,
1789 .vop_revoke = devfs_revoke,
1790 .vop_rmdir = VOP_PANIC,
1791 .vop_setattr = devfs_setattr,
1793 .vop_setlabel = devfs_setlabel,
1795 .vop_strategy = VOP_PANIC,
1796 .vop_symlink = VOP_PANIC,
1797 .vop_vptocnp = devfs_vptocnp,
1798 .vop_write = dead_write,
1802 * Our calling convention to the device drivers used to be that we passed
1803 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1804 * flags instead since that's what open(), close() and ioctl() takes and
1805 * we don't really want vnode.h in device drivers.
1806 * We solved the source compatibility by redefining some vnode flags to
1807 * be the same as the fcntl ones and by sending down the bitwise OR of
1808 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1809 * pulls the rug out under this.
1811 CTASSERT(O_NONBLOCK == IO_NDELAY);
1812 CTASSERT(O_FSYNC == IO_SYNC);