windows-volume-status-tracking-20070610
[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
23 /*extern void afsi_log(char *pattern, ...);*/
24
25 extern osi_hyper_t hzero;
26
27 /* File locks */
28 osi_queue_t *cm_allFileLocks;
29 osi_queue_t *cm_freeFileLocks;
30 unsigned long cm_lockRefreshCycle;
31
32 /* lock for globals */
33 osi_rwlock_t cm_scacheLock;
34
35 /* Dummy scache entry for use with pioctl fids */
36 cm_scache_t cm_fakeSCache;
37
38 #ifdef AFS_FREELANCE_CLIENT
39 extern osi_mutex_t cm_Freelance_Lock;
40 #endif
41
42 /* must be called with cm_scacheLock write-locked! */
43 void cm_AdjustScacheLRU(cm_scache_t *scp)
44 {
45     if (scp == cm_data.scacheLRULastp)
46         cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
47     osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
48     osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
49     if (!cm_data.scacheLRULastp) 
50         cm_data.scacheLRULastp = scp;
51 }
52
53 /* call with scache write-locked and mutex held */
54 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
55 {
56     cm_scache_t **lscpp;
57     cm_scache_t *tscp;
58     int i;
59         
60     if (scp->flags & CM_SCACHEFLAG_INHASH) {
61         /* hash it out first */
62         i = CM_SCACHE_HASH(&scp->fid);
63         for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
64              tscp;
65              lscpp = &tscp->nextp, tscp = tscp->nextp) {
66             if (tscp == scp) {
67                 *lscpp = scp->nextp;
68                 scp->flags &= ~CM_SCACHEFLAG_INHASH;
69                 break;
70             }
71         }
72     }
73 }
74
75 /* called with cm_scacheLock write-locked; recycles an existing scp. 
76  *
77  * this function ignores all of the locking hierarchy.  
78  */
79 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
80 {
81     if (scp->refCount != 0) {
82         return -1;
83     }
84
85     if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
86         osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
87 #ifdef DEBUG
88         osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
89 #endif
90         return -1;
91     }
92
93     cm_RemoveSCacheFromHashTable(scp);
94
95 #if 0
96     if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
97         osi_queueData_t *qdp;
98         cm_buf_t *bufp;
99
100         while(qdp = scp->bufWritesp) {
101             bufp = osi_GetQData(qdp);
102             osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
103             osi_QDFree(qdp);
104             if (bufp) {
105                 lock_ObtainMutex(&bufp->mx);
106                 bufp->cmFlags &= ~CM_BUF_CMSTORING;
107                 bufp->flags &= ~CM_BUF_DIRTY;
108                 bufp->flags |= CM_BUF_ERROR;
109                 bufp->error = VNOVNODE;
110                 bufp->dataVersion = -1; /* bad */
111                 bufp->dirtyCounter++;
112                 if (bufp->flags & CM_BUF_WAITING) {
113                     osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
114                     osi_Wakeup((long) &bufp);
115                 }
116                 lock_ReleaseMutex(&bufp->mx);
117                 buf_Release(bufp);
118             }
119         }
120         while(qdp = scp->bufReadsp) {
121             bufp = osi_GetQData(qdp);
122             osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
123             osi_QDFree(qdp);
124             if (bufp) {
125                 lock_ObtainMutex(&bufp->mx);
126                 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
127                 bufp->flags &= ~CM_BUF_DIRTY;
128                 bufp->flags |= CM_BUF_ERROR;
129                 bufp->error = VNOVNODE;
130                 bufp->dataVersion = -1; /* bad */
131                 bufp->dirtyCounter++;
132                 if (bufp->flags & CM_BUF_WAITING) {
133                     osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
134                     osi_Wakeup((long) &bufp);
135                 }
136                 lock_ReleaseMutex(&bufp->mx);
137                 buf_Release(bufp);
138             }
139         }
140         buf_CleanDirtyBuffers(scp); 
141     } else {
142         /* look for things that shouldn't still be set */
143         osi_assert(scp->bufWritesp == NULL);
144         osi_assert(scp->bufReadsp == NULL);
145     }
146 #endif
147
148     /* invalidate so next merge works fine;
149      * also initialize some flags */
150     scp->fileType = 0;
151     scp->flags &= ~(CM_SCACHEFLAG_STATD
152                      | CM_SCACHEFLAG_DELETED
153                      | CM_SCACHEFLAG_RO
154                      | CM_SCACHEFLAG_PURERO
155                      | CM_SCACHEFLAG_OVERQUOTA
156                      | CM_SCACHEFLAG_OUTOFSPACE
157                      | CM_SCACHEFLAG_EACCESS);
158     scp->serverModTime = 0;
159     scp->dataVersion = 0;
160     scp->bulkStatProgress = hzero;
161     scp->waitCount = 0;
162
163     scp->fid.vnode = 0;
164     scp->fid.volume = 0;
165     scp->fid.unique = 0;
166     scp->fid.cell = 0;
167
168     /* discard callback */
169     if (scp->cbServerp) {
170         cm_PutServer(scp->cbServerp);
171         scp->cbServerp = NULL;
172     }
173     scp->cbExpires = 0;
174
175     /* remove from dnlc */
176     cm_dnlcPurgedp(scp);
177     cm_dnlcPurgevp(scp);
178
179     /* discard cached status; if non-zero, Close
180      * tried to store this to server but failed */
181     scp->mask = 0;
182
183     /* drop held volume ref */
184     if (scp->volp) {
185         cm_PutVolume(scp->volp);
186         scp->volp = NULL;
187     }
188
189     /* discard symlink info */
190     scp->mountPointStringp[0] = '\0';
191     memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
192     memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
193
194     /* reset locking info */
195     scp->fileLocksH = NULL;
196     scp->fileLocksT = NULL;
197     scp->serverLock = (-1);
198     scp->exclusiveLocks = 0;
199     scp->sharedLocks = 0;
200
201     /* not locked, but there can be no references to this guy
202      * while we hold the global refcount lock.
203      */
204     cm_FreeAllACLEnts(scp);
205     return 0;
206 }
207
208
209 /* called with cm_scacheLock write-locked; find a vnode to recycle.
210  * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
211  */
212 cm_scache_t *cm_GetNewSCache(void)
213 {
214     cm_scache_t *scp;
215     int retry = 0;
216
217 #if 0
218     /* first pass - look for deleted objects */
219     for ( scp = cm_data.scacheLRULastp;
220           scp;
221           scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
222     {
223         osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
224
225         if (scp->refCount == 0) {
226             if (scp->flags & CM_SCACHEFLAG_DELETED) {
227                 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
228                 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
229
230                     /* we found an entry, so return it */
231                     /* now remove from the LRU queue and put it back at the
232                      * head of the LRU queue.
233                      */
234                     cm_AdjustScacheLRU(scp);
235
236                     /* and we're done */
237                     return scp;
238                 } 
239                 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
240             } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
241                 /* we found an entry, so return it */
242                 /* now remove from the LRU queue and put it back at the
243                 * head of the LRU queue.
244                 */
245                 cm_AdjustScacheLRU(scp);
246
247                 /* and we're done */
248                 return scp;
249             }
250         }       
251     }   
252     osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
253 #endif 
254
255     if (cm_data.currentSCaches >= cm_data.maxSCaches) {
256         /* There were no deleted scache objects that we could use.  Try to find
257          * one that simply hasn't been used in a while.
258          */
259         for ( scp = cm_data.scacheLRULastp;
260               scp;
261               scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
262         {
263             /* It is possible for the refCount to be zero and for there still
264              * to be outstanding dirty buffers.  If there are dirty buffers,
265              * we must not recycle the scp. */
266             if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
267                 if (!buf_DirtyBuffersExist(&scp->fid)) {
268                     if (!cm_RecycleSCache(scp, 0)) {
269                         /* we found an entry, so return it */
270                         /* now remove from the LRU queue and put it back at the
271                          * head of the LRU queue.
272                          */
273                         cm_AdjustScacheLRU(scp);
274
275                         /* and we're done */
276                         return scp;
277                     }
278                 } else {
279                     osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
280                 }
281             }   
282         }
283         osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
284
285         return NULL;
286     }
287         
288     /* if we get here, we should allocate a new scache entry.  We either are below
289      * quota or we have a leak and need to allocate a new one to avoid panicing.
290      */
291     scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
292     osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
293     memset(scp, 0, sizeof(cm_scache_t));
294     scp->magic = CM_SCACHE_MAGIC;
295     lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
296     lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
297     scp->serverLock = -1;
298
299     /* and put it in the LRU queue */
300     osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
301     if (!cm_data.scacheLRULastp) 
302         cm_data.scacheLRULastp = scp;
303     cm_data.currentSCaches++;
304     cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
305     cm_dnlcPurgevp(scp); 
306     scp->allNextp = cm_data.allSCachesp;
307     cm_data.allSCachesp = scp;
308     return scp;
309 }       
310
311 /* like strcmp, only for fids */
312 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
313 {
314     if (ap->vnode != bp->vnode) 
315         return 1;
316     if (ap->volume != bp->volume) 
317         return 1;
318     if (ap->unique != bp->unique) 
319         return 1;
320     if (ap->cell != bp->cell) 
321         return 1;
322     return 0;
323 }
324
325 void cm_fakeSCacheInit(int newFile)
326 {
327     if ( newFile ) {
328         memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
329         cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
330         /* can leave clientModTime at 0 */
331         cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
332         cm_data.fakeSCache.unixModeBits = 0777;
333         cm_data.fakeSCache.length.LowPart = 1000;
334         cm_data.fakeSCache.linkCount = 1;
335         cm_data.fakeSCache.refCount = 1;
336     }
337     lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
338 }
339
340 long
341 cm_ValidateSCache(void)
342 {
343     cm_scache_t * scp, *lscp;
344     long i;
345
346     if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
347          cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
348         afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
349         fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
350         return -17;
351     }
352
353     for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0; 
354           scp;
355           lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
356         if (scp->magic != CM_SCACHE_MAGIC) {
357             afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
358             fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
359             return -1;
360         }
361         if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
362             afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
363             fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
364             return -2;
365         }
366         if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
367             afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
368             fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
369             return -3;
370         }
371         if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
372             afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
373             fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
374             return -4;
375         }
376         if (i > cm_data.currentSCaches ) {
377             afsi_log("cm_ValidateSCache failure: LRU First queue loops");
378             fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
379             return -13;
380         }
381         if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
382             afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
383             fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
384             return -15;
385         }
386     }
387
388     for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
389           lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
390         if (scp->magic != CM_SCACHE_MAGIC) {
391             afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
392             fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
393             return -5;
394         }
395         if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
396             afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
397             fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
398             return -6;
399         }
400         if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
401             afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
402             fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
403             return -7;
404         }
405         if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
406             afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
407             fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
408             return -8;
409         }
410         if (i > cm_data.currentSCaches ) {
411             afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
412             fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
413             return -14;
414         }
415         if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
416             afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
417             fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
418             return -16;
419         }
420     }
421
422     for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
423         for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
424             if (scp->magic != CM_SCACHE_MAGIC) {
425                 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
426                 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
427                 return -9;
428             }
429             if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
430                 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
431                 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
432                 return -10;
433             }
434             if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
435                 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
436                 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
437                 return -11;
438             }
439             if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
440                 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
441                 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
442                 return -12;
443             }
444         }
445     }
446
447     return cm_dnlcValidate();
448 }
449
450 long
451 cm_ShutdownSCache(void)
452 {
453     cm_scache_t * scp;
454
455     for ( scp = cm_data.allSCachesp; scp;
456           scp = scp->allNextp ) {
457         if (scp->randomACLp) {
458             lock_ObtainMutex(&scp->mx);
459             cm_FreeAllACLEnts(scp);
460             lock_ReleaseMutex(&scp->mx);
461         }
462         lock_FinalizeMutex(&scp->mx);
463         lock_FinalizeRWLock(&scp->bufCreateLock);
464     }
465
466     return cm_dnlcShutdown();
467 }
468
469 void cm_InitSCache(int newFile, long maxSCaches)
470 {
471     static osi_once_t once;
472         
473     if (osi_Once(&once)) {
474         lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
475         if ( newFile ) {
476             memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
477             cm_data.allSCachesp = NULL;
478             cm_data.currentSCaches = 0;
479             cm_data.maxSCaches = maxSCaches;
480             cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
481         } else {
482             cm_scache_t * scp;
483
484             for ( scp = cm_data.allSCachesp; scp;
485                   scp = scp->allNextp ) {
486                 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
487                 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
488
489                 scp->cbServerp = NULL;
490                 scp->cbExpires = 0;
491                 scp->fileLocksH = NULL;
492                 scp->fileLocksT = NULL;
493                 scp->serverLock = (-1);
494                 scp->lastRefreshCycle = 0;
495                 scp->exclusiveLocks = 0;
496                 scp->sharedLocks = 0;
497                 scp->openReads = 0;
498                 scp->openWrites = 0;
499                 scp->openShares = 0;
500                 scp->openExcls = 0;
501                 scp->waitCount = 0;
502                 scp->flags &= ~CM_SCACHEFLAG_WAITING;
503             }
504         }
505         cm_allFileLocks = NULL;
506         cm_freeFileLocks = NULL;
507         cm_lockRefreshCycle = 0;
508         cm_fakeSCacheInit(newFile);
509         cm_dnlcInit(newFile);
510         osi_EndOnce(&once);
511     }
512 }
513
514 /* version that doesn't bother creating the entry if we don't find it */
515 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
516 {
517     long hash;
518     cm_scache_t *scp;
519
520     hash = CM_SCACHE_HASH(fidp);
521
522     if (fidp->cell == 0) {
523         return NULL;
524     }
525
526     lock_ObtainWrite(&cm_scacheLock);
527     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
528         if (cm_FidCmp(fidp, &scp->fid) == 0) {
529             cm_HoldSCacheNoLock(scp);
530             cm_AdjustScacheLRU(scp);
531             lock_ReleaseWrite(&cm_scacheLock);
532             return scp;
533         }
534     }
535     lock_ReleaseWrite(&cm_scacheLock);
536     return NULL;
537 }
538
539 #ifdef DEBUG_REFCOUNT
540 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
541                   cm_req_t *reqp, char * file, long line)
542 #else
543 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
544                   cm_req_t *reqp)
545 #endif
546 {
547     long hash;
548     cm_scache_t *scp;
549     long code;
550     cm_volume_t *volp = NULL;
551     cm_cell_t *cellp;
552     char* mp = NULL;
553     int special; // yj: boolean variable to test if file is on root.afs
554     int isRoot;
555     extern cm_fid_t cm_rootFid;
556         
557     hash = CM_SCACHE_HASH(fidp);
558         
559     osi_assert(fidp->cell != 0);
560
561     if (fidp->cell== cm_data.rootFid.cell && 
562          fidp->volume==cm_data.rootFid.volume &&
563          fidp->vnode==0x0 && fidp->unique==0x0)
564     {
565         osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
566     }
567
568     // yj: check if we have the scp, if so, we don't need
569     // to do anything else
570     lock_ObtainWrite(&cm_scacheLock);
571     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
572         if (cm_FidCmp(fidp, &scp->fid) == 0) {
573 #ifdef DEBUG_REFCOUNT
574             afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
575             osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
576 #endif
577             cm_HoldSCacheNoLock(scp);
578             *outScpp = scp;
579             cm_AdjustScacheLRU(scp);
580             lock_ReleaseWrite(&cm_scacheLock);
581             return 0;
582         }
583     }
584
585     // yj: when we get here, it means we don't have an scp
586     // so we need to either load it or fake it, depending
587     // on whether the file is "special", see below.
588
589     // yj: if we're trying to get an scp for a file that's
590     // on root.afs of homecell, we want to handle it specially
591     // because we have to fill in the status stuff 'coz we
592     // don't want trybulkstat to fill it in for us
593 #ifdef AFS_FREELANCE_CLIENT
594     special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
595                fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
596                !(fidp->vnode==0x1 && fidp->unique==0x1));
597     isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
598               fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
599               fidp->vnode==0x1 && fidp->unique==0x1);
600     if (cm_freelanceEnabled && isRoot) {
601         osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
602         /* freelance: if we are trying to get the root scp for the first
603          * time, we will just put in a place holder entry. 
604          */
605         volp = NULL;
606     }
607           
608     if (cm_freelanceEnabled && special) {
609         osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
610         if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
611             lock_ObtainMutex(&cm_Freelance_Lock);
612             mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
613             lock_ReleaseMutex(&cm_Freelance_Lock);
614         } else {
615             mp = "";
616         }
617         scp = cm_GetNewSCache();
618         if (scp == NULL) {
619             osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
620             lock_ReleaseWrite(&cm_scacheLock);
621             return CM_ERROR_WOULDBLOCK;
622         }
623
624 #if not_too_dangerous
625         /* dropping the cm_scacheLock allows more than one thread
626          * to obtain the same cm_scache_t from the LRU list.  Since
627          * the refCount is known to be zero at this point we have to
628          * assume that no one else is using the one this is returned.
629          */
630         lock_ReleaseWrite(&cm_scacheLock);
631         lock_ObtainMutex(&scp->mx);
632         lock_ObtainWrite(&cm_scacheLock);
633 #endif
634         scp->fid = *fidp;
635         scp->volp = cm_data.rootSCachep->volp;
636         cm_GetVolume(scp->volp);        /* grab an additional reference */
637         scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
638         scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
639         scp->dotdotFid.unique=1;
640         scp->dotdotFid.vnode=1;
641         scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
642         scp->nextp=cm_data.scacheHashTablep[hash];
643         cm_data.scacheHashTablep[hash]=scp;
644         scp->flags |= CM_SCACHEFLAG_INHASH;
645         scp->refCount = 1;
646         osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
647         if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
648             scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
649         else 
650             scp->fileType = CM_SCACHETYPE_INVALID;
651
652         lock_ObtainMutex(&cm_Freelance_Lock);
653         scp->length.LowPart = (DWORD)strlen(mp)+4;
654         scp->length.HighPart = 0;
655         strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
656         scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
657         lock_ReleaseMutex(&cm_Freelance_Lock);
658
659         scp->owner=0x0;
660         scp->unixModeBits=0777;
661         scp->clientModTime=FakeFreelanceModTime;
662         scp->serverModTime=FakeFreelanceModTime;
663         scp->parentUnique = 0x1;
664         scp->parentVnode=0x1;
665         scp->group=0;
666         scp->dataVersion=cm_data.fakeDirVersion;
667         scp->lockDataVersion=-1; /* no lock yet */
668 #if not_too_dangerous
669         lock_ReleaseMutex(&scp->mx);
670 #endif
671         *outScpp = scp;
672         lock_ReleaseWrite(&cm_scacheLock);
673 #ifdef DEBUG_REFCOUNT
674         afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
675         osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
676 #endif
677         return 0;
678     }
679     // end of yj code
680 #endif /* AFS_FREELANCE_CLIENT */
681
682     /* otherwise, we need to find the volume */
683     if (!cm_freelanceEnabled || !isRoot) {
684         lock_ReleaseWrite(&cm_scacheLock);      /* for perf. reasons */
685         cellp = cm_FindCellByID(fidp->cell);
686         if (!cellp) 
687             return CM_ERROR_NOSUCHCELL;
688
689         code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
690         if (code) 
691             return code;
692         lock_ObtainWrite(&cm_scacheLock);
693     }
694         
695     /* otherwise, we have the volume, now reverify that the scp doesn't
696      * exist, and proceed.
697      */
698     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
699         if (cm_FidCmp(fidp, &scp->fid) == 0) {
700 #ifdef DEBUG_REFCOUNT
701             afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
702             osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
703 #endif
704             cm_HoldSCacheNoLock(scp);
705             osi_assert(scp->volp == volp);
706             cm_AdjustScacheLRU(scp);
707             lock_ReleaseWrite(&cm_scacheLock);
708             if (volp)
709                 cm_PutVolume(volp);
710             *outScpp = scp;
711             return 0;
712         }
713     }
714         
715     /* now, if we don't have the fid, recycle something */
716     scp = cm_GetNewSCache();
717     if (scp == NULL) {
718         osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
719         lock_ReleaseWrite(&cm_scacheLock);
720         if (volp)
721             cm_PutVolume(volp);
722         return CM_ERROR_WOULDBLOCK;
723     }
724     osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
725
726     osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
727
728 #if not_too_dangerous
729     /* dropping the cm_scacheLock allows more than one thread
730      * to obtain the same cm_scache_t from the LRU list.  Since
731      * the refCount is known to be zero at this point we have to
732      * assume that no one else is using the one this is returned.
733      */
734     lock_ReleaseWrite(&cm_scacheLock);
735     lock_ObtainMutex(&scp->mx);
736     lock_ObtainWrite(&cm_scacheLock);
737 #endif
738     scp->fid = *fidp;
739     scp->volp = volp;   /* a held reference */
740
741     if (!cm_freelanceEnabled || !isRoot) {
742         /* if this scache entry represents a volume root then we need 
743          * to copy the dotdotFipd from the volume structure where the 
744          * "master" copy is stored (defect 11489)
745          */
746         if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
747             scp->dotdotFid = volp->dotdotFid;
748         }
749           
750         if (volp->ro.ID == fidp->volume)
751             scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
752         else if (volp->bk.ID == fidp->volume)
753             scp->flags |= CM_SCACHEFLAG_RO;
754     }
755     scp->nextp = cm_data.scacheHashTablep[hash];
756     cm_data.scacheHashTablep[hash] = scp;
757     scp->flags |= CM_SCACHEFLAG_INHASH;
758     scp->refCount = 1;
759     osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
760 #if not_too_dangerous
761     lock_ReleaseMutex(&scp->mx);
762 #endif
763
764     /* XXX - The following fields in the cm_scache are 
765      * uninitialized:
766      *   fileType
767      *   parentVnode
768      *   parentUnique
769      */
770     lock_ReleaseWrite(&cm_scacheLock);
771         
772     /* now we have a held scache entry; just return it */
773     *outScpp = scp;
774 #ifdef DEBUG_REFCOUNT
775     afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
776     osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
777 #endif
778     return 0;
779 }
780
781 /* Returns a held reference to the scache's parent 
782  * if it exists */
783 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
784 {
785     long code = 0;
786     int i;
787     cm_fid_t    parent_fid;
788     cm_scache_t * pscp = NULL;
789
790     lock_ObtainRead(&cm_scacheLock);
791     parent_fid = scp->fid;
792     parent_fid.vnode = scp->parentVnode;
793     parent_fid.unique = scp->parentUnique;
794
795     if (cm_FidCmp(&scp->fid, &parent_fid)) {
796         i = CM_SCACHE_HASH(&parent_fid);
797         for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
798             if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
799                 cm_HoldSCacheNoLock(pscp);
800                 break;
801             }
802         }
803     }
804
805     lock_ReleaseRead(&cm_scacheLock);
806
807     return pscp;
808 }
809
810 /* synchronize a fetch, store, read, write, fetch status or store status.
811  * Called with scache mutex held, and returns with it held, but temporarily
812  * drops it during the fetch.
813  * 
814  * At most one flag can be on in flags, if this is an RPC request.
815  *
816  * Also, if we're fetching or storing data, we must ensure that we have a buffer.
817  *
818  * There are a lot of weird restrictions here; here's an attempt to explain the
819  * rationale for the concurrency restrictions implemented in this function.
820  *
821  * First, although the file server will break callbacks when *another* machine
822  * modifies a file or status block, the client itself is responsible for
823  * concurrency control on its own requests.  Callback breaking events are rare,
824  * and simply invalidate any concurrent new status info.
825  *
826  * In the absence of callback breaking messages, we need to know how to
827  * synchronize incoming responses describing updates to files.  We synchronize
828  * operations that update the data version by comparing the data versions.
829  * However, updates that do not update the data, but only the status, can't be
830  * synchronized with fetches or stores, since there's nothing to compare
831  * to tell which operation executed first at the server.
832  *
833  * Thus, we can allow multiple ops that change file data, or dir data, and
834  * fetches.  However, status storing ops have to be done serially.
835  *
836  * Furthermore, certain data-changing ops are incompatible: we can't read or
837  * write a buffer while doing a truncate.  We can't read and write the same
838  * buffer at the same time, or write while fetching or storing, or read while
839  * fetching a buffer (this may change).  We can't fetch and store at the same
840  * time, either.
841  *
842  * With respect to status, we can't read and write at the same time, read while
843  * fetching, write while fetching or storing, or fetch and store at the same time.
844  *
845  * We can't allow a get callback RPC to run in concurrently with something that
846  * will return updated status, since we could start a call, have the server
847  * return status, have another machine make an update to the status (which
848  * doesn't change serverModTime), have the original machine get a new callback,
849  * and then have the original machine merge in the early, old info from the
850  * first call.  At this point, the easiest way to avoid this problem is to have
851  * getcallback calls conflict with all others for the same vnode.  Other calls
852  * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
853  * vnode must be careful not to merge in their status unless they have obtained
854  * a callback from the start of their call.
855  *
856  * Note added 1/23/96
857  * Concurrent StoreData RPC's can cause trouble if the file is being extended.
858  * Each such RPC passes a FileLength parameter, which the server uses to do
859  * pre-truncation if necessary.  So if two RPC's are processed out of order at
860  * the server, the one with the smaller FileLength will be processed last,
861  * possibly resulting in a bogus truncation.  The simplest way to avoid this
862  * is to serialize all StoreData RPC's.  This is the reason we defined
863  * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
864  */
865 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
866                afs_uint32 rights, afs_uint32 flags)
867 {
868     osi_queueData_t *qdp;
869     long code;
870     cm_buf_t *tbufp;
871     afs_uint32 outRights;
872     int bufLocked;
873     afs_uint32 sleep_scp_flags = 0;
874     afs_uint32 sleep_buf_cmflags = 0;
875     afs_uint32 sleep_scp_bufs = 0;
876
877     /* lookup this first */
878     bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
879
880         if (bufp)
881                 osi_assert(bufp->refCount > 0);
882
883
884     /* Do the access check.  Now we don't really do the access check
885      * atomically, since the caller doesn't expect the parent dir to be
886      * returned locked, and that is what we'd have to do to prevent a
887      * callback breaking message on the parent due to a setacl call from
888      * being processed while we're running.  So, instead, we check things
889      * here, and if things look fine with the access, we proceed to finish
890      * the rest of this check.  Sort of a hack, but probably good enough.
891      */
892
893     while (1) {
894         if (flags & CM_SCACHESYNC_FETCHSTATUS) {
895             /* if we're bringing in a new status block, ensure that
896              * we aren't already doing so, and that no one is
897              * changing the status concurrently, either.  We need
898              * to do this, even if the status is of a different
899              * type, since we don't have the ability to figure out,
900              * in the AFS 3 protocols, which status-changing
901              * operation ran first, or even which order a read and
902              * a write occurred in.
903              */
904             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
905                                | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
906                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
907                 goto sleep;
908             }
909         }
910         if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
911                       | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
912             /* if we're going to make an RPC to change the status, make sure
913              * that no one is bringing in or sending out the status.
914              */
915             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
916                               CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
917                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
918                 goto sleep;
919             }
920             if (scp->bufReadsp || scp->bufWritesp) {
921                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
922                 goto sleep;
923             }
924         }
925         if (flags & CM_SCACHESYNC_FETCHDATA) {
926             /* if we're bringing in a new chunk of data, make sure that
927              * nothing is happening to that chunk, and that we aren't
928              * changing the basic file status info, either.
929              */
930             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
931                                | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
932                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
933                 goto sleep;
934             }
935             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
936                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
937                 goto sleep;
938             }
939         }
940         if (flags & CM_SCACHESYNC_STOREDATA) {
941             /* same as fetch data */
942             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
943                                | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
944                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
945                 goto sleep;
946             }
947             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
948                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp, bufp);
949                 goto sleep;
950             }
951         }
952
953         if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
954             /* Don't allow concurrent StoreData RPC's */
955             if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
956                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
957                 goto sleep;
958             }
959         }
960
961         if (flags & CM_SCACHESYNC_ASYNCSTORE) {
962             /* Don't allow more than one BKG store request */
963             if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
964                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
965                 goto sleep;
966             }
967         }
968
969         if (flags & CM_SCACHESYNC_LOCK) {
970             /* Don't allow concurrent fiddling with lock lists */
971             if (scp->flags & CM_SCACHEFLAG_LOCKING) {
972                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
973                 goto sleep;
974             }
975         }
976
977         /* now the operations that don't correspond to making RPCs */
978         if (flags & CM_SCACHESYNC_GETSTATUS) {
979             /* we can use the status that's here, if we're not
980              * bringing in new status.
981              */
982             if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
983                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
984                 goto sleep;
985             }
986         }
987         if (flags & CM_SCACHESYNC_SETSTATUS) {
988             /* we can make a change to the local status, as long as
989              * the status isn't changing now.
990              *
991              * If we're fetching or storing a chunk of data, we can
992              * change the status locally, since the fetch/store
993              * operations don't change any of the data that we're
994              * changing here.
995              */
996             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
997                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
998                 goto sleep;
999             }
1000         }
1001         if (flags & CM_SCACHESYNC_READ) {
1002             /* we're going to read the data, make sure that the
1003              * status is available, and that the data is here.  It
1004              * is OK to read while storing the data back.
1005              */
1006             if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1007                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1008                 goto sleep;
1009             }
1010             if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1011                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1012                 goto sleep;
1013             }
1014         }
1015         if (flags & CM_SCACHESYNC_WRITE) {
1016             /* don't write unless the status is stable and the chunk
1017              * is stable.
1018              */
1019             if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1020                                | CM_SCACHEFLAG_SIZESTORING)) {
1021                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1022                 goto sleep;
1023             }
1024             if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
1025                 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
1026                 goto sleep;
1027             }
1028         }
1029
1030         // yj: modified this so that callback only checked if we're
1031         // not checking something on /afs
1032         /* fix the conditional to match the one in cm_HaveCallback */
1033         if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1034 #ifdef AFS_FREELANCE_CLIENT
1035              && (!cm_freelanceEnabled || 
1036                   !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1037                   scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1038                   scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1039                   cm_fakeDirCallback < 2)
1040 #endif /* AFS_FREELANCE_CLIENT */
1041              ) {
1042             if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1043                 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1044                           scp);
1045                 if (bufLocked) 
1046                     lock_ReleaseMutex(&bufp->mx);
1047                 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1048                 if (bufLocked) {
1049                     lock_ReleaseMutex(&scp->mx);
1050                     lock_ObtainMutex(&bufp->mx);
1051                     lock_ObtainMutex(&scp->mx);
1052                 }
1053                 if (code) 
1054                     return code;
1055                 flags &= ~CM_SCACHESYNC_FORCECB;        /* only force once */
1056                 continue;
1057             }
1058         }
1059
1060         if (rights) {
1061             /* can't check access rights without a callback */
1062             osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1063
1064             if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1065                 return CM_ERROR_READONLY;
1066
1067             if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1068                 if (~outRights & rights) 
1069                     return CM_ERROR_NOACCESS;
1070             }
1071             else {
1072                 /* we don't know the required access rights */
1073                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1074                 code = cm_GetAccessRights(scp, userp, reqp);
1075                 if (bufLocked) {
1076                     lock_ReleaseMutex(&scp->mx);
1077                     lock_ObtainMutex(&bufp->mx);
1078                     lock_ObtainMutex(&scp->mx);
1079                 }
1080                 if (code) 
1081                     return code;
1082                 continue;
1083             }
1084         }
1085
1086         /* if we get here, we're happy */
1087         break;
1088
1089       sleep:
1090         /* first check if we're not supposed to wait: fail 
1091          * in this case, returning with everything still locked.
1092          */
1093         if (flags & CM_SCACHESYNC_NOWAIT) 
1094             return CM_ERROR_WOULDBLOCK;
1095
1096         sleep_scp_flags = scp->flags;           /* so we know why we slept */
1097         sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1098         sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1099
1100         /* wait here, then try again */
1101         osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1102         if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1103             scp->waitCount++;
1104             scp->waitRequests++;
1105             osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests", 
1106                      scp, scp->waitCount, scp->waitRequests);
1107         } else {
1108             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1109             scp->flags |= CM_SCACHEFLAG_WAITING;
1110             scp->waitCount = scp->waitRequests = 1;
1111         }
1112         if (bufLocked) 
1113             lock_ReleaseMutex(&bufp->mx);
1114         osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1115
1116         smb_UpdateServerPriority();
1117
1118         if (bufLocked) 
1119             lock_ObtainMutex(&bufp->mx);
1120         lock_ObtainMutex(&scp->mx);
1121         scp->waitCount--;
1122         osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests", 
1123                  scp, scp->waitCount, scp->waitRequests);
1124         if (scp->waitCount == 0) {
1125             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1126             scp->flags &= ~CM_SCACHEFLAG_WAITING;
1127             scp->waitRequests = 0;
1128         }
1129     } /* big while loop */
1130         
1131     /* now, update the recorded state for RPC-type calls */
1132     if (flags & CM_SCACHESYNC_FETCHSTATUS)
1133         scp->flags |= CM_SCACHEFLAG_FETCHING;
1134     if (flags & CM_SCACHESYNC_STORESTATUS)
1135         scp->flags |= CM_SCACHEFLAG_STORING;
1136     if (flags & CM_SCACHESYNC_STORESIZE)
1137         scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1138     if (flags & CM_SCACHESYNC_GETCALLBACK)
1139         scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1140     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1141         scp->flags |= CM_SCACHEFLAG_DATASTORING;
1142     if (flags & CM_SCACHESYNC_ASYNCSTORE)
1143         scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1144     if (flags & CM_SCACHESYNC_LOCK)
1145         scp->flags |= CM_SCACHEFLAG_LOCKING;
1146
1147     /* now update the buffer pointer */
1148     if (flags & CM_SCACHESYNC_FETCHDATA) {
1149         /* ensure that the buffer isn't already in the I/O list */
1150         if (bufp) {
1151             for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1152                 tbufp = osi_GetQData(qdp);
1153                 osi_assert(tbufp != bufp);
1154             }
1155         }
1156
1157         /* queue a held reference to the buffer in the "reading" I/O list */
1158         qdp = osi_QDAlloc();
1159         osi_SetQData(qdp, bufp);
1160         if (bufp) {
1161             buf_Hold(bufp);
1162             bufp->cmFlags |= CM_BUF_CMFETCHING;
1163         }
1164         osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1165     }
1166
1167     if (flags & CM_SCACHESYNC_STOREDATA) {
1168         /* ensure that the buffer isn't already in the I/O list */
1169         if (bufp) {
1170             for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1171                 tbufp = osi_GetQData(qdp);
1172                 osi_assert(tbufp != bufp);
1173             }
1174         }
1175
1176         /* queue a held reference to the buffer in the "writing" I/O list */
1177         qdp = osi_QDAlloc();
1178         osi_SetQData(qdp, bufp);
1179         if (bufp) {
1180             buf_Hold(bufp);
1181             bufp->cmFlags |= CM_BUF_CMSTORING;
1182         }
1183         osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1184     }
1185
1186     return 0;
1187 }
1188
1189 /* for those syncops that setup for RPCs.
1190  * Called with scache locked.
1191  */
1192 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1193 {
1194     osi_queueData_t *qdp;
1195     cm_buf_t *tbufp;
1196
1197     lock_AssertMutex(&scp->mx);
1198
1199     /* now, update the recorded state for RPC-type calls */
1200     if (flags & CM_SCACHESYNC_FETCHSTATUS)
1201         scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1202     if (flags & CM_SCACHESYNC_STORESTATUS)
1203         scp->flags &= ~CM_SCACHEFLAG_STORING;
1204     if (flags & CM_SCACHESYNC_STORESIZE)
1205         scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1206     if (flags & CM_SCACHESYNC_GETCALLBACK)
1207         scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1208     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1209         scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1210     if (flags & CM_SCACHESYNC_ASYNCSTORE)
1211         scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1212     if (flags & CM_SCACHESYNC_LOCK)
1213         scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1214
1215     /* now update the buffer pointer */
1216     if (flags & CM_SCACHESYNC_FETCHDATA) {
1217         int release = 0;
1218
1219         /* ensure that the buffer isn't already in the I/O list */
1220         for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1221             tbufp = osi_GetQData(qdp);
1222             if (tbufp == bufp) 
1223                 break;
1224         }
1225         if (qdp) {
1226             osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1227             osi_QDFree(qdp);
1228             release = 1;
1229         }
1230         if (bufp) {
1231             bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1232             if (bufp->flags & CM_BUF_WAITING) {
1233                 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1234                 osi_Wakeup((LONG_PTR) &bufp);
1235             }
1236             if (release)
1237                 buf_Release(bufp);
1238         }
1239     }
1240
1241     /* now update the buffer pointer */
1242     if (flags & CM_SCACHESYNC_STOREDATA) {
1243         int release = 0;
1244         /* ensure that the buffer isn't already in the I/O list */
1245         for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1246             tbufp = osi_GetQData(qdp);
1247             if (tbufp == bufp) 
1248                 break;
1249         }
1250         if (qdp) {
1251             osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1252             osi_QDFree(qdp);
1253             release = 1;
1254         }
1255         if (bufp) {
1256             bufp->cmFlags &= ~CM_BUF_CMSTORING;
1257             if (bufp->flags & CM_BUF_WAITING) {
1258                 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1259                 osi_Wakeup((LONG_PTR) &bufp);
1260             }
1261             if (release)
1262                 buf_Release(bufp);
1263         }
1264     }
1265
1266     /* and wakeup anyone who is waiting */
1267     if (scp->flags & CM_SCACHEFLAG_WAITING) {
1268         osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1269         osi_Wakeup((LONG_PTR) &scp->flags);
1270     }
1271 }       
1272
1273 /* merge in a response from an RPC.  The scp must be locked, and the callback
1274  * is optional.
1275  *
1276  * Don't overwrite any status info that is dirty, since we could have a store
1277  * operation (such as store data) that merges some info in, and we don't want
1278  * to lose the local updates.  Typically, there aren't many updates we do
1279  * locally, anyway, probably only mtime.
1280  *
1281  * There is probably a bug in here where a chmod (which doesn't change
1282  * serverModTime) that occurs between two fetches, both of whose responses are
1283  * handled after the callback breaking is done, but only one of whose calls
1284  * started before that, can cause old info to be merged from the first call.
1285  */
1286 void cm_MergeStatus(cm_scache_t *dscp, 
1287                     cm_scache_t *scp, AFSFetchStatus *statusp, 
1288                     AFSVolSync *volsyncp,
1289                     cm_user_t *userp, afs_uint32 flags)
1290 {
1291     // yj: i want to create some fake status for the /afs directory and the
1292     // entries under that directory
1293 #ifdef AFS_FREELANCE_CLIENT
1294     if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1295         osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1296         statusp->InterfaceVersion = 0x1;
1297         statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1298         statusp->LinkCount = scp->linkCount;
1299         statusp->Length = cm_fakeDirSize;
1300         statusp->Length_hi = 0;
1301         statusp->DataVersion = cm_data.fakeDirVersion;
1302         statusp->Author = 0x1;
1303         statusp->Owner = 0x0;
1304         statusp->CallerAccess = 0x9;
1305         statusp->AnonymousAccess = 0x9;
1306         statusp->UnixModeBits = 0777;
1307         statusp->ParentVnode = 0x1;
1308         statusp->ParentUnique = 0x1;
1309         statusp->ResidencyMask = 0;
1310         statusp->ClientModTime = FakeFreelanceModTime;
1311         statusp->ServerModTime = FakeFreelanceModTime;
1312         statusp->Group = 0;
1313         statusp->SyncCounter = 0;
1314         statusp->dataVersionHigh = 0;
1315         statusp->errorCode = 0;
1316     }
1317 #endif /* AFS_FREELANCE_CLIENT */
1318
1319     if (statusp->errorCode != 0) {      
1320         scp->flags |= CM_SCACHEFLAG_EACCESS;
1321         osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1322
1323         scp->fileType = 0;      /* unknown */
1324
1325         scp->serverModTime = 0;
1326         scp->clientModTime = 0;
1327         scp->length.LowPart = 0;
1328         scp->length.HighPart = 0;
1329         scp->serverLength.LowPart = 0;
1330         scp->serverLength.HighPart = 0;
1331         scp->linkCount = 0;
1332         scp->owner = 0;
1333         scp->group = 0;
1334         scp->unixModeBits = 0;
1335         scp->anyAccess = 0;
1336         scp->dataVersion = 0;
1337
1338         if (dscp) {
1339             scp->parentVnode = dscp->fid.vnode;
1340             scp->parentUnique = dscp->fid.unique;
1341         } else {
1342             scp->parentVnode = 0;
1343             scp->parentUnique = 0;
1344         }
1345         return;
1346     } else {
1347         scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1348     }
1349
1350     if (!(flags & CM_MERGEFLAG_FORCE)
1351          && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1352         struct cm_cell *cellp;
1353
1354         cellp = cm_FindCellByID(scp->fid.cell);
1355         if (scp->cbServerp) {
1356             struct cm_volume *volp = NULL;
1357
1358             cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1359                               (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1360             osi_Log2(afsd_logp, "old data from server %x volume %s",
1361                       scp->cbServerp->addr.sin_addr.s_addr,
1362                       volp ? volp->namep : "(unknown)");
1363             if (volp)
1364                 cm_PutVolume(volp);
1365         }
1366         osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1367                   scp, scp->dataVersion, statusp->DataVersion);
1368         /* we have a number of data fetch/store operations running
1369          * concurrently, and we can tell which one executed last at the
1370          * server by its mtime.
1371          * Choose the one with the largest mtime, and ignore the rest.
1372          *
1373          * These concurrent calls are incompatible with setting the
1374          * mtime, so we won't have a locally changed mtime here.
1375          *
1376          * We could also have ACL info for a different user than usual,
1377          * in which case we have to do that part of the merge, anyway.
1378          * We won't have to worry about the info being old, since we
1379          * won't have concurrent calls
1380          * that change file status running from this machine.
1381          *
1382          * Added 3/17/98:  if we see data version regression on an RO
1383          * file, it's probably due to a server holding an out-of-date
1384          * replica, rather than to concurrent RPC's.  Failures to
1385          * release replicas are now flagged by the volserver, but only
1386          * since AFS 3.4 5.22, so there are plenty of clients getting
1387          * out-of-date replicas out there.
1388          *
1389          * If we discover an out-of-date replica, by this time it's too
1390          * late to go to another server and retry.  Also, we can't
1391          * reject the merge, because then there is no way for
1392          * GetAccess to do its work, and the caller gets into an
1393          * infinite loop.  So we just grin and bear it.
1394          */
1395         if (!(scp->flags & CM_SCACHEFLAG_RO))
1396             return;
1397     }       
1398
1399     scp->serverModTime = statusp->ServerModTime;
1400
1401     if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1402         scp->clientModTime = statusp->ClientModTime;
1403     }
1404     if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1405         scp->length.LowPart = statusp->Length;
1406         scp->length.HighPart = statusp->Length_hi;
1407     }
1408
1409     scp->serverLength.LowPart = statusp->Length;
1410     scp->serverLength.HighPart = statusp->Length_hi;
1411
1412     scp->linkCount = statusp->LinkCount;
1413     scp->owner = statusp->Owner;
1414     scp->group = statusp->Group;
1415     scp->unixModeBits = statusp->UnixModeBits & 07777;
1416
1417     if (statusp->FileType == File)
1418         scp->fileType = CM_SCACHETYPE_FILE;
1419     else if (statusp->FileType == Directory)
1420         scp->fileType = CM_SCACHETYPE_DIRECTORY;
1421     else if (statusp->FileType == SymbolicLink) {
1422         if ((scp->unixModeBits & 0111) == 0)
1423             scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1424         else
1425             scp->fileType = CM_SCACHETYPE_SYMLINK;
1426     }       
1427     else {
1428         osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1429         scp->fileType = CM_SCACHETYPE_INVALID;  /* invalid */
1430     }
1431     /* and other stuff */
1432     scp->parentVnode = statusp->ParentVnode;
1433     scp->parentUnique = statusp->ParentUnique;
1434         
1435     /* and merge in the private acl cache info, if this is more than the public
1436      * info; merge in the public stuff in any case.
1437      */
1438     scp->anyAccess = statusp->AnonymousAccess;
1439
1440     if (userp != NULL) {
1441         cm_AddACLCache(scp, userp, statusp->CallerAccess);
1442     }
1443
1444     if ((flags & CM_MERGEFLAG_STOREDATA) &&
1445         statusp->DataVersion - scp->dataVersion == 1) {
1446         cm_buf_t *bp;
1447
1448         for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1449         {
1450             if (cm_FidCmp(&scp->fid, &bp->fid) == 0 && 
1451                 bp->dataVersion == scp->dataVersion)
1452                 bp->dataVersion = statusp->DataVersion;
1453         }
1454
1455     }
1456     scp->dataVersion = statusp->DataVersion;
1457 }
1458
1459 /* note that our stat cache info is incorrect, so force us eventually
1460  * to stat the file again.  There may be dirty data associated with
1461  * this vnode, and we want to preserve that information.
1462  *
1463  * This function works by simply simulating a loss of the callback.
1464  *
1465  * This function must be called with the scache locked.
1466  */
1467 void cm_DiscardSCache(cm_scache_t *scp)
1468 {
1469     lock_AssertMutex(&scp->mx);
1470     if (scp->cbServerp) {
1471         cm_PutServer(scp->cbServerp);
1472         scp->cbServerp = NULL;
1473     }
1474     scp->cbExpires = 0;
1475     scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1476     cm_dnlcPurgedp(scp);
1477     cm_dnlcPurgevp(scp);
1478     cm_FreeAllACLEnts(scp);
1479
1480     /* Force mount points and symlinks to be re-evaluated */
1481     scp->mountPointStringp[0] = '\0';
1482 }
1483
1484 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1485 {
1486     afsFidp->Volume = fidp->volume;
1487     afsFidp->Vnode = fidp->vnode;
1488     afsFidp->Unique = fidp->unique;
1489 }       
1490
1491 #ifdef DEBUG_REFCOUNT
1492 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1493 #else
1494 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1495 #endif
1496 {
1497     osi_assert(scp != 0);
1498     scp->refCount++;
1499 #ifdef DEBUG_REFCOUNT
1500     osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1501     afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1502 #endif
1503 }
1504
1505 #ifdef DEBUG_REFCOUNT
1506 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1507 #else
1508 void cm_HoldSCache(cm_scache_t *scp)
1509 #endif
1510 {
1511     osi_assert(scp != 0);
1512     lock_ObtainWrite(&cm_scacheLock);
1513     scp->refCount++;
1514 #ifdef DEBUG_REFCOUNT
1515     osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1516     afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1517 #endif
1518     lock_ReleaseWrite(&cm_scacheLock);
1519 }
1520
1521 #ifdef DEBUG_REFCOUNT
1522 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1523 #else
1524 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1525 #endif
1526 {
1527     osi_assert(scp != NULL);
1528     if (scp->refCount == 0)
1529         osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1530     osi_assert(scp->refCount-- >= 0);
1531 #ifdef DEBUG_REFCOUNT
1532     osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1533     afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1534 #endif
1535 }
1536
1537 #ifdef DEBUG_REFCOUNT
1538 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1539 #else
1540 void cm_ReleaseSCache(cm_scache_t *scp)
1541 #endif
1542 {
1543     osi_assert(scp != NULL);
1544     lock_ObtainWrite(&cm_scacheLock);
1545     if (scp->refCount == 0)
1546         osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1547     osi_assert(scp->refCount != 0);
1548     scp->refCount--;
1549 #ifdef DEBUG_REFCOUNT
1550     osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1551     afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1552 #endif
1553     lock_ReleaseWrite(&cm_scacheLock);
1554 }
1555
1556 /* just look for the scp entry to get filetype */
1557 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1558 int cm_FindFileType(cm_fid_t *fidp)
1559 {
1560     long hash;
1561     cm_scache_t *scp;
1562         
1563     hash = CM_SCACHE_HASH(fidp);
1564         
1565     osi_assert(fidp->cell != 0);
1566
1567     lock_ObtainWrite(&cm_scacheLock);
1568     for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1569         if (cm_FidCmp(fidp, &scp->fid) == 0) {
1570             lock_ReleaseWrite(&cm_scacheLock);
1571             return scp->fileType;
1572         }
1573     }
1574     lock_ReleaseWrite(&cm_scacheLock);
1575     return 0;
1576 }
1577
1578 /* dump all scp's that have reference count > 0 to a file. 
1579  * cookie is used to identify this batch for easy parsing, 
1580  * and it a string provided by a caller 
1581  */
1582 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1583 {
1584     int zilch;
1585     cm_scache_t *scp;
1586     char output[1024];
1587     int i;
1588   
1589     if (lock)
1590         lock_ObtainRead(&cm_scacheLock);
1591   
1592     sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1593     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1594   
1595     for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp) 
1596     {
1597         if (scp->refCount != 0)
1598         {
1599             sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\r\n", 
1600                     cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique, 
1601                     scp->refCount);
1602             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1603         }
1604     }
1605   
1606     sprintf(output, "%s - dumping cm_data.hashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1607     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1608   
1609     for (i = 0; i < cm_data.scacheHashTableSize; i++)
1610     {
1611         for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp) 
1612         {
1613             if (scp->refCount != 0)
1614             {
1615                 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\r\n", 
1616                          cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, 
1617                          scp->fid.unique, scp->refCount);
1618                 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1619             }
1620         }
1621     }
1622
1623     sprintf(output, "%s - Done dumping scache.\r\n", cookie);
1624     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1625   
1626     if (lock)
1627         lock_ReleaseRead(&cm_scacheLock);       
1628     return (0);     
1629 }
1630