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

Cache object: 395f6270cb660b0027fa8a201c4f39b9


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