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