1995-08-18 19:14:33 +04:00
|
|
|
/* $NetBSD: adlookup.c,v 1.10 1995/08/18 15:14:33 chopps Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-05-11 22:49:06 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Christian E. Hopps
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Christian E. Hopps.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
1994-10-29 10:58:24 +03:00
|
|
|
#include <sys/systm.h>
|
1994-05-11 22:49:06 +04:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/namei.h>
|
1994-06-18 00:06:05 +04:00
|
|
|
#include <sys/mount.h>
|
1994-05-11 22:49:06 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <adosfs/adosfs.h>
|
|
|
|
|
1994-12-28 11:51:56 +03:00
|
|
|
#ifdef ADOSFS_EXACTMATCH
|
1994-05-11 22:49:06 +04:00
|
|
|
#define strmatch(s1, l1, s2, l2) \
|
|
|
|
((l1) == (l2) && bcmp((s1), (s2), (l1)) == 0)
|
1994-12-28 11:51:56 +03:00
|
|
|
#else
|
|
|
|
int
|
|
|
|
strmatch(s1, l1, s2, l2)
|
|
|
|
char *s1, *s2;
|
|
|
|
int l1, l2;
|
|
|
|
{
|
|
|
|
if (l1 != l2)
|
|
|
|
return 0;
|
|
|
|
while (--l1 >= 0) {
|
|
|
|
char c;
|
|
|
|
c = *s1++;
|
|
|
|
if (c != *s2) {
|
|
|
|
if (c >= 'A' && c <= 'Z' && c + ('a' - 'A') != *s2)
|
|
|
|
return 0;
|
|
|
|
if (c >= 'a' && c <= 'z' && c + ('A' - 'a') != *s2)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
1994-05-11 22:49:06 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* adosfs lookup. enters with:
|
|
|
|
* pvp (parent vnode) referenced and locked.
|
|
|
|
* exit with:
|
1995-08-18 19:14:33 +04:00
|
|
|
* target vp referenced and locked.
|
1994-05-11 22:49:06 +04:00
|
|
|
* unlock pvp unless LOCKPARENT and at end of search.
|
|
|
|
* special cases:
|
|
|
|
* pvp == vp, just ref pvp, pvp already holds a ref and lock from
|
|
|
|
* caller, this will not occur with RENAME or CREATE.
|
|
|
|
* LOOKUP always unlocks parent if last element. (not now!?!?)
|
|
|
|
*/
|
|
|
|
int
|
1994-06-18 00:06:05 +04:00
|
|
|
adosfs_lookup(sp)
|
|
|
|
struct vop_lookup_args /* {
|
|
|
|
struct vnode *a_dvp;
|
|
|
|
struct vnode **a_vpp;
|
|
|
|
struct componentname *a_cnp;
|
|
|
|
} */ *sp;
|
1994-05-11 22:49:06 +04:00
|
|
|
{
|
1994-06-18 00:06:05 +04:00
|
|
|
int nameiop, last, lockp, wantp, flags, error, vpid, nocache, i;
|
|
|
|
struct componentname *cnp;
|
|
|
|
struct vnode **vpp; /* place to store result */
|
|
|
|
struct anode *ap; /* anode to find */
|
|
|
|
struct vnode *vdp; /* vnode of search dir */
|
|
|
|
struct anode *adp; /* anode of search dir */
|
|
|
|
struct ucred *ucp; /* lookup credentials */
|
1994-05-11 22:49:06 +04:00
|
|
|
u_long bn, plen, hval;
|
1994-06-18 00:06:05 +04:00
|
|
|
char *pelt;
|
|
|
|
|
1994-05-11 22:49:06 +04:00
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
1994-06-18 00:06:05 +04:00
|
|
|
advopprint(sp);
|
1994-05-11 22:49:06 +04:00
|
|
|
#endif
|
1994-06-18 00:06:05 +04:00
|
|
|
cnp = sp->a_cnp;
|
|
|
|
vdp = sp->a_dvp;
|
|
|
|
adp = VTOA(vdp);
|
|
|
|
vpp = sp->a_vpp;
|
|
|
|
*vpp = NULL;
|
|
|
|
ucp = cnp->cn_cred;
|
|
|
|
nameiop = cnp->cn_nameiop;
|
|
|
|
flags = cnp->cn_flags;
|
|
|
|
last = flags & ISLASTCN;
|
|
|
|
lockp = flags & LOCKPARENT;
|
|
|
|
wantp = flags & (LOCKPARENT | WANTPARENT);
|
|
|
|
pelt = cnp->cn_nameptr;
|
|
|
|
plen = cnp->cn_namelen;
|
1994-05-11 22:49:06 +04:00
|
|
|
nocache = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check that:
|
|
|
|
* pvp is a dir, and that the user has rights to access
|
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
if (vdp->v_type != VDIR)
|
|
|
|
return (ENOTDIR);
|
|
|
|
if (error = VOP_ACCESS(vdp, VEXEC, ucp, cnp->cn_proc))
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
/*
|
|
|
|
* cache lookup algorithm borrowed from ufs_lookup()
|
|
|
|
* its not consistent with otherthings in this function..
|
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
if (error = cache_lookup(vdp, vpp, cnp)) {
|
|
|
|
if (error == ENOENT)
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
|
1994-06-18 00:06:05 +04:00
|
|
|
vpid = (*vpp)->v_id;
|
|
|
|
if (vdp == *vpp) {
|
|
|
|
VREF(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
error = 0;
|
1994-06-18 00:06:05 +04:00
|
|
|
} else if (flags & ISDOTDOT) {
|
|
|
|
VOP_UNLOCK(vdp); /* race */
|
|
|
|
error = vget(*vpp, 1);
|
1994-05-11 22:49:06 +04:00
|
|
|
if (error == 0 && lockp && last)
|
1994-06-18 00:06:05 +04:00
|
|
|
error = VOP_LOCK(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
} else {
|
1994-06-18 00:06:05 +04:00
|
|
|
error = vget(*vpp, 1);
|
|
|
|
/* if (lockp == 0 || error || last) */
|
|
|
|
if (lockp == 0 || error || last == 0)
|
|
|
|
VOP_UNLOCK(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
if (error == 0) {
|
1994-06-18 00:06:05 +04:00
|
|
|
if (vpid == vdp->v_id)
|
|
|
|
return (0);
|
|
|
|
vput(*vpp);
|
|
|
|
if (lockp && vdp != *vpp && last)
|
|
|
|
VOP_UNLOCK(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
*vpp = NULL;
|
|
|
|
if (error = VOP_LOCK(vdp))
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fake a '.'
|
|
|
|
*/
|
|
|
|
if (plen == 1 && pelt[0] == '.') {
|
|
|
|
/* see special cases in prologue. */
|
1994-06-18 00:06:05 +04:00
|
|
|
*vpp = vdp;
|
1994-05-11 22:49:06 +04:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* fake a ".."
|
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
if (flags & ISDOTDOT) {
|
|
|
|
if (vdp->v_type == VDIR && (vdp->v_flag & VROOT))
|
1994-05-11 22:49:06 +04:00
|
|
|
panic("adosfs .. attemped through root");
|
|
|
|
/*
|
1994-06-18 00:06:05 +04:00
|
|
|
* cannot get `..' while `vdp' is locked
|
|
|
|
* e.g. procA holds lock on `..' and waits for `vdp'
|
|
|
|
* we wait for `..' and hold lock on `vdp'. deadlock.
|
|
|
|
* becuase `vdp' may have been acheived through symlink
|
1994-05-11 22:49:06 +04:00
|
|
|
* fancy detection code that decreases the race
|
|
|
|
* window size is not reasonably possible.
|
1994-06-18 00:06:05 +04:00
|
|
|
*
|
|
|
|
* basically unlock the parent, try and lock the child (..)
|
|
|
|
* if that fails relock the parent (ignoring error) and
|
|
|
|
* fail. Otherwise we have the child (..) if this is the
|
|
|
|
* last and the caller requested LOCKPARENT, attempt to
|
|
|
|
* relock the parent. If that fails unlock the child (..)
|
|
|
|
* and fail. Otherwise we have succeded.
|
|
|
|
*
|
1994-05-11 22:49:06 +04:00
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
VOP_UNLOCK(vdp); /* race */
|
|
|
|
if (error = VFS_VGET(vdp->v_mount, (ino_t)adp->pblock, vpp))
|
|
|
|
VOP_LOCK(vdp);
|
|
|
|
else if (last && lockp && (error = VOP_LOCK(vdp)))
|
|
|
|
vput(*vpp);
|
1994-05-11 22:49:06 +04:00
|
|
|
if (error) {
|
1994-06-18 00:06:05 +04:00
|
|
|
*vpp = NULL;
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
goto found_lockdone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hash the name and grab the first block in chain
|
|
|
|
* then walk the chain. if chain has not been fully
|
|
|
|
* walked before, track the count in `tabi'
|
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
hval = adoshash(pelt, plen, adp->ntabent);
|
|
|
|
bn = adp->tab[hval];
|
|
|
|
i = min(adp->tabi[hval], 0);
|
1994-05-11 22:49:06 +04:00
|
|
|
while (bn != 0) {
|
1994-06-18 00:06:05 +04:00
|
|
|
if (error = VFS_VGET(vdp->v_mount, (ino_t)bn, vpp)) {
|
1994-05-11 22:49:06 +04:00
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
|
|
|
printf("[aget] %d)", error);
|
|
|
|
#endif
|
1994-06-18 00:06:05 +04:00
|
|
|
/* XXX check to unlock parent possibly? */
|
1994-05-11 22:49:06 +04:00
|
|
|
return(error);
|
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
ap = VTOA(*vpp);
|
1994-06-03 03:40:56 +04:00
|
|
|
if (i <= 0) {
|
1994-06-18 00:06:05 +04:00
|
|
|
if (--i < adp->tabi[hval])
|
|
|
|
adp->tabi[hval] = i;
|
1994-06-03 03:40:56 +04:00
|
|
|
/*
|
|
|
|
* last header in chain lock count down by
|
|
|
|
* negating it to positive
|
|
|
|
*/
|
|
|
|
if (ap->hashf == 0) {
|
|
|
|
#ifdef DEBUG
|
1994-06-18 00:06:05 +04:00
|
|
|
if (i != adp->tabi[hval])
|
1994-06-03 03:40:56 +04:00
|
|
|
panic("adlookup: wrong chain count");
|
|
|
|
#endif
|
1994-06-18 00:06:05 +04:00
|
|
|
adp->tabi[hval] = -adp->tabi[hval];
|
1994-06-03 03:40:56 +04:00
|
|
|
}
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
if (strmatch(pelt, plen, ap->name, strlen(ap->name)))
|
|
|
|
goto found;
|
|
|
|
bn = ap->hashf;
|
1994-06-18 00:06:05 +04:00
|
|
|
vput(*vpp);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
*vpp = NULL;
|
1994-05-11 22:49:06 +04:00
|
|
|
/*
|
|
|
|
* not found
|
|
|
|
*/
|
1994-06-18 00:06:05 +04:00
|
|
|
if ((nameiop == CREATE || nameiop == RENAME) && last) {
|
|
|
|
if (error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) {
|
1994-05-11 22:49:06 +04:00
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
|
|
|
printf("[VOP_ACCESS] %d)", error);
|
|
|
|
#endif
|
1994-06-18 00:06:05 +04:00
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
if (lockp == 0)
|
1994-06-18 00:06:05 +04:00
|
|
|
VOP_UNLOCK(vdp);
|
|
|
|
cnp->cn_nameiop |= SAVENAME;
|
1994-05-11 22:49:06 +04:00
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
|
|
|
printf("EJUSTRETURN)");
|
|
|
|
#endif
|
|
|
|
return(EJUSTRETURN);
|
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
|
|
|
|
cache_enter(vdp, NULL, cnp);
|
1994-05-11 22:49:06 +04:00
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
|
|
|
printf("ENOENT)");
|
|
|
|
#endif
|
|
|
|
return(ENOENT);
|
|
|
|
|
|
|
|
found:
|
1994-06-18 00:06:05 +04:00
|
|
|
if (nameiop == DELETE && last) {
|
|
|
|
if (error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) {
|
|
|
|
if (vdp != *vpp)
|
|
|
|
vput(*vpp);
|
|
|
|
*vpp = NULL;
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
|
|
|
nocache = 1;
|
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
if (nameiop == RENAME && wantp && last) {
|
|
|
|
if (vdp == *vpp)
|
1994-05-11 22:49:06 +04:00
|
|
|
return(EISDIR);
|
1994-06-18 00:06:05 +04:00
|
|
|
if (error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) {
|
|
|
|
vput(*vpp);
|
|
|
|
*vpp = NULL;
|
|
|
|
return (error);
|
1994-05-11 22:49:06 +04:00
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
cnp->cn_flags |= SAVENAME;
|
1994-05-11 22:49:06 +04:00
|
|
|
nocache = 1;
|
|
|
|
}
|
1994-06-18 00:06:05 +04:00
|
|
|
if (vdp == *vpp)
|
|
|
|
VREF(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
else if (lockp == 0 || last == 0)
|
1994-06-18 00:06:05 +04:00
|
|
|
VOP_UNLOCK(vdp);
|
1994-05-11 22:49:06 +04:00
|
|
|
found_lockdone:
|
1994-06-18 00:06:05 +04:00
|
|
|
if ((cnp->cn_flags & MAKEENTRY) && nocache == 0)
|
|
|
|
cache_enter(vdp, *vpp, cnp);
|
1994-05-11 22:49:06 +04:00
|
|
|
|
|
|
|
#ifdef ADOSFS_DIAGNOSTIC
|
|
|
|
printf("0)\n");
|
|
|
|
#endif
|
|
|
|
return(0);
|
|
|
|
}
|