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

Annotation of src/sys/ufs/lfs/ulfs_lookup.c, Revision 1.25

1.25    ! mlelstv     1: /*     $NetBSD: ulfs_lookup.c,v 1.24 2015/05/31 15:48:03 hannken Exp $ */
1.1       dholland    2: /*  from NetBSD: ufs_lookup.c,v 1.122 2013/01/22 09:39:18 dholland Exp  */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  * (c) UNIX System Laboratories, Inc.
                      8:  * All or some portions of this file are derived from material licensed
                      9:  * to the University of California by American Telephone and Telegraph
                     10:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     11:  * the permission of UNIX System Laboratories, Inc.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
                     21:  * 3. Neither the name of the University nor the names of its contributors
                     22:  *    may be used to endorse or promote products derived from this software
                     23:  *    without specific prior written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     26:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     27:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     28:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     29:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     30:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     31:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     32:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     33:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     34:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     35:  * SUCH DAMAGE.
                     36:  *
                     37:  *     @(#)ufs_lookup.c        8.9 (Berkeley) 8/11/94
                     38:  */
                     39:
                     40: #include <sys/cdefs.h>
1.25    ! mlelstv    41: __KERNEL_RCSID(0, "$NetBSD: ulfs_lookup.c,v 1.24 2015/05/31 15:48:03 hannken Exp $");
1.1       dholland   42:
                     43: #ifdef _KERNEL_OPT
1.3       dholland   44: #include "opt_lfs.h"
1.1       dholland   45: #endif
                     46:
                     47: #include <sys/param.h>
                     48: #include <sys/systm.h>
                     49: #include <sys/namei.h>
                     50: #include <sys/buf.h>
                     51: #include <sys/file.h>
                     52: #include <sys/stat.h>
                     53: #include <sys/mount.h>
                     54: #include <sys/vnode.h>
                     55: #include <sys/kernel.h>
                     56: #include <sys/kauth.h>
                     57: #include <sys/wapbl.h>
                     58: #include <sys/fstrans.h>
                     59: #include <sys/proc.h>
                     60: #include <sys/kmem.h>
                     61:
1.13      dholland   62: #include <ufs/lfs/lfs_extern.h>
                     63:
1.2       dholland   64: #include <ufs/lfs/ulfs_inode.h>
1.3       dholland   65: #ifdef LFS_DIRHASH
1.2       dholland   66: #include <ufs/lfs/ulfs_dirhash.h>
1.1       dholland   67: #endif
1.2       dholland   68: #include <ufs/lfs/ulfsmount.h>
                     69: #include <ufs/lfs/ulfs_extern.h>
                     70: #include <ufs/lfs/ulfs_bswap.h>
1.1       dholland   71:
                     72: #include <miscfs/genfs/genfs.h>
                     73:
                     74: #ifdef DIAGNOSTIC
1.5       dholland   75: int    lfs_dirchk = 1;
1.1       dholland   76: #else
1.5       dholland   77: int    lfs_dirchk = 0;
1.1       dholland   78: #endif
                     79:
                     80: /*
                     81:  * Convert a component of a pathname into a pointer to a locked inode.
                     82:  * This is a very central and rather complicated routine.
                     83:  * If the file system is not maintained in a strict tree hierarchy,
                     84:  * this can result in a deadlock situation (see comments in code below).
                     85:  *
                     86:  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
                     87:  * on whether the name is to be looked up, created, renamed, or deleted.
                     88:  * When CREATE, RENAME, or DELETE is specified, information usable in
                     89:  * creating, renaming, or deleting a directory entry may be calculated.
                     90:  * If flag has LOCKPARENT or'ed into it and the target of the pathname
                     91:  * exists, lookup returns both the target and its parent directory locked.
                     92:  * When creating or renaming and LOCKPARENT is specified, the target may
                     93:  * not be ".".  When deleting and LOCKPARENT is specified, the target may
                     94:  * be "."., but the caller must check to ensure it does an vrele and vput
                     95:  * instead of two vputs.
                     96:  *
1.4       dholland   97:  * Overall outline of ulfs_lookup:
1.1       dholland   98:  *
                     99:  *     check accessibility of directory
                    100:  *     look for name in cache, if found, then if at end of path
                    101:  *       and deleting or creating, drop it, else return name
                    102:  *     search for name in directory, to found or notfound
                    103:  * notfound:
                    104:  *     if creating, return locked directory, leaving info on available slots
                    105:  *     else return error
                    106:  * found:
                    107:  *     if at end of path and deleting, return information to allow delete
                    108:  *     if at end of path and rewriting (RENAME and LOCKPARENT), lock target
                    109:  *       inode and return info to allow rewrite
                    110:  *     if not at end, add name to cache; if at end and neither creating
                    111:  *       nor deleting, add name to cache
                    112:  */
                    113: int
1.4       dholland  114: ulfs_lookup(void *v)
1.1       dholland  115: {
1.19      hannken   116:        struct vop_lookup_v2_args /* {
1.1       dholland  117:                struct vnode *a_dvp;
                    118:                struct vnode **a_vpp;
                    119:                struct componentname *a_cnp;
                    120:        } */ *ap = v;
                    121:        struct vnode *vdp = ap->a_dvp;  /* vnode for directory being searched */
                    122:        struct inode *dp = VTOI(vdp);   /* inode for directory being searched */
                    123:        struct buf *bp;                 /* a buffer of directory entries */
1.7       dholland  124:        struct lfs_direct *ep;          /* the current directory entry */
1.1       dholland  125:        int entryoffsetinblock;         /* offset of ep in bp's buffer */
                    126:        enum {
                    127:                NONE,           /* need to search a slot for our new entry */
                    128:                COMPACT,        /* a compaction can make a slot in the current
                    129:                                   DIRBLKSIZ block */
                    130:                FOUND,          /* found a slot (or no need to search) */
                    131:        } slotstatus;
                    132:        doff_t slotoffset;              /* offset of area with free space.
                    133:                                           a special value -1 for invalid */
                    134:        int slotsize;                   /* size of area at slotoffset */
                    135:        int slotfreespace;              /* accumulated amount of space free in
                    136:                                           the current DIRBLKSIZ block */
                    137:        int slotneeded;                 /* size of the entry we're seeking */
                    138:        int numdirpasses;               /* strategy for directory search */
                    139:        doff_t endsearch;               /* offset to end directory search */
                    140:        doff_t prevoff;                 /* previous value of ulr_offset */
1.24      hannken   141:        struct vnode *tdp;              /* returned by vcache_get */
1.1       dholland  142:        doff_t enduseful;               /* pointer past last used dir slot.
                    143:                                           used for directory truncation. */
                    144:        u_long bmask;                   /* block offset mask */
                    145:        int error;
                    146:        struct vnode **vpp = ap->a_vpp;
                    147:        struct componentname *cnp = ap->a_cnp;
                    148:        kauth_cred_t cred = cnp->cn_cred;
                    149:        int flags;
                    150:        int nameiop = cnp->cn_nameiop;
1.15      dholland  151:        struct lfs *fs = dp->i_lfs;
                    152:        const int needswap = ULFS_MPNEEDSWAP(fs);
                    153:        int dirblksiz = fs->um_dirblksiz;
1.1       dholland  154:        ino_t foundino;
1.4       dholland  155:        struct ulfs_lookup_results *results;
1.1       dholland  156:        int iswhiteout;                 /* temp result from cache_lookup() */
                    157:
                    158:        flags = cnp->cn_flags;
                    159:
                    160:        bp = NULL;
                    161:        slotoffset = -1;
                    162:        *vpp = NULL;
                    163:        endsearch = 0; /* silence compiler warning */
                    164:
                    165:        /*
                    166:         * Produce the auxiliary lookup results into i_crap. Increment
                    167:         * its serial number so elsewhere we can tell if we're using
                    168:         * stale results. This should not be done this way. XXX.
                    169:         */
                    170:        results = &dp->i_crap;
                    171:        dp->i_crapcounter++;
                    172:
                    173:        /*
                    174:         * Check accessiblity of directory.
                    175:         */
                    176:        if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
                    177:                return (error);
                    178:
                    179:        if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
                    180:            (nameiop == DELETE || nameiop == RENAME))
                    181:                return (EROFS);
                    182:
                    183:        /*
                    184:         * We now have a segment name to search for, and a directory to search.
                    185:         *
                    186:         * Before tediously performing a linear scan of the directory,
                    187:         * check the name cache to see if the directory/name pair
                    188:         * we are looking for is known already.
                    189:         */
                    190:        if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
                    191:                         cnp->cn_nameiop, cnp->cn_flags, &iswhiteout, vpp)) {
                    192:                if (iswhiteout) {
                    193:                        cnp->cn_flags |= ISWHITEOUT;
                    194:                }
                    195:                return *vpp == NULLVP ? ENOENT : 0;
                    196:        }
                    197:        if (iswhiteout) {
                    198:                /*
                    199:                 * The namecache set iswhiteout without finding a
                    200:                 * cache entry. As of this writing (20121014), this
                    201:                 * can happen if there was a whiteout entry that has
                    202:                 * been invalidated by the lookup. It is not clear if
                    203:                 * it is correct to set ISWHITEOUT in this case or
                    204:                 * not; however, doing so retains the prior behavior,
                    205:                 * so we'll go with that until some clearer answer
                    206:                 * appears. XXX
                    207:                 */
                    208:                cnp->cn_flags |= ISWHITEOUT;
                    209:        }
                    210:
                    211:        fstrans_start(vdp->v_mount, FSTRANS_SHARED);
                    212:
                    213:        /*
                    214:         * Suppress search for slots unless creating
                    215:         * file and at end of pathname, in which case
                    216:         * we watch for a place to put the new file in
                    217:         * case it doesn't already exist.
                    218:         */
                    219:        slotstatus = FOUND;
                    220:        slotfreespace = slotsize = slotneeded = 0;
                    221:        if ((nameiop == CREATE || nameiop == RENAME) && (flags & ISLASTCN)) {
                    222:                slotstatus = NONE;
1.9       dholland  223:                slotneeded = LFS_DIRECTSIZ(cnp->cn_namelen);
1.1       dholland  224:        }
                    225:
                    226:        /*
                    227:         * If there is cached information on a previous search of
                    228:         * this directory, pick up where we last left off.
                    229:         * We cache only lookups as these are the most common
                    230:         * and have the greatest payoff. Caching CREATE has little
                    231:         * benefit as it usually must search the entire directory
                    232:         * to determine that the entry does not exist. Caching the
                    233:         * location of the last DELETE or RENAME has not reduced
                    234:         * profiling time and hence has been removed in the interest
                    235:         * of simplicity.
                    236:         */
                    237:        bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
                    238:
