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

Cache object: 5dfd602c83f9280be93a817f101a9e7d


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