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/ufs/ufs/ufs_lookup.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 /*      $NetBSD: ufs_lookup.c,v 1.55 2004/03/06 06:54:12 yamt Exp $     */
    2 
    3 /*
    4  * Copyright (c) 1989, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * (c) UNIX System Laboratories, Inc.
    7  * All or some portions of this file are derived from material licensed
    8  * to the University of California by American Telephone and Telegraph
    9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   10  * the permission of UNIX System Laboratories, Inc.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)ufs_lookup.c        8.9 (Berkeley) 8/11/94
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: ufs_lookup.c,v 1.55 2004/03/06 06:54:12 yamt Exp $");
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/namei.h>
   45 #include <sys/buf.h>
   46 #include <sys/file.h>
   47 #include <sys/stat.h>
   48 #include <sys/mount.h>
   49 #include <sys/vnode.h>
   50 #include <sys/kernel.h>
   51 
   52 #include <ufs/ufs/inode.h>
   53 #include <ufs/ufs/dir.h>
   54 #include <ufs/ufs/ufsmount.h>
   55 #include <ufs/ufs/ufs_extern.h>
   56 #include <ufs/ufs/ufs_bswap.h>
   57 
   58 #ifdef DIAGNOSTIC
   59 int     dirchk = 1;
   60 #else
   61 int     dirchk = 0;
   62 #endif
   63 
   64 #define FSFMT(vp)   ((vp)->v_mount->mnt_maxsymlinklen <= 0)
   65 
   66 /*
   67  * Convert a component of a pathname into a pointer to a locked inode.
   68  * This is a very central and rather complicated routine.
   69  * If the file system is not maintained in a strict tree hierarchy,
   70  * this can result in a deadlock situation (see comments in code below).
   71  *
   72  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
   73  * on whether the name is to be looked up, created, renamed, or deleted.
   74  * When CREATE, RENAME, or DELETE is specified, information usable in
   75  * creating, renaming, or deleting a directory entry may be calculated.
   76  * If flag has LOCKPARENT or'ed into it and the target of the pathname
   77  * exists, lookup returns both the target and its parent directory locked.
   78  * When creating or renaming and LOCKPARENT is specified, the target may
   79  * not be ".".  When deleting and LOCKPARENT is specified, the target may
   80  * be "."., but the caller must check to ensure it does an vrele and vput
   81  * instead of two vputs.
   82  *
   83  * Overall outline of ufs_lookup:
   84  *
   85  *      check accessibility of directory
   86  *      look for name in cache, if found, then if at end of path
   87  *        and deleting or creating, drop it, else return name
   88  *      search for name in directory, to found or notfound
   89  * notfound:
   90  *      if creating, return locked directory, leaving info on available slots
   91  *      else return error
   92  * found:
   93  *      if at end of path and deleting, return information to allow delete
   94  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
   95  *        inode and return info to allow rewrite
   96  *      if not at end, add name to cache; if at end and neither creating
   97  *        nor deleting, add name to cache
   98  */
   99 int
  100 ufs_lookup(v)
  101         void *v;
  102 {
  103         struct vop_lookup_args /* {
  104                 struct vnode *a_dvp;
  105                 struct vnode **a_vpp;
  106                 struct componentname *a_cnp;
  107         } */ *ap = v;
  108         struct vnode *vdp;              /* vnode for directory being searched */
  109         struct inode *dp;               /* inode for directory being searched */
  110         struct buf *bp;                 /* a buffer of directory entries */
  111         struct direct *ep;              /* the current directory entry */
  112         int entryoffsetinblock;         /* offset of ep in bp's buffer */
  113         enum {NONE, COMPACT, FOUND} slotstatus;
  114         doff_t slotoffset;              /* offset of area with free space */
  115         int slotsize;                   /* size of area at slotoffset */
  116         int slotfreespace;              /* amount of space free in slot */
  117         int slotneeded;                 /* size of the entry we're seeking */
  118         int numdirpasses;               /* strategy for directory search */
  119         doff_t endsearch;               /* offset to end directory search */
  120         doff_t prevoff;                 /* prev entry dp->i_offset */
  121         struct vnode *pdp;              /* saved dp during symlink work */
  122         struct vnode *tdp;              /* returned by VFS_VGET */
  123         doff_t enduseful;               /* pointer past last used dir slot */
  124         u_long bmask;                   /* block offset mask */
  125         int lockparent;                 /* 1 => lockparent flag is set */
  126         int wantparent;                 /* 1 => wantparent or lockparent flag */
  127         int namlen, error;
  128         struct vnode **vpp = ap->a_vpp;
  129         struct componentname *cnp = ap->a_cnp;
  130         struct ucred *cred = cnp->cn_cred;
  131         int flags;
  132         int nameiop = cnp->cn_nameiop;
  133         const int needswap = UFS_MPNEEDSWAP(ap->a_dvp->v_mount);
  134         int dirblksiz = DIRBLKSIZ;
  135         ino_t foundino;
  136         if (UFS_MPISAPPLEUFS(ap->a_dvp->v_mount)) {
  137                 dirblksiz = APPLEUFS_DIRBLKSIZ;
  138         }
  139 
  140         cnp->cn_flags &= ~PDIRUNLOCK;
  141         flags = cnp->cn_flags;
  142 
  143         bp = NULL;
  144         slotoffset = -1;
  145         *vpp = NULL;
  146         vdp = ap->a_dvp;
  147         dp = VTOI(vdp);
  148         lockparent = flags & LOCKPARENT;
  149         wantparent = flags & (LOCKPARENT|WANTPARENT);
  150 
  151 
  152         /*
  153          * Check accessiblity of directory.
  154          */
  155         if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
  156                 return (error);
  157 
  158         if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
  159             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
  160                 return (EROFS);
  161 
  162         /*
  163          * We now have a segment name to search for, and a directory to search.
  164          *
  165          * Before tediously performing a linear scan of the directory,
  166          * check the name cache to see if the directory/name pair
  167          * we are looking for is known already.
  168          */
  169         if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
  170                 return (error);
  171 
  172         /*
  173          * Suppress search for slots unless creating
  174          * file and at end of pathname, in which case
  175          * we watch for a place to put the new file in
  176          * case it doesn't already exist.
  177          */
  178         slotstatus = FOUND;
  179         slotfreespace = slotsize = slotneeded = 0;
  180         if ((nameiop == CREATE || nameiop == RENAME) &&
  181             (flags & ISLASTCN)) {
  182                 slotstatus = NONE;
  183                 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
  184                         cnp->cn_namelen + 3) &~ 3;
  185         }
  186 
  187         /*
  188          * If there is cached information on a previous search of
  189          * this directory, pick up where we last left off.
  190          * We cache only lookups as these are the most common
  191          * and have the greatest payoff. Caching CREATE has little
  192          * benefit as it usually must search the entire directory
  193          * to determine that the entry does not exist. Caching the
  194          * location of the last DELETE or RENAME has not reduced
  195          * profiling time and hence has been removed in the interest
  196          * of simplicity.
  197          */
  198         bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
  199         if (nameiop != LOOKUP || dp->i_diroff == 0 ||
  200             dp->i_diroff >= dp->i_size) {
  201                 entryoffsetinblock = 0;
  202                 dp->i_offset = 0;
  203                 numdirpasses = 1;
  204         } else {
  205                 dp->i_offset = dp->i_diroff;
  206                 if ((entryoffsetinblock = dp->i_offset & bmask) &&
  207                     (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
  208                         return (error);
  209                 numdirpasses = 2;
  210                 nchstats.ncs_2passes++;
  211         }
  212         prevoff = dp->i_offset;
  213         endsearch = roundup(dp->i_size, dirblksiz);
  214         enduseful = 0;
  215 
  216 searchloop:
  217         while (dp->i_offset < endsearch) {
  218                 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
  219                         preempt(1);
  220                 /*
  221                  * If necessary, get the next directory block.
  222                  */
  223                 if ((dp->i_offset & bmask) == 0) {
  224                         if (bp != NULL)
  225                                 brelse(bp);
  226                         error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL,
  227                                              &bp);
  228                         if (error)
  229                                 return (error);
  230                         entryoffsetinblock = 0;
  231                 }
  232                 /*
  233                  * If still looking for a slot, and at a DIRBLKSIZE
  234                  * boundary, have to start looking for free space again.
  235                  */
  236                 if (slotstatus == NONE &&
  237                     (entryoffsetinblock & (dirblksiz - 1)) == 0) {
  238                         slotoffset = -1;
  239                         slotfreespace = 0;
  240                 }
  241                 /*
  242                  * Get pointer to next entry.
  243                  * Full validation checks are slow, so we only check
  244                  * enough to insure forward progress through the
  245                  * directory. Complete checks can be run by patching
  246                  * "dirchk" to be true.
  247                  */
  248                 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
  249                 if (ep->d_reclen == 0 ||
  250                     (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
  251                         int i;
  252 
  253                         ufs_dirbad(dp, dp->i_offset, "mangled entry");
  254                         i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
  255                         dp->i_offset += i;
  256                         entryoffsetinblock += i;
  257                         continue;
  258                 }
  259 
  260                 /*
  261                  * If an appropriate sized slot has not yet been found,
  262                  * check to see if one is available. Also accumulate space
  263                  * in the current block so that we can determine if
  264                  * compaction is viable.
  265                  */
  266                 if (slotstatus != FOUND) {
  267                         int size = ufs_rw16(ep->d_reclen, needswap);
  268 
  269                         if (ep->d_ino != 0)
  270                                 size -= DIRSIZ(FSFMT(vdp), ep, needswap);
  271                         if (size > 0) {
  272                                 if (size >= slotneeded) {
  273                                         slotstatus = FOUND;
  274                                         slotoffset = dp->i_offset;
  275                                         slotsize = ufs_rw16(ep->d_reclen,
  276                                                 needswap);
  277                                 } else if (slotstatus == NONE) {
  278                                         slotfreespace += size;
  279                                         if (slotoffset == -1)
  280                                                 slotoffset = dp->i_offset;
  281                                         if (slotfreespace >= slotneeded) {
  282                                                 slotstatus = COMPACT;
  283                                                 slotsize = dp->i_offset +
  284                                                     ufs_rw16(ep->d_reclen,
  285                                                              needswap)
  286                                                     - slotoffset;
  287                                         }
  288                                 }
  289                         }
  290                 }
  291 
  292                 /*
  293                  * Check for a name match.
  294                  */
  295                 if (ep->d_ino) {
  296 #if (BYTE_ORDER == LITTLE_ENDIAN)
  297                         if (vdp->v_mount->mnt_maxsymlinklen > 0 ||
  298                             needswap != 0)
  299                                 namlen = ep->d_namlen;
  300                         else
  301                                 namlen = ep->d_type;
  302 #else
  303                         if (vdp->v_mount->mnt_maxsymlinklen <= 0
  304                             && needswap != 0) 
  305                                 namlen = ep->d_type;
  306                         else
  307                                 namlen = ep->d_namlen;
  308 #endif
  309                         if (namlen == cnp->cn_namelen &&
  310                             !memcmp(cnp->cn_nameptr, ep->d_name,
  311                                 (unsigned)namlen)) {
  312                                 /*
  313                                  * Save directory entry's inode number and
  314                                  * reclen in ndp->ni_ufs area, and release
  315                                  * directory buffer.
  316                                  */
  317                                 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
  318                                     ep->d_type == DT_WHT) {
  319                                         slotstatus = FOUND;
  320                                         slotoffset = dp->i_offset;
  321                                         slotsize = ufs_rw16(ep->d_reclen,
  322                                             needswap);
  323                                         dp->i_reclen = slotsize;
  324                                         /*
  325                                          * This is used to set dp->i_endoff,
  326                                          * which may be used by ufs_direnter2()
  327                                          * as a length to truncate the
  328                                          * directory to.  Therefore, it must
  329                                          * point past the end of the last
  330                                          * non-empty directory entry.  We don't
  331                                          * know where that is in this case, so
  332                                          * we effectively disable shrinking by
  333                                          * using the existing size of the
  334                                          * directory.
  335                                          *
  336                                          * Note that we wouldn't expect to
  337                                          * shrink the directory while rewriting
  338                                          * an existing entry anyway.
  339                                          */
  340                                         enduseful = endsearch;
  341                                         ap->a_cnp->cn_flags |= ISWHITEOUT;
  342                                         numdirpasses--;
  343                                         goto notfound;
  344                                 }
  345                                 foundino = ufs_rw32(ep->d_ino, needswap);
  346                                 dp->i_reclen = ufs_rw16(ep->d_reclen, needswap);
  347                                 goto found;
  348                         }
  349                 }
  350                 prevoff = dp->i_offset;
  351                 dp->i_offset += ufs_rw16(ep->d_reclen, needswap);
  352                 entryoffsetinblock += ufs_rw16(ep->d_reclen, needswap);
  353                 if (ep->d_ino)
  354                         enduseful = dp->i_offset;
  355         }
  356 notfound:
  357         /*
  358          * If we started in the middle of the directory and failed
  359          * to find our target, we must check the beginning as well.
  360          */
  361         if (numdirpasses == 2) {
  362                 numdirpasses--;
  363                 dp->i_offset = 0;
  364                 endsearch = dp->i_diroff;
  365                 goto searchloop;
  366         }
  367         if (bp != NULL)
  368                 brelse(bp);
  369         /*
  370          * If creating, and at end of pathname and current
  371          * directory has not been removed, then can consider
  372          * allowing file to be created.
  373          */
  374         if ((nameiop == CREATE || nameiop == RENAME ||
  375              (nameiop == DELETE &&
  376               (ap->a_cnp->cn_flags & DOWHITEOUT) &&
  377               (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
  378             (flags & ISLASTCN) && dp->i_ffs_effnlink != 0) {
  379                 /*
  380                  * Access for write is interpreted as allowing
  381                  * creation of files in the directory.
  382                  */
  383                 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
  384                 if (error)
  385                         return (error);
  386                 /*
  387                  * Return an indication of where the new directory
  388                  * entry should be put.  If we didn't find a slot,
  389                  * then set dp->i_count to 0 indicating
  390                  * that the new slot belongs at the end of the
  391                  * directory. If we found a slot, then the new entry
  392                  * can be put in the range from dp->i_offset to
  393                  * dp->i_offset + dp->i_count.
  394                  */
  395                 if (slotstatus == NONE) {
  396                         dp->i_offset = roundup(dp->i_size, dirblksiz);
  397                         dp->i_count = 0;
  398                         enduseful = dp->i_offset;
  399                 } else if (nameiop == DELETE) {
  400                         dp->i_offset = slotoffset;
  401                         if ((dp->i_offset & (dirblksiz - 1)) == 0)
  402                                 dp->i_count = 0;
  403                         else
  404                                 dp->i_count = dp->i_offset - prevoff;
  405                 } else {
  406                         dp->i_offset = slotoffset;
  407                         dp->i_count = slotsize;
  408                         if (enduseful < slotoffset + slotsize)
  409                                 enduseful = slotoffset + slotsize;
  410                 }
  411                 dp->i_endoff = roundup(enduseful, dirblksiz);
  412 #if 0 /* commented out by dbj. none of the on disk fields changed */
  413                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
  414 #endif
  415                 /*
  416                  * We return with the directory locked, so that
  417                  * the parameters we set up above will still be
  418                  * valid if we actually decide to do a direnter().
  419                  * We return ni_vp == NULL to indicate that the entry
  420                  * does not currently exist; we leave a pointer to
  421                  * the (locked) directory inode in ndp->ni_dvp.
  422                  * The pathname buffer is saved so that the name
  423                  * can be obtained later.
  424                  *
  425                  * NB - if the directory is unlocked, then this
  426                  * information cannot be used.
  427                  */
  428                 cnp->cn_flags |= SAVENAME;
  429                 if (!lockparent) {
  430                         VOP_UNLOCK(vdp, 0);
  431                         cnp->cn_flags |= PDIRUNLOCK;
  432                 }
  433                 return (EJUSTRETURN);
  434         }
  435         /*
  436          * Insert name into cache (as non-existent) if appropriate.
  437          */
  438         if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
  439                 cache_enter(vdp, *vpp, cnp);
  440         return (ENOENT);
  441 
  442 found:
  443         if (numdirpasses == 2)
  444                 nchstats.ncs_pass2++;
  445         /*
  446          * Check that directory length properly reflects presence
  447          * of this entry.
  448          */
  449         if (dp->i_offset + DIRSIZ(FSFMT(vdp), ep, needswap) > dp->i_size) {
  450                 ufs_dirbad(dp, dp->i_offset, "i_size too small");
  451                 dp->i_size = dp->i_offset + DIRSIZ(FSFMT(vdp), ep, needswap);
  452                 DIP_ASSIGN(dp, size, dp->i_size);
  453                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
  454         }
  455         brelse(bp);
  456 
  457         /*
  458          * Found component in pathname.
  459          * If the final component of path name, save information
  460          * in the cache as to where the entry was found.
  461          */
  462         if ((flags & ISLASTCN) && nameiop == LOOKUP)
  463                 dp->i_diroff = dp->i_offset &~ (dirblksiz - 1);
  464 
  465         /*
  466          * If deleting, and at end of pathname, return
  467          * parameters which can be used to remove file.
  468          * If the wantparent flag isn't set, we return only
  469          * the directory (in ndp->ni_dvp), otherwise we go
  470          * on and lock the inode, being careful with ".".
  471          */
  472         if (nameiop == DELETE && (flags & ISLASTCN)) {
  473                 /*
  474                  * Write access to directory required to delete files.
  475                  */
  476                 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
  477                 if (error)
  478                         return (error);
  479                 /*
  480                  * Return pointer to current entry in dp->i_offset,
  481                  * and distance past previous entry (if there
  482                  * is a previous entry in this block) in dp->i_count.
  483                  * Save directory inode pointer in ndp->ni_dvp for dirremove().
  484                  */
  485                 if ((dp->i_offset & (dirblksiz - 1)) == 0)
  486                         dp->i_count = 0;
  487                 else
  488                         dp->i_count = dp->i_offset - prevoff;
  489                 if (dp->i_number == foundino) {
  490                         VREF(vdp);
  491                         *vpp = vdp;
  492                         return (0);
  493                 }
  494                 if (flags & ISDOTDOT)
  495                         VOP_UNLOCK(vdp, 0); /* race to get the inode */
  496                 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
  497                 if (flags & ISDOTDOT)
  498                         vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
  499                 if (error)
  500                         return (error);
  501                 /*
  502                  * If directory is "sticky", then user must own
  503                  * the directory, or the file in it, else she
  504                  * may not delete it (unless she's root). This
  505                  * implements append-only directories.
  506                  */
  507                 if ((dp->i_mode & ISVTX) &&
  508                     cred->cr_uid != 0 &&
  509                     cred->cr_uid != dp->i_uid &&
  510                     VTOI(tdp)->i_uid != cred->cr_uid) {
  511                         vput(tdp);
  512                         return (EPERM);
  513                 }
  514                 *vpp = tdp;
  515                 if (!lockparent) {
  516                         VOP_UNLOCK(vdp, 0);
  517                         cnp->cn_flags |= PDIRUNLOCK;
  518                 }
  519                 return (0);
  520         }
  521 
  522         /*
  523          * If rewriting (RENAME), return the inode and the
  524          * information required to rewrite the present directory
  525          * Must get inode of directory entry to verify it's a
  526          * regular file, or empty directory.
  527          */
  528         if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
  529                 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
  530                 if (error)
  531                         return (error);
  532                 /*
  533                  * Careful about locking second inode.
  534                  * This can only occur if the target is ".".
  535                  */
  536                 if (dp->i_number == foundino)
  537                         return (EISDIR);
  538                 if (flags & ISDOTDOT)
  539                         VOP_UNLOCK(vdp, 0); /* race to get the inode */
  540                 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
  541                 if (flags & ISDOTDOT)
  542                         vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
  543                 if (error)
  544                         return (error);
  545                 *vpp = tdp;
  546                 cnp->cn_flags |= SAVENAME;
  547                 if (!lockparent) {
  548                         VOP_UNLOCK(vdp, 0);
  549                         cnp->cn_flags |= PDIRUNLOCK;
  550                 }
  551                 return (0);
  552         }
  553 
  554         /*
  555          * Step through the translation in the name.  We do not `vput' the
  556          * directory because we may need it again if a symbolic link
  557          * is relative to the current directory.  Instead we save it
  558          * unlocked as "pdp".  We must get the target inode before unlocking
  559          * the directory to insure that the inode will not be removed
  560          * before we get it.  We prevent deadlock by always fetching
  561          * inodes from the root, moving down the directory tree. Thus
  562          * when following backward pointers ".." we must unlock the
  563          * parent directory before getting the requested directory.
  564          * There is a potential race condition here if both the current
  565          * and parent directories are removed before the VFS_VGET for the
  566          * inode associated with ".." returns.  We hope that this occurs
  567          * infrequently since we cannot avoid this race condition without
  568          * implementing a sophisticated deadlock detection algorithm.
  569          * Note also that this simple deadlock detection scheme will not
  570          * work if the file system has any hard links other than ".."
  571          * that point backwards in the directory structure.
  572          */
  573         pdp = vdp;
  574         if (flags & ISDOTDOT) {
  575                 VOP_UNLOCK(pdp, 0);     /* race to get the inode */
  576                 cnp->cn_flags |= PDIRUNLOCK;
  577                 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
  578                 if (error) {
  579                         if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
  580                                 cnp->cn_flags &= ~PDIRUNLOCK;
  581                         return (error);
  582                 }
  583                 if (lockparent && (flags & ISLASTCN)) {
  584                         if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
  585                                 vput(tdp);
  586                                 return (error);
  587                         }
  588                         cnp->cn_flags &= ~PDIRUNLOCK;
  589                 }
  590                 *vpp = tdp;
  591         } else if (dp->i_number == foundino) {
  592                 VREF(vdp);      /* we want ourself, ie "." */
  593                 *vpp = vdp;
  594         } else {
  595                 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
  596                 if (error)
  597                         return (error);
  598                 if (!lockparent || !(flags & ISLASTCN)) {
  599                         VOP_UNLOCK(pdp, 0);
  600                         cnp->cn_flags |= PDIRUNLOCK;
  601                 }
  602                 *vpp = tdp;
  603         }
  604 
  605         /*
  606          * Insert name into cache if appropriate.
  607          */
  608         if (cnp->cn_flags & MAKEENTRY)
  609                 cache_enter(vdp, *vpp, cnp);
  610         return (0);
  611 }
  612 
  613 void
  614 ufs_dirbad(ip, offset, how)
  615         struct inode *ip;
  616         doff_t offset;
  617         char *how;
  618 {
  619         struct mount *mp;
  620 
  621         mp = ITOV(ip)->v_mount;
  622         printf("%s: bad dir ino %d at offset %d: %s\n",
  623             mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
  624         if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0)
  625                 panic("bad dir");
  626 }
  627 
  628 /*
  629  * Do consistency checking on a directory entry:
  630  *      record length must be multiple of 4
  631  *      entry must fit in rest of its DIRBLKSIZ block
  632  *      record must be large enough to contain entry
  633  *      name is not longer than MAXNAMLEN
  634  *      name must be as long as advertised, and null terminated
  635  */
  636 int
  637 ufs_dirbadentry(dp, ep, entryoffsetinblock)
  638         struct vnode *dp;
  639         struct direct *ep;
  640         int entryoffsetinblock;
  641 {
  642         int i;
  643         int namlen;
  644         const int needswap = UFS_MPNEEDSWAP(dp->v_mount);
  645         int dirblksiz = DIRBLKSIZ;
  646         if (UFS_MPISAPPLEUFS(dp->v_mount)) {
  647                 dirblksiz = APPLEUFS_DIRBLKSIZ;
  648         }
  649 
  650 #if (BYTE_ORDER == LITTLE_ENDIAN)
  651         if (dp->v_mount->mnt_maxsymlinklen > 0 || needswap != 0)
  652                 namlen = ep->d_namlen;
  653         else
  654                 namlen = ep->d_type;
  655 #else
  656         if (dp->v_mount->mnt_maxsymlinklen <= 0 && needswap != 0)
  657                 namlen = ep->d_type;
  658         else
  659         namlen = ep->d_namlen;
  660 #endif
  661         if ((ufs_rw16(ep->d_reclen, needswap) & 0x3) != 0 ||
  662             ufs_rw16(ep->d_reclen, needswap) >
  663                 dirblksiz - (entryoffsetinblock & (dirblksiz - 1)) ||
  664             ufs_rw16(ep->d_reclen, needswap) <
  665                 DIRSIZ(FSFMT(dp), ep, needswap) ||
  666             namlen > MAXNAMLEN) {
  667                 /*return (1); */
  668                 printf("First bad, reclen=%x, DIRSIZ=%lu, namlen=%d, flags=%x "
  669                         "entryoffsetinblock=%d, dirblksiz = %d\n",
  670                         ufs_rw16(ep->d_reclen, needswap),
  671                         (u_long)DIRSIZ(FSFMT(dp), ep, needswap),
  672                         namlen, dp->v_mount->mnt_flag, entryoffsetinblock,dirblksiz);
  673                 goto bad;
  674         }
  675         if (ep->d_ino == 0)
  676                 return (0);
  677         for (i = 0; i < namlen; i++)
  678                 if (ep->d_name[i] == '\0') {
  679                         /*return (1); */
  680                         printf("Second bad\n");
  681                         goto bad;
  682         }
  683         if (ep->d_name[i])
  684                 goto bad;
  685         return (0);
  686 bad:
  687         return (1);
  688 }
  689 
  690 /*
  691  * Construct a new directory entry after a call to namei, using the
  692  * parameters that it left in the componentname argument cnp. The
  693  * argument ip is the inode to which the new directory entry will refer.
  694  */
  695 void
  696 ufs_makedirentry(ip, cnp, newdirp)
  697         struct inode *ip;
  698         struct componentname *cnp;
  699         struct direct *newdirp;
  700 {
  701 #ifdef DIAGNOSTIC
  702         if ((cnp->cn_flags & SAVENAME) == 0)
  703                 panic("makedirentry: missing name");
  704 #endif
  705         newdirp->d_ino = ip->i_number;
  706         newdirp->d_namlen = cnp->cn_namelen;
  707         memcpy(newdirp->d_name, cnp->cn_nameptr, (size_t)cnp->cn_namelen);
  708         newdirp->d_name[cnp->cn_namelen] = '\0';
  709         if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
  710                 newdirp->d_type = IFTODT(ip->i_mode);
  711         else {
  712                 newdirp->d_type = 0;
  713         }
  714 }
  715 
  716 /*
  717  * Write a directory entry after a call to namei, using the parameters
  718  * that it left in nameidata. The argument dirp is the new directory
  719  * entry contents. Dvp is a pointer to the directory to be written,
  720  * which was left locked by namei. Remaining parameters (dp->i_offset,
  721  * dp->i_count) indicate how the space for the new entry is to be obtained.
  722  * Non-null bp indicates that a directory is being created (for the
  723  * soft dependency code).
  724  */
  725 int
  726 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
  727         struct vnode *dvp;
  728         struct vnode *tvp;
  729         struct direct *dirp;
  730         struct componentname *cnp;
  731         struct buf *newdirbp;
  732 {
  733         struct ucred *cr;
  734         struct proc *p;
  735         int newentrysize;
  736         struct inode *dp;
  737         struct buf *bp;
  738         u_int dsize;
  739         struct direct *ep, *nep;
  740         int error, ret, blkoff, loc, spacefree, flags;
  741         char *dirbuf;
  742         struct timespec ts;
  743         const int needswap = UFS_MPNEEDSWAP(dvp->v_mount);
  744         int dirblksiz = DIRBLKSIZ;
  745         if (UFS_MPISAPPLEUFS(dvp->v_mount)) {
  746                 dirblksiz = APPLEUFS_DIRBLKSIZ;
  747         }
  748 
  749         error = 0;
  750         cr = cnp->cn_cred;
  751         p = cnp->cn_proc;
  752 
  753         dp = VTOI(dvp);
  754         newentrysize = DIRSIZ(0, dirp, 0);
  755 
  756         if (dp->i_count == 0) {
  757                 /*
  758                  * If dp->i_count is 0, then namei could find no
  759                  * space in the directory. Here, dp->i_offset will
  760                  * be on a directory block boundary and we will write the
  761                  * new entry into a fresh block.
  762                  */
  763                 if (dp->i_offset & (dirblksiz - 1))
  764                         panic("ufs_direnter: newblk");
  765                 flags = B_CLRBUF;
  766                 if (!DOINGSOFTDEP(dvp))
  767                         flags |= B_SYNC;
  768                 if ((error = VOP_BALLOC(dvp, (off_t)dp->i_offset, dirblksiz,
  769                     cr, flags, &bp)) != 0) {
  770                         if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
  771                                 bdwrite(newdirbp);
  772                         return (error);
  773                 }
  774                 dp->i_size = dp->i_offset + dirblksiz;
  775                 DIP_ASSIGN(dp, size, dp->i_size);
  776                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
  777                 uvm_vnp_setsize(dvp, dp->i_size);
  778                 dirp->d_reclen = ufs_rw16(dirblksiz, needswap);
  779                 dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
  780                 if (dvp->v_mount->mnt_maxsymlinklen <= 0) {
  781 #if (BYTE_ORDER == LITTLE_ENDIAN)
  782                         if (needswap == 0) {
  783 #else
  784                         if (needswap != 0) {
  785 #endif
  786                                 u_char tmp = dirp->d_namlen;
  787                                 dirp->d_namlen = dirp->d_type;
  788                                 dirp->d_type = tmp;
  789                         }
  790                 }
  791                 blkoff = dp->i_offset &
  792                     (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
  793                 memcpy((caddr_t)bp->b_data + blkoff, (caddr_t)dirp,
  794                     newentrysize);
  795                 if (DOINGSOFTDEP(dvp)) {
  796                         /*
  797                          * Ensure that the entire newly allocated block is a
  798                          * valid directory so that future growth within the
  799                          * block does not have to ensure that the block is
  800                          * written before the inode.
  801                          */
  802                         blkoff += dirblksiz;
  803                         while (blkoff < bp->b_bcount) {
  804                                 ((struct direct *)
  805                                    (bp->b_data + blkoff))->d_reclen = dirblksiz;
  806                                 blkoff += dirblksiz;
  807                         }
  808                         if (softdep_setup_directory_add(bp, dp, dp->i_offset,
  809                             ufs_rw32(dirp->d_ino, needswap), newdirbp, 1) == 0) {
  810                                 bdwrite(bp);
  811                                 TIMEVAL_TO_TIMESPEC(&time, &ts);
  812                                 return VOP_UPDATE(dvp, &ts, &ts, UPDATE_DIROP);
  813                         }
  814                         /* We have just allocated a directory block in an
  815                          * indirect block. Rather than tracking when it gets
  816                          * claimed by the inode, we simply do a VOP_FSYNC
  817                          * now to ensure that it is there (in case the user
  818                          * does a future fsync). Note that we have to unlock
  819                          * the inode for the entry that we just entered, as
  820                          * the VOP_FSYNC may need to lock other inodes which
  821                          * can lead to deadlock if we also hold a lock on
  822                          * the newly entered node.
  823                          */
  824                         error = VOP_BWRITE(bp);
  825                         if (error != 0)
  826                                 return (error);
  827                         if (tvp != NULL)
  828                                 VOP_UNLOCK(tvp, 0);
  829                         error = VOP_FSYNC(dvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
  830                         if (tvp != 0)
  831                                 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
  832                         return (error);
  833                 } else {
  834                         error = VOP_BWRITE(bp);
  835                 }
  836                 TIMEVAL_TO_TIMESPEC(&time, &ts);
  837                 ret = VOP_UPDATE(dvp, &ts, &ts, UPDATE_DIROP);
  838                 if (error == 0)
  839                         return (ret);
  840                 return (error);
  841         }
  842 
  843         /*
  844          * If dp->i_count is non-zero, then namei found space for the new
  845          * entry in the range dp->i_offset to dp->i_offset + dp->i_count
  846          * in the directory. To use this space, we may have to compact
  847          * the entries located there, by copying them together towards the
  848          * beginning of the block, leaving the free space in one usable
  849          * chunk at the end.
  850          */
  851 
  852         /*
  853          * Increase size of directory if entry eats into new space.
  854          * This should never push the size past a new multiple of
  855          * DIRBLKSIZE.
  856          *
  857          * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
  858          */
  859         if (dp->i_offset + dp->i_count > dp->i_size) {
  860                 dp->i_size = dp->i_offset + dp->i_count;
  861                 DIP_ASSIGN(dp, size, dp->i_size);
  862                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
  863         }
  864         /*
  865          * Get the block containing the space for the new directory entry.
  866          */
  867         error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
  868         if (error) {
  869                 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
  870                         bdwrite(newdirbp);
  871                 return (error);
  872         }
  873         /*
  874          * Find space for the new entry. In the simple case, the entry at
  875          * offset base will have the space. If it does not, then namei
  876          * arranged that compacting the region dp->i_offset to
  877          * dp->i_offset + dp->i_count would yield the space.
  878          */
  879         ep = (struct direct *)dirbuf;
  880         dsize = DIRSIZ(FSFMT(dvp), ep, needswap);
  881         spacefree = ufs_rw16(ep->d_reclen, needswap) - dsize;
  882         for (loc = ufs_rw16(ep->d_reclen, needswap); loc < dp->i_count; ) {
  883                 nep = (struct direct *)(dirbuf + loc);
  884                 if (ep->d_ino) {
  885                         /* trim the existing slot */
  886                         ep->d_reclen = ufs_rw16(dsize, needswap);
  887                         ep = (struct direct *)((char *)ep + dsize);
  888                 } else {
  889                         /* overwrite; nothing there; header is ours */
  890                         spacefree += dsize;
  891                 }
  892                 dsize = DIRSIZ(FSFMT(dvp), nep, needswap);
  893                 spacefree += ufs_rw16(nep->d_reclen, needswap) - dsize;
  894                 loc += ufs_rw16(nep->d_reclen, needswap);
  895                 if (DOINGSOFTDEP(dvp))
  896                         softdep_change_directoryentry_offset(dp, dirbuf,
  897                             (caddr_t)nep, (caddr_t)ep, dsize);
  898                 else
  899                         memcpy((caddr_t)ep, (caddr_t)nep, dsize);
  900         }
  901         /*
  902          * Update the pointer fields in the previous entry (if any),
  903          * copy in the new entry, and write out the block.
  904          */
  905         if (ep->d_ino == 0 ||
  906             (ufs_rw32(ep->d_ino, needswap) == WINO &&
  907              memcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
  908                 if (spacefree + dsize < newentrysize)
  909                         panic("ufs_direnter: compact1");
  910                 dirp->d_reclen = spacefree + dsize;
  911         } else {
  912                 if (spacefree < newentrysize)
  913                         panic("ufs_direnter: compact2");
  914                 dirp->d_reclen = spacefree;
  915                 ep->d_reclen = ufs_rw16(dsize, needswap);
  916                 ep = (struct direct *)((char *)ep + dsize);
  917         }
  918         dirp->d_reclen = ufs_rw16(dirp->d_reclen, needswap);
  919         dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
  920         if (dvp->v_mount->mnt_maxsymlinklen <= 0) {
  921 #if (BYTE_ORDER == LITTLE_ENDIAN)
  922                 if (needswap == 0) {
  923 #else
  924                 if (needswap != 0) {
  925 #endif
  926                         u_char tmp = dirp->d_namlen;
  927                         dirp->d_namlen = dirp->d_type;
  928                         dirp->d_type = tmp;
  929                 }
  930         }
  931         memcpy((caddr_t)ep, (caddr_t)dirp, (u_int)newentrysize);
  932         if (DOINGSOFTDEP(dvp)) {
  933                 softdep_setup_directory_add(bp, dp,
  934                     dp->i_offset + (caddr_t)ep - dirbuf,
  935                         ufs_rw32(dirp->d_ino, needswap), newdirbp, 0);
  936                 bdwrite(bp);
  937         } else {
  938                 error = VOP_BWRITE(bp);
  939         }
  940         dp->i_flag |= IN_CHANGE | IN_UPDATE;
  941         /*
  942          * If all went well, and the directory can be shortened, proceed
  943          * with the truncation. Note that we have to unlock the inode for
  944          * the entry that we just entered, as the truncation may need to
  945          * lock other inodes which can lead to deadlock if we also hold a
  946          * lock on the newly entered node.
  947          */
  948         if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
  949                 if (DOINGSOFTDEP(dvp) && (tvp != NULL))
  950                         VOP_UNLOCK(tvp, 0);
  951                 (void) VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cr, p);
  952                 if (DOINGSOFTDEP(dvp) && (tvp != NULL))
  953                         vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
  954         }
  955         return (error);
  956 }
  957 
  958 /*
  959  * Remove a directory entry after a call to namei, using
  960  * the parameters which it left in nameidata. The entry
  961  * dp->i_offset contains the offset into the directory of the
  962  * entry to be eliminated.  The dp->i_count field contains the
  963  * size of the previous record in the directory.  If this
  964  * is 0, the first entry is being deleted, so we need only
  965  * zero the inode number to mark the entry as free.  If the
  966  * entry is not the first in the directory, we must reclaim
  967  * the space of the now empty record by adding the record size
  968  * to the size of the previous entry.
  969  */
  970 int
  971 ufs_dirremove(dvp, ip, flags, isrmdir)
  972         struct vnode *dvp;
  973         struct inode *ip;
  974         int flags;
  975         int isrmdir;
  976 {
  977         struct inode *dp;
  978         struct direct *ep;
  979         struct buf *bp;
  980         int error;
  981 #ifdef FFS_EI
  982         const int needswap = UFS_MPNEEDSWAP(dvp->v_mount);
  983 #endif
  984 
  985         dp = VTOI(dvp);
  986 
  987         if (flags & DOWHITEOUT) {
  988                 /*
  989                  * Whiteout entry: set d_ino to WINO.
  990                  */
  991                 error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (void *)&ep,
  992                                      &bp);
  993                 if (error)
  994                         return (error);
  995                 ep->d_ino = ufs_rw32(WINO, needswap);
  996                 ep->d_type = DT_WHT;
  997                 goto out;
  998         }
  999 
 1000         if ((error = VOP_BLKATOFF(dvp,
 1001             (off_t)(dp->i_offset - dp->i_count), (void *)&ep, &bp)) != 0)
 1002                 return (error);
 1003 
 1004         if (dp->i_count == 0) {
 1005                 /*
 1006                  * First entry in block: set d_ino to zero.
 1007                  */
 1008                 ep->d_ino = 0;
 1009         } else {
 1010                 /*
 1011                  * Collapse new free space into previous entry.
 1012                  */
 1013                 ep->d_reclen =
 1014                     ufs_rw16(ufs_rw16(ep->d_reclen, needswap) + dp->i_reclen,
 1015                         needswap);
 1016         }
 1017 out:
 1018         if (DOINGSOFTDEP(dvp)) {
 1019                 if (ip) {
 1020                         ip->i_ffs_effnlink--;
 1021                         softdep_change_linkcnt(ip);
 1022                         softdep_setup_remove(bp, dp, ip, isrmdir);
 1023                 }
 1024                 bdwrite(bp);
 1025         } else {
 1026                 if (ip) {
 1027                         ip->i_ffs_effnlink--;
 1028                         ip->i_nlink--;
 1029                         DIP_ASSIGN(ip, nlink, ip->i_nlink);
 1030                         ip->i_flag |= IN_CHANGE;
 1031                 }
 1032                 error = VOP_BWRITE(bp);
 1033         }
 1034         dp->i_flag |= IN_CHANGE | IN_UPDATE;
 1035         return (error);
 1036 }
 1037 
 1038 /*
 1039  * Rewrite an existing directory entry to point at the inode
 1040  * supplied.  The parameters describing the directory entry are
 1041  * set up by a call to namei.
 1042  */
 1043 int
 1044 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir, iflags)
 1045         struct inode *dp, *oip;
 1046         ino_t newinum;
 1047         int newtype;
 1048         int isrmdir;
 1049         int iflags;
 1050 {
 1051         struct buf *bp;
 1052         struct direct *ep;
 1053         struct vnode *vdp = ITOV(dp);
 1054         int error;
 1055 
 1056         error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (void *)&ep, &bp);
 1057         if (error)
 1058                 return (error);
 1059         ep->d_ino = ufs_rw32(newinum, UFS_MPNEEDSWAP(vdp->v_mount));
 1060         if (vdp->v_mount->mnt_maxsymlinklen > 0)
 1061                 ep->d_type = newtype;
 1062         oip->i_ffs_effnlink--;
 1063         if (DOINGSOFTDEP(vdp)) {
 1064                 softdep_change_linkcnt(oip);
 1065                 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
 1066                 bdwrite(bp);
 1067         } else {
 1068                 oip->i_nlink--;
 1069                 DIP_ASSIGN(oip, nlink, oip->i_nlink);
 1070                 oip->i_flag |= IN_CHANGE;
 1071                 error = VOP_BWRITE(bp);
 1072         }
 1073         dp->i_flag |= iflags;
 1074         return (error);
 1075 }
 1076 
 1077 /*
 1078  * Check if a directory is empty or not.
 1079  * Inode supplied must be locked.
 1080  *
 1081  * Using a struct dirtemplate here is not precisely
 1082  * what we want, but better than using a struct direct.
 1083  *
 1084  * NB: does not handle corrupted directories.
 1085  */
 1086 int
 1087 ufs_dirempty(ip, parentino, cred)
 1088         struct inode *ip;
 1089         ino_t parentino;
 1090         struct ucred *cred;
 1091 {
 1092         doff_t off;
 1093         struct dirtemplate dbuf;
 1094         struct direct *dp = (struct direct *)&dbuf;
 1095         int error, namlen;
 1096         size_t count;
 1097         const int needswap = UFS_IPNEEDSWAP(ip);
 1098 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
 1099 
 1100         for (off = 0; off < ip->i_size;
 1101             off += ufs_rw16(dp->d_reclen, needswap)) {
 1102                 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
 1103                    UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
 1104                 /*
 1105                  * Since we read MINDIRSIZ, residual must
 1106                  * be 0 unless we're at end of file.
 1107                  */
 1108                 if (error || count != 0)
 1109                         return (0);
 1110                 /* avoid infinite loops */
 1111                 if (dp->d_reclen == 0)
 1112                         return (0);
 1113                 /* skip empty entries */
 1114                 if (dp->d_ino == 0 || ufs_rw32(dp->d_ino, needswap) == WINO)
 1115                         continue;
 1116                 /* accept only "." and ".." */
 1117 #if (BYTE_ORDER == LITTLE_ENDIAN)
 1118                 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0 || needswap != 0)
 1119                         namlen = dp->d_namlen;
 1120                 else
 1121                         namlen = dp->d_type;
 1122 #else
 1123                 if (ITOV(ip)->v_mount->mnt_maxsymlinklen <= 0 && needswap != 0)
 1124                         namlen = dp->d_type;
 1125                 else
 1126                         namlen = dp->d_namlen;
 1127 #endif
 1128                 if (namlen > 2)
 1129                         return (0);
 1130                 if (dp->d_name[0] != '.')
 1131                         return (0);
 1132                 /*
 1133                  * At this point namlen must be 1 or 2.
 1134                  * 1 implies ".", 2 implies ".." if second
 1135                  * char is also "."
 1136                  */
 1137                 if (namlen == 1 &&
 1138                     ufs_rw32(dp->d_ino, needswap) == ip->i_number)
 1139                         continue;
 1140                 if (dp->d_name[1] == '.' &&
 1141                     ufs_rw32(dp->d_ino, needswap) == parentino)
 1142                         continue;
 1143                 return (0);
 1144         }
 1145         return (1);
 1146 }
 1147 
 1148 /*
 1149  * Check if source directory is in the path of the target directory.
 1150  * Target is supplied locked, source is unlocked.
 1151  * The target is always vput before returning.
 1152  */
 1153 int
 1154 ufs_checkpath(source, target, cred)
 1155         struct inode *source, *target;
 1156         struct ucred *cred;
 1157 {
 1158         struct vnode *vp = ITOV(target);
 1159         int error, rootino, namlen;
 1160         struct dirtemplate dirbuf;
 1161         const int needswap = UFS_MPNEEDSWAP(vp->v_mount);
 1162 
 1163         vp = ITOV(target);
 1164         if (target->i_number == source->i_number) {
 1165                 error = EEXIST;
 1166                 goto out;
 1167         }
 1168         rootino = ROOTINO;
 1169         error = 0;
 1170         if (target->i_number == rootino)
 1171                 goto out;
 1172 
 1173         for (;;) {
 1174                 if (vp->v_type != VDIR) {
 1175                         error = ENOTDIR;
 1176                         break;
 1177                 }
 1178                 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
 1179                     sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
 1180                     IO_NODELOCKED, cred, NULL, (struct proc *)0);
 1181                 if (error != 0)
 1182                         break;
 1183 #if (BYTE_ORDER == LITTLE_ENDIAN)
 1184                 if (vp->v_mount->mnt_maxsymlinklen > 0 ||
 1185                         needswap != 0)
 1186                         namlen = dirbuf.dotdot_namlen;
 1187                 else
 1188                         namlen = dirbuf.dotdot_type;
 1189 #else
 1190                 if (vp->v_mount->mnt_maxsymlinklen == 0 &&
 1191                     needswap != 0)
 1192                         namlen = dirbuf.dotdot_type;
 1193                 else
 1194                         namlen = dirbuf.dotdot_namlen;
 1195 #endif
 1196                 if (namlen != 2 ||
 1197                     dirbuf.dotdot_name[0] != '.' ||
 1198                     dirbuf.dotdot_name[1] != '.') {
 1199                         error = ENOTDIR;
 1200                         break;
 1201                 }
 1202                 if (ufs_rw32(dirbuf.dotdot_ino, needswap) == source->i_number) {
 1203                         error = EINVAL;
 1204                         break;
 1205                 }
 1206                 if (ufs_rw32(dirbuf.dotdot_ino, needswap) == rootino)
 1207                         break;
 1208                 vput(vp);
 1209                 error = VFS_VGET(vp->v_mount,
 1210                     ufs_rw32(dirbuf.dotdot_ino, needswap), &vp);
 1211                 if (error) {
 1212                         vp = NULL;
 1213                         break;
 1214                 }
 1215         }
 1216 
 1217 out:
 1218         if (error == ENOTDIR)
 1219                 printf("checkpath: .. not a directory\n");
 1220         if (vp != NULL)
 1221                 vput(vp);
 1222         return (error);
 1223 }

Cache object: 6b72ca3c5b4db0aaa4746ba5144aa911


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