1.3       dholland  239: #ifdef LFS_DIRHASH
1.1       dholland  240:        /*
                    241:         * Use dirhash for fast operations on large directories. The logic
                    242:         * to determine whether to hash the directory is contained within
1.4       dholland  243:         * ulfsdirhash_build(); a zero return means that it decided to hash
1.1       dholland  244:         * this directory and it successfully built up the hash table.
                    245:         */
1.4       dholland  246:        if (ulfsdirhash_build(dp) == 0) {
1.1       dholland  247:                /* Look for a free slot if needed. */
                    248:                enduseful = dp->i_size;
                    249:                if (slotstatus != FOUND) {
1.4       dholland  250:                        slotoffset = ulfsdirhash_findfree(dp, slotneeded,
1.1       dholland  251:                            &slotsize);
                    252:                        if (slotoffset >= 0) {
                    253:                                slotstatus = COMPACT;
1.4       dholland  254:                                enduseful = ulfsdirhash_enduseful(dp);
1.1       dholland  255:                                if (enduseful < 0)
                    256:                                        enduseful = dp->i_size;
                    257:                        }
                    258:                }
                    259:                /* Look up the component. */
                    260:                numdirpasses = 1;
                    261:                entryoffsetinblock = 0; /* silence compiler warning */
1.4       dholland  262:                switch (ulfsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
1.1       dholland  263:                    &results->ulr_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
                    264:                case 0:
1.7       dholland  265:                        ep = (struct lfs_direct *)((char *)bp->b_data +
1.1       dholland  266:                            (results->ulr_offset & bmask));
                    267:                        goto foundentry;
                    268:                case ENOENT:
                    269:                        results->ulr_offset = roundup(dp->i_size, dirblksiz);
                    270:                        goto notfound;
                    271:                default:
                    272:                        /* Something failed; just do a linear search. */
                    273:                        break;
                    274:                }
                    275:        }
1.3       dholland  276: #endif /* LFS_DIRHASH */
1.1       dholland  277:
                    278:        if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
                    279:            results->ulr_diroff >= dp->i_size) {
                    280:                entryoffsetinblock = 0;
                    281:                results->ulr_offset = 0;
                    282:                numdirpasses = 1;
                    283:        } else {
                    284:                results->ulr_offset = results->ulr_diroff;
                    285:                if ((entryoffsetinblock = results->ulr_offset & bmask) &&
1.4       dholland  286:                    (error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
1.1       dholland  287:                    NULL, &bp, false)))
                    288:                        goto out;
                    289:                numdirpasses = 2;
1.21      joerg     290:                namecache_count_2passes();
1.1       dholland  291:        }
                    292:        prevoff = results->ulr_offset;
                    293:        endsearch = roundup(dp->i_size, dirblksiz);
                    294:        enduseful = 0;
                    295:
                    296: searchloop:
                    297:        while (results->ulr_offset < endsearch) {
                    298:                if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
                    299:                        preempt();
                    300:                /*
                    301:                 * If necessary, get the next directory block.
                    302:                 */
                    303:                if ((results->ulr_offset & bmask) == 0) {
                    304:                        if (bp != NULL)
                    305:                                brelse(bp, 0);
1.4       dholland  306:                        error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
1.1       dholland  307:                            NULL, &bp, false);
                    308:                        if (error)
                    309:                                goto out;
                    310:                        entryoffsetinblock = 0;
                    311:                }
                    312:                /*
                    313:                 * If still looking for a slot, and at a DIRBLKSIZ
                    314:                 * boundary, have to start looking for free space again.
                    315:                 */
                    316:                if (slotstatus == NONE &&
                    317:                    (entryoffsetinblock & (dirblksiz - 1)) == 0) {
                    318:                        slotoffset = -1;
                    319:                        slotfreespace = 0;
                    320:                }
                    321:                /*
                    322:                 * Get pointer to next entry.
                    323:                 * Full validation checks are slow, so we only check
                    324:                 * enough to insure forward progress through the
                    325:                 * directory. Complete checks can be run by patching
1.5       dholland  326:                 * "lfs_dirchk" to be true.
1.1       dholland  327:                 */
                    328:                KASSERT(bp != NULL);
