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