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