1.7       dholland  329:                ep = (struct lfs_direct *)((char *)bp->b_data + entryoffsetinblock);
1.1       dholland  330:                if (ep->d_reclen == 0 ||
1.5       dholland  331:                    (lfs_dirchk && ulfs_dirbadentry(vdp, ep, entryoffsetinblock))) {
1.1       dholland  332:                        int i;
                    333:
1.4       dholland  334:                        ulfs_dirbad(dp, results->ulr_offset, "mangled entry");
1.1       dholland  335:                        i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
                    336:                        results->ulr_offset += i;
                    337:                        entryoffsetinblock += i;
                    338:                        continue;
                    339:                }
                    340:
                    341:                /*
                    342:                 * If an appropriate sized slot has not yet been found,
                    343:                 * check to see if one is available. Also accumulate space
                    344:                 * in the current block so that we can determine if
                    345:                 * compaction is viable.
                    346:                 */
                    347:                if (slotstatus != FOUND) {
1.4       dholland  348:                        int size = ulfs_rw16(ep->d_reclen, needswap);
1.1       dholland  349:
                    350:                        if (ep->d_ino != 0)
1.9       dholland  351:                                size -= LFS_DIRSIZ(FSFMT(vdp), ep, needswap);
1.1       dholland  352:                        if (size > 0) {
                    353:                                if (size >= slotneeded) {
                    354:                                        slotstatus = FOUND;
                    355:                                        slotoffset = results->ulr_offset;
1.4       dholland  356:                                        slotsize = ulfs_rw16(ep->d_reclen,
1.1       dholland  357:                                            needswap);
                    358:                                } else if (slotstatus == NONE) {
                    359:                                        slotfreespace += size;
                    360:                                        if (slotoffset == -1)
                    361:                                                slotoffset = results->ulr_offset;
                    362:                                        if (slotfreespace >= slotneeded) {
                    363:                                                slotstatus = COMPACT;
                    364:                                                slotsize = results->ulr_offset +
1.4       dholland  365:                                                    ulfs_rw16(ep->d_reclen,
1.1       dholland  366:                                                             needswap) -
                    367:                                                    slotoffset;
                    368:                                        }
                    369:                                }
                    370:                        }
                    371:                }
                    372:
                    373:                /*
                    374:                 * Check for a name match.
                    375:                 */
                    376:                if (ep->d_ino) {
                    377:                        int namlen;
                    378:
                    379: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    380:                        if (FSFMT(vdp) && needswap == 0)
                    381:                                namlen = ep->d_type;
                    382:                        else
                    383:                                namlen = ep->d_namlen;
                    384: #else
                    385:                        if (FSFMT(vdp) && needswap != 0)
                    386:                                namlen = ep->d_type;
                    387:                        else
                    388:                                namlen = ep->d_namlen;
                    389: #endif
                    390:                        if (namlen == cnp->cn_namelen &&
                    391:                            !memcmp(cnp->cn_nameptr, ep->d_name,
                    392:                            (unsigned)namlen)) {
1.3       dholland  393: #ifdef LFS_DIRHASH
1.1       dholland  394: foundentry:
                    395: #endif
                    396:                                /*
                    397:                                 * Save directory entry's inode number and
                    398:                                 * reclen, and release directory buffer.
                    399:                                 */
1.7       dholland  400:                                if (!FSFMT(vdp) && ep->d_type == LFS_DT_WHT) {
1.1       dholland  401:                                        slotstatus = FOUND;
                    402:                                        slotoffset = results->ulr_offset;
1.4       dholland  403:                                        slotsize = ulfs_rw16(ep->d_reclen,
1.1       dholland  404:                                            needswap);
                    405:                                        results->ulr_reclen = slotsize;
                    406:                                        /*
                    407:                                         * This is used to set
                    408:                                         * results->ulr_endoff,
1.4       dholland  409:                                         * which may be used by ulfs_direnter()
1.1       dholland  410:                                         * as a length to truncate the
                    411:                                         * directory to.  Therefore, it must
                    412:                                         * point past the end of the last
                    413:                                         * non-empty directory entry.  We don't
                    414:                                         * know where that is in this case, so
                    415:                                         * we effectively disable shrinking by
                    416:                                         * using the existing size of the
                    417:                                         * directory.
                    418:                                         *
                    419:                                         * Note that we wouldn't expect to
                    420:                                         * shrink the directory while rewriting
                    421:                                         * an existing entry anyway.
                    422:                                         */
                    423:                                        enduseful = endsearch;
                    424:                                        cnp->cn_flags |= ISWHITEOUT;
                    425:                                        numdirpasses--;
                    426:                                        goto notfound;
                    427:                                }
1.4       dholland  428:                                foundino = ulfs_rw32(ep->d_ino, needswap);
1.1       dholland  429:                                results->ulr_reclen =
1.4       dholland  430:                                    ulfs_rw16(ep->d_reclen, needswap);
1.1       dholland  431:                                goto found;
                    432:                        }
                    433:                }
                    434:                prevoff = results->ulr_offset;
1.4       dholland  435:                results->ulr_offset += ulfs_rw16(ep->d_reclen, needswap);
                    436:                entryoffsetinblock += ulfs_rw16(ep->d_reclen, needswap);
1.1       dholland  437:                if (ep->d_ino)
                    438:                        enduseful = results->ulr_offset;
                    439:        }
                    440: notfound:
                    441:        /*
                    442:         * If we started in the middle of the directory and failed
                    443:         * to find our target, we must check the beginning as well.
                    444:         */
                    445:        if (numdirpasses == 2) {
                    446:                numdirpasses--;
                    447:                results->ulr_offset = 0;
                    448:                endsearch = results->ulr_diroff;
                    449:                goto searchloop;
                    450:        }
                    451:        if (bp != NULL)
                    452:                brelse(bp, 0);
                    453:        /*
                    454:         * If creating, and at end of pathname and current
                    455:         * directory has not been removed, then can consider
                    456:         * allowing file to be created.
                    457:         */
                    458:        if ((nameiop == CREATE || nameiop == RENAME ||
                    459:             (nameiop == DELETE &&
                    460:              (cnp->cn_flags & DOWHITEOUT) &&
                    461:              (cnp->cn_flags & ISWHITEOUT))) &&
                    462:            (flags & ISLASTCN) && dp->i_nlink != 0) {
                    463:                /*
                    464:                 * Access for write is interpreted as allowing
                    465:                 * creation of files in the directory.
                    466:                 */
                    467:                error = VOP_ACCESS(vdp, VWRITE, cred);
                    468:                if (error)
                    469:                        goto out;
                    470:                /*
                    471:                 * Return an indication of where the new directory
                    472:                 * entry should be put.  If we didn't find a slot,
                    473:                 * then set results->ulr_count to 0 indicating
                    474:                 * that the new slot belongs at the end of the
                    475:                 * directory. If we found a slot, then the new entry
                    476:                 * can be put in the range from results->ulr_offset to
                    477:                 * results->ulr_offset + results->ulr_count.
                    478:                 */
                    479:                if (slotstatus == NONE) {
                    480:                        results->ulr_offset = roundup(dp->i_size, dirblksiz);
                    481:                        results->ulr_count = 0;
                    482:                        enduseful = results->ulr_offset;
                    483:                } else if (nameiop == DELETE) {
                    484:                        results->ulr_offset = slotoffset;
                    485:                        if ((results->ulr_offset & (dirblksiz - 1)) == 0)
                    486:                                results->ulr_count = 0;
                    487:                        else
                    488:                                results->ulr_count =
                    489:                                    results->ulr_offset - prevoff;
                    490:                } else {
                    491:                        results->ulr_offset = slotoffset;
                    492:                        results->ulr_count = slotsize;
                    493:                        if (enduseful < slotoffset + slotsize)
                    494:                                enduseful = slotoffset + slotsize;
                    495:                }
                    496:                results->ulr_endoff = roundup(enduseful, dirblksiz);
                    497: #if 0 /* commented out by dbj. none of the on disk fields changed */
                    498:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    499: #endif
                    500:                /*
                    501:                 * We return with the directory locked, so that
                    502:                 * the parameters we set up above will still be
                    503:                 * valid if we actually decide to do a direnter().
                    504:                 * We return ni_vp == NULL to indicate that the entry
                    505:                 * does not currently exist; we leave a pointer to
                    506:                 * the (locked) directory inode in ndp->ni_dvp.
                    507:                 *
                    508:                 * NB - if the directory is unlocked, then this
                    509:                 * information cannot be used.
                    510:                 */
                    511:                error = EJUSTRETURN;
                    512:                goto out;
                    513:        }
                    514:        /*
                    515:         * Insert name into cache (as non-existent) if appropriate.
                    516:         */
                    517:        if (nameiop != CREATE) {
                    518:                cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
                    519:                            cnp->cn_flags);
                    520:        }
                    521:        error = ENOENT;
                    522:        goto out;
                    523:
                    524: found:
                    525:        if (numdirpasses == 2)
