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