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