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