2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
26 #include <afs/unified_afs.h>
28 /*extern void afsi_log(char *pattern, ...);*/
30 extern osi_hyper_t hzero;
33 osi_queue_t *cm_allFileLocks;
34 osi_queue_t *cm_freeFileLocks;
35 unsigned long cm_lockRefreshCycle;
37 /* lock for globals */
38 osi_rwlock_t cm_scacheLock;
40 /* Dummy scache entry for use with pioctl fids */
41 cm_scache_t cm_fakeSCache;
43 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
45 #ifdef AFS_FREELANCE_CLIENT
46 extern osi_mutex_t cm_Freelance_Lock;
50 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
54 lock_ObtainWrite(&cm_data.rootSCachep->rw);
55 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
56 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
58 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
59 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
61 return cm_data.rootSCachep;
65 /* must be called with cm_scacheLock write-locked! */
66 void cm_AdjustScacheLRU(cm_scache_t *scp)
68 lock_AssertWrite(&cm_scacheLock);
69 if (!(scp->flags & CM_SCACHEFLAG_DELETED)) {
70 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
71 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
75 /* call with cm_scacheLock write-locked and scp rw held */
76 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
82 lock_AssertWrite(&cm_scacheLock);
83 lock_AssertWrite(&scp->rw);
84 if (scp->flags & CM_SCACHEFLAG_INHASH) {
85 /* hash it out first */
86 i = CM_SCACHE_HASH(&scp->fid);
87 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
89 lscpp = &tscp->nextp, tscp = tscp->nextp) {
93 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
100 /* called with cm_scacheLock and scp write-locked */
101 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
104 /* destroy directory Bplus Tree */
106 LARGE_INTEGER start, end;
108 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
110 * We are not holding the dirlock and obtaining it
111 * requires that we drop the scp->rw. As a result
112 * we will leave the dirBplus tree intact but
113 * invalidate the version number so that whatever
114 * operation is currently active can safely complete
115 * but the contents will be ignored on the next
116 * directory operation.
118 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
122 QueryPerformanceCounter(&start);
124 freeBtree(scp->dirBplus);
125 scp->dirBplus = NULL;
126 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
127 QueryPerformanceCounter(&end);
130 lock_ReleaseWrite(&scp->dirlock);
132 bplus_free_time += (end.QuadPart - start.QuadPart);
137 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
138 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
144 lock_AssertWrite(&cm_scacheLock);
145 lock_AssertWrite(&scp->rw);
147 if (scp->refCount != 0) {
151 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
152 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
154 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
159 if (scp->redirBufCount != 0) {
164 fileType = scp->fileType;
165 callback = scp->cbExpires ? 1 : 0;
167 cm_RemoveSCacheFromHashTable(scp);
169 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
170 !cm_accessPerFileCheck) {
171 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
174 if (!(volp->flags & CM_VOLUMEFLAG_DFS_VOLUME))
175 cm_EAccesClearParentEntries(&fid);
181 /* invalidate so next merge works fine;
182 * also initialize some flags */
184 _InterlockedAnd(&scp->flags,
185 ~( CM_SCACHEFLAG_DELETED
187 | CM_SCACHEFLAG_PURERO
188 | CM_SCACHEFLAG_OVERQUOTA
189 | CM_SCACHEFLAG_OUTOFSPACE));
190 scp->serverModTime = 0;
191 scp->dataVersion = CM_SCACHE_VERSION_BAD;
192 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
193 scp->bulkStatProgress = hzero;
195 scp->waitQueueT = NULL;
197 if (scp->cbServerp) {
198 cm_PutServer(scp->cbServerp);
199 scp->cbServerp = NULL;
203 scp->volumeCreationDate = 0;
211 /* remove from dnlc */
215 /* discard cached status; if non-zero, Close
216 * tried to store this to server but failed */
219 /* discard symlink info */
220 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
221 scp->mountPointStringp[0] = '\0';
222 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
223 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
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) {
322 success = lock_TryWrite(&scp->rw);
324 lock_ObtainWrite(&cm_scacheLock);
325 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
326 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
328 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
330 lock_ReleaseWrite(&scp->rw);
332 } else if (!success) {
333 osi_Log1(afsd_logp, "GetNewSCache failed to obtain lock scp 0x%p", scp);
337 /* Found a likely candidate. Save type and fid in case we succeed */
339 fileType = scp->fileType;
341 if (!cm_RecycleSCache(scp, 0)) {
342 /* we found an entry, so return it.
343 * remove from the LRU queue and put it back at the
344 * head of the LRU queue.
346 cm_AdjustScacheLRU(scp);
348 /* and we're done - SUCCESS */
349 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
352 lock_ReleaseWrite(&scp->rw);
355 osi_Log1(afsd_logp,"GetNewSCache redirector is holding extents scp 0x%p", scp);
357 osi_Log1(afsd_logp, "GetNewSCache dirty buffers scp 0x%p", scp);
359 lock_ObtainWrite(&cm_scacheLock);
360 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
361 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
363 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
370 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
373 * The entire LRU queue was walked and no available cm_scache_t was
374 * found. Drop the cm_scacheLock and sleep for a moment to give a
375 * chance for cm_scache_t objects to be released.
377 lock_ReleaseWrite(&cm_scacheLock);
379 lock_ObtainWrite(&cm_scacheLock);
387 /* if we get here, we should allocate a new scache entry. We either are below
388 * quota or we have a leak and need to allocate a new one to avoid panicing.
390 scp = cm_data.scacheBaseAddress + InterlockedIncrement(&cm_data.currentSCaches) - 1;
391 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
392 "invalid cm_scache_t address");
393 memset(scp, 0, sizeof(cm_scache_t));
394 scp->magic = CM_SCACHE_MAGIC;
395 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
396 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
397 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
399 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
401 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
402 scp->serverLock = -1;
403 scp->dataVersion = CM_SCACHE_VERSION_BAD;
404 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
405 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
406 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
408 /* and put it in the LRU queue */
409 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
410 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
412 scp->allNextp = cm_data.allSCachesp;
413 cm_data.allSCachesp = scp;
417 lock_ReleaseWrite(&cm_scacheLock);
422 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
425 fidp->volume = volume;
427 fidp->unique = unique;
428 CM_FID_GEN_HASH(fidp);
431 /* like strcmp, only for fids */
432 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
434 if (ap->hash != bp->hash)
436 if (ap->vnode != bp->vnode)
438 if (ap->volume != bp->volume)
440 if (ap->unique != bp->unique)
442 if (ap->cell != bp->cell)
447 void cm_fakeSCacheInit(int newFile)
450 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
451 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
452 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
453 cm_data.fakeSCache.cbExpires = (time_t)-1;
454 cm_data.fakeSCache.cbExpires = time(NULL);
455 /* can leave clientModTime at 0 */
456 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
457 cm_data.fakeSCache.unixModeBits = 0777;
458 cm_data.fakeSCache.length.LowPart = 1000;
459 cm_data.fakeSCache.linkCount = 1;
460 cm_data.fakeSCache.refCount = 1;
461 cm_data.fakeSCache.serverLock = -1;
462 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
464 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
465 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
466 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
467 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
471 cm_ValidateSCache(void)
473 cm_scache_t * scp, *lscp;
476 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
477 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
478 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
479 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
483 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
485 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
486 if (scp->magic != CM_SCACHE_MAGIC) {
487 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
488 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
491 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
492 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
493 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
496 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
497 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
498 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
501 if (i > cm_data.currentSCaches ) {
502 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
503 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
506 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
507 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
508 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
513 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
514 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
515 if (scp->magic != CM_SCACHE_MAGIC) {
516 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
517 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
520 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
521 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
522 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
525 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
526 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
527 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
530 if (i > cm_data.currentSCaches ) {
531 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
532 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
535 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
536 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
537 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
542 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
543 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
545 hash = CM_SCACHE_HASH(&scp->fid);
546 if (scp->magic != CM_SCACHE_MAGIC) {
547 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
548 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
551 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
552 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
553 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
556 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
557 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
558 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
562 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
563 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
569 return cm_dnlcValidate();
573 cm_SuspendSCache(void)
578 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
581 * After this call all servers are marked down.
582 * Do not clear the callbacks, instead change the
583 * expiration time so that the callbacks will be expired
584 * when the servers are marked back up. However, we
585 * want the callbacks to be preserved as long as the
586 * servers are down. That way if the machine resumes
587 * without network, the stat cache item will still be
592 lock_ObtainWrite(&cm_scacheLock);
593 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
594 if (scp->cbServerp) {
595 if (scp->flags & CM_SCACHEFLAG_PURERO) {
596 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
598 if (volp->cbExpiresRO == scp->cbExpires)
599 volp->cbExpiresRO = now+1;
603 scp->cbExpires = now+1;
606 lock_ReleaseWrite(&cm_scacheLock);
610 cm_ShutdownSCache(void)
612 cm_scache_t * scp, * nextp;
614 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
616 lock_ObtainWrite(&cm_scacheLock);
618 for ( scp = cm_data.allSCachesp; scp;
620 nextp = scp->allNextp;
621 lock_ReleaseWrite(&cm_scacheLock);
623 lock_ObtainWrite(&scp->dirlock);
625 lock_ObtainWrite(&scp->rw);
626 lock_ObtainWrite(&cm_scacheLock);
628 if (scp->randomACLp) {
629 cm_FreeAllACLEnts(scp);
632 if (scp->cbServerp) {
633 cm_PutServer(scp->cbServerp);
634 scp->cbServerp = NULL;
638 lock_ReleaseWrite(&scp->rw);
642 freeBtree(scp->dirBplus);
643 scp->dirBplus = NULL;
644 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
645 lock_ReleaseWrite(&scp->dirlock);
646 lock_FinalizeRWLock(&scp->dirlock);
648 lock_FinalizeRWLock(&scp->rw);
649 lock_FinalizeRWLock(&scp->bufCreateLock);
650 lock_FinalizeMutex(&scp->redirMx);
652 lock_ReleaseWrite(&cm_scacheLock);
654 return cm_dnlcShutdown();
657 void cm_InitSCache(int newFile, long maxSCaches)
659 static osi_once_t once;
661 if (osi_Once(&once)) {
662 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
664 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
665 cm_data.allSCachesp = NULL;
666 cm_data.currentSCaches = 0;
667 cm_data.maxSCaches = maxSCaches;
668 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
672 for ( scp = cm_data.allSCachesp; scp;
673 scp = scp->allNextp ) {
674 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
675 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
677 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
679 scp->cbServerp = NULL;
682 scp->volumeCreationDate = 0;
683 scp->fileLocksH = NULL;
684 scp->fileLocksT = NULL;
685 scp->serverLock = (-1);
686 scp->lastRefreshCycle = 0;
687 scp->exclusiveLocks = 0;
688 scp->sharedLocks = 0;
696 scp->dirBplus = NULL;
697 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
699 scp->waitQueueT = NULL;
700 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
702 scp->redirBufCount = 0;
703 scp->redirQueueT = NULL;
704 scp->redirQueueH = NULL;
705 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
708 cm_allFileLocks = NULL;
709 cm_freeFileLocks = NULL;
710 cm_lockRefreshCycle = 0;
711 cm_fakeSCacheInit(newFile);
712 cm_allFreeWaiters = NULL;
713 cm_dnlcInit(newFile);
718 /* version that doesn't bother creating the entry if we don't find it */
719 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
724 hash = CM_SCACHE_HASH(fidp);
726 if (fidp->cell == 0) {
730 lock_ObtainRead(&cm_scacheLock);
731 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
732 if (cm_FidCmp(fidp, &scp->fid) == 0) {
733 cm_HoldSCacheNoLock(scp);
734 lock_ConvertRToW(&cm_scacheLock);
735 cm_AdjustScacheLRU(scp);
736 lock_ReleaseWrite(&cm_scacheLock);
740 lock_ReleaseRead(&cm_scacheLock);
744 #ifdef DEBUG_REFCOUNT
745 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
746 cm_req_t *reqp, char * file, long line)
748 long cm_GetSCache(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
753 cm_scache_t *scp = NULL;
754 cm_scache_t *newScp = NULL;
756 cm_volume_t *volp = NULL;
758 int special = 0; // yj: boolean variable to test if file is on root.afs
760 extern cm_fid_t cm_rootFid;
763 hash = CM_SCACHE_HASH(fidp);
766 return CM_ERROR_INVAL;
768 #ifdef AFS_FREELANCE_CLIENT
769 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
770 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
771 !(fidp->vnode==0x1 && fidp->unique==0x1));
772 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
773 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
774 fidp->vnode==0x1 && fidp->unique==0x1);
777 // yj: check if we have the scp, if so, we don't need
778 // to do anything else
779 lock_ObtainRead(&cm_scacheLock);
780 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
781 if (cm_FidCmp(fidp, &scp->fid) == 0) {
782 #ifdef DEBUG_REFCOUNT
783 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
784 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
786 #ifdef AFS_FREELANCE_CLIENT
787 if (cm_freelanceEnabled && special &&
788 cm_data.fakeDirVersion != scp->dataVersion)
791 if (parentFidp && scp->parentVnode == 0) {
792 scp->parentVnode = parentFidp->vnode;
793 scp->parentUnique = parentFidp->unique;
795 cm_HoldSCacheNoLock(scp);
797 lock_ConvertRToW(&cm_scacheLock);
798 cm_AdjustScacheLRU(scp);
799 lock_ReleaseWrite(&cm_scacheLock);
803 lock_ReleaseRead(&cm_scacheLock);
805 // yj: when we get here, it means we don't have an scp
806 // so we need to either load it or fake it, depending
807 // on whether the file is "special", see below.
809 // yj: if we're trying to get an scp for a file that's
810 // on root.afs of homecell, we want to handle it specially
811 // because we have to fill in the status stuff 'coz we
812 // don't want trybulkstat to fill it in for us
813 #ifdef AFS_FREELANCE_CLIENT
814 if (cm_freelanceEnabled && isRoot) {
815 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
816 /* freelance: if we are trying to get the root scp for the first
817 * time, we will just put in a place holder entry.
822 if (cm_freelanceEnabled && special) {
823 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
825 if (cm_getLocalMountPointChange()) {
826 cm_clearLocalMountPointChange();
827 cm_reInitLocalMountPoints();
831 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
833 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
834 return CM_ERROR_WOULDBLOCK;
837 lock_ObtainWrite(&scp->rw);
840 cm_SetFid(&scp->dotdotFid,AFS_FAKE_ROOT_CELL_ID,AFS_FAKE_ROOT_VOL_ID,1,1);
842 scp->parentVnode = parentFidp->vnode;
843 scp->parentUnique = parentFidp->unique;
845 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
846 lock_ObtainWrite(&cm_scacheLock);
847 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
848 scp->nextp = cm_data.scacheHashTablep[hash];
849 cm_data.scacheHashTablep[hash] = scp;
850 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
852 refCount = InterlockedIncrement(&scp->refCount);
853 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
854 lock_ReleaseWrite(&cm_scacheLock);
856 /* must be called after the scp->fid is set */
857 cm_FreelanceFetchMountPointString(scp);
858 cm_FreelanceFetchFileType(scp);
860 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
861 scp->length.HighPart = 0;
863 scp->unixModeBits=0777;
864 scp->clientModTime=FakeFreelanceModTime;
865 scp->serverModTime=FakeFreelanceModTime;
866 scp->parentUnique = 0x1;
867 scp->parentVnode=0x1;
869 scp->dataVersion=cm_data.fakeDirVersion;
870 scp->bufDataVersionLow=cm_data.fakeDirVersion;
871 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
873 lock_ReleaseWrite(&scp->rw);
875 #ifdef DEBUG_REFCOUNT
876 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
877 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
882 #endif /* AFS_FREELANCE_CLIENT */
884 /* we don't have the fid, recycle something */
885 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
886 if (newScp == NULL) {
887 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
888 return CM_ERROR_WOULDBLOCK;
890 #ifdef DEBUG_REFCOUNT
891 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
893 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
895 /* otherwise, we need to find the volume */
896 if (!cm_freelanceEnabled || !isRoot) {
897 cellp = cm_FindCellByID(fidp->cell, 0);
899 /* put back newScp so it can be reused */
900 lock_ObtainWrite(&cm_scacheLock);
901 newScp->flags |= CM_SCACHEFLAG_DELETED;
902 cm_AdjustScacheLRU(newScp);
903 lock_ReleaseWrite(&newScp->rw);
904 lock_ReleaseWrite(&cm_scacheLock);
905 return CM_ERROR_NOSUCHCELL;
908 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
910 /* put back newScp so it can be reused */
911 lock_ObtainWrite(&cm_scacheLock);
912 newScp->flags |= CM_SCACHEFLAG_DELETED;
913 cm_AdjustScacheLRU(newScp);
914 lock_ReleaseWrite(&newScp->rw);
915 lock_ReleaseWrite(&cm_scacheLock);
921 * otherwise, we have the volume, now reverify that the scp doesn't
922 * exist, and proceed. make sure that we hold the cm_scacheLock
923 * write-locked until the scp is put into the hash table in order
926 lock_ObtainWrite(&cm_scacheLock);
927 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
928 if (cm_FidCmp(fidp, &scp->fid) == 0) {
929 #ifdef DEBUG_REFCOUNT
930 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
931 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
933 if (parentFidp && scp->parentVnode == 0) {
934 scp->parentVnode = parentFidp->vnode;
935 scp->parentUnique = parentFidp->unique;
939 cm_HoldSCacheNoLock(scp);
940 cm_AdjustScacheLRU(scp);
942 /* put back newScp so it can be reused */
943 newScp->flags |= CM_SCACHEFLAG_DELETED;
944 cm_AdjustScacheLRU(newScp);
945 lock_ReleaseWrite(&newScp->rw);
946 lock_ReleaseWrite(&cm_scacheLock);
955 if (!cm_freelanceEnabled || !isRoot) {
956 /* if this scache entry represents a volume root then we need
957 * to copy the dotdotFid from the volume structure where the
958 * "master" copy is stored (defect 11489)
960 if (volp->vol[ROVOL].ID == fidp->volume) {
961 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
962 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
963 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
964 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
965 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
966 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
967 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
969 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
970 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
974 scp->parentVnode = parentFidp->vnode;
975 scp->parentUnique = parentFidp->unique;
980 scp->nextp = cm_data.scacheHashTablep[hash];
981 cm_data.scacheHashTablep[hash] = scp;
982 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
983 refCount = InterlockedIncrement(&scp->refCount);
984 lock_ReleaseWrite(&cm_scacheLock);
985 lock_ReleaseWrite(&scp->rw);
986 #ifdef DEBUG_REFCOUNT
987 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
989 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
991 /* XXX - The following fields in the cm_scache are
998 /* now we have a held scache entry; just return it */
1000 #ifdef DEBUG_REFCOUNT
1001 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
1002 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
1007 /* Returns a held reference to the scache's parent
1009 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
1013 cm_fid_t parent_fid;
1014 cm_scache_t * pscp = NULL;
1016 if (scp->parentVnode == 0)
1019 lock_ObtainWrite(&cm_scacheLock);
1020 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
1022 if (cm_FidCmp(&scp->fid, &parent_fid)) {
1023 i = CM_SCACHE_HASH(&parent_fid);
1024 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
1025 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
1026 cm_HoldSCacheNoLock(pscp);
1032 lock_ReleaseWrite(&cm_scacheLock);
1037 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1039 cm_scache_waiter_t * w;
1041 lock_ObtainWrite(&cm_scacheLock);
1042 if (cm_allFreeWaiters == NULL) {
1043 w = malloc(sizeof(*w));
1044 memset(w, 0, sizeof(*w));
1046 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1047 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1050 w->threadId = thrd_Current();
1052 cm_HoldSCacheNoLock(scp);
1056 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1057 lock_ReleaseWrite(&cm_scacheLock);
1059 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1062 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1064 cm_scache_waiter_t * w;
1067 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1069 lock_ObtainRead(&cm_scacheLock);
1070 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1072 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1073 if (w->flags == flags && w->bufp == bufp) {
1078 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1079 this_is_me = (w->threadId == thrd_Current());
1080 lock_ReleaseRead(&cm_scacheLock);
1083 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1087 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1089 lock_ObtainWrite(&cm_scacheLock);
1090 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1091 cm_ReleaseSCacheNoLock(scp);
1092 memset(w, 0, sizeof(*w));
1093 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1094 lock_ReleaseWrite(&cm_scacheLock);
1100 /* synchronize a fetch, store, read, write, fetch status or store status.
1101 * Called with scache mutex held, and returns with it held, but temporarily
1102 * drops it during the fetch.
1104 * At most one flag can be on in flags, if this is an RPC request.
1106 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1108 * There are a lot of weird restrictions here; here's an attempt to explain the
1109 * rationale for the concurrency restrictions implemented in this function.
1111 * First, although the file server will break callbacks when *another* machine
1112 * modifies a file or status block, the client itself is responsible for
1113 * concurrency control on its own requests. Callback breaking events are rare,
1114 * and simply invalidate any concurrent new status info.
1116 * In the absence of callback breaking messages, we need to know how to
1117 * synchronize incoming responses describing updates to files. We synchronize
1118 * operations that update the data version by comparing the data versions.
1119 * However, updates that do not update the data, but only the status, can't be
1120 * synchronized with fetches or stores, since there's nothing to compare
1121 * to tell which operation executed first at the server.
1123 * Thus, we can allow multiple ops that change file data, or dir data, and
1124 * fetches. However, status storing ops have to be done serially.
1126 * Furthermore, certain data-changing ops are incompatible: we can't read or
1127 * write a buffer while doing a truncate. We can't read and write the same
1128 * buffer at the same time, or write while fetching or storing, or read while
1129 * fetching a buffer (this may change). We can't fetch and store at the same
1132 * With respect to status, we can't read and write at the same time, read while
1133 * fetching, write while fetching or storing, or fetch and store at the same time.
1135 * We can't allow a get callback RPC to run in concurrently with something that
1136 * will return updated status, since we could start a call, have the server
1137 * return status, have another machine make an update to the status (which
1138 * doesn't change serverModTime), have the original machine get a new callback,
1139 * and then have the original machine merge in the early, old info from the
1140 * first call. At this point, the easiest way to avoid this problem is to have
1141 * getcallback calls conflict with all others for the same vnode. Other calls
1142 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1143 * vnode must be careful not to merge in their status unless they have obtained
1144 * a callback from the start of their call.
1146 * Note added 1/23/96
1147 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1148 * Each such RPC passes a FileLength parameter, which the server uses to do
1149 * pre-truncation if necessary. So if two RPC's are processed out of order at
1150 * the server, the one with the smaller FileLength will be processed last,
1151 * possibly resulting in a bogus truncation. The simplest way to avoid this
1152 * is to serialize all StoreData RPC's. This is the reason we defined
1153 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1155 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1156 * readers which may be requesting overlapping ranges.
1158 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1159 afs_uint32 rights, afs_uint32 flags)
1161 osi_queueData_t *qdp;
1164 afs_uint32 outRights;
1166 afs_uint32 sleep_scp_flags = 0;
1167 afs_uint32 sleep_buf_cmflags = 0;
1168 afs_uint32 sleep_scp_bufs = 0;
1170 afs_int32 waitCount;
1171 afs_int32 waitRequests;
1173 lock_AssertWrite(&scp->rw);
1175 /* lookup this first */
1176 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1179 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1182 /* Do the access check. Now we don't really do the access check
1183 * atomically, since the caller doesn't expect the parent dir to be
1184 * returned locked, and that is what we'd have to do to prevent a
1185 * callback breaking message on the parent due to a setacl call from
1186 * being processed while we're running. So, instead, we check things
1187 * here, and if things look fine with the access, we proceed to finish
1188 * the rest of this check. Sort of a hack, but probably good enough.
1192 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1193 /* if we're bringing in a new status block, ensure that
1194 * we aren't already doing so, and that no one is
1195 * changing the status concurrently, either. We need
1196 * to do this, even if the status is of a different
1197 * type, since we don't have the ability to figure out,
1198 * in the AFS 3 protocols, which status-changing
1199 * operation ran first, or even which order a read and
1200 * a write occurred in.
1202 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1203 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1204 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1208 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1209 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1210 /* if we're going to make an RPC to change the status, make sure
1211 * that no one is bringing in or sending out the status.
1213 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1214 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1215 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1218 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1219 (scp->bufReadsp || scp->bufWritesp)) {
1220 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1224 if (flags & CM_SCACHESYNC_FETCHDATA) {
1225 /* if we're bringing in a new chunk of data, make sure that
1226 * nothing is happening to that chunk, and that we aren't
1227 * changing the basic file status info, either.
1229 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1230 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1231 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1234 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1235 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1239 if (flags & CM_SCACHESYNC_STOREDATA) {
1240 /* same as fetch data */
1241 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1242 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1243 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1246 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1247 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1252 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1253 /* Don't allow concurrent StoreData RPC's */
1254 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1255 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1260 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1261 /* Don't allow more than one BKG store request */
1262 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1263 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1268 if (flags & CM_SCACHESYNC_LOCK) {
1269 /* Don't allow concurrent fiddling with lock lists */
1270 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1271 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1276 /* now the operations that don't correspond to making RPCs */
1277 if (flags & CM_SCACHESYNC_GETSTATUS) {
1278 /* we can use the status that's here, if we're not
1279 * bringing in new status.
1281 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1282 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1286 if (flags & CM_SCACHESYNC_SETSTATUS) {
1287 /* we can make a change to the local status, as long as
1288 * the status isn't changing now.
1290 * If we're fetching or storing a chunk of data, we can
1291 * change the status locally, since the fetch/store
1292 * operations don't change any of the data that we're
1295 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1296 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1297 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1301 if (flags & CM_SCACHESYNC_READ) {
1302 /* we're going to read the data, make sure that the
1303 * status is available, and that the data is here. It
1304 * is OK to read while storing the data back.
1306 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1307 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1310 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1311 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1314 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1315 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1319 if (flags & CM_SCACHESYNC_WRITE) {
1320 /* don't write unless the status is stable and the chunk
1323 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1324 CM_SCACHEFLAG_SIZESTORING)) {
1325 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1328 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1330 CM_BUF_CMWRITING))) {
1331 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1333 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1334 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1335 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1341 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1342 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1343 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1346 if (cm_EAccesFindEntry(userp, &scp->fid))
1347 return CM_ERROR_NOACCESS;
1350 lock_ReleaseMutex(&bufp->mx);
1351 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1353 lock_ReleaseWrite(&scp->rw);
1354 lock_ObtainMutex(&bufp->mx);
1355 lock_ObtainWrite(&scp->rw);
1359 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1365 /* can't check access rights without a callback */
1366 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1368 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1369 return CM_ERROR_READONLY;
1371 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1372 if (~outRights & rights)
1373 return CM_ERROR_NOACCESS;
1376 /* we don't know the required access rights */
1377 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1378 code = cm_GetAccessRights(scp, userp, reqp);
1380 lock_ReleaseWrite(&scp->rw);
1381 lock_ObtainMutex(&bufp->mx);
1382 lock_ObtainWrite(&scp->rw);
1390 if (flags & CM_SCACHESYNC_BULKREAD) {
1391 /* Don't allow concurrent fiddling with lock lists */
1392 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1393 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1398 /* if we get here, we're happy */
1402 /* first check if we're not supposed to wait: fail
1403 * in this case, returning with everything still locked.
1405 if (flags & CM_SCACHESYNC_NOWAIT)
1406 return CM_ERROR_WOULDBLOCK;
1408 /* These are used for minidump debugging */
1409 sleep_scp_flags = scp->flags; /* so we know why we slept */
1410 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1411 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1413 /* wait here, then try again */
1414 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1416 waitCount = InterlockedIncrement(&scp->waitCount);
1417 waitRequests = InterlockedIncrement(&scp->waitRequests);
1418 if (waitCount > 1) {
1419 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1420 scp, waitCount, waitRequests);
1422 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1423 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1426 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1430 lock_ReleaseMutex(&bufp->mx);
1431 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1433 lock_ObtainMutex(&bufp->mx);
1434 lock_ObtainWrite(&scp->rw);
1435 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1437 cm_UpdateServerPriority();
1439 waitCount = InterlockedDecrement(&scp->waitCount);
1440 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1441 scp, waitCount, scp->waitRequests);
1442 if (waitCount == 0) {
1443 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1444 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1445 scp->waitRequests = 0;
1447 } /* big while loop */
1449 /* now, update the recorded state for RPC-type calls */
1450 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1451 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1452 if (flags & CM_SCACHESYNC_STORESTATUS)
1453 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1454 if (flags & CM_SCACHESYNC_SETSIZE)
1455 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1456 if (flags & CM_SCACHESYNC_STORESIZE)
1457 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1458 if (flags & CM_SCACHESYNC_GETCALLBACK)
1459 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1460 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1461 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1462 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1463 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1464 if (flags & CM_SCACHESYNC_LOCK)
1465 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1466 if (flags & CM_SCACHESYNC_BULKREAD)
1467 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1469 /* now update the buffer pointer */
1470 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1471 /* ensure that the buffer isn't already in the I/O list */
1472 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1473 tbufp = osi_GetQData(qdp);
1474 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1477 /* queue a held reference to the buffer in the "reading" I/O list */
1478 qdp = osi_QDAlloc();
1479 osi_SetQData(qdp, bufp);
1482 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1483 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1486 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1487 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1488 "attempting to store extents on a non-file object");
1490 /* ensure that the buffer isn't already in the I/O list */
1491 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1492 tbufp = osi_GetQData(qdp);
1493 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1496 /* queue a held reference to the buffer in the "writing" I/O list */
1497 qdp = osi_QDAlloc();
1498 osi_SetQData(qdp, bufp);
1500 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1501 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1504 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1505 /* mark the buffer as being written to. */
1506 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1512 /* for those syncops that setup for RPCs.
1513 * Called with scache locked.
1515 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1517 osi_queueData_t *qdp;
1520 lock_AssertWrite(&scp->rw);
1522 /* now, update the recorded state for RPC-type calls */
1523 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1524 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1525 if (flags & CM_SCACHESYNC_STORESTATUS)
1526 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1527 if (flags & CM_SCACHESYNC_SETSIZE)
1528 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1529 if (flags & CM_SCACHESYNC_STORESIZE)
1530 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1531 if (flags & CM_SCACHESYNC_GETCALLBACK)
1532 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1533 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1534 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1535 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1536 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1537 if (flags & CM_SCACHESYNC_LOCK)
1538 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1539 if (flags & CM_SCACHESYNC_BULKREAD)
1540 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1542 /* now update the buffer pointer */
1543 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1546 /* ensure that the buffer is in the I/O list */
1547 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1548 tbufp = osi_GetQData(qdp);
1553 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1557 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1558 if (bufp->flags & CM_BUF_WAITING) {
1559 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1560 osi_Wakeup((LONG_PTR) &bufp);
1566 /* now update the buffer pointer */
1567 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1569 /* ensure that the buffer is in the I/O list */
1570 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1571 tbufp = osi_GetQData(qdp);
1576 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1580 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1581 if (bufp->flags & CM_BUF_WAITING) {
1582 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1583 osi_Wakeup((LONG_PTR) &bufp);
1589 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1590 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1591 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1594 /* and wakeup anyone who is waiting */
1595 if ((scp->flags & CM_SCACHEFLAG_WAITING) ||
1596 !osi_QIsEmpty(&scp->waitQueueH)) {
1597 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1598 osi_Wakeup((LONG_PTR) &scp->flags);
1603 dv_diff(afs_uint64 dv1, afs_uint64 dv2)
1605 if ( dv1 - dv2 > 0x7FFFFFFF )
1606 return (afs_uint32)(dv2 - dv1);
1608 return (afs_uint32)(dv1 - dv2);
1612 cm_IsStatusValid(AFSFetchStatus *statusp)
1614 if (statusp->InterfaceVersion != 0x1 ||
1615 !(statusp->FileType > 0 && statusp->FileType <= SymbolicLink)) {
1622 /* merge in a response from an RPC. The scp must be locked, and the callback
1625 * Don't overwrite any status info that is dirty, since we could have a store
1626 * operation (such as store data) that merges some info in, and we don't want
1627 * to lose the local updates. Typically, there aren't many updates we do
1628 * locally, anyway, probably only mtime.
1630 * There is probably a bug in here where a chmod (which doesn't change
1631 * serverModTime) that occurs between two fetches, both of whose responses are
1632 * handled after the callback breaking is done, but only one of whose calls
1633 * started before that, can cause old info to be merged from the first call.
1635 long cm_MergeStatus(cm_scache_t *dscp,
1636 cm_scache_t *scp, AFSFetchStatus *statusp,
1637 AFSVolSync *volsyncp,
1638 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1640 afs_uint64 dataVersion;
1641 struct cm_volume *volp = NULL;
1642 struct cm_cell *cellp = NULL;
1643 int rdr_invalidate = 0;
1644 afs_uint32 activeRPCs;
1646 lock_AssertWrite(&scp->rw);
1648 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1650 // yj: i want to create some fake status for the /afs directory and the
1651 // entries under that directory
1652 #ifdef AFS_FREELANCE_CLIENT
1653 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1654 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1655 if (scp == cm_data.rootSCachep) {
1656 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1657 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1658 statusp->Length = cm_fakeDirSize;
1659 statusp->Length_hi = 0;
1661 statusp->FileType = scp->fileType;
1662 statusp->Length = scp->length.LowPart;
1663 statusp->Length_hi = scp->length.HighPart;
1665 statusp->InterfaceVersion = 0x1;
1666 statusp->LinkCount = scp->linkCount;
1667 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1668 statusp->Author = 0x1;
1669 statusp->Owner = 0x0;
1670 statusp->CallerAccess = 0x9;
1671 statusp->AnonymousAccess = 0x9;
1672 statusp->UnixModeBits = 0777;
1673 statusp->ParentVnode = 0x1;
1674 statusp->ParentUnique = 0x1;
1675 statusp->ResidencyMask = 0;
1676 statusp->ClientModTime = FakeFreelanceModTime;
1677 statusp->ServerModTime = FakeFreelanceModTime;
1679 statusp->SyncCounter = 0;
1680 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1681 statusp->lockCount = 0;
1682 statusp->errorCode = 0;
1684 #endif /* AFS_FREELANCE_CLIENT */
1686 if (!cm_IsStatusValid(statusp)) {
1687 osi_Log3(afsd_logp, "Merge: Bad Status scp 0x%p Invalid InterfaceVersion %d FileType %d",
1688 scp, statusp->InterfaceVersion, statusp->FileType);
1689 return CM_ERROR_INVAL;
1692 if (statusp->errorCode != 0) {
1693 switch (statusp->errorCode) {
1698 cm_EAccesAddEntry(userp, &scp->fid, &dscp->fid);
1700 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1702 if (scp->fid.vnode & 0x1)
1703 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1705 scp->fileType = CM_SCACHETYPE_UNKNOWN;
1707 scp->serverModTime = 0;
1708 scp->clientModTime = 0;
1709 scp->length.LowPart = 0;
1710 scp->length.HighPart = 0;
1711 scp->serverLength.LowPart = 0;
1712 scp->serverLength.HighPart = 0;
1716 scp->unixModeBits = 0;
1718 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1719 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1720 scp->fsLockCount = 0;
1722 if (dscp && dscp != scp) {
1723 scp->parentVnode = dscp->fid.vnode;
1724 scp->parentUnique = dscp->fid.unique;
1726 scp->parentVnode = 0;
1727 scp->parentUnique = 0;
1730 if (RDR_Initialized)
1734 dataVersion = statusp->dataVersionHigh;
1736 dataVersion |= statusp->DataVersion;
1738 if (!(flags & CM_MERGEFLAG_FORCE) &&
1739 dataVersion < scp->dataVersion &&
1740 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1742 cellp = cm_FindCellByID(scp->fid.cell, 0);
1743 if (scp->cbServerp) {
1744 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1745 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1746 osi_Log2(afsd_logp, "old data from server %x volume %s",
1747 scp->cbServerp->addr.sin_addr.s_addr,
1748 volp ? volp->namep : "(unknown)");
1751 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1752 scp, scp->dataVersion, dataVersion);
1753 /* we have a number of data fetch/store operations running
1754 * concurrently, and we can tell which one executed last at the
1755 * server by its mtime.
1756 * Choose the one with the largest mtime, and ignore the rest.
1758 * These concurrent calls are incompatible with setting the
1759 * mtime, so we won't have a locally changed mtime here.
1761 * We could also have ACL info for a different user than usual,
1762 * in which case we have to do that part of the merge, anyway.
1763 * We won't have to worry about the info being old, since we
1764 * won't have concurrent calls
1765 * that change file status running from this machine.
1767 * Added 3/17/98: if we see data version regression on an RO
1768 * file, it's probably due to a server holding an out-of-date
1769 * replica, rather than to concurrent RPC's. Failures to
1770 * release replicas are now flagged by the volserver, but only
1771 * since AFS 3.4 5.22, so there are plenty of clients getting
1772 * out-of-date replicas out there.
1774 * If we discover an out-of-date replica, by this time it's too
1775 * late to go to another server and retry. Also, we can't
1776 * reject the merge, because then there is no way for
1777 * GetAccess to do its work, and the caller gets into an
1778 * infinite loop. So we just grin and bear it.
1780 if (!(scp->flags & CM_SCACHEFLAG_RO))
1785 * The first field of the volsync parameter is supposed to be the
1786 * volume creation date. Unfortunately, pre-OpenAFS 1.4.11 and 1.6.0
1787 * file servers do not populate the VolSync structure for BulkStat and
1788 * InlineBulkStat RPCs. As a result, the volume creation date is not
1789 * trustworthy when status is obtained via [Inline]BulkStatus RPCs.
1790 * If cm_readonlyVolumeVersioning is set, it is assumed that all file
1791 * servers populate the VolSync structure at all times.
1793 if (cm_readonlyVolumeVersioning || !(flags & CM_MERGEFLAG_BULKSTAT))
1794 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1796 scp->volumeCreationDate = 0;
1798 scp->serverModTime = statusp->ServerModTime;
1800 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1801 scp->clientModTime = statusp->ClientModTime;
1803 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1804 scp->length.LowPart = statusp->Length;
1805 scp->length.HighPart = statusp->Length_hi;
1808 scp->serverLength.LowPart = statusp->Length;
1809 scp->serverLength.HighPart = statusp->Length_hi;
1811 scp->linkCount = statusp->LinkCount;
1812 scp->owner = statusp->Owner;
1813 scp->group = statusp->Group;
1814 scp->unixModeBits = statusp->UnixModeBits & 07777;
1816 if (statusp->FileType == File)
1817 scp->fileType = CM_SCACHETYPE_FILE;
1818 else if (statusp->FileType == Directory)
1819 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1820 else if (statusp->FileType == SymbolicLink) {
1821 if ((scp->unixModeBits & 0111) == 0)
1822 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1824 scp->fileType = CM_SCACHETYPE_SYMLINK;
1827 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1828 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1830 /* and other stuff */
1831 scp->parentVnode = statusp->ParentVnode;
1832 scp->parentUnique = statusp->ParentUnique;
1834 /* -1 is a write lock; any positive values are read locks */
1835 scp->fsLockCount = (afs_int32)statusp->lockCount;
1837 /* and merge in the private acl cache info, if this is more than the public
1838 * info; merge in the public stuff in any case.
1840 scp->anyAccess = statusp->AnonymousAccess;
1842 if (userp != NULL) {
1843 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1846 if (dataVersion != 0 && scp->dataVersion != CM_SCACHE_VERSION_BAD &&
1847 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && (dataVersion != scp->dataVersion) ||
1848 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1849 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs))) {
1851 * We now know that all of the data buffers that we have associated
1852 * with this scp are invalid. Subsequent operations will go faster
1853 * if the buffers are removed from the hash tables.
1855 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1856 * those version numbers will be updated as part of the directory operation.
1858 * We do not remove storedata buffers because they will still be valid.
1863 cm_buf_t *bp, *prevBp, *nextBp;
1865 lock_ObtainWrite(&buf_globalLock);
1866 i = BUF_FILEHASH(&scp->fid);
1867 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1869 nextBp = bp->fileHashp;
1871 * if the buffer belongs to this stat cache entry
1872 * and the buffer mutex can be obtained, check the
1873 * reference count and if it is zero, remove the buffer
1874 * from the hash tables. If there are references,
1875 * the buffer might be updated to the current version
1876 * so leave it in place.
1878 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1879 lock_TryMutex(&bp->mx)) {
1880 if (bp->refCount == 0 &&
1881 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1882 !(bp->qFlags & CM_BUF_QREDIR)) {
1883 prevBp = bp->fileHashBackp;
1884 bp->fileHashBackp = bp->fileHashp = NULL;
1886 prevBp->fileHashp = nextBp;
1888 cm_data.buf_fileHashTablepp[i] = nextBp;
1890 nextBp->fileHashBackp = prevBp;
1892 j = BUF_HASH(&bp->fid, &bp->offset);
1893 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1894 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1899 /* we better find it */
1900 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1902 *lbpp = bp->hashp; /* hash out */
1905 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1907 lock_ReleaseMutex(&bp->mx);
1910 lock_ReleaseWrite(&buf_globalLock);
1913 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1914 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1915 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1917 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1918 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1919 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1920 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1921 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1924 /* We maintain a range of buffer dataVersion values which are considered
1925 * valid. This avoids the need to update the dataVersion on each buffer
1926 * object during an uncontested storeData operation. As a result this
1927 * merge status no longer has performance characteristics derived from
1928 * the size of the file.
1930 * For directory buffers, only current dataVersion values are up to date.
1932 if (((flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (dv_diff(dataVersion, scp->dataVersion) > activeRPCs)) ||
1933 (!(flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (scp->dataVersion != dataVersion)) ||
1934 scp->bufDataVersionLow == CM_SCACHE_VERSION_BAD ||
1935 scp->fileType == CM_SCACHETYPE_DIRECTORY ||
1936 flags & CM_MERGEFLAG_CACHE_BYPASS) {
1937 scp->bufDataVersionLow = dataVersion;
1940 if (RDR_Initialized) {
1942 * The redirector maintains its own cached status information which
1943 * must be updated when a DV change occurs that is not the result
1944 * of a redirector initiated data change.
1946 * If the current old DV is BAD, send a DV change notification.
1948 * If the DV has changed and request was not initiated by the
1949 * redirector, send a DV change notification.
1951 * If the request was initiated by the redirector, send a notification
1952 * for store and directory operations that result in a DV change greater
1953 * than the number of active RPCs or any other operation that results
1954 * in an unexpected DV change such as FetchStatus.
1957 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1959 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
1961 } else if (reqp->flags & CM_REQ_SOURCE_REDIR) {
1962 if (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1963 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs - 1)) {
1965 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1966 dv_diff(dataVersion, scp->dataVersion) > activeRPCs) {
1971 scp->dataVersion = dataVersion;
1974 * If someone is waiting for status information, we can wake them up
1975 * now even though the entity that issued the FetchStatus may not
1976 * have completed yet.
1978 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1981 * We just successfully merged status on the stat cache object.
1982 * This means that the associated volume must be online.
1986 cellp = cm_FindCellByID(scp->fid.cell, 0);
1987 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1990 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1991 if (statep->state != vl_online) {
1992 lock_ObtainWrite(&volp->rw);
1993 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1994 statep->state = vl_online;
1995 lock_ReleaseWrite(&volp->rw);
1999 /* Remove cached EACCES / EPERM errors if the file is a directory */
2000 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
2001 !(volp && (volp->flags & CM_VOLUMEFLAG_DFS_VOLUME)) &&
2002 !cm_accessPerFileCheck)
2004 cm_EAccesClearParentEntries(&scp->fid);
2012 * The scache rw lock cannot be held across the invalidation.
2013 * Doing so can result in deadlocks with other threads processing
2014 * requests initiated by the afs redirector.
2016 if (rdr_invalidate) {
2017 lock_ReleaseWrite(&scp->rw);
2018 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
2019 scp->fid.unique, scp->fid.hash,
2020 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
2021 lock_ObtainWrite(&scp->rw);
2027 /* note that our stat cache info is incorrect, so force us eventually
2028 * to stat the file again. There may be dirty data associated with
2029 * this vnode, and we want to preserve that information.
2031 * This function works by simply simulating a loss of the callback.
2033 * This function must be called with the scache locked.
2035 void cm_DiscardSCache(cm_scache_t *scp)
2037 lock_AssertWrite(&scp->rw);
2038 if (scp->cbServerp) {
2039 cm_PutServer(scp->cbServerp);
2040 scp->cbServerp = NULL;
2044 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
2045 cm_dnlcPurgedp(scp);
2046 cm_dnlcPurgevp(scp);
2047 cm_FreeAllACLEnts(scp);
2049 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
2050 cm_VolStatus_Invalidate_DFS_Mapping(scp);
2053 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
2055 afsFidp->Volume = fidp->volume;
2056 afsFidp->Vnode = fidp->vnode;
2057 afsFidp->Unique = fidp->unique;
2060 #ifdef DEBUG_REFCOUNT
2061 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2063 void cm_HoldSCacheNoLock(cm_scache_t *scp)
2068 osi_assertx(scp != NULL, "null cm_scache_t");
2069 lock_AssertAny(&cm_scacheLock);
2070 refCount = InterlockedIncrement(&scp->refCount);
2071 #ifdef DEBUG_REFCOUNT
2072 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
2073 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
2077 #ifdef DEBUG_REFCOUNT
2078 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
2080 void cm_HoldSCache(cm_scache_t *scp)
2085 osi_assertx(scp != NULL, "null cm_scache_t");
2086 lock_ObtainRead(&cm_scacheLock);
2087 refCount = InterlockedIncrement(&scp->refCount);
2088 #ifdef DEBUG_REFCOUNT
2089 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
2090 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
2092 lock_ReleaseRead(&cm_scacheLock);
2095 #ifdef DEBUG_REFCOUNT
2096 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2098 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2103 osi_assertx(scp != NULL, "null cm_scache_t");
2104 lock_AssertAny(&cm_scacheLock);
2106 refCount = InterlockedDecrement(&scp->refCount);
2107 #ifdef DEBUG_REFCOUNT
2109 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
2111 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2112 #ifdef DEBUG_REFCOUNT
2113 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
2114 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
2117 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2121 lockstate = lock_GetRWLockState(&cm_scacheLock);
2122 if (lockstate != OSI_RWLOCK_WRITEHELD)
2123 lock_ReleaseRead(&cm_scacheLock);
2125 lock_ReleaseWrite(&cm_scacheLock);
2127 lock_ObtainWrite(&scp->rw);
2128 if (scp->flags & CM_SCACHEFLAG_DELETED)
2131 if (refCount == 0 && deleted) {
2132 lock_ObtainWrite(&cm_scacheLock);
2133 cm_RecycleSCache(scp, 0);
2134 if (lockstate != OSI_RWLOCK_WRITEHELD)
2135 lock_ConvertWToR(&cm_scacheLock);
2137 if (lockstate != OSI_RWLOCK_WRITEHELD)
2138 lock_ObtainRead(&cm_scacheLock);
2140 lock_ObtainWrite(&cm_scacheLock);
2142 lock_ReleaseWrite(&scp->rw);
2146 #ifdef DEBUG_REFCOUNT
2147 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2149 void cm_ReleaseSCache(cm_scache_t *scp)
2154 osi_assertx(scp != NULL, "null cm_scache_t");
2155 lock_ObtainRead(&cm_scacheLock);
2156 refCount = InterlockedDecrement(&scp->refCount);
2157 #ifdef DEBUG_REFCOUNT
2159 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2161 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2162 #ifdef DEBUG_REFCOUNT
2163 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2164 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2166 lock_ReleaseRead(&cm_scacheLock);
2168 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2170 lock_ObtainWrite(&scp->rw);
2171 if (scp->flags & CM_SCACHEFLAG_DELETED)
2174 lock_ObtainWrite(&cm_scacheLock);
2175 cm_RecycleSCache(scp, 0);
2176 lock_ReleaseWrite(&cm_scacheLock);
2178 lock_ReleaseWrite(&scp->rw);
2182 /* just look for the scp entry to get filetype */
2183 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2184 int cm_FindFileType(cm_fid_t *fidp)
2189 hash = CM_SCACHE_HASH(fidp);
2191 osi_assertx(fidp->cell != 0, "unassigned cell value");
2193 lock_ObtainWrite(&cm_scacheLock);
2194 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2195 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2196 lock_ReleaseWrite(&cm_scacheLock);
2197 return scp->fileType;
2200 lock_ReleaseWrite(&cm_scacheLock);
2204 /* dump all scp's that have reference count > 0 to a file.
2205 * cookie is used to identify this batch for easy parsing,
2206 * and it a string provided by a caller
2208 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2217 lock_ObtainRead(&cm_scacheLock);
2219 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2220 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2222 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2225 char *srvStr = NULL;
2226 afs_uint32 srvStrRpc = TRUE;
2230 if (scp->cbServerp) {
2231 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2232 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2233 srvStr = malloc(16); /* enough for 255.255.255.255 */
2235 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2239 if (scp->cbExpires) {
2244 cbt[strlen(cbt)-1] = '\0';
2247 if (scp->volumeCreationDate) {
2248 t = scp->volumeCreationDate;
2251 cdrot = strdup(cdrot);
2252 cdrot[strlen(cdrot)-1] = '\0';
2256 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2257 "mpDV=%I64d mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2258 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2259 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2260 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mpDataVersion, scp->mountPointStringp,
2261 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2262 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2263 cdrot ? cdrot : "<none>", scp->refCount);
2264 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2266 if (scp->fileLocksH) {
2267 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2268 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2270 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2271 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2272 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2273 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2274 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2275 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2276 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2279 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2280 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2285 RpcStringFree(&srvStr);
2295 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2296 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2297 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2298 cookie, cm_data.scacheHashTableSize);
2299 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2301 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2303 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2305 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2306 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2307 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2311 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2312 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2314 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2315 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2317 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2318 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2319 sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2320 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2321 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2322 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2325 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2326 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2329 lock_ReleaseRead(&cm_scacheLock);