Windows: cm_GiveUpAllCallBacksAllServersMulti()
[openafs.git] / src / WINNT / afsd / cm_scache.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 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <afs/stds.h>
15
16 #include <windows.h>
17 #include <winsock2.h>
18 #include <nb30.h>
19 #include <malloc.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <osi.h>
23
24 #include "afsd.h"
25 #include "cm_btree.h"
26
27 /*extern void afsi_log(char *pattern, ...);*/
28
29 extern osi_hyper_t hzero;
30
31 /* File locks */
32 osi_queue_t *cm_allFileLocks;
33 osi_queue_t *cm_freeFileLocks;
34 unsigned long cm_lockRefreshCycle;
35
36 /* lock for globals */
37 osi_rwlock_t cm_scacheLock;
38
39 /* Dummy scache entry for use with pioctl fids */
40 cm_scache_t cm_fakeSCache;
41
42 osi_queue_t * cm_allFreeWaiters;        /* protected by cm_scacheLock */
43
44 #ifdef AFS_FREELANCE_CLIENT
45 extern osi_mutex_t cm_Freelance_Lock;
46 #endif
47
48 cm_scache_t *
49 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
50 {
51     afs_int32 code;
52
53     lock_ObtainWrite(&cm_data.rootSCachep->rw);
54     code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
55                       CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
56     if (!code)
57         cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
58     lock_ReleaseWrite(&cm_data.rootSCachep->rw);
59
60     return cm_data.rootSCachep;
61 }
62
63
64 /* must be called with cm_scacheLock write-locked! */
65 void cm_AdjustScacheLRU(cm_scache_t *scp)
66 {
67     lock_AssertWrite(&cm_scacheLock);
68     osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
69     osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
70 }
71
72 /* call with cm_scacheLock write-locked and scp rw held */
73 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
74 {
75     cm_scache_t **lscpp;
76     cm_scache_t *tscp;
77     int i;
78         
79     lock_AssertWrite(&cm_scacheLock);
80     lock_AssertWrite(&scp->rw);
81     if (scp->flags & CM_SCACHEFLAG_INHASH) {
82         /* hash it out first */
83         i = CM_SCACHE_HASH(&scp->fid);
84         for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
85              tscp;
86              lscpp = &tscp->nextp, tscp = tscp->nextp) {
87             if (tscp == scp) {
88                 *lscpp = scp->nextp;
89                 scp->nextp = NULL;
90                 scp->flags &= ~CM_SCACHEFLAG_INHASH;
91                 break;
92             }
93         }
94     }
95 }
96
97 /* called with cm_scacheLock and scp write-locked */
98 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
99 {
100 #ifdef USE_BPLUS
101     /* destroy directory Bplus Tree */
102     if (scp->dirBplus) {
103         LARGE_INTEGER start, end;
104
105         if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
106             /* 
107              * We are not holding the dirlock and obtaining it
108              * requires that we drop the scp->rw.  As a result
109              * we will leave the dirBplus tree intact but 
110              * invalidate the version number so that whatever
111              * operation is currently active can safely complete
112              * but the contents will be ignored on the next 
113              * directory operation.
114              */
115             scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
116             return;
117         }
118
119         QueryPerformanceCounter(&start);
120         bplus_free_tree++;
121         freeBtree(scp->dirBplus);
122         scp->dirBplus = NULL;
123         scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
124         QueryPerformanceCounter(&end);
125         
126         if (!dirlock) 
127             lock_ReleaseWrite(&scp->dirlock);
128
129         bplus_free_time += (end.QuadPart - start.QuadPart);
130     }
131 #endif
132 }
133
134 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
135 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
136 {
137     if (scp->refCount != 0) {
138         return -1;
139     }
140
141     if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
142         osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
143 #ifdef DEBUG
144         osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
145 #endif
146         return -1;
147     }
148
149     cm_RemoveSCacheFromHashTable(scp);
150
151 #if 0
152     if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
153         osi_queueData_t *qdp;
154         cm_buf_t *bufp;
155
156         while(qdp = scp->bufWritesp) {
157             bufp = osi_GetQData(qdp);
158             osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
159             osi_QDFree(qdp);
160             if (bufp) {
161                 lock_ObtainMutex(&bufp->mx);
162                 bufp->cmFlags &= ~CM_BUF_CMSTORING;
163                 bufp->flags &= ~CM_BUF_DIRTY;
164                 bufp->dirty_offset = 0;
165                 bufp->dirty_length = 0;
166                 bufp->flags |= CM_BUF_ERROR;
167                 bufp->error = VNOVNODE;
168                 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
169                 bufp->dirtyCounter++;
170                 if (bufp->flags & CM_BUF_WAITING) {
171                     osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
172                     osi_Wakeup((long) &bufp);
173                 }
174                 lock_ReleaseMutex(&bufp->mx);
175                 buf_Release(bufp);
176             }
177         }
178         while(qdp = scp->bufReadsp) {
179             bufp = osi_GetQData(qdp);
180             osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
181             osi_QDFree(qdp);
182             if (bufp) {
183                 lock_ObtainMutex(&bufp->mx);
184                 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
185                 bufp->flags &= ~CM_BUF_DIRTY;
186                 bufp->dirty_offset = 0;
187                 bufp->dirty_length = 0;
188                 bufp->flags |= CM_BUF_ERROR;
189                 bufp->error = VNOVNODE;
190                 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
191                 bufp->dirtyCounter++;
192                 if (bufp->flags & CM_BUF_WAITING) {
193                     osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
194                     osi_Wakeup((long) &bufp);
195                 }
196                 lock_ReleaseMutex(&bufp->mx);
197                 buf_Release(bufp);
198             }
199         }
200         buf_CleanDirtyBuffers(scp); 
201     } else {
202         /* look for things that shouldn't still be set */
203         osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
204         osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
205     }
206 #endif
207
208     /* invalidate so next merge works fine;
209      * also initialize some flags */
210     scp->fileType = 0;
211     scp->flags &= ~(CM_SCACHEFLAG_STATD
212                      | CM_SCACHEFLAG_DELETED
213                      | CM_SCACHEFLAG_RO
214                      | CM_SCACHEFLAG_PURERO
215                      | CM_SCACHEFLAG_OVERQUOTA
216                      | CM_SCACHEFLAG_OUTOFSPACE
217                      | CM_SCACHEFLAG_EACCESS);
218     scp->serverModTime = 0;
219     scp->dataVersion = CM_SCACHE_VERSION_BAD;
220     scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
221     scp->bulkStatProgress = hzero;
222     scp->waitCount = 0;
223     scp->waitQueueT = NULL;
224
225     if (scp->cbServerp) {
226         cm_PutServer(scp->cbServerp);
227         scp->cbServerp = NULL;
228     }
229     scp->cbExpires = 0;
230     scp->volumeCreationDate = 0;
231
232     scp->fid.vnode = 0;
233     scp->fid.volume = 0;
234     scp->fid.unique = 0;
235     scp->fid.cell = 0;
236     scp->fid.hash = 0;
237
238     /* remove from dnlc */
239     cm_dnlcPurgedp(scp);
240     cm_dnlcPurgevp(scp);
241
242     /* discard cached status; if non-zero, Close
243      * tried to store this to server but failed */
244     scp->mask = 0;
245
246     /* discard symlink info */
247     scp->mountPointStringp[0] = '\0';
248     memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
249     memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
250
251     /* reset locking info */
252     scp->fileLocksH = NULL;
253     scp->fileLocksT = NULL;
254     scp->serverLock = (-1);
255     scp->exclusiveLocks = 0;
256     scp->sharedLocks = 0;
257     scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
258     scp->fsLockCount = 0;
259
260     /* not locked, but there can be no references to this guy
261      * while we hold the global refcount lock.
262      */
263     cm_FreeAllACLEnts(scp);
264
265     cm_ResetSCacheDirectory(scp, 0);
266     return 0;
267 }
268
269
270 /* 
271  * called with cm_scacheLock write-locked; find a vnode to recycle.
272  * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
273  * returns scp->mx held.
274  */
275 cm_scache_t *cm_GetNewSCache(void)
276 {
277     cm_scache_t *scp;
278     int retry = 0;
279
280     lock_AssertWrite(&cm_scacheLock);
281 #if 0
282     /* first pass - look for deleted objects */
283     for ( scp = cm_data.scacheLRULastp;
284           scp;
285           scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
286     {
287         osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
288                     "invalid cm_scache_t address");
289
290         if (scp->refCount == 0) {
291             if (scp->flags & CM_SCACHEFLAG_DELETED) {
292                 if (!lock_TryWrite(&scp->rw))
293                     continue;
294
295                 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
296                 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
297
298                     /* we found an entry, so return it */
299                     /* now remove from the LRU queue and put it back at the
300                      * head of the LRU queue.
301                      */
302                     cm_AdjustScacheLRU(scp);
303
304                     /* and we're done */
305                     return scp;
306                 } 
307                 lock_ReleaseWrite(&scp->rw);
308                 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
309             } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
310                 if (!lock_TryWrite(&scp->rw))
311                     continue;
312
313                 /* we found an entry, so return it */
314                 /* now remove from the LRU queue and put it back at the
315                 * head of the LRU queue.
316                 */
317                 cm_AdjustScacheLRU(scp);
318
319                 /* and we're done */
320                 return scp;
321             }
322         }       
323     }   
324     osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
325 #endif 
326
327     if (cm_data.currentSCaches >= cm_data.maxSCaches) {
328         /* There were no deleted scache objects that we could use.  Try to find
329          * one that simply hasn't been used in a while.
330          */
331         for ( scp = cm_data.scacheLRULastp;
332               scp;
333               scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
334         {
335             /* It is possible for the refCount to be zero and for there still
336              * to be outstanding dirty buffers.  If there are dirty buffers,
337              * we must not recycle the scp. */
338             if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
339                 if (!buf_DirtyBuffersExist(&scp->fid)) {
340                     if (!lock_TryWrite(&scp->rw))
341                         continue;
342
343                     if (!cm_RecycleSCache(scp, 0)) {
344                         /* we found an entry, so return it */
345                         /* now remove from the LRU queue and put it back at the
346                          * head of the LRU queue.
347                          */
348                         cm_AdjustScacheLRU(scp);
349
350                         /* and we're done */
351                         return scp;
352                     }
353                     lock_ReleaseWrite(&scp->rw);
354                 } else {
355                     osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
356                 }
357             }   
358         }
359         osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
360
361         return NULL;
362     }
363         
364     /* if we get here, we should allocate a new scache entry.  We either are below
365      * quota or we have a leak and need to allocate a new one to avoid panicing.
366      */
367     scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
368     osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
369                 "invalid cm_scache_t address");
370     memset(scp, 0, sizeof(cm_scache_t));
371     scp->magic = CM_SCACHE_MAGIC;
372     lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
373     osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
374     lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
375 #ifdef USE_BPLUS
376     lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
377 #endif
378     scp->serverLock = -1;
379
380     /* and put it in the LRU queue */
381     osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
382     if (!cm_data.scacheLRULastp) 
383         cm_data.scacheLRULastp = scp;
384     cm_data.currentSCaches++;
385     cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
386     cm_dnlcPurgevp(scp); 
387     scp->allNextp = cm_data.allSCachesp;
388     cm_data.allSCachesp = scp;
389     return scp;
390 }       
391
392 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
393 {
394     fidp->cell = cell;
395     fidp->volume = volume;
396     fidp->vnode = vnode;
397     fidp->unique = unique;
398     fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
399 }
400
401 /* like strcmp, only for fids */
402 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
403 {
404     if (ap->hash != bp->hash)
405         return 1;
406     if (ap->vnode != bp->vnode)
407         return 1;
408     if (ap->volume != bp->volume) 
409         return 1;
410     if (ap->unique != bp->unique) 
411         return 1;
412     if (ap->cell != bp->cell) 
413         return 1;
414     return 0;
415 }
416
417 void cm_fakeSCacheInit(int newFile)
418 {
419     if ( newFile ) {
420         memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
421         cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
422         cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
423         cm_data.fakeSCache.cbExpires = (time_t)-1;
424         /* can leave clientModTime at 0 */
425         cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
426         cm_data.fakeSCache.unixModeBits = 0777;
427         cm_data.fakeSCache.length.LowPart = 1000;
428         cm_data.fakeSCache.linkCount = 1;
429         cm_data.fakeSCache.refCount = 1;
430         cm_data.fakeSCache.serverLock = -1;
431         cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
432     }
433     lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
434     lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
435     lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
436 }
437
438 long
439 cm_ValidateSCache(void)
440 {
441     cm_scache_t * scp, *lscp;
442     long i;
443
444     if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
445          cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
446         afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
447         fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
448         return -17;
449     }
450
451     for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0; 
452           scp;
453           lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
454         if (scp->magic != CM_SCACHE_MAGIC) {
455             afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
456             fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
457             return -1;
458         }
459         if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
460             afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
461             fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
462             return -2;
463         }
464         if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
465             afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
466             fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
467             return -3;
468         }
469         if (i > cm_data.currentSCaches ) {
470             afsi_log("cm_ValidateSCache failure: LRU First queue loops");
471             fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
472             return -13;
473         }
474         if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
475             afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
476             fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
477             return -15;
478         }
479     }
480
481     for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
482           lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
483         if (scp->magic != CM_SCACHE_MAGIC) {
484             afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
485             fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
486             return -5;
487         }
488         if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
489             afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
490             fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
491             return -6;
492         }
493         if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
494             afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
495             fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
496             return -7;
497         }
498         if (i > cm_data.currentSCaches ) {
499             afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
500             fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
501             return -14;
502         }
503         if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
504             afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
505             fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
506             return -16;
507         }
508     }
509
510     for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
511         for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
512             afs_uint32 hash;
513             hash = CM_SCACHE_HASH(&scp->fid);
514             if (scp->magic != CM_SCACHE_MAGIC) {
515                 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
516                 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
517                 return -9;
518             }
519             if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
520                 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
521                 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
522                 return -10;
523             }
524             if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
525                 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
526                 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
527                 return -11;
528             }
529             if (hash != i) {
530                 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
531                 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
532                 return -13;
533             }
534         }
535     }
536
537     return cm_dnlcValidate();
538 }
539
540 void
541 cm_SuspendSCache(void)
542 {
543     cm_scache_t * scp;
544     time_t now;
545
546     cm_GiveUpAllCallbacksAllServersMulti(TRUE);
547
548     /* 
549      * After this call all servers are marked down.
550      * Do not clear the callbacks, instead change the
551      * expiration time so that the callbacks will be expired
552      * when the servers are marked back up.  However, we
553      * want the callbacks to be preserved as long as the 
554      * servers are down.  That way if the machine resumes
555      * without network, the stat cache item will still be
556      * considered valid.
557      */
558     now = time(NULL);
559
560     lock_ObtainWrite(&cm_scacheLock);
561     for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
562         if (scp->cbServerp) {
563             if (scp->flags & CM_SCACHEFLAG_PURERO) {
564                 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
565                 if (volp) {
566                     if (volp->cbExpiresRO == scp->cbExpires)
567                         volp->cbExpiresRO = now+1;
568                     cm_PutVolume(volp);
569                 }
570             }
571             scp->cbExpires = now+1;
572         }
573     }
574     lock_ReleaseWrite(&cm_scacheLock);
575 }
576
577 long
578 cm_ShutdownSCache(void)
579 {
580     cm_scache_t * scp;
581
582     lock_ObtainWrite(&cm_scacheLock);
583
584     for ( scp = cm_data.allSCachesp; scp;
585           scp = scp->allNextp ) {
586         if (scp->randomACLp) {
587             lock_ReleaseWrite(&cm_scacheLock);
588             lock_ObtainWrite(&scp->rw);
589             lock_ObtainWrite(&cm_scacheLock);
590             cm_FreeAllACLEnts(scp);
591             lock_ReleaseWrite(&scp->rw);
592         }
593
594         if (scp->cbServerp) {
595             cm_PutServer(scp->cbServerp);
596             scp->cbServerp = NULL;
597         }
598         scp->cbExpires = 0;
599         scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
600
601 #ifdef USE_BPLUS
602         if (scp->dirBplus)
603             freeBtree(scp->dirBplus);
604         scp->dirBplus = NULL;
605         scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
606         lock_FinalizeRWLock(&scp->dirlock);
607 #endif
608         lock_FinalizeRWLock(&scp->rw);
609         lock_FinalizeRWLock(&scp->bufCreateLock);
610     }
611     lock_ReleaseWrite(&cm_scacheLock);
612
613     cm_GiveUpAllCallbacksAllServersMulti(FALSE);
614
615     return cm_dnlcShutdown();
616 }
617
618 void cm_InitSCache(int newFile, long maxSCaches)
619 {
620     static osi_once_t once;
621         
622     if (osi_Once(&once)) {
623         lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
624         if ( newFile ) {
625             memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
626             cm_data.allSCachesp = NULL;
627             cm_data.currentSCaches = 0;
628             cm_data.maxSCaches = maxSCaches;
629             cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
630         } else {
631             cm_scache_t * scp;
632
633             for ( scp = cm_data.allSCachesp; scp;
634                   scp = scp->allNextp ) {
635                 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
636                 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
637 #ifdef USE_BPLUS
638                 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
639 #endif
640                 scp->cbServerp = NULL;
641                 scp->cbExpires = 0;
642                 scp->volumeCreationDate = 0;
643                 scp->fileLocksH = NULL;
644                 scp->fileLocksT = NULL;
645                 scp->serverLock = (-1);
646                 scp->lastRefreshCycle = 0;
647                 scp->exclusiveLocks = 0;
648                 scp->sharedLocks = 0;
649                 scp->openReads = 0;
650                 scp->openWrites = 0;
651                 scp->openShares = 0;
652                 scp->openExcls = 0;
653                 scp->waitCount = 0;
654 #ifdef USE_BPLUS
655                 scp->dirBplus = NULL;
656                 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
657 #endif
658                 scp->waitQueueT = NULL;
659                 scp->flags &= ~CM_SCACHEFLAG_WAITING;
660             }
661         }
662         cm_allFileLocks = NULL;
663         cm_freeFileLocks = NULL;
664         cm_lockRefreshCycle = 0;
665         cm_fakeSCacheInit(newFile);
666         cm_allFreeWaiters = NULL;
667         cm_dnlcInit(newFile);
668         osi_EndOnce(&once);
669     }
670 }
671
672 /* version that doesn't bother creating the entry if we don't find it */
673 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
674 {
675     long hash;
676     cm_scache_t *scp;
677
678     hash = CM_SCACHE_HASH(fidp);
679
680     if (fidp->cell == 0) {
681         return NULL;
682     }
683
684     lock_ObtainRead(&cm_scacheLock);
685     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
686         if (cm_FidCmp(fidp, &scp->fid) == 0) {
687             cm_HoldSCacheNoLock(scp);
688             lock_ConvertRToW(&cm_scacheLock);
689             cm_AdjustScacheLRU(scp);
690             lock_ReleaseWrite(&cm_scacheLock);
691             return scp;
692         }
693     }
694     lock_ReleaseRead(&cm_scacheLock);
695     return NULL;
696 }
697
698 #ifdef DEBUG_REFCOUNT
699 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
700                   cm_req_t *reqp, char * file, long line)
701 #else
702 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
703                   cm_req_t *reqp)
704 #endif
705 {
706     long hash;
707     cm_scache_t *scp = NULL;
708     long code;
709     cm_volume_t *volp = NULL;
710     cm_cell_t *cellp;
711     int special = 0; // yj: boolean variable to test if file is on root.afs
712     int isRoot = 0;
713     extern cm_fid_t cm_rootFid;
714         
715     hash = CM_SCACHE_HASH(fidp);
716         
717     if (fidp->cell == 0)
718         return CM_ERROR_INVAL;
719
720 #ifdef AFS_FREELANCE_CLIENT
721     special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
722                fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
723                !(fidp->vnode==0x1 && fidp->unique==0x1));
724     isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
725               fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
726               fidp->vnode==0x1 && fidp->unique==0x1);
727 #endif
728
729     // yj: check if we have the scp, if so, we don't need
730     // to do anything else
731     lock_ObtainWrite(&cm_scacheLock);
732     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
733         if (cm_FidCmp(fidp, &scp->fid) == 0) {
734 #ifdef DEBUG_REFCOUNT
735             afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
736             osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
737 #endif
738 #ifdef AFS_FREELANCE_CLIENT
739             if (cm_freelanceEnabled && special && 
740                 cm_data.fakeDirVersion != scp->dataVersion)
741                 break;
742 #endif
743             cm_HoldSCacheNoLock(scp);
744             *outScpp = scp;
745             cm_AdjustScacheLRU(scp);
746             lock_ReleaseWrite(&cm_scacheLock);
747             return 0;
748         }
749     }
750
751     // yj: when we get here, it means we don't have an scp
752     // so we need to either load it or fake it, depending
753     // on whether the file is "special", see below.
754
755     // yj: if we're trying to get an scp for a file that's
756     // on root.afs of homecell, we want to handle it specially
757     // because we have to fill in the status stuff 'coz we
758     // don't want trybulkstat to fill it in for us
759 #ifdef AFS_FREELANCE_CLIENT
760     if (cm_freelanceEnabled && isRoot) {
761         osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
762         /* freelance: if we are trying to get the root scp for the first
763          * time, we will just put in a place holder entry. 
764          */
765         volp = NULL;
766     }
767           
768     if (cm_freelanceEnabled && special) {
769         lock_ReleaseWrite(&cm_scacheLock);
770         osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
771
772         if (cm_getLocalMountPointChange()) {
773             cm_clearLocalMountPointChange();
774             cm_reInitLocalMountPoints();
775         }
776
777         lock_ObtainWrite(&cm_scacheLock);
778         if (scp == NULL) {
779             scp = cm_GetNewSCache();    /* returns scp->rw held */
780             if (scp == NULL) {
781                 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
782                 lock_ReleaseWrite(&cm_scacheLock);
783                 return CM_ERROR_WOULDBLOCK;
784             }
785         } else {
786             lock_ReleaseWrite(&cm_scacheLock);
787             lock_ObtainWrite(&scp->rw);
788             lock_ObtainWrite(&cm_scacheLock);
789         }
790         scp->fid = *fidp;
791         scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
792         scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
793         scp->dotdotFid.unique=1;
794         scp->dotdotFid.vnode=1;
795         scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
796         if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
797             scp->nextp = cm_data.scacheHashTablep[hash];
798             cm_data.scacheHashTablep[hash] = scp;
799             scp->flags |= CM_SCACHEFLAG_INHASH;
800         }
801         scp->refCount = 1;
802         osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
803
804         /* must be called after the scp->fid is set */
805         cm_FreelanceFetchMountPointString(scp);
806         cm_FreelanceFetchFileType(scp);
807         
808         scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
809         scp->length.HighPart = 0;
810         scp->owner=0x0;
811         scp->unixModeBits=0777;
812         scp->clientModTime=FakeFreelanceModTime;
813         scp->serverModTime=FakeFreelanceModTime;
814         scp->parentUnique = 0x1;
815         scp->parentVnode=0x1;
816         scp->group=0;
817         scp->dataVersion=cm_data.fakeDirVersion;
818         scp->bufDataVersionLow=cm_data.fakeDirVersion;
819         scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
820         scp->fsLockCount=0;
821         lock_ReleaseWrite(&scp->rw);
822         lock_ReleaseWrite(&cm_scacheLock);
823         *outScpp = scp;
824 #ifdef DEBUG_REFCOUNT
825         afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
826         osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
827 #endif
828         return 0;
829     }
830     // end of yj code
831 #endif /* AFS_FREELANCE_CLIENT */
832
833     /* otherwise, we need to find the volume */
834     if (!cm_freelanceEnabled || !isRoot) {
835         lock_ReleaseWrite(&cm_scacheLock);      /* for perf. reasons */
836         cellp = cm_FindCellByID(fidp->cell, 0);
837         if (!cellp) 
838             return CM_ERROR_NOSUCHCELL;
839
840         code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
841         if (code) 
842             return code;
843         lock_ObtainWrite(&cm_scacheLock);
844     }
845         
846     /* otherwise, we have the volume, now reverify that the scp doesn't
847      * exist, and proceed.
848      */
849     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
850         if (cm_FidCmp(fidp, &scp->fid) == 0) {
851 #ifdef DEBUG_REFCOUNT
852             afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
853             osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
854 #endif
855             cm_HoldSCacheNoLock(scp);
856             cm_AdjustScacheLRU(scp);
857             lock_ReleaseWrite(&cm_scacheLock);
858             if (volp)
859                 cm_PutVolume(volp);
860             *outScpp = scp;
861             return 0;
862         }
863     }
864         
865     /* now, if we don't have the fid, recycle something */
866     scp = cm_GetNewSCache();    /* returns scp->rw held */
867     if (scp == NULL) {
868         osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
869         lock_ReleaseWrite(&cm_scacheLock);
870         if (volp)
871             cm_PutVolume(volp);
872         return CM_ERROR_WOULDBLOCK;
873     }
874 #ifdef DEBUG_REFCOUNT
875     afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
876 #endif
877     osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
878
879     osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
880
881     scp->fid = *fidp;
882     if (!cm_freelanceEnabled || !isRoot) {
883         /* if this scache entry represents a volume root then we need 
884          * to copy the dotdotFipd from the volume structure where the 
885          * "master" copy is stored (defect 11489)
886          */
887         if (volp->vol[ROVOL].ID == fidp->volume) {
888             scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
889             if (scp->fid.vnode == 1 && scp->fid.unique == 1)
890                 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
891         } else if (volp->vol[BACKVOL].ID == fidp->volume) {
892             scp->flags |= CM_SCACHEFLAG_RO;
893             if (scp->fid.vnode == 1 && scp->fid.unique == 1)
894                 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
895         } else {
896             if (scp->fid.vnode == 1 && scp->fid.unique == 1)
897                 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
898         }
899     }
900     if (volp)
901         cm_PutVolume(volp);
902     scp->nextp = cm_data.scacheHashTablep[hash];
903     cm_data.scacheHashTablep[hash] = scp;
904     scp->flags |= CM_SCACHEFLAG_INHASH;
905     lock_ReleaseWrite(&scp->rw);
906     scp->refCount = 1;
907 #ifdef DEBUG_REFCOUNT
908     afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
909 #endif
910     osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
911
912     /* XXX - The following fields in the cm_scache are 
913      * uninitialized:
914      *   fileType
915      *   parentVnode
916      *   parentUnique
917      */
918         
919     /* now we have a held scache entry; just return it */
920     *outScpp = scp;
921 #ifdef DEBUG_REFCOUNT
922     afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
923     osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
924 #endif
925     lock_ReleaseWrite(&cm_scacheLock);
926     return 0;
927 }
928
929 /* Returns a held reference to the scache's parent 
930  * if it exists */
931 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
932 {
933     long code = 0;
934     int i;
935     cm_fid_t    parent_fid;
936     cm_scache_t * pscp = NULL;
937
938     lock_ObtainWrite(&cm_scacheLock);
939     cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
940
941     if (cm_FidCmp(&scp->fid, &parent_fid)) {
942         i = CM_SCACHE_HASH(&parent_fid);
943         for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
944             if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
945                 cm_HoldSCacheNoLock(pscp);
946                 break;
947             }
948         }
949     }
950
951     lock_ReleaseWrite(&cm_scacheLock);
952
953     return pscp;
954 }
955
956 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
957 {
958     cm_scache_waiter_t * w;
959
960     lock_ObtainWrite(&cm_scacheLock);
961     if (cm_allFreeWaiters == NULL) {
962         w = malloc(sizeof(*w));
963         memset(w, 0, sizeof(*w));
964     } else {
965         w = (cm_scache_waiter_t *) cm_allFreeWaiters;
966         osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
967     }
968
969     w->threadId = thrd_Current();
970     w->scp = scp;
971     cm_HoldSCacheNoLock(scp);
972     w->flags = flags;
973     w->bufp = bufp;
974
975     osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
976     lock_ReleaseWrite(&cm_scacheLock);
977
978     osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
979 }
980
981 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
982 {
983     cm_scache_waiter_t * w;
984     int this_is_me;
985
986     osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
987
988     lock_ObtainRead(&cm_scacheLock);
989     for (w = (cm_scache_waiter_t *)scp->waitQueueH;
990          w;
991          w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
992         if (w->flags == flags && w->bufp == bufp) {
993             break;
994         }
995     }
996
997     osi_assertx(w != NULL, "null cm_scache_waiter_t");
998     this_is_me = (w->threadId == thrd_Current());
999     lock_ReleaseRead(&cm_scacheLock);
1000
1001     if (!this_is_me) {
1002         osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1003         return 0;
1004     }
1005
1006     osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1007
1008     lock_ObtainWrite(&cm_scacheLock);
1009     osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1010     cm_ReleaseSCacheNoLock(scp);
1011     memset(w, 0, sizeof(*w));
1012     osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1013     lock_ReleaseWrite(&cm_scacheLock);
1014
1015     return 1;
1016 }
1017
1018
1019 /* synchronize a fetch, store, read, write, fetch status or store status.
1020  * Called with scache mutex held, and returns with it held, but temporarily
1021  * drops it during the fetch.
1022  * 
1023  * At most one flag can be on in flags, if this is an RPC request.
1024  *
1025  * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1026  *
1027  * There are a lot of weird restrictions here; here's an attempt to explain the
1028  * rationale for the concurrency restrictions implemented in this function.
1029  *
1030  * First, although the file server will break callbacks when *another* machine
1031  * modifies a file or status block, the client itself is responsible for
1032  * concurrency control on its own requests.  Callback breaking events are rare,
1033  * and simply invalidate any concurrent new status info.
1034  *
1035  * In the absence of callback breaking messages, we need to know how to
1036  * synchronize incoming responses describing updates to files.  We synchronize
1037  * operations that update the data version by comparing the data versions.
1038  * However, updates that do not update the data, but only the status, can't be
1039  * synchronized with fetches or stores, since there's nothing to compare
1040  * to tell which operation executed first at the server.
1041  *
1042  * Thus, we can allow multiple ops that change file data, or dir data, and
1043  * fetches.  However, status storing ops have to be done serially.
1044  *
1045  * Furthermore, certain data-changing ops are incompatible: we can't read or
1046  * write a buffer while doing a truncate.  We can't read and write the same
1047  * buffer at the same time, or write while fetching or storing, or read while
1048  * fetching a buffer (this may change).  We can't fetch and store at the same
1049  * time, either.
1050  *
1051  * With respect to status, we can't read and write at the same time, read while
1052  * fetching, write while fetching or storing, or fetch and store at the same time.
1053  *
1054  * We can't allow a get callback RPC to run in concurrently with something that
1055  * will return updated status, since we could start a call, have the server
1056  * return status, have another machine make an update to the status (which
1057  * doesn't change serverModTime), have the original machine get a new callback,
1058  * and then have the original machine merge in the early, old info from the
1059  * first call.  At this point, the easiest way to avoid this problem is to have
1060  * getcallback calls conflict with all others for the same vnode.  Other calls
1061  * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1062  * vnode must be careful not to merge in their status unless they have obtained
1063  * a callback from the start of their call.
1064  *
1065  * Note added 1/23/96
1066  * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1067  * Each such RPC passes a FileLength parameter, which the server uses to do
1068  * pre-truncation if necessary.  So if two RPC's are processed out of order at
1069  * the server, the one with the smaller FileLength will be processed last,
1070  * possibly resulting in a bogus truncation.  The simplest way to avoid this
1071  * is to serialize all StoreData RPC's.  This is the reason we defined
1072  * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1073  */
1074 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1075                afs_uint32 rights, afs_uint32 flags)
1076 {
1077     osi_queueData_t *qdp;
1078     long code;
1079     cm_buf_t *tbufp;
1080     afs_uint32 outRights;
1081     int bufLocked;
1082     afs_uint32 sleep_scp_flags = 0;
1083     afs_uint32 sleep_buf_cmflags = 0;
1084     afs_uint32 sleep_scp_bufs = 0;
1085     int wakeupCycle;
1086
1087     lock_AssertWrite(&scp->rw);
1088
1089     /* lookup this first */
1090     bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1091
1092     if (bufp)
1093         osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1094
1095
1096     /* Do the access check.  Now we don't really do the access check
1097      * atomically, since the caller doesn't expect the parent dir to be
1098      * returned locked, and that is what we'd have to do to prevent a
1099      * callback breaking message on the parent due to a setacl call from
1100      * being processed while we're running.  So, instead, we check things
1101      * here, and if things look fine with the access, we proceed to finish
1102      * the rest of this check.  Sort of a hack, but probably good enough.
1103      */
1104
1105     while (1) {
1106         if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1107             /* if we're bringing in a new status block, ensure that
1108              * we aren't already doing so, and that no one is
1109              * changing the status concurrently, either.  We need
1110              * to do this, even if the status is of a different
1111              * type, since we don't have the ability to figure out,
1112              * in the AFS 3 protocols, which status-changing
1113              * operation ran first, or even which order a read and
1114              * a write occurred in.
1115              */
1116             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1117                               CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1118                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1119                 goto sleep;
1120             }
1121         }
1122         if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1123                       | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1124             /* if we're going to make an RPC to change the status, make sure
1125              * that no one is bringing in or sending out the status.
1126              */
1127             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1128                               CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1129                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1130                 goto sleep;
1131             }
1132             if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1133                 (scp->bufReadsp || scp->bufWritesp)) {
1134                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1135                 goto sleep;
1136             }
1137         }
1138         if (flags & CM_SCACHESYNC_FETCHDATA) {
1139             /* if we're bringing in a new chunk of data, make sure that
1140              * nothing is happening to that chunk, and that we aren't
1141              * changing the basic file status info, either.
1142              */
1143             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1144                               CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1145                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1146                 goto sleep;
1147             }
1148             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1149                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1150                 goto sleep;
1151             }
1152         }
1153         if (flags & CM_SCACHESYNC_STOREDATA) {
1154             /* same as fetch data */
1155             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1156                                | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1157                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1158                 goto sleep;
1159             }
1160             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1161                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1162                 goto sleep;
1163             }
1164         }
1165
1166         if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1167             /* Don't allow concurrent StoreData RPC's */
1168             if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1169                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1170                 goto sleep;
1171             }
1172         }
1173
1174         if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1175             /* Don't allow more than one BKG store request */
1176             if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1177                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1178                 goto sleep;
1179             }
1180         }
1181
1182         if (flags & CM_SCACHESYNC_LOCK) {
1183             /* Don't allow concurrent fiddling with lock lists */
1184             if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1185                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1186                 goto sleep;
1187             }
1188         }
1189
1190         /* now the operations that don't correspond to making RPCs */
1191         if (flags & CM_SCACHESYNC_GETSTATUS) {
1192             /* we can use the status that's here, if we're not
1193              * bringing in new status.
1194              */
1195             if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1196                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1197                 goto sleep;
1198             }
1199         }
1200         if (flags & CM_SCACHESYNC_SETSTATUS) {
1201             /* we can make a change to the local status, as long as
1202              * the status isn't changing now.
1203              *
1204              * If we're fetching or storing a chunk of data, we can
1205              * change the status locally, since the fetch/store
1206              * operations don't change any of the data that we're
1207              * changing here.
1208              */
1209             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1210                               CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1211                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1212                 goto sleep;
1213             }
1214         }
1215         if (flags & CM_SCACHESYNC_READ) {
1216             /* we're going to read the data, make sure that the
1217              * status is available, and that the data is here.  It
1218              * is OK to read while storing the data back.
1219              */
1220             if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1221                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1222                 goto sleep;
1223             }
1224             if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1225                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1226                 goto sleep;
1227             }
1228             if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1229                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1230                 goto sleep;
1231             }
1232         }
1233         if (flags & CM_SCACHESYNC_WRITE) {
1234             /* don't write unless the status is stable and the chunk
1235              * is stable.
1236              */
1237             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1238                               CM_SCACHEFLAG_SIZESTORING)) {
1239                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1240                 goto sleep;
1241             }
1242             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1243                                           CM_BUF_CMSTORING |
1244                                           CM_BUF_CMWRITING))) {
1245                 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1246                          scp, bufp,
1247                          ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1248                           ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1249                            ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1250                             "UNKNOWN!!!"))));
1251                 goto sleep;
1252             }
1253         }
1254
1255         if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1256             if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1257                 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1258                           scp);
1259                 if (bufLocked) 
1260                     lock_ReleaseMutex(&bufp->mx);
1261                 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1262                 if (bufLocked) {
1263                     lock_ReleaseWrite(&scp->rw);
1264                     lock_ObtainMutex(&bufp->mx);
1265                     lock_ObtainWrite(&scp->rw);
1266                 }
1267                 if (code) 
1268                     return code;
1269                 flags &= ~CM_SCACHESYNC_FORCECB;        /* only force once */
1270                 continue;
1271             }
1272         }
1273
1274         if (rights) {
1275             /* can't check access rights without a callback */
1276             osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1277
1278             if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1279                 return CM_ERROR_READONLY;
1280
1281             if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1282                 if (~outRights & rights) 
1283                     return CM_ERROR_NOACCESS;
1284             }
1285             else {
1286                 /* we don't know the required access rights */
1287                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1288                 code = cm_GetAccessRights(scp, userp, reqp);
1289                 if (bufLocked) {
1290                     lock_ReleaseWrite(&scp->rw);
1291                     lock_ObtainMutex(&bufp->mx);
1292                     lock_ObtainWrite(&scp->rw);
1293                 }
1294                 if (code) 
1295                     return code;
1296                 continue;
1297             }
1298         }
1299
1300         /* if we get here, we're happy */
1301         break;
1302
1303       sleep:
1304         /* first check if we're not supposed to wait: fail 
1305          * in this case, returning with everything still locked.
1306          */
1307         if (flags & CM_SCACHESYNC_NOWAIT) 
1308             return CM_ERROR_WOULDBLOCK;
1309
1310         /* These are used for minidump debugging */
1311         sleep_scp_flags = scp->flags;           /* so we know why we slept */
1312         sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1313         sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1314
1315         /* wait here, then try again */
1316         osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1317         if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1318             scp->waitCount++;
1319             scp->waitRequests++;
1320             osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests", 
1321                      scp, scp->waitCount, scp->waitRequests);
1322         } else {
1323             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1324             scp->flags |= CM_SCACHEFLAG_WAITING;
1325             scp->waitCount = scp->waitRequests = 1;
1326         }
1327
1328         cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1329         wakeupCycle = 0;
1330         do {
1331             if (bufLocked) 
1332                 lock_ReleaseMutex(&bufp->mx);
1333             osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1334             if (bufLocked) 
1335                 lock_ObtainMutex(&bufp->mx);
1336             lock_ObtainWrite(&scp->rw);
1337         } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1338
1339         cm_UpdateServerPriority();
1340
1341         scp->waitCount--;
1342         osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests", 
1343                  scp, scp->waitCount, scp->waitRequests);
1344         if (scp->waitCount == 0) {
1345             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1346             scp->flags &= ~CM_SCACHEFLAG_WAITING;
1347             scp->waitRequests = 0;
1348         }
1349     } /* big while loop */
1350         
1351     /* now, update the recorded state for RPC-type calls */
1352     if (flags & CM_SCACHESYNC_FETCHSTATUS)
1353         scp->flags |= CM_SCACHEFLAG_FETCHING;
1354     if (flags & CM_SCACHESYNC_STORESTATUS)
1355         scp->flags |= CM_SCACHEFLAG_STORING;
1356     if (flags & CM_SCACHESYNC_SETSIZE)
1357         scp->flags |= CM_SCACHEFLAG_SIZESETTING;
1358     if (flags & CM_SCACHESYNC_STORESIZE)
1359         scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1360     if (flags & CM_SCACHESYNC_GETCALLBACK)
1361         scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1362     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1363         scp->flags |= CM_SCACHEFLAG_DATASTORING;
1364     if (flags & CM_SCACHESYNC_ASYNCSTORE)
1365         scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1366     if (flags & CM_SCACHESYNC_LOCK)
1367         scp->flags |= CM_SCACHEFLAG_LOCKING;
1368
1369     /* now update the buffer pointer */
1370     if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1371         /* ensure that the buffer isn't already in the I/O list */
1372         for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1373             tbufp = osi_GetQData(qdp);
1374             osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1375         }
1376
1377         /* queue a held reference to the buffer in the "reading" I/O list */
1378         qdp = osi_QDAlloc();
1379         osi_SetQData(qdp, bufp);
1380
1381         buf_Hold(bufp);
1382         bufp->cmFlags |= CM_BUF_CMFETCHING;
1383         osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1384     }
1385
1386     if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1387         osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1388             "attempting to store extents on a non-file object");
1389
1390         /* ensure that the buffer isn't already in the I/O list */
1391         for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1392             tbufp = osi_GetQData(qdp);
1393             osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1394         }
1395
1396         /* queue a held reference to the buffer in the "writing" I/O list */
1397         qdp = osi_QDAlloc();
1398         osi_SetQData(qdp, bufp);
1399         buf_Hold(bufp);
1400         bufp->cmFlags |= CM_BUF_CMSTORING;
1401         osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1402     }
1403
1404     if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1405         /* mark the buffer as being written to. */
1406         bufp->cmFlags |= CM_BUF_CMWRITING;
1407     }
1408
1409     return 0;
1410 }
1411
1412 /* for those syncops that setup for RPCs.
1413  * Called with scache locked.
1414  */
1415 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1416 {
1417     osi_queueData_t *qdp;
1418     cm_buf_t *tbufp;
1419
1420     lock_AssertWrite(&scp->rw);
1421
1422     /* now, update the recorded state for RPC-type calls */
1423     if (flags & CM_SCACHESYNC_FETCHSTATUS)
1424         scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1425     if (flags & CM_SCACHESYNC_STORESTATUS)
1426         scp->flags &= ~CM_SCACHEFLAG_STORING;
1427     if (flags & CM_SCACHESYNC_SETSIZE)
1428         scp->flags &= ~CM_SCACHEFLAG_SIZESETTING;
1429     if (flags & CM_SCACHESYNC_STORESIZE)
1430         scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1431     if (flags & CM_SCACHESYNC_GETCALLBACK)
1432         scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1433     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1434         scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1435     if (flags & CM_SCACHESYNC_ASYNCSTORE)
1436         scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1437     if (flags & CM_SCACHESYNC_LOCK)
1438         scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1439
1440     /* now update the buffer pointer */
1441     if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1442         int release = 0;
1443
1444         /* ensure that the buffer is in the I/O list */
1445         for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1446             tbufp = osi_GetQData(qdp);
1447             if (tbufp == bufp) 
1448                 break;
1449         }
1450         if (qdp) {
1451             osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1452             osi_QDFree(qdp);
1453             release = 1;
1454         }
1455         bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1456         if (bufp->flags & CM_BUF_WAITING) {
1457             osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1458             osi_Wakeup((LONG_PTR) &bufp);
1459         }
1460         if (release)
1461             buf_Release(bufp);
1462     }
1463
1464     /* now update the buffer pointer */
1465     if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1466         int release = 0;
1467         /* ensure that the buffer is in the I/O list */
1468         for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1469             tbufp = osi_GetQData(qdp);
1470             if (tbufp == bufp) 
1471                 break;
1472         }
1473         if (qdp) {
1474             osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1475             osi_QDFree(qdp);
1476             release = 1;
1477         }
1478         bufp->cmFlags &= ~CM_BUF_CMSTORING;
1479         if (bufp->flags & CM_BUF_WAITING) {
1480             osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1481             osi_Wakeup((LONG_PTR) &bufp);
1482         }
1483         if (release)
1484             buf_Release(bufp);
1485     }
1486
1487     if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1488         osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1489         bufp->cmFlags &= ~CM_BUF_CMWRITING;
1490     }
1491
1492     /* and wakeup anyone who is waiting */
1493     if (scp->flags & CM_SCACHEFLAG_WAITING) {
1494         osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1495         osi_Wakeup((LONG_PTR) &scp->flags);
1496     }
1497 }       
1498
1499 /* merge in a response from an RPC.  The scp must be locked, and the callback
1500  * is optional.
1501  *
1502  * Don't overwrite any status info that is dirty, since we could have a store
1503  * operation (such as store data) that merges some info in, and we don't want
1504  * to lose the local updates.  Typically, there aren't many updates we do
1505  * locally, anyway, probably only mtime.
1506  *
1507  * There is probably a bug in here where a chmod (which doesn't change
1508  * serverModTime) that occurs between two fetches, both of whose responses are
1509  * handled after the callback breaking is done, but only one of whose calls
1510  * started before that, can cause old info to be merged from the first call.
1511  */
1512 void cm_MergeStatus(cm_scache_t *dscp, 
1513                     cm_scache_t *scp, AFSFetchStatus *statusp, 
1514                     AFSVolSync *volsyncp,
1515                     cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1516 {
1517     afs_uint64 dataVersion;
1518     struct cm_volume *volp = NULL;
1519     struct cm_cell *cellp = NULL;
1520
1521     // yj: i want to create some fake status for the /afs directory and the
1522     // entries under that directory
1523 #ifdef AFS_FREELANCE_CLIENT
1524     if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1525          scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1526         if (scp == cm_data.rootSCachep) {
1527             osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1528             statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1529             statusp->Length = cm_fakeDirSize;
1530             statusp->Length_hi = 0;
1531         } else {
1532             statusp->FileType = scp->fileType;
1533             statusp->Length = scp->length.LowPart;
1534             statusp->Length_hi = scp->length.HighPart;
1535         }
1536         statusp->InterfaceVersion = 0x1;
1537         statusp->LinkCount = scp->linkCount;
1538         statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1539         statusp->Author = 0x1;
1540         statusp->Owner = 0x0;
1541         statusp->CallerAccess = 0x9;
1542         statusp->AnonymousAccess = 0x9;
1543         statusp->UnixModeBits = 0777;
1544         statusp->ParentVnode = 0x1;
1545         statusp->ParentUnique = 0x1;
1546         statusp->ResidencyMask = 0;
1547         statusp->ClientModTime = FakeFreelanceModTime;
1548         statusp->ServerModTime = FakeFreelanceModTime;
1549         statusp->Group = 0;
1550         statusp->SyncCounter = 0;
1551         statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1552         statusp->lockCount = 0;
1553         statusp->errorCode = 0;
1554     }
1555 #endif /* AFS_FREELANCE_CLIENT */
1556
1557     if (statusp->errorCode != 0) {      
1558         scp->flags |= CM_SCACHEFLAG_EACCESS;
1559         osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1560
1561         scp->fileType = 0;      /* unknown */
1562
1563         scp->serverModTime = 0;
1564         scp->clientModTime = 0;
1565         scp->length.LowPart = 0;
1566         scp->length.HighPart = 0;
1567         scp->serverLength.LowPart = 0;
1568         scp->serverLength.HighPart = 0;
1569         scp->linkCount = 0;
1570         scp->owner = 0;
1571         scp->group = 0;
1572         scp->unixModeBits = 0;
1573         scp->anyAccess = 0;
1574         scp->dataVersion = CM_SCACHE_VERSION_BAD;
1575         scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1576         scp->fsLockCount = 0;
1577
1578         if (dscp) {
1579             scp->parentVnode = dscp->fid.vnode;
1580             scp->parentUnique = dscp->fid.unique;
1581         } else {
1582             scp->parentVnode = 0;
1583             scp->parentUnique = 0;
1584         }
1585         goto done;
1586     } else {
1587         scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1588     }
1589
1590     dataVersion = statusp->dataVersionHigh;
1591     dataVersion <<= 32;
1592     dataVersion |= statusp->DataVersion;
1593
1594     if (!(flags & CM_MERGEFLAG_FORCE) && 
1595         dataVersion < scp->dataVersion &&
1596         scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1597
1598         cellp = cm_FindCellByID(scp->fid.cell, 0);
1599         if (scp->cbServerp) {
1600             cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1601                               reqp, CM_GETVOL_FLAG_CREATE, &volp);
1602             osi_Log2(afsd_logp, "old data from server %x volume %s",
1603                       scp->cbServerp->addr.sin_addr.s_addr,
1604                       volp ? volp->namep : "(unknown)");
1605         }
1606         osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1607                   scp, scp->dataVersion, dataVersion);
1608         /* we have a number of data fetch/store operations running
1609          * concurrently, and we can tell which one executed last at the
1610          * server by its mtime.
1611          * Choose the one with the largest mtime, and ignore the rest.
1612          *
1613          * These concurrent calls are incompatible with setting the
1614          * mtime, so we won't have a locally changed mtime here.
1615          *
1616          * We could also have ACL info for a different user than usual,
1617          * in which case we have to do that part of the merge, anyway.
1618          * We won't have to worry about the info being old, since we
1619          * won't have concurrent calls
1620          * that change file status running from this machine.
1621          *
1622          * Added 3/17/98:  if we see data version regression on an RO
1623          * file, it's probably due to a server holding an out-of-date
1624          * replica, rather than to concurrent RPC's.  Failures to
1625          * release replicas are now flagged by the volserver, but only
1626          * since AFS 3.4 5.22, so there are plenty of clients getting
1627          * out-of-date replicas out there.
1628          *
1629          * If we discover an out-of-date replica, by this time it's too
1630          * late to go to another server and retry.  Also, we can't
1631          * reject the merge, because then there is no way for
1632          * GetAccess to do its work, and the caller gets into an
1633          * infinite loop.  So we just grin and bear it.
1634          */
1635         if (!(scp->flags & CM_SCACHEFLAG_RO))
1636             goto done;
1637     }       
1638
1639     if (cm_readonlyVolumeVersioning)
1640         scp->volumeCreationDate = volsyncp->spare1;       /* volume creation date */
1641
1642     scp->serverModTime = statusp->ServerModTime;
1643
1644     if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1645         scp->clientModTime = statusp->ClientModTime;
1646     }
1647     if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1648         scp->length.LowPart = statusp->Length;
1649         scp->length.HighPart = statusp->Length_hi;
1650     }
1651
1652     scp->serverLength.LowPart = statusp->Length;
1653     scp->serverLength.HighPart = statusp->Length_hi;
1654
1655     scp->linkCount = statusp->LinkCount;
1656     scp->owner = statusp->Owner;
1657     scp->group = statusp->Group;
1658     scp->unixModeBits = statusp->UnixModeBits & 07777;
1659
1660     if (statusp->FileType == File)
1661         scp->fileType = CM_SCACHETYPE_FILE;
1662     else if (statusp->FileType == Directory)
1663         scp->fileType = CM_SCACHETYPE_DIRECTORY;
1664     else if (statusp->FileType == SymbolicLink) {
1665         if ((scp->unixModeBits & 0111) == 0)
1666             scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1667         else
1668             scp->fileType = CM_SCACHETYPE_SYMLINK;
1669     }       
1670     else {
1671         osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1672         scp->fileType = CM_SCACHETYPE_INVALID;  /* invalid */
1673     }
1674     /* and other stuff */
1675     scp->parentVnode = statusp->ParentVnode;
1676     scp->parentUnique = statusp->ParentUnique;
1677     scp->fsLockCount = statusp->lockCount;
1678
1679     /* and merge in the private acl cache info, if this is more than the public
1680      * info; merge in the public stuff in any case.
1681      */
1682     scp->anyAccess = statusp->AnonymousAccess;
1683
1684     if (userp != NULL) {
1685         cm_AddACLCache(scp, userp, statusp->CallerAccess);
1686     }
1687
1688     if (scp->dataVersion != 0 &&
1689         (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1690          (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1691         /* 
1692          * We now know that all of the data buffers that we have associated
1693          * with this scp are invalid.  Subsequent operations will go faster
1694          * if the buffers are removed from the hash tables.
1695          *
1696          * We do not remove directory buffers if the dataVersion delta is 1 because
1697          * those version numbers will be updated as part of the directory operation.
1698          *
1699          * We do not remove storedata buffers because they will still be valid.
1700          */
1701         int i, j;
1702         cm_buf_t **lbpp;
1703         cm_buf_t *tbp;
1704         cm_buf_t *bp, *prevBp, *nextBp;
1705
1706         lock_ObtainWrite(&buf_globalLock);
1707         i = BUF_FILEHASH(&scp->fid);
1708         for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1709         {
1710             nextBp = bp->fileHashp;
1711             /* 
1712              * if the buffer belongs to this stat cache entry
1713              * and the buffer mutex can be obtained, check the
1714              * reference count and if it is zero, remove the buffer
1715              * from the hash tables.  If there are references,
1716              * the buffer might be updated to the current version
1717              * so leave it in place.
1718              */
1719             if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1720                  lock_TryMutex(&bp->mx)) {
1721                 if (bp->refCount == 0 && 
1722                     !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1723                     prevBp = bp->fileHashBackp;
1724                     bp->fileHashBackp = bp->fileHashp = NULL;
1725                     if (prevBp)
1726                         prevBp->fileHashp = nextBp;
1727                     else
1728                         cm_data.buf_fileHashTablepp[i] = nextBp;
1729                     if (nextBp)
1730                         nextBp->fileHashBackp = prevBp;
1731
1732                     j = BUF_HASH(&bp->fid, &bp->offset);
1733                     lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1734                     for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1735                         if (tbp == bp) 
1736                             break;
1737                     }
1738
1739                     *lbpp = bp->hashp;  /* hash out */
1740                     bp->hashp = NULL;
1741
1742                     bp->qFlags &= ~CM_BUF_QINHASH;
1743                 }
1744                 lock_ReleaseMutex(&bp->mx);
1745             }
1746         }
1747         lock_ReleaseWrite(&buf_globalLock);
1748     }
1749
1750     /*
1751      * If the dataVersion has changed, the mountPointStringp must be cleared
1752      * in order to force a re-evaluation by cm_HandleLink().  The Windows CM
1753      * does not update a mountpoint or symlink by altering the contents of
1754      * the file data; but the Unix CM does.
1755      */
1756     if (scp->dataVersion != dataVersion)
1757         scp->mountPointStringp[0] = '\0';
1758
1759     /* We maintain a range of buffer dataVersion values which are considered 
1760      * valid.  This avoids the need to update the dataVersion on each buffer
1761      * object during an uncontested storeData operation.  As a result this 
1762      * merge status no longer has performance characteristics derived from
1763      * the size of the file.
1764      */
1765     if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) || 
1766          (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1767          scp->bufDataVersionLow == 0)
1768         scp->bufDataVersionLow = dataVersion;
1769     
1770     scp->dataVersion = dataVersion;
1771
1772     /* 
1773      * If someone is waiting for status information, we can wake them up
1774      * now even though the entity that issued the FetchStatus may not 
1775      * have completed yet.
1776      */
1777     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1778
1779     /*
1780      * We just successfully merged status on the stat cache object.
1781      * This means that the associated volume must be online.
1782      */
1783     if (!volp) {
1784         if (!cellp)
1785             cellp = cm_FindCellByID(scp->fid.cell, 0);
1786         cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1787     }
1788     if (volp) {
1789         cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1790         if (statep->state != vl_online) {
1791             lock_ObtainWrite(&volp->rw);
1792             cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1793             statep->state = vl_online;
1794             lock_ReleaseWrite(&volp->rw);
1795         }
1796     }
1797   done:
1798     if (volp)
1799         cm_PutVolume(volp);
1800
1801 }
1802
1803 /* note that our stat cache info is incorrect, so force us eventually
1804  * to stat the file again.  There may be dirty data associated with
1805  * this vnode, and we want to preserve that information.
1806  *
1807  * This function works by simply simulating a loss of the callback.
1808  *
1809  * This function must be called with the scache locked.
1810  */
1811 void cm_DiscardSCache(cm_scache_t *scp)
1812 {
1813     lock_AssertWrite(&scp->rw);
1814     if (scp->cbServerp) {
1815         cm_PutServer(scp->cbServerp);
1816         scp->cbServerp = NULL;
1817     }
1818     scp->cbExpires = 0;
1819     scp->volumeCreationDate = 0;
1820     scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
1821     cm_dnlcPurgedp(scp);
1822     cm_dnlcPurgevp(scp);
1823     cm_FreeAllACLEnts(scp);
1824
1825     if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1826         cm_VolStatus_Invalidate_DFS_Mapping(scp);
1827
1828     /* Force mount points and symlinks to be re-evaluated */
1829     scp->mountPointStringp[0] = '\0';
1830 }
1831
1832 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1833 {
1834     afsFidp->Volume = fidp->volume;
1835     afsFidp->Vnode = fidp->vnode;
1836     afsFidp->Unique = fidp->unique;
1837 }       
1838
1839 #ifdef DEBUG_REFCOUNT
1840 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1841 #else
1842 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1843 #endif
1844 {     
1845     afs_int32 refCount;
1846
1847     osi_assertx(scp != NULL, "null cm_scache_t");
1848     lock_AssertAny(&cm_scacheLock);
1849     refCount = InterlockedIncrement(&scp->refCount);
1850 #ifdef DEBUG_REFCOUNT
1851     osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1852     afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1853 #endif
1854 }
1855
1856 #ifdef DEBUG_REFCOUNT
1857 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1858 #else
1859 void cm_HoldSCache(cm_scache_t *scp)
1860 #endif
1861 {
1862     afs_int32 refCount;
1863
1864     osi_assertx(scp != NULL, "null cm_scache_t");
1865     lock_ObtainRead(&cm_scacheLock);
1866     refCount = InterlockedIncrement(&scp->refCount);
1867 #ifdef DEBUG_REFCOUNT
1868     osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1869     afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1870 #endif
1871     lock_ReleaseRead(&cm_scacheLock);
1872 }
1873
1874 #ifdef DEBUG_REFCOUNT
1875 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1876 #else
1877 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1878 #endif
1879 {
1880     afs_int32 refCount;
1881
1882     osi_assertx(scp != NULL, "null cm_scache_t");
1883     lock_AssertAny(&cm_scacheLock);
1884
1885     refCount = InterlockedDecrement(&scp->refCount);
1886 #ifdef DEBUG_REFCOUNT
1887     if (refCount < 0)
1888         osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1889 #endif
1890     osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1891 #ifdef DEBUG_REFCOUNT
1892     osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1893     afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1894 #endif
1895
1896     if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1897         int deleted = 0;
1898         long      lockstate;
1899
1900         lockstate = lock_GetRWLockState(&cm_scacheLock);
1901         if (lockstate != OSI_RWLOCK_WRITEHELD) 
1902             lock_ReleaseRead(&cm_scacheLock);
1903         else
1904             lock_ReleaseWrite(&cm_scacheLock);
1905
1906         lock_ObtainWrite(&scp->rw);
1907         if (scp->flags & CM_SCACHEFLAG_DELETED)
1908             deleted = 1;
1909
1910         if (refCount == 0 && deleted) {
1911             lock_ObtainWrite(&cm_scacheLock);
1912             cm_RecycleSCache(scp, 0);
1913             if (lockstate != OSI_RWLOCK_WRITEHELD) 
1914                 lock_ConvertWToR(&cm_scacheLock);
1915         } else {
1916             if (lockstate != OSI_RWLOCK_WRITEHELD) 
1917                 lock_ObtainRead(&cm_scacheLock);
1918             else
1919                 lock_ObtainWrite(&cm_scacheLock);
1920         }
1921         lock_ReleaseWrite(&scp->rw);
1922     }
1923 }
1924
1925 #ifdef DEBUG_REFCOUNT
1926 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1927 #else
1928 void cm_ReleaseSCache(cm_scache_t *scp)
1929 #endif
1930 {     
1931     afs_int32 refCount;
1932
1933     osi_assertx(scp != NULL, "null cm_scache_t");
1934     lock_ObtainRead(&cm_scacheLock);
1935     refCount = InterlockedDecrement(&scp->refCount);
1936 #ifdef DEBUG_REFCOUNT
1937     if (refCount < 0)
1938         osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1939 #endif
1940     osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1941 #ifdef DEBUG_REFCOUNT
1942     osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1943     afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1944 #endif
1945     lock_ReleaseRead(&cm_scacheLock);
1946
1947     if (scp->flags & CM_SCACHEFLAG_DELETED) {
1948         int deleted = 0;
1949         lock_ObtainWrite(&scp->rw);
1950         if (scp->flags & CM_SCACHEFLAG_DELETED)
1951             deleted = 1;
1952         if (deleted) {
1953             lock_ObtainWrite(&cm_scacheLock);
1954             cm_RecycleSCache(scp, 0);
1955             lock_ReleaseWrite(&cm_scacheLock);
1956         }
1957         lock_ReleaseWrite(&scp->rw);
1958     }
1959 }
1960
1961 /* just look for the scp entry to get filetype */
1962 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1963 int cm_FindFileType(cm_fid_t *fidp)
1964 {
1965     long hash;
1966     cm_scache_t *scp;
1967         
1968     hash = CM_SCACHE_HASH(fidp);
1969         
1970     osi_assertx(fidp->cell != 0, "unassigned cell value");
1971
1972     lock_ObtainWrite(&cm_scacheLock);
1973     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1974         if (cm_FidCmp(fidp, &scp->fid) == 0) {
1975             lock_ReleaseWrite(&cm_scacheLock);
1976             return scp->fileType;
1977         }
1978     }
1979     lock_ReleaseWrite(&cm_scacheLock);
1980     return 0;
1981 }
1982
1983 /* dump all scp's that have reference count > 0 to a file. 
1984  * cookie is used to identify this batch for easy parsing, 
1985  * and it a string provided by a caller 
1986  */
1987 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1988 {
1989     int zilch;
1990     cm_scache_t *scp;
1991     osi_queue_t *q;
1992     char output[2048];
1993     int i;
1994   
1995     if (lock)
1996         lock_ObtainRead(&cm_scacheLock);
1997   
1998     sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1999     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2000   
2001     for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp) 
2002     {
2003         time_t t;
2004         char *srvStr = NULL;
2005         afs_uint32 srvStrRpc = TRUE;
2006         char *cbt = NULL;
2007         char *cdrot = NULL;
2008
2009         if (scp->cbServerp) {
2010             if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2011                 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2012                 afs_asprintf(&srvStr, "%.0I", scp->cbServerp->addr.sin_addr.s_addr);
2013                 srvStrRpc = FALSE;
2014             }
2015         }
2016         if (scp->cbExpires) {
2017             t = scp->cbExpires;
2018             cbt = ctime(&t);
2019             if (cbt) {
2020                 cbt = strdup(cbt);
2021                 cbt[strlen(cbt)-1] = '\0';
2022             }
2023         }
2024         if (scp->volumeCreationDate) {
2025             t = scp->volumeCreationDate;
2026             cdrot = ctime(&t);
2027             if (cdrot) {
2028                 cdrot = strdup(cdrot);
2029                 cdrot[strlen(cdrot)-1] = '\0';
2030             }
2031         }
2032         sprintf(output,
2033                 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2034                 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2035                 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2036                 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2037                 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2038                 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2039                 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2040                 cdrot ? cdrot : "<none>", scp->refCount);
2041         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2042
2043         if (scp->fileLocksH) {
2044             sprintf(output, "  %s - begin dumping scp locks\r\n", cookie);
2045             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2046
2047             for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2048                 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2049                 sprintf(output, "  %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2050                         "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2051                         cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2052                         lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2053                 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2054             }       
2055
2056             sprintf(output, "  %s - done dumping scp locks\r\n", cookie);
2057             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2058         }
2059
2060         if (srvStr) {
2061             if (srvStrRpc)
2062                 RpcStringFree(&srvStr);
2063             else
2064                 free(srvStr);
2065         }
2066         if (cbt)
2067             free(cbt);
2068         if (cdrot)
2069             free(cdrot);
2070     }
2071   
2072     sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2073     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2074     sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2075             cookie, cm_data.scacheHashTableSize);
2076     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2077   
2078     for (i = 0; i < cm_data.scacheHashTableSize; i++)
2079     {
2080         for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp) 
2081         {
2082             sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n", 
2083                     cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2084             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2085         }
2086     }
2087
2088     sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2089     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2090
2091     sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2092     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2093
2094     for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2095         cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2096         sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n", 
2097                  cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length, 
2098                  lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2099         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2100     }       
2101
2102     sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2103     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2104
2105     if (lock)
2106         lock_ReleaseRead(&cm_scacheLock);       
2107     return (0);     
2108 }
2109