2 * Copyright (c) 2000-2004
3 * Poul-Henning Kamp. All rights reserved.
4 * Copyright (c) 1989, 1992-1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
32 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/dirent.h>
46 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
61 #include <sys/sysctl.h>
63 #include <sys/ttycom.h>
64 #include <sys/unistd.h>
65 #include <sys/vnode.h>
67 static struct vop_vector devfs_vnodeops;
68 static struct vop_vector devfs_specops;
69 static struct fileops devfs_ops_f;
71 #include <fs/devfs/devfs.h>
72 #include <fs/devfs/devfs_int.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_object.h>
80 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
82 struct mtx devfs_de_interlock;
83 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
84 struct sx clone_drain_lock;
85 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
86 struct mtx cdevpriv_mtx;
87 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
89 SYSCTL_DECL(_vfs_devfs);
91 static int devfs_dotimes;
92 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
93 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
96 * Update devfs node timestamp. Note that updates are unlocked and
97 * stat(2) could see partially updated times.
100 devfs_timestamp(struct timespec *tsp)
108 if (tsp->tv_sec != ts) {
116 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
120 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
121 if (*devp != fp->f_data) {
123 dev_relthread(*devp, *ref);
126 KASSERT((*devp)->si_refcount > 0,
127 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
130 curthread->td_fpop = fp;
135 devfs_get_cdevpriv(void **datap)
138 struct cdev_privdata *p;
141 fp = curthread->td_fpop;
147 *datap = p->cdpd_data;
154 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
157 struct cdev_priv *cdp;
158 struct cdev_privdata *p;
161 fp = curthread->td_fpop;
164 cdp = cdev2priv((struct cdev *)fp->f_data);
165 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
167 p->cdpd_dtr = priv_dtr;
169 mtx_lock(&cdevpriv_mtx);
170 if (fp->f_cdevpriv == NULL) {
171 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
173 mtx_unlock(&cdevpriv_mtx);
176 mtx_unlock(&cdevpriv_mtx);
177 free(p, M_CDEVPDATA);
184 devfs_destroy_cdevpriv(struct cdev_privdata *p)
187 mtx_assert(&cdevpriv_mtx, MA_OWNED);
188 KASSERT(p->cdpd_fp->f_cdevpriv == p,
189 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
190 p->cdpd_fp->f_cdevpriv = NULL;
191 LIST_REMOVE(p, cdpd_list);
192 mtx_unlock(&cdevpriv_mtx);
193 (p->cdpd_dtr)(p->cdpd_data);
194 free(p, M_CDEVPDATA);
198 devfs_fpdrop(struct file *fp)
200 struct cdev_privdata *p;
202 mtx_lock(&cdevpriv_mtx);
203 if ((p = fp->f_cdevpriv) == NULL) {
204 mtx_unlock(&cdevpriv_mtx);
207 devfs_destroy_cdevpriv(p);
211 devfs_clear_cdevpriv(void)
215 fp = curthread->td_fpop;
222 * On success devfs_populate_vp() returns with dmp->dm_lock held.
225 devfs_populate_vp(struct vnode *vp)
227 struct devfs_dirent *de;
228 struct devfs_mount *dmp;
231 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
233 dmp = VFSTODEVFS(vp->v_mount);
234 locked = VOP_ISLOCKED(vp);
236 sx_xlock(&dmp->dm_lock);
239 /* Can't call devfs_populate() with the vnode lock held. */
243 sx_xunlock(&dmp->dm_lock);
244 vn_lock(vp, locked | LK_RETRY);
245 sx_xlock(&dmp->dm_lock);
246 if (DEVFS_DMP_DROP(dmp)) {
247 sx_xunlock(&dmp->dm_lock);
248 devfs_unmount_final(dmp);
251 if ((vp->v_iflag & VI_DOOMED) != 0) {
252 sx_xunlock(&dmp->dm_lock);
257 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
258 if ((de->de_flags & DE_DOOMED) != 0) {
259 sx_xunlock(&dmp->dm_lock);
267 devfs_vptocnp(struct vop_vptocnp_args *ap)
269 struct vnode *vp = ap->a_vp;
270 struct vnode **dvp = ap->a_vpp;
271 struct devfs_mount *dmp;
272 char *buf = ap->a_buf;
273 int *buflen = ap->a_buflen;
274 struct devfs_dirent *dd, *de;
277 dmp = VFSTODEVFS(vp->v_mount);
279 error = devfs_populate_vp(vp);
286 if (vp->v_type == VCHR) {
287 i -= strlen(dd->de_cdp->cdp_c.si_name);
292 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
293 strlen(dd->de_cdp->cdp_c.si_name));
295 } else if (vp->v_type == VDIR) {
296 if (dd == dmp->dm_rootdir) {
301 i -= dd->de_dirent->d_namlen;
306 bcopy(dd->de_dirent->d_name, buf + i,
307 dd->de_dirent->d_namlen);
314 de = devfs_parent_dirent(de);
319 mtx_lock(&devfs_de_interlock);
323 mtx_unlock(&devfs_de_interlock);
329 mtx_unlock(&devfs_de_interlock);
333 sx_xunlock(&dmp->dm_lock);
338 * Construct the fully qualified path name relative to the mountpoint.
339 * If a NULL cnp is provided, no '/' is appended to the resulting path.
342 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
343 struct componentname *cnp)
346 struct devfs_dirent *de;
348 sx_assert(&dmp->dm_lock, SA_LOCKED);
353 i -= cnp->cn_namelen;
357 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
359 while (de != dmp->dm_rootdir) {
360 if (cnp != NULL || i < SPECNAMELEN) {
366 i -= de->de_dirent->d_namlen;
369 bcopy(de->de_dirent->d_name, buf + i,
370 de->de_dirent->d_namlen);
371 de = devfs_parent_dirent(de);
379 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
380 struct devfs_dirent *de)
385 if (de->de_flags & DE_DOOMED)
387 if (DEVFS_DE_DROP(de)) {
388 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
389 devfs_dirent_free(de);
391 if (DEVFS_DMP_DROP(dmp)) {
392 KASSERT(not_found == 1,
393 ("DEVFS mount struct freed before dirent"));
395 sx_xunlock(&dmp->dm_lock);
396 devfs_unmount_final(dmp);
398 if (not_found == 1 || (drop_dm_lock && not_found != 2))
399 sx_unlock(&dmp->dm_lock);
404 devfs_insmntque_dtr(struct vnode *vp, void *arg)
406 struct devfs_dirent *de;
408 de = (struct devfs_dirent *)arg;
409 mtx_lock(&devfs_de_interlock);
412 mtx_unlock(&devfs_de_interlock);
418 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
422 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
428 struct devfs_mount *dmp;
431 dmp = VFSTODEVFS(mp);
432 if (de->de_flags & DE_DOOMED) {
433 sx_xunlock(&dmp->dm_lock);
439 mtx_lock(&devfs_de_interlock);
443 mtx_unlock(&devfs_de_interlock);
444 sx_xunlock(&dmp->dm_lock);
445 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
446 sx_xlock(&dmp->dm_lock);
447 if (devfs_allocv_drop_refs(0, dmp, de)) {
451 else if ((vp->v_iflag & VI_DOOMED) != 0) {
452 mtx_lock(&devfs_de_interlock);
453 if (de->de_vnode == vp) {
457 mtx_unlock(&devfs_de_interlock);
461 sx_xunlock(&dmp->dm_lock);
465 mtx_unlock(&devfs_de_interlock);
466 if (de->de_dirent->d_type == DT_CHR) {
467 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
468 devfs_allocv_drop_refs(1, dmp, de);
471 dev = &de->de_cdp->cdp_c;
475 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
477 devfs_allocv_drop_refs(1, dmp, de);
478 printf("devfs_allocv: failed to allocate new vnode\n");
482 if (de->de_dirent->d_type == DT_CHR) {
487 /* XXX: v_rdev should be protect by vnode lock */
489 KASSERT(vp->v_usecount == 1,
490 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
491 dev->si_usecount += vp->v_usecount;
492 /* Special casing of ttys for deadfs. Probably redundant. */
494 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
495 vp->v_vflag |= VV_ISTTY;
498 if ((dev->si_flags & SI_ETERNAL) != 0)
499 vp->v_vflag |= VV_ETERNALDEV;
500 vp->v_op = &devfs_specops;
501 } else if (de->de_dirent->d_type == DT_DIR) {
503 } else if (de->de_dirent->d_type == DT_LNK) {
508 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
510 mtx_lock(&devfs_de_interlock);
513 mtx_unlock(&devfs_de_interlock);
514 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
516 (void) devfs_allocv_drop_refs(1, dmp, de);
519 if (devfs_allocv_drop_refs(0, dmp, de)) {
524 mac_devfs_vnode_associate(mp, de, vp);
526 sx_xunlock(&dmp->dm_lock);
532 devfs_access(struct vop_access_args *ap)
534 struct vnode *vp = ap->a_vp;
535 struct devfs_dirent *de;
540 if (vp->v_type == VDIR)
543 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
544 ap->a_accmode, ap->a_cred, NULL);
549 p = ap->a_td->td_proc;
550 /* We do, however, allow access to the controlling terminal */
552 if (!(p->p_flag & P_CONTROLT)) {
556 if (p->p_session->s_ttydp == de->de_cdp)
562 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
563 "devfs-only flag reuse failed");
566 devfs_close(struct vop_close_args *ap)
568 struct vnode *vp = ap->a_vp, *oldvp;
569 struct thread *td = ap->a_td;
571 struct cdev *dev = vp->v_rdev;
573 int dflags, error, ref, vp_locked;
576 * XXX: Don't call d_close() if we were called because of
577 * XXX: insmntque1() failure.
579 if (vp->v_data == NULL)
583 * Hack: a tty device that is a controlling terminal
584 * has a reference from the session structure.
585 * We cannot easily tell that a character device is
586 * a controlling terminal, unless it is the closing
587 * process' controlling terminal. In that case,
588 * if the reference count is 2 (this last descriptor
589 * plus the session), release the reference from the session.
594 if (vp == p->p_session->s_ttyvp) {
597 sx_xlock(&proctree_lock);
598 if (vp == p->p_session->s_ttyvp) {
599 SESS_LOCK(p->p_session);
601 if (count_dev(dev) == 2 &&
602 (vp->v_iflag & VI_DOOMED) == 0) {
603 p->p_session->s_ttyvp = NULL;
604 p->p_session->s_ttydp = NULL;
608 SESS_UNLOCK(p->p_session);
610 sx_xunlock(&proctree_lock);
617 * We do not want to really close the device if it
618 * is still in use unless we are trying to close it
619 * forcibly. Since every use (buffer, vnode, swap, cmap)
620 * holds a reference to the vnode, and because we mark
621 * any other vnodes that alias this device, when the
622 * sum of the reference counts on all the aliased
623 * vnodes descends to one, we are on last close.
625 dsw = dev_refthread(dev, &ref);
630 if (vp->v_iflag & VI_DOOMED) {
632 dflags |= FREVOKE | FNONBLOCK;
633 } else if (dsw->d_flags & D_TRACKCLOSE) {
634 /* Keep device updated on status. */
635 } else if (count_dev(dev) > 1) {
637 dev_relthread(dev, ref);
640 if (count_dev(dev) == 1)
641 dflags |= FLASTCLOSE;
644 vp_locked = VOP_ISLOCKED(vp);
646 KASSERT(dev->si_refcount > 0,
647 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
648 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
649 dev_relthread(dev, ref);
650 vn_lock(vp, vp_locked | LK_RETRY);
656 devfs_close_f(struct file *fp, struct thread *td)
662 * NB: td may be NULL if this descriptor is closed due to
663 * garbage collection from a closed UNIX domain socket.
665 fpop = curthread->td_fpop;
666 curthread->td_fpop = fp;
667 error = vnops.fo_close(fp, td);
668 curthread->td_fpop = fpop;
671 * The f_cdevpriv cannot be assigned non-NULL value while we
672 * are destroying the file.
674 if (fp->f_cdevpriv != NULL)
680 devfs_fsync(struct vop_fsync_args *ap)
684 struct devfs_dirent *de;
686 if (!vn_isdisk(ap->a_vp, &error)) {
687 bo = &ap->a_vp->v_bufobj;
688 de = ap->a_vp->v_data;
689 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
690 printf("Device %s went missing before all of the data "
691 "could be written to it; expect data loss.\n",
692 de->de_dirent->d_name);
694 error = vop_stdfsync(ap);
695 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
696 printf("devfs_fsync: vop_stdfsync failed.");
702 return (vop_stdfsync(ap));
706 devfs_getattr(struct vop_getattr_args *ap)
708 struct vnode *vp = ap->a_vp;
709 struct vattr *vap = ap->a_vap;
710 struct devfs_dirent *de;
711 struct devfs_mount *dmp;
713 struct timeval boottime;
716 error = devfs_populate_vp(vp);
720 dmp = VFSTODEVFS(vp->v_mount);
721 sx_xunlock(&dmp->dm_lock);
724 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
725 if (vp->v_type == VDIR) {
728 ("Null dir dirent in devfs_getattr vp=%p", vp));
730 vap->va_uid = de->de_uid;
731 vap->va_gid = de->de_gid;
732 vap->va_mode = de->de_mode;
733 if (vp->v_type == VLNK)
734 vap->va_size = strlen(de->de_symlink);
735 else if (vp->v_type == VDIR)
736 vap->va_size = vap->va_bytes = DEV_BSIZE;
739 if (vp->v_type != VDIR)
741 vap->va_blocksize = DEV_BSIZE;
742 vap->va_type = vp->v_type;
744 getboottime(&boottime);
747 if ((aa).tv_sec <= 3600) { \
748 (aa).tv_sec = boottime.tv_sec; \
749 (aa).tv_nsec = boottime.tv_usec * 1000; \
753 if (vp->v_type != VCHR) {
755 vap->va_atime = de->de_atime;
757 vap->va_mtime = de->de_mtime;
759 vap->va_ctime = de->de_ctime;
763 vap->va_atime = dev->si_atime;
765 vap->va_mtime = dev->si_mtime;
767 vap->va_ctime = dev->si_ctime;
769 vap->va_rdev = cdev2priv(dev)->cdp_inode;
774 vap->va_nlink = de->de_links;
775 vap->va_fileid = de->de_inode;
782 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
789 error = vnops.fo_ioctl(fp, com, data, cred, td);
795 devfs_ioctl(struct vop_ioctl_args *ap)
797 struct fiodgname_arg *fgn;
798 struct vnode *vpold, *vp;
810 dsw = devvn_refthread(vp, &dev, &ref);
813 KASSERT(dev->si_refcount > 0,
814 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
816 if (com == FIODTYPE) {
817 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
820 } else if (com == FIODGNAME) {
827 error = copyout(p, fgn->buf, i);
831 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
834 dev_relthread(dev, ref);
835 if (error == ENOIOCTL)
838 if (error == 0 && com == TIOCSCTTY) {
839 /* Do nothing if reassigning same control tty */
840 sx_slock(&proctree_lock);
841 if (td->td_proc->p_session->s_ttyvp == vp) {
842 sx_sunlock(&proctree_lock);
846 vpold = td->td_proc->p_session->s_ttyvp;
848 SESS_LOCK(td->td_proc->p_session);
849 td->td_proc->p_session->s_ttyvp = vp;
850 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
851 SESS_UNLOCK(td->td_proc->p_session);
853 sx_sunlock(&proctree_lock);
855 /* Get rid of reference to old control tty */
864 devfs_kqfilter_f(struct file *fp, struct knote *kn)
874 error = devfs_fp_check(fp, &dev, &dsw, &ref);
877 error = dsw->d_kqfilter(dev, kn);
879 dev_relthread(dev, ref);
884 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
886 struct cdev_priv *cdp;
894 dcr = cdp->cdp_c.si_cred;
898 error = prison_check(td->td_ucred, dcr);
901 /* We do, however, allow access to the controlling terminal */
904 if (!(p->p_flag & P_CONTROLT)) {
908 if (p->p_session->s_ttydp == cdp)
915 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
917 struct componentname *cnp;
918 struct vnode *dvp, **vpp;
920 struct devfs_dirent *de, *dd;
921 struct devfs_dirent **dde;
922 struct devfs_mount *dmp;
924 int error, flags, nameiop, dvplocked;
925 char specname[SPECNAMELEN + 1], *pname;
930 pname = cnp->cn_nameptr;
932 flags = cnp->cn_flags;
933 nameiop = cnp->cn_nameiop;
934 dmp = VFSTODEVFS(dvp->v_mount);
938 if ((flags & ISLASTCN) && nameiop == RENAME)
941 if (dvp->v_type != VDIR)
944 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
947 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
951 if (cnp->cn_namelen == 1 && *pname == '.') {
952 if ((flags & ISLASTCN) && nameiop != LOOKUP)
959 if (flags & ISDOTDOT) {
960 if ((flags & ISLASTCN) && nameiop != LOOKUP)
962 de = devfs_parent_dirent(dd);
965 dvplocked = VOP_ISLOCKED(dvp);
967 error = devfs_allocv(de, dvp->v_mount,
968 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
970 vn_lock(dvp, dvplocked | LK_RETRY);
975 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
976 while (de == NULL) { /* While(...) so we can use break */
978 if (nameiop == DELETE)
982 * OK, we didn't have an entry for the name we were asked for
983 * so we try to see if anybody can create it on demand.
985 pname = devfs_fqpn(specname, dmp, dd, cnp);
991 sx_xunlock(&dmp->dm_lock);
992 sx_slock(&clone_drain_lock);
993 EVENTHANDLER_INVOKE(dev_clone,
994 td->td_ucred, pname, strlen(pname), &cdev);
995 sx_sunlock(&clone_drain_lock);
998 sx_xlock(&dmp->dm_lock);
999 else if (devfs_populate_vp(dvp) != 0) {
1001 sx_xlock(&dmp->dm_lock);
1002 if (DEVFS_DMP_DROP(dmp)) {
1003 sx_xunlock(&dmp->dm_lock);
1004 devfs_unmount_final(dmp);
1006 sx_xunlock(&dmp->dm_lock);
1010 if (DEVFS_DMP_DROP(dmp)) {
1012 sx_xunlock(&dmp->dm_lock);
1013 devfs_unmount_final(dmp);
1023 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1024 if (dde != NULL && *dde != NULL)
1031 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1032 if ((nameiop == CREATE || nameiop == RENAME) &&
1033 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1034 cnp->cn_flags |= SAVENAME;
1035 return (EJUSTRETURN);
1040 if (devfs_prison_check(de, td))
1043 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1044 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1053 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
1060 devfs_lookup(struct vop_lookup_args *ap)
1063 struct devfs_mount *dmp;
1066 if (devfs_populate_vp(ap->a_dvp) != 0)
1069 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1071 j = devfs_lookupx(ap, &dm_unlock);
1073 sx_xunlock(&dmp->dm_lock);
1078 devfs_mknod(struct vop_mknod_args *ap)
1080 struct componentname *cnp;
1081 struct vnode *dvp, **vpp;
1082 struct devfs_dirent *dd, *de;
1083 struct devfs_mount *dmp;
1087 * The only type of node we should be creating here is a
1088 * character device, for anything else return EOPNOTSUPP.
1090 if (ap->a_vap->va_type != VCHR)
1091 return (EOPNOTSUPP);
1093 dmp = VFSTODEVFS(dvp->v_mount);
1100 sx_xlock(&dmp->dm_lock);
1101 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1102 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1104 if (de->de_dirent->d_type == DT_CHR &&
1105 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1107 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1108 de->de_dirent->d_namlen) != 0)
1110 if (de->de_flags & DE_WHITEOUT)
1116 de->de_flags &= ~DE_WHITEOUT;
1117 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1120 sx_xunlock(&dmp->dm_lock);
1126 devfs_open(struct vop_open_args *ap)
1128 struct thread *td = ap->a_td;
1129 struct vnode *vp = ap->a_vp;
1130 struct cdev *dev = vp->v_rdev;
1131 struct file *fp = ap->a_fp;
1132 int error, ref, vlocked;
1137 if (vp->v_type == VBLK)
1143 /* Make this field valid before any I/O in d_open. */
1144 if (dev->si_iosize_max == 0)
1145 dev->si_iosize_max = DFLTPHYS;
1147 dsw = dev_refthread(dev, &ref);
1150 if (fp == NULL && dsw->d_fdopen != NULL) {
1151 dev_relthread(dev, ref);
1155 vlocked = VOP_ISLOCKED(vp);
1164 if (dsw->d_fdopen != NULL)
1165 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1167 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1168 /* Clean up any cdevpriv upon error. */
1170 devfs_clear_cdevpriv();
1173 vn_lock(vp, vlocked | LK_RETRY);
1174 dev_relthread(dev, ref);
1176 if (error == ERESTART)
1181 #if 0 /* /dev/console */
1182 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1187 if (fp->f_ops == &badfileops)
1188 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1189 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1192 * Hint to the dofilewrite() to not force the buffer draining
1193 * on the writer to the file. Most likely, the write would
1194 * not need normal buffers.
1197 fp->f_vnread_flags |= FDEVFS_VNODE;
1203 devfs_pathconf(struct vop_pathconf_args *ap)
1206 switch (ap->a_name) {
1207 case _PC_MAC_PRESENT:
1210 * If MAC is enabled, devfs automatically supports
1211 * trivial non-persistant label storage.
1219 return (vop_stdpathconf(ap));
1226 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1234 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1236 error = vnops.fo_poll(fp, events, cred, td);
1239 error = dsw->d_poll(dev, events, td);
1241 dev_relthread(dev, ref);
1246 * Print out the contents of a special device vnode.
1249 devfs_print(struct vop_print_args *ap)
1252 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1257 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1258 int flags, struct thread *td)
1261 int ioflag, error, ref;
1266 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1269 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1271 error = vnops.fo_read(fp, uio, cred, flags, td);
1274 resid = uio->uio_resid;
1275 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1276 if (ioflag & O_DIRECT)
1277 ioflag |= IO_DIRECT;
1279 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1280 error = dsw->d_read(dev, uio, ioflag);
1281 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1282 devfs_timestamp(&dev->si_atime);
1284 dev_relthread(dev, ref);
1286 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1291 devfs_readdir(struct vop_readdir_args *ap)
1296 struct devfs_dirent *dd;
1297 struct devfs_dirent *de;
1298 struct devfs_mount *dmp;
1300 int *tmp_ncookies = NULL;
1302 if (ap->a_vp->v_type != VDIR)
1306 if (uio->uio_offset < 0)
1310 * XXX: This is a temporary hack to get around this filesystem not
1311 * supporting cookies. We store the location of the ncookies pointer
1312 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1313 * and set the number of cookies to 0. We then set the pointer to
1314 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1315 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1316 * pointer to its original location before returning to the caller.
1318 if (ap->a_ncookies != NULL) {
1319 tmp_ncookies = ap->a_ncookies;
1320 *ap->a_ncookies = 0;
1321 ap->a_ncookies = NULL;
1324 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1325 if (devfs_populate_vp(ap->a_vp) != 0) {
1326 if (tmp_ncookies != NULL)
1327 ap->a_ncookies = tmp_ncookies;
1331 de = ap->a_vp->v_data;
1333 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1334 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1335 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1337 if (devfs_prison_check(dd, uio->uio_td))
1339 if (dd->de_dirent->d_type == DT_DIR)
1344 if (dp->d_reclen > uio->uio_resid)
1346 dp->d_fileno = de->de_inode;
1347 if (off >= uio->uio_offset) {
1348 error = vfs_read_dirent(ap, dp, off);
1352 off += dp->d_reclen;
1354 sx_xunlock(&dmp->dm_lock);
1355 uio->uio_offset = off;
1358 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1361 if (tmp_ncookies != NULL)
1362 ap->a_ncookies = tmp_ncookies;
1368 devfs_readlink(struct vop_readlink_args *ap)
1370 struct devfs_dirent *de;
1372 de = ap->a_vp->v_data;
1373 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1377 devfs_reclaim(struct vop_reclaim_args *ap)
1380 struct devfs_dirent *de;
1383 mtx_lock(&devfs_de_interlock);
1386 de->de_vnode = NULL;
1389 mtx_unlock(&devfs_de_interlock);
1390 vnode_destroy_vobject(vp);
1395 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1401 MPASS(vp->v_type == VCHR);
1410 dev->si_usecount -= vp->v_usecount;
1419 devfs_remove(struct vop_remove_args *ap)
1421 struct vnode *dvp = ap->a_dvp;
1422 struct vnode *vp = ap->a_vp;
1423 struct devfs_dirent *dd;
1424 struct devfs_dirent *de, *de_covered;
1425 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1427 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1428 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1430 sx_xlock(&dmp->dm_lock);
1431 dd = ap->a_dvp->v_data;
1433 if (de->de_cdp == NULL) {
1434 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1435 if (de->de_dirent->d_type == DT_LNK) {
1436 de_covered = devfs_find(dd, de->de_dirent->d_name,
1437 de->de_dirent->d_namlen, 0);
1438 if (de_covered != NULL)
1439 de_covered->de_flags &= ~DE_COVERED;
1441 /* We need to unlock dvp because devfs_delete() may lock it. */
1445 devfs_delete(dmp, de, 0);
1446 sx_xunlock(&dmp->dm_lock);
1448 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1449 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1451 de->de_flags |= DE_WHITEOUT;
1452 sx_xunlock(&dmp->dm_lock);
1458 * Revoke is called on a tty when a terminal session ends. The vnode
1459 * is orphaned by setting v_op to deadfs so we need to let go of it
1460 * as well so that we create a new one next time around.
1464 devfs_revoke(struct vop_revoke_args *ap)
1466 struct vnode *vp = ap->a_vp, *vp2;
1468 struct cdev_priv *cdp;
1469 struct devfs_dirent *de;
1472 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1475 cdp = cdev2priv(dev);
1488 mtx_lock(&devfs_de_interlock);
1491 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1492 de = cdp->cdp_dirents[i];
1500 mtx_unlock(&devfs_de_interlock);
1501 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1515 mtx_unlock(&devfs_de_interlock);
1520 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1521 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1523 dev_rel(&cdp->cdp_c);
1527 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1532 devfs_rioctl(struct vop_ioctl_args *ap)
1535 struct devfs_mount *dmp;
1539 vn_lock(vp, LK_SHARED | LK_RETRY);
1540 if (vp->v_iflag & VI_DOOMED) {
1544 dmp = VFSTODEVFS(vp->v_mount);
1545 sx_xlock(&dmp->dm_lock);
1547 DEVFS_DMP_HOLD(dmp);
1548 devfs_populate(dmp);
1549 if (DEVFS_DMP_DROP(dmp)) {
1550 sx_xunlock(&dmp->dm_lock);
1551 devfs_unmount_final(dmp);
1554 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1555 sx_xunlock(&dmp->dm_lock);
1560 devfs_rread(struct vop_read_args *ap)
1563 if (ap->a_vp->v_type != VDIR)
1565 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1569 devfs_setattr(struct vop_setattr_args *ap)
1571 struct devfs_dirent *de;
1582 if ((vap->va_type != VNON) ||
1583 (vap->va_nlink != VNOVAL) ||
1584 (vap->va_fsid != VNOVAL) ||
1585 (vap->va_fileid != VNOVAL) ||
1586 (vap->va_blocksize != VNOVAL) ||
1587 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1588 (vap->va_rdev != VNOVAL) ||
1589 ((int)vap->va_bytes != VNOVAL) ||
1590 (vap->va_gen != VNOVAL)) {
1594 error = devfs_populate_vp(vp);
1599 if (vp->v_type == VDIR)
1603 if (vap->va_uid == (uid_t)VNOVAL)
1607 if (vap->va_gid == (gid_t)VNOVAL)
1611 if (uid != de->de_uid || gid != de->de_gid) {
1612 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1613 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1614 error = priv_check(td, PRIV_VFS_CHOWN);
1623 if (vap->va_mode != (mode_t)VNOVAL) {
1624 if (ap->a_cred->cr_uid != de->de_uid) {
1625 error = priv_check(td, PRIV_VFS_ADMIN);
1629 de->de_mode = vap->va_mode;
1633 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1634 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1637 if (vap->va_atime.tv_sec != VNOVAL) {
1638 if (vp->v_type == VCHR)
1639 vp->v_rdev->si_atime = vap->va_atime;
1641 de->de_atime = vap->va_atime;
1643 if (vap->va_mtime.tv_sec != VNOVAL) {
1644 if (vp->v_type == VCHR)
1645 vp->v_rdev->si_mtime = vap->va_mtime;
1647 de->de_mtime = vap->va_mtime;
1653 if (vp->v_type == VCHR)
1654 vfs_timestamp(&vp->v_rdev->si_ctime);
1656 vfs_timestamp(&de->de_mtime);
1660 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1666 devfs_setlabel(struct vop_setlabel_args *ap)
1669 struct devfs_dirent *de;
1674 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1675 mac_devfs_update(vp->v_mount, de, vp);
1682 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1685 return (vnops.fo_stat(fp, sb, cred, td));
1689 devfs_symlink(struct vop_symlink_args *ap)
1692 struct devfs_dirent *dd;
1693 struct devfs_dirent *de, *de_covered, *de_dotdot;
1694 struct devfs_mount *dmp;
1696 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1699 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1700 if (devfs_populate_vp(ap->a_dvp) != 0)
1703 dd = ap->a_dvp->v_data;
1704 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1705 de->de_flags = DE_USER;
1709 de->de_inode = alloc_unr(devfs_inos);
1711 de->de_dirent->d_type = DT_LNK;
1712 i = strlen(ap->a_target) + 1;
1713 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1714 bcopy(ap->a_target, de->de_symlink, i);
1716 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1718 de_covered = devfs_find(dd, de->de_dirent->d_name,
1719 de->de_dirent->d_namlen, 0);
1720 if (de_covered != NULL) {
1721 if ((de_covered->de_flags & DE_USER) != 0) {
1722 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1723 sx_xunlock(&dmp->dm_lock);
1726 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1727 ("devfs_symlink: entry %p already covered", de_covered));
1728 de_covered->de_flags |= DE_COVERED;
1731 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1732 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1733 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1734 devfs_dir_ref_de(dmp, dd);
1735 devfs_rules_apply(dmp, de);
1737 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1741 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1744 return (vnops.fo_truncate(fp, length, cred, td));
1748 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1749 int flags, struct thread *td)
1752 int error, ioflag, ref;
1757 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1760 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1762 error = vnops.fo_write(fp, uio, cred, flags, td);
1765 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1766 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1767 if (ioflag & O_DIRECT)
1768 ioflag |= IO_DIRECT;
1769 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1771 resid = uio->uio_resid;
1773 error = dsw->d_write(dev, uio, ioflag);
1774 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1775 devfs_timestamp(&dev->si_ctime);
1776 dev->si_mtime = dev->si_ctime;
1779 dev_relthread(dev, ref);
1781 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1786 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1787 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1802 * Ensure that file and memory protections are
1806 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0)
1807 maxprot = VM_PROT_NONE;
1809 maxprot = VM_PROT_EXECUTE;
1810 if ((fp->f_flag & FREAD) != 0)
1811 maxprot |= VM_PROT_READ;
1812 else if ((prot & VM_PROT_READ) != 0)
1816 * If we are sharing potential changes via MAP_SHARED and we
1817 * are trying to get write permission although we opened it
1818 * without asking for it, bail out.
1820 * Note that most character devices always share mappings.
1821 * The one exception is that D_MMAP_ANON devices
1822 * (i.e. /dev/zero) permit private writable mappings.
1824 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1825 * as well as updating maxprot to permit writing for
1826 * D_MMAP_ANON devices rather than doing that here.
1828 if ((flags & MAP_SHARED) != 0) {
1829 if ((fp->f_flag & FWRITE) != 0)
1830 maxprot |= VM_PROT_WRITE;
1831 else if ((prot & VM_PROT_WRITE) != 0)
1834 maxprot &= cap_maxprot;
1837 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1841 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1844 dev_relthread(dev, ref);
1848 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1851 vm_object_deallocate(object);
1856 dev2udev(struct cdev *x)
1860 return (cdev2priv(x)->cdp_inode);
1863 static struct fileops devfs_ops_f = {
1864 .fo_read = devfs_read_f,
1865 .fo_write = devfs_write_f,
1866 .fo_truncate = devfs_truncate_f,
1867 .fo_ioctl = devfs_ioctl_f,
1868 .fo_poll = devfs_poll_f,
1869 .fo_kqfilter = devfs_kqfilter_f,
1870 .fo_stat = devfs_stat_f,
1871 .fo_close = devfs_close_f,
1872 .fo_chmod = vn_chmod,
1873 .fo_chown = vn_chown,
1874 .fo_sendfile = vn_sendfile,
1876 .fo_fill_kinfo = vn_fill_kinfo,
1877 .fo_mmap = devfs_mmap_f,
1878 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1881 /* Vops for non-CHR vnodes in /dev. */
1882 static struct vop_vector devfs_vnodeops = {
1883 .vop_default = &default_vnodeops,
1885 .vop_access = devfs_access,
1886 .vop_getattr = devfs_getattr,
1887 .vop_ioctl = devfs_rioctl,
1888 .vop_lookup = devfs_lookup,
1889 .vop_mknod = devfs_mknod,
1890 .vop_pathconf = devfs_pathconf,
1891 .vop_read = devfs_rread,
1892 .vop_readdir = devfs_readdir,
1893 .vop_readlink = devfs_readlink,
1894 .vop_reclaim = devfs_reclaim,
1895 .vop_remove = devfs_remove,
1896 .vop_revoke = devfs_revoke,
1897 .vop_setattr = devfs_setattr,
1899 .vop_setlabel = devfs_setlabel,
1901 .vop_symlink = devfs_symlink,
1902 .vop_vptocnp = devfs_vptocnp,
1905 /* Vops for VCHR vnodes in /dev. */
1906 static struct vop_vector devfs_specops = {
1907 .vop_default = &default_vnodeops,
1909 .vop_access = devfs_access,
1910 .vop_bmap = VOP_PANIC,
1911 .vop_close = devfs_close,
1912 .vop_create = VOP_PANIC,
1913 .vop_fsync = devfs_fsync,
1914 .vop_getattr = devfs_getattr,
1915 .vop_ioctl = devfs_ioctl,
1916 .vop_link = VOP_PANIC,
1917 .vop_mkdir = VOP_PANIC,
1918 .vop_mknod = VOP_PANIC,
1919 .vop_open = devfs_open,
1920 .vop_pathconf = devfs_pathconf,
1921 .vop_poll = dead_poll,
1922 .vop_print = devfs_print,
1923 .vop_read = dead_read,
1924 .vop_readdir = VOP_PANIC,
1925 .vop_readlink = VOP_PANIC,
1926 .vop_reallocblks = VOP_PANIC,
1927 .vop_reclaim = devfs_reclaim_vchr,
1928 .vop_remove = devfs_remove,
1929 .vop_rename = VOP_PANIC,
1930 .vop_revoke = devfs_revoke,
1931 .vop_rmdir = VOP_PANIC,
1932 .vop_setattr = devfs_setattr,
1934 .vop_setlabel = devfs_setlabel,
1936 .vop_strategy = VOP_PANIC,
1937 .vop_symlink = VOP_PANIC,
1938 .vop_vptocnp = devfs_vptocnp,
1939 .vop_write = dead_write,
1943 * Our calling convention to the device drivers used to be that we passed
1944 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1945 * flags instead since that's what open(), close() and ioctl() takes and
1946 * we don't really want vnode.h in device drivers.
1947 * We solved the source compatibility by redefining some vnode flags to
1948 * be the same as the fcntl ones and by sending down the bitwise OR of
1949 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1950 * pulls the rug out under this.
1952 CTASSERT(O_NONBLOCK == IO_NDELAY);
1953 CTASSERT(O_FSYNC == IO_SYNC);