xref: /freebsd/sys/fs/ext2fs/ext2_lookup.c (revision b37f6c9805edb4b89f0a8c2b78f78a3dcfc0647b)
1 /*-
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * SPDX-License-Identifier: BSD-3-Clause
9  *
10  * Copyright (c) 1989, 1993
11  *	The Regents of the University of California.  All rights reserved.
12  * (c) UNIX System Laboratories, Inc.
13  * All or some portions of this file are derived from material licensed
14  * to the University of California by American Telephone and Telegraph
15  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16  * the permission of UNIX System Laboratories, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)ufs_lookup.c	8.6 (Berkeley) 4/1/94
43  * $FreeBSD$
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/endian.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/sysctl.h>
57 
58 #include <ufs/ufs/dir.h>
59 
60 #include <fs/ext2fs/inode.h>
61 #include <fs/ext2fs/ext2_mount.h>
62 #include <fs/ext2fs/ext2fs.h>
63 #include <fs/ext2fs/ext2_dinode.h>
64 #include <fs/ext2fs/ext2_dir.h>
65 #include <fs/ext2fs/ext2_extern.h>
66 #include <fs/ext2fs/fs.h>
67 
68 #ifdef INVARIANTS
69 static int dirchk = 1;
70 #else
71 static int dirchk = 0;
72 #endif
73 
74 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
75 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
76 
77 /*
78    DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
79    while it is the native blocksize in ext2fs - thus, a #define
80    is no longer appropriate
81 */
82 #undef  DIRBLKSIZ
83 
84 static u_char ext2_ft_to_dt[] = {
85 	DT_UNKNOWN,		/* EXT2_FT_UNKNOWN */
86 	DT_REG,			/* EXT2_FT_REG_FILE */
87 	DT_DIR,			/* EXT2_FT_DIR */
88 	DT_CHR,			/* EXT2_FT_CHRDEV */
89 	DT_BLK,			/* EXT2_FT_BLKDEV */
90 	DT_FIFO,		/* EXT2_FT_FIFO */
91 	DT_SOCK,		/* EXT2_FT_SOCK */
92 	DT_LNK,			/* EXT2_FT_SYMLINK */
93 };
94 #define	FTTODT(ft) \
95     ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
96 
97 static u_char dt_to_ext2_ft[] = {
98 	EXT2_FT_UNKNOWN,	/* DT_UNKNOWN */
99 	EXT2_FT_FIFO,		/* DT_FIFO */
100 	EXT2_FT_CHRDEV,		/* DT_CHR */
101 	EXT2_FT_UNKNOWN,	/* unused */
102 	EXT2_FT_DIR,		/* DT_DIR */
103 	EXT2_FT_UNKNOWN,	/* unused */
104 	EXT2_FT_BLKDEV,		/* DT_BLK */
105 	EXT2_FT_UNKNOWN,	/* unused */
106 	EXT2_FT_REG_FILE,	/* DT_REG */
107 	EXT2_FT_UNKNOWN,	/* unused */
108 	EXT2_FT_SYMLINK,	/* DT_LNK */
109 	EXT2_FT_UNKNOWN,	/* unused */
110 	EXT2_FT_SOCK,		/* DT_SOCK */
111 	EXT2_FT_UNKNOWN,	/* unused */
112 	EXT2_FT_UNKNOWN,	/* DT_WHT */
113 };
114 #define	DTTOFT(dt) \
115     ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
116 
117 static int	ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
118 		    int entryoffsetinblock);
119 static int	ext2_is_dot_entry(struct componentname *cnp);
120 static int	ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
121 		    struct componentname *cnp, ino_t *dd_ino);
122 
123 static int
124 ext2_is_dot_entry(struct componentname *cnp)
125 {
126 	if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
127 	    (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
128 		return (1);
129 	return (0);
130 }
131 
132 /*
133  * Vnode op for reading directories.
134  */
135 int
136 ext2_readdir(struct vop_readdir_args *ap)
137 {
138 	struct vnode *vp = ap->a_vp;
139 	struct uio *uio = ap->a_uio;
140 	struct buf *bp;
141 	struct inode *ip;
142 	struct ext2fs_direct_2 *dp, *edp;
143 	u_long *cookies;
144 	struct dirent dstdp;
145 	off_t offset, startoffset;
146 	size_t readcnt, skipcnt;
147 	ssize_t startresid;
148 	int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
149 	int error;
150 	u_int ncookies;
151 
152 	if (uio->uio_offset < 0)
153 		return (EINVAL);
154 	ip = VTOI(vp);
155 	if (ap->a_ncookies != NULL) {
156 		ncookies = uio->uio_resid;
157 		if (uio->uio_offset >= ip->i_size)
158 			ncookies = 0;
159 		else if (ip->i_size - uio->uio_offset < ncookies)
160 			ncookies = ip->i_size - uio->uio_offset;
161 		ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
162 		    e2d_namlen) + 4) + 1;
163 		cookies = mallocarray(ncookies, sizeof(*cookies), M_TEMP,
164 		    M_WAITOK);
165 		*ap->a_ncookies = ncookies;
166 		*ap->a_cookies = cookies;
167 	} else {
168 		ncookies = 0;
169 		cookies = NULL;
170 	}
171 	offset = startoffset = uio->uio_offset;
172 	startresid = uio->uio_resid;
173 	error = 0;
174 	while (error == 0 && uio->uio_resid > 0 &&
175 	    uio->uio_offset < ip->i_size) {
176 		error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
177 		if (error)
178 			break;
179 		if (bp->b_offset + bp->b_bcount > ip->i_size)
180 			readcnt = ip->i_size - bp->b_offset;
181 		else
182 			readcnt = bp->b_bcount;
183 		skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
184 		    ~(size_t)(DIRBLKSIZ - 1);
185 		offset = bp->b_offset + skipcnt;
186 		dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
187 		edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
188 		while (error == 0 && uio->uio_resid > 0 && dp < edp) {
189 			if (dp->e2d_reclen <= offsetof(struct ext2fs_direct_2,
190 			    e2d_namlen) || (caddr_t)dp + dp->e2d_reclen >
191 			    (caddr_t)edp) {
192 				error = EIO;
193 				break;
194 			}
195 			/*-
196 			 * "New" ext2fs directory entries differ in 3 ways
197 			 * from ufs on-disk ones:
198 			 * - the name is not necessarily NUL-terminated.
199 			 * - the file type field always exists and always
200 			 *   follows the name length field.
201 			 * - the file type is encoded in a different way.
202 			 *
203 			 * "Old" ext2fs directory entries need no special
204 			 * conversions, since they are binary compatible
205 			 * with "new" entries having a file type of 0 (i.e.,
206 			 * EXT2_FT_UNKNOWN).  Splitting the old name length
207 			 * field didn't make a mess like it did in ufs,
208 			 * because ext2fs uses a machine-independent disk
209 			 * layout.
210 			 */
211 			dstdp.d_namlen = dp->e2d_namlen;
212 			dstdp.d_type = FTTODT(dp->e2d_type);
213 			if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
214 			    dstdp.d_namlen > dp->e2d_reclen) {
215 				error = EIO;
216 				break;
217 			}
218 			if (offset < startoffset || dp->e2d_ino == 0)
219 				goto nextentry;
220 			dstdp.d_fileno = dp->e2d_ino;
221 			dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
222 			bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
223 			dstdp.d_name[dstdp.d_namlen] = '\0';
224 			if (dstdp.d_reclen > uio->uio_resid) {
225 				if (uio->uio_resid == startresid)
226 					error = EINVAL;
227 				else
228 					error = EJUSTRETURN;
229 				break;
230 			}
231 			/* Advance dp. */
232 			error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
233 			if (error)
234 				break;
235 			if (cookies != NULL) {
236 				KASSERT(ncookies > 0,
237 				    ("ext2_readdir: cookies buffer too small"));
238 				*cookies = offset + dp->e2d_reclen;
239 				cookies++;
240 				ncookies--;
241 			}
242 nextentry:
243 			offset += dp->e2d_reclen;
244 			dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
245 			    dp->e2d_reclen);
246 		}
247 		bqrelse(bp);
248 		uio->uio_offset = offset;
249 	}
250 	/* We need to correct uio_offset. */
251 	uio->uio_offset = offset;
252 	if (error == EJUSTRETURN)
253 		error = 0;
254 	if (ap->a_ncookies != NULL) {
255 		if (error == 0) {
256 			ap->a_ncookies -= ncookies;
257 		} else {
258 			free(*ap->a_cookies, M_TEMP);
259 			*ap->a_ncookies = 0;
260 			*ap->a_cookies = NULL;
261 		}
262 	}
263 	if (error == 0 && ap->a_eofflag)
264 		*ap->a_eofflag = ip->i_size <= uio->uio_offset;
265 	return (error);
266 }
267 
268 /*
269  * Convert a component of a pathname into a pointer to a locked inode.
270  * This is a very central and rather complicated routine.
271  * If the file system is not maintained in a strict tree hierarchy,
272  * this can result in a deadlock situation (see comments in code below).
273  *
274  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
275  * on whether the name is to be looked up, created, renamed, or deleted.
276  * When CREATE, RENAME, or DELETE is specified, information usable in
277  * creating, renaming, or deleting a directory entry may be calculated.
278  * If flag has LOCKPARENT or'ed into it and the target of the pathname
279  * exists, lookup returns both the target and its parent directory locked.
280  * When creating or renaming and LOCKPARENT is specified, the target may
281  * not be ".".  When deleting and LOCKPARENT is specified, the target may
282  * be "."., but the caller must check to ensure it does an vrele and vput
283  * instead of two vputs.
284  *
285  * Overall outline of ext2_lookup:
286  *
287  *	search for name in directory, to found or notfound
288  * notfound:
289  *	if creating, return locked directory, leaving info on available slots
290  *	else return error
291  * found:
292  *	if at end of path and deleting, return information to allow delete
293  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
294  *	  inode and return info to allow rewrite
295  *	if not at end, add name to cache; if at end and neither creating
296  *	  nor deleting, add name to cache
297  */
298 int
299 ext2_lookup(struct vop_cachedlookup_args *ap)
300 {
301 
302 	return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
303 }
304 
305 static int
306 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
307     ino_t *dd_ino)
308 {
309 	struct inode *dp;		/* inode for directory being searched */
310 	struct buf *bp;			/* a buffer of directory entries */
311 	struct ext2fs_direct_2 *ep;	/* the current directory entry */
312 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
313 	struct ext2fs_searchslot ss;
314 	doff_t i_diroff;		/* cached i_diroff value */
315 	doff_t i_offset;		/* cached i_offset value */
316 	int numdirpasses;		/* strategy for directory search */
317 	doff_t endsearch;		/* offset to end directory search */
318 	doff_t prevoff;			/* prev entry dp->i_offset */
319 	struct vnode *pdp;		/* saved dp during symlink work */
320 	struct vnode *tdp;		/* returned by VFS_VGET */
321 	doff_t enduseful;		/* pointer past last used dir slot */
322 	u_long bmask;			/* block offset mask */
323 	int error;
324 	struct ucred *cred = cnp->cn_cred;
325 	int flags = cnp->cn_flags;
326 	int nameiop = cnp->cn_nameiop;
327 	ino_t ino, ino1;
328 	int ltype;
329 	int entry_found = 0;
330 
331 	int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
332 
333 	if (vpp != NULL)
334 		*vpp = NULL;
335 
336 	dp = VTOI(vdp);
337 	bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
338 restart:
339 	bp = NULL;
340 	ss.slotoffset = -1;
341 
342 	/*
343 	 * We now have a segment name to search for, and a directory to search.
344 	 *
345 	 * Suppress search for slots unless creating
346 	 * file and at end of pathname, in which case
347 	 * we watch for a place to put the new file in
348 	 * case it doesn't already exist.
349 	 */
350 	i_diroff = dp->i_diroff;
351 	ss.slotstatus = FOUND;
352 	ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
353 	if ((nameiop == CREATE || nameiop == RENAME) &&
354 	    (flags & ISLASTCN)) {
355 		ss.slotstatus = NONE;
356 		ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
357 		/*
358 		 * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
359 		 * cnp->cn_namelen + 3) &~ 3;
360 		 */
361 	}
362 	/*
363 	 * Try to lookup dir entry using htree directory index.
364 	 *
365 	 * If we got an error or we want to find '.' or '..' entry,
366 	 * we will fall back to linear search.
367 	 */
368 	if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
369 		numdirpasses = 1;
370 		entryoffsetinblock = 0;
371 		switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
372 		    &bp, &entryoffsetinblock, &i_offset, &prevoff,
373 		    &enduseful, &ss)) {
374 		case 0:
375 			ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
376 			    (i_offset & bmask));
377 			goto foundentry;
378 		case ENOENT:
379 			i_offset = roundup2(dp->i_size, DIRBLKSIZ);
380 			goto notfound;
381 		default:
382 			/*
383 			 * Something failed; just fallback to do a linear
384 			 * search.
385 			 */
386 			break;
387 		}
388 	}
389 
390 	/*
391 	 * If there is cached information on a previous search of
392 	 * this directory, pick up where we last left off.
393 	 * We cache only lookups as these are the most common
394 	 * and have the greatest payoff. Caching CREATE has little
395 	 * benefit as it usually must search the entire directory
396 	 * to determine that the entry does not exist. Caching the
397 	 * location of the last DELETE or RENAME has not reduced
398 	 * profiling time and hence has been removed in the interest
399 	 * of simplicity.
400 	 */
401 	if (nameiop != LOOKUP || i_diroff == 0 ||
402 	    i_diroff > dp->i_size) {
403 		entryoffsetinblock = 0;
404 		i_offset = 0;
405 		numdirpasses = 1;
406 	} else {
407 		i_offset = i_diroff;
408 		if ((entryoffsetinblock = i_offset & bmask) &&
409 		    (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
410 		    &bp)))
411 			return (error);
412 		numdirpasses = 2;
413 		nchstats.ncs_2passes++;
414 	}
415 	prevoff = i_offset;
416 	endsearch = roundup2(dp->i_size, DIRBLKSIZ);
417 	enduseful = 0;
418 
419 searchloop:
420 	while (i_offset < endsearch) {
421 		/*
422 		 * If necessary, get the next directory block.
423 		 */
424 		if (bp != NULL)
425 			brelse(bp);
426 		error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
427 		if (error != 0)
428 			return (error);
429 		entryoffsetinblock = 0;
430 		/*
431 		 * If still looking for a slot, and at a DIRBLKSIZE
432 		 * boundary, have to start looking for free space again.
433 		 */
434 		if (ss.slotstatus == NONE &&
435 		    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
436 			ss.slotoffset = -1;
437 			ss.slotfreespace = 0;
438 		}
439 		error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
440 		    cnp->cn_nameptr, cnp->cn_namelen,
441 		    &entryoffsetinblock, &i_offset, &prevoff,
442 		    &enduseful, &ss);
443 		if (error != 0) {
444 			brelse(bp);
445 			return (error);
446 		}
447 		if (entry_found) {
448 			ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
449 			    (entryoffsetinblock & bmask));
450 foundentry:
451 			ino = ep->e2d_ino;
452 			goto found;
453 		}
454 	}
455 notfound:
456 	/*
457 	 * If we started in the middle of the directory and failed
458 	 * to find our target, we must check the beginning as well.
459 	 */
460 	if (numdirpasses == 2) {
461 		numdirpasses--;
462 		i_offset = 0;
463 		endsearch = i_diroff;
464 		goto searchloop;
465 	}
466 	if (bp != NULL)
467 		brelse(bp);
468 	/*
469 	 * If creating, and at end of pathname and current
470 	 * directory has not been removed, then can consider
471 	 * allowing file to be created.
472 	 */
473 	if ((nameiop == CREATE || nameiop == RENAME) &&
474 	    (flags & ISLASTCN) && dp->i_nlink != 0) {
475 		/*
476 		 * Access for write is interpreted as allowing
477 		 * creation of files in the directory.
478 		 */
479 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
480 			return (error);
481 		/*
482 		 * Return an indication of where the new directory
483 		 * entry should be put.  If we didn't find a slot,
484 		 * then set dp->i_count to 0 indicating
485 		 * that the new slot belongs at the end of the
486 		 * directory. If we found a slot, then the new entry
487 		 * can be put in the range from dp->i_offset to
488 		 * dp->i_offset + dp->i_count.
489 		 */
490 		if (ss.slotstatus == NONE) {
491 			dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
492 			dp->i_count = 0;
493 			enduseful = dp->i_offset;
494 		} else {
495 			dp->i_offset = ss.slotoffset;
496 			dp->i_count = ss.slotsize;
497 			if (enduseful < ss.slotoffset + ss.slotsize)
498 				enduseful = ss.slotoffset + ss.slotsize;
499 		}
500 		dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
501 		/*
502 		 * We return with the directory locked, so that
503 		 * the parameters we set up above will still be
504 		 * valid if we actually decide to do a direnter().
505 		 * We return ni_vp == NULL to indicate that the entry
506 		 * does not currently exist; we leave a pointer to
507 		 * the (locked) directory inode in ndp->ni_dvp.
508 		 * The pathname buffer is saved so that the name
509 		 * can be obtained later.
510 		 *
511 		 * NB - if the directory is unlocked, then this
512 		 * information cannot be used.
513 		 */
514 		cnp->cn_flags |= SAVENAME;
515 		return (EJUSTRETURN);
516 	}
517 	/*
518 	 * Insert name into cache (as non-existent) if appropriate.
519 	 */
520 	if ((cnp->cn_flags & MAKEENTRY) != 0)
521 		cache_enter(vdp, NULL, cnp);
522 	return (ENOENT);
523 
524 found:
525 	if (dd_ino != NULL)
526 		*dd_ino = ino;
527 	if (numdirpasses == 2)
528 		nchstats.ncs_pass2++;
529 	/*
530 	 * Check that directory length properly reflects presence
531 	 * of this entry.
532 	 */
533 	if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
534 	    > dp->i_size) {
535 		ext2_dirbad(dp, i_offset, "i_size too small");
536 		dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
537 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
538 	}
539 	brelse(bp);
540 
541 	/*
542 	 * Found component in pathname.
543 	 * If the final component of path name, save information
544 	 * in the cache as to where the entry was found.
545 	 */
546 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
547 		dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
548 	/*
549 	 * If deleting, and at end of pathname, return
550 	 * parameters which can be used to remove file.
551 	 */
552 	if (nameiop == DELETE && (flags & ISLASTCN)) {
553 		if (flags & LOCKPARENT)
554 			ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
555 		/*
556 		 * Write access to directory required to delete files.
557 		 */
558 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
559 			return (error);
560 		/*
561 		 * Return pointer to current entry in dp->i_offset,
562 		 * and distance past previous entry (if there
563 		 * is a previous entry in this block) in dp->i_count.
564 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
565 		 *
566 		 * Technically we shouldn't be setting these in the
567 		 * WANTPARENT case (first lookup in rename()), but any
568 		 * lookups that will result in directory changes will
569 		 * overwrite these.
570 		 */
571 		dp->i_offset = i_offset;
572 		if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
573 			dp->i_count = 0;
574 		else
575 			dp->i_count = dp->i_offset - prevoff;
576 		if (dd_ino != NULL)
577 			return (0);
578 		if (dp->i_number == ino) {
579 			VREF(vdp);
580 			*vpp = vdp;
581 			return (0);
582 		}
583 		if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
584 		    &tdp)) != 0)
585 			return (error);
586 		/*
587 		 * If directory is "sticky", then user must own
588 		 * the directory, or the file in it, else she
589 		 * may not delete it (unless she's root). This
590 		 * implements append-only directories.
591 		 */
592 		if ((dp->i_mode & ISVTX) &&
593 		    cred->cr_uid != 0 &&
594 		    cred->cr_uid != dp->i_uid &&
595 		    VTOI(tdp)->i_uid != cred->cr_uid) {
596 			vput(tdp);
597 			return (EPERM);
598 		}
599 		*vpp = tdp;
600 		return (0);
601 	}
602 
603 	/*
604 	 * If rewriting (RENAME), return the inode and the
605 	 * information required to rewrite the present directory
606 	 * Must get inode of directory entry to verify it's a
607 	 * regular file, or empty directory.
608 	 */
609 	if (nameiop == RENAME && (flags & ISLASTCN)) {
610 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
611 			return (error);
612 		/*
613 		 * Careful about locking second inode.
614 		 * This can only occur if the target is ".".
615 		 */
616 		dp->i_offset = i_offset;
617 		if (dp->i_number == ino)
618 			return (EISDIR);
619 		if (dd_ino != NULL)
620 			return (0);
621 		if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
622 		    &tdp)) != 0)
623 			return (error);
624 		*vpp = tdp;
625 		cnp->cn_flags |= SAVENAME;
626 		return (0);
627 	}
628 	if (dd_ino != NULL)
629 		return (0);
630 
631 	/*
632 	 * Step through the translation in the name.  We do not `vput' the
633 	 * directory because we may need it again if a symbolic link
634 	 * is relative to the current directory.  Instead we save it
635 	 * unlocked as "pdp".  We must get the target inode before unlocking
636 	 * the directory to insure that the inode will not be removed
637 	 * before we get it.  We prevent deadlock by always fetching
638 	 * inodes from the root, moving down the directory tree. Thus
639 	 * when following backward pointers ".." we must unlock the
640 	 * parent directory before getting the requested directory.
641 	 * There is a potential race condition here if both the current
642 	 * and parent directories are removed before the VFS_VGET for the
643 	 * inode associated with ".." returns.  We hope that this occurs
644 	 * infrequently since we cannot avoid this race condition without
645 	 * implementing a sophisticated deadlock detection algorithm.
646 	 * Note also that this simple deadlock detection scheme will not
647 	 * work if the file system has any hard links other than ".."
648 	 * that point backwards in the directory structure.
649 	 */
650 	pdp = vdp;
651 	if (flags & ISDOTDOT) {
652 		error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
653 		if (pdp->v_iflag & VI_DOOMED) {
654 			if (error == 0)
655 				vput(tdp);
656 			error = ENOENT;
657 		}
658 		if (error)
659 			return (error);
660 		/*
661 		 * Recheck that ".." entry in the vdp directory points
662 		 * to the inode we looked up before vdp lock was
663 		 * dropped.
664 		 */
665 		error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
666 		if (error) {
667 			vput(tdp);
668 			return (error);
669 		}
670 		if (ino1 != ino) {
671 			vput(tdp);
672 			goto restart;
673 		}
674 		*vpp = tdp;
675 	} else if (dp->i_number == ino) {
676 		VREF(vdp);	/* we want ourself, ie "." */
677 		/*
678 		 * When we lookup "." we still can be asked to lock it
679 		 * differently.
680 		 */
681 		ltype = cnp->cn_lkflags & LK_TYPE_MASK;
682 		if (ltype != VOP_ISLOCKED(vdp)) {
683 			if (ltype == LK_EXCLUSIVE)
684 				vn_lock(vdp, LK_UPGRADE | LK_RETRY);
685 			else	/* if (ltype == LK_SHARED) */
686 				vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
687 		}
688 		*vpp = vdp;
689 	} else {
690 		if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
691 		    &tdp)) != 0)
692 			return (error);
693 		*vpp = tdp;
694 	}
695 
696 	/*
697 	 * Insert name into cache if appropriate.
698 	 */
699 	if (cnp->cn_flags & MAKEENTRY)
700 		cache_enter(vdp, *vpp, cnp);
701 	return (0);
702 }
703 
704 int
705 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
706     const char *name, int namelen, int *entryoffsetinblockp,
707     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
708     struct ext2fs_searchslot *ssp)
709 {
710 	struct vnode *vdp;
711 	struct ext2fs_direct_2 *ep, *top;
712 	uint32_t bsize = ip->i_e2fs->e2fs_bsize;
713 	int offset = *entryoffsetinblockp;
714 	int namlen;
715 
716 	vdp = ITOV(ip);
717 
718 	ep = (struct ext2fs_direct_2 *)((char *)data + offset);
719 	top = (struct ext2fs_direct_2 *)((char *)data +
720 	    bsize - EXT2_DIR_REC_LEN(0));
721 
722 	while (ep < top) {
723 		/*
724 		 * Full validation checks are slow, so we only check
725 		 * enough to insure forward progress through the
726 		 * directory. Complete checks can be run by setting
727 		 * "vfs.e2fs.dirchk" to be true.
728 		 */
729 		if (ep->e2d_reclen == 0 ||
730 		    (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
731 			int i;
732 
733 			ext2_dirbad(ip, *offp, "mangled entry");
734 			i = bsize - (offset & (bsize - 1));
735 			*offp += i;
736 			offset += i;
737 			continue;
738 		}
739 
740 		/*
741 		 * If an appropriate sized slot has not yet been found,
742 		 * check to see if one is available. Also accumulate space
743 		 * in the current block so that we can determine if
744 		 * compaction is viable.
745 		 */
746 		if (ssp->slotstatus != FOUND) {
747 			int size = ep->e2d_reclen;
748 
749 			if (ep->e2d_ino != 0)
750 				size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
751 			if (size > 0) {
752 				if (size >= ssp->slotneeded) {
753 					ssp->slotstatus = FOUND;
754 					ssp->slotoffset = *offp;
755 					ssp->slotsize = ep->e2d_reclen;
756 				} else if (ssp->slotstatus == NONE) {
757 					ssp->slotfreespace += size;
758 					if (ssp->slotoffset == -1)
759 						ssp->slotoffset = *offp;
760 					if (ssp->slotfreespace >= ssp->slotneeded) {
761 						ssp->slotstatus = COMPACT;
762 						ssp->slotsize = *offp +
763 						    ep->e2d_reclen -
764 						    ssp->slotoffset;
765 					}
766 				}
767 			}
768 		}
769 		/*
770 		 * Check for a name match.
771 		 */
772 		if (ep->e2d_ino) {
773 			namlen = ep->e2d_namlen;
774 			if (namlen == namelen &&
775 			    !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
776 				/*
777 				 * Save directory entry's inode number and
778 				 * reclen in ndp->ni_ufs area, and release
779 				 * directory buffer.
780 				 */
781 				*foundp = 1;
782 				return (0);
783 			}
784 		}
785 		*prevoffp = *offp;
786 		*offp += ep->e2d_reclen;
787 		offset += ep->e2d_reclen;
788 		*entryoffsetinblockp = offset;
789 		if (ep->e2d_ino)
790 			*endusefulp = *offp;
791 		/*
792 		 * Get pointer to the next entry.
793 		 */
794 		ep = (struct ext2fs_direct_2 *)((char *)data + offset);
795 	}
796 
797 	return (0);
798 }
799 
800 void
801 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
802 {
803 	struct mount *mp;
804 
805 	mp = ITOV(ip)->v_mount;
806 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
807 		panic("ext2_dirbad: %s: bad dir ino %ju at offset %ld: %s\n",
808 		    mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
809 		    (long)offset, how);
810 	else
811 		(void)printf("%s: bad dir ino %ju at offset %ld: %s\n",
812 		    mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
813 		    (long)offset, how);
814 
815 }
816 
817 /*
818  * Do consistency checking on a directory entry:
819  *	record length must be multiple of 4
820  *	entry must fit in rest of its DIRBLKSIZ block
821  *	record must be large enough to contain entry
822  *	name is not longer than MAXNAMLEN
823  *	name must be as long as advertised, and null terminated
824  */
825 /*
826  *	changed so that it confirms to ext2_check_dir_entry
827  */
828 static int
829 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
830     int entryoffsetinblock)
831 {
832 	int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
833 
834 	char *error_msg = NULL;
835 
836 	if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
837 		error_msg = "rec_len is smaller than minimal";
838 	else if (de->e2d_reclen % 4 != 0)
839 		error_msg = "rec_len % 4 != 0";
840 	else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
841 		error_msg = "reclen is too small for name_len";
842 	else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
843 		error_msg = "directory entry across blocks";
844 	/* else LATER
845 	     if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
846 		error_msg = "inode out of bounds";
847 	*/
848 
849 	if (error_msg != NULL) {
850 		printf("bad directory entry: %s\n", error_msg);
851 		printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
852 			entryoffsetinblock, (unsigned long)de->e2d_ino,
853 			de->e2d_reclen, de->e2d_namlen);
854 	}
855 	return error_msg == NULL ? 0 : 1;
856 }
857 
858 /*
859  * Write a directory entry after a call to namei, using the parameters
860  * that it left in nameidata.  The argument ip is the inode which the new
861  * directory entry will refer to.  Dvp is a pointer to the directory to
862  * be written, which was left locked by namei. Remaining parameters
863  * (dp->i_offset, dp->i_count) indicate how the space for the new
864  * entry is to be obtained.
865  */
866 int
867 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
868 {
869 	struct inode *dp;
870 	struct ext2fs_direct_2 newdir;
871 	struct buf *bp;
872 	int error, newentrysize;
873 	int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
874 
875 
876 #ifdef INVARIANTS
877 	if ((cnp->cn_flags & SAVENAME) == 0)
878 		panic("ext2_direnter: missing name");
879 #endif
880 	dp = VTOI(dvp);
881 	newdir.e2d_ino = ip->i_number;
882 	newdir.e2d_namlen = cnp->cn_namelen;
883 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
884 	    EXT2F_INCOMPAT_FTYPE))
885 		newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
886 	else
887 		newdir.e2d_type = EXT2_FT_UNKNOWN;
888 	bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
889 	newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen);
890 
891 	if (ext2_htree_has_idx(dp)) {
892 		error = ext2_htree_add_entry(dvp, &newdir, cnp);
893 		if (error) {
894 			dp->i_flag &= ~IN_E3INDEX;
895 			dp->i_flag |= IN_CHANGE | IN_UPDATE;
896 		}
897 		return (error);
898 	}
899 
900 	if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
901 	    !ext2_htree_has_idx(dp)) {
902 		if ((dp->i_size / DIRBLKSIZ) == 1 &&
903 		    dp->i_offset == DIRBLKSIZ) {
904 			/*
905 			 * Making indexed directory when one block is not
906 			 * enough to save all entries.
907 			 */
908 			return ext2_htree_create_index(dvp, cnp, &newdir);
909 		}
910 	}
911 
912 	if (dp->i_count == 0) {
913 		/*
914 		 * If dp->i_count is 0, then namei could find no
915 		 * space in the directory. Here, dp->i_offset will
916 		 * be on a directory block boundary and we will write the
917 		 * new entry into a fresh block.
918 		 */
919 		if (dp->i_offset & (DIRBLKSIZ - 1))
920 			panic("ext2_direnter: newblk");
921 
922 		newdir.e2d_reclen = DIRBLKSIZ;
923 
924 		bp = getblk(ip->i_devvp, lblkno(dp->i_e2fs, dp->i_offset),
925 		    DIRBLKSIZ, 0, 0, 0);
926 		if (!bp)
927 			return (EIO);
928 
929 		memcpy(bp->b_data, &newdir, sizeof(struct ext2fs_direct_2));
930 
931 		ext2_dir_blk_csum_set(dp, bp);
932 		error = bwrite(bp);
933 		if (error)
934 			return (error);
935 
936 		dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
937 		dp->i_flag |= IN_CHANGE;
938 
939 		return (0);
940 	}
941 
942 	error = ext2_add_entry(dvp, &newdir);
943 	if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
944 		error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
945 		    cnp->cn_cred, cnp->cn_thread);
946 	return (error);
947 }
948 
949 /*
950  * Insert an entry into the directory block.
951  * Compact the contents.
952  */
953 int
954 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
955 {
956 	struct ext2fs_direct_2 *ep, *nep;
957 	struct inode *dp;
958 	struct buf *bp;
959 	u_int dsize;
960 	int error, loc, newentrysize, spacefree;
961 	char *dirbuf;
962 
963 	dp = VTOI(dvp);
964 
965 	/*
966 	 * If dp->i_count is non-zero, then namei found space
967 	 * for the new entry in the range dp->i_offset to
968 	 * dp->i_offset + dp->i_count in the directory.
969 	 * To use this space, we may have to compact the entries located
970 	 * there, by copying them together towards the beginning of the
971 	 * block, leaving the free space in one usable chunk at the end.
972 	 */
973 
974 	/*
975 	 * Increase size of directory if entry eats into new space.
976 	 * This should never push the size past a new multiple of
977 	 * DIRBLKSIZE.
978 	 *
979 	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
980 	 */
981 	if (dp->i_offset + dp->i_count > dp->i_size)
982 		dp->i_size = dp->i_offset + dp->i_count;
983 	/*
984 	 * Get the block containing the space for the new directory entry.
985 	 */
986 	if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
987 	    &bp)) != 0)
988 		return (error);
989 	/*
990 	 * Find space for the new entry. In the simple case, the entry at
991 	 * offset base will have the space. If it does not, then namei
992 	 * arranged that compacting the region dp->i_offset to
993 	 * dp->i_offset + dp->i_count would yield the
994 	 * space.
995 	 */
996 	newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
997 	ep = (struct ext2fs_direct_2 *)dirbuf;
998 	dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
999 	spacefree = ep->e2d_reclen - dsize;
1000 	for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
1001 		nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1002 		if (ep->e2d_ino) {
1003 			/* trim the existing slot */
1004 			ep->e2d_reclen = dsize;
1005 			ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1006 		} else {
1007 			/* overwrite; nothing there; header is ours */
1008 			spacefree += dsize;
1009 		}
1010 		dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1011 		spacefree += nep->e2d_reclen - dsize;
1012 		loc += nep->e2d_reclen;
1013 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1014 	}
1015 	/*
1016 	 * Update the pointer fields in the previous entry (if any),
1017 	 * copy in the new entry, and write out the block.
1018 	 */
1019 	if (ep->e2d_ino == 0) {
1020 		if (spacefree + dsize < newentrysize)
1021 			panic("ext2_direnter: compact1");
1022 		entry->e2d_reclen = spacefree + dsize;
1023 	} else {
1024 		if (spacefree < newentrysize)
1025 			panic("ext2_direnter: compact2");
1026 		entry->e2d_reclen = spacefree;
1027 		ep->e2d_reclen = dsize;
1028 		ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1029 	}
1030 	bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1031 	ext2_dir_blk_csum_set(dp, bp);
1032 	if (DOINGASYNC(dvp)) {
1033 		bdwrite(bp);
1034 		error = 0;
1035 	} else {
1036 		error = bwrite(bp);
1037 	}
1038 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1039 	return (error);
1040 }
1041 
1042 /*
1043  * Remove a directory entry after a call to namei, using
1044  * the parameters which it left in nameidata. The entry
1045  * dp->i_offset contains the offset into the directory of the
1046  * entry to be eliminated.  The dp->i_count field contains the
1047  * size of the previous record in the directory.  If this
1048  * is 0, the first entry is being deleted, so we need only
1049  * zero the inode number to mark the entry as free.  If the
1050  * entry is not the first in the directory, we must reclaim
1051  * the space of the now empty record by adding the record size
1052  * to the size of the previous entry.
1053  */
1054 int
1055 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1056 {
1057 	struct inode *dp;
1058 	struct ext2fs_direct_2 *ep, *rep;
1059 	struct buf *bp;
1060 	int error;
1061 
1062 	dp = VTOI(dvp);
1063 	if (dp->i_count == 0) {
1064 		/*
1065 		 * First entry in block: set d_ino to zero.
1066 		 */
1067 		if ((error =
1068 		    ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1069 		    &bp)) != 0)
1070 			return (error);
1071 		ep->e2d_ino = 0;
1072 		error = bwrite(bp);
1073 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
1074 		return (error);
1075 	}
1076 	/*
1077 	 * Collapse new free space into previous entry.
1078 	 */
1079 	if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1080 	    (char **)&ep, &bp)) != 0)
1081 		return (error);
1082 
1083 	/* Set 'rep' to the entry being removed. */
1084 	if (dp->i_count == 0)
1085 		rep = ep;
1086 	else
1087 		rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
1088 	ep->e2d_reclen += rep->e2d_reclen;
1089 	ext2_dir_blk_csum_set(dp, bp);
1090 	if (DOINGASYNC(dvp) && dp->i_count != 0)
1091 		bdwrite(bp);
1092 	else
1093 		error = bwrite(bp);
1094 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1095 	return (error);
1096 }
1097 
1098 /*
1099  * Rewrite an existing directory entry to point at the inode
1100  * supplied.  The parameters describing the directory entry are
1101  * set up by a call to namei.
1102  */
1103 int
1104 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1105 {
1106 	struct buf *bp;
1107 	struct ext2fs_direct_2 *ep;
1108 	struct vnode *vdp = ITOV(dp);
1109 	int error;
1110 
1111 	if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1112 	    &bp)) != 0)
1113 		return (error);
1114 	ep->e2d_ino = ip->i_number;
1115 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1116 	    EXT2F_INCOMPAT_FTYPE))
1117 		ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1118 	else
1119 		ep->e2d_type = EXT2_FT_UNKNOWN;
1120 	ext2_dir_blk_csum_set(dp, bp);
1121 	error = bwrite(bp);
1122 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1123 	return (error);
1124 }
1125 
1126 /*
1127  * Check if a directory is empty or not.
1128  * Inode supplied must be locked.
1129  *
1130  * Using a struct dirtemplate here is not precisely
1131  * what we want, but better than using a struct direct.
1132  *
1133  * NB: does not handle corrupted directories.
1134  */
1135 int
1136 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1137 {
1138 	off_t off;
1139 	struct dirtemplate dbuf;
1140 	struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1141 	int error, namlen;
1142 	ssize_t count;
1143 #define	MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1144 
1145 	for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
1146 		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1147 		    off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1148 		    NOCRED, &count, (struct thread *)0);
1149 		/*
1150 		 * Since we read MINDIRSIZ, residual must
1151 		 * be 0 unless we're at end of file.
1152 		 */
1153 		if (error || count != 0)
1154 			return (0);
1155 		/* avoid infinite loops */
1156 		if (dp->e2d_reclen == 0)
1157 			return (0);
1158 		/* skip empty entries */
1159 		if (dp->e2d_ino == 0)
1160 			continue;
1161 		/* accept only "." and ".." */
1162 		namlen = dp->e2d_namlen;
1163 		if (namlen > 2)
1164 			return (0);
1165 		if (dp->e2d_name[0] != '.')
1166 			return (0);
1167 		/*
1168 		 * At this point namlen must be 1 or 2.
1169 		 * 1 implies ".", 2 implies ".." if second
1170 		 * char is also "."
1171 		 */
1172 		if (namlen == 1)
1173 			continue;
1174 		if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
1175 			continue;
1176 		return (0);
1177 	}
1178 	return (1);
1179 }
1180 
1181 /*
1182  * Check if source directory is in the path of the target directory.
1183  * Target is supplied locked, source is unlocked.
1184  * The target is always vput before returning.
1185  */
1186 int
1187 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1188 {
1189 	struct vnode *vp;
1190 	int error, namlen;
1191 	struct dirtemplate dirbuf;
1192 
1193 	vp = ITOV(target);
1194 	if (target->i_number == source->i_number) {
1195 		error = EEXIST;
1196 		goto out;
1197 	}
1198 	if (target->i_number == EXT2_ROOTINO) {
1199 		error = 0;
1200 		goto out;
1201 	}
1202 
1203 	for (;;) {
1204 		if (vp->v_type != VDIR) {
1205 			error = ENOTDIR;
1206 			break;
1207 		}
1208 		error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1209 		    sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1210 		    IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1211 		    NULL);
1212 		if (error != 0)
1213 			break;
1214 		namlen = dirbuf.dotdot_type;	/* like ufs little-endian */
1215 		if (namlen != 2 ||
1216 		    dirbuf.dotdot_name[0] != '.' ||
1217 		    dirbuf.dotdot_name[1] != '.') {
1218 			error = ENOTDIR;
1219 			break;
1220 		}
1221 		if (dirbuf.dotdot_ino == source->i_number) {
1222 			error = EINVAL;
1223 			break;
1224 		}
1225 		if (dirbuf.dotdot_ino == EXT2_ROOTINO)
1226 			break;
1227 		vput(vp);
1228 		if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1229 		    LK_EXCLUSIVE, &vp)) != 0) {
1230 			vp = NULL;
1231 			break;
1232 		}
1233 	}
1234 
1235 out:
1236 	if (error == ENOTDIR)
1237 		printf("checkpath: .. not a directory\n");
1238 	if (vp != NULL)
1239 		vput(vp);
1240 	return (error);
1241 }
1242