2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
26 #include <afs/unified_afs.h>
28 /*extern void afsi_log(char *pattern, ...);*/
30 extern osi_hyper_t hzero;
33 osi_queue_t *cm_allFileLocks;
34 osi_queue_t *cm_freeFileLocks;
35 unsigned long cm_lockRefreshCycle;
37 /* lock for globals */
38 osi_rwlock_t cm_scacheLock;
40 /* Dummy scache entry for use with pioctl fids */
41 cm_scache_t cm_fakeSCache;
43 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
45 #ifdef AFS_FREELANCE_CLIENT
46 extern osi_mutex_t cm_Freelance_Lock;
50 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
54 lock_ObtainWrite(&cm_data.rootSCachep->rw);
55 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
56 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
58 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
59 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
61 return cm_data.rootSCachep;
65 /* must be called with cm_scacheLock write-locked! */
66 void cm_AdjustScacheLRU(cm_scache_t *scp)
68 lock_AssertWrite(&cm_scacheLock);
69 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
70 if (scp->flags & CM_SCACHEFLAG_DELETED) {
71 /* Since it has been deleted make it the first to be recycled. */
72 osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
74 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
78 /* call with cm_scacheLock write-locked and scp rw held */
79 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
85 lock_AssertWrite(&cm_scacheLock);
86 lock_AssertWrite(&scp->rw);
87 if (scp->flags & CM_SCACHEFLAG_INHASH) {
88 /* hash it out first */
89 i = CM_SCACHE_HASH(&scp->fid);
90 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
92 lscpp = &tscp->nextp, tscp = tscp->nextp) {
96 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
103 /* called with cm_scacheLock and scp write-locked */
104 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
107 /* destroy directory Bplus Tree */
109 LARGE_INTEGER start, end;
111 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
113 * We are not holding the dirlock and obtaining it
114 * requires that we drop the scp->rw. As a result
115 * we will leave the dirBplus tree intact but
116 * invalidate the version number so that whatever
117 * operation is currently active can safely complete
118 * but the contents will be ignored on the next
119 * directory operation.
121 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
125 QueryPerformanceCounter(&start);
127 freeBtree(scp->dirBplus);
128 scp->dirBplus = NULL;
129 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
130 QueryPerformanceCounter(&end);
133 lock_ReleaseWrite(&scp->dirlock);
135 bplus_free_time += (end.QuadPart - start.QuadPart);
140 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
141 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
147 lock_AssertWrite(&cm_scacheLock);
148 lock_AssertWrite(&scp->rw);
150 if (scp->refCount != 0) {
154 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
155 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
157 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
162 if (scp->redirBufCount != 0) {
167 fileType = scp->fileType;
168 callback = scp->cbExpires ? 1 : 0;
170 cm_RemoveSCacheFromHashTable(scp);
172 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
173 !cm_accessPerFileCheck) {
174 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
177 if (!(volp->flags & CM_VOLUMEFLAG_DFS_VOLUME))
178 cm_EAccesClearParentEntries(&fid);
184 /* invalidate so next merge works fine;
185 * also initialize some flags */
187 _InterlockedAnd(&scp->flags,
188 ~(CM_SCACHEFLAG_STATD
189 | CM_SCACHEFLAG_DELETED
191 | CM_SCACHEFLAG_PURERO
192 | CM_SCACHEFLAG_OVERQUOTA
193 | CM_SCACHEFLAG_OUTOFSPACE));
194 scp->serverModTime = 0;
195 scp->dataVersion = CM_SCACHE_VERSION_BAD;
196 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
197 scp->bulkStatProgress = hzero;
199 scp->waitQueueT = NULL;
201 if (scp->cbServerp) {
202 cm_PutServer(scp->cbServerp);
203 scp->cbServerp = NULL;
207 scp->volumeCreationDate = 0;
215 /* remove from dnlc */
219 /* discard cached status; if non-zero, Close
220 * tried to store this to server but failed */
223 /* discard symlink info */
224 scp->mpDataVersion = CM_SCACHE_VERSION_BAD;
225 scp->mountPointStringp[0] = '\0';
226 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
227 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
229 /* reset locking info */
230 scp->fileLocksH = NULL;
231 scp->fileLocksT = NULL;
232 scp->serverLock = (-1);
233 scp->exclusiveLocks = 0;
234 scp->sharedLocks = 0;
235 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
236 scp->fsLockCount = 0;
238 /* not locked, but there can be no references to this guy
239 * while we hold the global refcount lock.
241 cm_FreeAllACLEnts(scp);
243 cm_ResetSCacheDirectory(scp, 0);
245 if (RDR_Initialized && callback) {
247 * We drop the cm_scacheLock because it may be required to
248 * satisfy an ioctl request from the redirector. It should
249 * be safe to hold the scp->rw lock here because at this
250 * point (a) the object has just been recycled so the fid
251 * is nul and there are no requests that could possibly
252 * be issued by the redirector that would depend upon it.
254 lock_ReleaseWrite(&cm_scacheLock);
255 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
256 fid.unique, fid.hash,
257 fileType, AFS_INVALIDATE_EXPIRED);
258 lock_ObtainWrite(&cm_scacheLock);
266 * called with cm_scacheLock write-locked; find a vnode to recycle.
267 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
268 * returns scp->rw write-locked.
271 cm_GetNewSCache(afs_uint32 locked)
273 cm_scache_t *scp = NULL;
274 cm_scache_t *scp_prev = NULL;
275 cm_scache_t *scp_next = NULL;
279 lock_AssertWrite(&cm_scacheLock);
281 lock_ObtainWrite(&cm_scacheLock);
283 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
284 /* There were no deleted scache objects that we could use. Try to find
285 * one that simply hasn't been used in a while.
287 for (attempt = 0 ; attempt < 128; attempt++) {
288 afs_uint32 count = 0;
290 for ( scp = cm_data.scacheLRULastp;
292 scp = (cm_scache_t *) osi_QPrev(&scp->q))
295 * We save the prev and next pointers in the
296 * LRU because we are going to drop the cm_scacheLock and
297 * the order of the list could change out from beneath us.
298 * If both changed, it means that this entry has been moved
299 * within the LRU and it should no longer be recycled.
301 scp_prev = (cm_scache_t *) osi_QPrev(&scp->q);
302 scp_next = (cm_scache_t *) osi_QNext(&scp->q);
305 /* It is possible for the refCount to be zero and for there still
306 * to be outstanding dirty buffers. If there are dirty buffers,
307 * we must not recycle the scp.
309 * If the object is in use by the redirector, then avoid recycling
310 * it unless we have to.
312 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
313 afs_uint32 buf_dirty = 0;
314 afs_uint32 buf_rdr = 0;
316 lock_ReleaseWrite(&cm_scacheLock);
317 buf_dirty = buf_DirtyBuffersExist(&scp->fid);
319 buf_rdr = buf_RDRBuffersExist(&scp->fid);
321 if (!buf_dirty && !buf_rdr) {
326 success = lock_TryWrite(&scp->rw);
328 lock_ObtainWrite(&cm_scacheLock);
329 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
330 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
332 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
334 lock_ReleaseWrite(&scp->rw);
336 } else if (!success) {
337 osi_Log1(afsd_logp, "GetNewSCache failed to obtain lock scp 0x%p", scp);
341 /* Found a likely candidate. Save type and fid in case we succeed */
343 fileType = scp->fileType;
345 if (!cm_RecycleSCache(scp, 0)) {
346 /* we found an entry, so return it.
347 * remove from the LRU queue and put it back at the
348 * head of the LRU queue.
350 cm_AdjustScacheLRU(scp);
352 /* and we're done - SUCCESS */
353 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
356 lock_ReleaseWrite(&scp->rw);
359 osi_Log1(afsd_logp,"GetNewSCache redirector is holding extents scp 0x%p", scp);
361 osi_Log1(afsd_logp, "GetNewSCache dirty buffers scp 0x%p", scp);
363 lock_ObtainWrite(&cm_scacheLock);
364 if (scp_prev != (cm_scache_t *) osi_QPrev(&scp->q) &&
365 scp_next != (cm_scache_t *) osi_QNext(&scp->q))
367 osi_Log1(afsd_logp, "GetNewSCache scp 0x%p; LRU order changed", scp);
374 osi_Log2(afsd_logp, "GetNewSCache all scache entries in use (attempt = %d, count = %u)", attempt, count);
377 * The entire LRU queue was walked and no available cm_scache_t was
378 * found. Drop the cm_scacheLock and sleep for a moment to give a
379 * chance for cm_scache_t objects to be released.
381 lock_ReleaseWrite(&cm_scacheLock);
383 lock_ObtainWrite(&cm_scacheLock);
391 /* if we get here, we should allocate a new scache entry. We either are below
392 * quota or we have a leak and need to allocate a new one to avoid panicing.
394 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
395 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
396 "invalid cm_scache_t address");
397 memset(scp, 0, sizeof(cm_scache_t));
398 scp->magic = CM_SCACHE_MAGIC;
399 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
400 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
401 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
403 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
405 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
406 scp->serverLock = -1;
407 scp->dataVersion = CM_SCACHE_VERSION_BAD;
408 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
409 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
411 /* and put it in the LRU queue */
412 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **)&cm_data.scacheLRULastp, &scp->q);
413 cm_data.currentSCaches++;
414 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
416 scp->allNextp = cm_data.allSCachesp;
417 cm_data.allSCachesp = scp;
421 lock_ReleaseWrite(&cm_scacheLock);
426 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
429 fidp->volume = volume;
431 fidp->unique = unique;
432 CM_FID_GEN_HASH(fidp);
435 /* like strcmp, only for fids */
436 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
438 if (ap->hash != bp->hash)
440 if (ap->vnode != bp->vnode)
442 if (ap->volume != bp->volume)
444 if (ap->unique != bp->unique)
446 if (ap->cell != bp->cell)
451 void cm_fakeSCacheInit(int newFile)
454 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
455 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
456 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
457 cm_data.fakeSCache.cbExpires = (time_t)-1;
458 cm_data.fakeSCache.cbExpires = time(NULL);
459 /* can leave clientModTime at 0 */
460 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
461 cm_data.fakeSCache.unixModeBits = 0777;
462 cm_data.fakeSCache.length.LowPart = 1000;
463 cm_data.fakeSCache.linkCount = 1;
464 cm_data.fakeSCache.refCount = 1;
465 cm_data.fakeSCache.serverLock = -1;
466 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
468 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
469 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
470 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
471 lock_InitializeMutex(&cm_data.fakeSCache.redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
475 cm_ValidateSCache(void)
477 cm_scache_t * scp, *lscp;
480 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
481 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
482 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
483 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
487 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
489 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
490 if (scp->magic != CM_SCACHE_MAGIC) {
491 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
492 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
495 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
496 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
497 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
500 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
501 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
502 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
505 if (i > cm_data.currentSCaches ) {
506 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
507 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
510 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
511 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
512 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
517 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
518 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
519 if (scp->magic != CM_SCACHE_MAGIC) {
520 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
521 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
524 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
525 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
526 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
529 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
530 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
531 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
534 if (i > cm_data.currentSCaches ) {
535 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
536 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
539 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
540 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
541 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
546 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
547 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
549 hash = CM_SCACHE_HASH(&scp->fid);
550 if (scp->magic != CM_SCACHE_MAGIC) {
551 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
552 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
555 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
556 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
557 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
560 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
561 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
562 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
566 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
567 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
573 return cm_dnlcValidate();
577 cm_SuspendSCache(void)
582 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
585 * After this call all servers are marked down.
586 * Do not clear the callbacks, instead change the
587 * expiration time so that the callbacks will be expired
588 * when the servers are marked back up. However, we
589 * want the callbacks to be preserved as long as the
590 * servers are down. That way if the machine resumes
591 * without network, the stat cache item will still be
596 lock_ObtainWrite(&cm_scacheLock);
597 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
598 if (scp->cbServerp) {
599 if (scp->flags & CM_SCACHEFLAG_PURERO) {
600 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
602 if (volp->cbExpiresRO == scp->cbExpires)
603 volp->cbExpiresRO = now+1;
607 scp->cbExpires = now+1;
610 lock_ReleaseWrite(&cm_scacheLock);
614 cm_ShutdownSCache(void)
616 cm_scache_t * scp, * nextp;
618 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
620 lock_ObtainWrite(&cm_scacheLock);
622 for ( scp = cm_data.allSCachesp; scp;
624 nextp = scp->allNextp;
625 lock_ReleaseWrite(&cm_scacheLock);
627 lock_ObtainWrite(&scp->dirlock);
629 lock_ObtainWrite(&scp->rw);
630 lock_ObtainWrite(&cm_scacheLock);
632 if (scp->randomACLp) {
633 cm_FreeAllACLEnts(scp);
636 if (scp->cbServerp) {
637 cm_PutServer(scp->cbServerp);
638 scp->cbServerp = NULL;
642 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
643 lock_ReleaseWrite(&scp->rw);
647 freeBtree(scp->dirBplus);
648 scp->dirBplus = NULL;
649 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
650 lock_ReleaseWrite(&scp->dirlock);
651 lock_FinalizeRWLock(&scp->dirlock);
653 lock_FinalizeRWLock(&scp->rw);
654 lock_FinalizeRWLock(&scp->bufCreateLock);
655 lock_FinalizeMutex(&scp->redirMx);
657 lock_ReleaseWrite(&cm_scacheLock);
659 return cm_dnlcShutdown();
662 void cm_InitSCache(int newFile, long maxSCaches)
664 static osi_once_t once;
666 if (osi_Once(&once)) {
667 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
669 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
670 cm_data.allSCachesp = NULL;
671 cm_data.currentSCaches = 0;
672 cm_data.maxSCaches = maxSCaches;
673 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
677 for ( scp = cm_data.allSCachesp; scp;
678 scp = scp->allNextp ) {
679 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
680 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
682 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
684 scp->cbServerp = NULL;
687 scp->volumeCreationDate = 0;
688 scp->fileLocksH = NULL;
689 scp->fileLocksT = NULL;
690 scp->serverLock = (-1);
691 scp->lastRefreshCycle = 0;
692 scp->exclusiveLocks = 0;
693 scp->sharedLocks = 0;
701 scp->dirBplus = NULL;
702 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
704 scp->waitQueueT = NULL;
705 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
707 scp->redirBufCount = 0;
708 scp->redirQueueT = NULL;
709 scp->redirQueueH = NULL;
710 lock_InitializeMutex(&scp->redirMx, "cm_scache_t redirMx", LOCK_HIERARCHY_SCACHE_REDIRMX);
713 cm_allFileLocks = NULL;
714 cm_freeFileLocks = NULL;
715 cm_lockRefreshCycle = 0;
716 cm_fakeSCacheInit(newFile);
717 cm_allFreeWaiters = NULL;
718 cm_dnlcInit(newFile);
723 /* version that doesn't bother creating the entry if we don't find it */
724 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
729 hash = CM_SCACHE_HASH(fidp);
731 if (fidp->cell == 0) {
735 lock_ObtainRead(&cm_scacheLock);
736 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
737 if (cm_FidCmp(fidp, &scp->fid) == 0) {
738 cm_HoldSCacheNoLock(scp);
739 lock_ConvertRToW(&cm_scacheLock);
740 cm_AdjustScacheLRU(scp);
741 lock_ReleaseWrite(&cm_scacheLock);
745 lock_ReleaseRead(&cm_scacheLock);
749 #ifdef DEBUG_REFCOUNT
750 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
751 cm_req_t *reqp, char * file, long line)
753 long cm_GetSCache(cm_fid_t *fidp, cm_fid_t *parentFidp, cm_scache_t **outScpp, cm_user_t *userp,
758 cm_scache_t *scp = NULL;
759 cm_scache_t *newScp = NULL;
761 cm_volume_t *volp = NULL;
763 int special = 0; // yj: boolean variable to test if file is on root.afs
765 extern cm_fid_t cm_rootFid;
768 hash = CM_SCACHE_HASH(fidp);
771 return CM_ERROR_INVAL;
773 #ifdef AFS_FREELANCE_CLIENT
774 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
775 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
776 !(fidp->vnode==0x1 && fidp->unique==0x1));
777 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
778 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
779 fidp->vnode==0x1 && fidp->unique==0x1);
782 // yj: check if we have the scp, if so, we don't need
783 // to do anything else
784 lock_ObtainRead(&cm_scacheLock);
785 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
786 if (cm_FidCmp(fidp, &scp->fid) == 0) {
787 #ifdef DEBUG_REFCOUNT
788 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
789 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
791 #ifdef AFS_FREELANCE_CLIENT
792 if (cm_freelanceEnabled && special &&
793 cm_data.fakeDirVersion != scp->dataVersion)
796 if (parentFidp && scp->parentVnode == 0) {
797 scp->parentVnode = parentFidp->vnode;
798 scp->parentUnique = parentFidp->unique;
800 cm_HoldSCacheNoLock(scp);
802 lock_ConvertRToW(&cm_scacheLock);
803 cm_AdjustScacheLRU(scp);
804 lock_ReleaseWrite(&cm_scacheLock);
808 lock_ReleaseRead(&cm_scacheLock);
810 // yj: when we get here, it means we don't have an scp
811 // so we need to either load it or fake it, depending
812 // on whether the file is "special", see below.
814 // yj: if we're trying to get an scp for a file that's
815 // on root.afs of homecell, we want to handle it specially
816 // because we have to fill in the status stuff 'coz we
817 // don't want trybulkstat to fill it in for us
818 #ifdef AFS_FREELANCE_CLIENT
819 if (cm_freelanceEnabled && isRoot) {
820 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
821 /* freelance: if we are trying to get the root scp for the first
822 * time, we will just put in a place holder entry.
827 if (cm_freelanceEnabled && special) {
828 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
830 if (cm_getLocalMountPointChange()) {
831 cm_clearLocalMountPointChange();
832 cm_reInitLocalMountPoints();
836 scp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
838 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
839 return CM_ERROR_WOULDBLOCK;
842 lock_ObtainWrite(&scp->rw);
845 cm_SetFid(&scp->dotdotFid,AFS_FAKE_ROOT_CELL_ID,AFS_FAKE_ROOT_VOL_ID,1,1);
847 scp->parentVnode = parentFidp->vnode;
848 scp->parentUnique = parentFidp->unique;
850 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
851 lock_ObtainWrite(&cm_scacheLock);
852 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
853 scp->nextp = cm_data.scacheHashTablep[hash];
854 cm_data.scacheHashTablep[hash] = scp;
855 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
857 refCount = InterlockedIncrement(&scp->refCount);
858 osi_Log2(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
859 lock_ReleaseWrite(&cm_scacheLock);
861 /* must be called after the scp->fid is set */
862 cm_FreelanceFetchMountPointString(scp);
863 cm_FreelanceFetchFileType(scp);
865 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
866 scp->length.HighPart = 0;
868 scp->unixModeBits=0777;
869 scp->clientModTime=FakeFreelanceModTime;
870 scp->serverModTime=FakeFreelanceModTime;
871 scp->parentUnique = 0x1;
872 scp->parentVnode=0x1;
874 scp->dataVersion=cm_data.fakeDirVersion;
875 scp->bufDataVersionLow=cm_data.fakeDirVersion;
876 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
878 lock_ReleaseWrite(&scp->rw);
880 #ifdef DEBUG_REFCOUNT
881 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
882 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
887 #endif /* AFS_FREELANCE_CLIENT */
889 /* we don't have the fid, recycle something */
890 newScp = cm_GetNewSCache(FALSE); /* returns scp->rw held */
891 if (newScp == NULL) {
892 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
893 return CM_ERROR_WOULDBLOCK;
895 #ifdef DEBUG_REFCOUNT
896 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, newScp, newScp->flags);
898 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", newScp, newScp->flags);
900 /* otherwise, we need to find the volume */
901 if (!cm_freelanceEnabled || !isRoot) {
902 cellp = cm_FindCellByID(fidp->cell, 0);
904 /* put back newScp so it can be reused */
905 lock_ObtainWrite(&cm_scacheLock);
906 newScp->flags |= CM_SCACHEFLAG_DELETED;
907 cm_AdjustScacheLRU(newScp);
908 lock_ReleaseWrite(&newScp->rw);
909 lock_ReleaseWrite(&cm_scacheLock);
910 return CM_ERROR_NOSUCHCELL;
913 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
915 /* put back newScp so it can be reused */
916 lock_ObtainWrite(&cm_scacheLock);
917 newScp->flags |= CM_SCACHEFLAG_DELETED;
918 cm_AdjustScacheLRU(newScp);
919 lock_ReleaseWrite(&newScp->rw);
920 lock_ReleaseWrite(&cm_scacheLock);
926 * otherwise, we have the volume, now reverify that the scp doesn't
927 * exist, and proceed. make sure that we hold the cm_scacheLock
928 * write-locked until the scp is put into the hash table in order
931 lock_ObtainWrite(&cm_scacheLock);
932 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
933 if (cm_FidCmp(fidp, &scp->fid) == 0) {
934 #ifdef DEBUG_REFCOUNT
935 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
936 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
938 if (parentFidp && scp->parentVnode == 0) {
939 scp->parentVnode = parentFidp->vnode;
940 scp->parentUnique = parentFidp->unique;
944 cm_HoldSCacheNoLock(scp);
945 cm_AdjustScacheLRU(scp);
947 /* put back newScp so it can be reused */
948 newScp->flags |= CM_SCACHEFLAG_DELETED;
949 cm_AdjustScacheLRU(newScp);
950 lock_ReleaseWrite(&newScp->rw);
951 lock_ReleaseWrite(&cm_scacheLock);
960 if (!cm_freelanceEnabled || !isRoot) {
961 /* if this scache entry represents a volume root then we need
962 * to copy the dotdotFid from the volume structure where the
963 * "master" copy is stored (defect 11489)
965 if (volp->vol[ROVOL].ID == fidp->volume) {
966 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
967 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
968 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
969 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
970 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
971 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
972 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
974 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
975 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
979 scp->parentVnode = parentFidp->vnode;
980 scp->parentUnique = parentFidp->unique;
985 scp->nextp = cm_data.scacheHashTablep[hash];
986 cm_data.scacheHashTablep[hash] = scp;
987 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
988 refCount = InterlockedIncrement(&scp->refCount);
989 lock_ReleaseWrite(&cm_scacheLock);
990 lock_ReleaseWrite(&scp->rw);
991 #ifdef DEBUG_REFCOUNT
992 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", file, line, scp, refCount);
994 osi_Log2(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p refCount %d", scp, refCount);
996 /* XXX - The following fields in the cm_scache are
1003 /* now we have a held scache entry; just return it */
1005 #ifdef DEBUG_REFCOUNT
1006 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
1007 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
1012 /* Returns a held reference to the scache's parent
1014 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
1018 cm_fid_t parent_fid;
1019 cm_scache_t * pscp = NULL;
1021 if (scp->parentVnode == 0)
1024 lock_ObtainWrite(&cm_scacheLock);
1025 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
1027 if (cm_FidCmp(&scp->fid, &parent_fid)) {
1028 i = CM_SCACHE_HASH(&parent_fid);
1029 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
1030 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
1031 cm_HoldSCacheNoLock(pscp);
1037 lock_ReleaseWrite(&cm_scacheLock);
1042 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1044 cm_scache_waiter_t * w;
1046 lock_ObtainWrite(&cm_scacheLock);
1047 if (cm_allFreeWaiters == NULL) {
1048 w = malloc(sizeof(*w));
1049 memset(w, 0, sizeof(*w));
1051 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
1052 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
1055 w->threadId = thrd_Current();
1057 cm_HoldSCacheNoLock(scp);
1061 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1062 lock_ReleaseWrite(&cm_scacheLock);
1064 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
1067 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
1069 cm_scache_waiter_t * w;
1072 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1074 lock_ObtainRead(&cm_scacheLock);
1075 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1077 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1078 if (w->flags == flags && w->bufp == bufp) {
1083 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1084 this_is_me = (w->threadId == thrd_Current());
1085 lock_ReleaseRead(&cm_scacheLock);
1088 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1092 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1094 lock_ObtainWrite(&cm_scacheLock);
1095 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1096 cm_ReleaseSCacheNoLock(scp);
1097 memset(w, 0, sizeof(*w));
1098 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1099 lock_ReleaseWrite(&cm_scacheLock);
1105 /* synchronize a fetch, store, read, write, fetch status or store status.
1106 * Called with scache mutex held, and returns with it held, but temporarily
1107 * drops it during the fetch.
1109 * At most one flag can be on in flags, if this is an RPC request.
1111 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1113 * There are a lot of weird restrictions here; here's an attempt to explain the
1114 * rationale for the concurrency restrictions implemented in this function.
1116 * First, although the file server will break callbacks when *another* machine
1117 * modifies a file or status block, the client itself is responsible for
1118 * concurrency control on its own requests. Callback breaking events are rare,
1119 * and simply invalidate any concurrent new status info.
1121 * In the absence of callback breaking messages, we need to know how to
1122 * synchronize incoming responses describing updates to files. We synchronize
1123 * operations that update the data version by comparing the data versions.
1124 * However, updates that do not update the data, but only the status, can't be
1125 * synchronized with fetches or stores, since there's nothing to compare
1126 * to tell which operation executed first at the server.
1128 * Thus, we can allow multiple ops that change file data, or dir data, and
1129 * fetches. However, status storing ops have to be done serially.
1131 * Furthermore, certain data-changing ops are incompatible: we can't read or
1132 * write a buffer while doing a truncate. We can't read and write the same
1133 * buffer at the same time, or write while fetching or storing, or read while
1134 * fetching a buffer (this may change). We can't fetch and store at the same
1137 * With respect to status, we can't read and write at the same time, read while
1138 * fetching, write while fetching or storing, or fetch and store at the same time.
1140 * We can't allow a get callback RPC to run in concurrently with something that
1141 * will return updated status, since we could start a call, have the server
1142 * return status, have another machine make an update to the status (which
1143 * doesn't change serverModTime), have the original machine get a new callback,
1144 * and then have the original machine merge in the early, old info from the
1145 * first call. At this point, the easiest way to avoid this problem is to have
1146 * getcallback calls conflict with all others for the same vnode. Other calls
1147 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1148 * vnode must be careful not to merge in their status unless they have obtained
1149 * a callback from the start of their call.
1151 * Note added 1/23/96
1152 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1153 * Each such RPC passes a FileLength parameter, which the server uses to do
1154 * pre-truncation if necessary. So if two RPC's are processed out of order at
1155 * the server, the one with the smaller FileLength will be processed last,
1156 * possibly resulting in a bogus truncation. The simplest way to avoid this
1157 * is to serialize all StoreData RPC's. This is the reason we defined
1158 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1160 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1161 * readers which may be requesting overlapping ranges.
1163 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1164 afs_uint32 rights, afs_uint32 flags)
1166 osi_queueData_t *qdp;
1169 afs_uint32 outRights;
1171 afs_uint32 sleep_scp_flags = 0;
1172 afs_uint32 sleep_buf_cmflags = 0;
1173 afs_uint32 sleep_scp_bufs = 0;
1176 lock_AssertWrite(&scp->rw);
1178 /* lookup this first */
1179 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1182 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1185 /* Do the access check. Now we don't really do the access check
1186 * atomically, since the caller doesn't expect the parent dir to be
1187 * returned locked, and that is what we'd have to do to prevent a
1188 * callback breaking message on the parent due to a setacl call from
1189 * being processed while we're running. So, instead, we check things
1190 * here, and if things look fine with the access, we proceed to finish
1191 * the rest of this check. Sort of a hack, but probably good enough.
1195 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1196 /* if we're bringing in a new status block, ensure that
1197 * we aren't already doing so, and that no one is
1198 * changing the status concurrently, either. We need
1199 * to do this, even if the status is of a different
1200 * type, since we don't have the ability to figure out,
1201 * in the AFS 3 protocols, which status-changing
1202 * operation ran first, or even which order a read and
1203 * a write occurred in.
1205 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1206 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1207 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1211 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1212 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1213 /* if we're going to make an RPC to change the status, make sure
1214 * that no one is bringing in or sending out the status.
1216 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1217 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1218 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1221 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1222 (scp->bufReadsp || scp->bufWritesp)) {
1223 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1227 if (flags & CM_SCACHESYNC_FETCHDATA) {
1228 /* if we're bringing in a new chunk of data, make sure that
1229 * nothing is happening to that chunk, and that we aren't
1230 * changing the basic file status info, either.
1232 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1233 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1234 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1237 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1238 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1242 if (flags & CM_SCACHESYNC_STOREDATA) {
1243 /* same as fetch data */
1244 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1245 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1246 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1249 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1250 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1255 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1256 /* Don't allow concurrent StoreData RPC's */
1257 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1258 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1263 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1264 /* Don't allow more than one BKG store request */
1265 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1266 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1271 if (flags & CM_SCACHESYNC_LOCK) {
1272 /* Don't allow concurrent fiddling with lock lists */
1273 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1274 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1279 /* now the operations that don't correspond to making RPCs */
1280 if (flags & CM_SCACHESYNC_GETSTATUS) {
1281 /* we can use the status that's here, if we're not
1282 * bringing in new status.
1284 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1285 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1289 if (flags & CM_SCACHESYNC_SETSTATUS) {
1290 /* we can make a change to the local status, as long as
1291 * the status isn't changing now.
1293 * If we're fetching or storing a chunk of data, we can
1294 * change the status locally, since the fetch/store
1295 * operations don't change any of the data that we're
1298 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1299 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1300 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1304 if (flags & CM_SCACHESYNC_READ) {
1305 /* we're going to read the data, make sure that the
1306 * status is available, and that the data is here. It
1307 * is OK to read while storing the data back.
1309 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1310 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1313 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1314 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1317 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1318 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1322 if (flags & CM_SCACHESYNC_WRITE) {
1323 /* don't write unless the status is stable and the chunk
1326 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1327 CM_SCACHEFLAG_SIZESTORING)) {
1328 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1331 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1333 CM_BUF_CMWRITING))) {
1334 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1336 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1337 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1338 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1344 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1345 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1346 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1349 if (cm_EAccesFindEntry(userp, &scp->fid))
1350 return CM_ERROR_NOACCESS;
1353 lock_ReleaseMutex(&bufp->mx);
1354 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1356 lock_ReleaseWrite(&scp->rw);
1357 lock_ObtainMutex(&bufp->mx);
1358 lock_ObtainWrite(&scp->rw);
1362 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1368 /* can't check access rights without a callback */
1369 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1371 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1372 return CM_ERROR_READONLY;
1374 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1375 if (~outRights & rights)
1376 return CM_ERROR_NOACCESS;
1379 /* we don't know the required access rights */
1380 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1381 code = cm_GetAccessRights(scp, userp, reqp);
1383 lock_ReleaseWrite(&scp->rw);
1384 lock_ObtainMutex(&bufp->mx);
1385 lock_ObtainWrite(&scp->rw);
1393 if (flags & CM_SCACHESYNC_BULKREAD) {
1394 /* Don't allow concurrent fiddling with lock lists */
1395 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1396 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1401 /* if we get here, we're happy */
1405 /* first check if we're not supposed to wait: fail
1406 * in this case, returning with everything still locked.
1408 if (flags & CM_SCACHESYNC_NOWAIT)
1409 return CM_ERROR_WOULDBLOCK;
1411 /* These are used for minidump debugging */
1412 sleep_scp_flags = scp->flags; /* so we know why we slept */
1413 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1414 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1416 /* wait here, then try again */
1417 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1418 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1420 scp->waitRequests++;
1421 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1422 scp, scp->waitCount, scp->waitRequests);
1424 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1425 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1426 scp->waitCount = scp->waitRequests = 1;
1429 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1433 lock_ReleaseMutex(&bufp->mx);
1434 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1436 lock_ObtainMutex(&bufp->mx);
1437 lock_ObtainWrite(&scp->rw);
1438 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1440 cm_UpdateServerPriority();
1443 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1444 scp, scp->waitCount, scp->waitRequests);
1445 if (scp->waitCount == 0) {
1446 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1447 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1448 scp->waitRequests = 0;
1450 } /* big while loop */
1452 /* now, update the recorded state for RPC-type calls */
1453 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1454 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1455 if (flags & CM_SCACHESYNC_STORESTATUS)
1456 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1457 if (flags & CM_SCACHESYNC_SETSIZE)
1458 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1459 if (flags & CM_SCACHESYNC_STORESIZE)
1460 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1461 if (flags & CM_SCACHESYNC_GETCALLBACK)
1462 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1463 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1464 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1465 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1466 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1467 if (flags & CM_SCACHESYNC_LOCK)
1468 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1469 if (flags & CM_SCACHESYNC_BULKREAD)
1470 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1472 /* now update the buffer pointer */
1473 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1474 /* ensure that the buffer isn't already in the I/O list */
1475 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1476 tbufp = osi_GetQData(qdp);
1477 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1480 /* queue a held reference to the buffer in the "reading" I/O list */
1481 qdp = osi_QDAlloc();
1482 osi_SetQData(qdp, bufp);
1485 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1486 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1489 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1490 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1491 "attempting to store extents on a non-file object");
1493 /* ensure that the buffer isn't already in the I/O list */
1494 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1495 tbufp = osi_GetQData(qdp);
1496 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1499 /* queue a held reference to the buffer in the "writing" I/O list */
1500 qdp = osi_QDAlloc();
1501 osi_SetQData(qdp, bufp);
1503 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1504 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1507 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1508 /* mark the buffer as being written to. */
1509 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1515 /* for those syncops that setup for RPCs.
1516 * Called with scache locked.
1518 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1520 osi_queueData_t *qdp;
1523 lock_AssertWrite(&scp->rw);
1525 /* now, update the recorded state for RPC-type calls */
1526 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1527 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1528 if (flags & CM_SCACHESYNC_STORESTATUS)
1529 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1530 if (flags & CM_SCACHESYNC_SETSIZE)
1531 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1532 if (flags & CM_SCACHESYNC_STORESIZE)
1533 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1534 if (flags & CM_SCACHESYNC_GETCALLBACK)
1535 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1536 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1537 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1538 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1539 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1540 if (flags & CM_SCACHESYNC_LOCK)
1541 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1542 if (flags & CM_SCACHESYNC_BULKREAD)
1543 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1545 /* now update the buffer pointer */
1546 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1549 /* ensure that the buffer is in the I/O list */
1550 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1551 tbufp = osi_GetQData(qdp);
1556 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1560 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1561 if (bufp->flags & CM_BUF_WAITING) {
1562 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1563 osi_Wakeup((LONG_PTR) &bufp);
1569 /* now update the buffer pointer */
1570 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1572 /* ensure that the buffer is in the I/O list */
1573 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1574 tbufp = osi_GetQData(qdp);
1579 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1583 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1584 if (bufp->flags & CM_BUF_WAITING) {
1585 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1586 osi_Wakeup((LONG_PTR) &bufp);
1592 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1593 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1594 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1597 /* and wakeup anyone who is waiting */
1598 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1599 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1600 osi_Wakeup((LONG_PTR) &scp->flags);
1605 dv_diff(afs_uint64 dv1, afs_uint64 dv2)
1607 if ( dv1 - dv2 > 0x7FFFFFFF )
1608 return (afs_uint32)(dv2 - dv1);
1610 return (afs_uint32)(dv1 - dv2);
1613 /* merge in a response from an RPC. The scp must be locked, and the callback
1616 * Don't overwrite any status info that is dirty, since we could have a store
1617 * operation (such as store data) that merges some info in, and we don't want
1618 * to lose the local updates. Typically, there aren't many updates we do
1619 * locally, anyway, probably only mtime.
1621 * There is probably a bug in here where a chmod (which doesn't change
1622 * serverModTime) that occurs between two fetches, both of whose responses are
1623 * handled after the callback breaking is done, but only one of whose calls
1624 * started before that, can cause old info to be merged from the first call.
1626 void cm_MergeStatus(cm_scache_t *dscp,
1627 cm_scache_t *scp, AFSFetchStatus *statusp,
1628 AFSVolSync *volsyncp,
1629 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1631 afs_uint64 dataVersion;
1632 struct cm_volume *volp = NULL;
1633 struct cm_cell *cellp = NULL;
1634 int rdr_invalidate = 0;
1635 afs_uint32 activeRPCs;
1637 lock_AssertWrite(&scp->rw);
1639 activeRPCs = 1 + InterlockedDecrement(&scp->activeRPCs);
1641 // yj: i want to create some fake status for the /afs directory and the
1642 // entries under that directory
1643 #ifdef AFS_FREELANCE_CLIENT
1644 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1645 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1646 if (scp == cm_data.rootSCachep) {
1647 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1648 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1649 statusp->Length = cm_fakeDirSize;
1650 statusp->Length_hi = 0;
1652 statusp->FileType = scp->fileType;
1653 statusp->Length = scp->length.LowPart;
1654 statusp->Length_hi = scp->length.HighPart;
1656 statusp->InterfaceVersion = 0x1;
1657 statusp->LinkCount = scp->linkCount;
1658 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1659 statusp->Author = 0x1;
1660 statusp->Owner = 0x0;
1661 statusp->CallerAccess = 0x9;
1662 statusp->AnonymousAccess = 0x9;
1663 statusp->UnixModeBits = 0777;
1664 statusp->ParentVnode = 0x1;
1665 statusp->ParentUnique = 0x1;
1666 statusp->ResidencyMask = 0;
1667 statusp->ClientModTime = FakeFreelanceModTime;
1668 statusp->ServerModTime = FakeFreelanceModTime;
1670 statusp->SyncCounter = 0;
1671 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1672 statusp->lockCount = 0;
1673 statusp->errorCode = 0;
1675 #endif /* AFS_FREELANCE_CLIENT */
1677 if (statusp->InterfaceVersion != 0x1) {
1678 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p Invalid InterfaceVersion %u",
1679 scp, statusp->InterfaceVersion);
1683 if (statusp->errorCode != 0) {
1684 switch (statusp->errorCode) {
1689 cm_EAccesAddEntry(userp, &scp->fid, &dscp->fid);
1691 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1693 if (scp->fid.vnode & 0x1)
1694 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1696 scp->fileType = 0; /* unknown */
1698 scp->serverModTime = 0;
1699 scp->clientModTime = 0;
1700 scp->length.LowPart = 0;
1701 scp->length.HighPart = 0;
1702 scp->serverLength.LowPart = 0;
1703 scp->serverLength.HighPart = 0;
1707 scp->unixModeBits = 0;
1709 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1710 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1711 scp->fsLockCount = 0;
1713 if (dscp && dscp != scp) {
1714 scp->parentVnode = dscp->fid.vnode;
1715 scp->parentUnique = dscp->fid.unique;
1717 scp->parentVnode = 0;
1718 scp->parentUnique = 0;
1721 if (RDR_Initialized)
1725 dataVersion = statusp->dataVersionHigh;
1727 dataVersion |= statusp->DataVersion;
1729 if (!(flags & CM_MERGEFLAG_FORCE) &&
1730 dataVersion < scp->dataVersion &&
1731 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1733 cellp = cm_FindCellByID(scp->fid.cell, 0);
1734 if (scp->cbServerp) {
1735 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1736 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1737 osi_Log2(afsd_logp, "old data from server %x volume %s",
1738 scp->cbServerp->addr.sin_addr.s_addr,
1739 volp ? volp->namep : "(unknown)");
1742 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1743 scp, scp->dataVersion, dataVersion);
1744 /* we have a number of data fetch/store operations running
1745 * concurrently, and we can tell which one executed last at the
1746 * server by its mtime.
1747 * Choose the one with the largest mtime, and ignore the rest.
1749 * These concurrent calls are incompatible with setting the
1750 * mtime, so we won't have a locally changed mtime here.
1752 * We could also have ACL info for a different user than usual,
1753 * in which case we have to do that part of the merge, anyway.
1754 * We won't have to worry about the info being old, since we
1755 * won't have concurrent calls
1756 * that change file status running from this machine.
1758 * Added 3/17/98: if we see data version regression on an RO
1759 * file, it's probably due to a server holding an out-of-date
1760 * replica, rather than to concurrent RPC's. Failures to
1761 * release replicas are now flagged by the volserver, but only
1762 * since AFS 3.4 5.22, so there are plenty of clients getting
1763 * out-of-date replicas out there.
1765 * If we discover an out-of-date replica, by this time it's too
1766 * late to go to another server and retry. Also, we can't
1767 * reject the merge, because then there is no way for
1768 * GetAccess to do its work, and the caller gets into an
1769 * infinite loop. So we just grin and bear it.
1771 if (!(scp->flags & CM_SCACHEFLAG_RO))
1776 * The first field of the volsync parameter is supposed to be the
1777 * volume creation date. Unfortunately, pre-OpenAFS 1.4.11 and 1.6.0
1778 * file servers do not populate the VolSync structure for BulkStat and
1779 * InlineBulkStat RPCs. As a result, the volume creation date is not
1780 * trustworthy when status is obtained via [Inline]BulkStatus RPCs.
1781 * If cm_readonlyVolumeVersioning is set, it is assumed that all file
1782 * servers populate the VolSync structure at all times.
1784 if (cm_readonlyVolumeVersioning || !(flags & CM_MERGEFLAG_BULKSTAT))
1785 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1787 scp->volumeCreationDate = 0;
1789 scp->serverModTime = statusp->ServerModTime;
1791 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1792 scp->clientModTime = statusp->ClientModTime;
1794 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1795 scp->length.LowPart = statusp->Length;
1796 scp->length.HighPart = statusp->Length_hi;
1799 scp->serverLength.LowPart = statusp->Length;
1800 scp->serverLength.HighPart = statusp->Length_hi;
1802 scp->linkCount = statusp->LinkCount;
1803 scp->owner = statusp->Owner;
1804 scp->group = statusp->Group;
1805 scp->unixModeBits = statusp->UnixModeBits & 07777;
1807 if (statusp->FileType == File)
1808 scp->fileType = CM_SCACHETYPE_FILE;
1809 else if (statusp->FileType == Directory)
1810 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1811 else if (statusp->FileType == SymbolicLink) {
1812 if ((scp->unixModeBits & 0111) == 0)
1813 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1815 scp->fileType = CM_SCACHETYPE_SYMLINK;
1818 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1819 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1821 /* and other stuff */
1822 scp->parentVnode = statusp->ParentVnode;
1823 scp->parentUnique = statusp->ParentUnique;
1825 /* -1 is a write lock; any positive values are read locks */
1826 scp->fsLockCount = (afs_int32)statusp->lockCount;
1828 /* and merge in the private acl cache info, if this is more than the public
1829 * info; merge in the public stuff in any case.
1831 scp->anyAccess = statusp->AnonymousAccess;
1833 if (userp != NULL) {
1834 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1837 if (dataVersion != 0 && scp->dataVersion != CM_SCACHE_VERSION_BAD &&
1838 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && (dataVersion != scp->dataVersion) ||
1839 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1840 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs))) {
1842 * We now know that all of the data buffers that we have associated
1843 * with this scp are invalid. Subsequent operations will go faster
1844 * if the buffers are removed from the hash tables.
1846 * We do not remove directory buffers if the dataVersion delta is 'activeRPCs' because
1847 * those version numbers will be updated as part of the directory operation.
1849 * We do not remove storedata buffers because they will still be valid.
1854 cm_buf_t *bp, *prevBp, *nextBp;
1856 lock_ObtainWrite(&buf_globalLock);
1857 i = BUF_FILEHASH(&scp->fid);
1858 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1860 nextBp = bp->fileHashp;
1862 * if the buffer belongs to this stat cache entry
1863 * and the buffer mutex can be obtained, check the
1864 * reference count and if it is zero, remove the buffer
1865 * from the hash tables. If there are references,
1866 * the buffer might be updated to the current version
1867 * so leave it in place.
1869 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1870 lock_TryMutex(&bp->mx)) {
1871 if (bp->refCount == 0 &&
1872 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1873 !(bp->qFlags & CM_BUF_QREDIR)) {
1874 prevBp = bp->fileHashBackp;
1875 bp->fileHashBackp = bp->fileHashp = NULL;
1877 prevBp->fileHashp = nextBp;
1879 cm_data.buf_fileHashTablepp[i] = nextBp;
1881 nextBp->fileHashBackp = prevBp;
1883 j = BUF_HASH(&bp->fid, &bp->offset);
1884 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1885 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1890 /* we better find it */
1891 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1893 *lbpp = bp->hashp; /* hash out */
1896 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1898 lock_ReleaseMutex(&bp->mx);
1901 lock_ReleaseWrite(&buf_globalLock);
1904 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1905 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1906 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1908 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1909 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1910 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1911 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1912 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1915 /* We maintain a range of buffer dataVersion values which are considered
1916 * valid. This avoids the need to update the dataVersion on each buffer
1917 * object during an uncontested storeData operation. As a result this
1918 * merge status no longer has performance characteristics derived from
1919 * the size of the file.
1921 * For directory buffers, only current dataVersion values are up to date.
1923 if (((flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (dv_diff(dataVersion, scp->dataVersion) > activeRPCs)) ||
1924 (!(flags & (CM_MERGEFLAG_STOREDATA|CM_MERGEFLAG_DIROP)) && (scp->dataVersion != dataVersion)) ||
1925 scp->bufDataVersionLow == CM_SCACHE_VERSION_BAD ||
1926 scp->fileType == CM_SCACHETYPE_DIRECTORY)
1927 scp->bufDataVersionLow = dataVersion;
1929 if (RDR_Initialized) {
1931 * The redirector maintains its own cached status information which
1932 * must be updated when a DV change occurs that is not the result
1933 * of a redirector initiated data change.
1935 * If the current old DV is BAD, send a DV change notification.
1937 * If the DV has changed and request was not initiated by the
1938 * redirector, send a DV change notification.
1940 * If the request was initiated by the redirector, send a notification
1941 * for store and directory operations that result in a DV change greater
1942 * than the number of active RPCs or any other operation that results
1943 * in an unexpected DV change such as FetchStatus.
1946 if (scp->dataVersion == CM_SCACHE_VERSION_BAD && dataVersion != 0) {
1948 } else if (!(reqp->flags & CM_REQ_SOURCE_REDIR) && scp->dataVersion != dataVersion) {
1950 } else if (reqp->flags & CM_REQ_SOURCE_REDIR) {
1951 if (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1952 (dv_diff(dataVersion, scp->dataVersion) > activeRPCs - 1)) {
1954 } else if ((flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1955 dv_diff(dataVersion, scp->dataVersion) > activeRPCs) {
1960 scp->dataVersion = dataVersion;
1963 * If someone is waiting for status information, we can wake them up
1964 * now even though the entity that issued the FetchStatus may not
1965 * have completed yet.
1967 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1970 * We just successfully merged status on the stat cache object.
1971 * This means that the associated volume must be online.
1975 cellp = cm_FindCellByID(scp->fid.cell, 0);
1976 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1979 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1980 if (statep->state != vl_online) {
1981 lock_ObtainWrite(&volp->rw);
1982 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1983 statep->state = vl_online;
1984 lock_ReleaseWrite(&volp->rw);
1988 /* Remove cached EACCES / EPERM errors if the file is a directory */
1989 if (scp->fileType == CM_SCACHETYPE_DIRECTORY &&
1990 !(volp && (volp->flags & CM_VOLUMEFLAG_DFS_VOLUME)) &&
1991 !cm_accessPerFileCheck)
1993 cm_EAccesClearParentEntries(&scp->fid);
2001 * The scache rw lock cannot be held across the invalidation.
2002 * Doing so can result in deadlocks with other threads processing
2003 * requests initiated by the afs redirector.
2005 if (rdr_invalidate) {
2006 lock_ReleaseWrite(&scp->rw);
2007 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
2008 scp->fid.unique, scp->fid.hash,
2009 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
2010 lock_ObtainWrite(&scp->rw);
2014 /* note that our stat cache info is incorrect, so force us eventually
2015 * to stat the file again. There may be dirty data associated with
2016 * this vnode, and we want to preserve that information.
2018 * This function works by simply simulating a loss of the callback.
2020 * This function must be called with the scache locked.
2022 void cm_DiscardSCache(cm_scache_t *scp)
2024 lock_AssertWrite(&scp->rw);
2025 if (scp->cbServerp) {
2026 cm_PutServer(scp->cbServerp);
2027 scp->cbServerp = NULL;
2031 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
2032 cm_dnlcPurgedp(scp);
2033 cm_dnlcPurgevp(scp);
2034 cm_FreeAllACLEnts(scp);
2036 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
2037 cm_VolStatus_Invalidate_DFS_Mapping(scp);
2040 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
2042 afsFidp->Volume = fidp->volume;
2043 afsFidp->Vnode = fidp->vnode;
2044 afsFidp->Unique = fidp->unique;
2047 #ifdef DEBUG_REFCOUNT
2048 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2050 void cm_HoldSCacheNoLock(cm_scache_t *scp)
2055 osi_assertx(scp != NULL, "null cm_scache_t");
2056 lock_AssertAny(&cm_scacheLock);
2057 refCount = InterlockedIncrement(&scp->refCount);
2058 #ifdef DEBUG_REFCOUNT
2059 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
2060 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
2064 #ifdef DEBUG_REFCOUNT
2065 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
2067 void cm_HoldSCache(cm_scache_t *scp)
2072 osi_assertx(scp != NULL, "null cm_scache_t");
2073 lock_ObtainRead(&cm_scacheLock);
2074 refCount = InterlockedIncrement(&scp->refCount);
2075 #ifdef DEBUG_REFCOUNT
2076 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
2077 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
2079 lock_ReleaseRead(&cm_scacheLock);
2082 #ifdef DEBUG_REFCOUNT
2083 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
2085 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
2090 osi_assertx(scp != NULL, "null cm_scache_t");
2091 lock_AssertAny(&cm_scacheLock);
2093 refCount = InterlockedDecrement(&scp->refCount);
2094 #ifdef DEBUG_REFCOUNT
2096 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
2098 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2099 #ifdef DEBUG_REFCOUNT
2100 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
2101 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
2104 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
2108 lockstate = lock_GetRWLockState(&cm_scacheLock);
2109 if (lockstate != OSI_RWLOCK_WRITEHELD)
2110 lock_ReleaseRead(&cm_scacheLock);
2112 lock_ReleaseWrite(&cm_scacheLock);
2114 lock_ObtainWrite(&scp->rw);
2115 if (scp->flags & CM_SCACHEFLAG_DELETED)
2118 if (refCount == 0 && deleted) {
2119 lock_ObtainWrite(&cm_scacheLock);
2120 cm_RecycleSCache(scp, 0);
2121 if (lockstate != OSI_RWLOCK_WRITEHELD)
2122 lock_ConvertWToR(&cm_scacheLock);
2124 if (lockstate != OSI_RWLOCK_WRITEHELD)
2125 lock_ObtainRead(&cm_scacheLock);
2127 lock_ObtainWrite(&cm_scacheLock);
2129 lock_ReleaseWrite(&scp->rw);
2133 #ifdef DEBUG_REFCOUNT
2134 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
2136 void cm_ReleaseSCache(cm_scache_t *scp)
2141 osi_assertx(scp != NULL, "null cm_scache_t");
2142 lock_ObtainRead(&cm_scacheLock);
2143 refCount = InterlockedDecrement(&scp->refCount);
2144 #ifdef DEBUG_REFCOUNT
2146 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
2148 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
2149 #ifdef DEBUG_REFCOUNT
2150 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
2151 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
2153 lock_ReleaseRead(&cm_scacheLock);
2155 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2157 lock_ObtainWrite(&scp->rw);
2158 if (scp->flags & CM_SCACHEFLAG_DELETED)
2161 lock_ObtainWrite(&cm_scacheLock);
2162 cm_RecycleSCache(scp, 0);
2163 lock_ReleaseWrite(&cm_scacheLock);
2165 lock_ReleaseWrite(&scp->rw);
2169 /* just look for the scp entry to get filetype */
2170 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
2171 int cm_FindFileType(cm_fid_t *fidp)
2176 hash = CM_SCACHE_HASH(fidp);
2178 osi_assertx(fidp->cell != 0, "unassigned cell value");
2180 lock_ObtainWrite(&cm_scacheLock);
2181 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
2182 if (cm_FidCmp(fidp, &scp->fid) == 0) {
2183 lock_ReleaseWrite(&cm_scacheLock);
2184 return scp->fileType;
2187 lock_ReleaseWrite(&cm_scacheLock);
2191 /* dump all scp's that have reference count > 0 to a file.
2192 * cookie is used to identify this batch for easy parsing,
2193 * and it a string provided by a caller
2195 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2204 lock_ObtainRead(&cm_scacheLock);
2206 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2207 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2209 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2212 char *srvStr = NULL;
2213 afs_uint32 srvStrRpc = TRUE;
2217 if (scp->cbServerp) {
2218 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2219 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2220 srvStr = malloc(16); /* enough for 255.255.255.255 */
2222 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2226 if (scp->cbExpires) {
2231 cbt[strlen(cbt)-1] = '\0';
2234 if (scp->volumeCreationDate) {
2235 t = scp->volumeCreationDate;
2238 cdrot = strdup(cdrot);
2239 cdrot[strlen(cdrot)-1] = '\0';
2243 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2244 "mpDV=%I64d mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2245 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2246 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2247 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mpDataVersion, scp->mountPointStringp,
2248 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2249 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2250 cdrot ? cdrot : "<none>", scp->refCount);
2251 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2253 if (scp->fileLocksH) {
2254 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2255 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2257 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2258 cm_file_lock_t * lockp = fileq_to_cm_file_lock_t(q);
2259 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2260 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2261 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2262 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2263 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2266 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2267 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2272 RpcStringFree(&srvStr);
2282 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2283 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2284 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2285 cookie, cm_data.scacheHashTableSize);
2286 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2288 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2290 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2292 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2293 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2294 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2298 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2299 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2301 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2302 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2304 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2305 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2306 sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2307 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2308 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2309 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2312 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2313 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2316 lock_ReleaseRead(&cm_scacheLock);