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>
52 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.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);
85 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
89 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
90 if (*devp != fp->f_data) {
92 dev_relthread(*devp, *ref);
95 KASSERT((*devp)->si_refcount > 0,
96 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
99 curthread->td_fpop = fp;
104 devfs_get_cdevpriv(void **datap)
107 struct cdev_privdata *p;
110 fp = curthread->td_fpop;
116 *datap = p->cdpd_data;
123 devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t priv_dtr)
126 struct cdev_priv *cdp;
127 struct cdev_privdata *p;
130 fp = curthread->td_fpop;
133 cdp = cdev2priv((struct cdev *)fp->f_data);
134 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
136 p->cdpd_dtr = priv_dtr;
138 mtx_lock(&cdevpriv_mtx);
139 if (fp->f_cdevpriv == NULL) {
140 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
142 mtx_unlock(&cdevpriv_mtx);
145 mtx_unlock(&cdevpriv_mtx);
146 free(p, M_CDEVPDATA);
153 devfs_destroy_cdevpriv(struct cdev_privdata *p)
156 mtx_assert(&cdevpriv_mtx, MA_OWNED);
157 p->cdpd_fp->f_cdevpriv = NULL;
158 LIST_REMOVE(p, cdpd_list);
159 mtx_unlock(&cdevpriv_mtx);
160 (p->cdpd_dtr)(p->cdpd_data);
161 free(p, M_CDEVPDATA);
165 devfs_fpdrop(struct file *fp)
167 struct cdev_privdata *p;
169 mtx_lock(&cdevpriv_mtx);
170 if ((p = fp->f_cdevpriv) == NULL) {
171 mtx_unlock(&cdevpriv_mtx);
174 devfs_destroy_cdevpriv(p);
178 devfs_clear_cdevpriv(void)
182 fp = curthread->td_fpop;
189 * On success devfs_populate_vp() returns with dmp->dm_lock held.
192 devfs_populate_vp(struct vnode *vp)
194 struct devfs_dirent *de;
195 struct devfs_mount *dmp;
198 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
200 dmp = VFSTODEVFS(vp->v_mount);
201 locked = VOP_ISLOCKED(vp);
203 sx_xlock(&dmp->dm_lock);
206 /* Can't call devfs_populate() with the vnode lock held. */
210 sx_xunlock(&dmp->dm_lock);
211 vn_lock(vp, locked | LK_RETRY);
212 sx_xlock(&dmp->dm_lock);
213 if (DEVFS_DMP_DROP(dmp)) {
214 sx_xunlock(&dmp->dm_lock);
215 devfs_unmount_final(dmp);
218 if ((vp->v_iflag & VI_DOOMED) != 0) {
219 sx_xunlock(&dmp->dm_lock);
224 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
225 if ((de->de_flags & DE_DOOMED) != 0) {
226 sx_xunlock(&dmp->dm_lock);
234 devfs_vptocnp(struct vop_vptocnp_args *ap)
236 struct vnode *vp = ap->a_vp;
237 struct vnode **dvp = ap->a_vpp;
238 struct devfs_mount *dmp;
239 char *buf = ap->a_buf;
240 int *buflen = ap->a_buflen;
241 struct devfs_dirent *dd, *de;
244 dmp = VFSTODEVFS(vp->v_mount);
246 error = devfs_populate_vp(vp);
253 if (vp->v_type == VCHR) {
254 i -= strlen(dd->de_cdp->cdp_c.si_name);
259 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
260 strlen(dd->de_cdp->cdp_c.si_name));
262 } else if (vp->v_type == VDIR) {
263 if (dd == dmp->dm_rootdir) {
268 i -= dd->de_dirent->d_namlen;
273 bcopy(dd->de_dirent->d_name, buf + i,
274 dd->de_dirent->d_namlen);
281 de = devfs_parent_dirent(de);
286 mtx_lock(&devfs_de_interlock);
290 mtx_unlock(&devfs_de_interlock);
296 mtx_unlock(&devfs_de_interlock);
300 sx_xunlock(&dmp->dm_lock);
305 * Construct the fully qualified path name relative to the mountpoint.
306 * If a NULL cnp is provided, no '/' is appended to the resulting path.
309 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
310 struct componentname *cnp)
313 struct devfs_dirent *de;
315 sx_assert(&dmp->dm_lock, SA_LOCKED);
320 i -= cnp->cn_namelen;
324 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
326 while (de != dmp->dm_rootdir) {
327 if (cnp != NULL || i < SPECNAMELEN) {
333 i -= de->de_dirent->d_namlen;
336 bcopy(de->de_dirent->d_name, buf + i,
337 de->de_dirent->d_namlen);
338 de = devfs_parent_dirent(de);
346 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
347 struct devfs_dirent *de)
352 if (de->de_flags & DE_DOOMED)
354 if (DEVFS_DE_DROP(de)) {
355 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
356 devfs_dirent_free(de);
358 if (DEVFS_DMP_DROP(dmp)) {
359 KASSERT(not_found == 1,
360 ("DEVFS mount struct freed before dirent"));
362 sx_xunlock(&dmp->dm_lock);
363 devfs_unmount_final(dmp);
365 if (not_found == 1 || (drop_dm_lock && not_found != 2))
366 sx_unlock(&dmp->dm_lock);
371 devfs_insmntque_dtr(struct vnode *vp, void *arg)
373 struct devfs_dirent *de;
375 de = (struct devfs_dirent *)arg;
376 mtx_lock(&devfs_de_interlock);
379 mtx_unlock(&devfs_de_interlock);
385 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
389 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
395 struct devfs_mount *dmp;
398 dmp = VFSTODEVFS(mp);
399 if (de->de_flags & DE_DOOMED) {
400 sx_xunlock(&dmp->dm_lock);
406 mtx_lock(&devfs_de_interlock);
410 mtx_unlock(&devfs_de_interlock);
411 sx_xunlock(&dmp->dm_lock);
412 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
413 sx_xlock(&dmp->dm_lock);
414 if (devfs_allocv_drop_refs(0, dmp, de)) {
418 else if ((vp->v_iflag & VI_DOOMED) != 0) {
419 mtx_lock(&devfs_de_interlock);
420 if (de->de_vnode == vp) {
424 mtx_unlock(&devfs_de_interlock);
428 sx_xunlock(&dmp->dm_lock);
432 mtx_unlock(&devfs_de_interlock);
433 if (de->de_dirent->d_type == DT_CHR) {
434 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
435 devfs_allocv_drop_refs(1, dmp, de);
438 dev = &de->de_cdp->cdp_c;
442 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
444 devfs_allocv_drop_refs(1, dmp, de);
445 printf("devfs_allocv: failed to allocate new vnode\n");
449 if (de->de_dirent->d_type == DT_CHR) {
454 /* XXX: v_rdev should be protect by vnode lock */
456 KASSERT(vp->v_usecount == 1,
457 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
458 dev->si_usecount += vp->v_usecount;
459 /* Special casing of ttys for deadfs. Probably redundant. */
461 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
462 vp->v_vflag |= VV_ISTTY;
465 if ((dev->si_flags & SI_ETERNAL) != 0)
466 vp->v_vflag |= VV_ETERNALDEV;
467 vp->v_op = &devfs_specops;
468 } else if (de->de_dirent->d_type == DT_DIR) {
470 } else if (de->de_dirent->d_type == DT_LNK) {
475 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
477 mtx_lock(&devfs_de_interlock);
480 mtx_unlock(&devfs_de_interlock);
481 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
483 (void) devfs_allocv_drop_refs(1, dmp, de);
486 if (devfs_allocv_drop_refs(0, dmp, de)) {
491 mac_devfs_vnode_associate(mp, de, vp);
493 sx_xunlock(&dmp->dm_lock);
499 devfs_access(struct vop_access_args *ap)
501 struct vnode *vp = ap->a_vp;
502 struct devfs_dirent *de;
506 if (vp->v_type == VDIR)
509 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
510 ap->a_accmode, ap->a_cred, NULL);
515 /* We do, however, allow access to the controlling terminal */
516 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
518 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
525 devfs_close(struct vop_close_args *ap)
527 struct vnode *vp = ap->a_vp, *oldvp;
528 struct thread *td = ap->a_td;
529 struct cdev *dev = vp->v_rdev;
531 int vp_locked, error, ref;
534 * XXX: Don't call d_close() if we were called because of
535 * XXX: insmntque1() failure.
537 if (vp->v_data == NULL)
541 * Hack: a tty device that is a controlling terminal
542 * has a reference from the session structure.
543 * We cannot easily tell that a character device is
544 * a controlling terminal, unless it is the closing
545 * process' controlling terminal. In that case,
546 * if the reference count is 2 (this last descriptor
547 * plus the session), release the reference from the session.
550 sx_xlock(&proctree_lock);
551 if (td && vp == td->td_proc->p_session->s_ttyvp) {
552 SESS_LOCK(td->td_proc->p_session);
554 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
555 td->td_proc->p_session->s_ttyvp = NULL;
556 td->td_proc->p_session->s_ttydp = NULL;
560 SESS_UNLOCK(td->td_proc->p_session);
562 sx_xunlock(&proctree_lock);
566 * We do not want to really close the device if it
567 * is still in use unless we are trying to close it
568 * forcibly. Since every use (buffer, vnode, swap, cmap)
569 * holds a reference to the vnode, and because we mark
570 * any other vnodes that alias this device, when the
571 * sum of the reference counts on all the aliased
572 * vnodes descends to one, we are on last close.
574 dsw = dev_refthread(dev, &ref);
578 if (vp->v_iflag & VI_DOOMED) {
580 } else if (dsw->d_flags & D_TRACKCLOSE) {
581 /* Keep device updated on status. */
582 } else if (count_dev(dev) > 1) {
584 dev_relthread(dev, ref);
589 vp_locked = VOP_ISLOCKED(vp);
591 KASSERT(dev->si_refcount > 0,
592 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
593 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
594 dev_relthread(dev, ref);
595 vn_lock(vp, vp_locked | LK_RETRY);
601 devfs_close_f(struct file *fp, struct thread *td)
607 * NB: td may be NULL if this descriptor is closed due to
608 * garbage collection from a closed UNIX domain socket.
610 fpop = curthread->td_fpop;
611 curthread->td_fpop = fp;
612 error = vnops.fo_close(fp, td);
613 curthread->td_fpop = fpop;
616 * The f_cdevpriv cannot be assigned non-NULL value while we
617 * are destroying the file.
619 if (fp->f_cdevpriv != NULL)
625 devfs_fsync(struct vop_fsync_args *ap)
629 struct devfs_dirent *de;
631 if (!vn_isdisk(ap->a_vp, &error)) {
632 bo = &ap->a_vp->v_bufobj;
633 de = ap->a_vp->v_data;
634 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
635 printf("Device %s went missing before all of the data "
636 "could be written to it; expect data loss.\n",
637 de->de_dirent->d_name);
639 error = vop_stdfsync(ap);
640 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
641 panic("devfs_fsync: vop_stdfsync failed.");
647 return (vop_stdfsync(ap));
651 devfs_getattr(struct vop_getattr_args *ap)
653 struct vnode *vp = ap->a_vp;
654 struct vattr *vap = ap->a_vap;
656 struct devfs_dirent *de;
657 struct devfs_mount *dmp;
660 error = devfs_populate_vp(vp);
664 dmp = VFSTODEVFS(vp->v_mount);
665 sx_xunlock(&dmp->dm_lock);
668 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
669 if (vp->v_type == VDIR) {
672 ("Null dir dirent in devfs_getattr vp=%p", vp));
674 vap->va_uid = de->de_uid;
675 vap->va_gid = de->de_gid;
676 vap->va_mode = de->de_mode;
677 if (vp->v_type == VLNK)
678 vap->va_size = strlen(de->de_symlink);
679 else if (vp->v_type == VDIR)
680 vap->va_size = vap->va_bytes = DEV_BSIZE;
683 if (vp->v_type != VDIR)
685 vap->va_blocksize = DEV_BSIZE;
686 vap->va_type = vp->v_type;
690 if ((aa).tv_sec <= 3600) { \
691 (aa).tv_sec = boottime.tv_sec; \
692 (aa).tv_nsec = boottime.tv_usec * 1000; \
696 if (vp->v_type != VCHR) {
698 vap->va_atime = de->de_atime;
700 vap->va_mtime = de->de_mtime;
702 vap->va_ctime = de->de_ctime;
706 vap->va_atime = dev->si_atime;
708 vap->va_mtime = dev->si_mtime;
710 vap->va_ctime = dev->si_ctime;
712 vap->va_rdev = cdev2priv(dev)->cdp_inode;
717 vap->va_nlink = de->de_links;
718 vap->va_fileid = de->de_inode;
725 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
733 struct fiodgname_arg *fgn;
737 error = devfs_fp_check(fp, &dev, &dsw, &ref);
741 if (com == FIODTYPE) {
742 *(int *)data = dsw->d_flags & D_TYPEMASK;
744 dev_relthread(dev, ref);
746 } else if (com == FIODGNAME) {
753 error = copyout(p, fgn->buf, i);
755 dev_relthread(dev, ref);
758 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
760 dev_relthread(dev, ref);
761 if (error == ENOIOCTL)
763 if (error == 0 && com == TIOCSCTTY) {
766 /* Do nothing if reassigning same control tty */
767 sx_slock(&proctree_lock);
768 if (td->td_proc->p_session->s_ttyvp == vp) {
769 sx_sunlock(&proctree_lock);
773 vpold = td->td_proc->p_session->s_ttyvp;
775 SESS_LOCK(td->td_proc->p_session);
776 td->td_proc->p_session->s_ttyvp = vp;
777 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
778 SESS_UNLOCK(td->td_proc->p_session);
780 sx_sunlock(&proctree_lock);
782 /* Get rid of reference to old control tty */
791 devfs_kqfilter_f(struct file *fp, struct knote *kn)
801 error = devfs_fp_check(fp, &dev, &dsw, &ref);
804 error = dsw->d_kqfilter(dev, kn);
806 dev_relthread(dev, ref);
811 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
813 struct cdev_priv *cdp;
820 dcr = cdp->cdp_c.si_cred;
824 error = prison_check(td->td_ucred, dcr);
827 /* We do, however, allow access to the controlling terminal */
828 if (!(td->td_proc->p_flag & P_CONTROLT))
830 if (td->td_proc->p_session->s_ttydp == cdp)
836 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
838 struct componentname *cnp;
839 struct vnode *dvp, **vpp;
841 struct devfs_dirent *de, *dd;
842 struct devfs_dirent **dde;
843 struct devfs_mount *dmp;
845 int error, flags, nameiop, dvplocked;
846 char specname[SPECNAMELEN + 1], *pname;
851 pname = cnp->cn_nameptr;
853 flags = cnp->cn_flags;
854 nameiop = cnp->cn_nameiop;
855 dmp = VFSTODEVFS(dvp->v_mount);
859 if ((flags & ISLASTCN) && nameiop == RENAME)
862 if (dvp->v_type != VDIR)
865 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
868 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
872 if (cnp->cn_namelen == 1 && *pname == '.') {
873 if ((flags & ISLASTCN) && nameiop != LOOKUP)
880 if (flags & ISDOTDOT) {
881 if ((flags & ISLASTCN) && nameiop != LOOKUP)
883 de = devfs_parent_dirent(dd);
886 dvplocked = VOP_ISLOCKED(dvp);
888 error = devfs_allocv(de, dvp->v_mount,
889 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
891 vn_lock(dvp, dvplocked | LK_RETRY);
896 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
897 while (de == NULL) { /* While(...) so we can use break */
899 if (nameiop == DELETE)
903 * OK, we didn't have an entry for the name we were asked for
904 * so we try to see if anybody can create it on demand.
906 pname = devfs_fqpn(specname, dmp, dd, cnp);
912 sx_xunlock(&dmp->dm_lock);
913 sx_slock(&clone_drain_lock);
914 EVENTHANDLER_INVOKE(dev_clone,
915 td->td_ucred, pname, strlen(pname), &cdev);
916 sx_sunlock(&clone_drain_lock);
919 sx_xlock(&dmp->dm_lock);
920 else if (devfs_populate_vp(dvp) != 0) {
922 sx_xlock(&dmp->dm_lock);
923 if (DEVFS_DMP_DROP(dmp)) {
924 sx_xunlock(&dmp->dm_lock);
925 devfs_unmount_final(dmp);
927 sx_xunlock(&dmp->dm_lock);
931 if (DEVFS_DMP_DROP(dmp)) {
933 sx_xunlock(&dmp->dm_lock);
934 devfs_unmount_final(dmp);
944 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
945 if (dde != NULL && *dde != NULL)
952 if (de == NULL || de->de_flags & DE_WHITEOUT) {
953 if ((nameiop == CREATE || nameiop == RENAME) &&
954 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
955 cnp->cn_flags |= SAVENAME;
956 return (EJUSTRETURN);
961 if (devfs_prison_check(de, td))
964 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
965 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
974 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
981 devfs_lookup(struct vop_lookup_args *ap)
984 struct devfs_mount *dmp;
987 if (devfs_populate_vp(ap->a_dvp) != 0)
990 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
992 j = devfs_lookupx(ap, &dm_unlock);
994 sx_xunlock(&dmp->dm_lock);
999 devfs_mknod(struct vop_mknod_args *ap)
1001 struct componentname *cnp;
1002 struct vnode *dvp, **vpp;
1003 struct devfs_dirent *dd, *de;
1004 struct devfs_mount *dmp;
1008 * The only type of node we should be creating here is a
1009 * character device, for anything else return EOPNOTSUPP.
1011 if (ap->a_vap->va_type != VCHR)
1012 return (EOPNOTSUPP);
1014 dmp = VFSTODEVFS(dvp->v_mount);
1021 sx_xlock(&dmp->dm_lock);
1022 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1023 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1025 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1026 de->de_dirent->d_namlen) != 0)
1028 if (de->de_flags & DE_WHITEOUT)
1034 de->de_flags &= ~DE_WHITEOUT;
1035 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1038 sx_xunlock(&dmp->dm_lock);
1044 devfs_open(struct vop_open_args *ap)
1046 struct thread *td = ap->a_td;
1047 struct vnode *vp = ap->a_vp;
1048 struct cdev *dev = vp->v_rdev;
1049 struct file *fp = ap->a_fp;
1050 int error, ref, vlocked;
1055 if (vp->v_type == VBLK)
1061 /* Make this field valid before any I/O in d_open. */
1062 if (dev->si_iosize_max == 0)
1063 dev->si_iosize_max = DFLTPHYS;
1065 dsw = dev_refthread(dev, &ref);
1068 if (fp == NULL && dsw->d_fdopen != NULL) {
1069 dev_relthread(dev, ref);
1073 vlocked = VOP_ISLOCKED(vp);
1082 if (dsw->d_fdopen != NULL)
1083 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1085 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1086 /* cleanup any cdevpriv upon error */
1088 devfs_clear_cdevpriv();
1091 vn_lock(vp, vlocked | LK_RETRY);
1092 dev_relthread(dev, ref);
1094 if (error == ERESTART)
1099 #if 0 /* /dev/console */
1100 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1105 if (fp->f_ops == &badfileops)
1106 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1107 mtxp = mtx_pool_find(mtxpool_sleep, fp);
1110 * Hint to the dofilewrite() to not force the buffer draining
1111 * on the writer to the file. Most likely, the write would
1112 * not need normal buffers.
1115 fp->f_vnread_flags |= FDEVFS_VNODE;
1121 devfs_pathconf(struct vop_pathconf_args *ap)
1124 switch (ap->a_name) {
1125 case _PC_MAC_PRESENT:
1128 * If MAC is enabled, devfs automatically supports
1129 * trivial non-persistant label storage.
1137 return (vop_stdpathconf(ap));
1144 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1152 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1154 return (poll_no_poll(events));
1155 error = dsw->d_poll(dev, events, td);
1157 dev_relthread(dev, ref);
1162 * Print out the contents of a special device vnode.
1165 devfs_print(struct vop_print_args *ap)
1168 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1173 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1174 int flags, struct thread *td)
1177 int ioflag, error, ref;
1182 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1185 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1188 resid = uio->uio_resid;
1189 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1190 if (ioflag & O_DIRECT)
1191 ioflag |= IO_DIRECT;
1193 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1194 error = dsw->d_read(dev, uio, ioflag);
1195 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1196 vfs_timestamp(&dev->si_atime);
1198 dev_relthread(dev, ref);
1200 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1205 devfs_readdir(struct vop_readdir_args *ap)
1210 struct devfs_dirent *dd;
1211 struct devfs_dirent *de;
1212 struct devfs_mount *dmp;
1214 int *tmp_ncookies = NULL;
1216 if (ap->a_vp->v_type != VDIR)
1220 if (uio->uio_offset < 0)
1224 * XXX: This is a temporary hack to get around this filesystem not
1225 * supporting cookies. We store the location of the ncookies pointer
1226 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1227 * and set the number of cookies to 0. We then set the pointer to
1228 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1229 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1230 * pointer to its original location before returning to the caller.
1232 if (ap->a_ncookies != NULL) {
1233 tmp_ncookies = ap->a_ncookies;
1234 *ap->a_ncookies = 0;
1235 ap->a_ncookies = NULL;
1238 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1239 if (devfs_populate_vp(ap->a_vp) != 0) {
1240 if (tmp_ncookies != NULL)
1241 ap->a_ncookies = tmp_ncookies;
1245 de = ap->a_vp->v_data;
1247 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1248 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1249 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1251 if (devfs_prison_check(dd, uio->uio_td))
1253 if (dd->de_dirent->d_type == DT_DIR)
1258 if (dp->d_reclen > uio->uio_resid)
1260 dp->d_fileno = de->de_inode;
1261 if (off >= uio->uio_offset) {
1262 error = vfs_read_dirent(ap, dp, off);
1266 off += dp->d_reclen;
1268 sx_xunlock(&dmp->dm_lock);
1269 uio->uio_offset = off;
1272 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1275 if (tmp_ncookies != NULL)
1276 ap->a_ncookies = tmp_ncookies;
1282 devfs_readlink(struct vop_readlink_args *ap)
1284 struct devfs_dirent *de;
1286 de = ap->a_vp->v_data;
1287 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1291 devfs_reclaim(struct vop_reclaim_args *ap)
1293 struct vnode *vp = ap->a_vp;
1294 struct devfs_dirent *de;
1297 mtx_lock(&devfs_de_interlock);
1300 de->de_vnode = NULL;
1303 mtx_unlock(&devfs_de_interlock);
1305 vnode_destroy_vobject(vp);
1318 dev->si_usecount -= vp->v_usecount;
1326 devfs_remove(struct vop_remove_args *ap)
1328 struct vnode *dvp = ap->a_dvp;
1329 struct vnode *vp = ap->a_vp;
1330 struct devfs_dirent *dd;
1331 struct devfs_dirent *de, *de_covered;
1332 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1334 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1335 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1337 sx_xlock(&dmp->dm_lock);
1338 dd = ap->a_dvp->v_data;
1340 if (de->de_cdp == NULL) {
1341 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1342 if (de->de_dirent->d_type == DT_LNK) {
1343 de_covered = devfs_find(dd, de->de_dirent->d_name,
1344 de->de_dirent->d_namlen, 0);
1345 if (de_covered != NULL)
1346 de_covered->de_flags &= ~DE_COVERED;
1348 /* We need to unlock dvp because devfs_delete() may lock it. */
1352 devfs_delete(dmp, de, 0);
1353 sx_xunlock(&dmp->dm_lock);
1355 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1358 de->de_flags |= DE_WHITEOUT;
1359 sx_xunlock(&dmp->dm_lock);
1365 * Revoke is called on a tty when a terminal session ends. The vnode
1366 * is orphaned by setting v_op to deadfs so we need to let go of it
1367 * as well so that we create a new one next time around.
1371 devfs_revoke(struct vop_revoke_args *ap)
1373 struct vnode *vp = ap->a_vp, *vp2;
1375 struct cdev_priv *cdp;
1376 struct devfs_dirent *de;
1379 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1382 cdp = cdev2priv(dev);
1395 mtx_lock(&devfs_de_interlock);
1398 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1399 de = cdp->cdp_dirents[i];
1407 mtx_unlock(&devfs_de_interlock);
1408 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1422 mtx_unlock(&devfs_de_interlock);
1427 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1428 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1430 dev_rel(&cdp->cdp_c);
1434 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1439 devfs_rioctl(struct vop_ioctl_args *ap)
1442 struct devfs_mount *dmp;
1446 vn_lock(vp, LK_SHARED | LK_RETRY);
1447 if (vp->v_iflag & VI_DOOMED) {
1451 dmp = VFSTODEVFS(vp->v_mount);
1452 sx_xlock(&dmp->dm_lock);
1454 DEVFS_DMP_HOLD(dmp);
1455 devfs_populate(dmp);
1456 if (DEVFS_DMP_DROP(dmp)) {
1457 sx_xunlock(&dmp->dm_lock);
1458 devfs_unmount_final(dmp);
1461 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1462 sx_xunlock(&dmp->dm_lock);
1467 devfs_rread(struct vop_read_args *ap)
1470 if (ap->a_vp->v_type != VDIR)
1472 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1476 devfs_setattr(struct vop_setattr_args *ap)
1478 struct devfs_dirent *de;
1489 if ((vap->va_type != VNON) ||
1490 (vap->va_nlink != VNOVAL) ||
1491 (vap->va_fsid != VNOVAL) ||
1492 (vap->va_fileid != VNOVAL) ||
1493 (vap->va_blocksize != VNOVAL) ||
1494 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1495 (vap->va_rdev != VNOVAL) ||
1496 ((int)vap->va_bytes != VNOVAL) ||
1497 (vap->va_gen != VNOVAL)) {
1502 if (vp->v_type == VDIR)
1506 if (vap->va_uid == (uid_t)VNOVAL)
1510 if (vap->va_gid == (gid_t)VNOVAL)
1514 if (uid != de->de_uid || gid != de->de_gid) {
1515 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1516 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1517 error = priv_check(td, PRIV_VFS_CHOWN);
1526 if (vap->va_mode != (mode_t)VNOVAL) {
1527 if (ap->a_cred->cr_uid != de->de_uid) {
1528 error = priv_check(td, PRIV_VFS_ADMIN);
1532 de->de_mode = vap->va_mode;
1536 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1537 /* See the comment in ufs_vnops::ufs_setattr(). */
1538 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1539 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1540 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1542 if (vap->va_atime.tv_sec != VNOVAL) {
1543 if (vp->v_type == VCHR)
1544 vp->v_rdev->si_atime = vap->va_atime;
1546 de->de_atime = vap->va_atime;
1548 if (vap->va_mtime.tv_sec != VNOVAL) {
1549 if (vp->v_type == VCHR)
1550 vp->v_rdev->si_mtime = vap->va_mtime;
1552 de->de_mtime = vap->va_mtime;
1558 if (vp->v_type == VCHR)
1559 vfs_timestamp(&vp->v_rdev->si_ctime);
1561 vfs_timestamp(&de->de_mtime);
1568 devfs_setlabel(struct vop_setlabel_args *ap)
1571 struct devfs_dirent *de;
1576 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1577 mac_devfs_update(vp->v_mount, de, vp);
1584 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1587 return (vnops.fo_stat(fp, sb, cred, td));
1591 devfs_symlink(struct vop_symlink_args *ap)
1594 struct devfs_dirent *dd;
1595 struct devfs_dirent *de, *de_covered, *de_dotdot;
1596 struct devfs_mount *dmp;
1598 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1601 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1602 if (devfs_populate_vp(ap->a_dvp) != 0)
1605 dd = ap->a_dvp->v_data;
1606 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1607 de->de_flags = DE_USER;
1611 de->de_inode = alloc_unr(devfs_inos);
1613 de->de_dirent->d_type = DT_LNK;
1614 i = strlen(ap->a_target) + 1;
1615 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1616 bcopy(ap->a_target, de->de_symlink, i);
1618 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1620 de_covered = devfs_find(dd, de->de_dirent->d_name,
1621 de->de_dirent->d_namlen, 0);
1622 if (de_covered != NULL) {
1623 if ((de_covered->de_flags & DE_USER) != 0) {
1624 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1625 sx_xunlock(&dmp->dm_lock);
1628 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1629 ("devfs_symlink: entry %p already covered", de_covered));
1630 de_covered->de_flags |= DE_COVERED;
1633 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1634 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1635 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1636 devfs_dir_ref_de(dmp, dd);
1637 devfs_rules_apply(dmp, de);
1639 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1643 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1646 return (vnops.fo_truncate(fp, length, cred, td));
1650 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1651 int flags, struct thread *td)
1654 int error, ioflag, ref;
1659 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1662 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1665 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1666 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1667 if (ioflag & O_DIRECT)
1668 ioflag |= IO_DIRECT;
1669 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1671 resid = uio->uio_resid;
1673 error = dsw->d_write(dev, uio, ioflag);
1674 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1675 vfs_timestamp(&dev->si_ctime);
1676 dev->si_mtime = dev->si_ctime;
1679 dev_relthread(dev, ref);
1681 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1686 dev2udev(struct cdev *x)
1690 return (cdev2priv(x)->cdp_inode);
1693 static struct fileops devfs_ops_f = {
1694 .fo_read = devfs_read_f,
1695 .fo_write = devfs_write_f,
1696 .fo_truncate = devfs_truncate_f,
1697 .fo_ioctl = devfs_ioctl_f,
1698 .fo_poll = devfs_poll_f,
1699 .fo_kqfilter = devfs_kqfilter_f,
1700 .fo_stat = devfs_stat_f,
1701 .fo_close = devfs_close_f,
1702 .fo_chmod = vn_chmod,
1703 .fo_chown = vn_chown,
1704 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1707 static struct vop_vector devfs_vnodeops = {
1708 .vop_default = &default_vnodeops,
1710 .vop_access = devfs_access,
1711 .vop_getattr = devfs_getattr,
1712 .vop_ioctl = devfs_rioctl,
1713 .vop_lookup = devfs_lookup,
1714 .vop_mknod = devfs_mknod,
1715 .vop_pathconf = devfs_pathconf,
1716 .vop_read = devfs_rread,
1717 .vop_readdir = devfs_readdir,
1718 .vop_readlink = devfs_readlink,
1719 .vop_reclaim = devfs_reclaim,
1720 .vop_remove = devfs_remove,
1721 .vop_revoke = devfs_revoke,
1722 .vop_setattr = devfs_setattr,
1724 .vop_setlabel = devfs_setlabel,
1726 .vop_symlink = devfs_symlink,
1727 .vop_vptocnp = devfs_vptocnp,
1730 static struct vop_vector devfs_specops = {
1731 .vop_default = &default_vnodeops,
1733 .vop_access = devfs_access,
1734 .vop_bmap = VOP_PANIC,
1735 .vop_close = devfs_close,
1736 .vop_create = VOP_PANIC,
1737 .vop_fsync = devfs_fsync,
1738 .vop_getattr = devfs_getattr,
1739 .vop_link = VOP_PANIC,
1740 .vop_mkdir = VOP_PANIC,
1741 .vop_mknod = VOP_PANIC,
1742 .vop_open = devfs_open,
1743 .vop_pathconf = devfs_pathconf,
1744 .vop_print = devfs_print,
1745 .vop_read = VOP_PANIC,
1746 .vop_readdir = VOP_PANIC,
1747 .vop_readlink = VOP_PANIC,
1748 .vop_reallocblks = VOP_PANIC,
1749 .vop_reclaim = devfs_reclaim,
1750 .vop_remove = devfs_remove,
1751 .vop_rename = VOP_PANIC,
1752 .vop_revoke = devfs_revoke,
1753 .vop_rmdir = VOP_PANIC,
1754 .vop_setattr = devfs_setattr,
1756 .vop_setlabel = devfs_setlabel,
1758 .vop_strategy = VOP_PANIC,
1759 .vop_symlink = VOP_PANIC,
1760 .vop_vptocnp = devfs_vptocnp,
1761 .vop_write = VOP_PANIC,
1765 * Our calling convention to the device drivers used to be that we passed
1766 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1767 * flags instead since that's what open(), close() and ioctl() takes and
1768 * we don't really want vnode.h in device drivers.
1769 * We solved the source compatibility by redefining some vnode flags to
1770 * be the same as the fcntl ones and by sending down the bitwise OR of
1771 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1772 * pulls the rug out under this.
1774 CTASSERT(O_NONBLOCK == IO_NDELAY);
1775 CTASSERT(O_FSYNC == IO_SYNC);