[BACK]Return to ufs_lookup.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ufs

Annotation of src/sys/ufs/ufs/ufs_lookup.c, Revision 1.18

1.18    ! perry       1: /*     $NetBSD: ufs_lookup.c,v 1.17 1998/07/28 18:37:48 thorpej Exp $  */
1.2       cgd         2:
1.1       mycroft     3: /*
                      4:  * Copyright (c) 1989, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  * (c) UNIX System Laboratories, Inc.
                      7:  * All or some portions of this file are derived from material licensed
                      8:  * to the University of California by American Telephone and Telegraph
                      9:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     10:  * the permission of UNIX System Laboratories, Inc.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  * 3. All advertising materials mentioning features or use of this software
                     21:  *    must display the following acknowledgement:
                     22:  *     This product includes software developed by the University of
                     23:  *     California, Berkeley and its contributors.
                     24:  * 4. Neither the name of the University nor the names of its contributors
                     25:  *    may be used to endorse or promote products derived from this software
                     26:  *    without specific prior written permission.
                     27:  *
                     28:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     29:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     30:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     31:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     32:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     33:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     34:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     35:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     36:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     37:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     38:  * SUCH DAMAGE.
                     39:  *
1.3       mycroft    40:  *     @(#)ufs_lookup.c        8.9 (Berkeley) 8/11/94
1.1       mycroft    41:  */
                     42:
                     43: #include <sys/param.h>
1.7       christos   44: #include <sys/systm.h>
1.1       mycroft    45: #include <sys/namei.h>
                     46: #include <sys/buf.h>
                     47: #include <sys/file.h>
                     48: #include <sys/mount.h>
                     49: #include <sys/vnode.h>
                     50:
                     51: #include <ufs/ufs/quota.h>
                     52: #include <ufs/ufs/inode.h>
                     53: #include <ufs/ufs/dir.h>
                     54: #include <ufs/ufs/ufsmount.h>
                     55: #include <ufs/ufs/ufs_extern.h>
1.15      bouyer     56: #include <ufs/ufs/ufs_bswap.h>
1.1       mycroft    57:
                     58: struct nchstats nchstats;
                     59: #ifdef DIAGNOSTIC
                     60: int    dirchk = 1;
                     61: #else
                     62: int    dirchk = 0;
                     63: #endif
                     64:
1.15      bouyer     65: #define FSFMT(vp)   ((vp)->v_mount->mnt_maxsymlinklen <= 0)
1.1       mycroft    66:
                     67: /*
                     68:  * Convert a component of a pathname into a pointer to a locked inode.
                     69:  * This is a very central and rather complicated routine.
                     70:  * If the file system is not maintained in a strict tree hierarchy,
                     71:  * this can result in a deadlock situation (see comments in code below).
                     72:  *
                     73:  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
                     74:  * on whether the name is to be looked up, created, renamed, or deleted.
                     75:  * When CREATE, RENAME, or DELETE is specified, information usable in
                     76:  * creating, renaming, or deleting a directory entry may be calculated.
                     77:  * If flag has LOCKPARENT or'ed into it and the target of the pathname
                     78:  * exists, lookup returns both the target and its parent directory locked.
                     79:  * When creating or renaming and LOCKPARENT is specified, the target may
                     80:  * not be ".".  When deleting and LOCKPARENT is specified, the target may
                     81:  * be "."., but the caller must check to ensure it does an vrele and vput
                     82:  * instead of two vputs.
                     83:  *
                     84:  * Overall outline of ufs_lookup:
                     85:  *
                     86:  *     check accessibility of directory
                     87:  *     look for name in cache, if found, then if at end of path
                     88:  *       and deleting or creating, drop it, else return name
                     89:  *     search for name in directory, to found or notfound
                     90:  * notfound:
                     91:  *     if creating, return locked directory, leaving info on available slots
                     92:  *     else return error
                     93:  * found:
                     94:  *     if at end of path and deleting, return information to allow delete
                     95:  *     if at end of path and rewriting (RENAME and LOCKPARENT), lock target
                     96:  *       inode and return info to allow rewrite
                     97:  *     if not at end, add name to cache; if at end and neither creating
                     98:  *       nor deleting, add name to cache
                     99:  */
                    100: int
