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