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