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