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