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

Cache object: 707e61ae395af76ed1555b0057d11302


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