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