Annotation of Net2/isofs/isofs_lookup.c, revision 1.1.1.3

1.1       root        1: /*
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
1.1.1.3 ! root        5:  * Copyright (c) 1983 Atsushi Murai ([email protected])
        !             6:  * All rights reserved for Rock Ridge Extension Support.
        !             7:  *
1.1       root        8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  *
1.1.1.3 ! root       36:  *     from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
        !            37:  *     isofs_lookup.c,v 1.5 1993/07/19 13:40:03 cgd Exp
1.1       root       38:  */
                     39: 
                     40: #include "param.h"
                     41: #include "namei.h"
                     42: #include "buf.h"
                     43: #include "file.h"
                     44: #include "vnode.h"
                     45: #include "mount.h"
                     46: 
                     47: #include "iso.h"
                     48: #include "isofs_node.h"
1.1.1.3 ! root       49: #include "iso_rrip.h"
        !            50: #include "isofs_rrip.h"
1.1       root       51: 
                     52: struct nchstats nchstats;
                     53: 
                     54: /*
                     55:  * Convert a component of a pathname into a pointer to a locked inode.
                     56:  * This is a very central and rather complicated routine.
                     57:  * If the file system is not maintained in a strict tree hierarchy,
                     58:  * this can result in a deadlock situation (see comments in code below).
                     59:  *
                     60:  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
                     61:  * whether the name is to be looked up, created, renamed, or deleted.
                     62:  * When CREATE, RENAME, or DELETE is specified, information usable in
                     63:  * creating, renaming, or deleting a directory entry may be calculated.
                     64:  * If flag has LOCKPARENT or'ed into it and the target of the pathname
                     65:  * exists, lookup returns both the target and its parent directory locked.
                     66:  * When creating or renaming and LOCKPARENT is specified, the target may
                     67:  * not be ".".  When deleting and LOCKPARENT is specified, the target may
                     68:  * be "."., but the caller must check to ensure it does an vrele and iput
                     69:  * instead of two iputs.
                     70:  *
                     71:  * Overall outline of ufs_lookup:
                     72:  *
                     73:  *     check accessibility of directory
                     74:  *     look for name in cache, if found, then if at end of path
                     75:  *       and deleting or creating, drop it, else return name
                     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: isofs_lookup(vdp, ndp, p)
                     90:        register struct vnode *vdp;
                     91:        register struct nameidata *ndp;
                     92:        struct proc *p;
                     93: {
                     94:        register struct iso_node *dp;   /* the directory we are searching */
                     95:        register struct iso_mnt *imp;   /* file system that directory is in */
                     96:        struct buf *bp = 0;             /* a buffer of directory entries */
                     97:        register struct iso_directory_record *ep;
                     98:                                        /* the current directory entry */
                     99:        int entryoffsetinblock;         /* offset of ep in bp's buffer */
                    100:        enum {NONE, COMPACT, FOUND} slotstatus;
                    101:        int slotoffset = -1;            /* offset of area with free space */
                    102:        int slotsize;                   /* size of area at slotoffset */
                    103:        int slotfreespace;              /* amount of space free in slot */
                    104:        int slotneeded;                 /* size of the entry we're seeking */
                    105:        int numdirpasses;               /* strategy for directory search */
                    106:        int endsearch;                  /* offset to end directory search */
                    107:        struct iso_node *pdp;           /* saved dp during symlink work */
                    108:        struct iso_node *tdp;           /* returned by iget */
                    109:        int flag;                       /* LOOKUP, CREATE, RENAME, or DELETE */
                    110:        int lockparent;                 /* 1 => lockparent flag is set */
                    111:        int wantparent;                 /* 1 => wantparent or lockparent flag */
                    112:        int error;
                    113: 
                    114:        int reclen;
                    115:        int namelen;
1.1.1.3 ! root      116:        char altname[251];
        !           117:        int i;
