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 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);
74 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
78 /* call with cm_scacheLock write-locked and scp rw held */
79 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
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];
92 lscpp = &tscp->nextp, tscp = tscp->nextp) {
96 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
103 /* called with cm_scacheLock and scp write-locked */
104 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
107 /* destroy directory Bplus Tree */
109 LARGE_INTEGER start, end;
111 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
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.
121 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
125 QueryPerformanceCounter(&start);
127 freeBtree(scp->dirBplus);
128 scp->dirBplus = NULL;
129 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
130 QueryPerformanceCounter(&end);
133 lock_ReleaseWrite(&scp->dirlock);
135 bplus_free_time += (end.QuadPart - start.QuadPart);
140 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
141 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
147 lock_AssertWrite(&cm_scacheLock);
148 lock_AssertWrite(&scp->rw);
150 if (scp->refCount != 0) {
154 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
155 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
157 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
162 if (scp->redirBufCount != 0) {
167 fileType = scp->fileType;
168 callback = scp->cbExpires ? 1 : 0;
170 cm_RemoveSCacheFromHashTable(scp);
172 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
173 !cm_accessPerFileCheck) {
174 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
176 if (!(volp && (volp->flags & CM_VOLUMEFLAG_DFS_VOLUME)))
177 cm_EAccesClearParentEntries(&fid);
181 /* invalidate so next merge works fine;
182 * also initialize some flags */
184 _InterlockedAnd(&scp->flags,
185 ~(CM_SCACHEFLAG_STATD
186 | CM_SCACHEFLAG_DELETED
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;
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->mountPointStringp[0] = '\0';
222 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
223 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
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;
234 /* not locked, but there can be no references to this guy
235 * while we hold the global refcount lock.
237 cm_FreeAllACLEnts(scp);
239 cm_ResetSCacheDirectory(scp, 0);
241 if (RDR_Initialized && callback) {
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.
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);
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.
267 cm_GetNewSCache(afs_uint32 locked)
269 cm_scache_t *scp = NULL;
270 cm_scache_t *scp_prev = NULL;
271 cm_scache_t *scp_next = NULL;
275 lock_AssertWrite(&cm_scacheLock);
277 lock_ObtainWrite(&cm_scacheLock);
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.
283 for (attempt = 0 ; attempt < 128; attempt++) {
284 afs_uint32 count = 0;
286 for ( scp = cm_data.scacheLRULastp;
288 scp = (cm_scache_t *) osi_QPrev(&scp->q))
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.
297 scp_prev = (cm_scache_t *) osi_QPrev(&scp->q);
298 scp_next = (cm_scache_t *) osi_QNext(&scp->q);
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.
305 * If the object is in use by the redirector, then avoid recycling
306 * it unless we have to.
308 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
309 afs_uint32 buf_dirty = 0;
310 afs_uint32 buf_rdr = 0;
312 lock_ReleaseWrite(&cm_scacheLock);
313 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
315 buf_rdr = buf_RDRBuffersExist(&scp->fid);
317 if (!buf_dirty && !buf_rdr) {
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))
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))
334 lock_ReleaseWrite(&scp->rw);
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);
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))
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))
370 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
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.
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);
389 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
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;
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 */
402 scp->allNextp = cm_data.allSCachesp;
403 cm_data.allSCachesp = scp;
407 lock_ReleaseWrite(&cm_scacheLock);
412 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
415 fidp->volume = volume;
417 fidp->unique = unique;
418 CM_FID_GEN_HASH(fidp);
421 /* like strcmp, only for fids */
422 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
424 if (ap->hash != bp->hash)
426 if (ap->vnode != bp->vnode)
428 if (ap->volume != bp->volume)
430 if (ap->unique != bp->unique)
432 if (ap->cell != bp->cell)
437 void cm_fakeSCacheInit(int 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;
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);
461 cm_ValidateSCache(void)
463 cm_scache_t * scp, *lscp;
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");
473 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
532 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
533 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
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");
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");
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");
552 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
553 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
559 return cm_dnlcValidate();
563 cm_SuspendSCache(void)
568 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
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
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);
588 if (volp->cbExpiresRO == scp->cbExpires)
589 volp->cbExpiresRO = now+1;
593 scp->cbExpires = now+1;
596 lock_ReleaseWrite(&cm_scacheLock);
600 cm_ShutdownSCache(void)
602 cm_scache_t * scp, * nextp;
604 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
606 lock_ObtainWrite(&cm_scacheLock);
608 for ( scp = cm_data.allSCachesp; scp;
610 nextp = scp->allNextp;
611 lock_ReleaseWrite(&cm_scacheLock);
613 lock_ObtainWrite(&scp->dirlock);
615 lock_ObtainWrite(&scp->rw);
616 lock_ObtainWrite(&cm_scacheLock);
618 if (scp->randomACLp) {
619 cm_FreeAllACLEnts(scp);
622 if (scp->cbServerp) {
623 cm_PutServer(scp->cbServerp);
624 scp->cbServerp = NULL;
628 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
629 lock_ReleaseWrite(&scp->rw);
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);
639 lock_FinalizeRWLock(&scp->rw);
640 lock_FinalizeRWLock(&scp->bufCreateLock);
641 lock_FinalizeMutex(&scp->redirMx);
643 lock_ReleaseWrite(&cm_scacheLock);
645 return cm_dnlcShutdown();
648 void cm_InitSCache(int newFile, long maxSCaches)
650 static osi_once_t once;
652 if (osi_Once(&once)) {
653 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
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;
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);
668 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
670 scp->cbServerp = NULL;
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;
687 scp->dirBplus = NULL;
688 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
690 scp->waitQueueT = NULL;
691 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
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);
699 cm_allFileLocks = NULL;
700 cm_freeFileLocks = NULL;
701 cm_lockRefreshCycle = 0;
702 cm_fakeSCacheInit(newFile);
703 cm_allFreeWaiters = NULL;
704 cm_dnlcInit(newFile);
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)
715 hash = CM_SCACHE_HASH(fidp);
717 if (fidp->cell == 0) {
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);
731 lock_ReleaseRead(&cm_scacheLock);
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)
739 long cm_GetSCache(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
744 cm_scache_t *scp = NULL;
745 cm_scache_t *newScp = NULL;
747 cm_volume_t *volp = NULL;
749 int special = 0; // yj: boolean variable to test if file is on root.afs
751 extern cm_fid_t cm_rootFid;
754 hash = CM_SCACHE_HASH(fidp);
757 return CM_ERROR_INVAL;
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);
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);
777 #ifdef AFS_FREELANCE_CLIENT
778 if (cm_freelanceEnabled && special &&
779 cm_data.fakeDirVersion != scp->dataVersion)
782 if (parentFidp && scp->parentVnode == 0) {
783 scp->parentVnode = parentFidp->vnode;
784 scp->parentUnique = parentFidp->unique;
786 cm_HoldSCacheNoLock(scp);
788 lock_ConvertRToW(&cm_scacheLock);
789 cm_AdjustScacheLRU(scp);
790 lock_ReleaseWrite(&cm_scacheLock);
794 lock_ReleaseRead(&cm_scacheLock);
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.
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.
813 if (cm_freelanceEnabled && special) {
814 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
816 if (cm_getLocalMountPointChange()) {
817 cm_clearLocalMountPointChange();
818 cm_reInitLocalMountPoints();
822 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
824 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
825 return CM_ERROR_WOULDBLOCK;
828 lock_ObtainWrite(&scp->rw);
831 cm_SetFid(&scp->dotdotFid,AFS_FAKE_ROOT_CELL_ID,AFS_FAKE_ROOT_VOL_ID,1,1);
833 scp->parentVnode = parentFidp->vnode;
834 scp->parentUnique = parentFidp->unique;
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);
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);
847 /* must be called after the scp->fid is set */
848 cm_FreelanceFetchMountPointString(scp);
849 cm_FreelanceFetchFileType(scp);
851 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
852 scp->length.HighPart = 0;
854 scp->unixModeBits=0777;
855 scp->clientModTime=FakeFreelanceModTime;
856 scp->serverModTime=FakeFreelanceModTime;
857 scp->parentUnique = 0x1;
858 scp->parentVnode=0x1;
860 scp->dataVersion=cm_data.fakeDirVersion;
861 scp->bufDataVersionLow=cm_data.fakeDirVersion;
862 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
864 lock_ReleaseWrite(&scp->rw);
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);
873 #endif /* AFS_FREELANCE_CLIENT */
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;
881 #ifdef DEBUG_REFCOUNT
882 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
884 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
886 /* otherwise, we need to find the volume */
887 if (!cm_freelanceEnabled || !isRoot) {
888 cellp = cm_FindCellByID(fidp->cell, 0);
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;
899 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
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);
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
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);
924 if (parentFidp && scp->parentVnode == 0) {
925 scp->parentVnode = parentFidp->vnode;
926 scp->parentUnique = parentFidp->unique;
930 cm_HoldSCacheNoLock(scp);
931 cm_AdjustScacheLRU(scp);
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);
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)
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;
960 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
961 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
965 scp->parentVnode = parentFidp->vnode;
966 scp->parentUnique = parentFidp->unique;
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);
980 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
982 /* XXX - The following fields in the cm_scache are
989 /* now we have a held scache entry; just return it */
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);
998 /* Returns a held reference to the scache's parent
1000 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
1004 cm_fid_t parent_fid;
1005 cm_scache_t * pscp = NULL;
1007 if (scp->parentVnode == 0)
1010 lock_ObtainWrite(&cm_scacheLock);
1011 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
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);
1023 lock_ReleaseWrite(&cm_scacheLock);
1028 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1030 cm_scache_waiter_t * w;
1032 lock_ObtainWrite(&cm_scacheLock);
1033 if (cm_allFreeWaiters == NULL) {
1034 w = malloc(sizeof(*w));
1035 memset(w, 0, sizeof(*w));
1037 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1038 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1041 w->threadId = thrd_Current();
1043 cm_HoldSCacheNoLock(scp);
1047 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1048 lock_ReleaseWrite(&cm_scacheLock);
1050 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1053 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1055 cm_scache_waiter_t * w;
1058 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1060 lock_ObtainRead(&cm_scacheLock);
1061 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1063 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1064 if (w->flags == flags && w->bufp == bufp) {
1069 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1070 this_is_me = (w->threadId == thrd_Current());
1071 lock_ReleaseRead(&cm_scacheLock);
1074 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1078 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
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);
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.
1095 * At most one flag can be on in flags, if this is an RPC request.
1097 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
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.
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.
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.
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.
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
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.
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.
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.
1146 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1147 * readers which may be requesting overlapping ranges.
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)
1152 osi_queueData_t *qdp;
1155 afs_uint32 outRights;
1157 afs_uint32 sleep_scp_flags = 0;
1158 afs_uint32 sleep_buf_cmflags = 0;
1159 afs_uint32 sleep_scp_bufs = 0;
1162 lock_AssertWrite(&scp->rw);
1164 /* lookup this first */
1165 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1168 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
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.
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.
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);
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.
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);
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);
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.
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);
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);
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);
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);
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);
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);
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);
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.
1270 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1271 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
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.
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
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);
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.
1295 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1296 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
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);
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);
1308 if (flags & CM_SCACHESYNC_WRITE) {
1309 /* don't write unless the status is stable and the chunk
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);
1317 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1319 CM_BUF_CMWRITING))) {
1320 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
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" :
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",
1335 if (cm_EAccesFindEntry(userp, &scp->fid))
1336 return CM_ERROR_NOACCESS;
1339 lock_ReleaseMutex(&bufp->mx);
1340 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1342 lock_ReleaseWrite(&scp->rw);
1343 lock_ObtainMutex(&bufp->mx);
1344 lock_ObtainWrite(&scp->rw);
1348 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1354 /* can't check access rights without a callback */
1355 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1357 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1358 return CM_ERROR_READONLY;
1360 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1361 if (~outRights & rights)
1362 return CM_ERROR_NOACCESS;
1365 /* we don't know the required access rights */
1366 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1367 code = cm_GetAccessRights(scp, userp, reqp);
1369 lock_ReleaseWrite(&scp->rw);
1370 lock_ObtainMutex(&bufp->mx);
1371 lock_ObtainWrite(&scp->rw);
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);
1387 /* if we get here, we're happy */
1391 /* first check if we're not supposed to wait: fail
1392 * in this case, returning with everything still locked.
1394 if (flags & CM_SCACHESYNC_NOWAIT)
1395 return CM_ERROR_WOULDBLOCK;
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);
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 ) {
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);
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;
1415 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1419 lock_ReleaseMutex(&bufp->mx);
1420 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1422 lock_ObtainMutex(&bufp->mx);
1423 lock_ObtainWrite(&scp->rw);
1424 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1426 cm_UpdateServerPriority();
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;
1436 } /* big while loop */
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);
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");
1466 /* queue a held reference to the buffer in the "reading" I/O list */
1467 qdp = osi_QDAlloc();
1468 osi_SetQData(qdp, bufp);
1471 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1472 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
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");
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");
1485 /* queue a held reference to the buffer in the "writing" I/O list */
1486 qdp = osi_QDAlloc();
1487 osi_SetQData(qdp, bufp);
1489 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1490 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1493 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1494 /* mark the buffer as being written to. */
1495 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1501 /* for those syncops that setup for RPCs.
1502 * Called with scache locked.
1504 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1506 osi_queueData_t *qdp;
1509 lock_AssertWrite(&scp->rw);
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);
1531 /* now update the buffer pointer */
1532 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
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);
1542 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
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);
1555 /* now update the buffer pointer */
1556 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
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);
1565 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
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);
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);
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);
1591 dv_diff(afs_uint64 dv1, afs_uint64 dv2)
1593 if ( dv1 - dv2 > 0x7FFFFFFF )
1594 return (afs_uint32)(dv2 - dv1);
1596 return (afs_uint32)(dv1 - dv2);
1599 /* merge in a response from an RPC. The scp must be locked, and the callback
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.
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.
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)
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;
1623 lock_AssertWrite(&scp->rw);
1625 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
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;
1638 statusp->FileType = scp->fileType;
1639 statusp->Length = scp->length.LowPart;
1640 statusp->Length_hi = scp->length.HighPart;
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;
1656 statusp->SyncCounter = 0;
1657 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1658 statusp->lockCount = 0;
1659 statusp->errorCode = 0;
1661 #endif /* AFS_FREELANCE_CLIENT */
1663 if (statusp->errorCode != 0) {
1664 switch (statusp->errorCode) {
1669 cm_EAccesAddEntry(userp, &scp->fid, &dscp->fid);
1671 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1673 if (scp->fid.vnode & 0x1)
1674 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1676 scp->fileType = 0; /* unknown */
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;
1687 scp->unixModeBits = 0;
1689 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1690 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1691 scp->fsLockCount = 0;
1693 if (dscp && dscp != scp) {
1694 scp->parentVnode = dscp->fid.vnode;
1695 scp->parentUnique = dscp->fid.unique;
1697 scp->parentVnode = 0;
1698 scp->parentUnique = 0;
1701 if (RDR_Initialized)
1705 dataVersion = statusp->dataVersionHigh;
1707 dataVersion |= statusp->DataVersion;
1709 if (!(flags & CM_MERGEFLAG_FORCE) &&
1710 dataVersion < scp->dataVersion &&
1711 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
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)");
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.
1729 * These concurrent calls are incompatible with setting the
1730 * mtime, so we won't have a locally changed mtime here.
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.
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.
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.
1751 if (!(scp->flags & CM_SCACHEFLAG_RO))
1755 if (cm_readonlyVolumeVersioning)
1756 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1758 scp->serverModTime = statusp->ServerModTime;
1760 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1761 scp->clientModTime = statusp->ClientModTime;
1763 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1764 scp->length.LowPart = statusp->Length;
1765 scp->length.HighPart = statusp->Length_hi;
1768 scp->serverLength.LowPart = statusp->Length;
1769 scp->serverLength.HighPart = statusp->Length_hi;
1771 scp->linkCount = statusp->LinkCount;
1772 scp->owner = statusp->Owner;
1773 scp->group = statusp->Group;
1774 scp->unixModeBits = statusp->UnixModeBits & 07777;
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;
1784 scp->fileType = CM_SCACHETYPE_SYMLINK;
1787 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1788 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1790 /* and other stuff */
1791 scp->parentVnode = statusp->ParentVnode;
1792 scp->parentUnique = statusp->ParentUnique;
1794 /* -1 is a write lock; any positive values are read locks */
1795 scp->fsLockCount = (afs_int32)statusp->lockCount;
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.
1800 scp->anyAccess = statusp->AnonymousAccess;
1802 if (userp != NULL) {
1803 cm_AddACLCache(scp, userp, statusp->CallerAccess);
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))) {
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.
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.
1818 * We do not remove storedata buffers because they will still be valid.
1823 cm_buf_t *bp, *prevBp, *nextBp;
1825 lock_ObtainWrite(&buf_globalLock);
1826 i = BUF_FILEHASH(&scp->fid);
1827 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1829 nextBp = bp->fileHashp;
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.
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;
1846 prevBp->fileHashp = nextBp;
1848 cm_data.buf_fileHashTablepp[i] = nextBp;
1850 nextBp->fileHashBackp = prevBp;
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) {
1859 /* we better find it */
1860 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1862 *lbpp = bp->hashp; /* hash out */
1865 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1867 lock_ReleaseMutex(&bp->mx);
1870 lock_ReleaseWrite(&buf_globalLock);
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.
1879 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1880 scp->mountPointStringp[0] = '\0';
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);
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));
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.
1898 * For directory buffers, only current dataVersion values are up to date.
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;
1906 if (RDR_Initialized) {
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.
1912 * If the current old DV is BAD, send a DV change notification.
1914 * If the DV has changed and request was not initiated by the
1915 * redirector, send a DV change notification.
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.
1923 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1925 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
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)) {
1931 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1932 dv_diff(dataVersion, scp->dataVersion) > activeRPCs) {
1937 scp->dataVersion = dataVersion;
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.
1944 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1947 * We just successfully merged status on the stat cache object.
1948 * This means that the associated volume must be online.
1952 cellp = cm_FindCellByID(scp->fid.cell, 0);
1953 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &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);
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)
1970 cm_EAccesClearParentEntries(&scp->fid);
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.
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);
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.
1995 * This function works by simply simulating a loss of the callback.
1997 * This function must be called with the scache locked.
1999 void cm_DiscardSCache(cm_scache_t *scp)
2001 lock_AssertWrite(&scp->rw);
2002 if (scp->cbServerp) {
2003 cm_PutServer(scp->cbServerp);
2004 scp->cbServerp = NULL;
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);
2014 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
2015 cm_VolStatus_Invalidate_DFS_Mapping(scp);
2017 /* Force mount points and symlinks to be re-evaluated */
2018 scp->mountPointStringp[0] = '\0';
2021 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
2023 afsFidp->Volume = fidp->volume;
2024 afsFidp->Vnode = fidp->vnode;
2025 afsFidp->Unique = fidp->unique;
2028 #ifdef DEBUG_REFCOUNT
2029 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2031 void cm_HoldSCacheNoLock(cm_scache_t *scp)
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);
2045 #ifdef DEBUG_REFCOUNT
2046 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
2048 void cm_HoldSCache(cm_scache_t *scp)
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);
2060 lock_ReleaseRead(&cm_scacheLock);
2063 #ifdef DEBUG_REFCOUNT
2064 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2066 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2071 osi_assertx(scp != NULL, "null cm_scache_t");
2072 lock_AssertAny(&cm_scacheLock);
2074 refCount = InterlockedDecrement(&scp->refCount);
2075 #ifdef DEBUG_REFCOUNT
2077 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
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);
2085 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2089 lockstate = lock_GetRWLockState(&cm_scacheLock);
2090 if (lockstate != OSI_RWLOCK_WRITEHELD)
2091 lock_ReleaseRead(&cm_scacheLock);
2093 lock_ReleaseWrite(&cm_scacheLock);
2095 lock_ObtainWrite(&scp->rw);
2096 if (scp->flags & CM_SCACHEFLAG_DELETED)
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);
2105 if (lockstate != OSI_RWLOCK_WRITEHELD)
2106 lock_ObtainRead(&cm_scacheLock);
2108 lock_ObtainWrite(&cm_scacheLock);
2110 lock_ReleaseWrite(&scp->rw);
2114 #ifdef DEBUG_REFCOUNT
2115 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2117 void cm_ReleaseSCache(cm_scache_t *scp)
2122 osi_assertx(scp != NULL, "null cm_scache_t");
2123 lock_ObtainRead(&cm_scacheLock);
2124 refCount = InterlockedDecrement(&scp->refCount);
2125 #ifdef DEBUG_REFCOUNT
2127 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
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);
2134 lock_ReleaseRead(&cm_scacheLock);
2136 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2138 lock_ObtainWrite(&scp->rw);
2139 if (scp->flags & CM_SCACHEFLAG_DELETED)
2142 lock_ObtainWrite(&cm_scacheLock);
2143 cm_RecycleSCache(scp, 0);
2144 lock_ReleaseWrite(&cm_scacheLock);
2146 lock_ReleaseWrite(&scp->rw);
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)
2157 hash = CM_SCACHE_HASH(fidp);
2159 osi_assertx(fidp->cell != 0, "unassigned cell value");
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;
2168 lock_ReleaseWrite(&cm_scacheLock);
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
2176 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2185 lock_ObtainRead(&cm_scacheLock);
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);
2190 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2193 char *srvStr = NULL;
2194 afs_uint32 srvStrRpc = TRUE;
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 */
2203 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2207 if (scp->cbExpires) {
2212 cbt[strlen(cbt)-1] = '\0';
2215 if (scp->volumeCreationDate) {
2216 t = scp->volumeCreationDate;
2219 cdrot = strdup(cdrot);
2220 cdrot[strlen(cdrot)-1] = '\0';
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);
2234 if (scp->fileLocksH) {
2235 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2236 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
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);
2247 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2248 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2253 RpcStringFree(&srvStr);
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);
2269 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2271 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
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);
2279 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2280 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2282 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2283 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
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);
2293 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2294 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2297 lock_ReleaseRead(&cm_scacheLock);