1.21      joerg     526:                namecache_count_pass2();
1.1       dholland  527:        /*
                    528:         * Check that directory length properly reflects presence
                    529:         * of this entry.
                    530:         */
1.9       dholland  531:        if (results->ulr_offset + LFS_DIRSIZ(FSFMT(vdp), ep, needswap) > dp->i_size) {
1.4       dholland  532:                ulfs_dirbad(dp, results->ulr_offset, "i_size too small");
1.1       dholland  533:                dp->i_size =
1.9       dholland  534:                    results->ulr_offset + LFS_DIRSIZ(FSFMT(vdp), ep, needswap);
1.1       dholland  535:                DIP_ASSIGN(dp, size, dp->i_size);
                    536:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    537:        }
                    538:        brelse(bp, 0);
                    539:
                    540:        /*
                    541:         * Found component in pathname.
                    542:         * If the final component of path name, save information
                    543:         * in the cache as to where the entry was found.
                    544:         */
                    545:        if ((flags & ISLASTCN) && nameiop == LOOKUP)
                    546:                results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
                    547:
                    548:        /*
                    549:         * If deleting, and at end of pathname, return
                    550:         * parameters which can be used to remove file.
                    551:         * Lock the inode, being careful with ".".
                    552:         */
                    553:        if (nameiop == DELETE && (flags & ISLASTCN)) {
                    554:                /*
                    555:                 * Return pointer to current entry in results->ulr_offset,
                    556:                 * and distance past previous entry (if there
                    557:                 * is a previous entry in this block) in results->ulr_count.
                    558:                 * Save directory inode pointer in ndp->ni_dvp for dirremove().
                    559:                 */
                    560:                if ((results->ulr_offset & (dirblksiz - 1)) == 0)
                    561:                        results->ulr_count = 0;
                    562:                else
                    563:                        results->ulr_count = results->ulr_offset - prevoff;
                    564:                if (dp->i_number == foundino) {
                    565:                        vref(vdp);
                    566:                        tdp = vdp;
                    567:                } else {
1.24      hannken   568:                        error = vcache_get(vdp->v_mount,
                    569:                            &foundino, sizeof(foundino), &tdp);
1.1       dholland  570:                        if (error)
                    571:                                goto out;
                    572:                }
                    573:                /*
                    574:                 * Write access to directory required to delete files.
                    575:                 */
                    576:                error = VOP_ACCESS(vdp, VWRITE, cred);
                    577:                if (error) {
1.24      hannken   578:                        vrele(tdp);
1.1       dholland  579:                        goto out;
                    580:                }
                    581:                /*
                    582:                 * If directory is "sticky", then user must own
                    583:                 * the directory, or the file in it, else she
                    584:                 * may not delete it (unless she's root). This
                    585:                 * implements append-only directories.
                    586:                 */
                    587:                if (dp->i_mode & ISVTX) {
                    588:                        error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
                    589:                            tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
                    590:                            VTOI(tdp)->i_uid));
                    591:                        if (error) {
1.24      hannken   592:                                vrele(tdp);
1.1       dholland  593:                                error = EPERM;
                    594:                                goto out;
                    595:                        }
                    596:                }
                    597:                *vpp = tdp;
                    598:                error = 0;
                    599:                goto out;
                    600:        }
                    601:
                    602:        /*
                    603:         * If rewriting (RENAME), return the inode and the
                    604:         * information required to rewrite the present directory
                    605:         * Must get inode of directory entry to verify it's a
                    606:         * regular file, or empty directory.
                    607:         */
                    608:        if (nameiop == RENAME && (flags & ISLASTCN)) {
                    609:                error = VOP_ACCESS(vdp, VWRITE, cred);
                    610:                if (error)
                    611:                        goto out;
                    612:                /*
                    613:                 * Careful about locking second inode.
                    614:                 * This can only occur if the target is ".".
                    615:                 */
                    616:                if (dp->i_number == foundino) {
                    617:                        error = EISDIR;
                    618:                        goto out;
                    619:                }
1.24      hannken   620:                error = vcache_get(vdp->v_mount,
                    621:                    &foundino, sizeof(foundino), &tdp);
1.1       dholland  622:                if (error)
                    623:                        goto out;
                    624:                *vpp = tdp;
                    625:                error = 0;
                    626:                goto out;
                    627:        }
                    628:
1.24      hannken   629:        if (dp->i_number == foundino) {
1.1       dholland  630:                vref(vdp);      /* we want ourself, ie "." */
                    631:                *vpp = vdp;
                    632:        } else {
1.24      hannken   633:                error = vcache_get(vdp->v_mount,
                    634:                    &foundino, sizeof(foundino), &tdp);
1.1       dholland  635:                if (error)
                    636:                        goto out;
                    637:                *vpp = tdp;
                    638:        }
                    639:
                    640:        /*
                    641:         * Insert name into cache if appropriate.
                    642:         */
                    643:        cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
                    644:        error = 0;
                    645:
                    646: out:
                    647:        fstrans_done(vdp->v_mount);
                    648:        return error;
                    649: }
                    650:
                    651: void
1.4       dholland  652: ulfs_dirbad(struct inode *ip, doff_t offset, const char *how)
1.1       dholland  653: {
                    654:        struct mount *mp;
                    655:
                    656:        mp = ITOV(ip)->v_mount;
                    657:        printf("%s: bad dir ino %llu at offset %d: %s\n",
                    658:            mp->mnt_stat.f_mntonname, (unsigned long long)ip->i_number,
                    659:            offset, how);
1.25    ! mlelstv   660:        if ((mp->mnt_flag & MNT_RDONLY) == 0)
1.1       dholland  661:                panic("bad dir");
                    662: }
                    663:
                    664: /*
                    665:  * Do consistency checking on a directory entry:
                    666:  *     record length must be multiple of 4
                    667:  *     entry must fit in rest of its DIRBLKSIZ block
                    668:  *     record must be large enough to contain entry
1.8       dholland  669:  *     name is not longer than LFS_MAXNAMLEN
1.1       dholland  670:  *     name must be as long as advertised, and null terminated
                    671:  */
                    672: int