1.1       root      118: 
                    119:        ndp->ni_dvp = vdp;
                    120:        ndp->ni_vp = NULL;
                    121:        dp = VTOI(vdp);
                    122:        imp = dp->i_mnt;
                    123:        lockparent = ndp->ni_nameiop & LOCKPARENT;
                    124:        flag = ndp->ni_nameiop & OPMASK;
                    125:        wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
                    126: 
                    127:        /*
                    128:         * Check accessiblity of directory.
                    129:         */
                    130:        if ((dp->iso_flags & 2) == 0)
                    131:                return (ENOTDIR);
                    132: 
                    133:        /*
                    134:         * We now have a segment name to search for, and a directory to search.
                    135:         *
                    136:         * Before tediously performing a linear scan of the directory,
                    137:         * check the name cache to see if the directory/name pair
                    138:         * we are looking for is known already.
                    139:         */
                    140:        if (error = cache_lookup(ndp)) {
                    141:                int vpid;       /* capability number of vnode */
                    142: 
                    143:                if (error == ENOENT)
                    144:                        return (error);
                    145: #ifdef PARANOID
                    146:                if (vdp == ndp->ni_rdir && ndp->ni_isdotdot)
                    147:                        panic("ufs_lookup: .. through root");
                    148: #endif
                    149:                /*
                    150:                 * Get the next vnode in the path.
                    151:                 * See comment below starting `Step through' for
                    152:                 * an explaination of the locking protocol.
                    153:                 */
                    154:                pdp = dp;
                    155:                dp = VTOI(ndp->ni_vp);
                    156:                vdp = ndp->ni_vp;
                    157:                vpid = vdp->v_id;
                    158:                if (pdp == dp) {
                    159:                        VREF(vdp);
                    160:                        error = 0;
                    161:                } else if (ndp->ni_isdotdot) {
                    162:                        ISO_IUNLOCK(pdp);
                    163:                        error = vget(vdp);
                    164:                        if (!error && lockparent && *ndp->ni_next == '\0')
                    165:                                ISO_ILOCK(pdp);
                    166:                } else {
                    167:                        error = vget(vdp);
                    168:                        if (!lockparent || error || *ndp->ni_next != '\0')
                    169:                                ISO_IUNLOCK(pdp);
                    170:                }
                    171:                /*
                    172:                 * Check that the capability number did not change
                    173:                 * while we were waiting for the lock.
                    174:                 */
                    175:                if (!error) {
                    176:                        if (vpid == vdp->v_id)
                    177:                                return (0);
                    178:                        iso_iput(dp);
                    179:                        if (lockparent && pdp != dp && *ndp->ni_next == '\0')
                    180:                                ISO_IUNLOCK(pdp);
                    181:                }
                    182:                ISO_ILOCK(pdp);
                    183:                dp = pdp;
                    184:                vdp = ITOV(dp);
                    185:                ndp->ni_vp = NULL;
                    186:        }
                    187: 
                    188:        /*
                    189:         * If there is cached information on a previous search of
                    190:         * this directory, pick up where we last left off.
                    191:         * We cache only lookups as these are the most common
                    192:         * and have the greatest payoff. Caching CREATE has little
                    193:         * benefit as it usually must search the entire directory
                    194:         * to determine that the entry does not exist. Caching the
                    195:         * location of the last DELETE or RENAME has not reduced
                    196:         * profiling time and hence has been removed in the interest
                    197:         * of simplicity.
                    198:         */
                    199:        if (flag != LOOKUP || dp->i_diroff == 0 || dp->i_diroff > dp->i_size) {
                    200:                ndp->ni_ufs.ufs_offset = 0;
                    201:                numdirpasses = 1;
                    202:        } else {
                    203:                ndp->ni_ufs.ufs_offset = dp->i_diroff;
                    204:                entryoffsetinblock = iso_blkoff(imp, ndp->ni_ufs.ufs_offset);
                    205:                if (entryoffsetinblock != 0) {
                    206:                        if (error = iso_blkatoff(dp, ndp->ni_ufs.ufs_offset,
                    207:                            (char **)0, &bp))
                    208:                                return (error);
                    209:                }
                    210:                numdirpasses = 2;
                    211:                nchstats.ncs_2passes++;
                    212:        }
                    213:        endsearch = roundup(dp->i_size, imp->logical_block_size);
                    214: 
                    215: searchloop:
                    216:        while (ndp->ni_ufs.ufs_offset < endsearch) {
                    217:                /*
                    218:                 * If offset is on a block boundary,
                    219:                 * read the next directory block.
                    220:                 * Release previous if it exists.
                    221:                 */
                    222:                if (iso_blkoff(imp, ndp->ni_ufs.ufs_offset) == 0) {
                    223:                        if (bp != NULL)
                    224:                                brelse(bp);
                    225:                        if (error = iso_blkatoff(dp, ndp->ni_ufs.ufs_offset,
                    226:                                                 (char **)0, &bp))
                    227:                                return (error);
                    228:                        entryoffsetinblock = 0;
                    229:                }
                    230:                /*
                    231:                 * Get pointer to next entry.
                    232:                 */
                    233: 
                    234:                ep = (struct iso_directory_record *)
                    235:                        (bp->b_un.b_addr + entryoffsetinblock);
                    236: 
                    237:                reclen = isonum_711 (ep->length);
                    238:                if (reclen == 0) {
                    239:                        /* skip to next block, if any */
                    240:                        ndp->ni_ufs.ufs_offset =
                    241:                                roundup (ndp->ni_ufs.ufs_offset,
                    242:                                         imp->logical_block_size);
                    243:                        continue;
                    244:                }
                    245: 
                    246:                if (reclen < sizeof (struct iso_directory_record))
                    247:                        /* illegal entry, stop */
                    248:                        break;
                    249: 
1.1.1.2   root      250: /* 10 Aug 92*/ if (entryoffsetinblock + reclen -1 >= imp->logical_block_size)
1.1       root      251:                        /* entries are not allowed to cross boundaries */
                    252:                        break;
                    253: 
                    254:                /*
                    255:                 * Check for a name match.
                    256:                 */
                    257:                namelen = isonum_711 (ep->name_len);
                    258: 
                    259:                if (reclen < sizeof (struct iso_directory_record) + namelen)
                    260:                        /* illegal entry, stop */
                    261:                        break;
                    262: 
1.1.1.3 ! root      263:                if (namelen == 1
1.1       root      264:                     && ((ndp->ni_namelen == 1
                    265:                          && ndp->ni_ptr[0] == '.'
                    266:                          && ep->name[0] == 0)
1.1.1.3 ! root      267:                         || (ndp->ni_isdotdot && ep->name[0] == 1))) {
1.1       root      268:                        /*
                    269:                         * Save directory entry's inode number and
                    270:                         * reclen in ndp->ni_ufs area, and release
                    271:                         * directory buffer.
                    272:                         */
                    273:                        ndp->ni_ufs.ufs_ino = isonum_733 (ep->extent);
                    274:                        brelse(bp);
                    275:                        goto found;
1.1.1.3 ! root      276:                } else {
        !           277:                        switch ( imp->iso_ftype ) {
        !           278:                                case ISO_FTYPE_9660:
        !           279:                                if( ( namelen  >= ndp->ni_namelen ) &&
        !           280:                                            ( isofncmp( ndp->ni_ptr, ndp->ni_namelen, ep->name, namelen ) ) ) {
        !           281:                                                ndp->ni_ufs.ufs_ino = isonum_733 (ep->extent);
        !           282:                                                brelse(bp);
        !           283:                                                goto found;
        !           284:                                        }
        !           285:                                        break;
        !           286:                                case ISO_FTYPE_RRIP:
        !           287:                                        isofs_rrip_getname( ep, altname, &namelen );
        !           288:                                        if ( ( namelen == ndp->ni_namelen ) &&
        !           289:                                             ( !bcmp( ndp->ni_ptr, altname, ndp->ni_namelen ) ) ) {
        !           290:                                                ndp->ni_ufs.ufs_ino = isonum_733 (ep->extent);
        !           291:                                                brelse(bp);
        !           292:                                                goto found;
        !           293:                                        }
        !           294:                                        break;
        !           295:                                default:
        !           296:                                        break;
        !           297:                        }
1.1       root      298:                }
                    299:                ndp->ni_ufs.ufs_offset += reclen;
                    300:                entryoffsetinblock += reclen;
                    301:        }
                    302: /* notfound: */
                    303:        /*
                    304:         * If we started in the middle of the directory and failed
                    305:         * to find our target, we must check the beginning as well.
                    306:         */
                    307:        if (numdirpasses == 2) {
                    308:                numdirpasses--;
                    309:                ndp->ni_ufs.ufs_offset = 0;
                    310:                endsearch = dp->i_diroff;
                    311:                goto searchloop;
                    312:        }
                    313:        if (bp != NULL)
                    314:                brelse(bp);
                    315:        /*
                    316:         * Insert name into cache (as non-existent) if appropriate.
                    317:         */
                    318:        if (ndp->ni_makeentry)
                    319:                cache_enter(ndp);
                    320:        return (ENOENT);
                    321: 
                    322: found:
                    323:        if (numdirpasses == 2)
                    324:                nchstats.ncs_pass2++;
                    325: 
                    326:        /*
                    327:         * Found component in pathname.
                    328:         * If the final component of path name, save information
                    329:         * in the cache as to where the entry was found.
                    330:         */
                    331:        if (*ndp->ni_next == '\0' && flag == LOOKUP)
