Enable PutVCache for FBSD80 and higher
[openafs.git] / src / afs / VNOPS / afs_vnop_lookup.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * afs_lookup
13  * EvalMountPoint
14  * afs_DoBulkStat
15  */
16
17 #include <afsconfig.h>
18 #include "afs/param.h"
19
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21 #include "afsincludes.h"        /* Afs-based standard headers */
22 #include "afs/afs_stats.h"      /* statistics */
23 #include "afs/afs_cbqueue.h"
24 #include "afs/nfsclient.h"
25 #include "afs/exporter.h"
26 #include "afs/afs_osidnlc.h"
27 #include "afs/afs_dynroot.h"
28
29 extern struct vcache *afs_globalVp;
30
31 afs_int32 afs_bkvolpref = 0;
32 afs_int32 afs_bulkStatsDone;
33 static int bulkStatCounter = 0; /* counter for bulk stat seq. numbers */
34 int afs_fakestat_enable = 0;    /* 1: fakestat-all, 2: fakestat-crosscell */
35
36
37 /* this would be faster if it did comparison as int32word, but would be 
38  * dependant on byte-order and alignment, and I haven't figured out
39  * what "@sys" is in binary... */
40 #define AFS_EQ_ATSYS(name) (((name)[0]=='@')&&((name)[1]=='s')&&((name)[2]=='y')&&((name)[3]=='s')&&(!(name)[4]))
41
42 /* call under write lock, evaluate mvid field from a mt pt.
43  * avc is the vnode of the mount point object; must be write-locked.
44  * advc is the vnode of the containing directory (optional; if NULL and
45  *   EvalMountPoint succeeds, caller must initialize *avolpp->dotdot)
46  * avolpp is where we return a pointer to the volume named by the mount pt, if success
47  * areq is the identity of the caller.
48  *
49  * NOTE: this function returns a held volume structure in *volpp if it returns 0!
50  */
51 static int
52 EvalMountData(char type, char *data, afs_uint32 states, afs_uint32 cellnum,
53               struct volume **avolpp, register struct vrequest *areq,
54               afs_uint32 *acellidxp, afs_uint32 *avolnump,
55               afs_uint32 *avnoidp, afs_uint32 *auniqp)
56 {
57     struct volume *tvp = 0;
58     struct VenusFid tfid;
59     struct cell *tcell;
60     char *cpos, *volnamep;
61     char *buf, *endptr;
62     afs_int32 prefetch;         /* 1=>None  2=>RO  3=>BK */
63     afs_int32 mtptCell, assocCell = 0, hac = 0;
64     afs_int32 samecell, roname, len;
65     afs_uint32 volid = 0, cellidx, vnoid = 0, uniq = 0;
66
67     /* Start by figuring out and finding the cell */
68     cpos = afs_strchr(data, ':');       /* if cell name present */
69     if (cpos) {
70         volnamep = cpos + 1;
71         *cpos = 0;
72         if ((afs_strtoi_r(data, &endptr, &cellnum) == 0) &&
73             (endptr == cpos))
74             tcell = afs_GetCell(cellnum, READ_LOCK);
75         else {
76             tcell = afs_GetCellByName(data, READ_LOCK);
77             cellnum = 0;
78         }
79         *cpos = ':';
80     } else if (cellnum) {
81         volnamep = data;
82         tcell = afs_GetCell(cellnum, READ_LOCK);
83     } else {
84         /* No cellname or cellnum; return ENODEV */
85         return ENODEV;
86     }
87     if (!tcell) {
88         /* no cell found; return ENODEV */
89         return ENODEV;
90     }
91
92     cellidx = tcell->cellIndex;
93     mtptCell = tcell->cellNum;  /* The cell for the mountpoint */
94     if (tcell->lcellp) {
95         hac = 1;                /* has associated cell */
96         assocCell = tcell->lcellp->cellNum;     /* The associated cell */
97     }
98     afs_PutCell(tcell, READ_LOCK);
99
100     /* If there's nothing to look up, we can't proceed */
101     if (!*volnamep)
102         return ENODEV;
103
104     /* cell found. figure out volume */
105     cpos = afs_strchr(volnamep, ':');
106     if (cpos)
107         *cpos = 0;
108
109     /* Look for an all-numeric volume ID */
110     if ((afs_strtoi_r(volnamep, &endptr, &volid) == 0) &&
111         ((endptr == cpos) || (!*endptr)))
112     {
113         /* Ok. Is there a vnode and uniq? */
114         if (cpos) {
115             char *vnodep = (char *)(cpos + 1);
116             char *uniqp = NULL;
117             if ((!*vnodep) /* no vnode after colon */
118                 || !(uniqp = afs_strchr(vnodep, ':')) /* no colon for uniq */
119                 || (!*(++uniqp)) /* no uniq after colon */
120                 || (afs_strtoi_r(vnodep, &endptr, &vnoid) != 0) /* bad vno */
121                 || (*endptr != ':') /* bad vnode field */
122                 || (afs_strtoi_r(uniqp, &endptr, &uniq) != 0) /* bad uniq */
123                 || (*endptr)) /* anything after uniq */
124             {
125                 *cpos = ':';
126                 /* sorry. vnode and uniq, or nothing */
127                 return ENODEV;
128             }
129         }
130     } else
131             volid = 0;
132
133     /*
134      * If the volume ID was all-numeric, and they didn't ask for a
135      * pointer to the volume structure, then just return the number
136      * as-is.  This is currently only used for handling name lookups
137      * in the dynamic mount directory.
138      */
139     if (volid && !avolpp) {
140         if (*cpos)
141             *cpos = ':';
142         goto done;
143     }
144
145     /*
146      * If the volume ID was all-numeric, and the type was '%', then
147      * assume whoever made the mount point knew what they were doing,
148      * and don't second-guess them by forcing use of a RW volume when
149      * they gave the ID of something else.
150      */
151     if (volid && type == '%') {
152         tfid.Fid.Volume = volid;        /* remember BK volume */
153         tfid.Cell = mtptCell;
154         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
155         if (cpos) /* one way or another we're done */
156             *cpos = ':';
157         if (!tvp)
158             return ENODEV; /* afs_GetVolume failed; return ENODEV */
159         goto done;
160     }
161
162     /* Is volume name a "<n>.backup" or "<n>.readonly" name */
163     len = strlen(volnamep);
164     roname = ((len > 9) && (strcmp(&volnamep[len - 9], ".readonly") == 0))
165         || ((len > 7) && (strcmp(&volnamep[len - 7], ".backup") == 0));
166
167     /* When we cross mountpoint, do we stay in the same cell */
168     samecell = (cellnum == mtptCell) || (hac && (cellnum == assocCell));
169
170     /* Decide whether to prefetch the BK, or RO.  Also means we want the BK or
171      * RO.
172      * If this is a regular mountpoint with a RW volume name
173      * - If BK preference is enabled AND we remain within the same cell AND
174      *   start from a BK volume, then we will want to prefetch the BK volume.
175      * - If we cross a cell boundary OR start from a RO volume, then we will
176      *   want to prefetch the RO volume.
177      */
178     if ((type == '#') && !roname) {
179         if (afs_bkvolpref && samecell && (states & CBackup))
180             prefetch = 3;       /* Prefetch the BK */
181         else if (!samecell || (states & CRO))
182             prefetch = 2;       /* Prefetch the RO */
183         else
184             prefetch = 1;       /* Do not prefetch */
185     } else {
186         prefetch = 1;           /* Do not prefetch */
187     }
188
189     /* Get the volume struct. Unless this volume name has ".readonly" or
190      * ".backup" in it, this will get the volume struct for the RW volume.
191      * The RO volume will be prefetched if requested (but not returned).
192      * Set up to use volname first.
193      */
194     tvp = afs_GetVolumeByName(volnamep, mtptCell, prefetch, areq, WRITE_LOCK);
195
196     /* If no volume was found in this cell, try the associated linked cell */
197     if (!tvp && hac && areq->volumeError) {
198         tvp =
199             afs_GetVolumeByName(volnamep, assocCell, prefetch, areq,
200                                 WRITE_LOCK);
201     }
202
203     /* Still not found. If we are looking for the RO, then perhaps the RW 
204      * doesn't exist? Try adding ".readonly" to volname and look for that.
205      * Don't know why we do this. Would have still found it in above call - jpm.
206      */
207     if (!tvp && (prefetch == 2) && len < AFS_SMALLOCSIZ - 10) {
208         buf = (char *)osi_AllocSmallSpace(len + 10);
209
210         strcpy(buf, volnamep);
211         afs_strcat(buf, ".readonly");
212
213         tvp = afs_GetVolumeByName(buf, mtptCell, 1, areq, WRITE_LOCK);
214
215         /* Try the associated linked cell if failed */
216         if (!tvp && hac && areq->volumeError) {
217             tvp = afs_GetVolumeByName(buf, assocCell, 1, areq, WRITE_LOCK);
218         }
219         osi_FreeSmallSpace(buf);
220     }
221     /* done with volname */
222     if (cpos)
223         *cpos = ':';
224     if (!tvp)
225         return ENODEV;          /* Couldn't find the volume */
226     else
227         volid = tvp->volume;
228
229     /* Don't cross mountpoint from a BK to a BK volume */
230     if ((states & CBackup) && (tvp->states & VBackup)) {
231         afs_PutVolume(tvp, WRITE_LOCK);
232         return ENODEV;
233     }
234
235     /* If we want (prefetched) the BK and it exists, then drop the RW volume
236      * and get the BK.
237      * Otherwise, if we want (prefetched0 the RO and it exists, then drop the
238      * RW volume and get the RO.
239      * Otherwise, go with the RW.
240      */
241     if ((prefetch == 3) && tvp->backVol) {
242         tfid.Fid.Volume = tvp->backVol; /* remember BK volume */
243         tfid.Cell = tvp->cell;
244         afs_PutVolume(tvp, WRITE_LOCK); /* release old volume */
245         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
246         if (!tvp)
247             return ENODEV;      /* oops, can't do it */
248     } else if ((prefetch >= 2) && tvp->roVol) {
249         tfid.Fid.Volume = tvp->roVol;   /* remember RO volume */
250         tfid.Cell = tvp->cell;
251         afs_PutVolume(tvp, WRITE_LOCK); /* release old volume */
252         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
253         if (!tvp)
254             return ENODEV;      /* oops, can't do it */
255     }
256
257 done:
258     if (acellidxp)
259         *acellidxp = cellidx;
260     if (avolnump)
261         *avolnump = volid;
262     if (avnoidp)
263         *avnoidp = vnoid;
264     if (auniqp)
265         *auniqp = uniq;
266     if (avolpp)
267         *avolpp = tvp;
268     else if (tvp)
269         afs_PutVolume(tvp, WRITE_LOCK);
270     return 0;
271 }
272
273 int
274 EvalMountPoint(register struct vcache *avc, struct vcache *advc,
275                struct volume **avolpp, register struct vrequest *areq)
276 {
277     afs_int32 code;
278     afs_uint32 avnoid, auniq;
279
280     AFS_STATCNT(EvalMountPoint);
281 #ifdef notdef
282     if (avc->mvid && (avc->f.states & CMValid))
283         return 0;               /* done while racing */
284 #endif
285     *avolpp = NULL;
286     code = afs_HandleLink(avc, areq);
287     if (code)
288         return code;
289
290     /* Determine which cell and volume the mointpoint goes to */
291     code = EvalMountData(avc->linkData[0], avc->linkData + 1,
292                          avc->f.states, avc->f.fid.Cell, avolpp, areq, 0, 0,
293                          &avnoid, &auniq);
294     if (code) return code;
295
296     if (!avnoid)
297         avnoid = 1;
298
299     if (!auniq)
300         auniq = 1;
301
302     if (avc->mvid == 0)
303         avc->mvid =
304             (struct VenusFid *)osi_AllocSmallSpace(sizeof(struct VenusFid));
305     avc->mvid->Cell = (*avolpp)->cell;
306     avc->mvid->Fid.Volume = (*avolpp)->volume;
307     avc->mvid->Fid.Vnode = avnoid;
308     avc->mvid->Fid.Unique = auniq;
309     avc->f.states |= CMValid;
310
311     /* Used to: if the mount point is stored within a backup volume,
312      * then we should only update the parent pointer information if
313      * there's none already set, so as to avoid updating a volume's ..
314      * info with something in an OldFiles directory.
315      *
316      * Next two lines used to be under this if:
317      *
318      * if (!(avc->f.states & CBackup) || tvp->dotdot.Fid.Volume == 0)
319      *
320      * Now: update mount point back pointer on every call, so that we handle
321      * multiple mount points better.  This way, when du tries to go back
322      * via chddir(".."), it will end up exactly where it started, yet
323      * cd'ing via a new path to a volume will reset the ".." pointer
324      * to the new path.
325      */
326     (*avolpp)->mtpoint = avc->f.fid;    /* setup back pointer to mtpoint */
327     
328     if (advc)
329         (*avolpp)->dotdot = advc->f.fid;
330
331     return 0;
332 }
333
334 /*
335  * afs_InitFakeStat
336  *
337  * Must be called on an afs_fakestat_state object before calling
338  * afs_EvalFakeStat or afs_PutFakeStat.  Calling afs_PutFakeStat
339  * without calling afs_EvalFakeStat is legal, as long as this
340  * function is called.
341  */
342 void
343 afs_InitFakeStat(struct afs_fakestat_state *state)
344 {
345     if (!afs_fakestat_enable)
346         return;
347
348     state->valid = 1;
349     state->did_eval = 0;
350     state->need_release = 0;
351 }
352
353 /*
354  * afs_EvalFakeStat_int
355  *
356  * The actual implementation of afs_EvalFakeStat and afs_TryEvalFakeStat,
357  * which is called by those wrapper functions.
358  *
359  * Only issues RPCs if canblock is non-zero.
360  */
361 static int
362 afs_EvalFakeStat_int(struct vcache **avcp, struct afs_fakestat_state *state,
363                      struct vrequest *areq, int canblock)
364 {
365     struct vcache *tvc, *root_vp;
366     struct volume *tvolp = NULL;
367     int code = 0;
368
369     if (!afs_fakestat_enable)
370         return 0;
371
372     osi_Assert(state->valid == 1);
373     osi_Assert(state->did_eval == 0);
374     state->did_eval = 1;
375
376     tvc = *avcp;
377     if (tvc->mvstat != 1)
378         return 0;
379
380     if (canblock) {
381         /* Is the call to VerifyVCache really necessary? */
382         code = afs_VerifyVCache(tvc, areq);
383         if (code)
384             goto done;
385
386         ObtainWriteLock(&tvc->lock, 599);
387         code = EvalMountPoint(tvc, NULL, &tvolp, areq);
388         ReleaseWriteLock(&tvc->lock);
389         if (code)
390             goto done;
391         if (tvolp) {
392             tvolp->dotdot = tvc->f.fid;
393             tvolp->dotdot.Fid.Vnode = tvc->f.parent.vnode;
394             tvolp->dotdot.Fid.Unique = tvc->f.parent.unique;
395         }
396     }
397     if (tvc->mvid && (tvc->f.states & CMValid)) {
398         if (!canblock) {
399             afs_int32 retry;
400
401             do {
402                 retry = 0;
403                 ObtainWriteLock(&afs_xvcache, 597);
404                 root_vp = afs_FindVCache(tvc->mvid, &retry, IS_WLOCK);
405                 if (root_vp && retry) {
406                     ReleaseWriteLock(&afs_xvcache);
407                     afs_PutVCache(root_vp);
408                 }
409             } while (root_vp && retry);
410             ReleaseWriteLock(&afs_xvcache);
411         } else {
412             root_vp = afs_GetVCache(tvc->mvid, areq, NULL, NULL);
413         }
414         if (!root_vp) {
415             code = canblock ? ENOENT : 0;
416             goto done;
417         }
418 #ifdef AFS_DARWIN80_ENV
419         root_vp->f.m.Type = VDIR;
420         AFS_GUNLOCK();
421         code = afs_darwin_finalizevnode(root_vp, NULL, NULL, 0, 0);
422         AFS_GLOCK();
423         if (code) goto done;
424         vnode_ref(AFSTOV(root_vp));
425 #endif
426         if (tvolp && !afs_InReadDir(root_vp)) {
427             /* Is this always kosher?  Perhaps we should instead use
428              * NBObtainWriteLock to avoid potential deadlock.
429              */
430             ObtainWriteLock(&root_vp->lock, 598);
431             if (!root_vp->mvid)
432                 root_vp->mvid = osi_AllocSmallSpace(sizeof(struct VenusFid));
433             *root_vp->mvid = tvolp->dotdot;
434             ReleaseWriteLock(&root_vp->lock);
435         }
436         state->need_release = 1;
437         state->root_vp = root_vp;
438         *avcp = root_vp;
439         code = 0;
440     } else {
441         code = canblock ? ENOENT : 0;
442     }
443
444   done:
445     if (tvolp)
446         afs_PutVolume(tvolp, WRITE_LOCK);
447     return code;
448 }
449
450 /*
451  * afs_EvalFakeStat
452  *
453  * Automatically does the equivalent of EvalMountPoint for vcache entries
454  * which are mount points.  Remembers enough state to properly release
455  * the volume root vcache when afs_PutFakeStat() is called.
456  *
457  * State variable must be initialized by afs_InitFakeState() beforehand.
458  *
459  * Returns 0 when everything succeeds and *avcp points to the vcache entry
460  * that should be used for the real vnode operation.  Returns non-zero if
461  * something goes wrong and the error code should be returned to the user.
462  */
463 int
464 afs_EvalFakeStat(struct vcache **avcp, struct afs_fakestat_state *state,
465                  struct vrequest *areq)
466 {
467     return afs_EvalFakeStat_int(avcp, state, areq, 1);
468 }
469
470 /*
471  * afs_TryEvalFakeStat
472  *
473  * Same as afs_EvalFakeStat, but tries not to talk to remote servers
474  * and only evaluate the mount point if all the data is already in
475  * local caches.
476  *
477  * Returns 0 if everything succeeds and *avcp points to a valid
478  * vcache entry (possibly evaluated).
479  */
480 int
481 afs_TryEvalFakeStat(struct vcache **avcp, struct afs_fakestat_state *state,
482                     struct vrequest *areq)
483 {
484     return afs_EvalFakeStat_int(avcp, state, areq, 0);
485 }
486
487 /*
488  * afs_PutFakeStat
489  *
490  * Perform any necessary cleanup at the end of a vnode op, given that
491  * afs_InitFakeStat was previously called with this state.
492  */
493 void
494 afs_PutFakeStat(struct afs_fakestat_state *state)
495 {
496     if (!afs_fakestat_enable)
497         return;
498
499     osi_Assert(state->valid == 1);
500     if (state->need_release)
501         afs_PutVCache(state->root_vp);
502     state->valid = 0;
503 }
504
505 int
506 afs_ENameOK(register char *aname)
507 {
508     register int tlen;
509
510     AFS_STATCNT(ENameOK);
511     tlen = strlen(aname);
512     if (tlen >= 4 && strcmp(aname + tlen - 4, "@sys") == 0)
513         return 0;
514     return 1;
515 }
516
517 static int
518 afs_getsysname(register struct vrequest *areq, register struct vcache *adp,
519                register char *bufp, int *num, char **sysnamelist[])
520 {
521     register struct unixuser *au;
522     register afs_int32 error;
523
524     AFS_STATCNT(getsysname);
525
526     *sysnamelist = afs_sysnamelist;
527
528     if (!afs_nfsexporter)
529         strcpy(bufp, (*sysnamelist)[0]);
530     else {
531         au = afs_GetUser(areq->uid, adp->f.fid.Cell, 0);
532         if (au->exporter) {
533             error = EXP_SYSNAME(au->exporter, (char *)0, sysnamelist, num, 0);
534             if (error) {
535                 strcpy(bufp, "@sys");
536                 afs_PutUser(au, 0);
537                 return -1;
538             } else {
539                 strcpy(bufp, (*sysnamelist)[0]);
540             }
541         } else
542             strcpy(bufp, afs_sysname);
543         afs_PutUser(au, 0);
544     }
545     return 0;
546 }
547
548 void
549 Check_AtSys(register struct vcache *avc, const char *aname,
550             struct sysname_info *state, struct vrequest *areq)
551 {
552     int num = 0;
553     char **sysnamelist[MAXNUMSYSNAMES];
554
555     if (AFS_EQ_ATSYS(aname)) {
556         state->offset = 0;
557         state->name = (char *)osi_AllocLargeSpace(MAXSYSNAME);
558         state->allocked = 1;
559         state->index =
560             afs_getsysname(areq, avc, state->name, &num, sysnamelist);
561     } else {
562         state->offset = -1;
563         state->allocked = 0;
564         state->index = 0;
565         state->name = (char *)aname;
566     }
567 }
568
569 int
570 Next_AtSys(register struct vcache *avc, struct vrequest *areq,
571            struct sysname_info *state)
572 {
573     int num = afs_sysnamecount;
574     char **sysnamelist[MAXNUMSYSNAMES];
575
576     if (state->index == -1)
577         return 0;               /* No list */
578
579     /* Check for the initial state of aname != "@sys" in Check_AtSys */
580     if (state->offset == -1 && state->allocked == 0) {
581         register char *tname;
582
583         /* Check for .*@sys */
584         for (tname = state->name; *tname; tname++)
585             /*Move to the end of the string */ ;
586
587         if ((tname > state->name + 4) && (AFS_EQ_ATSYS(tname - 4))) {
588             state->offset = (tname - 4) - state->name;
589             tname = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
590             strncpy(tname, state->name, state->offset);
591             state->name = tname;
592             state->allocked = 1;
593             num = 0;
594             state->index =
595                 afs_getsysname(areq, avc, state->name + state->offset, &num,
596                                sysnamelist);
597             return 1;
598         } else
599             return 0;           /* .*@sys doesn't match either */
600     } else {
601         register struct unixuser *au;
602         register afs_int32 error;
603
604         *sysnamelist = afs_sysnamelist;
605
606         if (afs_nfsexporter) {
607             au = afs_GetUser(areq->uid, avc->f.fid.Cell, 0);
608             if (au->exporter) {
609                 error =
610                     EXP_SYSNAME(au->exporter, (char *)0, sysnamelist, &num, 0);
611                 if (error) {
612                     afs_PutUser(au, 0);
613                     return 0;
614                 }
615             }
616             afs_PutUser(au, 0);
617         }
618         if (++(state->index) >= num || !(*sysnamelist)[(unsigned int)state->index])
619             return 0;           /* end of list */
620     }
621     strcpy(state->name + state->offset, (*sysnamelist)[(unsigned int)state->index]);
622     return 1;
623 }
624
625 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
626
627 /* called with an unlocked directory and directory cookie.  Areqp
628  * describes who is making the call.
629  * Scans the next N (about 30, typically) directory entries, and does
630  * a bulk stat call to stat them all.
631  *
632  * Must be very careful when merging in RPC responses, since we dont
633  * want to overwrite newer info that was added by a file system mutating
634  * call that ran concurrently with our bulk stat call.
635  *
636  * We do that, as described below, by not merging in our info (always
637  * safe to skip the merge) if the status info is valid in the vcache entry.
638  *
639  * If adapt ever implements the bulk stat RPC, then this code will need to
640  * ensure that vcaches created for failed RPC's to older servers have the
641  * CForeign bit set.
642  */
643 static struct vcache *BStvc = NULL;
644
645 int
646 afs_DoBulkStat(struct vcache *adp, long dirCookie, struct vrequest *areqp)
647 {
648     int nentries;               /* # of entries to prefetch */
649     int nskip;                  /* # of slots in the LRU queue to skip */
650 #ifdef AFS_DARWIN80_ENV
651     int npasses = 0;
652     struct vnode *lruvp;
653 #endif
654     struct vcache *lruvcp;      /* vcache ptr of our goal pos in LRU queue */
655     struct dcache *dcp;         /* chunk containing the dir block */
656     afs_size_t temp;            /* temp for holding chunk length, &c. */
657     struct AFSFid *fidsp;       /* file IDs were collecting */
658     struct AFSCallBack *cbsp;   /* call back pointers */
659     struct AFSCallBack *tcbp;   /* temp callback ptr */
660     struct AFSFetchStatus *statsp;      /* file status info */
661     struct AFSVolSync volSync;  /* vol sync return info */
662     struct vcache *tvcp;        /* temp vcp */
663     struct afs_q *tq;           /* temp queue variable */
664     AFSCBFids fidParm;          /* file ID parm for bulk stat */
665     AFSBulkStats statParm;      /* stat info parm for bulk stat */
666     int fidIndex = 0;           /* which file were stating */
667     struct afs_conn *tcp = 0;   /* conn for call */
668     AFSCBs cbParm;              /* callback parm for bulk stat */
669     struct server *hostp = 0;   /* host we got callback from */
670     long startTime;             /* time we started the call,
671                                  * for callback expiration base
672                                  */
673     afs_size_t statSeqNo = 0;   /* Valued of file size to detect races */
674     int code;                   /* error code */
675     long newIndex;              /* new index in the dir */
676     struct DirEntry *dirEntryp; /* dir entry we are examining */
677     int i;
678     struct VenusFid afid;       /* file ID we are using now */
679     struct VenusFid tfid;       /* another temp. file ID */
680     afs_int32 retry;            /* handle low-level SGI MP race conditions */
681     long volStates;             /* flags from vol structure */
682     struct volume *volp = 0;    /* volume ptr */
683     struct VenusFid dotdot = {0, {0, 0, 0}};
684     int flagIndex = 0;          /* First file with bulk fetch flag set */
685     int inlinebulk = 0;         /* Did we use InlineBulk RPC or not? */
686     XSTATS_DECLS;
687     dotdot.Cell = 0;
688     dotdot.Fid.Unique = 0;
689     dotdot.Fid.Vnode = 0;
690
691     /* first compute some basic parameters.  We dont want to prefetch more
692      * than a fraction of the cache in any given call, and we want to preserve
693      * a portion of the LRU queue in any event, so as to avoid thrashing
694      * the entire stat cache (we will at least leave some of it alone).
695      * presently dont stat more than 1/8 the cache in any one call.      */
696     nentries = afs_cacheStats / 8;
697
698     /* dont bother prefetching more than one calls worth of info */
699     if (nentries > AFSCBMAX)
700         nentries = AFSCBMAX;
701
702     /* heuristic to make sure that things fit in 4K.  This means that
703      * we shouldnt make it any bigger than 47 entries.  I am typically
704      * going to keep it a little lower, since we don't want to load
705      * too much of the stat cache.
706      */
707     if (nentries > 30)
708         nentries = 30;
709
710     /* now, to reduce the stack size, well allocate two 4K blocks,
711      * one for fids and callbacks, and one for stat info.  Well set
712      * up our pointers to the memory from there, too.
713      */
714     statsp = (AFSFetchStatus *) 
715             osi_Alloc(AFSCBMAX * sizeof(AFSFetchStatus));
716     fidsp = (AFSFid *) osi_AllocLargeSpace(nentries * sizeof(AFSFid));
717     cbsp = (AFSCallBack *) 
718             osi_Alloc(AFSCBMAX * sizeof(AFSCallBack));
719
720     /* next, we must iterate over the directory, starting from the specified
721      * cookie offset (dirCookie), and counting out nentries file entries.
722      * We skip files that already have stat cache entries, since we
723      * dont want to bulk stat files that are already in the cache.
724      */
725   tagain:
726     code = afs_VerifyVCache(adp, areqp);
727     if (code)
728         goto done2;
729
730     dcp = afs_GetDCache(adp, (afs_size_t) 0, areqp, &temp, &temp, 1);
731     if (!dcp) {
732         code = ENOENT;
733         goto done2;
734     }
735
736     /* lock the directory cache entry */
737     ObtainReadLock(&adp->lock);
738     ObtainReadLock(&dcp->lock);
739
740     /*
741      * Make sure that the data in the cache is current. There are two
742      * cases we need to worry about:
743      * 1. The cache data is being fetched by another process.
744      * 2. The cache data is no longer valid
745      */
746     while ((adp->f.states & CStatd)
747            && (dcp->dflags & DFFetching)
748            && hsame(adp->f.m.DataVersion, dcp->f.versionNo)) {
749         afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
750                    __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER, dcp,
751                    ICL_TYPE_INT32, dcp->dflags);
752         ReleaseReadLock(&dcp->lock);
753         ReleaseReadLock(&adp->lock);
754         afs_osi_Sleep(&dcp->validPos);
755         ObtainReadLock(&adp->lock);
756         ObtainReadLock(&dcp->lock);
757     }
758     if (!(adp->f.states & CStatd)
759         || !hsame(adp->f.m.DataVersion, dcp->f.versionNo)) {
760         ReleaseReadLock(&dcp->lock);
761         ReleaseReadLock(&adp->lock);
762         afs_PutDCache(dcp);
763         goto tagain;
764     }
765
766     /* Generate a sequence number so we can tell whether we should
767      * store the attributes when processing the response. This number is
768      * stored in the file size when we set the CBulkFetching bit. If the
769      * CBulkFetching is still set and this value hasn't changed, then
770      * we know we were the last to set CBulkFetching bit for this file,
771      * and it is safe to set the status information for this file.
772      */
773     statSeqNo = bulkStatCounter++;
774     /* ensure against wrapping */
775     if (statSeqNo == 0)
776         statSeqNo = bulkStatCounter++;
777
778     /* now we have dir data in the cache, so scan the dir page */
779     fidIndex = 0;
780     flagIndex = 0;
781     while (1) {                 /* Should probably have some constant bound */
782         /* look for first safe entry to examine in the directory.  BlobScan
783          * looks for a the 1st allocated dir after the dirCookie slot.
784          */
785         newIndex = BlobScan(dcp, (dirCookie >> 5));
786         if (newIndex == 0)
787             break;
788
789         /* remember the updated directory cookie */
790         dirCookie = newIndex << 5;
791
792         /* get a ptr to the dir entry */
793         dirEntryp =
794             (struct DirEntry *)afs_dir_GetBlob(dcp, newIndex);
795         if (!dirEntryp)
796             break;
797
798         /* dont copy more than we have room for */
799         if (fidIndex >= nentries) {
800             DRelease(dirEntryp, 0);
801             break;
802         }
803
804         /* now, if the dir entry looks good, copy it out to our list.  Vnode
805          * 0 means deleted, although it should also be free were it deleted.
806          */
807         if (dirEntryp->fid.vnode != 0) {
808             /* dont copy entries we have in our cache.  This check will
809              * also make us skip "." and probably "..", unless it has
810              * disappeared from the cache since we did our namei call.
811              */
812             tfid.Cell = adp->f.fid.Cell;
813             tfid.Fid.Volume = adp->f.fid.Fid.Volume;
814             tfid.Fid.Vnode = ntohl(dirEntryp->fid.vnode);
815             tfid.Fid.Unique = ntohl(dirEntryp->fid.vunique);
816             do {
817                 retry = 0;
818                 ObtainWriteLock(&afs_xvcache, 130);
819                 tvcp = afs_FindVCache(&tfid, &retry, IS_WLOCK /* no stats | LRU */ );
820                 if (tvcp && retry) {
821                     ReleaseWriteLock(&afs_xvcache);
822                     afs_PutVCache(tvcp);
823                 }
824             } while (tvcp && retry);
825             if (!tvcp) {        /* otherwise, create manually */
826                 tvcp = afs_NewBulkVCache(&tfid, hostp, statSeqNo);
827                 if (tvcp)
828                 {
829                         ObtainWriteLock(&tvcp->lock, 505);
830                         ReleaseWriteLock(&afs_xvcache);
831                         afs_RemoveVCB(&tfid);
832                         ReleaseWriteLock(&tvcp->lock);
833                 } else {
834                         ReleaseWriteLock(&afs_xvcache);
835                 }
836             } else {
837                 ReleaseWriteLock(&afs_xvcache);
838             }
839             if (!tvcp)
840             {
841                 DRelease(dirEntryp, 0);
842                 ReleaseReadLock(&dcp->lock);
843                 ReleaseReadLock(&adp->lock);
844                 afs_PutDCache(dcp);
845                 goto done;      /* can happen if afs_NewVCache fails */
846             }
847
848             /* WARNING: afs_DoBulkStat uses the Length field to store a
849              * sequence number for each bulk status request. Under no
850              * circumstances should afs_DoBulkStat store a sequence number
851              * if the new length will be ignored when afs_ProcessFS is
852              * called with new stats. */
853 #ifdef AFS_SGI_ENV
854             if (!(tvcp->f.states & CStatd)
855                 && (!((tvcp->f.states & CBulkFetching) &&
856                       (tvcp->f.m.Length != statSeqNo)))
857                 && (tvcp->execsOrWriters <= 0)
858                 && !afs_DirtyPages(tvcp)
859                 && !AFS_VN_MAPPED((vnode_t *) tvcp))
860 #else
861             if (!(tvcp->f.states & CStatd)
862                 && (!((tvcp->f.states & CBulkFetching) &&
863                       (tvcp->f.m.Length != statSeqNo)))
864                 && (tvcp->execsOrWriters <= 0)
865                 && !afs_DirtyPages(tvcp))
866 #endif
867
868             {
869                 /* this entry doesnt exist in the cache, and is not
870                  * already being fetched by someone else, so add it to the
871                  * list of file IDs to obtain.
872                  *
873                  * We detect a callback breaking race condition by checking the
874                  * CBulkFetching state bit and the value in the file size.
875                  * It is safe to set the status only if the CBulkFetching
876                  * flag is still set and the value in the file size does
877                  * not change. NewBulkVCache sets us up for the new ones.
878                  * Set up the rest here.
879                  *
880                  * Don't fetch status for dirty files. We need to
881                  * preserve the value of the file size. We could
882                  * flush the pages, but it wouldn't be worthwhile.
883                  */
884                 if (!(tvcp->f.states & CBulkFetching)) {
885                     tvcp->f.states |= CBulkFetching;
886                     tvcp->f.m.Length = statSeqNo;
887                 }
888                 memcpy((char *)(fidsp + fidIndex), (char *)&tfid.Fid,
889                        sizeof(*fidsp));
890                 fidIndex++;
891             }
892             afs_PutVCache(tvcp);
893         }
894
895         /* if dir vnode has non-zero entry */
896         /* move to the next dir entry by adding in the # of entries
897          * used by this dir entry.
898          */
899         temp = afs_dir_NameBlobs(dirEntryp->name) << 5;
900         DRelease(dirEntryp, 0);
901         if (temp <= 0)
902             break;
903         dirCookie += temp;
904     }                           /* while loop over all dir entries */
905
906     /* now release the dir lock and prepare to make the bulk RPC */
907     ReleaseReadLock(&dcp->lock);
908     ReleaseReadLock(&adp->lock);
909
910     /* release the chunk */
911     afs_PutDCache(dcp);
912
913     /* dont make a null call */
914     if (fidIndex == 0)
915         goto done;
916
917     do {
918         /* setup the RPC parm structures */
919         fidParm.AFSCBFids_len = fidIndex;
920         fidParm.AFSCBFids_val = fidsp;
921         statParm.AFSBulkStats_len = fidIndex;
922         statParm.AFSBulkStats_val = statsp;
923         cbParm.AFSCBs_len = fidIndex;
924         cbParm.AFSCBs_val = cbsp;
925
926         /* start the timer; callback expirations are relative to this */
927         startTime = osi_Time();
928
929         tcp = afs_Conn(&adp->f.fid, areqp, SHARED_LOCK);
930         if (tcp) {
931             hostp = tcp->srvr->server;
932             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_BULKSTATUS);
933             RX_AFS_GUNLOCK();
934
935             if (!(tcp->srvr->server->flags & SNO_INLINEBULK)) {
936             retryonce:
937                 code =
938                     RXAFS_InlineBulkStatus(tcp->id, &fidParm, &statParm,
939                                            &cbParm, &volSync);
940                 if (code == RXGEN_OPCODE) {
941                     tcp->srvr->server->flags |= SNO_INLINEBULK;
942                     inlinebulk = 0;
943                     code =
944                         RXAFS_BulkStatus(tcp->id, &fidParm, &statParm,
945                                          &cbParm, &volSync);
946                 } else {
947                     inlinebulk = 1;
948                     if (!code && ((&statsp[0])->errorCode)) {
949                         /*
950                          * If this is an error needing retry, do so.
951                          * Retryable errors are all whole-volume or
952                          * whole-server.
953                          */
954                         if (afs_Analyze(tcp, (&statsp[0])->errorCode,
955                                         &adp->f.fid, areqp,
956                                         AFS_STATS_FS_RPCIDX_BULKSTATUS,
957                                         SHARED_LOCK, NULL) != 0)
958                             goto retryonce;
959                     }
960                 }
961             } else {
962                 inlinebulk = 0;
963                 code =
964                     RXAFS_BulkStatus(tcp->id, &fidParm, &statParm, &cbParm,
965                                      &volSync);
966             }
967             RX_AFS_GLOCK();
968             XSTATS_END_TIME;
969         } else
970             code = -1;
971     } while (afs_Analyze
972              (tcp, code, &adp->f.fid, areqp, AFS_STATS_FS_RPCIDX_BULKSTATUS,
973               SHARED_LOCK, NULL));
974
975     /* now, if we didnt get the info, bail out. */
976     if (code)
977         goto done;
978
979     /* we need vol flags to create the entries properly */
980     dotdot.Fid.Volume = 0;
981     volp = afs_GetVolume(&adp->f.fid, areqp, READ_LOCK);
982     if (volp) {
983         volStates = volp->states;
984         if (volp->dotdot.Fid.Volume != 0)
985             dotdot = volp->dotdot;
986     } else
987         volStates = 0;
988
989     /* find the place to merge the info into  We do this by skipping
990      * nskip entries in the LRU queue.  The more we skip, the more
991      * we preserve, since the head of the VLRU queue is the most recently
992      * referenced file.
993      */
994   reskip:
995     nskip = afs_cacheStats / 2; /* preserved fraction of the cache */
996     ObtainReadLock(&afs_xvcache);
997 #ifdef AFS_DARWIN80_ENV
998  reskip2:
999 #endif
1000     if (QEmpty(&VLRU)) {
1001         /* actually a serious error, probably should panic. Probably will 
1002          * panic soon, oh well. */
1003         ReleaseReadLock(&afs_xvcache);
1004         afs_warnuser("afs_DoBulkStat: VLRU empty!");
1005         goto done;
1006     }
1007     if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1008         refpanic("Bulkstat VLRU inconsistent");
1009     }
1010     for (tq = VLRU.next; tq != &VLRU; tq = QNext(tq)) {
1011         if (--nskip <= 0) {
1012 #ifdef AFS_DARWIN80_ENV
1013             if ((!(QTOV(tq)->f.states & CDeadVnode)&&!(QTOV(tq)->f.states & CVInit)))
1014 #endif
1015                 break;
1016         }
1017         if (QNext(QPrev(tq)) != tq) {
1018             BStvc = QTOV(tq);
1019             refpanic("BulkStat VLRU inconsistent");
1020         }
1021     }
1022     if (tq != &VLRU)
1023         lruvcp = QTOV(tq);
1024     else
1025         lruvcp = QTOV(VLRU.next);
1026
1027     /* now we have to hold this entry, so that it does not get moved
1028      * into the free list while we're running.  It could still get
1029      * moved within the lru queue, but hopefully that will be rare; it
1030      * doesn't hurt nearly as much.
1031      */
1032     retry = 0;
1033 #ifdef AFS_DARWIN80_ENV
1034     if (((lruvcp->f.states & CDeadVnode)||(lruvcp->f.states & CVInit))) {
1035         if (npasses == 0) {
1036             nskip = 1;
1037             npasses++;
1038             goto reskip2;
1039         } else
1040             panic("Can't find non-dead vnode in VLRU\n");
1041     }
1042     lruvp = AFSTOV(lruvcp);
1043     if (vnode_get(lruvp))       /* this bumps ref count */
1044         retry = 1;
1045     else if (vnode_ref(lruvp)) {
1046         AFS_GUNLOCK();
1047         /* AFSTOV(lruvcp) may be NULL */
1048         vnode_put(lruvp);
1049         AFS_GLOCK();
1050         retry = 1;
1051     }
1052 #else
1053     osi_vnhold(lruvcp, &retry);
1054 #endif
1055     ReleaseReadLock(&afs_xvcache);      /* could be read lock */
1056     if (retry)
1057         goto reskip;
1058
1059     /* otherwise, merge in the info.  We have to be quite careful here,
1060      * since we need to ensure that we don't merge old info over newer
1061      * stuff in a stat cache entry.  We're very conservative here: we don't
1062      * do the merge at all unless we ourselves create the stat cache
1063      * entry.  That's pretty safe, and should work pretty well, since we
1064      * typically expect to do the stat cache creation ourselves.
1065      *
1066      * We also have to take into account racing token revocations.
1067      */
1068     for (i = 0; i < fidIndex; i++) {
1069         if ((&statsp[i])->errorCode)
1070             continue;
1071         afid.Cell = adp->f.fid.Cell;
1072         afid.Fid.Volume = adp->f.fid.Fid.Volume;
1073         afid.Fid.Vnode = fidsp[i].Vnode;
1074         afid.Fid.Unique = fidsp[i].Unique;
1075         do {
1076             retry = 0;
1077             ObtainReadLock(&afs_xvcache);
1078             tvcp = afs_FindVCache(&afid, &retry, FIND_CDEAD /* !stats&!lru */);
1079             ReleaseReadLock(&afs_xvcache);
1080         } while (tvcp && retry);
1081
1082         /* The entry may no longer exist */
1083         if (tvcp == NULL) {
1084             continue;
1085         }
1086
1087         /* now we have the entry held, but we need to fill it in */
1088         ObtainWriteLock(&tvcp->lock, 131);
1089
1090         /* if CBulkFetching is not set, or if the file size no longer
1091          * matches the value we placed there when we set the CBulkFetching
1092          * flag, then someone else has done something with this node,
1093          * and we may not have the latest status information for this
1094          * file.  Leave the entry alone.
1095          */
1096         if (!(tvcp->f.states & CBulkFetching) || (tvcp->f.m.Length != statSeqNo)) {
1097             flagIndex++;
1098             ReleaseWriteLock(&tvcp->lock);
1099             afs_PutVCache(tvcp);
1100             continue;
1101         }
1102
1103         /* now copy ".." entry back out of volume structure, if necessary */
1104         if (tvcp->mvstat == 2 && (dotdot.Fid.Volume != 0)) {
1105             if (!tvcp->mvid)
1106                 tvcp->mvid = (struct VenusFid *)
1107                     osi_AllocSmallSpace(sizeof(struct VenusFid));
1108             *tvcp->mvid = dotdot;
1109         }
1110
1111 #ifdef AFS_DARWIN80_ENV
1112         if (((lruvcp->f.states & CDeadVnode)||(lruvcp->f.states & CVInit)))
1113             panic("vlru control point went dead\n");
1114 #endif
1115
1116         ObtainWriteLock(&afs_xvcache, 132);
1117         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1118             refpanic("Bulkstat VLRU inconsistent2");
1119         }
1120         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq)
1121             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq)) {
1122             refpanic("Bulkstat VLRU inconsistent4");
1123         }
1124         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq)
1125             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq)) {
1126             refpanic("Bulkstat VLRU inconsistent5");
1127         }
1128
1129         if (tvcp != lruvcp) {   /* if they are == don't move it, don't corrupt vlru */
1130             QRemove(&tvcp->vlruq);
1131             QAdd(&lruvcp->vlruq, &tvcp->vlruq);
1132         }
1133
1134         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1135             refpanic("Bulkstat VLRU inconsistent3");
1136         }
1137         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq)
1138             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq)) {
1139             refpanic("Bulkstat VLRU inconsistent5");
1140         }
1141         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq)
1142             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq)) {
1143             refpanic("Bulkstat VLRU inconsistent6");
1144         }
1145         ReleaseWriteLock(&afs_xvcache);
1146
1147         ObtainWriteLock(&afs_xcbhash, 494);
1148
1149         /* We need to check the flags again. We may have missed
1150          * something while we were waiting for a lock.
1151          */
1152         if (!(tvcp->f.states & CBulkFetching) || (tvcp->f.m.Length != statSeqNo)) {
1153             flagIndex++;
1154             ReleaseWriteLock(&tvcp->lock);
1155             ReleaseWriteLock(&afs_xcbhash);
1156             afs_PutVCache(tvcp);
1157             continue;
1158         }
1159
1160         /* now merge in the resulting status back into the vnode.
1161          * We only do this if the entry looks clear.
1162          */
1163         afs_ProcessFS(tvcp, &statsp[i], areqp);
1164 #if defined(AFS_LINUX22_ENV)
1165         afs_fill_inode(AFSTOV(tvcp), NULL);     /* reset inode operations */
1166 #endif
1167
1168         /* do some accounting for bulk stats: mark this entry as
1169          * loaded, so we can tell if we use it before it gets
1170          * recycled.
1171          */
1172         tvcp->f.states |= CBulkStat;
1173         tvcp->f.states &= ~CBulkFetching;
1174         flagIndex++;
1175         afs_bulkStatsDone++;
1176
1177         /* merge in vol info */
1178         if (volStates & VRO)
1179             tvcp->f.states |= CRO;
1180         if (volStates & VBackup)
1181             tvcp->f.states |= CBackup;
1182         if (volStates & VForeign)
1183             tvcp->f.states |= CForeign;
1184
1185         /* merge in the callback info */
1186         tvcp->f.states |= CTruth;
1187
1188         /* get ptr to the callback we are interested in */
1189         tcbp = cbsp + i;
1190
1191         if (tcbp->ExpirationTime != 0) {
1192             tvcp->cbExpires = tcbp->ExpirationTime + startTime;
1193             tvcp->callback = hostp;
1194             tvcp->f.states |= CStatd;
1195             afs_QueueCallback(tvcp, CBHash(tcbp->ExpirationTime), volp);
1196         } else if (tvcp->f.states & CRO) {
1197             /* ordinary callback on a read-only volume -- AFS 3.2 style */
1198             tvcp->cbExpires = 3600 + startTime;
1199             tvcp->callback = hostp;
1200             tvcp->f.states |= CStatd;
1201             afs_QueueCallback(tvcp, CBHash(3600), volp);
1202         } else {
1203             tvcp->callback = 0;
1204             tvcp->f.states &= ~(CStatd | CUnique);
1205             afs_DequeueCallback(tvcp);
1206             if ((tvcp->f.states & CForeign) || (vType(tvcp) == VDIR))
1207                 osi_dnlc_purgedp(tvcp); /* if it (could be) a directory */
1208         }
1209 #ifdef AFS_DARWIN80_ENV
1210         /* reclaim->FlushVCache will need xcbhash */
1211         if (((tvcp->f.states & CDeadVnode)||(tvcp->f.states & CVInit))) {
1212             ReleaseWriteLock(&afs_xcbhash);
1213             /* passing in a parent hangs getting the vnode lock */
1214             code = afs_darwin_finalizevnode(tvcp, NULL, NULL, 0, 1);
1215             if (code) {
1216                 /* It's gonna get recycled - shouldn't happen */
1217                 tvcp->callback = 0;
1218                 tvcp->f.states &= ~(CStatd | CUnique);
1219                 afs_DequeueCallback(tvcp);
1220                 if ((tvcp->f.states & CForeign) || (vType(tvcp) == VDIR))
1221                     osi_dnlc_purgedp(tvcp); /* if it (could be) a directory */
1222             } else {
1223                 /* re-acquire the usecount that finalizevnode disposed of */
1224                 vnode_ref(AFSTOV(tvcp));
1225             }
1226         } else
1227 #endif
1228         ReleaseWriteLock(&afs_xcbhash);
1229
1230         ReleaseWriteLock(&tvcp->lock);
1231         /* finally, we're done with the entry */
1232         afs_PutVCache(tvcp);
1233     }                           /* for all files we got back */
1234
1235     /* finally return the pointer into the LRU queue */
1236 #ifdef AFS_DARWIN80_ENV
1237     if (((lruvcp->f.states & CDeadVnode)||(lruvcp->f.states & CVInit)))
1238         panic("vlru control point went dead before put\n");
1239     AFS_GUNLOCK();
1240     vnode_put(lruvp);
1241     vnode_rele(lruvp);
1242     AFS_GLOCK();
1243 #else
1244     afs_PutVCache(lruvcp);
1245 #endif
1246
1247   done:
1248     /* Be sure to turn off the CBulkFetching flags */
1249     for (i = flagIndex; i < fidIndex; i++) {
1250         afid.Cell = adp->f.fid.Cell;
1251         afid.Fid.Volume = adp->f.fid.Fid.Volume;
1252         afid.Fid.Vnode = fidsp[i].Vnode;
1253         afid.Fid.Unique = fidsp[i].Unique;
1254         do {
1255             retry = 0;
1256             ObtainReadLock(&afs_xvcache);
1257             tvcp = afs_FindVCache(&afid, &retry, FIND_CDEAD /* !stats&!lru */);
1258             ReleaseReadLock(&afs_xvcache);
1259         } while (tvcp && retry);
1260         if (tvcp != NULL) {
1261             if ((tvcp->f.states & CBulkFetching)
1262                 && (tvcp->f.m.Length == statSeqNo)) {
1263                 tvcp->f.states &= ~CBulkFetching;
1264             }
1265             afs_PutVCache(tvcp);
1266         }
1267     }
1268     if (volp)
1269         afs_PutVolume(volp, READ_LOCK);
1270
1271     /* If we did the InlineBulk RPC pull out the return code */
1272     if (inlinebulk && code == 0) {
1273         if ((&statsp[0])->errorCode) {
1274             afs_Analyze(tcp, (&statsp[0])->errorCode, &adp->f.fid, areqp,
1275                         AFS_STATS_FS_RPCIDX_BULKSTATUS, SHARED_LOCK, NULL);
1276             code = (&statsp[0])->errorCode;
1277         }
1278     } else {
1279         code = 0;
1280     }
1281   done2:
1282     osi_FreeLargeSpace((char *)fidsp);
1283     osi_Free((char *)statsp, AFSCBMAX * sizeof(AFSFetchStatus));
1284     osi_Free((char *)cbsp, AFSCBMAX * sizeof(AFSCallBack));
1285     return code;
1286 }
1287
1288 /* was: (AFS_DEC_ENV) || defined(AFS_OSF30_ENV) || defined(AFS_NCR_ENV) */
1289 static int AFSDOBULK = 1;
1290
1291 int
1292 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
1293 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, struct pathname *pnp, int flags, struct vnode *rdir, afs_ucred_t *acred)
1294 #elif defined(UKERNEL)
1295 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, afs_ucred_t *acred, int flags)
1296 #else
1297 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, afs_ucred_t *acred)
1298 #endif
1299 {
1300     struct vrequest treq;
1301     char *tname = NULL;
1302     register struct vcache *tvc = 0;
1303     register afs_int32 code;
1304     register afs_int32 bulkcode = 0;
1305     int pass = 0, hit = 0;
1306     int force_eval = afs_fakestat_enable ? 0 : 1;
1307     long dirCookie;
1308     extern afs_int32 afs_mariner;       /*Writing activity to log? */
1309     afs_hyper_t versionNo;
1310     int no_read_access = 0;
1311     struct sysname_info sysState;       /* used only for @sys checking */
1312     int dynrootRetry = 1;
1313     struct afs_fakestat_state fakestate;
1314     int tryEvalOnly = 0;
1315     OSI_VC_CONVERT(adp);
1316
1317     AFS_STATCNT(afs_lookup);
1318     afs_InitFakeStat(&fakestate);
1319
1320     AFS_DISCON_LOCK();
1321     
1322     if ((code = afs_InitReq(&treq, acred)))
1323         goto done;
1324
1325     if (afs_fakestat_enable && adp->mvstat == 1) {
1326        if (strcmp(aname, ".directory") == 0)
1327            tryEvalOnly = 1;
1328     }
1329
1330 #if defined(AFS_DARWIN_ENV)
1331     /* Workaround for MacOSX Finder, which tries to look for
1332      * .DS_Store and Contents under every directory.
1333      */
1334     if (afs_fakestat_enable && adp->mvstat == 1) {
1335         if (strcmp(aname, ".DS_Store") == 0)
1336             tryEvalOnly = 1;
1337         if (strcmp(aname, "Contents") == 0)
1338             tryEvalOnly = 1;
1339     }
1340     if (afs_fakestat_enable && adp->mvstat == 2) {
1341         if (strncmp(aname, "._", 2) == 0)
1342             tryEvalOnly = 1;
1343     }
1344 #endif
1345
1346     if (tryEvalOnly)
1347         code = afs_TryEvalFakeStat(&adp, &fakestate, &treq);
1348     else
1349         code = afs_EvalFakeStat(&adp, &fakestate, &treq);
1350
1351     /*printf("Code is %d\n", code);*/
1352     
1353     if (tryEvalOnly && adp->mvstat == 1)
1354         code = ENOENT;
1355     if (code)
1356         goto done;
1357
1358     *avcp = NULL;               /* Since some callers don't initialize it */
1359
1360     /* come back to here if we encounter a non-existent object in a read-only
1361      * volume's directory */
1362
1363   redo:
1364     *avcp = NULL;               /* Since some callers don't initialize it */
1365     bulkcode = 0;
1366
1367     if (!(adp->f.states & CStatd) && !afs_InReadDir(adp)) {
1368         if ((code = afs_VerifyVCache2(adp, &treq))) {
1369             goto done;
1370         }
1371     } else
1372         code = 0;
1373
1374     /* watch for ".." in a volume root */
1375     if (adp->mvstat == 2 && aname[0] == '.' && aname[1] == '.' && !aname[2]) {
1376         /* looking up ".." in root via special hacks */
1377         if (adp->mvid == (struct VenusFid *)0 || adp->mvid->Fid.Volume == 0) {
1378             code = ENODEV;
1379             goto done;
1380         }
1381         /* otherwise we have the fid here, so we use it */
1382         /*printf("Getting vcache\n");*/
1383         tvc = afs_GetVCache(adp->mvid, &treq, NULL, NULL);
1384         afs_Trace3(afs_iclSetp, CM_TRACE_GETVCDOTDOT, ICL_TYPE_FID, adp->mvid,
1385                    ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, code);
1386         *avcp = tvc;
1387         code = (tvc ? 0 : ENOENT);
1388         hit = 1;
1389         if (tvc && !VREFCOUNT_GT(tvc, 0)) {
1390             osi_Panic("TT1");
1391         }
1392         if (code) {
1393             /*printf("LOOKUP GETVCDOTDOT -> %d\n", code); */
1394         }
1395         goto done;
1396     }
1397
1398     /* now check the access */
1399     if (treq.uid != adp->last_looker) {
1400         if (!afs_AccessOK(adp, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1401             *avcp = NULL;
1402             code = EACCES;
1403             goto done;
1404         } else
1405             adp->last_looker = treq.uid;
1406     }
1407
1408     /* Check for read access as well.  We need read access in order to
1409      * stat files, but not to stat subdirectories. */
1410     if (!afs_AccessOK(adp, PRSFS_READ, &treq, CHECK_MODE_BITS))
1411         no_read_access = 1;
1412
1413     /* special case lookup of ".".  Can we check for it sooner in this code,
1414      * for instance, way up before "redo:" ??
1415      * I'm not fiddling with the LRUQ here, either, perhaps I should, or else 
1416      * invent a lightweight version of GetVCache.
1417      */
1418     if (aname[0] == '.' && !aname[1]) { /* special case */
1419         ObtainReadLock(&afs_xvcache);
1420         osi_vnhold(adp, 0);
1421         ReleaseReadLock(&afs_xvcache);
1422 #ifdef AFS_DARWIN80_ENV
1423         vnode_get(AFSTOV(adp));
1424 #endif
1425         code = 0;
1426         *avcp = tvc = adp;
1427         hit = 1;
1428         if (adp && !VREFCOUNT_GT(adp, 0)) {
1429             osi_Panic("TT2");
1430         }
1431         goto done;
1432     }
1433
1434     /*
1435      * Special case lookup of ".." in the dynamic mount directory.
1436      * The parent of this directory is _always_ the AFS root volume.
1437      */
1438     if (afs_IsDynrootMount(adp) &&
1439         aname[0] == '.' && aname[1] == '.' && !aname[2]) {
1440
1441         ObtainReadLock(&afs_xvcache);
1442         osi_vnhold(afs_globalVp, 0);
1443         ReleaseReadLock(&afs_xvcache);
1444 #ifdef AFS_DARWIN80_ENV
1445         vnode_get(AFSTOV(afs_globalVp));
1446 #endif
1447         code = 0;
1448         *avcp = tvc = afs_globalVp;
1449         hit = 1;
1450         goto done;
1451     }
1452
1453     /*
1454      * Special case lookups in the dynamic mount directory.
1455      * The names here take the form cell:volume, similar to a mount point.
1456      * EvalMountData parses that and returns a cell and volume ID, which
1457      * we use to construct the appropriate dynroot Fid.
1458      */
1459     if (afs_IsDynrootMount(adp)) {
1460         struct VenusFid tfid;
1461         afs_uint32 cellidx, volid, vnoid, uniq;
1462
1463         code = EvalMountData('%', aname, 0, 0, NULL, &treq, &cellidx, &volid, &vnoid, &uniq);
1464         if (code)
1465             goto done;
1466         /* If a vnode was returned, it's not a real mount point */
1467         if (vnoid > 1) {
1468             struct cell *tcell = afs_GetCellByIndex(cellidx, READ_LOCK);
1469             tfid.Cell = tcell->cellNum;
1470             afs_PutCell(tcell, READ_LOCK);
1471             tfid.Fid.Vnode = vnoid;
1472             tfid.Fid.Volume = volid;
1473             tfid.Fid.Unique = uniq;
1474         } else {
1475             afs_GetDynrootMountFid(&tfid);
1476             tfid.Fid.Vnode = VNUM_FROM_TYPEID(VN_TYPE_MOUNT, cellidx << 2);
1477             tfid.Fid.Unique = volid;
1478         }
1479         *avcp = tvc = afs_GetVCache(&tfid, &treq, NULL, NULL);
1480         code = (tvc ? 0 : ENOENT);
1481         hit = 1;
1482         goto done;
1483     }
1484
1485 #ifdef AFS_LINUX26_ENV
1486     /*
1487      * Special case of the dynamic mount volume in a static root.
1488      * This is really unfortunate, but we need this for the translator.
1489      */
1490     if (adp == afs_globalVp && !afs_GetDynrootEnable() &&
1491         !strcmp(aname, AFS_DYNROOT_MOUNTNAME)) {
1492         struct VenusFid tfid;
1493
1494         afs_GetDynrootMountFid(&tfid);
1495         *avcp = tvc = afs_GetVCache(&tfid, &treq, NULL, NULL);
1496         code = 0;
1497         hit = 1;
1498         goto done;
1499     }
1500 #endif
1501
1502     Check_AtSys(adp, aname, &sysState, &treq);
1503     tname = sysState.name;
1504
1505     /* 1st Check_AtSys and lookup by tname is required here, for now,
1506      * because the dnlc is *not* told to remove entries for the parent
1507      * dir of file/dir op that afs_LocalHero likes, but dnlc is informed
1508      * if the cached entry for the parent dir is invalidated for a
1509      * non-local change.
1510      * Otherwise, we'd be able to do a dnlc lookup on an entry ending
1511      * w/@sys and know the dnlc was consistent with reality. */
1512     tvc = osi_dnlc_lookup(adp, tname, WRITE_LOCK);
1513     *avcp = tvc;                /* maybe wasn't initialized, but it is now */
1514     if (tvc) {
1515         if (no_read_access && vType(tvc) != VDIR && vType(tvc) != VLNK) {
1516             /* need read access on dir to stat non-directory / non-link */
1517             afs_PutVCache(tvc);
1518             *avcp = NULL;
1519             code = EACCES;
1520             goto done;
1521         }
1522 #ifdef AFS_LINUX22_ENV
1523         if (tvc->mvstat == 2) { /* we don't trust the dnlc for root vcaches */
1524             AFS_RELE(AFSTOV(tvc));
1525             *avcp = 0;
1526         } else {
1527             code = 0;
1528             hit = 1;
1529             goto done;
1530         }
1531 #else /* non - LINUX */
1532         code = 0;
1533         hit = 1;
1534         goto done;
1535 #endif /* linux22 */
1536     }
1537
1538     {                           /* sub-block just to reduce stack usage */
1539         register struct dcache *tdc;
1540         afs_size_t dirOffset, dirLen;
1541         struct VenusFid tfid;
1542
1543         /* now we have to lookup the next fid */
1544         if (afs_InReadDir(adp))
1545             tdc = adp->dcreaddir;
1546         else
1547             tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq,
1548                                 &dirOffset, &dirLen, 1);
1549         if (!tdc) {
1550             *avcp = NULL;       /* redundant, but harmless */
1551             code = EIO;
1552             goto done;
1553         }
1554
1555         /* now we will just call dir package with appropriate inode.
1556          * Dirs are always fetched in their entirety for now */
1557         ObtainReadLock(&adp->lock);
1558         ObtainReadLock(&tdc->lock);
1559
1560         /*
1561          * Make sure that the data in the cache is current. There are two
1562          * cases we need to worry about:
1563          * 1. The cache data is being fetched by another process.
1564          * 2. The cache data is no longer valid
1565          *
1566          * If a readdir is in progress _in this thread_, it has a shared
1567          * lock on the vcache and has obtained current data, so we just
1568          * use that.  This eliminates several possible deadlocks.  
1569          */
1570         if (!afs_InReadDir(adp)) {
1571             while ((adp->f.states & CStatd)
1572                    && (tdc->dflags & DFFetching)
1573                    && hsame(adp->f.m.DataVersion, tdc->f.versionNo)) {
1574                 ReleaseReadLock(&tdc->lock);
1575                 ReleaseReadLock(&adp->lock);
1576                 afs_osi_Sleep(&tdc->validPos);
1577                 ObtainReadLock(&adp->lock);
1578                 ObtainReadLock(&tdc->lock);
1579             }
1580             if (!(adp->f.states & CStatd)
1581                 || !hsame(adp->f.m.DataVersion, tdc->f.versionNo)) {
1582                 ReleaseReadLock(&tdc->lock);
1583                 ReleaseReadLock(&adp->lock);
1584                 afs_PutDCache(tdc);
1585                 if (tname && tname != aname)
1586                     osi_FreeLargeSpace(tname);
1587                 goto redo;
1588             }
1589         }
1590
1591         /* Save the version number for when we call osi_dnlc_enter */
1592         hset(versionNo, tdc->f.versionNo);
1593
1594         /*
1595          * check for, and handle "@sys" if it's there.  We should be able
1596          * to avoid the alloc and the strcpy with a little work, but it's
1597          * not pressing.  If there aren't any remote users (ie, via the 
1598          * NFS translator), we have a slightly easier job.
1599          * the faster way to do this is to check for *aname == '@' and if 
1600          * it's there, check for @sys, otherwise, assume there's no @sys 
1601          * then, if the lookup fails, check for .*@sys...
1602          */
1603         /* above now implemented by Check_AtSys and Next_AtSys */
1604
1605         /* lookup the name in the appropriate dir, and return a cache entry
1606          * on the resulting fid */
1607         code =
1608             afs_dir_LookupOffset(tdc, sysState.name, &tfid.Fid,
1609                                  &dirCookie);
1610
1611         /* If the first lookup doesn't succeed, maybe it's got @sys in the name */
1612         while (code == ENOENT && Next_AtSys(adp, &treq, &sysState))
1613             code =
1614                 afs_dir_LookupOffset(tdc, sysState.name, &tfid.Fid,
1615                                      &dirCookie);
1616         tname = sysState.name;
1617
1618         ReleaseReadLock(&tdc->lock);
1619         if (!afs_InReadDir(adp))
1620             afs_PutDCache(tdc);
1621         if (code == ENOENT && afs_IsDynroot(adp) && dynrootRetry && !tryEvalOnly) {
1622             ReleaseReadLock(&adp->lock);
1623             dynrootRetry = 0;
1624             if (tname[0] == '.')
1625                 afs_LookupAFSDB(tname + 1);
1626             else
1627                 afs_LookupAFSDB(tname);
1628             if (tname && tname != aname)
1629                 osi_FreeLargeSpace(tname);
1630             goto redo;
1631         } else {
1632             ReleaseReadLock(&adp->lock);
1633         }
1634
1635         /* new fid has same cell and volume */
1636         tfid.Cell = adp->f.fid.Cell;
1637         tfid.Fid.Volume = adp->f.fid.Fid.Volume;
1638         afs_Trace4(afs_iclSetp, CM_TRACE_LOOKUP, ICL_TYPE_POINTER, adp,
1639                    ICL_TYPE_STRING, tname, ICL_TYPE_FID, &tfid,
1640                    ICL_TYPE_INT32, code);
1641
1642         if (code) {
1643             if (code != ENOENT) {
1644                 /*printf("LOOKUP dirLookupOff -> %d\n", code);*/
1645             }
1646             goto done;
1647         }
1648
1649         /* prefetch some entries, if the dir is currently open.  The variable
1650          * dirCookie tells us where to start prefetching from.
1651          */
1652         if (!AFS_IS_DISCONNECTED && 
1653             AFSDOBULK && adp->opens > 0 && !(adp->f.states & CForeign)
1654             && !afs_IsDynroot(adp) && !afs_InReadDir(adp)) {
1655             afs_int32 retry;
1656             /* if the entry is not in the cache, or is in the cache,
1657              * but hasn't been statd, then do a bulk stat operation.
1658              */
1659             do {
1660                 retry = 0;
1661                 ObtainReadLock(&afs_xvcache);
1662                 tvc = afs_FindVCache(&tfid, &retry, 0 /* !stats,!lru */ );
1663                 ReleaseReadLock(&afs_xvcache);
1664             } while (tvc && retry);
1665
1666             if (!tvc || !(tvc->f.states & CStatd))
1667                 bulkcode = afs_DoBulkStat(adp, dirCookie, &treq);
1668             else
1669                 bulkcode = 0;
1670
1671             /* if the vcache isn't usable, release it */
1672             if (tvc && !(tvc->f.states & CStatd)) {
1673                 afs_PutVCache(tvc);
1674                 tvc = NULL;
1675             }
1676         } else {
1677             tvc = NULL;
1678             bulkcode = 0;
1679         }
1680
1681         /* now get the status info, if we don't already have it */
1682         /* This is kind of weird, but we might wind up accidentally calling
1683          * RXAFS_Lookup because we happened upon a file which legitimately
1684          * has a 0 uniquifier. That is the result of allowing unique to wrap
1685          * to 0. This was fixed in AFS 3.4. For CForeign, Unique == 0 means that
1686          * the file has not yet been looked up.
1687          */
1688         if (!tvc) {
1689             afs_int32 cached = 0;
1690             if (!tfid.Fid.Unique && (adp->f.states & CForeign)) {
1691                 tvc = afs_LookupVCache(&tfid, &treq, &cached, adp, tname);
1692             }
1693             if (!tvc && !bulkcode) {    /* lookup failed or wasn't called */
1694                 tvc = afs_GetVCache(&tfid, &treq, &cached, NULL);
1695             }
1696         }                       /* if !tvc */
1697     }                           /* sub-block just to reduce stack usage */
1698
1699     if (tvc) {
1700         if (adp->f.states & CForeign)
1701             tvc->f.states |= CForeign;
1702         tvc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1703         tvc->f.parent.unique = adp->f.fid.Fid.Unique;
1704         tvc->f.states &= ~CBulkStat;
1705
1706         if (afs_fakestat_enable == 2 && tvc->mvstat == 1) {
1707             ObtainSharedLock(&tvc->lock, 680);
1708             if (!tvc->linkData) {
1709                 UpgradeSToWLock(&tvc->lock, 681);
1710                 code = afs_HandleLink(tvc, &treq);
1711                 ConvertWToRLock(&tvc->lock);
1712             } else {
1713                 ConvertSToRLock(&tvc->lock);
1714                 code = 0;
1715             }
1716             if (!code && !afs_strchr(tvc->linkData, ':'))
1717                 force_eval = 1;
1718             ReleaseReadLock(&tvc->lock);
1719         }
1720         if (tvc->mvstat == 1 && (tvc->f.states & CMValid) && tvc->mvid != NULL)
1721           force_eval = 1; /* This is now almost for free, get it correct */
1722
1723 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1724         if (!(flags & AFS_LOOKUP_NOEVAL))
1725             /* don't eval mount points */
1726 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
1727             if (tvc->mvstat == 1 && force_eval) {
1728                 /* a mt point, possibly unevaluated */
1729                 struct volume *tvolp;
1730
1731                 ObtainWriteLock(&tvc->lock, 133);
1732                 code = EvalMountPoint(tvc, adp, &tvolp, &treq);
1733                 ReleaseWriteLock(&tvc->lock);
1734
1735                 if (code) {
1736                     afs_PutVCache(tvc);
1737                     if (tvolp)
1738                         afs_PutVolume(tvolp, WRITE_LOCK);
1739                     goto done;
1740                 }
1741
1742                 /* next, we want to continue using the target of the mt point */
1743                 if (tvc->mvid && (tvc->f.states & CMValid)) {
1744                     struct vcache *uvc;
1745                     /* now lookup target, to set .. pointer */
1746                     afs_Trace2(afs_iclSetp, CM_TRACE_LOOKUP1,
1747                                ICL_TYPE_POINTER, tvc, ICL_TYPE_FID,
1748                                &tvc->f.fid);
1749                     uvc = tvc;  /* remember for later */
1750
1751                     if (tvolp && (tvolp->states & VForeign)) {
1752                         /* XXXX tvolp has ref cnt on but not locked! XXX */
1753                         tvc =
1754                             afs_GetRootVCache(tvc->mvid, &treq, NULL, tvolp);
1755                     } else {
1756                         tvc = afs_GetVCache(tvc->mvid, &treq, NULL, NULL);
1757                     }
1758                     afs_PutVCache(uvc); /* we're done with it */
1759
1760                     if (!tvc) {
1761                         code = ENOENT;
1762                         if (tvolp) {
1763                             afs_PutVolume(tvolp, WRITE_LOCK);
1764                         }
1765                         goto done;
1766                     }
1767
1768                     /* now, if we came via a new mt pt (say because of a new
1769                      * release of a R/O volume), we must reevaluate the ..
1770                      * ptr to point back to the appropriate place */
1771                     if (tvolp) {
1772                         ObtainWriteLock(&tvc->lock, 134);
1773                         if (tvc->mvid == NULL) {
1774                             tvc->mvid = (struct VenusFid *)
1775                                 osi_AllocSmallSpace(sizeof(struct VenusFid));
1776                         }
1777                         /* setup backpointer */
1778                         *tvc->mvid = tvolp->dotdot;
1779                         ReleaseWriteLock(&tvc->lock);
1780                         afs_PutVolume(tvolp, WRITE_LOCK);
1781                     }
1782                 } else {
1783                     afs_PutVCache(tvc);
1784                     code = ENOENT;
1785                     if (tvolp)
1786                         afs_PutVolume(tvolp, WRITE_LOCK);
1787                     goto done;
1788                 }
1789             }
1790         *avcp = tvc;
1791         if (tvc && !VREFCOUNT_GT(tvc, 0)) {
1792             osi_Panic("TT3");
1793         }
1794         code = 0;
1795     } else {
1796         /* if we get here, we found something in a directory that couldn't
1797          * be located (a Multics "connection failure").  If the volume is
1798          * read-only, we try flushing this entry from the cache and trying
1799          * again. */
1800         if (!AFS_IS_DISCONNECTED) {
1801             if (pass == 0) {
1802                 struct volume *tv;
1803                 tv = afs_GetVolume(&adp->f.fid, &treq, READ_LOCK);
1804                 if (tv) {
1805                     if (tv->states & VRO) {
1806                         pass = 1;       /* try this *once* */
1807                         ObtainWriteLock(&afs_xcbhash, 495);
1808                         afs_DequeueCallback(adp);
1809                         /* re-stat to get later version */
1810                         adp->f.states &= ~CStatd;
1811                         ReleaseWriteLock(&afs_xcbhash);
1812                         osi_dnlc_purgedp(adp);
1813                         afs_PutVolume(tv, READ_LOCK);
1814                         goto redo;
1815                     }
1816                     afs_PutVolume(tv, READ_LOCK);
1817                 }
1818             }
1819             code = ENOENT;
1820         } else {
1821             code = ENETDOWN;
1822         }
1823     }
1824
1825   done:
1826     /* put the network buffer back, if need be */
1827     if (tname != aname && tname)
1828         osi_FreeLargeSpace(tname);
1829     if (code == 0) {
1830
1831         if (afs_mariner)
1832             afs_AddMarinerName(aname, tvc);
1833
1834 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1835         if (!(flags & AFS_LOOKUP_NOEVAL)) {
1836             /* Here we don't enter the name into the DNLC because we want the
1837              * evaluated mount dir to be there (the vcache for the mounted
1838              * volume) rather than the vc of the mount point itself.  We can
1839              * still find the mount point's vc in the vcache by its fid. */
1840 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
1841             if (!hit && force_eval) {
1842                 osi_dnlc_enter(adp, aname, tvc, &versionNo);
1843             } else {
1844 #ifdef AFS_LINUX20_ENV
1845                 /* So Linux inode cache is up to date. */
1846                 code = afs_VerifyVCache(tvc, &treq);
1847 #else
1848                 afs_PutFakeStat(&fakestate);
1849                 AFS_DISCON_UNLOCK();
1850                 return 0;       /* can't have been any errors if hit and !code */
1851 #endif
1852             }
1853 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1854         }
1855 #endif
1856     }
1857     if (bulkcode)
1858         code = bulkcode;
1859
1860     code = afs_CheckCode(code, &treq, 19);
1861     if (code) {
1862         /* If there is an error, make sure *avcp is null.
1863          * Alphas panic otherwise - defect 10719.
1864          */
1865         *avcp = NULL;
1866     }
1867
1868     afs_PutFakeStat(&fakestate);
1869     AFS_DISCON_UNLOCK();
1870     return code;
1871 }