1.7       dholland  673: ulfs_dirbadentry(struct vnode *dp, struct lfs_direct *ep, int entryoffsetinblock)
1.1       dholland  674: {
                    675:        int i;
                    676:        int namlen;
1.4       dholland  677:        struct ulfsmount *ump = VFSTOULFS(dp->v_mount);
1.15      dholland  678:        struct lfs *fs = ump->um_lfs;
                    679:        const int needswap = ULFS_MPNEEDSWAP(fs);
                    680:        int dirblksiz = fs->um_dirblksiz;
1.1       dholland  681:
                    682: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    683:        if (FSFMT(dp) && needswap == 0)
                    684:                namlen = ep->d_type;
                    685:        else
                    686:                namlen = ep->d_namlen;
                    687: #else
                    688:        if (FSFMT(dp) && needswap != 0)
                    689:                namlen = ep->d_type;
                    690:        else
                    691:                namlen = ep->d_namlen;
                    692: #endif
1.4       dholland  693:        if ((ulfs_rw16(ep->d_reclen, needswap) & 0x3) != 0 ||
                    694:            ulfs_rw16(ep->d_reclen, needswap) >
1.1       dholland  695:                dirblksiz - (entryoffsetinblock & (dirblksiz - 1)) ||
1.4       dholland  696:            ulfs_rw16(ep->d_reclen, needswap) <
1.9       dholland  697:                LFS_DIRSIZ(FSFMT(dp), ep, needswap) ||
1.8       dholland  698:            namlen > LFS_MAXNAMLEN) {
1.1       dholland  699:                /*return (1); */
                    700:                printf("First bad, reclen=%#x, DIRSIZ=%lu, namlen=%d, "
                    701:                        "flags=%#x, entryoffsetinblock=%d, dirblksiz = %d\n",
1.4       dholland  702:                        ulfs_rw16(ep->d_reclen, needswap),
1.9       dholland  703:                        (u_long)LFS_DIRSIZ(FSFMT(dp), ep, needswap),
1.1       dholland  704:                        namlen, dp->v_mount->mnt_flag, entryoffsetinblock,
                    705:                        dirblksiz);
                    706:                goto bad;
                    707:        }
                    708:        if (ep->d_ino == 0)
                    709:                return (0);
                    710:        for (i = 0; i < namlen; i++)
                    711:                if (ep->d_name[i] == '\0') {
                    712:                        /*return (1); */
                    713:                        printf("Second bad\n");
                    714:                        goto bad;
                    715:        }
                    716:        if (ep->d_name[i])
                    717:                goto bad;
                    718:        return (0);
                    719: bad:
                    720:        return (1);
                    721: }
                    722:
                    723: /*
                    724:  * Construct a new directory entry after a call to namei, using the
                    725:  * name in the componentname argument cnp. The argument ip is the
                    726:  * inode to which the new directory entry will refer.
                    727:  */
                    728: void
1.4       dholland  729: ulfs_makedirentry(struct inode *ip, struct componentname *cnp,
1.7       dholland  730:     struct lfs_direct *newdirp)
1.1       dholland  731: {
                    732:        newdirp->d_ino = ip->i_number;
                    733:        newdirp->d_namlen = cnp->cn_namelen;
                    734:        memcpy(newdirp->d_name, cnp->cn_nameptr, (size_t)cnp->cn_namelen);
                    735:        newdirp->d_name[cnp->cn_namelen] = '\0';
                    736:        if (FSFMT(ITOV(ip)))
                    737:                newdirp->d_type = 0;
                    738:        else
1.9       dholland  739:                newdirp->d_type = LFS_IFTODT(ip->i_mode);
1.1       dholland  740: }
                    741:
                    742: /*
                    743:  * Write a directory entry after a call to namei, using the parameters
1.4       dholland  744:  * that ulfs_lookup left in nameidata and in the ulfs_lookup_results.
1.1       dholland  745:  *
                    746:  * DVP is the directory to be updated. It must be locked.
1.4       dholland  747:  * ULR is the ulfs_lookup_results structure from the final lookup step.
1.1       dholland  748:  * TVP is not used. (XXX: why is it here? remove it)
                    749:  * DIRP is the new directory entry contents.
                    750:  * CNP is the componentname from the final lookup step.
                    751:  * NEWDIRBP is not used and (XXX) should be removed. The previous
                    752:  * comment here said it was used by the now-removed softupdates code.
                    753:  *
                    754:  * The link count of the target inode is *not* incremented; the
                    755:  * caller does that.
                    756:  *
1.4       dholland  757:  * If ulr->ulr_count is 0, ulfs_lookup did not find space to insert the
1.1       dholland  758:  * directory entry. ulr_offset, which is the place to put the entry,
                    759:  * should be on a block boundary (and should be at the end of the
                    760:  * directory AFAIK) and a fresh block is allocated to put the new
                    761:  * directory entry in.
                    762:  *
1.4       dholland  763:  * If ulr->ulr_count is not zero, ulfs_lookup found a slot to insert
1.1       dholland  764:  * the entry into. This slot ranges from ulr_offset to ulr_offset +
                    765:  * ulr_count. However, this slot may already be partially populated
                    766:  * requiring compaction. See notes below.
                    767:  *
                    768:  * Furthermore, if ulr_count is not zero and ulr_endoff is not the
                    769:  * same as i_size, the directory is truncated to size ulr_endoff.
                    770:  */
                    771: int
