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

Cache object: 341873c5b1b159e7c2aee5cb1d677a33


[ 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.