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);
177 if (!(volp->flags & CM_VOLUMEFLAG_DFS_VOLUME))
178 cm_EAccesClearParentEntries(&fid);
184 /* invalidate so next merge works fine;
185 * also initialize some flags */
187 _InterlockedAnd(&scp->flags,
188 ~( CM_SCACHEFLAG_DELETED
190 | CM_SCACHEFLAG_PURERO
191 | CM_SCACHEFLAG_OVERQUOTA
192 | CM_SCACHEFLAG_OUTOFSPACE));
193 scp->serverModTime = 0;
194 scp->dataVersion = CM_SCACHE_VERSION_BAD;
195 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
196 scp->bulkStatProgress = hzero;
198 scp->waitQueueT = NULL;
200 if (scp->cbServerp) {
201 cm_PutServer(scp->cbServerp);
202 scp->cbServerp = NULL;
206 scp->volumeCreationDate = 0;
214 /* remove from dnlc */
218 /* discard cached status; if non-zero, Close
219 * tried to store this to server but failed */
222 /* discard symlink info */
223 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
224 scp->mountPointStringp[0] = '\0';
225 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
226 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
228 /* reset locking info */
229 scp->fileLocksH = NULL;
230 scp->fileLocksT = NULL;
231 scp->serverLock = (-1);
232 scp->exclusiveLocks = 0;
233 scp->sharedLocks = 0;
234 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
235 scp->fsLockCount = 0;
237 /* not locked, but there can be no references to this guy
238 * while we hold the global refcount lock.
240 cm_FreeAllACLEnts(scp);
242 cm_ResetSCacheDirectory(scp, 0);
244 if (RDR_Initialized && callback) {
246 * We drop the cm_scacheLock because it may be required to
247 * satisfy an ioctl request from the redirector. It should
248 * be safe to hold the scp->rw lock here because at this
249 * point (a) the object has just been recycled so the fid
250 * is nul and there are no requests that could possibly
251 * be issued by the redirector that would depend upon it.
253 lock_ReleaseWrite(&cm_scacheLock);
254 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
255 fid.unique, fid.hash,
256 fileType, AFS_INVALIDATE_EXPIRED);
257 lock_ObtainWrite(&cm_scacheLock);
265 * called with cm_scacheLock write-locked; find a vnode to recycle.
266 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
267 * returns scp->rw write-locked.
270 cm_GetNewSCache(afs_uint32 locked)
272 cm_scache_t *scp = NULL;
273 cm_scache_t *scp_prev = NULL;
274 cm_scache_t *scp_next = NULL;
278 lock_AssertWrite(&cm_scacheLock);
280 lock_ObtainWrite(&cm_scacheLock);
282 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
283 /* There were no deleted scache objects that we could use. Try to find
284 * one that simply hasn't been used in a while.
286 for (attempt = 0 ; attempt < 128; attempt++) {
287 afs_uint32 count = 0;
289 for ( scp = cm_data.scacheLRULastp;
291 scp = (cm_scache_t *) osi_QPrev(&scp->q))
294 * We save the prev and next pointers in the
295 * LRU because we are going to drop the cm_scacheLock and
296 * the order of the list could change out from beneath us.
297 * If both changed, it means that this entry has been moved
298 * within the LRU and it should no longer be recycled.
300 scp_prev = (cm_scache_t *) osi_QPrev(&scp->q);
301 scp_next = (cm_scache_t *) osi_QNext(&scp->q);
304 /* It is possible for the refCount to be zero and for there still
305 * to be outstanding dirty buffers. If there are dirty buffers,
306 * we must not recycle the scp.
308 * If the object is in use by the redirector, then avoid recycling
309 * it unless we have to.
311 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
312 afs_uint32 buf_dirty = 0;
313 afs_uint32 buf_rdr = 0;
315 lock_ReleaseWrite(&cm_scacheLock);
316 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
318 buf_rdr = buf_RDRBuffersExist(&scp->fid);
320 if (!buf_dirty && !buf_rdr) {
325 success = lock_TryWrite(&scp->rw);
327 lock_ObtainWrite(&cm_scacheLock);
328 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
329 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
331 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
333 lock_ReleaseWrite(&scp->rw);
335 } else if (!success) {
336 osi_Log1(afsd_logp, "GetNewSCache failed to obtain lock scp 0x%p", scp);
340 /* Found a likely candidate. Save type and fid in case we succeed */
342 fileType = scp->fileType;
344 if (!cm_RecycleSCache(scp, 0)) {
345 /* we found an entry, so return it.
346 * remove from the LRU queue and put it back at the
347 * head of the LRU queue.
349 cm_AdjustScacheLRU(scp);
351 /* and we're done - SUCCESS */
352 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
355 lock_ReleaseWrite(&scp->rw);
358 osi_Log1(afsd_logp,"GetNewSCache redirector is holding extents scp 0x%p", scp);
360 osi_Log1(afsd_logp, "GetNewSCache dirty buffers 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))
366 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
373 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
376 * The entire LRU queue was walked and no available cm_scache_t was
377 * found. Drop the cm_scacheLock and sleep for a moment to give a
378 * chance for cm_scache_t objects to be released.
380 lock_ReleaseWrite(&cm_scacheLock);
382 lock_ObtainWrite(&cm_scacheLock);
390 /* if we get here, we should allocate a new scache entry. We either are below
391 * quota or we have a leak and need to allocate a new one to avoid panicing.
393 scp = cm_data.scacheBaseAddress + InterlockedIncrement(&cm_data.currentSCaches) - 1;
394 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
395 "invalid cm_scache_t address");
396 memset(scp, 0, sizeof(cm_scache_t));
397 scp->magic = CM_SCACHE_MAGIC;
398 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
399 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
400 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
402 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
404 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
405 scp->serverLock = -1;
406 scp->dataVersion = CM_SCACHE_VERSION_BAD;
407 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
408 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
410 /* and put it in the LRU queue */
411 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
412 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
414 scp->allNextp = cm_data.allSCachesp;
415 cm_data.allSCachesp = scp;
419 lock_ReleaseWrite(&cm_scacheLock);
424 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
427 fidp->volume = volume;
429 fidp->unique = unique;
430 CM_FID_GEN_HASH(fidp);
433 /* like strcmp, only for fids */
434 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
436 if (ap->hash != bp->hash)
438 if (ap->vnode != bp->vnode)
440 if (ap->volume != bp->volume)
442 if (ap->unique != bp->unique)
444 if (ap->cell != bp->cell)
449 void cm_fakeSCacheInit(int newFile)
452 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
453 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
454 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
455 cm_data.fakeSCache.cbExpires = (time_t)-1;
456 cm_data.fakeSCache.cbExpires = time(NULL);
457 /* can leave clientModTime at 0 */
458 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
459 cm_data.fakeSCache.unixModeBits = 0777;
460 cm_data.fakeSCache.length.LowPart = 1000;
461 cm_data.fakeSCache.linkCount = 1;
462 cm_data.fakeSCache.refCount = 1;
463 cm_data.fakeSCache.serverLock = -1;
464 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
466 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
467 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
468 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
469 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
473 cm_ValidateSCache(void)
475 cm_scache_t * scp, *lscp;
478 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
479 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
480 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
481 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
485 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
487 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
488 if (scp->magic != CM_SCACHE_MAGIC) {
489 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
490 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
493 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
494 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
495 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
498 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
499 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
500 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
503 if (i > cm_data.currentSCaches ) {
504 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
505 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
508 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
509 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
510 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
515 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
516 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
517 if (scp->magic != CM_SCACHE_MAGIC) {
518 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
519 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
522 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
523 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
524 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
527 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
528 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
529 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
532 if (i > cm_data.currentSCaches ) {
533 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
534 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
537 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
538 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
539 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
544 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
545 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
547 hash = CM_SCACHE_HASH(&scp->fid);
548 if (scp->magic != CM_SCACHE_MAGIC) {
549 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
550 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
553 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
554 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
555 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
558 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
559 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
560 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
564 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
565 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
571 return cm_dnlcValidate();
575 cm_SuspendSCache(void)
580 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
583 * After this call all servers are marked down.
584 * Do not clear the callbacks, instead change the
585 * expiration time so that the callbacks will be expired
586 * when the servers are marked back up. However, we
587 * want the callbacks to be preserved as long as the
588 * servers are down. That way if the machine resumes
589 * without network, the stat cache item will still be
594 lock_ObtainWrite(&cm_scacheLock);
595 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
596 if (scp->cbServerp) {
597 if (scp->flags & CM_SCACHEFLAG_PURERO) {
598 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
600 if (volp->cbExpiresRO == scp->cbExpires)
601 volp->cbExpiresRO = now+1;
605 scp->cbExpires = now+1;
608 lock_ReleaseWrite(&cm_scacheLock);
612 cm_ShutdownSCache(void)
614 cm_scache_t * scp, * nextp;
616 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
618 lock_ObtainWrite(&cm_scacheLock);
620 for ( scp = cm_data.allSCachesp; scp;
622 nextp = scp->allNextp;
623 lock_ReleaseWrite(&cm_scacheLock);
625 lock_ObtainWrite(&scp->dirlock);
627 lock_ObtainWrite(&scp->rw);
628 lock_ObtainWrite(&cm_scacheLock);
630 if (scp->randomACLp) {
631 cm_FreeAllACLEnts(scp);
634 if (scp->cbServerp) {
635 cm_PutServer(scp->cbServerp);
636 scp->cbServerp = NULL;
640 lock_ReleaseWrite(&scp->rw);
644 freeBtree(scp->dirBplus);
645 scp->dirBplus = NULL;
646 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
647 lock_ReleaseWrite(&scp->dirlock);
648 lock_FinalizeRWLock(&scp->dirlock);
650 lock_FinalizeRWLock(&scp->rw);
651 lock_FinalizeRWLock(&scp->bufCreateLock);
652 lock_FinalizeMutex(&scp->redirMx);
654 lock_ReleaseWrite(&cm_scacheLock);
656 return cm_dnlcShutdown();
659 void cm_InitSCache(int newFile, long maxSCaches)
661 static osi_once_t once;
663 if (osi_Once(&once)) {
664 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
666 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
667 cm_data.allSCachesp = NULL;
668 cm_data.currentSCaches = 0;
669 cm_data.maxSCaches = maxSCaches;
670 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
674 for ( scp = cm_data.allSCachesp; scp;
675 scp = scp->allNextp ) {
676 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
677 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
679 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
681 scp->cbServerp = NULL;
684 scp->volumeCreationDate = 0;
685 scp->fileLocksH = NULL;
686 scp->fileLocksT = NULL;
687 scp->serverLock = (-1);
688 scp->lastRefreshCycle = 0;
689 scp->exclusiveLocks = 0;
690 scp->sharedLocks = 0;
698 scp->dirBplus = NULL;
699 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
701 scp->waitQueueT = NULL;
702 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
704 scp->redirBufCount = 0;
705 scp->redirQueueT = NULL;
706 scp->redirQueueH = NULL;
707 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
710 cm_allFileLocks = NULL;
711 cm_freeFileLocks = NULL;
712 cm_lockRefreshCycle = 0;
713 cm_fakeSCacheInit(newFile);
714 cm_allFreeWaiters = NULL;
715 cm_dnlcInit(newFile);
720 /* version that doesn't bother creating the entry if we don't find it */
721 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
726 hash = CM_SCACHE_HASH(fidp);
728 if (fidp->cell == 0) {
732 lock_ObtainRead(&cm_scacheLock);
733 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
734 if (cm_FidCmp(fidp, &scp->fid) == 0) {
735 cm_HoldSCacheNoLock(scp);
736 lock_ConvertRToW(&cm_scacheLock);
737 cm_AdjustScacheLRU(scp);
738 lock_ReleaseWrite(&cm_scacheLock);
742 lock_ReleaseRead(&cm_scacheLock);
746 #ifdef DEBUG_REFCOUNT
747 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
748 cm_req_t *reqp, char * file, long line)
750 long cm_GetSCache(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
755 cm_scache_t *scp = NULL;
756 cm_scache_t *newScp = NULL;
758 cm_volume_t *volp = NULL;
760 int special = 0; // yj: boolean variable to test if file is on root.afs
762 extern cm_fid_t cm_rootFid;
765 hash = CM_SCACHE_HASH(fidp);
768 return CM_ERROR_INVAL;
770 #ifdef AFS_FREELANCE_CLIENT
771 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
772 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
773 !(fidp->vnode==0x1 && fidp->unique==0x1));
774 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
775 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
776 fidp->vnode==0x1 && fidp->unique==0x1);
779 // yj: check if we have the scp, if so, we don't need
780 // to do anything else
781 lock_ObtainRead(&cm_scacheLock);
782 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
783 if (cm_FidCmp(fidp, &scp->fid) == 0) {
784 #ifdef DEBUG_REFCOUNT
785 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
786 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
788 #ifdef AFS_FREELANCE_CLIENT
789 if (cm_freelanceEnabled && special &&
790 cm_data.fakeDirVersion != scp->dataVersion)
793 if (parentFidp && scp->parentVnode == 0) {
794 scp->parentVnode = parentFidp->vnode;
795 scp->parentUnique = parentFidp->unique;
797 cm_HoldSCacheNoLock(scp);
799 lock_ConvertRToW(&cm_scacheLock);
800 cm_AdjustScacheLRU(scp);
801 lock_ReleaseWrite(&cm_scacheLock);
805 lock_ReleaseRead(&cm_scacheLock);
807 // yj: when we get here, it means we don't have an scp
808 // so we need to either load it or fake it, depending
809 // on whether the file is "special", see below.
811 // yj: if we're trying to get an scp for a file that's
812 // on root.afs of homecell, we want to handle it specially
813 // because we have to fill in the status stuff 'coz we
814 // don't want trybulkstat to fill it in for us
815 #ifdef AFS_FREELANCE_CLIENT
816 if (cm_freelanceEnabled && isRoot) {
817 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
818 /* freelance: if we are trying to get the root scp for the first
819 * time, we will just put in a place holder entry.
824 if (cm_freelanceEnabled && special) {
825 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
827 if (cm_getLocalMountPointChange()) {
828 cm_clearLocalMountPointChange();
829 cm_reInitLocalMountPoints();
833 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
835 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
836 return CM_ERROR_WOULDBLOCK;
839 lock_ObtainWrite(&scp->rw);
842 cm_SetFid(&scp->dotdotFid,AFS_FAKE_ROOT_CELL_ID,AFS_FAKE_ROOT_VOL_ID,1,1);
844 scp->parentVnode = parentFidp->vnode;
845 scp->parentUnique = parentFidp->unique;
847 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
848 lock_ObtainWrite(&cm_scacheLock);
849 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
850 scp->nextp = cm_data.scacheHashTablep[hash];
851 cm_data.scacheHashTablep[hash] = scp;
852 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
854 refCount = InterlockedIncrement(&scp->refCount);
855 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
856 lock_ReleaseWrite(&cm_scacheLock);
858 /* must be called after the scp->fid is set */
859 cm_FreelanceFetchMountPointString(scp);
860 cm_FreelanceFetchFileType(scp);
862 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
863 scp->length.HighPart = 0;
865 scp->unixModeBits=0777;
866 scp->clientModTime=FakeFreelanceModTime;
867 scp->serverModTime=FakeFreelanceModTime;
868 scp->parentUnique = 0x1;
869 scp->parentVnode=0x1;
871 scp->dataVersion=cm_data.fakeDirVersion;
872 scp->bufDataVersionLow=cm_data.fakeDirVersion;
873 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
875 lock_ReleaseWrite(&scp->rw);
877 #ifdef DEBUG_REFCOUNT
878 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
879 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
884 #endif /* AFS_FREELANCE_CLIENT */
886 /* we don't have the fid, recycle something */
887 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
888 if (newScp == NULL) {
889 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
890 return CM_ERROR_WOULDBLOCK;
892 #ifdef DEBUG_REFCOUNT
893 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
895 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
897 /* otherwise, we need to find the volume */
898 if (!cm_freelanceEnabled || !isRoot) {
899 cellp = cm_FindCellByID(fidp->cell, 0);
901 /* put back newScp so it can be reused */
902 lock_ObtainWrite(&cm_scacheLock);
903 newScp->flags |= CM_SCACHEFLAG_DELETED;
904 cm_AdjustScacheLRU(newScp);
905 lock_ReleaseWrite(&newScp->rw);
906 lock_ReleaseWrite(&cm_scacheLock);
907 return CM_ERROR_NOSUCHCELL;
910 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
912 /* put back newScp so it can be reused */
913 lock_ObtainWrite(&cm_scacheLock);
914 newScp->flags |= CM_SCACHEFLAG_DELETED;
915 cm_AdjustScacheLRU(newScp);
916 lock_ReleaseWrite(&newScp->rw);
917 lock_ReleaseWrite(&cm_scacheLock);
923 * otherwise, we have the volume, now reverify that the scp doesn't
924 * exist, and proceed. make sure that we hold the cm_scacheLock
925 * write-locked until the scp is put into the hash table in order
928 lock_ObtainWrite(&cm_scacheLock);
929 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
930 if (cm_FidCmp(fidp, &scp->fid) == 0) {
931 #ifdef DEBUG_REFCOUNT
932 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
933 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
935 if (parentFidp && scp->parentVnode == 0) {
936 scp->parentVnode = parentFidp->vnode;
937 scp->parentUnique = parentFidp->unique;
941 cm_HoldSCacheNoLock(scp);
942 cm_AdjustScacheLRU(scp);
944 /* put back newScp so it can be reused */
945 newScp->flags |= CM_SCACHEFLAG_DELETED;
946 cm_AdjustScacheLRU(newScp);
947 lock_ReleaseWrite(&newScp->rw);
948 lock_ReleaseWrite(&cm_scacheLock);
957 if (!cm_freelanceEnabled || !isRoot) {
958 /* if this scache entry represents a volume root then we need
959 * to copy the dotdotFid from the volume structure where the
960 * "master" copy is stored (defect 11489)
962 if (volp->vol[ROVOL].ID == fidp->volume) {
963 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
964 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
965 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
966 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
967 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
968 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
969 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
971 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
972 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
976 scp->parentVnode = parentFidp->vnode;
977 scp->parentUnique = parentFidp->unique;
982 scp->nextp = cm_data.scacheHashTablep[hash];
983 cm_data.scacheHashTablep[hash] = scp;
984 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
985 refCount = InterlockedIncrement(&scp->refCount);
986 lock_ReleaseWrite(&cm_scacheLock);
987 lock_ReleaseWrite(&scp->rw);
988 #ifdef DEBUG_REFCOUNT
989 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
991 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
993 /* XXX - The following fields in the cm_scache are
1000 /* now we have a held scache entry; just return it */
1002 #ifdef DEBUG_REFCOUNT
1003 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
1004 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
1009 /* Returns a held reference to the scache's parent
1011 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
1015 cm_fid_t parent_fid;
1016 cm_scache_t * pscp = NULL;
1018 if (scp->parentVnode == 0)
1021 lock_ObtainWrite(&cm_scacheLock);
1022 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
1024 if (cm_FidCmp(&scp->fid, &parent_fid)) {
1025 i = CM_SCACHE_HASH(&parent_fid);
1026 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
1027 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
1028 cm_HoldSCacheNoLock(pscp);
1034 lock_ReleaseWrite(&cm_scacheLock);
1039 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1041 cm_scache_waiter_t * w;
1043 lock_ObtainWrite(&cm_scacheLock);
1044 if (cm_allFreeWaiters == NULL) {
1045 w = malloc(sizeof(*w));
1046 memset(w, 0, sizeof(*w));
1048 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1049 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1052 w->threadId = thrd_Current();
1054 cm_HoldSCacheNoLock(scp);
1058 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1059 lock_ReleaseWrite(&cm_scacheLock);
1061 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1064 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1066 cm_scache_waiter_t * w;
1069 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1071 lock_ObtainRead(&cm_scacheLock);
1072 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1074 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1075 if (w->flags == flags && w->bufp == bufp) {
1080 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1081 this_is_me = (w->threadId == thrd_Current());
1082 lock_ReleaseRead(&cm_scacheLock);
1085 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1089 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1091 lock_ObtainWrite(&cm_scacheLock);
1092 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1093 cm_ReleaseSCacheNoLock(scp);
1094 memset(w, 0, sizeof(*w));
1095 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1096 lock_ReleaseWrite(&cm_scacheLock);
1102 /* synchronize a fetch, store, read, write, fetch status or store status.
1103 * Called with scache mutex held, and returns with it held, but temporarily
1104 * drops it during the fetch.
1106 * At most one flag can be on in flags, if this is an RPC request.
1108 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1110 * There are a lot of weird restrictions here; here's an attempt to explain the
1111 * rationale for the concurrency restrictions implemented in this function.
1113 * First, although the file server will break callbacks when *another* machine
1114 * modifies a file or status block, the client itself is responsible for
1115 * concurrency control on its own requests. Callback breaking events are rare,
1116 * and simply invalidate any concurrent new status info.
1118 * In the absence of callback breaking messages, we need to know how to
1119 * synchronize incoming responses describing updates to files. We synchronize
1120 * operations that update the data version by comparing the data versions.
1121 * However, updates that do not update the data, but only the status, can't be
1122 * synchronized with fetches or stores, since there's nothing to compare
1123 * to tell which operation executed first at the server.
1125 * Thus, we can allow multiple ops that change file data, or dir data, and
1126 * fetches. However, status storing ops have to be done serially.
1128 * Furthermore, certain data-changing ops are incompatible: we can't read or
1129 * write a buffer while doing a truncate. We can't read and write the same
1130 * buffer at the same time, or write while fetching or storing, or read while
1131 * fetching a buffer (this may change). We can't fetch and store at the same
1134 * With respect to status, we can't read and write at the same time, read while
1135 * fetching, write while fetching or storing, or fetch and store at the same time.
1137 * We can't allow a get callback RPC to run in concurrently with something that
1138 * will return updated status, since we could start a call, have the server
1139 * return status, have another machine make an update to the status (which
1140 * doesn't change serverModTime), have the original machine get a new callback,
1141 * and then have the original machine merge in the early, old info from the
1142 * first call. At this point, the easiest way to avoid this problem is to have
1143 * getcallback calls conflict with all others for the same vnode. Other calls
1144 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1145 * vnode must be careful not to merge in their status unless they have obtained
1146 * a callback from the start of their call.
1148 * Note added 1/23/96
1149 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1150 * Each such RPC passes a FileLength parameter, which the server uses to do
1151 * pre-truncation if necessary. So if two RPC's are processed out of order at
1152 * the server, the one with the smaller FileLength will be processed last,
1153 * possibly resulting in a bogus truncation. The simplest way to avoid this
1154 * is to serialize all StoreData RPC's. This is the reason we defined
1155 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1157 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1158 * readers which may be requesting overlapping ranges.
1160 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1161 afs_uint32 rights, afs_uint32 flags)
1163 osi_queueData_t *qdp;
1166 afs_uint32 outRights;
1168 afs_uint32 sleep_scp_flags = 0;
1169 afs_uint32 sleep_buf_cmflags = 0;
1170 afs_uint32 sleep_scp_bufs = 0;
1172 afs_int32 waitCount;
1173 afs_int32 waitRequests;
1175 lock_AssertWrite(&scp->rw);
1177 /* lookup this first */
1178 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1181 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1184 /* Do the access check. Now we don't really do the access check
1185 * atomically, since the caller doesn't expect the parent dir to be
1186 * returned locked, and that is what we'd have to do to prevent a
1187 * callback breaking message on the parent due to a setacl call from
1188 * being processed while we're running. So, instead, we check things
1189 * here, and if things look fine with the access, we proceed to finish
1190 * the rest of this check. Sort of a hack, but probably good enough.
1194 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1195 /* if we're bringing in a new status block, ensure that
1196 * we aren't already doing so, and that no one is
1197 * changing the status concurrently, either. We need
1198 * to do this, even if the status is of a different
1199 * type, since we don't have the ability to figure out,
1200 * in the AFS 3 protocols, which status-changing
1201 * operation ran first, or even which order a read and
1202 * a write occurred in.
1204 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1205 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1206 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1210 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1211 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1212 /* if we're going to make an RPC to change the status, make sure
1213 * that no one is bringing in or sending out the status.
1215 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1216 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1217 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1220 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1221 (scp->bufReadsp || scp->bufWritesp)) {
1222 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1226 if (flags & CM_SCACHESYNC_FETCHDATA) {
1227 /* if we're bringing in a new chunk of data, make sure that
1228 * nothing is happening to that chunk, and that we aren't
1229 * changing the basic file status info, either.
1231 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1232 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1233 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1236 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1237 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1241 if (flags & CM_SCACHESYNC_STOREDATA) {
1242 /* same as fetch data */
1243 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1244 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1245 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1248 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1249 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1254 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1255 /* Don't allow concurrent StoreData RPC's */
1256 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1257 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1262 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1263 /* Don't allow more than one BKG store request */
1264 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1265 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1270 if (flags & CM_SCACHESYNC_LOCK) {
1271 /* Don't allow concurrent fiddling with lock lists */
1272 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1273 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1278 /* now the operations that don't correspond to making RPCs */
1279 if (flags & CM_SCACHESYNC_GETSTATUS) {
1280 /* we can use the status that's here, if we're not
1281 * bringing in new status.
1283 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1284 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1288 if (flags & CM_SCACHESYNC_SETSTATUS) {
1289 /* we can make a change to the local status, as long as
1290 * the status isn't changing now.
1292 * If we're fetching or storing a chunk of data, we can
1293 * change the status locally, since the fetch/store
1294 * operations don't change any of the data that we're
1297 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1298 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1299 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1303 if (flags & CM_SCACHESYNC_READ) {
1304 /* we're going to read the data, make sure that the
1305 * status is available, and that the data is here. It
1306 * is OK to read while storing the data back.
1308 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1309 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1312 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1313 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1316 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1317 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1321 if (flags & CM_SCACHESYNC_WRITE) {
1322 /* don't write unless the status is stable and the chunk
1325 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1326 CM_SCACHEFLAG_SIZESTORING)) {
1327 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1330 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1332 CM_BUF_CMWRITING))) {
1333 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1335 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1336 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1337 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1343 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1344 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1345 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1348 if (cm_EAccesFindEntry(userp, &scp->fid))
1349 return CM_ERROR_NOACCESS;
1352 lock_ReleaseMutex(&bufp->mx);
1353 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1355 lock_ReleaseWrite(&scp->rw);
1356 lock_ObtainMutex(&bufp->mx);
1357 lock_ObtainWrite(&scp->rw);
1361 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1367 /* can't check access rights without a callback */
1368 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1370 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1371 return CM_ERROR_READONLY;
1373 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1374 if (~outRights & rights)
1375 return CM_ERROR_NOACCESS;
1378 /* we don't know the required access rights */
1379 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1380 code = cm_GetAccessRights(scp, userp, reqp);
1382 lock_ReleaseWrite(&scp->rw);
1383 lock_ObtainMutex(&bufp->mx);
1384 lock_ObtainWrite(&scp->rw);
1392 if (flags & CM_SCACHESYNC_BULKREAD) {
1393 /* Don't allow concurrent fiddling with lock lists */
1394 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1395 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1400 /* if we get here, we're happy */
1404 /* first check if we're not supposed to wait: fail
1405 * in this case, returning with everything still locked.
1407 if (flags & CM_SCACHESYNC_NOWAIT)
1408 return CM_ERROR_WOULDBLOCK;
1410 /* These are used for minidump debugging */
1411 sleep_scp_flags = scp->flags; /* so we know why we slept */
1412 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1413 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1415 /* wait here, then try again */
1416 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1418 waitCount = InterlockedIncrement(&scp->waitCount);
1419 waitRequests = InterlockedIncrement(&scp->waitRequests);
1420 if (waitCount > 1) {
1421 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1422 scp, waitCount, waitRequests);
1424 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1425 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1428 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1432 lock_ReleaseMutex(&bufp->mx);
1433 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1435 lock_ObtainMutex(&bufp->mx);
1436 lock_ObtainWrite(&scp->rw);
1437 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1439 cm_UpdateServerPriority();
1441 waitCount = InterlockedDecrement(&scp->waitCount);
1442 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1443 scp, waitCount, scp->waitRequests);
1444 if (waitCount == 0) {
1445 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1446 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1447 scp->waitRequests = 0;
1449 } /* big while loop */
1451 /* now, update the recorded state for RPC-type calls */
1452 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1453 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1454 if (flags & CM_SCACHESYNC_STORESTATUS)
1455 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1456 if (flags & CM_SCACHESYNC_SETSIZE)
1457 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1458 if (flags & CM_SCACHESYNC_STORESIZE)
1459 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1460 if (flags & CM_SCACHESYNC_GETCALLBACK)
1461 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1462 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1463 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1464 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1465 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1466 if (flags & CM_SCACHESYNC_LOCK)
1467 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1468 if (flags & CM_SCACHESYNC_BULKREAD)
1469 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1471 /* now update the buffer pointer */
1472 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1473 /* ensure that the buffer isn't already in the I/O list */
1474 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1475 tbufp = osi_GetQData(qdp);
1476 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1479 /* queue a held reference to the buffer in the "reading" I/O list */
1480 qdp = osi_QDAlloc();
1481 osi_SetQData(qdp, bufp);
1484 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1485 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1488 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1489 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1490 "attempting to store extents on a non-file object");
1492 /* ensure that the buffer isn't already in the I/O list */
1493 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1494 tbufp = osi_GetQData(qdp);
1495 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1498 /* queue a held reference to the buffer in the "writing" I/O list */
1499 qdp = osi_QDAlloc();
1500 osi_SetQData(qdp, bufp);
1502 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1503 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1506 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1507 /* mark the buffer as being written to. */
1508 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1514 /* for those syncops that setup for RPCs.
1515 * Called with scache locked.
1517 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1519 osi_queueData_t *qdp;
1522 lock_AssertWrite(&scp->rw);
1524 /* now, update the recorded state for RPC-type calls */
1525 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1526 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1527 if (flags & CM_SCACHESYNC_STORESTATUS)
1528 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1529 if (flags & CM_SCACHESYNC_SETSIZE)
1530 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1531 if (flags & CM_SCACHESYNC_STORESIZE)
1532 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1533 if (flags & CM_SCACHESYNC_GETCALLBACK)
1534 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1535 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1536 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1537 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1538 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1539 if (flags & CM_SCACHESYNC_LOCK)
1540 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1541 if (flags & CM_SCACHESYNC_BULKREAD)
1542 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1544 /* now update the buffer pointer */
1545 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1548 /* ensure that the buffer is in the I/O list */
1549 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1550 tbufp = osi_GetQData(qdp);
1555 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1559 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1560 if (bufp->flags & CM_BUF_WAITING) {
1561 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1562 osi_Wakeup((LONG_PTR) &bufp);
1568 /* now update the buffer pointer */
1569 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1571 /* ensure that the buffer is in the I/O list */
1572 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1573 tbufp = osi_GetQData(qdp);
1578 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1582 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1583 if (bufp->flags & CM_BUF_WAITING) {
1584 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1585 osi_Wakeup((LONG_PTR) &bufp);
1591 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1592 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1593 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1596 /* and wakeup anyone who is waiting */
1597 if ((scp->flags & CM_SCACHEFLAG_WAITING) ||
1598 !osi_QIsEmpty(&scp->waitQueueH)) {
1599 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1600 osi_Wakeup((LONG_PTR) &scp->flags);
1605 dv_diff(afs_uint64 dv1, afs_uint64 dv2)
1607 if ( dv1 - dv2 > 0x7FFFFFFF )
1608 return (afs_uint32)(dv2 - dv1);
1610 return (afs_uint32)(dv1 - dv2);
1613 /* merge in a response from an RPC. The scp must be locked, and the callback
1616 * Don't overwrite any status info that is dirty, since we could have a store
1617 * operation (such as store data) that merges some info in, and we don't want
1618 * to lose the local updates. Typically, there aren't many updates we do
1619 * locally, anyway, probably only mtime.
1621 * There is probably a bug in here where a chmod (which doesn't change
1622 * serverModTime) that occurs between two fetches, both of whose responses are
1623 * handled after the callback breaking is done, but only one of whose calls
1624 * started before that, can cause old info to be merged from the first call.
1626 void cm_MergeStatus(cm_scache_t *dscp,
1627 cm_scache_t *scp, AFSFetchStatus *statusp,
1628 AFSVolSync *volsyncp,
1629 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1631 afs_uint64 dataVersion;
1632 struct cm_volume *volp = NULL;
1633 struct cm_cell *cellp = NULL;
1634 int rdr_invalidate = 0;
1635 afs_uint32 activeRPCs;
1637 lock_AssertWrite(&scp->rw);
1639 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1641 // yj: i want to create some fake status for the /afs directory and the
1642 // entries under that directory
1643 #ifdef AFS_FREELANCE_CLIENT
1644 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1645 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1646 if (scp == cm_data.rootSCachep) {
1647 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1648 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1649 statusp->Length = cm_fakeDirSize;
1650 statusp->Length_hi = 0;
1652 statusp->FileType = scp->fileType;
1653 statusp->Length = scp->length.LowPart;
1654 statusp->Length_hi = scp->length.HighPart;
1656 statusp->InterfaceVersion = 0x1;
1657 statusp->LinkCount = scp->linkCount;
1658 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1659 statusp->Author = 0x1;
1660 statusp->Owner = 0x0;
1661 statusp->CallerAccess = 0x9;
1662 statusp->AnonymousAccess = 0x9;
1663 statusp->UnixModeBits = 0777;
1664 statusp->ParentVnode = 0x1;
1665 statusp->ParentUnique = 0x1;
1666 statusp->ResidencyMask = 0;
1667 statusp->ClientModTime = FakeFreelanceModTime;
1668 statusp->ServerModTime = FakeFreelanceModTime;
1670 statusp->SyncCounter = 0;
1671 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1672 statusp->lockCount = 0;
1673 statusp->errorCode = 0;
1675 #endif /* AFS_FREELANCE_CLIENT */
1677 if (statusp->InterfaceVersion != 0x1) {
1678 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p Invalid InterfaceVersion %u",
1679 scp, statusp->InterfaceVersion);
1683 if (statusp->errorCode != 0) {
1684 switch (statusp->errorCode) {
1689 cm_EAccesAddEntry(userp, &scp->fid, &dscp->fid);
1691 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1693 if (scp->fid.vnode & 0x1)
1694 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1696 scp->fileType = 0; /* unknown */
1698 scp->serverModTime = 0;
1699 scp->clientModTime = 0;
1700 scp->length.LowPart = 0;
1701 scp->length.HighPart = 0;
1702 scp->serverLength.LowPart = 0;
1703 scp->serverLength.HighPart = 0;
1707 scp->unixModeBits = 0;
1709 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1710 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1711 scp->fsLockCount = 0;
1713 if (dscp && dscp != scp) {
1714 scp->parentVnode = dscp->fid.vnode;
1715 scp->parentUnique = dscp->fid.unique;
1717 scp->parentVnode = 0;
1718 scp->parentUnique = 0;
1721 if (RDR_Initialized)
1725 dataVersion = statusp->dataVersionHigh;
1727 dataVersion |= statusp->DataVersion;
1729 if (!(flags & CM_MERGEFLAG_FORCE) &&
1730 dataVersion < scp->dataVersion &&
1731 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1733 cellp = cm_FindCellByID(scp->fid.cell, 0);
1734 if (scp->cbServerp) {
1735 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1736 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1737 osi_Log2(afsd_logp, "old data from server %x volume %s",
1738 scp->cbServerp->addr.sin_addr.s_addr,
1739 volp ? volp->namep : "(unknown)");
1742 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1743 scp, scp->dataVersion, dataVersion);
1744 /* we have a number of data fetch/store operations running
1745 * concurrently, and we can tell which one executed last at the
1746 * server by its mtime.
1747 * Choose the one with the largest mtime, and ignore the rest.
1749 * These concurrent calls are incompatible with setting the
1750 * mtime, so we won't have a locally changed mtime here.
1752 * We could also have ACL info for a different user than usual,
1753 * in which case we have to do that part of the merge, anyway.
1754 * We won't have to worry about the info being old, since we
1755 * won't have concurrent calls
1756 * that change file status running from this machine.
1758 * Added 3/17/98: if we see data version regression on an RO
1759 * file, it's probably due to a server holding an out-of-date
1760 * replica, rather than to concurrent RPC's. Failures to
1761 * release replicas are now flagged by the volserver, but only
1762 * since AFS 3.4 5.22, so there are plenty of clients getting
1763 * out-of-date replicas out there.
1765 * If we discover an out-of-date replica, by this time it's too
1766 * late to go to another server and retry. Also, we can't
1767 * reject the merge, because then there is no way for
1768 * GetAccess to do its work, and the caller gets into an
1769 * infinite loop. So we just grin and bear it.
1771 if (!(scp->flags & CM_SCACHEFLAG_RO))
1776 * The first field of the volsync parameter is supposed to be the
1777 * volume creation date. Unfortunately, pre-OpenAFS 1.4.11 and 1.6.0
1778 * file servers do not populate the VolSync structure for BulkStat and
1779 * InlineBulkStat RPCs. As a result, the volume creation date is not
1780 * trustworthy when status is obtained via [Inline]BulkStatus RPCs.
1781 * If cm_readonlyVolumeVersioning is set, it is assumed that all file
1782 * servers populate the VolSync structure at all times.
1784 if (cm_readonlyVolumeVersioning || !(flags & CM_MERGEFLAG_BULKSTAT))
1785 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1787 scp->volumeCreationDate = 0;
1789 scp->serverModTime = statusp->ServerModTime;
1791 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1792 scp->clientModTime = statusp->ClientModTime;
1794 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1795 scp->length.LowPart = statusp->Length;
1796 scp->length.HighPart = statusp->Length_hi;
1799 scp->serverLength.LowPart = statusp->Length;
1800 scp->serverLength.HighPart = statusp->Length_hi;
1802 scp->linkCount = statusp->LinkCount;
1803 scp->owner = statusp->Owner;
1804 scp->group = statusp->Group;
1805 scp->unixModeBits = statusp->UnixModeBits & 07777;
1807 if (statusp->FileType == File)
1808 scp->fileType = CM_SCACHETYPE_FILE;
1809 else if (statusp->FileType == Directory)
1810 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1811 else if (statusp->FileType == SymbolicLink) {
1812 if ((scp->unixModeBits & 0111) == 0)
1813 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1815 scp->fileType = CM_SCACHETYPE_SYMLINK;
1818 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1819 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1821 /* and other stuff */
1822 scp->parentVnode = statusp->ParentVnode;
1823 scp->parentUnique = statusp->ParentUnique;
1825 /* -1 is a write lock; any positive values are read locks */
1826 scp->fsLockCount = (afs_int32)statusp->lockCount;
1828 /* and merge in the private acl cache info, if this is more than the public
1829 * info; merge in the public stuff in any case.
1831 scp->anyAccess = statusp->AnonymousAccess;
1833 if (userp != NULL) {
1834 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1837 if (dataVersion != 0 && scp->dataVersion != CM_SCACHE_VERSION_BAD &&
1838 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && (dataVersion != scp->dataVersion) ||
1839 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1840 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs))) {
1842 * We now know that all of the data buffers that we have associated
1843 * with this scp are invalid. Subsequent operations will go faster
1844 * if the buffers are removed from the hash tables.
1846 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1847 * those version numbers will be updated as part of the directory operation.
1849 * We do not remove storedata buffers because they will still be valid.
1854 cm_buf_t *bp, *prevBp, *nextBp;
1856 lock_ObtainWrite(&buf_globalLock);
1857 i = BUF_FILEHASH(&scp->fid);
1858 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1860 nextBp = bp->fileHashp;
1862 * if the buffer belongs to this stat cache entry
1863 * and the buffer mutex can be obtained, check the
1864 * reference count and if it is zero, remove the buffer
1865 * from the hash tables. If there are references,
1866 * the buffer might be updated to the current version
1867 * so leave it in place.
1869 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1870 lock_TryMutex(&bp->mx)) {
1871 if (bp->refCount == 0 &&
1872 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1873 !(bp->qFlags & CM_BUF_QREDIR)) {
1874 prevBp = bp->fileHashBackp;
1875 bp->fileHashBackp = bp->fileHashp = NULL;
1877 prevBp->fileHashp = nextBp;
1879 cm_data.buf_fileHashTablepp[i] = nextBp;
1881 nextBp->fileHashBackp = prevBp;
1883 j = BUF_HASH(&bp->fid, &bp->offset);
1884 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1885 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1890 /* we better find it */
1891 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1893 *lbpp = bp->hashp; /* hash out */
1896 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1898 lock_ReleaseMutex(&bp->mx);
1901 lock_ReleaseWrite(&buf_globalLock);
1904 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1905 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1906 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1908 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1909 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1910 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1911 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1912 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1915 /* We maintain a range of buffer dataVersion values which are considered
1916 * valid. This avoids the need to update the dataVersion on each buffer
1917 * object during an uncontested storeData operation. As a result this
1918 * merge status no longer has performance characteristics derived from
1919 * the size of the file.
1921 * For directory buffers, only current dataVersion values are up to date.
1923 if (((flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (dv_diff(dataVersion, scp->dataVersion) > activeRPCs)) ||
1924 (!(flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (scp->dataVersion != dataVersion)) ||
1925 scp->bufDataVersionLow == CM_SCACHE_VERSION_BAD ||
1926 scp->fileType == CM_SCACHETYPE_DIRECTORY)
1927 scp->bufDataVersionLow = dataVersion;
1929 if (RDR_Initialized) {
1931 * The redirector maintains its own cached status information which
1932 * must be updated when a DV change occurs that is not the result
1933 * of a redirector initiated data change.
1935 * If the current old DV is BAD, send a DV change notification.
1937 * If the DV has changed and request was not initiated by the
1938 * redirector, send a DV change notification.
1940 * If the request was initiated by the redirector, send a notification
1941 * for store and directory operations that result in a DV change greater
1942 * than the number of active RPCs or any other operation that results
1943 * in an unexpected DV change such as FetchStatus.
1946 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1948 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
1950 } else if (reqp->flags & CM_REQ_SOURCE_REDIR) {
1951 if (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1952 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs - 1)) {
1954 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1955 dv_diff(dataVersion, scp->dataVersion) > activeRPCs) {
1960 scp->dataVersion = dataVersion;
1963 * If someone is waiting for status information, we can wake them up
1964 * now even though the entity that issued the FetchStatus may not
1965 * have completed yet.
1967 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1970 * We just successfully merged status on the stat cache object.
1971 * This means that the associated volume must be online.
1975 cellp = cm_FindCellByID(scp->fid.cell, 0);
1976 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1979 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1980 if (statep->state != vl_online) {
1981 lock_ObtainWrite(&volp->rw);
1982 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1983 statep->state = vl_online;
1984 lock_ReleaseWrite(&volp->rw);
1988 /* Remove cached EACCES / EPERM errors if the file is a directory */
1989 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
1990 !(volp && (volp->flags & CM_VOLUMEFLAG_DFS_VOLUME)) &&
1991 !cm_accessPerFileCheck)
1993 cm_EAccesClearParentEntries(&scp->fid);
2001 * The scache rw lock cannot be held across the invalidation.
2002 * Doing so can result in deadlocks with other threads processing
2003 * requests initiated by the afs redirector.
2005 if (rdr_invalidate) {
2006 lock_ReleaseWrite(&scp->rw);
2007 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
2008 scp->fid.unique, scp->fid.hash,
2009 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
2010 lock_ObtainWrite(&scp->rw);
2014 /* note that our stat cache info is incorrect, so force us eventually
2015 * to stat the file again. There may be dirty data associated with
2016 * this vnode, and we want to preserve that information.
2018 * This function works by simply simulating a loss of the callback.
2020 * This function must be called with the scache locked.
2022 void cm_DiscardSCache(cm_scache_t *scp)
2024 lock_AssertWrite(&scp->rw);
2025 if (scp->cbServerp) {
2026 cm_PutServer(scp->cbServerp);
2027 scp->cbServerp = NULL;
2031 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
2032 cm_dnlcPurgedp(scp);
2033 cm_dnlcPurgevp(scp);
2034 cm_FreeAllACLEnts(scp);
2036 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
2037 cm_VolStatus_Invalidate_DFS_Mapping(scp);
2040 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
2042 afsFidp->Volume = fidp->volume;
2043 afsFidp->Vnode = fidp->vnode;
2044 afsFidp->Unique = fidp->unique;
2047 #ifdef DEBUG_REFCOUNT
2048 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2050 void cm_HoldSCacheNoLock(cm_scache_t *scp)
2055 osi_assertx(scp != NULL, "null cm_scache_t");
2056 lock_AssertAny(&cm_scacheLock);
2057 refCount = InterlockedIncrement(&scp->refCount);
2058 #ifdef DEBUG_REFCOUNT
2059 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
2060 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
2064 #ifdef DEBUG_REFCOUNT
2065 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
2067 void cm_HoldSCache(cm_scache_t *scp)
2072 osi_assertx(scp != NULL, "null cm_scache_t");
2073 lock_ObtainRead(&cm_scacheLock);
2074 refCount = InterlockedIncrement(&scp->refCount);
2075 #ifdef DEBUG_REFCOUNT
2076 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
2077 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
2079 lock_ReleaseRead(&cm_scacheLock);
2082 #ifdef DEBUG_REFCOUNT
2083 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2085 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2090 osi_assertx(scp != NULL, "null cm_scache_t");
2091 lock_AssertAny(&cm_scacheLock);
2093 refCount = InterlockedDecrement(&scp->refCount);
2094 #ifdef DEBUG_REFCOUNT
2096 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
2098 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2099 #ifdef DEBUG_REFCOUNT
2100 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
2101 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
2104 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2108 lockstate = lock_GetRWLockState(&cm_scacheLock);
2109 if (lockstate != OSI_RWLOCK_WRITEHELD)
2110 lock_ReleaseRead(&cm_scacheLock);
2112 lock_ReleaseWrite(&cm_scacheLock);
2114 lock_ObtainWrite(&scp->rw);
2115 if (scp->flags & CM_SCACHEFLAG_DELETED)
2118 if (refCount == 0 && deleted) {
2119 lock_ObtainWrite(&cm_scacheLock);
2120 cm_RecycleSCache(scp, 0);
2121 if (lockstate != OSI_RWLOCK_WRITEHELD)
2122 lock_ConvertWToR(&cm_scacheLock);
2124 if (lockstate != OSI_RWLOCK_WRITEHELD)
2125 lock_ObtainRead(&cm_scacheLock);
2127 lock_ObtainWrite(&cm_scacheLock);
2129 lock_ReleaseWrite(&scp->rw);
2133 #ifdef DEBUG_REFCOUNT
2134 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2136 void cm_ReleaseSCache(cm_scache_t *scp)
2141 osi_assertx(scp != NULL, "null cm_scache_t");
2142 lock_ObtainRead(&cm_scacheLock);
2143 refCount = InterlockedDecrement(&scp->refCount);
2144 #ifdef DEBUG_REFCOUNT
2146 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2148 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2149 #ifdef DEBUG_REFCOUNT
2150 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2151 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2153 lock_ReleaseRead(&cm_scacheLock);
2155 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2157 lock_ObtainWrite(&scp->rw);
2158 if (scp->flags & CM_SCACHEFLAG_DELETED)
2161 lock_ObtainWrite(&cm_scacheLock);
2162 cm_RecycleSCache(scp, 0);
2163 lock_ReleaseWrite(&cm_scacheLock);
2165 lock_ReleaseWrite(&scp->rw);
2169 /* just look for the scp entry to get filetype */
2170 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2171 int cm_FindFileType(cm_fid_t *fidp)
2176 hash = CM_SCACHE_HASH(fidp);
2178 osi_assertx(fidp->cell != 0, "unassigned cell value");
2180 lock_ObtainWrite(&cm_scacheLock);
2181 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2182 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2183 lock_ReleaseWrite(&cm_scacheLock);
2184 return scp->fileType;
2187 lock_ReleaseWrite(&cm_scacheLock);
2191 /* dump all scp's that have reference count > 0 to a file.
2192 * cookie is used to identify this batch for easy parsing,
2193 * and it a string provided by a caller
2195 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2204 lock_ObtainRead(&cm_scacheLock);
2206 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2207 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2209 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2212 char *srvStr = NULL;
2213 afs_uint32 srvStrRpc = TRUE;
2217 if (scp->cbServerp) {
2218 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2219 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2220 srvStr = malloc(16); /* enough for 255.255.255.255 */
2222 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2226 if (scp->cbExpires) {
2231 cbt[strlen(cbt)-1] = '\0';
2234 if (scp->volumeCreationDate) {
2235 t = scp->volumeCreationDate;
2238 cdrot = strdup(cdrot);
2239 cdrot[strlen(cdrot)-1] = '\0';
2243 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2244 "mpDV=%I64d mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2245 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2246 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2247 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mpDataVersion, scp->mountPointStringp,
2248 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2249 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2250 cdrot ? cdrot : "<none>", scp->refCount);
2251 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2253 if (scp->fileLocksH) {
2254 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2255 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2257 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2258 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2259 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2260 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2261 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2262 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2263 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2266 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2267 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2272 RpcStringFree(&srvStr);
2282 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2283 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2284 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2285 cookie, cm_data.scacheHashTableSize);
2286 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2288 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2290 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2292 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2293 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2294 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2298 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2299 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2301 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2302 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2304 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2305 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2306 sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2307 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2308 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2309 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2312 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2313 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2316 lock_ReleaseRead(&cm_scacheLock);