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