1.7       christos  101: ufs_lookup(v)
                    102:        void *v;
                    103: {
1.1       mycroft   104:        struct vop_lookup_args /* {
                    105:                struct vnode *a_dvp;
                    106:                struct vnode **a_vpp;
                    107:                struct componentname *a_cnp;
1.7       christos  108:        } */ *ap = v;
1.1       mycroft   109:        register struct vnode *vdp;     /* vnode for directory being searched */
                    110:        register struct inode *dp;      /* inode for directory being searched */
                    111:        struct buf *bp;                 /* a buffer of directory entries */
                    112:        register struct direct *ep;     /* the current directory entry */
                    113:        int entryoffsetinblock;         /* offset of ep in bp's buffer */
                    114:        enum {NONE, COMPACT, FOUND} slotstatus;
                    115:        doff_t slotoffset;              /* offset of area with free space */
                    116:        int slotsize;                   /* size of area at slotoffset */
                    117:        int slotfreespace;              /* amount of space free in slot */
                    118:        int slotneeded;                 /* size of the entry we're seeking */
                    119:        int numdirpasses;               /* strategy for directory search */
                    120:        doff_t endsearch;               /* offset to end directory search */
                    121:        doff_t prevoff;                 /* prev entry dp->i_offset */
                    122:        struct vnode *pdp;              /* saved dp during symlink work */
                    123:        struct vnode *tdp;              /* returned by VFS_VGET */
                    124:        doff_t enduseful;               /* pointer past last used dir slot */
                    125:        u_long bmask;                   /* block offset mask */
                    126:        int lockparent;                 /* 1 => lockparent flag is set */
                    127:        int wantparent;                 /* 1 => wantparent or lockparent flag */
                    128:        int namlen, error;
                    129:        struct vnode **vpp = ap->a_vpp;
                    130:        struct componentname *cnp = ap->a_cnp;
                    131:        struct ucred *cred = cnp->cn_cred;
                    132:        int flags = cnp->cn_flags;
                    133:        int nameiop = cnp->cn_nameiop;
1.15      bouyer    134:        const int needswap = UFS_MPNEEDSWAP(ap->a_dvp->v_mount);
1.1       mycroft   135:
                    136:        bp = NULL;
                    137:        slotoffset = -1;
                    138:        *vpp = NULL;
                    139:        vdp = ap->a_dvp;
                    140:        dp = VTOI(vdp);
                    141:        lockparent = flags & LOCKPARENT;
                    142:        wantparent = flags & (LOCKPARENT|WANTPARENT);
                    143:
1.15      bouyer    144:
1.1       mycroft   145:        /*
                    146:         * Check accessiblity of directory.
                    147:         */
1.11      mycroft   148:        if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
1.1       mycroft   149:                return (error);
                    150:
1.14      fvdl      151:        if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
                    152:            (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
                    153:                return (EROFS);
                    154:
1.1       mycroft   155:        /*
                    156:         * We now have a segment name to search for, and a directory to search.
                    157:         *
                    158:         * Before tediously performing a linear scan of the directory,
                    159:         * check the name cache to see if the directory/name pair
                    160:         * we are looking for is known already.
                    161:         */
1.7       christos  162:        if ((error = cache_lookup(vdp, vpp, cnp)) != 0) {
1.1       mycroft   163:                int vpid;       /* capability number of vnode */
                    164:
                    165:                if (error == ENOENT)
                    166:                        return (error);
                    167:                /*
                    168:                 * Get the next vnode in the path.
                    169:                 * See comment below starting `Step through' for
                    170:                 * an explaination of the locking protocol.
                    171:                 */
                    172:                pdp = vdp;
                    173:                dp = VTOI(*vpp);
                    174:                vdp = *vpp;
                    175:                vpid = vdp->v_id;
                    176:                if (pdp == vdp) {   /* lookup on "." */
                    177:                        VREF(vdp);
                    178:                        error = 0;
                    179:                } else if (flags & ISDOTDOT) {
1.14      fvdl      180:                        VOP_UNLOCK(pdp, 0);
                    181:                        error = vget(vdp, LK_EXCLUSIVE);
1.1       mycroft   182:                        if (!error && lockparent && (flags & ISLASTCN))
1.14      fvdl      183:                                error = vn_lock(pdp, LK_EXCLUSIVE);
1.1       mycroft   184:                } else {
1.14      fvdl      185:                        error = vget(vdp, LK_EXCLUSIVE);
1.1       mycroft   186:                        if (!lockparent || error || !(flags & ISLASTCN))
1.14      fvdl      187:                                VOP_UNLOCK(pdp, 0);
1.1       mycroft   188:                }
                    189:                /*
                    190:                 * Check that the capability number did not change
                    191:                 * while we were waiting for the lock.
                    192:                 */
                    193:                if (!error) {
                    194:                        if (vpid == vdp->v_id)
                    195:                                return (0);
                    196:                        vput(vdp);
                    197:                        if (lockparent && pdp != vdp && (flags & ISLASTCN))
1.14      fvdl      198:                                VOP_UNLOCK(pdp, 0);
1.1       mycroft   199:                }
1.14      fvdl      200:                if ((error = vn_lock(pdp, LK_EXCLUSIVE)) != 0)
1.1       mycroft   201:                        return (error);
                    202:                vdp = pdp;
                    203:                dp = VTOI(pdp);
                    204:                *vpp = NULL;
                    205:        }
                    206:
                    207:        /*
                    208:         * Suppress search for slots unless creating
                    209:         * file and at end of pathname, in which case
                    210:         * we watch for a place to put the new file in
                    211:         * case it doesn't already exist.
                    212:         */
                    213:        slotstatus = FOUND;
                    214:        slotfreespace = slotsize = slotneeded = 0;
                    215:        if ((nameiop == CREATE || nameiop == RENAME) &&
                    216:            (flags & ISLASTCN)) {
                    217:                slotstatus = NONE;
                    218:                slotneeded = (sizeof(struct direct) - MAXNAMLEN +
                    219:                        cnp->cn_namelen + 3) &~ 3;
                    220:        }
                    221:
                    222:        /*
                    223:         * If there is cached information on a previous search of
                    224:         * this directory, pick up where we last left off.
                    225:         * We cache only lookups as these are the most common
                    226:         * and have the greatest payoff. Caching CREATE has little
                    227:         * benefit as it usually must search the entire directory
                    228:         * to determine that the entry does not exist. Caching the
                    229:         * location of the last DELETE or RENAME has not reduced
                    230:         * profiling time and hence has been removed in the interest
                    231:         * of simplicity.
                    232:         */
                    233:        bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
                    234:        if (nameiop != LOOKUP || dp->i_diroff == 0 ||
1.13      bouyer    235:            dp->i_diroff > dp->i_ffs_size) {
1.1       mycroft   236:                entryoffsetinblock = 0;
                    237:                dp->i_offset = 0;
                    238:                numdirpasses = 1;
                    239:        } else {
                    240:                dp->i_offset = dp->i_diroff;
                    241:                if ((entryoffsetinblock = dp->i_offset & bmask) &&
                    242:                    (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
                    243:                        return (error);
                    244:                numdirpasses = 2;
                    245:                nchstats.ncs_2passes++;
                    246:        }
                    247:        prevoff = dp->i_offset;
1.13      bouyer    248:        endsearch = roundup(dp->i_ffs_size, DIRBLKSIZ);
1.1       mycroft   249:        enduseful = 0;
                    250:
                    251: searchloop:
                    252:        while (dp->i_offset < endsearch) {
                    253:                /*
                    254:                 * If necessary, get the next directory block.
                    255:                 */
                    256:                if ((dp->i_offset & bmask) == 0) {
                    257:                        if (bp != NULL)
                    258:                                brelse(bp);
1.7       christos  259:                        error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL,
                    260:                                             &bp);
                    261:                        if (error)
1.1       mycroft   262:                                return (error);
                    263:                        entryoffsetinblock = 0;
                    264:                }
                    265:                /*
                    266:                 * If still looking for a slot, and at a DIRBLKSIZE
                    267:                 * boundary, have to start looking for free space again.
                    268:                 */
                    269:                if (slotstatus == NONE &&
                    270:                    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
                    271:                        slotoffset = -1;
                    272:                        slotfreespace = 0;
                    273:                }
                    274:                /*
                    275:                 * Get pointer to next entry.
                    276:                 * Full validation checks are slow, so we only check
                    277:                 * enough to insure forward progress through the
                    278:                 * directory. Complete checks can be run by patching
                    279:                 * "dirchk" to be true.
                    280:                 */
                    281:                ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
                    282:                if (ep->d_reclen == 0 ||
1.7       christos  283:                    (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
1.1       mycroft   284:                        int i;
                    285:
                    286:                        ufs_dirbad(dp, dp->i_offset, "mangled entry");
                    287:                        i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
                    288:                        dp->i_offset += i;
                    289:                        entryoffsetinblock += i;
                    290:                        continue;
                    291:                }
                    292:
                    293:                /*
                    294:                 * If an appropriate sized slot has not yet been found,
                    295:                 * check to see if one is available. Also accumulate space
                    296:                 * in the current block so that we can determine if
                    297:                 * compaction is viable.
                    298:                 */
                    299:                if (slotstatus != FOUND) {
1.15      bouyer    300:                        int size = ufs_rw16(ep->d_reclen, needswap);
1.1       mycroft   301:
                    302:                        if (ep->d_ino != 0)
1.15      bouyer    303:                                size -= DIRSIZ(FSFMT(vdp), ep, needswap);
1.1       mycroft   304:                        if (size > 0) {
                    305:                                if (size >= slotneeded) {
                    306:                                        slotstatus = FOUND;
                    307:                                        slotoffset = dp->i_offset;
1.15      bouyer    308:                                        slotsize = ufs_rw16(ep->d_reclen,
1.16      kleink    309:                                            needswap);
1.1       mycroft   310:                                } else if (slotstatus == NONE) {
                    311:                                        slotfreespace += size;
                    312:                                        if (slotoffset == -1)
                    313:                                                slotoffset = dp->i_offset;
                    314:                                        if (slotfreespace >= slotneeded) {
                    315:                                                slotstatus = COMPACT;
                    316:                                                slotsize = dp->i_offset +
1.16      kleink    317:                                                    ufs_rw16(ep->d_reclen,
                    318:                                                             needswap)
                    319:                                                    - slotoffset;
1.1       mycroft   320:                                        }
                    321:                                }
                    322:                        }
                    323:                }
                    324:
                    325:                /*
                    326:                 * Check for a name match.
                    327:                 */
                    328:                if (ep->d_ino) {
1.15      bouyer    329: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    330:                                if (vdp->v_mount->mnt_maxsymlinklen > 0 || needswap != 0)
1.1       mycroft   331:                                        namlen = ep->d_namlen;
                    332:                                else
                    333:                                        namlen = ep->d_type;
1.15      bouyer    334: #else
                    335:                                if (vdp->v_mount->mnt_maxsymlinklen <= 0 && needswap != 0)
                    336:                                        namlen = ep->d_type;
                    337:                                else
1.1       mycroft   338:                                namlen = ep->d_namlen;
1.15      bouyer    339: #endif
1.1       mycroft   340:                        if (namlen == cnp->cn_namelen &&
1.18    ! perry     341:                            !memcmp(cnp->cn_nameptr, ep->d_name,
1.1       mycroft   342:                                (unsigned)namlen)) {
                    343:                                /*
                    344:                                 * Save directory entry's inode number and
                    345:                                 * reclen in ndp->ni_ufs area, and release
                    346:                                 * directory buffer.
                    347:                                 */
1.4       mycroft   348:                                if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
                    349:                                    ep->d_type == DT_WHT) {
1.3       mycroft   350:                                        slotstatus = FOUND;
                    351:                                        slotoffset = dp->i_offset;
1.16      kleink    352:                                        slotsize = ufs_rw16(ep->d_reclen,
                    353:                                            needswap);
1.3       mycroft   354:                                        dp->i_reclen = slotsize;
1.6       mycroft   355:                                        /*
                    356:                                         * This is used to set dp->i_endoff,
                    357:                                         * which may be used by ufs_direnter2()
                    358:                                         * as a length to truncate the
                    359:                                         * directory to.  Therefore, it must
                    360:                                         * point past the end of the last
                    361:                                         * non-empty directory entry.  We don't
                    362:                                         * know where that is in this case, so
                    363:                                         * we effectively disable shrinking by
                    364:                                         * using the existing size of the
                    365:                                         * directory.
                    366:                                         *
                    367:                                         * Note that we wouldn't expect to
                    368:                                         * shrink the directory while rewriting
                    369:                                         * an existing entry anyway.
                    370:                                         */
                    371:                                        enduseful = endsearch;
1.3       mycroft   372:                                        ap->a_cnp->cn_flags |= ISWHITEOUT;
                    373:                                        numdirpasses--;
                    374:                                        goto notfound;
                    375:                                }
1.15      bouyer    376:                                dp->i_ino = ufs_rw32(ep->d_ino, needswap);
                    377:                                dp->i_reclen = ufs_rw16(ep->d_reclen, needswap);
1.3       mycroft   378:                                brelse(bp);
1.1       mycroft   379:                                goto found;
                    380:                        }
                    381:                }
                    382:                prevoff = dp->i_offset;
1.15      bouyer    383:                dp->i_offset += ufs_rw16(ep->d_reclen, needswap);
                    384:                entryoffsetinblock += ufs_rw16(ep->d_reclen, needswap);
1.1       mycroft   385:                if (ep->d_ino)
                    386:                        enduseful = dp->i_offset;
                    387:        }
1.3       mycroft   388: notfound:
1.1       mycroft   389:        /*
                    390:         * If we started in the middle of the directory and failed
                    391:         * to find our target, we must check the beginning as well.
                    392:         */
                    393:        if (numdirpasses == 2) {
                    394:                numdirpasses--;
                    395:                dp->i_offset = 0;
                    396:                endsearch = dp->i_diroff;
                    397:                goto searchloop;
                    398:        }
                    399:        if (bp != NULL)
                    400:                brelse(bp);
                    401:        /*
                    402:         * If creating, and at end of pathname and current
                    403:         * directory has not been removed, then can consider
                    404:         * allowing file to be created.
                    405:         */
1.3       mycroft   406:        if ((nameiop == CREATE || nameiop == RENAME ||
                    407:             (nameiop == DELETE &&
                    408:              (ap->a_cnp->cn_flags & DOWHITEOUT) &&
                    409:              (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
1.13      bouyer    410:            (flags & ISLASTCN) && dp->i_ffs_nlink != 0) {
1.12      kleink    411:                /*
1.1       mycroft   412:                 * Access for write is interpreted as allowing
                    413:                 * creation of files in the directory.
                    414:                 */
1.7       christos  415:                error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
                    416:                if (error)
1.1       mycroft   417:                        return (error);
                    418:                /*
                    419:                 * Return an indication of where the new directory
                    420:                 * entry should be put.  If we didn't find a slot,
                    421:                 * then set dp->i_count to 0 indicating
                    422:                 * that the new slot belongs at the end of the
                    423:                 * directory. If we found a slot, then the new entry
                    424:                 * can be put in the range from dp->i_offset to
                    425:                 * dp->i_offset + dp->i_count.
                    426:                 */
                    427:                if (slotstatus == NONE) {
1.13      bouyer    428:                        dp->i_offset = roundup(dp->i_ffs_size, DIRBLKSIZ);
1.1       mycroft   429:                        dp->i_count = 0;
1.6       mycroft   430:                        enduseful = dp->i_offset;
1.3       mycroft   431:                } else if (nameiop == DELETE) {
                    432:                        dp->i_offset = slotoffset;
                    433:                        if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
                    434:                                dp->i_count = 0;
                    435:                        else
                    436:                                dp->i_count = dp->i_offset - prevoff;
1.1       mycroft   437:                } else {
                    438:                        dp->i_offset = slotoffset;
                    439:                        dp->i_count = slotsize;
                    440:                        if (enduseful < slotoffset + slotsize)
                    441:                                enduseful = slotoffset + slotsize;
                    442:                }
1.6       mycroft   443:                dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
1.1       mycroft   444:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    445:                /*
                    446:                 * We return with the directory locked, so that
                    447:                 * the parameters we set up above will still be
                    448:                 * valid if we actually decide to do a direnter().
                    449:                 * We return ni_vp == NULL to indicate that the entry
                    450:                 * does not currently exist; we leave a pointer to
                    451:                 * the (locked) directory inode in ndp->ni_dvp.
                    452:                 * The pathname buffer is saved so that the name
                    453:                 * can be obtained later.
                    454:                 *
                    455:                 * NB - if the directory is unlocked, then this
                    456:                 * information cannot be used.
                    457:                 */
                    458:                cnp->cn_flags |= SAVENAME;
                    459:                if (!lockparent)
1.14      fvdl      460:                        VOP_UNLOCK(vdp, 0);
1.1       mycroft   461:                return (EJUSTRETURN);
                    462:        }
                    463:        /*
                    464:         * Insert name into cache (as non-existent) if appropriate.
                    465:         */
                    466:        if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
                    467:                cache_enter(vdp, *vpp, cnp);
                    468:        return (ENOENT);
                    469:
                    470: found:
                    471:        if (numdirpasses == 2)
                    472:                nchstats.ncs_pass2++;
                    473:        /*
                    474:         * Check that directory length properly reflects presence
                    475:         * of this entry.
                    476:         */
1.15      bouyer    477:        if (entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep, needswap) >
                    478:                dp->i_ffs_size) {
1.1       mycroft   479:                ufs_dirbad(dp, dp->i_offset, "i_size too small");
1.15      bouyer    480:                dp->i_ffs_size = entryoffsetinblock +
                    481:                        DIRSIZ(FSFMT(vdp), ep, needswap);
1.1       mycroft   482:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    483:        }
                    484:
                    485:        /*
                    486:         * Found component in pathname.
                    487:         * If the final component of path name, save information
                    488:         * in the cache as to where the entry was found.
                    489:         */
                    490:        if ((flags & ISLASTCN) && nameiop == LOOKUP)
                    491:                dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
                    492:
                    493:        /*
                    494:         * If deleting, and at end of pathname, return
                    495:         * parameters which can be used to remove file.
                    496:         * If the wantparent flag isn't set, we return only
                    497:         * the directory (in ndp->ni_dvp), otherwise we go
                    498:         * on and lock the inode, being careful with ".".
                    499:         */
                    500:        if (nameiop == DELETE && (flags & ISLASTCN)) {
                    501:                /*
                    502:                 * Write access to directory required to delete files.
                    503:                 */
1.7       christos  504:                error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
                    505:                if (error)
1.1       mycroft   506:                        return (error);
                    507:                /*
                    508:                 * Return pointer to current entry in dp->i_offset,
                    509:                 * and distance past previous entry (if there
                    510:                 * is a previous entry in this block) in dp->i_count.
                    511:                 * Save directory inode pointer in ndp->ni_dvp for dirremove().
                    512:                 */
                    513:                if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
                    514:                        dp->i_count = 0;
                    515:                else
                    516:                        dp->i_count = dp->i_offset - prevoff;
                    517:                if (dp->i_number == dp->i_ino) {
                    518:                        VREF(vdp);
                    519:                        *vpp = vdp;
                    520:                        return (0);
                    521:                }
1.7       christos  522:                error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
                    523:                if (error)
1.1       mycroft   524:                        return (error);
                    525:                /*
                    526:                 * If directory is "sticky", then user must own
                    527:                 * the directory, or the file in it, else she
                    528:                 * may not delete it (unless she's root). This
                    529:                 * implements append-only directories.
                    530:                 */
1.13      bouyer    531:                if ((dp->i_ffs_mode & ISVTX) &&
1.1       mycroft   532:                    cred->cr_uid != 0 &&
1.13      bouyer    533:                    cred->cr_uid != dp->i_ffs_uid &&
                    534:                    VTOI(tdp)->i_ffs_uid != cred->cr_uid) {
1.1       mycroft   535:                        vput(tdp);
                    536:                        return (EPERM);
                    537:                }
                    538:                *vpp = tdp;
                    539:                if (!lockparent)
1.14      fvdl      540:                        VOP_UNLOCK(vdp, 0);
1.1       mycroft   541:                return (0);
                    542:        }
                    543:
                    544:        /*
                    545:         * If rewriting (RENAME), return the inode and the
                    546:         * information required to rewrite the present directory
                    547:         * Must get inode of directory entry to verify it's a
                    548:         * regular file, or empty directory.
                    549:         */
1.14      fvdl      550:        if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
1.7       christos  551:                error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
                    552:                if (error)
1.1       mycroft   553:                        return (error);
                    554:                /*
                    555:                 * Careful about locking second inode.
                    556:                 * This can only occur if the target is ".".
                    557:                 */
                    558:                if (dp->i_number == dp->i_ino)
                    559:                        return (EISDIR);
1.7       christos  560:                error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
                    561:                if (error)
1.1       mycroft   562:                        return (error);
                    563:                *vpp = tdp;
                    564:                cnp->cn_flags |= SAVENAME;
                    565:                if (!lockparent)
1.14      fvdl      566:                        VOP_UNLOCK(vdp, 0);
1.1       mycroft   567:                return (0);
                    568:        }
                    569:
                    570:        /*
                    571:         * Step through the translation in the name.  We do not `vput' the
                    572:         * directory because we may need it again if a symbolic link
                    573:         * is relative to the current directory.  Instead we save it
                    574:         * unlocked as "pdp".  We must get the target inode before unlocking
                    575:         * the directory to insure that the inode will not be removed
                    576:         * before we get it.  We prevent deadlock by always fetching
                    577:         * inodes from the root, moving down the directory tree. Thus
                    578:         * when following backward pointers ".." we must unlock the
                    579:         * parent directory before getting the requested directory.
                    580:         * There is a potential race condition here if both the current
                    581:         * and parent directories are removed before the VFS_VGET for the
                    582:         * inode associated with ".." returns.  We hope that this occurs
                    583:         * infrequently since we cannot avoid this race condition without
                    584:         * implementing a sophisticated deadlock detection algorithm.
                    585:         * Note also that this simple deadlock detection scheme will not
                    586:         * work if the file system has any hard links other than ".."
                    587:         * that point backwards in the directory structure.
                    588:         */
                    589:        pdp = vdp;
                    590:        if (flags & ISDOTDOT) {
1.14      fvdl      591:                VOP_UNLOCK(pdp, 0);     /* race to get the inode */
1.7       christos  592:                error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
                    593:                if (error) {
1.14      fvdl      594:                        vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
1.1       mycroft   595:                        return (error);
                    596:                }
                    597:                if (lockparent && (flags & ISLASTCN) &&
1.14      fvdl      598:                    (error = vn_lock(pdp, LK_EXCLUSIVE))) {
1.1       mycroft   599:                        vput(tdp);
                    600:                        return (error);
                    601:                }
                    602:                *vpp = tdp;
                    603:        } else if (dp->i_number == dp->i_ino) {
                    604:                VREF(vdp);      /* we want ourself, ie "." */
                    605:                *vpp = vdp;
                    606:        } else {
1.7       christos  607:                error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
                    608:                if (error)
1.1       mycroft   609:                        return (error);
                    610:                if (!lockparent || !(flags & ISLASTCN))
1.14      fvdl      611:                        VOP_UNLOCK(pdp, 0);
1.1       mycroft   612:                *vpp = tdp;
                    613:        }
                    614:
                    615:        /*
                    616:         * Insert name into cache if appropriate.
                    617:         */
                    618:        if (cnp->cn_flags & MAKEENTRY)
                    619:                cache_enter(vdp, *vpp, cnp);
                    620:        return (0);
                    621: }
                    622:
                    623: void
                    624: ufs_dirbad(ip, offset, how)
                    625:        struct inode *ip;
                    626:        doff_t offset;
                    627:        char *how;
                    628: {
                    629:        struct mount *mp;
                    630:
                    631:        mp = ITOV(ip)->v_mount;
1.9       christos  632:        printf("%s: bad dir ino %d at offset %d: %s\n",
1.1       mycroft   633:            mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
                    634:        if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0)
                    635:                panic("bad dir");
                    636: }
                    637:
                    638: /*
                    639:  * Do consistency checking on a directory entry:
                    640:  *     record length must be multiple of 4
                    641:  *     entry must fit in rest of its DIRBLKSIZ block
                    642:  *     record must be large enough to contain entry
                    643:  *     name is not longer than MAXNAMLEN
                    644:  *     name must be as long as advertised, and null terminated
                    645:  */
                    646: int
                    647: ufs_dirbadentry(dp, ep, entryoffsetinblock)
                    648:        struct vnode *dp;
                    649:        register struct direct *ep;
                    650:        int entryoffsetinblock;
                    651: {
                    652:        register int i;
                    653:        int namlen;
1.15      bouyer    654:        const int needswap = UFS_MPNEEDSWAP(dp->v_mount);
1.1       mycroft   655:
1.16      kleink    656: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    657:        if (dp->v_mount->mnt_maxsymlinklen > 0 || needswap != 0)
                    658:                namlen = ep->d_namlen;
                    659:        else
                    660:                namlen = ep->d_type;
                    661: #else
                    662:        if (dp->v_mount->mnt_maxsymlinklen == 0 && needswap != 0)
                    663:                namlen = ep->d_type;
                    664:        else
1.1       mycroft   665:                namlen = ep->d_namlen;
1.16      kleink    666: #endif
1.15      bouyer    667:        if ((ufs_rw16(ep->d_reclen, needswap) & 0x3) != 0 ||
                    668:            ufs_rw16(ep->d_reclen, needswap) >
1.16      kleink    669:                DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
1.15      bouyer    670:            ufs_rw16(ep->d_reclen, needswap) <
1.16      kleink    671:                DIRSIZ(FSFMT(dp), ep, needswap) ||
                    672:            namlen > MAXNAMLEN) {
1.1       mycroft   673:                /*return (1); */
1.15      bouyer    674:                printf("First bad, reclen=%x, DIRSIZ=%d, namlen=%d, flags=%x "
                    675:                        "entryoffsetinblock=%d\n",
                    676:                        ufs_rw16(ep->d_reclen, needswap),
                    677:                        DIRSIZ(FSFMT(dp), ep, needswap),
                    678:                        namlen, dp->v_mount->mnt_flag, entryoffsetinblock);
1.1       mycroft   679:                goto bad;
                    680:        }
                    681:        if (ep->d_ino == 0)
                    682:                return (0);
                    683:        for (i = 0; i < namlen; i++)
                    684:                if (ep->d_name[i] == '\0') {
                    685:                        /*return (1); */
1.9       christos  686:                        printf("Second bad\n");
1.1       mycroft   687:                        goto bad;
                    688:        }
                    689:        if (ep->d_name[i])
                    690:                goto bad;
                    691:        return (0);
                    692: bad:
                    693:        return (1);
                    694: }
                    695:
                    696: /*
                    697:  * Write a directory entry after a call to namei, using the parameters
                    698:  * that it left in nameidata.  The argument ip is the inode which the new
                    699:  * directory entry will refer to.  Dvp is a pointer to the directory to
                    700:  * be written, which was left locked by namei. Remaining parameters
                    701:  * (dp->i_offset, dp->i_count) indicate how the space for the new
                    702:  * entry is to be obtained.
                    703:  */
                    704: int
                    705: ufs_direnter(ip, dvp, cnp)
                    706:        struct inode *ip;
                    707:        struct vnode *dvp;
                    708:        register struct componentname *cnp;
                    709: {
                    710:        register struct inode *dp;
1.16      kleink    711:
                    712:        /* In host byte order here, ufs_direnter2() will swap it. */
                    713:        struct direct newdir;
1.1       mycroft   714:
                    715: #ifdef DIAGNOSTIC
                    716:        if ((cnp->cn_flags & SAVENAME) == 0)
                    717:                panic("direnter: missing name");
                    718: #endif
                    719:        dp = VTOI(dvp);
                    720:        newdir.d_ino = ip->i_number;
                    721:        newdir.d_namlen = cnp->cn_namelen;
1.18    ! perry     722:        memcpy(newdir.d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
1.1       mycroft   723:        if (dvp->v_mount->mnt_maxsymlinklen > 0)
1.13      bouyer    724:                newdir.d_type = IFTODT(ip->i_ffs_mode);
1.16      kleink    725:        else
1.1       mycroft   726:                newdir.d_type = 0;
1.16      kleink    727:
                    728:        /*
                    729:         * Byte order swapping and new->old format conversion is handled by
                    730:         * ufs_direnter2().
                    731:         */
1.3       mycroft   732:        return (ufs_direnter2(dvp, &newdir, cnp->cn_cred, cnp->cn_proc));
                    733: }
                    734:
                    735: /*
                    736:  * Common entry point for directory entry removal used by ufs_direnter
                    737:  * and ufs_whiteout
                    738:  */
1.7       christos  739: int
1.3       mycroft   740: ufs_direnter2(dvp, dirp, cr, p)
                    741:        struct vnode *dvp;
                    742:        struct direct *dirp;
                    743:        struct ucred *cr;
                    744:        struct proc *p;
                    745: {
                    746:        int newentrysize;
                    747:        struct inode *dp;
                    748:        struct buf *bp;
                    749:        struct iovec aiov;
                    750:        struct uio auio;
                    751:        u_int dsize;
                    752:        struct direct *ep, *nep;
                    753:        int error, loc, spacefree;
                    754:        char *dirbuf;
1.15      bouyer    755:        const int needswap = UFS_MPNEEDSWAP(dvp->v_mount);
1.3       mycroft   756:
                    757:        dp = VTOI(dvp);
1.15      bouyer    758:        newentrysize = DIRSIZ(0, dirp, 0);
1.3       mycroft   759:
1.1       mycroft   760:        if (dp->i_count == 0) {
                    761:                /*
                    762:                 * If dp->i_count is 0, then namei could find no
                    763:                 * space in the directory. Here, dp->i_offset will
                    764:                 * be on a directory block boundary and we will write the
                    765:                 * new entry into a fresh block.
                    766:                 */
                    767:                if (dp->i_offset & (DIRBLKSIZ - 1))
1.3       mycroft   768:                        panic("ufs_direnter2: newblk");
1.1       mycroft   769:                auio.uio_offset = dp->i_offset;
1.15      bouyer    770:                dirp->d_reclen = ufs_rw16(DIRBLKSIZ, needswap);
                    771:                dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
                    772:                if (dvp->v_mount->mnt_maxsymlinklen <= 0)
                    773: #if (BYTE_ORDER == LITTLE_ENDIAN)
1.16      kleink    774:                        if (needswap == 0) {
1.15      bouyer    775: #else
1.16      kleink    776:                        if (needswap != 0) {
1.15      bouyer    777: #endif
                    778:                                u_char tmp = dirp->d_namlen;
                    779:                                dirp->d_namlen = dirp->d_type;
                    780:                                dirp->d_type = tmp;
                    781:                        }
1.1       mycroft   782:                auio.uio_resid = newentrysize;
                    783:                aiov.iov_len = newentrysize;
1.3       mycroft   784:                aiov.iov_base = (caddr_t)dirp;
1.1       mycroft   785:                auio.uio_iov = &aiov;
                    786:                auio.uio_iovcnt = 1;
                    787:                auio.uio_rw = UIO_WRITE;
                    788:                auio.uio_segflg = UIO_SYSSPACE;
                    789:                auio.uio_procp = (struct proc *)0;
1.3       mycroft   790:                error = VOP_WRITE(dvp, &auio, IO_SYNC, cr);
1.1       mycroft   791:                if (DIRBLKSIZ >
                    792:                    VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
                    793:                        /* XXX should grow with balloc() */
1.3       mycroft   794:                        panic("ufs_direnter2: frag size");
1.1       mycroft   795:                else if (!error) {
1.13      bouyer    796:                        dp->i_ffs_size = roundup(dp->i_ffs_size, DIRBLKSIZ);
1.1       mycroft   797:                        dp->i_flag |= IN_CHANGE;
                    798:                }
                    799:                return (error);
                    800:        }
                    801:
                    802:        /*
                    803:         * If dp->i_count is non-zero, then namei found space
                    804:         * for the new entry in the range dp->i_offset to
                    805:         * dp->i_offset + dp->i_count in the directory.
                    806:         * To use this space, we may have to compact the entries located
                    807:         * there, by copying them together towards the beginning of the
                    808:         * block, leaving the free space in one usable chunk at the end.
                    809:         */
                    810:
                    811:        /*
                    812:         * Increase size of directory if entry eats into new space.
                    813:         * This should never push the size past a new multiple of
                    814:         * DIRBLKSIZE.
                    815:         *
                    816:         * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
                    817:         */
1.13      bouyer    818:        if (dp->i_offset + dp->i_count > dp->i_ffs_size)
                    819:                dp->i_ffs_size = dp->i_offset + dp->i_count;
1.1       mycroft   820:        /*
                    821:         * Get the block containing the space for the new directory entry.
                    822:         */
1.7       christos  823:        error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
                    824:        if (error)
1.1       mycroft   825:                return (error);
                    826:        /*
                    827:         * Find space for the new entry. In the simple case, the entry at
                    828:         * offset base will have the space. If it does not, then namei
                    829:         * arranged that compacting the region dp->i_offset to
                    830:         * dp->i_offset + dp->i_count would yield the
                    831:         * space.
                    832:         */
                    833:        ep = (struct direct *)dirbuf;
1.15      bouyer    834:        dsize = DIRSIZ(FSFMT(dvp), ep, needswap);
                    835:        spacefree = ufs_rw16(ep->d_reclen, needswap) - dsize;
                    836:        for (loc = ufs_rw16(ep->d_reclen, needswap); loc < dp->i_count; ) {
1.1       mycroft   837:                nep = (struct direct *)(dirbuf + loc);
                    838:                if (ep->d_ino) {
                    839:                        /* trim the existing slot */
1.15      bouyer    840:                        ep->d_reclen = ufs_rw16(dsize, needswap);
1.1       mycroft   841:                        ep = (struct direct *)((char *)ep + dsize);
                    842:                } else {
                    843:                        /* overwrite; nothing there; header is ours */
                    844:                        spacefree += dsize;
                    845:                }
1.15      bouyer    846:                dsize = DIRSIZ(FSFMT(dvp), nep, needswap);
                    847:                spacefree += ufs_rw16(nep->d_reclen, needswap) - dsize;
                    848:                loc += ufs_rw16(nep->d_reclen, needswap);
1.18    ! perry     849:                memcpy((caddr_t)ep, (caddr_t)nep, dsize);
1.1       mycroft   850:        }
                    851:        /*
                    852:         * Update the pointer fields in the previous entry (if any),
                    853:         * copy in the new entry, and write out the block.
                    854:         */
1.3       mycroft   855:        if (ep->d_ino == 0 ||
1.15      bouyer    856:            (ufs_rw32(ep->d_ino, needswap) == WINO &&
1.18    ! perry     857:             memcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1.1       mycroft   858:                if (spacefree + dsize < newentrysize)
1.3       mycroft   859:                        panic("ufs_direnter2: compact1");
                    860:                dirp->d_reclen = spacefree + dsize;
1.1       mycroft   861:        } else {
                    862:                if (spacefree < newentrysize)
1.3       mycroft   863:                        panic("ufs_direnter2: compact2");
                    864:                dirp->d_reclen = spacefree;
1.15      bouyer    865:                ep->d_reclen = ufs_rw16(dsize, needswap);
1.1       mycroft   866:                ep = (struct direct *)((char *)ep + dsize);
                    867:        }
1.15      bouyer    868:        dirp->d_reclen = ufs_rw16(dirp->d_reclen, needswap);
                    869:        dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
                    870:        if (dvp->v_mount->mnt_maxsymlinklen <= 0)
                    871: #if (BYTE_ORDER == LITTLE_ENDIAN)
1.16      kleink    872:                if (needswap == 0) {
1.15      bouyer    873: #else
1.16      kleink    874:                if (needswap != 0) {
1.15      bouyer    875: #endif
                    876:                        u_char tmp = dirp->d_namlen;
                    877:                        dirp->d_namlen = dirp->d_type;
                    878:                        dirp->d_type = tmp;
                    879:                }
1.18    ! perry     880:        memcpy((caddr_t)ep, (caddr_t)dirp, (u_int)newentrysize);
1.1       mycroft   881:        error = VOP_BWRITE(bp);
                    882:        dp->i_flag |= IN_CHANGE | IN_UPDATE;
1.13      bouyer    883:        if (!error && dp->i_endoff && dp->i_endoff < dp->i_ffs_size)
1.3       mycroft   884:                error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cr, p);
1.1       mycroft   885:        return (error);
                    886: }
                    887:
                    888: /*
                    889:  * Remove a directory entry after a call to namei, using
                    890:  * the parameters which it left in nameidata. The entry
                    891:  * dp->i_offset contains the offset into the directory of the
                    892:  * entry to be eliminated.  The dp->i_count field contains the
                    893:  * size of the previous record in the directory.  If this
                    894:  * is 0, the first entry is being deleted, so we need only
                    895:  * zero the inode number to mark the entry as free.  If the
                    896:  * entry is not the first in the directory, we must reclaim
                    897:  * the space of the now empty record by adding the record size
                    898:  * to the size of the previous entry.
                    899:  */
                    900: int
                    901: ufs_dirremove(dvp, cnp)
                    902:        struct vnode *dvp;
                    903:        struct componentname *cnp;
                    904: {
                    905:        register struct inode *dp;
                    906:        struct direct *ep;
                    907:        struct buf *bp;
                    908:        int error;
                    909:
                    910:        dp = VTOI(dvp);
1.3       mycroft   911:
                    912:        if (cnp->cn_flags & DOWHITEOUT) {
                    913:                /*
                    914:                 * Whiteout entry: set d_ino to WINO.
                    915:                 */
1.7       christos  916:                error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep,
                    917:                                     &bp);
                    918:                if (error)
1.3       mycroft   919:                        return (error);
1.15      bouyer    920:                ep->d_ino = ufs_rw32(WINO, UFS_MPNEEDSWAP(dvp->v_mount));
1.3       mycroft   921:                ep->d_type = DT_WHT;
                    922:                error = VOP_BWRITE(bp);
                    923:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    924:                return (error);
                    925:        }
                    926:
1.1       mycroft   927:        if (dp->i_count == 0) {
                    928:                /*
                    929:                 * First entry in block: set d_ino to zero.
                    930:                 */
1.7       christos  931:                error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep,
                    932:                                     &bp);
                    933:                if (error)
1.1       mycroft   934:                        return (error);
                    935:                ep->d_ino = 0;
                    936:                error = VOP_BWRITE(bp);
                    937:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    938:                return (error);
                    939:        }
                    940:        /*
                    941:         * Collapse new free space into previous entry.
                    942:         */
1.7       christos  943:        error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
                    944:                             (char **)&ep, &bp);
                    945:        if (error)
1.1       mycroft   946:                return (error);
1.16      kleink    947:        ep->d_reclen = ufs_rw16(ufs_rw16(ep->d_reclen,
                    948:            UFS_MPNEEDSWAP(dvp->v_mount)) + dp->i_reclen,
                    949:            UFS_MPNEEDSWAP(dvp->v_mount));
1.1       mycroft   950:        error = VOP_BWRITE(bp);
                    951:        dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    952:        return (error);
                    953: }
                    954:
                    955: /*
                    956:  * Rewrite an existing directory entry to point at the inode
                    957:  * supplied.  The parameters describing the directory entry are
                    958:  * set up by a call to namei.
                    959:  */
                    960: int
                    961: ufs_dirrewrite(dp, ip, cnp)
                    962:        struct inode *dp, *ip;
                    963:        struct componentname *cnp;
                    964: {
                    965:        struct buf *bp;
                    966:        struct direct *ep;
                    967:        struct vnode *vdp = ITOV(dp);
                    968:        int error;
                    969:
1.7       christos  970:        error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
                    971:        if (error)
1.1       mycroft   972:                return (error);
1.15      bouyer    973:        ep->d_ino = ufs_rw32(ip->i_number, UFS_MPNEEDSWAP(vdp->v_mount));
1.1       mycroft   974:        if (vdp->v_mount->mnt_maxsymlinklen > 0)
1.13      bouyer    975:                ep->d_type = IFTODT(ip->i_ffs_mode);
1.1       mycroft   976:        error = VOP_BWRITE(bp);
                    977:        dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    978:        return (error);
                    979: }
                    980:
                    981: /*
                    982:  * Check if a directory is empty or not.
                    983:  * Inode supplied must be locked.
                    984:  *
                    985:  * Using a struct dirtemplate here is not precisely
                    986:  * what we want, but better than using a struct direct.
                    987:  *
                    988:  * NB: does not handle corrupted directories.
                    989:  */
                    990: int
                    991: ufs_dirempty(ip, parentino, cred)
                    992:        register struct inode *ip;
                    993:        ino_t parentino;
                    994:        struct ucred *cred;
                    995: {
                    996:        register off_t off;
                    997:        struct dirtemplate dbuf;
                    998:        register struct direct *dp = (struct direct *)&dbuf;
1.17      thorpej   999:        int error, namlen;
                   1000:        size_t count;
1.1       mycroft  1001: #define        MINDIRSIZ (sizeof (struct dirtemplate) / 2)
                   1002:
1.15      bouyer   1003:        for (off = 0; off < ip->i_ffs_size;
1.16      kleink   1004:             off += ufs_rw16(dp->d_reclen, UFS_IPNEEDSWAP(ip))) {
1.1       mycroft  1005:                error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
                   1006:                   UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
                   1007:                /*
                   1008:                 * Since we read MINDIRSIZ, residual must
                   1009:                 * be 0 unless we're at end of file.
                   1010:                 */
                   1011:                if (error || count != 0)
                   1012:                        return (0);
                   1013:                /* avoid infinite loops */
                   1014:                if (dp->d_reclen == 0)
                   1015:                        return (0);
                   1016:                /* skip empty entries */
1.15      bouyer   1017:                if (dp->d_ino == 0 ||
1.16      kleink   1018:                    ufs_rw32(dp->d_ino, UFS_IPNEEDSWAP(ip)) == WINO)
1.1       mycroft  1019:                        continue;
                   1020:                /* accept only "." and ".." */
1.16      kleink   1021: #if (BYTE_ORDER == LITTLE_ENDIAN)
                   1022:                if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0 ||
                   1023:                    UFS_IPNEEDSWAP(ip) != 0)
                   1024:                        namlen = dp->d_namlen;
                   1025:                else
                   1026:                        namlen = dp->d_type;
                   1027: #else
                   1028:                if (ITOV(ip)->v_mount->mnt_maxsymlinklen == 0 &&
                   1029:                    UFS_IPNEEDSWAP(ip) != 0)
                   1030:                        namlen = dp->d_type;
                   1031:                else
                   1032:                        namlen = dp->d_namlen;
                   1033: #endif
1.1       mycroft  1034:                if (namlen > 2)
                   1035:                        return (0);
                   1036:                if (dp->d_name[0] != '.')
                   1037:                        return (0);
                   1038:                /*
                   1039:                 * At this point namlen must be 1 or 2.
                   1040:                 * 1 implies ".", 2 implies ".." if second
                   1041:                 * char is also "."
                   1042:                 */
                   1043:                if (namlen == 1)
                   1044:                        continue;
1.15      bouyer   1045:                if (dp->d_name[1] == '.' &&
1.16      kleink   1046:                    ufs_rw32(dp->d_ino, UFS_IPNEEDSWAP(ip)) == parentino)
1.1       mycroft  1047:                        continue;
                   1048:                return (0);
                   1049:        }
                   1050:        return (1);
                   1051: }
                   1052:
                   1053: /*
                   1054:  * Check if source directory is in the path of the target directory.
                   1055:  * Target is supplied locked, source is unlocked.
                   1056:  * The target is always vput before returning.
                   1057:  */
                   1058: int
                   1059: ufs_checkpath(source, target, cred)
                   1060:        struct inode *source, *target;
                   1061:        struct ucred *cred;
                   1062: {
1.15      bouyer   1063:        struct vnode *vp = vp = ITOV(target);
1.1       mycroft  1064:        int error, rootino, namlen;
                   1065:        struct dirtemplate dirbuf;
1.15      bouyer   1066:        const int needswap = UFS_MPNEEDSWAP(vp->v_mount);
1.1       mycroft  1067:
                   1068:        vp = ITOV(target);
                   1069:        if (target->i_number == source->i_number) {
                   1070:                error = EEXIST;
                   1071:                goto out;
                   1072:        }
                   1073:        rootino = ROOTINO;
                   1074:        error = 0;
                   1075:        if (target->i_number == rootino)
                   1076:                goto out;
                   1077:
                   1078:        for (;;) {
                   1079:                if (vp->v_type != VDIR) {
                   1080:                        error = ENOTDIR;
                   1081:                        break;
                   1082:                }
                   1083:                error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
                   1084:                        sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1.17      thorpej  1085:                        IO_NODELOCKED, cred, NULL, (struct proc *)0);
1.1       mycroft  1086:                if (error != 0)
                   1087:                        break;
1.16      kleink   1088: #if (BYTE_ORDER == LITTLE_ENDIAN)
                   1089:                if (vp->v_mount->mnt_maxsymlinklen > 0 ||
                   1090:                    needswap != 0)
                   1091:                        namlen = dirbuf.dotdot_namlen;
                   1092:                else
                   1093:                        namlen = dirbuf.dotdot_type;
                   1094: #else
                   1095:                if (vp->v_mount->mnt_maxsymlinklen == 0 &&
                   1096:                    needswap != 0)
                   1097:                        namlen = dirbuf.dotdot_type;
                   1098:                else
1.1       mycroft  1099:                        namlen = dirbuf.dotdot_namlen;
1.16      kleink   1100: #endif
1.1       mycroft  1101:                if (namlen != 2 ||
                   1102:                    dirbuf.dotdot_name[0] != '.' ||
                   1103:                    dirbuf.dotdot_name[1] != '.') {
                   1104:                        error = ENOTDIR;
                   1105:                        break;
                   1106:                }
1.15      bouyer   1107:                if (ufs_rw32(dirbuf.dotdot_ino, needswap) == source->i_number) {
1.1       mycroft  1108:                        error = EINVAL;
                   1109:                        break;
                   1110:                }
1.15      bouyer   1111:                if (ufs_rw32(dirbuf.dotdot_ino, needswap) == rootino)
1.1       mycroft  1112:                        break;
                   1113:                vput(vp);
1.15      bouyer   1114:                error = VFS_VGET(vp->v_mount,
1.16      kleink   1115:                    ufs_rw32(dirbuf.dotdot_ino, needswap), &vp);
1.7       christos 1116:                if (error) {
1.1       mycroft  1117:                        vp = NULL;
                   1118:                        break;
                   1119:                }
                   1120:        }
                   1121:
                   1122: out:
                   1123:        if (error == ENOTDIR)
1.9       christos 1124:                printf("checkpath: .. not a directory\n");
1.1       mycroft  1125:        if (vp != NULL)
                   1126:                vput(vp);
                   1127:        return (error);
                   1128: }

CVSweb <webmaster@jp.NetBSD.org>