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