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

Cache object: 92e697cd209072364cbe25932b0b77f5


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