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>
61 #include <sys/ttycom.h>
62 #include <sys/unistd.h>
63 #include <sys/vnode.h>
65 static struct vop_vector devfs_vnodeops;
66 static struct vop_vector devfs_specops;
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);
84 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
88 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
89 if (*devp != fp->f_data) {
91 dev_relthread(*devp, *ref);
94 KASSERT((*devp)->si_refcount > 0,
95 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
98 curthread->td_fpop = fp;
103 devfs_get_cdevpriv(void **datap)
106 struct cdev_privdata *p;
109 fp = curthread->td_fpop;
115 *datap = p->cdpd_data;
122 devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t priv_dtr)
125 struct cdev_priv *cdp;
126 struct cdev_privdata *p;
129 fp = curthread->td_fpop;
132 cdp = cdev2priv((struct cdev *)fp->f_data);
133 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
135 p->cdpd_dtr = priv_dtr;
137 mtx_lock(&cdevpriv_mtx);
138 if (fp->f_cdevpriv == NULL) {
139 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
141 mtx_unlock(&cdevpriv_mtx);
144 mtx_unlock(&cdevpriv_mtx);
145 free(p, M_CDEVPDATA);
152 devfs_destroy_cdevpriv(struct cdev_privdata *p)
155 mtx_assert(&cdevpriv_mtx, MA_OWNED);
156 p->cdpd_fp->f_cdevpriv = NULL;
157 LIST_REMOVE(p, cdpd_list);
158 mtx_unlock(&cdevpriv_mtx);
159 (p->cdpd_dtr)(p->cdpd_data);
160 free(p, M_CDEVPDATA);
164 devfs_fpdrop(struct file *fp)
166 struct cdev_privdata *p;
168 mtx_lock(&cdevpriv_mtx);
169 if ((p = fp->f_cdevpriv) == NULL) {
170 mtx_unlock(&cdevpriv_mtx);
173 devfs_destroy_cdevpriv(p);
177 devfs_clear_cdevpriv(void)
181 fp = curthread->td_fpop;
188 * On success devfs_populate_vp() returns with dmp->dm_lock held.
191 devfs_populate_vp(struct vnode *vp)
193 struct devfs_dirent *de;
194 struct devfs_mount *dmp;
197 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
199 dmp = VFSTODEVFS(vp->v_mount);
200 locked = VOP_ISLOCKED(vp);
202 sx_xlock(&dmp->dm_lock);
205 /* Can't call devfs_populate() with the vnode lock held. */
209 sx_xunlock(&dmp->dm_lock);
210 vn_lock(vp, locked | LK_RETRY);
211 sx_xlock(&dmp->dm_lock);
212 if (DEVFS_DMP_DROP(dmp)) {
213 sx_xunlock(&dmp->dm_lock);
214 devfs_unmount_final(dmp);
217 if ((vp->v_iflag & VI_DOOMED) != 0) {
218 sx_xunlock(&dmp->dm_lock);
223 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
224 if ((de->de_flags & DE_DOOMED) != 0) {
225 sx_xunlock(&dmp->dm_lock);
233 devfs_vptocnp(struct vop_vptocnp_args *ap)
235 struct vnode *vp = ap->a_vp;
236 struct vnode **dvp = ap->a_vpp;
237 struct devfs_mount *dmp;
238 char *buf = ap->a_buf;
239 int *buflen = ap->a_buflen;
240 struct devfs_dirent *dd, *de;
243 dmp = VFSTODEVFS(vp->v_mount);
245 error = devfs_populate_vp(vp);
252 if (vp->v_type == VCHR) {
253 i -= strlen(dd->de_cdp->cdp_c.si_name);
258 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
259 strlen(dd->de_cdp->cdp_c.si_name));
261 } else if (vp->v_type == VDIR) {
262 if (dd == dmp->dm_rootdir) {
267 i -= dd->de_dirent->d_namlen;
272 bcopy(dd->de_dirent->d_name, buf + i,
273 dd->de_dirent->d_namlen);
280 de = devfs_parent_dirent(de);
285 mtx_lock(&devfs_de_interlock);
289 mtx_unlock(&devfs_de_interlock);
293 mtx_unlock(&devfs_de_interlock);
297 sx_xunlock(&dmp->dm_lock);
302 * Construct the fully qualified path name relative to the mountpoint.
303 * If a NULL cnp is provided, no '/' is appended to the resulting path.
306 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
307 struct componentname *cnp)
310 struct devfs_dirent *de;
312 sx_assert(&dmp->dm_lock, SA_LOCKED);
317 i -= cnp->cn_namelen;
321 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
323 while (de != dmp->dm_rootdir) {
324 if (cnp != NULL || i < SPECNAMELEN) {
330 i -= de->de_dirent->d_namlen;
333 bcopy(de->de_dirent->d_name, buf + i,
334 de->de_dirent->d_namlen);
335 de = devfs_parent_dirent(de);
343 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
344 struct devfs_dirent *de)
349 if (de->de_flags & DE_DOOMED)
351 if (DEVFS_DE_DROP(de)) {
352 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
353 devfs_dirent_free(de);
355 if (DEVFS_DMP_DROP(dmp)) {
356 KASSERT(not_found == 1,
357 ("DEVFS mount struct freed before dirent"));
359 sx_xunlock(&dmp->dm_lock);
360 devfs_unmount_final(dmp);
362 if (not_found == 1 || (drop_dm_lock && not_found != 2))
363 sx_unlock(&dmp->dm_lock);
368 devfs_insmntque_dtr(struct vnode *vp, void *arg)
370 struct devfs_dirent *de;
372 de = (struct devfs_dirent *)arg;
373 mtx_lock(&devfs_de_interlock);
376 mtx_unlock(&devfs_de_interlock);
382 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
386 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
392 struct devfs_mount *dmp;
395 dmp = VFSTODEVFS(mp);
396 if (de->de_flags & DE_DOOMED) {
397 sx_xunlock(&dmp->dm_lock);
402 mtx_lock(&devfs_de_interlock);
406 mtx_unlock(&devfs_de_interlock);
407 sx_xunlock(&dmp->dm_lock);
408 error = vget(vp, lockmode | LK_INTERLOCK, curthread);
409 sx_xlock(&dmp->dm_lock);
410 if (devfs_allocv_drop_refs(0, dmp, de)) {
416 sx_xunlock(&dmp->dm_lock);
419 sx_xunlock(&dmp->dm_lock);
423 mtx_unlock(&devfs_de_interlock);
424 if (de->de_dirent->d_type == DT_CHR) {
425 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
426 devfs_allocv_drop_refs(1, dmp, de);
429 dev = &de->de_cdp->cdp_c;
433 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
435 devfs_allocv_drop_refs(1, dmp, de);
436 printf("devfs_allocv: failed to allocate new vnode\n");
440 if (de->de_dirent->d_type == DT_CHR) {
445 /* XXX: v_rdev should be protect by vnode lock */
447 KASSERT(vp->v_usecount == 1,
448 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
449 dev->si_usecount += vp->v_usecount;
450 /* Special casing of ttys for deadfs. Probably redundant. */
452 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
453 vp->v_vflag |= VV_ISTTY;
456 if ((dev->si_flags & SI_ETERNAL) != 0)
457 vp->v_vflag |= VV_ETERNALDEV;
458 vp->v_op = &devfs_specops;
459 } else if (de->de_dirent->d_type == DT_DIR) {
461 } else if (de->de_dirent->d_type == DT_LNK) {
466 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
468 mtx_lock(&devfs_de_interlock);
471 mtx_unlock(&devfs_de_interlock);
472 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
474 (void) devfs_allocv_drop_refs(1, dmp, de);
477 if (devfs_allocv_drop_refs(0, dmp, de)) {
482 mac_devfs_vnode_associate(mp, de, vp);
484 sx_xunlock(&dmp->dm_lock);
490 devfs_access(struct vop_access_args *ap)
492 struct vnode *vp = ap->a_vp;
493 struct devfs_dirent *de;
497 if (vp->v_type == VDIR)
500 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
501 ap->a_accmode, ap->a_cred, NULL);
506 /* We do, however, allow access to the controlling terminal */
507 if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
509 if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
516 devfs_close(struct vop_close_args *ap)
518 struct vnode *vp = ap->a_vp, *oldvp;
519 struct thread *td = ap->a_td;
520 struct cdev *dev = vp->v_rdev;
522 int vp_locked, error, ref;
525 * XXX: Don't call d_close() if we were called because of
526 * XXX: insmntque1() failure.
528 if (vp->v_data == NULL)
532 * Hack: a tty device that is a controlling terminal
533 * has a reference from the session structure.
534 * We cannot easily tell that a character device is
535 * a controlling terminal, unless it is the closing
536 * process' controlling terminal. In that case,
537 * if the reference count is 2 (this last descriptor
538 * plus the session), release the reference from the session.
541 sx_xlock(&proctree_lock);
542 if (td && vp == td->td_proc->p_session->s_ttyvp) {
543 SESS_LOCK(td->td_proc->p_session);
545 if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
546 td->td_proc->p_session->s_ttyvp = NULL;
547 td->td_proc->p_session->s_ttydp = NULL;
551 SESS_UNLOCK(td->td_proc->p_session);
553 sx_xunlock(&proctree_lock);
557 * We do not want to really close the device if it
558 * is still in use unless we are trying to close it
559 * forcibly. Since every use (buffer, vnode, swap, cmap)
560 * holds a reference to the vnode, and because we mark
561 * any other vnodes that alias this device, when the
562 * sum of the reference counts on all the aliased
563 * vnodes descends to one, we are on last close.
565 dsw = dev_refthread(dev, &ref);
569 if (vp->v_iflag & VI_DOOMED) {
571 } else if (dsw->d_flags & D_TRACKCLOSE) {
572 /* Keep device updated on status. */
573 } else if (count_dev(dev) > 1) {
575 dev_relthread(dev, ref);
580 vp_locked = VOP_ISLOCKED(vp);
582 KASSERT(dev->si_refcount > 0,
583 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
584 error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
585 dev_relthread(dev, ref);
586 vn_lock(vp, vp_locked | LK_RETRY);
592 devfs_close_f(struct file *fp, struct thread *td)
599 error = vnops.fo_close(fp, td);
605 devfs_fsync(struct vop_fsync_args *ap)
609 struct devfs_dirent *de;
611 if (!vn_isdisk(ap->a_vp, &error)) {
612 bo = &ap->a_vp->v_bufobj;
613 de = ap->a_vp->v_data;
614 if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
615 printf("Device %s went missing before all of the data "
616 "could be written to it; expect data loss.\n",
617 de->de_dirent->d_name);
619 error = vop_stdfsync(ap);
620 if (bo->bo_dirty.bv_cnt != 0 || error != 0)
621 panic("devfs_fsync: vop_stdfsync failed.");
627 return (vop_stdfsync(ap));
631 devfs_getattr(struct vop_getattr_args *ap)
633 struct vnode *vp = ap->a_vp;
634 struct vattr *vap = ap->a_vap;
636 struct devfs_dirent *de;
637 struct devfs_mount *dmp;
640 error = devfs_populate_vp(vp);
644 dmp = VFSTODEVFS(vp->v_mount);
645 sx_xunlock(&dmp->dm_lock);
648 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
649 if (vp->v_type == VDIR) {
652 ("Null dir dirent in devfs_getattr vp=%p", vp));
654 vap->va_uid = de->de_uid;
655 vap->va_gid = de->de_gid;
656 vap->va_mode = de->de_mode;
657 if (vp->v_type == VLNK)
658 vap->va_size = strlen(de->de_symlink);
659 else if (vp->v_type == VDIR)
660 vap->va_size = vap->va_bytes = DEV_BSIZE;
663 if (vp->v_type != VDIR)
665 vap->va_blocksize = DEV_BSIZE;
666 vap->va_type = vp->v_type;
670 if ((aa).tv_sec <= 3600) { \
671 (aa).tv_sec = boottime.tv_sec; \
672 (aa).tv_nsec = boottime.tv_usec * 1000; \
676 if (vp->v_type != VCHR) {
678 vap->va_atime = de->de_atime;
680 vap->va_mtime = de->de_mtime;
682 vap->va_ctime = de->de_ctime;
686 vap->va_atime = dev->si_atime;
688 vap->va_mtime = dev->si_mtime;
690 vap->va_ctime = dev->si_ctime;
692 vap->va_rdev = cdev2priv(dev)->cdp_inode;
697 vap->va_nlink = de->de_links;
698 vap->va_fileid = de->de_inode;
705 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
713 struct fiodgname_arg *fgn;
717 error = devfs_fp_check(fp, &dev, &dsw, &ref);
721 if (com == FIODTYPE) {
722 *(int *)data = dsw->d_flags & D_TYPEMASK;
724 dev_relthread(dev, ref);
726 } else if (com == FIODGNAME) {
733 error = copyout(p, fgn->buf, i);
735 dev_relthread(dev, ref);
738 error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
740 dev_relthread(dev, ref);
741 if (error == ENOIOCTL)
743 if (error == 0 && com == TIOCSCTTY) {
746 /* Do nothing if reassigning same control tty */
747 sx_slock(&proctree_lock);
748 if (td->td_proc->p_session->s_ttyvp == vp) {
749 sx_sunlock(&proctree_lock);
753 vpold = td->td_proc->p_session->s_ttyvp;
755 SESS_LOCK(td->td_proc->p_session);
756 td->td_proc->p_session->s_ttyvp = vp;
757 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
758 SESS_UNLOCK(td->td_proc->p_session);
760 sx_sunlock(&proctree_lock);
762 /* Get rid of reference to old control tty */
771 devfs_kqfilter_f(struct file *fp, struct knote *kn)
781 error = devfs_fp_check(fp, &dev, &dsw, &ref);
784 error = dsw->d_kqfilter(dev, kn);
786 dev_relthread(dev, ref);
791 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
793 struct cdev_priv *cdp;
800 dcr = cdp->cdp_c.si_cred;
804 error = prison_check(td->td_ucred, dcr);
807 /* We do, however, allow access to the controlling terminal */
808 if (!(td->td_proc->p_flag & P_CONTROLT))
810 if (td->td_proc->p_session->s_ttydp == cdp)
816 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
818 struct componentname *cnp;
819 struct vnode *dvp, **vpp;
821 struct devfs_dirent *de, *dd;
822 struct devfs_dirent **dde;
823 struct devfs_mount *dmp;
825 int error, flags, nameiop, dvplocked;
826 char specname[SPECNAMELEN + 1], *pname;
831 pname = cnp->cn_nameptr;
833 flags = cnp->cn_flags;
834 nameiop = cnp->cn_nameiop;
835 dmp = VFSTODEVFS(dvp->v_mount);
839 if ((flags & ISLASTCN) && nameiop == RENAME)
842 if (dvp->v_type != VDIR)
845 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
848 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
852 if (cnp->cn_namelen == 1 && *pname == '.') {
853 if ((flags & ISLASTCN) && nameiop != LOOKUP)
860 if (flags & ISDOTDOT) {
861 if ((flags & ISLASTCN) && nameiop != LOOKUP)
863 de = devfs_parent_dirent(dd);
866 dvplocked = VOP_ISLOCKED(dvp);
868 error = devfs_allocv(de, dvp->v_mount,
869 cnp->cn_lkflags & LK_TYPE_MASK, vpp);
871 vn_lock(dvp, dvplocked | LK_RETRY);
876 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
877 while (de == NULL) { /* While(...) so we can use break */
879 if (nameiop == DELETE)
883 * OK, we didn't have an entry for the name we were asked for
884 * so we try to see if anybody can create it on demand.
886 pname = devfs_fqpn(specname, dmp, dd, cnp);
892 sx_xunlock(&dmp->dm_lock);
893 sx_slock(&clone_drain_lock);
894 EVENTHANDLER_INVOKE(dev_clone,
895 td->td_ucred, pname, strlen(pname), &cdev);
896 sx_sunlock(&clone_drain_lock);
899 sx_xlock(&dmp->dm_lock);
900 else if (devfs_populate_vp(dvp) != 0) {
902 sx_xlock(&dmp->dm_lock);
903 if (DEVFS_DMP_DROP(dmp)) {
904 sx_xunlock(&dmp->dm_lock);
905 devfs_unmount_final(dmp);
907 sx_xunlock(&dmp->dm_lock);
911 if (DEVFS_DMP_DROP(dmp)) {
913 sx_xunlock(&dmp->dm_lock);
914 devfs_unmount_final(dmp);
924 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
925 if (dde != NULL && *dde != NULL)
932 if (de == NULL || de->de_flags & DE_WHITEOUT) {
933 if ((nameiop == CREATE || nameiop == RENAME) &&
934 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
935 cnp->cn_flags |= SAVENAME;
936 return (EJUSTRETURN);
941 if (devfs_prison_check(de, td))
944 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
945 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
954 error = devfs_allocv(de, dvp->v_mount, cnp->cn_lkflags & LK_TYPE_MASK,
961 devfs_lookup(struct vop_lookup_args *ap)
964 struct devfs_mount *dmp;
967 if (devfs_populate_vp(ap->a_dvp) != 0)
970 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
972 j = devfs_lookupx(ap, &dm_unlock);
974 sx_xunlock(&dmp->dm_lock);
979 devfs_mknod(struct vop_mknod_args *ap)
981 struct componentname *cnp;
982 struct vnode *dvp, **vpp;
983 struct devfs_dirent *dd, *de;
984 struct devfs_mount *dmp;
988 * The only type of node we should be creating here is a
989 * character device, for anything else return EOPNOTSUPP.
991 if (ap->a_vap->va_type != VCHR)
994 dmp = VFSTODEVFS(dvp->v_mount);
1001 sx_xlock(&dmp->dm_lock);
1002 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1003 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1005 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1006 de->de_dirent->d_namlen) != 0)
1008 if (de->de_flags & DE_WHITEOUT)
1014 de->de_flags &= ~DE_WHITEOUT;
1015 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1018 sx_xunlock(&dmp->dm_lock);
1024 devfs_open(struct vop_open_args *ap)
1026 struct thread *td = ap->a_td;
1027 struct vnode *vp = ap->a_vp;
1028 struct cdev *dev = vp->v_rdev;
1029 struct file *fp = ap->a_fp;
1030 int error, ref, vlocked;
1034 if (vp->v_type == VBLK)
1040 /* Make this field valid before any I/O in d_open. */
1041 if (dev->si_iosize_max == 0)
1042 dev->si_iosize_max = DFLTPHYS;
1044 dsw = dev_refthread(dev, &ref);
1048 vlocked = VOP_ISLOCKED(vp);
1057 if (dsw->d_fdopen != NULL)
1058 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1060 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1063 vn_lock(vp, vlocked | LK_RETRY);
1064 dev_relthread(dev, ref);
1068 #if 0 /* /dev/console */
1069 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1074 if (fp->f_ops == &badfileops)
1075 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1080 devfs_pathconf(struct vop_pathconf_args *ap)
1083 switch (ap->a_name) {
1084 case _PC_MAC_PRESENT:
1087 * If MAC is enabled, devfs automatically supports
1088 * trivial non-persistant label storage.
1096 return (vop_stdpathconf(ap));
1103 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1111 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1113 return (poll_no_poll(events));
1114 error = dsw->d_poll(dev, events, td);
1116 dev_relthread(dev, ref);
1121 * Print out the contents of a special device vnode.
1124 devfs_print(struct vop_print_args *ap)
1127 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1133 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
1136 int ioflag, error, ref, resid;
1141 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1144 resid = uio->uio_resid;
1145 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1146 if (ioflag & O_DIRECT)
1147 ioflag |= IO_DIRECT;
1149 if ((flags & FOF_OFFSET) == 0)
1150 uio->uio_offset = fp->f_offset;
1152 error = dsw->d_read(dev, uio, ioflag);
1153 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1154 vfs_timestamp(&dev->si_atime);
1156 dev_relthread(dev, ref);
1158 if ((flags & FOF_OFFSET) == 0)
1159 fp->f_offset = uio->uio_offset;
1160 fp->f_nextoff = uio->uio_offset;
1165 devfs_readdir(struct vop_readdir_args *ap)
1170 struct devfs_dirent *dd;
1171 struct devfs_dirent *de;
1172 struct devfs_mount *dmp;
1174 int *tmp_ncookies = NULL;
1176 if (ap->a_vp->v_type != VDIR)
1180 if (uio->uio_offset < 0)
1184 * XXX: This is a temporary hack to get around this filesystem not
1185 * supporting cookies. We store the location of the ncookies pointer
1186 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1187 * and set the number of cookies to 0. We then set the pointer to
1188 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1189 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1190 * pointer to its original location before returning to the caller.
1192 if (ap->a_ncookies != NULL) {
1193 tmp_ncookies = ap->a_ncookies;
1194 *ap->a_ncookies = 0;
1195 ap->a_ncookies = NULL;
1198 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1199 if (devfs_populate_vp(ap->a_vp) != 0) {
1200 if (tmp_ncookies != NULL)
1201 ap->a_ncookies = tmp_ncookies;
1205 de = ap->a_vp->v_data;
1207 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1208 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1209 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1211 if (devfs_prison_check(dd, uio->uio_td))
1213 if (dd->de_dirent->d_type == DT_DIR)
1218 if (dp->d_reclen > uio->uio_resid)
1220 dp->d_fileno = de->de_inode;
1221 if (off >= uio->uio_offset) {
1222 error = vfs_read_dirent(ap, dp, off);
1226 off += dp->d_reclen;
1228 sx_xunlock(&dmp->dm_lock);
1229 uio->uio_offset = off;
1232 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1235 if (tmp_ncookies != NULL)
1236 ap->a_ncookies = tmp_ncookies;
1242 devfs_readlink(struct vop_readlink_args *ap)
1244 struct devfs_dirent *de;
1246 de = ap->a_vp->v_data;
1247 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1251 devfs_reclaim(struct vop_reclaim_args *ap)
1253 struct vnode *vp = ap->a_vp;
1254 struct devfs_dirent *de;
1257 mtx_lock(&devfs_de_interlock);
1260 de->de_vnode = NULL;
1263 mtx_unlock(&devfs_de_interlock);
1265 vnode_destroy_vobject(vp);
1278 dev->si_usecount -= vp->v_usecount;
1286 devfs_remove(struct vop_remove_args *ap)
1288 struct vnode *dvp = ap->a_dvp;
1289 struct vnode *vp = ap->a_vp;
1290 struct devfs_dirent *dd;
1291 struct devfs_dirent *de, *de_covered;
1292 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1294 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1295 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1297 sx_xlock(&dmp->dm_lock);
1298 dd = ap->a_dvp->v_data;
1300 if (de->de_cdp == NULL) {
1301 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1302 if (de->de_dirent->d_type == DT_LNK) {
1303 de_covered = devfs_find(dd, de->de_dirent->d_name,
1304 de->de_dirent->d_namlen, 0);
1305 if (de_covered != NULL)
1306 de_covered->de_flags &= ~DE_COVERED;
1308 /* We need to unlock dvp because devfs_delete() may lock it. */
1312 devfs_delete(dmp, de, 0);
1313 sx_xunlock(&dmp->dm_lock);
1315 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1316 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1318 de->de_flags |= DE_WHITEOUT;
1319 sx_xunlock(&dmp->dm_lock);
1325 * Revoke is called on a tty when a terminal session ends. The vnode
1326 * is orphaned by setting v_op to deadfs so we need to let go of it
1327 * as well so that we create a new one next time around.
1331 devfs_revoke(struct vop_revoke_args *ap)
1333 struct vnode *vp = ap->a_vp, *vp2;
1335 struct cdev_priv *cdp;
1336 struct devfs_dirent *de;
1339 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1342 cdp = cdev2priv(dev);
1355 mtx_lock(&devfs_de_interlock);
1358 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1359 de = cdp->cdp_dirents[i];
1367 mtx_unlock(&devfs_de_interlock);
1368 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1382 mtx_unlock(&devfs_de_interlock);
1387 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1388 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1390 dev_rel(&cdp->cdp_c);
1394 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1399 devfs_rioctl(struct vop_ioctl_args *ap)
1402 struct devfs_mount *dmp;
1406 vn_lock(vp, LK_SHARED | LK_RETRY);
1407 if (vp->v_iflag & VI_DOOMED) {
1411 dmp = VFSTODEVFS(vp->v_mount);
1412 sx_xlock(&dmp->dm_lock);
1414 DEVFS_DMP_HOLD(dmp);
1415 devfs_populate(dmp);
1416 if (DEVFS_DMP_DROP(dmp)) {
1417 sx_xunlock(&dmp->dm_lock);
1418 devfs_unmount_final(dmp);
1421 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1422 sx_xunlock(&dmp->dm_lock);
1427 devfs_rread(struct vop_read_args *ap)
1430 if (ap->a_vp->v_type != VDIR)
1432 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1436 devfs_setattr(struct vop_setattr_args *ap)
1438 struct devfs_dirent *de;
1449 if ((vap->va_type != VNON) ||
1450 (vap->va_nlink != VNOVAL) ||
1451 (vap->va_fsid != VNOVAL) ||
1452 (vap->va_fileid != VNOVAL) ||
1453 (vap->va_blocksize != VNOVAL) ||
1454 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1455 (vap->va_rdev != VNOVAL) ||
1456 ((int)vap->va_bytes != VNOVAL) ||
1457 (vap->va_gen != VNOVAL)) {
1462 if (vp->v_type == VDIR)
1466 if (vap->va_uid == (uid_t)VNOVAL)
1470 if (vap->va_gid == (gid_t)VNOVAL)
1474 if (uid != de->de_uid || gid != de->de_gid) {
1475 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1476 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1477 error = priv_check(td, PRIV_VFS_CHOWN);
1486 if (vap->va_mode != (mode_t)VNOVAL) {
1487 if (ap->a_cred->cr_uid != de->de_uid) {
1488 error = priv_check(td, PRIV_VFS_ADMIN);
1492 de->de_mode = vap->va_mode;
1496 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1497 /* See the comment in ufs_vnops::ufs_setattr(). */
1498 if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
1499 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1500 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
1502 if (vap->va_atime.tv_sec != VNOVAL) {
1503 if (vp->v_type == VCHR)
1504 vp->v_rdev->si_atime = vap->va_atime;
1506 de->de_atime = vap->va_atime;
1508 if (vap->va_mtime.tv_sec != VNOVAL) {
1509 if (vp->v_type == VCHR)
1510 vp->v_rdev->si_mtime = vap->va_mtime;
1512 de->de_mtime = vap->va_mtime;
1518 if (vp->v_type == VCHR)
1519 vfs_timestamp(&vp->v_rdev->si_ctime);
1521 vfs_timestamp(&de->de_mtime);
1528 devfs_setlabel(struct vop_setlabel_args *ap)
1531 struct devfs_dirent *de;
1536 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1537 mac_devfs_update(vp->v_mount, de, vp);
1544 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1547 return (vnops.fo_stat(fp, sb, cred, td));
1551 devfs_symlink(struct vop_symlink_args *ap)
1554 struct devfs_dirent *dd;
1555 struct devfs_dirent *de, *de_covered, *de_dotdot;
1556 struct devfs_mount *dmp;
1558 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1561 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1562 if (devfs_populate_vp(ap->a_dvp) != 0)
1565 dd = ap->a_dvp->v_data;
1566 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1567 de->de_flags = DE_USER;
1571 de->de_inode = alloc_unr(devfs_inos);
1573 de->de_dirent->d_type = DT_LNK;
1574 i = strlen(ap->a_target) + 1;
1575 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1576 bcopy(ap->a_target, de->de_symlink, i);
1578 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1580 de_covered = devfs_find(dd, de->de_dirent->d_name,
1581 de->de_dirent->d_namlen, 0);
1582 if (de_covered != NULL) {
1583 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1584 ("devfs_symlink: entry %p already covered", de_covered));
1585 de_covered->de_flags |= DE_COVERED;
1588 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1589 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1590 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1591 devfs_dir_ref_de(dmp, dd);
1593 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1597 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1600 return (vnops.fo_truncate(fp, length, cred, td));
1605 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
1608 int error, ioflag, ref, resid;
1613 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1616 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1617 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1618 if (ioflag & O_DIRECT)
1619 ioflag |= IO_DIRECT;
1620 if ((flags & FOF_OFFSET) == 0)
1621 uio->uio_offset = fp->f_offset;
1623 resid = uio->uio_resid;
1625 error = dsw->d_write(dev, uio, ioflag);
1626 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1627 vfs_timestamp(&dev->si_ctime);
1628 dev->si_mtime = dev->si_ctime;
1631 dev_relthread(dev, ref);
1633 if ((flags & FOF_OFFSET) == 0)
1634 fp->f_offset = uio->uio_offset;
1635 fp->f_nextoff = uio->uio_offset;
1640 dev2udev(struct cdev *x)
1644 return (cdev2priv(x)->cdp_inode);
1647 static struct fileops devfs_ops_f = {
1648 .fo_read = devfs_read_f,
1649 .fo_write = devfs_write_f,
1650 .fo_truncate = devfs_truncate_f,
1651 .fo_ioctl = devfs_ioctl_f,
1652 .fo_poll = devfs_poll_f,
1653 .fo_kqfilter = devfs_kqfilter_f,
1654 .fo_stat = devfs_stat_f,
1655 .fo_close = devfs_close_f,
1656 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1659 static struct vop_vector devfs_vnodeops = {
1660 .vop_default = &default_vnodeops,
1662 .vop_access = devfs_access,
1663 .vop_getattr = devfs_getattr,
1664 .vop_ioctl = devfs_rioctl,
1665 .vop_lookup = devfs_lookup,
1666 .vop_mknod = devfs_mknod,
1667 .vop_pathconf = devfs_pathconf,
1668 .vop_read = devfs_rread,
1669 .vop_readdir = devfs_readdir,
1670 .vop_readlink = devfs_readlink,
1671 .vop_reclaim = devfs_reclaim,
1672 .vop_remove = devfs_remove,
1673 .vop_revoke = devfs_revoke,
1674 .vop_setattr = devfs_setattr,
1676 .vop_setlabel = devfs_setlabel,
1678 .vop_symlink = devfs_symlink,
1679 .vop_vptocnp = devfs_vptocnp,
1682 static struct vop_vector devfs_specops = {
1683 .vop_default = &default_vnodeops,
1685 .vop_access = devfs_access,
1686 .vop_bmap = VOP_PANIC,
1687 .vop_close = devfs_close,
1688 .vop_create = VOP_PANIC,
1689 .vop_fsync = devfs_fsync,
1690 .vop_getattr = devfs_getattr,
1691 .vop_link = VOP_PANIC,
1692 .vop_mkdir = VOP_PANIC,
1693 .vop_mknod = VOP_PANIC,
1694 .vop_open = devfs_open,
1695 .vop_pathconf = devfs_pathconf,
1696 .vop_print = devfs_print,
1697 .vop_read = VOP_PANIC,
1698 .vop_readdir = VOP_PANIC,
1699 .vop_readlink = VOP_PANIC,
1700 .vop_reallocblks = VOP_PANIC,
1701 .vop_reclaim = devfs_reclaim,
1702 .vop_remove = devfs_remove,
1703 .vop_rename = VOP_PANIC,
1704 .vop_revoke = devfs_revoke,
1705 .vop_rmdir = VOP_PANIC,
1706 .vop_setattr = devfs_setattr,
1708 .vop_setlabel = devfs_setlabel,
1710 .vop_strategy = VOP_PANIC,
1711 .vop_symlink = VOP_PANIC,
1712 .vop_vptocnp = devfs_vptocnp,
1713 .vop_write = VOP_PANIC,
1717 * Our calling convention to the device drivers used to be that we passed
1718 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1719 * flags instead since that's what open(), close() and ioctl() takes and
1720 * we don't really want vnode.h in device drivers.
1721 * We solved the source compatibility by redefining some vnode flags to
1722 * be the same as the fcntl ones and by sending down the bitwise OR of
1723 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1724 * pulls the rug out under this.
1726 CTASSERT(O_NONBLOCK == IO_NDELAY);
1727 CTASSERT(O_FSYNC == IO_SYNC);