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)
143 lock_AssertWrite(&cm_scacheLock);
144 lock_AssertWrite(&scp->rw);
146 if (scp->refCount != 0) {
150 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
151 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
153 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
158 if (scp->redirBufCount != 0) {
162 cm_RemoveSCacheFromHashTable(scp);
164 /* invalidate so next merge works fine;
165 * also initialize some flags */
167 _InterlockedAnd(&scp->flags,
168 ~(CM_SCACHEFLAG_STATD
169 | CM_SCACHEFLAG_DELETED
171 | CM_SCACHEFLAG_PURERO
172 | CM_SCACHEFLAG_OVERQUOTA
173 | CM_SCACHEFLAG_OUTOFSPACE
174 | CM_SCACHEFLAG_EACCESS));
175 scp->serverModTime = 0;
176 scp->dataVersion = CM_SCACHE_VERSION_BAD;
177 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
178 scp->bulkStatProgress = hzero;
180 scp->waitQueueT = NULL;
182 if (scp->cbServerp) {
183 cm_PutServer(scp->cbServerp);
184 scp->cbServerp = NULL;
187 scp->volumeCreationDate = 0;
195 /* remove from dnlc */
199 /* discard cached status; if non-zero, Close
200 * tried to store this to server but failed */
203 /* discard symlink info */
204 scp->mountPointStringp[0] = '\0';
205 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
206 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
208 /* reset locking info */
209 scp->fileLocksH = NULL;
210 scp->fileLocksT = NULL;
211 scp->serverLock = (-1);
212 scp->exclusiveLocks = 0;
213 scp->sharedLocks = 0;
214 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
215 scp->fsLockCount = 0;
217 /* not locked, but there can be no references to this guy
218 * while we hold the global refcount lock.
220 cm_FreeAllACLEnts(scp);
222 cm_ResetSCacheDirectory(scp, 0);
228 * called with cm_scacheLock write-locked; find a vnode to recycle.
229 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
230 * returns scp->rw write-locked.
233 cm_GetNewSCache(afs_uint32 locked)
235 cm_scache_t *scp = NULL;
239 lock_AssertWrite(&cm_scacheLock);
241 lock_ObtainWrite(&cm_scacheLock);
243 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
244 /* There were no deleted scache objects that we could use. Try to find
245 * one that simply hasn't been used in a while.
247 for (retry = 0 ; retry < 2; retry++) {
248 for ( scp = cm_data.scacheLRULastp;
250 scp = (cm_scache_t *) osi_QPrev(&scp->q))
252 /* It is possible for the refCount to be zero and for there still
253 * to be outstanding dirty buffers. If there are dirty buffers,
254 * we must not recycle the scp.
256 * If the object is in use by the redirector, then avoid recycling
257 * it unless we have to.
259 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
260 afs_uint32 buf_dirty = 0;
261 afs_uint32 buf_rdr = 0;
263 lock_ReleaseWrite(&cm_scacheLock);
264 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
266 buf_rdr = buf_RDRBuffersExist(&scp->fid);
267 lock_ObtainWrite(&cm_scacheLock);
269 if (!buf_dirty && !buf_rdr) {
273 if (!lock_TryWrite(&scp->rw))
276 /* Found a likely candidate. Save type and fid in case we succeed */
278 fileType = scp->fileType;
280 if (!cm_RecycleSCache(scp, 0)) {
281 /* we found an entry, so return it.
282 * remove from the LRU queue and put it back at the
283 * head of the LRU queue.
285 cm_AdjustScacheLRU(scp);
287 if (RDR_Initialized) {
289 * We drop the cm_scacheLock because it may be required to
290 * satisfy an ioctl request from the redirector. It should
291 * be safe to hold the scp->rw lock here because at this
292 * point (a) the object has just been recycled so the fid
293 * is nul and there are no requests that could possibly
294 * be issued by the redirector that would depend upon it.
296 lock_ReleaseWrite(&cm_scacheLock);
297 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
298 fid.unique, fid.hash,
299 fileType, AFS_INVALIDATE_EXPIRED);
300 lock_ObtainWrite(&cm_scacheLock);
304 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
307 lock_ReleaseWrite(&scp->rw);
309 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%p", scp);
313 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
318 /* if we get here, we should allocate a new scache entry. We either are below
319 * quota or we have a leak and need to allocate a new one to avoid panicing.
321 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
322 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
323 "invalid cm_scache_t address");
324 memset(scp, 0, sizeof(cm_scache_t));
325 scp->magic = CM_SCACHE_MAGIC;
326 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
327 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
328 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
330 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
332 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
333 scp->serverLock = -1;
335 /* and put it in the LRU queue */
336 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
337 cm_data.currentSCaches++;
338 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
340 scp->allNextp = cm_data.allSCachesp;
341 cm_data.allSCachesp = scp;
345 lock_ReleaseWrite(&cm_scacheLock);
350 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
353 fidp->volume = volume;
355 fidp->unique = unique;
356 CM_FID_GEN_HASH(fidp);
359 /* like strcmp, only for fids */
360 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
362 if (ap->hash != bp->hash)
364 if (ap->vnode != bp->vnode)
366 if (ap->volume != bp->volume)
368 if (ap->unique != bp->unique)
370 if (ap->cell != bp->cell)
375 void cm_fakeSCacheInit(int newFile)
378 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
379 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
380 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
381 cm_data.fakeSCache.cbExpires = (time_t)-1;
382 /* can leave clientModTime at 0 */
383 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
384 cm_data.fakeSCache.unixModeBits = 0777;
385 cm_data.fakeSCache.length.LowPart = 1000;
386 cm_data.fakeSCache.linkCount = 1;
387 cm_data.fakeSCache.refCount = 1;
388 cm_data.fakeSCache.serverLock = -1;
389 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
391 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
392 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
393 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
394 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
398 cm_ValidateSCache(void)
400 cm_scache_t * scp, *lscp;
403 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
404 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
405 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
406 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
410 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
412 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
413 if (scp->magic != CM_SCACHE_MAGIC) {
414 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
415 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
418 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
419 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
420 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
423 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
424 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
425 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
428 if (i > cm_data.currentSCaches ) {
429 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
430 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
433 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
434 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
435 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
440 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
441 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
442 if (scp->magic != CM_SCACHE_MAGIC) {
443 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
444 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
447 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
448 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
449 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
452 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
453 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
454 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
457 if (i > cm_data.currentSCaches ) {
458 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
459 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
462 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
463 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
464 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
469 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
470 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
472 hash = CM_SCACHE_HASH(&scp->fid);
473 if (scp->magic != CM_SCACHE_MAGIC) {
474 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
475 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
478 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
479 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
480 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
483 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
484 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
485 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
489 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
490 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
496 return cm_dnlcValidate();
500 cm_SuspendSCache(void)
505 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
508 * After this call all servers are marked down.
509 * Do not clear the callbacks, instead change the
510 * expiration time so that the callbacks will be expired
511 * when the servers are marked back up. However, we
512 * want the callbacks to be preserved as long as the
513 * servers are down. That way if the machine resumes
514 * without network, the stat cache item will still be
519 lock_ObtainWrite(&cm_scacheLock);
520 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
521 if (scp->cbServerp) {
522 if (scp->flags & CM_SCACHEFLAG_PURERO) {
523 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
525 if (volp->cbExpiresRO == scp->cbExpires)
526 volp->cbExpiresRO = now+1;
530 scp->cbExpires = now+1;
533 lock_ReleaseWrite(&cm_scacheLock);
537 cm_ShutdownSCache(void)
539 cm_scache_t * scp, * nextp;
541 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
543 lock_ObtainWrite(&cm_scacheLock);
545 for ( scp = cm_data.allSCachesp; scp;
547 nextp = scp->allNextp;
548 lock_ReleaseWrite(&cm_scacheLock);
550 lock_ObtainWrite(&scp->dirlock);
552 lock_ObtainWrite(&scp->rw);
553 lock_ObtainWrite(&cm_scacheLock);
555 if (scp->randomACLp) {
556 cm_FreeAllACLEnts(scp);
559 if (scp->cbServerp) {
560 cm_PutServer(scp->cbServerp);
561 scp->cbServerp = NULL;
564 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
565 lock_ReleaseWrite(&scp->rw);
569 freeBtree(scp->dirBplus);
570 scp->dirBplus = NULL;
571 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
572 lock_ReleaseWrite(&scp->dirlock);
573 lock_FinalizeRWLock(&scp->dirlock);
575 lock_FinalizeRWLock(&scp->rw);
576 lock_FinalizeRWLock(&scp->bufCreateLock);
577 lock_FinalizeMutex(&scp->redirMx);
579 lock_ReleaseWrite(&cm_scacheLock);
581 return cm_dnlcShutdown();
584 void cm_InitSCache(int newFile, long maxSCaches)
586 static osi_once_t once;
588 if (osi_Once(&once)) {
589 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
591 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
592 cm_data.allSCachesp = NULL;
593 cm_data.currentSCaches = 0;
594 cm_data.maxSCaches = maxSCaches;
595 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
599 for ( scp = cm_data.allSCachesp; scp;
600 scp = scp->allNextp ) {
601 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
602 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
604 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
606 scp->cbServerp = NULL;
608 scp->volumeCreationDate = 0;
609 scp->fileLocksH = NULL;
610 scp->fileLocksT = NULL;
611 scp->serverLock = (-1);
612 scp->lastRefreshCycle = 0;
613 scp->exclusiveLocks = 0;
614 scp->sharedLocks = 0;
622 scp->dirBplus = NULL;
623 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
625 scp->waitQueueT = NULL;
626 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
628 scp->redirBufCount = 0;
629 scp->redirQueueT = NULL;
630 scp->redirQueueH = NULL;
631 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
634 cm_allFileLocks = NULL;
635 cm_freeFileLocks = NULL;
636 cm_lockRefreshCycle = 0;
637 cm_fakeSCacheInit(newFile);
638 cm_allFreeWaiters = NULL;
639 cm_dnlcInit(newFile);
644 /* version that doesn't bother creating the entry if we don't find it */
645 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
650 hash = CM_SCACHE_HASH(fidp);
652 if (fidp->cell == 0) {
656 lock_ObtainRead(&cm_scacheLock);
657 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
658 if (cm_FidCmp(fidp, &scp->fid) == 0) {
659 cm_HoldSCacheNoLock(scp);
660 lock_ConvertRToW(&cm_scacheLock);
661 cm_AdjustScacheLRU(scp);
662 lock_ReleaseWrite(&cm_scacheLock);
666 lock_ReleaseRead(&cm_scacheLock);
670 #ifdef DEBUG_REFCOUNT
671 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
672 cm_req_t *reqp, char * file, long line)
674 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
679 cm_scache_t *scp = NULL;
680 cm_scache_t *newScp = NULL;
682 cm_volume_t *volp = NULL;
684 int special = 0; // yj: boolean variable to test if file is on root.afs
686 extern cm_fid_t cm_rootFid;
689 hash = CM_SCACHE_HASH(fidp);
692 return CM_ERROR_INVAL;
694 #ifdef AFS_FREELANCE_CLIENT
695 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
696 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
697 !(fidp->vnode==0x1 && fidp->unique==0x1));
698 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
699 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
700 fidp->vnode==0x1 && fidp->unique==0x1);
703 // yj: check if we have the scp, if so, we don't need
704 // to do anything else
705 lock_ObtainRead(&cm_scacheLock);
706 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
707 if (cm_FidCmp(fidp, &scp->fid) == 0) {
708 #ifdef DEBUG_REFCOUNT
709 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
710 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
712 #ifdef AFS_FREELANCE_CLIENT
713 if (cm_freelanceEnabled && special &&
714 cm_data.fakeDirVersion != scp->dataVersion)
717 cm_HoldSCacheNoLock(scp);
719 lock_ConvertRToW(&cm_scacheLock);
720 cm_AdjustScacheLRU(scp);
721 lock_ReleaseWrite(&cm_scacheLock);
725 lock_ReleaseRead(&cm_scacheLock);
727 // yj: when we get here, it means we don't have an scp
728 // so we need to either load it or fake it, depending
729 // on whether the file is "special", see below.
731 // yj: if we're trying to get an scp for a file that's
732 // on root.afs of homecell, we want to handle it specially
733 // because we have to fill in the status stuff 'coz we
734 // don't want trybulkstat to fill it in for us
735 #ifdef AFS_FREELANCE_CLIENT
736 if (cm_freelanceEnabled && isRoot) {
737 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
738 /* freelance: if we are trying to get the root scp for the first
739 * time, we will just put in a place holder entry.
744 if (cm_freelanceEnabled && special) {
745 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
747 if (cm_getLocalMountPointChange()) {
748 cm_clearLocalMountPointChange();
749 cm_reInitLocalMountPoints();
753 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
755 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
756 return CM_ERROR_WOULDBLOCK;
759 lock_ObtainWrite(&scp->rw);
762 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
763 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
764 scp->dotdotFid.unique=1;
765 scp->dotdotFid.vnode=1;
766 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
767 lock_ObtainWrite(&cm_scacheLock);
768 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
769 scp->nextp = cm_data.scacheHashTablep[hash];
770 cm_data.scacheHashTablep[hash] = scp;
771 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
773 refCount = InterlockedIncrement(&scp->refCount);
774 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
775 lock_ReleaseWrite(&cm_scacheLock);
777 /* must be called after the scp->fid is set */
778 cm_FreelanceFetchMountPointString(scp);
779 cm_FreelanceFetchFileType(scp);
781 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
782 scp->length.HighPart = 0;
784 scp->unixModeBits=0777;
785 scp->clientModTime=FakeFreelanceModTime;
786 scp->serverModTime=FakeFreelanceModTime;
787 scp->parentUnique = 0x1;
788 scp->parentVnode=0x1;
790 scp->dataVersion=cm_data.fakeDirVersion;
791 scp->bufDataVersionLow=cm_data.fakeDirVersion;
792 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
794 lock_ReleaseWrite(&scp->rw);
796 #ifdef DEBUG_REFCOUNT
797 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
798 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
803 #endif /* AFS_FREELANCE_CLIENT */
805 /* we don't have the fid, recycle something */
806 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
807 if (newScp == NULL) {
808 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
809 return CM_ERROR_WOULDBLOCK;
811 #ifdef DEBUG_REFCOUNT
812 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
814 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
816 /* otherwise, we need to find the volume */
817 if (!cm_freelanceEnabled || !isRoot) {
818 cellp = cm_FindCellByID(fidp->cell, 0);
820 /* put back newScp so it can be reused */
821 lock_ObtainWrite(&cm_scacheLock);
822 newScp->flags |= CM_SCACHEFLAG_DELETED;
823 cm_AdjustScacheLRU(newScp);
824 lock_ReleaseWrite(&newScp->rw);
825 lock_ReleaseWrite(&cm_scacheLock);
826 return CM_ERROR_NOSUCHCELL;
829 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
831 /* put back newScp so it can be reused */
832 lock_ObtainWrite(&cm_scacheLock);
833 newScp->flags |= CM_SCACHEFLAG_DELETED;
834 cm_AdjustScacheLRU(newScp);
835 lock_ReleaseWrite(&newScp->rw);
836 lock_ReleaseWrite(&cm_scacheLock);
842 * otherwise, we have the volume, now reverify that the scp doesn't
843 * exist, and proceed. make sure that we hold the cm_scacheLock
844 * write-locked until the scp is put into the hash table in order
847 lock_ObtainWrite(&cm_scacheLock);
848 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
849 if (cm_FidCmp(fidp, &scp->fid) == 0) {
850 #ifdef DEBUG_REFCOUNT
851 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
852 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
856 cm_HoldSCacheNoLock(scp);
857 cm_AdjustScacheLRU(scp);
859 /* put back newScp so it can be reused */
860 newScp->flags |= CM_SCACHEFLAG_DELETED;
861 cm_AdjustScacheLRU(newScp);
862 lock_ReleaseWrite(&newScp->rw);
863 lock_ReleaseWrite(&cm_scacheLock);
872 if (!cm_freelanceEnabled || !isRoot) {
873 /* if this scache entry represents a volume root then we need
874 * to copy the dotdotFipd from the volume structure where the
875 * "master" copy is stored (defect 11489)
877 if (volp->vol[ROVOL].ID == fidp->volume) {
878 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
879 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
880 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
881 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
882 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
883 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
884 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
886 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
887 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
893 scp->nextp = cm_data.scacheHashTablep[hash];
894 cm_data.scacheHashTablep[hash] = scp;
895 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
896 refCount = InterlockedIncrement(&scp->refCount);
897 lock_ReleaseWrite(&cm_scacheLock);
898 lock_ReleaseWrite(&scp->rw);
899 #ifdef DEBUG_REFCOUNT
900 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
902 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
904 /* XXX - The following fields in the cm_scache are
911 /* now we have a held scache entry; just return it */
913 #ifdef DEBUG_REFCOUNT
914 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
915 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
920 /* Returns a held reference to the scache's parent
922 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
927 cm_scache_t * pscp = NULL;
929 lock_ObtainWrite(&cm_scacheLock);
930 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
932 if (cm_FidCmp(&scp->fid, &parent_fid)) {
933 i = CM_SCACHE_HASH(&parent_fid);
934 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
935 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
936 cm_HoldSCacheNoLock(pscp);
942 lock_ReleaseWrite(&cm_scacheLock);
947 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
949 cm_scache_waiter_t * w;
951 lock_ObtainWrite(&cm_scacheLock);
952 if (cm_allFreeWaiters == NULL) {
953 w = malloc(sizeof(*w));
954 memset(w, 0, sizeof(*w));
956 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
957 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
960 w->threadId = thrd_Current();
962 cm_HoldSCacheNoLock(scp);
966 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
967 lock_ReleaseWrite(&cm_scacheLock);
969 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
972 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
974 cm_scache_waiter_t * w;
977 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
979 lock_ObtainRead(&cm_scacheLock);
980 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
982 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
983 if (w->flags == flags && w->bufp == bufp) {
988 osi_assertx(w != NULL, "null cm_scache_waiter_t");
989 this_is_me = (w->threadId == thrd_Current());
990 lock_ReleaseRead(&cm_scacheLock);
993 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
997 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
999 lock_ObtainWrite(&cm_scacheLock);
1000 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1001 cm_ReleaseSCacheNoLock(scp);
1002 memset(w, 0, sizeof(*w));
1003 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1004 lock_ReleaseWrite(&cm_scacheLock);
1010 /* synchronize a fetch, store, read, write, fetch status or store status.
1011 * Called with scache mutex held, and returns with it held, but temporarily
1012 * drops it during the fetch.
1014 * At most one flag can be on in flags, if this is an RPC request.
1016 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1018 * There are a lot of weird restrictions here; here's an attempt to explain the
1019 * rationale for the concurrency restrictions implemented in this function.
1021 * First, although the file server will break callbacks when *another* machine
1022 * modifies a file or status block, the client itself is responsible for
1023 * concurrency control on its own requests. Callback breaking events are rare,
1024 * and simply invalidate any concurrent new status info.
1026 * In the absence of callback breaking messages, we need to know how to
1027 * synchronize incoming responses describing updates to files. We synchronize
1028 * operations that update the data version by comparing the data versions.
1029 * However, updates that do not update the data, but only the status, can't be
1030 * synchronized with fetches or stores, since there's nothing to compare
1031 * to tell which operation executed first at the server.
1033 * Thus, we can allow multiple ops that change file data, or dir data, and
1034 * fetches. However, status storing ops have to be done serially.
1036 * Furthermore, certain data-changing ops are incompatible: we can't read or
1037 * write a buffer while doing a truncate. We can't read and write the same
1038 * buffer at the same time, or write while fetching or storing, or read while
1039 * fetching a buffer (this may change). We can't fetch and store at the same
1042 * With respect to status, we can't read and write at the same time, read while
1043 * fetching, write while fetching or storing, or fetch and store at the same time.
1045 * We can't allow a get callback RPC to run in concurrently with something that
1046 * will return updated status, since we could start a call, have the server
1047 * return status, have another machine make an update to the status (which
1048 * doesn't change serverModTime), have the original machine get a new callback,
1049 * and then have the original machine merge in the early, old info from the
1050 * first call. At this point, the easiest way to avoid this problem is to have
1051 * getcallback calls conflict with all others for the same vnode. Other calls
1052 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1053 * vnode must be careful not to merge in their status unless they have obtained
1054 * a callback from the start of their call.
1056 * Note added 1/23/96
1057 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1058 * Each such RPC passes a FileLength parameter, which the server uses to do
1059 * pre-truncation if necessary. So if two RPC's are processed out of order at
1060 * the server, the one with the smaller FileLength will be processed last,
1061 * possibly resulting in a bogus truncation. The simplest way to avoid this
1062 * is to serialize all StoreData RPC's. This is the reason we defined
1063 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1065 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1066 * readers which may be requesting overlapping ranges.
1068 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1069 afs_uint32 rights, afs_uint32 flags)
1071 osi_queueData_t *qdp;
1074 afs_uint32 outRights;
1076 afs_uint32 sleep_scp_flags = 0;
1077 afs_uint32 sleep_buf_cmflags = 0;
1078 afs_uint32 sleep_scp_bufs = 0;
1081 lock_AssertWrite(&scp->rw);
1083 /* lookup this first */
1084 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1087 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1090 /* Do the access check. Now we don't really do the access check
1091 * atomically, since the caller doesn't expect the parent dir to be
1092 * returned locked, and that is what we'd have to do to prevent a
1093 * callback breaking message on the parent due to a setacl call from
1094 * being processed while we're running. So, instead, we check things
1095 * here, and if things look fine with the access, we proceed to finish
1096 * the rest of this check. Sort of a hack, but probably good enough.
1100 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1101 /* if we're bringing in a new status block, ensure that
1102 * we aren't already doing so, and that no one is
1103 * changing the status concurrently, either. We need
1104 * to do this, even if the status is of a different
1105 * type, since we don't have the ability to figure out,
1106 * in the AFS 3 protocols, which status-changing
1107 * operation ran first, or even which order a read and
1108 * a write occurred in.
1110 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1111 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1112 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1116 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1117 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1118 /* if we're going to make an RPC to change the status, make sure
1119 * that no one is bringing in or sending out the status.
1121 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1122 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1123 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1126 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1127 (scp->bufReadsp || scp->bufWritesp)) {
1128 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1132 if (flags & CM_SCACHESYNC_FETCHDATA) {
1133 /* if we're bringing in a new chunk of data, make sure that
1134 * nothing is happening to that chunk, and that we aren't
1135 * changing the basic file status info, either.
1137 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1138 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1139 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1142 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1143 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1147 if (flags & CM_SCACHESYNC_STOREDATA) {
1148 /* same as fetch data */
1149 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1150 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1151 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1154 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1155 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1160 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1161 /* Don't allow concurrent StoreData RPC's */
1162 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1163 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1168 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1169 /* Don't allow more than one BKG store request */
1170 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1171 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1176 if (flags & CM_SCACHESYNC_LOCK) {
1177 /* Don't allow concurrent fiddling with lock lists */
1178 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1179 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1184 /* now the operations that don't correspond to making RPCs */
1185 if (flags & CM_SCACHESYNC_GETSTATUS) {
1186 /* we can use the status that's here, if we're not
1187 * bringing in new status.
1189 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1190 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1194 if (flags & CM_SCACHESYNC_SETSTATUS) {
1195 /* we can make a change to the local status, as long as
1196 * the status isn't changing now.
1198 * If we're fetching or storing a chunk of data, we can
1199 * change the status locally, since the fetch/store
1200 * operations don't change any of the data that we're
1203 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1204 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1205 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1209 if (flags & CM_SCACHESYNC_READ) {
1210 /* we're going to read the data, make sure that the
1211 * status is available, and that the data is here. It
1212 * is OK to read while storing the data back.
1214 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1215 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1218 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1219 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1222 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1223 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1227 if (flags & CM_SCACHESYNC_WRITE) {
1228 /* don't write unless the status is stable and the chunk
1231 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1232 CM_SCACHEFLAG_SIZESTORING)) {
1233 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1236 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1238 CM_BUF_CMWRITING))) {
1239 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1241 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1242 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1243 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1249 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1250 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1251 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1254 lock_ReleaseMutex(&bufp->mx);
1255 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1257 lock_ReleaseWrite(&scp->rw);
1258 lock_ObtainMutex(&bufp->mx);
1259 lock_ObtainWrite(&scp->rw);
1263 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1269 /* can't check access rights without a callback */
1270 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1272 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1273 return CM_ERROR_READONLY;
1275 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1276 if (~outRights & rights)
1277 return CM_ERROR_NOACCESS;
1280 /* we don't know the required access rights */
1281 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1282 code = cm_GetAccessRights(scp, userp, reqp);
1284 lock_ReleaseWrite(&scp->rw);
1285 lock_ObtainMutex(&bufp->mx);
1286 lock_ObtainWrite(&scp->rw);
1294 if (flags & CM_SCACHESYNC_BULKREAD) {
1295 /* Don't allow concurrent fiddling with lock lists */
1296 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1297 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1302 /* if we get here, we're happy */
1306 /* first check if we're not supposed to wait: fail
1307 * in this case, returning with everything still locked.
1309 if (flags & CM_SCACHESYNC_NOWAIT)
1310 return CM_ERROR_WOULDBLOCK;
1312 /* These are used for minidump debugging */
1313 sleep_scp_flags = scp->flags; /* so we know why we slept */
1314 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1315 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1317 /* wait here, then try again */
1318 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1319 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1321 scp->waitRequests++;
1322 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1323 scp, scp->waitCount, scp->waitRequests);
1325 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1326 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1327 scp->waitCount = scp->waitRequests = 1;
1330 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1334 lock_ReleaseMutex(&bufp->mx);
1335 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1337 lock_ObtainMutex(&bufp->mx);
1338 lock_ObtainWrite(&scp->rw);
1339 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1341 cm_UpdateServerPriority();
1344 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1345 scp, scp->waitCount, scp->waitRequests);
1346 if (scp->waitCount == 0) {
1347 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1348 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1349 scp->waitRequests = 0;
1351 } /* big while loop */
1353 /* now, update the recorded state for RPC-type calls */
1354 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1355 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1356 if (flags & CM_SCACHESYNC_STORESTATUS)
1357 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1358 if (flags & CM_SCACHESYNC_SETSIZE)
1359 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1360 if (flags & CM_SCACHESYNC_STORESIZE)
1361 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1362 if (flags & CM_SCACHESYNC_GETCALLBACK)
1363 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1364 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1365 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1366 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1367 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1368 if (flags & CM_SCACHESYNC_LOCK)
1369 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1370 if (flags & CM_SCACHESYNC_BULKREAD)
1371 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1373 /* now update the buffer pointer */
1374 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1375 /* ensure that the buffer isn't already in the I/O list */
1376 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1377 tbufp = osi_GetQData(qdp);
1378 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1381 /* queue a held reference to the buffer in the "reading" I/O list */
1382 qdp = osi_QDAlloc();
1383 osi_SetQData(qdp, bufp);
1386 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1387 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1390 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1391 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1392 "attempting to store extents on a non-file object");
1394 /* ensure that the buffer isn't already in the I/O list */
1395 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1396 tbufp = osi_GetQData(qdp);
1397 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1400 /* queue a held reference to the buffer in the "writing" I/O list */
1401 qdp = osi_QDAlloc();
1402 osi_SetQData(qdp, bufp);
1404 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1405 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1408 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1409 /* mark the buffer as being written to. */
1410 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1416 /* for those syncops that setup for RPCs.
1417 * Called with scache locked.
1419 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1421 osi_queueData_t *qdp;
1424 lock_AssertWrite(&scp->rw);
1426 /* now, update the recorded state for RPC-type calls */
1427 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1428 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1429 if (flags & CM_SCACHESYNC_STORESTATUS)
1430 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1431 if (flags & CM_SCACHESYNC_SETSIZE)
1432 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1433 if (flags & CM_SCACHESYNC_STORESIZE)
1434 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1435 if (flags & CM_SCACHESYNC_GETCALLBACK)
1436 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1437 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1438 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1439 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1440 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1441 if (flags & CM_SCACHESYNC_LOCK)
1442 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1443 if (flags & CM_SCACHESYNC_BULKREAD)
1444 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1446 /* now update the buffer pointer */
1447 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1450 /* ensure that the buffer is in the I/O list */
1451 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1452 tbufp = osi_GetQData(qdp);
1457 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1461 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1462 if (bufp->flags & CM_BUF_WAITING) {
1463 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1464 osi_Wakeup((LONG_PTR) &bufp);
1470 /* now update the buffer pointer */
1471 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1473 /* ensure that the buffer is in the I/O list */
1474 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1475 tbufp = osi_GetQData(qdp);
1480 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1484 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1485 if (bufp->flags & CM_BUF_WAITING) {
1486 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1487 osi_Wakeup((LONG_PTR) &bufp);
1493 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1494 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1495 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1498 /* and wakeup anyone who is waiting */
1499 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1500 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1501 osi_Wakeup((LONG_PTR) &scp->flags);
1505 /* merge in a response from an RPC. The scp must be locked, and the callback
1508 * Don't overwrite any status info that is dirty, since we could have a store
1509 * operation (such as store data) that merges some info in, and we don't want
1510 * to lose the local updates. Typically, there aren't many updates we do
1511 * locally, anyway, probably only mtime.
1513 * There is probably a bug in here where a chmod (which doesn't change
1514 * serverModTime) that occurs between two fetches, both of whose responses are
1515 * handled after the callback breaking is done, but only one of whose calls
1516 * started before that, can cause old info to be merged from the first call.
1518 void cm_MergeStatus(cm_scache_t *dscp,
1519 cm_scache_t *scp, AFSFetchStatus *statusp,
1520 AFSVolSync *volsyncp,
1521 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1523 afs_uint64 dataVersion;
1524 struct cm_volume *volp = NULL;
1525 struct cm_cell *cellp = NULL;
1526 int rdr_invalidate = 0;
1527 afs_uint32 activeRPCs;
1529 lock_AssertWrite(&scp->rw);
1531 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1533 // yj: i want to create some fake status for the /afs directory and the
1534 // entries under that directory
1535 #ifdef AFS_FREELANCE_CLIENT
1536 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1537 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1538 if (scp == cm_data.rootSCachep) {
1539 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1540 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1541 statusp->Length = cm_fakeDirSize;
1542 statusp->Length_hi = 0;
1544 statusp->FileType = scp->fileType;
1545 statusp->Length = scp->length.LowPart;
1546 statusp->Length_hi = scp->length.HighPart;
1548 statusp->InterfaceVersion = 0x1;
1549 statusp->LinkCount = scp->linkCount;
1550 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1551 statusp->Author = 0x1;
1552 statusp->Owner = 0x0;
1553 statusp->CallerAccess = 0x9;
1554 statusp->AnonymousAccess = 0x9;
1555 statusp->UnixModeBits = 0777;
1556 statusp->ParentVnode = 0x1;
1557 statusp->ParentUnique = 0x1;
1558 statusp->ResidencyMask = 0;
1559 statusp->ClientModTime = FakeFreelanceModTime;
1560 statusp->ServerModTime = FakeFreelanceModTime;
1562 statusp->SyncCounter = 0;
1563 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1564 statusp->lockCount = 0;
1565 statusp->errorCode = 0;
1567 #endif /* AFS_FREELANCE_CLIENT */
1569 if (statusp->errorCode != 0) {
1570 switch (statusp->errorCode) {
1575 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1577 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1579 if (scp->fid.vnode & 0x1)
1580 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1582 scp->fileType = 0; /* unknown */
1584 scp->serverModTime = 0;
1585 scp->clientModTime = 0;
1586 scp->length.LowPart = 0;
1587 scp->length.HighPart = 0;
1588 scp->serverLength.LowPart = 0;
1589 scp->serverLength.HighPart = 0;
1593 scp->unixModeBits = 0;
1595 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1596 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1597 scp->fsLockCount = 0;
1600 scp->parentVnode = dscp->fid.vnode;
1601 scp->parentUnique = dscp->fid.unique;
1603 scp->parentVnode = 0;
1604 scp->parentUnique = 0;
1607 if (RDR_Initialized)
1610 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1613 dataVersion = statusp->dataVersionHigh;
1615 dataVersion |= statusp->DataVersion;
1617 if (!(flags & CM_MERGEFLAG_FORCE) &&
1618 dataVersion < scp->dataVersion &&
1619 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1621 cellp = cm_FindCellByID(scp->fid.cell, 0);
1622 if (scp->cbServerp) {
1623 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1624 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1625 osi_Log2(afsd_logp, "old data from server %x volume %s",
1626 scp->cbServerp->addr.sin_addr.s_addr,
1627 volp ? volp->namep : "(unknown)");
1629 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1630 scp, scp->dataVersion, dataVersion);
1631 /* we have a number of data fetch/store operations running
1632 * concurrently, and we can tell which one executed last at the
1633 * server by its mtime.
1634 * Choose the one with the largest mtime, and ignore the rest.
1636 * These concurrent calls are incompatible with setting the
1637 * mtime, so we won't have a locally changed mtime here.
1639 * We could also have ACL info for a different user than usual,
1640 * in which case we have to do that part of the merge, anyway.
1641 * We won't have to worry about the info being old, since we
1642 * won't have concurrent calls
1643 * that change file status running from this machine.
1645 * Added 3/17/98: if we see data version regression on an RO
1646 * file, it's probably due to a server holding an out-of-date
1647 * replica, rather than to concurrent RPC's. Failures to
1648 * release replicas are now flagged by the volserver, but only
1649 * since AFS 3.4 5.22, so there are plenty of clients getting
1650 * out-of-date replicas out there.
1652 * If we discover an out-of-date replica, by this time it's too
1653 * late to go to another server and retry. Also, we can't
1654 * reject the merge, because then there is no way for
1655 * GetAccess to do its work, and the caller gets into an
1656 * infinite loop. So we just grin and bear it.
1658 if (!(scp->flags & CM_SCACHEFLAG_RO))
1662 if (cm_readonlyVolumeVersioning)
1663 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1665 scp->serverModTime = statusp->ServerModTime;
1667 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1668 scp->clientModTime = statusp->ClientModTime;
1670 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1671 scp->length.LowPart = statusp->Length;
1672 scp->length.HighPart = statusp->Length_hi;
1675 scp->serverLength.LowPart = statusp->Length;
1676 scp->serverLength.HighPart = statusp->Length_hi;
1678 scp->linkCount = statusp->LinkCount;
1679 scp->owner = statusp->Owner;
1680 scp->group = statusp->Group;
1681 scp->unixModeBits = statusp->UnixModeBits & 07777;
1683 if (statusp->FileType == File)
1684 scp->fileType = CM_SCACHETYPE_FILE;
1685 else if (statusp->FileType == Directory)
1686 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1687 else if (statusp->FileType == SymbolicLink) {
1688 if ((scp->unixModeBits & 0111) == 0)
1689 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1691 scp->fileType = CM_SCACHETYPE_SYMLINK;
1694 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1695 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1697 /* and other stuff */
1698 scp->parentVnode = statusp->ParentVnode;
1699 scp->parentUnique = statusp->ParentUnique;
1701 /* -1 is a write lock; any positive values are read locks */
1702 scp->fsLockCount = (afs_int32)statusp->lockCount;
1704 /* and merge in the private acl cache info, if this is more than the public
1705 * info; merge in the public stuff in any case.
1707 scp->anyAccess = statusp->AnonymousAccess;
1709 if (userp != NULL) {
1710 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1713 if (dataVersion != 0 &&
1714 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1715 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > activeRPCs)) {
1717 * We now know that all of the data buffers that we have associated
1718 * with this scp are invalid. Subsequent operations will go faster
1719 * if the buffers are removed from the hash tables.
1721 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1722 * those version numbers will be updated as part of the directory operation.
1724 * We do not remove storedata buffers because they will still be valid.
1729 cm_buf_t *bp, *prevBp, *nextBp;
1731 lock_ObtainWrite(&buf_globalLock);
1732 i = BUF_FILEHASH(&scp->fid);
1733 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1735 nextBp = bp->fileHashp;
1737 * if the buffer belongs to this stat cache entry
1738 * and the buffer mutex can be obtained, check the
1739 * reference count and if it is zero, remove the buffer
1740 * from the hash tables. If there are references,
1741 * the buffer might be updated to the current version
1742 * so leave it in place.
1744 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1745 lock_TryMutex(&bp->mx)) {
1746 if (bp->refCount == 0 &&
1747 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1748 !(bp->qFlags & CM_BUF_QREDIR)) {
1749 prevBp = bp->fileHashBackp;
1750 bp->fileHashBackp = bp->fileHashp = NULL;
1752 prevBp->fileHashp = nextBp;
1754 cm_data.buf_fileHashTablepp[i] = nextBp;
1756 nextBp->fileHashBackp = prevBp;
1758 j = BUF_HASH(&bp->fid, &bp->offset);
1759 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1760 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1765 /* we better find it */
1766 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1768 *lbpp = bp->hashp; /* hash out */
1771 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1773 lock_ReleaseMutex(&bp->mx);
1776 lock_ReleaseWrite(&buf_globalLock);
1780 * If the dataVersion has changed, the mountPointStringp must be cleared
1781 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1782 * does not update a mountpoint or symlink by altering the contents of
1783 * the file data; but the Unix CM does.
1785 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1786 scp->mountPointStringp[0] = '\0';
1788 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1789 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1791 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1792 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1793 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1794 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1795 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1798 /* We maintain a range of buffer dataVersion values which are considered
1799 * valid. This avoids the need to update the dataVersion on each buffer
1800 * object during an uncontested storeData operation. As a result this
1801 * merge status no longer has performance characteristics derived from
1802 * the size of the file.
1804 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > activeRPCs) ||
1805 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1806 scp->bufDataVersionLow == 0)
1807 scp->bufDataVersionLow = dataVersion;
1809 if (RDR_Initialized) {
1810 if (scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1812 } else if ( ( !(reqp->flags & CM_REQ_SOURCE_REDIR) || !(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA))) &&
1813 scp->dataVersion != dataVersion && (dataVersion - scp->dataVersion > activeRPCs - 1)) {
1815 } else if ( (reqp->flags & CM_REQ_SOURCE_REDIR) && (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1816 dataVersion - scp->dataVersion > activeRPCs) {
1820 scp->dataVersion = dataVersion;
1823 * If someone is waiting for status information, we can wake them up
1824 * now even though the entity that issued the FetchStatus may not
1825 * have completed yet.
1827 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1830 * We just successfully merged status on the stat cache object.
1831 * This means that the associated volume must be online.
1835 cellp = cm_FindCellByID(scp->fid.cell, 0);
1836 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1839 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1840 if (statep->state != vl_online) {
1841 lock_ObtainWrite(&volp->rw);
1842 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1843 statep->state = vl_online;
1844 lock_ReleaseWrite(&volp->rw);
1853 * The scache rw lock cannot be held across the invalidation.
1854 * Doing so can result in deadlocks with other threads processing
1855 * requests initiated by the afs redirector.
1857 if (rdr_invalidate) {
1858 lock_ReleaseWrite(&scp->rw);
1859 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1860 scp->fid.unique, scp->fid.hash,
1861 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
1862 lock_ObtainWrite(&scp->rw);
1866 /* note that our stat cache info is incorrect, so force us eventually
1867 * to stat the file again. There may be dirty data associated with
1868 * this vnode, and we want to preserve that information.
1870 * This function works by simply simulating a loss of the callback.
1872 * This function must be called with the scache locked.
1874 void cm_DiscardSCache(cm_scache_t *scp)
1876 lock_AssertWrite(&scp->rw);
1877 if (scp->cbServerp) {
1878 cm_PutServer(scp->cbServerp);
1879 scp->cbServerp = NULL;
1882 scp->volumeCreationDate = 0;
1883 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
1884 cm_dnlcPurgedp(scp);
1885 cm_dnlcPurgevp(scp);
1886 cm_FreeAllACLEnts(scp);
1888 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1889 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1891 /* Force mount points and symlinks to be re-evaluated */
1892 scp->mountPointStringp[0] = '\0';
1895 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1897 afsFidp->Volume = fidp->volume;
1898 afsFidp->Vnode = fidp->vnode;
1899 afsFidp->Unique = fidp->unique;
1902 #ifdef DEBUG_REFCOUNT
1903 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1905 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1910 osi_assertx(scp != NULL, "null cm_scache_t");
1911 lock_AssertAny(&cm_scacheLock);
1912 refCount = InterlockedIncrement(&scp->refCount);
1913 #ifdef DEBUG_REFCOUNT
1914 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1915 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1919 #ifdef DEBUG_REFCOUNT
1920 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1922 void cm_HoldSCache(cm_scache_t *scp)
1927 osi_assertx(scp != NULL, "null cm_scache_t");
1928 lock_ObtainRead(&cm_scacheLock);
1929 refCount = InterlockedIncrement(&scp->refCount);
1930 #ifdef DEBUG_REFCOUNT
1931 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1932 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1934 lock_ReleaseRead(&cm_scacheLock);
1937 #ifdef DEBUG_REFCOUNT
1938 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1940 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1945 osi_assertx(scp != NULL, "null cm_scache_t");
1946 lock_AssertAny(&cm_scacheLock);
1948 refCount = InterlockedDecrement(&scp->refCount);
1949 #ifdef DEBUG_REFCOUNT
1951 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1953 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1954 #ifdef DEBUG_REFCOUNT
1955 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1956 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1959 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1963 lockstate = lock_GetRWLockState(&cm_scacheLock);
1964 if (lockstate != OSI_RWLOCK_WRITEHELD)
1965 lock_ReleaseRead(&cm_scacheLock);
1967 lock_ReleaseWrite(&cm_scacheLock);
1969 lock_ObtainWrite(&scp->rw);
1970 if (scp->flags & CM_SCACHEFLAG_DELETED)
1973 if (refCount == 0 && deleted) {
1974 lock_ObtainWrite(&cm_scacheLock);
1975 cm_RecycleSCache(scp, 0);
1976 if (lockstate != OSI_RWLOCK_WRITEHELD)
1977 lock_ConvertWToR(&cm_scacheLock);
1979 if (lockstate != OSI_RWLOCK_WRITEHELD)
1980 lock_ObtainRead(&cm_scacheLock);
1982 lock_ObtainWrite(&cm_scacheLock);
1984 lock_ReleaseWrite(&scp->rw);
1988 #ifdef DEBUG_REFCOUNT
1989 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1991 void cm_ReleaseSCache(cm_scache_t *scp)
1996 osi_assertx(scp != NULL, "null cm_scache_t");
1997 lock_ObtainRead(&cm_scacheLock);
1998 refCount = InterlockedDecrement(&scp->refCount);
1999 #ifdef DEBUG_REFCOUNT
2001 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2003 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2004 #ifdef DEBUG_REFCOUNT
2005 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2006 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2008 lock_ReleaseRead(&cm_scacheLock);
2010 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2012 lock_ObtainWrite(&scp->rw);
2013 if (scp->flags & CM_SCACHEFLAG_DELETED)
2016 lock_ObtainWrite(&cm_scacheLock);
2017 cm_RecycleSCache(scp, 0);
2018 lock_ReleaseWrite(&cm_scacheLock);
2020 lock_ReleaseWrite(&scp->rw);
2024 /* just look for the scp entry to get filetype */
2025 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2026 int cm_FindFileType(cm_fid_t *fidp)
2031 hash = CM_SCACHE_HASH(fidp);
2033 osi_assertx(fidp->cell != 0, "unassigned cell value");
2035 lock_ObtainWrite(&cm_scacheLock);
2036 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2037 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2038 lock_ReleaseWrite(&cm_scacheLock);
2039 return scp->fileType;
2042 lock_ReleaseWrite(&cm_scacheLock);
2046 /* dump all scp's that have reference count > 0 to a file.
2047 * cookie is used to identify this batch for easy parsing,
2048 * and it a string provided by a caller
2050 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2059 lock_ObtainRead(&cm_scacheLock);
2061 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2062 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2064 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2067 char *srvStr = NULL;
2068 afs_uint32 srvStrRpc = TRUE;
2072 if (scp->cbServerp) {
2073 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2074 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2075 srvStr = malloc(16); /* enough for 255.255.255.255 */
2077 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2081 if (scp->cbExpires) {
2086 cbt[strlen(cbt)-1] = '\0';
2089 if (scp->volumeCreationDate) {
2090 t = scp->volumeCreationDate;
2093 cdrot = strdup(cdrot);
2094 cdrot[strlen(cdrot)-1] = '\0';
2098 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2099 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2100 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2101 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2102 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2103 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2104 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2105 cdrot ? cdrot : "<none>", scp->refCount);
2106 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2108 if (scp->fileLocksH) {
2109 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2110 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2112 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2113 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2114 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2115 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2116 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2117 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2118 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2121 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2122 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2127 RpcStringFree(&srvStr);
2137 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2138 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2139 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2140 cookie, cm_data.scacheHashTableSize);
2141 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2143 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2145 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2147 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2148 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2149 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2153 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2154 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2156 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2157 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2159 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2160 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2161 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",
2162 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2163 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2164 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2167 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2168 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2171 lock_ReleaseRead(&cm_scacheLock);