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

Cache object: 12048b68a421e0e9a287785112074c8b


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