Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_lookup.c revision 1.80
      1  1.80  christos /*	$NetBSD: ext2fs_lookup.c,v 1.80 2016/06/24 17:21:30 christos 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.80  christos __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.80 2016/06/24 17:21:30 christos 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.67      para #include <sys/kmem.h>
     61   1.1    bouyer #include <sys/malloc.h>
     62   1.1    bouyer #include <sys/dirent.h>
     63  1.44      elad #include <sys/kauth.h>
     64  1.53        ad #include <sys/proc.h>
     65   1.1    bouyer 
     66   1.1    bouyer #include <ufs/ufs/inode.h>
     67   1.1    bouyer #include <ufs/ufs/ufsmount.h>
     68   1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     69   1.1    bouyer 
     70   1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     71   1.1    bouyer #include <ufs/ext2fs/ext2fs_dir.h>
     72   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     73  1.80  christos #include <ufs/ext2fs/ext2fs_htree.h>
     74   1.1    bouyer 
     75  1.68      elad #include <miscfs/genfs/genfs.h>
     76  1.68      elad 
     77   1.1    bouyer extern	int dirchk;
     78   1.1    bouyer 
     79  1.37   xtraeme static void	ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir,
     80  1.37   xtraeme 					  struct dirent *ffsdir);
     81  1.37   xtraeme static int	ext2fs_dirbadentry(struct vnode *dp,
     82   1.1    bouyer 					  struct ext2fs_direct *de,
     83  1.37   xtraeme 					  int entryoffsetinblock);
     84   1.1    bouyer 
     85   1.1    bouyer /*
     86   1.1    bouyer  * the problem that is tackled below is the fact that FFS
     87   1.1    bouyer  * includes the terminating zero on disk while EXT2FS doesn't
     88   1.1    bouyer  * this implies that we need to introduce some padding.
     89  1.32     perry  * For instance, a filename "sbin" has normally a reclen 12
     90  1.32     perry  * in EXT2, but 16 in FFS.
     91   1.1    bouyer  * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
     92   1.1    bouyer  * If it wasn't for that, the complete ufs code for directories would
     93   1.1    bouyer  * have worked w/o changes (except for the difference in DIRBLKSIZ)
     94   1.1    bouyer  */
     95   1.1    bouyer static void
     96  1.37   xtraeme ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir, struct dirent *ffsdir)
     97   1.1    bouyer {
     98   1.7     perry 	memset(ffsdir, 0, sizeof(struct dirent));
     99   1.3    bouyer 	ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
    100  1.13    bouyer 	ffsdir->d_namlen = e2dir->e2d_namlen;
    101   1.1    bouyer 
    102   1.1    bouyer 	ffsdir->d_type = DT_UNKNOWN;		/* don't know more here */
    103   1.1    bouyer #ifdef DIAGNOSTIC
    104  1.36  christos #if MAXNAMLEN < E2FS_MAXNAMLEN
    105   1.1    bouyer 	/*
    106  1.36  christos 	 * we should handle this more gracefully !
    107   1.1    bouyer 	 */
    108  1.13    bouyer 	if (e2dir->e2d_namlen > MAXNAMLEN)
    109  1.21    provos 		panic("ext2fs: e2dir->e2d_namlen");
    110  1.19   thorpej #endif
    111   1.1    bouyer #endif
    112   1.1    bouyer 	strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
    113   1.1    bouyer 
    114  1.32     perry 	/* Godmar thinks: since e2dir->e2d_reclen can be big and means
    115   1.1    bouyer 	   nothing anyway, we compute our own reclen according to what
    116   1.1    bouyer 	   we think is right
    117   1.1    bouyer 	 */
    118  1.35  christos 	ffsdir->d_reclen = _DIRENT_SIZE(ffsdir);
    119   1.1    bouyer }
    120   1.1    bouyer 
    121  1.80  christos static int
    122  1.80  christos ext2fs_is_dot_entry(struct componentname *cnp)
    123  1.80  christos {
    124  1.80  christos 	return cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
    125  1.80  christos 	    (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0');
    126  1.80  christos }
    127  1.80  christos 
    128   1.1    bouyer /*
    129   1.1    bouyer  * Vnode op for reading directories.
    130   1.1    bouyer  *
    131   1.1    bouyer  * Convert the on-disk entries to <sys/dirent.h> entries.
    132   1.1    bouyer  * the problem is that the conversion will blow up some entries by four bytes,
    133   1.1    bouyer  * so it can't be done in place. This is too bad. Right now the conversion is
    134  1.32     perry  * done entry by entry, the converted entry is sent via uiomove.
    135   1.1    bouyer  *
    136   1.1    bouyer  * XXX allocate a buffer, convert as many entries as possible, then send
    137   1.1    bouyer  * the whole buffer to uiomove
    138   1.1    bouyer  */
    139   1.1    bouyer int
    140  1.37   xtraeme ext2fs_readdir(void *v)
    141   1.1    bouyer {
    142   1.1    bouyer 	struct vop_readdir_args /* {
    143   1.1    bouyer 		struct vnode *a_vp;
    144   1.1    bouyer 		struct uio *a_uio;
    145  1.44      elad 		kauth_cred_t a_cred;
    146   1.5      fvdl 		int **a_eofflag;
    147   1.5      fvdl 		off_t **a_cookies;
    148   1.1    bouyer 		int ncookies;
    149   1.1    bouyer 	} */ *ap = v;
    150   1.5      fvdl 	struct uio *uio = ap->a_uio;
    151   1.1    bouyer 	int error;
    152   1.2    bouyer 	size_t e2fs_count, readcnt;
    153   1.5      fvdl 	struct vnode *vp = ap->a_vp;
    154   1.5      fvdl 	struct m_ext2fs *fs = VTOI(vp)->i_e2fs;
    155   1.1    bouyer 
    156   1.1    bouyer 	struct ext2fs_direct *dp;
    157  1.52    rumble 	struct dirent *dstd;
    158   1.1    bouyer 	struct uio auio;
    159   1.1    bouyer 	struct iovec aiov;
    160  1.50  christos 	void *dirbuf;
    161   1.1    bouyer 	off_t off = uio->uio_offset;
    162   1.5      fvdl 	off_t *cookies = NULL;
    163   1.5      fvdl 	int nc = 0, ncookies = 0;
    164   1.3    bouyer 	int e2d_reclen;
    165   1.1    bouyer 
    166   1.5      fvdl 	if (vp->v_type != VDIR)
    167   1.5      fvdl 		return (ENOTDIR);
    168   1.5      fvdl 
    169   1.2    bouyer 	e2fs_count = uio->uio_resid;
    170   1.1    bouyer 	/* Make sure we don't return partial entries. */
    171   1.2    bouyer 	e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
    172   1.2    bouyer 	if (e2fs_count <= 0)
    173   1.1    bouyer 		return (EINVAL);
    174   1.1    bouyer 
    175   1.1    bouyer 	auio = *uio;
    176   1.1    bouyer 	auio.uio_iov = &aiov;
    177   1.1    bouyer 	auio.uio_iovcnt = 1;
    178   1.2    bouyer 	aiov.iov_len = e2fs_count;
    179   1.2    bouyer 	auio.uio_resid = e2fs_count;
    180  1.40      yamt 	UIO_SETUP_SYSSPACE(&auio);
    181  1.67      para 	dirbuf = kmem_alloc(e2fs_count, KM_SLEEP);
    182  1.67      para 	dstd = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    183   1.5      fvdl 	if (ap->a_ncookies) {
    184  1.52    rumble 		nc = e2fs_count / _DIRENT_MINSIZE((struct dirent *)0);
    185  1.52    rumble 		ncookies = nc;
    186  1.16   thorpej 		cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
    187   1.5      fvdl 		*ap->a_cookies = cookies;
    188   1.5      fvdl 	}
    189   1.1    bouyer 	aiov.iov_base = dirbuf;
    190   1.1    bouyer 
    191  1.78  riastrad 	error = UFS_BUFRD(ap->a_vp, &auio, 0, ap->a_cred);
    192   1.1    bouyer 	if (error == 0) {
    193   1.2    bouyer 		readcnt = e2fs_count - auio.uio_resid;
    194  1.32     perry 		for (dp = (struct ext2fs_direct *)dirbuf;
    195   1.1    bouyer 			(char *)dp < (char *)dirbuf + readcnt; ) {
    196   1.3    bouyer 			e2d_reclen = fs2h16(dp->e2d_reclen);
    197   1.3    bouyer 			if (e2d_reclen == 0) {
    198   1.1    bouyer 				error = EIO;
    199   1.1    bouyer 				break;
    200   1.1    bouyer 			}
    201  1.52    rumble 			ext2fs_dirconv2ffs(dp, dstd);
    202  1.52    rumble 			if(dstd->d_reclen > uio->uio_resid) {
    203   1.1    bouyer 				break;
    204   1.1    bouyer 			}
    205  1.52    rumble 			error = uiomove(dstd, dstd->d_reclen, uio);
    206  1.52    rumble 			if (error != 0) {
    207   1.1    bouyer 				break;
    208   1.1    bouyer 			}
    209   1.3    bouyer 			off = off + e2d_reclen;
    210   1.1    bouyer 			if (cookies != NULL) {
    211   1.1    bouyer 				*cookies++ = off;
    212   1.1    bouyer 				if (--ncookies <= 0){
    213   1.1    bouyer 					break;  /* out of cookies */
    214   1.1    bouyer 				}
    215   1.1    bouyer 			}
    216   1.1    bouyer 			/* advance dp */
    217   1.3    bouyer 			dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
    218   1.1    bouyer 		}
    219   1.1    bouyer 		/* we need to correct uio_offset */
    220   1.1    bouyer 		uio->uio_offset = off;
    221   1.1    bouyer 	}
    222  1.67      para 	kmem_free(dirbuf, e2fs_count);
    223  1.67      para 	kmem_free(dstd, sizeof(*dstd));
    224  1.31        ws 	*ap->a_eofflag = ext2fs_size(VTOI(ap->a_vp)) <= uio->uio_offset;
    225   1.5      fvdl 	if (ap->a_ncookies) {
    226   1.5      fvdl 		if (error) {
    227  1.16   thorpej 			free(*ap->a_cookies, M_TEMP);
    228   1.5      fvdl 			*ap->a_ncookies = 0;
    229   1.5      fvdl 			*ap->a_cookies = NULL;
    230   1.5      fvdl 		} else
    231   1.5      fvdl 			*ap->a_ncookies = nc - ncookies;
    232   1.5      fvdl 	}
    233   1.1    bouyer 	return (error);
    234   1.1    bouyer }
    235   1.1    bouyer 
    236   1.1    bouyer /*
    237   1.1    bouyer  * Convert a component of a pathname into a pointer to a locked inode.
    238   1.1    bouyer  * This is a very central and rather complicated routine.
    239   1.1    bouyer  * If the file system is not maintained in a strict tree hierarchy,
    240   1.1    bouyer  * this can result in a deadlock situation (see comments in code below).
    241   1.1    bouyer  *
    242   1.1    bouyer  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
    243   1.1    bouyer  * on whether the name is to be looked up, created, renamed, or deleted.
    244   1.1    bouyer  * When CREATE, RENAME, or DELETE is specified, information usable in
    245   1.1    bouyer  * creating, renaming, or deleting a directory entry may be calculated.
    246   1.1    bouyer  * If flag has LOCKPARENT or'ed into it and the target of the pathname
    247   1.1    bouyer  * exists, lookup returns both the target and its parent directory locked.
    248   1.1    bouyer  * When creating or renaming and LOCKPARENT is specified, the target may
    249   1.1    bouyer  * not be ".".  When deleting and LOCKPARENT is specified, the target may
    250   1.1    bouyer  * be "."., but the caller must check to ensure it does an vrele and vput
    251   1.1    bouyer  * instead of two vputs.
    252   1.1    bouyer  *
    253   1.1    bouyer  * Overall outline of ext2fs_lookup:
    254   1.1    bouyer  *
    255   1.1    bouyer  *	check accessibility of directory
    256   1.1    bouyer  *	look for name in cache, if found, then if at end of path
    257   1.1    bouyer  *	  and deleting or creating, drop it, else return name
    258   1.1    bouyer  *	search for name in directory, to found or notfound
    259   1.1    bouyer  * notfound:
    260   1.1    bouyer  *	if creating, return locked directory, leaving info on available slots
    261   1.1    bouyer  *	else return error
    262   1.1    bouyer  * found:
    263   1.1    bouyer  *	if at end of path and deleting, return information to allow delete
    264   1.1    bouyer  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
    265   1.1    bouyer  *	  inode and return info to allow rewrite
    266   1.1    bouyer  *	if not at end, add name to cache; if at end and neither creating
    267   1.1    bouyer  *	  nor deleting, add name to cache
    268   1.1    bouyer  */
    269   1.1    bouyer int
    270  1.37   xtraeme ext2fs_lookup(void *v)
    271   1.1    bouyer {
    272  1.74   hannken 	struct vop_lookup_v2_args /* {
    273   1.1    bouyer 		struct vnode *a_dvp;
    274   1.1    bouyer 		struct vnode **a_vpp;
    275   1.1    bouyer 		struct componentname *a_cnp;
    276   1.1    bouyer 	} */ *ap = v;
    277  1.29   mycroft 	struct vnode *vdp = ap->a_dvp;	/* vnode for directory being searched */
    278  1.29   mycroft 	struct inode *dp = VTOI(vdp);	/* inode for directory being searched */
    279   1.1    bouyer 	struct buf *bp;			/* a buffer of directory entries */
    280  1.60   tsutsui 	struct ext2fs_direct *ep;	/* the current directory entry */
    281   1.1    bouyer 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
    282  1.80  christos 	enum ext2fs_slotstatus slotstatus;
    283   1.1    bouyer 	doff_t slotoffset;		/* offset of area with free space */
    284   1.1    bouyer 	int slotsize;			/* size of area at slotoffset */
    285   1.1    bouyer 	int slotfreespace;		/* amount of space free in slot */
    286   1.1    bouyer 	int slotneeded;			/* size of the entry we're seeking */
    287   1.1    bouyer 	int numdirpasses;		/* strategy for directory search */
    288   1.1    bouyer 	doff_t endsearch;		/* offset to end directory search */
    289   1.1    bouyer 	doff_t prevoff;			/* prev entry dp->i_offset */
    290  1.75   hannken 	struct vnode *tdp;		/* returned by vcache_get */
    291   1.1    bouyer 	doff_t enduseful;		/* pointer past last used dir slot */
    292   1.1    bouyer 	u_long bmask;			/* block offset mask */
    293   1.1    bouyer 	int namlen, error;
    294   1.1    bouyer 	struct vnode **vpp = ap->a_vpp;
    295   1.1    bouyer 	struct componentname *cnp = ap->a_cnp;
    296  1.44      elad 	kauth_cred_t cred = cnp->cn_cred;
    297  1.29   mycroft 	int flags;
    298   1.1    bouyer 	int nameiop = cnp->cn_nameiop;
    299  1.29   mycroft 	struct ufsmount *ump = dp->i_ump;
    300  1.29   mycroft 	int dirblksiz = ump->um_dirblksiz;
    301  1.23      yamt 	ino_t foundino;
    302  1.65  dholland 	struct ufs_lookup_results *results;
    303  1.80  christos 	doff_t i_offset;		/* cached i_offset value */
    304  1.80  christos 	struct ext2fs_searchslot ss;
    305   1.1    bouyer 
    306  1.29   mycroft 	flags = cnp->cn_flags;
    307   1.1    bouyer 
    308   1.1    bouyer 	bp = NULL;
    309   1.1    bouyer 	slotoffset = -1;
    310   1.1    bouyer 	*vpp = NULL;
    311  1.47       chs 
    312   1.1    bouyer 	/*
    313  1.65  dholland 	 * Produce the auxiliary lookup results into i_crap. Increment
    314  1.65  dholland 	 * its serial number so elsewhere we can tell if we're using
    315  1.65  dholland 	 * stale results. This should not be done this way. XXX.
    316  1.65  dholland 	 */
    317  1.65  dholland 	results = &dp->i_crap;
    318  1.65  dholland 	dp->i_crapcounter++;
    319  1.65  dholland 
    320  1.65  dholland 	/*
    321   1.1    bouyer 	 * Check accessiblity of directory.
    322   1.1    bouyer 	 */
    323  1.54     pooka 	if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
    324   1.9    bouyer 		return (error);
    325   1.9    bouyer 
    326   1.5      fvdl 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    327   1.5      fvdl 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    328   1.5      fvdl 		return (EROFS);
    329   1.1    bouyer 
    330   1.1    bouyer 	/*
    331   1.1    bouyer 	 * We now have a segment name to search for, and a directory to search.
    332   1.1    bouyer 	 *
    333   1.1    bouyer 	 * Before tediously performing a linear scan of the directory,
    334   1.1    bouyer 	 * check the name cache to see if the directory/name pair
    335   1.1    bouyer 	 * we are looking for is known already.
    336   1.1    bouyer 	 */
    337  1.72  dholland 	if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
    338  1.72  dholland 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
    339  1.71  dholland 		return *vpp == NULLVP ? ENOENT : 0;
    340  1.71  dholland 	}
    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.29   mycroft 	    (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.29   mycroft 	bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
    368  1.65  dholland 	if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
    369  1.65  dholland 	    results->ulr_diroff >= ext2fs_size(dp)) {
    370   1.1    bouyer 		entryoffsetinblock = 0;
    371  1.65  dholland 		results->ulr_offset = 0;
    372   1.1    bouyer 		numdirpasses = 1;
    373   1.1    bouyer 	} else {
    374  1.65  dholland 		results->ulr_offset = results->ulr_diroff;
    375  1.65  dholland 		if ((entryoffsetinblock = results->ulr_offset & bmask) &&
    376  1.65  dholland 		    (error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL, &bp)))
    377   1.1    bouyer 			return (error);
    378   1.1    bouyer 		numdirpasses = 2;
    379  1.77     joerg 		namecache_count_2passes();
    380   1.1    bouyer 	}
    381  1.65  dholland 	prevoff = results->ulr_offset;
    382  1.31        ws 	endsearch = roundup(ext2fs_size(dp), dirblksiz);
    383   1.1    bouyer 	enduseful = 0;
    384   1.1    bouyer 
    385  1.80  christos 	/*
    386  1.80  christos 	 * Try to lookup dir entry using htree directory index.
    387  1.80  christos 	 *
    388  1.80  christos 	 * If we got an error or we want to find '.' or '..' entry,
    389  1.80  christos 	 * we will fall back to linear search.
    390  1.80  christos 	 */
    391  1.80  christos 	if (ext2fs_htree_has_idx(dp) && ext2fs_is_dot_entry(cnp)) {
    392  1.80  christos 		numdirpasses = 1;
    393  1.80  christos 		entryoffsetinblock = 0;
    394  1.80  christos 
    395  1.80  christos 		int htree_lookup_ret = ext2fs_htree_lookup(dp, cnp->cn_nameptr,
    396  1.80  christos 		    cnp->cn_namelen, &bp, &entryoffsetinblock, &i_offset,
    397  1.80  christos 		    &prevoff, &enduseful, &ss);
    398  1.80  christos 		switch (htree_lookup_ret) {
    399  1.80  christos 		case 0:
    400  1.80  christos 			ep = (struct ext2fs_direct*)((char *)bp->b_data +
    401  1.80  christos 			    (i_offset & bmask));
    402  1.80  christos 			foundino = ep->e2d_ino;
    403  1.80  christos 			goto found;
    404  1.80  christos 		case ENOENT:
    405  1.80  christos 			i_offset = roundup2(dp->i_size, dp->i_e2fs->e2fs_bsize);
    406  1.80  christos 			goto notfound;
    407  1.80  christos 		default:
    408  1.80  christos 			/*
    409  1.80  christos 			 * Something failed; just fallback to do a linear
    410  1.80  christos 			 * search.
    411  1.80  christos 			 */
    412  1.80  christos 			break;
    413  1.80  christos 		}
    414  1.80  christos 	}
    415  1.80  christos 
    416   1.1    bouyer searchloop:
    417  1.65  dholland 	while (results->ulr_offset < endsearch) {
    418  1.29   mycroft 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    419  1.49        ad 			preempt();
    420   1.1    bouyer 		/*
    421   1.1    bouyer 		 * If necessary, get the next directory block.
    422   1.1    bouyer 		 */
    423  1.65  dholland 		if ((results->ulr_offset & bmask) == 0) {
    424   1.1    bouyer 			if (bp != NULL)
    425  1.53        ad 				brelse(bp, 0);
    426  1.65  dholland 			error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL,
    427  1.29   mycroft 			    &bp);
    428   1.1    bouyer 			if (error != 0)
    429   1.1    bouyer 				return (error);
    430   1.1    bouyer 			entryoffsetinblock = 0;
    431   1.1    bouyer 		}
    432   1.1    bouyer 		/*
    433   1.1    bouyer 		 * If still looking for a slot, and at a dirblksize
    434   1.1    bouyer 		 * boundary, have to start looking for free space again.
    435   1.1    bouyer 		 */
    436   1.1    bouyer 		if (slotstatus == NONE &&
    437  1.29   mycroft 		    (entryoffsetinblock & (dirblksiz - 1)) == 0) {
    438   1.1    bouyer 			slotoffset = -1;
    439   1.1    bouyer 			slotfreespace = 0;
    440   1.1    bouyer 		}
    441   1.1    bouyer 		/*
    442   1.1    bouyer 		 * Get pointer to next entry.
    443   1.1    bouyer 		 * Full validation checks are slow, so we only check
    444   1.1    bouyer 		 * enough to insure forward progress through the
    445   1.1    bouyer 		 * directory. Complete checks can be run by patching
    446   1.1    bouyer 		 * "dirchk" to be true.
    447   1.1    bouyer 		 */
    448  1.43  christos 		KASSERT(bp != NULL);
    449   1.1    bouyer 		ep = (struct ext2fs_direct *)
    450   1.1    bouyer 			((char *)bp->b_data + entryoffsetinblock);
    451   1.1    bouyer 		if (ep->e2d_reclen == 0 ||
    452   1.9    bouyer 		    (dirchk &&
    453  1.29   mycroft 		     ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
    454   1.1    bouyer 			int i;
    455  1.29   mycroft 
    456  1.65  dholland 			ufs_dirbad(dp, results->ulr_offset, "mangled entry");
    457  1.29   mycroft 			i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
    458  1.65  dholland 			results->ulr_offset += i;
    459   1.1    bouyer 			entryoffsetinblock += i;
    460   1.1    bouyer 			continue;
    461   1.1    bouyer 		}
    462   1.1    bouyer 
    463   1.1    bouyer 		/*
    464   1.1    bouyer 		 * If an appropriate sized slot has not yet been found,
    465   1.1    bouyer 		 * check to see if one is available. Also accumulate space
    466   1.1    bouyer 		 * in the current block so that we can determine if
    467   1.1    bouyer 		 * compaction is viable.
    468   1.1    bouyer 		 */
    469   1.1    bouyer 		if (slotstatus != FOUND) {
    470   1.3    bouyer 			int size = fs2h16(ep->e2d_reclen);
    471   1.1    bouyer 
    472   1.1    bouyer 			if (ep->e2d_ino != 0)
    473  1.13    bouyer 				size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
    474   1.1    bouyer 			if (size > 0) {
    475   1.1    bouyer 				if (size >= slotneeded) {
    476   1.1    bouyer 					slotstatus = FOUND;
    477  1.65  dholland 					slotoffset = results->ulr_offset;
    478   1.3    bouyer 					slotsize = fs2h16(ep->e2d_reclen);
    479   1.1    bouyer 				} else if (slotstatus == NONE) {
    480   1.1    bouyer 					slotfreespace += size;
    481   1.1    bouyer 					if (slotoffset == -1)
    482  1.65  dholland 						slotoffset = results->ulr_offset;
    483   1.1    bouyer 					if (slotfreespace >= slotneeded) {
    484   1.1    bouyer 						slotstatus = COMPACT;
    485  1.65  dholland 						slotsize = results->ulr_offset +
    486  1.29   mycroft 						    fs2h16(ep->e2d_reclen) -
    487  1.29   mycroft 						    slotoffset;
    488   1.1    bouyer 					}
    489   1.1    bouyer 				}
    490   1.1    bouyer 			}
    491   1.1    bouyer 		}
    492   1.1    bouyer 
    493   1.1    bouyer 		/*
    494   1.1    bouyer 		 * Check for a name match.
    495   1.1    bouyer 		 */
    496   1.1    bouyer 		if (ep->e2d_ino) {
    497  1.13    bouyer 			namlen = ep->e2d_namlen;
    498   1.1    bouyer 			if (namlen == cnp->cn_namelen &&
    499  1.29   mycroft 			    !memcmp(cnp->cn_nameptr, ep->e2d_name,
    500  1.29   mycroft 			    (unsigned)namlen)) {
    501   1.1    bouyer 				/*
    502   1.1    bouyer 				 * Save directory entry's inode number and
    503   1.1    bouyer 				 * reclen in ndp->ni_ufs area, and release
    504   1.1    bouyer 				 * directory buffer.
    505   1.1    bouyer 				 */
    506  1.23      yamt 				foundino = fs2h32(ep->e2d_ino);
    507  1.65  dholland 				results->ulr_reclen = fs2h16(ep->e2d_reclen);
    508   1.1    bouyer 				goto found;
    509   1.1    bouyer 			}
    510   1.1    bouyer 		}
    511  1.65  dholland 		prevoff = results->ulr_offset;
    512  1.65  dholland 		results->ulr_offset += fs2h16(ep->e2d_reclen);
    513   1.3    bouyer 		entryoffsetinblock += fs2h16(ep->e2d_reclen);
    514   1.1    bouyer 		if (ep->e2d_ino)
    515  1.65  dholland 			enduseful = results->ulr_offset;
    516   1.1    bouyer 	}
    517  1.80  christos notfound:
    518   1.1    bouyer 	/*
    519   1.1    bouyer 	 * If we started in the middle of the directory and failed
    520   1.1    bouyer 	 * to find our target, we must check the beginning as well.
    521   1.1    bouyer 	 */
    522   1.1    bouyer 	if (numdirpasses == 2) {
    523   1.1    bouyer 		numdirpasses--;
    524  1.65  dholland 		results->ulr_offset = 0;
    525  1.65  dholland 		endsearch = results->ulr_diroff;
    526   1.1    bouyer 		goto searchloop;
    527   1.1    bouyer 	}
    528   1.1    bouyer 	if (bp != NULL)
    529  1.53        ad 		brelse(bp, 0);
    530   1.1    bouyer 	/*
    531   1.1    bouyer 	 * If creating, and at end of pathname and current
    532   1.1    bouyer 	 * directory has not been removed, then can consider
    533   1.1    bouyer 	 * allowing file to be created.
    534   1.1    bouyer 	 */
    535   1.1    bouyer 	if ((nameiop == CREATE || nameiop == RENAME) &&
    536  1.29   mycroft 	    (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
    537   1.1    bouyer 		/*
    538   1.1    bouyer 		 * Access for write is interpreted as allowing
    539   1.1    bouyer 		 * creation of files in the directory.
    540   1.1    bouyer 		 */
    541  1.54     pooka 		error = VOP_ACCESS(vdp, VWRITE, cred);
    542  1.29   mycroft 		if (error)
    543   1.1    bouyer 			return (error);
    544   1.1    bouyer 		/*
    545   1.1    bouyer 		 * Return an indication of where the new directory
    546   1.1    bouyer 		 * entry should be put.  If we didn't find a slot,
    547  1.65  dholland 		 * then set results->ulr_count to 0 indicating
    548   1.1    bouyer 		 * that the new slot belongs at the end of the
    549   1.1    bouyer 		 * directory. If we found a slot, then the new entry
    550  1.65  dholland 		 * can be put in the range from results->ulr_offset to
    551  1.65  dholland 		 * results->ulr_offset + results->ulr_count.
    552   1.1    bouyer 		 */
    553   1.1    bouyer 		if (slotstatus == NONE) {
    554  1.65  dholland 			results->ulr_offset = roundup(ext2fs_size(dp), dirblksiz);
    555  1.65  dholland 			results->ulr_count = 0;
    556  1.65  dholland 			enduseful = results->ulr_offset;
    557   1.1    bouyer 		} else {
    558  1.65  dholland 			results->ulr_offset = slotoffset;
    559  1.65  dholland 			results->ulr_count = slotsize;
    560   1.1    bouyer 			if (enduseful < slotoffset + slotsize)
    561   1.1    bouyer 				enduseful = slotoffset + slotsize;
    562   1.1    bouyer 		}
    563  1.65  dholland 		results->ulr_endoff = roundup(enduseful, dirblksiz);
    564  1.29   mycroft #if 0
    565   1.1    bouyer 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    566  1.29   mycroft #endif
    567   1.1    bouyer 		/*
    568   1.1    bouyer 		 * We return with the directory locked, so that
    569   1.1    bouyer 		 * the parameters we set up above will still be
    570   1.1    bouyer 		 * valid if we actually decide to do a direnter().
    571   1.1    bouyer 		 * We return ni_vp == NULL to indicate that the entry
    572   1.1    bouyer 		 * does not currently exist; we leave a pointer to
    573   1.1    bouyer 		 * the (locked) directory inode in ndp->ni_dvp.
    574   1.1    bouyer 		 *
    575   1.1    bouyer 		 * NB - if the directory is unlocked, then this
    576   1.1    bouyer 		 * information cannot be used.
    577   1.1    bouyer 		 */
    578   1.1    bouyer 		return (EJUSTRETURN);
    579   1.1    bouyer 	}
    580   1.1    bouyer 	/*
    581   1.1    bouyer 	 * Insert name into cache (as non-existent) if appropriate.
    582   1.1    bouyer 	 */
    583  1.70     rmind 	if (nameiop != CREATE) {
    584  1.72  dholland 		cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    585  1.72  dholland 			    cnp->cn_flags);
    586  1.70     rmind 	}
    587  1.70     rmind 	return ENOENT;
    588   1.1    bouyer 
    589   1.1    bouyer found:
    590  1.29   mycroft 	if (numdirpasses == 2)
    591  1.77     joerg 		namecache_count_pass2();
    592   1.1    bouyer 	/*
    593   1.1    bouyer 	 * Check that directory length properly reflects presence
    594   1.1    bouyer 	 * of this entry.
    595   1.1    bouyer 	 */
    596  1.65  dholland 	if (results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
    597  1.65  dholland 		ufs_dirbad(dp, results->ulr_offset, "i_size too small");
    598  1.31        ws 		error = ext2fs_setsize(dp,
    599  1.65  dholland 				results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
    600  1.31        ws 		if (error) {
    601  1.53        ad 			brelse(bp, 0);
    602  1.31        ws 			return (error);
    603  1.31        ws 		}
    604   1.1    bouyer 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
    605  1.34       kml 		uvm_vnp_setsize(vdp, ext2fs_size(dp));
    606   1.1    bouyer 	}
    607  1.53        ad 	brelse(bp, 0);
    608   1.1    bouyer 
    609   1.1    bouyer 	/*
    610   1.1    bouyer 	 * Found component in pathname.
    611   1.1    bouyer 	 * If the final component of path name, save information
    612   1.1    bouyer 	 * in the cache as to where the entry was found.
    613   1.1    bouyer 	 */
    614   1.1    bouyer 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
    615  1.65  dholland 		results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
    616   1.1    bouyer 
    617   1.1    bouyer 	/*
    618   1.1    bouyer 	 * If deleting, and at end of pathname, return
    619   1.1    bouyer 	 * parameters which can be used to remove file.
    620  1.47       chs 	 * Lock the inode, being careful with ".".
    621   1.1    bouyer 	 */
    622   1.1    bouyer 	if (nameiop == DELETE && (flags & ISLASTCN)) {
    623   1.1    bouyer 		/*
    624  1.65  dholland 		 * Return pointer to current entry in results->ulr_offset,
    625   1.1    bouyer 		 * and distance past previous entry (if there
    626  1.65  dholland 		 * is a previous entry in this block) in results->ulr_count.
    627   1.1    bouyer 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
    628   1.1    bouyer 		 */
    629  1.65  dholland 		if ((results->ulr_offset & (dirblksiz - 1)) == 0)
    630  1.65  dholland 			results->ulr_count = 0;
    631   1.1    bouyer 		else
    632  1.65  dholland 			results->ulr_count = results->ulr_offset - prevoff;
    633  1.69   hannken 		if (dp->i_number == foundino) {
    634  1.69   hannken 			vref(vdp);
    635  1.69   hannken 			tdp = vdp;
    636  1.69   hannken 		} else {
    637  1.75   hannken 			error = vcache_get(vdp->v_mount,
    638  1.75   hannken 			    &foundino, sizeof(foundino), &tdp);
    639  1.69   hannken 			if (error)
    640  1.69   hannken 				return (error);
    641  1.69   hannken 		}
    642   1.1    bouyer 		/*
    643  1.68      elad 		 * Write access to directory required to delete files.
    644  1.68      elad 		 */
    645  1.68      elad 		if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0) {
    646  1.75   hannken 			vrele(tdp);
    647  1.68      elad 			return (error);
    648  1.68      elad 		}
    649  1.68      elad 		/*
    650   1.1    bouyer 		 * If directory is "sticky", then user must own
    651   1.1    bouyer 		 * the directory, or the file in it, else she
    652   1.1    bouyer 		 * may not delete it (unless she's root). This
    653   1.1    bouyer 		 * implements append-only directories.
    654   1.1    bouyer 		 */
    655  1.68      elad 		if (dp->i_e2fs_mode & ISVTX) {
    656  1.68      elad 			error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
    657  1.68      elad 			    tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
    658  1.68      elad 			    VTOI(tdp)->i_uid));
    659  1.68      elad 			if (error) {
    660  1.75   hannken 				vrele(tdp);
    661  1.68      elad 				return (EPERM);
    662  1.68      elad 			}
    663  1.68      elad 		}
    664   1.1    bouyer 		*vpp = tdp;
    665   1.1    bouyer 		return (0);
    666   1.1    bouyer 	}
    667   1.1    bouyer 
    668   1.1    bouyer 	/*
    669   1.1    bouyer 	 * If rewriting (RENAME), return the inode and the
    670   1.1    bouyer 	 * information required to rewrite the present directory
    671   1.1    bouyer 	 * Must get inode of directory entry to verify it's a
    672   1.1    bouyer 	 * regular file, or empty directory.
    673   1.1    bouyer 	 */
    674  1.47       chs 	if (nameiop == RENAME && (flags & ISLASTCN)) {
    675  1.54     pooka 		error = VOP_ACCESS(vdp, VWRITE, cred);
    676   1.9    bouyer 		if (error)
    677   1.1    bouyer 			return (error);
    678   1.1    bouyer 		/*
    679   1.1    bouyer 		 * Careful about locking second inode.
    680   1.1    bouyer 		 * This can only occur if the target is ".".
    681   1.1    bouyer 		 */
    682  1.23      yamt 		if (dp->i_number == foundino)
    683   1.1    bouyer 			return (EISDIR);
    684  1.75   hannken 		error = vcache_get(vdp->v_mount,
    685  1.75   hannken 		    &foundino, sizeof(foundino), &tdp);
    686   1.9    bouyer 		if (error)
    687   1.1    bouyer 			return (error);
    688   1.1    bouyer 		*vpp = tdp;
    689   1.1    bouyer 		return (0);
    690   1.1    bouyer 	}
    691   1.1    bouyer 
    692  1.75   hannken 	if (dp->i_number == foundino) {
    693  1.61     pooka 		vref(vdp);	/* we want ourself, ie "." */
    694   1.1    bouyer 		*vpp = vdp;
    695   1.1    bouyer 	} else {
    696  1.75   hannken 		error = vcache_get(vdp->v_mount,
    697  1.75   hannken 		    &foundino, sizeof(foundino), &tdp);
    698  1.29   mycroft 		if (error)
    699   1.1    bouyer 			return (error);
    700   1.1    bouyer 		*vpp = tdp;
    701   1.1    bouyer 	}
    702   1.1    bouyer 
    703   1.1    bouyer 	/*
    704   1.1    bouyer 	 * Insert name into cache if appropriate.
    705   1.1    bouyer 	 */
    706  1.72  dholland 	cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
    707  1.70     rmind 	return 0;
    708   1.1    bouyer }
    709   1.1    bouyer 
    710  1.80  christos int
    711  1.80  christos ext2fs_search_dirblock(struct inode *ip, void *data, int *foundp,
    712  1.80  christos     const char *name, int namelen, int *entryoffsetinblockp,
    713  1.80  christos     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
    714  1.80  christos     struct ext2fs_searchslot *ssp)
    715  1.80  christos {
    716  1.80  christos 	struct vnode *vdp = ITOV(ip);
    717  1.80  christos 	struct ext2fs_direct *ep, *top;
    718  1.80  christos 	uint32_t bsize = ip->i_e2fs->e2fs_bsize;
    719  1.80  christos 	int offset = *entryoffsetinblockp;
    720  1.80  christos 	int namlen;
    721  1.80  christos 
    722  1.80  christos 	ep = (void *)((char *)data + offset);
    723  1.80  christos 	top = (void *)((char *)data + bsize - EXT2_DIR_REC_LEN(0));
    724  1.80  christos 
    725  1.80  christos 	while (ep < top) {
    726  1.80  christos 		/*
    727  1.80  christos 		 * Full validation checks are slow, so we only check
    728  1.80  christos 		 * enough to insure forward progress through the
    729  1.80  christos 		 * directory. Complete checks can be run by setting
    730  1.80  christos 		 * "vfs.e2fs.dirchk" to be true.
    731  1.80  christos 		 */
    732  1.80  christos 		if (ep->e2d_reclen == 0 ||
    733  1.80  christos 		    (dirchk && ext2fs_dirbadentry(vdp, ep, offset))) {
    734  1.80  christos 			int i;
    735  1.80  christos 			ufs_dirbad(ip, *offp, "mangled entry");
    736  1.80  christos 			i = bsize - (offset & (bsize - 1));
    737  1.80  christos 			*offp += i;
    738  1.80  christos 			offset += i;
    739  1.80  christos 			continue;
    740  1.80  christos 		}
    741  1.80  christos 
    742  1.80  christos 		/*
    743  1.80  christos 		 * If an appropriate sized slot has not yet been found,
    744  1.80  christos 		 * check to see if one is available. Also accumulate space
    745  1.80  christos 		 * in the current block so that we can determine if
    746  1.80  christos 		 * compaction is viable.
    747  1.80  christos 		 */
    748  1.80  christos 		if (ssp->slotstatus != FOUND) {
    749  1.80  christos 			int size = ep->e2d_reclen;
    750  1.80  christos 
    751  1.80  christos 			if (ep->e2d_ino != 0)
    752  1.80  christos 				size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
    753  1.80  christos 			if (size >= ssp->slotneeded) {
    754  1.80  christos 				ssp->slotstatus = FOUND;
    755  1.80  christos 				ssp->slotoffset = *offp;
    756  1.80  christos 				ssp->slotsize = ep->e2d_reclen;
    757  1.80  christos 			} else if (size > 0 && ssp->slotstatus == NONE) {
    758  1.80  christos 				ssp->slotfreespace += size;
    759  1.80  christos 				if (ssp->slotoffset == -1)
    760  1.80  christos 					ssp->slotoffset = *offp;
    761  1.80  christos 				if (ssp->slotfreespace >= ssp->slotneeded) {
    762  1.80  christos 					ssp->slotstatus = COMPACT;
    763  1.80  christos 					ssp->slotsize = *offp + ep->e2d_reclen -
    764  1.80  christos 					    ssp->slotoffset;
    765  1.80  christos 				}
    766  1.80  christos 			}
    767  1.80  christos 		}
    768  1.80  christos 
    769  1.80  christos 		/*
    770  1.80  christos 		 * Check for a name match.
    771  1.80  christos 		 */
    772  1.80  christos 		if (ep->e2d_ino) {
    773  1.80  christos 			namlen = ep->e2d_namlen;
    774  1.80  christos 			if (namlen == namelen &&
    775  1.80  christos 			    !memcmp(name, ep->e2d_name, (unsigned)namlen)) {
    776  1.80  christos 				/*
    777  1.80  christos 				 * Save directory entry's inode number and
    778  1.80  christos 				 * reclen in ndp->ni_ufs area, and release
    779  1.80  christos 				 * directory buffer.
    780  1.80  christos 				 */
    781  1.80  christos 				*foundp = 1;
    782  1.80  christos 				return 0;
    783  1.80  christos 			}
    784  1.80  christos 		}
    785  1.80  christos 		*prevoffp = *offp;
    786  1.80  christos 		*offp += ep->e2d_reclen;
    787  1.80  christos 		offset += ep->e2d_reclen;
    788  1.80  christos 		*entryoffsetinblockp = offset;
    789  1.80  christos 		if (ep->e2d_ino)
    790  1.80  christos 			*endusefulp = *offp;
    791  1.80  christos 		/*
    792  1.80  christos 		 * Get pointer to the next entry.
    793  1.80  christos 		 */
    794  1.80  christos 		ep = (void *)((char *)data + offset);
    795  1.80  christos 	}
    796  1.80  christos 
    797  1.80  christos 	return 0;
    798  1.80  christos }
    799  1.80  christos 
    800   1.1    bouyer /*
    801   1.1    bouyer  * Do consistency checking on a directory entry:
    802   1.1    bouyer  *	record length must be multiple of 4
    803   1.1    bouyer  *	entry must fit in rest of its dirblksize block
    804   1.1    bouyer  *	record must be large enough to contain entry
    805  1.36  christos  *	name is not longer than EXT2FS_MAXNAMLEN
    806   1.1    bouyer  *	name must be as long as advertised, and null terminated
    807   1.1    bouyer  */
    808   1.1    bouyer /*
    809   1.1    bouyer  *	changed so that it confirms to ext2fs_check_dir_entry
    810   1.1    bouyer  */
    811   1.1    bouyer static int
    812  1.37   xtraeme ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
    813  1.37   xtraeme 		int entryoffsetinblock)
    814   1.1    bouyer {
    815  1.29   mycroft 	struct ufsmount *ump = VFSTOUFS(dp->v_mount);
    816  1.29   mycroft 	int dirblksiz = ump->um_dirblksiz;
    817   1.1    bouyer 
    818  1.33  christos 		const char *error_msg = NULL;
    819   1.3    bouyer 		int reclen = fs2h16(de->e2d_reclen);
    820  1.13    bouyer 		int namlen = de->e2d_namlen;
    821   1.1    bouyer 
    822   1.3    bouyer 		if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
    823  1.36  christos 			error_msg = "rec_len is smaller than minimal";
    824   1.3    bouyer 		else if (reclen % 4 != 0)
    825  1.36  christos 			error_msg = "rec_len % 4 != 0";
    826  1.36  christos 		else if (namlen > EXT2FS_MAXNAMLEN)
    827  1.36  christos 			error_msg = "namlen > EXT2FS_MAXNAMLEN";
    828   1.3    bouyer 		else if (reclen < EXT2FS_DIRSIZ(namlen))
    829  1.36  christos 			error_msg = "reclen is too small for name_len";
    830  1.29   mycroft 		else if (entryoffsetinblock + reclen > dirblksiz)
    831  1.36  christos 			error_msg = "directory entry across blocks";
    832   1.9    bouyer 		else if (fs2h32(de->e2d_ino) >
    833   1.9    bouyer 		    VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
    834  1.36  christos 			error_msg = "inode out of bounds";
    835   1.1    bouyer 
    836   1.1    bouyer 		if (error_msg != NULL) {
    837   1.1    bouyer 			printf( "bad directory entry: %s\n"
    838   1.9    bouyer 			    "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
    839   1.9    bouyer 			    error_msg, entryoffsetinblock,
    840   1.9    bouyer 			    (unsigned long) fs2h32(de->e2d_ino),
    841   1.9    bouyer 			    reclen, namlen);
    842   1.1    bouyer 			panic("ext2fs_dirbadentry");
    843   1.1    bouyer 		}
    844   1.1    bouyer 		return error_msg == NULL ? 0 : 1;
    845   1.1    bouyer }
    846   1.1    bouyer 
    847   1.1    bouyer /*
    848   1.1    bouyer  * Write a directory entry after a call to namei, using the parameters
    849   1.1    bouyer  * that it left in nameidata.  The argument ip is the inode which the new
    850   1.1    bouyer  * directory entry will refer to.  Dvp is a pointer to the directory to
    851   1.1    bouyer  * be written, which was left locked by namei. Remaining parameters
    852  1.65  dholland  * (ulr_offset, ulr_count) indicate how the space for the new
    853   1.1    bouyer  * entry is to be obtained.
    854   1.1    bouyer  */
    855   1.1    bouyer int
    856  1.66  dholland ext2fs_direnter(struct inode *ip, struct vnode *dvp,
    857  1.66  dholland 		const struct ufs_lookup_results *ulr,
    858  1.66  dholland 		struct componentname *cnp)
    859   1.1    bouyer {
    860  1.15  augustss 	struct ext2fs_direct *ep, *nep;
    861  1.15  augustss 	struct inode *dp;
    862   1.1    bouyer 	struct buf *bp;
    863   1.1    bouyer 	struct ext2fs_direct newdir;
    864   1.1    bouyer 	struct iovec aiov;
    865   1.1    bouyer 	struct uio auio;
    866   1.1    bouyer 	u_int dsize;
    867   1.1    bouyer 	int error, loc, newentrysize, spacefree;
    868   1.1    bouyer 	char *dirbuf;
    869  1.29   mycroft 	struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
    870  1.29   mycroft 	int dirblksiz = ump->um_dirblksiz;
    871   1.1    bouyer 
    872   1.1    bouyer 	dp = VTOI(dvp);
    873  1.65  dholland 
    874   1.3    bouyer 	newdir.e2d_ino = h2fs32(ip->i_number);
    875  1.13    bouyer 	newdir.e2d_namlen = cnp->cn_namelen;
    876  1.13    bouyer 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
    877  1.13    bouyer 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
    878  1.24      fvdl 		newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
    879  1.13    bouyer 	} else {
    880  1.13    bouyer 		newdir.e2d_type = 0;
    881  1.57   tsutsui 	}
    882   1.7     perry 	memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
    883   1.3    bouyer 	newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
    884  1.65  dholland 	if (ulr->ulr_count == 0) {
    885   1.1    bouyer 		/*
    886  1.65  dholland 		 * If ulr_count is 0, then namei could find no
    887  1.65  dholland 		 * space in the directory. Here, ulr_offset will
    888   1.1    bouyer 		 * be on a directory block boundary and we will write the
    889   1.1    bouyer 		 * new entry into a fresh block.
    890   1.1    bouyer 		 */
    891  1.65  dholland 		if (ulr->ulr_offset & (dirblksiz - 1))
    892   1.1    bouyer 			panic("ext2fs_direnter: newblk");
    893  1.65  dholland 		auio.uio_offset = ulr->ulr_offset;
    894  1.29   mycroft 		newdir.e2d_reclen = h2fs16(dirblksiz);
    895   1.1    bouyer 		auio.uio_resid = newentrysize;
    896   1.1    bouyer 		aiov.iov_len = newentrysize;
    897  1.50  christos 		aiov.iov_base = (void *)&newdir;
    898   1.1    bouyer 		auio.uio_iov = &aiov;
    899   1.1    bouyer 		auio.uio_iovcnt = 1;
    900   1.1    bouyer 		auio.uio_rw = UIO_WRITE;
    901  1.40      yamt 		UIO_SETUP_SYSSPACE(&auio);
    902  1.79  riastrad 		error = ext2fs_bufwr(dvp, &auio, IO_SYNC, cnp->cn_cred);
    903  1.29   mycroft 		if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
    904   1.1    bouyer 			/* XXX should grow with balloc() */
    905   1.1    bouyer 			panic("ext2fs_direnter: frag size");
    906   1.1    bouyer 		else if (!error) {
    907  1.31        ws 			error = ext2fs_setsize(dp,
    908  1.31        ws 				roundup(ext2fs_size(dp), dirblksiz));
    909  1.31        ws 			if (error)
    910  1.31        ws 				return (error);
    911   1.1    bouyer 			dp->i_flag |= IN_CHANGE;
    912  1.34       kml 			uvm_vnp_setsize(dvp, ext2fs_size(dp));
    913   1.1    bouyer 		}
    914   1.1    bouyer 		return (error);
    915   1.1    bouyer 	}
    916   1.1    bouyer 
    917   1.1    bouyer 	/*
    918  1.65  dholland 	 * If ulr_count is non-zero, then namei found space
    919  1.65  dholland 	 * for the new entry in the range ulr_offset to
    920  1.65  dholland 	 * ulr_offset + ulr_count in the directory.
    921   1.1    bouyer 	 * To use this space, we may have to compact the entries located
    922   1.1    bouyer 	 * there, by copying them together towards the beginning of the
    923   1.1    bouyer 	 * block, leaving the free space in one usable chunk at the end.
    924   1.1    bouyer 	 */
    925   1.1    bouyer 
    926   1.1    bouyer 	/*
    927   1.1    bouyer 	 * Get the block containing the space for the new directory entry.
    928   1.1    bouyer 	 */
    929  1.65  dholland 	if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0)
    930   1.1    bouyer 		return (error);
    931   1.1    bouyer 	/*
    932   1.1    bouyer 	 * Find space for the new entry. In the simple case, the entry at
    933   1.1    bouyer 	 * offset base will have the space. If it does not, then namei
    934  1.65  dholland 	 * arranged that compacting the region ulr_offset to
    935  1.65  dholland 	 * ulr_offset + ulr_count would yield the
    936   1.1    bouyer 	 * space.
    937   1.1    bouyer 	 */
    938   1.1    bouyer 	ep = (struct ext2fs_direct *)dirbuf;
    939  1.13    bouyer 	dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
    940   1.3    bouyer 	spacefree = fs2h16(ep->e2d_reclen) - dsize;
    941  1.65  dholland 	for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count; ) {
    942   1.1    bouyer 		nep = (struct ext2fs_direct *)(dirbuf + loc);
    943   1.1    bouyer 		if (ep->e2d_ino) {
    944   1.1    bouyer 			/* trim the existing slot */
    945   1.3    bouyer 			ep->e2d_reclen = h2fs16(dsize);
    946   1.1    bouyer 			ep = (struct ext2fs_direct *)((char *)ep + dsize);
    947   1.1    bouyer 		} else {
    948   1.1    bouyer 			/* overwrite; nothing there; header is ours */
    949   1.1    bouyer 			spacefree += dsize;
    950   1.1    bouyer 		}
    951  1.13    bouyer 		dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
    952   1.3    bouyer 		spacefree += fs2h16(nep->e2d_reclen) - dsize;
    953   1.3    bouyer 		loc += fs2h16(nep->e2d_reclen);
    954  1.50  christos 		memcpy((void *)ep, (void *)nep, dsize);
    955   1.1    bouyer 	}
    956   1.1    bouyer 	/*
    957   1.1    bouyer 	 * Update the pointer fields in the previous entry (if any),
    958   1.1    bouyer 	 * copy in the new entry, and write out the block.
    959   1.1    bouyer 	 */
    960   1.1    bouyer 	if (ep->e2d_ino == 0) {
    961   1.1    bouyer #ifdef DIAGNOSTIC
    962   1.1    bouyer 		if (spacefree + dsize < newentrysize)
    963   1.1    bouyer 			panic("ext2fs_direnter: compact1");
    964   1.1    bouyer #endif
    965   1.3    bouyer 		newdir.e2d_reclen = h2fs16(spacefree + dsize);
    966   1.1    bouyer 	} else {
    967   1.1    bouyer #ifdef DIAGNOSTIC
    968   1.1    bouyer 		if (spacefree < newentrysize) {
    969   1.1    bouyer 			printf("ext2fs_direnter: compact2 %u %u",
    970   1.9    bouyer 			    (u_int)spacefree, (u_int)newentrysize);
    971   1.1    bouyer 			panic("ext2fs_direnter: compact2");
    972   1.1    bouyer 		}
    973   1.1    bouyer #endif
    974   1.3    bouyer 		newdir.e2d_reclen = h2fs16(spacefree);
    975   1.3    bouyer 		ep->e2d_reclen = h2fs16(dsize);
    976   1.1    bouyer 		ep = (struct ext2fs_direct *)((char *)ep + dsize);
    977   1.1    bouyer 	}
    978  1.50  christos 	memcpy((void *)ep, (void *)&newdir, (u_int)newentrysize);
    979  1.64   hannken 	error = VOP_BWRITE(bp->b_vp, bp);
    980   1.1    bouyer 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
    981  1.65  dholland 	if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp))
    982  1.65  dholland 		error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC,
    983  1.55     pooka 		    cnp->cn_cred);
    984   1.1    bouyer 	return (error);
    985   1.1    bouyer }
    986   1.1    bouyer 
    987   1.1    bouyer /*
    988   1.1    bouyer  * Remove a directory entry after a call to namei, using
    989  1.65  dholland  * the auxiliary results it provided. The entry
    990  1.65  dholland  * ulr_offset contains the offset into the directory of the
    991  1.65  dholland  * entry to be eliminated.  The ulr_count field contains the
    992   1.1    bouyer  * size of the previous record in the directory.  If this
    993   1.1    bouyer  * is 0, the first entry is being deleted, so we need only
    994   1.1    bouyer  * zero the inode number to mark the entry as free.  If the
    995   1.1    bouyer  * entry is not the first in the directory, we must reclaim
    996   1.1    bouyer  * the space of the now empty record by adding the record size
    997   1.1    bouyer  * to the size of the previous entry.
    998   1.1    bouyer  */
    999   1.1    bouyer int
   1000  1.66  dholland ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr,
   1001  1.66  dholland 		 struct componentname *cnp)
   1002   1.1    bouyer {
   1003  1.15  augustss 	struct inode *dp;
   1004   1.1    bouyer 	struct ext2fs_direct *ep;
   1005   1.1    bouyer 	struct buf *bp;
   1006   1.1    bouyer 	int error;
   1007  1.32     perry 
   1008   1.1    bouyer 	dp = VTOI(dvp);
   1009  1.65  dholland 
   1010  1.65  dholland 	if (ulr->ulr_count == 0) {
   1011   1.1    bouyer 		/*
   1012   1.1    bouyer 		 * First entry in block: set d_ino to zero.
   1013   1.1    bouyer 		 */
   1014  1.65  dholland 		error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset,
   1015  1.22   thorpej 		    (void *)&ep, &bp);
   1016   1.1    bouyer 		if (error != 0)
   1017   1.1    bouyer 			return (error);
   1018   1.1    bouyer 		ep->e2d_ino = 0;
   1019  1.64   hannken 		error = VOP_BWRITE(bp->b_vp, bp);
   1020   1.1    bouyer 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1021   1.1    bouyer 		return (error);
   1022   1.1    bouyer 	}
   1023   1.1    bouyer 	/*
   1024   1.1    bouyer 	 * Collapse new free space into previous entry.
   1025   1.1    bouyer 	 */
   1026  1.65  dholland 	error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count),
   1027  1.22   thorpej 	    (void *)&ep, &bp);
   1028   1.1    bouyer 	if (error != 0)
   1029   1.1    bouyer 		return (error);
   1030  1.65  dholland 	ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen);
   1031  1.64   hannken 	error = VOP_BWRITE(bp->b_vp, bp);
   1032   1.1    bouyer 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1033   1.1    bouyer 	return (error);
   1034   1.1    bouyer }
   1035   1.1    bouyer 
   1036   1.1    bouyer /*
   1037   1.1    bouyer  * Rewrite an existing directory entry to point at the inode
   1038   1.1    bouyer  * supplied.  The parameters describing the directory entry are
   1039   1.1    bouyer  * set up by a call to namei.
   1040   1.1    bouyer  */
   1041   1.1    bouyer int
   1042  1.66  dholland ext2fs_dirrewrite(struct inode *dp, const struct ufs_lookup_results *ulr,
   1043  1.66  dholland     struct inode *ip, struct componentname *cnp)
   1044   1.1    bouyer {
   1045   1.1    bouyer 	struct buf *bp;
   1046   1.1    bouyer 	struct ext2fs_direct *ep;
   1047   1.1    bouyer 	struct vnode *vdp = ITOV(dp);
   1048   1.1    bouyer 	int error;
   1049   1.1    bouyer 
   1050  1.65  dholland 	error = ext2fs_blkatoff(vdp, (off_t)ulr->ulr_offset, (void *)&ep, &bp);
   1051   1.1    bouyer 	if (error != 0)
   1052   1.1    bouyer 		return (error);
   1053   1.3    bouyer 	ep->e2d_ino = h2fs32(ip->i_number);
   1054  1.13    bouyer 	if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
   1055  1.13    bouyer 	    (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
   1056  1.24      fvdl 		ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
   1057  1.13    bouyer 	} else {
   1058  1.13    bouyer 		ep->e2d_type = 0;
   1059  1.13    bouyer 	}
   1060  1.64   hannken 	error = VOP_BWRITE(bp->b_vp, bp);
   1061   1.1    bouyer 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
   1062   1.1    bouyer 	return (error);
   1063   1.1    bouyer }
   1064   1.1    bouyer 
   1065   1.1    bouyer /*
   1066   1.1    bouyer  * Check if a directory is empty or not.
   1067   1.1    bouyer  * Inode supplied must be locked.
   1068   1.1    bouyer  *
   1069   1.1    bouyer  * Using a struct dirtemplate here is not precisely
   1070   1.1    bouyer  * what we want, but better than using a struct ext2fs_direct.
   1071   1.1    bouyer  *
   1072   1.1    bouyer  * NB: does not handle corrupted directories.
   1073   1.1    bouyer  */
   1074   1.1    bouyer int
   1075  1.44      elad ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
   1076   1.1    bouyer {
   1077  1.15  augustss 	off_t off;
   1078   1.1    bouyer 	struct ext2fs_dirtemplate dbuf;
   1079  1.15  augustss 	struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
   1080   1.6    mjacob 	int error, namlen;
   1081   1.6    mjacob 	size_t count;
   1082  1.32     perry 
   1083   1.1    bouyer #define	MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
   1084   1.1    bouyer 
   1085  1.31        ws 	for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
   1086  1.78  riastrad 		error = ufs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
   1087  1.78  riastrad 		    off, IO_NODELOCKED, cred, &count, NULL);
   1088   1.1    bouyer 		/*
   1089   1.1    bouyer 		 * Since we read MINDIRSIZ, residual must
   1090   1.1    bouyer 		 * be 0 unless we're at end of file.
   1091   1.1    bouyer 		 */
   1092   1.1    bouyer 		if (error || count != 0)
   1093   1.1    bouyer 			return (0);
   1094   1.1    bouyer 		/* avoid infinite loops */
   1095   1.1    bouyer 		if (dp->e2d_reclen == 0)
   1096   1.1    bouyer 			return (0);
   1097   1.1    bouyer 		/* skip empty entries */
   1098   1.1    bouyer 		if (dp->e2d_ino == 0)
   1099   1.1    bouyer 			continue;
   1100   1.1    bouyer 		/* accept only "." and ".." */
   1101  1.13    bouyer 		namlen = dp->e2d_namlen;
   1102   1.1    bouyer 		if (namlen > 2)
   1103   1.1    bouyer 			return (0);
   1104   1.1    bouyer 		if (dp->e2d_name[0] != '.')
   1105   1.1    bouyer 			return (0);
   1106   1.1    bouyer 		/*
   1107   1.1    bouyer 		 * At this point namlen must be 1 or 2.
   1108   1.1    bouyer 		 * 1 implies ".", 2 implies ".." if second
   1109   1.1    bouyer 		 * char is also "."
   1110   1.1    bouyer 		 */
   1111   1.1    bouyer 		if (namlen == 1)
   1112   1.1    bouyer 			continue;
   1113   1.3    bouyer 		if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
   1114   1.1    bouyer 			continue;
   1115   1.1    bouyer 		return (0);
   1116   1.1    bouyer 	}
   1117   1.1    bouyer 	return (1);
   1118   1.1    bouyer }
   1119