1*0a6a1f1dSLionel Sambuc /* $NetBSD: ulfs_lookup.c,v 1.34 2015/09/21 01:24:23 dholland Exp $ */
284d9c625SLionel Sambuc /* from NetBSD: ufs_lookup.c,v 1.122 2013/01/22 09:39:18 dholland Exp */
384d9c625SLionel Sambuc
484d9c625SLionel Sambuc /*
584d9c625SLionel Sambuc * Copyright (c) 1989, 1993
684d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
784d9c625SLionel Sambuc * (c) UNIX System Laboratories, Inc.
884d9c625SLionel Sambuc * All or some portions of this file are derived from material licensed
984d9c625SLionel Sambuc * to the University of California by American Telephone and Telegraph
1084d9c625SLionel Sambuc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1184d9c625SLionel Sambuc * the permission of UNIX System Laboratories, Inc.
1284d9c625SLionel Sambuc *
1384d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1484d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1584d9c625SLionel Sambuc * are met:
1684d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1784d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1884d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1984d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
2084d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
2184d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
2284d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
2384d9c625SLionel Sambuc * without specific prior written permission.
2484d9c625SLionel Sambuc *
2584d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2684d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2784d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2884d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2984d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3084d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3184d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3284d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3384d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3484d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3584d9c625SLionel Sambuc * SUCH DAMAGE.
3684d9c625SLionel Sambuc *
3784d9c625SLionel Sambuc * @(#)ufs_lookup.c 8.9 (Berkeley) 8/11/94
3884d9c625SLionel Sambuc */
3984d9c625SLionel Sambuc
4084d9c625SLionel Sambuc #include <sys/cdefs.h>
41*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ulfs_lookup.c,v 1.34 2015/09/21 01:24:23 dholland Exp $");
4284d9c625SLionel Sambuc
4384d9c625SLionel Sambuc #ifdef _KERNEL_OPT
4484d9c625SLionel Sambuc #include "opt_lfs.h"
4584d9c625SLionel Sambuc #endif
4684d9c625SLionel Sambuc
4784d9c625SLionel Sambuc #include <sys/param.h>
4884d9c625SLionel Sambuc #include <sys/systm.h>
4984d9c625SLionel Sambuc #include <sys/namei.h>
5084d9c625SLionel Sambuc #include <sys/buf.h>
5184d9c625SLionel Sambuc #include <sys/file.h>
5284d9c625SLionel Sambuc #include <sys/stat.h>
5384d9c625SLionel Sambuc #include <sys/mount.h>
5484d9c625SLionel Sambuc #include <sys/vnode.h>
5584d9c625SLionel Sambuc #include <sys/kernel.h>
5684d9c625SLionel Sambuc #include <sys/kauth.h>
5784d9c625SLionel Sambuc #include <sys/wapbl.h>
5884d9c625SLionel Sambuc #include <sys/fstrans.h>
5984d9c625SLionel Sambuc #include <sys/proc.h>
6084d9c625SLionel Sambuc #include <sys/kmem.h>
6184d9c625SLionel Sambuc
62*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs.h>
63*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs_accessors.h>
6484d9c625SLionel Sambuc #include <ufs/lfs/lfs_extern.h>
6584d9c625SLionel Sambuc
6684d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
6784d9c625SLionel Sambuc #ifdef LFS_DIRHASH
6884d9c625SLionel Sambuc #include <ufs/lfs/ulfs_dirhash.h>
6984d9c625SLionel Sambuc #endif
7084d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
7184d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
7284d9c625SLionel Sambuc #include <ufs/lfs/ulfs_bswap.h>
7384d9c625SLionel Sambuc
7484d9c625SLionel Sambuc #include <miscfs/genfs/genfs.h>
7584d9c625SLionel Sambuc
7684d9c625SLionel Sambuc #ifdef DIAGNOSTIC
7784d9c625SLionel Sambuc int lfs_dirchk = 1;
7884d9c625SLionel Sambuc #else
7984d9c625SLionel Sambuc int lfs_dirchk = 0;
8084d9c625SLionel Sambuc #endif
8184d9c625SLionel Sambuc
8284d9c625SLionel Sambuc /*
8384d9c625SLionel Sambuc * Convert a component of a pathname into a pointer to a locked inode.
8484d9c625SLionel Sambuc * This is a very central and rather complicated routine.
8584d9c625SLionel Sambuc * If the file system is not maintained in a strict tree hierarchy,
8684d9c625SLionel Sambuc * this can result in a deadlock situation (see comments in code below).
8784d9c625SLionel Sambuc *
8884d9c625SLionel Sambuc * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
8984d9c625SLionel Sambuc * on whether the name is to be looked up, created, renamed, or deleted.
9084d9c625SLionel Sambuc * When CREATE, RENAME, or DELETE is specified, information usable in
9184d9c625SLionel Sambuc * creating, renaming, or deleting a directory entry may be calculated.
9284d9c625SLionel Sambuc * If flag has LOCKPARENT or'ed into it and the target of the pathname
9384d9c625SLionel Sambuc * exists, lookup returns both the target and its parent directory locked.
9484d9c625SLionel Sambuc * When creating or renaming and LOCKPARENT is specified, the target may
9584d9c625SLionel Sambuc * not be ".". When deleting and LOCKPARENT is specified, the target may
9684d9c625SLionel Sambuc * be "."., but the caller must check to ensure it does an vrele and vput
9784d9c625SLionel Sambuc * instead of two vputs.
9884d9c625SLionel Sambuc *
9984d9c625SLionel Sambuc * Overall outline of ulfs_lookup:
10084d9c625SLionel Sambuc *
10184d9c625SLionel Sambuc * check accessibility of directory
10284d9c625SLionel Sambuc * look for name in cache, if found, then if at end of path
10384d9c625SLionel Sambuc * and deleting or creating, drop it, else return name
10484d9c625SLionel Sambuc * search for name in directory, to found or notfound
10584d9c625SLionel Sambuc * notfound:
10684d9c625SLionel Sambuc * if creating, return locked directory, leaving info on available slots
10784d9c625SLionel Sambuc * else return error
10884d9c625SLionel Sambuc * found:
10984d9c625SLionel Sambuc * if at end of path and deleting, return information to allow delete
11084d9c625SLionel Sambuc * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
11184d9c625SLionel Sambuc * inode and return info to allow rewrite
11284d9c625SLionel Sambuc * if not at end, add name to cache; if at end and neither creating
11384d9c625SLionel Sambuc * nor deleting, add name to cache
11484d9c625SLionel Sambuc */
11584d9c625SLionel Sambuc int
ulfs_lookup(void * v)11684d9c625SLionel Sambuc ulfs_lookup(void *v)
11784d9c625SLionel Sambuc {
118*0a6a1f1dSLionel Sambuc struct vop_lookup_v2_args /* {
11984d9c625SLionel Sambuc struct vnode *a_dvp;
12084d9c625SLionel Sambuc struct vnode **a_vpp;
12184d9c625SLionel Sambuc struct componentname *a_cnp;
12284d9c625SLionel Sambuc } */ *ap = v;
12384d9c625SLionel Sambuc struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
12484d9c625SLionel Sambuc struct inode *dp = VTOI(vdp); /* inode for directory being searched */
12584d9c625SLionel Sambuc struct buf *bp; /* a buffer of directory entries */
126*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *ep; /* the current directory entry */
12784d9c625SLionel Sambuc int entryoffsetinblock; /* offset of ep in bp's buffer */
12884d9c625SLionel Sambuc enum {
12984d9c625SLionel Sambuc NONE, /* need to search a slot for our new entry */
13084d9c625SLionel Sambuc COMPACT, /* a compaction can make a slot in the current
13184d9c625SLionel Sambuc DIRBLKSIZ block */
13284d9c625SLionel Sambuc FOUND, /* found a slot (or no need to search) */
13384d9c625SLionel Sambuc } slotstatus;
13484d9c625SLionel Sambuc doff_t slotoffset; /* offset of area with free space.
13584d9c625SLionel Sambuc a special value -1 for invalid */
13684d9c625SLionel Sambuc int slotsize; /* size of area at slotoffset */
13784d9c625SLionel Sambuc int slotfreespace; /* accumulated amount of space free in
13884d9c625SLionel Sambuc the current DIRBLKSIZ block */
13984d9c625SLionel Sambuc int slotneeded; /* size of the entry we're seeking */
14084d9c625SLionel Sambuc int numdirpasses; /* strategy for directory search */
14184d9c625SLionel Sambuc doff_t endsearch; /* offset to end directory search */
14284d9c625SLionel Sambuc doff_t prevoff; /* previous value of ulr_offset */
143*0a6a1f1dSLionel Sambuc struct vnode *tdp; /* returned by vcache_get */
14484d9c625SLionel Sambuc doff_t enduseful; /* pointer past last used dir slot.
14584d9c625SLionel Sambuc used for directory truncation. */
14684d9c625SLionel Sambuc u_long bmask; /* block offset mask */
14784d9c625SLionel Sambuc int error;
14884d9c625SLionel Sambuc struct vnode **vpp = ap->a_vpp;
14984d9c625SLionel Sambuc struct componentname *cnp = ap->a_cnp;
15084d9c625SLionel Sambuc kauth_cred_t cred = cnp->cn_cred;
15184d9c625SLionel Sambuc int flags;
15284d9c625SLionel Sambuc int nameiop = cnp->cn_nameiop;
15384d9c625SLionel Sambuc struct lfs *fs = dp->i_lfs;
15484d9c625SLionel Sambuc int dirblksiz = fs->um_dirblksiz;
15584d9c625SLionel Sambuc ino_t foundino;
15684d9c625SLionel Sambuc struct ulfs_lookup_results *results;
15784d9c625SLionel Sambuc int iswhiteout; /* temp result from cache_lookup() */
15884d9c625SLionel Sambuc
15984d9c625SLionel Sambuc flags = cnp->cn_flags;
16084d9c625SLionel Sambuc
16184d9c625SLionel Sambuc bp = NULL;
16284d9c625SLionel Sambuc slotoffset = -1;
16384d9c625SLionel Sambuc *vpp = NULL;
16484d9c625SLionel Sambuc endsearch = 0; /* silence compiler warning */
16584d9c625SLionel Sambuc
16684d9c625SLionel Sambuc /*
16784d9c625SLionel Sambuc * Produce the auxiliary lookup results into i_crap. Increment
16884d9c625SLionel Sambuc * its serial number so elsewhere we can tell if we're using
16984d9c625SLionel Sambuc * stale results. This should not be done this way. XXX.
17084d9c625SLionel Sambuc */
17184d9c625SLionel Sambuc results = &dp->i_crap;
17284d9c625SLionel Sambuc dp->i_crapcounter++;
17384d9c625SLionel Sambuc
17484d9c625SLionel Sambuc /*
17584d9c625SLionel Sambuc * Check accessiblity of directory.
17684d9c625SLionel Sambuc */
17784d9c625SLionel Sambuc if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
17884d9c625SLionel Sambuc return (error);
17984d9c625SLionel Sambuc
18084d9c625SLionel Sambuc if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
18184d9c625SLionel Sambuc (nameiop == DELETE || nameiop == RENAME))
18284d9c625SLionel Sambuc return (EROFS);
18384d9c625SLionel Sambuc
18484d9c625SLionel Sambuc /*
18584d9c625SLionel Sambuc * We now have a segment name to search for, and a directory to search.
18684d9c625SLionel Sambuc *
18784d9c625SLionel Sambuc * Before tediously performing a linear scan of the directory,
18884d9c625SLionel Sambuc * check the name cache to see if the directory/name pair
18984d9c625SLionel Sambuc * we are looking for is known already.
19084d9c625SLionel Sambuc */
19184d9c625SLionel Sambuc if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
19284d9c625SLionel Sambuc cnp->cn_nameiop, cnp->cn_flags, &iswhiteout, vpp)) {
19384d9c625SLionel Sambuc if (iswhiteout) {
19484d9c625SLionel Sambuc cnp->cn_flags |= ISWHITEOUT;
19584d9c625SLionel Sambuc }
19684d9c625SLionel Sambuc return *vpp == NULLVP ? ENOENT : 0;
19784d9c625SLionel Sambuc }
19884d9c625SLionel Sambuc if (iswhiteout) {
19984d9c625SLionel Sambuc /*
20084d9c625SLionel Sambuc * The namecache set iswhiteout without finding a
20184d9c625SLionel Sambuc * cache entry. As of this writing (20121014), this
20284d9c625SLionel Sambuc * can happen if there was a whiteout entry that has
20384d9c625SLionel Sambuc * been invalidated by the lookup. It is not clear if
20484d9c625SLionel Sambuc * it is correct to set ISWHITEOUT in this case or
20584d9c625SLionel Sambuc * not; however, doing so retains the prior behavior,
20684d9c625SLionel Sambuc * so we'll go with that until some clearer answer
20784d9c625SLionel Sambuc * appears. XXX
20884d9c625SLionel Sambuc */
20984d9c625SLionel Sambuc cnp->cn_flags |= ISWHITEOUT;
21084d9c625SLionel Sambuc }
21184d9c625SLionel Sambuc
21284d9c625SLionel Sambuc fstrans_start(vdp->v_mount, FSTRANS_SHARED);
21384d9c625SLionel Sambuc
21484d9c625SLionel Sambuc /*
21584d9c625SLionel Sambuc * Suppress search for slots unless creating
21684d9c625SLionel Sambuc * file and at end of pathname, in which case
21784d9c625SLionel Sambuc * we watch for a place to put the new file in
21884d9c625SLionel Sambuc * case it doesn't already exist.
21984d9c625SLionel Sambuc */
22084d9c625SLionel Sambuc slotstatus = FOUND;
22184d9c625SLionel Sambuc slotfreespace = slotsize = slotneeded = 0;
22284d9c625SLionel Sambuc if ((nameiop == CREATE || nameiop == RENAME) && (flags & ISLASTCN)) {
22384d9c625SLionel Sambuc slotstatus = NONE;
224*0a6a1f1dSLionel Sambuc slotneeded = LFS_DIRECTSIZ(fs, cnp->cn_namelen);
22584d9c625SLionel Sambuc }
22684d9c625SLionel Sambuc
22784d9c625SLionel Sambuc /*
22884d9c625SLionel Sambuc * If there is cached information on a previous search of
22984d9c625SLionel Sambuc * this directory, pick up where we last left off.
23084d9c625SLionel Sambuc * We cache only lookups as these are the most common
23184d9c625SLionel Sambuc * and have the greatest payoff. Caching CREATE has little
23284d9c625SLionel Sambuc * benefit as it usually must search the entire directory
23384d9c625SLionel Sambuc * to determine that the entry does not exist. Caching the
23484d9c625SLionel Sambuc * location of the last DELETE or RENAME has not reduced
23584d9c625SLionel Sambuc * profiling time and hence has been removed in the interest
23684d9c625SLionel Sambuc * of simplicity.
23784d9c625SLionel Sambuc */
23884d9c625SLionel Sambuc bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
23984d9c625SLionel Sambuc
24084d9c625SLionel Sambuc #ifdef LFS_DIRHASH
24184d9c625SLionel Sambuc /*
24284d9c625SLionel Sambuc * Use dirhash for fast operations on large directories. The logic
24384d9c625SLionel Sambuc * to determine whether to hash the directory is contained within
24484d9c625SLionel Sambuc * ulfsdirhash_build(); a zero return means that it decided to hash
24584d9c625SLionel Sambuc * this directory and it successfully built up the hash table.
24684d9c625SLionel Sambuc */
24784d9c625SLionel Sambuc if (ulfsdirhash_build(dp) == 0) {
24884d9c625SLionel Sambuc /* Look for a free slot if needed. */
24984d9c625SLionel Sambuc enduseful = dp->i_size;
25084d9c625SLionel Sambuc if (slotstatus != FOUND) {
25184d9c625SLionel Sambuc slotoffset = ulfsdirhash_findfree(dp, slotneeded,
25284d9c625SLionel Sambuc &slotsize);
25384d9c625SLionel Sambuc if (slotoffset >= 0) {
25484d9c625SLionel Sambuc slotstatus = COMPACT;
25584d9c625SLionel Sambuc enduseful = ulfsdirhash_enduseful(dp);
25684d9c625SLionel Sambuc if (enduseful < 0)
25784d9c625SLionel Sambuc enduseful = dp->i_size;
25884d9c625SLionel Sambuc }
25984d9c625SLionel Sambuc }
26084d9c625SLionel Sambuc /* Look up the component. */
26184d9c625SLionel Sambuc numdirpasses = 1;
26284d9c625SLionel Sambuc entryoffsetinblock = 0; /* silence compiler warning */
26384d9c625SLionel Sambuc switch (ulfsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
26484d9c625SLionel Sambuc &results->ulr_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
26584d9c625SLionel Sambuc case 0:
266*0a6a1f1dSLionel Sambuc ep = (LFS_DIRHEADER *)((char *)bp->b_data +
26784d9c625SLionel Sambuc (results->ulr_offset & bmask));
26884d9c625SLionel Sambuc goto foundentry;
26984d9c625SLionel Sambuc case ENOENT:
27084d9c625SLionel Sambuc results->ulr_offset = roundup(dp->i_size, dirblksiz);
27184d9c625SLionel Sambuc goto notfound;
27284d9c625SLionel Sambuc default:
27384d9c625SLionel Sambuc /* Something failed; just do a linear search. */
27484d9c625SLionel Sambuc break;
27584d9c625SLionel Sambuc }
27684d9c625SLionel Sambuc }
27784d9c625SLionel Sambuc #endif /* LFS_DIRHASH */
27884d9c625SLionel Sambuc
27984d9c625SLionel Sambuc if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
28084d9c625SLionel Sambuc results->ulr_diroff >= dp->i_size) {
28184d9c625SLionel Sambuc entryoffsetinblock = 0;
28284d9c625SLionel Sambuc results->ulr_offset = 0;
28384d9c625SLionel Sambuc numdirpasses = 1;
28484d9c625SLionel Sambuc } else {
28584d9c625SLionel Sambuc results->ulr_offset = results->ulr_diroff;
28684d9c625SLionel Sambuc if ((entryoffsetinblock = results->ulr_offset & bmask) &&
28784d9c625SLionel Sambuc (error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
28884d9c625SLionel Sambuc NULL, &bp, false)))
28984d9c625SLionel Sambuc goto out;
29084d9c625SLionel Sambuc numdirpasses = 2;
291*0a6a1f1dSLionel Sambuc namecache_count_2passes();
29284d9c625SLionel Sambuc }
29384d9c625SLionel Sambuc prevoff = results->ulr_offset;
29484d9c625SLionel Sambuc endsearch = roundup(dp->i_size, dirblksiz);
29584d9c625SLionel Sambuc enduseful = 0;
29684d9c625SLionel Sambuc
29784d9c625SLionel Sambuc searchloop:
29884d9c625SLionel Sambuc while (results->ulr_offset < endsearch) {
29984d9c625SLionel Sambuc if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
30084d9c625SLionel Sambuc preempt();
30184d9c625SLionel Sambuc /*
30284d9c625SLionel Sambuc * If necessary, get the next directory block.
30384d9c625SLionel Sambuc */
30484d9c625SLionel Sambuc if ((results->ulr_offset & bmask) == 0) {
30584d9c625SLionel Sambuc if (bp != NULL)
30684d9c625SLionel Sambuc brelse(bp, 0);
30784d9c625SLionel Sambuc error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
30884d9c625SLionel Sambuc NULL, &bp, false);
30984d9c625SLionel Sambuc if (error)
31084d9c625SLionel Sambuc goto out;
31184d9c625SLionel Sambuc entryoffsetinblock = 0;
31284d9c625SLionel Sambuc }
31384d9c625SLionel Sambuc /*
31484d9c625SLionel Sambuc * If still looking for a slot, and at a DIRBLKSIZ
31584d9c625SLionel Sambuc * boundary, have to start looking for free space again.
31684d9c625SLionel Sambuc */
31784d9c625SLionel Sambuc if (slotstatus == NONE &&
31884d9c625SLionel Sambuc (entryoffsetinblock & (dirblksiz - 1)) == 0) {
31984d9c625SLionel Sambuc slotoffset = -1;
32084d9c625SLionel Sambuc slotfreespace = 0;
32184d9c625SLionel Sambuc }
32284d9c625SLionel Sambuc /*
32384d9c625SLionel Sambuc * Get pointer to next entry.
32484d9c625SLionel Sambuc * Full validation checks are slow, so we only check
32584d9c625SLionel Sambuc * enough to insure forward progress through the
32684d9c625SLionel Sambuc * directory. Complete checks can be run by patching
32784d9c625SLionel Sambuc * "lfs_dirchk" to be true.
32884d9c625SLionel Sambuc */
32984d9c625SLionel Sambuc KASSERT(bp != NULL);
330*0a6a1f1dSLionel Sambuc ep = (LFS_DIRHEADER *)((char *)bp->b_data + entryoffsetinblock);
331*0a6a1f1dSLionel Sambuc if (lfs_dir_getreclen(fs, ep) == 0 ||
33284d9c625SLionel Sambuc (lfs_dirchk && ulfs_dirbadentry(vdp, ep, entryoffsetinblock))) {
33384d9c625SLionel Sambuc int i;
33484d9c625SLionel Sambuc
33584d9c625SLionel Sambuc ulfs_dirbad(dp, results->ulr_offset, "mangled entry");
33684d9c625SLionel Sambuc i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
33784d9c625SLionel Sambuc results->ulr_offset += i;
33884d9c625SLionel Sambuc entryoffsetinblock += i;
33984d9c625SLionel Sambuc continue;
34084d9c625SLionel Sambuc }
34184d9c625SLionel Sambuc
34284d9c625SLionel Sambuc /*
34384d9c625SLionel Sambuc * If an appropriate sized slot has not yet been found,
34484d9c625SLionel Sambuc * check to see if one is available. Also accumulate space
34584d9c625SLionel Sambuc * in the current block so that we can determine if
34684d9c625SLionel Sambuc * compaction is viable.
34784d9c625SLionel Sambuc */
34884d9c625SLionel Sambuc if (slotstatus != FOUND) {
349*0a6a1f1dSLionel Sambuc int size = lfs_dir_getreclen(fs, ep);
35084d9c625SLionel Sambuc
351*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep) != 0)
352*0a6a1f1dSLionel Sambuc size -= LFS_DIRSIZ(fs, ep);
35384d9c625SLionel Sambuc if (size > 0) {
35484d9c625SLionel Sambuc if (size >= slotneeded) {
35584d9c625SLionel Sambuc slotstatus = FOUND;
35684d9c625SLionel Sambuc slotoffset = results->ulr_offset;
357*0a6a1f1dSLionel Sambuc slotsize = lfs_dir_getreclen(fs, ep);
35884d9c625SLionel Sambuc } else if (slotstatus == NONE) {
35984d9c625SLionel Sambuc slotfreespace += size;
36084d9c625SLionel Sambuc if (slotoffset == -1)
36184d9c625SLionel Sambuc slotoffset = results->ulr_offset;
36284d9c625SLionel Sambuc if (slotfreespace >= slotneeded) {
36384d9c625SLionel Sambuc slotstatus = COMPACT;
36484d9c625SLionel Sambuc slotsize = results->ulr_offset +
365*0a6a1f1dSLionel Sambuc lfs_dir_getreclen(fs, ep) -
36684d9c625SLionel Sambuc slotoffset;
36784d9c625SLionel Sambuc }
36884d9c625SLionel Sambuc }
36984d9c625SLionel Sambuc }
37084d9c625SLionel Sambuc }
37184d9c625SLionel Sambuc
37284d9c625SLionel Sambuc /*
37384d9c625SLionel Sambuc * Check for a name match.
37484d9c625SLionel Sambuc */
375*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep)) {
37684d9c625SLionel Sambuc int namlen;
37784d9c625SLionel Sambuc
378*0a6a1f1dSLionel Sambuc namlen = lfs_dir_getnamlen(fs, ep);
37984d9c625SLionel Sambuc if (namlen == cnp->cn_namelen &&
380*0a6a1f1dSLionel Sambuc !memcmp(cnp->cn_nameptr, lfs_dir_nameptr(fs, ep),
38184d9c625SLionel Sambuc (unsigned)namlen)) {
38284d9c625SLionel Sambuc #ifdef LFS_DIRHASH
38384d9c625SLionel Sambuc foundentry:
38484d9c625SLionel Sambuc #endif
38584d9c625SLionel Sambuc /*
38684d9c625SLionel Sambuc * Save directory entry's inode number and
38784d9c625SLionel Sambuc * reclen, and release directory buffer.
38884d9c625SLionel Sambuc */
389*0a6a1f1dSLionel Sambuc if (!FSFMT(vdp) && lfs_dir_gettype(fs, ep) == LFS_DT_WHT) {
39084d9c625SLionel Sambuc slotstatus = FOUND;
39184d9c625SLionel Sambuc slotoffset = results->ulr_offset;
392*0a6a1f1dSLionel Sambuc slotsize = lfs_dir_getreclen(fs, ep);
39384d9c625SLionel Sambuc results->ulr_reclen = slotsize;
39484d9c625SLionel Sambuc /*
39584d9c625SLionel Sambuc * This is used to set
39684d9c625SLionel Sambuc * results->ulr_endoff,
39784d9c625SLionel Sambuc * which may be used by ulfs_direnter()
39884d9c625SLionel Sambuc * as a length to truncate the
39984d9c625SLionel Sambuc * directory to. Therefore, it must
40084d9c625SLionel Sambuc * point past the end of the last
40184d9c625SLionel Sambuc * non-empty directory entry. We don't
40284d9c625SLionel Sambuc * know where that is in this case, so
40384d9c625SLionel Sambuc * we effectively disable shrinking by
40484d9c625SLionel Sambuc * using the existing size of the
40584d9c625SLionel Sambuc * directory.
40684d9c625SLionel Sambuc *
40784d9c625SLionel Sambuc * Note that we wouldn't expect to
40884d9c625SLionel Sambuc * shrink the directory while rewriting
40984d9c625SLionel Sambuc * an existing entry anyway.
41084d9c625SLionel Sambuc */
41184d9c625SLionel Sambuc enduseful = endsearch;
41284d9c625SLionel Sambuc cnp->cn_flags |= ISWHITEOUT;
41384d9c625SLionel Sambuc numdirpasses--;
41484d9c625SLionel Sambuc goto notfound;
41584d9c625SLionel Sambuc }
416*0a6a1f1dSLionel Sambuc foundino = lfs_dir_getino(fs, ep);
417*0a6a1f1dSLionel Sambuc results->ulr_reclen = lfs_dir_getreclen(fs, ep);
41884d9c625SLionel Sambuc goto found;
41984d9c625SLionel Sambuc }
42084d9c625SLionel Sambuc }
42184d9c625SLionel Sambuc prevoff = results->ulr_offset;
422*0a6a1f1dSLionel Sambuc results->ulr_offset += lfs_dir_getreclen(fs, ep);
423*0a6a1f1dSLionel Sambuc entryoffsetinblock += lfs_dir_getreclen(fs, ep);
424*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep))
42584d9c625SLionel Sambuc enduseful = results->ulr_offset;
42684d9c625SLionel Sambuc }
42784d9c625SLionel Sambuc notfound:
42884d9c625SLionel Sambuc /*
42984d9c625SLionel Sambuc * If we started in the middle of the directory and failed
43084d9c625SLionel Sambuc * to find our target, we must check the beginning as well.
43184d9c625SLionel Sambuc */
43284d9c625SLionel Sambuc if (numdirpasses == 2) {
43384d9c625SLionel Sambuc numdirpasses--;
43484d9c625SLionel Sambuc results->ulr_offset = 0;
43584d9c625SLionel Sambuc endsearch = results->ulr_diroff;
43684d9c625SLionel Sambuc goto searchloop;
43784d9c625SLionel Sambuc }
43884d9c625SLionel Sambuc if (bp != NULL)
43984d9c625SLionel Sambuc brelse(bp, 0);
44084d9c625SLionel Sambuc /*
44184d9c625SLionel Sambuc * If creating, and at end of pathname and current
44284d9c625SLionel Sambuc * directory has not been removed, then can consider
44384d9c625SLionel Sambuc * allowing file to be created.
44484d9c625SLionel Sambuc */
44584d9c625SLionel Sambuc if ((nameiop == CREATE || nameiop == RENAME ||
44684d9c625SLionel Sambuc (nameiop == DELETE &&
44784d9c625SLionel Sambuc (cnp->cn_flags & DOWHITEOUT) &&
44884d9c625SLionel Sambuc (cnp->cn_flags & ISWHITEOUT))) &&
44984d9c625SLionel Sambuc (flags & ISLASTCN) && dp->i_nlink != 0) {
45084d9c625SLionel Sambuc /*
45184d9c625SLionel Sambuc * Access for write is interpreted as allowing
45284d9c625SLionel Sambuc * creation of files in the directory.
45384d9c625SLionel Sambuc */
45484d9c625SLionel Sambuc error = VOP_ACCESS(vdp, VWRITE, cred);
45584d9c625SLionel Sambuc if (error)
45684d9c625SLionel Sambuc goto out;
45784d9c625SLionel Sambuc /*
45884d9c625SLionel Sambuc * Return an indication of where the new directory
45984d9c625SLionel Sambuc * entry should be put. If we didn't find a slot,
46084d9c625SLionel Sambuc * then set results->ulr_count to 0 indicating
46184d9c625SLionel Sambuc * that the new slot belongs at the end of the
46284d9c625SLionel Sambuc * directory. If we found a slot, then the new entry
46384d9c625SLionel Sambuc * can be put in the range from results->ulr_offset to
46484d9c625SLionel Sambuc * results->ulr_offset + results->ulr_count.
46584d9c625SLionel Sambuc */
46684d9c625SLionel Sambuc if (slotstatus == NONE) {
46784d9c625SLionel Sambuc results->ulr_offset = roundup(dp->i_size, dirblksiz);
46884d9c625SLionel Sambuc results->ulr_count = 0;
46984d9c625SLionel Sambuc enduseful = results->ulr_offset;
47084d9c625SLionel Sambuc } else if (nameiop == DELETE) {
47184d9c625SLionel Sambuc results->ulr_offset = slotoffset;
47284d9c625SLionel Sambuc if ((results->ulr_offset & (dirblksiz - 1)) == 0)
47384d9c625SLionel Sambuc results->ulr_count = 0;
47484d9c625SLionel Sambuc else
47584d9c625SLionel Sambuc results->ulr_count =
47684d9c625SLionel Sambuc results->ulr_offset - prevoff;
47784d9c625SLionel Sambuc } else {
47884d9c625SLionel Sambuc results->ulr_offset = slotoffset;
47984d9c625SLionel Sambuc results->ulr_count = slotsize;
48084d9c625SLionel Sambuc if (enduseful < slotoffset + slotsize)
48184d9c625SLionel Sambuc enduseful = slotoffset + slotsize;
48284d9c625SLionel Sambuc }
48384d9c625SLionel Sambuc results->ulr_endoff = roundup(enduseful, dirblksiz);
48484d9c625SLionel Sambuc #if 0 /* commented out by dbj. none of the on disk fields changed */
48584d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
48684d9c625SLionel Sambuc #endif
48784d9c625SLionel Sambuc /*
48884d9c625SLionel Sambuc * We return with the directory locked, so that
48984d9c625SLionel Sambuc * the parameters we set up above will still be
49084d9c625SLionel Sambuc * valid if we actually decide to do a direnter().
49184d9c625SLionel Sambuc * We return ni_vp == NULL to indicate that the entry
49284d9c625SLionel Sambuc * does not currently exist; we leave a pointer to
49384d9c625SLionel Sambuc * the (locked) directory inode in ndp->ni_dvp.
49484d9c625SLionel Sambuc *
49584d9c625SLionel Sambuc * NB - if the directory is unlocked, then this
49684d9c625SLionel Sambuc * information cannot be used.
49784d9c625SLionel Sambuc */
49884d9c625SLionel Sambuc error = EJUSTRETURN;
49984d9c625SLionel Sambuc goto out;
50084d9c625SLionel Sambuc }
50184d9c625SLionel Sambuc /*
50284d9c625SLionel Sambuc * Insert name into cache (as non-existent) if appropriate.
50384d9c625SLionel Sambuc */
50484d9c625SLionel Sambuc if (nameiop != CREATE) {
50584d9c625SLionel Sambuc cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
50684d9c625SLionel Sambuc cnp->cn_flags);
50784d9c625SLionel Sambuc }
50884d9c625SLionel Sambuc error = ENOENT;
50984d9c625SLionel Sambuc goto out;
51084d9c625SLionel Sambuc
51184d9c625SLionel Sambuc found:
51284d9c625SLionel Sambuc if (numdirpasses == 2)
513*0a6a1f1dSLionel Sambuc namecache_count_pass2();
51484d9c625SLionel Sambuc /*
51584d9c625SLionel Sambuc * Check that directory length properly reflects presence
51684d9c625SLionel Sambuc * of this entry.
51784d9c625SLionel Sambuc */
518*0a6a1f1dSLionel Sambuc if (results->ulr_offset + LFS_DIRSIZ(fs, ep) > dp->i_size) {
51984d9c625SLionel Sambuc ulfs_dirbad(dp, results->ulr_offset, "i_size too small");
52084d9c625SLionel Sambuc dp->i_size =
521*0a6a1f1dSLionel Sambuc results->ulr_offset + LFS_DIRSIZ(fs, ep);
52284d9c625SLionel Sambuc DIP_ASSIGN(dp, size, dp->i_size);
52384d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
52484d9c625SLionel Sambuc }
52584d9c625SLionel Sambuc brelse(bp, 0);
52684d9c625SLionel Sambuc
52784d9c625SLionel Sambuc /*
52884d9c625SLionel Sambuc * Found component in pathname.
52984d9c625SLionel Sambuc * If the final component of path name, save information
53084d9c625SLionel Sambuc * in the cache as to where the entry was found.
53184d9c625SLionel Sambuc */
53284d9c625SLionel Sambuc if ((flags & ISLASTCN) && nameiop == LOOKUP)
53384d9c625SLionel Sambuc results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
53484d9c625SLionel Sambuc
53584d9c625SLionel Sambuc /*
53684d9c625SLionel Sambuc * If deleting, and at end of pathname, return
53784d9c625SLionel Sambuc * parameters which can be used to remove file.
53884d9c625SLionel Sambuc * Lock the inode, being careful with ".".
53984d9c625SLionel Sambuc */
54084d9c625SLionel Sambuc if (nameiop == DELETE && (flags & ISLASTCN)) {
54184d9c625SLionel Sambuc /*
54284d9c625SLionel Sambuc * Return pointer to current entry in results->ulr_offset,
54384d9c625SLionel Sambuc * and distance past previous entry (if there
54484d9c625SLionel Sambuc * is a previous entry in this block) in results->ulr_count.
54584d9c625SLionel Sambuc * Save directory inode pointer in ndp->ni_dvp for dirremove().
54684d9c625SLionel Sambuc */
54784d9c625SLionel Sambuc if ((results->ulr_offset & (dirblksiz - 1)) == 0)
54884d9c625SLionel Sambuc results->ulr_count = 0;
54984d9c625SLionel Sambuc else
55084d9c625SLionel Sambuc results->ulr_count = results->ulr_offset - prevoff;
55184d9c625SLionel Sambuc if (dp->i_number == foundino) {
55284d9c625SLionel Sambuc vref(vdp);
55384d9c625SLionel Sambuc tdp = vdp;
55484d9c625SLionel Sambuc } else {
555*0a6a1f1dSLionel Sambuc error = vcache_get(vdp->v_mount,
556*0a6a1f1dSLionel Sambuc &foundino, sizeof(foundino), &tdp);
55784d9c625SLionel Sambuc if (error)
55884d9c625SLionel Sambuc goto out;
55984d9c625SLionel Sambuc }
56084d9c625SLionel Sambuc /*
56184d9c625SLionel Sambuc * Write access to directory required to delete files.
56284d9c625SLionel Sambuc */
56384d9c625SLionel Sambuc error = VOP_ACCESS(vdp, VWRITE, cred);
56484d9c625SLionel Sambuc if (error) {
56584d9c625SLionel Sambuc vrele(tdp);
56684d9c625SLionel Sambuc goto out;
56784d9c625SLionel Sambuc }
56884d9c625SLionel Sambuc /*
56984d9c625SLionel Sambuc * If directory is "sticky", then user must own
57084d9c625SLionel Sambuc * the directory, or the file in it, else she
57184d9c625SLionel Sambuc * may not delete it (unless she's root). This
57284d9c625SLionel Sambuc * implements append-only directories.
57384d9c625SLionel Sambuc */
57484d9c625SLionel Sambuc if (dp->i_mode & ISVTX) {
57584d9c625SLionel Sambuc error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
57684d9c625SLionel Sambuc tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
57784d9c625SLionel Sambuc VTOI(tdp)->i_uid));
57884d9c625SLionel Sambuc if (error) {
57984d9c625SLionel Sambuc vrele(tdp);
58084d9c625SLionel Sambuc error = EPERM;
58184d9c625SLionel Sambuc goto out;
58284d9c625SLionel Sambuc }
58384d9c625SLionel Sambuc }
58484d9c625SLionel Sambuc *vpp = tdp;
58584d9c625SLionel Sambuc error = 0;
58684d9c625SLionel Sambuc goto out;
58784d9c625SLionel Sambuc }
58884d9c625SLionel Sambuc
58984d9c625SLionel Sambuc /*
59084d9c625SLionel Sambuc * If rewriting (RENAME), return the inode and the
59184d9c625SLionel Sambuc * information required to rewrite the present directory
59284d9c625SLionel Sambuc * Must get inode of directory entry to verify it's a
59384d9c625SLionel Sambuc * regular file, or empty directory.
59484d9c625SLionel Sambuc */
59584d9c625SLionel Sambuc if (nameiop == RENAME && (flags & ISLASTCN)) {
59684d9c625SLionel Sambuc error = VOP_ACCESS(vdp, VWRITE, cred);
59784d9c625SLionel Sambuc if (error)
59884d9c625SLionel Sambuc goto out;
59984d9c625SLionel Sambuc /*
60084d9c625SLionel Sambuc * Careful about locking second inode.
60184d9c625SLionel Sambuc * This can only occur if the target is ".".
60284d9c625SLionel Sambuc */
60384d9c625SLionel Sambuc if (dp->i_number == foundino) {
60484d9c625SLionel Sambuc error = EISDIR;
60584d9c625SLionel Sambuc goto out;
60684d9c625SLionel Sambuc }
607*0a6a1f1dSLionel Sambuc error = vcache_get(vdp->v_mount,
608*0a6a1f1dSLionel Sambuc &foundino, sizeof(foundino), &tdp);
60984d9c625SLionel Sambuc if (error)
61084d9c625SLionel Sambuc goto out;
61184d9c625SLionel Sambuc *vpp = tdp;
61284d9c625SLionel Sambuc error = 0;
61384d9c625SLionel Sambuc goto out;
61484d9c625SLionel Sambuc }
61584d9c625SLionel Sambuc
616*0a6a1f1dSLionel Sambuc if (dp->i_number == foundino) {
61784d9c625SLionel Sambuc vref(vdp); /* we want ourself, ie "." */
61884d9c625SLionel Sambuc *vpp = vdp;
61984d9c625SLionel Sambuc } else {
620*0a6a1f1dSLionel Sambuc error = vcache_get(vdp->v_mount,
621*0a6a1f1dSLionel Sambuc &foundino, sizeof(foundino), &tdp);
62284d9c625SLionel Sambuc if (error)
62384d9c625SLionel Sambuc goto out;
62484d9c625SLionel Sambuc *vpp = tdp;
62584d9c625SLionel Sambuc }
62684d9c625SLionel Sambuc
62784d9c625SLionel Sambuc /*
62884d9c625SLionel Sambuc * Insert name into cache if appropriate.
62984d9c625SLionel Sambuc */
63084d9c625SLionel Sambuc cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
63184d9c625SLionel Sambuc error = 0;
63284d9c625SLionel Sambuc
63384d9c625SLionel Sambuc out:
63484d9c625SLionel Sambuc fstrans_done(vdp->v_mount);
63584d9c625SLionel Sambuc return error;
63684d9c625SLionel Sambuc }
63784d9c625SLionel Sambuc
63884d9c625SLionel Sambuc void
ulfs_dirbad(struct inode * ip,doff_t offset,const char * how)63984d9c625SLionel Sambuc ulfs_dirbad(struct inode *ip, doff_t offset, const char *how)
64084d9c625SLionel Sambuc {
64184d9c625SLionel Sambuc struct mount *mp;
64284d9c625SLionel Sambuc
64384d9c625SLionel Sambuc mp = ITOV(ip)->v_mount;
64484d9c625SLionel Sambuc printf("%s: bad dir ino %llu at offset %d: %s\n",
64584d9c625SLionel Sambuc mp->mnt_stat.f_mntonname, (unsigned long long)ip->i_number,
64684d9c625SLionel Sambuc offset, how);
647*0a6a1f1dSLionel Sambuc if ((mp->mnt_flag & MNT_RDONLY) == 0)
64884d9c625SLionel Sambuc panic("bad dir");
64984d9c625SLionel Sambuc }
65084d9c625SLionel Sambuc
65184d9c625SLionel Sambuc /*
65284d9c625SLionel Sambuc * Do consistency checking on a directory entry:
65384d9c625SLionel Sambuc * record length must be multiple of 4
65484d9c625SLionel Sambuc * entry must fit in rest of its DIRBLKSIZ block
65584d9c625SLionel Sambuc * record must be large enough to contain entry
65684d9c625SLionel Sambuc * name is not longer than LFS_MAXNAMLEN
65784d9c625SLionel Sambuc * name must be as long as advertised, and null terminated
65884d9c625SLionel Sambuc */
65984d9c625SLionel Sambuc int
ulfs_dirbadentry(struct vnode * dp,LFS_DIRHEADER * ep,int entryoffsetinblock)660*0a6a1f1dSLionel Sambuc ulfs_dirbadentry(struct vnode *dp, LFS_DIRHEADER *ep, int entryoffsetinblock)
66184d9c625SLionel Sambuc {
66284d9c625SLionel Sambuc int i;
66384d9c625SLionel Sambuc int namlen;
664*0a6a1f1dSLionel Sambuc unsigned reclen;
66584d9c625SLionel Sambuc struct ulfsmount *ump = VFSTOULFS(dp->v_mount);
66684d9c625SLionel Sambuc struct lfs *fs = ump->um_lfs;
66784d9c625SLionel Sambuc int dirblksiz = fs->um_dirblksiz;
668*0a6a1f1dSLionel Sambuc const char *name;
66984d9c625SLionel Sambuc
670*0a6a1f1dSLionel Sambuc namlen = lfs_dir_getnamlen(fs, ep);
671*0a6a1f1dSLionel Sambuc reclen = lfs_dir_getreclen(fs, ep);
672*0a6a1f1dSLionel Sambuc if ((reclen & 0x3) != 0 ||
673*0a6a1f1dSLionel Sambuc reclen > dirblksiz - (entryoffsetinblock & (dirblksiz - 1)) ||
674*0a6a1f1dSLionel Sambuc reclen < LFS_DIRSIZ(fs, ep) || namlen > LFS_MAXNAMLEN) {
67584d9c625SLionel Sambuc /*return (1); */
67684d9c625SLionel Sambuc printf("First bad, reclen=%#x, DIRSIZ=%lu, namlen=%d, "
67784d9c625SLionel Sambuc "flags=%#x, entryoffsetinblock=%d, dirblksiz = %d\n",
678*0a6a1f1dSLionel Sambuc lfs_dir_getreclen(fs, ep),
679*0a6a1f1dSLionel Sambuc (u_long)LFS_DIRSIZ(fs, ep),
68084d9c625SLionel Sambuc namlen, dp->v_mount->mnt_flag, entryoffsetinblock,
68184d9c625SLionel Sambuc dirblksiz);
68284d9c625SLionel Sambuc goto bad;
68384d9c625SLionel Sambuc }
684*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep) == 0)
68584d9c625SLionel Sambuc return (0);
686*0a6a1f1dSLionel Sambuc name = lfs_dir_nameptr(fs, ep);
68784d9c625SLionel Sambuc for (i = 0; i < namlen; i++)
688*0a6a1f1dSLionel Sambuc if (name[i] == '\0') {
68984d9c625SLionel Sambuc /*return (1); */
69084d9c625SLionel Sambuc printf("Second bad\n");
69184d9c625SLionel Sambuc goto bad;
69284d9c625SLionel Sambuc }
693*0a6a1f1dSLionel Sambuc if (name[i])
69484d9c625SLionel Sambuc goto bad;
69584d9c625SLionel Sambuc return (0);
69684d9c625SLionel Sambuc bad:
69784d9c625SLionel Sambuc return (1);
69884d9c625SLionel Sambuc }
69984d9c625SLionel Sambuc
70084d9c625SLionel Sambuc /*
701*0a6a1f1dSLionel Sambuc * Assign the contents of directory entry DIRP, on volume FS.
702*0a6a1f1dSLionel Sambuc *
703*0a6a1f1dSLionel Sambuc * NAME/NAMLEN is the name, which is not necessarily null terminated.
704*0a6a1f1dSLionel Sambuc * INUM is the inode number, and DTYPE is the type code (LFS_DT_*).
705*0a6a1f1dSLionel Sambuc *
706*0a6a1f1dSLionel Sambuc * Note that these values typically come from:
707*0a6a1f1dSLionel Sambuc * cnp->cn_nameptr
708*0a6a1f1dSLionel Sambuc * cnp->cn_namelen
709*0a6a1f1dSLionel Sambuc * ip->i_number
710*0a6a1f1dSLionel Sambuc * LFS_IFTODT(ip->i_mode)
711*0a6a1f1dSLionel Sambuc *
712*0a6a1f1dSLionel Sambuc * Does not set d_reclen.
71384d9c625SLionel Sambuc */
714*0a6a1f1dSLionel Sambuc static void
ulfs_direntry_assign(struct lfs * fs,LFS_DIRHEADER * dirp,const char * name,size_t namlen,ino_t inum,unsigned dtype)715*0a6a1f1dSLionel Sambuc ulfs_direntry_assign(struct lfs *fs, LFS_DIRHEADER *dirp,
716*0a6a1f1dSLionel Sambuc const char *name, size_t namlen,
717*0a6a1f1dSLionel Sambuc ino_t inum, unsigned dtype)
71884d9c625SLionel Sambuc {
719*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, dirp, inum);
720*0a6a1f1dSLionel Sambuc lfs_dir_setnamlen(fs, dirp, namlen);
721*0a6a1f1dSLionel Sambuc lfs_dir_settype(fs, dirp, dtype);
722*0a6a1f1dSLionel Sambuc memcpy(lfs_dir_nameptr(fs, dirp), name, namlen);
723*0a6a1f1dSLionel Sambuc lfs_dir_nameptr(fs, dirp)[namlen] = '\0';
72484d9c625SLionel Sambuc }
72584d9c625SLionel Sambuc
72684d9c625SLionel Sambuc /*
72784d9c625SLionel Sambuc * Write a directory entry after a call to namei, using the parameters
72884d9c625SLionel Sambuc * that ulfs_lookup left in nameidata and in the ulfs_lookup_results.
72984d9c625SLionel Sambuc *
73084d9c625SLionel Sambuc * DVP is the directory to be updated. It must be locked.
73184d9c625SLionel Sambuc * ULR is the ulfs_lookup_results structure from the final lookup step.
73284d9c625SLionel Sambuc * TVP is not used. (XXX: why is it here? remove it)
73384d9c625SLionel Sambuc * CNP is the componentname from the final lookup step.
734*0a6a1f1dSLionel Sambuc * INUM is the inode number to insert into the new directory entry.
735*0a6a1f1dSLionel Sambuc * DTYPE is the type code (LFS_DT_*) to insert into the new directory entry.
73684d9c625SLionel Sambuc * NEWDIRBP is not used and (XXX) should be removed. The previous
73784d9c625SLionel Sambuc * comment here said it was used by the now-removed softupdates code.
73884d9c625SLionel Sambuc *
73984d9c625SLionel Sambuc * The link count of the target inode is *not* incremented; the
74084d9c625SLionel Sambuc * caller does that.
74184d9c625SLionel Sambuc *
74284d9c625SLionel Sambuc * If ulr->ulr_count is 0, ulfs_lookup did not find space to insert the
74384d9c625SLionel Sambuc * directory entry. ulr_offset, which is the place to put the entry,
74484d9c625SLionel Sambuc * should be on a block boundary (and should be at the end of the
74584d9c625SLionel Sambuc * directory AFAIK) and a fresh block is allocated to put the new
74684d9c625SLionel Sambuc * directory entry in.
74784d9c625SLionel Sambuc *
74884d9c625SLionel Sambuc * If ulr->ulr_count is not zero, ulfs_lookup found a slot to insert
74984d9c625SLionel Sambuc * the entry into. This slot ranges from ulr_offset to ulr_offset +
75084d9c625SLionel Sambuc * ulr_count. However, this slot may already be partially populated
75184d9c625SLionel Sambuc * requiring compaction. See notes below.
75284d9c625SLionel Sambuc *
75384d9c625SLionel Sambuc * Furthermore, if ulr_count is not zero and ulr_endoff is not the
75484d9c625SLionel Sambuc * same as i_size, the directory is truncated to size ulr_endoff.
75584d9c625SLionel Sambuc */
75684d9c625SLionel Sambuc int
ulfs_direnter(struct vnode * dvp,const struct ulfs_lookup_results * ulr,struct vnode * tvp,struct componentname * cnp,ino_t inum,unsigned dtype,struct buf * newdirbp)75784d9c625SLionel Sambuc ulfs_direnter(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
758*0a6a1f1dSLionel Sambuc struct vnode *tvp,
759*0a6a1f1dSLionel Sambuc struct componentname *cnp, ino_t inum, unsigned dtype,
760*0a6a1f1dSLionel Sambuc struct buf *newdirbp)
76184d9c625SLionel Sambuc {
76284d9c625SLionel Sambuc kauth_cred_t cr;
76384d9c625SLionel Sambuc int newentrysize;
76484d9c625SLionel Sambuc struct inode *dp;
76584d9c625SLionel Sambuc struct buf *bp;
76684d9c625SLionel Sambuc u_int dsize;
767*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *ep, *nep;
76884d9c625SLionel Sambuc int error, ret, lfs_blkoff, loc, spacefree;
76984d9c625SLionel Sambuc char *dirbuf;
77084d9c625SLionel Sambuc struct timespec ts;
77184d9c625SLionel Sambuc struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
77284d9c625SLionel Sambuc struct lfs *fs = ump->um_lfs;
77384d9c625SLionel Sambuc int dirblksiz = fs->um_dirblksiz;
774*0a6a1f1dSLionel Sambuc const char *name;
775*0a6a1f1dSLionel Sambuc unsigned namlen, reclen;
776*0a6a1f1dSLionel Sambuc #ifdef LFS_DIRHASH
777*0a6a1f1dSLionel Sambuc int dohashadd;
778*0a6a1f1dSLionel Sambuc #endif
77984d9c625SLionel Sambuc
78084d9c625SLionel Sambuc error = 0;
781*0a6a1f1dSLionel Sambuc name = cnp->cn_nameptr; /* note: not null-terminated */
782*0a6a1f1dSLionel Sambuc namlen = cnp->cn_namelen;
78384d9c625SLionel Sambuc cr = cnp->cn_cred;
78484d9c625SLionel Sambuc
78584d9c625SLionel Sambuc dp = VTOI(dvp);
786*0a6a1f1dSLionel Sambuc newentrysize = LFS_DIRECTSIZ(fs, namlen);
78784d9c625SLionel Sambuc
78884d9c625SLionel Sambuc if (ulr->ulr_count == 0) {
78984d9c625SLionel Sambuc /*
79084d9c625SLionel Sambuc * If ulr_count is 0, then namei could find no
79184d9c625SLionel Sambuc * space in the directory. Here, ulr_offset will
79284d9c625SLionel Sambuc * be on a directory block boundary and we will write the
79384d9c625SLionel Sambuc * new entry into a fresh block.
79484d9c625SLionel Sambuc */
79584d9c625SLionel Sambuc if (ulr->ulr_offset & (dirblksiz - 1))
79684d9c625SLionel Sambuc panic("ulfs_direnter: newblk");
79784d9c625SLionel Sambuc if ((error = lfs_balloc(dvp, (off_t)ulr->ulr_offset, dirblksiz,
79884d9c625SLionel Sambuc cr, B_CLRBUF | B_SYNC, &bp)) != 0) {
79984d9c625SLionel Sambuc return (error);
80084d9c625SLionel Sambuc }
80184d9c625SLionel Sambuc dp->i_size = ulr->ulr_offset + dirblksiz;
80284d9c625SLionel Sambuc DIP_ASSIGN(dp, size, dp->i_size);
80384d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
80484d9c625SLionel Sambuc uvm_vnp_setsize(dvp, dp->i_size);
80584d9c625SLionel Sambuc lfs_blkoff = ulr->ulr_offset & (ump->um_mountp->mnt_stat.f_iosize - 1);
806*0a6a1f1dSLionel Sambuc ep = (LFS_DIRHEADER *)((char *)bp->b_data + lfs_blkoff);
807*0a6a1f1dSLionel Sambuc ulfs_direntry_assign(fs, ep, name, namlen, inum, dtype);
808*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, ep, dirblksiz);
80984d9c625SLionel Sambuc #ifdef LFS_DIRHASH
81084d9c625SLionel Sambuc if (dp->i_dirhash != NULL) {
81184d9c625SLionel Sambuc ulfsdirhash_newblk(dp, ulr->ulr_offset);
812*0a6a1f1dSLionel Sambuc ulfsdirhash_add(dp, ep, ulr->ulr_offset);
81384d9c625SLionel Sambuc ulfsdirhash_checkblock(dp, (char *)bp->b_data + lfs_blkoff,
81484d9c625SLionel Sambuc ulr->ulr_offset);
81584d9c625SLionel Sambuc }
81684d9c625SLionel Sambuc #endif
81784d9c625SLionel Sambuc error = VOP_BWRITE(bp->b_vp, bp);
81884d9c625SLionel Sambuc vfs_timestamp(&ts);
81984d9c625SLionel Sambuc ret = lfs_update(dvp, &ts, &ts, UPDATE_DIROP);
82084d9c625SLionel Sambuc if (error == 0)
82184d9c625SLionel Sambuc return (ret);
82284d9c625SLionel Sambuc return (error);
82384d9c625SLionel Sambuc }
82484d9c625SLionel Sambuc
82584d9c625SLionel Sambuc /*
82684d9c625SLionel Sambuc * If ulr_count is non-zero, then namei found space for the new
82784d9c625SLionel Sambuc * entry in the range ulr_offset to ulr_offset + ulr_count
82884d9c625SLionel Sambuc * in the directory. To use this space, we may have to compact
82984d9c625SLionel Sambuc * the entries located there, by copying them together towards the
83084d9c625SLionel Sambuc * beginning of the block, leaving the free space in one usable
83184d9c625SLionel Sambuc * chunk at the end.
83284d9c625SLionel Sambuc */
83384d9c625SLionel Sambuc
83484d9c625SLionel Sambuc /*
83584d9c625SLionel Sambuc * Increase size of directory if entry eats into new space.
83684d9c625SLionel Sambuc * This should never push the size past a new multiple of
83784d9c625SLionel Sambuc * DIRBLKSIZ.
83884d9c625SLionel Sambuc *
83984d9c625SLionel Sambuc * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
84084d9c625SLionel Sambuc */
84184d9c625SLionel Sambuc if (ulr->ulr_offset + ulr->ulr_count > dp->i_size) {
84284d9c625SLionel Sambuc #ifdef DIAGNOSTIC
84384d9c625SLionel Sambuc printf("ulfs_direnter: reached 4.2-only block, "
84484d9c625SLionel Sambuc "not supposed to happen\n");
84584d9c625SLionel Sambuc #endif
84684d9c625SLionel Sambuc dp->i_size = ulr->ulr_offset + ulr->ulr_count;
84784d9c625SLionel Sambuc DIP_ASSIGN(dp, size, dp->i_size);
84884d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
84984d9c625SLionel Sambuc }
85084d9c625SLionel Sambuc /*
85184d9c625SLionel Sambuc * Get the block containing the space for the new directory entry.
85284d9c625SLionel Sambuc */
85384d9c625SLionel Sambuc error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp, true);
85484d9c625SLionel Sambuc if (error) {
85584d9c625SLionel Sambuc return (error);
85684d9c625SLionel Sambuc }
85784d9c625SLionel Sambuc /*
85884d9c625SLionel Sambuc * Find space for the new entry. In the simple case, the entry at
85984d9c625SLionel Sambuc * offset base will have the space. If it does not, then namei
86084d9c625SLionel Sambuc * arranged that compacting the region ulr_offset to
86184d9c625SLionel Sambuc * ulr_offset + ulr_count would yield the space.
86284d9c625SLionel Sambuc */
863*0a6a1f1dSLionel Sambuc ep = (LFS_DIRHEADER *)dirbuf;
864*0a6a1f1dSLionel Sambuc dsize = (lfs_dir_getino(fs, ep) != 0) ? LFS_DIRSIZ(fs, ep) : 0;
865*0a6a1f1dSLionel Sambuc spacefree = lfs_dir_getreclen(fs, ep) - dsize;
866*0a6a1f1dSLionel Sambuc for (loc = lfs_dir_getreclen(fs, ep); loc < ulr->ulr_count; ) {
867*0a6a1f1dSLionel Sambuc nep = (LFS_DIRHEADER *)(dirbuf + loc);
86884d9c625SLionel Sambuc
86984d9c625SLionel Sambuc /* Trim the existing slot (NB: dsize may be zero). */
870*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, ep, dsize);
871*0a6a1f1dSLionel Sambuc ep = LFS_NEXTDIR(fs, ep);
87284d9c625SLionel Sambuc
873*0a6a1f1dSLionel Sambuc reclen = lfs_dir_getreclen(fs, nep);
87484d9c625SLionel Sambuc loc += reclen;
875*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, nep) == 0) {
87684d9c625SLionel Sambuc /*
87784d9c625SLionel Sambuc * A mid-block unused entry. Such entries are
87884d9c625SLionel Sambuc * never created by the kernel, but fsck_ffs
87984d9c625SLionel Sambuc * can create them (and it doesn't fix them).
88084d9c625SLionel Sambuc *
88184d9c625SLionel Sambuc * Add up the free space, and initialise the
88284d9c625SLionel Sambuc * relocated entry since we don't memcpy it.
88384d9c625SLionel Sambuc */
88484d9c625SLionel Sambuc spacefree += reclen;
885*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, ep, 0);
88684d9c625SLionel Sambuc dsize = 0;
88784d9c625SLionel Sambuc continue;
88884d9c625SLionel Sambuc }
889*0a6a1f1dSLionel Sambuc dsize = LFS_DIRSIZ(fs, nep);
89084d9c625SLionel Sambuc spacefree += reclen - dsize;
89184d9c625SLionel Sambuc #ifdef LFS_DIRHASH
89284d9c625SLionel Sambuc if (dp->i_dirhash != NULL)
89384d9c625SLionel Sambuc ulfsdirhash_move(dp, nep,
89484d9c625SLionel Sambuc ulr->ulr_offset + ((char *)nep - dirbuf),
89584d9c625SLionel Sambuc ulr->ulr_offset + ((char *)ep - dirbuf));
89684d9c625SLionel Sambuc #endif
89784d9c625SLionel Sambuc memcpy((void *)ep, (void *)nep, dsize);
89884d9c625SLionel Sambuc }
89984d9c625SLionel Sambuc /*
90084d9c625SLionel Sambuc * Here, `ep' points to a directory entry containing `dsize' in-use
90184d9c625SLionel Sambuc * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
90284d9c625SLionel Sambuc * then the entry is completely unused (dsize == 0). The value
90384d9c625SLionel Sambuc * of ep->d_reclen is always indeterminate.
90484d9c625SLionel Sambuc *
90584d9c625SLionel Sambuc * Update the pointer fields in the previous entry (if any),
90684d9c625SLionel Sambuc * copy in the new entry, and write out the block.
90784d9c625SLionel Sambuc */
908*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep) == 0 ||
909*0a6a1f1dSLionel Sambuc (lfs_dir_getino(fs, ep) == ULFS_WINO &&
910*0a6a1f1dSLionel Sambuc memcmp(lfs_dir_nameptr(fs, ep), name, namlen) == 0)) {
91184d9c625SLionel Sambuc if (spacefree + dsize < newentrysize)
91284d9c625SLionel Sambuc panic("ulfs_direnter: compact1");
913*0a6a1f1dSLionel Sambuc reclen = spacefree + dsize;
914*0a6a1f1dSLionel Sambuc #ifdef LFS_DIRHASH
915*0a6a1f1dSLionel Sambuc dohashadd = (lfs_dir_getino(fs, ep) == 0);
916*0a6a1f1dSLionel Sambuc #endif
91784d9c625SLionel Sambuc } else {
91884d9c625SLionel Sambuc if (spacefree < newentrysize)
91984d9c625SLionel Sambuc panic("ulfs_direnter: compact2");
920*0a6a1f1dSLionel Sambuc reclen = spacefree;
921*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, ep, dsize);
922*0a6a1f1dSLionel Sambuc ep = LFS_NEXTDIR(fs, ep);
92384d9c625SLionel Sambuc #ifdef LFS_DIRHASH
924*0a6a1f1dSLionel Sambuc dohashadd = 1;
92584d9c625SLionel Sambuc #endif
926*0a6a1f1dSLionel Sambuc }
927*0a6a1f1dSLionel Sambuc
928*0a6a1f1dSLionel Sambuc ulfs_direntry_assign(fs, ep, name, namlen, inum, dtype);
929*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, ep, reclen);
93084d9c625SLionel Sambuc #ifdef LFS_DIRHASH
931*0a6a1f1dSLionel Sambuc if (dp->i_dirhash != NULL && dohashadd)
932*0a6a1f1dSLionel Sambuc ulfsdirhash_add(dp, ep, ulr->ulr_offset + ((char *)ep - dirbuf));
93384d9c625SLionel Sambuc if (dp->i_dirhash != NULL)
93484d9c625SLionel Sambuc ulfsdirhash_checkblock(dp, dirbuf -
93584d9c625SLionel Sambuc (ulr->ulr_offset & (dirblksiz - 1)),
93684d9c625SLionel Sambuc ulr->ulr_offset & ~(dirblksiz - 1));
93784d9c625SLionel Sambuc #endif
93884d9c625SLionel Sambuc error = VOP_BWRITE(bp->b_vp, bp);
93984d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
94084d9c625SLionel Sambuc /*
94184d9c625SLionel Sambuc * If all went well, and the directory can be shortened, proceed
94284d9c625SLionel Sambuc * with the truncation. Note that we have to unlock the inode for
94384d9c625SLionel Sambuc * the entry that we just entered, as the truncation may need to
94484d9c625SLionel Sambuc * lock other inodes which can lead to deadlock if we also hold a
94584d9c625SLionel Sambuc * lock on the newly entered node.
94684d9c625SLionel Sambuc */
94784d9c625SLionel Sambuc if (error == 0 && ulr->ulr_endoff && ulr->ulr_endoff < dp->i_size) {
94884d9c625SLionel Sambuc #ifdef LFS_DIRHASH
94984d9c625SLionel Sambuc if (dp->i_dirhash != NULL)
95084d9c625SLionel Sambuc ulfsdirhash_dirtrunc(dp, ulr->ulr_endoff);
95184d9c625SLionel Sambuc #endif
95284d9c625SLionel Sambuc (void) lfs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC, cr);
95384d9c625SLionel Sambuc }
95484d9c625SLionel Sambuc return (error);
95584d9c625SLionel Sambuc }
95684d9c625SLionel Sambuc
95784d9c625SLionel Sambuc /*
95884d9c625SLionel Sambuc * Remove a directory entry after a call to namei, using the
95984d9c625SLionel Sambuc * parameters that ulfs_lookup left in nameidata and in the
96084d9c625SLionel Sambuc * ulfs_lookup_results.
96184d9c625SLionel Sambuc *
96284d9c625SLionel Sambuc * DVP is the directory to be updated. It must be locked.
96384d9c625SLionel Sambuc * ULR is the ulfs_lookup_results structure from the final lookup step.
96484d9c625SLionel Sambuc * IP, if not null, is the inode being unlinked.
96584d9c625SLionel Sambuc * FLAGS may contain DOWHITEOUT.
96684d9c625SLionel Sambuc * ISRMDIR is not used and (XXX) should be removed.
96784d9c625SLionel Sambuc *
96884d9c625SLionel Sambuc * If FLAGS contains DOWHITEOUT the entry is replaced with a whiteout
96984d9c625SLionel Sambuc * instead of being cleared.
97084d9c625SLionel Sambuc *
97184d9c625SLionel Sambuc * ulr->ulr_offset contains the position of the directory entry
97284d9c625SLionel Sambuc * to be removed.
97384d9c625SLionel Sambuc *
97484d9c625SLionel Sambuc * ulr->ulr_reclen contains the size of the directory entry to be
97584d9c625SLionel Sambuc * removed.
97684d9c625SLionel Sambuc *
97784d9c625SLionel Sambuc * ulr->ulr_count contains the size of the *previous* directory
97884d9c625SLionel Sambuc * entry. This allows finding it, for free space management. If
97984d9c625SLionel Sambuc * ulr_count is 0, the target entry is at the beginning of the
98084d9c625SLionel Sambuc * directory. (Does this ever happen? The first entry should be ".",
98184d9c625SLionel Sambuc * which should only be removed at rmdir time. Does rmdir come here
98284d9c625SLionel Sambuc * to clear out the "." and ".." entries? Perhaps, but I doubt it.)
98384d9c625SLionel Sambuc *
98484d9c625SLionel Sambuc * The space is marked free by adding it to the record length (not
98584d9c625SLionel Sambuc * name length) of the preceding entry. If the first entry becomes
98684d9c625SLionel Sambuc * free, it is marked free by setting the inode number to 0.
98784d9c625SLionel Sambuc *
98884d9c625SLionel Sambuc * The link count of IP is decremented. Note that this is not the
98984d9c625SLionel Sambuc * inverse behavior of ulfs_direnter, which does not adjust link
99084d9c625SLionel Sambuc * counts. Sigh.
99184d9c625SLionel Sambuc */
99284d9c625SLionel Sambuc int
ulfs_dirremove(struct vnode * dvp,const struct ulfs_lookup_results * ulr,struct inode * ip,int flags,int isrmdir)99384d9c625SLionel Sambuc ulfs_dirremove(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
99484d9c625SLionel Sambuc struct inode *ip, int flags, int isrmdir)
99584d9c625SLionel Sambuc {
99684d9c625SLionel Sambuc struct inode *dp = VTOI(dvp);
997*0a6a1f1dSLionel Sambuc struct lfs *fs = dp->i_lfs;
998*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *ep;
99984d9c625SLionel Sambuc struct buf *bp;
100084d9c625SLionel Sambuc int error;
100184d9c625SLionel Sambuc
100284d9c625SLionel Sambuc if (flags & DOWHITEOUT) {
100384d9c625SLionel Sambuc /*
100484d9c625SLionel Sambuc * Whiteout entry: set d_ino to ULFS_WINO.
100584d9c625SLionel Sambuc */
100684d9c625SLionel Sambuc error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, (void *)&ep,
100784d9c625SLionel Sambuc &bp, true);
100884d9c625SLionel Sambuc if (error)
100984d9c625SLionel Sambuc return (error);
1010*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, ep, ULFS_WINO);
1011*0a6a1f1dSLionel Sambuc lfs_dir_settype(fs, ep, LFS_DT_WHT);
101284d9c625SLionel Sambuc goto out;
101384d9c625SLionel Sambuc }
101484d9c625SLionel Sambuc
101584d9c625SLionel Sambuc if ((error = ulfs_blkatoff(dvp,
101684d9c625SLionel Sambuc (off_t)(ulr->ulr_offset - ulr->ulr_count), (void *)&ep, &bp, true)) != 0)
101784d9c625SLionel Sambuc return (error);
101884d9c625SLionel Sambuc
101984d9c625SLionel Sambuc #ifdef LFS_DIRHASH
102084d9c625SLionel Sambuc /*
102184d9c625SLionel Sambuc * Remove the dirhash entry. This is complicated by the fact
102284d9c625SLionel Sambuc * that `ep' is the previous entry when ulr_count != 0.
102384d9c625SLionel Sambuc */
102484d9c625SLionel Sambuc if (dp->i_dirhash != NULL)
102584d9c625SLionel Sambuc ulfsdirhash_remove(dp, (ulr->ulr_count == 0) ? ep :
1026*0a6a1f1dSLionel Sambuc LFS_NEXTDIR(fs, ep), ulr->ulr_offset);
102784d9c625SLionel Sambuc #endif
102884d9c625SLionel Sambuc
102984d9c625SLionel Sambuc if (ulr->ulr_count == 0) {
103084d9c625SLionel Sambuc /*
103184d9c625SLionel Sambuc * First entry in block: set d_ino to zero.
103284d9c625SLionel Sambuc */
1033*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, ep, 0);
103484d9c625SLionel Sambuc } else {
103584d9c625SLionel Sambuc /*
103684d9c625SLionel Sambuc * Collapse new free space into previous entry.
103784d9c625SLionel Sambuc */
1038*0a6a1f1dSLionel Sambuc lfs_dir_setreclen(fs, ep,
1039*0a6a1f1dSLionel Sambuc lfs_dir_getreclen(fs, ep) + ulr->ulr_reclen);
104084d9c625SLionel Sambuc }
104184d9c625SLionel Sambuc
104284d9c625SLionel Sambuc #ifdef LFS_DIRHASH
104384d9c625SLionel Sambuc if (dp->i_dirhash != NULL) {
104484d9c625SLionel Sambuc int dirblksiz = ip->i_lfs->um_dirblksiz;
104584d9c625SLionel Sambuc ulfsdirhash_checkblock(dp, (char *)ep -
104684d9c625SLionel Sambuc ((ulr->ulr_offset - ulr->ulr_count) & (dirblksiz - 1)),
104784d9c625SLionel Sambuc ulr->ulr_offset & ~(dirblksiz - 1));
104884d9c625SLionel Sambuc }
104984d9c625SLionel Sambuc #endif
105084d9c625SLionel Sambuc
105184d9c625SLionel Sambuc out:
105284d9c625SLionel Sambuc if (ip) {
105384d9c625SLionel Sambuc ip->i_nlink--;
105484d9c625SLionel Sambuc DIP_ASSIGN(ip, nlink, ip->i_nlink);
105584d9c625SLionel Sambuc ip->i_flag |= IN_CHANGE;
105684d9c625SLionel Sambuc }
105784d9c625SLionel Sambuc /*
105884d9c625SLionel Sambuc * XXX did it ever occur to anyone that it might be a good
105984d9c625SLionel Sambuc * idea to restore ip->i_nlink if this fails? Or something?
106084d9c625SLionel Sambuc * Currently on error return from this function the state of
106184d9c625SLionel Sambuc * ip->i_nlink depends on what happened, and callers
106284d9c625SLionel Sambuc * definitely do not take this into account.
106384d9c625SLionel Sambuc */
106484d9c625SLionel Sambuc error = VOP_BWRITE(bp->b_vp, bp);
106584d9c625SLionel Sambuc dp->i_flag |= IN_CHANGE | IN_UPDATE;
106684d9c625SLionel Sambuc /*
106784d9c625SLionel Sambuc * If the last named reference to a snapshot goes away,
106884d9c625SLionel Sambuc * drop its snapshot reference so that it will be reclaimed
106984d9c625SLionel Sambuc * when last open reference goes away.
107084d9c625SLionel Sambuc */
107184d9c625SLionel Sambuc if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 &&
107284d9c625SLionel Sambuc ip->i_nlink == 0)
107384d9c625SLionel Sambuc ulfs_snapgone(ip);
107484d9c625SLionel Sambuc return (error);
107584d9c625SLionel Sambuc }
107684d9c625SLionel Sambuc
107784d9c625SLionel Sambuc /*
107884d9c625SLionel Sambuc * Rewrite an existing directory entry to point at the inode supplied.
107984d9c625SLionel Sambuc *
108084d9c625SLionel Sambuc * DP is the directory to update.
108184d9c625SLionel Sambuc * OFFSET is the position of the entry in question. It may come
108284d9c625SLionel Sambuc * from ulr_offset of a ulfs_lookup_results.
108384d9c625SLionel Sambuc * OIP is the old inode the directory previously pointed to.
108484d9c625SLionel Sambuc * NEWINUM is the number of the new inode.
108584d9c625SLionel Sambuc * NEWTYPE is the new value for the type field of the directory entry.
108684d9c625SLionel Sambuc * (This is ignored if the fs doesn't support that.)
108784d9c625SLionel Sambuc * ISRMDIR is not used and (XXX) should be removed.
108884d9c625SLionel Sambuc * IFLAGS are added to DP's inode flags.
108984d9c625SLionel Sambuc *
109084d9c625SLionel Sambuc * The link count of OIP is decremented. Note that the link count of
109184d9c625SLionel Sambuc * the new inode is *not* incremented. Yay for symmetry.
109284d9c625SLionel Sambuc */
109384d9c625SLionel Sambuc int
ulfs_dirrewrite(struct inode * dp,off_t offset,struct inode * oip,ino_t newinum,int newtype,int isrmdir,int iflags)109484d9c625SLionel Sambuc ulfs_dirrewrite(struct inode *dp, off_t offset,
109584d9c625SLionel Sambuc struct inode *oip, ino_t newinum, int newtype,
109684d9c625SLionel Sambuc int isrmdir, int iflags)
109784d9c625SLionel Sambuc {
1098*0a6a1f1dSLionel Sambuc struct lfs *fs = dp->i_lfs;
109984d9c625SLionel Sambuc struct buf *bp;
1100*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *ep;
110184d9c625SLionel Sambuc struct vnode *vdp = ITOV(dp);
110284d9c625SLionel Sambuc int error;
110384d9c625SLionel Sambuc
110484d9c625SLionel Sambuc error = ulfs_blkatoff(vdp, offset, (void *)&ep, &bp, true);
110584d9c625SLionel Sambuc if (error)
110684d9c625SLionel Sambuc return (error);
1107*0a6a1f1dSLionel Sambuc lfs_dir_setino(fs, ep, newinum);
1108*0a6a1f1dSLionel Sambuc lfs_dir_settype(fs, ep, newtype);
110984d9c625SLionel Sambuc oip->i_nlink--;
111084d9c625SLionel Sambuc DIP_ASSIGN(oip, nlink, oip->i_nlink);
111184d9c625SLionel Sambuc oip->i_flag |= IN_CHANGE;
111284d9c625SLionel Sambuc error = VOP_BWRITE(bp->b_vp, bp);
111384d9c625SLionel Sambuc dp->i_flag |= iflags;
111484d9c625SLionel Sambuc /*
111584d9c625SLionel Sambuc * If the last named reference to a snapshot goes away,
111684d9c625SLionel Sambuc * drop its snapshot reference so that it will be reclaimed
111784d9c625SLionel Sambuc * when last open reference goes away.
111884d9c625SLionel Sambuc */
111984d9c625SLionel Sambuc if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_nlink == 0)
112084d9c625SLionel Sambuc ulfs_snapgone(oip);
112184d9c625SLionel Sambuc return (error);
112284d9c625SLionel Sambuc }
112384d9c625SLionel Sambuc
112484d9c625SLionel Sambuc /*
112584d9c625SLionel Sambuc * Check if a directory is empty or not.
112684d9c625SLionel Sambuc * Inode supplied must be locked.
112784d9c625SLionel Sambuc *
112884d9c625SLionel Sambuc * Using a struct lfs_dirtemplate here is not precisely
112984d9c625SLionel Sambuc * what we want, but better than using a struct lfs_direct.
113084d9c625SLionel Sambuc *
113184d9c625SLionel Sambuc * NB: does not handle corrupted directories.
113284d9c625SLionel Sambuc */
113384d9c625SLionel Sambuc int
ulfs_dirempty(struct inode * ip,ino_t parentino,kauth_cred_t cred)113484d9c625SLionel Sambuc ulfs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
113584d9c625SLionel Sambuc {
1136*0a6a1f1dSLionel Sambuc struct lfs *fs = ip->i_lfs;
113784d9c625SLionel Sambuc doff_t off;
1138*0a6a1f1dSLionel Sambuc union lfs_dirtemplate dbuf;
1139*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *dp = (LFS_DIRHEADER *)&dbuf;
114084d9c625SLionel Sambuc int error, namlen;
1141*0a6a1f1dSLionel Sambuc const char *name;
114284d9c625SLionel Sambuc size_t count;
1143*0a6a1f1dSLionel Sambuc /* XXX this should probably use LFS_DIRECTSIZ(fs, 2) */
1144*0a6a1f1dSLionel Sambuc #define MINDIRSIZ (sizeof (struct lfs_dirtemplate64) / 2)
114584d9c625SLionel Sambuc
1146*0a6a1f1dSLionel Sambuc for (off = 0; off < ip->i_size; off += lfs_dir_getreclen(fs, dp)) {
1147*0a6a1f1dSLionel Sambuc error = ulfs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
1148*0a6a1f1dSLionel Sambuc off, IO_NODELOCKED, cred, &count, NULL);
114984d9c625SLionel Sambuc /*
115084d9c625SLionel Sambuc * Since we read MINDIRSIZ, residual must
115184d9c625SLionel Sambuc * be 0 unless we're at end of file.
115284d9c625SLionel Sambuc */
115384d9c625SLionel Sambuc if (error || count != 0)
115484d9c625SLionel Sambuc return (0);
115584d9c625SLionel Sambuc /* avoid infinite loops */
1156*0a6a1f1dSLionel Sambuc if (lfs_dir_getreclen(fs, dp) == 0)
115784d9c625SLionel Sambuc return (0);
115884d9c625SLionel Sambuc /* skip empty entries */
1159*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, dp) == 0 ||
1160*0a6a1f1dSLionel Sambuc lfs_dir_getino(fs, dp) == ULFS_WINO)
116184d9c625SLionel Sambuc continue;
116284d9c625SLionel Sambuc /* accept only "." and ".." */
1163*0a6a1f1dSLionel Sambuc namlen = lfs_dir_getnamlen(fs, dp);
1164*0a6a1f1dSLionel Sambuc name = lfs_dir_nameptr(fs, dp);
116584d9c625SLionel Sambuc if (namlen > 2)
116684d9c625SLionel Sambuc return (0);
1167*0a6a1f1dSLionel Sambuc if (name[0] != '.')
116884d9c625SLionel Sambuc return (0);
116984d9c625SLionel Sambuc /*
117084d9c625SLionel Sambuc * At this point namlen must be 1 or 2.
117184d9c625SLionel Sambuc * 1 implies ".", 2 implies ".." if second
117284d9c625SLionel Sambuc * char is also "."
117384d9c625SLionel Sambuc */
1174*0a6a1f1dSLionel Sambuc if (namlen == 1 && lfs_dir_getino(fs, dp) == ip->i_number)
117584d9c625SLionel Sambuc continue;
1176*0a6a1f1dSLionel Sambuc if (name[1] == '.' && lfs_dir_getino(fs, dp) == parentino)
117784d9c625SLionel Sambuc continue;
117884d9c625SLionel Sambuc return (0);
117984d9c625SLionel Sambuc }
118084d9c625SLionel Sambuc return (1);
118184d9c625SLionel Sambuc }
118284d9c625SLionel Sambuc
118384d9c625SLionel Sambuc #define ULFS_DIRRABLKS 0
118484d9c625SLionel Sambuc int ulfs_dirrablks = ULFS_DIRRABLKS;
118584d9c625SLionel Sambuc
118684d9c625SLionel Sambuc /*
118784d9c625SLionel Sambuc * ulfs_blkatoff: Return buffer with the contents of block "offset" from
118884d9c625SLionel Sambuc * the beginning of directory "vp". If "res" is non-NULL, fill it in with
118984d9c625SLionel Sambuc * a pointer to the remaining space in the directory. If the caller intends
119084d9c625SLionel Sambuc * to modify the buffer returned, "modify" must be true.
119184d9c625SLionel Sambuc */
119284d9c625SLionel Sambuc
119384d9c625SLionel Sambuc int
ulfs_blkatoff(struct vnode * vp,off_t offset,char ** res,struct buf ** bpp,bool modify)119484d9c625SLionel Sambuc ulfs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp,
119584d9c625SLionel Sambuc bool modify)
119684d9c625SLionel Sambuc {
119784d9c625SLionel Sambuc struct inode *ip __diagused;
119884d9c625SLionel Sambuc struct buf *bp;
119984d9c625SLionel Sambuc daddr_t lbn;
120084d9c625SLionel Sambuc const int dirrablks = ulfs_dirrablks;
120184d9c625SLionel Sambuc daddr_t *blks;
120284d9c625SLionel Sambuc int *blksizes;
120384d9c625SLionel Sambuc int run, error;
120484d9c625SLionel Sambuc struct mount *mp = vp->v_mount;
120584d9c625SLionel Sambuc const int bshift = mp->mnt_fs_bshift;
120684d9c625SLionel Sambuc const int bsize = 1 << bshift;
120784d9c625SLionel Sambuc off_t eof;
120884d9c625SLionel Sambuc
120984d9c625SLionel Sambuc blks = kmem_alloc((1 + dirrablks) * sizeof(daddr_t), KM_SLEEP);
121084d9c625SLionel Sambuc blksizes = kmem_alloc((1 + dirrablks) * sizeof(int), KM_SLEEP);
121184d9c625SLionel Sambuc ip = VTOI(vp);
121284d9c625SLionel Sambuc KASSERT(vp->v_size == ip->i_size);
121384d9c625SLionel Sambuc GOP_SIZE(vp, vp->v_size, &eof, 0);
121484d9c625SLionel Sambuc lbn = offset >> bshift;
121584d9c625SLionel Sambuc
121684d9c625SLionel Sambuc for (run = 0; run <= dirrablks;) {
121784d9c625SLionel Sambuc const off_t curoff = lbn << bshift;
121884d9c625SLionel Sambuc const int size = MIN(eof - curoff, bsize);
121984d9c625SLionel Sambuc
122084d9c625SLionel Sambuc if (size == 0) {
122184d9c625SLionel Sambuc break;
122284d9c625SLionel Sambuc }
122384d9c625SLionel Sambuc KASSERT(curoff < eof);
122484d9c625SLionel Sambuc blks[run] = lbn;
122584d9c625SLionel Sambuc blksizes[run] = size;
122684d9c625SLionel Sambuc lbn++;
122784d9c625SLionel Sambuc run++;
122884d9c625SLionel Sambuc if (size != bsize) {
122984d9c625SLionel Sambuc break;
123084d9c625SLionel Sambuc }
123184d9c625SLionel Sambuc }
123284d9c625SLionel Sambuc KASSERT(run >= 1);
123384d9c625SLionel Sambuc error = breadn(vp, blks[0], blksizes[0], &blks[1], &blksizes[1],
1234*0a6a1f1dSLionel Sambuc run - 1, (modify ? B_MODIFY : 0), &bp);
123584d9c625SLionel Sambuc if (error != 0) {
123684d9c625SLionel Sambuc *bpp = NULL;
123784d9c625SLionel Sambuc goto out;
123884d9c625SLionel Sambuc }
123984d9c625SLionel Sambuc if (res) {
124084d9c625SLionel Sambuc *res = (char *)bp->b_data + (offset & (bsize - 1));
124184d9c625SLionel Sambuc }
124284d9c625SLionel Sambuc *bpp = bp;
124384d9c625SLionel Sambuc
124484d9c625SLionel Sambuc out:
124584d9c625SLionel Sambuc kmem_free(blks, (1 + dirrablks) * sizeof(daddr_t));
124684d9c625SLionel Sambuc kmem_free(blksizes, (1 + dirrablks) * sizeof(int));
124784d9c625SLionel Sambuc return error;
124884d9c625SLionel Sambuc }
1249