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/fs/ext2fs/ext2_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  *  modified for Lites 1.1
    3  *
    4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
    5  *  University of Utah, Department of Computer Science
    6  */
    7 /*-
    8  * Copyright (c) 1989, 1993
    9  *      The Regents of the University of California.  All rights reserved.
   10  * (c) UNIX System Laboratories, Inc.
   11  * All or some portions of this file are derived from material licensed
   12  * to the University of California by American Telephone and Telegraph
   13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   14  * the permission of UNIX System Laboratories, Inc.
   15  *
   16  * Redistribution and use in source and binary forms, with or without
   17  * modification, are permitted provided that the following conditions
   18  * are met:
   19  * 1. Redistributions of source code must retain the above copyright
   20  *    notice, this list of conditions and the following disclaimer.
   21  * 2. Redistributions in binary form must reproduce the above copyright
   22  *    notice, this list of conditions and the following disclaimer in the
   23  *    documentation and/or other materials provided with the distribution.
   24  * 4. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  *      @(#)ufs_lookup.c        8.6 (Berkeley) 4/1/94
   41  * $FreeBSD: releng/10.4/sys/fs/ext2fs/ext2_lookup.c 311232 2017-01-04 02:43:33Z pfg $
   42  */
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/namei.h>
   47 #include <sys/bio.h>
   48 #include <sys/buf.h>
   49 #include <sys/endian.h>
   50 #include <sys/mount.h>
   51 #include <sys/vnode.h>
   52 #include <sys/malloc.h>
   53 #include <sys/dirent.h>
   54 #include <sys/sysctl.h>
   55 
   56 #include <ufs/ufs/dir.h>
   57 
   58 #include <fs/ext2fs/inode.h>
   59 #include <fs/ext2fs/ext2_mount.h>
   60 #include <fs/ext2fs/ext2fs.h>
   61 #include <fs/ext2fs/ext2_dinode.h>
   62 #include <fs/ext2fs/ext2_dir.h>
   63 #include <fs/ext2fs/ext2_extern.h>
   64 
   65 #ifdef INVARIANTS
   66 static int dirchk = 1;
   67 #else
   68 static int dirchk = 0;
   69 #endif
   70 
   71 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
   72 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
   73 
   74 /*
   75    DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
   76    while it is the native blocksize in ext2fs - thus, a #define
   77    is no longer appropriate
   78 */
   79 #undef  DIRBLKSIZ
   80 
   81 static u_char ext2_ft_to_dt[] = {
   82         DT_UNKNOWN,             /* EXT2_FT_UNKNOWN */
   83         DT_REG,                 /* EXT2_FT_REG_FILE */
   84         DT_DIR,                 /* EXT2_FT_DIR */
   85         DT_CHR,                 /* EXT2_FT_CHRDEV */
   86         DT_BLK,                 /* EXT2_FT_BLKDEV */
   87         DT_FIFO,                /* EXT2_FT_FIFO */
   88         DT_SOCK,                /* EXT2_FT_SOCK */
   89         DT_LNK,                 /* EXT2_FT_SYMLINK */
   90 };
   91 #define FTTODT(ft) \
   92     ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
   93 
   94 static u_char dt_to_ext2_ft[] = {
   95         EXT2_FT_UNKNOWN,        /* DT_UNKNOWN */
   96         EXT2_FT_FIFO,           /* DT_FIFO */
   97         EXT2_FT_CHRDEV,         /* DT_CHR */
   98         EXT2_FT_UNKNOWN,        /* unused */
   99         EXT2_FT_DIR,            /* DT_DIR */
  100         EXT2_FT_UNKNOWN,        /* unused */
  101         EXT2_FT_BLKDEV,         /* DT_BLK */
  102         EXT2_FT_UNKNOWN,        /* unused */
  103         EXT2_FT_REG_FILE,       /* DT_REG */
  104         EXT2_FT_UNKNOWN,        /* unused */
  105         EXT2_FT_SYMLINK,        /* DT_LNK */
  106         EXT2_FT_UNKNOWN,        /* unused */
  107         EXT2_FT_SOCK,           /* DT_SOCK */
  108         EXT2_FT_UNKNOWN,        /* unused */
  109         EXT2_FT_UNKNOWN,        /* DT_WHT */
  110 };
  111 #define DTTOFT(dt) \
  112     ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
  113 
  114 static int      ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
  115                     int entryoffsetinblock);
  116 static int      ext2_is_dot_entry(struct componentname *cnp);
  117 static int      ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
  118                     struct componentname *cnp, ino_t *dd_ino);
  119 
  120 static int
  121 ext2_is_dot_entry(struct componentname *cnp)
  122 {
  123         if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
  124             (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
  125                 return (1);
  126         return (0);
  127 }
  128 
  129 /*
  130  * Vnode op for reading directories.
  131  */
  132 int
  133 ext2_readdir(struct vop_readdir_args *ap)
  134 {
  135         struct vnode *vp = ap->a_vp;
  136         struct uio *uio = ap->a_uio;
  137         struct buf *bp;
  138         struct inode *ip;
  139         struct ext2fs_direct_2 *dp, *edp;
  140         u_long *cookies;
  141         struct dirent dstdp;
  142         off_t offset, startoffset;
  143         size_t readcnt, skipcnt;
  144         ssize_t startresid;
  145         int ncookies;
  146         int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
  147         int error;
  148 
  149         if (uio->uio_offset < 0)
  150                 return (EINVAL);
  151         ip = VTOI(vp);
  152         if (ap->a_ncookies != NULL) {
  153                 ncookies = uio->uio_resid;
  154                 if (uio->uio_offset >= ip->i_size)
  155                         ncookies = 0;
  156                 else if (ip->i_size - uio->uio_offset < ncookies)
  157                         ncookies = ip->i_size - uio->uio_offset;
  158                 ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
  159                     e2d_namlen) + 4) + 1;
  160                 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
  161                 *ap->a_ncookies = ncookies;
  162                 *ap->a_cookies = cookies;
  163         } else {
  164                 ncookies = 0;
  165                 cookies = NULL;
  166         }
  167         offset = startoffset = uio->uio_offset;
  168         startresid = uio->uio_resid;
  169         error = 0;
  170         while (error == 0 && uio->uio_resid > 0 &&
  171             uio->uio_offset < ip->i_size) {
  172                 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
  173                 if (error)
  174                         break;
  175                 if (bp->b_offset + bp->b_bcount > ip->i_size)
  176                         readcnt = ip->i_size - bp->b_offset;
  177                 else
  178                         readcnt = bp->b_bcount;
  179                 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
  180                     ~(size_t)(DIRBLKSIZ - 1);
  181                 offset = bp->b_offset + skipcnt;
  182                 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
  183                 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
  184                 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
  185                         if (dp->e2d_reclen <= offsetof(struct ext2fs_direct_2,
  186                             e2d_namlen) || (caddr_t)dp + dp->e2d_reclen >
  187                             (caddr_t)edp) {
  188                                 error = EIO;
  189                                 break;
  190                         }
  191                         /*-
  192                          * "New" ext2fs directory entries differ in 3 ways
  193                          * from ufs on-disk ones:
  194                          * - the name is not necessarily NUL-terminated.
  195                          * - the file type field always exists and always
  196                          *   follows the name length field.
  197                          * - the file type is encoded in a different way.
  198                          *
  199                          * "Old" ext2fs directory entries need no special
  200                          * conversions, since they are binary compatible
  201                          * with "new" entries having a file type of 0 (i.e.,
  202                          * EXT2_FT_UNKNOWN).  Splitting the old name length
  203                          * field didn't make a mess like it did in ufs,
  204                          * because ext2fs uses a machine-independent disk
  205                          * layout.
  206                          */
  207                         dstdp.d_namlen = dp->e2d_namlen;
  208                         dstdp.d_type = FTTODT(dp->e2d_type);
  209                         if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
  210                             dstdp.d_namlen > dp->e2d_reclen) {
  211                                 error = EIO;
  212                                 break;
  213                         }
  214                         if (offset < startoffset || dp->e2d_ino == 0)
  215                                 goto nextentry;
  216                         dstdp.d_fileno = dp->e2d_ino;
  217                         dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
  218                         bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
  219                         dstdp.d_name[dstdp.d_namlen] = '\0';
  220                         if (dstdp.d_reclen > uio->uio_resid) {
  221                                 if (uio->uio_resid == startresid)
  222                                         error = EINVAL;
  223                                 else
  224                                         error = EJUSTRETURN;
  225                                 break;
  226                         }
  227                         /* Advance dp. */
  228                         error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
  229                         if (error)
  230                                 break;
  231                         if (cookies != NULL) {
  232                                 KASSERT(ncookies > 0,
  233                                     ("ext2_readdir: cookies buffer too small"));
  234                                 *cookies = offset + dp->e2d_reclen;
  235                                 cookies++;
  236                                 ncookies--;
  237                         }
  238 nextentry:
  239                         offset += dp->e2d_reclen;
  240                         dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
  241                             dp->e2d_reclen);
  242                 }
  243                 bqrelse(bp);
  244                 uio->uio_offset = offset;
  245         }
  246         /* We need to correct uio_offset. */
  247         uio->uio_offset = offset;
  248         if (error == EJUSTRETURN)
  249                 error = 0;
  250         if (ap->a_ncookies != NULL) {
  251                 if (error == 0) {
  252                         ap->a_ncookies -= ncookies;
  253                 } else {
  254                         free(*ap->a_cookies, M_TEMP);
  255                         *ap->a_ncookies = 0;
  256                         *ap->a_cookies = NULL;
  257                 }
  258         }
  259         if (error == 0 && ap->a_eofflag)
  260                 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
  261         return (error);
  262 }
  263 
  264 /*
  265  * Convert a component of a pathname into a pointer to a locked inode.
  266  * This is a very central and rather complicated routine.
  267  * If the file system is not maintained in a strict tree hierarchy,
  268  * this can result in a deadlock situation (see comments in code below).
  269  *
  270  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
  271  * on whether the name is to be looked up, created, renamed, or deleted.
  272  * When CREATE, RENAME, or DELETE is specified, information usable in
  273  * creating, renaming, or deleting a directory entry may be calculated.
  274  * If flag has LOCKPARENT or'ed into it and the target of the pathname
  275  * exists, lookup returns both the target and its parent directory locked.
  276  * When creating or renaming and LOCKPARENT is specified, the target may
  277  * not be ".".  When deleting and LOCKPARENT is specified, the target may
  278  * be "."., but the caller must check to ensure it does an vrele and vput
  279  * instead of two vputs.
  280  *
  281  * Overall outline of ext2_lookup:
  282  *
  283  *      search for name in directory, to found or notfound
  284  * notfound:
  285  *      if creating, return locked directory, leaving info on available slots
  286  *      else return error
  287  * found:
  288  *      if at end of path and deleting, return information to allow delete
  289  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
  290  *        inode and return info to allow rewrite
  291  *      if not at end, add name to cache; if at end and neither creating
  292  *        nor deleting, add name to cache
  293  */
  294 int
  295 ext2_lookup(struct vop_cachedlookup_args *ap)
  296 {
  297 
  298         return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
  299 }
  300 
  301 static int
  302 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
  303     ino_t *dd_ino)
  304 {
  305         struct inode *dp;               /* inode for directory being searched */
  306         struct buf *bp;                 /* a buffer of directory entries */
  307         struct ext2fs_direct_2 *ep;     /* the current directory entry */
  308         int entryoffsetinblock;         /* offset of ep in bp's buffer */
  309         struct ext2fs_searchslot ss;
  310         doff_t i_diroff;                /* cached i_diroff value */
  311         doff_t i_offset;                /* cached i_offset value */
  312         int numdirpasses;               /* strategy for directory search */
  313         doff_t endsearch;               /* offset to end directory search */
  314         doff_t prevoff;                 /* prev entry dp->i_offset */
  315         struct vnode *pdp;              /* saved dp during symlink work */
  316         struct vnode *tdp;              /* returned by VFS_VGET */
  317         doff_t enduseful;               /* pointer past last used dir slot */
  318         u_long bmask;                   /* block offset mask */
  319         int error;
  320         struct ucred *cred = cnp->cn_cred;
  321         int flags = cnp->cn_flags;
  322         int nameiop = cnp->cn_nameiop;
  323         ino_t ino, ino1;
  324         int ltype;
  325         int entry_found = 0;
  326 
  327         int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
  328 
  329         if (vpp != NULL)
  330                 *vpp = NULL;
  331 
  332         dp = VTOI(vdp);
  333         bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
  334 restart:
  335         bp = NULL;
  336         ss.slotoffset = -1;
  337 
  338         /*
  339          * We now have a segment name to search for, and a directory to search.
  340          *
  341          * Suppress search for slots unless creating
  342          * file and at end of pathname, in which case
  343          * we watch for a place to put the new file in
  344          * case it doesn't already exist.
  345          */
  346         i_diroff = dp->i_diroff;
  347         ss.slotstatus = FOUND;
  348         ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
  349         if ((nameiop == CREATE || nameiop == RENAME) &&
  350             (flags & ISLASTCN)) {
  351                 ss.slotstatus = NONE;
  352                 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
  353                 /*
  354                  * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
  355                  * cnp->cn_namelen + 3) &~ 3;
  356                  */
  357         }
  358         /*
  359          * Try to lookup dir entry using htree directory index.
  360          *
  361          * If we got an error or we want to find '.' or '..' entry,
  362          * we will fall back to linear search.
  363          */
  364         if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
  365                 numdirpasses = 1;
  366                 entryoffsetinblock = 0;
  367                 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
  368                     &bp, &entryoffsetinblock, &i_offset, &prevoff,
  369                     &enduseful, &ss)) {
  370                 case 0:
  371                         ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
  372                             (i_offset & bmask));
  373                         goto foundentry;
  374                 case ENOENT:
  375                         i_offset = roundup2(dp->i_size, DIRBLKSIZ);
  376                         goto notfound;
  377                 default:
  378                         /*
  379                          * Something failed; just fallback to do a linear
  380                          * search.
  381                          */
  382                         break;
  383                 }
  384         }
  385 
  386         /*
  387          * If there is cached information on a previous search of
  388          * this directory, pick up where we last left off.
  389          * We cache only lookups as these are the most common
  390          * and have the greatest payoff. Caching CREATE has little
  391          * benefit as it usually must search the entire directory
  392          * to determine that the entry does not exist. Caching the
  393          * location of the last DELETE or RENAME has not reduced
  394          * profiling time and hence has been removed in the interest
  395          * of simplicity.
  396          */
  397         if (nameiop != LOOKUP || i_diroff == 0 ||
  398             i_diroff > dp->i_size) {
  399                 entryoffsetinblock = 0;
  400                 i_offset = 0;
  401                 numdirpasses = 1;
  402         } else {
  403                 i_offset = i_diroff;
  404                 if ((entryoffsetinblock = i_offset & bmask) &&
  405                     (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
  406                     &bp)))
  407                         return (error);
  408                 numdirpasses = 2;
  409                 nchstats.ncs_2passes++;
  410         }
  411         prevoff = i_offset;
  412         endsearch = roundup2(dp->i_size, DIRBLKSIZ);
  413         enduseful = 0;
  414 
  415 searchloop:
  416         while (i_offset < endsearch) {
  417                 /*
  418                  * If necessary, get the next directory block.
  419                  */
  420                 if (bp != NULL)
  421                         brelse(bp);
  422                 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
  423                 if (error != 0)
  424                         return (error);
  425                 entryoffsetinblock = 0;
  426                 /*
  427                  * If still looking for a slot, and at a DIRBLKSIZE
  428                  * boundary, have to start looking for free space again.
  429                  */
  430                 if (ss.slotstatus == NONE &&
  431                     (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
  432                         ss.slotoffset = -1;
  433                         ss.slotfreespace = 0;
  434                 }
  435                 error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
  436                     cnp->cn_nameptr, cnp->cn_namelen,
  437                     &entryoffsetinblock, &i_offset, &prevoff,
  438                     &enduseful, &ss);
  439                 if (error != 0) {
  440                         brelse(bp);
  441                         return (error);
  442                 }
  443                 if (entry_found) {
  444                         ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
  445                             (entryoffsetinblock & bmask));
  446 foundentry:
  447                         ino = ep->e2d_ino;
  448                         goto found;
  449                 }
  450         }
  451 notfound:
  452         /*
  453          * If we started in the middle of the directory and failed
  454          * to find our target, we must check the beginning as well.
  455          */
  456         if (numdirpasses == 2) {
  457                 numdirpasses--;
  458                 i_offset = 0;
  459                 endsearch = i_diroff;
  460                 goto searchloop;
  461         }
  462         if (bp != NULL)
  463                 brelse(bp);
  464         /*
  465          * If creating, and at end of pathname and current
  466          * directory has not been removed, then can consider
  467          * allowing file to be created.
  468          */
  469         if ((nameiop == CREATE || nameiop == RENAME) &&
  470             (flags & ISLASTCN) && dp->i_nlink != 0) {
  471                 /*
  472                  * Access for write is interpreted as allowing
  473                  * creation of files in the directory.
  474                  */
  475                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
  476                         return (error);
  477                 /*
  478                  * Return an indication of where the new directory
  479                  * entry should be put.  If we didn't find a slot,
  480                  * then set dp->i_count to 0 indicating
  481                  * that the new slot belongs at the end of the
  482                  * directory. If we found a slot, then the new entry
  483                  * can be put in the range from dp->i_offset to
  484                  * dp->i_offset + dp->i_count.
  485                  */
  486                 if (ss.slotstatus == NONE) {
  487                         dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
  488                         dp->i_count = 0;
  489                         enduseful = dp->i_offset;
  490                 } else {
  491                         dp->i_offset = ss.slotoffset;
  492                         dp->i_count = ss.slotsize;
  493                         if (enduseful < ss.slotoffset + ss.slotsize)
  494                                 enduseful = ss.slotoffset + ss.slotsize;
  495                 }
  496                 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
  497                 /*
  498                  * We return with the directory locked, so that
  499                  * the parameters we set up above will still be
  500                  * valid if we actually decide to do a direnter().
  501                  * We return ni_vp == NULL to indicate that the entry
  502                  * does not currently exist; we leave a pointer to
  503                  * the (locked) directory inode in ndp->ni_dvp.
  504                  * The pathname buffer is saved so that the name
  505                  * can be obtained later.
  506                  *
  507                  * NB - if the directory is unlocked, then this
  508                  * information cannot be used.
  509                  */
  510                 cnp->cn_flags |= SAVENAME;
  511                 return (EJUSTRETURN);
  512         }
  513         /*
  514          * Insert name into cache (as non-existent) if appropriate.
  515          */
  516         if ((cnp->cn_flags & MAKEENTRY) != 0)
  517                 cache_enter(vdp, NULL, cnp);
  518         return (ENOENT);
  519 
  520 found:
  521         if (dd_ino != NULL)
  522                 *dd_ino = ino;
  523         if (numdirpasses == 2)
  524                 nchstats.ncs_pass2++;
  525         /*
  526          * Check that directory length properly reflects presence
  527          * of this entry.
  528          */
  529         if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
  530             > dp->i_size) {
  531                 ext2_dirbad(dp, i_offset, "i_size too small");
  532                 dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
  533                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
  534         }
  535         brelse(bp);
  536 
  537         /*
  538          * Found component in pathname.
  539          * If the final component of path name, save information
  540          * in the cache as to where the entry was found.
  541          */
  542         if ((flags & ISLASTCN) && nameiop == LOOKUP)
  543                 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
  544         /*
  545          * If deleting, and at end of pathname, return
  546          * parameters which can be used to remove file.
  547          */
  548         if (nameiop == DELETE && (flags & ISLASTCN)) {
  549                 if (flags & LOCKPARENT)
  550                         ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
  551                 /*
  552                  * Write access to directory required to delete files.
  553                  */
  554                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
  555                         return (error);
  556                 /*
  557                  * Return pointer to current entry in dp->i_offset,
  558                  * and distance past previous entry (if there
  559                  * is a previous entry in this block) in dp->i_count.
  560                  * Save directory inode pointer in ndp->ni_dvp for dirremove().
  561                  *
  562                  * Technically we shouldn't be setting these in the
  563                  * WANTPARENT case (first lookup in rename()), but any
  564                  * lookups that will result in directory changes will
  565                  * overwrite these.
  566                  */
  567                 dp->i_offset = i_offset;
  568                 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
  569                         dp->i_count = 0;
  570                 else
  571                         dp->i_count = dp->i_offset - prevoff;
  572                 if (dd_ino != NULL)
  573                         return (0);
  574                 if (dp->i_number == ino) {
  575                         VREF(vdp);
  576                         *vpp = vdp;
  577                         return (0);
  578                 }
  579                 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
  580                     &tdp)) != 0)
  581                         return (error);
  582                 /*
  583                  * If directory is "sticky", then user must own
  584                  * the directory, or the file in it, else she
  585                  * may not delete it (unless she's root). This
  586                  * implements append-only directories.
  587                  */
  588                 if ((dp->i_mode & ISVTX) &&
  589                     cred->cr_uid != 0 &&
  590                     cred->cr_uid != dp->i_uid &&
  591                     VTOI(tdp)->i_uid != cred->cr_uid) {
  592                         vput(tdp);
  593                         return (EPERM);
  594                 }
  595                 *vpp = tdp;
  596                 return (0);
  597         }
  598 
  599         /*
  600          * If rewriting (RENAME), return the inode and the
  601          * information required to rewrite the present directory
  602          * Must get inode of directory entry to verify it's a
  603          * regular file, or empty directory.
  604          */
  605         if (nameiop == RENAME && (flags & ISLASTCN)) {
  606                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
  607                         return (error);
  608                 /*
  609                  * Careful about locking second inode.
  610                  * This can only occur if the target is ".".
  611                  */
  612                 dp->i_offset = i_offset;
  613                 if (dp->i_number == ino)
  614                         return (EISDIR);
  615                 if (dd_ino != NULL)
  616                         return (0);
  617                 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
  618                     &tdp)) != 0)
  619                         return (error);
  620                 *vpp = tdp;
  621                 cnp->cn_flags |= SAVENAME;
  622                 return (0);
  623         }
  624         if (dd_ino != NULL)
  625                 return (0);
  626 
  627         /*
  628          * Step through the translation in the name.  We do not `vput' the
  629          * directory because we may need it again if a symbolic link
  630          * is relative to the current directory.  Instead we save it
  631          * unlocked as "pdp".  We must get the target inode before unlocking
  632          * the directory to insure that the inode will not be removed
  633          * before we get it.  We prevent deadlock by always fetching
  634          * inodes from the root, moving down the directory tree. Thus
  635          * when following backward pointers ".." we must unlock the
  636          * parent directory before getting the requested directory.
  637          * There is a potential race condition here if both the current
  638          * and parent directories are removed before the VFS_VGET for the
  639          * inode associated with ".." returns.  We hope that this occurs
  640          * infrequently since we cannot avoid this race condition without
  641          * implementing a sophisticated deadlock detection algorithm.
  642          * Note also that this simple deadlock detection scheme will not
  643          * work if the file system has any hard links other than ".."
  644          * that point backwards in the directory structure.
  645          */
  646         pdp = vdp;
  647         if (flags & ISDOTDOT) {
  648                 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
  649                 if (pdp->v_iflag & VI_DOOMED) {
  650                         if (error == 0)
  651                                 vput(tdp);
  652                         error = ENOENT;
  653                 }
  654                 if (error)
  655                         return (error);
  656                 /*
  657                  * Recheck that ".." entry in the vdp directory points
  658                  * to the inode we looked up before vdp lock was
  659                  * dropped.
  660                  */
  661                 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
  662                 if (error) {
  663                         vput(tdp);
  664                         return (error);
  665                 }
  666                 if (ino1 != ino) {
  667                         vput(tdp);
  668                         goto restart;
  669                 }
  670                 *vpp = tdp;
  671         } else if (dp->i_number == ino) {
  672                 VREF(vdp);      /* we want ourself, ie "." */
  673                 /*
  674                  * When we lookup "." we still can be asked to lock it
  675                  * differently.
  676                  */
  677                 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
  678                 if (ltype != VOP_ISLOCKED(vdp)) {
  679                         if (ltype == LK_EXCLUSIVE)
  680                                 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
  681                         else    /* if (ltype == LK_SHARED) */
  682                                 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
  683                 }
  684                 *vpp = vdp;
  685         } else {
  686                 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
  687                     &tdp)) != 0)
  688                         return (error);
  689                 *vpp = tdp;
  690         }
  691 
  692         /*
  693          * Insert name into cache if appropriate.
  694          */
  695         if (cnp->cn_flags & MAKEENTRY)
  696                 cache_enter(vdp, *vpp, cnp);
  697         return (0);
  698 }
  699 
  700 int
  701 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
  702     const char *name, int namelen, int *entryoffsetinblockp,
  703     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
  704     struct ext2fs_searchslot *ssp)
  705 {
  706         struct vnode *vdp;
  707         struct ext2fs_direct_2 *ep, *top;
  708         uint32_t bsize = ip->i_e2fs->e2fs_bsize;
  709         int offset = *entryoffsetinblockp;
  710         int namlen;
  711 
  712         vdp = ITOV(ip);
  713 
  714         ep = (struct ext2fs_direct_2 *)((char *)data + offset);
  715         top = (struct ext2fs_direct_2 *)((char *)data +
  716             bsize - EXT2_DIR_REC_LEN(0));
  717 
  718         while (ep < top) {
  719                 /*
  720                  * Full validation checks are slow, so we only check
  721                  * enough to insure forward progress through the
  722                  * directory. Complete checks can be run by setting
  723                  * "vfs.e2fs.dirchk" to be true.
  724                  */
  725                 if (ep->e2d_reclen == 0 ||
  726                     (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
  727                         int i;
  728 
  729                         ext2_dirbad(ip, *offp, "mangled entry");
  730                         i = bsize - (offset & (bsize - 1));
  731                         *offp += i;
  732                         offset += i;
  733                         continue;
  734                 }
  735 
  736                 /*
  737                  * If an appropriate sized slot has not yet been found,
  738                  * check to see if one is available. Also accumulate space
  739                  * in the current block so that we can determine if
  740                  * compaction is viable.
  741                  */
  742                 if (ssp->slotstatus != FOUND) {
  743                         int size = ep->e2d_reclen;
  744 
  745                         if (ep->e2d_ino != 0)
  746                                 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
  747                         if (size > 0) {
  748                                 if (size >= ssp->slotneeded) {
  749                                         ssp->slotstatus = FOUND;
  750                                         ssp->slotoffset = *offp;
  751                                         ssp->slotsize = ep->e2d_reclen;
  752                                 } else if (ssp->slotstatus == NONE) {
  753                                         ssp->slotfreespace += size;
  754                                         if (ssp->slotoffset == -1)
  755                                                 ssp->slotoffset = *offp;
  756                                         if (ssp->slotfreespace >= ssp->slotneeded) {
  757                                                 ssp->slotstatus = COMPACT;
  758                                                 ssp->slotsize = *offp +
  759                                                     ep->e2d_reclen -
  760                                                     ssp->slotoffset;
  761                                         }
  762                                 }
  763                         }
  764                 }
  765                 /*
  766                  * Check for a name match.
  767                  */
  768                 if (ep->e2d_ino) {
  769                         namlen = ep->e2d_namlen;
  770                         if (namlen == namelen &&
  771                             !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
  772                                 /*
  773                                  * Save directory entry's inode number and
  774                                  * reclen in ndp->ni_ufs area, and release
  775                                  * directory buffer.
  776                                  */
  777                                 *foundp = 1;
  778                                 return (0);
  779                         }
  780                 }
  781                 *prevoffp = *offp;
  782                 *offp += ep->e2d_reclen;
  783                 offset += ep->e2d_reclen;
  784                 *entryoffsetinblockp = offset;
  785                 if (ep->e2d_ino)
  786                         *endusefulp = *offp;
  787                 /*
  788                  * Get pointer to the next entry.
  789                  */
  790                 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
  791         }
  792 
  793         return (0);
  794 }
  795 
  796 void
  797 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
  798 {
  799         struct mount *mp;
  800 
  801         mp = ITOV(ip)->v_mount;
  802         if ((mp->mnt_flag & MNT_RDONLY) == 0)
  803                 panic("ext2_dirbad: %s: bad dir ino %lu at offset %ld: %s\n",
  804                         mp->mnt_stat.f_mntonname, (u_long)ip->i_number,(long)offset, how);
  805         else
  806         (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
  807             mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
  808 
  809 }
  810 
  811 /*
  812  * Do consistency checking on a directory entry:
  813  *      record length must be multiple of 4
  814  *      entry must fit in rest of its DIRBLKSIZ block
  815  *      record must be large enough to contain entry
  816  *      name is not longer than MAXNAMLEN
  817  *      name must be as long as advertised, and null terminated
  818  */
  819 /*
  820  *      changed so that it confirms to ext2_check_dir_entry
  821  */
  822 static int
  823 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
  824     int entryoffsetinblock)
  825 {
  826         int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
  827 
  828         char *error_msg = NULL;
  829 
  830         if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
  831                 error_msg = "rec_len is smaller than minimal";
  832         else if (de->e2d_reclen % 4 != 0)
  833                 error_msg = "rec_len % 4 != 0";
  834         else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
  835                 error_msg = "reclen is too small for name_len";
  836         else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
  837                 error_msg = "directory entry across blocks";
  838         /* else LATER
  839              if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
  840                 error_msg = "inode out of bounds";
  841         */
  842 
  843         if (error_msg != NULL) {
  844                 printf("bad directory entry: %s\n", error_msg);
  845                 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
  846                         entryoffsetinblock, (unsigned long)de->e2d_ino,
  847                         de->e2d_reclen, de->e2d_namlen);
  848         }
  849         return error_msg == NULL ? 0 : 1;
  850 }
  851 
  852 /*
  853  * Write a directory entry after a call to namei, using the parameters
  854  * that it left in nameidata.  The argument ip is the inode which the new
  855  * directory entry will refer to.  Dvp is a pointer to the directory to
  856  * be written, which was left locked by namei. Remaining parameters
  857  * (dp->i_offset, dp->i_count) indicate how the space for the new
  858  * entry is to be obtained.
  859  */
  860 int
  861 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
  862 {
  863         struct inode *dp;
  864         struct ext2fs_direct_2 newdir;
  865         struct iovec aiov;
  866         struct uio auio;
  867         int error, newentrysize;
  868         int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
  869 
  870 
  871 #ifdef INVARIANTS
  872         if ((cnp->cn_flags & SAVENAME) == 0)
  873                 panic("ext2_direnter: missing name");
  874 #endif
  875         dp = VTOI(dvp);
  876         newdir.e2d_ino = ip->i_number;
  877         newdir.e2d_namlen = cnp->cn_namelen;
  878         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
  879             EXT2F_INCOMPAT_FTYPE))
  880                 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
  881         else
  882                 newdir.e2d_type = EXT2_FT_UNKNOWN;
  883         bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
  884         newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen);
  885 
  886         if (ext2_htree_has_idx(dp)) {
  887                 error = ext2_htree_add_entry(dvp, &newdir, cnp);
  888                 if (error) {
  889                         dp->i_flag &= ~IN_E3INDEX;
  890                         dp->i_flag |= IN_CHANGE | IN_UPDATE;
  891                 }
  892                 return (error);
  893         }
  894 
  895         if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
  896             !ext2_htree_has_idx(dp)) {
  897                 if ((dp->i_size / DIRBLKSIZ) == 1 &&
  898                     dp->i_offset == DIRBLKSIZ) {
  899                         /*
  900                          * Making indexed directory when one block is not
  901                          * enough to save all entries.
  902                          */
  903                         return ext2_htree_create_index(dvp, cnp, &newdir);
  904                 }
  905         }
  906 
  907         if (dp->i_count == 0) {
  908                 /*
  909                  * If dp->i_count is 0, then namei could find no
  910                  * space in the directory. Here, dp->i_offset will
  911                  * be on a directory block boundary and we will write the
  912                  * new entry into a fresh block.
  913                  */
  914                 if (dp->i_offset & (DIRBLKSIZ - 1))
  915                         panic("ext2_direnter: newblk");
  916                 auio.uio_offset = dp->i_offset;
  917                 newdir.e2d_reclen = DIRBLKSIZ;
  918                 auio.uio_resid = newentrysize;
  919                 aiov.iov_len = newentrysize;
  920                 aiov.iov_base = (caddr_t)&newdir;
  921                 auio.uio_iov = &aiov;
  922                 auio.uio_iovcnt = 1;
  923                 auio.uio_rw = UIO_WRITE;
  924                 auio.uio_segflg = UIO_SYSSPACE;
  925                 auio.uio_td = (struct thread *)0;
  926                 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
  927                 if (DIRBLKSIZ >
  928                     VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
  929                         /* XXX should grow with balloc() */
  930                         panic("ext2_direnter: frag size");
  931                 else if (!error) {
  932                         dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
  933                         dp->i_flag |= IN_CHANGE;
  934                 }
  935                 return (error);
  936         }
  937 
  938         error = ext2_add_entry(dvp, &newdir);
  939         if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
  940                 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
  941                     cnp->cn_cred, cnp->cn_thread);
  942         return (error);
  943 }
  944 
  945 /*
  946  * Insert an entry into the directory block.
  947  * Compact the contents.
  948  */
  949 int
  950 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
  951 {
  952         struct ext2fs_direct_2 *ep, *nep;
  953         struct inode *dp;
  954         struct buf *bp;
  955         u_int dsize;
  956         int error, loc, newentrysize, spacefree;
  957         char *dirbuf;
  958 
  959         dp = VTOI(dvp);
  960 
  961         /*
  962          * If dp->i_count is non-zero, then namei found space
  963          * for the new entry in the range dp->i_offset to
  964          * dp->i_offset + dp->i_count in the directory.
  965          * To use this space, we may have to compact the entries located
  966          * there, by copying them together towards the beginning of the
  967          * block, leaving the free space in one usable chunk at the end.
  968          */
  969 
  970         /*
  971          * Increase size of directory if entry eats into new space.
  972          * This should never push the size past a new multiple of
  973          * DIRBLKSIZE.
  974          *
  975          * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
  976          */
  977         if (dp->i_offset + dp->i_count > dp->i_size)
  978                 dp->i_size = dp->i_offset + dp->i_count;
  979         /*
  980          * Get the block containing the space for the new directory entry.
  981          */
  982         if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
  983             &bp)) != 0)
  984                 return (error);
  985         /*
  986          * Find space for the new entry. In the simple case, the entry at
  987          * offset base will have the space. If it does not, then namei
  988          * arranged that compacting the region dp->i_offset to
  989          * dp->i_offset + dp->i_count would yield the
  990          * space.
  991          */
  992         newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
  993         ep = (struct ext2fs_direct_2 *)dirbuf;
  994         dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
  995         spacefree = ep->e2d_reclen - dsize;
  996         for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
  997                 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
  998                 if (ep->e2d_ino) {
  999                         /* trim the existing slot */
 1000                         ep->e2d_reclen = dsize;
 1001                         ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
 1002                 } else {
 1003                         /* overwrite; nothing there; header is ours */
 1004                         spacefree += dsize;
 1005                 }
 1006                 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
 1007                 spacefree += nep->e2d_reclen - dsize;
 1008                 loc += nep->e2d_reclen;
 1009                 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
 1010         }
 1011         /*
 1012          * Update the pointer fields in the previous entry (if any),
 1013          * copy in the new entry, and write out the block.
 1014          */
 1015         if (ep->e2d_ino == 0) {
 1016                 if (spacefree + dsize < newentrysize)
 1017                         panic("ext2_direnter: compact1");
 1018                 entry->e2d_reclen = spacefree + dsize;
 1019         } else {
 1020                 if (spacefree < newentrysize)
 1021                         panic("ext2_direnter: compact2");
 1022                 entry->e2d_reclen = spacefree;
 1023                 ep->e2d_reclen = dsize;
 1024                 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
 1025         }
 1026         bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
 1027         if (DOINGASYNC(dvp)) {
 1028                 bdwrite(bp);
 1029                 error = 0;
 1030         } else {
 1031                 error = bwrite(bp);
 1032         }
 1033         dp->i_flag |= IN_CHANGE | IN_UPDATE;
 1034         return (error);
 1035 }
 1036 
 1037 /*
 1038  * Remove a directory entry after a call to namei, using
 1039  * the parameters which it left in nameidata. The entry
 1040  * dp->i_offset contains the offset into the directory of the
 1041  * entry to be eliminated.  The dp->i_count field contains the
 1042  * size of the previous record in the directory.  If this
 1043  * is 0, the first entry is being deleted, so we need only
 1044  * zero the inode number to mark the entry as free.  If the
 1045  * entry is not the first in the directory, we must reclaim
 1046  * the space of the now empty record by adding the record size
 1047  * to the size of the previous entry.
 1048  */
 1049 int
 1050 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
 1051 {
 1052         struct inode *dp;
 1053         struct ext2fs_direct_2 *ep, *rep;
 1054         struct buf *bp;
 1055         int error;
 1056 
 1057         dp = VTOI(dvp);
 1058         if (dp->i_count == 0) {
 1059                 /*
 1060                  * First entry in block: set d_ino to zero.
 1061                  */
 1062                 if ((error =
 1063                     ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
 1064                     &bp)) != 0)
 1065                         return (error);
 1066                 ep->e2d_ino = 0;
 1067                 error = bwrite(bp);
 1068                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
 1069                 return (error);
 1070         }
 1071         /*
 1072          * Collapse new free space into previous entry.
 1073          */
 1074         if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
 1075             (char **)&ep, &bp)) != 0)
 1076                 return (error);
 1077 
 1078         /* Set 'rep' to the entry being removed. */
 1079         if (dp->i_count == 0)
 1080                 rep = ep;
 1081         else
 1082                 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
 1083         ep->e2d_reclen += rep->e2d_reclen;
 1084         if (DOINGASYNC(dvp) && dp->i_count != 0)
 1085                 bdwrite(bp);
 1086         else
 1087                 error = bwrite(bp);
 1088         dp->i_flag |= IN_CHANGE | IN_UPDATE;
 1089         return (error);
 1090 }
 1091 
 1092 /*
 1093  * Rewrite an existing directory entry to point at the inode
 1094  * supplied.  The parameters describing the directory entry are
 1095  * set up by a call to namei.
 1096  */
 1097 int
 1098 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
 1099 {
 1100         struct buf *bp;
 1101         struct ext2fs_direct_2 *ep;
 1102         struct vnode *vdp = ITOV(dp);
 1103         int error;
 1104 
 1105         if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
 1106             &bp)) != 0)
 1107                 return (error);
 1108         ep->e2d_ino = ip->i_number;
 1109         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
 1110             EXT2F_INCOMPAT_FTYPE))
 1111                 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
 1112         else
 1113                 ep->e2d_type = EXT2_FT_UNKNOWN;
 1114         error = bwrite(bp);
 1115         dp->i_flag |= IN_CHANGE | IN_UPDATE;
 1116         return (error);
 1117 }
 1118 
 1119 /*
 1120  * Check if a directory is empty or not.
 1121  * Inode supplied must be locked.
 1122  *
 1123  * Using a struct dirtemplate here is not precisely
 1124  * what we want, but better than using a struct direct.
 1125  *
 1126  * NB: does not handle corrupted directories.
 1127  */
 1128 int
 1129 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
 1130 {
 1131         off_t off;
 1132         struct dirtemplate dbuf;
 1133         struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
 1134         int error, namlen;
 1135         ssize_t count;
 1136 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
 1137 
 1138         for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
 1139                 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
 1140                     off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
 1141                     NOCRED, &count, (struct thread *)0);
 1142                 /*
 1143                  * Since we read MINDIRSIZ, residual must
 1144                  * be 0 unless we're at end of file.
 1145                  */
 1146                 if (error || count != 0)
 1147                         return (0);
 1148                 /* avoid infinite loops */
 1149                 if (dp->e2d_reclen == 0)
 1150                         return (0);
 1151                 /* skip empty entries */
 1152                 if (dp->e2d_ino == 0)
 1153                         continue;
 1154                 /* accept only "." and ".." */
 1155                 namlen = dp->e2d_namlen;
 1156                 if (namlen > 2)
 1157                         return (0);
 1158                 if (dp->e2d_name[0] != '.')
 1159                         return (0);
 1160                 /*
 1161                  * At this point namlen must be 1 or 2.
 1162                  * 1 implies ".", 2 implies ".." if second
 1163                  * char is also "."
 1164                  */
 1165                 if (namlen == 1)
 1166                         continue;
 1167                 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
 1168                         continue;
 1169                 return (0);
 1170         }
 1171         return (1);
 1172 }
 1173 
 1174 /*
 1175  * Check if source directory is in the path of the target directory.
 1176  * Target is supplied locked, source is unlocked.
 1177  * The target is always vput before returning.
 1178  */
 1179 int
 1180 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
 1181 {
 1182         struct vnode *vp;
 1183         int error, namlen;
 1184         struct dirtemplate dirbuf;
 1185 
 1186         vp = ITOV(target);
 1187         if (target->i_number == source->i_number) {
 1188                 error = EEXIST;
 1189                 goto out;
 1190         }
 1191         if (target->i_number == EXT2_ROOTINO) {
 1192                 error = 0;
 1193                 goto out;
 1194         }
 1195 
 1196         for (;;) {
 1197                 if (vp->v_type != VDIR) {
 1198                         error = ENOTDIR;
 1199                         break;
 1200                 }
 1201                 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
 1202                     sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
 1203                     IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
 1204                     NULL);
 1205                 if (error != 0)
 1206                         break;
 1207                 namlen = dirbuf.dotdot_type;    /* like ufs little-endian */
 1208                 if (namlen != 2 ||
 1209                     dirbuf.dotdot_name[0] != '.' ||
 1210                     dirbuf.dotdot_name[1] != '.') {
 1211                         error = ENOTDIR;
 1212                         break;
 1213                 }
 1214                 if (dirbuf.dotdot_ino == source->i_number) {
 1215                         error = EINVAL;
 1216                         break;
 1217                 }
 1218                 if (dirbuf.dotdot_ino == EXT2_ROOTINO)
 1219                         break;
 1220                 vput(vp);
 1221                 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
 1222                     LK_EXCLUSIVE, &vp)) != 0) {
 1223                         vp = NULL;
 1224                         break;
 1225                 }
 1226         }
 1227 
 1228 out:
 1229         if (error == ENOTDIR)
 1230                 printf("checkpath: .. not a directory\n");
 1231         if (vp != NULL)
 1232                 vput(vp);
 1233         return (error);
 1234 }

Cache object: bb6656b340ae94a133921c9b4584c0a7


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