Home | History | Annotate | Line # | Download | only in adosfs
adlookup.c revision 1.7.10.1
      1  1.7.10.1      elad /*	$NetBSD: adlookup.c,v 1.7.10.1 2006/03/08 01:31:33 elad Exp $	*/
      2       1.1  jdolecek 
      3       1.1  jdolecek /*
      4       1.1  jdolecek  * Copyright (c) 1994 Christian E. Hopps
      5       1.1  jdolecek  * Copyright (c) 1996 Matthias Scheler
      6       1.1  jdolecek  * All rights reserved.
      7       1.1  jdolecek  *
      8       1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
      9       1.1  jdolecek  * modification, are permitted provided that the following conditions
     10       1.1  jdolecek  * are met:
     11       1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     12       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     13       1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     16       1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     17       1.1  jdolecek  *    must display the following acknowledgement:
     18       1.1  jdolecek  *      This product includes software developed by Christian E. Hopps.
     19       1.1  jdolecek  * 4. The name of the author may not be used to endorse or promote products
     20       1.1  jdolecek  *    derived from this software without specific prior written permission
     21       1.1  jdolecek  *
     22       1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.1  jdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1  jdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1  jdolecek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.1  jdolecek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.1  jdolecek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.1  jdolecek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.1  jdolecek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.1  jdolecek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.1  jdolecek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1  jdolecek  */
     33       1.1  jdolecek 
     34       1.1  jdolecek #include <sys/cdefs.h>
     35  1.7.10.1      elad __KERNEL_RCSID(0, "$NetBSD: adlookup.c,v 1.7.10.1 2006/03/08 01:31:33 elad Exp $");
     36       1.1  jdolecek 
     37       1.1  jdolecek #include <sys/param.h>
     38       1.1  jdolecek #include <sys/systm.h>
     39       1.1  jdolecek #include <sys/vnode.h>
     40       1.1  jdolecek #include <sys/namei.h>
     41       1.1  jdolecek #include <sys/mount.h>
     42       1.1  jdolecek #include <sys/time.h>
     43       1.1  jdolecek #include <sys/queue.h>
     44       1.1  jdolecek #include <fs/adosfs/adosfs.h>
     45       1.1  jdolecek 
     46       1.1  jdolecek #ifdef ADOSFS_EXACTMATCH
     47       1.1  jdolecek #define strmatch(s1, l1, s2, l2, i) \
     48       1.1  jdolecek     ((l1) == (l2) && memcmp((s1), (s2), (l1)) == 0)
     49       1.1  jdolecek #else
     50       1.1  jdolecek #define strmatch(s1, l1, s2, l2, i) \
     51       1.1  jdolecek     ((l1) == (l2) && adoscaseequ((s1), (s2), (l1), (i)))
     52       1.1  jdolecek #endif
     53       1.1  jdolecek 
     54       1.1  jdolecek /*
     55       1.1  jdolecek  * adosfs lookup. enters with:
     56       1.1  jdolecek  * pvp (parent vnode) referenced and locked.
     57       1.1  jdolecek  * exit with:
     58       1.1  jdolecek  *	target vp referenced and locked.
     59       1.1  jdolecek  *	unlock pvp unless LOCKPARENT and at end of search.
     60       1.1  jdolecek  * special cases:
     61       1.1  jdolecek  *	pvp == vp, just ref pvp, pvp already holds a ref and lock from
     62       1.1  jdolecek  *	    caller, this will not occur with RENAME or CREATE.
     63       1.1  jdolecek  *	LOOKUP always unlocks parent if last element. (not now!?!?)
     64       1.1  jdolecek  */
     65       1.1  jdolecek int
     66       1.1  jdolecek adosfs_lookup(v)
     67       1.1  jdolecek 	void *v;
     68       1.1  jdolecek {
     69       1.1  jdolecek 	struct vop_lookup_args /* {
     70       1.1  jdolecek 		struct vnode *a_dvp;
     71       1.1  jdolecek 		struct vnode **a_vpp;
     72       1.1  jdolecek 		struct componentname *a_cnp;
     73       1.1  jdolecek 	} */ *sp = v;
     74       1.1  jdolecek 	int nameiop, last, lockp, wantp, flags, error, nocache, i;
     75       1.1  jdolecek 	struct componentname *cnp;
     76       1.1  jdolecek 	struct vnode **vpp;	/* place to store result */
     77       1.1  jdolecek 	struct anode *ap;	/* anode to find */
     78       1.1  jdolecek 	struct vnode *vdp;	/* vnode of search dir */
     79       1.1  jdolecek 	struct anode *adp;	/* anode of search dir */
     80  1.7.10.1      elad 	kauth_cred_t ucp;	/* lookup credentials */
     81       1.1  jdolecek 	u_long bn, plen, hval;
     82       1.1  jdolecek 	const u_char *pelt;
     83       1.1  jdolecek 
     84       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
     85       1.1  jdolecek 	advopprint(sp);
     86       1.1  jdolecek #endif
     87       1.1  jdolecek 	cnp = sp->a_cnp;
     88       1.1  jdolecek 	vdp = sp->a_dvp;
     89       1.1  jdolecek 	adp = VTOA(vdp);
     90       1.1  jdolecek 	vpp = sp->a_vpp;
     91       1.1  jdolecek 	*vpp = NULL;
     92       1.1  jdolecek 	ucp = cnp->cn_cred;
     93       1.1  jdolecek 	nameiop = cnp->cn_nameiop;
     94       1.1  jdolecek 	cnp->cn_flags &= ~PDIRUNLOCK;
     95       1.1  jdolecek 	flags = cnp->cn_flags;
     96       1.1  jdolecek 	last = flags & ISLASTCN;
     97       1.1  jdolecek 	lockp = flags & LOCKPARENT;
     98       1.1  jdolecek 	wantp = flags & (LOCKPARENT | WANTPARENT);
     99       1.1  jdolecek 	pelt = (const u_char *)cnp->cn_nameptr;
    100       1.1  jdolecek 	plen = cnp->cn_namelen;
    101       1.1  jdolecek 	nocache = 0;
    102       1.6     perry 
    103       1.6     perry 	/*
    104       1.1  jdolecek 	 * Check accessiblity of directory.
    105       1.1  jdolecek 	 */
    106       1.7  christos 	if ((error = VOP_ACCESS(vdp, VEXEC, ucp, cnp->cn_lwp)) != 0)
    107       1.1  jdolecek 		return (error);
    108       1.1  jdolecek 
    109       1.1  jdolecek 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
    110       1.1  jdolecek 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    111       1.1  jdolecek 		return (EROFS);
    112       1.1  jdolecek 
    113       1.1  jdolecek 	/*
    114       1.1  jdolecek 	 * Before tediously performing a linear scan of the directory,
    115       1.1  jdolecek 	 * check the name cache to see if the directory/name pair
    116       1.1  jdolecek 	 * we are looking for is known already.
    117       1.1  jdolecek 	 */
    118       1.1  jdolecek 	if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
    119       1.1  jdolecek 		return (error);
    120       1.1  jdolecek 
    121       1.1  jdolecek 	/*
    122       1.1  jdolecek 	 * fake a '.'
    123       1.1  jdolecek 	 */
    124       1.1  jdolecek 	if (plen == 1 && pelt[0] == '.') {
    125       1.1  jdolecek 		/* see special cases in prologue. */
    126       1.1  jdolecek 		*vpp = vdp;
    127       1.1  jdolecek 		goto found;
    128       1.1  jdolecek 	}
    129       1.1  jdolecek 	/*
    130       1.1  jdolecek 	 * fake a ".."
    131       1.1  jdolecek 	 */
    132       1.1  jdolecek 	if (flags & ISDOTDOT) {
    133       1.6     perry 		if (vdp->v_type == VDIR && (vdp->v_flag & VROOT))
    134       1.1  jdolecek 			panic("adosfs .. attempted through root");
    135       1.1  jdolecek 		/*
    136       1.1  jdolecek 		 * cannot get `..' while `vdp' is locked
    137       1.1  jdolecek 		 * e.g. procA holds lock on `..' and waits for `vdp'
    138       1.1  jdolecek 		 * we wait for `..' and hold lock on `vdp'. deadlock.
    139       1.5       wiz 		 * because `vdp' may have been achieved through symlink
    140       1.1  jdolecek 		 * fancy detection code that decreases the race
    141       1.1  jdolecek 		 * window size is not reasonably possible.
    142       1.1  jdolecek 		 *
    143       1.1  jdolecek 		 * basically unlock the parent, try and lock the child (..)
    144       1.6     perry 		 * if that fails relock the parent (ignoring error) and
    145       1.1  jdolecek 		 * fail.  Otherwise we have the child (..) if this is the
    146       1.1  jdolecek 		 * last and the caller requested LOCKPARENT, attempt to
    147       1.1  jdolecek 		 * relock the parent.  If that fails unlock the child (..)
    148       1.1  jdolecek 		 * and fail. Otherwise we have succeded.
    149       1.6     perry 		 *
    150       1.1  jdolecek 		 */
    151       1.1  jdolecek 		VOP_UNLOCK(vdp, 0); /* race */
    152       1.1  jdolecek 		cnp->cn_flags |= PDIRUNLOCK;
    153       1.6     perry 		if ((error = VFS_VGET(vdp->v_mount,
    154       1.1  jdolecek 				      (ino_t)adp->pblock, vpp)) != 0) {
    155       1.1  jdolecek 			if (vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY) == 0)
    156       1.1  jdolecek 				cnp->cn_flags &= ~PDIRUNLOCK;
    157       1.1  jdolecek 		} else if (last && lockp ) {
    158       1.1  jdolecek 		    if ((error = vn_lock(vdp, LK_EXCLUSIVE))) {
    159       1.1  jdolecek 			vput(*vpp);
    160       1.1  jdolecek 		    } else {
    161       1.1  jdolecek 			cnp->cn_flags &= ~PDIRUNLOCK;
    162       1.1  jdolecek 		    }
    163       1.1  jdolecek 		}
    164       1.1  jdolecek 		if (error) {
    165       1.1  jdolecek 			*vpp = NULL;
    166       1.1  jdolecek 			return (error);
    167       1.1  jdolecek 		}
    168       1.1  jdolecek 		goto found_lockdone;
    169       1.1  jdolecek 	}
    170       1.1  jdolecek 
    171       1.1  jdolecek 	/*
    172       1.1  jdolecek 	 * hash the name and grab the first block in chain
    173       1.1  jdolecek 	 * then walk the chain. if chain has not been fully
    174       1.1  jdolecek 	 * walked before, track the count in `tabi'
    175       1.1  jdolecek 	 */
    176       1.1  jdolecek 	hval = adoshash(pelt, plen, adp->ntabent, IS_INTER(adp->amp));
    177       1.1  jdolecek 	bn = adp->tab[hval];
    178       1.1  jdolecek 	i = min(adp->tabi[hval], 0);
    179       1.1  jdolecek 	while (bn != 0) {
    180       1.7  christos 		if ((error = VFS_VGET(vdp->v_mount, (ino_t)bn, vpp
    181       1.7  christos 				      )) != 0) {
    182       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
    183       1.1  jdolecek 			printf("[aget] %d)", error);
    184       1.1  jdolecek #endif
    185       1.1  jdolecek 			/* XXX check to unlock parent possibly? */
    186       1.1  jdolecek 			return(error);
    187       1.1  jdolecek 		}
    188       1.1  jdolecek 		ap = VTOA(*vpp);
    189       1.1  jdolecek 		if (i <= 0) {
    190       1.1  jdolecek 			if (--i < adp->tabi[hval])
    191       1.6     perry 				adp->tabi[hval] = i;
    192       1.1  jdolecek 			/*
    193       1.1  jdolecek 			 * last header in chain lock count down by
    194       1.1  jdolecek 			 * negating it to positive
    195       1.1  jdolecek 			 */
    196       1.1  jdolecek 			if (ap->hashf == 0) {
    197       1.1  jdolecek #ifdef DEBUG
    198       1.1  jdolecek 				if (i != adp->tabi[hval])
    199       1.1  jdolecek 					panic("adlookup: wrong chain count");
    200       1.1  jdolecek #endif
    201       1.1  jdolecek 				adp->tabi[hval] = -adp->tabi[hval];
    202       1.1  jdolecek 			}
    203       1.1  jdolecek 		}
    204       1.6     perry 		if (strmatch(pelt, plen, ap->name, strlen(ap->name),
    205       1.1  jdolecek 		    IS_INTER(adp->amp)))
    206       1.1  jdolecek 			goto found;
    207       1.1  jdolecek 		bn = ap->hashf;
    208       1.1  jdolecek 		vput(*vpp);
    209       1.1  jdolecek 	}
    210       1.1  jdolecek 	*vpp = NULL;
    211       1.1  jdolecek 	/*
    212       1.1  jdolecek 	 * not found
    213       1.1  jdolecek 	 */
    214       1.1  jdolecek 	if ((nameiop == CREATE || nameiop == RENAME) && last) {
    215       1.1  jdolecek 
    216       1.1  jdolecek 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
    217       1.1  jdolecek 			return (EROFS);
    218       1.1  jdolecek 
    219       1.7  christos 		if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_lwp)) != 0) {
    220       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
    221       1.1  jdolecek 			printf("[VOP_ACCESS] %d)", error);
    222       1.1  jdolecek #endif
    223       1.1  jdolecek 			return (error);
    224       1.1  jdolecek 		}
    225       1.1  jdolecek 		if (lockp == 0) {
    226       1.1  jdolecek 			VOP_UNLOCK(vdp, 0);
    227       1.1  jdolecek 			cnp->cn_flags |= PDIRUNLOCK;
    228       1.1  jdolecek 		}
    229       1.1  jdolecek 		cnp->cn_nameiop |= SAVENAME;
    230       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
    231       1.1  jdolecek 		printf("EJUSTRETURN)");
    232       1.1  jdolecek #endif
    233       1.1  jdolecek 		return(EJUSTRETURN);
    234       1.1  jdolecek 	}
    235       1.1  jdolecek 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    236       1.1  jdolecek 		cache_enter(vdp, NULL, cnp);
    237       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
    238       1.1  jdolecek 	printf("ENOENT)");
    239       1.1  jdolecek #endif
    240       1.1  jdolecek 	return(ENOENT);
    241       1.1  jdolecek 
    242       1.1  jdolecek found:
    243       1.1  jdolecek 	if (nameiop == DELETE && last)  {
    244       1.7  christos 		if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_lwp)) != 0) {
    245       1.1  jdolecek 			if (vdp != *vpp)
    246       1.1  jdolecek 				vput(*vpp);
    247       1.1  jdolecek 			*vpp = NULL;
    248       1.1  jdolecek 			return (error);
    249       1.1  jdolecek 		}
    250       1.1  jdolecek 		nocache = 1;
    251       1.6     perry 	}
    252       1.1  jdolecek 	if (nameiop == RENAME && wantp && last) {
    253       1.1  jdolecek 		if (vdp == *vpp)
    254       1.1  jdolecek 			return(EISDIR);
    255       1.7  christos 		if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_lwp)) != 0) {
    256       1.1  jdolecek 			vput(*vpp);
    257       1.1  jdolecek 			*vpp = NULL;
    258       1.1  jdolecek 			return (error);
    259       1.1  jdolecek 		}
    260       1.1  jdolecek 		cnp->cn_flags |= SAVENAME;
    261       1.1  jdolecek 		nocache = 1;
    262       1.1  jdolecek 	}
    263       1.1  jdolecek 	if (vdp == *vpp)
    264       1.1  jdolecek 		VREF(vdp);
    265       1.1  jdolecek 	else if (lockp == 0 || last == 0) {
    266       1.1  jdolecek 		VOP_UNLOCK(vdp, 0);
    267       1.1  jdolecek 		cnp->cn_flags |= PDIRUNLOCK;
    268       1.1  jdolecek 	}
    269       1.1  jdolecek found_lockdone:
    270       1.1  jdolecek 	if ((cnp->cn_flags & MAKEENTRY) && nocache == 0)
    271       1.1  jdolecek 		cache_enter(vdp, *vpp, cnp);
    272       1.1  jdolecek 
    273       1.1  jdolecek #ifdef ADOSFS_DIAGNOSTIC
    274       1.1  jdolecek 	printf("0)\n");
    275       1.1  jdolecek #endif
    276       1.1  jdolecek 	return(0);
    277       1.1  jdolecek }
    278