Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_lookup.c revision 1.3
      1 /*	$NetBSD: ext2fs_lookup.c,v 1.3 1997/10/09 15:42:51 bouyer Exp $	*/
      2 
      3 /*
      4  * Modified for NetBSD 1.2E
      5  * May 1997, Manuel Bouyer
      6  * Laboratoire d'informatique de Paris VI
      7  */
      8 /*
      9  *  modified for Lites 1.1
     10  *
     11  *  Aug 1995, Godmar Back (gback (at) cs.utah.edu)
     12  *  University of Utah, Department of Computer Science
     13  */
     14 /*
     15  * Copyright (c) 1989, 1993
     16  *	The Regents of the University of California.  All rights reserved.
     17  * (c) UNIX System Laboratories, Inc.
     18  * All or some portions of this file are derived from material licensed
     19  * to the University of California by American Telephone and Telegraph
     20  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     21  * the permission of UNIX System Laboratories, Inc.
     22  *
     23  * Redistribution and use in source and binary forms, with or without
     24  * modification, are permitted provided that the following conditions
     25  * are met:
     26  * 1. Redistributions of source code must retain the above copyright
     27  *	notice, this list of conditions and the following disclaimer.
     28  * 2. Redistributions in binary form must reproduce the above copyright
     29  *	notice, this list of conditions and the following disclaimer in the
     30  *	documentation and/or other materials provided with the distribution.
     31  * 3. All advertising materials mentioning features or use of this software
     32  *	must display the following acknowledgement:
     33  *	This product includes software developed by the University of
     34  *	California, Berkeley and its contributors.
     35  * 4. Neither the name of the University nor the names of its contributors
     36  *	may be used to endorse or promote products derived from this software
     37  *	without specific prior written permission.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     49  * SUCH DAMAGE.
     50  *
     51  *	@(#)ufs_lookup.c	8.6 (Berkeley) 4/1/94
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/namei.h>
     57 #include <sys/buf.h>
     58 #include <sys/file.h>
     59 #include <sys/mount.h>
     60 #include <sys/vnode.h>
     61 #include <sys/malloc.h>
     62 #include <sys/dirent.h>
     63 
     64 #include <ufs/ufs/quota.h>
     65 #include <ufs/ufs/inode.h>
     66 #include <ufs/ufs/ufsmount.h>
     67 #include <ufs/ufs/ufs_extern.h>
     68 
     69 #include <ufs/ext2fs/ext2fs_extern.h>
     70 #include <ufs/ext2fs/ext2fs_dir.h>
     71 #include <ufs/ext2fs/ext2fs.h>
     72 
     73 extern	int dirchk;
     74 
     75 static void	ext2fs_dirconv2ffs __P((struct ext2fs_direct *e2dir,
     76 					  struct dirent *ffsdir));
     77 static int	ext2fs_dirbadentry __P((struct vnode *dp,
     78 					  struct ext2fs_direct *de,
     79 					  int entryoffsetinblock));
     80 
     81 /*
     82  * the problem that is tackled below is the fact that FFS
     83  * includes the terminating zero on disk while EXT2FS doesn't
     84  * this implies that we need to introduce some padding.
     85  * For instance, a filename "sbin" has normally a reclen 12
     86  * in EXT2, but 16 in FFS.
     87  * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
     88  * If it wasn't for that, the complete ufs code for directories would
     89  * have worked w/o changes (except for the difference in DIRBLKSIZ)
     90  */
     91 static void
     92 ext2fs_dirconv2ffs( e2dir, ffsdir)
     93 	struct ext2fs_direct	*e2dir;
     94 	struct dirent 		*ffsdir;
     95 {
     96 	bzero(ffsdir, sizeof(struct dirent));
     97 	ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
     98 	ffsdir->d_namlen = fs2h16(e2dir->e2d_namlen);
     99 
    100 	ffsdir->d_type = DT_UNKNOWN;		/* don't know more here */
    101 #ifdef DIAGNOSTIC
    102 	/*
    103 	 * XXX Rigth now this can't happen, but if one day
    104 	 * MAXNAMLEN != E2FS_MAXNAMLEN we should handle this more gracefully !
    105 	 */
    106 	if (fs2h16(e2dir->e2d_namlen) > MAXNAMLEN)
    107 		panic("ext2fs: e2dir->e2d_namlen\n");
    108 #endif
    109 	strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
    110 
    111 	/* Godmar thinks: since e2dir->e2d_reclen can be big and means
    112 	   nothing anyway, we compute our own reclen according to what
    113 	   we think is right
    114 	 */
    115 	ffsdir->d_reclen = DIRENT_SIZE(ffsdir);
    116 }
    117 
    118 /*
    119  * Vnode op for reading directories.
    120  *
    121  * Convert the on-disk entries to <sys/dirent.h> entries.
    122  * the problem is that the conversion will blow up some entries by four bytes,
    123  * so it can't be done in place. This is too bad. Right now the conversion is
    124  * done entry by entry, the converted entry is sent via uiomove.
    125  *
    126  * XXX allocate a buffer, convert as many entries as possible, then send
    127  * the whole buffer to uiomove
    128  */
    129 int
    130 ext2fs_readdir(v)
    131 	void *v;
    132 {
    133 	struct vop_readdir_args /* {
    134 		struct vnode *a_vp;
    135 		struct uio *a_uio;
    136 		struct ucred *a_cred;
    137 		int *a_eofflag;
    138 		u_long *a_cookies;
    139 		int ncookies;
    140 	} */ *ap = v;
    141 	register struct uio *uio = ap->a_uio;
    142 	int error;
    143 	size_t e2fs_count, readcnt;
    144 	struct m_ext2fs *fs = VTOI(ap->a_vp)->i_e2fs;
    145 
    146 	struct ext2fs_direct *dp;
    147 	struct dirent dstd;
    148 	struct uio auio;
    149 	struct iovec aiov;
    150 	caddr_t dirbuf;
    151 	off_t off = uio->uio_offset;
    152 	u_long *cookies = ap->a_cookies;
    153 	int ncookies = ap->a_ncookies;
    154 	int e2d_reclen;
    155 
    156 	e2fs_count = uio->uio_resid;
    157 	/* Make sure we don't return partial entries. */
    158 	e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
    159 	if (e2fs_count <= 0)
    160 		return (EINVAL);
    161 
    162 	auio = *uio;
    163 	auio.uio_iov = &aiov;
    164 	auio.uio_iovcnt = 1;
    165 	auio.uio_segflg = UIO_SYSSPACE;
    166 	aiov.iov_len = e2fs_count;
    167 	auio.uio_resid = e2fs_count;
    168 	MALLOC(dirbuf, caddr_t, e2fs_count, M_TEMP, M_WAITOK);
    169 	bzero(dirbuf, e2fs_count);
    170 	aiov.iov_base = dirbuf;
    171 
    172 	error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
    173 	if (error == 0) {
    174 		readcnt = e2fs_count - auio.uio_resid;
    175 		for (dp = (struct ext2fs_direct *)dirbuf;
    176 			(char *)dp < (char *)dirbuf + readcnt; ) {
    177 			e2d_reclen = fs2h16(dp->e2d_reclen);
    178 			if (e2d_reclen == 0) {
    179 				error = EIO;
    180 				break;
    181 			}
    182 			ext2fs_dirconv2ffs(dp, &dstd);
    183 			if(dstd.d_reclen > uio->uio_resid) {
    184 				break;
    185 			}
    186 			if ((error = uiomove((caddr_t)&dstd, dstd.d_reclen, uio)) != 0) {
    187 				break;
    188 			}
    189 			off = off + e2d_reclen;
    190 			if (cookies != NULL) {
    191 				*cookies++ = off;
    192 				if (--ncookies <= 0){
    193 					break;  /* out of cookies */
    194 				}
    195 			}
    196 			/* advance dp */
    197 			dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
    198 		}
    199 		/* we need to correct uio_offset */
    200 		uio->uio_offset = off;
    201 	}
    202 	FREE(dirbuf, M_TEMP);
    203 	*ap->a_eofflag = VTOI(ap->a_vp)->i_e2fs_size <= uio->uio_offset;
    204 	return (error);
    205 }
    206 
    207 /*
    208  * Convert a component of a pathname into a pointer to a locked inode.
    209  * This is a very central and rather complicated routine.
    210  * If the file system is not maintained in a strict tree hierarchy,
    211  * this can result in a deadlock situation (see comments in code below).
    212  *
    213  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
    214  * on whether the name is to be looked up, created, renamed, or deleted.
    215  * When CREATE, RENAME, or DELETE is specified, information usable in
    216  * creating, renaming, or deleting a directory entry may be calculated.
    217  * If flag has LOCKPARENT or'ed into it and the target of the pathname
    218  * exists, lookup returns both the target and its parent directory locked.
    219  * When creating or renaming and LOCKPARENT is specified, the target may
    220  * not be ".".  When deleting and LOCKPARENT is specified, the target may
    221  * be "."., but the caller must check to ensure it does an vrele and vput
    222  * instead of two vputs.
    223  *
    224  * Overall outline of ext2fs_lookup:
    225  *
    226  *	check accessibility of directory
    227  *	look for name in cache, if found, then if at end of path
    228  *	  and deleting or creating, drop it, else return name
    229  *	search for name in directory, to found or notfound
    230  * notfound:
    231  *	if creating, return locked directory, leaving info on available slots
    232  *	else return error
    233  * found:
    234  *	if at end of path and deleting, return information to allow delete
    235  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
    236  *	  inode and return info to allow rewrite
    237  *	if not at end, add name to cache; if at end and neither creating
    238  *	  nor deleting, add name to cache
    239  */
    240 int
    241 ext2fs_lookup(v)
    242 	void *v;
    243 {
    244 	struct vop_lookup_args /* {
    245 		struct vnode *a_dvp;
    246 		struct vnode **a_vpp;
    247 		struct componentname *a_cnp;
    248 	} */ *ap = v;
    249 	register struct vnode *vdp;	/* vnode for directory being searched */
    250 	register struct inode *dp;	/* inode for directory being searched */
    251 	struct buf *bp;			/* a buffer of directory entries */
    252 	register struct ext2fs_direct *ep; /* the current directory entry */
    253 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
    254 	enum {NONE, COMPACT, FOUND} slotstatus;
    255 	doff_t slotoffset;		/* offset of area with free space */
    256 	int slotsize;			/* size of area at slotoffset */
    257 	int slotfreespace;		/* amount of space free in slot */
    258 	int slotneeded;			/* size of the entry we're seeking */
    259 	int numdirpasses;		/* strategy for directory search */
    260 	doff_t endsearch;		/* offset to end directory search */
    261 	doff_t prevoff;			/* prev entry dp->i_offset */
    262 	struct vnode *pdp;		/* saved dp during symlink work */
    263 	struct vnode *tdp;		/* returned by VFS_VGET */
    264 	doff_t enduseful;		/* pointer past last used dir slot */
    265 	u_long bmask;			/* block offset mask */
    266 	int lockparent;			/* 1 => lockparent flag is set */
    267 	int wantparent;			/* 1 => wantparent or lockparent flag */
    268 	int namlen, error;
    269 	struct vnode **vpp = ap->a_vpp;
    270 	struct componentname *cnp = ap->a_cnp;
    271 	struct ucred *cred = cnp->cn_cred;
    272 	int flags = cnp->cn_flags;
    273 	int nameiop = cnp->cn_nameiop;
    274 
    275 	int	dirblksize = VTOI(ap->a_dvp)->i_e2fs->e2fs_bsize;
    276 
    277 	bp = NULL;
    278 	slotoffset = -1;
    279 	*vpp = NULL;
    280 	vdp = ap->a_dvp;
    281 	dp = VTOI(vdp);
    282 	lockparent = flags & LOCKPARENT;
    283 	wantparent = flags & (LOCKPARENT|WANTPARENT);
    284 	/*
    285 	 * Check accessiblity of directory.
    286 	 */
    287 	if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
    288 		return (error);
    289 
    290 	/*
    291 	 * We now have a segment name to search for, and a directory to search.
    292 	 *
    293 	 * Before tediously performing a linear scan of the directory,
    294 	 * check the name cache to see if the directory/name pair
    295 	 * we are looking for is known already.
    296 	 */
    297 	if ((error = cache_lookup(vdp, vpp, cnp)) != 0) {
    298 		int vpid;	/* capability number of vnode */
    299 
    300 		if (error == ENOENT)
    301 			return (error);
    302 		/*
    303 		 * Get the next vnode in the path.
    304 		 * See comment below starting `Step through' for
    305 		 * an explaination of the locking protocol.
    306 		 */
    307 		pdp = vdp;
    308 		dp = VTOI(*vpp);
    309 		vdp = *vpp;
    310 		vpid = vdp->v_id;
    311 		if (pdp == vdp) {   /* lookup on "." */
    312 			VREF(vdp);
    313 			error = 0;
    314 		} else if (flags & ISDOTDOT) {
    315 			VOP_UNLOCK(pdp);
    316 			error = vget(vdp, 1);
    317 			if (!error && lockparent && (flags & ISLASTCN))
    318 				error = VOP_LOCK(pdp);
    319 		} else {
    320 			error = vget(vdp, 1);
    321 			if (!lockparent || error || !(flags & ISLASTCN))
    322 				VOP_UNLOCK(pdp);
    323 		}
    324 		/*
    325 		 * Check that the capability number did not change
    326 		 * while we were waiting for the lock.
    327 		 */
    328 		if (!error) {
    329 			if (vpid == vdp->v_id)
    330 				return (0);
    331 			vput(vdp);
    332 			if (lockparent && pdp != vdp && (flags & ISLASTCN))
    333 				VOP_UNLOCK(pdp);
    334 		}
    335 		if ((error = VOP_LOCK(pdp)) != 0)
    336 			return (error);
    337 		vdp = pdp;
    338 		dp = VTOI(pdp);
    339 		*vpp = NULL;
    340 	}
    341 
    342 	/*
    343 	 * Suppress search for slots unless creating
    344 	 * file and at end of pathname, in which case
    345 	 * we watch for a place to put the new file in
    346 	 * case it doesn't already exist.
    347 	 */
    348 	slotstatus = FOUND;
    349 	slotfreespace = slotsize = slotneeded = 0;
    350 	if ((nameiop == CREATE || nameiop == RENAME) &&
    351 		(flags & ISLASTCN)) {
    352 		slotstatus = NONE;
    353 		slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
    354 	}
    355 
    356 	/*
    357 	 * If there is cached information on a previous search of
    358 	 * this directory, pick up where we last left off.
    359 	 * We cache only lookups as these are the most common
    360 	 * and have the greatest payoff. Caching CREATE has little
    361 	 * benefit as it usually must search the entire directory
    362 	 * to determine that the entry does not exist. Caching the
    363 	 * location of the last DELETE or RENAME has not reduced
    364 	 * profiling time and hence has been removed in the interest
    365 	 * of simplicity.
    366 	 */
    367 	bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
    368 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
    369 		dp->i_diroff > dp->i_e2fs_size) {
    370 		entryoffsetinblock = 0;
    371 		dp->i_offset = 0;
    372 		numdirpasses = 1;
    373 	} else {
    374 		dp->i_offset = dp->i_diroff;
    375 		if ((entryoffsetinblock = dp->i_offset & bmask) &&
    376 			(error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
    377 			return (error);
    378 		numdirpasses = 2;
    379 	}
    380 	prevoff = dp->i_offset;
    381 	endsearch = roundup(dp->i_e2fs_size, dirblksize);
    382 	enduseful = 0;
    383 
    384 searchloop:
    385 	while (dp->i_offset < endsearch) {
    386 		/*
    387 		 * If necessary, get the next directory block.
    388 		 */
    389 		if ((dp->i_offset & bmask) == 0) {
    390 			if (bp != NULL)
    391 				brelse(bp);
    392 			error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp);
    393 			if (error != 0)
    394 				return (error);
    395 			entryoffsetinblock = 0;
    396 		}
    397 		/*
    398 		 * If still looking for a slot, and at a dirblksize
    399 		 * boundary, have to start looking for free space again.
    400 		 */
    401 		if (slotstatus == NONE &&
    402 			(entryoffsetinblock & (dirblksize - 1)) == 0) {
    403 			slotoffset = -1;
    404 			slotfreespace = 0;
    405 		}
    406 		/*
    407 		 * Get pointer to next entry.
    408 		 * Full validation checks are slow, so we only check
    409 		 * enough to insure forward progress through the
    410 		 * directory. Complete checks can be run by patching
    411 		 * "dirchk" to be true.
    412 		 */
    413 		ep = (struct ext2fs_direct *)
    414 			((char *)bp->b_data + entryoffsetinblock);
    415 		if (ep->e2d_reclen == 0 ||
    416 			(dirchk && ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
    417 			int i;
    418 			ufs_dirbad(dp, dp->i_offset, "mangled entry");
    419 			i = dirblksize - (entryoffsetinblock & (dirblksize - 1));
    420 			dp->i_offset += i;
    421 			entryoffsetinblock += i;
    422 			continue;
    423 		}
    424 
    425 		/*
    426 		 * If an appropriate sized slot has not yet been found,
    427 		 * check to see if one is available. Also accumulate space
    428 		 * in the current block so that we can determine if
    429 		 * compaction is viable.
    430 		 */
    431 		if (slotstatus != FOUND) {
    432 			int size = fs2h16(ep->e2d_reclen);
    433 
    434 			if (ep->e2d_ino != 0)
    435 				size -= EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
    436 			if (size > 0) {
    437 				if (size >= slotneeded) {
    438 					slotstatus = FOUND;
    439 					slotoffset = dp->i_offset;
    440 					slotsize = fs2h16(ep->e2d_reclen);
    441 				} else if (slotstatus == NONE) {
    442 					slotfreespace += size;
    443 					if (slotoffset == -1)
    444 						slotoffset = dp->i_offset;
    445 					if (slotfreespace >= slotneeded) {
    446 						slotstatus = COMPACT;
    447 						slotsize = dp->i_offset +
    448 							  fs2h16(ep->e2d_reclen) - slotoffset;
    449 					}
    450 				}
    451 			}
    452 		}
    453 
    454 		/*
    455 		 * Check for a name match.
    456 		 */
    457 		if (ep->e2d_ino) {
    458 			namlen = fs2h16(ep->e2d_namlen);
    459 			if (namlen == cnp->cn_namelen &&
    460 				!bcmp(cnp->cn_nameptr, ep->e2d_name,
    461 				(unsigned)namlen)) {
    462 				/*
    463 				 * Save directory entry's inode number and
    464 				 * reclen in ndp->ni_ufs area, and release
    465 				 * directory buffer.
    466 				 */
    467 				dp->i_ino = fs2h32(ep->e2d_ino);
    468 				dp->i_reclen = fs2h16(ep->e2d_reclen);
    469 				brelse(bp);
    470 				goto found;
    471 			}
    472 		}
    473 		prevoff = dp->i_offset;
    474 		dp->i_offset += fs2h16(ep->e2d_reclen);
    475 		entryoffsetinblock += fs2h16(ep->e2d_reclen);
    476 		if (ep->e2d_ino)
    477 			enduseful = dp->i_offset;
    478 	}
    479 /* notfound: */
    480 	/*
    481 	 * If we started in the middle of the directory and failed
    482 	 * to find our target, we must check the beginning as well.
    483 	 */
    484 	if (numdirpasses == 2) {
    485 		numdirpasses--;
    486 		dp->i_offset = 0;
    487 		endsearch = dp->i_diroff;
    488 		goto searchloop;
    489 	}
    490 	if (bp != NULL)
    491 		brelse(bp);
    492 	/*
    493 	 * If creating, and at end of pathname and current
    494 	 * directory has not been removed, then can consider
    495 	 * allowing file to be created.
    496 	 */
    497 	if ((nameiop == CREATE || nameiop == RENAME) &&
    498 		(flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
    499 		/*
    500 		 * Creation of files on a read-only mounted file system
    501 		 * is pointless, so don't proceed any further.
    502 		 */
    503 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
    504 					return (EROFS);
    505 		/*
    506 		 * Access for write is interpreted as allowing
    507 		 * creation of files in the directory.
    508 		 */
    509 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
    510 			return (error);
    511 		/*
    512 		 * Return an indication of where the new directory
    513 		 * entry should be put.  If we didn't find a slot,
    514 		 * then set dp->i_count to 0 indicating
    515 		 * that the new slot belongs at the end of the
    516 		 * directory. If we found a slot, then the new entry
    517 		 * can be put in the range from dp->i_offset to
    518 		 * dp->i_offset + dp->i_count.
    519 		 */
    520 		if (slotstatus == NONE) {
    521 			dp->i_offset = roundup(dp->i_e2fs_size, dirblksize);
    522 			dp->i_count = 0;
    523 			enduseful = dp->i_offset;
    524 		} else {
    525 			dp->i_offset = slotoffset;
    526 			dp->i_count = slotsize;
    527 			if (enduseful < slotoffset + slotsize)
    528 				enduseful = slotoffset + slotsize;
    529 		}
    530 		dp->i_endoff = roundup(enduseful, dirblksize);
    531 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    532 		/*
    533 		 * We return with the directory locked, so that
    534 		 * the parameters we set up above will still be
    535 		 * valid if we actually decide to do a direnter().
    536 		 * We return ni_vp == NULL to indicate that the entry
    537 		 * does not currently exist; we leave a pointer to
    538 		 * the (locked) directory inode in ndp->ni_dvp.
    539 		 * The pathname buffer is saved so that the name
    540 		 * can be obtained later.
    541 		 *
    542 		 * NB - if the directory is unlocked, then this
    543 		 * information cannot be used.
    544 		 */
    545 		cnp->cn_flags |= SAVENAME;
    546 		if (!lockparent)
    547 			VOP_UNLOCK(vdp);
    548 		return (EJUSTRETURN);
    549 	}
    550 	/*
    551 	 * Insert name into cache (as non-existent) if appropriate.
    552 	 */
    553 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    554 		cache_enter(vdp, *vpp, cnp);
    555 	return (ENOENT);
    556 
    557 found:
    558 	/*
    559 	 * Check that directory length properly reflects presence
    560 	 * of this entry.
    561 	 */
    562 	if (entryoffsetinblock + EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen))
    563 		> dp->i_e2fs_size) {
    564 		ufs_dirbad(dp, dp->i_offset, "i_size too small");
    565 		dp->i_e2fs_size = entryoffsetinblock +
    566 			EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
    567 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    568 	}
    569 
    570 	/*
    571 	 * Found component in pathname.
    572 	 * If the final component of path name, save information
    573 	 * in the cache as to where the entry was found.
    574 	 */
    575 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
    576 		dp->i_diroff = dp->i_offset &~ (dirblksize - 1);
    577 
    578 	/*
    579 	 * If deleting, and at end of pathname, return
    580 	 * parameters which can be used to remove file.
    581 	 * If the wantparent flag isn't set, we return only
    582 	 * the directory (in ndp->ni_dvp), otherwise we go
    583 	 * on and lock the inode, being careful with ".".
    584 	 */
    585 	if (nameiop == DELETE && (flags & ISLASTCN)) {
    586 		/*
    587 		 * Write access to directory required to delete files.
    588 		 */
    589 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
    590 			return (error);
    591 		/*
    592 		 * Return pointer to current entry in dp->i_offset,
    593 		 * and distance past previous entry (if there
    594 		 * is a previous entry in this block) in dp->i_count.
    595 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
    596 		 */
    597 		if ((dp->i_offset & (dirblksize - 1)) == 0)
    598 			dp->i_count = 0;
    599 		else
    600 			dp->i_count = dp->i_offset - prevoff;
    601 		if (dp->i_number == dp->i_ino) {
    602 			VREF(vdp);
    603 			*vpp = vdp;
    604 			return (0);
    605 		}
    606 		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
    607 			return (error);
    608 		/*
    609 		 * If directory is "sticky", then user must own
    610 		 * the directory, or the file in it, else she
    611 		 * may not delete it (unless she's root). This
    612 		 * implements append-only directories.
    613 		 */
    614 		if ((dp->i_e2fs_mode & ISVTX) &&
    615 			cred->cr_uid != 0 &&
    616 			cred->cr_uid != dp->i_e2fs_uid &&
    617 			VTOI(tdp)->i_e2fs_uid != cred->cr_uid) {
    618 			vput(tdp);
    619 			return (EPERM);
    620 		}
    621 		*vpp = tdp;
    622 		if (!lockparent)
    623 			VOP_UNLOCK(vdp);
    624 		return (0);
    625 	}
    626 
    627 	/*
    628 	 * If rewriting (RENAME), return the inode and the
    629 	 * information required to rewrite the present directory
    630 	 * Must get inode of directory entry to verify it's a
    631 	 * regular file, or empty directory.
    632 	 */
    633 	if (nameiop == RENAME && wantparent &&
    634 		(flags & ISLASTCN)) {
    635 		if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
    636 			return (error);
    637 		/*
    638 		 * Careful about locking second inode.
    639 		 * This can only occur if the target is ".".
    640 		 */
    641 		if (dp->i_number == dp->i_ino)
    642 			return (EISDIR);
    643 		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
    644 			return (error);
    645 		*vpp = tdp;
    646 		cnp->cn_flags |= SAVENAME;
    647 		if (!lockparent)
    648 			VOP_UNLOCK(vdp);
    649 		return (0);
    650 	}
    651 
    652 	/*
    653 	 * Step through the translation in the name.  We do not `vput' the
    654 	 * directory because we may need it again if a symbolic link
    655 	 * is relative to the current directory.  Instead we save it
    656 	 * unlocked as "pdp".  We must get the target inode before unlocking
    657 	 * the directory to insure that the inode will not be removed
    658 	 * before we get it.  We prevent deadlock by always fetching
    659 	 * inodes from the root, moving down the directory tree. Thus
    660 	 * when following backward pointers ".." we must unlock the
    661 	 * parent directory before getting the requested directory.
    662 	 * There is a potential race condition here if both the current
    663 	 * and parent directories are removed before the VFS_VGET for the
    664 	 * inode associated with ".." returns.  We hope that this occurs
    665 	 * infrequently since we cannot avoid this race condition without
    666 	 * implementing a sophisticated deadlock detection algorithm.
    667 	 * Note also that this simple deadlock detection scheme will not
    668 	 * work if the file system has any hard links other than ".."
    669 	 * that point backwards in the directory structure.
    670 	 */
    671 	pdp = vdp;
    672 	if (flags & ISDOTDOT) {
    673 		VOP_UNLOCK(pdp);	/* race to get the inode */
    674 		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) {
    675 			VOP_LOCK(pdp);
    676 			return (error);
    677 		}
    678 		if (lockparent && (flags & ISLASTCN) &&
    679 			(error = VOP_LOCK(pdp)) != 0) {
    680 			vput(tdp);
    681 			return (error);
    682 		}
    683 		*vpp = tdp;
    684 	} else if (dp->i_number == dp->i_ino) {
    685 		VREF(vdp);	/* we want ourself, ie "." */
    686 		*vpp = vdp;
    687 	} else {
    688 		if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
    689 			return (error);
    690 		if (!lockparent || !(flags & ISLASTCN))
    691 			VOP_UNLOCK(pdp);
    692 		*vpp = tdp;
    693 	}
    694 
    695 	/*
    696 	 * Insert name into cache if appropriate.
    697 	 */
    698 	if (cnp->cn_flags & MAKEENTRY)
    699 		cache_enter(vdp, *vpp, cnp);
    700 	return (0);
    701 }
    702 
    703 /*
    704  * Do consistency checking on a directory entry:
    705  *	record length must be multiple of 4
    706  *	entry must fit in rest of its dirblksize block
    707  *	record must be large enough to contain entry
    708  *	name is not longer than MAXNAMLEN
    709  *	name must be as long as advertised, and null terminated
    710  */
    711 /*
    712  *	changed so that it confirms to ext2fs_check_dir_entry
    713  */
    714 static int
    715 ext2fs_dirbadentry(dp, de, entryoffsetinblock)
    716 	struct vnode *dp;
    717 	register struct ext2fs_direct *de;
    718 	int entryoffsetinblock;
    719 {
    720 	int	dirblksize = VTOI(dp)->i_e2fs->e2fs_bsize;
    721 
    722 		char * error_msg = NULL;
    723 		int reclen = fs2h16(de->e2d_reclen);
    724 		int namlen = fs2h16(de->e2d_namlen);
    725 
    726 		if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
    727 				error_msg = "rec_len is smaller than minimal";
    728 		else if (reclen % 4 != 0)
    729 				error_msg = "rec_len % 4 != 0";
    730 		else if (reclen < EXT2FS_DIRSIZ(namlen))
    731 				error_msg = "reclen is too small for name_len";
    732 		else if (entryoffsetinblock + reclen > dirblksize)
    733 				error_msg = "directory entry across blocks";
    734 		else if (fs2h32(de->e2d_ino) > VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
    735 				error_msg = "inode out of bounds";
    736 
    737 		if (error_msg != NULL) {
    738 			printf( "bad directory entry: %s\n"
    739 						"offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
    740 						error_msg, entryoffsetinblock,
    741 			(unsigned long) fs2h32(de->e2d_ino), reclen, namlen);
    742 			panic("ext2fs_dirbadentry");
    743 		}
    744 		return error_msg == NULL ? 0 : 1;
    745 }
    746 
    747 /*
    748  * Write a directory entry after a call to namei, using the parameters
    749  * that it left in nameidata.  The argument ip is the inode which the new
    750  * directory entry will refer to.  Dvp is a pointer to the directory to
    751  * be written, which was left locked by namei. Remaining parameters
    752  * (dp->i_offset, dp->i_count) indicate how the space for the new
    753  * entry is to be obtained.
    754  */
    755 int
    756 ext2fs_direnter(ip, dvp, cnp)
    757 	struct inode *ip;
    758 	struct vnode *dvp;
    759 	register struct componentname *cnp;
    760 {
    761 	register struct ext2fs_direct *ep, *nep;
    762 	register struct inode *dp;
    763 	struct buf *bp;
    764 	struct ext2fs_direct newdir;
    765 	struct iovec aiov;
    766 	struct uio auio;
    767 	u_int dsize;
    768 	int error, loc, newentrysize, spacefree;
    769 	char *dirbuf;
    770 	int	 dirblksize = ip->i_e2fs->e2fs_bsize;
    771 
    772 
    773 #ifdef DIAGNOSTIC
    774 	if ((cnp->cn_flags & SAVENAME) == 0)
    775 		panic("direnter: missing name");
    776 #endif
    777 	dp = VTOI(dvp);
    778 	newdir.e2d_ino = h2fs32(ip->i_number);
    779 	newdir.e2d_namlen = h2fs16(cnp->cn_namelen);
    780 	bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
    781 	newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
    782 	if (dp->i_count == 0) {
    783 		/*
    784 		 * If dp->i_count is 0, then namei could find no
    785 		 * space in the directory. Here, dp->i_offset will
    786 		 * be on a directory block boundary and we will write the
    787 		 * new entry into a fresh block.
    788 		 */
    789 		if (dp->i_offset & (dirblksize - 1))
    790 			panic("ext2fs_direnter: newblk");
    791 		auio.uio_offset = dp->i_offset;
    792 		newdir.e2d_reclen = h2fs16(dirblksize);
    793 		auio.uio_resid = newentrysize;
    794 		aiov.iov_len = newentrysize;
    795 		aiov.iov_base = (caddr_t)&newdir;
    796 		auio.uio_iov = &aiov;
    797 		auio.uio_iovcnt = 1;
    798 		auio.uio_rw = UIO_WRITE;
    799 		auio.uio_segflg = UIO_SYSSPACE;
    800 		auio.uio_procp = (struct proc *)0;
    801 		error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
    802 		if (dirblksize >
    803 			VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
    804 			/* XXX should grow with balloc() */
    805 			panic("ext2fs_direnter: frag size");
    806 		else if (!error) {
    807 			dp->i_e2fs_size = roundup(dp->i_e2fs_size, dirblksize);
    808 			dp->i_flag |= IN_CHANGE;
    809 		}
    810 		return (error);
    811 	}
    812 
    813 	/*
    814 	 * If dp->i_count is non-zero, then namei found space
    815 	 * for the new entry in the range dp->i_offset to
    816 	 * dp->i_offset + dp->i_count in the directory.
    817 	 * To use this space, we may have to compact the entries located
    818 	 * there, by copying them together towards the beginning of the
    819 	 * block, leaving the free space in one usable chunk at the end.
    820 	 */
    821 
    822 	/*
    823 	 * Get the block containing the space for the new directory entry.
    824 	 */
    825 	if ((error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
    826 		return (error);
    827 	/*
    828 	 * Find space for the new entry. In the simple case, the entry at
    829 	 * offset base will have the space. If it does not, then namei
    830 	 * arranged that compacting the region dp->i_offset to
    831 	 * dp->i_offset + dp->i_count would yield the
    832 	 * space.
    833 	 */
    834 	ep = (struct ext2fs_direct *)dirbuf;
    835 	dsize = EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
    836 	spacefree = fs2h16(ep->e2d_reclen) - dsize;
    837 	for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) {
    838 		nep = (struct ext2fs_direct *)(dirbuf + loc);
    839 		if (ep->e2d_ino) {
    840 			/* trim the existing slot */
    841 			ep->e2d_reclen = h2fs16(dsize);
    842 			ep = (struct ext2fs_direct *)((char *)ep + dsize);
    843 		} else {
    844 			/* overwrite; nothing there; header is ours */
    845 			spacefree += dsize;
    846 		}
    847 		dsize = EXT2FS_DIRSIZ(fs2h16(nep->e2d_namlen));
    848 		spacefree += fs2h16(nep->e2d_reclen) - dsize;
    849 		loc += fs2h16(nep->e2d_reclen);
    850 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
    851 	}
    852 	/*
    853 	 * Update the pointer fields in the previous entry (if any),
    854 	 * copy in the new entry, and write out the block.
    855 	 */
    856 	if (ep->e2d_ino == 0) {
    857 #ifdef DIAGNOSTIC
    858 		if (spacefree + dsize < newentrysize)
    859 			panic("ext2fs_direnter: compact1");
    860 #endif
    861 		newdir.e2d_reclen = h2fs16(spacefree + dsize);
    862 	} else {
    863 #ifdef DIAGNOSTIC
    864 		if (spacefree < newentrysize) {
    865 			printf("ext2fs_direnter: compact2 %u %u",
    866 						(u_int)spacefree, (u_int)newentrysize);
    867 			panic("ext2fs_direnter: compact2");
    868 		}
    869 #endif
    870 		newdir.e2d_reclen = h2fs16(spacefree);
    871 		ep->e2d_reclen = h2fs16(dsize);
    872 		ep = (struct ext2fs_direct *)((char *)ep + dsize);
    873 	}
    874 	bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
    875 	error = VOP_BWRITE(bp);
    876 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
    877 	if (!error && dp->i_endoff && dp->i_endoff < dp->i_e2fs_size)
    878 		error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
    879 			cnp->cn_cred, cnp->cn_proc);
    880 	return (error);
    881 }
    882 
    883 /*
    884  * Remove a directory entry after a call to namei, using
    885  * the parameters which it left in nameidata. The entry
    886  * dp->i_offset contains the offset into the directory of the
    887  * entry to be eliminated.  The dp->i_count field contains the
    888  * size of the previous record in the directory.  If this
    889  * is 0, the first entry is being deleted, so we need only
    890  * zero the inode number to mark the entry as free.  If the
    891  * entry is not the first in the directory, we must reclaim
    892  * the space of the now empty record by adding the record size
    893  * to the size of the previous entry.
    894  */
    895 int
    896 ext2fs_dirremove(dvp, cnp)
    897 	struct vnode *dvp;
    898 	struct componentname *cnp;
    899 {
    900 	register struct inode *dp;
    901 	struct ext2fs_direct *ep;
    902 	struct buf *bp;
    903 	int error;
    904 
    905 	dp = VTOI(dvp);
    906 	if (dp->i_count == 0) {
    907 		/*
    908 		 * First entry in block: set d_ino to zero.
    909 		 */
    910 		error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp);
    911 		if (error != 0)
    912 			return (error);
    913 		ep->e2d_ino = 0;
    914 		error = VOP_BWRITE(bp);
    915 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    916 		return (error);
    917 	}
    918 	/*
    919 	 * Collapse new free space into previous entry.
    920 	 */
    921 	error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
    922 			(char **)&ep, &bp);
    923 	if (error != 0)
    924 		return (error);
    925 	ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen);
    926 	error = VOP_BWRITE(bp);
    927 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
    928 	return (error);
    929 }
    930 
    931 /*
    932  * Rewrite an existing directory entry to point at the inode
    933  * supplied.  The parameters describing the directory entry are
    934  * set up by a call to namei.
    935  */
    936 int
    937 ext2fs_dirrewrite(dp, ip, cnp)
    938 	struct inode *dp, *ip;
    939 	struct componentname *cnp;
    940 {
    941 	struct buf *bp;
    942 	struct ext2fs_direct *ep;
    943 	struct vnode *vdp = ITOV(dp);
    944 	int error;
    945 
    946 	error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
    947 	if (error != 0)
    948 		return (error);
    949 	ep->e2d_ino = h2fs32(ip->i_number);
    950 	error = VOP_BWRITE(bp);
    951 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
    952 	return (error);
    953 }
    954 
    955 /*
    956  * Check if a directory is empty or not.
    957  * Inode supplied must be locked.
    958  *
    959  * Using a struct dirtemplate here is not precisely
    960  * what we want, but better than using a struct ext2fs_direct.
    961  *
    962  * NB: does not handle corrupted directories.
    963  */
    964 int
    965 ext2fs_dirempty(ip, parentino, cred)
    966 	register struct inode *ip;
    967 	ino_t parentino;
    968 	struct ucred *cred;
    969 {
    970 	register off_t off;
    971 	struct ext2fs_dirtemplate dbuf;
    972 	register struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
    973 	int error, count, namlen;
    974 
    975 #define	MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
    976 
    977 	for (off = 0; off < ip->i_e2fs_size; off += fs2h16(dp->e2d_reclen)) {
    978 		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
    979 		   UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
    980 		/*
    981 		 * Since we read MINDIRSIZ, residual must
    982 		 * be 0 unless we're at end of file.
    983 		 */
    984 		if (error || count != 0)
    985 			return (0);
    986 		/* avoid infinite loops */
    987 		if (dp->e2d_reclen == 0)
    988 			return (0);
    989 		/* skip empty entries */
    990 		if (dp->e2d_ino == 0)
    991 			continue;
    992 		/* accept only "." and ".." */
    993 		namlen = fs2h16(dp->e2d_namlen);
    994 		if (namlen > 2)
    995 			return (0);
    996 		if (dp->e2d_name[0] != '.')
    997 			return (0);
    998 		/*
    999 		 * At this point namlen must be 1 or 2.
   1000 		 * 1 implies ".", 2 implies ".." if second
   1001 		 * char is also "."
   1002 		 */
   1003 		if (namlen == 1)
   1004 			continue;
   1005 		if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
   1006 			continue;
   1007 		return (0);
   1008 	}
   1009 	return (1);
   1010 }
   1011 
   1012 /*
   1013  * Check if source directory is in the path of the target directory.
   1014  * Target is supplied locked, source is unlocked.
   1015  * The target is always vput before returning.
   1016  */
   1017 int
   1018 ext2fs_checkpath(source, target, cred)
   1019 	struct inode *source, *target;
   1020 	struct ucred *cred;
   1021 {
   1022 	struct vnode *vp;
   1023 	int error, rootino, namlen;
   1024 	struct ext2fs_dirtemplate dirbuf;
   1025 	u_int32_t ino;
   1026 
   1027 	vp = ITOV(target);
   1028 	if (target->i_number == source->i_number) {
   1029 		error = EEXIST;
   1030 		goto out;
   1031 	}
   1032 	rootino = ROOTINO;
   1033 	error = 0;
   1034 	if (target->i_number == rootino)
   1035 		goto out;
   1036 
   1037 	for (;;) {
   1038 		if (vp->v_type != VDIR) {
   1039 			error = ENOTDIR;
   1040 			break;
   1041 		}
   1042 		error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
   1043 			sizeof (struct ext2fs_dirtemplate), (off_t)0, UIO_SYSSPACE,
   1044 			IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
   1045 		if (error != 0)
   1046 			break;
   1047 		namlen = fs2h16(dirbuf.dotdot_namlen);
   1048 		if (namlen != 2 ||
   1049 			dirbuf.dotdot_name[0] != '.' ||
   1050 			dirbuf.dotdot_name[1] != '.') {
   1051 			error = ENOTDIR;
   1052 			break;
   1053 		}
   1054 		ino = fs2h32(dirbuf.dotdot_ino);
   1055 		if (ino == source->i_number) {
   1056 			error = EINVAL;
   1057 			break;
   1058 		}
   1059 		if (ino == rootino)
   1060 			break;
   1061 		vput(vp);
   1062 		error = VFS_VGET(vp->v_mount, ino, &vp);
   1063 		if (error != 0) {
   1064 			vp = NULL;
   1065 			break;
   1066 		}
   1067 	}
   1068 
   1069 out:
   1070 	if (error == ENOTDIR) {
   1071 		printf("checkpath: .. not a directory\n");
   1072 		panic("checkpath");
   1073 	}
   1074 	if (vp != NULL)
   1075 		vput(vp);
   1076 	return (error);
   1077 }
   1078 
   1079