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