Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_lookup.c revision 1.85
      1 /*	$NetBSD: ext2fs_lookup.c,v 1.85 2016/08/14 11:44:54 jdolecek Exp $	*/
      2 
      3 /*
      4  * Modified for NetBSD 1.2E
      5  * May 1997, Manuel Bouyer
      6  * Laboratoire d'informatique de Paris VI
      7  */
      8 /*
      9  *  modified for Lites 1.1
     10  *
     11  *  Aug 1995, Godmar Back (gback (at) cs.utah.edu)
     12  *  University of Utah, Department of Computer Science
     13  */
     14 /*
     15  * Copyright (c) 1989, 1993
     16  *	The Regents of the University of California.  All rights reserved.
     17  * (c) UNIX System Laboratories, Inc.
     18  * All or some portions of this file are derived from material licensed
     19  * to the University of California by American Telephone and Telegraph
     20  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     21  * the permission of UNIX System Laboratories, Inc.
     22  *
     23  * Redistribution and use in source and binary forms, with or without
     24  * modification, are permitted provided that the following conditions
     25  * are met:
     26  * 1. Redistributions of source code must retain the above copyright
     27  *    notice, this list of conditions and the following disclaimer.
     28  * 2. Redistributions in binary form must reproduce the above copyright
     29  *    notice, this list of conditions and the following disclaimer in the
     30  *    documentation and/or other materials provided with the distribution.
     31  * 3. Neither the name of the University nor the names of its contributors
     32  *    may be used to endorse or promote products derived from this software
     33  *    without specific prior written permission.
     34  *
     35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     45  * SUCH DAMAGE.
     46  *
     47  *	@(#)ufs_lookup.c	8.6 (Berkeley) 4/1/94
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.85 2016/08/14 11:44:54 jdolecek Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/namei.h>
     56 #include <sys/buf.h>
     57 #include <sys/file.h>
     58 #include <sys/mount.h>
     59 #include <sys/vnode.h>
     60 #include <sys/kmem.h>
     61 #include <sys/malloc.h>
     62 #include <sys/dirent.h>
     63 #include <sys/kauth.h>
     64 #include <sys/proc.h>
     65 
     66 #include <ufs/ufs/inode.h>
     67 #include <ufs/ufs/ufsmount.h>
     68 #include <ufs/ufs/ufs_extern.h>
     69 
     70 #include <ufs/ext2fs/ext2fs_extern.h>
     71 #include <ufs/ext2fs/ext2fs_dir.h>
     72 #include <ufs/ext2fs/ext2fs.h>
     73 #include <ufs/ext2fs/ext2fs_htree.h>
     74 
     75 #include <miscfs/genfs/genfs.h>
     76 
     77 extern	int dirchk;
     78 
     79 static void	ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir,
     80 					  struct dirent *ffsdir);
     81 static int	ext2fs_dirbadentry(struct vnode *dp,
     82 					  struct ext2fs_direct *de,
     83 					  int entryoffsetinblock);
     84 
     85 /*
     86  * the problem that is tackled below is the fact that FFS
     87  * includes the terminating zero on disk while EXT2FS doesn't
     88  * this implies that we need to introduce some padding.
     89  * For instance, a filename "sbin" has normally a reclen 12
     90  * in EXT2, but 16 in FFS.
     91  * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
     92  * If it wasn't for that, the complete ufs code for directories would
     93  * have worked w/o changes (except for the difference in DIRBLKSIZ)
     94  */
     95 static void
     96 ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir, struct dirent *ffsdir)
     97 {
     98 	memset(ffsdir, 0, sizeof(struct dirent));
     99 	ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
    100 	ffsdir->d_namlen = e2dir->e2d_namlen;
    101 
    102 	ffsdir->d_type = ext2dt2dt(e2dir->e2d_type);
    103 
    104 #ifdef DIAGNOSTIC
    105 #if MAXNAMLEN < E2FS_MAXNAMLEN
    106 	/*
    107 	 * we should handle this more gracefully !
    108 	 */
    109 	if (e2dir->e2d_namlen > MAXNAMLEN)
    110 		panic("ext2fs: e2dir->e2d_namlen");
    111 #endif
    112 #endif
    113 	strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
    114 
    115 	/* Godmar thinks: since e2dir->e2d_reclen can be big and means
    116 	   nothing anyway, we compute our own reclen according to what
    117 	   we think is right
    118 	 */
    119 	ffsdir->d_reclen = _DIRENT_SIZE(ffsdir);
    120 }
    121 
    122 static int
    123 ext2fs_is_dot_entry(struct componentname *cnp)
    124 {
    125 	return cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
    126 	    (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0');
    127 }
    128 
    129 /*
    130  * Vnode op for reading directories.
    131  *
    132  * Convert the on-disk entries to <sys/dirent.h> entries.
    133  * the problem is that the conversion will blow up some entries by four bytes,
    134  * so it can't be done in place. This is too bad. Right now the conversion is
    135  * done entry by entry, the converted entry is sent via uiomove.
    136  *
    137  * XXX allocate a buffer, convert as many entries as possible, then send
    138  * the whole buffer to uiomove
    139  */
    140 int
    141 ext2fs_readdir(void *v)
    142 {
    143 	struct vop_readdir_args /* {
    144 		struct vnode *a_vp;
    145 		struct uio *a_uio;
    146 		kauth_cred_t a_cred;
    147 		int **a_eofflag;
    148 		off_t **a_cookies;
    149 		int ncookies;
    150 	} */ *ap = v;
    151 	struct uio *uio = ap->a_uio;
    152 	int error;
    153 	size_t e2fs_count, readcnt;
    154 	struct vnode *vp = ap->a_vp;
    155 	struct m_ext2fs *fs = VTOI(vp)->i_e2fs;
    156 
    157 	struct ext2fs_direct *dp;
    158 	struct dirent *dstd;
    159 	struct uio auio;
    160 	struct iovec aiov;
    161 	void *dirbuf;
    162 	off_t off = uio->uio_offset;
    163 	off_t *cookies = NULL;
    164 	int nc = 0, ncookies = 0;
    165 	int e2d_reclen;
    166 
    167 	if (vp->v_type != VDIR)
    168 		return ENOTDIR;
    169 
    170 	e2fs_count = uio->uio_resid;
    171 	/* Make sure we don't return partial entries. */
    172 	e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
    173 	if (e2fs_count <= 0)
    174 		return EINVAL;
    175 
    176 	auio = *uio;
    177 	auio.uio_iov = &aiov;
    178 	auio.uio_iovcnt = 1;
    179 	aiov.iov_len = e2fs_count;
    180 	auio.uio_resid = e2fs_count;
    181 	UIO_SETUP_SYSSPACE(&auio);
    182 	dirbuf = kmem_alloc(e2fs_count, KM_SLEEP);
    183 	dstd = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    184 	if (ap->a_ncookies) {
    185 		nc = e2fs_count / _DIRENT_MINSIZE((struct dirent *)0);
    186 		ncookies = nc;
    187 		cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
    188 		*ap->a_cookies = cookies;
    189 	}
    190 	aiov.iov_base = dirbuf;
    191 
    192 	error = UFS_BUFRD(ap->a_vp, &auio, 0, ap->a_cred);
    193 	if (error == 0) {
    194 		readcnt = e2fs_count - auio.uio_resid;
    195 		for (dp = (struct ext2fs_direct *)dirbuf;
    196 			(char *)dp < (char *)dirbuf + readcnt;) {
    197 			e2d_reclen = fs2h16(dp->e2d_reclen);
    198 			if (e2d_reclen == 0) {
    199 				error = EIO;
    200 				break;
    201 			}
    202 			ext2fs_dirconv2ffs(dp, dstd);
    203 			if(dstd->d_reclen > uio->uio_resid) {
    204 				break;
    205 			}
    206 			error = uiomove(dstd, dstd->d_reclen, uio);
    207 			if (error != 0) {
    208 				break;
    209 			}
    210 			off = off + e2d_reclen;
    211 			if (cookies != NULL) {
    212 				*cookies++ = off;
    213 				if (--ncookies <= 0){
    214 					break;  /* out of cookies */
    215 				}
    216 			}
    217 			/* advance dp */
    218 			dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
    219 		}
    220 		/* we need to correct uio_offset */
    221 		uio->uio_offset = off;
    222 	}
    223 	kmem_free(dirbuf, e2fs_count);
    224 	kmem_free(dstd, sizeof(*dstd));
    225 	*ap->a_eofflag = ext2fs_size(VTOI(ap->a_vp)) <= uio->uio_offset;
    226 	if (ap->a_ncookies) {
    227 		if (error) {
    228 			free(*ap->a_cookies, M_TEMP);
    229 			*ap->a_ncookies = 0;
    230 			*ap->a_cookies = NULL;
    231 		} else
    232 			*ap->a_ncookies = nc - ncookies;
    233 	}
    234 	return error;
    235 }
    236 
    237 /*
    238  * Convert a component of a pathname into a pointer to a locked inode.
    239  * This is a very central and rather complicated routine.
    240  * If the file system is not maintained in a strict tree hierarchy,
    241  * this can result in a deadlock situation (see comments in code below).
    242  *
    243  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
    244  * on whether the name is to be looked up, created, renamed, or deleted.
    245  * When CREATE, RENAME, or DELETE is specified, information usable in
    246  * creating, renaming, or deleting a directory entry may be calculated.
    247  * If flag has LOCKPARENT or'ed into it and the target of the pathname
    248  * exists, lookup returns both the target and its parent directory locked.
    249  * When creating or renaming and LOCKPARENT is specified, the target may
    250  * not be ".".  When deleting and LOCKPARENT is specified, the target may
    251  * be "."., but the caller must check to ensure it does an vrele and vput
    252  * instead of two vputs.
    253  *
    254  * Overall outline of ext2fs_lookup:
    255  *
    256  *	check accessibility of directory
    257  *	look for name in cache, if found, then if at end of path
    258  *	  and deleting or creating, drop it, else return name
    259  *	search for name in directory, to found or notfound
    260  * notfound:
    261  *	if creating, return locked directory, leaving info on available slots
    262  *	else return error
    263  * found:
    264  *	if at end of path and deleting, return information to allow delete
    265  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
    266  *	  inode and return info to allow rewrite
    267  *	if not at end, add name to cache; if at end and neither creating
    268  *	  nor deleting, add name to cache
    269  */
    270 int
    271 ext2fs_lookup(void *v)
    272 {
    273 	struct vop_lookup_v2_args /* {
    274 		struct vnode *a_dvp;
    275 		struct vnode **a_vpp;
    276 		struct componentname *a_cnp;
    277 	} */ *ap = v;
    278 	struct vnode *vdp = ap->a_dvp;	/* vnode for directory being searched */
    279 	struct inode *dp = VTOI(vdp);	/* inode for directory being searched */
    280 	struct buf *bp;			/* a buffer of directory entries */
    281 	struct ext2fs_direct *ep;	/* the current directory entry */
    282 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
    283 	enum ext2fs_slotstatus slotstatus;
    284 	doff_t slotoffset;		/* offset of area with free space */
    285 	int slotsize;			/* size of area at slotoffset */
    286 	int slotfreespace;		/* amount of space free in slot */
    287 	int slotneeded;			/* size of the entry we're seeking */
    288 	int numdirpasses;		/* strategy for directory search */
    289 	doff_t endsearch;		/* offset to end directory search */
    290 	doff_t prevoff;			/* prev entry dp->i_offset */
    291 	struct vnode *tdp;		/* returned by vcache_get */
    292 	doff_t enduseful;		/* pointer past last used dir slot */
    293 	u_long bmask;			/* block offset mask */
    294 	int namlen, error;
    295 	struct vnode **vpp = ap->a_vpp;
    296 	struct componentname *cnp = ap->a_cnp;
    297 	kauth_cred_t cred = cnp->cn_cred;
    298 	int flags;
    299 	int nameiop = cnp->cn_nameiop;
    300 	struct ufsmount *ump = dp->i_ump;
    301 	int dirblksiz = ump->um_dirblksiz;
    302 	ino_t foundino;
    303 	struct ufs_lookup_results *results;
    304 	doff_t i_offset;		/* cached i_offset value */
    305 	struct ext2fs_searchslot ss;
    306 
    307 	flags = cnp->cn_flags;
    308 
    309 	bp = NULL;
    310 	slotoffset = -1;
    311 	*vpp = NULL;
    312 
    313 	/*
    314 	 * Produce the auxiliary lookup results into i_crap. Increment
    315 	 * its serial number so elsewhere we can tell if we're using
    316 	 * stale results. This should not be done this way. XXX.
    317 	 */
    318 	results = &dp->i_crap;
    319 	dp->i_crapcounter++;
    320 
    321 	/*
    322 	 * Check accessiblity of directory.
    323 	 */
    324 	if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
    325 		return error;
    326 
    327 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    328 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    329 		return EROFS;
    330 
    331 	/*
    332 	 * We now have a segment name to search for, and a directory to search.
    333 	 *
    334 	 * Before tediously performing a linear scan of the directory,
    335 	 * check the name cache to see if the directory/name pair
    336 	 * we are looking for is known already.
    337 	 */
    338 	if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
    339 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
    340 		return *vpp == NULLVP ? ENOENT : 0;
    341 	}
    342 
    343 	/*
    344 	 * Suppress search for slots unless creating
    345 	 * file and at end of pathname, in which case
    346 	 * we watch for a place to put the new file in
    347 	 * case it doesn't already exist.
    348 	 */
    349 	slotstatus = FOUND;
    350 	slotfreespace = slotsize = slotneeded = 0;
    351 	if ((nameiop == CREATE || nameiop == RENAME) &&
    352 	    (flags & ISLASTCN)) {
    353 		slotstatus = NONE;
    354 		slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
    355 	}
    356 
    357 	/*
    358 	 * If there is cached information on a previous search of
    359 	 * this directory, pick up where we last left off.
    360 	 * We cache only lookups as these are the most common
    361 	 * and have the greatest payoff. Caching CREATE has little
    362 	 * benefit as it usually must search the entire directory
    363 	 * to determine that the entry does not exist. Caching the
    364 	 * location of the last DELETE or RENAME has not reduced
    365 	 * profiling time and hence has been removed in the interest
    366 	 * of simplicity.
    367 	 */
    368 	bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
    369 	if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
    370 	    results->ulr_diroff >= ext2fs_size(dp)) {
    371 		entryoffsetinblock = 0;
    372 		results->ulr_offset = 0;
    373 		numdirpasses = 1;
    374 	} else {
    375 		results->ulr_offset = results->ulr_diroff;
    376 		if ((entryoffsetinblock = results->ulr_offset & bmask) &&
    377 		    (error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL, &bp)))
    378 			return error;
    379 		numdirpasses = 2;
    380 		namecache_count_2passes();
    381 	}
    382 	prevoff = results->ulr_offset;
    383 	endsearch = roundup(ext2fs_size(dp), dirblksiz);
    384 	enduseful = 0;
    385 
    386 	/*
    387 	 * Try to lookup dir entry using htree directory index.
    388 	 *
    389 	 * If we got an error or we want to find '.' or '..' entry,
    390 	 * we will fall back to linear search.
    391 	 */
    392 	if (!ext2fs_is_dot_entry(cnp) && ext2fs_htree_has_idx(dp)) {
    393 		numdirpasses = 1;
    394 		entryoffsetinblock = 0;
    395 
    396 		int htree_lookup_ret = ext2fs_htree_lookup(dp, cnp->cn_nameptr,
    397 		    cnp->cn_namelen, &bp, &entryoffsetinblock, &i_offset,
    398 		    &prevoff, &enduseful, &ss);
    399 		switch (htree_lookup_ret) {
    400 		case 0:
    401 			ep = (struct ext2fs_direct*)((char *)bp->b_data +
    402 			    (i_offset & bmask));
    403 			foundino = ep->e2d_ino;
    404 			goto found;
    405 		case ENOENT:
    406 			i_offset = roundup2(dp->i_size, dp->i_e2fs->e2fs_bsize);
    407 			goto notfound;
    408 		default:
    409 			/*
    410 			 * Something failed; just fallback to do a linear
    411 			 * search.
    412 			 */
    413 			break;
    414 		}
    415 	}
    416 
    417 searchloop:
    418 	while (results->ulr_offset < endsearch) {
    419 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    420 			preempt();
    421 		/*
    422 		 * If necessary, get the next directory block.
    423 		 */
    424 		if ((results->ulr_offset & bmask) == 0) {
    425 			if (bp != NULL)
    426 				brelse(bp, 0);
    427 			error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL,
    428 			    &bp);
    429 			if (error != 0)
    430 				return error;
    431 			entryoffsetinblock = 0;
    432 		}
    433 		/*
    434 		 * If still looking for a slot, and at a dirblksize
    435 		 * boundary, have to start looking for free space again.
    436 		 */
    437 		if (slotstatus == NONE &&
    438 		    (entryoffsetinblock & (dirblksiz - 1)) == 0) {
    439 			slotoffset = -1;
    440 			slotfreespace = 0;
    441 		}
    442 		/*
    443 		 * Get pointer to next entry.
    444 		 * Full validation checks are slow, so we only check
    445 		 * enough to insure forward progress through the
    446 		 * directory. Complete checks can be run by patching
    447 		 * "dirchk" to be true.
    448 		 */
    449 		KASSERT(bp != NULL);
    450 		ep = (struct ext2fs_direct *)
    451 			((char *)bp->b_data + entryoffsetinblock);
    452 		if (ep->e2d_reclen == 0 ||
    453 		    (dirchk &&
    454 		     ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
    455 			int i;
    456 
    457 			ufs_dirbad(dp, results->ulr_offset, "mangled entry");
    458 			i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
    459 			results->ulr_offset += i;
    460 			entryoffsetinblock += i;
    461 			continue;
    462 		}
    463 
    464 		/*
    465 		 * If an appropriate sized slot has not yet been found,
    466 		 * check to see if one is available. Also accumulate space
    467 		 * in the current block so that we can determine if
    468 		 * compaction is viable.
    469 		 */
    470 		if (slotstatus != FOUND) {
    471 			int size = fs2h16(ep->e2d_reclen);
    472 
    473 			if (ep->e2d_ino != 0)
    474 				size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
    475 			if (size > 0) {
    476 				if (size >= slotneeded) {
    477 					slotstatus = FOUND;
    478 					slotoffset = results->ulr_offset;
    479 					slotsize = fs2h16(ep->e2d_reclen);
    480 				} else if (slotstatus == NONE) {
    481 					slotfreespace += size;
    482 					if (slotoffset == -1)
    483 						slotoffset = results->ulr_offset;
    484 					if (slotfreespace >= slotneeded) {
    485 						slotstatus = COMPACT;
    486 						slotsize = results->ulr_offset +
    487 						    fs2h16(ep->e2d_reclen) -
    488 						    slotoffset;
    489 					}
    490 				}
    491 			}
    492 		}
    493 
    494 		/*
    495 		 * Check for a name match.
    496 		 */
    497 		if (ep->e2d_ino) {
    498 			namlen = ep->e2d_namlen;
    499 			if (namlen == cnp->cn_namelen &&
    500 			    !memcmp(cnp->cn_nameptr, ep->e2d_name,
    501 			    (unsigned)namlen)) {
    502 				/*
    503 				 * Save directory entry's inode number and
    504 				 * reclen in ndp->ni_ufs area, and release
    505 				 * directory buffer.
    506 				 */
    507 				foundino = fs2h32(ep->e2d_ino);
    508 				results->ulr_reclen = fs2h16(ep->e2d_reclen);
    509 				goto found;
    510 			}
    511 		}
    512 		prevoff = results->ulr_offset;
    513 		results->ulr_offset += fs2h16(ep->e2d_reclen);
    514 		entryoffsetinblock += fs2h16(ep->e2d_reclen);
    515 		if (ep->e2d_ino)
    516 			enduseful = results->ulr_offset;
    517 	}
    518 notfound:
    519 	/*
    520 	 * If we started in the middle of the directory and failed
    521 	 * to find our target, we must check the beginning as well.
    522 	 */
    523 	if (numdirpasses == 2) {
    524 		numdirpasses--;
    525 		results->ulr_offset = 0;
    526 		endsearch = results->ulr_diroff;
    527 		goto searchloop;
    528 	}
    529 	if (bp != NULL)
    530 		brelse(bp, 0);
    531 	/*
    532 	 * If creating, and at end of pathname and current
    533 	 * directory has not been removed, then can consider
    534 	 * allowing file to be created.
    535 	 */
    536 	if ((nameiop == CREATE || nameiop == RENAME) &&
    537 	    (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
    538 		/*
    539 		 * Access for write is interpreted as allowing
    540 		 * creation of files in the directory.
    541 		 */
    542 		error = VOP_ACCESS(vdp, VWRITE, cred);
    543 		if (error)
    544 			return error;
    545 		/*
    546 		 * Return an indication of where the new directory
    547 		 * entry should be put.  If we didn't find a slot,
    548 		 * then set results->ulr_count to 0 indicating
    549 		 * that the new slot belongs at the end of the
    550 		 * directory. If we found a slot, then the new entry
    551 		 * can be put in the range from results->ulr_offset to
    552 		 * results->ulr_offset + results->ulr_count.
    553 		 */
    554 		if (slotstatus == NONE) {
    555 			results->ulr_offset = roundup(ext2fs_size(dp), dirblksiz);
    556 			results->ulr_count = 0;
    557 			enduseful = results->ulr_offset;
    558 		} else {
    559 			results->ulr_offset = slotoffset;
    560 			results->ulr_count = slotsize;
    561 			if (enduseful < slotoffset + slotsize)
    562 				enduseful = slotoffset + slotsize;
    563 		}
    564 		results->ulr_endoff = roundup(enduseful, dirblksiz);
    565 #if 0
    566 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    567 #endif
    568 		/*
    569 		 * We return with the directory locked, so that
    570 		 * the parameters we set up above will still be
    571 		 * valid if we actually decide to do a direnter().
    572 		 * We return ni_vp == NULL to indicate that the entry
    573 		 * does not currently exist; we leave a pointer to
    574 		 * the (locked) directory inode in ndp->ni_dvp.
    575 		 *
    576 		 * NB - if the directory is unlocked, then this
    577 		 * information cannot be used.
    578 		 */
    579 		return EJUSTRETURN;
    580 	}
    581 	/*
    582 	 * Insert name into cache (as non-existent) if appropriate.
    583 	 */
    584 	if (nameiop != CREATE) {
    585 		cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    586 		    cnp->cn_flags);
    587 	}
    588 	return ENOENT;
    589 
    590 found:
    591 	if (numdirpasses == 2)
    592 		namecache_count_pass2();
    593 	/*
    594 	 * Check that directory length properly reflects presence
    595 	 * of this entry.
    596 	 */
    597 	if (results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
    598 		ufs_dirbad(dp, results->ulr_offset, "i_size too small");
    599 		error = ext2fs_setsize(dp,
    600 				results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
    601 		if (error) {
    602 			brelse(bp, 0);
    603 			return error;
    604 		}
    605 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    606 		uvm_vnp_setsize(vdp, ext2fs_size(dp));
    607 	}
    608 	brelse(bp, 0);
    609 
    610 	/*
    611 	 * Found component in pathname.
    612 	 * If the final component of path name, save information
    613 	 * in the cache as to where the entry was found.
    614 	 */
    615 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
    616 		results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
    617 
    618 	/*
    619 	 * If deleting, and at end of pathname, return
    620 	 * parameters which can be used to remove file.
    621 	 * Lock the inode, being careful with ".".
    622 	 */
    623 	if (nameiop == DELETE && (flags & ISLASTCN)) {
    624 		/*
    625 		 * Return pointer to current entry in results->ulr_offset,
    626 		 * and distance past previous entry (if there
    627 		 * is a previous entry in this block) in results->ulr_count.
    628 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
    629 		 */
    630 		if ((results->ulr_offset & (dirblksiz - 1)) == 0)
    631 			results->ulr_count = 0;
    632 		else
    633 			results->ulr_count = results->ulr_offset - prevoff;
    634 		if (dp->i_number == foundino) {
    635 			vref(vdp);
    636 			tdp = vdp;
    637 		} else {
    638 			error = vcache_get(vdp->v_mount,
    639 			    &foundino, sizeof(foundino), &tdp);
    640 			if (error)
    641 				return error;
    642 		}
    643 		/*
    644 		 * Write access to directory required to delete files.
    645 		 */
    646 		if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0) {
    647 			vrele(tdp);
    648 			return error;
    649 		}
    650 		/*
    651 		 * If directory is "sticky", then user must own
    652 		 * the directory, or the file in it, else she
    653 		 * may not delete it (unless she's root). This
    654 		 * implements append-only directories.
    655 		 */
    656 		if (dp->i_e2fs_mode & ISVTX) {
    657 			error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
    658 			    tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
    659 			    VTOI(tdp)->i_uid));
    660 			if (error) {
    661 				vrele(tdp);
    662 				return EPERM;
    663 			}
    664 		}
    665 		*vpp = tdp;
    666 		return 0;
    667 	}
    668 
    669 	/*
    670 	 * If rewriting (RENAME), return the inode and the
    671 	 * information required to rewrite the present directory
    672 	 * Must get inode of directory entry to verify it's a
    673 	 * regular file, or empty directory.
    674 	 */
    675 	if (nameiop == RENAME && (flags & ISLASTCN)) {
    676 		error = VOP_ACCESS(vdp, VWRITE, cred);
    677 		if (error)
    678 			return error;
    679 		/*
    680 		 * Careful about locking second inode.
    681 		 * This can only occur if the target is ".".
    682 		 */
    683 		if (dp->i_number == foundino)
    684 			return EISDIR;
    685 		error = vcache_get(vdp->v_mount,
    686 		    &foundino, sizeof(foundino), &tdp);
    687 		if (error)
    688 			return error;
    689 		*vpp = tdp;
    690 		return 0;
    691 	}
    692 
    693 	if (dp->i_number == foundino) {
    694 		vref(vdp);	/* we want ourself, ie "." */
    695 		*vpp = vdp;
    696 	} else {
    697 		error = vcache_get(vdp->v_mount,
    698 		    &foundino, sizeof(foundino), &tdp);
    699 		if (error)
    700 			return error;
    701 		*vpp = tdp;
    702 	}
    703 
    704 	/*
    705 	 * Insert name into cache if appropriate.
    706 	 */
    707 	cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
    708 	return 0;
    709 }
    710 static void
    711 ext2fs_accumulatespace (struct ext2fs_searchslot *ssp, struct ext2fs_direct *ep,
    712     doff_t *offp)
    713 {
    714 	int size = ep->e2d_reclen;
    715 
    716 	if (ep->e2d_ino != 0)
    717 		size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
    718 
    719 	if (size <= 0)
    720 		return;
    721 
    722 	if (size >= ssp->slotneeded) {
    723 		ssp->slotstatus = FOUND;
    724 		ssp->slotoffset = *offp;
    725 		ssp->slotsize = ep->e2d_reclen;
    726 		return;
    727 	}
    728 
    729 	if (ssp->slotstatus != NONE)
    730 		return;
    731 
    732 	ssp->slotfreespace += size;
    733 	if (ssp->slotoffset == -1)
    734 		ssp->slotoffset = *offp;
    735 
    736 	if (ssp->slotfreespace >= ssp->slotneeded) {
    737 		ssp->slotstatus = COMPACT;
    738 		ssp->slotsize = *offp + ep->e2d_reclen - ssp->slotoffset;
    739 	}
    740 }
    741 
    742 int
    743 ext2fs_search_dirblock(struct inode *ip, void *data, int *foundp,
    744     const char *name, int namelen, int *entryoffsetinblockp,
    745     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
    746     struct ext2fs_searchslot *ssp)
    747 {
    748 	struct vnode *vdp = ITOV(ip);
    749 	struct ext2fs_direct *ep, *top;
    750 	uint32_t bsize = ip->i_e2fs->e2fs_bsize;
    751 	int offset = *entryoffsetinblockp;
    752 	int namlen;
    753 
    754 	ep = (void *)((char *)data + offset);
    755 	top = (void *)((char *)data + bsize - EXT2_DIR_REC_LEN(0));
    756 
    757 	while (ep < top) {
    758 		/*
    759 		 * Full validation checks are slow, so we only check
    760 		 * enough to insure forward progress through the
    761 		 * directory. Complete checks can be run by setting
    762 		 * "vfs.e2fs.dirchk" to be true.
    763 		 */
    764 		if (ep->e2d_reclen == 0 ||
    765 		    (dirchk && ext2fs_dirbadentry(vdp, ep, offset))) {
    766 			int i;
    767 			ufs_dirbad(ip, *offp, "mangled entry");
    768 			i = bsize - (offset & (bsize - 1));
    769 			*offp += i;
    770 			offset += i;
    771 			continue;
    772 		}
    773 
    774 		/*
    775 		 * If an appropriate sized slot has not yet been found,
    776 		 * check to see if one is available. Also accumulate space
    777 		 * in the current block so that we can determine if
    778 		 * compaction is viable.
    779 		 */
    780 		if (ssp->slotstatus != FOUND)
    781 			ext2fs_accumulatespace(ssp, ep, offp);
    782 
    783 		/*
    784 		 * Check for a name match.
    785 		 */
    786 		if (ep->e2d_ino) {
    787 			namlen = ep->e2d_namlen;
    788 			if (namlen == namelen &&
    789 			    !memcmp(name, ep->e2d_name, (unsigned)namlen)) {
    790 				/*
    791 				 * Save directory entry's inode number and
    792 				 * reclen in ndp->ni_ufs area, and release
    793 				 * directory buffer.
    794 				 */
    795 				*foundp = 1;
    796 				return 0;
    797 			}
    798 		}
    799 		*prevoffp = *offp;
    800 		*offp += ep->e2d_reclen;
    801 		offset += ep->e2d_reclen;
    802 		*entryoffsetinblockp = offset;
    803 		if (ep->e2d_ino)
    804 			*endusefulp = *offp;
    805 		/*
    806 		 * Get pointer to the next entry.
    807 		 */
    808 		ep = (void *)((char *)data + offset);
    809 	}
    810 
    811 	return 0;
    812 }
    813 
    814 /*
    815  * Do consistency checking on a directory entry:
    816  *	record length must be multiple of 4
    817  *	entry must fit in rest of its dirblksize block
    818  *	record must be large enough to contain entry
    819  *	name is not longer than EXT2FS_MAXNAMLEN
    820  *	name must be as long as advertised, and null terminated
    821  */
    822 /*
    823  *	changed so that it confirms to ext2fs_check_dir_entry
    824  */
    825 static int
    826 ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
    827 		int entryoffsetinblock)
    828 {
    829 	struct ufsmount *ump = VFSTOUFS(dp->v_mount);
    830 	int dirblksiz = ump->um_dirblksiz;
    831 
    832 	const char *error_msg = NULL;
    833 	int reclen = fs2h16(de->e2d_reclen);
    834 	int namlen = de->e2d_namlen;
    835 
    836 	if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
    837 		error_msg = "rec_len is smaller than minimal";
    838 #if 0
    839 	else if (reclen % 4 != 0)
    840 		error_msg = "rec_len % 4 != 0";
    841 #endif
    842 	else if (namlen > EXT2FS_MAXNAMLEN)
    843 		error_msg = "namlen > EXT2FS_MAXNAMLEN";
    844 	else if (reclen < EXT2FS_DIRSIZ(namlen))
    845 		error_msg = "reclen is too small for name_len";
    846 	else if (entryoffsetinblock + reclen > dirblksiz)
    847 		error_msg = "directory entry across blocks";
    848 	else if (fs2h32(de->e2d_ino) >
    849 	    VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
    850 		error_msg = "inode out of bounds";
    851 
    852 	if (error_msg != NULL) {
    853 		printf( "bad directory entry: %s\n"
    854 		    "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
    855 		    error_msg, entryoffsetinblock,
    856 		    (unsigned long) fs2h32(de->e2d_ino),
    857 		    reclen, namlen);
    858 		panic("ext2fs_dirbadentry");
    859 	}
    860 	return error_msg == NULL ? 0 : 1;
    861 }
    862 
    863 /*
    864  * Write a directory entry after a call to namei, using the parameters
    865  * that it left in nameidata.  The argument ip is the inode which the new
    866  * directory entry will refer to.  Dvp is a pointer to the directory to
    867  * be written, which was left locked by namei. Remaining parameters
    868  * (ulr_offset, ulr_count) indicate how the space for the new
    869  * entry is to be obtained.
    870  */
    871 int
    872 ext2fs_direnter(struct inode *ip, struct vnode *dvp,
    873     const struct ufs_lookup_results *ulr, struct componentname *cnp)
    874 {
    875 	struct inode *dp;
    876 	struct ext2fs_direct newdir;
    877 	struct iovec aiov;
    878 	struct uio auio;
    879 	int error, newentrysize;
    880 	struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
    881 	int dirblksiz = ump->um_dirblksiz;
    882 
    883 	dp = VTOI(dvp);
    884 
    885 	newdir.e2d_ino = h2fs32(ip->i_number);
    886 	newdir.e2d_namlen = cnp->cn_namelen;
    887 	if (EXT2F_HAS_INCOMPAT_FEATURE(ip->i_e2fs, EXT2F_INCOMPAT_FTYPE)) {
    888 		newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
    889 	} else {
    890 		newdir.e2d_type = 0;
    891 	}
    892 	memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
    893 	newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
    894 
    895 	if (ext2fs_htree_has_idx(dp)) {
    896 		error = ext2fs_htree_add_entry(dvp, &newdir, cnp);
    897 		if (error) {
    898 			dp->i_e2fs_flags&= ~EXT2_INDEX;
    899 			dp->i_flag |= IN_CHANGE | IN_UPDATE;
    900 		}
    901 		return error;
    902 	}
    903 
    904 	/*
    905 	 * TODO check if Htree index is not created for the directory then
    906 	 * create one if directory entries get overflew the first dir-block
    907 	 */
    908 	if (ulr->ulr_count == 0) {
    909 		/*
    910 		 * If ulr_count is 0, then namei could find no
    911 		 * space in the directory. Here, ulr_offset will
    912 		 * be on a directory block boundary and we will write the
    913 		 * new entry into a fresh block.
    914 		 */
    915 		if (ulr->ulr_offset & (dirblksiz - 1))
    916 			panic("ext2fs_direnter: newblk");
    917 		auio.uio_offset = ulr->ulr_offset;
    918 		newdir.e2d_reclen = h2fs16(dirblksiz);
    919 		auio.uio_resid = newentrysize;
    920 		aiov.iov_len = newentrysize;
    921 		aiov.iov_base = (void *)&newdir;
    922 		auio.uio_iov = &aiov;
    923 		auio.uio_iovcnt = 1;
    924 		auio.uio_rw = UIO_WRITE;
    925 		UIO_SETUP_SYSSPACE(&auio);
    926 		error = ext2fs_bufwr(dvp, &auio, IO_SYNC, cnp->cn_cred);
    927 		if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
    928 			/* XXX should grow with balloc() */
    929 			panic("ext2fs_direnter: frag size");
    930 		else if (!error) {
    931 			error = ext2fs_setsize(dp,
    932 				roundup(ext2fs_size(dp), dirblksiz));
    933 			if (error)
    934 				return error;
    935 			dp->i_flag |= IN_CHANGE;
    936 			uvm_vnp_setsize(dvp, ext2fs_size(dp));
    937 		}
    938 		return error;
    939 	}
    940 
    941 	error = ext2fs_add_entry(dvp, &newdir, ulr);
    942 
    943 	if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp))
    944 		error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC,
    945 		    cnp->cn_cred);
    946 	return error;
    947 }
    948 
    949 /*
    950  * Insert an entry into the directory block.
    951  * Compact the contents.
    952  */
    953 
    954 int
    955 ext2fs_add_entry (struct vnode* dvp, struct ext2fs_direct *entry,
    956     const struct ufs_lookup_results *ulr)
    957 {
    958 	struct ext2fs_direct *ep, *nep;
    959 	struct inode *dp;
    960 	struct buf *bp;
    961 	u_int dsize;
    962 	int error, loc, newentrysize, spacefree;
    963 	char *dirbuf;
    964 
    965 	dp = VTOI(dvp);
    966 
    967 	/*
    968 	 * If ulr_count is non-zero, then namei found space
    969 	 * for the new entry in the range ulr_offset to
    970 	 * ulr_offset + ulr_count in the directory.
    971 	 * To use this space, we may have to compact the entries located
    972 	 * there, by copying them together towards the beginning of the
    973 	 * block, leaving the free space in one usable chunk at the end.
    974 	 */
    975 
    976 	/*
    977 	 * Get the block containing the space for the new directory entry.
    978 	 */
    979 	if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0)
    980 		return error;
    981 	/*
    982 	 * Find space for the new entry. In the simple case, the entry at
    983 	 * offset base will have the space. If it does not, then namei
    984 	 * arranged that compacting the region ulr_offset to
    985 	 * ulr_offset + ulr_count would yield the
    986 	 * space.
    987 	 */
    988 	ep = (struct ext2fs_direct *)dirbuf;
    989 	newentrysize = dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
    990 	spacefree = fs2h16(ep->e2d_reclen) - dsize;
    991 	for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count;) {
    992 		nep = (struct ext2fs_direct *)(dirbuf + loc);
    993 		if (ep->e2d_ino) {
    994 			/* trim the existing slot */
    995 			ep->e2d_reclen = h2fs16(dsize);
    996 			ep = (struct ext2fs_direct *)((char *)ep + dsize);
    997 		} else {
    998 			/* overwrite; nothing there; header is ours */
    999 			spacefree += dsize;
   1000 		}
   1001 		dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
   1002 		spacefree += fs2h16(nep->e2d_reclen) - dsize;
   1003 		loc += fs2h16(nep->e2d_reclen);
   1004 		memcpy((void *)ep, (void *)nep, dsize);
   1005 	}
   1006 	/*
   1007 	 * Update the pointer fields in the previous entry (if any),
   1008 	 * copy in the new entry, and write out the block.
   1009 	 */
   1010 	if (ep->e2d_ino == 0) {
   1011 #ifdef DIAGNOSTIC
   1012 		if (spacefree + dsize < newentrysize)
   1013 			panic("ext2fs_direnter: compact1");
   1014 #endif
   1015 		entry->e2d_reclen = h2fs16(spacefree + dsize);
   1016 	} else {
   1017 #ifdef DIAGNOSTIC
   1018 		if (spacefree < newentrysize) {
   1019 			printf("ext2fs_direnter: compact2 %u %u",
   1020 			    (u_int)spacefree, (u_int)newentrysize);
   1021 			panic("ext2fs_direnter: compact2");
   1022 		}
   1023 #endif
   1024 		entry->e2d_reclen = h2fs16(spacefree);
   1025 		ep->e2d_reclen = h2fs16(dsize);
   1026 		ep = (struct ext2fs_direct *)((char *)ep + dsize);
   1027 	}
   1028 	memcpy(ep, entry, (u_int)newentrysize);
   1029 	error = VOP_BWRITE(bp->b_vp, bp);
   1030 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1031 	return error;
   1032 }
   1033 
   1034 /*
   1035  * Remove a directory entry after a call to namei, using
   1036  * the auxiliary results it provided. The entry
   1037  * ulr_offset contains the offset into the directory of the
   1038  * entry to be eliminated.  The ulr_count field contains the
   1039  * size of the previous record in the directory.  If this
   1040  * is 0, the first entry is being deleted, so we need only
   1041  * zero the inode number to mark the entry as free.  If the
   1042  * entry is not the first in the directory, we must reclaim
   1043  * the space of the now empty record by adding the record size
   1044  * to the size of the previous entry.
   1045  */
   1046 int
   1047 ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr,
   1048 		 struct componentname *cnp)
   1049 {
   1050 	struct inode *dp;
   1051 	struct ext2fs_direct *ep;
   1052 	struct buf *bp;
   1053 	int error;
   1054 
   1055 	dp = VTOI(dvp);
   1056 
   1057 	if (ulr->ulr_count == 0) {
   1058 		/*
   1059 		 * First entry in block: set d_ino to zero.
   1060 		 */
   1061 		error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset,
   1062 		    (void *)&ep, &bp);
   1063 		if (error != 0)
   1064 			return error;
   1065 		ep->e2d_ino = 0;
   1066 		error = VOP_BWRITE(bp->b_vp, bp);
   1067 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1068 		return error;
   1069 	}
   1070 	/*
   1071 	 * Collapse new free space into previous entry.
   1072 	 */
   1073 	error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count),
   1074 	    (void *)&ep, &bp);
   1075 	if (error != 0)
   1076 		return error;
   1077 	ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen);
   1078 	error = VOP_BWRITE(bp->b_vp, bp);
   1079 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1080 	return error;
   1081 }
   1082 
   1083 /*
   1084  * Rewrite an existing directory entry to point at the inode
   1085  * supplied.  The parameters describing the directory entry are
   1086  * set up by a call to namei.
   1087  */
   1088 int
   1089 ext2fs_dirrewrite(struct inode *dp, const struct ufs_lookup_results *ulr,
   1090     struct inode *ip, struct componentname *cnp)
   1091 {
   1092 	struct buf *bp;
   1093 	struct ext2fs_direct *ep;
   1094 	struct vnode *vdp = ITOV(dp);
   1095 	int error;
   1096 
   1097 	error = ext2fs_blkatoff(vdp, (off_t)ulr->ulr_offset, (void *)&ep, &bp);
   1098 	if (error != 0)
   1099 		return error;
   1100 	ep->e2d_ino = h2fs32(ip->i_number);
   1101 	if (EXT2F_HAS_INCOMPAT_FEATURE(dp->i_e2fs, EXT2F_INCOMPAT_FTYPE)) {
   1102 		ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
   1103 	} else {
   1104 		ep->e2d_type = 0;
   1105 	}
   1106 	error = VOP_BWRITE(bp->b_vp, bp);
   1107 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1108 	return error;
   1109 }
   1110 
   1111 /*
   1112  * Check if a directory is empty or not.
   1113  * Inode supplied must be locked.
   1114  *
   1115  * Using a struct dirtemplate here is not precisely
   1116  * what we want, but better than using a struct ext2fs_direct.
   1117  *
   1118  * NB: does not handle corrupted directories.
   1119  */
   1120 int
   1121 ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
   1122 {
   1123 	off_t off;
   1124 	struct ext2fs_dirtemplate dbuf;
   1125 	struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
   1126 	int error, namlen;
   1127 	size_t count;
   1128 
   1129 #define	MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
   1130 
   1131 	for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
   1132 		error = ufs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
   1133 		    off, IO_NODELOCKED, cred, &count, NULL);
   1134 		/*
   1135 		 * Since we read MINDIRSIZ, residual must
   1136 		 * be 0 unless we're at end of file.
   1137 		 */
   1138 		if (error || count != 0)
   1139 			return 0;
   1140 		/* avoid infinite loops */
   1141 		if (dp->e2d_reclen == 0)
   1142 			return 0;
   1143 		/* skip empty entries */
   1144 		if (dp->e2d_ino == 0)
   1145 			continue;
   1146 		/* accept only "." and ".." */
   1147 		namlen = dp->e2d_namlen;
   1148 		if (namlen > 2)
   1149 			return 0;
   1150 		if (dp->e2d_name[0] != '.')
   1151 			return 0;
   1152 		/*
   1153 		 * At this point namlen must be 1 or 2.
   1154 		 * 1 implies ".", 2 implies ".." if second
   1155 		 * char is also "."
   1156 		 */
   1157 		if (namlen == 1)
   1158 			continue;
   1159 		if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
   1160 			continue;
   1161 		return 0;
   1162 	}
   1163 	return 1;
   1164 }
   1165