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