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