1.1.1.3 ! root      332:                dp->i_diroff = ndp->ni_ufs.ufs_offset;
        !           333:                        /* &~ (imp->logical_block_size - 1); */
1.1       root      334: 
                    335:        /*
                    336:         * Step through the translation in the name.  We do not `iput' the
                    337:         * directory because we may need it again if a symbolic link
                    338:         * is relative to the current directory.  Instead we save it
                    339:         * unlocked as "pdp".  We must get the target inode before unlocking
                    340:         * the directory to insure that the inode will not be removed
                    341:         * before we get it.  We prevent deadlock by always fetching
                    342:         * inodes from the root, moving down the directory tree. Thus
                    343:         * when following backward pointers ".." we must unlock the
                    344:         * parent directory before getting the requested directory.
                    345:         * There is a potential race condition here if both the current
                    346:         * and parent directories are removed before the `iget' for the
                    347:         * inode associated with ".." returns.  We hope that this occurs
                    348:         * infrequently since we cannot avoid this race condition without
                    349:         * implementing a sophisticated deadlock detection algorithm.
                    350:         * Note also that this simple deadlock detection scheme will not
                    351:         * work if the file system has any hard links other than ".."
                    352:         * that point backwards in the directory structure.
                    353:         */
                    354:        pdp = dp;
                    355:        if (ndp->ni_isdotdot) {
                    356:                ISO_IUNLOCK(pdp);       /* race to get the inode */
                    357:                if (error = iso_iget(dp, ndp->ni_ufs.ufs_ino, &tdp, ep)) {
                    358:                        ISO_ILOCK(pdp);
                    359:                        return (error);
                    360:                }
                    361:                if (lockparent && *ndp->ni_next == '\0')
                    362:                        ISO_ILOCK(pdp);
                    363:                ndp->ni_vp = ITOV(tdp);
                    364:        } else if (dp->i_number == ndp->ni_ufs.ufs_ino) {
                    365:                VREF(vdp);      /* we want ourself, ie "." */
                    366:                ndp->ni_vp = vdp;
                    367:        } else {
                    368:                if (error = iso_iget(dp, ndp->ni_ufs.ufs_ino, &tdp, ep))
                    369:                        return (error);
                    370:                if (!lockparent || *ndp->ni_next != '\0')
                    371:                        ISO_IUNLOCK(pdp);
                    372:                ndp->ni_vp = ITOV(tdp);
                    373:        }
                    374: 
                    375:        /*
                    376:         * Insert name into cache if appropriate.
                    377:         */
                    378:        if (ndp->ni_makeentry)
                    379:                cache_enter(ndp);
                    380:        return (0);
                    381: }
                    382: 
                    383: 
                    384: /*
                    385:  * Return buffer with contents of block "offset"
                    386:  * from the beginning of directory "ip".  If "res"
                    387:  * is non-zero, fill it in with a pointer to the
                    388:  * remaining space in the directory.
                    389:  */
                    390: iso_blkatoff(ip, offset, res, bpp)
                    391:        struct iso_node *ip;
                    392:        off_t offset;
                    393:        char **res;
                    394:        struct buf **bpp;
                    395: {
                    396:        register struct iso_mnt *imp = ip->i_mnt;
                    397:        daddr_t lbn = iso_lblkno (imp, offset);
                    398:        int bsize = iso_blksize (imp, ip, lbn);
                    399:        struct buf *bp;
                    400:        int error;
                    401: 
                    402:        *bpp = 0;
                    403:        if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) {
                    404:                brelse(bp);
                    405:                return (error);
                    406:        }
                    407:        if (res)
                    408:                *res = bp->b_un.b_addr + iso_blkoff(imp, offset);
                    409:        *bpp = bp;
                    410: 
                    411:        return (0);
                    412: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.