2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2004
5 * Poul-Henning Kamp. All rights reserved.
6 * Copyright (c) 1989, 1992-1993, 1995
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software donated to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
34 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/dirent.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
52 #include <sys/filio.h>
54 #include <sys/kernel.h>
55 #include <sys/limits.h>
57 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
65 #include <sys/sysctl.h>
67 #include <sys/ttycom.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
71 static struct vop_vector devfs_vnodeops;
72 static struct vop_vector devfs_specops;
73 static struct fileops devfs_ops_f;
75 #include <fs/devfs/devfs.h>
76 #include <fs/devfs/devfs_int.h>
78 #include <security/mac/mac_framework.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_object.h>
84 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
86 struct mtx devfs_de_interlock;
87 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
88 struct sx clone_drain_lock;
89 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
90 struct mtx cdevpriv_mtx;
91 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
93 SYSCTL_DECL(_vfs_devfs);
95 static int devfs_dotimes;
96 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
97 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
100 * Update devfs node timestamp. Note that updates are unlocked and
101 * stat(2) could see partially updated times.
104 devfs_timestamp(struct timespec *tsp)
112 if (tsp->tv_sec != ts) {
120 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
124 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
125 if (*devp != fp->f_data) {
127 dev_relthread(*devp, *ref);
130 KASSERT((*devp)->si_refcount > 0,
131 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
134 curthread->td_fpop = fp;
139 devfs_get_cdevpriv(void **datap)
142 struct cdev_privdata *p;
145 fp = curthread->td_fpop;
151 *datap = p->cdpd_data;
158 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
161 struct cdev_priv *cdp;
162 struct cdev_privdata *p;
165 fp = curthread->td_fpop;
168 cdp = cdev2priv((struct cdev *)fp->f_data);
169 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
171 p->cdpd_dtr = priv_dtr;
173 mtx_lock(&cdevpriv_mtx);
174 if (fp->f_cdevpriv == NULL) {
175 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
177 mtx_unlock(&cdevpriv_mtx);
180 mtx_unlock(&cdevpriv_mtx);
181 free(p, M_CDEVPDATA);
188 devfs_destroy_cdevpriv(struct cdev_privdata *p)
191 mtx_assert(&cdevpriv_mtx, MA_OWNED);
192 KASSERT(p->cdpd_fp->f_cdevpriv == p,
193 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
194 p->cdpd_fp->f_cdevpriv = NULL;
195 LIST_REMOVE(p, cdpd_list);
196 mtx_unlock(&cdevpriv_mtx);
197 (p->cdpd_dtr)(p->cdpd_data);
198 free(p, M_CDEVPDATA);
202 devfs_fpdrop(struct file *fp)
204 struct cdev_privdata *p;
206 mtx_lock(&cdevpriv_mtx);
207 if ((p = fp->f_cdevpriv) == NULL) {
208 mtx_unlock(&cdevpriv_mtx);
211 devfs_destroy_cdevpriv(p);
215 devfs_clear_cdevpriv(void)
219 fp = curthread->td_fpop;
226 * On success devfs_populate_vp() returns with dmp->dm_lock held.
229 devfs_populate_vp(struct vnode *vp)
231 struct devfs_dirent *de;
232 struct devfs_mount *dmp;
235 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
237 dmp = VFSTODEVFS(vp->v_mount);
238 locked = VOP_ISLOCKED(vp);
240 sx_xlock(&dmp->dm_lock);
243 /* Can't call devfs_populate() with the vnode lock held. */
247 sx_xunlock(&dmp->dm_lock);
248 vn_lock(vp, locked | LK_RETRY);
249 sx_xlock(&dmp->dm_lock);
250 if (DEVFS_DMP_DROP(dmp)) {
251 sx_xunlock(&dmp->dm_lock);
252 devfs_unmount_final(dmp);
255 if (VN_IS_DOOMED(vp)) {
256 sx_xunlock(&dmp->dm_lock);
261 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
262 if ((de->de_flags & DE_DOOMED) != 0) {
263 sx_xunlock(&dmp->dm_lock);
271 devfs_vptocnp(struct vop_vptocnp_args *ap)
273 struct vnode *vp = ap->a_vp;
274 struct vnode **dvp = ap->a_vpp;
275 struct devfs_mount *dmp;
276 char *buf = ap->a_buf;
277 int *buflen = ap->a_buflen;
278 struct devfs_dirent *dd, *de;
281 dmp = VFSTODEVFS(vp->v_mount);
283 error = devfs_populate_vp(vp);
287 if (vp->v_type != VCHR && vp->v_type != VDIR) {
293 if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
300 i -= dd->de_dirent->d_namlen;
305 bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
307 de = devfs_parent_dirent(dd);
312 mtx_lock(&devfs_de_interlock);
316 mtx_unlock(&devfs_de_interlock);
322 mtx_unlock(&devfs_de_interlock);
326 sx_xunlock(&dmp->dm_lock);
331 * Construct the fully qualified path name relative to the mountpoint.
332 * If a NULL cnp is provided, no '/' is appended to the resulting path.
335 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
336 struct componentname *cnp)
339 struct devfs_dirent *de;
341 sx_assert(&dmp->dm_lock, SA_LOCKED);
346 i -= cnp->cn_namelen;
350 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
352 while (de != dmp->dm_rootdir) {
353 if (cnp != NULL || i < SPECNAMELEN) {
359 i -= de->de_dirent->d_namlen;
362 bcopy(de->de_dirent->d_name, buf + i,
363 de->de_dirent->d_namlen);
364 de = devfs_parent_dirent(de);
372 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
373 struct devfs_dirent *de)
378 if (de->de_flags & DE_DOOMED)
380 if (DEVFS_DE_DROP(de)) {
381 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
382 devfs_dirent_free(de);
384 if (DEVFS_DMP_DROP(dmp)) {
385 KASSERT(not_found == 1,
386 ("DEVFS mount struct freed before dirent"));
388 sx_xunlock(&dmp->dm_lock);
389 devfs_unmount_final(dmp);
391 if (not_found == 1 || (drop_dm_lock && not_found != 2))
392 sx_unlock(&dmp->dm_lock);
397 devfs_insmntque_dtr(struct vnode *vp, void *arg)
399 struct devfs_dirent *de;
401 de = (struct devfs_dirent *)arg;
402 mtx_lock(&devfs_de_interlock);
405 mtx_unlock(&devfs_de_interlock);
411 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
415 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
421 struct devfs_mount *dmp;
424 dmp = VFSTODEVFS(mp);
425 if (de->de_flags & DE_DOOMED) {
426 sx_xunlock(&dmp->dm_lock);
432 mtx_lock(&devfs_de_interlock);
436 mtx_unlock(&devfs_de_interlock);
437 sx_xunlock(&dmp->dm_lock);
438 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
439 sx_xlock(&dmp->dm_lock);
440 if (devfs_allocv_drop_refs(0, dmp, de)) {
444 else if (VN_IS_DOOMED(vp)) {
445 mtx_lock(&devfs_de_interlock);
446 if (de->de_vnode == vp) {
450 mtx_unlock(&devfs_de_interlock);
454 sx_xunlock(&dmp->dm_lock);
458 mtx_unlock(&devfs_de_interlock);
459 if (de->de_dirent->d_type == DT_CHR) {
460 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
461 devfs_allocv_drop_refs(1, dmp, de);
464 dev = &de->de_cdp->cdp_c;
468 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
470 devfs_allocv_drop_refs(1, dmp, de);
471 printf("devfs_allocv: failed to allocate new vnode\n");
475 if (de->de_dirent->d_type == DT_CHR) {
480 /* XXX: v_rdev should be protect by vnode lock */
482 KASSERT(vp->v_usecount == 1,
483 ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
485 /* Special casing of ttys for deadfs. Probably redundant. */
487 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
488 vp->v_vflag |= VV_ISTTY;
491 if ((dev->si_flags & SI_ETERNAL) != 0)
492 vp->v_vflag |= VV_ETERNALDEV;
493 vp->v_op = &devfs_specops;
494 } else if (de->de_dirent->d_type == DT_DIR) {
496 } else if (de->de_dirent->d_type == DT_LNK) {
501 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
503 mtx_lock(&devfs_de_interlock);
506 mtx_unlock(&devfs_de_interlock);
507 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
509 (void) devfs_allocv_drop_refs(1, dmp, de);
512 if (devfs_allocv_drop_refs(0, dmp, de)) {
517 mac_devfs_vnode_associate(mp, de, vp);
519 sx_xunlock(&dmp->dm_lock);
525 devfs_access(struct vop_access_args *ap)
527 struct vnode *vp = ap->a_vp;
528 struct devfs_dirent *de;
533 if (vp->v_type == VDIR)
536 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
537 ap->a_accmode, ap->a_cred, NULL);
542 p = ap->a_td->td_proc;
543 /* We do, however, allow access to the controlling terminal */
545 if (!(p->p_flag & P_CONTROLT)) {
549 if (p->p_session->s_ttydp == de->de_cdp)
555 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
556 "devfs-only flag reuse failed");
559 devfs_close(struct vop_close_args *ap)
561 struct vnode *vp = ap->a_vp, *oldvp;
562 struct thread *td = ap->a_td;
564 struct cdev *dev = vp->v_rdev;
566 int dflags, error, ref, vp_locked;
569 * XXX: Don't call d_close() if we were called because of
570 * XXX: insmntque1() failure.
572 if (vp->v_data == NULL)
576 * Hack: a tty device that is a controlling terminal
577 * has a reference from the session structure.
578 * We cannot easily tell that a character device is
579 * a controlling terminal, unless it is the closing
580 * process' controlling terminal. In that case,
581 * if the reference count is 2 (this last descriptor
582 * plus the session), release the reference from the session.
584 if (vp->v_usecount == 2 && td != NULL) {
587 if (vp == p->p_session->s_ttyvp) {
590 sx_xlock(&proctree_lock);
591 if (vp == p->p_session->s_ttyvp) {
592 SESS_LOCK(p->p_session);
594 if (vp->v_usecount == 2 && vcount(vp) == 1 &&
596 p->p_session->s_ttyvp = NULL;
597 p->p_session->s_ttydp = NULL;
601 SESS_UNLOCK(p->p_session);
603 sx_xunlock(&proctree_lock);
610 * We do not want to really close the device if it
611 * is still in use unless we are trying to close it
612 * forcibly. Since every use (buffer, vnode, swap, cmap)
613 * holds a reference to the vnode, and because we mark
614 * any other vnodes that alias this device, when the
615 * sum of the reference counts on all the aliased
616 * vnodes descends to one, we are on last close.
618 dsw = dev_refthread(dev, &ref);
623 if (vp->v_usecount == 1 && vcount(vp) == 1)
624 dflags |= FLASTCLOSE;
625 if (VN_IS_DOOMED(vp)) {
627 dflags |= FREVOKE | FNONBLOCK;
628 } else if (dsw->d_flags & D_TRACKCLOSE) {
629 /* Keep device updated on status. */
630 } else if ((dflags & FLASTCLOSE) == 0) {
632 dev_relthread(dev, ref);
637 vp_locked = VOP_ISLOCKED(vp);
639 KASSERT(dev->si_refcount > 0,
640 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
641 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
642 dev_relthread(dev, ref);
643 vn_lock(vp, vp_locked | LK_RETRY);
649 devfs_close_f(struct file *fp, struct thread *td)
655 * NB: td may be NULL if this descriptor is closed due to
656 * garbage collection from a closed UNIX domain socket.
658 fpop = curthread->td_fpop;
659 curthread->td_fpop = fp;
660 error = vnops.fo_close(fp, td);
661 curthread->td_fpop = fpop;
664 * The f_cdevpriv cannot be assigned non-NULL value while we
665 * are destroying the file.
667 if (fp->f_cdevpriv != NULL)
673 devfs_getattr(struct vop_getattr_args *ap)
675 struct vnode *vp = ap->a_vp;
676 struct vattr *vap = ap->a_vap;
677 struct devfs_dirent *de;
678 struct devfs_mount *dmp;
680 struct timeval boottime;
683 error = devfs_populate_vp(vp);
687 dmp = VFSTODEVFS(vp->v_mount);
688 sx_xunlock(&dmp->dm_lock);
691 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
692 if (vp->v_type == VDIR) {
695 ("Null dir dirent in devfs_getattr vp=%p", vp));
697 vap->va_uid = de->de_uid;
698 vap->va_gid = de->de_gid;
699 vap->va_mode = de->de_mode;
700 if (vp->v_type == VLNK)
701 vap->va_size = strlen(de->de_symlink);
702 else if (vp->v_type == VDIR)
703 vap->va_size = vap->va_bytes = DEV_BSIZE;
706 if (vp->v_type != VDIR)
708 vap->va_blocksize = DEV_BSIZE;
709 vap->va_type = vp->v_type;
711 getboottime(&boottime);
714 if ((aa).tv_sec <= 3600) { \
715 (aa).tv_sec = boottime.tv_sec; \
716 (aa).tv_nsec = boottime.tv_usec * 1000; \
720 if (vp->v_type != VCHR) {
722 vap->va_atime = de->de_atime;
724 vap->va_mtime = de->de_mtime;
726 vap->va_ctime = de->de_ctime;
730 vap->va_atime = dev->si_atime;
732 vap->va_mtime = dev->si_mtime;
734 vap->va_ctime = dev->si_ctime;
736 vap->va_rdev = cdev2priv(dev)->cdp_inode;
741 vap->va_nlink = de->de_links;
742 vap->va_fileid = de->de_inode;
749 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
756 error = vnops.fo_ioctl(fp, com, data, cred, td);
762 fiodgname_buf_get_ptr(void *fgnp, u_long com)
765 struct fiodgname_arg fgn;
766 #ifdef COMPAT_FREEBSD32
767 struct fiodgname_arg32 fgn32;
774 return (fgnup->fgn.buf);
775 #ifdef COMPAT_FREEBSD32
777 return ((void *)(uintptr_t)fgnup->fgn32.buf);
780 panic("Unhandled ioctl command %ld", com);
785 devfs_ioctl(struct vop_ioctl_args *ap)
787 struct fiodgname_arg *fgn;
788 struct vnode *vpold, *vp;
800 dsw = devvn_refthread(vp, &dev, &ref);
803 KASSERT(dev->si_refcount > 0,
804 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
808 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
812 #ifdef COMPAT_FREEBSD32
821 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
824 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
827 dev_relthread(dev, ref);
828 if (error == ENOIOCTL)
831 if (error == 0 && com == TIOCSCTTY) {
833 * Do nothing if reassigning same control tty, or if the
834 * control tty has already disappeared. If it disappeared,
835 * it's because we were racing with TIOCNOTTY. TIOCNOTTY
836 * already took care of releasing the old vnode and we have
837 * nothing left to do.
839 sx_slock(&proctree_lock);
840 if (td->td_proc->p_session->s_ttyvp == vp ||
841 td->td_proc->p_session->s_ttyp == NULL) {
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;
925 int error, flags, nameiop, dvplocked;
926 char specname[SPECNAMELEN + 1], *pname;
931 pname = cnp->cn_nameptr;
933 flags = cnp->cn_flags;
934 nameiop = cnp->cn_nameiop;
936 dmp = VFSTODEVFS(mp);
940 if ((flags & ISLASTCN) && nameiop == RENAME)
943 if (dvp->v_type != VDIR)
946 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
949 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
953 if (cnp->cn_namelen == 1 && *pname == '.') {
954 if ((flags & ISLASTCN) && nameiop != LOOKUP)
961 if (flags & ISDOTDOT) {
962 if ((flags & ISLASTCN) && nameiop != LOOKUP)
964 de = devfs_parent_dirent(dd);
967 dvplocked = VOP_ISLOCKED(dvp);
969 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
972 vn_lock(dvp, dvplocked | LK_RETRY);
977 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
978 while (de == NULL) { /* While(...) so we can use break */
980 if (nameiop == DELETE)
984 * OK, we didn't have an entry for the name we were asked for
985 * so we try to see if anybody can create it on demand.
987 pname = devfs_fqpn(specname, dmp, dd, cnp);
993 sx_xunlock(&dmp->dm_lock);
994 sx_slock(&clone_drain_lock);
995 EVENTHANDLER_INVOKE(dev_clone,
996 td->td_ucred, pname, strlen(pname), &cdev);
997 sx_sunlock(&clone_drain_lock);
1000 sx_xlock(&dmp->dm_lock);
1001 else if (devfs_populate_vp(dvp) != 0) {
1003 sx_xlock(&dmp->dm_lock);
1004 if (DEVFS_DMP_DROP(dmp)) {
1005 sx_xunlock(&dmp->dm_lock);
1006 devfs_unmount_final(dmp);
1008 sx_xunlock(&dmp->dm_lock);
1012 if (DEVFS_DMP_DROP(dmp)) {
1014 sx_xunlock(&dmp->dm_lock);
1015 devfs_unmount_final(dmp);
1025 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1026 if (dde != NULL && *dde != NULL)
1033 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1034 if ((nameiop == CREATE || nameiop == RENAME) &&
1035 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1036 cnp->cn_flags |= SAVENAME;
1037 return (EJUSTRETURN);
1042 if (devfs_prison_check(de, td))
1045 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1046 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1055 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1061 devfs_lookup(struct vop_lookup_args *ap)
1064 struct devfs_mount *dmp;
1067 if (devfs_populate_vp(ap->a_dvp) != 0)
1070 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1072 j = devfs_lookupx(ap, &dm_unlock);
1074 sx_xunlock(&dmp->dm_lock);
1079 devfs_mknod(struct vop_mknod_args *ap)
1081 struct componentname *cnp;
1082 struct vnode *dvp, **vpp;
1083 struct devfs_dirent *dd, *de;
1084 struct devfs_mount *dmp;
1088 * The only type of node we should be creating here is a
1089 * character device, for anything else return EOPNOTSUPP.
1091 if (ap->a_vap->va_type != VCHR)
1092 return (EOPNOTSUPP);
1094 dmp = VFSTODEVFS(dvp->v_mount);
1101 sx_xlock(&dmp->dm_lock);
1102 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1103 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1105 if (de->de_dirent->d_type == DT_CHR &&
1106 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1108 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1109 de->de_dirent->d_namlen) != 0)
1111 if (de->de_flags & DE_WHITEOUT)
1117 de->de_flags &= ~DE_WHITEOUT;
1118 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1121 sx_xunlock(&dmp->dm_lock);
1127 devfs_open(struct vop_open_args *ap)
1129 struct thread *td = ap->a_td;
1130 struct vnode *vp = ap->a_vp;
1131 struct cdev *dev = vp->v_rdev;
1132 struct file *fp = ap->a_fp;
1133 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);
1193 devfs_pathconf(struct vop_pathconf_args *ap)
1196 switch (ap->a_name) {
1197 case _PC_FILESIZEBITS:
1201 *ap->a_retval = NAME_MAX;
1204 *ap->a_retval = INT_MAX;
1206 case _PC_SYMLINK_MAX:
1207 *ap->a_retval = MAXPATHLEN;
1210 if (ap->a_vp->v_vflag & VV_ISTTY) {
1211 *ap->a_retval = MAX_CANON;
1216 if (ap->a_vp->v_vflag & VV_ISTTY) {
1217 *ap->a_retval = MAX_INPUT;
1222 if (ap->a_vp->v_vflag & VV_ISTTY) {
1223 *ap->a_retval = _POSIX_VDISABLE;
1227 case _PC_MAC_PRESENT:
1230 * If MAC is enabled, devfs automatically supports
1231 * trivial non-persistant label storage.
1238 case _PC_CHOWN_RESTRICTED:
1242 return (vop_stdpathconf(ap));
1249 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1257 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1259 error = vnops.fo_poll(fp, events, cred, td);
1262 error = dsw->d_poll(dev, events, td);
1264 dev_relthread(dev, ref);
1269 * Print out the contents of a special device vnode.
1272 devfs_print(struct vop_print_args *ap)
1275 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1280 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1281 int flags, struct thread *td)
1284 int ioflag, error, ref;
1289 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1292 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1294 error = vnops.fo_read(fp, uio, cred, flags, td);
1297 resid = uio->uio_resid;
1298 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1299 if (ioflag & O_DIRECT)
1300 ioflag |= IO_DIRECT;
1302 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1303 error = dsw->d_read(dev, uio, ioflag);
1304 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1305 devfs_timestamp(&dev->si_atime);
1307 dev_relthread(dev, ref);
1309 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1314 devfs_readdir(struct vop_readdir_args *ap)
1319 struct devfs_dirent *dd;
1320 struct devfs_dirent *de;
1321 struct devfs_mount *dmp;
1323 int *tmp_ncookies = NULL;
1325 if (ap->a_vp->v_type != VDIR)
1329 if (uio->uio_offset < 0)
1333 * XXX: This is a temporary hack to get around this filesystem not
1334 * supporting cookies. We store the location of the ncookies pointer
1335 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1336 * and set the number of cookies to 0. We then set the pointer to
1337 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1338 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1339 * pointer to its original location before returning to the caller.
1341 if (ap->a_ncookies != NULL) {
1342 tmp_ncookies = ap->a_ncookies;
1343 *ap->a_ncookies = 0;
1344 ap->a_ncookies = NULL;
1347 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1348 if (devfs_populate_vp(ap->a_vp) != 0) {
1349 if (tmp_ncookies != NULL)
1350 ap->a_ncookies = tmp_ncookies;
1354 de = ap->a_vp->v_data;
1356 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1357 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1358 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1360 if (devfs_prison_check(dd, uio->uio_td))
1362 if (dd->de_dirent->d_type == DT_DIR)
1367 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1368 if (dp->d_reclen > uio->uio_resid)
1370 dp->d_fileno = de->de_inode;
1371 /* NOTE: d_off is the offset for the *next* entry. */
1372 dp->d_off = off + dp->d_reclen;
1373 if (off >= uio->uio_offset) {
1374 error = vfs_read_dirent(ap, dp, off);
1378 off += dp->d_reclen;
1380 sx_xunlock(&dmp->dm_lock);
1381 uio->uio_offset = off;
1384 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1387 if (tmp_ncookies != NULL)
1388 ap->a_ncookies = tmp_ncookies;
1394 devfs_readlink(struct vop_readlink_args *ap)
1396 struct devfs_dirent *de;
1398 de = ap->a_vp->v_data;
1399 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1403 devfs_reclaim(struct vop_reclaim_args *ap)
1406 struct devfs_dirent *de;
1409 mtx_lock(&devfs_de_interlock);
1412 de->de_vnode = NULL;
1415 mtx_unlock(&devfs_de_interlock);
1420 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1426 MPASS(vp->v_type == VCHR);
1435 dev->si_usecount -= (vp->v_usecount > 0);
1444 devfs_remove(struct vop_remove_args *ap)
1446 struct vnode *dvp = ap->a_dvp;
1447 struct vnode *vp = ap->a_vp;
1448 struct devfs_dirent *dd;
1449 struct devfs_dirent *de, *de_covered;
1450 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1452 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1453 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1455 sx_xlock(&dmp->dm_lock);
1456 dd = ap->a_dvp->v_data;
1458 if (de->de_cdp == NULL) {
1459 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1460 if (de->de_dirent->d_type == DT_LNK) {
1461 de_covered = devfs_find(dd, de->de_dirent->d_name,
1462 de->de_dirent->d_namlen, 0);
1463 if (de_covered != NULL)
1464 de_covered->de_flags &= ~DE_COVERED;
1466 /* We need to unlock dvp because devfs_delete() may lock it. */
1470 devfs_delete(dmp, de, 0);
1471 sx_xunlock(&dmp->dm_lock);
1473 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1476 de->de_flags |= DE_WHITEOUT;
1477 sx_xunlock(&dmp->dm_lock);
1483 * Revoke is called on a tty when a terminal session ends. The vnode
1484 * is orphaned by setting v_op to deadfs so we need to let go of it
1485 * as well so that we create a new one next time around.
1489 devfs_revoke(struct vop_revoke_args *ap)
1491 struct vnode *vp = ap->a_vp, *vp2;
1493 struct cdev_priv *cdp;
1494 struct devfs_dirent *de;
1497 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1500 cdp = cdev2priv(dev);
1513 mtx_lock(&devfs_de_interlock);
1516 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1517 de = cdp->cdp_dirents[i];
1525 mtx_unlock(&devfs_de_interlock);
1526 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1540 mtx_unlock(&devfs_de_interlock);
1545 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1546 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1548 dev_rel(&cdp->cdp_c);
1552 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1557 devfs_rioctl(struct vop_ioctl_args *ap)
1560 struct devfs_mount *dmp;
1564 vn_lock(vp, LK_SHARED | LK_RETRY);
1565 if (VN_IS_DOOMED(vp)) {
1569 dmp = VFSTODEVFS(vp->v_mount);
1570 sx_xlock(&dmp->dm_lock);
1572 DEVFS_DMP_HOLD(dmp);
1573 devfs_populate(dmp);
1574 if (DEVFS_DMP_DROP(dmp)) {
1575 sx_xunlock(&dmp->dm_lock);
1576 devfs_unmount_final(dmp);
1579 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1580 sx_xunlock(&dmp->dm_lock);
1585 devfs_rread(struct vop_read_args *ap)
1588 if (ap->a_vp->v_type != VDIR)
1590 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1594 devfs_setattr(struct vop_setattr_args *ap)
1596 struct devfs_dirent *de;
1607 if ((vap->va_type != VNON) ||
1608 (vap->va_nlink != VNOVAL) ||
1609 (vap->va_fsid != VNOVAL) ||
1610 (vap->va_fileid != VNOVAL) ||
1611 (vap->va_blocksize != VNOVAL) ||
1612 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1613 (vap->va_rdev != VNOVAL) ||
1614 ((int)vap->va_bytes != VNOVAL) ||
1615 (vap->va_gen != VNOVAL)) {
1619 error = devfs_populate_vp(vp);
1624 if (vp->v_type == VDIR)
1628 if (vap->va_uid == (uid_t)VNOVAL)
1632 if (vap->va_gid == (gid_t)VNOVAL)
1636 if (uid != de->de_uid || gid != de->de_gid) {
1637 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1638 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1639 error = priv_check(td, PRIV_VFS_CHOWN);
1648 if (vap->va_mode != (mode_t)VNOVAL) {
1649 if (ap->a_cred->cr_uid != de->de_uid) {
1650 error = priv_check(td, PRIV_VFS_ADMIN);
1654 de->de_mode = vap->va_mode;
1658 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1659 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1662 if (vap->va_atime.tv_sec != VNOVAL) {
1663 if (vp->v_type == VCHR)
1664 vp->v_rdev->si_atime = vap->va_atime;
1666 de->de_atime = vap->va_atime;
1668 if (vap->va_mtime.tv_sec != VNOVAL) {
1669 if (vp->v_type == VCHR)
1670 vp->v_rdev->si_mtime = vap->va_mtime;
1672 de->de_mtime = vap->va_mtime;
1678 if (vp->v_type == VCHR)
1679 vfs_timestamp(&vp->v_rdev->si_ctime);
1681 vfs_timestamp(&de->de_mtime);
1685 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1691 devfs_setlabel(struct vop_setlabel_args *ap)
1694 struct devfs_dirent *de;
1699 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1700 mac_devfs_update(vp->v_mount, de, vp);
1707 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1710 return (vnops.fo_stat(fp, sb, cred, td));
1714 devfs_symlink(struct vop_symlink_args *ap)
1717 struct devfs_dirent *dd;
1718 struct devfs_dirent *de, *de_covered, *de_dotdot;
1719 struct devfs_mount *dmp;
1721 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1724 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1725 if (devfs_populate_vp(ap->a_dvp) != 0)
1728 dd = ap->a_dvp->v_data;
1729 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1730 de->de_flags = DE_USER;
1734 de->de_inode = alloc_unr(devfs_inos);
1736 de->de_dirent->d_type = DT_LNK;
1737 i = strlen(ap->a_target) + 1;
1738 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1739 bcopy(ap->a_target, de->de_symlink, i);
1741 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1743 de_covered = devfs_find(dd, de->de_dirent->d_name,
1744 de->de_dirent->d_namlen, 0);
1745 if (de_covered != NULL) {
1746 if ((de_covered->de_flags & DE_USER) != 0) {
1747 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1748 sx_xunlock(&dmp->dm_lock);
1751 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1752 ("devfs_symlink: entry %p already covered", de_covered));
1753 de_covered->de_flags |= DE_COVERED;
1756 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1757 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1758 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1759 devfs_dir_ref_de(dmp, dd);
1760 devfs_rules_apply(dmp, de);
1762 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1766 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1769 return (vnops.fo_truncate(fp, length, cred, td));
1773 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1774 int flags, struct thread *td)
1777 int error, ioflag, ref;
1782 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1785 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1787 error = vnops.fo_write(fp, uio, cred, flags, td);
1790 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1791 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1792 if (ioflag & O_DIRECT)
1793 ioflag |= IO_DIRECT;
1794 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1796 resid = uio->uio_resid;
1798 error = dsw->d_write(dev, uio, ioflag);
1799 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1800 devfs_timestamp(&dev->si_ctime);
1801 dev->si_mtime = dev->si_ctime;
1804 dev_relthread(dev, ref);
1806 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1811 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1812 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1827 * Ensure that file and memory protections are
1831 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1832 maxprot = VM_PROT_NONE;
1833 if ((prot & VM_PROT_EXECUTE) != 0)
1836 maxprot = VM_PROT_EXECUTE;
1837 if ((fp->f_flag & FREAD) != 0)
1838 maxprot |= VM_PROT_READ;
1839 else if ((prot & VM_PROT_READ) != 0)
1843 * If we are sharing potential changes via MAP_SHARED and we
1844 * are trying to get write permission although we opened it
1845 * without asking for it, bail out.
1847 * Note that most character devices always share mappings.
1848 * The one exception is that D_MMAP_ANON devices
1849 * (i.e. /dev/zero) permit private writable mappings.
1851 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1852 * as well as updating maxprot to permit writing for
1853 * D_MMAP_ANON devices rather than doing that here.
1855 if ((flags & MAP_SHARED) != 0) {
1856 if ((fp->f_flag & FWRITE) != 0)
1857 maxprot |= VM_PROT_WRITE;
1858 else if ((prot & VM_PROT_WRITE) != 0)
1861 maxprot &= cap_maxprot;
1864 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1868 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1871 dev_relthread(dev, ref);
1875 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1878 vm_object_deallocate(object);
1883 dev2udev(struct cdev *x)
1887 return (cdev2priv(x)->cdp_inode);
1890 static struct fileops devfs_ops_f = {
1891 .fo_read = devfs_read_f,
1892 .fo_write = devfs_write_f,
1893 .fo_truncate = devfs_truncate_f,
1894 .fo_ioctl = devfs_ioctl_f,
1895 .fo_poll = devfs_poll_f,
1896 .fo_kqfilter = devfs_kqfilter_f,
1897 .fo_stat = devfs_stat_f,
1898 .fo_close = devfs_close_f,
1899 .fo_chmod = vn_chmod,
1900 .fo_chown = vn_chown,
1901 .fo_sendfile = vn_sendfile,
1903 .fo_fill_kinfo = vn_fill_kinfo,
1904 .fo_mmap = devfs_mmap_f,
1905 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
1908 /* Vops for non-CHR vnodes in /dev. */
1909 static struct vop_vector devfs_vnodeops = {
1910 .vop_default = &default_vnodeops,
1912 .vop_access = devfs_access,
1913 .vop_getattr = devfs_getattr,
1914 .vop_ioctl = devfs_rioctl,
1915 .vop_lookup = devfs_lookup,
1916 .vop_mknod = devfs_mknod,
1917 .vop_pathconf = devfs_pathconf,
1918 .vop_read = devfs_rread,
1919 .vop_readdir = devfs_readdir,
1920 .vop_readlink = devfs_readlink,
1921 .vop_reclaim = devfs_reclaim,
1922 .vop_remove = devfs_remove,
1923 .vop_revoke = devfs_revoke,
1924 .vop_setattr = devfs_setattr,
1926 .vop_setlabel = devfs_setlabel,
1928 .vop_symlink = devfs_symlink,
1929 .vop_vptocnp = devfs_vptocnp,
1931 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
1933 /* Vops for VCHR vnodes in /dev. */
1934 static struct vop_vector devfs_specops = {
1935 .vop_default = &default_vnodeops,
1937 .vop_access = devfs_access,
1938 .vop_bmap = VOP_PANIC,
1939 .vop_close = devfs_close,
1940 .vop_create = VOP_PANIC,
1941 .vop_fsync = vop_stdfsync,
1942 .vop_getattr = devfs_getattr,
1943 .vop_ioctl = devfs_ioctl,
1944 .vop_link = VOP_PANIC,
1945 .vop_mkdir = VOP_PANIC,
1946 .vop_mknod = VOP_PANIC,
1947 .vop_open = devfs_open,
1948 .vop_pathconf = devfs_pathconf,
1949 .vop_poll = dead_poll,
1950 .vop_print = devfs_print,
1951 .vop_read = dead_read,
1952 .vop_readdir = VOP_PANIC,
1953 .vop_readlink = VOP_PANIC,
1954 .vop_reallocblks = VOP_PANIC,
1955 .vop_reclaim = devfs_reclaim_vchr,
1956 .vop_remove = devfs_remove,
1957 .vop_rename = VOP_PANIC,
1958 .vop_revoke = devfs_revoke,
1959 .vop_rmdir = VOP_PANIC,
1960 .vop_setattr = devfs_setattr,
1962 .vop_setlabel = devfs_setlabel,
1964 .vop_strategy = VOP_PANIC,
1965 .vop_symlink = VOP_PANIC,
1966 .vop_vptocnp = devfs_vptocnp,
1967 .vop_write = dead_write,
1969 VFS_VOP_VECTOR_REGISTER(devfs_specops);
1972 * Our calling convention to the device drivers used to be that we passed
1973 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1974 * flags instead since that's what open(), close() and ioctl() takes and
1975 * we don't really want vnode.h in device drivers.
1976 * We solved the source compatibility by redefining some vnode flags to
1977 * be the same as the fcntl ones and by sending down the bitwise OR of
1978 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
1979 * pulls the rug out under this.
1981 CTASSERT(O_NONBLOCK == IO_NDELAY);
1982 CTASSERT(O_FSYNC == IO_SYNC);