ext2_lookup.c revision 125843
1118611Snjl/*
2118611Snjl *  modified for Lites 1.1
3118611Snjl *
4118611Snjl *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5118611Snjl *  University of Utah, Department of Computer Science
6118611Snjl */
7118611Snjl/*
8118611Snjl * Copyright (c) 1989, 1993
9118611Snjl *	The Regents of the University of California.  All rights reserved.
10118611Snjl * (c) UNIX System Laboratories, Inc.
11118611Snjl * All or some portions of this file are derived from material licensed
12202771Sjkim * to the University of California by American Telephone and Telegraph
13118611Snjl * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14118611Snjl * the permission of UNIX System Laboratories, Inc.
15118611Snjl *
16118611Snjl * Redistribution and use in source and binary forms, with or without
17118611Snjl * modification, are permitted provided that the following conditions
18118611Snjl * are met:
19118611Snjl * 1. Redistributions of source code must retain the above copyright
20118611Snjl *    notice, this list of conditions and the following disclaimer.
21118611Snjl * 2. Redistributions in binary form must reproduce the above copyright
22118611Snjl *    notice, this list of conditions and the following disclaimer in the
23118611Snjl *    documentation and/or other materials provided with the distribution.
24118611Snjl * 3. All advertising materials mentioning features or use of this software
25118611Snjl *    must display the following acknowledgement:
26118611Snjl *	This product includes software developed by the University of
27118611Snjl *	California, Berkeley and its contributors.
28118611Snjl * 4. Neither the name of the University nor the names of its contributors
29118611Snjl *    may be used to endorse or promote products derived from this software
30118611Snjl *    without specific prior written permission.
31118611Snjl *
32118611Snjl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33118611Snjl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34118611Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35118611Snjl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36118611Snjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37118611Snjl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38118611Snjl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39118611Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40118611Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41118611Snjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42118611Snjl * SUCH DAMAGE.
43118611Snjl *
44118611Snjl *	@(#)ufs_lookup.c	8.6 (Berkeley) 4/1/94
45118611Snjl * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_lookup.c 125843 2004-02-15 08:19:42Z bde $
46118611Snjl */
47118611Snjl
48118611Snjl#include <sys/param.h>
49118611Snjl#include <sys/systm.h>
50118611Snjl#include <sys/namei.h>
51118611Snjl#include <sys/bio.h>
52118611Snjl#include <sys/buf.h>
53118611Snjl#include <sys/mount.h>
54118611Snjl#include <sys/vnode.h>
55118611Snjl#include <sys/malloc.h>
56118611Snjl#include <sys/dirent.h>
57118611Snjl#include <sys/sysctl.h>
58118611Snjl
59118611Snjl#include <ufs/ufs/dir.h>
60118611Snjl
61118611Snjl#include <gnu/ext2fs/inode.h>
62118611Snjl#include <gnu/ext2fs/ext2_mount.h>
63118611Snjl#include <gnu/ext2fs/ext2_extern.h>
64118611Snjl#include <gnu/ext2fs/ext2_fs.h>
65118611Snjl#include <gnu/ext2fs/ext2_fs_sb.h>
66118611Snjl
67118611Snjl#ifdef DIAGNOSTIC
68118611Snjlstatic int dirchk = 1;
69118611Snjl#else
70118611Snjlstatic int dirchk = 0;
71118611Snjl#endif
72118611Snjl
73118611SnjlSYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
74118611SnjlSYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
75118611Snjl
76118611Snjl/*
77118611Snjl   DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
78118611Snjl   while it is the native blocksize in ext2fs - thus, a #define
79118611Snjl   is no longer appropriate
80118611Snjl*/
81118611Snjl#undef  DIRBLKSIZ
82118611Snjl
83118611Snjlstatic u_char ext2_ft_to_dt[] = {
84118611Snjl	DT_UNKNOWN,		/* EXT2_FT_UNKNOWN */
85118611Snjl	DT_REG,			/* EXT2_FT_REG_FILE */
86118611Snjl	DT_DIR,			/* EXT2_FT_DIR */
87118611Snjl	DT_CHR,			/* EXT2_FT_CHRDEV */
88118611Snjl	DT_BLK,			/* EXT2_FT_BLKDEV */
89118611Snjl	DT_FIFO,		/* EXT2_FT_FIFO */
90118611Snjl	DT_SOCK,		/* EXT2_FT_SOCK */
91118611Snjl	DT_LNK,			/* EXT2_FT_SYMLINK */
92118611Snjl};
93118611Snjl#define	FTTODT(ft)						\
94118611Snjl    ((ft) > sizeof(ext2_ft_to_dt) / sizeof(ext2_ft_to_dt[0]) ?	\
95118611Snjl    DT_UNKNOWN : ext2_ft_to_dt[(ft)])
96118611Snjl
97118611Snjlstatic u_char dt_to_ext2_ft[] = {
98118611Snjl	EXT2_FT_UNKNOWN,	/* DT_UNKNOWN */
99118611Snjl	EXT2_FT_FIFO,		/* DT_FIFO */
100118611Snjl	EXT2_FT_CHRDEV,		/* DT_CHR */
101118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
102118611Snjl	EXT2_FT_DIR,		/* DT_DIR */
103118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
104118611Snjl	EXT2_FT_BLKDEV,		/* DT_BLK */
105118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
106118611Snjl	EXT2_FT_REG_FILE,	/* DT_REG */
107118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
108118611Snjl	EXT2_FT_SYMLINK,	/* DT_LNK */
109118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
110118611Snjl	EXT2_FT_SOCK,		/* DT_SOCK */
111118611Snjl	EXT2_FT_UNKNOWN,	/* unused */
112118611Snjl	EXT2_FT_UNKNOWN,	/* DT_WHT */
113118611Snjl};
114118611Snjl#define	DTTOFT(dt)						\
115118611Snjl    ((dt) > sizeof(dt_to_ext2_ft) / sizeof(dt_to_ext2_ft[0]) ?	\
116118611Snjl    EXT2_FT_UNKNOWN : dt_to_ext2_ft[(dt)])
117118611Snjl
118151937Sjkimstatic int	ext2_dirbadentry(struct vnode *dp, struct ext2_dir_entry_2 *de,
119118611Snjl		    int entryoffsetinblock);
120118611Snjl
121118611Snjl/*
122118611Snjl * Vnode op for reading directories.
123151937Sjkim *
124118611Snjl * The routine below assumes that the on-disk format of a directory
125151937Sjkim * is the same as that defined by <sys/dirent.h>. If the on-disk
126151937Sjkim * format changes, then it will be necessary to do a conversion
127151937Sjkim * from the on-disk format that read returns to the format defined
128151937Sjkim * by <sys/dirent.h>.
129151937Sjkim */
130193529Sjkim/*
131193529Sjkim * this is exactly what we do here - the problem is that the conversion
132193529Sjkim * will blow up some entries by four bytes, so it can't be done in place.
133193529Sjkim * This is too bad. Right now the conversion is done entry by entry, the
134193529Sjkim * converted entry is sent via uiomove.
135193529Sjkim *
136193529Sjkim * XXX allocate a buffer, convert as many entries as possible, then send
137193529Sjkim * the whole buffer to uiomove
138193529Sjkim */
139193529Sjkimint
140193529Sjkimext2_readdir(ap)
141193529Sjkim	struct vop_readdir_args /* {
142193529Sjkim		struct vnode *a_vp;
143193529Sjkim		struct uio *a_uio;
144193529Sjkim		struct ucred *a_cred;
145193529Sjkim	} */ *ap;
146193529Sjkim{
147193529Sjkim	struct uio *uio = ap->a_uio;
148193529Sjkim	int count, error;
149193529Sjkim
150118611Snjl	struct ext2_dir_entry_2 *edp, *dp;
151118611Snjl	int ncookies;
152118611Snjl	struct dirent dstdp;
153118611Snjl	struct uio auio;
154118611Snjl	struct iovec aiov;
155118611Snjl	caddr_t dirbuf;
156118611Snjl	int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->s_blocksize;
157118611Snjl	int readcnt;
158118611Snjl	off_t startoffset = uio->uio_offset;
159118611Snjl
160118611Snjl	count = uio->uio_resid;
161118611Snjl	/*
162118611Snjl	 * Avoid complications for partial directory entries by adjusting
163118611Snjl	 * the i/o to end at a block boundary.  Don't give up (like ufs
164151937Sjkim	 * does) if the initial adjustment gives a negative count, since
165118611Snjl	 * many callers don't supply a large enough buffer.  The correct
166118611Snjl	 * size is a little larger than DIRBLKSIZ to allow for expansion
167118611Snjl	 * of directory entries, but some callers just use 512.
168118611Snjl	 */
169118611Snjl	count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
170118611Snjl	if (count <= 0)
171118611Snjl		count += DIRBLKSIZ;
172202771Sjkim
173118611Snjl#ifdef EXT2FS_DEBUG
174118611Snjl	printf("ext2_readdir: uio_offset = %lld, uio_resid = %d, count = %d\n",
175118611Snjl	    uio->uio_offset, uio->uio_resid, count);
176118611Snjl#endif
177118611Snjl
178118611Snjl	auio = *uio;
179118611Snjl	auio.uio_iov = &aiov;
180202771Sjkim	auio.uio_iovcnt = 1;
181202771Sjkim	auio.uio_resid = count;
182202771Sjkim	auio.uio_segflg = UIO_SYSSPACE;
183202771Sjkim	aiov.iov_len = count;
184118611Snjl	MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
185118611Snjl	aiov.iov_base = dirbuf;
186118611Snjl	error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
187118611Snjl	if (error == 0) {
188118611Snjl		readcnt = count - auio.uio_resid;
189118611Snjl		edp = (struct ext2_dir_entry_2 *)&dirbuf[readcnt];
190118611Snjl		ncookies = 0;
191118611Snjl		bzero(&dstdp, offsetof(struct dirent, d_name));
192118611Snjl		for (dp = (struct ext2_dir_entry_2 *)dirbuf;
193118611Snjl		    !error && uio->uio_resid > 0 && dp < edp; ) {
194118611Snjl			/*-
195118611Snjl			 * "New" ext2fs directory entries differ in 3 ways
196118611Snjl			 * from ufs on-disk ones:
197118611Snjl			 * - the name is not necessarily NUL-terminated.
198118611Snjl			 * - the file type field always exists and always
199118611Snjl			 *   follows the name length field.
200118611Snjl			 * - the file type is encoded in a different way.
201118611Snjl			 *
202118611Snjl			 * "Old" ext2fs directory entries need no special
203118611Snjl			 * conversions, since they are binary compatible
204118611Snjl			 * with "new" entries having a file type of 0 (i.e.,
205118611Snjl			 * EXT2_FT_UNKNOWN).  Splitting the old name length
206118611Snjl			 * field didn't make a mess like it did in ufs,
207118611Snjl			 * because ext2fs uses a machine-independent disk
208118611Snjl			 * layout.
209118611Snjl			 */
210118611Snjl			dstdp.d_fileno = dp->inode;
211118611Snjl			dstdp.d_type = FTTODT(dp->file_type);
212118611Snjl			dstdp.d_namlen = dp->name_len;
213151937Sjkim			dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
214118611Snjl			bcopy(dp->name, dstdp.d_name, dstdp.d_namlen);
215118611Snjl			bzero(dstdp.d_name + dstdp.d_namlen,
216118611Snjl			    dstdp.d_reclen - offsetof(struct dirent, d_name) -
217118611Snjl			    dstdp.d_namlen);
218118611Snjl
219118611Snjl			if (dp->rec_len > 0) {
220118611Snjl				if(dstdp.d_reclen <= uio->uio_resid) {
221118611Snjl					/* advance dp */
222118611Snjl					dp = (struct ext2_dir_entry_2 *)
223118611Snjl					    ((char *)dp + dp->rec_len);
224118611Snjl					error =
225118611Snjl					  uiomove(&dstdp, dstdp.d_reclen, uio);
226118611Snjl					if (!error)
227118611Snjl						ncookies++;
228118611Snjl				} else
229118611Snjl					break;
230118611Snjl			} else {
231118611Snjl				error = EIO;
232118611Snjl				break;
233118611Snjl			}
234118611Snjl		}
235151937Sjkim		/* we need to correct uio_offset */
236151937Sjkim		uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
237118611Snjl
238118611Snjl		if (!error && ap->a_ncookies != NULL) {
239118611Snjl			u_long *cookiep, *cookies, *ecookies;
240118611Snjl			off_t off;
241118611Snjl
242118611Snjl			if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
243118611Snjl				panic("ext2_readdir: unexpected uio from NFS server");
244118611Snjl			MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
245118611Snjl			       M_WAITOK);
246193529Sjkim			off = startoffset;
247193529Sjkim			for (dp = (struct ext2_dir_entry_2 *)dirbuf,
248193529Sjkim			     cookiep = cookies, ecookies = cookies + ncookies;
249193529Sjkim			     cookiep < ecookies;
250193529Sjkim			     dp = (struct ext2_dir_entry_2 *)((caddr_t) dp + dp->rec_len)) {
251151937Sjkim				off += dp->rec_len;
252151937Sjkim				*cookiep++ = (u_long) off;
253151937Sjkim			}
254151937Sjkim			*ap->a_ncookies = ncookies;
255118611Snjl			*ap->a_cookies = cookies;
256118611Snjl		}
257118611Snjl	}
258118611Snjl	FREE(dirbuf, M_TEMP);
259118611Snjl	if (ap->a_eofflag)
260118611Snjl		*ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
261118611Snjl	return (error);
262118611Snjl}
263118611Snjl
264118611Snjl/*
265118611Snjl * Convert a component of a pathname into a pointer to a locked inode.
266118611Snjl * This is a very central and rather complicated routine.
267118611Snjl * If the file system is not maintained in a strict tree hierarchy,
268118611Snjl * this can result in a deadlock situation (see comments in code below).
269118611Snjl *
270118611Snjl * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
271118611Snjl * on whether the name is to be looked up, created, renamed, or deleted.
272118611Snjl * When CREATE, RENAME, or DELETE is specified, information usable in
273118611Snjl * creating, renaming, or deleting a directory entry may be calculated.
274118611Snjl * If flag has LOCKPARENT or'ed into it and the target of the pathname
275118611Snjl * exists, lookup returns both the target and its parent directory locked.
276118611Snjl * When creating or renaming and LOCKPARENT is specified, the target may
277118611Snjl * not be ".".  When deleting and LOCKPARENT is specified, the target may
278118611Snjl * be "."., but the caller must check to ensure it does an vrele and vput
279118611Snjl * instead of two vputs.
280118611Snjl *
281118611Snjl * Overall outline of ext2_lookup:
282118611Snjl *
283118611Snjl *	search for name in directory, to found or notfound
284118611Snjl * notfound:
285118611Snjl *	if creating, return locked directory, leaving info on available slots
286118611Snjl *	else return error
287118611Snjl * found:
288118611Snjl *	if at end of path and deleting, return information to allow delete
289118611Snjl *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
290118611Snjl *	  inode and return info to allow rewrite
291118611Snjl *	if not at end, add name to cache; if at end and neither creating
292118611Snjl *	  nor deleting, add name to cache
293118611Snjl */
294118611Snjlint
295118611Snjlext2_lookup(ap)
296118611Snjl	struct vop_cachedlookup_args /* {
297118611Snjl		struct vnode *a_dvp;
298118611Snjl		struct vnode **a_vpp;
299118611Snjl		struct componentname *a_cnp;
300118611Snjl	} */ *ap;
301118611Snjl{
302118611Snjl	struct vnode *vdp;		/* vnode for directory being searched */
303118611Snjl	struct inode *dp;		/* inode for directory being searched */
304118611Snjl	struct buf *bp;			/* a buffer of directory entries */
305118611Snjl	struct ext2_dir_entry_2 *ep;	/* the current directory entry */
306151937Sjkim	int entryoffsetinblock;		/* offset of ep in bp's buffer */
307151937Sjkim	enum {NONE, COMPACT, FOUND} slotstatus;
308118611Snjl	doff_t slotoffset;		/* offset of area with free space */
309118611Snjl	int slotsize;			/* size of area at slotoffset */
310151937Sjkim	int slotfreespace;		/* amount of space free in slot */
311193529Sjkim	int slotneeded;			/* size of the entry we're seeking */
312193529Sjkim	int numdirpasses;		/* strategy for directory search */
313118611Snjl	doff_t endsearch;		/* offset to end directory search */
314118611Snjl	doff_t prevoff;			/* prev entry dp->i_offset */
315118611Snjl	struct vnode *pdp;		/* saved dp during symlink work */
316151937Sjkim	struct vnode *tdp;		/* returned by VFS_VGET */
317151937Sjkim	doff_t enduseful;		/* pointer past last used dir slot */
318118611Snjl	u_long bmask;			/* block offset mask */
319151937Sjkim	int lockparent;			/* 1 => lockparent flag is set */
320193529Sjkim	int wantparent;			/* 1 => wantparent or lockparent flag */
321193529Sjkim	int namlen, error;
322118611Snjl	struct vnode **vpp = ap->a_vpp;
323118611Snjl	struct componentname *cnp = ap->a_cnp;
324151937Sjkim	struct ucred *cred = cnp->cn_cred;
325118611Snjl	int flags = cnp->cn_flags;
326118611Snjl	int nameiop = cnp->cn_nameiop;
327151937Sjkim	struct thread *td = cnp->cn_thread;
328118611Snjl
329118611Snjl	int	DIRBLKSIZ = VTOI(ap->a_dvp)->i_e2fs->s_blocksize;
330118611Snjl
331118611Snjl	bp = NULL;
332118611Snjl	slotoffset = -1;
333118611Snjl	*vpp = NULL;
334118611Snjl	vdp = ap->a_dvp;
335118611Snjl	dp = VTOI(vdp);
336118611Snjl	lockparent = flags & LOCKPARENT;
337118611Snjl	wantparent = flags & (LOCKPARENT|WANTPARENT);
338118611Snjl
339118611Snjl	/*
340118611Snjl	 * We now have a segment name to search for, and a directory to search.
341118611Snjl	 */
342118611Snjl
343118611Snjl	/*
344118611Snjl	 * Suppress search for slots unless creating
345118611Snjl	 * file and at end of pathname, in which case
346118611Snjl	 * we watch for a place to put the new file in
347118611Snjl	 * case it doesn't already exist.
348118611Snjl	 */
349118611Snjl	slotstatus = FOUND;
350118611Snjl	slotfreespace = slotsize = slotneeded = 0;
351118611Snjl	if ((nameiop == CREATE || nameiop == RENAME) &&
352118611Snjl	    (flags & ISLASTCN)) {
353118611Snjl		slotstatus = NONE;
354118611Snjl		slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
355118611Snjl		/* was
356118611Snjl		slotneeded = (sizeof(struct direct) - MAXNAMLEN +
357118611Snjl			cnp->cn_namelen + 3) &~ 3; */
358118611Snjl	}
359118611Snjl
360118611Snjl	/*
361118611Snjl	 * If there is cached information on a previous search of
362118611Snjl	 * this directory, pick up where we last left off.
363118611Snjl	 * We cache only lookups as these are the most common
364118611Snjl	 * and have the greatest payoff. Caching CREATE has little
365118611Snjl	 * benefit as it usually must search the entire directory
366118611Snjl	 * to determine that the entry does not exist. Caching the
367118611Snjl	 * location of the last DELETE or RENAME has not reduced
368118611Snjl	 * profiling time and hence has been removed in the interest
369118611Snjl	 * of simplicity.
370118611Snjl	 */
371118611Snjl	bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
372118611Snjl	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
373118611Snjl	    dp->i_diroff > dp->i_size) {
374118611Snjl		entryoffsetinblock = 0;
375118611Snjl		dp->i_offset = 0;
376118611Snjl		numdirpasses = 1;
377118611Snjl	} else {
378118611Snjl		dp->i_offset = dp->i_diroff;
379118611Snjl		if ((entryoffsetinblock = dp->i_offset & bmask) &&
380118611Snjl		    (error = ext2_blkatoff(vdp, (off_t)dp->i_offset, NULL,
381118611Snjl		    &bp)))
382118611Snjl			return (error);
383118611Snjl		numdirpasses = 2;
384118611Snjl		nchstats.ncs_2passes++;
385118611Snjl	}
386118611Snjl	prevoff = dp->i_offset;
387118611Snjl	endsearch = roundup(dp->i_size, DIRBLKSIZ);
388118611Snjl	enduseful = 0;
389118611Snjl
390118611Snjlsearchloop:
391118611Snjl	while (dp->i_offset < endsearch) {
392118611Snjl		/*
393118611Snjl		 * If necessary, get the next directory block.
394118611Snjl		 */
395118611Snjl		if ((dp->i_offset & bmask) == 0) {
396118611Snjl			if (bp != NULL)
397118611Snjl				brelse(bp);
398118611Snjl			if ((error =
399118611Snjl			    ext2_blkatoff(vdp, (off_t)dp->i_offset, NULL,
400118611Snjl			    &bp)) != 0)
401118611Snjl				return (error);
402118611Snjl			entryoffsetinblock = 0;
403118611Snjl		}
404118611Snjl		/*
405118611Snjl		 * If still looking for a slot, and at a DIRBLKSIZE
406118611Snjl		 * boundary, have to start looking for free space again.
407118611Snjl		 */
408118611Snjl		if (slotstatus == NONE &&
409118611Snjl		    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
410151937Sjkim			slotoffset = -1;
411118611Snjl			slotfreespace = 0;
412118611Snjl		}
413118611Snjl		/*
414118611Snjl		 * Get pointer to next entry.
415118611Snjl		 * Full validation checks are slow, so we only check
416118611Snjl		 * enough to insure forward progress through the
417118611Snjl		 * directory. Complete checks can be run by setting
418118611Snjl		 * "vfs.e2fs.dirchk" to be true.
419118611Snjl		 */
420118611Snjl		ep = (struct ext2_dir_entry_2 *)
421118611Snjl			((char *)bp->b_data + entryoffsetinblock);
422118611Snjl		if (ep->rec_len == 0 ||
423118611Snjl		    (dirchk && ext2_dirbadentry(vdp, ep, entryoffsetinblock))) {
424118611Snjl			int i;
425118611Snjl			ext2_dirbad(dp, dp->i_offset, "mangled entry");
426118611Snjl			i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
427118611Snjl			dp->i_offset += i;
428118611Snjl			entryoffsetinblock += i;
429118611Snjl			continue;
430118611Snjl		}
431118611Snjl
432118611Snjl		/*
433118611Snjl		 * If an appropriate sized slot has not yet been found,
434118611Snjl		 * check to see if one is available. Also accumulate space
435118611Snjl		 * in the current block so that we can determine if
436118611Snjl		 * compaction is viable.
437118611Snjl		 */
438118611Snjl		if (slotstatus != FOUND) {
439118611Snjl			int size = ep->rec_len;
440118611Snjl
441118611Snjl			if (ep->inode != 0)
442118611Snjl				size -= EXT2_DIR_REC_LEN(ep->name_len);
443118611Snjl			if (size > 0) {
444118611Snjl				if (size >= slotneeded) {
445118611Snjl					slotstatus = FOUND;
446118611Snjl					slotoffset = dp->i_offset;
447118611Snjl					slotsize = ep->rec_len;
448118611Snjl				} else if (slotstatus == NONE) {
449118611Snjl					slotfreespace += size;
450118611Snjl					if (slotoffset == -1)
451118611Snjl						slotoffset = dp->i_offset;
452118611Snjl					if (slotfreespace >= slotneeded) {
453118611Snjl						slotstatus = COMPACT;
454118611Snjl						slotsize = dp->i_offset +
455118611Snjl						      ep->rec_len - slotoffset;
456118611Snjl					}
457118611Snjl				}
458151937Sjkim			}
459118611Snjl		}
460118611Snjl
461118611Snjl		/*
462118611Snjl		 * Check for a name match.
463118611Snjl		 */
464118611Snjl		if (ep->inode) {
465118611Snjl			namlen = ep->name_len;
466118611Snjl			if (namlen == cnp->cn_namelen &&
467118611Snjl			    !bcmp(cnp->cn_nameptr, ep->name,
468118611Snjl				(unsigned)namlen)) {
469118611Snjl				/*
470118611Snjl				 * Save directory entry's inode number and
471118611Snjl				 * reclen in ndp->ni_ufs area, and release
472118611Snjl				 * directory buffer.
473118611Snjl				 */
474118611Snjl				dp->i_ino = ep->inode;
475118611Snjl				dp->i_reclen = ep->rec_len;
476118611Snjl				goto found;
477118611Snjl			}
478118611Snjl		}
479118611Snjl		prevoff = dp->i_offset;
480118611Snjl		dp->i_offset += ep->rec_len;
481118611Snjl		entryoffsetinblock += ep->rec_len;
482118611Snjl		if (ep->inode)
483118611Snjl			enduseful = dp->i_offset;
484118611Snjl	}
485118611Snjl/* notfound: */
486118611Snjl	/*
487118611Snjl	 * If we started in the middle of the directory and failed
488118611Snjl	 * to find our target, we must check the beginning as well.
489118611Snjl	 */
490118611Snjl	if (numdirpasses == 2) {
491118611Snjl		numdirpasses--;
492118611Snjl		dp->i_offset = 0;
493118611Snjl		endsearch = dp->i_diroff;
494118611Snjl		goto searchloop;
495118611Snjl	}
496118611Snjl	if (bp != NULL)
497118611Snjl		brelse(bp);
498118611Snjl	/*
499118611Snjl	 * If creating, and at end of pathname and current
500118611Snjl	 * directory has not been removed, then can consider
501118611Snjl	 * allowing file to be created.
502118611Snjl	 */
503118611Snjl	if ((nameiop == CREATE || nameiop == RENAME) &&
504118611Snjl	    (flags & ISLASTCN) && dp->i_nlink != 0) {
505118611Snjl		/*
506118611Snjl		 * Access for write is interpreted as allowing
507118611Snjl		 * creation of files in the directory.
508118611Snjl		 */
509118611Snjl		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
510118611Snjl			return (error);
511118611Snjl		/*
512118611Snjl		 * Return an indication of where the new directory
513118611Snjl		 * entry should be put.  If we didn't find a slot,
514118611Snjl		 * then set dp->i_count to 0 indicating
515118611Snjl		 * that the new slot belongs at the end of the
516118611Snjl		 * directory. If we found a slot, then the new entry
517118611Snjl		 * can be put in the range from dp->i_offset to
518118611Snjl		 * dp->i_offset + dp->i_count.
519118611Snjl		 */
520118611Snjl		if (slotstatus == NONE) {
521118611Snjl			dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
522118611Snjl			dp->i_count = 0;
523118611Snjl			enduseful = dp->i_offset;
524118611Snjl		} else {
525118611Snjl			dp->i_offset = slotoffset;
526118611Snjl			dp->i_count = slotsize;
527118611Snjl			if (enduseful < slotoffset + slotsize)
528167802Sjkim				enduseful = slotoffset + slotsize;
529118611Snjl		}
530118611Snjl		dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
531118611Snjl		dp->i_flag |= IN_CHANGE | IN_UPDATE;
532118611Snjl		/*
533118611Snjl		 * We return with the directory locked, so that
534199337Sjkim		 * the parameters we set up above will still be
535118611Snjl		 * valid if we actually decide to do a direnter().
536118611Snjl		 * We return ni_vp == NULL to indicate that the entry
537118611Snjl		 * does not currently exist; we leave a pointer to
538118611Snjl		 * the (locked) directory inode in ndp->ni_dvp.
539118611Snjl		 * The pathname buffer is saved so that the name
540118611Snjl		 * can be obtained later.
541118611Snjl		 *
542118611Snjl		 * NB - if the directory is unlocked, then this
543118611Snjl		 * information cannot be used.
544118611Snjl		 */
545118611Snjl		cnp->cn_flags |= SAVENAME;
546118611Snjl		if (!lockparent)
547118611Snjl			VOP_UNLOCK(vdp, 0, td);
548118611Snjl		return (EJUSTRETURN);
549118611Snjl	}
550118611Snjl	/*
551118611Snjl	 * Insert name into cache (as non-existent) if appropriate.
552118611Snjl	 */
553118611Snjl	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
554118611Snjl		cache_enter(vdp, *vpp, cnp);
555118611Snjl	return (ENOENT);
556118611Snjl
557118611Snjlfound:
558118611Snjl	if (numdirpasses == 2)
559118611Snjl		nchstats.ncs_pass2++;
560118611Snjl	/*
561118611Snjl	 * Check that directory length properly reflects presence
562118611Snjl	 * of this entry.
563118611Snjl	 */
564118611Snjl	if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->name_len)
565167802Sjkim		> dp->i_size) {
566167802Sjkim		ext2_dirbad(dp, dp->i_offset, "i_size too small");
567167802Sjkim		dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->name_len);
568167802Sjkim		dp->i_flag |= IN_CHANGE | IN_UPDATE;
569167802Sjkim	}
570167802Sjkim	brelse(bp);
571167802Sjkim
572167802Sjkim	/*
573167802Sjkim	 * Found component in pathname.
574167802Sjkim	 * If the final component of path name, save information
575167802Sjkim	 * in the cache as to where the entry was found.
576167802Sjkim	 */
577167802Sjkim	if ((flags & ISLASTCN) && nameiop == LOOKUP)
578167802Sjkim		dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
579167802Sjkim
580118611Snjl	/*
581118611Snjl	 * If deleting, and at end of pathname, return
582118611Snjl	 * parameters which can be used to remove file.
583118611Snjl	 * If the wantparent flag isn't set, we return only
584118611Snjl	 * the directory (in ndp->ni_dvp), otherwise we go
585118611Snjl	 * on and lock the inode, being careful with ".".
586118611Snjl	 */
587118611Snjl	if (nameiop == DELETE && (flags & ISLASTCN)) {
588118611Snjl		/*
589118611Snjl		 * Write access to directory required to delete files.
590118611Snjl		 */
591118611Snjl		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
592118611Snjl			return (error);
593118611Snjl		/*
594118611Snjl		 * Return pointer to current entry in dp->i_offset,
595118611Snjl		 * and distance past previous entry (if there
596118611Snjl		 * is a previous entry in this block) in dp->i_count.
597118611Snjl		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
598118611Snjl		 */
599118611Snjl		if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
600118611Snjl			dp->i_count = 0;
601118611Snjl		else
602118611Snjl			dp->i_count = dp->i_offset - prevoff;
603118611Snjl		if (dp->i_number == dp->i_ino) {
604118611Snjl			VREF(vdp);
605118611Snjl			*vpp = vdp;
606118611Snjl			return (0);
607118611Snjl		}
608118611Snjl		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
609118611Snjl		    &tdp)) != 0)
610118611Snjl			return (error);
611118611Snjl		/*
612118611Snjl		 * If directory is "sticky", then user must own
613118611Snjl		 * the directory, or the file in it, else she
614118611Snjl		 * may not delete it (unless she's root). This
615118611Snjl		 * implements append-only directories.
616118611Snjl		 */
617118611Snjl		if ((dp->i_mode & ISVTX) &&
618118611Snjl		    cred->cr_uid != 0 &&
619118611Snjl		    cred->cr_uid != dp->i_uid &&
620118611Snjl		    VTOI(tdp)->i_uid != cred->cr_uid) {
621118611Snjl			vput(tdp);
622118611Snjl			return (EPERM);
623118611Snjl		}
624118611Snjl		*vpp = tdp;
625118611Snjl		if (!lockparent)
626118611Snjl			VOP_UNLOCK(vdp, 0, td);
627118611Snjl		return (0);
628118611Snjl	}
629118611Snjl
630118611Snjl	/*
631118611Snjl	 * If rewriting (RENAME), return the inode and the
632118611Snjl	 * information required to rewrite the present directory
633118611Snjl	 * Must get inode of directory entry to verify it's a
634118611Snjl	 * regular file, or empty directory.
635118611Snjl	 */
636118611Snjl	if (nameiop == RENAME && wantparent &&
637118611Snjl	    (flags & ISLASTCN)) {
638118611Snjl		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
639118611Snjl			return (error);
640118611Snjl		/*
641118611Snjl		 * Careful about locking second inode.
642118611Snjl		 * This can only occur if the target is ".".
643118611Snjl		 */
644118611Snjl		if (dp->i_number == dp->i_ino)
645118611Snjl			return (EISDIR);
646118611Snjl		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
647118611Snjl		    &tdp)) != 0)
648118611Snjl			return (error);
649151937Sjkim		*vpp = tdp;
650118611Snjl		cnp->cn_flags |= SAVENAME;
651118611Snjl		if (!lockparent)
652151937Sjkim			VOP_UNLOCK(vdp, 0, td);
653151937Sjkim		return (0);
654118611Snjl	}
655118611Snjl
656118611Snjl	/*
657118611Snjl	 * Step through the translation in the name.  We do not `vput' the
658118611Snjl	 * directory because we may need it again if a symbolic link
659118611Snjl	 * is relative to the current directory.  Instead we save it
660118611Snjl	 * unlocked as "pdp".  We must get the target inode before unlocking
661118611Snjl	 * the directory to insure that the inode will not be removed
662118611Snjl	 * before we get it.  We prevent deadlock by always fetching
663118611Snjl	 * inodes from the root, moving down the directory tree. Thus
664118611Snjl	 * when following backward pointers ".." we must unlock the
665118611Snjl	 * parent directory before getting the requested directory.
666118611Snjl	 * There is a potential race condition here if both the current
667118611Snjl	 * and parent directories are removed before the VFS_VGET for the
668118611Snjl	 * inode associated with ".." returns.  We hope that this occurs
669118611Snjl	 * infrequently since we cannot avoid this race condition without
670118611Snjl	 * implementing a sophisticated deadlock detection algorithm.
671	 * Note also that this simple deadlock detection scheme will not
672	 * work if the file system has any hard links other than ".."
673	 * that point backwards in the directory structure.
674	 */
675	pdp = vdp;
676	if (flags & ISDOTDOT) {
677		VOP_UNLOCK(pdp, 0, td);	/* race to get the inode */
678		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
679		    &tdp)) != 0) {
680			vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
681			return (error);
682		}
683		if (lockparent && (flags & ISLASTCN) &&
684		    (error = vn_lock(pdp, LK_EXCLUSIVE, td))) {
685			vput(tdp);
686			return (error);
687		}
688		*vpp = tdp;
689	} else if (dp->i_number == dp->i_ino) {
690		VREF(vdp);	/* we want ourself, ie "." */
691		*vpp = vdp;
692	} else {
693		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
694		    &tdp)) != 0)
695			return (error);
696		if (!lockparent || !(flags & ISLASTCN))
697			VOP_UNLOCK(pdp, 0, td);
698		*vpp = tdp;
699	}
700
701	/*
702	 * Insert name into cache if appropriate.
703	 */
704	if (cnp->cn_flags & MAKEENTRY)
705		cache_enter(vdp, *vpp, cnp);
706	return (0);
707}
708
709void
710ext2_dirbad(ip, offset, how)
711	struct inode *ip;
712	doff_t offset;
713	char *how;
714{
715	struct mount *mp;
716
717	mp = ITOV(ip)->v_mount;
718	(void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
719	    mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
720	if ((mp->mnt_flag & MNT_RDONLY) == 0)
721		panic("ext2_dirbad: bad dir");
722}
723
724/*
725 * Do consistency checking on a directory entry:
726 *	record length must be multiple of 4
727 *	entry must fit in rest of its DIRBLKSIZ block
728 *	record must be large enough to contain entry
729 *	name is not longer than MAXNAMLEN
730 *	name must be as long as advertised, and null terminated
731 */
732/*
733 *	changed so that it confirms to ext2_check_dir_entry
734 */
735static int
736ext2_dirbadentry(dp, de, entryoffsetinblock)
737	struct vnode *dp;
738	struct ext2_dir_entry_2 *de;
739	int entryoffsetinblock;
740{
741	int	DIRBLKSIZ = VTOI(dp)->i_e2fs->s_blocksize;
742
743	char * error_msg = NULL;
744
745	if (de->rec_len < EXT2_DIR_REC_LEN(1))
746		error_msg = "rec_len is smaller than minimal";
747	else if (de->rec_len % 4 != 0)
748		error_msg = "rec_len % 4 != 0";
749	else if (de->rec_len < EXT2_DIR_REC_LEN(de->name_len))
750		error_msg = "reclen is too small for name_len";
751	else if (entryoffsetinblock + de->rec_len > DIRBLKSIZ)
752		error_msg = "directory entry across blocks";
753	/* else LATER
754	     if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
755		error_msg = "inode out of bounds";
756	*/
757
758	if (error_msg != NULL) {
759		printf("bad directory entry: %s\n", error_msg);
760		printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
761			entryoffsetinblock, (unsigned long)de->inode,
762			de->rec_len, de->name_len);
763	}
764	return error_msg == NULL ? 0 : 1;
765}
766
767/*
768 * Write a directory entry after a call to namei, using the parameters
769 * that it left in nameidata.  The argument ip is the inode which the new
770 * directory entry will refer to.  Dvp is a pointer to the directory to
771 * be written, which was left locked by namei. Remaining parameters
772 * (dp->i_offset, dp->i_count) indicate how the space for the new
773 * entry is to be obtained.
774 */
775int
776ext2_direnter(ip, dvp, cnp)
777	struct inode *ip;
778	struct vnode *dvp;
779	struct componentname *cnp;
780{
781	struct ext2_dir_entry_2 *ep, *nep;
782	struct inode *dp;
783	struct buf *bp;
784	struct ext2_dir_entry_2 newdir;
785	struct iovec aiov;
786	struct uio auio;
787	u_int dsize;
788	int error, loc, newentrysize, spacefree;
789	char *dirbuf;
790	int     DIRBLKSIZ = ip->i_e2fs->s_blocksize;
791
792
793#if DIAGNOSTIC
794	if ((cnp->cn_flags & SAVENAME) == 0)
795		panic("direnter: missing name");
796#endif
797	dp = VTOI(dvp);
798	newdir.inode = ip->i_number;
799	newdir.name_len = cnp->cn_namelen;
800	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
801	    EXT2_FEATURE_INCOMPAT_FILETYPE))
802		newdir.file_type = DTTOFT(IFTODT(ip->i_mode));
803	else
804		newdir.file_type = EXT2_FT_UNKNOWN;
805	bcopy(cnp->cn_nameptr, newdir.name, (unsigned)cnp->cn_namelen + 1);
806	newentrysize = EXT2_DIR_REC_LEN(newdir.name_len);
807	if (dp->i_count == 0) {
808		/*
809		 * If dp->i_count is 0, then namei could find no
810		 * space in the directory. Here, dp->i_offset will
811		 * be on a directory block boundary and we will write the
812		 * new entry into a fresh block.
813		 */
814		if (dp->i_offset & (DIRBLKSIZ - 1))
815			panic("ext2_direnter: newblk");
816		auio.uio_offset = dp->i_offset;
817		newdir.rec_len = DIRBLKSIZ;
818		auio.uio_resid = newentrysize;
819		aiov.iov_len = newentrysize;
820		aiov.iov_base = (caddr_t)&newdir;
821		auio.uio_iov = &aiov;
822		auio.uio_iovcnt = 1;
823		auio.uio_rw = UIO_WRITE;
824		auio.uio_segflg = UIO_SYSSPACE;
825		auio.uio_td = (struct thread *)0;
826		error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
827		if (DIRBLKSIZ >
828		    VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
829			/* XXX should grow with balloc() */
830			panic("ext2_direnter: frag size");
831		else if (!error) {
832			dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
833			dp->i_flag |= IN_CHANGE;
834		}
835		return (error);
836	}
837
838	/*
839	 * If dp->i_count is non-zero, then namei found space
840	 * for the new entry in the range dp->i_offset to
841	 * dp->i_offset + dp->i_count in the directory.
842	 * To use this space, we may have to compact the entries located
843	 * there, by copying them together towards the beginning of the
844	 * block, leaving the free space in one usable chunk at the end.
845	 */
846
847	/*
848	 * Increase size of directory if entry eats into new space.
849	 * This should never push the size past a new multiple of
850	 * DIRBLKSIZE.
851	 *
852	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
853	 */
854	if (dp->i_offset + dp->i_count > dp->i_size)
855		dp->i_size = dp->i_offset + dp->i_count;
856	/*
857	 * Get the block containing the space for the new directory entry.
858	 */
859	if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
860	    &bp)) != 0)
861		return (error);
862	/*
863	 * Find space for the new entry. In the simple case, the entry at
864	 * offset base will have the space. If it does not, then namei
865	 * arranged that compacting the region dp->i_offset to
866	 * dp->i_offset + dp->i_count would yield the
867	 * space.
868	 */
869	ep = (struct ext2_dir_entry_2 *)dirbuf;
870	dsize = EXT2_DIR_REC_LEN(ep->name_len);
871	spacefree = ep->rec_len - dsize;
872	for (loc = ep->rec_len; loc < dp->i_count; ) {
873		nep = (struct ext2_dir_entry_2 *)(dirbuf + loc);
874		if (ep->inode) {
875			/* trim the existing slot */
876			ep->rec_len = dsize;
877			ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
878		} else {
879			/* overwrite; nothing there; header is ours */
880			spacefree += dsize;
881		}
882		dsize = EXT2_DIR_REC_LEN(nep->name_len);
883		spacefree += nep->rec_len - dsize;
884		loc += nep->rec_len;
885		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
886	}
887	/*
888	 * Update the pointer fields in the previous entry (if any),
889	 * copy in the new entry, and write out the block.
890	 */
891	if (ep->inode == 0) {
892		if (spacefree + dsize < newentrysize)
893			panic("ext2_direnter: compact1");
894		newdir.rec_len = spacefree + dsize;
895	} else {
896		if (spacefree < newentrysize)
897			panic("ext2_direnter: compact2");
898		newdir.rec_len = spacefree;
899		ep->rec_len = dsize;
900		ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
901	}
902	bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
903	error = BUF_WRITE(bp);
904	dp->i_flag |= IN_CHANGE | IN_UPDATE;
905	if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
906		error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
907		    cnp->cn_cred, cnp->cn_thread);
908	return (error);
909}
910
911/*
912 * Remove a directory entry after a call to namei, using
913 * the parameters which it left in nameidata. The entry
914 * dp->i_offset contains the offset into the directory of the
915 * entry to be eliminated.  The dp->i_count field contains the
916 * size of the previous record in the directory.  If this
917 * is 0, the first entry is being deleted, so we need only
918 * zero the inode number to mark the entry as free.  If the
919 * entry is not the first in the directory, we must reclaim
920 * the space of the now empty record by adding the record size
921 * to the size of the previous entry.
922 */
923int
924ext2_dirremove(dvp, cnp)
925	struct vnode *dvp;
926	struct componentname *cnp;
927{
928	struct inode *dp;
929	struct ext2_dir_entry_2 *ep;
930	struct buf *bp;
931	int error;
932
933	dp = VTOI(dvp);
934	if (dp->i_count == 0) {
935		/*
936		 * First entry in block: set d_ino to zero.
937		 */
938		if ((error =
939		    ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
940		    &bp)) != 0)
941			return (error);
942		ep->inode = 0;
943		error = BUF_WRITE(bp);
944		dp->i_flag |= IN_CHANGE | IN_UPDATE;
945		return (error);
946	}
947	/*
948	 * Collapse new free space into previous entry.
949	 */
950	if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
951	    (char **)&ep, &bp)) != 0)
952		return (error);
953	ep->rec_len += dp->i_reclen;
954	error = BUF_WRITE(bp);
955	dp->i_flag |= IN_CHANGE | IN_UPDATE;
956	return (error);
957}
958
959/*
960 * Rewrite an existing directory entry to point at the inode
961 * supplied.  The parameters describing the directory entry are
962 * set up by a call to namei.
963 */
964int
965ext2_dirrewrite(dp, ip, cnp)
966	struct inode *dp, *ip;
967	struct componentname *cnp;
968{
969	struct buf *bp;
970	struct ext2_dir_entry_2 *ep;
971	struct vnode *vdp = ITOV(dp);
972	int error;
973
974	if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
975	    &bp)) != 0)
976		return (error);
977	ep->inode = ip->i_number;
978	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
979	    EXT2_FEATURE_INCOMPAT_FILETYPE))
980		ep->file_type = DTTOFT(IFTODT(ip->i_mode));
981	else
982		ep->file_type = EXT2_FT_UNKNOWN;
983	error = BUF_WRITE(bp);
984	dp->i_flag |= IN_CHANGE | IN_UPDATE;
985	return (error);
986}
987
988/*
989 * Check if a directory is empty or not.
990 * Inode supplied must be locked.
991 *
992 * Using a struct dirtemplate here is not precisely
993 * what we want, but better than using a struct direct.
994 *
995 * NB: does not handle corrupted directories.
996 */
997int
998ext2_dirempty(ip, parentino, cred)
999	struct inode *ip;
1000	ino_t parentino;
1001	struct ucred *cred;
1002{
1003	off_t off;
1004	struct dirtemplate dbuf;
1005	struct ext2_dir_entry_2 *dp = (struct ext2_dir_entry_2 *)&dbuf;
1006	int error, count, namlen;
1007#define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1008
1009	for (off = 0; off < ip->i_size; off += dp->rec_len) {
1010		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1011		    off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1012		    NOCRED, &count, (struct thread *)0);
1013		/*
1014		 * Since we read MINDIRSIZ, residual must
1015		 * be 0 unless we're at end of file.
1016		 */
1017		if (error || count != 0)
1018			return (0);
1019		/* avoid infinite loops */
1020		if (dp->rec_len == 0)
1021			return (0);
1022		/* skip empty entries */
1023		if (dp->inode == 0)
1024			continue;
1025		/* accept only "." and ".." */
1026		namlen = dp->name_len;
1027		if (namlen > 2)
1028			return (0);
1029		if (dp->name[0] != '.')
1030			return (0);
1031		/*
1032		 * At this point namlen must be 1 or 2.
1033		 * 1 implies ".", 2 implies ".." if second
1034		 * char is also "."
1035		 */
1036		if (namlen == 1)
1037			continue;
1038		if (dp->name[1] == '.' && dp->inode == parentino)
1039			continue;
1040		return (0);
1041	}
1042	return (1);
1043}
1044
1045/*
1046 * Check if source directory is in the path of the target directory.
1047 * Target is supplied locked, source is unlocked.
1048 * The target is always vput before returning.
1049 */
1050int
1051ext2_checkpath(source, target, cred)
1052	struct inode *source, *target;
1053	struct ucred *cred;
1054{
1055	struct vnode *vp;
1056	int error, rootino, namlen;
1057	struct dirtemplate dirbuf;
1058
1059	vp = ITOV(target);
1060	if (target->i_number == source->i_number) {
1061		error = EEXIST;
1062		goto out;
1063	}
1064	rootino = ROOTINO;
1065	error = 0;
1066	if (target->i_number == rootino)
1067		goto out;
1068
1069	for (;;) {
1070		if (vp->v_type != VDIR) {
1071			error = ENOTDIR;
1072			break;
1073		}
1074		error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1075			sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1076			IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, (int *)0,
1077			(struct thread *)0);
1078		if (error != 0)
1079			break;
1080		namlen = dirbuf.dotdot_type;	/* like ufs little-endian */
1081		if (namlen != 2 ||
1082		    dirbuf.dotdot_name[0] != '.' ||
1083		    dirbuf.dotdot_name[1] != '.') {
1084			error = ENOTDIR;
1085			break;
1086		}
1087		if (dirbuf.dotdot_ino == source->i_number) {
1088			error = EINVAL;
1089			break;
1090		}
1091		if (dirbuf.dotdot_ino == rootino)
1092			break;
1093		vput(vp);
1094		if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1095		    LK_EXCLUSIVE, &vp)) != 0) {
1096			vp = NULL;
1097			break;
1098		}
1099	}
1100
1101out:
1102	if (error == ENOTDIR)
1103		printf("checkpath: .. not a directory\n");
1104	if (vp != NULL)
1105		vput(vp);
1106	return (error);
1107}
1108