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

Cache object: 3556a8f326bdbd259f33c08222cb8e63


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