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

Cache object: b587415b3365468c5e1a01c61fe7dfe9


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