2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
26 #include <afs/unified_afs.h>
28 /*extern void afsi_log(char *pattern, ...);*/
30 extern osi_hyper_t hzero;
33 osi_queue_t *cm_allFileLocks;
34 osi_queue_t *cm_freeFileLocks;
35 unsigned long cm_lockRefreshCycle;
37 /* lock for globals */
38 osi_rwlock_t cm_scacheLock;
40 /* Dummy scache entry for use with pioctl fids */
41 cm_scache_t cm_fakeSCache;
43 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
45 #ifdef AFS_FREELANCE_CLIENT
46 extern osi_mutex_t cm_Freelance_Lock;
50 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
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);
58 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
59 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
61 return cm_data.rootSCachep;
65 /* must be called with cm_scacheLock write-locked! */
66 void cm_AdjustScacheLRU(cm_scache_t *scp)
68 lock_AssertWrite(&cm_scacheLock);
69 if (!(scp->flags & CM_SCACHEFLAG_DELETED)) {
70 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
71 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
75 /* call with cm_scacheLock write-locked and scp rw held */
76 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
82 lock_AssertWrite(&cm_scacheLock);
83 lock_AssertWrite(&scp->rw);
84 if (scp->flags & CM_SCACHEFLAG_INHASH) {
85 /* hash it out first */
86 i = CM_SCACHE_HASH(&scp->fid);
87 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
89 lscpp = &tscp->nextp, tscp = tscp->nextp) {
93 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
100 /* called with cm_scacheLock and scp write-locked */
101 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
104 /* destroy directory Bplus Tree */
106 LARGE_INTEGER start, end;
108 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
110 * We are not holding the dirlock and obtaining it
111 * requires that we drop the scp->rw. As a result
112 * we will leave the dirBplus tree intact but
113 * invalidate the version number so that whatever
114 * operation is currently active can safely complete
115 * but the contents will be ignored on the next
116 * directory operation.
118 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
122 QueryPerformanceCounter(&start);
124 freeBtree(scp->dirBplus);
125 scp->dirBplus = NULL;
126 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
127 QueryPerformanceCounter(&end);
130 lock_ReleaseWrite(&scp->dirlock);
132 bplus_free_time += (end.QuadPart - start.QuadPart);
137 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
138 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
144 lock_AssertWrite(&cm_scacheLock);
145 lock_AssertWrite(&scp->rw);
147 if (scp->refCount != 0) {
151 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
152 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
154 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
159 if (scp->redirBufCount != 0) {
164 fileType = scp->fileType;
165 callback = scp->cbExpires ? 1 : 0;
167 cm_RemoveSCacheFromHashTable(scp);
169 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
170 !cm_accessPerFileCheck) {
171 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
174 if (!(volp->flags & CM_VOLUMEFLAG_DFS_VOLUME))
175 cm_EAccesClearParentEntries(&fid);
181 /* invalidate so next merge works fine;
182 * also initialize some flags */
184 _InterlockedAnd(&scp->flags,
185 ~( CM_SCACHEFLAG_DELETED
187 | CM_SCACHEFLAG_PURERO
188 | CM_SCACHEFLAG_OVERQUOTA
189 | CM_SCACHEFLAG_OUTOFSPACE
190 | CM_SCACHEFLAG_ASYNCSTORING));
191 scp->serverModTime = 0;
192 scp->dataVersion = CM_SCACHE_VERSION_BAD;
193 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
194 scp->bulkStatProgress = hzero;
196 scp->waitQueueT = NULL;
198 if (scp->cbServerp) {
199 cm_PutServer(scp->cbServerp);
200 scp->cbServerp = NULL;
204 scp->volumeCreationDate = 0;
212 /* remove from dnlc */
216 /* discard cached status; if non-zero, Close
217 * tried to store this to server but failed */
220 /* discard symlink info */
221 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
222 scp->mountPointStringp[0] = '\0';
223 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
224 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
226 /* reset locking info */
227 scp->fileLocksH = NULL;
228 scp->fileLocksT = NULL;
229 scp->serverLock = (-1);
230 scp->exclusiveLocks = 0;
231 scp->sharedLocks = 0;
232 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
233 scp->fsLockCount = 0;
235 /* not locked, but there can be no references to this guy
236 * while we hold the global refcount lock.
238 cm_FreeAllACLEnts(scp);
240 cm_ResetSCacheDirectory(scp, 0);
242 if (RDR_Initialized && callback) {
244 * We drop the cm_scacheLock because it may be required to
245 * satisfy an ioctl request from the redirector. It should
246 * be safe to hold the scp->rw lock here because at this
247 * point (a) the object has just been recycled so the fid
248 * is nul and there are no requests that could possibly
249 * be issued by the redirector that would depend upon it.
251 lock_ReleaseWrite(&cm_scacheLock);
252 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
253 fid.unique, fid.hash,
254 fileType, AFS_INVALIDATE_EXPIRED);
255 lock_ObtainWrite(&cm_scacheLock);
263 * called with cm_scacheLock write-locked; find a vnode to recycle.
264 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
265 * returns scp->rw write-locked.
268 cm_GetNewSCache(afs_uint32 locked)
270 cm_scache_t *scp = NULL;
271 cm_scache_t *scp_prev = NULL;
272 cm_scache_t *scp_next = NULL;
276 lock_AssertWrite(&cm_scacheLock);
278 lock_ObtainWrite(&cm_scacheLock);
280 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
281 /* There were no deleted scache objects that we could use. Try to find
282 * one that simply hasn't been used in a while.
284 for (attempt = 0 ; attempt < 128; attempt++) {
285 afs_uint32 count = 0;
287 for ( scp = cm_data.scacheLRULastp;
289 scp = (cm_scache_t *) osi_QPrev(&scp->q))
292 * We save the prev and next pointers in the
293 * LRU because we are going to drop the cm_scacheLock and
294 * the order of the list could change out from beneath us.
295 * If both changed, it means that this entry has been moved
296 * within the LRU and it should no longer be recycled.
298 scp_prev = (cm_scache_t *) osi_QPrev(&scp->q);
299 scp_next = (cm_scache_t *) osi_QNext(&scp->q);
302 /* It is possible for the refCount to be zero and for there still
303 * to be outstanding dirty buffers. If there are dirty buffers,
304 * we must not recycle the scp.
306 * If the object is in use by the redirector, then avoid recycling
307 * it unless we have to.
309 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
310 afs_uint32 buf_dirty = 0;
311 afs_uint32 buf_rdr = 0;
313 lock_ReleaseWrite(&cm_scacheLock);
314 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
316 buf_rdr = buf_RDRBuffersExist(&scp->fid);
318 if (!buf_dirty && !buf_rdr) {
323 success = lock_TryWrite(&scp->rw);
325 lock_ObtainWrite(&cm_scacheLock);
326 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
327 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
329 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
331 lock_ReleaseWrite(&scp->rw);
333 } else if (!success) {
334 osi_Log1(afsd_logp, "GetNewSCache failed to obtain lock scp 0x%p", scp);
338 /* Found a likely candidate. Save type and fid in case we succeed */
340 fileType = scp->fileType;
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.
347 cm_AdjustScacheLRU(scp);
349 /* and we're done - SUCCESS */
350 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
353 lock_ReleaseWrite(&scp->rw);
356 osi_Log1(afsd_logp,"GetNewSCache redirector is holding extents scp 0x%p", scp);
358 osi_Log1(afsd_logp, "GetNewSCache dirty buffers scp 0x%p", scp);
360 lock_ObtainWrite(&cm_scacheLock);
361 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
362 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
364 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
371 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
374 * The entire LRU queue was walked and no available cm_scache_t was
375 * found. Drop the cm_scacheLock and sleep for a moment to give a
376 * chance for cm_scache_t objects to be released.
378 lock_ReleaseWrite(&cm_scacheLock);
380 lock_ObtainWrite(&cm_scacheLock);
388 /* if we get here, we should allocate a new scache entry. We either are below
389 * quota or we have a leak and need to allocate a new one to avoid panicing.
391 scp = cm_data.scacheBaseAddress + InterlockedIncrement(&cm_data.currentSCaches) - 1;
392 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
393 "invalid cm_scache_t address");
394 memset(scp, 0, sizeof(cm_scache_t));
395 scp->magic = CM_SCACHE_MAGIC;
396 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
397 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
398 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
400 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
402 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
403 scp->serverLock = -1;
404 scp->dataVersion = CM_SCACHE_VERSION_BAD;
405 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
406 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
407 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
409 /* and put it in the LRU queue */
410 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
411 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
413 scp->allNextp = cm_data.allSCachesp;
414 cm_data.allSCachesp = scp;
418 lock_ReleaseWrite(&cm_scacheLock);
423 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
426 fidp->volume = volume;
428 fidp->unique = unique;
429 CM_FID_GEN_HASH(fidp);
432 /* like strcmp, only for fids */
433 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
435 if (ap->hash != bp->hash)
437 if (ap->vnode != bp->vnode)
439 if (ap->volume != bp->volume)
441 if (ap->unique != bp->unique)
443 if (ap->cell != bp->cell)
448 void cm_fakeSCacheInit(int newFile)
451 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
452 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
453 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
454 cm_data.fakeSCache.cbExpires = (time_t)-1;
455 cm_data.fakeSCache.cbExpires = time(NULL);
456 /* can leave clientModTime at 0 */
457 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
458 cm_data.fakeSCache.unixModeBits = 0777;
459 cm_data.fakeSCache.length.LowPart = 1000;
460 cm_data.fakeSCache.linkCount = 1;
461 cm_data.fakeSCache.refCount = 1;
462 cm_data.fakeSCache.serverLock = -1;
463 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
465 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
466 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
467 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
468 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
472 cm_ValidateSCache(void)
474 cm_scache_t * scp, *lscp;
477 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
478 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
479 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
480 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
484 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
486 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
487 if (scp->magic != CM_SCACHE_MAGIC) {
488 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
489 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
492 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
493 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
494 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
497 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
498 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
499 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
502 if (i > cm_data.currentSCaches ) {
503 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
504 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
507 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
508 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
509 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
514 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
515 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
516 if (scp->magic != CM_SCACHE_MAGIC) {
517 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
518 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
521 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
522 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
523 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
526 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
527 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
528 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
531 if (i > cm_data.currentSCaches ) {
532 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
533 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
536 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
537 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
538 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
543 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
544 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
546 hash = CM_SCACHE_HASH(&scp->fid);
547 if (scp->magic != CM_SCACHE_MAGIC) {
548 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
549 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
552 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
553 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
554 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
557 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
558 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
559 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
563 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
564 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
570 return cm_dnlcValidate();
574 cm_SuspendSCache(void)
579 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
582 * After this call all servers are marked down.
583 * Do not clear the callbacks, instead change the
584 * expiration time so that the callbacks will be expired
585 * when the servers are marked back up. However, we
586 * want the callbacks to be preserved as long as the
587 * servers are down. That way if the machine resumes
588 * without network, the stat cache item will still be
593 lock_ObtainWrite(&cm_scacheLock);
594 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
595 if (scp->cbServerp) {
596 if (scp->flags & CM_SCACHEFLAG_PURERO) {
597 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
599 if (volp->cbExpiresRO == scp->cbExpires)
600 volp->cbExpiresRO = now+1;
604 scp->cbExpires = now+1;
607 lock_ReleaseWrite(&cm_scacheLock);
611 cm_ShutdownSCache(void)
613 cm_scache_t * scp, * nextp;
615 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
617 lock_ObtainWrite(&cm_scacheLock);
619 for ( scp = cm_data.allSCachesp; scp;
621 nextp = scp->allNextp;
622 lock_ReleaseWrite(&cm_scacheLock);
624 lock_ObtainWrite(&scp->dirlock);
626 lock_ObtainWrite(&scp->rw);
627 lock_ObtainWrite(&cm_scacheLock);
629 if (scp->randomACLp) {
630 cm_FreeAllACLEnts(scp);
633 if (scp->cbServerp) {
634 cm_PutServer(scp->cbServerp);
635 scp->cbServerp = NULL;
639 lock_ReleaseWrite(&scp->rw);
643 freeBtree(scp->dirBplus);
644 scp->dirBplus = NULL;
645 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
646 lock_ReleaseWrite(&scp->dirlock);
647 lock_FinalizeRWLock(&scp->dirlock);
649 lock_FinalizeRWLock(&scp->rw);
650 lock_FinalizeRWLock(&scp->bufCreateLock);
651 lock_FinalizeMutex(&scp->redirMx);
653 lock_ReleaseWrite(&cm_scacheLock);
655 return cm_dnlcShutdown();
658 void cm_InitSCache(int newFile, long maxSCaches)
660 static osi_once_t once;
662 if (osi_Once(&once)) {
663 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
665 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
666 cm_data.allSCachesp = NULL;
667 cm_data.currentSCaches = 0;
668 cm_data.maxSCaches = maxSCaches;
669 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
673 for ( scp = cm_data.allSCachesp; scp;
674 scp = scp->allNextp ) {
675 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
676 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
678 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
680 scp->cbServerp = NULL;
683 scp->volumeCreationDate = 0;
684 scp->fileLocksH = NULL;
685 scp->fileLocksT = NULL;
686 scp->serverLock = (-1);
687 scp->lastRefreshCycle = 0;
688 scp->exclusiveLocks = 0;
689 scp->sharedLocks = 0;
697 scp->dirBplus = NULL;
698 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
700 scp->waitQueueT = NULL;
701 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
703 scp->redirBufCount = 0;
704 scp->redirQueueT = NULL;
705 scp->redirQueueH = NULL;
706 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
709 cm_allFileLocks = NULL;
710 cm_freeFileLocks = NULL;
711 cm_lockRefreshCycle = 0;
712 cm_fakeSCacheInit(newFile);
713 cm_allFreeWaiters = NULL;
714 cm_dnlcInit(newFile);
719 /* version that doesn't bother creating the entry if we don't find it */
720 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
725 hash = CM_SCACHE_HASH(fidp);
727 if (fidp->cell == 0) {
731 lock_ObtainRead(&cm_scacheLock);
732 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
733 if (cm_FidCmp(fidp, &scp->fid) == 0) {
734 cm_HoldSCacheNoLock(scp);
735 lock_ConvertRToW(&cm_scacheLock);
736 cm_AdjustScacheLRU(scp);
737 lock_ReleaseWrite(&cm_scacheLock);
741 lock_ReleaseRead(&cm_scacheLock);
745 #ifdef DEBUG_REFCOUNT
746 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
747 cm_req_t *reqp, char * file, long line)
749 long cm_GetSCache(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
754 cm_scache_t *scp = NULL;
755 cm_scache_t *newScp = NULL;
757 cm_volume_t *volp = NULL;
759 int special = 0; // yj: boolean variable to test if file is on root.afs
761 extern cm_fid_t cm_rootFid;
764 hash = CM_SCACHE_HASH(fidp);
767 return CM_ERROR_INVAL;
769 #ifdef AFS_FREELANCE_CLIENT
770 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
771 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
772 !(fidp->vnode==0x1 && fidp->unique==0x1));
773 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
774 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
775 fidp->vnode==0x1 && fidp->unique==0x1);
778 // yj: check if we have the scp, if so, we don't need
779 // to do anything else
780 lock_ObtainRead(&cm_scacheLock);
781 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
782 if (cm_FidCmp(fidp, &scp->fid) == 0) {
783 #ifdef DEBUG_REFCOUNT
784 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
785 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
787 #ifdef AFS_FREELANCE_CLIENT
788 if (cm_freelanceEnabled && special &&
789 cm_data.fakeDirVersion != scp->dataVersion)
792 if (parentFidp && scp->parentVnode == 0) {
793 scp->parentVnode = parentFidp->vnode;
794 scp->parentUnique = parentFidp->unique;
796 cm_HoldSCacheNoLock(scp);
798 lock_ConvertRToW(&cm_scacheLock);
799 cm_AdjustScacheLRU(scp);
800 lock_ReleaseWrite(&cm_scacheLock);
804 lock_ReleaseRead(&cm_scacheLock);
806 // yj: when we get here, it means we don't have an scp
807 // so we need to either load it or fake it, depending
808 // on whether the file is "special", see below.
810 // yj: if we're trying to get an scp for a file that's
811 // on root.afs of homecell, we want to handle it specially
812 // because we have to fill in the status stuff 'coz we
813 // don't want trybulkstat to fill it in for us
814 #ifdef AFS_FREELANCE_CLIENT
815 if (cm_freelanceEnabled && isRoot) {
816 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
817 /* freelance: if we are trying to get the root scp for the first
818 * time, we will just put in a place holder entry.
823 if (cm_freelanceEnabled && special) {
824 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
826 if (cm_getLocalMountPointChange()) {
827 cm_clearLocalMountPointChange();
828 cm_reInitLocalMountPoints();
832 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
834 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
835 return CM_ERROR_WOULDBLOCK;
838 lock_ObtainWrite(&scp->rw);
841 cm_SetFid(&scp->dotdotFid,AFS_FAKE_ROOT_CELL_ID,AFS_FAKE_ROOT_VOL_ID,1,1);
843 scp->parentVnode = parentFidp->vnode;
844 scp->parentUnique = parentFidp->unique;
846 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
847 lock_ObtainWrite(&cm_scacheLock);
848 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
849 scp->nextp = cm_data.scacheHashTablep[hash];
850 cm_data.scacheHashTablep[hash] = scp;
851 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
853 refCount = InterlockedIncrement(&scp->refCount);
854 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
855 lock_ReleaseWrite(&cm_scacheLock);
857 /* must be called after the scp->fid is set */
858 cm_FreelanceFetchMountPointString(scp);
859 cm_FreelanceFetchFileType(scp);
861 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
862 scp->length.HighPart = 0;
864 scp->unixModeBits=0777;
865 scp->clientModTime=FakeFreelanceModTime;
866 scp->serverModTime=FakeFreelanceModTime;
867 scp->parentUnique = 0x1;
868 scp->parentVnode=0x1;
870 scp->dataVersion=cm_data.fakeDirVersion;
871 scp->bufDataVersionLow=cm_data.fakeDirVersion;
872 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
874 lock_ReleaseWrite(&scp->rw);
876 #ifdef DEBUG_REFCOUNT
877 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
878 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
883 #endif /* AFS_FREELANCE_CLIENT */
885 /* we don't have the fid, recycle something */
886 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
887 if (newScp == NULL) {
888 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
889 return CM_ERROR_WOULDBLOCK;
891 #ifdef DEBUG_REFCOUNT
892 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
894 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
896 /* otherwise, we need to find the volume */
897 if (!cm_freelanceEnabled || !isRoot) {
898 cellp = cm_FindCellByID(fidp->cell, 0);
900 /* put back newScp so it can be reused */
901 lock_ObtainWrite(&cm_scacheLock);
902 _InterlockedOr(&newScp->flags, CM_SCACHEFLAG_DELETED);
903 cm_AdjustScacheLRU(newScp);
904 lock_ReleaseWrite(&newScp->rw);
905 lock_ReleaseWrite(&cm_scacheLock);
906 return CM_ERROR_NOSUCHCELL;
909 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
911 /* put back newScp so it can be reused */
912 lock_ObtainWrite(&cm_scacheLock);
913 _InterlockedOr(&newScp->flags, CM_SCACHEFLAG_DELETED);
914 cm_AdjustScacheLRU(newScp);
915 lock_ReleaseWrite(&newScp->rw);
916 lock_ReleaseWrite(&cm_scacheLock);
922 * otherwise, we have the volume, now reverify that the scp doesn't
923 * exist, and proceed. make sure that we hold the cm_scacheLock
924 * write-locked until the scp is put into the hash table in order
927 lock_ObtainWrite(&cm_scacheLock);
928 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
929 if (cm_FidCmp(fidp, &scp->fid) == 0) {
930 #ifdef DEBUG_REFCOUNT
931 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
932 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
934 if (parentFidp && scp->parentVnode == 0) {
935 scp->parentVnode = parentFidp->vnode;
936 scp->parentUnique = parentFidp->unique;
940 cm_HoldSCacheNoLock(scp);
941 cm_AdjustScacheLRU(scp);
943 /* put back newScp so it can be reused */
944 _InterlockedOr(&newScp->flags, CM_SCACHEFLAG_DELETED);
945 cm_AdjustScacheLRU(newScp);
946 lock_ReleaseWrite(&newScp->rw);
947 lock_ReleaseWrite(&cm_scacheLock);
956 if (!cm_freelanceEnabled || !isRoot) {
957 /* if this scache entry represents a volume root then we need
958 * to copy the dotdotFid from the volume structure where the
959 * "master" copy is stored (defect 11489)
961 if (volp->vol[ROVOL].ID == fidp->volume) {
962 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
963 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
964 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
965 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
966 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
967 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
968 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
970 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
971 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
975 scp->parentVnode = parentFidp->vnode;
976 scp->parentUnique = parentFidp->unique;
981 scp->nextp = cm_data.scacheHashTablep[hash];
982 cm_data.scacheHashTablep[hash] = scp;
983 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
984 refCount = InterlockedIncrement(&scp->refCount);
985 lock_ReleaseWrite(&cm_scacheLock);
986 lock_ReleaseWrite(&scp->rw);
987 #ifdef DEBUG_REFCOUNT
988 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
990 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
992 /* XXX - The following fields in the cm_scache are
999 /* now we have a held scache entry; just return it */
1001 #ifdef DEBUG_REFCOUNT
1002 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
1003 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
1008 /* Returns a held reference to the scache's parent
1010 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
1014 cm_fid_t parent_fid;
1015 cm_scache_t * pscp = NULL;
1017 if (scp->parentVnode == 0)
1020 lock_ObtainWrite(&cm_scacheLock);
1021 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
1023 if (cm_FidCmp(&scp->fid, &parent_fid)) {
1024 i = CM_SCACHE_HASH(&parent_fid);
1025 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
1026 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
1027 cm_HoldSCacheNoLock(pscp);
1033 lock_ReleaseWrite(&cm_scacheLock);
1038 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1040 cm_scache_waiter_t * w;
1042 lock_ObtainWrite(&cm_scacheLock);
1043 if (cm_allFreeWaiters == NULL) {
1044 w = malloc(sizeof(*w));
1045 memset(w, 0, sizeof(*w));
1047 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1048 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1051 w->threadId = thrd_Current();
1053 cm_HoldSCacheNoLock(scp);
1057 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1058 lock_ReleaseWrite(&cm_scacheLock);
1060 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1063 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1065 cm_scache_waiter_t * w;
1068 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1070 lock_ObtainRead(&cm_scacheLock);
1071 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1073 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1074 if (w->flags == flags && w->bufp == bufp) {
1079 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1080 this_is_me = (w->threadId == thrd_Current());
1081 lock_ReleaseRead(&cm_scacheLock);
1084 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1088 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1090 lock_ObtainWrite(&cm_scacheLock);
1091 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1092 cm_ReleaseSCacheNoLock(scp);
1093 memset(w, 0, sizeof(*w));
1094 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1095 lock_ReleaseWrite(&cm_scacheLock);
1101 /* synchronize a fetch, store, read, write, fetch status or store status.
1102 * Called with scache mutex held, and returns with it held, but temporarily
1103 * drops it during the fetch.
1105 * At most one flag can be on in flags, if this is an RPC request.
1107 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1109 * There are a lot of weird restrictions here; here's an attempt to explain the
1110 * rationale for the concurrency restrictions implemented in this function.
1112 * First, although the file server will break callbacks when *another* machine
1113 * modifies a file or status block, the client itself is responsible for
1114 * concurrency control on its own requests. Callback breaking events are rare,
1115 * and simply invalidate any concurrent new status info.
1117 * In the absence of callback breaking messages, we need to know how to
1118 * synchronize incoming responses describing updates to files. We synchronize
1119 * operations that update the data version by comparing the data versions.
1120 * However, updates that do not update the data, but only the status, can't be
1121 * synchronized with fetches or stores, since there's nothing to compare
1122 * to tell which operation executed first at the server.
1124 * Thus, we can allow multiple ops that change file data, or dir data, and
1125 * fetches. However, status storing ops have to be done serially.
1127 * Furthermore, certain data-changing ops are incompatible: we can't read or
1128 * write a buffer while doing a truncate. We can't read and write the same
1129 * buffer at the same time, or write while fetching or storing, or read while
1130 * fetching a buffer (this may change). We can't fetch and store at the same
1133 * With respect to status, we can't read and write at the same time, read while
1134 * fetching, write while fetching or storing, or fetch and store at the same time.
1136 * We can't allow a get callback RPC to run in concurrently with something that
1137 * will return updated status, since we could start a call, have the server
1138 * return status, have another machine make an update to the status (which
1139 * doesn't change serverModTime), have the original machine get a new callback,
1140 * and then have the original machine merge in the early, old info from the
1141 * first call. At this point, the easiest way to avoid this problem is to have
1142 * getcallback calls conflict with all others for the same vnode. Other calls
1143 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1144 * vnode must be careful not to merge in their status unless they have obtained
1145 * a callback from the start of their call.
1147 * Note added 1/23/96
1148 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1149 * Each such RPC passes a FileLength parameter, which the server uses to do
1150 * pre-truncation if necessary. So if two RPC's are processed out of order at
1151 * the server, the one with the smaller FileLength will be processed last,
1152 * possibly resulting in a bogus truncation. The simplest way to avoid this
1153 * is to serialize all StoreData RPC's. This is the reason we defined
1154 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1156 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1157 * readers which may be requesting overlapping ranges.
1159 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1160 afs_uint32 rights, afs_uint32 flags)
1162 osi_queueData_t *qdp;
1165 afs_uint32 outRights;
1167 afs_uint32 sleep_scp_flags = 0;
1168 afs_uint32 sleep_buf_cmflags = 0;
1169 afs_uint32 sleep_scp_bufs = 0;
1171 afs_int32 waitCount;
1172 afs_int32 waitRequests;
1174 lock_AssertWrite(&scp->rw);
1176 /* lookup this first */
1177 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1180 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1183 /* Do the access check. Now we don't really do the access check
1184 * atomically, since the caller doesn't expect the parent dir to be
1185 * returned locked, and that is what we'd have to do to prevent a
1186 * callback breaking message on the parent due to a setacl call from
1187 * being processed while we're running. So, instead, we check things
1188 * here, and if things look fine with the access, we proceed to finish
1189 * the rest of this check. Sort of a hack, but probably good enough.
1193 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1194 /* if we're bringing in a new status block, ensure that
1195 * we aren't already doing so, and that no one is
1196 * changing the status concurrently, either. We need
1197 * to do this, even if the status is of a different
1198 * type, since we don't have the ability to figure out,
1199 * in the AFS 3 protocols, which status-changing
1200 * operation ran first, or even which order a read and
1201 * a write occurred in.
1203 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1204 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1205 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1209 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1210 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1211 /* if we're going to make an RPC to change the status, make sure
1212 * that no one is bringing in or sending out the status.
1214 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1215 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1216 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1219 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1220 (scp->bufReadsp || scp->bufWritesp)) {
1221 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1225 if (flags & CM_SCACHESYNC_FETCHDATA) {
1226 /* if we're bringing in a new chunk of data, make sure that
1227 * nothing is happening to that chunk, and that we aren't
1228 * changing the basic file status info, either.
1230 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1231 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1232 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
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 FETCHDATA", scp, bufp);
1240 if (flags & CM_SCACHESYNC_STOREDATA) {
1241 /* same as fetch data */
1242 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1243 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1244 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1247 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1248 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1253 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1254 /* Don't allow concurrent StoreData RPC's */
1255 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1256 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1261 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1262 /* Don't allow more than one BKG store request */
1263 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1264 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1269 if (flags & CM_SCACHESYNC_LOCK) {
1270 /* Don't allow concurrent fiddling with lock lists */
1271 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1272 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1277 /* now the operations that don't correspond to making RPCs */
1278 if (flags & CM_SCACHESYNC_GETSTATUS) {
1279 /* we can use the status that's here, if we're not
1280 * bringing in new status.
1282 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1283 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1287 if (flags & CM_SCACHESYNC_SETSTATUS) {
1288 /* we can make a change to the local status, as long as
1289 * the status isn't changing now.
1291 * If we're fetching or storing a chunk of data, we can
1292 * change the status locally, since the fetch/store
1293 * operations don't change any of the data that we're
1296 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1297 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1298 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1302 if (flags & CM_SCACHESYNC_READ) {
1303 /* we're going to read the data, make sure that the
1304 * status is available, and that the data is here. It
1305 * is OK to read while storing the data back.
1307 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1308 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1311 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1312 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1315 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1316 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1320 if (flags & CM_SCACHESYNC_WRITE) {
1321 /* don't write unless the status is stable and the chunk
1324 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1325 CM_SCACHEFLAG_SIZESTORING)) {
1326 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1329 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1331 CM_BUF_CMWRITING))) {
1332 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1334 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1335 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1336 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1342 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1343 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1344 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1347 if (cm_EAccesFindEntry(userp, &scp->fid))
1348 return CM_ERROR_NOACCESS;
1351 lock_ReleaseMutex(&bufp->mx);
1352 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1354 lock_ReleaseWrite(&scp->rw);
1355 lock_ObtainMutex(&bufp->mx);
1356 lock_ObtainWrite(&scp->rw);
1360 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1366 /* can't check access rights without a callback */
1367 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1369 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1370 return CM_ERROR_READONLY;
1372 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1373 if (~outRights & rights)
1374 return CM_ERROR_NOACCESS;
1377 /* we don't know the required access rights */
1378 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1379 code = cm_GetAccessRights(scp, userp, reqp);
1381 lock_ReleaseWrite(&scp->rw);
1382 lock_ObtainMutex(&bufp->mx);
1383 lock_ObtainWrite(&scp->rw);
1391 if (flags & CM_SCACHESYNC_BULKREAD) {
1392 /* Don't allow concurrent fiddling with lock lists */
1393 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1394 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1399 /* if we get here, we're happy */
1403 /* first check if we're not supposed to wait: fail
1404 * in this case, returning with everything still locked.
1406 if (flags & CM_SCACHESYNC_NOWAIT)
1407 return CM_ERROR_WOULDBLOCK;
1409 /* These are used for minidump debugging */
1410 sleep_scp_flags = scp->flags; /* so we know why we slept */
1411 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1412 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1414 /* wait here, then try again */
1415 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1417 waitCount = InterlockedIncrement(&scp->waitCount);
1418 waitRequests = InterlockedIncrement(&scp->waitRequests);
1419 if (waitCount > 1) {
1420 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1421 scp, waitCount, waitRequests);
1423 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1424 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1427 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1431 lock_ReleaseMutex(&bufp->mx);
1432 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1434 lock_ObtainMutex(&bufp->mx);
1435 lock_ObtainWrite(&scp->rw);
1436 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1438 cm_UpdateServerPriority();
1440 waitCount = InterlockedDecrement(&scp->waitCount);
1441 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1442 scp, waitCount, scp->waitRequests);
1443 if (waitCount == 0) {
1444 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1445 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1446 scp->waitRequests = 0;
1448 } /* big while loop */
1450 /* now, update the recorded state for RPC-type calls */
1451 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1452 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1453 if (flags & CM_SCACHESYNC_STORESTATUS)
1454 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1455 if (flags & CM_SCACHESYNC_SETSIZE)
1456 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1457 if (flags & CM_SCACHESYNC_STORESIZE)
1458 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1459 if (flags & CM_SCACHESYNC_GETCALLBACK)
1460 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1461 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1462 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1463 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1464 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1465 if (flags & CM_SCACHESYNC_LOCK)
1466 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1467 if (flags & CM_SCACHESYNC_BULKREAD)
1468 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1470 /* now update the buffer pointer */
1471 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1472 /* ensure that the buffer isn't already in the I/O list */
1473 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1474 tbufp = osi_GetQData(qdp);
1475 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1478 /* queue a held reference to the buffer in the "reading" I/O list */
1479 qdp = osi_QDAlloc();
1480 osi_SetQData(qdp, bufp);
1483 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1484 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1487 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1488 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1489 "attempting to store extents on a non-file object");
1491 /* ensure that the buffer isn't already in the I/O list */
1492 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1493 tbufp = osi_GetQData(qdp);
1494 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1497 /* queue a held reference to the buffer in the "writing" I/O list */
1498 qdp = osi_QDAlloc();
1499 osi_SetQData(qdp, bufp);
1501 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1502 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1505 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1506 /* mark the buffer as being written to. */
1507 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1513 /* for those syncops that setup for RPCs.
1514 * Called with scache locked.
1516 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1518 osi_queueData_t *qdp;
1521 lock_AssertWrite(&scp->rw);
1523 /* now, update the recorded state for RPC-type calls */
1524 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1525 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1526 if (flags & CM_SCACHESYNC_STORESTATUS)
1527 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1528 if (flags & CM_SCACHESYNC_SETSIZE)
1529 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1530 if (flags & CM_SCACHESYNC_STORESIZE)
1531 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1532 if (flags & CM_SCACHESYNC_GETCALLBACK)
1533 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1534 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1535 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1536 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1537 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1538 if (flags & CM_SCACHESYNC_LOCK)
1539 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1540 if (flags & CM_SCACHESYNC_BULKREAD)
1541 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1543 /* now update the buffer pointer */
1544 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1547 /* ensure that the buffer is in the I/O list */
1548 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1549 tbufp = osi_GetQData(qdp);
1554 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1558 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1559 if (bufp->flags & CM_BUF_WAITING) {
1560 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1561 osi_Wakeup((LONG_PTR) &bufp);
1567 /* now update the buffer pointer */
1568 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1570 /* ensure that the buffer is in the I/O list */
1571 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1572 tbufp = osi_GetQData(qdp);
1577 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1581 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1582 if (bufp->flags & CM_BUF_WAITING) {
1583 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1584 osi_Wakeup((LONG_PTR) &bufp);
1590 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1591 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1592 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1595 /* and wakeup anyone who is waiting */
1596 if ((scp->flags & CM_SCACHEFLAG_WAITING) ||
1597 !osi_QIsEmpty(&scp->waitQueueH)) {
1598 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1599 osi_Wakeup((LONG_PTR) &scp->flags);
1604 dv_diff(afs_uint64 dv1, afs_uint64 dv2)
1606 if ( dv1 - dv2 > 0x7FFFFFFF )
1607 return (afs_uint32)(dv2 - dv1);
1609 return (afs_uint32)(dv1 - dv2);
1613 cm_IsStatusValid(AFSFetchStatus *statusp)
1615 if (statusp->InterfaceVersion != 0x1 ||
1616 !(statusp->FileType > 0 && statusp->FileType <= SymbolicLink)) {
1623 /* merge in a response from an RPC. The scp must be locked, and the callback
1626 * Don't overwrite any status info that is dirty, since we could have a store
1627 * operation (such as store data) that merges some info in, and we don't want
1628 * to lose the local updates. Typically, there aren't many updates we do
1629 * locally, anyway, probably only mtime.
1631 * There is probably a bug in here where a chmod (which doesn't change
1632 * serverModTime) that occurs between two fetches, both of whose responses are
1633 * handled after the callback breaking is done, but only one of whose calls
1634 * started before that, can cause old info to be merged from the first call.
1636 long cm_MergeStatus(cm_scache_t *dscp,
1637 cm_scache_t *scp, AFSFetchStatus *statusp,
1638 AFSVolSync *volsyncp,
1639 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1641 afs_uint64 dataVersion;
1642 struct cm_volume *volp = NULL;
1643 struct cm_cell *cellp = NULL;
1644 int rdr_invalidate = 0;
1645 afs_uint32 activeRPCs;
1647 lock_AssertWrite(&scp->rw);
1649 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1651 // yj: i want to create some fake status for the /afs directory and the
1652 // entries under that directory
1653 #ifdef AFS_FREELANCE_CLIENT
1654 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1655 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1656 if (scp == cm_data.rootSCachep) {
1657 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1658 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1659 statusp->Length = cm_fakeDirSize;
1660 statusp->Length_hi = 0;
1662 statusp->FileType = scp->fileType;
1663 statusp->Length = scp->length.LowPart;
1664 statusp->Length_hi = scp->length.HighPart;
1666 statusp->InterfaceVersion = 0x1;
1667 statusp->LinkCount = scp->linkCount;
1668 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1669 statusp->Author = 0x1;
1670 statusp->Owner = 0x0;
1671 statusp->CallerAccess = 0x9;
1672 statusp->AnonymousAccess = 0x9;
1673 statusp->UnixModeBits = 0777;
1674 statusp->ParentVnode = 0x1;
1675 statusp->ParentUnique = 0x1;
1676 statusp->ResidencyMask = 0;
1677 statusp->ClientModTime = FakeFreelanceModTime;
1678 statusp->ServerModTime = FakeFreelanceModTime;
1680 statusp->SyncCounter = 0;
1681 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1682 statusp->lockCount = 0;
1683 statusp->errorCode = 0;
1685 #endif /* AFS_FREELANCE_CLIENT */
1687 if (!cm_IsStatusValid(statusp)) {
1688 osi_Log3(afsd_logp, "Merge: Bad Status scp 0x%p Invalid InterfaceVersion %d FileType %d",
1689 scp, statusp->InterfaceVersion, statusp->FileType);
1690 return CM_ERROR_INVAL;
1693 if (statusp->errorCode != 0) {
1694 switch (statusp->errorCode) {
1699 cm_EAccesAddEntry(userp, &scp->fid, &dscp->fid);
1701 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1703 if (scp->fid.vnode & 0x1)
1704 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1706 scp->fileType = CM_SCACHETYPE_UNKNOWN;
1708 scp->serverModTime = 0;
1709 scp->clientModTime = 0;
1710 scp->length.LowPart = 0;
1711 scp->length.HighPart = 0;
1712 scp->serverLength.LowPart = 0;
1713 scp->serverLength.HighPart = 0;
1717 scp->unixModeBits = 0;
1719 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1720 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1721 scp->fsLockCount = 0;
1723 if (dscp && dscp != scp) {
1724 scp->parentVnode = dscp->fid.vnode;
1725 scp->parentUnique = dscp->fid.unique;
1727 scp->parentVnode = 0;
1728 scp->parentUnique = 0;
1731 if (RDR_Initialized)
1735 dataVersion = statusp->dataVersionHigh;
1737 dataVersion |= statusp->DataVersion;
1739 if (!(flags & CM_MERGEFLAG_FORCE) &&
1740 dataVersion < scp->dataVersion &&
1741 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1743 cellp = cm_FindCellByID(scp->fid.cell, 0);
1744 if (scp->cbServerp) {
1745 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1746 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1747 osi_Log2(afsd_logp, "old data from server %x volume %s",
1748 scp->cbServerp->addr.sin_addr.s_addr,
1749 volp ? volp->namep : "(unknown)");
1752 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1753 scp, scp->dataVersion, dataVersion);
1754 /* we have a number of data fetch/store operations running
1755 * concurrently, and we can tell which one executed last at the
1756 * server by its mtime.
1757 * Choose the one with the largest mtime, and ignore the rest.
1759 * These concurrent calls are incompatible with setting the
1760 * mtime, so we won't have a locally changed mtime here.
1762 * We could also have ACL info for a different user than usual,
1763 * in which case we have to do that part of the merge, anyway.
1764 * We won't have to worry about the info being old, since we
1765 * won't have concurrent calls
1766 * that change file status running from this machine.
1768 * Added 3/17/98: if we see data version regression on an RO
1769 * file, it's probably due to a server holding an out-of-date
1770 * replica, rather than to concurrent RPC's. Failures to
1771 * release replicas are now flagged by the volserver, but only
1772 * since AFS 3.4 5.22, so there are plenty of clients getting
1773 * out-of-date replicas out there.
1775 * If we discover an out-of-date replica, by this time it's too
1776 * late to go to another server and retry. Also, we can't
1777 * reject the merge, because then there is no way for
1778 * GetAccess to do its work, and the caller gets into an
1779 * infinite loop. So we just grin and bear it.
1781 if (!(scp->flags & CM_SCACHEFLAG_RO))
1786 * The first field of the volsync parameter is supposed to be the
1787 * volume creation date. Unfortunately, pre-OpenAFS 1.4.11 and 1.6.0
1788 * file servers do not populate the VolSync structure for BulkStat and
1789 * InlineBulkStat RPCs. As a result, the volume creation date is not
1790 * trustworthy when status is obtained via [Inline]BulkStatus RPCs.
1791 * If cm_readonlyVolumeVersioning is set, it is assumed that all file
1792 * servers populate the VolSync structure at all times.
1794 if (cm_readonlyVolumeVersioning || !(flags & CM_MERGEFLAG_BULKSTAT))
1795 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1797 scp->volumeCreationDate = 0;
1799 scp->serverModTime = statusp->ServerModTime;
1801 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1802 scp->clientModTime = statusp->ClientModTime;
1804 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1805 scp->length.LowPart = statusp->Length;
1806 scp->length.HighPart = statusp->Length_hi;
1809 scp->serverLength.LowPart = statusp->Length;
1810 scp->serverLength.HighPart = statusp->Length_hi;
1812 scp->linkCount = statusp->LinkCount;
1813 scp->owner = statusp->Owner;
1814 scp->group = statusp->Group;
1815 scp->unixModeBits = statusp->UnixModeBits & 07777;
1817 if (statusp->FileType == File)
1818 scp->fileType = CM_SCACHETYPE_FILE;
1819 else if (statusp->FileType == Directory)
1820 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1821 else if (statusp->FileType == SymbolicLink) {
1822 if ((scp->unixModeBits & 0111) == 0)
1823 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1825 scp->fileType = CM_SCACHETYPE_SYMLINK;
1828 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1829 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1831 /* and other stuff */
1832 scp->parentVnode = statusp->ParentVnode;
1833 scp->parentUnique = statusp->ParentUnique;
1835 /* -1 is a write lock; any positive values are read locks */
1836 scp->fsLockCount = (afs_int32)statusp->lockCount;
1838 /* and merge in the private acl cache info, if this is more than the public
1839 * info; merge in the public stuff in any case.
1841 scp->anyAccess = statusp->AnonymousAccess;
1843 if (userp != NULL) {
1844 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1847 if (dataVersion != 0 && scp->dataVersion != CM_SCACHE_VERSION_BAD &&
1848 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && (dataVersion != scp->dataVersion) ||
1849 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1850 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs))) {
1852 * We now know that all of the data buffers that we have associated
1853 * with this scp are invalid. Subsequent operations will go faster
1854 * if the buffers are removed from the hash tables.
1856 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1857 * those version numbers will be updated as part of the directory operation.
1859 * We do not remove storedata buffers because they will still be valid.
1864 cm_buf_t *bp, *prevBp, *nextBp;
1866 lock_ObtainWrite(&buf_globalLock);
1867 i = BUF_FILEHASH(&scp->fid);
1868 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1870 nextBp = bp->fileHashp;
1872 * if the buffer belongs to this stat cache entry
1873 * and the buffer mutex can be obtained, check the
1874 * reference count and if it is zero, remove the buffer
1875 * from the hash tables. If there are references,
1876 * the buffer might be updated to the current version
1877 * so leave it in place.
1879 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1880 lock_TryMutex(&bp->mx)) {
1881 if (bp->refCount == 0 &&
1882 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1883 !(bp->qFlags & CM_BUF_QREDIR)) {
1884 prevBp = bp->fileHashBackp;
1885 bp->fileHashBackp = bp->fileHashp = NULL;
1887 prevBp->fileHashp = nextBp;
1889 cm_data.buf_fileHashTablepp[i] = nextBp;
1891 nextBp->fileHashBackp = prevBp;
1893 j = BUF_HASH(&bp->fid, &bp->offset);
1894 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1895 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1900 /* we better find it */
1901 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1903 *lbpp = bp->hashp; /* hash out */
1906 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1908 lock_ReleaseMutex(&bp->mx);
1911 lock_ReleaseWrite(&buf_globalLock);
1914 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1915 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1916 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1918 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1919 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1920 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1921 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1922 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1925 /* We maintain a range of buffer dataVersion values which are considered
1926 * valid. This avoids the need to update the dataVersion on each buffer
1927 * object during an uncontested storeData operation. As a result this
1928 * merge status no longer has performance characteristics derived from
1929 * the size of the file.
1931 * For directory buffers, only current dataVersion values are up to date.
1933 if (((flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (dv_diff(dataVersion, scp->dataVersion) > activeRPCs)) ||
1934 (!(flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (scp->dataVersion != dataVersion)) ||
1935 scp->bufDataVersionLow == CM_SCACHE_VERSION_BAD ||
1936 scp->fileType == CM_SCACHETYPE_DIRECTORY ||
1937 flags & CM_MERGEFLAG_CACHE_BYPASS) {
1938 scp->bufDataVersionLow = dataVersion;
1941 if (RDR_Initialized) {
1943 * The redirector maintains its own cached status information which
1944 * must be updated when a DV change occurs that is not the result
1945 * of a redirector initiated data change.
1947 * If the current old DV is BAD, send a DV change notification.
1949 * If the DV has changed and request was not initiated by the
1950 * redirector, send a DV change notification.
1952 * If the request was initiated by the redirector, send a notification
1953 * for store and directory operations that result in a DV change greater
1954 * than the number of active RPCs or any other operation that results
1955 * in an unexpected DV change such as FetchStatus.
1958 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1960 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
1962 } else if (reqp->flags & CM_REQ_SOURCE_REDIR) {
1963 if (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1964 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs - 1)) {
1966 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1967 dv_diff(dataVersion, scp->dataVersion) > activeRPCs) {
1972 scp->dataVersion = dataVersion;
1975 * If someone is waiting for status information, we can wake them up
1976 * now even though the entity that issued the FetchStatus may not
1977 * have completed yet.
1979 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1982 * We just successfully merged status on the stat cache object.
1983 * This means that the associated volume must be online.
1987 cellp = cm_FindCellByID(scp->fid.cell, 0);
1988 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1991 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1992 if (statep->state != vl_online) {
1993 lock_ObtainWrite(&volp->rw);
1994 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1995 statep->state = vl_online;
1996 lock_ReleaseWrite(&volp->rw);
2000 /* Remove cached EACCES / EPERM errors if the file is a directory */
2001 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
2002 !(volp && (volp->flags & CM_VOLUMEFLAG_DFS_VOLUME)) &&
2003 !cm_accessPerFileCheck)
2005 cm_EAccesClearParentEntries(&scp->fid);
2013 * The scache rw lock cannot be held across the invalidation.
2014 * Doing so can result in deadlocks with other threads processing
2015 * requests initiated by the afs redirector.
2017 if (rdr_invalidate) {
2018 lock_ReleaseWrite(&scp->rw);
2019 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
2020 scp->fid.unique, scp->fid.hash,
2021 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
2022 lock_ObtainWrite(&scp->rw);
2028 /* note that our stat cache info is incorrect, so force us eventually
2029 * to stat the file again. There may be dirty data associated with
2030 * this vnode, and we want to preserve that information.
2032 * This function works by simply simulating a loss of the callback.
2034 * This function must be called with the scache locked.
2036 void cm_DiscardSCache(cm_scache_t *scp)
2038 lock_AssertWrite(&scp->rw);
2039 if (scp->cbServerp) {
2040 cm_PutServer(scp->cbServerp);
2041 scp->cbServerp = NULL;
2045 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
2046 cm_dnlcPurgedp(scp);
2047 cm_dnlcPurgevp(scp);
2048 cm_FreeAllACLEnts(scp);
2050 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
2051 cm_VolStatus_Invalidate_DFS_Mapping(scp);
2054 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
2056 afsFidp->Volume = fidp->volume;
2057 afsFidp->Vnode = fidp->vnode;
2058 afsFidp->Unique = fidp->unique;
2061 #ifdef DEBUG_REFCOUNT
2062 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2064 void cm_HoldSCacheNoLock(cm_scache_t *scp)
2069 osi_assertx(scp != NULL, "null cm_scache_t");
2070 lock_AssertAny(&cm_scacheLock);
2071 refCount = InterlockedIncrement(&scp->refCount);
2072 #ifdef DEBUG_REFCOUNT
2073 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
2074 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
2078 #ifdef DEBUG_REFCOUNT
2079 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
2081 void cm_HoldSCache(cm_scache_t *scp)
2086 osi_assertx(scp != NULL, "null cm_scache_t");
2087 lock_ObtainRead(&cm_scacheLock);
2088 refCount = InterlockedIncrement(&scp->refCount);
2089 #ifdef DEBUG_REFCOUNT
2090 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
2091 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
2093 lock_ReleaseRead(&cm_scacheLock);
2096 #ifdef DEBUG_REFCOUNT
2097 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2099 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2104 osi_assertx(scp != NULL, "null cm_scache_t");
2105 lock_AssertAny(&cm_scacheLock);
2107 refCount = InterlockedDecrement(&scp->refCount);
2108 #ifdef DEBUG_REFCOUNT
2110 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
2112 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2113 #ifdef DEBUG_REFCOUNT
2114 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
2115 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
2118 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2122 lockstate = lock_GetRWLockState(&cm_scacheLock);
2123 if (lockstate != OSI_RWLOCK_WRITEHELD)
2124 lock_ReleaseRead(&cm_scacheLock);
2126 lock_ReleaseWrite(&cm_scacheLock);
2128 lock_ObtainWrite(&scp->rw);
2129 if (scp->flags & CM_SCACHEFLAG_DELETED)
2132 if (refCount == 0 && deleted) {
2133 lock_ObtainWrite(&cm_scacheLock);
2134 cm_RecycleSCache(scp, 0);
2135 if (lockstate != OSI_RWLOCK_WRITEHELD)
2136 lock_ConvertWToR(&cm_scacheLock);
2138 if (lockstate != OSI_RWLOCK_WRITEHELD)
2139 lock_ObtainRead(&cm_scacheLock);
2141 lock_ObtainWrite(&cm_scacheLock);
2143 lock_ReleaseWrite(&scp->rw);
2147 #ifdef DEBUG_REFCOUNT
2148 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2150 void cm_ReleaseSCache(cm_scache_t *scp)
2155 osi_assertx(scp != NULL, "null cm_scache_t");
2156 lock_ObtainRead(&cm_scacheLock);
2157 refCount = InterlockedDecrement(&scp->refCount);
2158 #ifdef DEBUG_REFCOUNT
2160 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2162 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2163 #ifdef DEBUG_REFCOUNT
2164 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2165 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2167 lock_ReleaseRead(&cm_scacheLock);
2169 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2171 lock_ObtainWrite(&scp->rw);
2172 if (scp->flags & CM_SCACHEFLAG_DELETED)
2175 lock_ObtainWrite(&cm_scacheLock);
2176 cm_RecycleSCache(scp, 0);
2177 lock_ReleaseWrite(&cm_scacheLock);
2179 lock_ReleaseWrite(&scp->rw);
2183 /* just look for the scp entry to get filetype */
2184 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2185 int cm_FindFileType(cm_fid_t *fidp)
2190 hash = CM_SCACHE_HASH(fidp);
2192 osi_assertx(fidp->cell != 0, "unassigned cell value");
2194 lock_ObtainWrite(&cm_scacheLock);
2195 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2196 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2197 lock_ReleaseWrite(&cm_scacheLock);
2198 return scp->fileType;
2201 lock_ReleaseWrite(&cm_scacheLock);
2205 /* dump all scp's that have reference count > 0 to a file.
2206 * cookie is used to identify this batch for easy parsing,
2207 * and it a string provided by a caller
2209 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2218 lock_ObtainRead(&cm_scacheLock);
2220 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2221 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2223 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2226 char *srvStr = NULL;
2227 afs_uint32 srvStrRpc = TRUE;
2231 if (scp->cbServerp) {
2232 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2233 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2234 srvStr = malloc(16); /* enough for 255.255.255.255 */
2236 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2240 if (scp->cbExpires) {
2245 cbt[strlen(cbt)-1] = '\0';
2248 if (scp->volumeCreationDate) {
2249 t = scp->volumeCreationDate;
2252 cdrot = strdup(cdrot);
2253 cdrot[strlen(cdrot)-1] = '\0';
2257 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2258 "mpDV=%I64d mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2259 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2260 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2261 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mpDataVersion, scp->mountPointStringp,
2262 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2263 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2264 cdrot ? cdrot : "<none>", scp->refCount);
2265 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2267 if (scp->fileLocksH) {
2268 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2269 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2271 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2272 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2273 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2274 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2275 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2276 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2277 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2280 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2281 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2286 RpcStringFree(&srvStr);
2296 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2297 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2298 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2299 cookie, cm_data.scacheHashTableSize);
2300 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2302 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2304 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2306 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2307 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2308 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2312 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2313 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2315 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2316 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2318 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2319 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2320 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",
2321 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2322 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2323 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2326 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2327 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2330 lock_ReleaseRead(&cm_scacheLock);