xref: /freebsd-src/sys/fs/ext2fs/ext2_lookup.c (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
1e09c00caSUlf Lilleengen /*-
2e09c00caSUlf Lilleengen  *  modified for Lites 1.1
3e09c00caSUlf Lilleengen  *
4e09c00caSUlf Lilleengen  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5e09c00caSUlf Lilleengen  *  University of Utah, Department of Computer Science
6e09c00caSUlf Lilleengen  */
7e09c00caSUlf Lilleengen /*-
851369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
951369649SPedro F. Giffuni  *
10e09c00caSUlf Lilleengen  * Copyright (c) 1989, 1993
11e09c00caSUlf Lilleengen  *	The Regents of the University of California.  All rights reserved.
12e09c00caSUlf Lilleengen  * (c) UNIX System Laboratories, Inc.
13e09c00caSUlf Lilleengen  * All or some portions of this file are derived from material licensed
14e09c00caSUlf Lilleengen  * to the University of California by American Telephone and Telegraph
15e09c00caSUlf Lilleengen  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16e09c00caSUlf Lilleengen  * the permission of UNIX System Laboratories, Inc.
17e09c00caSUlf Lilleengen  *
18e09c00caSUlf Lilleengen  * Redistribution and use in source and binary forms, with or without
19e09c00caSUlf Lilleengen  * modification, are permitted provided that the following conditions
20e09c00caSUlf Lilleengen  * are met:
21e09c00caSUlf Lilleengen  * 1. Redistributions of source code must retain the above copyright
22e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer.
23e09c00caSUlf Lilleengen  * 2. Redistributions in binary form must reproduce the above copyright
24e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer in the
25e09c00caSUlf Lilleengen  *    documentation and/or other materials provided with the distribution.
26fca15474SPedro F. Giffuni  * 3. Neither the name of the University nor the names of its contributors
27e09c00caSUlf Lilleengen  *    may be used to endorse or promote products derived from this software
28e09c00caSUlf Lilleengen  *    without specific prior written permission.
29e09c00caSUlf Lilleengen  *
30e09c00caSUlf Lilleengen  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31e09c00caSUlf Lilleengen  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32e09c00caSUlf Lilleengen  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33e09c00caSUlf Lilleengen  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34e09c00caSUlf Lilleengen  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35e09c00caSUlf Lilleengen  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36e09c00caSUlf Lilleengen  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37e09c00caSUlf Lilleengen  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38e09c00caSUlf Lilleengen  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39e09c00caSUlf Lilleengen  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40e09c00caSUlf Lilleengen  * SUCH DAMAGE.
41e09c00caSUlf Lilleengen  */
42e09c00caSUlf Lilleengen 
43e09c00caSUlf Lilleengen #include <sys/param.h>
44e09c00caSUlf Lilleengen #include <sys/systm.h>
45e09c00caSUlf Lilleengen #include <sys/namei.h>
46e09c00caSUlf Lilleengen #include <sys/bio.h>
47e09c00caSUlf Lilleengen #include <sys/buf.h>
48e09c00caSUlf Lilleengen #include <sys/endian.h>
49e09c00caSUlf Lilleengen #include <sys/mount.h>
50e09c00caSUlf Lilleengen #include <sys/vnode.h>
51e09c00caSUlf Lilleengen #include <sys/malloc.h>
52e09c00caSUlf Lilleengen #include <sys/dirent.h>
53ebc94b66SFedor Uporov #include <sys/sdt.h>
54e09c00caSUlf Lilleengen #include <sys/sysctl.h>
55e09c00caSUlf Lilleengen 
56e09c00caSUlf Lilleengen #include <ufs/ufs/dir.h>
57e09c00caSUlf Lilleengen 
58e06e5241SFedor Uporov #include <fs/ext2fs/fs.h>
59e09c00caSUlf Lilleengen #include <fs/ext2fs/inode.h>
60e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_mount.h>
61e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2fs.h>
62bbfe24fbSJohn Baldwin #include <fs/ext2fs/ext2_dinode.h>
63e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_dir.h>
64bbfe24fbSJohn Baldwin #include <fs/ext2fs/ext2_extern.h>
65512f29d1SFedor Uporov #include <fs/ext2fs/fs.h>
66e09c00caSUlf Lilleengen 
67ebc94b66SFedor Uporov SDT_PROVIDER_DECLARE(ext2fs);
68ebc94b66SFedor Uporov /*
69ebc94b66SFedor Uporov  * ext2fs trace probe:
70ebc94b66SFedor Uporov  * arg0: verbosity. Higher numbers give more verbose messages
71ebc94b66SFedor Uporov  * arg1: Textual message
72ebc94b66SFedor Uporov  */
73ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , lookup, trace, "int", "char*");
74ebc94b66SFedor Uporov SDT_PROBE_DEFINE4(ext2fs, , trace, ext2_dirbad_error,
75ebc94b66SFedor Uporov     "char*", "ino_t", "doff_t", "char*");
76ebc94b66SFedor Uporov SDT_PROBE_DEFINE5(ext2fs, , trace, ext2_dirbadentry_error,
77ebc94b66SFedor Uporov     "char*", "int", "uint32_t", "uint16_t", "uint8_t");
78ebc94b66SFedor Uporov 
797029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
807029da5cSPawel Biernacki     "EXT2FS filesystem");
81e09c00caSUlf Lilleengen 
82e09c00caSUlf Lilleengen /*
83e09c00caSUlf Lilleengen    DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
84e09c00caSUlf Lilleengen    while it is the native blocksize in ext2fs - thus, a #define
85e09c00caSUlf Lilleengen    is no longer appropriate
86e09c00caSUlf Lilleengen */
87e09c00caSUlf Lilleengen #undef  DIRBLKSIZ
88e09c00caSUlf Lilleengen 
89e09c00caSUlf Lilleengen static u_char ext2_ft_to_dt[] = {
90e09c00caSUlf Lilleengen 	DT_UNKNOWN,		/* EXT2_FT_UNKNOWN */
91e09c00caSUlf Lilleengen 	DT_REG,			/* EXT2_FT_REG_FILE */
92e09c00caSUlf Lilleengen 	DT_DIR,			/* EXT2_FT_DIR */
93e09c00caSUlf Lilleengen 	DT_CHR,			/* EXT2_FT_CHRDEV */
94e09c00caSUlf Lilleengen 	DT_BLK,			/* EXT2_FT_BLKDEV */
95e09c00caSUlf Lilleengen 	DT_FIFO,		/* EXT2_FT_FIFO */
96e09c00caSUlf Lilleengen 	DT_SOCK,		/* EXT2_FT_SOCK */
97e09c00caSUlf Lilleengen 	DT_LNK,			/* EXT2_FT_SYMLINK */
98e09c00caSUlf Lilleengen };
99e09c00caSUlf Lilleengen #define	FTTODT(ft) \
100ef024b0dSPedro F. Giffuni     ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
101e09c00caSUlf Lilleengen 
102e09c00caSUlf Lilleengen static u_char dt_to_ext2_ft[] = {
103e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* DT_UNKNOWN */
104e09c00caSUlf Lilleengen 	EXT2_FT_FIFO,		/* DT_FIFO */
105e09c00caSUlf Lilleengen 	EXT2_FT_CHRDEV,		/* DT_CHR */
106e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
107e09c00caSUlf Lilleengen 	EXT2_FT_DIR,		/* DT_DIR */
108e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
109e09c00caSUlf Lilleengen 	EXT2_FT_BLKDEV,		/* DT_BLK */
110e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
111e09c00caSUlf Lilleengen 	EXT2_FT_REG_FILE,	/* DT_REG */
112e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
113e09c00caSUlf Lilleengen 	EXT2_FT_SYMLINK,	/* DT_LNK */
114e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
115e09c00caSUlf Lilleengen 	EXT2_FT_SOCK,		/* DT_SOCK */
116e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* unused */
117e09c00caSUlf Lilleengen 	EXT2_FT_UNKNOWN,	/* DT_WHT */
118e09c00caSUlf Lilleengen };
119e09c00caSUlf Lilleengen #define	DTTOFT(dt) \
120ef024b0dSPedro F. Giffuni     ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
121e09c00caSUlf Lilleengen 
122bb9f1ba4SFedor Uporov static int	ext2_check_direntry(struct vnode *dp,
123bb9f1ba4SFedor Uporov 		    struct ext2fs_direct_2 *de, int entryoffsetinblock);
1249824e4adSPedro F. Giffuni static int	ext2_is_dot_entry(struct componentname *cnp);
125553c9b4dSPedro F. Giffuni static int	ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
126553c9b4dSPedro F. Giffuni 		    struct componentname *cnp, ino_t *dd_ino);
127e09c00caSUlf Lilleengen 
1289824e4adSPedro F. Giffuni static int
ext2_is_dot_entry(struct componentname * cnp)1299824e4adSPedro F. Giffuni ext2_is_dot_entry(struct componentname *cnp)
1309824e4adSPedro F. Giffuni {
1319824e4adSPedro F. Giffuni 	if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
1329824e4adSPedro F. Giffuni 	    (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
1339824e4adSPedro F. Giffuni 		return (1);
1349824e4adSPedro F. Giffuni 	return (0);
1359824e4adSPedro F. Giffuni }
1369824e4adSPedro F. Giffuni 
137e09c00caSUlf Lilleengen /*
138e09c00caSUlf Lilleengen  * Vnode op for reading directories.
139e09c00caSUlf Lilleengen  */
140e09c00caSUlf Lilleengen int
ext2_readdir(struct vop_readdir_args * ap)141a9d1b299SPedro F. Giffuni ext2_readdir(struct vop_readdir_args *ap)
142e09c00caSUlf Lilleengen {
14378d912bbSPedro F. Giffuni 	struct vnode *vp = ap->a_vp;
144e09c00caSUlf Lilleengen 	struct uio *uio = ap->a_uio;
14578d912bbSPedro F. Giffuni 	struct buf *bp;
14678d912bbSPedro F. Giffuni 	struct inode *ip;
14778d912bbSPedro F. Giffuni 	struct ext2fs_direct_2 *dp, *edp;
148b214fcceSAlan Somers 	uint64_t *cookies;
149e09c00caSUlf Lilleengen 	struct dirent dstdp;
15078d912bbSPedro F. Giffuni 	off_t offset, startoffset;
15178d912bbSPedro F. Giffuni 	size_t readcnt, skipcnt;
15278d912bbSPedro F. Giffuni 	ssize_t startresid;
153040fb18bSPedro F. Giffuni 	u_int ncookies;
154e09c00caSUlf Lilleengen 	int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
15578d912bbSPedro F. Giffuni 	int error;
156e09c00caSUlf Lilleengen 
1579670f481SPedro F. Giffuni 	if (uio->uio_offset < 0)
1589670f481SPedro F. Giffuni 		return (EINVAL);
15978d912bbSPedro F. Giffuni 	ip = VTOI(vp);
16078d912bbSPedro F. Giffuni 	if (ap->a_ncookies != NULL) {
1617cbd6d33SPedro F. Giffuni 		if (uio->uio_resid < 0)
1627cbd6d33SPedro F. Giffuni 			ncookies = 0;
1637cbd6d33SPedro F. Giffuni 		else
16478d912bbSPedro F. Giffuni 			ncookies = uio->uio_resid;
16578d912bbSPedro F. Giffuni 		if (uio->uio_offset >= ip->i_size)
16678d912bbSPedro F. Giffuni 			ncookies = 0;
16778d912bbSPedro F. Giffuni 		else if (ip->i_size - uio->uio_offset < ncookies)
16878d912bbSPedro F. Giffuni 			ncookies = ip->i_size - uio->uio_offset;
16978d912bbSPedro F. Giffuni 		ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
17078d912bbSPedro F. Giffuni 		    e2d_namlen) + 4) + 1;
171f9834d10SPedro F. Giffuni 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
17278d912bbSPedro F. Giffuni 		*ap->a_ncookies = ncookies;
17378d912bbSPedro F. Giffuni 		*ap->a_cookies = cookies;
17478d912bbSPedro F. Giffuni 	} else {
17578d912bbSPedro F. Giffuni 		ncookies = 0;
17678d912bbSPedro F. Giffuni 		cookies = NULL;
17778d912bbSPedro F. Giffuni 	}
17878d912bbSPedro F. Giffuni 	offset = startoffset = uio->uio_offset;
17978d912bbSPedro F. Giffuni 	startresid = uio->uio_resid;
18078d912bbSPedro F. Giffuni 	error = 0;
18178d912bbSPedro F. Giffuni 	while (error == 0 && uio->uio_resid > 0 &&
18278d912bbSPedro F. Giffuni 	    uio->uio_offset < ip->i_size) {
18378d912bbSPedro F. Giffuni 		error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
18478d912bbSPedro F. Giffuni 		if (error)
18578d912bbSPedro F. Giffuni 			break;
18678d912bbSPedro F. Giffuni 		if (bp->b_offset + bp->b_bcount > ip->i_size)
18778d912bbSPedro F. Giffuni 			readcnt = ip->i_size - bp->b_offset;
18878d912bbSPedro F. Giffuni 		else
18978d912bbSPedro F. Giffuni 			readcnt = bp->b_bcount;
19078d912bbSPedro F. Giffuni 		skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
19178d912bbSPedro F. Giffuni 		    ~(size_t)(DIRBLKSIZ - 1);
19278d912bbSPedro F. Giffuni 		offset = bp->b_offset + skipcnt;
19378d912bbSPedro F. Giffuni 		dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
19478d912bbSPedro F. Giffuni 		edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
19578d912bbSPedro F. Giffuni 		while (error == 0 && uio->uio_resid > 0 && dp < edp) {
196cd3acfe7SFedor Uporov 			if (le16toh(dp->e2d_reclen) <= offsetof(struct ext2fs_direct_2,
197cd3acfe7SFedor Uporov 			    e2d_namlen) || (caddr_t)dp + le16toh(dp->e2d_reclen) >
19878d912bbSPedro F. Giffuni 			    (caddr_t)edp) {
19978d912bbSPedro F. Giffuni 				error = EIO;
20078d912bbSPedro F. Giffuni 				break;
20178d912bbSPedro F. Giffuni 			}
202e09c00caSUlf Lilleengen 			/*-
203e09c00caSUlf Lilleengen 			 * "New" ext2fs directory entries differ in 3 ways
204e09c00caSUlf Lilleengen 			 * from ufs on-disk ones:
205e09c00caSUlf Lilleengen 			 * - the name is not necessarily NUL-terminated.
206e09c00caSUlf Lilleengen 			 * - the file type field always exists and always
207e09c00caSUlf Lilleengen 			 *   follows the name length field.
208e09c00caSUlf Lilleengen 			 * - the file type is encoded in a different way.
209e09c00caSUlf Lilleengen 			 *
210e09c00caSUlf Lilleengen 			 * "Old" ext2fs directory entries need no special
211e09c00caSUlf Lilleengen 			 * conversions, since they are binary compatible
212e09c00caSUlf Lilleengen 			 * with "new" entries having a file type of 0 (i.e.,
213e09c00caSUlf Lilleengen 			 * EXT2_FT_UNKNOWN).  Splitting the old name length
214e09c00caSUlf Lilleengen 			 * field didn't make a mess like it did in ufs,
215e09c00caSUlf Lilleengen 			 * because ext2fs uses a machine-independent disk
216e09c00caSUlf Lilleengen 			 * layout.
217e09c00caSUlf Lilleengen 			 */
218e09c00caSUlf Lilleengen 			dstdp.d_namlen = dp->e2d_namlen;
21978d912bbSPedro F. Giffuni 			dstdp.d_type = FTTODT(dp->e2d_type);
22078d912bbSPedro F. Giffuni 			if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
221cd3acfe7SFedor Uporov 			    dstdp.d_namlen > le16toh(dp->e2d_reclen)) {
222e09c00caSUlf Lilleengen 				error = EIO;
223e09c00caSUlf Lilleengen 				break;
224e09c00caSUlf Lilleengen 			}
225cd3acfe7SFedor Uporov 			if (offset < startoffset || le32toh(dp->e2d_ino) == 0)
22678d912bbSPedro F. Giffuni 				goto nextentry;
227cd3acfe7SFedor Uporov 			dstdp.d_fileno = le32toh(dp->e2d_ino);
22878d912bbSPedro F. Giffuni 			dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
22978d912bbSPedro F. Giffuni 			bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
2301c4ca778SKonstantin Belousov 			/* NOTE: d_off is the offset of the *next* entry. */
231cd3acfe7SFedor Uporov 			dstdp.d_off = offset + le16toh(dp->e2d_reclen);
2326d2e2df7SMark Johnston 			dirent_terminate(&dstdp);
23378d912bbSPedro F. Giffuni 			if (dstdp.d_reclen > uio->uio_resid) {
23478d912bbSPedro F. Giffuni 				if (uio->uio_resid == startresid)
23578d912bbSPedro F. Giffuni 					error = EINVAL;
23678d912bbSPedro F. Giffuni 				else
23778d912bbSPedro F. Giffuni 					error = EJUSTRETURN;
23878d912bbSPedro F. Giffuni 				break;
239e09c00caSUlf Lilleengen 			}
24078d912bbSPedro F. Giffuni 			/* Advance dp. */
24178d912bbSPedro F. Giffuni 			error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
24278d912bbSPedro F. Giffuni 			if (error)
24378d912bbSPedro F. Giffuni 				break;
24478d912bbSPedro F. Giffuni 			if (cookies != NULL) {
24578d912bbSPedro F. Giffuni 				KASSERT(ncookies > 0,
24678d912bbSPedro F. Giffuni 				    ("ext2_readdir: cookies buffer too small"));
247cd3acfe7SFedor Uporov 				*cookies = offset + le16toh(dp->e2d_reclen);
24878d912bbSPedro F. Giffuni 				cookies++;
24978d912bbSPedro F. Giffuni 				ncookies--;
250e09c00caSUlf Lilleengen 			}
25178d912bbSPedro F. Giffuni nextentry:
252cd3acfe7SFedor Uporov 			offset += le16toh(dp->e2d_reclen);
25378d912bbSPedro F. Giffuni 			dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
254cd3acfe7SFedor Uporov 			    le16toh(dp->e2d_reclen));
25578d912bbSPedro F. Giffuni 		}
25678d912bbSPedro F. Giffuni 		bqrelse(bp);
25778d912bbSPedro F. Giffuni 		uio->uio_offset = offset;
25878d912bbSPedro F. Giffuni 	}
25978d912bbSPedro F. Giffuni 	/* We need to correct uio_offset. */
26078d912bbSPedro F. Giffuni 	uio->uio_offset = offset;
26178d912bbSPedro F. Giffuni 	if (error == EJUSTRETURN)
26278d912bbSPedro F. Giffuni 		error = 0;
26378d912bbSPedro F. Giffuni 	if (ap->a_ncookies != NULL) {
26478d912bbSPedro F. Giffuni 		if (error == 0) {
265e84e4421SKirk McKusick 			*ap->a_ncookies -= ncookies;
26678d912bbSPedro F. Giffuni 		} else {
26778d912bbSPedro F. Giffuni 			free(*ap->a_cookies, M_TEMP);
26878d912bbSPedro F. Giffuni 			*ap->a_ncookies = 0;
26978d912bbSPedro F. Giffuni 			*ap->a_cookies = NULL;
270e09c00caSUlf Lilleengen 		}
271e09c00caSUlf Lilleengen 	}
27278d912bbSPedro F. Giffuni 	if (error == 0 && ap->a_eofflag)
27378d912bbSPedro F. Giffuni 		*ap->a_eofflag = ip->i_size <= uio->uio_offset;
274e09c00caSUlf Lilleengen 	return (error);
275e09c00caSUlf Lilleengen }
276e09c00caSUlf Lilleengen 
277e09c00caSUlf Lilleengen /*
278e09c00caSUlf Lilleengen  * Convert a component of a pathname into a pointer to a locked inode.
279e09c00caSUlf Lilleengen  * This is a very central and rather complicated routine.
280e09c00caSUlf Lilleengen  * If the file system is not maintained in a strict tree hierarchy,
281e09c00caSUlf Lilleengen  * this can result in a deadlock situation (see comments in code below).
282e09c00caSUlf Lilleengen  *
283e09c00caSUlf Lilleengen  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
284e09c00caSUlf Lilleengen  * on whether the name is to be looked up, created, renamed, or deleted.
285e09c00caSUlf Lilleengen  * When CREATE, RENAME, or DELETE is specified, information usable in
286e09c00caSUlf Lilleengen  * creating, renaming, or deleting a directory entry may be calculated.
287e09c00caSUlf Lilleengen  * If flag has LOCKPARENT or'ed into it and the target of the pathname
288e09c00caSUlf Lilleengen  * exists, lookup returns both the target and its parent directory locked.
289e09c00caSUlf Lilleengen  * When creating or renaming and LOCKPARENT is specified, the target may
290e09c00caSUlf Lilleengen  * not be ".".  When deleting and LOCKPARENT is specified, the target may
291e09c00caSUlf Lilleengen  * be "."., but the caller must check to ensure it does an vrele and vput
292e09c00caSUlf Lilleengen  * instead of two vputs.
293e09c00caSUlf Lilleengen  *
294e09c00caSUlf Lilleengen  * Overall outline of ext2_lookup:
295e09c00caSUlf Lilleengen  *
296e09c00caSUlf Lilleengen  *	search for name in directory, to found or notfound
297e09c00caSUlf Lilleengen  * notfound:
298e09c00caSUlf Lilleengen  *	if creating, return locked directory, leaving info on available slots
299e09c00caSUlf Lilleengen  *	else return error
300e09c00caSUlf Lilleengen  * found:
301e09c00caSUlf Lilleengen  *	if at end of path and deleting, return information to allow delete
302e09c00caSUlf Lilleengen  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
303e09c00caSUlf Lilleengen  *	  inode and return info to allow rewrite
304e09c00caSUlf Lilleengen  *	if not at end, add name to cache; if at end and neither creating
305e09c00caSUlf Lilleengen  *	  nor deleting, add name to cache
306e09c00caSUlf Lilleengen  */
307e09c00caSUlf Lilleengen int
ext2_lookup(struct vop_cachedlookup_args * ap)308a9d1b299SPedro F. Giffuni ext2_lookup(struct vop_cachedlookup_args *ap)
309e09c00caSUlf Lilleengen {
310553c9b4dSPedro F. Giffuni 
311553c9b4dSPedro F. Giffuni 	return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
312553c9b4dSPedro F. Giffuni }
313553c9b4dSPedro F. Giffuni 
314553c9b4dSPedro F. Giffuni static int
ext2_lookup_ino(struct vnode * vdp,struct vnode ** vpp,struct componentname * cnp,ino_t * dd_ino)315553c9b4dSPedro F. Giffuni ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
316553c9b4dSPedro F. Giffuni     ino_t *dd_ino)
317553c9b4dSPedro F. Giffuni {
318e09c00caSUlf Lilleengen 	struct inode *dp;		/* inode for directory being searched */
319e09c00caSUlf Lilleengen 	struct buf *bp;			/* a buffer of directory entries */
320e09c00caSUlf Lilleengen 	struct ext2fs_direct_2 *ep;	/* the current directory entry */
321e09c00caSUlf Lilleengen 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
3229824e4adSPedro F. Giffuni 	struct ext2fs_searchslot ss;
323e09c00caSUlf Lilleengen 	doff_t i_diroff;		/* cached i_diroff value */
324e09c00caSUlf Lilleengen 	doff_t i_offset;		/* cached i_offset value */
325e09c00caSUlf Lilleengen 	int numdirpasses;		/* strategy for directory search */
326e09c00caSUlf Lilleengen 	doff_t endsearch;		/* offset to end directory search */
327e09c00caSUlf Lilleengen 	doff_t prevoff;			/* prev entry dp->i_offset */
328e09c00caSUlf Lilleengen 	struct vnode *pdp;		/* saved dp during symlink work */
329e09c00caSUlf Lilleengen 	struct vnode *tdp;		/* returned by VFS_VGET */
330e09c00caSUlf Lilleengen 	doff_t enduseful;		/* pointer past last used dir slot */
331e09c00caSUlf Lilleengen 	u_long bmask;			/* block offset mask */
3329824e4adSPedro F. Giffuni 	int error;
333e09c00caSUlf Lilleengen 	struct ucred *cred = cnp->cn_cred;
334e09c00caSUlf Lilleengen 	int flags = cnp->cn_flags;
335e09c00caSUlf Lilleengen 	int nameiop = cnp->cn_nameiop;
336553c9b4dSPedro F. Giffuni 	ino_t ino, ino1;
337e09c00caSUlf Lilleengen 	int ltype;
3389824e4adSPedro F. Giffuni 	int entry_found = 0;
339e09c00caSUlf Lilleengen 
340553c9b4dSPedro F. Giffuni 	int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
341e09c00caSUlf Lilleengen 
342553c9b4dSPedro F. Giffuni 	if (vpp != NULL)
343e09c00caSUlf Lilleengen 		*vpp = NULL;
344553c9b4dSPedro F. Giffuni 
345e09c00caSUlf Lilleengen 	dp = VTOI(vdp);
346f82a066cSJohn Baldwin 	bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
347553c9b4dSPedro F. Giffuni restart:
348553c9b4dSPedro F. Giffuni 	bp = NULL;
3499824e4adSPedro F. Giffuni 	ss.slotoffset = -1;
350f82a066cSJohn Baldwin 
351e09c00caSUlf Lilleengen 	/*
352e09c00caSUlf Lilleengen 	 * We now have a segment name to search for, and a directory to search.
3539824e4adSPedro F. Giffuni 	 *
354e09c00caSUlf Lilleengen 	 * Suppress search for slots unless creating
355e09c00caSUlf Lilleengen 	 * file and at end of pathname, in which case
356e09c00caSUlf Lilleengen 	 * we watch for a place to put the new file in
357e09c00caSUlf Lilleengen 	 * case it doesn't already exist.
358e09c00caSUlf Lilleengen 	 */
359e09c00caSUlf Lilleengen 	i_diroff = dp->i_diroff;
3609824e4adSPedro F. Giffuni 	ss.slotstatus = FOUND;
3619824e4adSPedro F. Giffuni 	ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
362e09c00caSUlf Lilleengen 	if ((nameiop == CREATE || nameiop == RENAME) &&
363e09c00caSUlf Lilleengen 	    (flags & ISLASTCN)) {
3649824e4adSPedro F. Giffuni 		ss.slotstatus = NONE;
3659824e4adSPedro F. Giffuni 		ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
366bf9a211dSPedro F. Giffuni 		/*
367bf9a211dSPedro F. Giffuni 		 * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
368bf9a211dSPedro F. Giffuni 		 * cnp->cn_namelen + 3) &~ 3;
369bf9a211dSPedro F. Giffuni 		 */
370e09c00caSUlf Lilleengen 	}
371e09c00caSUlf Lilleengen 	/*
3729824e4adSPedro F. Giffuni 	 * Try to lookup dir entry using htree directory index.
3739824e4adSPedro F. Giffuni 	 *
3749824e4adSPedro F. Giffuni 	 * If we got an error or we want to find '.' or '..' entry,
3759824e4adSPedro F. Giffuni 	 * we will fall back to linear search.
3769824e4adSPedro F. Giffuni 	 */
3779824e4adSPedro F. Giffuni 	if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
3789824e4adSPedro F. Giffuni 		numdirpasses = 1;
3799824e4adSPedro F. Giffuni 		entryoffsetinblock = 0;
3809824e4adSPedro F. Giffuni 		switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
3819824e4adSPedro F. Giffuni 		    &bp, &entryoffsetinblock, &i_offset, &prevoff,
3829824e4adSPedro F. Giffuni 		    &enduseful, &ss)) {
3839824e4adSPedro F. Giffuni 		case 0:
3849824e4adSPedro F. Giffuni 			ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
3859824e4adSPedro F. Giffuni 			    (i_offset & bmask));
3869824e4adSPedro F. Giffuni 			goto foundentry;
3879824e4adSPedro F. Giffuni 		case ENOENT:
3889824e4adSPedro F. Giffuni 			i_offset = roundup2(dp->i_size, DIRBLKSIZ);
3899824e4adSPedro F. Giffuni 			goto notfound;
3909824e4adSPedro F. Giffuni 		default:
3919824e4adSPedro F. Giffuni 			/*
3929824e4adSPedro F. Giffuni 			 * Something failed; just fallback to do a linear
3939824e4adSPedro F. Giffuni 			 * search.
3949824e4adSPedro F. Giffuni 			 */
3959824e4adSPedro F. Giffuni 			break;
3969824e4adSPedro F. Giffuni 		}
3979824e4adSPedro F. Giffuni 	}
3989824e4adSPedro F. Giffuni 
3999824e4adSPedro F. Giffuni 	/*
400e09c00caSUlf Lilleengen 	 * If there is cached information on a previous search of
401e09c00caSUlf Lilleengen 	 * this directory, pick up where we last left off.
402e09c00caSUlf Lilleengen 	 * We cache only lookups as these are the most common
403e09c00caSUlf Lilleengen 	 * and have the greatest payoff. Caching CREATE has little
404e09c00caSUlf Lilleengen 	 * benefit as it usually must search the entire directory
405e09c00caSUlf Lilleengen 	 * to determine that the entry does not exist. Caching the
406e09c00caSUlf Lilleengen 	 * location of the last DELETE or RENAME has not reduced
407e09c00caSUlf Lilleengen 	 * profiling time and hence has been removed in the interest
408e09c00caSUlf Lilleengen 	 * of simplicity.
409e09c00caSUlf Lilleengen 	 */
410e09c00caSUlf Lilleengen 	if (nameiop != LOOKUP || i_diroff == 0 ||
411e09c00caSUlf Lilleengen 	    i_diroff > dp->i_size) {
412e09c00caSUlf Lilleengen 		entryoffsetinblock = 0;
413e09c00caSUlf Lilleengen 		i_offset = 0;
414e09c00caSUlf Lilleengen 		numdirpasses = 1;
415e09c00caSUlf Lilleengen 	} else {
416e09c00caSUlf Lilleengen 		i_offset = i_diroff;
417e09c00caSUlf Lilleengen 		if ((entryoffsetinblock = i_offset & bmask) &&
418e09c00caSUlf Lilleengen 		    (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
419e09c00caSUlf Lilleengen 		    &bp)))
420e09c00caSUlf Lilleengen 			return (error);
421e09c00caSUlf Lilleengen 		numdirpasses = 2;
422e09c00caSUlf Lilleengen 		nchstats.ncs_2passes++;
423e09c00caSUlf Lilleengen 	}
424e09c00caSUlf Lilleengen 	prevoff = i_offset;
425e09c00caSUlf Lilleengen 	endsearch = roundup2(dp->i_size, DIRBLKSIZ);
426e09c00caSUlf Lilleengen 	enduseful = 0;
427e09c00caSUlf Lilleengen 
428e09c00caSUlf Lilleengen searchloop:
429e09c00caSUlf Lilleengen 	while (i_offset < endsearch) {
430e09c00caSUlf Lilleengen 		/*
431e09c00caSUlf Lilleengen 		 * If necessary, get the next directory block.
432e09c00caSUlf Lilleengen 		 */
433e09c00caSUlf Lilleengen 		if (bp != NULL)
434e09c00caSUlf Lilleengen 			brelse(bp);
4359824e4adSPedro F. Giffuni 		error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
4369824e4adSPedro F. Giffuni 		if (error != 0)
437e09c00caSUlf Lilleengen 			return (error);
438e49d64a7SFedor Uporov 
439e09c00caSUlf Lilleengen 		entryoffsetinblock = 0;
440e49d64a7SFedor Uporov 		if (ss.slotstatus == NONE) {
4419824e4adSPedro F. Giffuni 			ss.slotoffset = -1;
4429824e4adSPedro F. Giffuni 			ss.slotfreespace = 0;
443e09c00caSUlf Lilleengen 		}
444e49d64a7SFedor Uporov 
4459824e4adSPedro F. Giffuni 		error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
4469824e4adSPedro F. Giffuni 		    cnp->cn_nameptr, cnp->cn_namelen,
4479824e4adSPedro F. Giffuni 		    &entryoffsetinblock, &i_offset, &prevoff,
4489824e4adSPedro F. Giffuni 		    &enduseful, &ss);
4499824e4adSPedro F. Giffuni 		if (error != 0) {
4509824e4adSPedro F. Giffuni 			brelse(bp);
4519824e4adSPedro F. Giffuni 			return (error);
452e09c00caSUlf Lilleengen 		}
4539824e4adSPedro F. Giffuni 		if (entry_found) {
4549824e4adSPedro F. Giffuni 			ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
4559824e4adSPedro F. Giffuni 			    (entryoffsetinblock & bmask));
4569824e4adSPedro F. Giffuni foundentry:
457cd3acfe7SFedor Uporov 			ino = le32toh(ep->e2d_ino);
458e09c00caSUlf Lilleengen 			goto found;
459e09c00caSUlf Lilleengen 		}
460e09c00caSUlf Lilleengen 	}
4619824e4adSPedro F. Giffuni notfound:
462e09c00caSUlf Lilleengen 	/*
463e09c00caSUlf Lilleengen 	 * If we started in the middle of the directory and failed
464e09c00caSUlf Lilleengen 	 * to find our target, we must check the beginning as well.
465e09c00caSUlf Lilleengen 	 */
466e09c00caSUlf Lilleengen 	if (numdirpasses == 2) {
467e09c00caSUlf Lilleengen 		numdirpasses--;
468e09c00caSUlf Lilleengen 		i_offset = 0;
469e09c00caSUlf Lilleengen 		endsearch = i_diroff;
470e09c00caSUlf Lilleengen 		goto searchloop;
471e09c00caSUlf Lilleengen 	}
472e09c00caSUlf Lilleengen 	if (bp != NULL)
473e09c00caSUlf Lilleengen 		brelse(bp);
474e09c00caSUlf Lilleengen 	/*
475e09c00caSUlf Lilleengen 	 * If creating, and at end of pathname and current
476e09c00caSUlf Lilleengen 	 * directory has not been removed, then can consider
477e09c00caSUlf Lilleengen 	 * allowing file to be created.
478e09c00caSUlf Lilleengen 	 */
479e09c00caSUlf Lilleengen 	if ((nameiop == CREATE || nameiop == RENAME) &&
480e09c00caSUlf Lilleengen 	    (flags & ISLASTCN) && dp->i_nlink != 0) {
481e09c00caSUlf Lilleengen 		/*
482e09c00caSUlf Lilleengen 		 * Access for write is interpreted as allowing
483e09c00caSUlf Lilleengen 		 * creation of files in the directory.
484e09c00caSUlf Lilleengen 		 */
485b4a58fbfSMateusz Guzik 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0)
486e09c00caSUlf Lilleengen 			return (error);
487e09c00caSUlf Lilleengen 		/*
488e09c00caSUlf Lilleengen 		 * Return an indication of where the new directory
489e09c00caSUlf Lilleengen 		 * entry should be put.  If we didn't find a slot,
490e09c00caSUlf Lilleengen 		 * then set dp->i_count to 0 indicating
491e09c00caSUlf Lilleengen 		 * that the new slot belongs at the end of the
492e09c00caSUlf Lilleengen 		 * directory. If we found a slot, then the new entry
493e09c00caSUlf Lilleengen 		 * can be put in the range from dp->i_offset to
494e09c00caSUlf Lilleengen 		 * dp->i_offset + dp->i_count.
495e09c00caSUlf Lilleengen 		 */
4969824e4adSPedro F. Giffuni 		if (ss.slotstatus == NONE) {
497e09c00caSUlf Lilleengen 			dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
498e09c00caSUlf Lilleengen 			dp->i_count = 0;
499e09c00caSUlf Lilleengen 			enduseful = dp->i_offset;
500e09c00caSUlf Lilleengen 		} else {
5019824e4adSPedro F. Giffuni 			dp->i_offset = ss.slotoffset;
5029824e4adSPedro F. Giffuni 			dp->i_count = ss.slotsize;
5039824e4adSPedro F. Giffuni 			if (enduseful < ss.slotoffset + ss.slotsize)
5049824e4adSPedro F. Giffuni 				enduseful = ss.slotoffset + ss.slotsize;
505e09c00caSUlf Lilleengen 		}
506e09c00caSUlf Lilleengen 		dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
507e09c00caSUlf Lilleengen 		/*
508e09c00caSUlf Lilleengen 		 * We return with the directory locked, so that
509e09c00caSUlf Lilleengen 		 * the parameters we set up above will still be
510e09c00caSUlf Lilleengen 		 * valid if we actually decide to do a direnter().
511e09c00caSUlf Lilleengen 		 * We return ni_vp == NULL to indicate that the entry
512e09c00caSUlf Lilleengen 		 * does not currently exist; we leave a pointer to
513e09c00caSUlf Lilleengen 		 * the (locked) directory inode in ndp->ni_dvp.
514e09c00caSUlf Lilleengen 		 *
515e09c00caSUlf Lilleengen 		 * NB - if the directory is unlocked, then this
516e09c00caSUlf Lilleengen 		 * information cannot be used.
517e09c00caSUlf Lilleengen 		 */
518e09c00caSUlf Lilleengen 		return (EJUSTRETURN);
519e09c00caSUlf Lilleengen 	}
520e09c00caSUlf Lilleengen 	/*
521e09c00caSUlf Lilleengen 	 * Insert name into cache (as non-existent) if appropriate.
522e09c00caSUlf Lilleengen 	 */
5236c21f6edSKonstantin Belousov 	if ((cnp->cn_flags & MAKEENTRY) != 0)
524553c9b4dSPedro F. Giffuni 		cache_enter(vdp, NULL, cnp);
525e09c00caSUlf Lilleengen 	return (ENOENT);
526e09c00caSUlf Lilleengen 
527e09c00caSUlf Lilleengen found:
528553c9b4dSPedro F. Giffuni 	if (dd_ino != NULL)
529553c9b4dSPedro F. Giffuni 		*dd_ino = ino;
530e09c00caSUlf Lilleengen 	if (numdirpasses == 2)
531e09c00caSUlf Lilleengen 		nchstats.ncs_pass2++;
532e09c00caSUlf Lilleengen 	/*
533e09c00caSUlf Lilleengen 	 * Check that directory length properly reflects presence
534e09c00caSUlf Lilleengen 	 * of this entry.
535e09c00caSUlf Lilleengen 	 */
536cd3acfe7SFedor Uporov 	if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen) >
537cd3acfe7SFedor Uporov 	    dp->i_size) {
538e09c00caSUlf Lilleengen 		ext2_dirbad(dp, i_offset, "i_size too small");
5393c2dc524SFedor Uporov 		brelse(bp);
5403c2dc524SFedor Uporov 		return (EIO);
541e09c00caSUlf Lilleengen 	}
542e09c00caSUlf Lilleengen 	brelse(bp);
543e09c00caSUlf Lilleengen 
544e09c00caSUlf Lilleengen 	/*
545e09c00caSUlf Lilleengen 	 * Found component in pathname.
546e09c00caSUlf Lilleengen 	 * If the final component of path name, save information
547e09c00caSUlf Lilleengen 	 * in the cache as to where the entry was found.
548e09c00caSUlf Lilleengen 	 */
549e09c00caSUlf Lilleengen 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
550d9c9c81cSPedro F. Giffuni 		dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
551e09c00caSUlf Lilleengen 	/*
552e09c00caSUlf Lilleengen 	 * If deleting, and at end of pathname, return
553e09c00caSUlf Lilleengen 	 * parameters which can be used to remove file.
554e09c00caSUlf Lilleengen 	 */
555e09c00caSUlf Lilleengen 	if (nameiop == DELETE && (flags & ISLASTCN)) {
556d849f17dSPedro F. Giffuni 		if (flags & LOCKPARENT)
557d849f17dSPedro F. Giffuni 			ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
558e09c00caSUlf Lilleengen 		/*
559e09c00caSUlf Lilleengen 		 * Write access to directory required to delete files.
560e09c00caSUlf Lilleengen 		 */
561b4a58fbfSMateusz Guzik 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0)
562e09c00caSUlf Lilleengen 			return (error);
563e09c00caSUlf Lilleengen 		/*
564e09c00caSUlf Lilleengen 		 * Return pointer to current entry in dp->i_offset,
565e09c00caSUlf Lilleengen 		 * and distance past previous entry (if there
566e09c00caSUlf Lilleengen 		 * is a previous entry in this block) in dp->i_count.
567e09c00caSUlf Lilleengen 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
568d849f17dSPedro F. Giffuni 		 *
569d849f17dSPedro F. Giffuni 		 * Technically we shouldn't be setting these in the
570d849f17dSPedro F. Giffuni 		 * WANTPARENT case (first lookup in rename()), but any
571d849f17dSPedro F. Giffuni 		 * lookups that will result in directory changes will
572d849f17dSPedro F. Giffuni 		 * overwrite these.
573e09c00caSUlf Lilleengen 		 */
574d849f17dSPedro F. Giffuni 		dp->i_offset = i_offset;
575e09c00caSUlf Lilleengen 		if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
576e09c00caSUlf Lilleengen 			dp->i_count = 0;
577e09c00caSUlf Lilleengen 		else
578e09c00caSUlf Lilleengen 			dp->i_count = dp->i_offset - prevoff;
579553c9b4dSPedro F. Giffuni 		if (dd_ino != NULL)
580553c9b4dSPedro F. Giffuni 			return (0);
581e09c00caSUlf Lilleengen 		if (dp->i_number == ino) {
582e09c00caSUlf Lilleengen 			VREF(vdp);
583e09c00caSUlf Lilleengen 			*vpp = vdp;
584e09c00caSUlf Lilleengen 			return (0);
585e09c00caSUlf Lilleengen 		}
586e09c00caSUlf Lilleengen 		if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
587e09c00caSUlf Lilleengen 		    &tdp)) != 0)
588e09c00caSUlf Lilleengen 			return (error);
589e09c00caSUlf Lilleengen 		/*
590e09c00caSUlf Lilleengen 		 * If directory is "sticky", then user must own
591e09c00caSUlf Lilleengen 		 * the directory, or the file in it, else she
592e09c00caSUlf Lilleengen 		 * may not delete it (unless she's root). This
593e09c00caSUlf Lilleengen 		 * implements append-only directories.
594e09c00caSUlf Lilleengen 		 */
595d8ba45e2SEd Maste 		if ((dp->i_mode & ISVTX) &&
596e09c00caSUlf Lilleengen 		    cred->cr_uid != 0 &&
597e09c00caSUlf Lilleengen 		    cred->cr_uid != dp->i_uid &&
598e09c00caSUlf Lilleengen 		    VTOI(tdp)->i_uid != cred->cr_uid) {
599e09c00caSUlf Lilleengen 			vput(tdp);
600e09c00caSUlf Lilleengen 			return (EPERM);
601e09c00caSUlf Lilleengen 		}
602e09c00caSUlf Lilleengen 		*vpp = tdp;
603e09c00caSUlf Lilleengen 		return (0);
604e09c00caSUlf Lilleengen 	}
605e09c00caSUlf Lilleengen 
606e09c00caSUlf Lilleengen 	/*
607e09c00caSUlf Lilleengen 	 * If rewriting (RENAME), return the inode and the
608e09c00caSUlf Lilleengen 	 * information required to rewrite the present directory
609e09c00caSUlf Lilleengen 	 * Must get inode of directory entry to verify it's a
610e09c00caSUlf Lilleengen 	 * regular file, or empty directory.
611e09c00caSUlf Lilleengen 	 */
612e09c00caSUlf Lilleengen 	if (nameiop == RENAME && (flags & ISLASTCN)) {
613b4a58fbfSMateusz Guzik 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, curthread)) != 0)
614e09c00caSUlf Lilleengen 			return (error);
615e09c00caSUlf Lilleengen 		/*
616e09c00caSUlf Lilleengen 		 * Careful about locking second inode.
617e09c00caSUlf Lilleengen 		 * This can only occur if the target is ".".
618e09c00caSUlf Lilleengen 		 */
619d849f17dSPedro F. Giffuni 		dp->i_offset = i_offset;
620e09c00caSUlf Lilleengen 		if (dp->i_number == ino)
621e09c00caSUlf Lilleengen 			return (EISDIR);
622553c9b4dSPedro F. Giffuni 		if (dd_ino != NULL)
623553c9b4dSPedro F. Giffuni 			return (0);
624e09c00caSUlf Lilleengen 		if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
625e09c00caSUlf Lilleengen 		    &tdp)) != 0)
626e09c00caSUlf Lilleengen 			return (error);
627e09c00caSUlf Lilleengen 		*vpp = tdp;
628e09c00caSUlf Lilleengen 		return (0);
629e09c00caSUlf Lilleengen 	}
630553c9b4dSPedro F. Giffuni 	if (dd_ino != NULL)
631553c9b4dSPedro F. Giffuni 		return (0);
632e09c00caSUlf Lilleengen 
633e09c00caSUlf Lilleengen 	/*
634e09c00caSUlf Lilleengen 	 * Step through the translation in the name.  We do not `vput' the
635e09c00caSUlf Lilleengen 	 * directory because we may need it again if a symbolic link
636e09c00caSUlf Lilleengen 	 * is relative to the current directory.  Instead we save it
637e09c00caSUlf Lilleengen 	 * unlocked as "pdp".  We must get the target inode before unlocking
638e09c00caSUlf Lilleengen 	 * the directory to insure that the inode will not be removed
639e09c00caSUlf Lilleengen 	 * before we get it.  We prevent deadlock by always fetching
640e09c00caSUlf Lilleengen 	 * inodes from the root, moving down the directory tree. Thus
641e09c00caSUlf Lilleengen 	 * when following backward pointers ".." we must unlock the
642e09c00caSUlf Lilleengen 	 * parent directory before getting the requested directory.
643e09c00caSUlf Lilleengen 	 * There is a potential race condition here if both the current
644e09c00caSUlf Lilleengen 	 * and parent directories are removed before the VFS_VGET for the
645e09c00caSUlf Lilleengen 	 * inode associated with ".." returns.  We hope that this occurs
646e09c00caSUlf Lilleengen 	 * infrequently since we cannot avoid this race condition without
647e09c00caSUlf Lilleengen 	 * implementing a sophisticated deadlock detection algorithm.
648e09c00caSUlf Lilleengen 	 * Note also that this simple deadlock detection scheme will not
649e09c00caSUlf Lilleengen 	 * work if the file system has any hard links other than ".."
650e09c00caSUlf Lilleengen 	 * that point backwards in the directory structure.
651e09c00caSUlf Lilleengen 	 */
652e09c00caSUlf Lilleengen 	pdp = vdp;
653e09c00caSUlf Lilleengen 	if (flags & ISDOTDOT) {
654d849f17dSPedro F. Giffuni 		error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
655abd80ddbSMateusz Guzik 		if (VN_IS_DOOMED(pdp)) {
656553c9b4dSPedro F. Giffuni 			if (error == 0)
657553c9b4dSPedro F. Giffuni 				vput(tdp);
658553c9b4dSPedro F. Giffuni 			error = ENOENT;
659553c9b4dSPedro F. Giffuni 		}
660553c9b4dSPedro F. Giffuni 		if (error)
661e09c00caSUlf Lilleengen 			return (error);
662553c9b4dSPedro F. Giffuni 		/*
663553c9b4dSPedro F. Giffuni 		 * Recheck that ".." entry in the vdp directory points
664553c9b4dSPedro F. Giffuni 		 * to the inode we looked up before vdp lock was
665553c9b4dSPedro F. Giffuni 		 * dropped.
666553c9b4dSPedro F. Giffuni 		 */
667553c9b4dSPedro F. Giffuni 		error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
668553c9b4dSPedro F. Giffuni 		if (error) {
669553c9b4dSPedro F. Giffuni 			vput(tdp);
670553c9b4dSPedro F. Giffuni 			return (error);
671553c9b4dSPedro F. Giffuni 		}
672553c9b4dSPedro F. Giffuni 		if (ino1 != ino) {
673553c9b4dSPedro F. Giffuni 			vput(tdp);
674553c9b4dSPedro F. Giffuni 			goto restart;
675553c9b4dSPedro F. Giffuni 		}
676e09c00caSUlf Lilleengen 		*vpp = tdp;
677e09c00caSUlf Lilleengen 	} else if (dp->i_number == ino) {
678e09c00caSUlf Lilleengen 		VREF(vdp);	/* we want ourself, ie "." */
679e09c00caSUlf Lilleengen 		/*
680e09c00caSUlf Lilleengen 		 * When we lookup "." we still can be asked to lock it
681e09c00caSUlf Lilleengen 		 * differently.
682e09c00caSUlf Lilleengen 		 */
683e09c00caSUlf Lilleengen 		ltype = cnp->cn_lkflags & LK_TYPE_MASK;
684e09c00caSUlf Lilleengen 		if (ltype != VOP_ISLOCKED(vdp)) {
685e09c00caSUlf Lilleengen 			if (ltype == LK_EXCLUSIVE)
686e09c00caSUlf Lilleengen 				vn_lock(vdp, LK_UPGRADE | LK_RETRY);
687e09c00caSUlf Lilleengen 			else	/* if (ltype == LK_SHARED) */
688e09c00caSUlf Lilleengen 				vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
689e09c00caSUlf Lilleengen 		}
690e09c00caSUlf Lilleengen 		*vpp = vdp;
691e09c00caSUlf Lilleengen 	} else {
692e09c00caSUlf Lilleengen 		if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
693e09c00caSUlf Lilleengen 		    &tdp)) != 0)
694e09c00caSUlf Lilleengen 			return (error);
695e09c00caSUlf Lilleengen 		*vpp = tdp;
696e09c00caSUlf Lilleengen 	}
697e09c00caSUlf Lilleengen 
698e09c00caSUlf Lilleengen 	/*
699e09c00caSUlf Lilleengen 	 * Insert name into cache if appropriate.
700e09c00caSUlf Lilleengen 	 */
701e09c00caSUlf Lilleengen 	if (cnp->cn_flags & MAKEENTRY)
702e09c00caSUlf Lilleengen 		cache_enter(vdp, *vpp, cnp);
703e09c00caSUlf Lilleengen 	return (0);
704e09c00caSUlf Lilleengen }
705e09c00caSUlf Lilleengen 
7069824e4adSPedro F. Giffuni int
ext2_search_dirblock(struct inode * ip,void * data,int * foundp,const char * name,int namelen,int * entryoffsetinblockp,doff_t * offp,doff_t * prevoffp,doff_t * endusefulp,struct ext2fs_searchslot * ssp)7079824e4adSPedro F. Giffuni ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
7089824e4adSPedro F. Giffuni     const char *name, int namelen, int *entryoffsetinblockp,
7099824e4adSPedro F. Giffuni     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
7109824e4adSPedro F. Giffuni     struct ext2fs_searchslot *ssp)
7119824e4adSPedro F. Giffuni {
7129824e4adSPedro F. Giffuni 	struct vnode *vdp;
7139824e4adSPedro F. Giffuni 	struct ext2fs_direct_2 *ep, *top;
7149824e4adSPedro F. Giffuni 	uint32_t bsize = ip->i_e2fs->e2fs_bsize;
7159824e4adSPedro F. Giffuni 	int offset = *entryoffsetinblockp;
7169824e4adSPedro F. Giffuni 	int namlen;
7179824e4adSPedro F. Giffuni 
7189824e4adSPedro F. Giffuni 	vdp = ITOV(ip);
7199824e4adSPedro F. Giffuni 
7209824e4adSPedro F. Giffuni 	ep = (struct ext2fs_direct_2 *)((char *)data + offset);
721e49d64a7SFedor Uporov 	top = (struct ext2fs_direct_2 *)((char *)data + bsize);
7229824e4adSPedro F. Giffuni 	while (ep < top) {
723bb9f1ba4SFedor Uporov 		if (ext2_check_direntry(vdp, ep, offset)) {
7249824e4adSPedro F. Giffuni 			int i;
725bf9a211dSPedro F. Giffuni 
7269824e4adSPedro F. Giffuni 			ext2_dirbad(ip, *offp, "mangled entry");
7279824e4adSPedro F. Giffuni 			i = bsize - (offset & (bsize - 1));
7289824e4adSPedro F. Giffuni 			*offp += i;
7299824e4adSPedro F. Giffuni 			offset += i;
730bb9f1ba4SFedor Uporov 			ep = (struct ext2fs_direct_2 *)((char *)data + offset);
7319824e4adSPedro F. Giffuni 			continue;
7329824e4adSPedro F. Giffuni 		}
7339824e4adSPedro F. Giffuni 
7349824e4adSPedro F. Giffuni 		/*
7359824e4adSPedro F. Giffuni 		 * If an appropriate sized slot has not yet been found,
7369824e4adSPedro F. Giffuni 		 * check to see if one is available. Also accumulate space
7379824e4adSPedro F. Giffuni 		 * in the current block so that we can determine if
7389824e4adSPedro F. Giffuni 		 * compaction is viable.
7399824e4adSPedro F. Giffuni 		 */
7409824e4adSPedro F. Giffuni 		if (ssp->slotstatus != FOUND) {
741cd3acfe7SFedor Uporov 			int size = le16toh(ep->e2d_reclen);
7429824e4adSPedro F. Giffuni 
7439824e4adSPedro F. Giffuni 			if (ep->e2d_ino != 0)
7449824e4adSPedro F. Giffuni 				size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
745e49d64a7SFedor Uporov 			else if (ext2_is_dirent_tail(ip, ep))
746e49d64a7SFedor Uporov 				size -= sizeof(struct ext2fs_direct_tail);
7479824e4adSPedro F. Giffuni 			if (size > 0) {
7489824e4adSPedro F. Giffuni 				if (size >= ssp->slotneeded) {
7499824e4adSPedro F. Giffuni 					ssp->slotstatus = FOUND;
7509824e4adSPedro F. Giffuni 					ssp->slotoffset = *offp;
751cd3acfe7SFedor Uporov 					ssp->slotsize = le16toh(ep->e2d_reclen);
7529824e4adSPedro F. Giffuni 				} else if (ssp->slotstatus == NONE) {
7539824e4adSPedro F. Giffuni 					ssp->slotfreespace += size;
7549824e4adSPedro F. Giffuni 					if (ssp->slotoffset == -1)
7559824e4adSPedro F. Giffuni 						ssp->slotoffset = *offp;
7569824e4adSPedro F. Giffuni 					if (ssp->slotfreespace >= ssp->slotneeded) {
7579824e4adSPedro F. Giffuni 						ssp->slotstatus = COMPACT;
7589824e4adSPedro F. Giffuni 						ssp->slotsize = *offp +
759cd3acfe7SFedor Uporov 						    le16toh(ep->e2d_reclen) -
7609824e4adSPedro F. Giffuni 						    ssp->slotoffset;
7619824e4adSPedro F. Giffuni 					}
7629824e4adSPedro F. Giffuni 				}
7639824e4adSPedro F. Giffuni 			}
7649824e4adSPedro F. Giffuni 		}
7659824e4adSPedro F. Giffuni 		/*
7669824e4adSPedro F. Giffuni 		 * Check for a name match.
7679824e4adSPedro F. Giffuni 		 */
768cd3acfe7SFedor Uporov 		if (ep->e2d_ino != 0) {
7699824e4adSPedro F. Giffuni 			namlen = ep->e2d_namlen;
7709824e4adSPedro F. Giffuni 			if (namlen == namelen &&
7719824e4adSPedro F. Giffuni 			    !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
7729824e4adSPedro F. Giffuni 				/*
7739824e4adSPedro F. Giffuni 				 * Save directory entry's inode number and
7749824e4adSPedro F. Giffuni 				 * reclen in ndp->ni_ufs area, and release
7759824e4adSPedro F. Giffuni 				 * directory buffer.
7769824e4adSPedro F. Giffuni 				 */
7779824e4adSPedro F. Giffuni 				*foundp = 1;
7789824e4adSPedro F. Giffuni 				return (0);
7799824e4adSPedro F. Giffuni 			}
7809824e4adSPedro F. Giffuni 		}
7819824e4adSPedro F. Giffuni 		*prevoffp = *offp;
782cd3acfe7SFedor Uporov 		*offp += le16toh(ep->e2d_reclen);
783cd3acfe7SFedor Uporov 		offset += le16toh(ep->e2d_reclen);
7849824e4adSPedro F. Giffuni 		*entryoffsetinblockp = offset;
785cd3acfe7SFedor Uporov 		if (ep->e2d_ino != 0)
7869824e4adSPedro F. Giffuni 			*endusefulp = *offp;
7879824e4adSPedro F. Giffuni 		/*
7889824e4adSPedro F. Giffuni 		 * Get pointer to the next entry.
7899824e4adSPedro F. Giffuni 		 */
7909824e4adSPedro F. Giffuni 		ep = (struct ext2fs_direct_2 *)((char *)data + offset);
7919824e4adSPedro F. Giffuni 	}
7929824e4adSPedro F. Giffuni 
7939824e4adSPedro F. Giffuni 	return (0);
7949824e4adSPedro F. Giffuni }
7959824e4adSPedro F. Giffuni 
796e09c00caSUlf Lilleengen void
ext2_dirbad(struct inode * ip,doff_t offset,char * how)797a9d1b299SPedro F. Giffuni ext2_dirbad(struct inode *ip, doff_t offset, char *how)
798e09c00caSUlf Lilleengen {
7993c2dc524SFedor Uporov 
800ebc94b66SFedor Uporov 	SDT_PROBE4(ext2fs, , trace, ext2_dirbad_error,
801*bb95e6faSFedor Uporov 	    ITOV(ip)->v_mount->mnt_stat.f_mntonname, ip->i_number, offset, how);
802e09c00caSUlf Lilleengen }
803e09c00caSUlf Lilleengen 
804e09c00caSUlf Lilleengen /*
805e09c00caSUlf Lilleengen  * Do consistency checking on a directory entry:
806e09c00caSUlf Lilleengen  *	record length must be multiple of 4
807e09c00caSUlf Lilleengen  *	entry must fit in rest of its DIRBLKSIZ block
808e09c00caSUlf Lilleengen  *	record must be large enough to contain entry
809e09c00caSUlf Lilleengen  *	name is not longer than MAXNAMLEN
810e09c00caSUlf Lilleengen  *	name must be as long as advertised, and null terminated
811366da717SFedor Uporov  *	inode number less then inode count
812366da717SFedor Uporov  *	if root inode entry, it have correct name
813e09c00caSUlf Lilleengen  */
814e09c00caSUlf Lilleengen static int
ext2_check_direntry(struct vnode * dp,struct ext2fs_direct_2 * de,int entryoffsetinblock)815bb9f1ba4SFedor Uporov ext2_check_direntry(struct vnode *dp, struct ext2fs_direct_2 *de,
816a9d1b299SPedro F. Giffuni     int entryoffsetinblock)
817e09c00caSUlf Lilleengen {
818bb9f1ba4SFedor Uporov 	struct m_ext2fs *fs = VTOI(dp)->i_e2fs;
819e09c00caSUlf Lilleengen 	char *error_msg = NULL;
820e09c00caSUlf Lilleengen 
821cd3acfe7SFedor Uporov 	if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(1))
822e09c00caSUlf Lilleengen 		error_msg = "rec_len is smaller than minimal";
823cd3acfe7SFedor Uporov 	else if (le16toh(de->e2d_reclen) % 4 != 0)
824e09c00caSUlf Lilleengen 		error_msg = "rec_len % 4 != 0";
825cd3acfe7SFedor Uporov 	else if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(de->e2d_namlen))
826e09c00caSUlf Lilleengen 		error_msg = "reclen is too small for name_len";
827bb9f1ba4SFedor Uporov 	else if (entryoffsetinblock + le16toh(de->e2d_reclen)> fs->e2fs_bsize)
828e09c00caSUlf Lilleengen 		error_msg = "directory entry across blocks";
829bb9f1ba4SFedor Uporov 	else if (le32toh(de->e2d_ino) > fs->e2fs->e2fs_icount)
830bb9f1ba4SFedor Uporov 		error_msg = "directory entry inode out of bounds";
831366da717SFedor Uporov 	else if (le32toh(de->e2d_ino) == EXT2_ROOTINO &&
832366da717SFedor Uporov 	    ((de->e2d_namlen != 1 && de->e2d_namlen != 2) ||
833366da717SFedor Uporov 	    (de->e2d_name[0] != '.') ||
834366da717SFedor Uporov 	    (de->e2d_namlen == 2 && de->e2d_name[1] != '.')))
835366da717SFedor Uporov 		error_msg = "bad root directory entry";
836e09c00caSUlf Lilleengen 
837e09c00caSUlf Lilleengen 	if (error_msg != NULL) {
838ebc94b66SFedor Uporov 		SDT_PROBE5(ext2fs, , trace, ext2_dirbadentry_error,
839ebc94b66SFedor Uporov 		    error_msg, entryoffsetinblock,
840cd3acfe7SFedor Uporov 		    le32toh(de->e2d_ino), le16toh(de->e2d_reclen),
841cd3acfe7SFedor Uporov 		    de->e2d_namlen);
842e09c00caSUlf Lilleengen 	}
843bb9f1ba4SFedor Uporov 	return (error_msg == NULL ? 0 : EINVAL);
844e09c00caSUlf Lilleengen }
845e09c00caSUlf Lilleengen 
846e09c00caSUlf Lilleengen /*
84717c7b27fSFedor Uporov  * Insert an entry into the fresh directory block.
84817c7b27fSFedor Uporov  * Initialize entry tail if the metadata_csum feature is turned on.
84917c7b27fSFedor Uporov  */
85017c7b27fSFedor Uporov static int
ext2_add_first_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry,struct componentname * cnp)85117c7b27fSFedor Uporov ext2_add_first_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
85217c7b27fSFedor Uporov     struct componentname *cnp)
85317c7b27fSFedor Uporov {
85417c7b27fSFedor Uporov 	struct inode *dp;
85517c7b27fSFedor Uporov 	struct iovec aiov;
85617c7b27fSFedor Uporov 	struct uio auio;
85717c7b27fSFedor Uporov 	char* buf = NULL;
85817c7b27fSFedor Uporov 	int dirblksize, error;
85917c7b27fSFedor Uporov 
86017c7b27fSFedor Uporov 	dp = VTOI(dvp);
86117c7b27fSFedor Uporov 	dirblksize = dp->i_e2fs->e2fs_bsize;
86217c7b27fSFedor Uporov 
86317c7b27fSFedor Uporov 	if (dp->i_offset & (dirblksize - 1))
86417c7b27fSFedor Uporov 		panic("ext2_add_first_entry: bad directory offset");
86517c7b27fSFedor Uporov 
86617c7b27fSFedor Uporov 	if (EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs,
86717c7b27fSFedor Uporov 	    EXT2F_ROCOMPAT_METADATA_CKSUM)) {
868cd3acfe7SFedor Uporov 		entry->e2d_reclen = htole16(dirblksize -
869cd3acfe7SFedor Uporov 		    sizeof(struct ext2fs_direct_tail));
87017c7b27fSFedor Uporov 		buf = malloc(dirblksize, M_TEMP, M_WAITOK);
87117c7b27fSFedor Uporov 		memcpy(buf, entry, EXT2_DIR_REC_LEN(entry->e2d_namlen));
87217c7b27fSFedor Uporov 		ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, dirblksize));
87317c7b27fSFedor Uporov 		ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf);
87417c7b27fSFedor Uporov 
87517c7b27fSFedor Uporov 		auio.uio_offset = dp->i_offset;
87617c7b27fSFedor Uporov 		auio.uio_resid = dirblksize;
87717c7b27fSFedor Uporov 		aiov.iov_len = auio.uio_resid;
87817c7b27fSFedor Uporov 		aiov.iov_base = (caddr_t)buf;
87917c7b27fSFedor Uporov 	} else {
880cd3acfe7SFedor Uporov 		entry->e2d_reclen = htole16(dirblksize);
88117c7b27fSFedor Uporov 		auio.uio_offset = dp->i_offset;
88217c7b27fSFedor Uporov 		auio.uio_resid = EXT2_DIR_REC_LEN(entry->e2d_namlen);
88317c7b27fSFedor Uporov 		aiov.iov_len = auio.uio_resid;
88417c7b27fSFedor Uporov 		aiov.iov_base = (caddr_t)entry;
88517c7b27fSFedor Uporov 	}
88617c7b27fSFedor Uporov 
88717c7b27fSFedor Uporov 	auio.uio_iov = &aiov;
88817c7b27fSFedor Uporov 	auio.uio_iovcnt = 1;
88917c7b27fSFedor Uporov 	auio.uio_rw = UIO_WRITE;
89017c7b27fSFedor Uporov 	auio.uio_segflg = UIO_SYSSPACE;
89117c7b27fSFedor Uporov 	auio.uio_td = (struct thread *)0;
89217c7b27fSFedor Uporov 	error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
89317c7b27fSFedor Uporov 	if (error)
89417c7b27fSFedor Uporov 		goto out;
89517c7b27fSFedor Uporov 
89617c7b27fSFedor Uporov 	dp->i_size = roundup2(dp->i_size, dirblksize);
89717c7b27fSFedor Uporov 	dp->i_flag |= IN_CHANGE;
89817c7b27fSFedor Uporov 
89917c7b27fSFedor Uporov out:
90017c7b27fSFedor Uporov 	free(buf, M_TEMP);
90117c7b27fSFedor Uporov 	return (error);
90217c7b27fSFedor Uporov 
90317c7b27fSFedor Uporov }
90417c7b27fSFedor Uporov 
90517c7b27fSFedor Uporov /*
906e09c00caSUlf Lilleengen  * Write a directory entry after a call to namei, using the parameters
907e09c00caSUlf Lilleengen  * that it left in nameidata.  The argument ip is the inode which the new
908e09c00caSUlf Lilleengen  * directory entry will refer to.  Dvp is a pointer to the directory to
909e09c00caSUlf Lilleengen  * be written, which was left locked by namei. Remaining parameters
910e09c00caSUlf Lilleengen  * (dp->i_offset, dp->i_count) indicate how the space for the new
911e09c00caSUlf Lilleengen  * entry is to be obtained.
912e09c00caSUlf Lilleengen  */
913e09c00caSUlf Lilleengen int
ext2_direnter(struct inode * ip,struct vnode * dvp,struct componentname * cnp)914a9d1b299SPedro F. Giffuni ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
915e09c00caSUlf Lilleengen {
916e09c00caSUlf Lilleengen 	struct inode *dp;
917e09c00caSUlf Lilleengen 	struct ext2fs_direct_2 newdir;
918e09c00caSUlf Lilleengen 	int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
9197d84ca67SPedro F. Giffuni 	int error;
920e09c00caSUlf Lilleengen 
921e09c00caSUlf Lilleengen 	dp = VTOI(dvp);
922cd3acfe7SFedor Uporov 	newdir.e2d_ino = htole32(ip->i_number);
923e09c00caSUlf Lilleengen 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
924cd3acfe7SFedor Uporov 	    EXT2F_INCOMPAT_FTYPE)) {
925cd3acfe7SFedor Uporov 		newdir.e2d_namlen = cnp->cn_namelen;
926e09c00caSUlf Lilleengen 		newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
927cd3acfe7SFedor Uporov 	} else
928cd3acfe7SFedor Uporov 		newdir.e2d_namlen = htole16(cnp->cn_namelen);
929cd3acfe7SFedor Uporov 
930e09c00caSUlf Lilleengen 	bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
9319824e4adSPedro F. Giffuni 
9329824e4adSPedro F. Giffuni 	if (ext2_htree_has_idx(dp)) {
9339824e4adSPedro F. Giffuni 		error = ext2_htree_add_entry(dvp, &newdir, cnp);
9349824e4adSPedro F. Giffuni 		if (error) {
9359b58c801SPedro F. Giffuni 			dp->i_flag &= ~IN_E3INDEX;
9369824e4adSPedro F. Giffuni 			dp->i_flag |= IN_CHANGE | IN_UPDATE;
9379824e4adSPedro F. Giffuni 		}
9389824e4adSPedro F. Giffuni 		return (error);
9399824e4adSPedro F. Giffuni 	}
9409824e4adSPedro F. Giffuni 
9419824e4adSPedro F. Giffuni 	if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
9429824e4adSPedro F. Giffuni 	    !ext2_htree_has_idx(dp)) {
9439824e4adSPedro F. Giffuni 		if ((dp->i_size / DIRBLKSIZ) == 1 &&
9449824e4adSPedro F. Giffuni 		    dp->i_offset == DIRBLKSIZ) {
9459824e4adSPedro F. Giffuni 			/*
9469824e4adSPedro F. Giffuni 			 * Making indexed directory when one block is not
9479824e4adSPedro F. Giffuni 			 * enough to save all entries.
9489824e4adSPedro F. Giffuni 			 */
9499824e4adSPedro F. Giffuni 			return ext2_htree_create_index(dvp, cnp, &newdir);
9509824e4adSPedro F. Giffuni 		}
9519824e4adSPedro F. Giffuni 	}
9529824e4adSPedro F. Giffuni 
953e09c00caSUlf Lilleengen 	/*
954e09c00caSUlf Lilleengen 	 * If dp->i_count is 0, then namei could find no
955e09c00caSUlf Lilleengen 	 * space in the directory. Here, dp->i_offset will
956e09c00caSUlf Lilleengen 	 * be on a directory block boundary and we will write the
957e09c00caSUlf Lilleengen 	 * new entry into a fresh block.
958e09c00caSUlf Lilleengen 	 */
95917c7b27fSFedor Uporov 	if (dp->i_count == 0)
96017c7b27fSFedor Uporov 		return ext2_add_first_entry(dvp, &newdir, cnp);
961e09c00caSUlf Lilleengen 
9629824e4adSPedro F. Giffuni 	error = ext2_add_entry(dvp, &newdir);
9639824e4adSPedro F. Giffuni 	if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
9649824e4adSPedro F. Giffuni 		error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
965b4a58fbfSMateusz Guzik 		    cnp->cn_cred, curthread);
9669824e4adSPedro F. Giffuni 	return (error);
9679824e4adSPedro F. Giffuni }
9689824e4adSPedro F. Giffuni 
9699824e4adSPedro F. Giffuni /*
9709824e4adSPedro F. Giffuni  * Insert an entry into the directory block.
9719824e4adSPedro F. Giffuni  * Compact the contents.
9729824e4adSPedro F. Giffuni  */
9739824e4adSPedro F. Giffuni int
ext2_add_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry)9749824e4adSPedro F. Giffuni ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
9759824e4adSPedro F. Giffuni {
9769824e4adSPedro F. Giffuni 	struct ext2fs_direct_2 *ep, *nep;
9779824e4adSPedro F. Giffuni 	struct inode *dp;
9789824e4adSPedro F. Giffuni 	struct buf *bp;
9799824e4adSPedro F. Giffuni 	u_int dsize;
9809824e4adSPedro F. Giffuni 	int error, loc, newentrysize, spacefree;
9819824e4adSPedro F. Giffuni 	char *dirbuf;
9829824e4adSPedro F. Giffuni 
9839824e4adSPedro F. Giffuni 	dp = VTOI(dvp);
9849824e4adSPedro F. Giffuni 
985e09c00caSUlf Lilleengen 	/*
986e09c00caSUlf Lilleengen 	 * If dp->i_count is non-zero, then namei found space
987e09c00caSUlf Lilleengen 	 * for the new entry in the range dp->i_offset to
988e09c00caSUlf Lilleengen 	 * dp->i_offset + dp->i_count in the directory.
989e09c00caSUlf Lilleengen 	 * To use this space, we may have to compact the entries located
990e09c00caSUlf Lilleengen 	 * there, by copying them together towards the beginning of the
991e09c00caSUlf Lilleengen 	 * block, leaving the free space in one usable chunk at the end.
992e09c00caSUlf Lilleengen 	 */
993e09c00caSUlf Lilleengen 
994e09c00caSUlf Lilleengen 	/*
995e09c00caSUlf Lilleengen 	 * Increase size of directory if entry eats into new space.
996e09c00caSUlf Lilleengen 	 * This should never push the size past a new multiple of
997e09c00caSUlf Lilleengen 	 * DIRBLKSIZE.
998e09c00caSUlf Lilleengen 	 *
999e09c00caSUlf Lilleengen 	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
1000e09c00caSUlf Lilleengen 	 */
1001e09c00caSUlf Lilleengen 	if (dp->i_offset + dp->i_count > dp->i_size)
1002e09c00caSUlf Lilleengen 		dp->i_size = dp->i_offset + dp->i_count;
1003e09c00caSUlf Lilleengen 	/*
1004e09c00caSUlf Lilleengen 	 * Get the block containing the space for the new directory entry.
1005e09c00caSUlf Lilleengen 	 */
1006e09c00caSUlf Lilleengen 	if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
1007e09c00caSUlf Lilleengen 	    &bp)) != 0)
1008e09c00caSUlf Lilleengen 		return (error);
1009e09c00caSUlf Lilleengen 	/*
1010e09c00caSUlf Lilleengen 	 * Find space for the new entry. In the simple case, the entry at
1011e09c00caSUlf Lilleengen 	 * offset base will have the space. If it does not, then namei
1012e09c00caSUlf Lilleengen 	 * arranged that compacting the region dp->i_offset to
1013e09c00caSUlf Lilleengen 	 * dp->i_offset + dp->i_count would yield the
1014e09c00caSUlf Lilleengen 	 * space.
1015e09c00caSUlf Lilleengen 	 */
10169824e4adSPedro F. Giffuni 	newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
1017e09c00caSUlf Lilleengen 	ep = (struct ext2fs_direct_2 *)dirbuf;
1018e09c00caSUlf Lilleengen 	dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
1019cd3acfe7SFedor Uporov 	spacefree = le16toh(ep->e2d_reclen) - dsize;
1020cd3acfe7SFedor Uporov 	for (loc = le16toh(ep->e2d_reclen); loc < dp->i_count; ) {
1021e09c00caSUlf Lilleengen 		nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1022cd3acfe7SFedor Uporov 		if (le32toh(ep->e2d_ino)) {
1023e09c00caSUlf Lilleengen 			/* trim the existing slot */
1024cd3acfe7SFedor Uporov 			ep->e2d_reclen = htole16(dsize);
1025e09c00caSUlf Lilleengen 			ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1026e09c00caSUlf Lilleengen 		} else {
1027e09c00caSUlf Lilleengen 			/* overwrite; nothing there; header is ours */
1028e09c00caSUlf Lilleengen 			spacefree += dsize;
1029e09c00caSUlf Lilleengen 		}
1030e09c00caSUlf Lilleengen 		dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1031cd3acfe7SFedor Uporov 		spacefree += le16toh(nep->e2d_reclen) - dsize;
1032cd3acfe7SFedor Uporov 		loc += le16toh(nep->e2d_reclen);
1033e09c00caSUlf Lilleengen 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1034e09c00caSUlf Lilleengen 	}
1035e09c00caSUlf Lilleengen 	/*
1036e09c00caSUlf Lilleengen 	 * Update the pointer fields in the previous entry (if any),
1037e09c00caSUlf Lilleengen 	 * copy in the new entry, and write out the block.
1038e09c00caSUlf Lilleengen 	 */
1039e09c00caSUlf Lilleengen 	if (ep->e2d_ino == 0) {
1040e09c00caSUlf Lilleengen 		if (spacefree + dsize < newentrysize)
1041e09c00caSUlf Lilleengen 			panic("ext2_direnter: compact1");
1042cd3acfe7SFedor Uporov 		entry->e2d_reclen = htole16(spacefree + dsize);
1043e09c00caSUlf Lilleengen 	} else {
1044e09c00caSUlf Lilleengen 		if (spacefree < newentrysize)
1045e09c00caSUlf Lilleengen 			panic("ext2_direnter: compact2");
1046cd3acfe7SFedor Uporov 		entry->e2d_reclen = htole16(spacefree);
1047cd3acfe7SFedor Uporov 		ep->e2d_reclen = htole16(dsize);
1048e09c00caSUlf Lilleengen 		ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1049e09c00caSUlf Lilleengen 	}
10509824e4adSPedro F. Giffuni 	bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
10516d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
10529e880b87SJohn Baldwin 	if (DOINGASYNC(dvp)) {
10539e880b87SJohn Baldwin 		bdwrite(bp);
10549e880b87SJohn Baldwin 		error = 0;
10559e880b87SJohn Baldwin 	} else {
1056e09c00caSUlf Lilleengen 		error = bwrite(bp);
10579e880b87SJohn Baldwin 	}
1058e09c00caSUlf Lilleengen 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1059e09c00caSUlf Lilleengen 	return (error);
1060e09c00caSUlf Lilleengen }
1061e09c00caSUlf Lilleengen 
1062e09c00caSUlf Lilleengen /*
1063e09c00caSUlf Lilleengen  * Remove a directory entry after a call to namei, using
1064e09c00caSUlf Lilleengen  * the parameters which it left in nameidata. The entry
1065e09c00caSUlf Lilleengen  * dp->i_offset contains the offset into the directory of the
1066e09c00caSUlf Lilleengen  * entry to be eliminated.  The dp->i_count field contains the
1067e09c00caSUlf Lilleengen  * size of the previous record in the directory.  If this
1068e09c00caSUlf Lilleengen  * is 0, the first entry is being deleted, so we need only
1069e09c00caSUlf Lilleengen  * zero the inode number to mark the entry as free.  If the
1070e09c00caSUlf Lilleengen  * entry is not the first in the directory, we must reclaim
1071e09c00caSUlf Lilleengen  * the space of the now empty record by adding the record size
1072e09c00caSUlf Lilleengen  * to the size of the previous entry.
1073e09c00caSUlf Lilleengen  */
1074e09c00caSUlf Lilleengen int
ext2_dirremove(struct vnode * dvp,struct componentname * cnp)1075a9d1b299SPedro F. Giffuni ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1076e09c00caSUlf Lilleengen {
1077e09c00caSUlf Lilleengen 	struct inode *dp;
1078e09c00caSUlf Lilleengen 	struct ext2fs_direct_2 *ep, *rep;
1079e09c00caSUlf Lilleengen 	struct buf *bp;
1080e09c00caSUlf Lilleengen 	int error;
1081e09c00caSUlf Lilleengen 
1082e09c00caSUlf Lilleengen 	dp = VTOI(dvp);
1083e09c00caSUlf Lilleengen 	if (dp->i_count == 0) {
1084e09c00caSUlf Lilleengen 		/*
1085e09c00caSUlf Lilleengen 		 * First entry in block: set d_ino to zero.
1086e09c00caSUlf Lilleengen 		 */
1087e09c00caSUlf Lilleengen 		if ((error =
1088e09c00caSUlf Lilleengen 		    ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1089e09c00caSUlf Lilleengen 		    &bp)) != 0)
1090e09c00caSUlf Lilleengen 			return (error);
1091e09c00caSUlf Lilleengen 		ep->e2d_ino = 0;
109217c7b27fSFedor Uporov 		ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1093e09c00caSUlf Lilleengen 		error = bwrite(bp);
1094e09c00caSUlf Lilleengen 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
1095e09c00caSUlf Lilleengen 		return (error);
1096e09c00caSUlf Lilleengen 	}
1097e09c00caSUlf Lilleengen 	/*
1098e09c00caSUlf Lilleengen 	 * Collapse new free space into previous entry.
1099e09c00caSUlf Lilleengen 	 */
1100e09c00caSUlf Lilleengen 	if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1101e09c00caSUlf Lilleengen 	    (char **)&ep, &bp)) != 0)
1102e09c00caSUlf Lilleengen 		return (error);
1103e09c00caSUlf Lilleengen 
1104e09c00caSUlf Lilleengen 	/* Set 'rep' to the entry being removed. */
1105e09c00caSUlf Lilleengen 	if (dp->i_count == 0)
1106e09c00caSUlf Lilleengen 		rep = ep;
1107e09c00caSUlf Lilleengen 	else
1108cd3acfe7SFedor Uporov 		rep = (struct ext2fs_direct_2 *)((char *)ep +
1109cd3acfe7SFedor Uporov 		    le16toh(ep->e2d_reclen));
1110e09c00caSUlf Lilleengen 	ep->e2d_reclen += rep->e2d_reclen;
11116d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
11129e880b87SJohn Baldwin 	if (DOINGASYNC(dvp) && dp->i_count != 0)
11139e880b87SJohn Baldwin 		bdwrite(bp);
11149e880b87SJohn Baldwin 	else
1115e09c00caSUlf Lilleengen 		error = bwrite(bp);
1116e09c00caSUlf Lilleengen 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1117e09c00caSUlf Lilleengen 	return (error);
1118e09c00caSUlf Lilleengen }
1119e09c00caSUlf Lilleengen 
1120e09c00caSUlf Lilleengen /*
1121e09c00caSUlf Lilleengen  * Rewrite an existing directory entry to point at the inode
1122e09c00caSUlf Lilleengen  * supplied.  The parameters describing the directory entry are
1123e09c00caSUlf Lilleengen  * set up by a call to namei.
1124e09c00caSUlf Lilleengen  */
1125e09c00caSUlf Lilleengen int
ext2_dirrewrite(struct inode * dp,struct inode * ip,struct componentname * cnp)1126a9d1b299SPedro F. Giffuni ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1127e09c00caSUlf Lilleengen {
1128e09c00caSUlf Lilleengen 	struct buf *bp;
1129e09c00caSUlf Lilleengen 	struct ext2fs_direct_2 *ep;
1130e09c00caSUlf Lilleengen 	struct vnode *vdp = ITOV(dp);
1131e09c00caSUlf Lilleengen 	int error;
1132e09c00caSUlf Lilleengen 
1133e09c00caSUlf Lilleengen 	if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1134e09c00caSUlf Lilleengen 	    &bp)) != 0)
1135e09c00caSUlf Lilleengen 		return (error);
1136cd3acfe7SFedor Uporov 	ep->e2d_ino = htole32(ip->i_number);
1137e09c00caSUlf Lilleengen 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1138e09c00caSUlf Lilleengen 	    EXT2F_INCOMPAT_FTYPE))
1139e09c00caSUlf Lilleengen 		ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1140e09c00caSUlf Lilleengen 	else
1141e09c00caSUlf Lilleengen 		ep->e2d_type = EXT2_FT_UNKNOWN;
11426d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1143e09c00caSUlf Lilleengen 	error = bwrite(bp);
1144e09c00caSUlf Lilleengen 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
1145e09c00caSUlf Lilleengen 	return (error);
1146e09c00caSUlf Lilleengen }
1147e09c00caSUlf Lilleengen 
1148e09c00caSUlf Lilleengen /*
1149e09c00caSUlf Lilleengen  * Check if a directory is empty or not.
1150e09c00caSUlf Lilleengen  * Inode supplied must be locked.
1151e09c00caSUlf Lilleengen  *
1152e09c00caSUlf Lilleengen  * Using a struct dirtemplate here is not precisely
1153e09c00caSUlf Lilleengen  * what we want, but better than using a struct direct.
1154e09c00caSUlf Lilleengen  *
1155e09c00caSUlf Lilleengen  * NB: does not handle corrupted directories.
1156e09c00caSUlf Lilleengen  */
1157e09c00caSUlf Lilleengen int
ext2_dirempty(struct inode * ip,ino_t parentino,struct ucred * cred)1158a9d1b299SPedro F. Giffuni ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1159e09c00caSUlf Lilleengen {
1160e09c00caSUlf Lilleengen 	off_t off;
1161e09c00caSUlf Lilleengen 	struct dirtemplate dbuf;
1162e09c00caSUlf Lilleengen 	struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1163526d0bd5SKonstantin Belousov 	int error, namlen;
1164526d0bd5SKonstantin Belousov 	ssize_t count;
1165e09c00caSUlf Lilleengen #define	MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1166e09c00caSUlf Lilleengen 
1167cd3acfe7SFedor Uporov 	for (off = 0; off < ip->i_size; off += le16toh(dp->e2d_reclen)) {
1168e09c00caSUlf Lilleengen 		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1169e09c00caSUlf Lilleengen 		    off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1170e09c00caSUlf Lilleengen 		    NOCRED, &count, (struct thread *)0);
1171e09c00caSUlf Lilleengen 		/*
1172e09c00caSUlf Lilleengen 		 * Since we read MINDIRSIZ, residual must
1173e09c00caSUlf Lilleengen 		 * be 0 unless we're at end of file.
1174e09c00caSUlf Lilleengen 		 */
1175e09c00caSUlf Lilleengen 		if (error || count != 0)
1176e09c00caSUlf Lilleengen 			return (0);
1177e09c00caSUlf Lilleengen 		/* avoid infinite loops */
1178e09c00caSUlf Lilleengen 		if (dp->e2d_reclen == 0)
1179e09c00caSUlf Lilleengen 			return (0);
1180e09c00caSUlf Lilleengen 		/* skip empty entries */
1181e09c00caSUlf Lilleengen 		if (dp->e2d_ino == 0)
1182e09c00caSUlf Lilleengen 			continue;
1183e09c00caSUlf Lilleengen 		/* accept only "." and ".." */
1184e09c00caSUlf Lilleengen 		namlen = dp->e2d_namlen;
1185e09c00caSUlf Lilleengen 		if (namlen > 2)
1186e09c00caSUlf Lilleengen 			return (0);
1187e09c00caSUlf Lilleengen 		if (dp->e2d_name[0] != '.')
1188e09c00caSUlf Lilleengen 			return (0);
1189e09c00caSUlf Lilleengen 		/*
1190e09c00caSUlf Lilleengen 		 * At this point namlen must be 1 or 2.
1191e09c00caSUlf Lilleengen 		 * 1 implies ".", 2 implies ".." if second
1192e09c00caSUlf Lilleengen 		 * char is also "."
1193e09c00caSUlf Lilleengen 		 */
1194e09c00caSUlf Lilleengen 		if (namlen == 1)
1195e09c00caSUlf Lilleengen 			continue;
1196cd3acfe7SFedor Uporov 		if (dp->e2d_name[1] == '.' && le32toh(dp->e2d_ino) == parentino)
1197e09c00caSUlf Lilleengen 			continue;
1198e09c00caSUlf Lilleengen 		return (0);
1199e09c00caSUlf Lilleengen 	}
1200e09c00caSUlf Lilleengen 	return (1);
1201e09c00caSUlf Lilleengen }
1202e09c00caSUlf Lilleengen 
1203e09c00caSUlf Lilleengen /*
1204e09c00caSUlf Lilleengen  * Check if source directory is in the path of the target directory.
1205e09c00caSUlf Lilleengen  * Target is supplied locked, source is unlocked.
1206e09c00caSUlf Lilleengen  * The target is always vput before returning.
1207e09c00caSUlf Lilleengen  */
1208e09c00caSUlf Lilleengen int
ext2_checkpath(struct inode * source,struct inode * target,struct ucred * cred)1209a9d1b299SPedro F. Giffuni ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1210e09c00caSUlf Lilleengen {
1211e09c00caSUlf Lilleengen 	struct vnode *vp;
1212555368dcSPedro F. Giffuni 	int error, namlen;
1213e09c00caSUlf Lilleengen 	struct dirtemplate dirbuf;
1214e09c00caSUlf Lilleengen 
1215e09c00caSUlf Lilleengen 	vp = ITOV(target);
1216e09c00caSUlf Lilleengen 	if (target->i_number == source->i_number) {
1217e09c00caSUlf Lilleengen 		error = EEXIST;
1218e09c00caSUlf Lilleengen 		goto out;
1219e09c00caSUlf Lilleengen 	}
1220d4273344SPedro F. Giffuni 	if (target->i_number == EXT2_ROOTINO) {
1221e09c00caSUlf Lilleengen 		error = 0;
1222e09c00caSUlf Lilleengen 		goto out;
1223d4273344SPedro F. Giffuni 	}
1224e09c00caSUlf Lilleengen 
1225e09c00caSUlf Lilleengen 	for (;;) {
1226e09c00caSUlf Lilleengen 		if (vp->v_type != VDIR) {
1227e09c00caSUlf Lilleengen 			error = ENOTDIR;
1228e09c00caSUlf Lilleengen 			break;
1229e09c00caSUlf Lilleengen 		}
1230e09c00caSUlf Lilleengen 		error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1231e09c00caSUlf Lilleengen 		    sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1232e09c00caSUlf Lilleengen 		    IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1233e09c00caSUlf Lilleengen 		    NULL);
1234e09c00caSUlf Lilleengen 		if (error != 0)
1235e09c00caSUlf Lilleengen 			break;
1236e09c00caSUlf Lilleengen 		namlen = dirbuf.dotdot_type;	/* like ufs little-endian */
1237e09c00caSUlf Lilleengen 		if (namlen != 2 ||
1238e09c00caSUlf Lilleengen 		    dirbuf.dotdot_name[0] != '.' ||
1239e09c00caSUlf Lilleengen 		    dirbuf.dotdot_name[1] != '.') {
1240e09c00caSUlf Lilleengen 			error = ENOTDIR;
1241e09c00caSUlf Lilleengen 			break;
1242e09c00caSUlf Lilleengen 		}
1243cd3acfe7SFedor Uporov 		if (le32toh(dirbuf.dotdot_ino) == source->i_number) {
1244e09c00caSUlf Lilleengen 			error = EINVAL;
1245e09c00caSUlf Lilleengen 			break;
1246e09c00caSUlf Lilleengen 		}
1247cd3acfe7SFedor Uporov 		if (le32toh(dirbuf.dotdot_ino) == EXT2_ROOTINO)
1248e09c00caSUlf Lilleengen 			break;
1249e09c00caSUlf Lilleengen 		vput(vp);
1250cd3acfe7SFedor Uporov 		if ((error = VFS_VGET(vp->v_mount, le32toh(dirbuf.dotdot_ino),
1251e09c00caSUlf Lilleengen 		    LK_EXCLUSIVE, &vp)) != 0) {
1252e09c00caSUlf Lilleengen 			vp = NULL;
1253e09c00caSUlf Lilleengen 			break;
1254e09c00caSUlf Lilleengen 		}
1255e09c00caSUlf Lilleengen 	}
1256e09c00caSUlf Lilleengen 
1257e09c00caSUlf Lilleengen out:
1258e09c00caSUlf Lilleengen 	if (error == ENOTDIR)
1259ebc94b66SFedor Uporov 		SDT_PROBE2(ext2fs, , lookup, trace, 1,
1260ebc94b66SFedor Uporov 		    "checkpath: .. not a directory");
1261e09c00caSUlf Lilleengen 	if (vp != NULL)
1262e09c00caSUlf Lilleengen 		vput(vp);
1263e09c00caSUlf Lilleengen 	return (error);
1264e09c00caSUlf Lilleengen }
1265