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/isofs/cd9660/cd9660_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, 1994
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley
    6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
    7  * Support code is derived from software contributed to Berkeley
    8  * by Atsushi Murai (amurai@spec.co.jp).
    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  *      from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
   39  *      @(#)cd9660_lookup.c     8.2 (Berkeley) 1/23/94
   40  */
   41 
   42 #include <sys/cdefs.h>
   43 __FBSDID("$FreeBSD: releng/5.2/sys/isofs/cd9660/cd9660_lookup.c 120492 2003-09-26 20:26:25Z fjoe $");
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/namei.h>
   48 #include <sys/bio.h>
   49 #include <sys/buf.h>
   50 #include <sys/vnode.h>
   51 #include <sys/mount.h>
   52 
   53 #include <isofs/cd9660/iso.h>
   54 #include <isofs/cd9660/cd9660_node.h>
   55 #include <isofs/cd9660/iso_rrip.h>
   56 
   57 /*
   58  * Convert a component of a pathname into a pointer to a locked inode.
   59  * This is a very central and rather complicated routine.
   60  * If the filesystem is not maintained in a strict tree hierarchy,
   61  * this can result in a deadlock situation (see comments in code below).
   62  *
   63  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
   64  * whether the name is to be looked up, created, renamed, or deleted.
   65  * When CREATE, RENAME, or DELETE is specified, information usable in
   66  * creating, renaming, or deleting a directory entry may be calculated.
   67  * If flag has LOCKPARENT or'ed into it and the target of the pathname
   68  * exists, lookup returns both the target and its parent directory locked.
   69  * When creating or renaming and LOCKPARENT is specified, the target may
   70  * not be ".".  When deleting and LOCKPARENT is specified, the target may
   71  * be "."., but the caller must check to ensure it does an vrele and iput
   72  * instead of two iputs.
   73  *
   74  * Overall outline of ufs_lookup:
   75  *
   76  *      search for name in directory, to found or notfound
   77  * notfound:
   78  *      if creating, return locked directory, leaving info on available slots
   79  *      else return error
   80  * found:
   81  *      if at end of path and deleting, return information to allow delete
   82  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
   83  *        inode and return info to allow rewrite
   84  *      if not at end, add name to cache; if at end and neither creating
   85  *        nor deleting, add name to cache
   86  *
   87  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
   88  */
   89 int
   90 cd9660_lookup(ap)
   91         struct vop_cachedlookup_args /* {
   92                 struct vnode *a_dvp;
   93                 struct vnode **a_vpp;
   94                 struct componentname *a_cnp;
   95         } */ *ap;
   96 {
   97         register struct vnode *vdp;     /* vnode for directory being searched */
   98         register struct iso_node *dp;   /* inode for directory being searched */
   99         register struct iso_mnt *imp;   /* filesystem that directory is in */
  100         struct buf *bp;                 /* a buffer of directory entries */
  101         struct iso_directory_record *ep = 0;/* the current directory entry */
  102         int entryoffsetinblock;         /* offset of ep in bp's buffer */
  103         int saveoffset = 0;             /* offset of last directory entry in dir */
  104         int numdirpasses;               /* strategy for directory search */
  105         doff_t endsearch;               /* offset to end directory search */
  106         struct vnode *pdp;              /* saved dp during symlink work */
  107         struct vnode *tdp;              /* returned by cd9660_vget_internal */
  108         u_long bmask;                   /* block offset mask */
  109         int lockparent;                 /* 1 => lockparent flag is set */
  110         int error;
  111         ino_t ino = 0;
  112         int reclen;
  113         u_short namelen;
  114         int isoflags;
  115         char altname[NAME_MAX];
  116         int res;
  117         int assoc, len;
  118         char *name;
  119         struct vnode **vpp = ap->a_vpp;
  120         struct componentname *cnp = ap->a_cnp;
  121         int flags = cnp->cn_flags;
  122         int nameiop = cnp->cn_nameiop;
  123         struct thread *td = cnp->cn_thread;
  124 
  125         bp = NULL;
  126         *vpp = NULL;
  127         vdp = ap->a_dvp;
  128         dp = VTOI(vdp);
  129         imp = dp->i_mnt;
  130         lockparent = flags & LOCKPARENT;
  131         cnp->cn_flags &= ~PDIRUNLOCK;
  132 
  133         /*
  134          * We now have a segment name to search for, and a directory to search.
  135          */
  136 
  137         len = cnp->cn_namelen;
  138         name = cnp->cn_nameptr;
  139         /*
  140          * A leading `=' means, we are looking for an associated file
  141          */
  142         if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
  143         {
  144                 len--;
  145                 name++;
  146         }
  147 
  148         /*
  149          * If there is cached information on a previous search of
  150          * this directory, pick up where we last left off.
  151          * We cache only lookups as these are the most common
  152          * and have the greatest payoff. Caching CREATE has little
  153          * benefit as it usually must search the entire directory
  154          * to determine that the entry does not exist. Caching the
  155          * location of the last DELETE or RENAME has not reduced
  156          * profiling time and hence has been removed in the interest
  157          * of simplicity.
  158          */
  159         bmask = imp->im_bmask;
  160         if (nameiop != LOOKUP || dp->i_diroff == 0 ||
  161             dp->i_diroff > dp->i_size) {
  162                 entryoffsetinblock = 0;
  163                 dp->i_offset = 0;
  164                 numdirpasses = 1;
  165         } else {
  166                 dp->i_offset = dp->i_diroff;
  167                 if ((entryoffsetinblock = dp->i_offset & bmask) &&
  168                     (error = cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
  169                                 return (error);
  170                 numdirpasses = 2;
  171                 nchstats.ncs_2passes++;
  172         }
  173         endsearch = dp->i_size;
  174         
  175 searchloop:
  176         while (dp->i_offset < endsearch) {
  177                 /*
  178                  * If offset is on a block boundary,
  179                  * read the next directory block.
  180                  * Release previous if it exists.
  181                  */
  182                 if ((dp->i_offset & bmask) == 0) {
  183                         if (bp != NULL)
  184                                 brelse(bp);
  185                         if ((error =
  186                             cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
  187                                 return (error);
  188                         entryoffsetinblock = 0;
  189                 }
  190                 /*
  191                  * Get pointer to next entry.
  192                  */
  193                 ep = (struct iso_directory_record *)
  194                         ((char *)bp->b_data + entryoffsetinblock);
  195                 
  196                 reclen = isonum_711(ep->length);
  197                 if (reclen == 0) {
  198                         /* skip to next block, if any */
  199                         dp->i_offset =
  200                             (dp->i_offset & ~bmask) + imp->logical_block_size;
  201                         continue;
  202                 }
  203 
  204                 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
  205                         /* illegal entry, stop */
  206                         break;
  207 
  208                 if (entryoffsetinblock + reclen > imp->logical_block_size)
  209                         /* entries are not allowed to cross boundaries */
  210                         break;
  211                 
  212                 namelen = isonum_711(ep->name_len);
  213                 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
  214                                       &ep->date[6]: ep->flags);
  215 
  216                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
  217                         /* illegal entry, stop */
  218                         break;
  219                 
  220                 /*
  221                  * Check for a name match.
  222                  */
  223                 switch (imp->iso_ftype) {
  224                 default:
  225                         if (!(isoflags & 4) == !assoc) {
  226                                 if ((len == 1
  227                                      && *name == '.')
  228                                     || (flags & ISDOTDOT)) {
  229                                         if (namelen == 1
  230                                             && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
  231                                                 /*
  232                                                  * Save directory entry's inode number and
  233                                                  * release directory buffer.
  234                                                  */
  235                                                 dp->i_ino = isodirino(ep, imp);
  236                                                 goto found;
  237                                         }
  238                                         if (namelen != 1
  239                                             || ep->name[0] != 0)
  240                                                 goto notfound;
  241                                 } else if (!(res = isofncmp(name, len,
  242                                                             ep->name, namelen,
  243                                                             imp->joliet_level,
  244                                                             imp->im_flags,
  245                                                             imp->im_d2l,
  246                                                             imp->im_l2d))) {
  247                                         if (isoflags & 2)
  248                                                 ino = isodirino(ep, imp);
  249                                         else
  250                                                 ino = dbtob(bp->b_blkno)
  251                                                         + entryoffsetinblock;
  252                                         saveoffset = dp->i_offset;
  253                                 } else if (ino)
  254                                         goto foundino;
  255 #ifdef  NOSORTBUG       /* On some CDs directory entries are not sorted correctly */
  256                                 else if (res < 0)
  257                                         goto notfound;
  258                                 else if (res > 0 && numdirpasses == 2)
  259                                         numdirpasses++;
  260 #endif
  261                         }
  262                         break;
  263                 case ISO_FTYPE_RRIP:
  264                         if (isonum_711(ep->flags)&2)
  265                                 ino = isodirino(ep, imp);
  266                         else
  267                                 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
  268                         dp->i_ino = ino;
  269                         cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
  270                         if (namelen == cnp->cn_namelen
  271                             && !bcmp(name,altname,namelen))
  272                                 goto found;
  273                         ino = 0;
  274                         break;
  275                 }
  276                 dp->i_offset += reclen;
  277                 entryoffsetinblock += reclen;
  278         }
  279         if (ino) {
  280 foundino:
  281                 dp->i_ino = ino;
  282                 if (saveoffset != dp->i_offset) {
  283                         if (lblkno(imp, dp->i_offset) !=
  284                             lblkno(imp, saveoffset)) {
  285                                 if (bp != NULL)
  286                                         brelse(bp);
  287                                 if ((error = cd9660_blkatoff(vdp,
  288                                     (off_t)saveoffset, NULL, &bp)) != 0)
  289                                         return (error);
  290                         }
  291                         entryoffsetinblock = saveoffset & bmask;
  292                         ep = (struct iso_directory_record *)
  293                                 ((char *)bp->b_data + entryoffsetinblock);
  294                         dp->i_offset = saveoffset;
  295                 }
  296                 goto found;
  297         }
  298 notfound:
  299         /*
  300          * If we started in the middle of the directory and failed
  301          * to find our target, we must check the beginning as well.
  302          */
  303         if (numdirpasses == 2) {
  304                 numdirpasses--;
  305                 dp->i_offset = 0;
  306                 endsearch = dp->i_diroff;
  307                 goto searchloop;
  308         }
  309         if (bp != NULL)
  310                 brelse(bp);
  311 
  312         /*
  313          * Insert name into cache (as non-existent) if appropriate.
  314          */
  315         if (cnp->cn_flags & MAKEENTRY)
  316                 cache_enter(vdp, *vpp, cnp);
  317         if (nameiop == CREATE || nameiop == RENAME)
  318                 return (EROFS);
  319         return (ENOENT);
  320 
  321 found:
  322         if (numdirpasses == 2)
  323                 nchstats.ncs_pass2++;
  324         
  325         /*
  326          * Found component in pathname.
  327          * If the final component of path name, save information
  328          * in the cache as to where the entry was found.
  329          */
  330         if ((flags & ISLASTCN) && nameiop == LOOKUP)
  331                 dp->i_diroff = dp->i_offset;
  332 
  333         /*
  334          * Step through the translation in the name.  We do not `iput' the
  335          * directory because we may need it again if a symbolic link
  336          * is relative to the current directory.  Instead we save it
  337          * unlocked as "pdp".  We must get the target inode before unlocking
  338          * the directory to insure that the inode will not be removed
  339          * before we get it.  We prevent deadlock by always fetching
  340          * inodes from the root, moving down the directory tree. Thus
  341          * when following backward pointers ".." we must unlock the
  342          * parent directory before getting the requested directory.
  343          * There is a potential race condition here if both the current
  344          * and parent directories are removed before the `iget' for the
  345          * inode associated with ".." returns.  We hope that this occurs
  346          * infrequently since we cannot avoid this race condition without
  347          * implementing a sophisticated deadlock detection algorithm.
  348          * Note also that this simple deadlock detection scheme will not
  349          * work if the filesystem has any hard links other than ".."
  350          * that point backwards in the directory structure.
  351          */
  352         pdp = vdp;
  353         /*
  354          * If ino is different from dp->i_ino,
  355          * it's a relocated directory.
  356          */
  357         if (flags & ISDOTDOT) {
  358                 VOP_UNLOCK(pdp, 0, td); /* race to get the inode */
  359                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino,
  360                                              LK_EXCLUSIVE, &tdp,
  361                                              dp->i_ino != ino, ep);
  362                 brelse(bp);
  363                 if (error) {
  364                         vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
  365                         return (error);
  366                 }
  367                 if (lockparent && (flags & ISLASTCN)) {
  368                         if ((error = vn_lock(pdp, LK_EXCLUSIVE, td)) != 0) {
  369                                 cnp->cn_flags |= PDIRUNLOCK;
  370                                 vput(tdp);
  371                                 return (error);
  372                         }
  373                 } else
  374                         cnp->cn_flags |= PDIRUNLOCK;
  375                 *vpp = tdp;
  376         } else if (dp->i_number == dp->i_ino) {
  377                 brelse(bp);
  378                 VREF(vdp);      /* we want ourself, ie "." */
  379                 *vpp = vdp;
  380         } else {
  381                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino,
  382                                              LK_EXCLUSIVE, &tdp,
  383                                              dp->i_ino != ino, ep);
  384                 brelse(bp);
  385                 if (error)
  386                         return (error);
  387                 if (!lockparent || !(flags & ISLASTCN)) {
  388                         cnp->cn_flags |= PDIRUNLOCK;
  389                         VOP_UNLOCK(pdp, 0, td);
  390                 }
  391                 *vpp = tdp;
  392         }
  393 
  394         /*
  395          * Insert name into cache if appropriate.
  396          */
  397         if (cnp->cn_flags & MAKEENTRY)
  398                 cache_enter(vdp, *vpp, cnp);
  399         return (0);
  400 }
  401 
  402 /*
  403  * Return buffer with the contents of block "offset" from the beginning of
  404  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
  405  * remaining space in the directory.
  406  */
  407 int
  408 cd9660_blkatoff(vp, offset, res, bpp)
  409         struct vnode *vp;
  410         off_t offset;
  411         char **res;
  412         struct buf **bpp;
  413 {
  414         struct iso_node *ip;
  415         register struct iso_mnt *imp;
  416         struct buf *bp;
  417         daddr_t lbn;
  418         int bsize, bshift, error;
  419 
  420         ip = VTOI(vp);
  421         imp = ip->i_mnt;
  422         lbn = lblkno(imp, offset);
  423         bsize = blksize(imp, ip, lbn);
  424         bshift = imp->im_bshift;
  425 
  426         if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
  427                 brelse(bp);
  428                 *bpp = NULL;
  429                 return (error);
  430         }
  431 
  432         /*
  433          * We must BMAP the buffer because the directory code may use b_blkno
  434          * to calculate the inode for certain types of directory entries.
  435          * We could get away with not doing it before we VMIO-backed the
  436          * directories because the buffers would get freed atomically with
  437          * the invalidation of their data.  But with VMIO-backed buffers
  438          * the buffers may be freed and then later reconstituted - and the
  439          * reconstituted buffer will have no knowledge of b_blkno.
  440          */
  441         if (bp->b_blkno == bp->b_lblkno) {
  442                 bp->b_blkno = (ip->iso_start + bp->b_lblkno) << (bshift - DEV_BSHIFT);
  443         }
  444 
  445         if (res)
  446                 *res = (char *)bp->b_data + blkoff(imp, offset);
  447         *bpp = bp;
  448         return (0);
  449 }

Cache object: b06cc655cc341451fabe35720abddd90


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