1.4       dholland  772: ulfs_direnter(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
1.7       dholland  773:     struct vnode *tvp, struct lfs_direct *dirp,
1.1       dholland  774:     struct componentname *cnp, struct buf *newdirbp)
                    775: {
                    776:        kauth_cred_t cr;
                    777:        int newentrysize;
                    778:        struct inode *dp;
                    779:        struct buf *bp;
                    780:        u_int dsize;
1.7       dholland  781:        struct lfs_direct *ep, *nep;
1.12      christos  782:        int error, ret, lfs_blkoff, loc, spacefree;
1.1       dholland  783:        char *dirbuf;
                    784:        struct timespec ts;
1.4       dholland  785:        struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
1.15      dholland  786:        struct lfs *fs = ump->um_lfs;
                    787:        const int needswap = ULFS_MPNEEDSWAP(fs);
                    788:        int dirblksiz = fs->um_dirblksiz;
1.1       dholland  789:
                    790:        error = 0;
                    791:        cr = cnp->cn_cred;
                    792:
                    793:        dp = VTOI(dvp);
1.9       dholland  794:        newentrysize = LFS_DIRSIZ(0, dirp, 0);
1.1       dholland  795:
                    796:        if (ulr->ulr_count == 0) {
                    797:                /*
                    798:                 * If ulr_count is 0, then namei could find no
                    799:                 * space in the directory. Here, ulr_offset will
                    800:                 * be on a directory block boundary and we will write the
                    801:                 * new entry into a fresh block.
                    802:                 */
                    803:                if (ulr->ulr_offset & (dirblksiz - 1))
1.4       dholland  804:                        panic("ulfs_direnter: newblk");
1.14      dholland  805:                if ((error = lfs_balloc(dvp, (off_t)ulr->ulr_offset, dirblksiz,
1.1       dholland  806:                    cr, B_CLRBUF | B_SYNC, &bp)) != 0) {
                    807:                        return (error);
                    808:                }
                    809:                dp->i_size = ulr->ulr_offset + dirblksiz;
                    810:                DIP_ASSIGN(dp, size, dp->i_size);
                    811:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    812:                uvm_vnp_setsize(dvp, dp->i_size);
1.4       dholland  813:                dirp->d_reclen = ulfs_rw16(dirblksiz, needswap);
                    814:                dirp->d_ino = ulfs_rw32(dirp->d_ino, needswap);
1.1       dholland  815:                if (FSFMT(dvp)) {
                    816: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    817:                        if (needswap == 0) {
                    818: #else
                    819:                        if (needswap != 0) {
                    820: #endif
                    821:                                u_char tmp = dirp->d_namlen;
                    822:                                dirp->d_namlen = dirp->d_type;
                    823:                                dirp->d_type = tmp;
                    824:                        }
                    825:                }
1.12      christos  826:                lfs_blkoff = ulr->ulr_offset & (ump->um_mountp->mnt_stat.f_iosize - 1);
                    827:                memcpy((char *)bp->b_data + lfs_blkoff, dirp, newentrysize);
1.3       dholland  828: #ifdef LFS_DIRHASH
1.1       dholland  829:                if (dp->i_dirhash != NULL) {
1.4       dholland  830:                        ulfsdirhash_newblk(dp, ulr->ulr_offset);
                    831:                        ulfsdirhash_add(dp, dirp, ulr->ulr_offset);
1.12      christos  832:                        ulfsdirhash_checkblock(dp, (char *)bp->b_data + lfs_blkoff,
1.1       dholland  833:                            ulr->ulr_offset);
                    834:                }
                    835: #endif
                    836:                error = VOP_BWRITE(bp->b_vp, bp);
                    837:                vfs_timestamp(&ts);
1.14      dholland  838:                ret = lfs_update(dvp, &ts, &ts, UPDATE_DIROP);
1.1       dholland  839:                if (error == 0)
                    840:                        return (ret);
                    841:                return (error);
                    842:        }
                    843:
                    844:        /*
                    845:         * If ulr_count is non-zero, then namei found space for the new
                    846:         * entry in the range ulr_offset to ulr_offset + ulr_count
                    847:         * in the directory. To use this space, we may have to compact
                    848:         * the entries located there, by copying them together towards the
                    849:         * beginning of the block, leaving the free space in one usable
                    850:         * chunk at the end.
                    851:         */
                    852:
                    853:        /*
                    854:         * Increase size of directory if entry eats into new space.
                    855:         * This should never push the size past a new multiple of
                    856:         * DIRBLKSIZ.
                    857:         *
                    858:         * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
                    859:         */
                    860:        if (ulr->ulr_offset + ulr->ulr_count > dp->i_size) {
                    861: #ifdef DIAGNOSTIC
1.4       dholland  862:                printf("ulfs_direnter: reached 4.2-only block, "
1.1       dholland  863:                       "not supposed to happen\n");
                    864: #endif
                    865:                dp->i_size = ulr->ulr_offset + ulr->ulr_count;
                    866:                DIP_ASSIGN(dp, size, dp->i_size);
                    867:                dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    868:        }
                    869:        /*
                    870:         * Get the block containing the space for the new directory entry.
                    871:         */
1.4       dholland  872:        error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp, true);
1.1       dholland  873:        if (error) {
                    874:                return (error);
                    875:        }
                    876:        /*
                    877:         * Find space for the new entry. In the simple case, the entry at
                    878:         * offset base will have the space. If it does not, then namei
                    879:         * arranged that compacting the region ulr_offset to
                    880:         * ulr_offset + ulr_count would yield the space.
                    881:         */
1.7       dholland  882:        ep = (struct lfs_direct *)dirbuf;
1.9       dholland  883:        dsize = (ep->d_ino != 0) ? LFS_DIRSIZ(FSFMT(dvp), ep, needswap) : 0;
1.4       dholland  884:        spacefree = ulfs_rw16(ep->d_reclen, needswap) - dsize;
                    885:        for (loc = ulfs_rw16(ep->d_reclen, needswap); loc < ulr->ulr_count; ) {
1.1       dholland  886:                uint16_t reclen;
                    887:
1.7       dholland  888:                nep = (struct lfs_direct *)(dirbuf + loc);
1.1       dholland  889:
                    890:                /* Trim the existing slot (NB: dsize may be zero). */
1.4       dholland  891:                ep->d_reclen = ulfs_rw16(dsize, needswap);
1.7       dholland  892:                ep = (struct lfs_direct *)((char *)ep + dsize);
1.1       dholland  893:
1.4       dholland  894:                reclen = ulfs_rw16(nep->d_reclen, needswap);
1.1       dholland  895:                loc += reclen;
                    896:                if (nep->d_ino == 0) {
                    897:                        /*
                    898:                         * A mid-block unused entry. Such entries are
                    899:                         * never created by the kernel, but fsck_ffs
                    900:                         * can create them (and it doesn't fix them).
                    901:                         *
                    902:                         * Add up the free space, and initialise the
                    903:                         * relocated entry since we don't memcpy it.
                    904:                         */
                    905:                        spacefree += reclen;
                    906:                        ep->d_ino = 0;
                    907:                        dsize = 0;
                    908:                        continue;
                    909:                }
1.9       dholland  910:                dsize = LFS_DIRSIZ(FSFMT(dvp), nep, needswap);
1.1       dholland  911:                spacefree += reclen - dsize;
1.3       dholland  912: #ifdef LFS_DIRHASH
1.1       dholland  913:                if (dp->i_dirhash != NULL)
1.4       dholland  914:                        ulfsdirhash_move(dp, nep,
1.1       dholland  915:                            ulr->ulr_offset + ((char *)nep - dirbuf),
                    916:                            ulr->ulr_offset + ((char *)ep - dirbuf));
                    917: #endif
                    918:                memcpy((void *)ep, (void *)nep, dsize);
                    919:        }
                    920:        /*
                    921:         * Here, `ep' points to a directory entry containing `dsize' in-use
                    922:         * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
                    923:         * then the entry is completely unused (dsize == 0). The value
                    924:         * of ep->d_reclen is always indeterminate.
                    925:         *
                    926:         * Update the pointer fields in the previous entry (if any),
                    927:         * copy in the new entry, and write out the block.
                    928:         */
                    929:        if (ep->d_ino == 0 ||
1.4       dholland  930:            (ulfs_rw32(ep->d_ino, needswap) == ULFS_WINO &&
1.1       dholland  931:             memcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
                    932:                if (spacefree + dsize < newentrysize)
1.4       dholland  933:                        panic("ulfs_direnter: compact1");
1.1       dholland  934:                dirp->d_reclen = spacefree + dsize;
                    935:        } else {
                    936:                if (spacefree < newentrysize)
1.4       dholland  937:                        panic("ulfs_direnter: compact2");
1.1       dholland  938:                dirp->d_reclen = spacefree;
1.4       dholland  939:                ep->d_reclen = ulfs_rw16(dsize, needswap);
1.7       dholland  940:                ep = (struct lfs_direct *)((char *)ep + dsize);
1.1       dholland  941:        }
1.4       dholland  942:        dirp->d_reclen = ulfs_rw16(dirp->d_reclen, needswap);
                    943:        dirp->d_ino = ulfs_rw32(dirp->d_ino, needswap);
1.1       dholland  944:        if (FSFMT(dvp)) {
                    945: #if (BYTE_ORDER == LITTLE_ENDIAN)
                    946:                if (needswap == 0) {
                    947: #else
                    948:                if (needswap != 0) {
                    949: #endif
                    950:                        u_char tmp = dirp->d_namlen;
                    951:                        dirp->d_namlen = dirp->d_type;
                    952:                        dirp->d_type = tmp;
                    953:                }
                    954:        }
1.3       dholland  955: #ifdef LFS_DIRHASH
1.1       dholland  956:        if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
                    957:            dirp->d_reclen == spacefree))
1.4       dholland  958:                ulfsdirhash_add(dp, dirp, ulr->ulr_offset + ((char *)ep - dirbuf));
1.1       dholland  959: #endif
                    960:        memcpy((void *)ep, (void *)dirp, (u_int)newentrysize);
1.3       dholland  961: #ifdef LFS_DIRHASH
1.1       dholland  962:        if (dp->i_dirhash != NULL)
1.4       dholland  963:                ulfsdirhash_checkblock(dp, dirbuf -
1.1       dholland  964:                    (ulr->ulr_offset & (dirblksiz - 1)),
                    965:                    ulr->ulr_offset & ~(dirblksiz - 1));
                    966: #endif
                    967:        error = VOP_BWRITE(bp->b_vp, bp);
                    968:        dp->i_flag |= IN_CHANGE | IN_UPDATE;
                    969:        /*
                    970:         * If all went well, and the directory can be shortened, proceed
                    971:         * with the truncation. Note that we have to unlock the inode for
                    972:         * the entry that we just entered, as the truncation may need to
                    973:         * lock other inodes which can lead to deadlock if we also hold a
                    974:         * lock on the newly entered node.
                    975:         */
                    976:        if (error == 0 && ulr->ulr_endoff && ulr->ulr_endoff < dp->i_size) {
1.3       dholland  977: #ifdef LFS_DIRHASH
1.1       dholland  978:                if (dp->i_dirhash != NULL)
1.4       dholland  979:                        ulfsdirhash_dirtrunc(dp, ulr->ulr_endoff);
1.1       dholland  980: #endif
1.14      dholland  981:                (void) lfs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC, cr);
1.1       dholland  982:        }
                    983:        return (error);
                    984: }
                    985:
                    986: /*
                    987:  * Remove a directory entry after a call to namei, using the
1.4       dholland  988:  * parameters that ulfs_lookup left in nameidata and in the
                    989:  * ulfs_lookup_results.
1.1       dholland  990:  *
                    991:  * DVP is the directory to be updated. It must be locked.
1.4       dholland  992:  * ULR is the ulfs_lookup_results structure from the final lookup step.
1.1       dholland  993:  * IP, if not null, is the inode being unlinked.
                    994:  * FLAGS may contain DOWHITEOUT.
                    995:  * ISRMDIR is not used and (XXX) should be removed.
                    996:  *
                    997:  * If FLAGS contains DOWHITEOUT the entry is replaced with a whiteout
                    998:  * instead of being cleared.
                    999:  *
                   1000:  * ulr->ulr_offset contains the position of the directory entry
                   1001:  * to be removed.
                   1002:  *
                   1003:  * ulr->ulr_reclen contains the size of the directory entry to be
                   1004:  * removed.
                   1005:  *
                   1006:  * ulr->ulr_count contains the size of the *previous* directory
                   1007:  * entry. This allows finding it, for free space management. If
                   1008:  * ulr_count is 0, the target entry is at the beginning of the
                   1009:  * directory. (Does this ever happen? The first entry should be ".",
                   1010:  * which should only be removed at rmdir time. Does rmdir come here
                   1011:  * to clear out the "." and ".." entries? Perhaps, but I doubt it.)
                   1012:  *
                   1013:  * The space is marked free by adding it to the record length (not
                   1014:  * name length) of the preceding entry. If the first entry becomes
                   1015:  * free, it is marked free by setting the inode number to 0.
                   1016:  *
                   1017:  * The link count of IP is decremented. Note that this is not the
1.4       dholland 1018:  * inverse behavior of ulfs_direnter, which does not adjust link
1.1       dholland 1019:  * counts. Sigh.
                   1020:  */
                   1021: int
