]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/devfs/devfs_vnops.c
MFHead @r350386
[FreeBSD/FreeBSD.git] / sys / fs / devfs / devfs_vnops.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
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.
8  *
9  * This code is derived from software donated to Berkeley by
10  * Jan-Simon Pendry.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  *      @(#)kernfs_vnops.c      8.15 (Berkeley) 5/21/95
34  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
35  *
36  * $FreeBSD$
37  */
38
39 /*
40  * TODO:
41  *      mkdir: want it ?
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/dirent.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/filio.h>
53 #include <sys/jail.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mman.h>
58 #include <sys/mount.h>
59 #include <sys/namei.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/stat.h>
63 #include <sys/sx.h>
64 #include <sys/sysctl.h>
65 #include <sys/time.h>
66 #include <sys/ttycom.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
69
70 static struct vop_vector devfs_vnodeops;
71 static struct vop_vector devfs_specops;
72 static struct fileops devfs_ops_f;
73
74 #include <fs/devfs/devfs.h>
75 #include <fs/devfs/devfs_int.h>
76
77 #include <security/mac/mac_framework.h>
78
79 #include <vm/vm.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_object.h>
82
83 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
84
85 struct mtx      devfs_de_interlock;
86 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
87 struct sx       clone_drain_lock;
88 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
89 struct mtx      cdevpriv_mtx;
90 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
91
92 SYSCTL_DECL(_vfs_devfs);
93
94 static int devfs_dotimes;
95 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
96     &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
97
98 /*
99  * Update devfs node timestamp.  Note that updates are unlocked and
100  * stat(2) could see partially updated times.
101  */
102 static void
103 devfs_timestamp(struct timespec *tsp)
104 {
105         time_t ts;
106
107         if (devfs_dotimes) {
108                 vfs_timestamp(tsp);
109         } else {
110                 ts = time_second;
111                 if (tsp->tv_sec != ts) {
112                         tsp->tv_sec = ts;
113                         tsp->tv_nsec = 0;
114                 }
115         }
116 }
117
118 static int
119 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
120     int *ref)
121 {
122
123         *dswp = devvn_refthread(fp->f_vnode, devp, ref);
124         if (*devp != fp->f_data) {
125                 if (*dswp != NULL)
126                         dev_relthread(*devp, *ref);
127                 return (ENXIO);
128         }
129         KASSERT((*devp)->si_refcount > 0,
130             ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
131         if (*dswp == NULL)
132                 return (ENXIO);
133         curthread->td_fpop = fp;
134         return (0);
135 }
136
137 int
138 devfs_get_cdevpriv(void **datap)
139 {
140         struct file *fp;
141         struct cdev_privdata *p;
142         int error;
143
144         fp = curthread->td_fpop;
145         if (fp == NULL)
146                 return (EBADF);
147         p = fp->f_cdevpriv;
148         if (p != NULL) {
149                 error = 0;
150                 *datap = p->cdpd_data;
151         } else
152                 error = ENOENT;
153         return (error);
154 }
155
156 int
157 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
158 {
159         struct file *fp;
160         struct cdev_priv *cdp;
161         struct cdev_privdata *p;
162         int error;
163
164         fp = curthread->td_fpop;
165         if (fp == NULL)
166                 return (ENOENT);
167         cdp = cdev2priv((struct cdev *)fp->f_data);
168         p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
169         p->cdpd_data = priv;
170         p->cdpd_dtr = priv_dtr;
171         p->cdpd_fp = fp;
172         mtx_lock(&cdevpriv_mtx);
173         if (fp->f_cdevpriv == NULL) {
174                 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
175                 fp->f_cdevpriv = p;
176                 mtx_unlock(&cdevpriv_mtx);
177                 error = 0;
178         } else {
179                 mtx_unlock(&cdevpriv_mtx);
180                 free(p, M_CDEVPDATA);
181                 error = EBUSY;
182         }
183         return (error);
184 }
185
186 void
187 devfs_destroy_cdevpriv(struct cdev_privdata *p)
188 {
189
190         mtx_assert(&cdevpriv_mtx, MA_OWNED);
191         KASSERT(p->cdpd_fp->f_cdevpriv == p,
192             ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
193         p->cdpd_fp->f_cdevpriv = NULL;
194         LIST_REMOVE(p, cdpd_list);
195         mtx_unlock(&cdevpriv_mtx);
196         (p->cdpd_dtr)(p->cdpd_data);
197         free(p, M_CDEVPDATA);
198 }
199
200 static void
201 devfs_fpdrop(struct file *fp)
202 {
203         struct cdev_privdata *p;
204
205         mtx_lock(&cdevpriv_mtx);
206         if ((p = fp->f_cdevpriv) == NULL) {
207                 mtx_unlock(&cdevpriv_mtx);
208                 return;
209         }
210         devfs_destroy_cdevpriv(p);
211 }
212
213 void
214 devfs_clear_cdevpriv(void)
215 {
216         struct file *fp;
217
218         fp = curthread->td_fpop;
219         if (fp == NULL)
220                 return;
221         devfs_fpdrop(fp);
222 }
223
224 /*
225  * On success devfs_populate_vp() returns with dmp->dm_lock held.
226  */
227 static int
228 devfs_populate_vp(struct vnode *vp)
229 {
230         struct devfs_dirent *de;
231         struct devfs_mount *dmp;
232         int locked;
233
234         ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
235
236         dmp = VFSTODEVFS(vp->v_mount);
237         locked = VOP_ISLOCKED(vp);
238
239         sx_xlock(&dmp->dm_lock);
240         DEVFS_DMP_HOLD(dmp);
241
242         /* Can't call devfs_populate() with the vnode lock held. */
243         VOP_UNLOCK(vp, 0);
244         devfs_populate(dmp);
245
246         sx_xunlock(&dmp->dm_lock);
247         vn_lock(vp, locked | LK_RETRY);
248         sx_xlock(&dmp->dm_lock);
249         if (DEVFS_DMP_DROP(dmp)) {
250                 sx_xunlock(&dmp->dm_lock);
251                 devfs_unmount_final(dmp);
252                 return (ERESTART);
253         }
254         if ((vp->v_iflag & VI_DOOMED) != 0) {
255                 sx_xunlock(&dmp->dm_lock);
256                 return (ERESTART);
257         }
258         de = vp->v_data;
259         KASSERT(de != NULL,
260             ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
261         if ((de->de_flags & DE_DOOMED) != 0) {
262                 sx_xunlock(&dmp->dm_lock);
263                 return (ERESTART);
264         }
265
266         return (0);
267 }
268
269 static int
270 devfs_vptocnp(struct vop_vptocnp_args *ap)
271 {
272         struct vnode *vp = ap->a_vp;
273         struct vnode **dvp = ap->a_vpp;
274         struct devfs_mount *dmp;
275         char *buf = ap->a_buf;
276         int *buflen = ap->a_buflen;
277         struct devfs_dirent *dd, *de;
278         int i, error;
279
280         dmp = VFSTODEVFS(vp->v_mount);
281
282         error = devfs_populate_vp(vp);
283         if (error != 0)
284                 return (error);
285
286         i = *buflen;
287         dd = vp->v_data;
288
289         if (vp->v_type == VCHR) {
290                 i -= strlen(dd->de_cdp->cdp_c.si_name);
291                 if (i < 0) {
292                         error = ENOMEM;
293                         goto finished;
294                 }
295                 bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
296                     strlen(dd->de_cdp->cdp_c.si_name));
297                 de = dd->de_dir;
298         } else if (vp->v_type == VDIR) {
299                 if (dd == dmp->dm_rootdir) {
300                         *dvp = vp;
301                         vref(*dvp);
302                         goto finished;
303                 }
304                 i -= dd->de_dirent->d_namlen;
305                 if (i < 0) {
306                         error = ENOMEM;
307                         goto finished;
308                 }
309                 bcopy(dd->de_dirent->d_name, buf + i,
310                     dd->de_dirent->d_namlen);
311                 de = dd;
312         } else {
313                 error = ENOENT;
314                 goto finished;
315         }
316         *buflen = i;
317         de = devfs_parent_dirent(de);
318         if (de == NULL) {
319                 error = ENOENT;
320                 goto finished;
321         }
322         mtx_lock(&devfs_de_interlock);
323         *dvp = de->de_vnode;
324         if (*dvp != NULL) {
325                 VI_LOCK(*dvp);
326                 mtx_unlock(&devfs_de_interlock);
327                 vholdl(*dvp);
328                 VI_UNLOCK(*dvp);
329                 vref(*dvp);
330                 vdrop(*dvp);
331         } else {
332                 mtx_unlock(&devfs_de_interlock);
333                 error = ENOENT;
334         }
335 finished:
336         sx_xunlock(&dmp->dm_lock);
337         return (error);
338 }
339
340 /*
341  * Construct the fully qualified path name relative to the mountpoint.
342  * If a NULL cnp is provided, no '/' is appended to the resulting path.
343  */
344 char *
345 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
346     struct componentname *cnp)
347 {
348         int i;
349         struct devfs_dirent *de;
350
351         sx_assert(&dmp->dm_lock, SA_LOCKED);
352
353         i = SPECNAMELEN;
354         buf[i] = '\0';
355         if (cnp != NULL)
356                 i -= cnp->cn_namelen;
357         if (i < 0)
358                  return (NULL);
359         if (cnp != NULL)
360                 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
361         de = dd;
362         while (de != dmp->dm_rootdir) {
363                 if (cnp != NULL || i < SPECNAMELEN) {
364                         i--;
365                         if (i < 0)
366                                  return (NULL);
367                         buf[i] = '/';
368                 }
369                 i -= de->de_dirent->d_namlen;
370                 if (i < 0)
371                          return (NULL);
372                 bcopy(de->de_dirent->d_name, buf + i,
373                     de->de_dirent->d_namlen);
374                 de = devfs_parent_dirent(de);
375                 if (de == NULL)
376                         return (NULL);
377         }
378         return (buf + i);
379 }
380
381 static int
382 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
383         struct devfs_dirent *de)
384 {
385         int not_found;
386
387         not_found = 0;
388         if (de->de_flags & DE_DOOMED)
389                 not_found = 1;
390         if (DEVFS_DE_DROP(de)) {
391                 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
392                 devfs_dirent_free(de);
393         }
394         if (DEVFS_DMP_DROP(dmp)) {
395                 KASSERT(not_found == 1,
396                         ("DEVFS mount struct freed before dirent"));
397                 not_found = 2;
398                 sx_xunlock(&dmp->dm_lock);
399                 devfs_unmount_final(dmp);
400         }
401         if (not_found == 1 || (drop_dm_lock && not_found != 2))
402                 sx_unlock(&dmp->dm_lock);
403         return (not_found);
404 }
405
406 static void
407 devfs_insmntque_dtr(struct vnode *vp, void *arg)
408 {
409         struct devfs_dirent *de;
410
411         de = (struct devfs_dirent *)arg;
412         mtx_lock(&devfs_de_interlock);
413         vp->v_data = NULL;
414         de->de_vnode = NULL;
415         mtx_unlock(&devfs_de_interlock);
416         vgone(vp);
417         vput(vp);
418 }
419
420 /*
421  * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
422  * it on return.
423  */
424 int
425 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
426     struct vnode **vpp)
427 {
428         int error;
429         struct vnode *vp;
430         struct cdev *dev;
431         struct devfs_mount *dmp;
432         struct cdevsw *dsw;
433
434         dmp = VFSTODEVFS(mp);
435         if (de->de_flags & DE_DOOMED) {
436                 sx_xunlock(&dmp->dm_lock);
437                 return (ENOENT);
438         }
439 loop:
440         DEVFS_DE_HOLD(de);
441         DEVFS_DMP_HOLD(dmp);
442         mtx_lock(&devfs_de_interlock);
443         vp = de->de_vnode;
444         if (vp != NULL) {
445                 VI_LOCK(vp);
446                 mtx_unlock(&devfs_de_interlock);
447                 sx_xunlock(&dmp->dm_lock);
448                 vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
449                 sx_xlock(&dmp->dm_lock);
450                 if (devfs_allocv_drop_refs(0, dmp, de)) {
451                         vput(vp);
452                         return (ENOENT);
453                 }
454                 else if ((vp->v_iflag & VI_DOOMED) != 0) {
455                         mtx_lock(&devfs_de_interlock);
456                         if (de->de_vnode == vp) {
457                                 de->de_vnode = NULL;
458                                 vp->v_data = NULL;
459                         }
460                         mtx_unlock(&devfs_de_interlock);
461                         vput(vp);
462                         goto loop;
463                 }
464                 sx_xunlock(&dmp->dm_lock);
465                 *vpp = vp;
466                 return (0);
467         }
468         mtx_unlock(&devfs_de_interlock);
469         if (de->de_dirent->d_type == DT_CHR) {
470                 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
471                         devfs_allocv_drop_refs(1, dmp, de);
472                         return (ENOENT);
473                 }
474                 dev = &de->de_cdp->cdp_c;
475         } else {
476                 dev = NULL;
477         }
478         error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
479         if (error != 0) {
480                 devfs_allocv_drop_refs(1, dmp, de);
481                 printf("devfs_allocv: failed to allocate new vnode\n");
482                 return (error);
483         }
484
485         if (de->de_dirent->d_type == DT_CHR) {
486                 vp->v_type = VCHR;
487                 VI_LOCK(vp);
488                 dev_lock();
489                 dev_refl(dev);
490                 /* XXX: v_rdev should be protect by vnode lock */
491                 vp->v_rdev = dev;
492                 KASSERT(vp->v_usecount == 1,
493                     ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
494                 dev->si_usecount += vp->v_usecount;
495                 /* Special casing of ttys for deadfs.  Probably redundant. */
496                 dsw = dev->si_devsw;
497                 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
498                         vp->v_vflag |= VV_ISTTY;
499                 dev_unlock();
500                 VI_UNLOCK(vp);
501                 if ((dev->si_flags & SI_ETERNAL) != 0)
502                         vp->v_vflag |= VV_ETERNALDEV;
503                 vp->v_op = &devfs_specops;
504         } else if (de->de_dirent->d_type == DT_DIR) {
505                 vp->v_type = VDIR;
506         } else if (de->de_dirent->d_type == DT_LNK) {
507                 vp->v_type = VLNK;
508         } else {
509                 vp->v_type = VBAD;
510         }
511         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
512         VN_LOCK_ASHARE(vp);
513         mtx_lock(&devfs_de_interlock);
514         vp->v_data = de;
515         de->de_vnode = vp;
516         mtx_unlock(&devfs_de_interlock);
517         error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
518         if (error != 0) {
519                 (void) devfs_allocv_drop_refs(1, dmp, de);
520                 return (error);
521         }
522         if (devfs_allocv_drop_refs(0, dmp, de)) {
523                 vput(vp);
524                 return (ENOENT);
525         }
526 #ifdef MAC
527         mac_devfs_vnode_associate(mp, de, vp);
528 #endif
529         sx_xunlock(&dmp->dm_lock);
530         *vpp = vp;
531         return (0);
532 }
533
534 static int
535 devfs_access(struct vop_access_args *ap)
536 {
537         struct vnode *vp = ap->a_vp;
538         struct devfs_dirent *de;
539         struct proc *p;
540         int error;
541
542         de = vp->v_data;
543         if (vp->v_type == VDIR)
544                 de = de->de_dir;
545
546         error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
547             ap->a_accmode, ap->a_cred, NULL);
548         if (error == 0)
549                 return (0);
550         if (error != EACCES)
551                 return (error);
552         p = ap->a_td->td_proc;
553         /* We do, however, allow access to the controlling terminal */
554         PROC_LOCK(p);
555         if (!(p->p_flag & P_CONTROLT)) {
556                 PROC_UNLOCK(p);
557                 return (error);
558         }
559         if (p->p_session->s_ttydp == de->de_cdp)
560                 error = 0;
561         PROC_UNLOCK(p);
562         return (error);
563 }
564
565 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
566     "devfs-only flag reuse failed");
567
568 static int
569 devfs_close(struct vop_close_args *ap)
570 {
571         struct vnode *vp = ap->a_vp, *oldvp;
572         struct thread *td = ap->a_td;
573         struct proc *p;
574         struct cdev *dev = vp->v_rdev;
575         struct cdevsw *dsw;
576         int dflags, error, ref, vp_locked;
577
578         /*
579          * XXX: Don't call d_close() if we were called because of
580          * XXX: insmntque1() failure.
581          */
582         if (vp->v_data == NULL)
583                 return (0);
584
585         /*
586          * Hack: a tty device that is a controlling terminal
587          * has a reference from the session structure.
588          * We cannot easily tell that a character device is
589          * a controlling terminal, unless it is the closing
590          * process' controlling terminal.  In that case,
591          * if the reference count is 2 (this last descriptor
592          * plus the session), release the reference from the session.
593          */
594         if (td != NULL) {
595                 p = td->td_proc;
596                 PROC_LOCK(p);
597                 if (vp == p->p_session->s_ttyvp) {
598                         PROC_UNLOCK(p);
599                         oldvp = NULL;
600                         sx_xlock(&proctree_lock);
601                         if (vp == p->p_session->s_ttyvp) {
602                                 SESS_LOCK(p->p_session);
603                                 VI_LOCK(vp);
604                                 if (count_dev(dev) == 2 &&
605                                     (vp->v_iflag & VI_DOOMED) == 0) {
606                                         p->p_session->s_ttyvp = NULL;
607                                         p->p_session->s_ttydp = NULL;
608                                         oldvp = vp;
609                                 }
610                                 VI_UNLOCK(vp);
611                                 SESS_UNLOCK(p->p_session);
612                         }
613                         sx_xunlock(&proctree_lock);
614                         if (oldvp != NULL)
615                                 vrele(oldvp);
616                 } else
617                         PROC_UNLOCK(p);
618         }
619         /*
620          * We do not want to really close the device if it
621          * is still in use unless we are trying to close it
622          * forcibly. Since every use (buffer, vnode, swap, cmap)
623          * holds a reference to the vnode, and because we mark
624          * any other vnodes that alias this device, when the
625          * sum of the reference counts on all the aliased
626          * vnodes descends to one, we are on last close.
627          */
628         dsw = dev_refthread(dev, &ref);
629         if (dsw == NULL)
630                 return (ENXIO);
631         dflags = 0;
632         VI_LOCK(vp);
633         if (vp->v_iflag & VI_DOOMED) {
634                 /* Forced close. */
635                 dflags |= FREVOKE | FNONBLOCK;
636         } else if (dsw->d_flags & D_TRACKCLOSE) {
637                 /* Keep device updated on status. */
638         } else if (count_dev(dev) > 1) {
639                 VI_UNLOCK(vp);
640                 dev_relthread(dev, ref);
641                 return (0);
642         }
643         if (count_dev(dev) == 1)
644                 dflags |= FLASTCLOSE;
645         vholdl(vp);
646         VI_UNLOCK(vp);
647         vp_locked = VOP_ISLOCKED(vp);
648         VOP_UNLOCK(vp, 0);
649         KASSERT(dev->si_refcount > 0,
650             ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
651         error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
652         dev_relthread(dev, ref);
653         vn_lock(vp, vp_locked | LK_RETRY);
654         vdrop(vp);
655         return (error);
656 }
657
658 static int
659 devfs_close_f(struct file *fp, struct thread *td)
660 {
661         int error;
662         struct file *fpop;
663
664         /*
665          * NB: td may be NULL if this descriptor is closed due to
666          * garbage collection from a closed UNIX domain socket.
667          */
668         fpop = curthread->td_fpop;
669         curthread->td_fpop = fp;
670         error = vnops.fo_close(fp, td);
671         curthread->td_fpop = fpop;
672
673         /*
674          * The f_cdevpriv cannot be assigned non-NULL value while we
675          * are destroying the file.
676          */
677         if (fp->f_cdevpriv != NULL)
678                 devfs_fpdrop(fp);
679         return (error);
680 }
681
682 static int
683 devfs_getattr(struct vop_getattr_args *ap)
684 {
685         struct vnode *vp = ap->a_vp;
686         struct vattr *vap = ap->a_vap;
687         struct devfs_dirent *de;
688         struct devfs_mount *dmp;
689         struct cdev *dev;
690         struct timeval boottime;
691         int error;
692
693         error = devfs_populate_vp(vp);
694         if (error != 0)
695                 return (error);
696
697         dmp = VFSTODEVFS(vp->v_mount);
698         sx_xunlock(&dmp->dm_lock);
699
700         de = vp->v_data;
701         KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
702         if (vp->v_type == VDIR) {
703                 de = de->de_dir;
704                 KASSERT(de != NULL,
705                     ("Null dir dirent in devfs_getattr vp=%p", vp));
706         }
707         vap->va_uid = de->de_uid;
708         vap->va_gid = de->de_gid;
709         vap->va_mode = de->de_mode;
710         if (vp->v_type == VLNK)
711                 vap->va_size = strlen(de->de_symlink);
712         else if (vp->v_type == VDIR)
713                 vap->va_size = vap->va_bytes = DEV_BSIZE;
714         else
715                 vap->va_size = 0;
716         if (vp->v_type != VDIR)
717                 vap->va_bytes = 0;
718         vap->va_blocksize = DEV_BSIZE;
719         vap->va_type = vp->v_type;
720
721         getboottime(&boottime);
722 #define fix(aa)                                                 \
723         do {                                                    \
724                 if ((aa).tv_sec <= 3600) {                      \
725                         (aa).tv_sec = boottime.tv_sec;          \
726                         (aa).tv_nsec = boottime.tv_usec * 1000; \
727                 }                                               \
728         } while (0)
729
730         if (vp->v_type != VCHR)  {
731                 fix(de->de_atime);
732                 vap->va_atime = de->de_atime;
733                 fix(de->de_mtime);
734                 vap->va_mtime = de->de_mtime;
735                 fix(de->de_ctime);
736                 vap->va_ctime = de->de_ctime;
737         } else {
738                 dev = vp->v_rdev;
739                 fix(dev->si_atime);
740                 vap->va_atime = dev->si_atime;
741                 fix(dev->si_mtime);
742                 vap->va_mtime = dev->si_mtime;
743                 fix(dev->si_ctime);
744                 vap->va_ctime = dev->si_ctime;
745
746                 vap->va_rdev = cdev2priv(dev)->cdp_inode;
747         }
748         vap->va_gen = 0;
749         vap->va_flags = 0;
750         vap->va_filerev = 0;
751         vap->va_nlink = de->de_links;
752         vap->va_fileid = de->de_inode;
753
754         return (error);
755 }
756
757 /* ARGSUSED */
758 static int
759 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
760 {
761         struct file *fpop;
762         int error;
763
764         fpop = td->td_fpop;
765         td->td_fpop = fp;
766         error = vnops.fo_ioctl(fp, com, data, cred, td);
767         td->td_fpop = fpop;
768         return (error);
769 }
770
771 void *
772 fiodgname_buf_get_ptr(void *fgnp, u_long com)
773 {
774         union {
775                 struct fiodgname_arg    fgn;
776 #ifdef COMPAT_FREEBSD32
777                 struct fiodgname_arg32  fgn32;
778 #endif
779         } *fgnup;
780
781         fgnup = fgnp;
782         switch (com) {
783         case FIODGNAME:
784                 return (fgnup->fgn.buf);
785 #ifdef COMPAT_FREEBSD32
786         case FIODGNAME_32:
787                 return ((void *)(uintptr_t)fgnup->fgn32.buf);
788 #endif
789         default:
790                 panic("Unhandled ioctl command %ld", com);
791         }
792 }
793
794 static int
795 devfs_ioctl(struct vop_ioctl_args *ap)
796 {
797         struct fiodgname_arg *fgn;
798         struct vnode *vpold, *vp;
799         struct cdevsw *dsw;
800         struct thread *td;
801         struct cdev *dev;
802         int error, ref, i;
803         const char *p;
804         u_long com;
805
806         vp = ap->a_vp;
807         com = ap->a_command;
808         td = ap->a_td;
809
810         dsw = devvn_refthread(vp, &dev, &ref);
811         if (dsw == NULL)
812                 return (ENXIO);
813         KASSERT(dev->si_refcount > 0,
814             ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
815
816         switch (com) {
817         case FIODTYPE:
818                 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
819                 error = 0;
820                 break;
821         case FIODGNAME:
822 #ifdef  COMPAT_FREEBSD32
823         case FIODGNAME_32:
824 #endif
825                 fgn = ap->a_data;
826                 p = devtoname(dev);
827                 i = strlen(p) + 1;
828                 if (i > fgn->len)
829                         error = EINVAL;
830                 else
831                         error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
832                 break;
833         default:
834                 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
835         }
836
837         dev_relthread(dev, ref);
838         if (error == ENOIOCTL)
839                 error = ENOTTY;
840
841         if (error == 0 && com == TIOCSCTTY) {
842                 /* Do nothing if reassigning same control tty */
843                 sx_slock(&proctree_lock);
844                 if (td->td_proc->p_session->s_ttyvp == vp) {
845                         sx_sunlock(&proctree_lock);
846                         return (0);
847                 }
848
849                 vpold = td->td_proc->p_session->s_ttyvp;
850                 VREF(vp);
851                 SESS_LOCK(td->td_proc->p_session);
852                 td->td_proc->p_session->s_ttyvp = vp;
853                 td->td_proc->p_session->s_ttydp = cdev2priv(dev);
854                 SESS_UNLOCK(td->td_proc->p_session);
855
856                 sx_sunlock(&proctree_lock);
857
858                 /* Get rid of reference to old control tty */
859                 if (vpold)
860                         vrele(vpold);
861         }
862         return (error);
863 }
864
865 /* ARGSUSED */
866 static int
867 devfs_kqfilter_f(struct file *fp, struct knote *kn)
868 {
869         struct cdev *dev;
870         struct cdevsw *dsw;
871         int error, ref;
872         struct file *fpop;
873         struct thread *td;
874
875         td = curthread;
876         fpop = td->td_fpop;
877         error = devfs_fp_check(fp, &dev, &dsw, &ref);
878         if (error)
879                 return (error);
880         error = dsw->d_kqfilter(dev, kn);
881         td->td_fpop = fpop;
882         dev_relthread(dev, ref);
883         return (error);
884 }
885
886 static inline int
887 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
888 {
889         struct cdev_priv *cdp;
890         struct ucred *dcr;
891         struct proc *p;
892         int error;
893
894         cdp = de->de_cdp;
895         if (cdp == NULL)
896                 return (0);
897         dcr = cdp->cdp_c.si_cred;
898         if (dcr == NULL)
899                 return (0);
900
901         error = prison_check(td->td_ucred, dcr);
902         if (error == 0)
903                 return (0);
904         /* We do, however, allow access to the controlling terminal */
905         p = td->td_proc;
906         PROC_LOCK(p);
907         if (!(p->p_flag & P_CONTROLT)) {
908                 PROC_UNLOCK(p);
909                 return (error);
910         }
911         if (p->p_session->s_ttydp == cdp)
912                 error = 0;
913         PROC_UNLOCK(p);
914         return (error);
915 }
916
917 static int
918 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
919 {
920         struct componentname *cnp;
921         struct vnode *dvp, **vpp;
922         struct thread *td;
923         struct devfs_dirent *de, *dd;
924         struct devfs_dirent **dde;
925         struct devfs_mount *dmp;
926         struct mount *mp;
927         struct cdev *cdev;
928         int error, flags, nameiop, dvplocked;
929         char specname[SPECNAMELEN + 1], *pname;
930
931         cnp = ap->a_cnp;
932         vpp = ap->a_vpp;
933         dvp = ap->a_dvp;
934         pname = cnp->cn_nameptr;
935         td = cnp->cn_thread;
936         flags = cnp->cn_flags;
937         nameiop = cnp->cn_nameiop;
938         mp = dvp->v_mount;
939         dmp = VFSTODEVFS(mp);
940         dd = dvp->v_data;
941         *vpp = NULLVP;
942
943         if ((flags & ISLASTCN) && nameiop == RENAME)
944                 return (EOPNOTSUPP);
945
946         if (dvp->v_type != VDIR)
947                 return (ENOTDIR);
948
949         if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
950                 return (EIO);
951
952         error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
953         if (error)
954                 return (error);
955
956         if (cnp->cn_namelen == 1 && *pname == '.') {
957                 if ((flags & ISLASTCN) && nameiop != LOOKUP)
958                         return (EINVAL);
959                 *vpp = dvp;
960                 VREF(dvp);
961                 return (0);
962         }
963
964         if (flags & ISDOTDOT) {
965                 if ((flags & ISLASTCN) && nameiop != LOOKUP)
966                         return (EINVAL);
967                 de = devfs_parent_dirent(dd);
968                 if (de == NULL)
969                         return (ENOENT);
970                 dvplocked = VOP_ISLOCKED(dvp);
971                 VOP_UNLOCK(dvp, 0);
972                 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
973                     vpp);
974                 *dm_unlock = 0;
975                 vn_lock(dvp, dvplocked | LK_RETRY);
976                 return (error);
977         }
978
979         dd = dvp->v_data;
980         de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
981         while (de == NULL) {    /* While(...) so we can use break */
982
983                 if (nameiop == DELETE)
984                         return (ENOENT);
985
986                 /*
987                  * OK, we didn't have an entry for the name we were asked for
988                  * so we try to see if anybody can create it on demand.
989                  */
990                 pname = devfs_fqpn(specname, dmp, dd, cnp);
991                 if (pname == NULL)
992                         break;
993
994                 cdev = NULL;
995                 DEVFS_DMP_HOLD(dmp);
996                 sx_xunlock(&dmp->dm_lock);
997                 sx_slock(&clone_drain_lock);
998                 EVENTHANDLER_INVOKE(dev_clone,
999                     td->td_ucred, pname, strlen(pname), &cdev);
1000                 sx_sunlock(&clone_drain_lock);
1001
1002                 if (cdev == NULL)
1003                         sx_xlock(&dmp->dm_lock);
1004                 else if (devfs_populate_vp(dvp) != 0) {
1005                         *dm_unlock = 0;
1006                         sx_xlock(&dmp->dm_lock);
1007                         if (DEVFS_DMP_DROP(dmp)) {
1008                                 sx_xunlock(&dmp->dm_lock);
1009                                 devfs_unmount_final(dmp);
1010                         } else
1011                                 sx_xunlock(&dmp->dm_lock);
1012                         dev_rel(cdev);
1013                         return (ENOENT);
1014                 }
1015                 if (DEVFS_DMP_DROP(dmp)) {
1016                         *dm_unlock = 0;
1017                         sx_xunlock(&dmp->dm_lock);
1018                         devfs_unmount_final(dmp);
1019                         if (cdev != NULL)
1020                                 dev_rel(cdev);
1021                         return (ENOENT);
1022                 }
1023
1024                 if (cdev == NULL)
1025                         break;
1026
1027                 dev_lock();
1028                 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1029                 if (dde != NULL && *dde != NULL)
1030                         de = *dde;
1031                 dev_unlock();
1032                 dev_rel(cdev);
1033                 break;
1034         }
1035
1036         if (de == NULL || de->de_flags & DE_WHITEOUT) {
1037                 if ((nameiop == CREATE || nameiop == RENAME) &&
1038                     (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1039                         cnp->cn_flags |= SAVENAME;
1040                         return (EJUSTRETURN);
1041                 }
1042                 return (ENOENT);
1043         }
1044
1045         if (devfs_prison_check(de, td))
1046                 return (ENOENT);
1047
1048         if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1049                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1050                 if (error)
1051                         return (error);
1052                 if (*vpp == dvp) {
1053                         VREF(dvp);
1054                         *vpp = dvp;
1055                         return (0);
1056                 }
1057         }
1058         error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1059         *dm_unlock = 0;
1060         return (error);
1061 }
1062
1063 static int
1064 devfs_lookup(struct vop_lookup_args *ap)
1065 {
1066         int j;
1067         struct devfs_mount *dmp;
1068         int dm_unlock;
1069
1070         if (devfs_populate_vp(ap->a_dvp) != 0)
1071                 return (ENOTDIR);
1072
1073         dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1074         dm_unlock = 1;
1075         j = devfs_lookupx(ap, &dm_unlock);
1076         if (dm_unlock == 1)
1077                 sx_xunlock(&dmp->dm_lock);
1078         return (j);
1079 }
1080
1081 static int
1082 devfs_mknod(struct vop_mknod_args *ap)
1083 {
1084         struct componentname *cnp;
1085         struct vnode *dvp, **vpp;
1086         struct devfs_dirent *dd, *de;
1087         struct devfs_mount *dmp;
1088         int error;
1089
1090         /*
1091          * The only type of node we should be creating here is a
1092          * character device, for anything else return EOPNOTSUPP.
1093          */
1094         if (ap->a_vap->va_type != VCHR)
1095                 return (EOPNOTSUPP);
1096         dvp = ap->a_dvp;
1097         dmp = VFSTODEVFS(dvp->v_mount);
1098
1099         cnp = ap->a_cnp;
1100         vpp = ap->a_vpp;
1101         dd = dvp->v_data;
1102
1103         error = ENOENT;
1104         sx_xlock(&dmp->dm_lock);
1105         TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1106                 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1107                         continue;
1108                 if (de->de_dirent->d_type == DT_CHR &&
1109                     (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1110                         continue;
1111                 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1112                     de->de_dirent->d_namlen) != 0)
1113                         continue;
1114                 if (de->de_flags & DE_WHITEOUT)
1115                         break;
1116                 goto notfound;
1117         }
1118         if (de == NULL)
1119                 goto notfound;
1120         de->de_flags &= ~DE_WHITEOUT;
1121         error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1122         return (error);
1123 notfound:
1124         sx_xunlock(&dmp->dm_lock);
1125         return (error);
1126 }
1127
1128 /* ARGSUSED */
1129 static int
1130 devfs_open(struct vop_open_args *ap)
1131 {
1132         struct thread *td = ap->a_td;
1133         struct vnode *vp = ap->a_vp;
1134         struct cdev *dev = vp->v_rdev;
1135         struct file *fp = ap->a_fp;
1136         int error, ref, vlocked;
1137         struct cdevsw *dsw;
1138         struct file *fpop;
1139         struct mtx *mtxp;
1140
1141         if (vp->v_type == VBLK)
1142                 return (ENXIO);
1143
1144         if (dev == NULL)
1145                 return (ENXIO);
1146
1147         /* Make this field valid before any I/O in d_open. */
1148         if (dev->si_iosize_max == 0)
1149                 dev->si_iosize_max = DFLTPHYS;
1150
1151         dsw = dev_refthread(dev, &ref);
1152         if (dsw == NULL)
1153                 return (ENXIO);
1154         if (fp == NULL && dsw->d_fdopen != NULL) {
1155                 dev_relthread(dev, ref);
1156                 return (ENXIO);
1157         }
1158
1159         vlocked = VOP_ISLOCKED(vp);
1160         VOP_UNLOCK(vp, 0);
1161
1162         fpop = td->td_fpop;
1163         td->td_fpop = fp;
1164         if (fp != NULL) {
1165                 fp->f_data = dev;
1166                 fp->f_vnode = vp;
1167         }
1168         if (dsw->d_fdopen != NULL)
1169                 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1170         else
1171                 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1172         /* Clean up any cdevpriv upon error. */
1173         if (error != 0)
1174                 devfs_clear_cdevpriv();
1175         td->td_fpop = fpop;
1176
1177         vn_lock(vp, vlocked | LK_RETRY);
1178         dev_relthread(dev, ref);
1179         if (error != 0) {
1180                 if (error == ERESTART)
1181                         error = EINTR;
1182                 return (error);
1183         }
1184
1185 #if 0   /* /dev/console */
1186         KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1187 #else
1188         if (fp == NULL)
1189                 return (error);
1190 #endif
1191         if (fp->f_ops == &badfileops)
1192                 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1193         mtxp = mtx_pool_find(mtxpool_sleep, fp);
1194
1195         /*
1196          * Hint to the dofilewrite() to not force the buffer draining
1197          * on the writer to the file.  Most likely, the write would
1198          * not need normal buffers.
1199          */
1200         mtx_lock(mtxp);
1201         fp->f_vnread_flags |= FDEVFS_VNODE;
1202         mtx_unlock(mtxp);
1203         return (error);
1204 }
1205
1206 static int
1207 devfs_pathconf(struct vop_pathconf_args *ap)
1208 {
1209
1210         switch (ap->a_name) {
1211         case _PC_FILESIZEBITS:
1212                 *ap->a_retval = 64;
1213                 return (0);
1214         case _PC_NAME_MAX:
1215                 *ap->a_retval = NAME_MAX;
1216                 return (0);
1217         case _PC_LINK_MAX:
1218                 *ap->a_retval = INT_MAX;
1219                 return (0);
1220         case _PC_SYMLINK_MAX:
1221                 *ap->a_retval = MAXPATHLEN;
1222                 return (0);
1223         case _PC_MAX_CANON:
1224                 if (ap->a_vp->v_vflag & VV_ISTTY) {
1225                         *ap->a_retval = MAX_CANON;
1226                         return (0);
1227                 }
1228                 return (EINVAL);
1229         case _PC_MAX_INPUT:
1230                 if (ap->a_vp->v_vflag & VV_ISTTY) {
1231                         *ap->a_retval = MAX_INPUT;
1232                         return (0);
1233                 }
1234                 return (EINVAL);
1235         case _PC_VDISABLE:
1236                 if (ap->a_vp->v_vflag & VV_ISTTY) {
1237                         *ap->a_retval = _POSIX_VDISABLE;
1238                         return (0);
1239                 }
1240                 return (EINVAL);
1241         case _PC_MAC_PRESENT:
1242 #ifdef MAC
1243                 /*
1244                  * If MAC is enabled, devfs automatically supports
1245                  * trivial non-persistant label storage.
1246                  */
1247                 *ap->a_retval = 1;
1248 #else
1249                 *ap->a_retval = 0;
1250 #endif
1251                 return (0);
1252         case _PC_CHOWN_RESTRICTED:
1253                 *ap->a_retval = 1;
1254                 return (0);
1255         default:
1256                 return (vop_stdpathconf(ap));
1257         }
1258         /* NOTREACHED */
1259 }
1260
1261 /* ARGSUSED */
1262 static int
1263 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1264 {
1265         struct cdev *dev;
1266         struct cdevsw *dsw;
1267         int error, ref;
1268         struct file *fpop;
1269
1270         fpop = td->td_fpop;
1271         error = devfs_fp_check(fp, &dev, &dsw, &ref);
1272         if (error != 0) {
1273                 error = vnops.fo_poll(fp, events, cred, td);
1274                 return (error);
1275         }
1276         error = dsw->d_poll(dev, events, td);
1277         td->td_fpop = fpop;
1278         dev_relthread(dev, ref);
1279         return(error);
1280 }
1281
1282 /*
1283  * Print out the contents of a special device vnode.
1284  */
1285 static int
1286 devfs_print(struct vop_print_args *ap)
1287 {
1288
1289         printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1290         return (0);
1291 }
1292
1293 static int
1294 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1295     int flags, struct thread *td)
1296 {
1297         struct cdev *dev;
1298         int ioflag, error, ref;
1299         ssize_t resid;
1300         struct cdevsw *dsw;
1301         struct file *fpop;
1302
1303         if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1304                 return (EINVAL);
1305         fpop = td->td_fpop;
1306         error = devfs_fp_check(fp, &dev, &dsw, &ref);
1307         if (error != 0) {
1308                 error = vnops.fo_read(fp, uio, cred, flags, td);
1309                 return (error);
1310         }
1311         resid = uio->uio_resid;
1312         ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1313         if (ioflag & O_DIRECT)
1314                 ioflag |= IO_DIRECT;
1315
1316         foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1317         error = dsw->d_read(dev, uio, ioflag);
1318         if (uio->uio_resid != resid || (error == 0 && resid != 0))
1319                 devfs_timestamp(&dev->si_atime);
1320         td->td_fpop = fpop;
1321         dev_relthread(dev, ref);
1322
1323         foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1324         return (error);
1325 }
1326
1327 static int
1328 devfs_readdir(struct vop_readdir_args *ap)
1329 {
1330         int error;
1331         struct uio *uio;
1332         struct dirent *dp;
1333         struct devfs_dirent *dd;
1334         struct devfs_dirent *de;
1335         struct devfs_mount *dmp;
1336         off_t off;
1337         int *tmp_ncookies = NULL;
1338
1339         if (ap->a_vp->v_type != VDIR)
1340                 return (ENOTDIR);
1341
1342         uio = ap->a_uio;
1343         if (uio->uio_offset < 0)
1344                 return (EINVAL);
1345
1346         /*
1347          * XXX: This is a temporary hack to get around this filesystem not
1348          * supporting cookies. We store the location of the ncookies pointer
1349          * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1350          * and set the number of cookies to 0. We then set the pointer to
1351          * NULL so that vfs_read_dirent doesn't try to call realloc() on 
1352          * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1353          * pointer to its original location before returning to the caller.
1354          */
1355         if (ap->a_ncookies != NULL) {
1356                 tmp_ncookies = ap->a_ncookies;
1357                 *ap->a_ncookies = 0;
1358                 ap->a_ncookies = NULL;
1359         }
1360
1361         dmp = VFSTODEVFS(ap->a_vp->v_mount);
1362         if (devfs_populate_vp(ap->a_vp) != 0) {
1363                 if (tmp_ncookies != NULL)
1364                         ap->a_ncookies = tmp_ncookies;
1365                 return (EIO);
1366         }
1367         error = 0;
1368         de = ap->a_vp->v_data;
1369         off = 0;
1370         TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1371                 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1372                 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1373                         continue;
1374                 if (devfs_prison_check(dd, uio->uio_td))
1375                         continue;
1376                 if (dd->de_dirent->d_type == DT_DIR)
1377                         de = dd->de_dir;
1378                 else
1379                         de = dd;
1380                 dp = dd->de_dirent;
1381                 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1382                 if (dp->d_reclen > uio->uio_resid)
1383                         break;
1384                 dp->d_fileno = de->de_inode;
1385                 /* NOTE: d_off is the offset for the *next* entry. */
1386                 dp->d_off = off + dp->d_reclen;
1387                 if (off >= uio->uio_offset) {
1388                         error = vfs_read_dirent(ap, dp, off);
1389                         if (error)
1390                                 break;
1391                 }
1392                 off += dp->d_reclen;
1393         }
1394         sx_xunlock(&dmp->dm_lock);
1395         uio->uio_offset = off;
1396
1397         /*
1398          * Restore ap->a_ncookies if it wasn't originally NULL in the first
1399          * place.
1400          */
1401         if (tmp_ncookies != NULL)
1402                 ap->a_ncookies = tmp_ncookies;
1403
1404         return (error);
1405 }
1406
1407 static int
1408 devfs_readlink(struct vop_readlink_args *ap)
1409 {
1410         struct devfs_dirent *de;
1411
1412         de = ap->a_vp->v_data;
1413         return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1414 }
1415
1416 static int
1417 devfs_reclaim(struct vop_reclaim_args *ap)
1418 {
1419         struct vnode *vp;
1420         struct devfs_dirent *de;
1421
1422         vp = ap->a_vp;
1423         mtx_lock(&devfs_de_interlock);
1424         de = vp->v_data;
1425         if (de != NULL) {
1426                 de->de_vnode = NULL;
1427                 vp->v_data = NULL;
1428         }
1429         mtx_unlock(&devfs_de_interlock);
1430         vnode_destroy_vobject(vp);
1431         return (0);
1432 }
1433
1434 static int
1435 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1436 {
1437         struct vnode *vp;
1438         struct cdev *dev;
1439
1440         vp = ap->a_vp;
1441         MPASS(vp->v_type == VCHR);
1442
1443         devfs_reclaim(ap);
1444
1445         VI_LOCK(vp);
1446         dev_lock();
1447         dev = vp->v_rdev;
1448         vp->v_rdev = NULL;
1449         if (dev != NULL)
1450                 dev->si_usecount -= vp->v_usecount;
1451         dev_unlock();
1452         VI_UNLOCK(vp);
1453         if (dev != NULL)
1454                 dev_rel(dev);
1455         return (0);
1456 }
1457
1458 static int
1459 devfs_remove(struct vop_remove_args *ap)
1460 {
1461         struct vnode *dvp = ap->a_dvp;
1462         struct vnode *vp = ap->a_vp;
1463         struct devfs_dirent *dd;
1464         struct devfs_dirent *de, *de_covered;
1465         struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1466
1467         ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1468         ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1469
1470         sx_xlock(&dmp->dm_lock);
1471         dd = ap->a_dvp->v_data;
1472         de = vp->v_data;
1473         if (de->de_cdp == NULL) {
1474                 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1475                 if (de->de_dirent->d_type == DT_LNK) {
1476                         de_covered = devfs_find(dd, de->de_dirent->d_name,
1477                             de->de_dirent->d_namlen, 0);
1478                         if (de_covered != NULL)
1479                                 de_covered->de_flags &= ~DE_COVERED;
1480                 }
1481                 /* We need to unlock dvp because devfs_delete() may lock it. */
1482                 VOP_UNLOCK(vp, 0);
1483                 if (dvp != vp)
1484                         VOP_UNLOCK(dvp, 0);
1485                 devfs_delete(dmp, de, 0);
1486                 sx_xunlock(&dmp->dm_lock);
1487                 if (dvp != vp)
1488                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1489                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1490         } else {
1491                 de->de_flags |= DE_WHITEOUT;
1492                 sx_xunlock(&dmp->dm_lock);
1493         }
1494         return (0);
1495 }
1496
1497 /*
1498  * Revoke is called on a tty when a terminal session ends.  The vnode
1499  * is orphaned by setting v_op to deadfs so we need to let go of it
1500  * as well so that we create a new one next time around.
1501  *
1502  */
1503 static int
1504 devfs_revoke(struct vop_revoke_args *ap)
1505 {
1506         struct vnode *vp = ap->a_vp, *vp2;
1507         struct cdev *dev;
1508         struct cdev_priv *cdp;
1509         struct devfs_dirent *de;
1510         u_int i;
1511
1512         KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1513
1514         dev = vp->v_rdev;
1515         cdp = cdev2priv(dev);
1516  
1517         dev_lock();
1518         cdp->cdp_inuse++;
1519         dev_unlock();
1520
1521         vhold(vp);
1522         vgone(vp);
1523         vdrop(vp);
1524
1525         VOP_UNLOCK(vp,0);
1526  loop:
1527         for (;;) {
1528                 mtx_lock(&devfs_de_interlock);
1529                 dev_lock();
1530                 vp2 = NULL;
1531                 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1532                         de = cdp->cdp_dirents[i];
1533                         if (de == NULL)
1534                                 continue;
1535
1536                         vp2 = de->de_vnode;
1537                         if (vp2 != NULL) {
1538                                 dev_unlock();
1539                                 VI_LOCK(vp2);
1540                                 mtx_unlock(&devfs_de_interlock);
1541                                 if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1542                                     curthread))
1543                                         goto loop;
1544                                 vhold(vp2);
1545                                 vgone(vp2);
1546                                 vdrop(vp2);
1547                                 vput(vp2);
1548                                 break;
1549                         } 
1550                 }
1551                 if (vp2 != NULL) {
1552                         continue;
1553                 }
1554                 dev_unlock();
1555                 mtx_unlock(&devfs_de_interlock);
1556                 break;
1557         }
1558         dev_lock();
1559         cdp->cdp_inuse--;
1560         if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1561                 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1562                 dev_unlock();
1563                 dev_rel(&cdp->cdp_c);
1564         } else
1565                 dev_unlock();
1566
1567         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1568         return (0);
1569 }
1570
1571 static int
1572 devfs_rioctl(struct vop_ioctl_args *ap)
1573 {
1574         struct vnode *vp;
1575         struct devfs_mount *dmp;
1576         int error;
1577
1578         vp = ap->a_vp;
1579         vn_lock(vp, LK_SHARED | LK_RETRY);
1580         if (vp->v_iflag & VI_DOOMED) {
1581                 VOP_UNLOCK(vp, 0);
1582                 return (EBADF);
1583         }
1584         dmp = VFSTODEVFS(vp->v_mount);
1585         sx_xlock(&dmp->dm_lock);
1586         VOP_UNLOCK(vp, 0);
1587         DEVFS_DMP_HOLD(dmp);
1588         devfs_populate(dmp);
1589         if (DEVFS_DMP_DROP(dmp)) {
1590                 sx_xunlock(&dmp->dm_lock);
1591                 devfs_unmount_final(dmp);
1592                 return (ENOENT);
1593         }
1594         error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1595         sx_xunlock(&dmp->dm_lock);
1596         return (error);
1597 }
1598
1599 static int
1600 devfs_rread(struct vop_read_args *ap)
1601 {
1602
1603         if (ap->a_vp->v_type != VDIR)
1604                 return (EINVAL);
1605         return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1606 }
1607
1608 static int
1609 devfs_setattr(struct vop_setattr_args *ap)
1610 {
1611         struct devfs_dirent *de;
1612         struct vattr *vap;
1613         struct vnode *vp;
1614         struct thread *td;
1615         int c, error;
1616         uid_t uid;
1617         gid_t gid;
1618
1619         vap = ap->a_vap;
1620         vp = ap->a_vp;
1621         td = curthread;
1622         if ((vap->va_type != VNON) ||
1623             (vap->va_nlink != VNOVAL) ||
1624             (vap->va_fsid != VNOVAL) ||
1625             (vap->va_fileid != VNOVAL) ||
1626             (vap->va_blocksize != VNOVAL) ||
1627             (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1628             (vap->va_rdev != VNOVAL) ||
1629             ((int)vap->va_bytes != VNOVAL) ||
1630             (vap->va_gen != VNOVAL)) {
1631                 return (EINVAL);
1632         }
1633
1634         error = devfs_populate_vp(vp);
1635         if (error != 0)
1636                 return (error);
1637
1638         de = vp->v_data;
1639         if (vp->v_type == VDIR)
1640                 de = de->de_dir;
1641
1642         c = 0;
1643         if (vap->va_uid == (uid_t)VNOVAL)
1644                 uid = de->de_uid;
1645         else
1646                 uid = vap->va_uid;
1647         if (vap->va_gid == (gid_t)VNOVAL)
1648                 gid = de->de_gid;
1649         else
1650                 gid = vap->va_gid;
1651         if (uid != de->de_uid || gid != de->de_gid) {
1652                 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1653                     (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1654                         error = priv_check(td, PRIV_VFS_CHOWN);
1655                         if (error != 0)
1656                                 goto ret;
1657                 }
1658                 de->de_uid = uid;
1659                 de->de_gid = gid;
1660                 c = 1;
1661         }
1662
1663         if (vap->va_mode != (mode_t)VNOVAL) {
1664                 if (ap->a_cred->cr_uid != de->de_uid) {
1665                         error = priv_check(td, PRIV_VFS_ADMIN);
1666                         if (error != 0)
1667                                 goto ret;
1668                 }
1669                 de->de_mode = vap->va_mode;
1670                 c = 1;
1671         }
1672
1673         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1674                 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1675                 if (error != 0)
1676                         goto ret;
1677                 if (vap->va_atime.tv_sec != VNOVAL) {
1678                         if (vp->v_type == VCHR)
1679                                 vp->v_rdev->si_atime = vap->va_atime;
1680                         else
1681                                 de->de_atime = vap->va_atime;
1682                 }
1683                 if (vap->va_mtime.tv_sec != VNOVAL) {
1684                         if (vp->v_type == VCHR)
1685                                 vp->v_rdev->si_mtime = vap->va_mtime;
1686                         else
1687                                 de->de_mtime = vap->va_mtime;
1688                 }
1689                 c = 1;
1690         }
1691
1692         if (c) {
1693                 if (vp->v_type == VCHR)
1694                         vfs_timestamp(&vp->v_rdev->si_ctime);
1695                 else
1696                         vfs_timestamp(&de->de_mtime);
1697         }
1698
1699 ret:
1700         sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1701         return (error);
1702 }
1703
1704 #ifdef MAC
1705 static int
1706 devfs_setlabel(struct vop_setlabel_args *ap)
1707 {
1708         struct vnode *vp;
1709         struct devfs_dirent *de;
1710
1711         vp = ap->a_vp;
1712         de = vp->v_data;
1713
1714         mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1715         mac_devfs_update(vp->v_mount, de, vp);
1716
1717         return (0);
1718 }
1719 #endif
1720
1721 static int
1722 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1723 {
1724
1725         return (vnops.fo_stat(fp, sb, cred, td));
1726 }
1727
1728 static int
1729 devfs_symlink(struct vop_symlink_args *ap)
1730 {
1731         int i, error;
1732         struct devfs_dirent *dd;
1733         struct devfs_dirent *de, *de_covered, *de_dotdot;
1734         struct devfs_mount *dmp;
1735
1736         error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1737         if (error)
1738                 return(error);
1739         dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1740         if (devfs_populate_vp(ap->a_dvp) != 0)
1741                 return (ENOENT);
1742
1743         dd = ap->a_dvp->v_data;
1744         de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1745         de->de_flags = DE_USER;
1746         de->de_uid = 0;
1747         de->de_gid = 0;
1748         de->de_mode = 0755;
1749         de->de_inode = alloc_unr(devfs_inos);
1750         de->de_dir = dd;
1751         de->de_dirent->d_type = DT_LNK;
1752         i = strlen(ap->a_target) + 1;
1753         de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1754         bcopy(ap->a_target, de->de_symlink, i);
1755 #ifdef MAC
1756         mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1757 #endif
1758         de_covered = devfs_find(dd, de->de_dirent->d_name,
1759             de->de_dirent->d_namlen, 0);
1760         if (de_covered != NULL) {
1761                 if ((de_covered->de_flags & DE_USER) != 0) {
1762                         devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1763                         sx_xunlock(&dmp->dm_lock);
1764                         return (EEXIST);
1765                 }
1766                 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1767                     ("devfs_symlink: entry %p already covered", de_covered));
1768                 de_covered->de_flags |= DE_COVERED;
1769         }
1770
1771         de_dotdot = TAILQ_FIRST(&dd->de_dlist);         /* "." */
1772         de_dotdot = TAILQ_NEXT(de_dotdot, de_list);     /* ".." */
1773         TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1774         devfs_dir_ref_de(dmp, dd);
1775         devfs_rules_apply(dmp, de);
1776
1777         return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1778 }
1779
1780 static int
1781 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1782 {
1783
1784         return (vnops.fo_truncate(fp, length, cred, td));
1785 }
1786
1787 static int
1788 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1789     int flags, struct thread *td)
1790 {
1791         struct cdev *dev;
1792         int error, ioflag, ref;
1793         ssize_t resid;
1794         struct cdevsw *dsw;
1795         struct file *fpop;
1796
1797         if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1798                 return (EINVAL);
1799         fpop = td->td_fpop;
1800         error = devfs_fp_check(fp, &dev, &dsw, &ref);
1801         if (error != 0) {
1802                 error = vnops.fo_write(fp, uio, cred, flags, td);
1803                 return (error);
1804         }
1805         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1806         ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1807         if (ioflag & O_DIRECT)
1808                 ioflag |= IO_DIRECT;
1809         foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1810
1811         resid = uio->uio_resid;
1812
1813         error = dsw->d_write(dev, uio, ioflag);
1814         if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1815                 devfs_timestamp(&dev->si_ctime);
1816                 dev->si_mtime = dev->si_ctime;
1817         }
1818         td->td_fpop = fpop;
1819         dev_relthread(dev, ref);
1820
1821         foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1822         return (error);
1823 }
1824
1825 static int
1826 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1827     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1828     struct thread *td)
1829 {
1830         struct cdev *dev;
1831         struct cdevsw *dsw;
1832         struct mount *mp;
1833         struct vnode *vp;
1834         struct file *fpop;
1835         vm_object_t object;
1836         vm_prot_t maxprot;
1837         int error, ref;
1838
1839         vp = fp->f_vnode;
1840
1841         /*
1842          * Ensure that file and memory protections are
1843          * compatible.
1844          */
1845         mp = vp->v_mount;
1846         if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1847                 maxprot = VM_PROT_NONE;
1848                 if ((prot & VM_PROT_EXECUTE) != 0)
1849                         return (EACCES);
1850         } else
1851                 maxprot = VM_PROT_EXECUTE;
1852         if ((fp->f_flag & FREAD) != 0)
1853                 maxprot |= VM_PROT_READ;
1854         else if ((prot & VM_PROT_READ) != 0)
1855                 return (EACCES);
1856
1857         /*
1858          * If we are sharing potential changes via MAP_SHARED and we
1859          * are trying to get write permission although we opened it
1860          * without asking for it, bail out.
1861          *
1862          * Note that most character devices always share mappings.
1863          * The one exception is that D_MMAP_ANON devices
1864          * (i.e. /dev/zero) permit private writable mappings.
1865          *
1866          * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1867          * as well as updating maxprot to permit writing for
1868          * D_MMAP_ANON devices rather than doing that here.
1869          */
1870         if ((flags & MAP_SHARED) != 0) {
1871                 if ((fp->f_flag & FWRITE) != 0)
1872                         maxprot |= VM_PROT_WRITE;
1873                 else if ((prot & VM_PROT_WRITE) != 0)
1874                         return (EACCES);
1875         }
1876         maxprot &= cap_maxprot;
1877
1878         fpop = td->td_fpop;
1879         error = devfs_fp_check(fp, &dev, &dsw, &ref);
1880         if (error != 0)
1881                 return (error);
1882
1883         error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1884             &object);
1885         td->td_fpop = fpop;
1886         dev_relthread(dev, ref);
1887         if (error != 0)
1888                 return (error);
1889
1890         error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1891             foff, FALSE, td);
1892         if (error != 0)
1893                 vm_object_deallocate(object);
1894         return (error);
1895 }
1896
1897 dev_t
1898 dev2udev(struct cdev *x)
1899 {
1900         if (x == NULL)
1901                 return (NODEV);
1902         return (cdev2priv(x)->cdp_inode);
1903 }
1904
1905 static struct fileops devfs_ops_f = {
1906         .fo_read =      devfs_read_f,
1907         .fo_write =     devfs_write_f,
1908         .fo_truncate =  devfs_truncate_f,
1909         .fo_ioctl =     devfs_ioctl_f,
1910         .fo_poll =      devfs_poll_f,
1911         .fo_kqfilter =  devfs_kqfilter_f,
1912         .fo_stat =      devfs_stat_f,
1913         .fo_close =     devfs_close_f,
1914         .fo_chmod =     vn_chmod,
1915         .fo_chown =     vn_chown,
1916         .fo_sendfile =  vn_sendfile,
1917         .fo_seek =      vn_seek,
1918         .fo_fill_kinfo = vn_fill_kinfo,
1919         .fo_mmap =      devfs_mmap_f,
1920         .fo_flags =     DFLAG_PASSABLE | DFLAG_SEEKABLE
1921 };
1922
1923 /* Vops for non-CHR vnodes in /dev. */
1924 static struct vop_vector devfs_vnodeops = {
1925         .vop_default =          &default_vnodeops,
1926
1927         .vop_access =           devfs_access,
1928         .vop_getattr =          devfs_getattr,
1929         .vop_ioctl =            devfs_rioctl,
1930         .vop_lookup =           devfs_lookup,
1931         .vop_mknod =            devfs_mknod,
1932         .vop_pathconf =         devfs_pathconf,
1933         .vop_read =             devfs_rread,
1934         .vop_readdir =          devfs_readdir,
1935         .vop_readlink =         devfs_readlink,
1936         .vop_reclaim =          devfs_reclaim,
1937         .vop_remove =           devfs_remove,
1938         .vop_revoke =           devfs_revoke,
1939         .vop_setattr =          devfs_setattr,
1940 #ifdef MAC
1941         .vop_setlabel =         devfs_setlabel,
1942 #endif
1943         .vop_symlink =          devfs_symlink,
1944         .vop_vptocnp =          devfs_vptocnp,
1945 };
1946
1947 /* Vops for VCHR vnodes in /dev. */
1948 static struct vop_vector devfs_specops = {
1949         .vop_default =          &default_vnodeops,
1950
1951         .vop_access =           devfs_access,
1952         .vop_bmap =             VOP_PANIC,
1953         .vop_close =            devfs_close,
1954         .vop_create =           VOP_PANIC,
1955         .vop_fsync =            vop_stdfsync,
1956         .vop_getattr =          devfs_getattr,
1957         .vop_ioctl =            devfs_ioctl,
1958         .vop_link =             VOP_PANIC,
1959         .vop_mkdir =            VOP_PANIC,
1960         .vop_mknod =            VOP_PANIC,
1961         .vop_open =             devfs_open,
1962         .vop_pathconf =         devfs_pathconf,
1963         .vop_poll =             dead_poll,
1964         .vop_print =            devfs_print,
1965         .vop_read =             dead_read,
1966         .vop_readdir =          VOP_PANIC,
1967         .vop_readlink =         VOP_PANIC,
1968         .vop_reallocblks =      VOP_PANIC,
1969         .vop_reclaim =          devfs_reclaim_vchr,
1970         .vop_remove =           devfs_remove,
1971         .vop_rename =           VOP_PANIC,
1972         .vop_revoke =           devfs_revoke,
1973         .vop_rmdir =            VOP_PANIC,
1974         .vop_setattr =          devfs_setattr,
1975 #ifdef MAC
1976         .vop_setlabel =         devfs_setlabel,
1977 #endif
1978         .vop_strategy =         VOP_PANIC,
1979         .vop_symlink =          VOP_PANIC,
1980         .vop_vptocnp =          devfs_vptocnp,
1981         .vop_write =            dead_write,
1982 };
1983
1984 /*
1985  * Our calling convention to the device drivers used to be that we passed
1986  * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_ 
1987  * flags instead since that's what open(), close() and ioctl() takes and
1988  * we don't really want vnode.h in device drivers.
1989  * We solved the source compatibility by redefining some vnode flags to
1990  * be the same as the fcntl ones and by sending down the bitwise OR of
1991  * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
1992  * pulls the rug out under this.
1993  */
1994 CTASSERT(O_NONBLOCK == IO_NDELAY);
1995 CTASSERT(O_FSYNC == IO_SYNC);