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