2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
26 #include <afs/unified_afs.h>
28 /*extern void afsi_log(char *pattern, ...);*/
30 extern osi_hyper_t hzero;
33 osi_queue_t *cm_allFileLocks;
34 osi_queue_t *cm_freeFileLocks;
35 unsigned long cm_lockRefreshCycle;
37 /* lock for globals */
38 osi_rwlock_t cm_scacheLock;
40 /* Dummy scache entry for use with pioctl fids */
41 cm_scache_t cm_fakeSCache;
43 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
45 #ifdef AFS_FREELANCE_CLIENT
46 extern osi_mutex_t cm_Freelance_Lock;
50 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
54 lock_ObtainWrite(&cm_data.rootSCachep->rw);
55 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
56 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
58 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
59 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
61 return cm_data.rootSCachep;
65 /* must be called with cm_scacheLock write-locked! */
66 void cm_AdjustScacheLRU(cm_scache_t *scp)
68 lock_AssertWrite(&cm_scacheLock);
69 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
70 if (scp->flags & CM_SCACHEFLAG_DELETED) {
71 /* Since it has been deleted make it the first to be recycled. */
72 osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
74 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
78 /* call with cm_scacheLock write-locked and scp rw held */
79 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
85 lock_AssertWrite(&cm_scacheLock);
86 lock_AssertWrite(&scp->rw);
87 if (scp->flags & CM_SCACHEFLAG_INHASH) {
88 /* hash it out first */
89 i = CM_SCACHE_HASH(&scp->fid);
90 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
92 lscpp = &tscp->nextp, tscp = tscp->nextp) {
96 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
103 /* called with cm_scacheLock and scp write-locked */
104 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
107 /* destroy directory Bplus Tree */
109 LARGE_INTEGER start, end;
111 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
113 * We are not holding the dirlock and obtaining it
114 * requires that we drop the scp->rw. As a result
115 * we will leave the dirBplus tree intact but
116 * invalidate the version number so that whatever
117 * operation is currently active can safely complete
118 * but the contents will be ignored on the next
119 * directory operation.
121 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
125 QueryPerformanceCounter(&start);
127 freeBtree(scp->dirBplus);
128 scp->dirBplus = NULL;
129 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
130 QueryPerformanceCounter(&end);
133 lock_ReleaseWrite(&scp->dirlock);
135 bplus_free_time += (end.QuadPart - start.QuadPart);
140 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
141 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
147 lock_AssertWrite(&cm_scacheLock);
148 lock_AssertWrite(&scp->rw);
150 if (scp->refCount != 0) {
155 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
156 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
158 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
163 if (scp->redirBufCount != 0) {
168 fileType = scp->fileType;
169 callback = scp->cbExpires ? 1 : 0;
171 cm_RemoveSCacheFromHashTable(scp);
173 /* invalidate so next merge works fine;
174 * also initialize some flags */
176 _InterlockedAnd(&scp->flags,
177 ~(CM_SCACHEFLAG_STATD
178 | CM_SCACHEFLAG_DELETED
180 | CM_SCACHEFLAG_PURERO
181 | CM_SCACHEFLAG_OVERQUOTA
182 | CM_SCACHEFLAG_OUTOFSPACE
183 | CM_SCACHEFLAG_EACCESS));
184 scp->serverModTime = 0;
185 scp->dataVersion = CM_SCACHE_VERSION_BAD;
186 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
187 scp->bulkStatProgress = hzero;
189 scp->waitQueueT = NULL;
191 if (scp->cbServerp) {
192 cm_PutServer(scp->cbServerp);
193 scp->cbServerp = NULL;
196 scp->volumeCreationDate = 0;
204 /* remove from dnlc */
208 /* discard cached status; if non-zero, Close
209 * tried to store this to server but failed */
212 /* discard symlink info */
213 scp->mountPointStringp[0] = '\0';
214 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
215 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
217 /* reset locking info */
218 scp->fileLocksH = NULL;
219 scp->fileLocksT = NULL;
220 scp->serverLock = (-1);
221 scp->exclusiveLocks = 0;
222 scp->sharedLocks = 0;
223 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
224 scp->fsLockCount = 0;
226 /* not locked, but there can be no references to this guy
227 * while we hold the global refcount lock.
229 cm_FreeAllACLEnts(scp);
231 cm_ResetSCacheDirectory(scp, 0);
233 if (RDR_Initialized && callback) {
235 * We drop the cm_scacheLock because it may be required to
236 * satisfy an ioctl request from the redirector. It should
237 * be safe to hold the scp->rw lock here because at this
238 * point (a) the object has just been recycled so the fid
239 * is nul and there are no requests that could possibly
240 * be issued by the redirector that would depend upon it.
242 lock_ReleaseWrite(&cm_scacheLock);
243 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
244 fid.unique, fid.hash,
245 fileType, AFS_INVALIDATE_EXPIRED);
246 lock_ObtainWrite(&cm_scacheLock);
254 * called with cm_scacheLock write-locked; find a vnode to recycle.
255 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
256 * returns scp->rw write-locked.
259 cm_GetNewSCache(afs_uint32 locked)
261 cm_scache_t *scp = NULL;
262 cm_scache_t *scp_prev = NULL;
263 cm_scache_t *scp_next = NULL;
267 lock_AssertWrite(&cm_scacheLock);
269 lock_ObtainWrite(&cm_scacheLock);
271 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
272 /* There were no deleted scache objects that we could use. Try to find
273 * one that simply hasn't been used in a while.
275 for (attempt = 0 ; attempt < 128; attempt++) {
276 afs_uint32 count = 0;
278 for ( scp = cm_data.scacheLRULastp;
280 scp = (cm_scache_t *) osi_QPrev(&scp->q))
283 * We save the prev and next pointers in the
284 * LRU because we are going to drop the cm_scacheLock and
285 * the order of the list could change out from beneath us.
286 * If both changed, it means that this entry has been moved
287 * within the LRU and it should no longer be recycled.
289 scp_prev = (cm_scache_t *) osi_QPrev(&scp->q);
290 scp_next = (cm_scache_t *) osi_QNext(&scp->q);
293 /* It is possible for the refCount to be zero and for there still
294 * to be outstanding dirty buffers. If there are dirty buffers,
295 * we must not recycle the scp.
297 * If the object is in use by the redirector, then avoid recycling
298 * it unless we have to.
300 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
301 afs_uint32 buf_dirty = 0;
302 afs_uint32 buf_rdr = 0;
304 lock_ReleaseWrite(&cm_scacheLock);
305 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
307 buf_rdr = buf_RDRBuffersExist(&scp->fid);
309 if (!buf_dirty && !buf_rdr) {
313 if (!lock_TryWrite(&scp->rw)) {
314 lock_ObtainWrite(&cm_scacheLock);
315 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
316 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
322 lock_ObtainWrite(&cm_scacheLock);
323 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
324 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
326 lock_ReleaseWrite(&scp->rw);
330 /* Found a likely candidate. Save type and fid in case we succeed */
332 fileType = scp->fileType;
334 if (!cm_RecycleSCache(scp, 0)) {
335 /* we found an entry, so return it.
336 * remove from the LRU queue and put it back at the
337 * head of the LRU queue.
339 cm_AdjustScacheLRU(scp);
342 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
345 lock_ReleaseWrite(&scp->rw);
346 } else if (!buf_rdr) {
347 osi_Log1(afsd_logp, "GetNewSCache dirty buffers scp 0x%p", scp);
348 lock_ObtainWrite(&cm_scacheLock);
349 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
350 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
353 osi_Log1(afsd_logp,"GetNewSCache redirector is holding extents scp 0x%p", scp);
354 lock_ObtainWrite(&cm_scacheLock);
355 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
356 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
362 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
367 /* if we get here, we should allocate a new scache entry. We either are below
368 * quota or we have a leak and need to allocate a new one to avoid panicing.
370 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
371 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
372 "invalid cm_scache_t address");
373 memset(scp, 0, sizeof(cm_scache_t));
374 scp->magic = CM_SCACHE_MAGIC;
375 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
376 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
377 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
379 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
381 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
382 scp->serverLock = -1;
383 scp->dataVersion = CM_SCACHE_VERSION_BAD;
384 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
385 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
387 /* and put it in the LRU queue */
388 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
389 cm_data.currentSCaches++;
390 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
392 scp->allNextp = cm_data.allSCachesp;
393 cm_data.allSCachesp = scp;
397 lock_ReleaseWrite(&cm_scacheLock);
402 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
405 fidp->volume = volume;
407 fidp->unique = unique;
408 CM_FID_GEN_HASH(fidp);
411 /* like strcmp, only for fids */
412 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
414 if (ap->hash != bp->hash)
416 if (ap->vnode != bp->vnode)
418 if (ap->volume != bp->volume)
420 if (ap->unique != bp->unique)
422 if (ap->cell != bp->cell)
427 void cm_fakeSCacheInit(int newFile)
430 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
431 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
432 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
433 cm_data.fakeSCache.cbExpires = (time_t)-1;
434 /* can leave clientModTime at 0 */
435 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
436 cm_data.fakeSCache.unixModeBits = 0777;
437 cm_data.fakeSCache.length.LowPart = 1000;
438 cm_data.fakeSCache.linkCount = 1;
439 cm_data.fakeSCache.refCount = 1;
440 cm_data.fakeSCache.serverLock = -1;
441 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
443 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
444 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
445 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
446 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
450 cm_ValidateSCache(void)
452 cm_scache_t * scp, *lscp;
455 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
456 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
457 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
458 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
462 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
464 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
465 if (scp->magic != CM_SCACHE_MAGIC) {
466 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
467 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
470 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
471 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
472 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
475 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
476 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
477 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
480 if (i > cm_data.currentSCaches ) {
481 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
482 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
485 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
486 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
487 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
492 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
493 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
494 if (scp->magic != CM_SCACHE_MAGIC) {
495 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
496 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
499 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
500 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
501 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
504 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
505 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
506 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
509 if (i > cm_data.currentSCaches ) {
510 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
511 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
514 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
515 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
516 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
521 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
522 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
524 hash = CM_SCACHE_HASH(&scp->fid);
525 if (scp->magic != CM_SCACHE_MAGIC) {
526 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
527 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
530 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
531 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
532 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
535 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
536 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
537 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
541 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
542 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
548 return cm_dnlcValidate();
552 cm_SuspendSCache(void)
557 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
560 * After this call all servers are marked down.
561 * Do not clear the callbacks, instead change the
562 * expiration time so that the callbacks will be expired
563 * when the servers are marked back up. However, we
564 * want the callbacks to be preserved as long as the
565 * servers are down. That way if the machine resumes
566 * without network, the stat cache item will still be
571 lock_ObtainWrite(&cm_scacheLock);
572 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
573 if (scp->cbServerp) {
574 if (scp->flags & CM_SCACHEFLAG_PURERO) {
575 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
577 if (volp->cbExpiresRO == scp->cbExpires)
578 volp->cbExpiresRO = now+1;
582 scp->cbExpires = now+1;
585 lock_ReleaseWrite(&cm_scacheLock);
589 cm_ShutdownSCache(void)
591 cm_scache_t * scp, * nextp;
593 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
595 lock_ObtainWrite(&cm_scacheLock);
597 for ( scp = cm_data.allSCachesp; scp;
599 nextp = scp->allNextp;
600 lock_ReleaseWrite(&cm_scacheLock);
602 lock_ObtainWrite(&scp->dirlock);
604 lock_ObtainWrite(&scp->rw);
605 lock_ObtainWrite(&cm_scacheLock);
607 if (scp->randomACLp) {
608 cm_FreeAllACLEnts(scp);
611 if (scp->cbServerp) {
612 cm_PutServer(scp->cbServerp);
613 scp->cbServerp = NULL;
616 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
617 lock_ReleaseWrite(&scp->rw);
621 freeBtree(scp->dirBplus);
622 scp->dirBplus = NULL;
623 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
624 lock_ReleaseWrite(&scp->dirlock);
625 lock_FinalizeRWLock(&scp->dirlock);
627 lock_FinalizeRWLock(&scp->rw);
628 lock_FinalizeRWLock(&scp->bufCreateLock);
629 lock_FinalizeMutex(&scp->redirMx);
631 lock_ReleaseWrite(&cm_scacheLock);
633 return cm_dnlcShutdown();
636 void cm_InitSCache(int newFile, long maxSCaches)
638 static osi_once_t once;
640 if (osi_Once(&once)) {
641 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
643 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
644 cm_data.allSCachesp = NULL;
645 cm_data.currentSCaches = 0;
646 cm_data.maxSCaches = maxSCaches;
647 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
651 for ( scp = cm_data.allSCachesp; scp;
652 scp = scp->allNextp ) {
653 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
654 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
656 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
658 scp->cbServerp = NULL;
660 scp->volumeCreationDate = 0;
661 scp->fileLocksH = NULL;
662 scp->fileLocksT = NULL;
663 scp->serverLock = (-1);
664 scp->lastRefreshCycle = 0;
665 scp->exclusiveLocks = 0;
666 scp->sharedLocks = 0;
674 scp->dirBplus = NULL;
675 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
677 scp->waitQueueT = NULL;
678 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
680 scp->redirBufCount = 0;
681 scp->redirQueueT = NULL;
682 scp->redirQueueH = NULL;
683 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
686 cm_allFileLocks = NULL;
687 cm_freeFileLocks = NULL;
688 cm_lockRefreshCycle = 0;
689 cm_fakeSCacheInit(newFile);
690 cm_allFreeWaiters = NULL;
691 cm_dnlcInit(newFile);
696 /* version that doesn't bother creating the entry if we don't find it */
697 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
702 hash = CM_SCACHE_HASH(fidp);
704 if (fidp->cell == 0) {
708 lock_ObtainRead(&cm_scacheLock);
709 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
710 if (cm_FidCmp(fidp, &scp->fid) == 0) {
711 cm_HoldSCacheNoLock(scp);
712 lock_ConvertRToW(&cm_scacheLock);
713 cm_AdjustScacheLRU(scp);
714 lock_ReleaseWrite(&cm_scacheLock);
718 lock_ReleaseRead(&cm_scacheLock);
722 #ifdef DEBUG_REFCOUNT
723 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
724 cm_req_t *reqp, char * file, long line)
726 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
731 cm_scache_t *scp = NULL;
732 cm_scache_t *newScp = NULL;
734 cm_volume_t *volp = NULL;
736 int special = 0; // yj: boolean variable to test if file is on root.afs
738 extern cm_fid_t cm_rootFid;
741 hash = CM_SCACHE_HASH(fidp);
744 return CM_ERROR_INVAL;
746 #ifdef AFS_FREELANCE_CLIENT
747 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
748 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
749 !(fidp->vnode==0x1 && fidp->unique==0x1));
750 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
751 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
752 fidp->vnode==0x1 && fidp->unique==0x1);
755 // yj: check if we have the scp, if so, we don't need
756 // to do anything else
757 lock_ObtainRead(&cm_scacheLock);
758 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
759 if (cm_FidCmp(fidp, &scp->fid) == 0) {
760 #ifdef DEBUG_REFCOUNT
761 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
762 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
764 #ifdef AFS_FREELANCE_CLIENT
765 if (cm_freelanceEnabled && special &&
766 cm_data.fakeDirVersion != scp->dataVersion)
769 cm_HoldSCacheNoLock(scp);
771 lock_ConvertRToW(&cm_scacheLock);
772 cm_AdjustScacheLRU(scp);
773 lock_ReleaseWrite(&cm_scacheLock);
777 lock_ReleaseRead(&cm_scacheLock);
779 // yj: when we get here, it means we don't have an scp
780 // so we need to either load it or fake it, depending
781 // on whether the file is "special", see below.
783 // yj: if we're trying to get an scp for a file that's
784 // on root.afs of homecell, we want to handle it specially
785 // because we have to fill in the status stuff 'coz we
786 // don't want trybulkstat to fill it in for us
787 #ifdef AFS_FREELANCE_CLIENT
788 if (cm_freelanceEnabled && isRoot) {
789 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
790 /* freelance: if we are trying to get the root scp for the first
791 * time, we will just put in a place holder entry.
796 if (cm_freelanceEnabled && special) {
797 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
799 if (cm_getLocalMountPointChange()) {
800 cm_clearLocalMountPointChange();
801 cm_reInitLocalMountPoints();
805 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
807 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
808 return CM_ERROR_WOULDBLOCK;
811 lock_ObtainWrite(&scp->rw);
814 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
815 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
816 scp->dotdotFid.unique=1;
817 scp->dotdotFid.vnode=1;
818 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
819 lock_ObtainWrite(&cm_scacheLock);
820 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
821 scp->nextp = cm_data.scacheHashTablep[hash];
822 cm_data.scacheHashTablep[hash] = scp;
823 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
825 refCount = InterlockedIncrement(&scp->refCount);
826 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
827 lock_ReleaseWrite(&cm_scacheLock);
829 /* must be called after the scp->fid is set */
830 cm_FreelanceFetchMountPointString(scp);
831 cm_FreelanceFetchFileType(scp);
833 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
834 scp->length.HighPart = 0;
836 scp->unixModeBits=0777;
837 scp->clientModTime=FakeFreelanceModTime;
838 scp->serverModTime=FakeFreelanceModTime;
839 scp->parentUnique = 0x1;
840 scp->parentVnode=0x1;
842 scp->dataVersion=cm_data.fakeDirVersion;
843 scp->bufDataVersionLow=cm_data.fakeDirVersion;
844 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
846 lock_ReleaseWrite(&scp->rw);
848 #ifdef DEBUG_REFCOUNT
849 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
850 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
855 #endif /* AFS_FREELANCE_CLIENT */
857 /* we don't have the fid, recycle something */
858 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
859 if (newScp == NULL) {
860 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
861 return CM_ERROR_WOULDBLOCK;
863 #ifdef DEBUG_REFCOUNT
864 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
866 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
868 /* otherwise, we need to find the volume */
869 if (!cm_freelanceEnabled || !isRoot) {
870 cellp = cm_FindCellByID(fidp->cell, 0);
872 /* put back newScp so it can be reused */
873 lock_ObtainWrite(&cm_scacheLock);
874 newScp->flags |= CM_SCACHEFLAG_DELETED;
875 cm_AdjustScacheLRU(newScp);
876 lock_ReleaseWrite(&newScp->rw);
877 lock_ReleaseWrite(&cm_scacheLock);
878 return CM_ERROR_NOSUCHCELL;
881 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
883 /* put back newScp so it can be reused */
884 lock_ObtainWrite(&cm_scacheLock);
885 newScp->flags |= CM_SCACHEFLAG_DELETED;
886 cm_AdjustScacheLRU(newScp);
887 lock_ReleaseWrite(&newScp->rw);
888 lock_ReleaseWrite(&cm_scacheLock);
894 * otherwise, we have the volume, now reverify that the scp doesn't
895 * exist, and proceed. make sure that we hold the cm_scacheLock
896 * write-locked until the scp is put into the hash table in order
899 lock_ObtainWrite(&cm_scacheLock);
900 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
901 if (cm_FidCmp(fidp, &scp->fid) == 0) {
902 #ifdef DEBUG_REFCOUNT
903 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
904 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
908 cm_HoldSCacheNoLock(scp);
909 cm_AdjustScacheLRU(scp);
911 /* put back newScp so it can be reused */
912 newScp->flags |= CM_SCACHEFLAG_DELETED;
913 cm_AdjustScacheLRU(newScp);
914 lock_ReleaseWrite(&newScp->rw);
915 lock_ReleaseWrite(&cm_scacheLock);
924 if (!cm_freelanceEnabled || !isRoot) {
925 /* if this scache entry represents a volume root then we need
926 * to copy the dotdotFipd from the volume structure where the
927 * "master" copy is stored (defect 11489)
929 if (volp->vol[ROVOL].ID == fidp->volume) {
930 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
931 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
932 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
933 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
934 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
935 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
936 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
938 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
939 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
945 scp->nextp = cm_data.scacheHashTablep[hash];
946 cm_data.scacheHashTablep[hash] = scp;
947 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
948 refCount = InterlockedIncrement(&scp->refCount);
949 lock_ReleaseWrite(&cm_scacheLock);
950 lock_ReleaseWrite(&scp->rw);
951 #ifdef DEBUG_REFCOUNT
952 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
954 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
956 /* XXX - The following fields in the cm_scache are
963 /* now we have a held scache entry; just return it */
965 #ifdef DEBUG_REFCOUNT
966 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
967 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
972 /* Returns a held reference to the scache's parent
974 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
979 cm_scache_t * pscp = NULL;
981 lock_ObtainWrite(&cm_scacheLock);
982 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
984 if (cm_FidCmp(&scp->fid, &parent_fid)) {
985 i = CM_SCACHE_HASH(&parent_fid);
986 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
987 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
988 cm_HoldSCacheNoLock(pscp);
994 lock_ReleaseWrite(&cm_scacheLock);
999 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1001 cm_scache_waiter_t * w;
1003 lock_ObtainWrite(&cm_scacheLock);
1004 if (cm_allFreeWaiters == NULL) {
1005 w = malloc(sizeof(*w));
1006 memset(w, 0, sizeof(*w));
1008 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1009 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1012 w->threadId = thrd_Current();
1014 cm_HoldSCacheNoLock(scp);
1018 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1019 lock_ReleaseWrite(&cm_scacheLock);
1021 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1024 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1026 cm_scache_waiter_t * w;
1029 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1031 lock_ObtainRead(&cm_scacheLock);
1032 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1034 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1035 if (w->flags == flags && w->bufp == bufp) {
1040 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1041 this_is_me = (w->threadId == thrd_Current());
1042 lock_ReleaseRead(&cm_scacheLock);
1045 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1049 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1051 lock_ObtainWrite(&cm_scacheLock);
1052 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1053 cm_ReleaseSCacheNoLock(scp);
1054 memset(w, 0, sizeof(*w));
1055 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1056 lock_ReleaseWrite(&cm_scacheLock);
1062 /* synchronize a fetch, store, read, write, fetch status or store status.
1063 * Called with scache mutex held, and returns with it held, but temporarily
1064 * drops it during the fetch.
1066 * At most one flag can be on in flags, if this is an RPC request.
1068 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1070 * There are a lot of weird restrictions here; here's an attempt to explain the
1071 * rationale for the concurrency restrictions implemented in this function.
1073 * First, although the file server will break callbacks when *another* machine
1074 * modifies a file or status block, the client itself is responsible for
1075 * concurrency control on its own requests. Callback breaking events are rare,
1076 * and simply invalidate any concurrent new status info.
1078 * In the absence of callback breaking messages, we need to know how to
1079 * synchronize incoming responses describing updates to files. We synchronize
1080 * operations that update the data version by comparing the data versions.
1081 * However, updates that do not update the data, but only the status, can't be
1082 * synchronized with fetches or stores, since there's nothing to compare
1083 * to tell which operation executed first at the server.
1085 * Thus, we can allow multiple ops that change file data, or dir data, and
1086 * fetches. However, status storing ops have to be done serially.
1088 * Furthermore, certain data-changing ops are incompatible: we can't read or
1089 * write a buffer while doing a truncate. We can't read and write the same
1090 * buffer at the same time, or write while fetching or storing, or read while
1091 * fetching a buffer (this may change). We can't fetch and store at the same
1094 * With respect to status, we can't read and write at the same time, read while
1095 * fetching, write while fetching or storing, or fetch and store at the same time.
1097 * We can't allow a get callback RPC to run in concurrently with something that
1098 * will return updated status, since we could start a call, have the server
1099 * return status, have another machine make an update to the status (which
1100 * doesn't change serverModTime), have the original machine get a new callback,
1101 * and then have the original machine merge in the early, old info from the
1102 * first call. At this point, the easiest way to avoid this problem is to have
1103 * getcallback calls conflict with all others for the same vnode. Other calls
1104 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1105 * vnode must be careful not to merge in their status unless they have obtained
1106 * a callback from the start of their call.
1108 * Note added 1/23/96
1109 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1110 * Each such RPC passes a FileLength parameter, which the server uses to do
1111 * pre-truncation if necessary. So if two RPC's are processed out of order at
1112 * the server, the one with the smaller FileLength will be processed last,
1113 * possibly resulting in a bogus truncation. The simplest way to avoid this
1114 * is to serialize all StoreData RPC's. This is the reason we defined
1115 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1117 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1118 * readers which may be requesting overlapping ranges.
1120 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1121 afs_uint32 rights, afs_uint32 flags)
1123 osi_queueData_t *qdp;
1126 afs_uint32 outRights;
1128 afs_uint32 sleep_scp_flags = 0;
1129 afs_uint32 sleep_buf_cmflags = 0;
1130 afs_uint32 sleep_scp_bufs = 0;
1133 lock_AssertWrite(&scp->rw);
1135 /* lookup this first */
1136 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1139 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1142 /* Do the access check. Now we don't really do the access check
1143 * atomically, since the caller doesn't expect the parent dir to be
1144 * returned locked, and that is what we'd have to do to prevent a
1145 * callback breaking message on the parent due to a setacl call from
1146 * being processed while we're running. So, instead, we check things
1147 * here, and if things look fine with the access, we proceed to finish
1148 * the rest of this check. Sort of a hack, but probably good enough.
1152 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1153 /* if we're bringing in a new status block, ensure that
1154 * we aren't already doing so, and that no one is
1155 * changing the status concurrently, either. We need
1156 * to do this, even if the status is of a different
1157 * type, since we don't have the ability to figure out,
1158 * in the AFS 3 protocols, which status-changing
1159 * operation ran first, or even which order a read and
1160 * a write occurred in.
1162 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1163 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1164 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1168 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1169 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1170 /* if we're going to make an RPC to change the status, make sure
1171 * that no one is bringing in or sending out the status.
1173 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1174 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1175 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1178 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1179 (scp->bufReadsp || scp->bufWritesp)) {
1180 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1184 if (flags & CM_SCACHESYNC_FETCHDATA) {
1185 /* if we're bringing in a new chunk of data, make sure that
1186 * nothing is happening to that chunk, and that we aren't
1187 * changing the basic file status info, either.
1189 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1190 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1191 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1194 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1195 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1199 if (flags & CM_SCACHESYNC_STOREDATA) {
1200 /* same as fetch data */
1201 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1202 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1203 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1206 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1207 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1212 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1213 /* Don't allow concurrent StoreData RPC's */
1214 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1215 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1220 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1221 /* Don't allow more than one BKG store request */
1222 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1223 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1228 if (flags & CM_SCACHESYNC_LOCK) {
1229 /* Don't allow concurrent fiddling with lock lists */
1230 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1231 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1236 /* now the operations that don't correspond to making RPCs */
1237 if (flags & CM_SCACHESYNC_GETSTATUS) {
1238 /* we can use the status that's here, if we're not
1239 * bringing in new status.
1241 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1242 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1246 if (flags & CM_SCACHESYNC_SETSTATUS) {
1247 /* we can make a change to the local status, as long as
1248 * the status isn't changing now.
1250 * If we're fetching or storing a chunk of data, we can
1251 * change the status locally, since the fetch/store
1252 * operations don't change any of the data that we're
1255 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1256 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1257 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1261 if (flags & CM_SCACHESYNC_READ) {
1262 /* we're going to read the data, make sure that the
1263 * status is available, and that the data is here. It
1264 * is OK to read while storing the data back.
1266 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1267 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1270 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1271 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1274 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1275 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1279 if (flags & CM_SCACHESYNC_WRITE) {
1280 /* don't write unless the status is stable and the chunk
1283 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1284 CM_SCACHEFLAG_SIZESTORING)) {
1285 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1288 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1290 CM_BUF_CMWRITING))) {
1291 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1293 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1294 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1295 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1301 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1302 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1303 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1306 lock_ReleaseMutex(&bufp->mx);
1307 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1309 lock_ReleaseWrite(&scp->rw);
1310 lock_ObtainMutex(&bufp->mx);
1311 lock_ObtainWrite(&scp->rw);
1315 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1321 /* can't check access rights without a callback */
1322 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1324 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1325 return CM_ERROR_READONLY;
1327 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1328 if (~outRights & rights)
1329 return CM_ERROR_NOACCESS;
1332 /* we don't know the required access rights */
1333 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1334 code = cm_GetAccessRights(scp, userp, reqp);
1336 lock_ReleaseWrite(&scp->rw);
1337 lock_ObtainMutex(&bufp->mx);
1338 lock_ObtainWrite(&scp->rw);
1346 if (flags & CM_SCACHESYNC_BULKREAD) {
1347 /* Don't allow concurrent fiddling with lock lists */
1348 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1349 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1354 /* if we get here, we're happy */
1358 /* first check if we're not supposed to wait: fail
1359 * in this case, returning with everything still locked.
1361 if (flags & CM_SCACHESYNC_NOWAIT)
1362 return CM_ERROR_WOULDBLOCK;
1364 /* These are used for minidump debugging */
1365 sleep_scp_flags = scp->flags; /* so we know why we slept */
1366 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1367 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1369 /* wait here, then try again */
1370 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1371 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1373 scp->waitRequests++;
1374 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1375 scp, scp->waitCount, scp->waitRequests);
1377 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1378 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1379 scp->waitCount = scp->waitRequests = 1;
1382 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1386 lock_ReleaseMutex(&bufp->mx);
1387 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1389 lock_ObtainMutex(&bufp->mx);
1390 lock_ObtainWrite(&scp->rw);
1391 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1393 cm_UpdateServerPriority();
1396 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1397 scp, scp->waitCount, scp->waitRequests);
1398 if (scp->waitCount == 0) {
1399 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1400 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1401 scp->waitRequests = 0;
1403 } /* big while loop */
1405 /* now, update the recorded state for RPC-type calls */
1406 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1407 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1408 if (flags & CM_SCACHESYNC_STORESTATUS)
1409 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1410 if (flags & CM_SCACHESYNC_SETSIZE)
1411 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1412 if (flags & CM_SCACHESYNC_STORESIZE)
1413 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1414 if (flags & CM_SCACHESYNC_GETCALLBACK)
1415 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1416 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1417 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1418 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1419 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1420 if (flags & CM_SCACHESYNC_LOCK)
1421 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1422 if (flags & CM_SCACHESYNC_BULKREAD)
1423 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1425 /* now update the buffer pointer */
1426 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1427 /* ensure that the buffer isn't already in the I/O list */
1428 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1429 tbufp = osi_GetQData(qdp);
1430 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1433 /* queue a held reference to the buffer in the "reading" I/O list */
1434 qdp = osi_QDAlloc();
1435 osi_SetQData(qdp, bufp);
1438 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1439 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1442 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1443 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1444 "attempting to store extents on a non-file object");
1446 /* ensure that the buffer isn't already in the I/O list */
1447 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1448 tbufp = osi_GetQData(qdp);
1449 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1452 /* queue a held reference to the buffer in the "writing" I/O list */
1453 qdp = osi_QDAlloc();
1454 osi_SetQData(qdp, bufp);
1456 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1457 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1460 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1461 /* mark the buffer as being written to. */
1462 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1468 /* for those syncops that setup for RPCs.
1469 * Called with scache locked.
1471 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1473 osi_queueData_t *qdp;
1476 lock_AssertWrite(&scp->rw);
1478 /* now, update the recorded state for RPC-type calls */
1479 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1480 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1481 if (flags & CM_SCACHESYNC_STORESTATUS)
1482 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1483 if (flags & CM_SCACHESYNC_SETSIZE)
1484 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1485 if (flags & CM_SCACHESYNC_STORESIZE)
1486 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1487 if (flags & CM_SCACHESYNC_GETCALLBACK)
1488 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1489 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1490 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1491 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1492 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1493 if (flags & CM_SCACHESYNC_LOCK)
1494 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1495 if (flags & CM_SCACHESYNC_BULKREAD)
1496 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1498 /* now update the buffer pointer */
1499 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1502 /* ensure that the buffer is in the I/O list */
1503 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1504 tbufp = osi_GetQData(qdp);
1509 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1513 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1514 if (bufp->flags & CM_BUF_WAITING) {
1515 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1516 osi_Wakeup((LONG_PTR) &bufp);
1522 /* now update the buffer pointer */
1523 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1525 /* ensure that the buffer is in the I/O list */
1526 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1527 tbufp = osi_GetQData(qdp);
1532 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1536 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1537 if (bufp->flags & CM_BUF_WAITING) {
1538 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1539 osi_Wakeup((LONG_PTR) &bufp);
1545 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1546 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1547 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1550 /* and wakeup anyone who is waiting */
1551 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1552 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1553 osi_Wakeup((LONG_PTR) &scp->flags);
1557 /* merge in a response from an RPC. The scp must be locked, and the callback
1560 * Don't overwrite any status info that is dirty, since we could have a store
1561 * operation (such as store data) that merges some info in, and we don't want
1562 * to lose the local updates. Typically, there aren't many updates we do
1563 * locally, anyway, probably only mtime.
1565 * There is probably a bug in here where a chmod (which doesn't change
1566 * serverModTime) that occurs between two fetches, both of whose responses are
1567 * handled after the callback breaking is done, but only one of whose calls
1568 * started before that, can cause old info to be merged from the first call.
1570 void cm_MergeStatus(cm_scache_t *dscp,
1571 cm_scache_t *scp, AFSFetchStatus *statusp,
1572 AFSVolSync *volsyncp,
1573 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1575 afs_uint64 dataVersion;
1576 struct cm_volume *volp = NULL;
1577 struct cm_cell *cellp = NULL;
1578 int rdr_invalidate = 0;
1579 afs_uint32 activeRPCs;
1581 lock_AssertWrite(&scp->rw);
1583 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1585 // yj: i want to create some fake status for the /afs directory and the
1586 // entries under that directory
1587 #ifdef AFS_FREELANCE_CLIENT
1588 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1589 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1590 if (scp == cm_data.rootSCachep) {
1591 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1592 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1593 statusp->Length = cm_fakeDirSize;
1594 statusp->Length_hi = 0;
1596 statusp->FileType = scp->fileType;
1597 statusp->Length = scp->length.LowPart;
1598 statusp->Length_hi = scp->length.HighPart;
1600 statusp->InterfaceVersion = 0x1;
1601 statusp->LinkCount = scp->linkCount;
1602 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1603 statusp->Author = 0x1;
1604 statusp->Owner = 0x0;
1605 statusp->CallerAccess = 0x9;
1606 statusp->AnonymousAccess = 0x9;
1607 statusp->UnixModeBits = 0777;
1608 statusp->ParentVnode = 0x1;
1609 statusp->ParentUnique = 0x1;
1610 statusp->ResidencyMask = 0;
1611 statusp->ClientModTime = FakeFreelanceModTime;
1612 statusp->ServerModTime = FakeFreelanceModTime;
1614 statusp->SyncCounter = 0;
1615 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1616 statusp->lockCount = 0;
1617 statusp->errorCode = 0;
1619 #endif /* AFS_FREELANCE_CLIENT */
1621 if (statusp->errorCode != 0) {
1622 switch (statusp->errorCode) {
1627 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1629 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1631 if (scp->fid.vnode & 0x1)
1632 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1634 scp->fileType = 0; /* unknown */
1636 scp->serverModTime = 0;
1637 scp->clientModTime = 0;
1638 scp->length.LowPart = 0;
1639 scp->length.HighPart = 0;
1640 scp->serverLength.LowPart = 0;
1641 scp->serverLength.HighPart = 0;
1645 scp->unixModeBits = 0;
1647 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1648 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1649 scp->fsLockCount = 0;
1652 scp->parentVnode = dscp->fid.vnode;
1653 scp->parentUnique = dscp->fid.unique;
1655 scp->parentVnode = 0;
1656 scp->parentUnique = 0;
1659 if (RDR_Initialized)
1662 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1665 dataVersion = statusp->dataVersionHigh;
1667 dataVersion |= statusp->DataVersion;
1669 if (!(flags & CM_MERGEFLAG_FORCE) &&
1670 dataVersion < scp->dataVersion &&
1671 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1673 cellp = cm_FindCellByID(scp->fid.cell, 0);
1674 if (scp->cbServerp) {
1675 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1676 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1677 osi_Log2(afsd_logp, "old data from server %x volume %s",
1678 scp->cbServerp->addr.sin_addr.s_addr,
1679 volp ? volp->namep : "(unknown)");
1681 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1682 scp, scp->dataVersion, dataVersion);
1683 /* we have a number of data fetch/store operations running
1684 * concurrently, and we can tell which one executed last at the
1685 * server by its mtime.
1686 * Choose the one with the largest mtime, and ignore the rest.
1688 * These concurrent calls are incompatible with setting the
1689 * mtime, so we won't have a locally changed mtime here.
1691 * We could also have ACL info for a different user than usual,
1692 * in which case we have to do that part of the merge, anyway.
1693 * We won't have to worry about the info being old, since we
1694 * won't have concurrent calls
1695 * that change file status running from this machine.
1697 * Added 3/17/98: if we see data version regression on an RO
1698 * file, it's probably due to a server holding an out-of-date
1699 * replica, rather than to concurrent RPC's. Failures to
1700 * release replicas are now flagged by the volserver, but only
1701 * since AFS 3.4 5.22, so there are plenty of clients getting
1702 * out-of-date replicas out there.
1704 * If we discover an out-of-date replica, by this time it's too
1705 * late to go to another server and retry. Also, we can't
1706 * reject the merge, because then there is no way for
1707 * GetAccess to do its work, and the caller gets into an
1708 * infinite loop. So we just grin and bear it.
1710 if (!(scp->flags & CM_SCACHEFLAG_RO))
1714 if (cm_readonlyVolumeVersioning)
1715 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1717 scp->serverModTime = statusp->ServerModTime;
1719 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1720 scp->clientModTime = statusp->ClientModTime;
1722 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1723 scp->length.LowPart = statusp->Length;
1724 scp->length.HighPart = statusp->Length_hi;
1727 scp->serverLength.LowPart = statusp->Length;
1728 scp->serverLength.HighPart = statusp->Length_hi;
1730 scp->linkCount = statusp->LinkCount;
1731 scp->owner = statusp->Owner;
1732 scp->group = statusp->Group;
1733 scp->unixModeBits = statusp->UnixModeBits & 07777;
1735 if (statusp->FileType == File)
1736 scp->fileType = CM_SCACHETYPE_FILE;
1737 else if (statusp->FileType == Directory)
1738 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1739 else if (statusp->FileType == SymbolicLink) {
1740 if ((scp->unixModeBits & 0111) == 0)
1741 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1743 scp->fileType = CM_SCACHETYPE_SYMLINK;
1746 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1747 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1749 /* and other stuff */
1750 scp->parentVnode = statusp->ParentVnode;
1751 scp->parentUnique = statusp->ParentUnique;
1753 /* -1 is a write lock; any positive values are read locks */
1754 scp->fsLockCount = (afs_int32)statusp->lockCount;
1756 /* and merge in the private acl cache info, if this is more than the public
1757 * info; merge in the public stuff in any case.
1759 scp->anyAccess = statusp->AnonymousAccess;
1761 if (userp != NULL) {
1762 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1765 if (dataVersion != 0 &&
1766 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1767 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > activeRPCs)) {
1769 * We now know that all of the data buffers that we have associated
1770 * with this scp are invalid. Subsequent operations will go faster
1771 * if the buffers are removed from the hash tables.
1773 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1774 * those version numbers will be updated as part of the directory operation.
1776 * We do not remove storedata buffers because they will still be valid.
1781 cm_buf_t *bp, *prevBp, *nextBp;
1783 lock_ObtainWrite(&buf_globalLock);
1784 i = BUF_FILEHASH(&scp->fid);
1785 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1787 nextBp = bp->fileHashp;
1789 * if the buffer belongs to this stat cache entry
1790 * and the buffer mutex can be obtained, check the
1791 * reference count and if it is zero, remove the buffer
1792 * from the hash tables. If there are references,
1793 * the buffer might be updated to the current version
1794 * so leave it in place.
1796 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1797 lock_TryMutex(&bp->mx)) {
1798 if (bp->refCount == 0 &&
1799 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1800 !(bp->qFlags & CM_BUF_QREDIR)) {
1801 prevBp = bp->fileHashBackp;
1802 bp->fileHashBackp = bp->fileHashp = NULL;
1804 prevBp->fileHashp = nextBp;
1806 cm_data.buf_fileHashTablepp[i] = nextBp;
1808 nextBp->fileHashBackp = prevBp;
1810 j = BUF_HASH(&bp->fid, &bp->offset);
1811 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1812 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1817 /* we better find it */
1818 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1820 *lbpp = bp->hashp; /* hash out */
1823 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1825 lock_ReleaseMutex(&bp->mx);
1828 lock_ReleaseWrite(&buf_globalLock);
1832 * If the dataVersion has changed, the mountPointStringp must be cleared
1833 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1834 * does not update a mountpoint or symlink by altering the contents of
1835 * the file data; but the Unix CM does.
1837 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1838 scp->mountPointStringp[0] = '\0';
1840 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1841 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1843 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1844 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1845 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1846 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1847 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1850 /* We maintain a range of buffer dataVersion values which are considered
1851 * valid. This avoids the need to update the dataVersion on each buffer
1852 * object during an uncontested storeData operation. As a result this
1853 * merge status no longer has performance characteristics derived from
1854 * the size of the file.
1856 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > activeRPCs) ||
1857 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1858 scp->bufDataVersionLow == 0)
1859 scp->bufDataVersionLow = dataVersion;
1861 if (RDR_Initialized) {
1863 * The redirector maintains its own cached status information which
1864 * must be updated when a DV change occurs that is not the result
1865 * of a redirector initiated data change.
1867 * If the current old DV is BAD, send a DV change notification.
1869 * If the DV has changed and request was not initiated by the
1870 * redirector, send a DV change notification.
1872 * If the request was initiated by the redirector, send a notification
1873 * for store and directory operations that result in a DV change greater
1874 * than the number of active RPCs or any other operation that results
1875 * in an unexpected DV change such as FetchStatus.
1878 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1880 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
1882 } else if (reqp->flags & CM_REQ_SOURCE_REDIR) {
1883 if (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1884 (dataVersion - scp->dataVersion > activeRPCs - 1)) {
1886 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1887 dataVersion - scp->dataVersion > activeRPCs) {
1892 scp->dataVersion = dataVersion;
1895 * If someone is waiting for status information, we can wake them up
1896 * now even though the entity that issued the FetchStatus may not
1897 * have completed yet.
1899 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1902 * We just successfully merged status on the stat cache object.
1903 * This means that the associated volume must be online.
1907 cellp = cm_FindCellByID(scp->fid.cell, 0);
1908 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1911 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1912 if (statep->state != vl_online) {
1913 lock_ObtainWrite(&volp->rw);
1914 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1915 statep->state = vl_online;
1916 lock_ReleaseWrite(&volp->rw);
1925 * The scache rw lock cannot be held across the invalidation.
1926 * Doing so can result in deadlocks with other threads processing
1927 * requests initiated by the afs redirector.
1929 if (rdr_invalidate) {
1930 lock_ReleaseWrite(&scp->rw);
1931 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1932 scp->fid.unique, scp->fid.hash,
1933 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
1934 lock_ObtainWrite(&scp->rw);
1938 /* note that our stat cache info is incorrect, so force us eventually
1939 * to stat the file again. There may be dirty data associated with
1940 * this vnode, and we want to preserve that information.
1942 * This function works by simply simulating a loss of the callback.
1944 * This function must be called with the scache locked.
1946 void cm_DiscardSCache(cm_scache_t *scp)
1948 lock_AssertWrite(&scp->rw);
1949 if (scp->cbServerp) {
1950 cm_PutServer(scp->cbServerp);
1951 scp->cbServerp = NULL;
1954 scp->volumeCreationDate = 0;
1955 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
1956 cm_dnlcPurgedp(scp);
1957 cm_dnlcPurgevp(scp);
1958 cm_FreeAllACLEnts(scp);
1960 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1961 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1963 /* Force mount points and symlinks to be re-evaluated */
1964 scp->mountPointStringp[0] = '\0';
1967 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1969 afsFidp->Volume = fidp->volume;
1970 afsFidp->Vnode = fidp->vnode;
1971 afsFidp->Unique = fidp->unique;
1974 #ifdef DEBUG_REFCOUNT
1975 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1977 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1982 osi_assertx(scp != NULL, "null cm_scache_t");
1983 lock_AssertAny(&cm_scacheLock);
1984 refCount = InterlockedIncrement(&scp->refCount);
1985 #ifdef DEBUG_REFCOUNT
1986 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1987 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1991 #ifdef DEBUG_REFCOUNT
1992 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1994 void cm_HoldSCache(cm_scache_t *scp)
1999 osi_assertx(scp != NULL, "null cm_scache_t");
2000 lock_ObtainRead(&cm_scacheLock);
2001 refCount = InterlockedIncrement(&scp->refCount);
2002 #ifdef DEBUG_REFCOUNT
2003 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
2004 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
2006 lock_ReleaseRead(&cm_scacheLock);
2009 #ifdef DEBUG_REFCOUNT
2010 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2012 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2017 osi_assertx(scp != NULL, "null cm_scache_t");
2018 lock_AssertAny(&cm_scacheLock);
2020 refCount = InterlockedDecrement(&scp->refCount);
2021 #ifdef DEBUG_REFCOUNT
2023 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
2025 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2026 #ifdef DEBUG_REFCOUNT
2027 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
2028 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
2031 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2035 lockstate = lock_GetRWLockState(&cm_scacheLock);
2036 if (lockstate != OSI_RWLOCK_WRITEHELD)
2037 lock_ReleaseRead(&cm_scacheLock);
2039 lock_ReleaseWrite(&cm_scacheLock);
2041 lock_ObtainWrite(&scp->rw);
2042 if (scp->flags & CM_SCACHEFLAG_DELETED)
2045 if (refCount == 0 && deleted) {
2046 lock_ObtainWrite(&cm_scacheLock);
2047 cm_RecycleSCache(scp, 0);
2048 if (lockstate != OSI_RWLOCK_WRITEHELD)
2049 lock_ConvertWToR(&cm_scacheLock);
2051 if (lockstate != OSI_RWLOCK_WRITEHELD)
2052 lock_ObtainRead(&cm_scacheLock);
2054 lock_ObtainWrite(&cm_scacheLock);
2056 lock_ReleaseWrite(&scp->rw);
2060 #ifdef DEBUG_REFCOUNT
2061 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2063 void cm_ReleaseSCache(cm_scache_t *scp)
2068 osi_assertx(scp != NULL, "null cm_scache_t");
2069 lock_ObtainRead(&cm_scacheLock);
2070 refCount = InterlockedDecrement(&scp->refCount);
2071 #ifdef DEBUG_REFCOUNT
2073 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2075 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2076 #ifdef DEBUG_REFCOUNT
2077 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2078 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2080 lock_ReleaseRead(&cm_scacheLock);
2082 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2084 lock_ObtainWrite(&scp->rw);
2085 if (scp->flags & CM_SCACHEFLAG_DELETED)
2088 lock_ObtainWrite(&cm_scacheLock);
2089 cm_RecycleSCache(scp, 0);
2090 lock_ReleaseWrite(&cm_scacheLock);
2092 lock_ReleaseWrite(&scp->rw);
2096 /* just look for the scp entry to get filetype */
2097 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2098 int cm_FindFileType(cm_fid_t *fidp)
2103 hash = CM_SCACHE_HASH(fidp);
2105 osi_assertx(fidp->cell != 0, "unassigned cell value");
2107 lock_ObtainWrite(&cm_scacheLock);
2108 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2109 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2110 lock_ReleaseWrite(&cm_scacheLock);
2111 return scp->fileType;
2114 lock_ReleaseWrite(&cm_scacheLock);
2118 /* dump all scp's that have reference count > 0 to a file.
2119 * cookie is used to identify this batch for easy parsing,
2120 * and it a string provided by a caller
2122 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2131 lock_ObtainRead(&cm_scacheLock);
2133 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2134 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2136 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2139 char *srvStr = NULL;
2140 afs_uint32 srvStrRpc = TRUE;
2144 if (scp->cbServerp) {
2145 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2146 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2147 srvStr = malloc(16); /* enough for 255.255.255.255 */
2149 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2153 if (scp->cbExpires) {
2158 cbt[strlen(cbt)-1] = '\0';
2161 if (scp->volumeCreationDate) {
2162 t = scp->volumeCreationDate;
2165 cdrot = strdup(cdrot);
2166 cdrot[strlen(cdrot)-1] = '\0';
2170 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2171 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2172 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2173 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2174 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2175 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2176 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2177 cdrot ? cdrot : "<none>", scp->refCount);
2178 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2180 if (scp->fileLocksH) {
2181 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2182 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2184 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2185 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2186 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2187 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2188 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2189 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2190 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2193 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2194 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2199 RpcStringFree(&srvStr);
2209 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2210 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2211 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2212 cookie, cm_data.scacheHashTableSize);
2213 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2215 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2217 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2219 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2220 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2221 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2225 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2226 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2228 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2229 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2231 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2232 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2233 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",
2234 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2235 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2236 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2239 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2240 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2243 lock_ReleaseRead(&cm_scacheLock);