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