Annotation of Net2/ufs/ufs_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:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
1.1.1.3 ! root       33:  *     from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
        !            34:  *     ufs_lookup.c,v 1.3 1993/05/20 03:53:39 cgd Exp
1.1       root       35:  */
                     36: 
                     37: #include "param.h"
                     38: #include "namei.h"
                     39: #include "buf.h"
                     40: #include "file.h"
                     41: #include "vnode.h"
                     42: 
                     43: #include "quota.h"
                     44: #include "inode.h"
                     45: #include "dir.h"
                     46: #include "fs.h"
                     47: 
                     48: struct nchstats nchstats;
                     49: #ifdef DIAGNOSTIC
                     50: int    dirchk = 1;
                     51: #else
                     52: int    dirchk = 0;
                     53: #endif
                     54: 
                     55: /*
                     56:  * Convert a component of a pathname into a pointer to a locked inode.
                     57:  * This is a very central and rather complicated routine.
                     58:  * If the file system is not maintained in a strict tree hierarchy,
                     59:  * this can result in a deadlock situation (see comments in code below).
                     60:  *
                     61:  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
                     62:  * whether the name is to be looked up, created, renamed, or deleted.
                     63:  * When CREATE, RENAME, or DELETE is specified, information usable in
                     64:  * creating, renaming, or deleting a directory entry may be calculated.
                     65:  * If flag has LOCKPARENT or'ed into it and the target of the pathname
                     66:  * exists, lookup returns both the target and its parent directory locked.
                     67:  * When creating or renaming and LOCKPARENT is specified, the target may
                     68:  * not be ".".  When deleting and LOCKPARENT is specified, the target may
                     69:  * be "."., but the caller must check to ensure it does an vrele and iput
                     70:  * instead of two iputs.
                     71:  *
                     72:  * Overall outline of ufs_lookup:
                     73:  *
                     74:  *     check accessibility of directory
                     75:  *     look for name in cache, if found, then if at end of path
                     76:  *       and deleting or creating, drop it, else return name
                     77:  *     search for name in directory, to found or notfound
                     78:  * notfound:
                     79:  *     if creating, return locked directory, leaving info on available slots
                     80:  *     else return error
                     81:  * found:
                     82:  *     if at end of path and deleting, return information to allow delete
                     83:  *     if at end of path and rewriting (RENAME and LOCKPARENT), lock target
                     84:  *       inode and return info to allow rewrite
                     85:  *     if not at end, add name to cache; if at end and neither creating
                     86:  *       nor deleting, add name to cache
                     87:  *
                     88:  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
                     89:  */
                     90: ufs_lookup(vdp, ndp, p)
                     91:        register struct vnode *vdp;
                     92:        register struct nameidata *ndp;
                     93:        struct proc *p;
                     94: {
                     95:        register struct inode *dp;      /* the directory we are searching */
                     96:        register struct fs *fs;         /* file system that directory is in */
                     97:        struct buf *bp = 0;             /* a buffer of directory entries */
                     98:        register struct direct *ep;     /* 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:        int prevoff;                    /* ndp->ni_ufs.ufs_offset of previous entry */
                    108:        struct inode *pdp;              /* saved dp during symlink work */
                    109:        struct inode *tdp;              /* returned by iget */
                    110:        off_t enduseful;                /* pointer past last used dir slot */
                    111:        int flag;                       /* LOOKUP, CREATE, RENAME, or DELETE */
                    112:        int lockparent;                 /* 1 => lockparent flag is set */
                    113:        int wantparent;                 /* 1 => wantparent or lockparent flag */
                    114:        int error;
                    115: 
                    116:        ndp->ni_dvp = vdp;
                    117:        ndp->ni_vp = NULL;
                    118:        dp = VTOI(vdp);
                    119:        fs = dp->i_fs;
                    120:        lockparent = ndp->ni_nameiop & LOCKPARENT;
                    121:        flag = ndp->ni_nameiop & OPMASK;
                    122:        wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
                    123: 
                    124:        /*
                    125:         * Check accessiblity of directory.
                    126:         */
                    127:        if ((dp->i_mode&IFMT) != IFDIR)
                    128:                return (ENOTDIR);
                    129:        if (error = ufs_access(vdp, VEXEC, ndp->ni_cred, p))
                    130:                return (error);
                    131: 
                    132:        /*
                    133:         * We now have a segment name to search for, and a directory to search.
                    134:         *
                    135:         * Before tediously performing a linear scan of the directory,
                    136:         * check the name cache to see if the directory/name pair
                    137:         * we are looking for is known already.
                    138:         */
                    139:        if (error = cache_lookup(ndp)) {
                    140:                int vpid;       /* capability number of vnode */
                    141: 
                    142:                if (error == ENOENT)
                    143:                        return (error);
                    144: #ifdef PARANOID
1.1.1.3 ! root      145:                if (vdp == ndp->ni_rootdir && ndp->ni_isdotdot)
1.1       root      146:                        panic("ufs_lookup: .. through root");
                    147: #endif
                    148:                /*
                    149:                 * Get the next vnode in the path.
                    150:                 * See comment below starting `Step through' for
                    151:                 * an explaination of the locking protocol.
                    152:                 */
                    153:                pdp = dp;
                    154:                dp = VTOI(ndp->ni_vp);
                    155:                vdp = ndp->ni_vp;
                    156:                vpid = vdp->v_id;
                    157:                if (pdp == dp) {
                    158:                        VREF(vdp);
                    159:                        error = 0;
                    160:                } else if (ndp->ni_isdotdot) {
                    161:                        IUNLOCK(pdp);
                    162:                        error = vget(vdp);
                    163:                        if (!error && lockparent && *ndp->ni_next == '\0')
                    164:                                ILOCK(pdp);
                    165:                } else {
                    166:                        error = vget(vdp);
                    167:                        if (!lockparent || error || *ndp->ni_next != '\0')
                    168:                                IUNLOCK(pdp);
                    169:                }
                    170:                /*
                    171:                 * Check that the capability number did not change
                    172:                 * while we were waiting for the lock.
                    173:                 */
                    174:                if (!error) {
                    175:                        if (vpid == vdp->v_id)
                    176:                                return (0);
                    177:                        iput(dp);
                    178:                        if (lockparent && pdp != dp && *ndp->ni_next == '\0')
                    179:                                IUNLOCK(pdp);
                    180:                }
                    181:                ILOCK(pdp);
                    182:                dp = pdp;
                    183:                vdp = ITOV(dp);
                    184:                ndp->ni_vp = NULL;
                    185:        }
                    186: 
                    187:        /*
                    188:         * Suppress search for slots unless creating
                    189:         * file and at end of pathname, in which case
                    190:         * we watch for a place to put the new file in
                    191:         * case it doesn't already exist.
                    192:         */
                    193:        slotstatus = FOUND;
                    194:        if ((flag == CREATE || flag == RENAME) && *ndp->ni_next == 0) {
                    195:                slotstatus = NONE;
                    196:                slotfreespace = 0;
                    197:                slotneeded = ((sizeof (struct direct) - (MAXNAMLEN + 1)) +
                    198:                        ((ndp->ni_namelen + 1 + 3) &~ 3));
                    199:        }
                    200: 
                    201:        /*
                    202:         * If there is cached information on a previous search of
                    203:         * this directory, pick up where we last left off.
                    204:         * We cache only lookups as these are the most common
                    205:         * and have the greatest payoff. Caching CREATE has little
                    206:         * benefit as it usually must search the entire directory
                    207:         * to determine that the entry does not exist. Caching the
                    208:         * location of the last DELETE or RENAME has not reduced
                    209:         * profiling time and hence has been removed in the interest
                    210:         * of simplicity.
                    211:         */
                    212:        if (flag != LOOKUP || dp->i_diroff == 0 || dp->i_diroff > dp->i_size) {
                    213:                ndp->ni_ufs.ufs_offset = 0;
                    214:                numdirpasses = 1;
                    215:        } else {
                    216:                ndp->ni_ufs.ufs_offset = dp->i_diroff;
                    217:                entryoffsetinblock = blkoff(fs, ndp->ni_ufs.ufs_offset);
                    218:                if (entryoffsetinblock != 0) {
                    219:                        if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset,
                    220:                            (char **)0, &bp))
                    221:                                return (error);
                    222:                }
                    223:                numdirpasses = 2;
                    224:                nchstats.ncs_2passes++;
                    225:        }
                    226:        endsearch = roundup(dp->i_size, DIRBLKSIZ);
                    227:        enduseful = 0;
                    228: 
                    229: searchloop:
                    230:        while (ndp->ni_ufs.ufs_offset < endsearch) {
                    231:                /*
                    232:                 * If offset is on a block boundary,
                    233:                 * read the next directory block.
                    234:                 * Release previous if it exists.
                    235:                 */
                    236:                if (blkoff(fs, ndp->ni_ufs.ufs_offset) == 0) {
                    237:                        if (bp != NULL)
                    238:                                brelse(bp);
                    239:                        if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset,
                    240:                            (char **)0, &bp))
                    241:                                return (error);
                    242:                        entryoffsetinblock = 0;
                    243:                }
                    244:                /*
                    245:                 * If still looking for a slot, and at a DIRBLKSIZE
                    246:                 * boundary, have to start looking for free space again.
                    247:                 */
                    248:                if (slotstatus == NONE &&
                    249:                    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
                    250:                        slotoffset = -1;
                    251:                        slotfreespace = 0;
                    252:                }
                    253:                /*
                    254:                 * Get pointer to next entry.
                    255:                 * Full validation checks are slow, so we only check
                    256:                 * enough to insure forward progress through the
                    257:                 * directory. Complete checks can be run by patching
                    258:                 * "dirchk" to be true.
                    259:                 */
                    260:                ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
                    261:                if (ep->d_reclen == 0 ||
                    262:                    dirchk && dirbadentry(ep, entryoffsetinblock)) {
                    263:                        int i;
                    264: 
                    265:                        dirbad(dp, ndp->ni_ufs.ufs_offset, "mangled entry");
                    266:                        i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
                    267:                        ndp->ni_ufs.ufs_offset += i;
                    268:                        entryoffsetinblock += i;
                    269:                        continue;
                    270:                }
                    271: 
                    272:                /*
                    273:                 * If an appropriate sized slot has not yet been found,
                    274:                 * check to see if one is available. Also accumulate space
                    275:                 * in the current block so that we can determine if
                    276:                 * compaction is viable.
                    277:                 */
                    278:                if (slotstatus != FOUND) {
                    279:                        int size = ep->d_reclen;
                    280: 
                    281:                        if (ep->d_ino != 0)
                    282:                                size -= DIRSIZ(ep);
                    283:                        if (size > 0) {
                    284:                                if (size >= slotneeded) {
                    285:                                        slotstatus = FOUND;
                    286:                                        slotoffset = ndp->ni_ufs.ufs_offset;
                    287:                                        slotsize = ep->d_reclen;
                    288:                                } else if (slotstatus == NONE) {
                    289:                                        slotfreespace += size;
                    290:                                        if (slotoffset == -1)
                    291:                                                slotoffset =
                    292:                                                      ndp->ni_ufs.ufs_offset;
                    293:                                        if (slotfreespace >= slotneeded) {
                    294:                                                slotstatus = COMPACT;
                    295:                                                slotsize =
                    296:                                                      ndp->ni_ufs.ufs_offset +
                    297:                                                      ep->d_reclen - slotoffset;
                    298:                                        }
                    299:                                }
                    300:                        }
                    301:                }
                    302: 
                    303:                /*
                    304:                 * Check for a name match.
                    305:                 */
                    306:                if (ep->d_ino) {
                    307:                        if (ep->d_namlen == ndp->ni_namelen &&
                    308:                            !bcmp(ndp->ni_ptr, ep->d_name,
                    309:                                (unsigned)ep->d_namlen)) {
                    310:                                /*
                    311:                                 * Save directory entry's inode number and
                    312:                                 * reclen in ndp->ni_ufs area, and release
                    313:                                 * directory buffer.
                    314:                                 */
                    315:                                ndp->ni_ufs.ufs_ino = ep->d_ino;
                    316:                                ndp->ni_ufs.ufs_reclen = ep->d_reclen;
                    317:                                goto found;
                    318:                        }
                    319:                }
                    320:                prevoff = ndp->ni_ufs.ufs_offset;
                    321:                ndp->ni_ufs.ufs_offset += ep->d_reclen;
                    322:                entryoffsetinblock += ep->d_reclen;
                    323:                if (ep->d_ino)
                    324:                        enduseful = ndp->ni_ufs.ufs_offset;
                    325:        }
                    326: /* notfound: */
                    327:        /*
                    328:         * If we started in the middle of the directory and failed
                    329:         * to find our target, we must check the beginning as well.
                    330:         */
                    331:        if (numdirpasses == 2) {
                    332:                numdirpasses--;
                    333:                ndp->ni_ufs.ufs_offset = 0;
                    334:                endsearch = dp->i_diroff;
                    335:                goto searchloop;
                    336:        }
                    337:        if (bp != NULL)
                    338:                brelse(bp);
                    339:        /*
                    340:         * If creating, and at end of pathname and current
                    341:         * directory has not been removed, then can consider
                    342:         * allowing file to be created.
                    343:         */
                    344:        if ((flag == CREATE || flag == RENAME) &&
                    345:            *ndp->ni_next == 0 && dp->i_nlink != 0) {
                    346:                /*
                    347:                 * Access for write is interpreted as allowing
                    348:                 * creation of files in the directory.
                    349:                 */
                    350:                if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                    351:                        return (error);
                    352:                /*
                    353:                 * Return an indication of where the new directory
                    354:                 * entry should be put.  If we didn't find a slot,
                    355:                 * then set ndp->ni_ufs.ufs_count to 0 indicating
                    356:                 * that the new slot belongs at the end of the
                    357:                 * directory. If we found a slot, then the new entry
                    358:                 * can be put in the range from ndp->ni_ufs.ufs_offset
                    359:                 * to ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count.
                    360:                 */
                    361:                if (slotstatus == NONE) {
                    362:                        ndp->ni_ufs.ufs_offset = roundup(dp->i_size, DIRBLKSIZ);
                    363:                        ndp->ni_ufs.ufs_count = 0;
                    364:                        enduseful = ndp->ni_ufs.ufs_offset;
                    365:                } else {
                    366:                        ndp->ni_ufs.ufs_offset = slotoffset;
                    367:                        ndp->ni_ufs.ufs_count = slotsize;
                    368:                        if (enduseful < slotoffset + slotsize)
                    369:                                enduseful = slotoffset + slotsize;
                    370:                }
                    371:                ndp->ni_ufs.ufs_endoff = roundup(enduseful, DIRBLKSIZ);
                    372:                dp->i_flag |= IUPD|ICHG;
                    373:                /*
                    374:                 * We return with the directory locked, so that
                    375:                 * the parameters we set up above will still be
                    376:                 * valid if we actually decide to do a direnter().
                    377:                 * We return ni_vp == NULL to indicate that the entry
                    378:                 * does not currently exist; we leave a pointer to
                    379:                 * the (locked) directory inode in ndp->ni_dvp.
                    380:                 * The pathname buffer is saved so that the name
                    381:                 * can be obtained later.
                    382:                 *
                    383:                 * NB - if the directory is unlocked, then this
                    384:                 * information cannot be used.
                    385:                 */
                    386:                ndp->ni_nameiop |= SAVENAME;
                    387:                if (!lockparent)
                    388:                        IUNLOCK(dp);
                    389:        }
                    390:        /*
                    391:         * Insert name into cache (as non-existent) if appropriate.
                    392:         */
                    393:        if (ndp->ni_makeentry && flag != CREATE)
                    394:                cache_enter(ndp);
                    395:        return (ENOENT);
                    396: 
                    397: found:
                    398:        if (numdirpasses == 2)
                    399:                nchstats.ncs_pass2++;
                    400:        /*
                    401:         * Check that directory length properly reflects presence
                    402:         * of this entry.
                    403:         */
                    404:        if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
                    405:                dirbad(dp, ndp->ni_ufs.ufs_offset, "i_size too small");
                    406:                dp->i_size = entryoffsetinblock + DIRSIZ(ep);
                    407:                dp->i_flag |= IUPD|ICHG;
                    408:        }
                    409: 
