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