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