LCOV - code coverage report
Current view: top level - isofs/cd9660 - cd9660_lookup.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 175 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: cd9660_lookup.c,v 1.27 2018/05/02 02:24:55 visa Exp $ */
       2             : /*      $NetBSD: cd9660_lookup.c,v 1.18 1997/05/08 16:19:59 mycroft Exp $       */
       3             : 
       4             : /*-
       5             :  * Copyright (c) 1989, 1993, 1994
       6             :  *      The Regents of the University of California.  All rights reserved.
       7             :  *
       8             :  * This code is derived from software contributed to Berkeley
       9             :  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
      10             :  * Support code is derived from software contributed to Berkeley
      11             :  * by Atsushi Murai (amurai@spec.co.jp).
      12             :  *
      13             :  * Redistribution and use in source and binary forms, with or without
      14             :  * modification, are permitted provided that the following conditions
      15             :  * are met:
      16             :  * 1. Redistributions of source code must retain the above copyright
      17             :  *    notice, this list of conditions and the following disclaimer.
      18             :  * 2. Redistributions in binary form must reproduce the above copyright
      19             :  *    notice, this list of conditions and the following disclaimer in the
      20             :  *    documentation and/or other materials provided with the distribution.
      21             :  * 3. Neither the name of the University nor the names of its contributors
      22             :  *    may be used to endorse or promote products derived from this software
      23             :  *    without specific prior written permission.
      24             :  *
      25             :  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      26             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      27             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      28             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      29             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      30             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      31             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      32             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      33             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      34             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      35             :  * SUCH DAMAGE.
      36             :  *
      37             :  *      from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
      38             :  *
      39             :  *      @(#)cd9660_lookup.c     8.5 (Berkeley) 12/5/94
      40             :  */
      41             : 
      42             : #include <sys/param.h>
      43             : #include <sys/namei.h>
      44             : #include <sys/buf.h>
      45             : #include <sys/vnode.h>
      46             : #include <sys/lock.h>
      47             : #include <sys/mount.h>
      48             : #include <sys/systm.h>
      49             : #include <sys/malloc.h>
      50             : 
      51             : #include <isofs/cd9660/iso.h>
      52             : #include <isofs/cd9660/cd9660_extern.h>
      53             : #include <isofs/cd9660/cd9660_node.h>
      54             : #include <isofs/cd9660/iso_rrip.h>
      55             : 
      56             : struct  nchstats iso_nchstats;
      57             : 
      58             : /*
      59             :  * Convert a component of a pathname into a pointer to a locked inode.
      60             :  * This is a very central and rather complicated routine.
      61             :  * If the file system is not maintained in a strict tree hierarchy,
      62             :  * this can result in a deadlock situation (see comments in code below).
      63             :  *
      64             :  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
      65             :  * whether the name is to be looked up, created, renamed, or deleted.
      66             :  * When CREATE, RENAME, or DELETE is specified, information usable in
      67             :  * creating, renaming, or deleting a directory entry may be calculated.
      68             :  * If flag has LOCKPARENT or'ed into it and the target of the pathname
      69             :  * exists, lookup returns both the target and its parent directory locked.
      70             :  * When creating or renaming and LOCKPARENT is specified, the target may
      71             :  * not be ".".  When deleting and LOCKPARENT is specified, the target may
      72             :  * be "."., but the caller must check to ensure it does an vrele and iput
      73             :  * instead of two iputs.
      74             :  *
      75             :  * Overall outline of cd9660_lookup:
      76             :  *
      77             :  *      check accessibility of directory
      78             :  *      look for name in cache, if found, then if at end of path
      79             :  *        and deleting or creating, drop it, else return name
      80             :  *      search for name in directory, to found or notfound
      81             :  * notfound:
      82             :  *      if creating, return locked directory, leaving info on available slots
      83             :  *      else return error
      84             :  * found:
      85             :  *      if at end of path and deleting, return information to allow delete
      86             :  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
      87             :  *        inode and return info to allow rewrite
      88             :  *      if not at end, add name to cache; if at end and neither creating
      89             :  *        nor deleting, add name to cache
      90             :  *
      91             :  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
      92             :  */
      93             : int
      94           0 : cd9660_lookup(v)
      95             :         void *v;
      96             : {
      97           0 :         struct vop_lookup_args *ap = v;
      98             :         register struct vnode *vdp;     /* vnode for directory being searched */
      99             :         register struct iso_node *dp;   /* inode for directory being searched */
     100             :         register struct iso_mnt *imp;   /* file system that directory is in */
     101           0 :         struct buf *bp;                 /* a buffer of directory entries */
     102             :         struct iso_directory_record *ep = NULL;
     103             :                                         /* the current directory entry */
     104             :         int entryoffsetinblock;         /* offset of ep in bp's buffer */
     105             :         int saveoffset = -1;            /* offset of last directory entry in dir */
     106             :         int numdirpasses;               /* strategy for directory search */
     107             :         doff_t endsearch;               /* offset to end directory search */
     108             :         struct vnode *pdp;              /* saved dp during symlink work */
     109           0 :         struct vnode *tdp;              /* returned by cd9660_vget_internal */
     110             :         u_long bmask;                   /* block offset mask */
     111             :         int lockparent;                 /* 1 => lockparent flag is set */
     112             :         int error;
     113             :         cdino_t ino = 0;
     114             :         int reclen;
     115           0 :         u_short namelen;
     116             :         char *altname;
     117             :         int res;
     118             :         int assoc, len;
     119             :         char *name;
     120           0 :         struct vnode **vpp = ap->a_vpp;
     121           0 :         struct componentname *cnp = ap->a_cnp;
     122           0 :         struct ucred *cred = cnp->cn_cred;
     123             :         int flags;
     124           0 :         int nameiop = cnp->cn_nameiop;
     125             : 
     126           0 :         cnp->cn_flags &= ~PDIRUNLOCK;
     127           0 :         flags = cnp->cn_flags;
     128             : 
     129           0 :         bp = NULL;
     130           0 :         *vpp = NULL;
     131           0 :         vdp = ap->a_dvp;
     132           0 :         dp = VTOI(vdp);
     133           0 :         imp = dp->i_mnt;
     134           0 :         lockparent = flags & LOCKPARENT;
     135             :         
     136             :         /*
     137             :          * Check accessiblity of directory.
     138             :          */
     139           0 :         if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
     140           0 :                 return (error);
     141             : 
     142           0 :         if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
     143           0 :             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
     144           0 :                 return (EROFS);
     145             :         
     146             :         /*
     147             :          * We now have a segment name to search for, and a directory to search.
     148             :          *
     149             :          * Before tediously performing a linear scan of the directory,
     150             :          * check the name cache to see if the directory/name pair
     151             :          * we are looking for is known already.
     152             :          */
     153           0 :         if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
     154           0 :                 return (error);
     155             : 
     156           0 :         len = cnp->cn_namelen;
     157           0 :         name = cnp->cn_nameptr;
     158             :         /*
     159             :          * A leading `=' means, we are looking for an associated file
     160             :          */
     161           0 :         assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR);
     162           0 :         if (assoc) {
     163           0 :                 len--;
     164           0 :                 name++;
     165           0 :         }
     166             :         
     167             :         /*
     168             :          * If there is cached information on a previous search of
     169             :          * this directory, pick up where we last left off.
     170             :          * We cache only lookups as these are the most common
     171             :          * and have the greatest payoff. Caching CREATE has little
     172             :          * benefit as it usually must search the entire directory
     173             :          * to determine that the entry does not exist. Caching the
     174             :          * location of the last DELETE or RENAME has not reduced
     175             :          * profiling time and hence has been removed in the interest
     176             :          * of simplicity.
     177             :          */
     178           0 :         bmask = imp->im_bmask;
     179           0 :         if (nameiop != LOOKUP || dp->i_diroff == 0 ||
     180           0 :             dp->i_diroff > dp->i_size) {
     181             :                 entryoffsetinblock = 0;
     182           0 :                 dp->i_offset = 0;
     183             :                 numdirpasses = 1;
     184           0 :         } else {
     185           0 :                 dp->i_offset = dp->i_diroff;
     186           0 :                 if ((entryoffsetinblock = dp->i_offset & bmask) &&
     187           0 :                     (error = cd9660_bufatoff(dp, (off_t)dp->i_offset, NULL, 
     188             :                         &bp)))
     189           0 :                                 return (error);
     190             :                 numdirpasses = 2;
     191           0 :                 iso_nchstats.ncs_2passes++;
     192             :         }
     193           0 :         endsearch = dp->i_size;
     194             :         
     195             : searchloop:
     196           0 :         while (dp->i_offset < endsearch) {
     197             :                 /*
     198             :                  * If offset is on a block boundary,
     199             :                  * read the next directory block.
     200             :                  * Release previous if it exists.
     201             :                  */
     202           0 :                 if ((dp->i_offset & bmask) == 0) {
     203           0 :                         if (bp != NULL)
     204           0 :                                 brelse(bp);
     205           0 :                         error = cd9660_bufatoff(dp, (off_t)dp->i_offset,
     206             :                                              NULL, &bp);
     207           0 :                         if (error)
     208           0 :                                 return (error);
     209             :                         entryoffsetinblock = 0;
     210           0 :                 }
     211             :                 /*
     212             :                  * Get pointer to next entry.
     213             :                  */
     214           0 :                 ep = (struct iso_directory_record *)
     215           0 :                         ((char *)bp->b_data + entryoffsetinblock);
     216             :                 
     217           0 :                 reclen = isonum_711(ep->length);
     218           0 :                 if (reclen == 0) {
     219             :                         /* skip to next block, if any */
     220           0 :                         dp->i_offset =
     221           0 :                             (dp->i_offset & ~bmask) + imp->logical_block_size;
     222           0 :                         continue;
     223             :                 }
     224             :                 
     225           0 :                 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
     226             :                         /* illegal entry, stop */
     227             :                         break;
     228             :                 
     229           0 :                 if (entryoffsetinblock + reclen > imp->logical_block_size)
     230             :                         /* entries are not allowed to cross boundaries */
     231             :                         break;
     232             :                 
     233           0 :                 namelen = isonum_711(ep->name_len);
     234             :                 
     235           0 :                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
     236             :                         /* illegal entry, stop */
     237             :                         break;
     238             :                 
     239             :                 /*
     240             :                  * Check for a name match.
     241             :                  */
     242           0 :                 switch (imp->iso_ftype) {
     243             :                 default:
     244           0 :                         if ((!(isonum_711(ep->flags)&4)) == !assoc) {
     245           0 :                                 if ((len == 1
     246           0 :                                      && *name == '.')
     247           0 :                                     || (flags & ISDOTDOT)) {
     248           0 :                                         if (namelen == 1
     249           0 :                                             && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
     250             :                                                 /*
     251             :                                                  * Save directory entry's inode number and
     252             :                                                  * release directory buffer.
     253             :                                                  */
     254           0 :                                                 dp->i_ino = isodirino(ep, imp);
     255           0 :                                                 goto found;
     256             :                                         }
     257           0 :                                         if (namelen != 1
     258           0 :                                             || ep->name[0] != 0)
     259             :                                                 goto notfound;
     260           0 :                                 } else if (!(res = isofncmp(name, len, 
     261           0 :                                     ep->name, namelen, imp->joliet_level))) {
     262           0 :                                         if (isonum_711(ep->flags)&2)
     263           0 :                                                 ino = isodirino(ep, imp);
     264             :                                         else
     265           0 :                                                 ino = dbtob(bp->b_blkno)
     266           0 :                                                         + entryoffsetinblock;
     267           0 :                                         saveoffset = dp->i_offset;
     268           0 :                                 } else if (ino)
     269             :                                         goto foundino;
     270             : #ifdef  NOSORTBUG       /* On some CDs directory entries are not sorted correctly */
     271             :                                 else if (res < 0)
     272             :                                         goto notfound;
     273             :                                 else if (res > 0 && numdirpasses == 2)
     274             :                                         numdirpasses++;
     275             : #endif
     276             :                         }
     277             :                         break;
     278             :                 case ISO_FTYPE_RRIP:
     279           0 :                         if (isonum_711(ep->flags)&2)
     280           0 :                                 ino = isodirino(ep, imp);
     281             :                         else
     282           0 :                                 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
     283           0 :                         dp->i_ino = ino;
     284           0 :                         altname = malloc(NAME_MAX, M_TEMP, M_WAITOK);
     285           0 :                         cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
     286           0 :                         if (namelen == cnp->cn_namelen
     287           0 :                             && !bcmp(name,altname,namelen)) {
     288           0 :                                 free(altname, M_TEMP, 0);
     289           0 :                                 goto found;
     290             :                         }
     291           0 :                         free(altname, M_TEMP, 0);
     292             :                         ino = 0;
     293           0 :                         break;
     294             :                 }
     295           0 :                 dp->i_offset += reclen;
     296             :                 entryoffsetinblock += reclen;
     297             :         }
     298           0 :         if (ino) {
     299             : foundino:
     300           0 :                 dp->i_ino = ino;
     301           0 :                 if (saveoffset != dp->i_offset) {
     302           0 :                         if (lblkno(imp, dp->i_offset) !=
     303           0 :                             lblkno(imp, saveoffset)) {
     304           0 :                                 if (bp != NULL)
     305           0 :                                         brelse(bp);
     306           0 :                                 if ((error = cd9660_bufatoff(dp,
     307           0 :                                             (off_t)saveoffset, NULL, &bp)) != 0)
     308           0 :                                         return (error);
     309             :                         }
     310           0 :                         entryoffsetinblock = saveoffset & bmask;
     311           0 :                         ep = (struct iso_directory_record *)
     312           0 :                                 ((char *)bp->b_data + entryoffsetinblock);
     313           0 :                         dp->i_offset = saveoffset;
     314           0 :                 }
     315             :                 goto found;
     316             :         }
     317             : notfound:
     318             :         /*
     319             :          * If we started in the middle of the directory and failed
     320             :          * to find our target, we must check the beginning as well.
     321             :          */
     322           0 :         if (numdirpasses == 2) {
     323           0 :                 numdirpasses--;
     324           0 :                 dp->i_offset = 0;
     325           0 :                 endsearch = dp->i_diroff;
     326           0 :                 goto searchloop;
     327             :         }
     328           0 :         if (bp != NULL)
     329           0 :                 brelse(bp);
     330             : 
     331             :         /*
     332             :          * Insert name into cache (as non-existent) if appropriate.
     333             :          */
     334           0 :         if (cnp->cn_flags & MAKEENTRY)
     335           0 :                 cache_enter(vdp, *vpp, cnp);
     336           0 :         if (nameiop == CREATE || nameiop == RENAME)
     337           0 :                 return (EJUSTRETURN);
     338           0 :         return (ENOENT);
     339             :         
     340             : found:
     341           0 :         if (numdirpasses == 2)
     342           0 :                 iso_nchstats.ncs_pass2++;
     343             :         
     344             :         /*
     345             :          * Found component in pathname.
     346             :          * If the final component of path name, save information
     347             :          * in the cache as to where the entry was found.
     348             :          */
     349           0 :         if ((flags & ISLASTCN) && nameiop == LOOKUP)
     350           0 :                 dp->i_diroff = dp->i_offset;
     351             :         
     352             :         /*
     353             :          * Step through the translation in the name.  We do not `iput' the
     354             :          * directory because we may need it again if a symbolic link
     355             :          * is relative to the current directory.  Instead we save it
     356             :          * unlocked as "pdp".  We must get the target inode before unlocking
     357             :          * the directory to insure that the inode will not be removed
     358             :          * before we get it.  We prevent deadlock by always fetching
     359             :          * inodes from the root, moving down the directory tree. Thus
     360             :          * when following backward pointers ".." we must unlock the
     361             :          * parent directory before getting the requested directory.
     362             :          * There is a potential race condition here if both the current
     363             :          * and parent directories are removed before the `iget' for the
     364             :          * inode associated with ".." returns.  We hope that this occurs
     365             :          * infrequently since we cannot avoid this race condition without
     366             :          * implementing a sophisticated deadlock detection algorithm.
     367             :          * Note also that this simple deadlock detection scheme will not
     368             :          * work if the file system has any hard links other than ".."
     369             :          * that point backwards in the directory structure.
     370             :          */
     371             :         pdp = vdp;
     372             :         /*
     373             :          * If ino is different from dp->i_ino,
     374             :          * it's a relocated directory.
     375             :          */
     376           0 :         if (flags & ISDOTDOT) {
     377           0 :                 brelse(bp);
     378           0 :                 VOP_UNLOCK(pdp);        /* race to get the inode */
     379           0 :                 cnp->cn_flags |= PDIRUNLOCK;
     380           0 :                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
     381           0 :                             dp->i_ino != ino, NULL);
     382           0 :                 if (error) {
     383           0 :                         if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
     384           0 :                                 cnp->cn_flags &= ~PDIRUNLOCK;
     385           0 :                         return (error);
     386             :                 }
     387           0 :                 if (lockparent && (flags & ISLASTCN)) {
     388           0 :                         if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
     389           0 :                                 vput(tdp);
     390           0 :                                 return (error);
     391             :                         }
     392           0 :                         cnp->cn_flags &= ~PDIRUNLOCK;
     393           0 :                 }
     394           0 :                 *vpp = tdp;
     395           0 :         } else if (dp->i_number == dp->i_ino) {
     396           0 :                 brelse(bp);
     397           0 :                 vref(vdp);      /* we want ourself, ie "." */
     398           0 :                 *vpp = vdp;
     399           0 :         } else {
     400           0 :                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
     401           0 :                                              dp->i_ino != ino, ep);
     402           0 :                 brelse(bp);
     403           0 :                 if (error)
     404           0 :                         return (error);
     405           0 :                 if (!lockparent || !(flags & ISLASTCN)) {
     406           0 :                         VOP_UNLOCK(pdp);
     407           0 :                         cnp->cn_flags |= PDIRUNLOCK;
     408           0 :                 }
     409           0 :                 *vpp = tdp;
     410             :         }
     411             :         
     412             :         /*
     413             :          * Insert name into cache if appropriate.
     414             :          */
     415           0 :         if (cnp->cn_flags & MAKEENTRY)
     416           0 :                 cache_enter(vdp, *vpp, cnp);
     417           0 :         return (0);
     418           0 : }
     419             : 
     420             : /*
     421             :  * Return buffer with the contents of block "offset" from the beginning of
     422             :  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     423             :  * remaining space in the directory.
     424             :  */
     425             : int
     426           0 : cd9660_bufatoff(struct iso_node *ip, off_t offset, char **res, 
     427             :     struct buf **bpp)
     428             : {
     429             :         struct iso_mnt *imp;
     430           0 :         struct buf *bp;
     431             :         daddr_t lbn;
     432             :         int bsize, error;
     433           0 :         struct vnode *vp = ITOV(ip);
     434             : 
     435           0 :         imp = ip->i_mnt;
     436           0 :         lbn = lblkno(imp, offset);
     437           0 :         bsize = blksize(imp, ip, lbn);
     438             :         
     439           0 :         if ((error = bread(vp, lbn, bsize, &bp)) != 0) {
     440           0 :                 brelse(bp);
     441           0 :                 *bpp = NULL;
     442           0 :                 return (error);
     443             :         }
     444           0 :         if (res)
     445           0 :                 *res = (char *)bp->b_data + blkoff(imp, offset);
     446           0 :         *bpp = bp;
     447           0 :         return (0);
     448           0 : }

Generated by: LCOV version 1.13