The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/ufs/ufs/ufs_lookup.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 4cdbfc9c0225367b0f1366e1c4d8067c


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