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