1.4       dholland 1022: ulfs_dirremove(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
1.1       dholland 1023:              struct inode *ip, int flags, int isrmdir)
                   1024: {
                   1025:        struct inode *dp = VTOI(dvp);
1.7       dholland 1026:        struct lfs_direct *ep;
1.1       dholland 1027:        struct buf *bp;
                   1028:        int error;
1.15      dholland 1029:        const int needswap = ULFS_MPNEEDSWAP(dp->i_lfs);
1.1       dholland 1030:
                   1031:        if (flags & DOWHITEOUT) {
                   1032:                /*
1.4       dholland 1033:                 * Whiteout entry: set d_ino to ULFS_WINO.
1.1       dholland 1034:                 */
1.4       dholland 1035:                error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, (void *)&ep,
1.1       dholland 1036:                                     &bp, true);
                   1037:                if (error)
                   1038:                        return (error);
1.4       dholland 1039:                ep->d_ino = ulfs_rw32(ULFS_WINO, needswap);
1.7       dholland 1040:                ep->d_type = LFS_DT_WHT;
1.1       dholland 1041:                goto out;
                   1042:        }
                   1043:
1.4       dholland 1044:        if ((error = ulfs_blkatoff(dvp,
1.1       dholland 1045:            (off_t)(ulr->ulr_offset - ulr->ulr_count), (void *)&ep, &bp, true)) != 0)
                   1046:                return (error);
                   1047:
1.3       dholland 1048: #ifdef LFS_DIRHASH
1.1       dholland 1049:        /*
                   1050:         * Remove the dirhash entry. This is complicated by the fact
                   1051:         * that `ep' is the previous entry when ulr_count != 0.
                   1052:         */
                   1053:        if (dp->i_dirhash != NULL)
1.4       dholland 1054:                ulfsdirhash_remove(dp, (ulr->ulr_count == 0) ? ep :
1.7       dholland 1055:                   (struct lfs_direct *)((char *)ep +
1.4       dholland 1056:                   ulfs_rw16(ep->d_reclen, needswap)), ulr->ulr_offset);
1.1       dholland 1057: #endif
                   1058:
                   1059:        if (ulr->ulr_count == 0) {
                   1060:                /*
                   1061:                 * First entry in block: set d_ino to zero.
                   1062:                 */
                   1063:                ep->d_ino = 0;
                   1064:        } else {
                   1065:                /*
                   1066:                 * Collapse new free space into previous entry.
                   1067:                 */
                   1068:                ep->d_reclen =
1.4       dholland 1069:                    ulfs_rw16(ulfs_rw16(ep->d_reclen, needswap) + ulr->ulr_reclen,
1.1       dholland 1070:                        needswap);
                   1071:        }
                   1072:
1.3       dholland 1073: #ifdef LFS_DIRHASH
1.1       dholland 1074:        if (dp->i_dirhash != NULL) {
1.15      dholland 1075:                int dirblksiz = ip->i_lfs->um_dirblksiz;
1.4       dholland 1076:                ulfsdirhash_checkblock(dp, (char *)ep -
1.1       dholland 1077:                    ((ulr->ulr_offset - ulr->ulr_count) & (dirblksiz - 1)),
                   1078:                    ulr->ulr_offset & ~(dirblksiz - 1));
                   1079:        }
                   1080: #endif
                   1081:
                   1082: out:
                   1083:        if (ip) {
                   1084:                ip->i_nlink--;
                   1085:                DIP_ASSIGN(ip, nlink, ip->i_nlink);
                   1086:                ip->i_flag |= IN_CHANGE;
                   1087:        }
                   1088:        /*
                   1089:         * XXX did it ever occur to anyone that it might be a good
                   1090:         * idea to restore ip->i_nlink if this fails? Or something?
                   1091:         * Currently on error return from this function the state of
                   1092:         * ip->i_nlink depends on what happened, and callers
                   1093:         * definitely do not take this into account.
                   1094:         */
                   1095:        error = VOP_BWRITE(bp->b_vp, bp);
                   1096:        dp->i_flag |= IN_CHANGE | IN_UPDATE;
                   1097:        /*
                   1098:         * If the last named reference to a snapshot goes away,
                   1099:         * drop its snapshot reference so that it will be reclaimed
                   1100:         * when last open reference goes away.
                   1101:         */
                   1102:        if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 &&
                   1103:            ip->i_nlink == 0)
1.6       dholland 1104:                ulfs_snapgone(ip);
1.1       dholland 1105:        return (error);
                   1106: }
                   1107:
                   1108: /*
                   1109:  * Rewrite an existing directory entry to point at the inode supplied.
                   1110:  *
                   1111:  * DP is the directory to update.
                   1112:  * OFFSET is the position of the entry in question. It may come
1.4       dholland 1113:  * from ulr_offset of a ulfs_lookup_results.
1.1       dholland 1114:  * OIP is the old inode the directory previously pointed to.
                   1115:  * NEWINUM is the number of the new inode.
                   1116:  * NEWTYPE is the new value for the type field of the directory entry.
                   1117:  * (This is ignored if the fs doesn't support that.)
                   1118:  * ISRMDIR is not used and (XXX) should be removed.
                   1119:  * IFLAGS are added to DP's inode flags.
                   1120:  *
                   1121:  * The link count of OIP is decremented. Note that the link count of
                   1122:  * the new inode is *not* incremented. Yay for symmetry.
                   1123:  */
                   1124: int
