1778c4050085bb1118a76df3ebedadfc0cbfb65e
[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  * Locals:
17  * afs_strcat
18  * AFS_EQ_ATSYS (macro)
19  * afs_index
20  */
21
22 #include "../afs/param.h"       /* Should be always first */
23 #include "../afs/sysincludes.h" /* Standard vendor system headers */
24 #include "../afs/afsincludes.h" /* Afs-based standard headers */
25 #include "../afs/afs_stats.h" /* statistics */
26 #include "../afs/afs_cbqueue.h"
27 #include "../afs/nfsclient.h"
28 #include "../afs/exporter.h"
29 #include "../afs/afs_osidnlc.h"
30
31
32 /**
33  * A few definitions. This is until we have a proper header file        
34  * which has prototypes for all functions
35  */
36
37 extern struct DirEntry * afs_dir_GetBlob();
38
39 extern afs_rwlock_t afs_xvcache;
40 extern afs_rwlock_t afs_xcbhash;
41 extern struct afs_exporter *afs_nfsexporter;
42 extern char *afs_sysname;
43 extern struct afs_q VLRU;                       /*vcache LRU*/
44 #ifdef AFS_LINUX22_ENV
45 extern struct inode_operations afs_symlink_iops, afs_dir_iops;
46 #endif
47
48
49 afs_int32 afs_bulkStatsDone;
50 static int bulkStatCounter = 0; /* counter for bulk stat seq. numbers */
51
52
53 /* this would be faster if it did comparison as int32word, but would be 
54  * dependant on byte-order and alignment, and I haven't figured out
55  * what "@sys" is in binary... */
56 #define AFS_EQ_ATSYS(name) (((name)[0]=='@')&&((name)[1]=='s')&&((name)[2]=='y')&&((name)[3]=='s')&&(!(name)[4]))
57
58 char *
59 afs_strcat(s1, s2)
60         register char *s1, *s2;
61 {
62         register char *os1;
63
64         AFS_STATCNT(strcat);
65         os1 = s1;
66         while (*s1++)
67                 ;
68         --s1;
69         while (*s1++ = *s2++)
70                 ;
71         return (os1);
72 }
73
74
75 char *afs_index(a, c)
76     register char *a, c; {
77     register char tc;
78     AFS_STATCNT(afs_index);
79     while (tc = *a) {
80         if (tc == c) return a;
81         else a++;
82     }
83     return (char *) 0;
84 }
85
86 /* call under write lock, evaluate mvid field from a mt pt.
87  * avc is the vnode of the mount point object.
88  * advc is the vnode of the containing directory
89  * avolpp is where we return a pointer to the volume named by the mount pt, if success
90  * areq is the identity of the caller.
91  *
92  * NOTE: this function returns a held volume structure in *volpp if it returns 0!
93  */
94 EvalMountPoint(avc, advc, avolpp, areq)
95     register struct vcache *avc;
96     struct volume **avolpp;
97     struct vcache *advc;            /* the containing dir */
98     register struct vrequest *areq;
99 {
100     afs_int32  code;
101     struct volume *tvp = 0;
102     struct VenusFid tfid;
103     struct cell *tcell;
104     char   *cpos, *volnamep;
105     char   type, buf[128];
106     afs_int32  prefetchRO;          /* 1=>No  2=>Yes */
107     afs_int32  mtptCell, assocCell, hac=0;
108     afs_int32  samecell, roname, len;
109
110     AFS_STATCNT(EvalMountPoint);
111 #ifdef notdef
112     if (avc->mvid && (avc->states & CMValid)) return 0; /* done while racing */
113 #endif
114     *avolpp = (struct volume *)0;
115     code = afs_HandleLink(avc, areq);
116     if (code) return code;
117
118     /* Determine which cell and volume the mointpoint goes to */
119     type = avc->linkData[0];                   /* '#'=>Regular '%'=>RW */
120     cpos = afs_index(&avc->linkData[1], ':');  /* if cell name present */
121     if (cpos) {
122        volnamep = cpos+1;
123        *cpos = 0;
124        tcell = afs_GetCellByName(&avc->linkData[1], READ_LOCK);
125        *cpos =  ':';
126     } else {
127        volnamep = &avc->linkData[1];
128        tcell = afs_GetCell(avc->fid.Cell, READ_LOCK);
129     }
130     if (!tcell) return ENODEV;
131
132     mtptCell = tcell->cell;               /* The cell for the mountpoint */
133     if (tcell->lcellp) {
134        hac = 1;                           /* has associated cell */
135        assocCell = tcell->lcellp->cell;   /* The associated cell */
136     }
137     afs_PutCell(tcell, READ_LOCK);          
138
139     /* Is volume name a "<n>.backup" or "<n>.readonly" name */
140     len = strlen(volnamep);
141     roname = ((len > 9) && (strcmp(&volnamep[len - 9],".readonly") == 0)) ||
142              ((len > 7) && (strcmp(&volnamep[len - 7],".backup")   == 0));
143
144     /* When we cross mountpoint, do we stay in the same cell */
145     samecell = (avc->fid.Cell == mtptCell) || (hac && (avc->fid.Cell == assocCell));
146
147     /* Decide whether to prefetch the RO. Also means we want the RO.
148      * If this is a regular mountpoint with a RW volume name and
149      * we cross a cell boundary -or- start from a RO volume, then we will
150      * want to prefetch the RO volume when we get the RW below.
151      */
152     if ( (type == '#') && !roname && (!samecell || (avc->states & CRO)) ) {
153        prefetchRO = 2; /* Yes, prefetch the RO */
154     } else {
155        prefetchRO = 1; /* No prefetch of the RO */
156     }
157
158     /* Get the volume struct. Unless this volume name has ".readonly" or
159      * ".backup" in it, this will get the volume struct for the RW volume.
160      * The RO volume will be prefetched if requested (but not returned).
161      */
162     tvp = afs_GetVolumeByName(volnamep, mtptCell, prefetchRO, areq, WRITE_LOCK);
163
164     /* If no volume was found in this cell, try the associated linked cell */
165     if (!tvp && hac && areq->volumeError) {
166        tvp = afs_GetVolumeByName(volnamep, assocCell, prefetchRO, areq, WRITE_LOCK);
167     }
168
169     /* Still not found. If we are looking for the RO, then perhaps the RW 
170      * doesn't exist? Try adding ".readonly" to volname and look for that.
171      * Don't know why we do this. Would have still found it in above call - jpm.
172      */
173     if (!tvp && (prefetchRO == 2)) {
174        strcpy(buf, volnamep);
175        afs_strcat(buf, ".readonly");
176
177        tvp = afs_GetVolumeByName(buf, mtptCell, 1, areq, WRITE_LOCK);
178
179        /* Try the associated linked cell if failed */
180        if (!tvp && hac && areq->volumeError) {
181           tvp = afs_GetVolumeByName(buf, assocCell, 1, areq, WRITE_LOCK);
182        }
183     }
184   
185     if (!tvp) return ENOENT;       /* Couldn't find the volume */
186
187     /* Don't cross mountpoint from a BK to a BK volume */
188     if ((avc->states & CBackup) && (tvp->states & VBackup)) {
189         afs_PutVolume(tvp, WRITE_LOCK);
190         return ELOOP;
191     }
192
193     /* If we want (prefetched) the RO and it exists, then drop the
194      * RW volume and get the RO. Othewise, go with the RW.
195      */
196     if ((prefetchRO == 2) && tvp->roVol) {
197        tfid.Fid.Volume = tvp->roVol;                 /* remember RO volume */
198        tfid.Cell       = tvp->cell;
199        afs_PutVolume(tvp, WRITE_LOCK);               /* release old volume */
200        tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK); /* get the new one */
201        if (!tvp) return ENOENT;                      /* oops, can't do it */
202     }
203
204     if (avc->mvid == 0)
205         avc->mvid = (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
206     avc->mvid->Cell = tvp->cell;
207     avc->mvid->Fid.Volume = tvp->volume;
208     avc->mvid->Fid.Vnode = 1;
209     avc->mvid->Fid.Unique = 1;
210     avc->states |= CMValid;
211
212     /* Used to: if the mount point is stored within a backup volume,
213      * then we should only update the parent pointer information if
214      * there's none already set, so as to avoid updating a volume's ..
215      * info with something in an OldFiles directory.
216      *
217      * Next two lines used to be under this if:
218      *
219      * if (!(avc->states & CBackup) || tvp->dotdot.Fid.Volume == 0)
220      *
221      * Now: update mount point back pointer on every call, so that we handle
222      * multiple mount points better.  This way, when du tries to go back
223      * via chddir(".."), it will end up exactly where it started, yet
224      * cd'ing via a new path to a volume will reset the ".." pointer
225      * to the new path.
226      */
227     tvp->mtpoint = avc->fid;    /* setup back pointer to mtpoint */
228     tvp->dotdot  = advc->fid;
229
230     *avolpp = tvp;
231     return 0;
232 }
233     
234 afs_ENameOK(aname)
235     register char *aname; {
236     register char tc;
237     register int tlen;
238
239     AFS_STATCNT(ENameOK);
240     tlen = strlen(aname);
241     if (tlen >= 4 && strcmp(aname+tlen-4, "@sys") == 0) return 0;
242     return 1;
243 }
244
245 Check_AtSys(avc, aname, outb, areq)
246     register struct vcache *avc;
247     char *aname, **outb;
248     struct vrequest *areq;
249 {
250     register char *tname;
251     register int error = 0, offset = -1;
252
253     for (tname=aname; *tname; tname++) /*Move to the end of the string*/;
254
255     /*
256      * If the current string is 4 chars long or more, check to see if the
257      * tail end is "@sys".
258      */
259     if ((tname >= aname + 4) && (AFS_EQ_ATSYS(tname-4)))
260         offset = (tname - 4) - aname;
261     if (offset < 0) {
262         tname = aname;
263     }  else {
264         tname = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
265         if (offset)
266             strncpy(tname, aname, offset);
267         if (!afs_nfsexporter) 
268             strcpy(tname+offset, (afs_sysname ? afs_sysname : SYS_NAME ));
269         else {
270             register struct unixuser *au;
271             register afs_int32 error;
272             au = afs_GetUser(areq->uid, avc->fid.Cell, 0); afs_PutUser(au, 0);  
273             if (au->exporter) {
274                 error = EXP_SYSNAME(au->exporter, (char *)0, tname+offset);
275                 if (error) 
276                     strcpy(tname+offset, "@sys");
277             } else {
278                 strcpy(tname+offset, (afs_sysname ? afs_sysname : SYS_NAME ));
279             }
280         }
281         error = 1;
282     }
283     *outb = tname;
284     return error;
285 }
286
287
288 char *afs_getsysname(areq, adp)
289     register struct vrequest *areq;
290     register struct vcache *adp; {
291     static char sysname[MAXSYSNAME];
292     register struct unixuser *au;
293     register afs_int32 error;
294
295     AFS_STATCNT(getsysname);
296     /* this whole interface is wrong, it should take a buffer ptr and copy
297      * the data out.
298      */
299     au = afs_GetUser(areq->uid, adp->fid.Cell, 0);
300     afs_PutUser(au, 0); 
301     if (au->exporter) {
302       error = EXP_SYSNAME(au->exporter, (char *)0, sysname);
303       if (error) return "@sys";
304       else return sysname;
305     } else {
306       return (afs_sysname == 0? SYS_NAME : afs_sysname);
307     }
308 }
309
310 void afs_HandleAtName(aname, aresult, areq, adp)
311     register char *aname;
312     register char *aresult;
313     register struct vrequest *areq;
314     register struct vcache *adp; {
315     register int tlen;
316     AFS_STATCNT(HandleAtName);
317     tlen = strlen(aname);
318     if (tlen >= 4 && strcmp(aname+tlen-4, "@sys")==0) {
319         strncpy(aresult, aname, tlen-4);
320         strcpy(aresult+tlen-4, afs_getsysname(areq, adp));
321     }
322     else strcpy(aresult, aname);
323     }
324
325 #if (defined(AFS_SGI62_ENV) || defined(AFS_SUN57_64BIT_ENV))
326 extern int BlobScan(ino64_t *afile, afs_int32 ablob);
327 #else
328 extern int BlobScan(afs_int32 *afile, afs_int32 ablob);
329 #endif
330
331
332 /* called with an unlocked directory and directory cookie.  Areqp
333  * describes who is making the call.
334  * Scans the next N (about 30, typically) directory entries, and does
335  * a bulk stat call to stat them all.
336  *
337  * Must be very careful when merging in RPC responses, since we dont
338  * want to overwrite newer info that was added by a file system mutating
339  * call that ran concurrently with our bulk stat call.
340  *
341  * We do that, as described below, by not merging in our info (always
342  * safe to skip the merge) if the status info is valid in the vcache entry.
343  *
344  * If adapt ever implements the bulk stat RPC, then this code will need to
345  * ensure that vcaches created for failed RPC's to older servers have the
346  * CForeign bit set.
347  */
348 struct vcache * BStvc = (struct vcache *) 0;
349 void afs_DoBulkStat(adp, dirCookie, areqp)
350   struct vcache *adp;
351   long dirCookie;
352   struct vrequest *areqp;
353 {
354     int nentries;               /* # of entries to prefetch */
355     int nskip;                  /* # of slots in the LRU queue to skip */
356     struct vcache *lruvcp;      /* vcache ptr of our goal pos in LRU queue */
357     struct dcache *dcp;         /* chunk containing the dir block */
358     char *statMemp;             /* status memory block */
359     char *cbfMemp;              /* callback and fid memory block */
360     long temp;                  /* temp for holding chunk length, &c. */
361     struct AFSFid *fidsp;       /* file IDs were collecting */
362     struct AFSCallBack *cbsp;   /* call back pointers */
363     struct AFSCallBack *tcbp;   /* temp callback ptr */
364     struct AFSFetchStatus *statsp;      /* file status info */
365     struct AFSVolSync volSync;  /* vol sync return info */
366     struct vcache *tvcp;        /* temp vcp */
367     struct afs_q *tq;           /* temp queue variable */
368     AFSCBFids fidParm;          /* file ID parm for bulk stat */
369     AFSBulkStats statParm;      /* stat info parm for bulk stat */
370     int fidIndex;               /* which file were stating */
371     struct conn *tcp;           /* conn for call */
372     AFSCBs cbParm;              /* callback parm for bulk stat */
373     struct server *hostp = 0;   /* host we got callback from */
374     long origEvenCBs;           /* original # of callbacks for even-fid files */
375     long origOddCBs;            /* original # of callbacks for odd-fid files */
376     long origEvenZaps;          /* original # of recycles for even-fid files */
377     long origOddZaps;           /* original # of recycles for odd-fid files */
378     long startTime;             /* time we started the call,
379                                  * for callback expiration base
380                                  */
381     int statSeqNo;              /* Valued of file size to detect races */
382     int code;                   /* error code */
383     long newIndex;              /* new index in the dir */
384     struct DirEntry *dirEntryp; /* dir entry we are examining */
385     int i;
386     struct VenusFid afid;       /* file ID we are using now */
387     struct VenusFid tfid;       /* another temp. file ID */
388     afs_int32 retry;                  /* handle low-level SGI MP race conditions */
389     long volStates;             /* flags from vol structure */
390     struct volume *volp=0;      /* volume ptr */
391     struct VenusFid dotdot;
392     int flagIndex;              /* First file with bulk fetch flag set */
393     XSTATS_DECLS
394
395     /* first compute some basic parameters.  We dont want to prefetch more
396      * than a fraction of the cache in any given call, and we want to preserve
397      * a portion of the LRU queue in any event, so as to avoid thrashing
398      * the entire stat cache (we will at least leave some of it alone).
399      * presently dont stat more than 1/8 the cache in any one call.      */
400     nentries = afs_cacheStats / 8;
401
402     /* dont bother prefetching more than one calls worth of info */
403     if (nentries > AFSCBMAX) nentries = AFSCBMAX;
404
405     /* heuristic to make sure that things fit in 4K.  This means that
406      * we shouldnt make it any bigger than 47 entries.  I am typically
407      * going to keep it a little lower, since we don't want to load
408      * too much of the stat cache.
409      */
410     if (nentries > 30) nentries = 30;
411
412     /* now, to reduce the stack size, well allocate two 4K blocks,
413      * one for fids and callbacks, and one for stat info.  Well set
414      * up our pointers to the memory from there, too.
415      */
416     statMemp = osi_AllocLargeSpace(nentries * sizeof(AFSFetchStatus));
417     statsp = (struct AFSFetchStatus *) statMemp;
418     cbfMemp = osi_AllocLargeSpace(nentries *
419         (sizeof(AFSCallBack) + sizeof(AFSFid)));
420     fidsp = (AFSFid *) cbfMemp;
421     cbsp = (AFSCallBack *) (cbfMemp + nentries * sizeof(AFSFid));
422
423     /* next, we must iterate over the directory, starting from the specified
424      * cookie offset (dirCookie), and counting out nentries file entries.
425      * We skip files that already have stat cache entries, since we
426      * dont want to bulk stat files that are already in the cache.
427      */
428 tagain:
429     code = afs_VerifyVCache(adp, areqp);
430     if (code) goto done;
431
432     dcp = afs_GetDCache(adp, 0, areqp, &temp, &temp, 1);
433     if (!dcp) {
434         code = ENOENT;
435         goto done;
436     }
437
438     /* lock the directory cache entry */
439     ObtainReadLock(&adp->lock);
440
441     /*
442      * Make sure that the data in the cache is current. There are two
443      * cases we need to worry about:
444      * 1. The cache data is being fetched by another process.
445      * 2. The cache data is no longer valid
446      */
447     while ((adp->states & CStatd)
448            && (dcp->flags & DFFetching)
449            && hsame(adp->m.DataVersion, dcp->f.versionNo)) {
450         dcp->flags |= DFWaiting;
451         ReleaseReadLock(&adp->lock);
452         afs_osi_Sleep(&dcp->validPos);
453         ObtainReadLock(&adp->lock);
454     }
455     if (!(adp->states & CStatd)
456         || !hsame(adp->m.DataVersion, dcp->f.versionNo)) {
457         ReleaseReadLock(&adp->lock);
458         afs_PutDCache(dcp);
459         goto tagain;
460     }
461
462     /* Generate a sequence number so we can tell whether we should
463      * store the attributes when processing the response. This number is
464      * stored in the file size when we set the CBulkFetching bit. If the
465      * CBulkFetching is still set and this value hasn't changed, then
466      * we know we were the last to set CBulkFetching bit for this file,
467      * and it is safe to set the status information for this file.
468      */
469     statSeqNo = bulkStatCounter++;
470
471     /* now we have dir data in the cache, so scan the dir page */
472     fidIndex = 0;
473     flagIndex = 0;
474     while (1) { /* Should probably have some constant bound */
475         /* look for first safe entry to examine in the directory.  BlobScan
476          * looks for a the 1st allocated dir after the dirCookie slot.
477          */
478         newIndex = BlobScan(&dcp->f.inode, (dirCookie>>5));
479         if (newIndex == 0) break;
480
481         /* remember the updated directory cookie */
482         dirCookie = newIndex << 5;
483
484         /* get a ptr to the dir entry */
485         dirEntryp =(struct DirEntry *)afs_dir_GetBlob(&dcp->f.inode, newIndex);
486         if (!dirEntryp) break;
487
488         /* dont copy more than we have room for */
489         if (fidIndex >= nentries) {
490           DRelease((char *) dirEntryp, 0);
491           break;
492         }
493
494         /* now, if the dir entry looks good, copy it out to our list.  Vnode
495          * 0 means deleted, although it should also be free were it deleted.
496          */
497         if (dirEntryp->fid.vnode != 0) {
498             /* dont copy entries we have in our cache.  This check will
499              * also make us skip "." and probably "..", unless it has
500              * disappeared from the cache since we did our namei call.
501              */
502             tfid.Cell = adp->fid.Cell;
503             tfid.Fid.Volume = adp->fid.Fid.Volume;
504             tfid.Fid.Vnode = ntohl(dirEntryp->fid.vnode);
505             tfid.Fid.Unique = ntohl(dirEntryp->fid.vunique);
506             do {
507               retry = 0;
508               ObtainWriteLock(&afs_xvcache, 130);
509               tvcp = afs_FindVCache(&tfid, 0, 0, &retry, 0 /* no stats | LRU */);
510               if (tvcp && retry) {
511                 ReleaseWriteLock(&afs_xvcache);
512                 afs_PutVCache(tvcp);
513               }
514             } while (tvcp && retry);
515             if (!tvcp) {          /* otherwise, create manually */
516               tvcp = afs_NewVCache(&tfid, hostp, 0, 0);
517               ObtainWriteLock(&tvcp->lock, 505);
518               ReleaseWriteLock(&afs_xvcache);
519               afs_RemoveVCB(&tfid);
520               ReleaseWriteLock(&tvcp->lock);
521             } else {
522               ReleaseWriteLock(&afs_xvcache);
523             }
524             if (!tvcp)
525               goto done; /* can't happen at present, more's the pity */
526
527             /* WARNING: afs_DoBulkStat uses the Length field to store a
528              * sequence number for each bulk status request. Under no
529              * circumstances should afs_DoBulkStat store a sequence number
530              * if the new length will be ignored when afs_ProcessFS is
531              * called with new stats. */
532 #ifdef AFS_SGI_ENV
533             if (!(tvcp->states & (CStatd|CBulkFetching))
534                 && (tvcp->execsOrWriters <= 0)
535                 && !afs_DirtyPages(tvcp)
536                 && !AFS_VN_MAPPED((vnode_t*)tvcp))
537 #else
538             if (!(tvcp->states & (CStatd|CBulkFetching))
539                 && (tvcp->execsOrWriters <= 0) 
540                 && !afs_DirtyPages(tvcp))
541 #endif
542
543             {
544                 /* this entry doesnt exist in the cache, and is not
545                  * already being fetched by someone else, so add it to the
546                  * list of file IDs to obtain.
547                  *
548                  * We detect a callback breaking race condition by checking the
549                  * CBulkFetching state bit and the value in the file size.
550                  * It is safe to set the status only if the CBulkFetching
551                  * flag is still set and the value in the file size does
552                  * not change.
553                  *
554                  * Don't fetch status for dirty files. We need to
555                  * preserve the value of the file size. We could
556                  * flush the pages, but it wouldn't be worthwhile.
557                  */
558                 bcopy((char *) &tfid.Fid, (char *)(fidsp+fidIndex),
559                       sizeof(*fidsp));
560                 tvcp->states |= CBulkFetching;
561                 tvcp->m.Length = statSeqNo;
562                 fidIndex++;
563             }
564             afs_PutVCache(tvcp);
565         }       /* if dir vnode has non-zero entry */
566
567         /* move to the next dir entry by adding in the # of entries
568          * used by this dir entry.
569          */
570         temp = afs_dir_NameBlobs(dirEntryp->name) << 5;
571         DRelease((char *) dirEntryp, 0);
572         if (temp <= 0) break;
573         dirCookie += temp;
574     }   /* while loop over all dir entries */
575
576     /* now release the dir lock and prepare to make the bulk RPC */
577     ReleaseReadLock(&adp->lock);
578
579     /* release the chunk */
580     afs_PutDCache(dcp);
581
582     /* dont make a null call */
583     if (fidIndex == 0) goto done;
584
585     do {
586         /* setup the RPC parm structures */
587         fidParm.AFSCBFids_len = fidIndex;
588         fidParm.AFSCBFids_val = fidsp;
589         statParm.AFSBulkStats_len = fidIndex;
590         statParm.AFSBulkStats_val = statsp;
591         cbParm.AFSCBs_len = fidIndex;
592         cbParm.AFSCBs_val = cbsp;
593
594         /* start the timer; callback expirations are relative to this */
595         startTime = osi_Time();
596
597         tcp = afs_Conn(&adp->fid, areqp, SHARED_LOCK);
598         if (tcp) {
599             hostp = tcp->srvr->server;
600             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_BULKSTATUS);
601 #ifdef RX_ENABLE_LOCKS
602             AFS_GUNLOCK();
603 #endif /* RX_ENABLE_LOCKS */
604             code = RXAFS_BulkStatus(tcp->id, &fidParm, &statParm, &cbParm,
605                                     &volSync);
606 #ifdef RX_ENABLE_LOCKS
607             AFS_GLOCK();
608 #endif /* RX_ENABLE_LOCKS */
609             XSTATS_END_TIME;
610         }
611         else code = -1;
612     } while (afs_Analyze(tcp, code, &adp->fid, areqp, 
613                          AFS_STATS_FS_RPCIDX_BULKSTATUS, SHARED_LOCK, (struct cell *)0));
614
615     /* now, if we didnt get the info, bail out. */
616     if (code) goto done;
617
618     /* we need vol flags to create the entries properly */
619     dotdot.Fid.Volume = 0;
620     volp = afs_GetVolume(&adp->fid, areqp, READ_LOCK);
621     if (volp) {
622         volStates = volp->states;
623         if (volp->dotdot.Fid.Volume != 0)
624             dotdot = volp->dotdot;
625     }
626     else volStates = 0;
627
628     /* find the place to merge the info into  We do this by skipping
629      * nskip entries in the LRU queue.  The more we skip, the more
630      * we preserve, since the head of the VLRU queue is the most recently
631      * referenced file.
632      */
633   reskip:
634     nskip = afs_cacheStats / 2;         /* preserved fraction of the cache */
635     ObtainReadLock(&afs_xvcache);
636     if (QEmpty(&VLRU)) {
637       /* actually a serious error, probably should panic. Probably will 
638        * panic soon, oh well. */
639       ReleaseReadLock(&afs_xvcache);
640       goto done;
641     }
642     if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
643        refpanic ("Bulkstat VLRU inconsistent");
644     }
645     for(tq = VLRU.next; tq != &VLRU; tq = QNext(tq)) {
646         if (--nskip <= 0) break;
647         else if (QNext(QPrev(tq)) != tq) {
648            BStvc = QTOV(tq);
649            refpanic ("BulkStat VLRU inconsistent");
650         }
651     }
652     if (tq != &VLRU) lruvcp = QTOV(tq);
653     else lruvcp = QTOV(VLRU.next);
654
655     /* now we have to hold this entry, so that it does not get moved
656      * into the free list while we're running.  It could still get
657      * moved within the lru queue, but hopefully that will be rare; it
658      * doesn't hurt nearly as much.
659      */
660     retry = 0;
661     osi_vnhold(lruvcp, &retry);
662     ReleaseReadLock(&afs_xvcache);           /* could be read lock */
663     if (retry)
664       goto reskip;
665
666     /* otherwise, merge in the info.  We have to be quite careful here,
667      * since we need to ensure that we don't merge old info over newer
668      * stuff in a stat cache entry.  We're very conservative here: we don't
669      * do the merge at all unless we ourselves create the stat cache
670      * entry.  That's pretty safe, and should work pretty well, since we
671      * typically expect to do the stat cache creation ourselves.
672      *
673      * We also have to take into account racing token revocations.
674      */
675     for(i=0; i<fidIndex; i++) {
676         afid.Cell = adp->fid.Cell;
677         afid.Fid.Volume = adp->fid.Fid.Volume;
678         afid.Fid.Vnode = fidsp[i].Vnode;
679         afid.Fid.Unique = fidsp[i].Unique;
680         do {
681            retry = 0;
682            ObtainReadLock(&afs_xvcache);
683            tvcp = afs_FindVCache(&afid, 1, 0, &retry, 0/* !stats&!lru*/);
684            ReleaseReadLock(&afs_xvcache);
685         } while (tvcp && retry);
686
687         /* The entry may no longer exist */
688         if (tvcp == NULL) {
689             continue;
690         }
691
692         /* now we have the entry held, but we need to fill it in */
693         ObtainWriteLock(&tvcp->lock,131);
694
695         /* if CBulkFetching is not set, or if the file size no longer
696          * matches the value we placed there when we set the CBulkFetching
697          * flag, then someone else has done something with this node,
698          * and we may not have the latest status information for this
699          * file.  Leave the entry alone.
700          */
701         if (!(tvcp->states & CBulkFetching) || (tvcp->m.Length != statSeqNo)) {
702             flagIndex++;
703             ReleaseWriteLock(&tvcp->lock);
704             afs_PutVCache(tvcp);
705             continue;
706         }
707
708         /* now copy ".." entry back out of volume structure, if necessary */
709         if (tvcp->mvstat == 2  && (dotdot.Fid.Volume != 0)) {
710             if (!tvcp->mvid)
711                 tvcp->mvid = (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
712             *tvcp->mvid = dotdot;
713         }
714
715         ObtainWriteLock(&afs_xvcache,132);
716         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
717            refpanic ("Bulkstat VLRU inconsistent2");
718         }
719         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq) 
720             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq))
721            refpanic ("Bulkstat VLRU inconsistent4");
722         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq) 
723             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq)) 
724            refpanic ("Bulkstat VLRU inconsistent5");
725
726         if (tvcp != lruvcp) {  /* if they are == don't move it, don't corrupt vlru */
727            QRemove(&tvcp->vlruq);
728            QAdd(&lruvcp->vlruq, &tvcp->vlruq);
729         }
730
731         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
732            refpanic ("Bulkstat VLRU inconsistent3");
733         }
734         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq) 
735             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq))
736            refpanic ("Bulkstat VLRU inconsistent5");
737         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq) 
738             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq))
739            refpanic ("Bulkstat VLRU inconsistent6");
740         ReleaseWriteLock(&afs_xvcache);
741
742         /* We need to check the flags again. We may have missed
743          * something while we were waiting for a lock.
744          */
745         if (!(tvcp->states & CBulkFetching) || (tvcp->m.Length != statSeqNo)) {
746             flagIndex++;
747             ReleaseWriteLock(&tvcp->lock);
748             afs_PutVCache(tvcp);
749             continue;
750         }
751
752         /* now merge in the resulting status back into the vnode.
753          * We only do this if the entry looks clear.
754          */
755         afs_ProcessFS(tvcp, &statsp[i], areqp);
756 #ifdef AFS_LINUX22_ENV
757         /* overwrite the ops if it's a directory or symlink. */
758         if (vType(tvcp) == VDIR)
759             tvcp->v.v_op = &afs_dir_iops;
760         else if (vType(tvcp) == VLNK)
761             tvcp->v.v_op = &afs_symlink_iops;
762 #endif
763
764         ObtainWriteLock(&afs_xcbhash, 494);
765
766         /* We need to check the flags once more. We may have missed
767          * something while we were waiting for a lock.
768          */
769         if (!(tvcp->states & CBulkFetching) || (tvcp->m.Length != statSeqNo)) {
770             flagIndex++;
771             ReleaseWriteLock(&afs_xcbhash);
772             ReleaseWriteLock(&tvcp->lock);
773             afs_PutVCache(tvcp);
774             continue;
775         }
776
777         /* do some accounting for bulk stats: mark this entry as
778          * loaded, so we can tell if we use it before it gets
779          * recycled.
780          */
781         tvcp->states |= CBulkStat;
782         tvcp->states &= ~CBulkFetching;
783         flagIndex++;
784         afs_bulkStatsDone++;
785
786         /* merge in vol info */
787         if (volStates & VRO) tvcp->states |= CRO;
788         if (volStates & VBackup) tvcp->states |= CBackup;
789         if (volStates & VForeign) tvcp->states |= CForeign;
790
791         /* merge in the callback info */
792         tvcp->states |= CTruth;
793
794         /* get ptr to the callback we are interested in */
795         tcbp = cbsp + i;
796
797         if (tcbp->ExpirationTime != 0) {
798             tvcp->cbExpires = tcbp->ExpirationTime+startTime;
799             tvcp->callback = hostp;
800             tvcp->states |= CStatd;
801             afs_QueueCallback(tvcp, CBHash(tcbp->ExpirationTime), volp);
802         }
803         else if (tvcp->states & CRO) {
804             /* ordinary callback on a read-only volume -- AFS 3.2 style */
805             tvcp->cbExpires = 3600+startTime;
806             tvcp->callback = hostp;
807             tvcp->states |= CStatd;
808             afs_QueueCallback(tvcp, CBHash(3600), volp);
809         }
810         else {
811             tvcp->callback = 0;
812             tvcp->states &= ~(CStatd|CUnique);  
813             afs_DequeueCallback(tvcp);
814             if ((tvcp->states & CForeign) || (vType(tvcp) == VDIR)) 
815               osi_dnlc_purgedp (tvcp);  /* if it (could be) a directory */
816         }
817         ReleaseWriteLock(&afs_xcbhash);
818
819         ReleaseWriteLock(&tvcp->lock);
820         /* finally, we're done with the entry */
821         afs_PutVCache(tvcp);
822     }   /* for all files we got back */
823
824     /* finally return the pointer into the LRU queue */
825     afs_PutVCache(lruvcp);
826
827   done:
828     /* Be sure to turn off the CBulkFetching flags */
829     for(i=flagIndex; i<fidIndex; i++) {
830         afid.Cell = adp->fid.Cell;
831         afid.Fid.Volume = adp->fid.Fid.Volume;
832         afid.Fid.Vnode = fidsp[i].Vnode;
833         afid.Fid.Unique = fidsp[i].Unique;
834         do {
835            retry = 0;
836            ObtainReadLock(&afs_xvcache);
837            tvcp = afs_FindVCache(&afid, 1, 0, &retry, 0/* !stats&!lru*/);
838            ReleaseReadLock(&afs_xvcache);
839         } while (tvcp && retry);
840         if (tvcp != NULL
841             && (tvcp->states & CBulkFetching)
842             && (tvcp->m.Length == statSeqNo)) {
843           tvcp->states &= ~CBulkFetching;
844         }
845         if (tvcp != NULL) {
846           afs_PutVCache(tvcp);
847         }
848     }
849     if ( volp )
850         afs_PutVolume(volp, READ_LOCK);
851     
852     osi_FreeLargeSpace(statMemp);
853     osi_FreeLargeSpace(cbfMemp);
854 }
855
856 /* was: (AFS_DEC_ENV) || defined(AFS_OSF30_ENV) || defined(AFS_NCR_ENV) */
857 int AFSDOBULK = 1;
858
859 #ifdef  AFS_OSF_ENV
860 afs_lookup(adp, ndp)
861     struct vcache *adp;
862     struct nameidata *ndp; {
863     char aname[MAXNAMLEN+1];    /* XXX */
864     struct vcache **avcp = (struct vcache **)&(ndp->ni_vp);
865     struct ucred *acred = ndp->ni_cred;
866     int wantparent = ndp->ni_nameiop & WANTPARENT;
867     int opflag = ndp->ni_nameiop & OPFLAG;
868 #else   /* AFS_OSF_ENV */
869 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
870 afs_lookup(OSI_VC_ARG(adp), aname, avcp, pnp, flags, rdir, acred)
871     struct pathname *pnp;
872     int flags;
873     struct vnode *rdir;
874 #else
875 afs_lookup(adp, aname, avcp, acred)
876 #endif
877     OSI_VC_DECL(adp);
878     struct vcache **avcp;
879     char *aname;
880     struct AFS_UCRED *acred; {
881 #endif
882     struct vrequest treq;
883     char *tname = (char *)0;
884     register struct vcache *tvc=0;
885     register afs_int32 code;
886     int pass = 0, hit = 0;
887     long dirCookie;
888     extern afs_int32 afs_mariner;                       /*Writing activity to log?*/
889     OSI_VC_CONVERT(adp)
890     afs_hyper_t versionNo;
891
892     AFS_STATCNT(afs_lookup);
893 #ifdef  AFS_OSF_ENV
894     ndp->ni_dvp = (struct vnode *)adp;
895     bcopy(ndp->ni_ptr, aname, ndp->ni_namelen);
896     aname[ndp->ni_namelen] = '\0';
897 #endif  /* AFS_OSF_ENV */
898
899     *avcp = (struct vcache *) 0;   /* Since some callers don't initialize it */
900
901     if (code = afs_InitReq(&treq, acred)) { 
902       goto done;
903     }
904
905     /* lookup the name aname in the appropriate dir, and return a cache entry
906       on the resulting fid */
907
908     /*
909      * check for, and handle "@sys" if it's there.  We should be able
910      * to avoid the alloc and the strcpy with a little work, but it's
911      * not pressing.  If there aren't any remote users (ie, via the 
912      * NFS translator), we have a slightly easier job.
913      * the faster way to do this is to check for *aname == '@' and if 
914      * it's there, check for @sys, otherwise, assume there's no @sys 
915      * then, if the lookup fails, check for .*@sys...
916      */
917     if (!AFS_EQ_ATSYS(aname)) {
918       tname = aname;
919     }
920     else {
921         tname = (char *) osi_AllocLargeSpace(AFS_SMALLOCSIZ);
922         if (!afs_nfsexporter) 
923           strcpy(tname, (afs_sysname ? afs_sysname : SYS_NAME ));
924         else {
925           register struct unixuser *au;
926           register afs_int32 error;
927           au = afs_GetUser(treq.uid, adp->fid.Cell, 0); afs_PutUser(au, 0);     
928           if (au->exporter) {
929             error = EXP_SYSNAME(au->exporter, (char *)0, tname);
930             if (error) 
931               strcpy(tname, "@sys");
932           } else {
933               strcpy(tname, (afs_sysname ? afs_sysname : SYS_NAME ));
934           }
935         }
936       }
937
938     /* come back to here if we encounter a non-existent object in a read-only
939        volume's directory */
940
941   redo:
942     *avcp = (struct vcache *) 0;   /* Since some callers don't initialize it */
943
944     if (!(adp->states & CStatd)) {
945         if (code = afs_VerifyVCache2(adp, &treq))
946           goto done;
947     }
948     else code = 0;
949
950     /* watch for ".." in a volume root */
951     if (adp->mvstat == 2 && tname[0] == '.' && tname[1] == '.' && !tname[2]) {
952         /* looking up ".." in root via special hacks */
953         if (adp->mvid == (struct VenusFid *) 0 || adp->mvid->Fid.Volume == 0) {
954 #ifdef  AFS_OSF_ENV
955             extern struct vcache *afs_globalVp;
956             if (adp == afs_globalVp) {
957                 struct vnode *rvp = (struct vnode *)adp;
958 /*
959                 ndp->ni_vp = rvp->v_vfsp->vfs_vnodecovered;
960                 ndp->ni_dvp = ndp->ni_vp;
961                 VN_HOLD(*avcp);
962 */
963                 code = ENODEV;
964                 goto done;
965             }
966 #endif
967             code = ENODEV;
968             goto done;
969         }
970         /* otherwise we have the fid here, so we use it */
971         tvc = afs_GetVCache(adp->mvid, &treq, (afs_int32 *)0,
972                             (struct vcache*)0, 0);
973         afs_Trace3(afs_iclSetp, CM_TRACE_GETVCDOTDOT,
974                    ICL_TYPE_FID, adp->mvid, ICL_TYPE_POINTER, tvc, 
975                    ICL_TYPE_INT32,  code);
976         *avcp = tvc;
977         code = (tvc ? 0 : ENOENT);
978         hit = 1;
979         if (tvc && !tvc->vrefCount) {
980             osi_Panic("TT1");
981         }
982         if (code) {
983             /*printf("LOOKUP GETVCDOTDOT -> %d\n", code);*/
984         }
985         goto done;
986     }
987
988     /* now check the access */
989     if (treq.uid != adp->last_looker) {  
990        if (!afs_AccessOK(adp, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
991          *avcp = (struct vcache *)0;
992          code = EACCES;
993          goto done;
994        }
995        else adp->last_looker = treq.uid;
996     } 
997
998
999     /* special case lookup of ".".  Can we check for it sooner in this code,
1000      * for instance, way up before "redo:" ??
1001      * I'm not fiddling with the LRUQ here, either, perhaps I should, or else 
1002      * invent a lightweight version of GetVCache.
1003      */
1004     if (tname[0] == '.' && !tname[1]) { /* special case */
1005         ObtainReadLock(&afs_xvcache);   
1006         osi_vnhold(adp, 0);
1007         ReleaseReadLock(&afs_xvcache);  
1008       code = 0;
1009       *avcp = tvc = adp;
1010       hit = 1;
1011         if (adp && !adp->vrefCount) {
1012             osi_Panic("TT2");
1013         }
1014       goto done;
1015     }
1016
1017     tvc = osi_dnlc_lookup (adp, tname, WRITE_LOCK);
1018     *avcp = tvc;  /* maybe wasn't initialized, but it is now */
1019 #ifdef AFS_LINUX22_ENV
1020     if (tvc) {
1021       if (tvc->mvstat == 2) { /* we don't trust the dnlc for root vcaches */
1022         AFS_RELE(tvc);
1023         *avcp = 0;
1024       }
1025       else {  
1026         code = 0;
1027         hit = 1;
1028         goto done;
1029       }
1030     }
1031 #else /* non - LINUX */
1032     if (tvc) {
1033       code = 0;
1034       hit = 1;
1035       goto done;
1036     }
1037 #endif /* linux22 */
1038
1039     {
1040     register struct dcache *tdc;
1041     afs_int32 dirOffset, dirLen;
1042     ino_t theDir;
1043     struct VenusFid tfid;
1044
1045     /* now we have to lookup the next fid */
1046     tdc = afs_GetDCache(adp, 0, &treq, &dirOffset, &dirLen, 1);
1047     if (!tdc) {
1048       *avcp = (struct vcache *)0;  /* redundant, but harmless */
1049       code = EIO;
1050       goto done;
1051     }
1052
1053     /* now we will just call dir package with appropriate inode.
1054       Dirs are always fetched in their entirety for now */
1055     /* If the first lookup doesn't succeed, maybe it's got @sys in the name */
1056     ObtainReadLock(&adp->lock);
1057
1058     /*
1059      * Make sure that the data in the cache is current. There are two
1060      * cases we need to worry about:
1061      * 1. The cache data is being fetched by another process.
1062      * 2. The cache data is no longer valid
1063      */
1064     while ((adp->states & CStatd)
1065            && (tdc->flags & DFFetching)
1066            && hsame(adp->m.DataVersion, tdc->f.versionNo)) {
1067         tdc->flags |= DFWaiting;
1068         ReleaseReadLock(&adp->lock);
1069         afs_osi_Sleep(&tdc->validPos);
1070         ObtainReadLock(&adp->lock);
1071     }
1072     if (!(adp->states & CStatd)
1073         || !hsame(adp->m.DataVersion, tdc->f.versionNo)) {
1074         ReleaseReadLock(&adp->lock);
1075         afs_PutDCache(tdc);
1076         goto redo;
1077     }
1078
1079     /* Save the version number for when we call osi_dnlc_enter */
1080     hset(versionNo, tdc->f.versionNo);
1081
1082     theDir = tdc->f.inode;
1083     code = afs_dir_LookupOffset(&theDir, tname, &tfid.Fid, &dirCookie);
1084     if (code == ENOENT && tname == aname) {
1085       int len;
1086       len = strlen(aname);
1087       if (len >= 4 && AFS_EQ_ATSYS(aname+len-4)) {
1088         tname = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1089         afs_HandleAtName(aname, tname, &treq, adp);
1090         code = afs_dir_LookupOffset(&theDir, tname, &tfid.Fid, &dirCookie);
1091       }
1092     }
1093     ReleaseReadLock(&adp->lock);
1094     afs_PutDCache(tdc);
1095
1096     /* new fid has same cell and volume */
1097     tfid.Cell = adp->fid.Cell;
1098     tfid.Fid.Volume = adp->fid.Fid.Volume;
1099     afs_Trace4(afs_iclSetp, CM_TRACE_LOOKUP, ICL_TYPE_POINTER, adp, 
1100                ICL_TYPE_STRING, tname,
1101                ICL_TYPE_FID, &tfid, ICL_TYPE_INT32, code);
1102
1103     if (code) {
1104         if (code != ENOENT) {
1105             printf("LOOKUP dirLookupOff -> %d\n", code);
1106         }
1107         goto done;
1108     }  
1109
1110     /* prefetch some entries, if the dir is currently open.  The variable
1111      * dirCookie tells us where to start prefetching from.
1112      */
1113     if (AFSDOBULK && adp->opens > 0 && !(adp->states & CForeign)) {
1114         afs_int32 retry;
1115         /* if the entry is not in the cache, or is in the cache,
1116          * but hasn't been statd, then do a bulk stat operation.
1117          */
1118         do {
1119            retry = 0;
1120            ObtainReadLock(&afs_xvcache);        
1121            tvc = afs_FindVCache(&tfid, 1, 0, &retry, 0/* !stats,!lru */);
1122            ReleaseReadLock(&afs_xvcache);       
1123         } while (tvc && retry);
1124
1125         if (!tvc || !(tvc->states & CStatd)) {
1126             afs_DoBulkStat(adp, dirCookie, &treq);
1127         }
1128
1129         /* if the vcache isn't usable, release it */
1130         if (tvc && !(tvc->states & CStatd)) {
1131             afs_PutVCache(tvc);
1132             tvc = (struct vcache *) 0;
1133         }
1134     }
1135     else tvc = (struct vcache *) 0;
1136     
1137     /* now get the status info, if we don't already have it */
1138     /* This is kind of weird, but we might wind up accidentally calling
1139      * RXAFS_Lookup because we happened upon a file which legitimately
1140      * has a 0 uniquifier. That is the result of allowing unique to wrap
1141      * to 0. This was fixed in AFS 3.4. For CForeigh, Unique == 0 means that
1142      * the file has not yet been looked up.
1143      */
1144     if (!tvc) {
1145        afs_int32 cached = 0;
1146        if (!tfid.Fid.Unique && (adp->states & CForeign)) {
1147             tvc = afs_LookupVCache(&tfid, &treq, &cached, WRITE_LOCK, 
1148                                    adp, tname);
1149        } 
1150        if (!tvc) {  /* lookup failed or wasn't called */
1151             tvc = afs_GetVCache(&tfid, &treq, &cached, (struct vcache*)0,
1152                                 WRITE_LOCK);
1153        }
1154     } /* if !tvc */
1155     } /* sub-block just to reduce stack usage */
1156
1157     if (tvc) {
1158        if (adp->states & CForeign)
1159            tvc->states |= CForeign;
1160         tvc->parentVnode = adp->fid.Fid.Vnode;
1161         tvc->parentUnique = adp->fid.Fid.Unique;
1162         tvc->states &= ~CBulkStat;
1163         if (tvc->mvstat == 1) {
1164           /* a mt point, possibly unevaluated */
1165           struct volume *tvolp;
1166
1167             ObtainWriteLock(&tvc->lock,133);
1168             code = EvalMountPoint(tvc, adp, &tvolp, &treq);
1169             ReleaseWriteLock(&tvc->lock);
1170             /* next, we want to continue using the target of the mt point */
1171             if (tvc->mvid && (tvc->states & CMValid)) {
1172               struct vcache *uvc;
1173                 /* now lookup target, to set .. pointer */
1174                 afs_Trace2(afs_iclSetp, CM_TRACE_LOOKUP1,
1175                            ICL_TYPE_POINTER, tvc, ICL_TYPE_FID, &tvc->fid);
1176                 uvc = tvc;      /* remember for later */
1177
1178                 if (tvolp && (tvolp->states & VForeign)) {
1179                     /* XXXX tvolp has ref cnt on but not locked! XXX */
1180                     tvc = afs_GetRootVCache(tvc->mvid, &treq, (afs_int32 *)0, tvolp, WRITE_LOCK);
1181                 } else {
1182                     tvc = afs_GetVCache(tvc->mvid, &treq, (afs_int32 *)0,
1183                                         (struct vcache*)0, WRITE_LOCK);
1184                 }
1185                 afs_PutVCache(uvc, WRITE_LOCK); /* we're done with it */
1186
1187                 if (!tvc) {
1188                     code = ENOENT;
1189                     if (tvolp) {
1190                         afs_PutVolume(tvolp, WRITE_LOCK);
1191                     }
1192                     goto done;
1193                 }
1194
1195                 /* now, if we came via a new mt pt (say because of a new
1196                  * release of a R/O volume), we must reevaluate the ..
1197                  * ptr to point back to the appropriate place */
1198                 if (tvolp) {
1199                     ObtainWriteLock(&tvc->lock,134);
1200                     if (tvc->mvid == (struct VenusFid *) 0) {
1201                         tvc->mvid = (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
1202                     }
1203                     /* setup backpointer */
1204                     *tvc->mvid = tvolp->dotdot;
1205                     ReleaseWriteLock(&tvc->lock);
1206                     afs_PutVolume(tvolp, WRITE_LOCK);
1207                 }
1208             }
1209             else {
1210                 afs_PutVCache(tvc, WRITE_LOCK);
1211                 code = ENOENT;
1212                 if (tvolp) afs_PutVolume(tvolp, WRITE_LOCK);
1213                 goto done;
1214             }
1215         }
1216         *avcp = tvc;
1217         if (tvc && !tvc->vrefCount) {
1218             osi_Panic("TT3");
1219         }
1220         code = 0;
1221     }
1222     else {
1223         /* if we get here, we found something in a directory that couldn't
1224            be located (a Multics "connection failure").  If the volume is
1225            read-only, we try flushing this entry from the cache and trying
1226            again. */
1227         if (pass == 0) {
1228             struct volume *tv;
1229             tv = afs_GetVolume(&adp->fid, &treq, READ_LOCK);
1230             if (tv) {
1231                 if (tv->states & VRO) {
1232                     pass = 1;                   /* try this *once* */
1233                     ObtainWriteLock(&afs_xcbhash, 495);
1234                     afs_DequeueCallback(adp);
1235                     /* re-stat to get later version */
1236                     adp->states &= ~CStatd;
1237                     ReleaseWriteLock(&afs_xcbhash);
1238                     osi_dnlc_purgedp(adp);
1239                     afs_PutVolume(tv, READ_LOCK);
1240                     goto redo;
1241                 }
1242                 afs_PutVolume(tv, READ_LOCK);
1243             }
1244         }
1245         code = ENOENT;
1246     }
1247
1248 done:
1249     /* put the network buffer back, if need be */
1250     if (tname != aname && tname) osi_FreeLargeSpace(tname);
1251     if (code == 0) {
1252 #ifdef  AFS_OSF_ENV
1253         /* Handle RENAME; only need to check rename "."  */
1254         if (opflag == RENAME && wantparent && *ndp->ni_next == 0) {
1255             if (!FidCmp(&(tvc->fid), &(adp->fid))) { 
1256                 afs_PutVCache(*avcp, WRITE_LOCK);
1257                 *avcp = NULL;
1258                 return afs_CheckCode(EISDIR, &treq, 18);
1259             }
1260         }
1261 #endif  /* AFS_OSF_ENV */
1262
1263         if (afs_mariner)
1264           afs_AddMarinerName(aname, tvc); 
1265         if (!hit) {
1266           osi_dnlc_enter (adp, aname, tvc, &versionNo);
1267         }
1268         else {
1269 #ifdef AFS_LINUX20_ENV
1270             /* So Linux inode cache is up to date. */
1271             code = afs_VerifyVCache(tvc, &treq);
1272 #else
1273             return 0;  /* can't have been any errors if hit and !code */
1274 #endif
1275         }
1276     }
1277     code = afs_CheckCode(code, &treq, 19);
1278     if (code) {
1279        /* If there is an error, make sure *avcp is null.
1280         * Alphas panic otherwise - defect 10719.
1281         */
1282        *avcp = (struct vcache *)0;
1283     }
1284
1285     return code;
1286 }