Home | History | Annotate | Line # | Download | only in filecorefs
filecore_lookup.c revision 1.5.4.3
      1 /*	$NetBSD: filecore_lookup.c,v 1.5.4.3 2007/10/27 11:35:02 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1993, 1994 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	filecore_lookup.c	1.1	1998/6/26
     32  */
     33 
     34 /*-
     35  * Copyright (c) 1998 Andrew McMurry
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	filecore_lookup.c	1.1	1998/6/26
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: filecore_lookup.c,v 1.5.4.3 2007/10/27 11:35:02 yamt Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/namei.h>
     73 #include <sys/buf.h>
     74 #include <sys/file.h>
     75 #include <sys/vnode.h>
     76 #include <sys/mount.h>
     77 #include <sys/systm.h>
     78 
     79 #include <fs/filecorefs/filecore.h>
     80 #include <fs/filecorefs/filecore_extern.h>
     81 #include <fs/filecorefs/filecore_node.h>
     82 
     83 struct	nchstats filecore_nchstats;
     84 
     85 /*
     86  * Convert a component of a pathname into a pointer to a locked inode.
     87  * This is a very central and rather complicated routine.
     88  * If the file system is not maintained in a strict tree hierarchy,
     89  * this can result in a deadlock situation (see comments in code below).
     90  *
     91  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
     92  * whether the name is to be looked up, created, renamed, or deleted.
     93  * When CREATE, RENAME, or DELETE is specified, information usable in
     94  * creating, renaming, or deleting a directory entry may be calculated.
     95  * If flag has LOCKPARENT or'ed into it and the target of the pathname
     96  * exists, lookup returns both the target and its parent directory locked.
     97  * When creating or renaming and LOCKPARENT is specified, the target may
     98  * not be ".".  When deleting and LOCKPARENT is specified, the target may
     99  * be "."., but the caller must check to ensure it does an vrele and iput
    100  * instead of two iputs.
    101  *
    102  * Overall outline of ufs_lookup:
    103  *
    104  *	check accessibility of directory
    105  *	look for name in cache, if found, then if at end of path
    106  *	  and deleting or creating, drop it, else return name
    107  *	search for name in directory, to found or notfound
    108  * notfound:
    109  *	if creating, return locked directory, leaving info on available slots
    110  *	else return error
    111  * found:
    112  *	if at end of path and deleting, return information to allow delete
    113  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
    114  *	  inode and return info to allow rewrite
    115  *	if not at end, add name to cache; if at end and neither creating
    116  *	  nor deleting, add name to cache
    117  *
    118  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
    119  */
    120 int
    121 filecore_lookup(v)
    122 	void *v;
    123 {
    124 	struct vop_lookup_args /* {
    125 		struct vnode *a_dvp;
    126 		struct vnode **a_vpp;
    127 		struct componentname *a_cnp;
    128 	} */ *ap = v;
    129 	struct vnode *vdp;		/* vnode for directory being searched */
    130 	struct filecore_node *dp;	/* inode for directory being searched */
    131 	struct filecore_mnt *fcmp;	/* file system that directory is in */
    132 	struct buf *bp;			/* a buffer of directory entries */
    133 	struct filecore_direntry *de;
    134 	int numdirpasses;		/* strategy for directory search */
    135 	struct vnode *pdp;		/* saved dp during symlink work */
    136 	struct vnode *tdp;		/* returned by filecore_vget_internal */
    137 	int error;
    138 	u_short namelen;
    139 	int res;
    140 	const char *name;
    141 	struct vnode **vpp = ap->a_vpp;
    142 	struct componentname *cnp = ap->a_cnp;
    143 	kauth_cred_t cred = cnp->cn_cred;
    144 	int flags;
    145 	int nameiop = cnp->cn_nameiop;
    146 	int i, endsearch;
    147 
    148 	flags = cnp->cn_flags;
    149 
    150 	bp = NULL;
    151 	*vpp = NULL;
    152 	vdp = ap->a_dvp;
    153 	dp = VTOI(vdp);
    154 	fcmp = dp->i_mnt;
    155 
    156 	/*
    157 	 * Check accessiblity of directory.
    158 	 */
    159 	if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_lwp)) != 0)
    160 		return (error);
    161 
    162 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    163 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    164 		return (EROFS);
    165 
    166 	/*
    167 	 * We now have a segment name to search for, and a directory to search.
    168 	 *
    169 	 * Before tediously performing a linear scan of the directory,
    170 	 * check the name cache to see if the directory/name pair
    171 	 * we are looking for is known already.
    172 	 */
    173 	if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
    174 		return (error);
    175 
    176 	name = cnp->cn_nameptr;
    177 	namelen = cnp->cn_namelen;
    178 
    179 	/*
    180 	 * If there is cached information on a previous search of
    181 	 * this directory, pick up where we last left off.
    182 	 * We cache only lookups as these are the most common
    183 	 * and have the greatest payoff. Caching CREATE has little
    184 	 * benefit as it usually must search the entire directory
    185 	 * to determine that the entry does not exist. Caching the
    186 	 * location of the last DELETE or RENAME has not reduced
    187 	 * profiling time and hence has been removed in the interest
    188 	 * of simplicity.
    189 	 */
    190 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
    191 	    dp->i_diroff >= FILECORE_MAXDIRENTS) {
    192 		i = 0;
    193 		numdirpasses = 1;
    194 	} else {
    195 		i = dp->i_diroff;
    196 		numdirpasses = 2;
    197 		filecore_nchstats.ncs_2passes++;
    198 	}
    199 	endsearch = FILECORE_MAXDIRENTS;
    200 
    201 	if ((flags & ISDOTDOT) || (name[0] == '.' && namelen == 1))
    202 		goto found;
    203 
    204 	error = filecore_dbread(dp, &bp);
    205 	if (error) {
    206 		brelse(bp, 0);
    207 		return error;
    208 	}
    209 
    210 	de = fcdirentry(bp->b_data, i);
    211 
    212 searchloop:
    213 	while (de->name[0] != 0 && i < endsearch) {
    214 		/*
    215 		 * Check for a name match.
    216 		 */
    217 		res = filecore_fncmp(de->name, name, namelen);
    218 
    219 		if (res == 0)
    220 			goto found;
    221 		if (res < 0)
    222 			goto notfound;
    223 
    224 		i++;
    225 		de++;
    226 	}
    227 
    228 notfound:
    229 	/*
    230 	 * If we started in the middle of the directory and failed
    231 	 * to find our target, we must check the beginning as well.
    232 	 */
    233 	if (numdirpasses == 2) {
    234 		numdirpasses--;
    235 		i = 0;
    236 		de = fcdirentry(bp->b_data, i);
    237 		endsearch = dp->i_diroff;
    238 		goto searchloop;
    239 	}
    240 	if (bp != NULL) {
    241 #ifdef FILECORE_DEBUG_BR
    242 			printf("brelse(%p) lo1\n", bp);
    243 #endif
    244 		brelse(bp, 0);
    245 	}
    246 
    247 	/*
    248 	 * Insert name into cache (as non-existent) if appropriate.
    249 	 */
    250 	if (cnp->cn_flags & MAKEENTRY)
    251 		cache_enter(vdp, *vpp, cnp);
    252 	if (nameiop == CREATE || nameiop == RENAME)
    253 		return (EROFS);
    254 	return (ENOENT);
    255 
    256 found:
    257 	if (numdirpasses == 2)
    258 		filecore_nchstats.ncs_pass2++;
    259 
    260 	/*
    261 	 * Found component in pathname.
    262 	 * If the final component of path name, save information
    263 	 * in the cache as to where the entry was found.
    264 	 */
    265 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
    266 		dp->i_diroff = i;
    267 
    268 	/*
    269 	 * Step through the translation in the name.  We do not `iput' the
    270 	 * directory because we may need it again if a symbolic link
    271 	 * is relative to the current directory.  Instead we save it
    272 	 * unlocked as "pdp".  We must get the target inode before unlocking
    273 	 * the directory to insure that the inode will not be removed
    274 	 * before we get it.  We prevent deadlock by always fetching
    275 	 * inodes from the root, moving down the directory tree. Thus
    276 	 * when following backward pointers ".." we must unlock the
    277 	 * parent directory before getting the requested directory.
    278 	 * There is a potential race condition here if both the current
    279 	 * and parent directories are removed before the `iget' for the
    280 	 * inode associated with ".." returns.  We hope that this occurs
    281 	 * infrequently since we cannot avoid this race condition without
    282 	 * implementing a sophisticated deadlock detection algorithm.
    283 	 * Note also that this simple deadlock detection scheme will not
    284 	 * work if the file system has any hard links other than ".."
    285 	 * that point backwards in the directory structure.
    286 	 */
    287 	pdp = vdp;
    288 
    289 	/*
    290 	 * If ino is different from dp->i_ino,
    291 	 * it's a relocated directory.
    292 	 */
    293 	if (flags & ISDOTDOT) {
    294 		ino_t pin = filecore_getparent(dp);
    295 
    296 		VOP_UNLOCK(pdp, 0);	/* race to get the inode */
    297 		error = VFS_VGET(vdp->v_mount, pin, &tdp);
    298 		vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
    299 		if (error) {
    300 			return error;
    301 		}
    302 		*vpp = tdp;
    303 	} else if (name[0] == '.' && namelen == 1) {
    304 		VREF(vdp);	/* we want ourself, ie "." */
    305 		*vpp = vdp;
    306 	} else {
    307 #ifdef FILECORE_DEBUG_BR
    308 			printf("brelse(%p) lo4\n", bp);
    309 #endif
    310 		brelse(bp, 0);
    311 		error = VFS_VGET(vdp->v_mount, dp->i_dirent.addr |
    312 		    (i << FILECORE_INO_INDEX), &tdp);
    313 		if (error)
    314 			return (error);
    315 		*vpp = tdp;
    316 	}
    317 
    318 	/*
    319 	 * Insert name into cache if appropriate.
    320 	 */
    321 	if (cnp->cn_flags & MAKEENTRY)
    322 		cache_enter(vdp, *vpp, cnp);
    323 	return (0);
    324 }
    325