1.4       dholland 1125: ulfs_dirrewrite(struct inode *dp, off_t offset,
1.1       dholland 1126:     struct inode *oip, ino_t newinum, int newtype,
                   1127:     int isrmdir, int iflags)
                   1128: {
                   1129:        struct buf *bp;
1.7       dholland 1130:        struct lfs_direct *ep;
1.1       dholland 1131:        struct vnode *vdp = ITOV(dp);
                   1132:        int error;
                   1133:
1.4       dholland 1134:        error = ulfs_blkatoff(vdp, offset, (void *)&ep, &bp, true);
1.1       dholland 1135:        if (error)
                   1136:                return (error);
1.15      dholland 1137:        ep->d_ino = ulfs_rw32(newinum, ULFS_IPNEEDSWAP(dp));
1.1       dholland 1138:        if (!FSFMT(vdp))
                   1139:                ep->d_type = newtype;
                   1140:        oip->i_nlink--;
                   1141:        DIP_ASSIGN(oip, nlink, oip->i_nlink);
                   1142:        oip->i_flag |= IN_CHANGE;
                   1143:        error = VOP_BWRITE(bp->b_vp, bp);
                   1144:        dp->i_flag |= iflags;
                   1145:        /*
                   1146:         * If the last named reference to a snapshot goes away,
                   1147:         * drop its snapshot reference so that it will be reclaimed
                   1148:         * when last open reference goes away.
                   1149:         */
                   1150:        if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_nlink == 0)
1.6       dholland 1151:                ulfs_snapgone(oip);
1.1       dholland 1152:        return (error);
                   1153: }
                   1154:
                   1155: /*
                   1156:  * Check if a directory is empty or not.
                   1157:  * Inode supplied must be locked.
                   1158:  *
1.7       dholland 1159:  * Using a struct lfs_dirtemplate here is not precisely
                   1160:  * what we want, but better than using a struct lfs_direct.
1.1       dholland 1161:  *
                   1162:  * NB: does not handle corrupted directories.
                   1163:  */
                   1164: int
1.4       dholland 1165: ulfs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
1.1       dholland 1166: {
                   1167:        doff_t off;
1.7       dholland 1168:        struct lfs_dirtemplate dbuf;
                   1169:        struct lfs_direct *dp = (struct lfs_direct *)&dbuf;
1.1       dholland 1170:        int error, namlen;
                   1171:        size_t count;
1.4       dholland 1172:        const int needswap = ULFS_IPNEEDSWAP(ip);
1.7       dholland 1173: #define        MINDIRSIZ (sizeof (struct lfs_dirtemplate) / 2)
1.1       dholland 1174:
                   1175:        for (off = 0; off < ip->i_size;
1.4       dholland 1176:            off += ulfs_rw16(dp->d_reclen, needswap)) {
1.22      riastrad 1177:                error = ulfs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
                   1178:                    off, IO_NODELOCKED, cred, &count, NULL);
1.1       dholland 1179:                /*
                   1180:                 * Since we read MINDIRSIZ, residual must
                   1181:                 * be 0 unless we're at end of file.
                   1182:                 */
                   1183:                if (error || count != 0)
                   1184:                        return (0);
                   1185:                /* avoid infinite loops */
                   1186:                if (dp->d_reclen == 0)
                   1187:                        return (0);
                   1188:                /* skip empty entries */
1.4       dholland 1189:                if (dp->d_ino == 0 || ulfs_rw32(dp->d_ino, needswap) == ULFS_WINO)
1.1       dholland 1190:                        continue;
                   1191:                /* accept only "." and ".." */
                   1192: #if (BYTE_ORDER == LITTLE_ENDIAN)
                   1193:                if (FSFMT(ITOV(ip)) && needswap == 0)
                   1194:                        namlen = dp->d_type;
                   1195:                else
                   1196:                        namlen = dp->d_namlen;
                   1197: #else
                   1198:                if (FSFMT(ITOV(ip)) && needswap != 0)
                   1199:                        namlen = dp->d_type;
                   1200:                else
                   1201:                        namlen = dp->d_namlen;
                   1202: #endif
                   1203:                if (namlen > 2)
                   1204:                        return (0);
                   1205:                if (dp->d_name[0] != '.')
                   1206:                        return (0);
                   1207:                /*
                   1208:                 * At this point namlen must be 1 or 2.
                   1209:                 * 1 implies ".", 2 implies ".." if second
                   1210:                 * char is also "."
                   1211:                 */
                   1212:                if (namlen == 1 &&
1.4       dholland 1213:                    ulfs_rw32(dp->d_ino, needswap) == ip->i_number)
1.1       dholland 1214:                        continue;
                   1215:                if (dp->d_name[1] == '.' &&
1.4       dholland 1216:                    ulfs_rw32(dp->d_ino, needswap) == parentino)
1.1       dholland 1217:                        continue;
                   1218:                return (0);
                   1219:        }
                   1220:        return (1);
                   1221: }
                   1222:
1.4       dholland 1223: #define        ULFS_DIRRABLKS 0
                   1224: int ulfs_dirrablks = ULFS_DIRRABLKS;
1.1       dholland 1225:
                   1226: /*
1.4       dholland 1227:  * ulfs_blkatoff: Return buffer with the contents of block "offset" from
1.1       dholland 1228:  * the beginning of directory "vp".  If "res" is non-NULL, fill it in with
                   1229:  * a pointer to the remaining space in the directory.  If the caller intends
                   1230:  * to modify the buffer returned, "modify" must be true.
                   1231:  */
                   1232:
                   1233: int
1.4       dholland 1234: ulfs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp,
1.1       dholland 1235:     bool modify)
                   1236: {
1.17      martin   1237:        struct inode *ip __diagused;
1.1       dholland 1238:        struct buf *bp;
                   1239:        daddr_t lbn;
1.4       dholland 1240:        const int dirrablks = ulfs_dirrablks;
1.1       dholland 1241:        daddr_t *blks;
                   1242:        int *blksizes;
                   1243:        int run, error;
                   1244:        struct mount *mp = vp->v_mount;
                   1245:        const int bshift = mp->mnt_fs_bshift;
                   1246:        const int bsize = 1 << bshift;
                   1247:        off_t eof;
                   1248:
                   1249:        blks = kmem_alloc((1 + dirrablks) * sizeof(daddr_t), KM_SLEEP);
                   1250:        blksizes = kmem_alloc((1 + dirrablks) * sizeof(int), KM_SLEEP);
                   1251:        ip = VTOI(vp);
                   1252:        KASSERT(vp->v_size == ip->i_size);
                   1253:        GOP_SIZE(vp, vp->v_size, &eof, 0);
                   1254:        lbn = offset >> bshift;
                   1255:
                   1256:        for (run = 0; run <= dirrablks;) {
                   1257:                const off_t curoff = lbn << bshift;
                   1258:                const int size = MIN(eof - curoff, bsize);
                   1259:
                   1260:                if (size == 0) {
                   1261:                        break;
                   1262:                }
                   1263:                KASSERT(curoff < eof);
                   1264:                blks[run] = lbn;
                   1265:                blksizes[run] = size;
                   1266:                lbn++;
                   1267:                run++;
                   1268:                if (size != bsize) {
                   1269:                        break;
                   1270:                }
                   1271:        }
                   1272:        KASSERT(run >= 1);
                   1273:        error = breadn(vp, blks[0], blksizes[0], &blks[1], &blksizes[1],
1.23      maxv     1274:            run - 1, (modify ? B_MODIFY : 0), &bp);
1.1       dholland 1275:        if (error != 0) {
                   1276:                *bpp = NULL;
                   1277:                goto out;
                   1278:        }
                   1279:        if (res) {
                   1280:                *res = (char *)bp->b_data + (offset & (bsize - 1));
                   1281:        }
                   1282:        *bpp = bp;
                   1283:
                   1284:  out:
                   1285:        kmem_free(blks, (1 + dirrablks) * sizeof(daddr_t));
                   1286:        kmem_free(blksizes, (1 + dirrablks) * sizeof(int));
                   1287:        return error;
                   1288: }

CVSweb <webmaster@jp.NetBSD.org>