1.1.1.2   root      410:        brelse(bp);
                    411: 
1.1       root      412:        /*
                    413:         * Found component in pathname.
                    414:         * If the final component of path name, save information
                    415:         * in the cache as to where the entry was found.
                    416:         */
                    417:        if (*ndp->ni_next == '\0' && flag == LOOKUP)
                    418:                dp->i_diroff = ndp->ni_ufs.ufs_offset &~ (DIRBLKSIZ - 1);
                    419: 
                    420:        /*
                    421:         * If deleting, and at end of pathname, return
                    422:         * parameters which can be used to remove file.
                    423:         * If the wantparent flag isn't set, we return only
                    424:         * the directory (in ndp->ni_dvp), otherwise we go
                    425:         * on and lock the inode, being careful with ".".
                    426:         */
                    427:        if (flag == DELETE && *ndp->ni_next == 0) {
                    428:                /*
                    429:                 * Write access to directory required to delete files.
                    430:                 */
                    431:                if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                    432:                        return (error);
                    433:                /*
                    434:                 * Return pointer to current entry in ndp->ni_ufs.ufs_offset,
                    435:                 * and distance past previous entry (if there
                    436:                 * is a previous entry in this block) in ndp->ni_ufs.ufs_count.
                    437:                 * Save directory inode pointer in ndp->ni_dvp for dirremove().
                    438:                 */
                    439:                if ((ndp->ni_ufs.ufs_offset&(DIRBLKSIZ-1)) == 0)
                    440:                        ndp->ni_ufs.ufs_count = 0;
                    441:                else
                    442:                        ndp->ni_ufs.ufs_count = ndp->ni_ufs.ufs_offset - prevoff;
                    443:                if (dp->i_number == ndp->ni_ufs.ufs_ino) {
                    444:                        VREF(vdp);
                    445:                        ndp->ni_vp = vdp;
                    446:                        return (0);
                    447:                }
                    448:                if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
                    449:                        return (error);
                    450:                /*
                    451:                 * If directory is "sticky", then user must own
                    452:                 * the directory, or the file in it, else she
                    453:                 * may not delete it (unless she's root). This
                    454:                 * implements append-only directories.
                    455:                 */
                    456:                if ((dp->i_mode & ISVTX) &&
                    457:                    ndp->ni_cred->cr_uid != 0 &&
                    458:                    ndp->ni_cred->cr_uid != dp->i_uid &&
                    459:                    tdp->i_uid != ndp->ni_cred->cr_uid) {
                    460:                        iput(tdp);
                    461:                        return (EPERM);
                    462:                }
                    463:                ndp->ni_vp = ITOV(tdp);
                    464:                if (!lockparent)
                    465:                        IUNLOCK(dp);
                    466:                return (0);
                    467:        }
                    468: 
                    469:        /*
                    470:         * If rewriting (RENAME), return the inode and the
                    471:         * information required to rewrite the present directory
                    472:         * Must get inode of directory entry to verify it's a
                    473:         * regular file, or empty directory.
                    474:         */
                    475:        if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
                    476:                if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                    477:                        return (error);
                    478:                /*
                    479:                 * Careful about locking second inode.
                    480:                 * This can only occur if the target is ".".
                    481:                 */
                    482:                if (dp->i_number == ndp->ni_ufs.ufs_ino)
                    483:                        return (EISDIR);
                    484:                if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
                    485:                        return (error);
                    486:                ndp->ni_vp = ITOV(tdp);
                    487:                ndp->ni_nameiop |= SAVENAME;
                    488:                if (!lockparent)
                    489:                        IUNLOCK(dp);
                    490:                return (0);
                    491:        }
                    492: 
                    493:        /*
                    494:         * Step through the translation in the name.  We do not `iput' the
                    495:         * directory because we may need it again if a symbolic link
                    496:         * is relative to the current directory.  Instead we save it
                    497:         * unlocked as "pdp".  We must get the target inode before unlocking
                    498:         * the directory to insure that the inode will not be removed
                    499:         * before we get it.  We prevent deadlock by always fetching
                    500:         * inodes from the root, moving down the directory tree. Thus
                    501:         * when following backward pointers ".." we must unlock the
                    502:         * parent directory before getting the requested directory.
                    503:         * There is a potential race condition here if both the current
                    504:         * and parent directories are removed before the `iget' for the
                    505:         * inode associated with ".." returns.  We hope that this occurs
                    506:         * infrequently since we cannot avoid this race condition without
                    507:         * implementing a sophisticated deadlock detection algorithm.
                    508:         * Note also that this simple deadlock detection scheme will not
                    509:         * work if the file system has any hard links other than ".."
                    510:         * that point backwards in the directory structure.
                    511:         */
                    512:        pdp = dp;
                    513:        if (ndp->ni_isdotdot) {
                    514:                IUNLOCK(pdp);   /* race to get the inode */
                    515:                if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp)) {
                    516:                        ILOCK(pdp);
                    517:                        return (error);
                    518:                }
                    519:                if (lockparent && *ndp->ni_next == '\0')
                    520:                        ILOCK(pdp);
                    521:                ndp->ni_vp = ITOV(tdp);
                    522:        } else if (dp->i_number == ndp->ni_ufs.ufs_ino) {
                    523:                VREF(vdp);      /* we want ourself, ie "." */
                    524:                ndp->ni_vp = vdp;
                    525:        } else {
                    526:                if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
                    527:                        return (error);
                    528:                if (!lockparent || *ndp->ni_next != '\0')
                    529:                        IUNLOCK(pdp);
                    530:                ndp->ni_vp = ITOV(tdp);
                    531:        }
                    532: 
                    533:        /*
                    534:         * Insert name into cache if appropriate.
                    535:         */
                    536:        if (ndp->ni_makeentry)
                    537:                cache_enter(ndp);
                    538:        return (0);
                    539: }
                    540: 
                    541: 
                    542: dirbad(ip, offset, how)
                    543:        struct inode *ip;
                    544:        off_t offset;
                    545:        char *how;
                    546: {
                    547: 
                    548:        printf("%s: bad dir ino %d at offset %d: %s\n",
                    549:            ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
                    550:        if (ip->i_fs->fs_ronly == 0)
                    551:                panic("bad dir");
                    552: }
                    553: 
                    554: /*
                    555:  * Do consistency checking on a directory entry:
                    556:  *     record length must be multiple of 4
                    557:  *     entry must fit in rest of its DIRBLKSIZ block
                    558:  *     record must be large enough to contain entry
                    559:  *     name is not longer than MAXNAMLEN
                    560:  *     name must be as long as advertised, and null terminated
                    561:  */
                    562: dirbadentry(ep, entryoffsetinblock)
                    563:        register struct direct *ep;
                    564:        int entryoffsetinblock;
                    565: {
                    566:        register int i;
                    567: 
                    568:        if ((ep->d_reclen & 0x3) != 0 ||
                    569:            ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
                    570:            ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN)
                    571:                return (1);
                    572:        for (i = 0; i < ep->d_namlen; i++)
                    573:                if (ep->d_name[i] == '\0')
                    574:                        return (1);
                    575:        return (ep->d_name[i]);
                    576: }
                    577: 
                    578: /*
                    579:  * Write a directory entry after a call to namei, using the parameters
                    580:  * that it left in nameidata.  The argument ip is the inode which the new
                    581:  * directory entry will refer to.  The nameidata field ndp->ni_dvp is a
                    582:  * pointer to the directory to be written, which was left locked by namei.
                    583:  * Remaining parameters (ndp->ni_ufs.ufs_offset, ndp->ni_ufs.ufs_count)
                    584:  * indicate how the space for the new entry is to be obtained.
                    585:  */
                    586: direnter(ip, ndp)
                    587:        struct inode *ip;
                    588:        register struct nameidata *ndp;
                    589: {
                    590:        register struct direct *ep, *nep;
                    591:        register struct inode *dp = VTOI(ndp->ni_dvp);
                    592:        struct buf *bp;
                    593:        int loc, spacefree, error = 0;
                    594:        u_int dsize;
                    595:        int newentrysize;
                    596:        char *dirbuf;
                    597:        struct uio auio;
                    598:        struct iovec aiov;
                    599:        struct direct newdir;
                    600: 
                    601: #ifdef DIAGNOSTIC
                    602:        if ((ndp->ni_nameiop & SAVENAME) == 0)
                    603:                panic("direnter: missing name");
                    604: #endif
                    605:        newdir.d_ino = ip->i_number;
                    606:        newdir.d_namlen = ndp->ni_namelen;
                    607:        bcopy(ndp->ni_ptr, newdir.d_name, (unsigned)ndp->ni_namelen + 1);
                    608:        newentrysize = DIRSIZ(&newdir);
                    609:        if (ndp->ni_ufs.ufs_count == 0) {
                    610:                /*
                    611:                 * If ndp->ni_ufs.ufs_count is 0, then namei could find no
                    612:                 * space in the directory. Here, ndp->ni_ufs.ufs_offset will
                    613:                 * be on a directory block boundary and we will write the
                    614:                 * new entry into a fresh block.
                    615:                 */
                    616:                if (ndp->ni_ufs.ufs_offset & (DIRBLKSIZ - 1))
                    617:                        panic("wdir: newblk");
                    618:                auio.uio_offset = ndp->ni_ufs.ufs_offset;
                    619:                newdir.d_reclen = DIRBLKSIZ;
                    620:                auio.uio_resid = newentrysize;
                    621:                aiov.iov_len = newentrysize;
                    622:                aiov.iov_base = (caddr_t)&newdir;
                    623:                auio.uio_iov = &aiov;
                    624:                auio.uio_iovcnt = 1;
                    625:                auio.uio_rw = UIO_WRITE;
                    626:                auio.uio_segflg = UIO_SYSSPACE;
                    627:                auio.uio_procp = (struct proc *)0;
                    628:                error = ufs_write(ndp->ni_dvp, &auio, IO_SYNC, ndp->ni_cred);
                    629:                if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
                    630:                        panic("wdir: blksize"); /* XXX - should grow w/balloc */
                    631:                } else if (!error) {
                    632:                        dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
                    633:                        dp->i_flag |= ICHG;
                    634:                }
                    635:                return (error);
                    636:        }
                    637: 
                    638:        /*
                    639:         * If ndp->ni_ufs.ufs_count is non-zero, then namei found space
                    640:         * for the new entry in the range ndp->ni_ufs.ufs_offset to
                    641:         * ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count in the directory.
                    642:         * To use this space, we may have to compact the entries located
                    643:         * there, by copying them together towards the beginning of the
                    644:         * block, leaving the free space in one usable chunk at the end.
                    645:         */
                    646: 
                    647:        /*
                    648:         * Increase size of directory if entry eats into new space.
                    649:         * This should never push the size past a new multiple of
                    650:         * DIRBLKSIZE.
                    651:         *
                    652:         * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
                    653:         */
                    654:        if (ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count > dp->i_size)
                    655:                dp->i_size = ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count;
                    656:        /*
                    657:         * Get the block containing the space for the new directory entry.
                    658:         */
                    659:        if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&dirbuf, &bp))
                    660:                return (error);
                    661:        /*
                    662:         * Find space for the new entry. In the simple case, the entry at
                    663:         * offset base will have the space. If it does not, then namei
                    664:         * arranged that compacting the region ndp->ni_ufs.ufs_offset to
                    665:         * ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count would yield the
                    666:         * space.
                    667:         */
                    668:        ep = (struct direct *)dirbuf;
                    669:        dsize = DIRSIZ(ep);
                    670:        spacefree = ep->d_reclen - dsize;
                    671:        for (loc = ep->d_reclen; loc < ndp->ni_ufs.ufs_count; ) {
                    672:                nep = (struct direct *)(dirbuf + loc);
                    673:                if (ep->d_ino) {
                    674:                        /* trim the existing slot */
                    675:                        ep->d_reclen = dsize;
                    676:                        ep = (struct direct *)((char *)ep + dsize);
                    677:                } else {
                    678:                        /* overwrite; nothing there; header is ours */
                    679:                        spacefree += dsize;
                    680:                }
                    681:                dsize = DIRSIZ(nep);
                    682:                spacefree += nep->d_reclen - dsize;
                    683:                loc += nep->d_reclen;
                    684:                bcopy((caddr_t)nep, (caddr_t)ep, dsize);
                    685:        }
                    686:        /*
                    687:         * Update the pointer fields in the previous entry (if any),
                    688:         * copy in the new entry, and write out the block.
                    689:         */
                    690:        if (ep->d_ino == 0) {
                    691:                if (spacefree + dsize < newentrysize)
                    692:                        panic("wdir: compact1");
                    693:                newdir.d_reclen = spacefree + dsize;
                    694:        } else {
                    695:                if (spacefree < newentrysize)
                    696:                        panic("wdir: compact2");
                    697:                newdir.d_reclen = spacefree;
                    698:                ep->d_reclen = dsize;
                    699:                ep = (struct direct *)((char *)ep + dsize);
                    700:        }
                    701:        bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
                    702:        error = bwrite(bp);
                    703:        dp->i_flag |= IUPD|ICHG;
                    704:        if (!error && ndp->ni_ufs.ufs_endoff &&
                    705:            ndp->ni_ufs.ufs_endoff < dp->i_size)
                    706:                error = itrunc(dp, (u_long)ndp->ni_ufs.ufs_endoff, IO_SYNC);
                    707:        return (error);
                    708: }
                    709: 
                    710: /*
                    711:  * Remove a directory entry after a call to namei, using
                    712:  * the parameters which it left in nameidata. The entry
                    713:  * ni_ufs.ufs_offset contains the offset into the directory of the
                    714:  * entry to be eliminated.  The ni_ufs.ufs_count field contains the
                    715:  * size of the previous record in the directory.  If this
                    716:  * is 0, the first entry is being deleted, so we need only
                    717:  * zero the inode number to mark the entry as free.  If the
                    718:  * entry is not the first in the directory, we must reclaim
                    719:  * the space of the now empty record by adding the record size
                    720:  * to the size of the previous entry.
                    721:  */
                    722: dirremove(ndp)
                    723:        register struct nameidata *ndp;
                    724: {
                    725:        register struct inode *dp = VTOI(ndp->ni_dvp);
                    726:        struct direct *ep;
                    727:        struct buf *bp;
                    728:        int error;
                    729: 
                    730:        if (ndp->ni_ufs.ufs_count == 0) {
                    731:                /*
                    732:                 * First entry in block: set d_ino to zero.
                    733:                 */
                    734:                error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&ep, &bp);
                    735:                if (error)
                    736:                        return (error);
                    737:                ep->d_ino = 0;
                    738:                error = bwrite(bp);
                    739:                dp->i_flag |= IUPD|ICHG;
                    740:                return (error);
                    741:        }
                    742:        /*
                    743:         * Collapse new free space into previous entry.
                    744:         */
                    745:        if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset - ndp->ni_ufs.ufs_count,
                    746:            (char **)&ep, &bp)) {
                    747:                return (error);
                    748:        }
                    749:        ep->d_reclen += ndp->ni_ufs.ufs_reclen;
                    750:        error = bwrite(bp);
                    751:        dp->i_flag |= IUPD|ICHG;
                    752:        return (error);
                    753: }
                    754: 
                    755: /*
                    756:  * Rewrite an existing directory entry to point at the inode
                    757:  * supplied.  The parameters describing the directory entry are
                    758:  * set up by a call to namei.
                    759:  */
                    760: dirrewrite(dp, ip, ndp)
                    761:        struct inode *dp, *ip;
                    762:        struct nameidata *ndp;
                    763: {
                    764:        struct direct *ep;
                    765:        struct buf *bp;
                    766:        int error;
                    767: 
                    768:        if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&ep, &bp))
                    769:                return (error);
                    770:        ep->d_ino = ip->i_number;
                    771:        error = bwrite(bp);
                    772:        dp->i_flag |= IUPD|ICHG;
                    773:        return (error);
                    774: }
                    775: 
                    776: /*
                    777:  * Return buffer with contents of block "offset"
                    778:  * from the beginning of directory "ip".  If "res"
                    779:  * is non-zero, fill it in with a pointer to the
                    780:  * remaining space in the directory.
                    781:  */
                    782: blkatoff(ip, offset, res, bpp)
                    783:        struct inode *ip;
                    784:        off_t offset;
                    785:        char **res;
                    786:        struct buf **bpp;
                    787: {
                    788:        register struct fs *fs = ip->i_fs;
                    789:        daddr_t lbn = lblkno(fs, offset);
                    790:        int bsize = blksize(fs, ip, lbn);
                    791:        struct buf *bp;
                    792:        daddr_t bn;
                    793:        int error;
                    794: 
                    795:        *bpp = 0;
                    796:        if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) {
                    797:                brelse(bp);
                    798:                return (error);
                    799:        }
                    800:        if (res)
                    801:                *res = bp->b_un.b_addr + blkoff(fs, offset);
                    802:        *bpp = bp;
                    803:        return (0);
                    804: }
                    805: 
                    806: /*
                    807:  * Check if a directory is empty or not.
                    808:  * Inode supplied must be locked.
                    809:  *
                    810:  * Using a struct dirtemplate here is not precisely
                    811:  * what we want, but better than using a struct direct.
                    812:  *
                    813:  * NB: does not handle corrupted directories.
                    814:  */
                    815: dirempty(ip, parentino, cred)
                    816:        register struct inode *ip;
                    817:        ino_t parentino;
                    818:        struct ucred *cred;
                    819: {
                    820:        register off_t off;
                    821:        struct dirtemplate dbuf;
                    822:        register struct direct *dp = (struct direct *)&dbuf;
                    823:        int error, count;
                    824: #define        MINDIRSIZ (sizeof (struct dirtemplate) / 2)
                    825: 
                    826:        for (off = 0; off < ip->i_size; off += dp->d_reclen) {
                    827:                error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
                    828:                   UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
                    829:                /*
                    830:                 * Since we read MINDIRSIZ, residual must
                    831:                 * be 0 unless we're at end of file.
                    832:                 */
                    833:                if (error || count != 0)
                    834:                        return (0);
                    835:                /* avoid infinite loops */
                    836:                if (dp->d_reclen == 0)
                    837:                        return (0);
                    838:                /* skip empty entries */
                    839:                if (dp->d_ino == 0)
                    840:                        continue;
                    841:                /* accept only "." and ".." */
                    842:                if (dp->d_namlen > 2)
                    843:                        return (0);
                    844:                if (dp->d_name[0] != '.')
                    845:                        return (0);
                    846:                /*
                    847:                 * At this point d_namlen must be 1 or 2.
                    848:                 * 1 implies ".", 2 implies ".." if second
                    849:                 * char is also "."
                    850:                 */
                    851:                if (dp->d_namlen == 1)
                    852:                        continue;
                    853:                if (dp->d_name[1] == '.' && dp->d_ino == parentino)
                    854:                        continue;
                    855:                return (0);
                    856:        }
                    857:        return (1);
                    858: }
                    859: 
                    860: /*
                    861:  * Check if source directory is in the path of the target directory.
                    862:  * Target is supplied locked, source is unlocked.
                    863:  * The target is always iput() before returning.
                    864:  */
                    865: checkpath(source, target, cred)
                    866:        struct inode *source, *target;
                    867:        struct ucred *cred;
                    868: {
                    869:        struct dirtemplate dirbuf;
                    870:        struct inode *ip;
                    871:        int error = 0;
                    872: 
                    873:        ip = target;
                    874:        if (ip->i_number == source->i_number) {
                    875:                error = EEXIST;
                    876:                goto out;
                    877:        }
                    878:        if (ip->i_number == ROOTINO)
                    879:                goto out;
                    880: 
                    881:        for (;;) {
                    882:                if ((ip->i_mode&IFMT) != IFDIR) {
                    883:                        error = ENOTDIR;
                    884:                        break;
                    885:                }
                    886:                error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)&dirbuf,
                    887:                        sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
                    888:                        IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
                    889:                if (error != 0)
                    890:                        break;
                    891:                if (dirbuf.dotdot_namlen != 2 ||
                    892:                    dirbuf.dotdot_name[0] != '.' ||
                    893:                    dirbuf.dotdot_name[1] != '.') {
                    894:                        error = ENOTDIR;
                    895:                        break;
                    896:                }
                    897:                if (dirbuf.dotdot_ino == source->i_number) {
                    898:                        error = EINVAL;
                    899:                        break;
                    900:                }
                    901:                if (dirbuf.dotdot_ino == ROOTINO)
                    902:                        break;
                    903:                iput(ip);
                    904:                if (error = iget(ip, dirbuf.dotdot_ino, &ip))
                    905:                        break;
                    906:        }
                    907: 
                    908: out:
                    909:        if (error == ENOTDIR)
                    910:                printf("checkpath: .. not a directory\n");
                    911:        if (ip != NULL)
                    912:                iput(ip);
                    913:        return (error);
                    914: }

unix.superglobalmegacorp.com

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