[BACK]Return to cd9660_lookup.c CVS log [TXT][DIR] Up to [local] / sys / isofs / cd9660

Annotation of sys/isofs/cd9660/cd9660_lookup.c, Revision 1.1

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

CVSweb