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