1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* $OpenBSD: adlookup.c,v 1.13 2003/03/16 00:30:38 margarida Exp $ */
/* $NetBSD: adlookup.c,v 1.17 1996/10/25 23:13:58 cgd Exp $ */
/*
* Copyright (c) 1994 Christian E. Hopps
* Copyright (c) 1996 Matthias Scheler
* 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>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <adosfs/adosfs.h>
#ifdef ADOSFS_EXACTMATCH
#define strmatch(s1, l1, s2, l2, i) \
((l1) == (l2) && bcmp((s1), (s2), (l1)) == 0)
#else
#define strmatch(s1, l1, s2, l2, i) \
((l1) == (l2) && adoscaseequ((s1), (s2), (l1), (i)))
#endif
/*
* adosfs lookup. enters with:
* pvp (parent vnode) referenced and locked.
* exit with:
* target vp referenced and locked.
* 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
adosfs_lookup(v)
void *v;
{
struct vop_lookup_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
} */ *sp = v;
int nameiop, last, lockp, wantp, flags, error, 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 */
struct proc *p;
u_int32_t plen, hval, vpid;
daddr_t bn;
char *pelt;
#ifdef ADOSFS_DIAGNOSTIC
advopprint(sp);
#endif
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;
cnp->cn_flags &= ~PDIRUNLOCK;
flags = cnp->cn_flags;
last = flags & ISLASTCN;
lockp = flags & LOCKPARENT;
wantp = flags & (LOCKPARENT | WANTPARENT);
pelt = cnp->cn_nameptr;
plen = cnp->cn_namelen;
p = cnp->cn_proc;
nocache = 0;
/*
* check that:
* pvp is a dir, and that the user has rights to access
*/
if (vdp->v_type != VDIR)
return (ENOTDIR);
if ((error = VOP_ACCESS(vdp, VEXEC, ucp, cnp->cn_proc)) != 0)
return (error);
if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
return (EROFS);
/*
* Before tediously performing a linear scan of the directory,
* check the name cache to see if the directory/name pair
* we are looking for is known already.
*/
if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
return (error);
/*
* fake a '.'
*/
if (plen == 1 && pelt[0] == '.') {
/* see special cases in prologue. */
*vpp = vdp;
goto found;
}
/*
* fake a ".."
*/
if (flags & ISDOTDOT) {
if (vdp->v_type == VDIR && (vdp->v_flag & VROOT))
panic("adosfs .. attempted lookup through root");
/*
* 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.
* because `vdp' may have been achieved through symlink
* fancy detection code that decreases the race
* window size is not reasonably possible.
*
* 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 succeeded.
*
*/
VOP_UNLOCK(vdp, 0, p); /* race */
cnp->cn_flags |= PDIRUNLOCK;
if ((error = VFS_VGET(vdp->v_mount, ABLKTOINO(adp->pblock),
vpp)) != 0) {
if (vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY, p) == 0)
cnp->cn_flags &= ~PDIRUNLOCK;
} else if (last && lockp) {
if ((error = vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY, p)))
vput(*vpp);
else
cnp->cn_flags &= ~PDIRUNLOCK;
}
if (error) {
*vpp = NULL;
return (error);
}
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'
*/
hval = adoshash(pelt, plen, adp->ntabent, IS_INTER(adp->amp));
bn = adp->tab[hval];
i = min(adp->tabi[hval], 0);
while (bn != 0) {
if ((error = VFS_VGET(vdp->v_mount, ABLKTOINO(bn), vpp)) !=
0) {
#ifdef ADOSFS_DIAGNOSTIC
printf("[aget] %d)", error);
#endif
/* XXX check to unlock parent possibly? */
return(error);
}
ap = VTOA(*vpp);
if (i <= 0) {
if (--i < adp->tabi[hval])
adp->tabi[hval] = i;
/*
* last header in chain lock count down by
* negating it to positive
*/
if (ap->hashf == 0) {
#ifdef DEBUG
if (i != adp->tabi[hval])
panic("adlookup: wrong chain count");
#endif
adp->tabi[hval] = -adp->tabi[hval];
}
}
if (strmatch(pelt, plen, ap->name, strlen(ap->name),
IS_INTER(adp->amp)))
goto found;
bn = ap->hashf;
vput(*vpp);
}
*vpp = NULL;
/*
* not found
*/
if ((nameiop == CREATE || nameiop == RENAME) && last) {
if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) != 0)
{
#ifdef ADOSFS_DIAGNOSTIC
printf("[VOP_ACCESS] %d)", error);
#endif
return (error);
}
if (lockp == 0) {
VOP_UNLOCK(vdp, 0, p);
cnp->cn_flags |= PDIRUNLOCK;
}
cnp->cn_nameiop |= SAVENAME;
#ifdef ADOSFS_DIAGNOSTIC
printf("EJUSTRETURN)");
#endif
return(EJUSTRETURN);
}
if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
cache_enter(vdp, NULL, cnp);
#ifdef ADOSFS_DIAGNOSTIC
printf("ENOENT)");
#endif
return(ENOENT);
found:
if (nameiop == DELETE && last) {
if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) != 0)
{
if (vdp != *vpp)
vput(*vpp);
*vpp = NULL;
return (error);
}
nocache = 1;
}
if (nameiop == RENAME && wantp && last) {
if (vdp == *vpp)
return(EISDIR);
if ((error = VOP_ACCESS(vdp, VWRITE, ucp, cnp->cn_proc)) != 0)
{
vput(*vpp);
*vpp = NULL;
return (error);
}
cnp->cn_flags |= SAVENAME;
nocache = 1;
}
if (vdp == *vpp)
VREF(vdp);
else if (lockp == 0 || last == 0) {
VOP_UNLOCK(vdp, 0, p);
cnp->cn_flags |= PDIRUNLOCK;
}
found_lockdone:
if ((cnp->cn_flags & MAKEENTRY) && nocache == 0)
cache_enter(vdp, *vpp, cnp);
#ifdef ADOSFS_DIAGNOSTIC
printf("0)\n");
#endif
return(0);
}
|