The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/devfs/devfs_vnops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 2bf97085a6e370eea21422243ec772b6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.