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