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>
27 /*extern void afsi_log(char *pattern, ...);*/
29 extern osi_hyper_t hzero;
32 osi_queue_t *cm_allFileLocks;
33 osi_queue_t *cm_freeFileLocks;
34 unsigned long cm_lockRefreshCycle;
36 /* lock for globals */
37 osi_rwlock_t cm_scacheLock;
39 /* Dummy scache entry for use with pioctl fids */
40 cm_scache_t cm_fakeSCache;
42 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
44 #ifdef AFS_FREELANCE_CLIENT
45 extern osi_mutex_t cm_Freelance_Lock;
49 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
53 lock_ObtainWrite(&cm_data.rootSCachep->rw);
54 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
55 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
57 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
58 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
60 return cm_data.rootSCachep;
64 /* must be called with cm_scacheLock write-locked! */
65 void cm_AdjustScacheLRU(cm_scache_t *scp)
67 lock_AssertWrite(&cm_scacheLock);
68 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
69 if (scp->flags & CM_SCACHEFLAG_DELETED) {
70 /* Since it has been deleted make it the first to be recycled. */
71 osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
73 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
77 /* call with cm_scacheLock write-locked and scp rw held */
78 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
84 lock_AssertWrite(&cm_scacheLock);
85 lock_AssertWrite(&scp->rw);
86 if (scp->flags & CM_SCACHEFLAG_INHASH) {
87 /* hash it out first */
88 i = CM_SCACHE_HASH(&scp->fid);
89 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
91 lscpp = &tscp->nextp, tscp = tscp->nextp) {
95 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
102 /* called with cm_scacheLock and scp write-locked */
103 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
106 /* destroy directory Bplus Tree */
108 LARGE_INTEGER start, end;
110 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
112 * We are not holding the dirlock and obtaining it
113 * requires that we drop the scp->rw. As a result
114 * we will leave the dirBplus tree intact but
115 * invalidate the version number so that whatever
116 * operation is currently active can safely complete
117 * but the contents will be ignored on the next
118 * directory operation.
120 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
124 QueryPerformanceCounter(&start);
126 freeBtree(scp->dirBplus);
127 scp->dirBplus = NULL;
128 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
129 QueryPerformanceCounter(&end);
132 lock_ReleaseWrite(&scp->dirlock);
134 bplus_free_time += (end.QuadPart - start.QuadPart);
139 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
140 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
142 lock_AssertWrite(&cm_scacheLock);
143 lock_AssertWrite(&scp->rw);
145 if (scp->refCount != 0) {
149 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
150 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
152 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
157 cm_RemoveSCacheFromHashTable(scp);
160 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
161 osi_queueData_t *qdp;
164 while(qdp = scp->bufWritesp) {
165 bufp = osi_GetQData(qdp);
166 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
169 lock_ObtainMutex(&bufp->mx);
170 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
171 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
172 bufp->dirty_offset = 0;
173 bufp->dirty_length = 0;
174 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
175 bufp->error = VNOVNODE;
176 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
177 bufp->dirtyCounter++;
178 if (bufp->flags & CM_BUF_WAITING) {
179 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
180 osi_Wakeup((long) &bufp);
182 lock_ReleaseMutex(&bufp->mx);
186 while(qdp = scp->bufReadsp) {
187 bufp = osi_GetQData(qdp);
188 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
191 lock_ObtainMutex(&bufp->mx);
192 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMFETCHING);
193 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
194 bufp->dirty_offset = 0;
195 bufp->dirty_length = 0;
196 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
197 bufp->error = VNOVNODE;
198 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
199 bufp->dirtyCounter++;
200 if (bufp->flags & CM_BUF_WAITING) {
201 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
202 osi_Wakeup((long) &bufp);
204 lock_ReleaseMutex(&bufp->mx);
208 buf_CleanDirtyBuffers(scp);
210 /* look for things that shouldn't still be set */
211 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
212 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
216 /* invalidate so next merge works fine;
217 * also initialize some flags */
219 _InterlockedAnd(&scp->flags,
220 ~(CM_SCACHEFLAG_STATD
221 | CM_SCACHEFLAG_DELETED
223 | CM_SCACHEFLAG_PURERO
224 | CM_SCACHEFLAG_OVERQUOTA
225 | CM_SCACHEFLAG_OUTOFSPACE
226 | CM_SCACHEFLAG_EACCESS));
227 scp->serverModTime = 0;
228 scp->dataVersion = CM_SCACHE_VERSION_BAD;
229 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
230 scp->bulkStatProgress = hzero;
232 scp->waitQueueT = NULL;
234 if (scp->cbServerp) {
235 cm_PutServer(scp->cbServerp);
236 scp->cbServerp = NULL;
239 scp->volumeCreationDate = 0;
247 /* remove from dnlc */
251 /* discard cached status; if non-zero, Close
252 * tried to store this to server but failed */
255 /* discard symlink info */
256 scp->mountPointStringp[0] = '\0';
257 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
258 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
260 /* reset locking info */
261 scp->fileLocksH = NULL;
262 scp->fileLocksT = NULL;
263 scp->serverLock = (-1);
264 scp->exclusiveLocks = 0;
265 scp->sharedLocks = 0;
266 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
267 scp->fsLockCount = 0;
269 /* not locked, but there can be no references to this guy
270 * while we hold the global refcount lock.
272 cm_FreeAllACLEnts(scp);
274 cm_ResetSCacheDirectory(scp, 0);
280 * called with cm_scacheLock write-locked; find a vnode to recycle.
281 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
282 * returns scp->rw write-locked.
284 cm_scache_t *cm_GetNewSCache(void)
289 lock_AssertWrite(&cm_scacheLock);
291 /* first pass - look for deleted objects */
292 for ( scp = cm_data.scacheLRULastp;
294 scp = (cm_scache_t *) osi_QPrev(&scp->q))
296 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
297 "invalid cm_scache_t address");
299 if (scp->refCount == 0) {
300 if (scp->flags & CM_SCACHEFLAG_DELETED) {
301 if (!lock_TryWrite(&scp->rw))
304 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%p", scp);
305 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
307 /* we found an entry, so return it */
308 /* now remove from the LRU queue and put it back at the
309 * head of the LRU queue.
311 cm_AdjustScacheLRU(scp);
316 lock_ReleaseWrite(&scp->rw);
317 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%p", scp);
318 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
319 if (!lock_TryWrite(&scp->rw))
322 /* we found an entry, so return it */
323 /* now remove from the LRU queue and put it back at the
324 * head of the LRU queue.
326 cm_AdjustScacheLRU(scp);
333 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
336 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
337 /* There were no deleted scache objects that we could use. Try to find
338 * one that simply hasn't been used in a while.
340 for ( scp = cm_data.scacheLRULastp;
342 scp = (cm_scache_t *) osi_QPrev(&scp->q))
344 /* It is possible for the refCount to be zero and for there still
345 * to be outstanding dirty buffers. If there are dirty buffers,
346 * we must not recycle the scp. */
347 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
348 if (!buf_DirtyBuffersExist(&scp->fid)) {
349 if (!lock_TryWrite(&scp->rw))
352 if (!cm_RecycleSCache(scp, 0)) {
353 /* we found an entry, so return it */
354 /* now remove from the LRU queue and put it back at the
355 * head of the LRU queue.
357 cm_AdjustScacheLRU(scp);
362 lock_ReleaseWrite(&scp->rw);
364 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
368 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
373 /* if we get here, we should allocate a new scache entry. We either are below
374 * quota or we have a leak and need to allocate a new one to avoid panicing.
376 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
377 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
378 "invalid cm_scache_t address");
379 memset(scp, 0, sizeof(cm_scache_t));
380 scp->magic = CM_SCACHE_MAGIC;
381 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
382 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
383 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
385 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
387 scp->serverLock = -1;
389 /* and put it in the LRU queue */
390 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
391 if (!cm_data.scacheLRULastp)
392 cm_data.scacheLRULastp = scp;
393 cm_data.currentSCaches++;
394 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
396 scp->allNextp = cm_data.allSCachesp;
397 cm_data.allSCachesp = scp;
401 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
404 fidp->volume = volume;
406 fidp->unique = unique;
407 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
410 /* like strcmp, only for fids */
411 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
413 if (ap->hash != bp->hash)
415 if (ap->vnode != bp->vnode)
417 if (ap->volume != bp->volume)
419 if (ap->unique != bp->unique)
421 if (ap->cell != bp->cell)
426 void cm_fakeSCacheInit(int newFile)
429 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
430 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
431 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
432 cm_data.fakeSCache.cbExpires = (time_t)-1;
433 /* can leave clientModTime at 0 */
434 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
435 cm_data.fakeSCache.unixModeBits = 0777;
436 cm_data.fakeSCache.length.LowPart = 1000;
437 cm_data.fakeSCache.linkCount = 1;
438 cm_data.fakeSCache.refCount = 1;
439 cm_data.fakeSCache.serverLock = -1;
440 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
442 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
443 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
444 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
448 cm_ValidateSCache(void)
450 cm_scache_t * scp, *lscp;
453 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
454 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
455 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
456 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
460 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
462 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
463 if (scp->magic != CM_SCACHE_MAGIC) {
464 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
465 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
468 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
469 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
470 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
473 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
474 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
475 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
478 if (i > cm_data.currentSCaches ) {
479 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
480 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
483 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
484 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
485 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
490 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
491 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
492 if (scp->magic != CM_SCACHE_MAGIC) {
493 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
494 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
497 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
498 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
499 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
502 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
503 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
504 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
507 if (i > cm_data.currentSCaches ) {
508 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
509 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
512 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
513 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
514 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
519 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
520 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
522 hash = CM_SCACHE_HASH(&scp->fid);
523 if (scp->magic != CM_SCACHE_MAGIC) {
524 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
525 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
528 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
529 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
530 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
533 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
534 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
535 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
539 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
540 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
546 return cm_dnlcValidate();
550 cm_SuspendSCache(void)
555 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
558 * After this call all servers are marked down.
559 * Do not clear the callbacks, instead change the
560 * expiration time so that the callbacks will be expired
561 * when the servers are marked back up. However, we
562 * want the callbacks to be preserved as long as the
563 * servers are down. That way if the machine resumes
564 * without network, the stat cache item will still be
569 lock_ObtainWrite(&cm_scacheLock);
570 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
571 if (scp->cbServerp) {
572 if (scp->flags & CM_SCACHEFLAG_PURERO) {
573 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
575 if (volp->cbExpiresRO == scp->cbExpires)
576 volp->cbExpiresRO = now+1;
580 scp->cbExpires = now+1;
583 lock_ReleaseWrite(&cm_scacheLock);
587 cm_ShutdownSCache(void)
589 cm_scache_t * scp, * nextp;
591 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
593 lock_ObtainWrite(&cm_scacheLock);
595 for ( scp = cm_data.allSCachesp; scp;
597 nextp = scp->allNextp;
598 lock_ReleaseWrite(&cm_scacheLock);
600 lock_ObtainWrite(&scp->dirlock);
602 lock_ObtainWrite(&scp->rw);
603 lock_ObtainWrite(&cm_scacheLock);
605 if (scp->randomACLp) {
606 cm_FreeAllACLEnts(scp);
609 if (scp->cbServerp) {
610 cm_PutServer(scp->cbServerp);
611 scp->cbServerp = NULL;
614 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
615 lock_ReleaseWrite(&scp->rw);
619 freeBtree(scp->dirBplus);
620 scp->dirBplus = NULL;
621 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
622 lock_ReleaseWrite(&scp->dirlock);
623 lock_FinalizeRWLock(&scp->dirlock);
625 lock_FinalizeRWLock(&scp->rw);
626 lock_FinalizeRWLock(&scp->bufCreateLock);
628 lock_ReleaseWrite(&cm_scacheLock);
630 return cm_dnlcShutdown();
633 void cm_InitSCache(int newFile, long maxSCaches)
635 static osi_once_t once;
637 if (osi_Once(&once)) {
638 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
640 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
641 cm_data.allSCachesp = NULL;
642 cm_data.currentSCaches = 0;
643 cm_data.maxSCaches = maxSCaches;
644 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
648 for ( scp = cm_data.allSCachesp; scp;
649 scp = scp->allNextp ) {
650 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
651 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
653 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
655 scp->cbServerp = NULL;
657 scp->volumeCreationDate = 0;
658 scp->fileLocksH = NULL;
659 scp->fileLocksT = NULL;
660 scp->serverLock = (-1);
661 scp->lastRefreshCycle = 0;
662 scp->exclusiveLocks = 0;
663 scp->sharedLocks = 0;
670 scp->dirBplus = NULL;
671 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
673 scp->waitQueueT = NULL;
674 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
677 cm_allFileLocks = NULL;
678 cm_freeFileLocks = NULL;
679 cm_lockRefreshCycle = 0;
680 cm_fakeSCacheInit(newFile);
681 cm_allFreeWaiters = NULL;
682 cm_dnlcInit(newFile);
687 /* version that doesn't bother creating the entry if we don't find it */
688 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
693 hash = CM_SCACHE_HASH(fidp);
695 if (fidp->cell == 0) {
699 lock_ObtainRead(&cm_scacheLock);
700 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
701 if (cm_FidCmp(fidp, &scp->fid) == 0) {
702 cm_HoldSCacheNoLock(scp);
703 lock_ConvertRToW(&cm_scacheLock);
704 cm_AdjustScacheLRU(scp);
705 lock_ReleaseWrite(&cm_scacheLock);
709 lock_ReleaseRead(&cm_scacheLock);
713 #ifdef DEBUG_REFCOUNT
714 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
715 cm_req_t *reqp, char * file, long line)
717 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
722 cm_scache_t *scp = NULL;
724 cm_volume_t *volp = NULL;
726 int special = 0; // yj: boolean variable to test if file is on root.afs
728 extern cm_fid_t cm_rootFid;
730 hash = CM_SCACHE_HASH(fidp);
733 return CM_ERROR_INVAL;
735 #ifdef AFS_FREELANCE_CLIENT
736 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
737 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
738 !(fidp->vnode==0x1 && fidp->unique==0x1));
739 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
740 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
741 fidp->vnode==0x1 && fidp->unique==0x1);
744 // yj: check if we have the scp, if so, we don't need
745 // to do anything else
746 lock_ObtainWrite(&cm_scacheLock);
747 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
748 if (cm_FidCmp(fidp, &scp->fid) == 0) {
749 #ifdef DEBUG_REFCOUNT
750 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
751 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
753 #ifdef AFS_FREELANCE_CLIENT
754 if (cm_freelanceEnabled && special &&
755 cm_data.fakeDirVersion != scp->dataVersion)
758 cm_HoldSCacheNoLock(scp);
760 cm_AdjustScacheLRU(scp);
761 lock_ReleaseWrite(&cm_scacheLock);
766 // yj: when we get here, it means we don't have an scp
767 // so we need to either load it or fake it, depending
768 // on whether the file is "special", see below.
770 // yj: if we're trying to get an scp for a file that's
771 // on root.afs of homecell, we want to handle it specially
772 // because we have to fill in the status stuff 'coz we
773 // don't want trybulkstat to fill it in for us
774 #ifdef AFS_FREELANCE_CLIENT
775 if (cm_freelanceEnabled && isRoot) {
776 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
777 /* freelance: if we are trying to get the root scp for the first
778 * time, we will just put in a place holder entry.
783 if (cm_freelanceEnabled && special) {
784 lock_ReleaseWrite(&cm_scacheLock);
785 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
787 if (cm_getLocalMountPointChange()) {
788 cm_clearLocalMountPointChange();
789 cm_reInitLocalMountPoints();
792 lock_ObtainWrite(&cm_scacheLock);
794 scp = cm_GetNewSCache(); /* returns scp->rw held */
796 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
797 lock_ReleaseWrite(&cm_scacheLock);
798 return CM_ERROR_WOULDBLOCK;
801 lock_ReleaseWrite(&cm_scacheLock);
802 lock_ObtainWrite(&scp->rw);
803 lock_ObtainWrite(&cm_scacheLock);
806 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
807 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
808 scp->dotdotFid.unique=1;
809 scp->dotdotFid.vnode=1;
810 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
811 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
812 scp->nextp = cm_data.scacheHashTablep[hash];
813 cm_data.scacheHashTablep[hash] = scp;
814 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
817 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
819 /* must be called after the scp->fid is set */
820 cm_FreelanceFetchMountPointString(scp);
821 cm_FreelanceFetchFileType(scp);
823 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
824 scp->length.HighPart = 0;
826 scp->unixModeBits=0777;
827 scp->clientModTime=FakeFreelanceModTime;
828 scp->serverModTime=FakeFreelanceModTime;
829 scp->parentUnique = 0x1;
830 scp->parentVnode=0x1;
832 scp->dataVersion=cm_data.fakeDirVersion;
833 scp->bufDataVersionLow=cm_data.fakeDirVersion;
834 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
836 lock_ReleaseWrite(&scp->rw);
837 lock_ReleaseWrite(&cm_scacheLock);
839 #ifdef DEBUG_REFCOUNT
840 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
841 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
846 #endif /* AFS_FREELANCE_CLIENT */
848 /* otherwise, we need to find the volume */
849 if (!cm_freelanceEnabled || !isRoot) {
850 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
851 cellp = cm_FindCellByID(fidp->cell, 0);
853 return CM_ERROR_NOSUCHCELL;
855 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
858 lock_ObtainWrite(&cm_scacheLock);
861 /* otherwise, we have the volume, now reverify that the scp doesn't
862 * exist, and proceed.
864 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
865 if (cm_FidCmp(fidp, &scp->fid) == 0) {
866 #ifdef DEBUG_REFCOUNT
867 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
868 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
870 cm_HoldSCacheNoLock(scp);
871 cm_AdjustScacheLRU(scp);
872 lock_ReleaseWrite(&cm_scacheLock);
880 /* now, if we don't have the fid, recycle something */
881 scp = cm_GetNewSCache(); /* returns scp->rw held */
883 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
884 lock_ReleaseWrite(&cm_scacheLock);
887 return CM_ERROR_WOULDBLOCK;
889 #ifdef DEBUG_REFCOUNT
890 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
892 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
894 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
897 if (!cm_freelanceEnabled || !isRoot) {
898 /* if this scache entry represents a volume root then we need
899 * to copy the dotdotFipd from the volume structure where the
900 * "master" copy is stored (defect 11489)
902 if (volp->vol[ROVOL].ID == fidp->volume) {
903 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
904 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
905 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
906 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
907 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
908 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
909 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
911 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
912 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
917 scp->nextp = cm_data.scacheHashTablep[hash];
918 cm_data.scacheHashTablep[hash] = scp;
919 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
920 lock_ReleaseWrite(&scp->rw);
922 #ifdef DEBUG_REFCOUNT
923 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
925 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
927 /* XXX - The following fields in the cm_scache are
934 /* now we have a held scache entry; just return it */
936 #ifdef DEBUG_REFCOUNT
937 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
938 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
940 lock_ReleaseWrite(&cm_scacheLock);
944 /* Returns a held reference to the scache's parent
946 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
951 cm_scache_t * pscp = NULL;
953 lock_ObtainWrite(&cm_scacheLock);
954 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
956 if (cm_FidCmp(&scp->fid, &parent_fid)) {
957 i = CM_SCACHE_HASH(&parent_fid);
958 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
959 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
960 cm_HoldSCacheNoLock(pscp);
966 lock_ReleaseWrite(&cm_scacheLock);
971 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
973 cm_scache_waiter_t * w;
975 lock_ObtainWrite(&cm_scacheLock);
976 if (cm_allFreeWaiters == NULL) {
977 w = malloc(sizeof(*w));
978 memset(w, 0, sizeof(*w));
980 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
981 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
984 w->threadId = thrd_Current();
986 cm_HoldSCacheNoLock(scp);
990 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
991 lock_ReleaseWrite(&cm_scacheLock);
993 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
996 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
998 cm_scache_waiter_t * w;
1001 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1003 lock_ObtainRead(&cm_scacheLock);
1004 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1006 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1007 if (w->flags == flags && w->bufp == bufp) {
1012 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1013 this_is_me = (w->threadId == thrd_Current());
1014 lock_ReleaseRead(&cm_scacheLock);
1017 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1021 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1023 lock_ObtainWrite(&cm_scacheLock);
1024 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1025 cm_ReleaseSCacheNoLock(scp);
1026 memset(w, 0, sizeof(*w));
1027 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1028 lock_ReleaseWrite(&cm_scacheLock);
1034 /* synchronize a fetch, store, read, write, fetch status or store status.
1035 * Called with scache mutex held, and returns with it held, but temporarily
1036 * drops it during the fetch.
1038 * At most one flag can be on in flags, if this is an RPC request.
1040 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1042 * There are a lot of weird restrictions here; here's an attempt to explain the
1043 * rationale for the concurrency restrictions implemented in this function.
1045 * First, although the file server will break callbacks when *another* machine
1046 * modifies a file or status block, the client itself is responsible for
1047 * concurrency control on its own requests. Callback breaking events are rare,
1048 * and simply invalidate any concurrent new status info.
1050 * In the absence of callback breaking messages, we need to know how to
1051 * synchronize incoming responses describing updates to files. We synchronize
1052 * operations that update the data version by comparing the data versions.
1053 * However, updates that do not update the data, but only the status, can't be
1054 * synchronized with fetches or stores, since there's nothing to compare
1055 * to tell which operation executed first at the server.
1057 * Thus, we can allow multiple ops that change file data, or dir data, and
1058 * fetches. However, status storing ops have to be done serially.
1060 * Furthermore, certain data-changing ops are incompatible: we can't read or
1061 * write a buffer while doing a truncate. We can't read and write the same
1062 * buffer at the same time, or write while fetching or storing, or read while
1063 * fetching a buffer (this may change). We can't fetch and store at the same
1066 * With respect to status, we can't read and write at the same time, read while
1067 * fetching, write while fetching or storing, or fetch and store at the same time.
1069 * We can't allow a get callback RPC to run in concurrently with something that
1070 * will return updated status, since we could start a call, have the server
1071 * return status, have another machine make an update to the status (which
1072 * doesn't change serverModTime), have the original machine get a new callback,
1073 * and then have the original machine merge in the early, old info from the
1074 * first call. At this point, the easiest way to avoid this problem is to have
1075 * getcallback calls conflict with all others for the same vnode. Other calls
1076 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1077 * vnode must be careful not to merge in their status unless they have obtained
1078 * a callback from the start of their call.
1080 * Note added 1/23/96
1081 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1082 * Each such RPC passes a FileLength parameter, which the server uses to do
1083 * pre-truncation if necessary. So if two RPC's are processed out of order at
1084 * the server, the one with the smaller FileLength will be processed last,
1085 * possibly resulting in a bogus truncation. The simplest way to avoid this
1086 * is to serialize all StoreData RPC's. This is the reason we defined
1087 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1089 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1090 afs_uint32 rights, afs_uint32 flags)
1092 osi_queueData_t *qdp;
1095 afs_uint32 outRights;
1097 afs_uint32 sleep_scp_flags = 0;
1098 afs_uint32 sleep_buf_cmflags = 0;
1099 afs_uint32 sleep_scp_bufs = 0;
1102 lock_AssertWrite(&scp->rw);
1104 /* lookup this first */
1105 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1108 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1111 /* Do the access check. Now we don't really do the access check
1112 * atomically, since the caller doesn't expect the parent dir to be
1113 * returned locked, and that is what we'd have to do to prevent a
1114 * callback breaking message on the parent due to a setacl call from
1115 * being processed while we're running. So, instead, we check things
1116 * here, and if things look fine with the access, we proceed to finish
1117 * the rest of this check. Sort of a hack, but probably good enough.
1121 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1122 /* if we're bringing in a new status block, ensure that
1123 * we aren't already doing so, and that no one is
1124 * changing the status concurrently, either. We need
1125 * to do this, even if the status is of a different
1126 * type, since we don't have the ability to figure out,
1127 * in the AFS 3 protocols, which status-changing
1128 * operation ran first, or even which order a read and
1129 * a write occurred in.
1131 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1132 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1133 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1137 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1138 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1139 /* if we're going to make an RPC to change the status, make sure
1140 * that no one is bringing in or sending out the status.
1142 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1143 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1144 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1147 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1148 (scp->bufReadsp || scp->bufWritesp)) {
1149 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1153 if (flags & CM_SCACHESYNC_FETCHDATA) {
1154 /* if we're bringing in a new chunk of data, make sure that
1155 * nothing is happening to that chunk, and that we aren't
1156 * changing the basic file status info, either.
1158 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1159 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1160 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1163 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1164 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1168 if (flags & CM_SCACHESYNC_STOREDATA) {
1169 /* same as fetch data */
1170 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1171 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1172 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1175 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1176 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1181 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1182 /* Don't allow concurrent StoreData RPC's */
1183 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1184 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1189 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1190 /* Don't allow more than one BKG store request */
1191 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1192 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1197 if (flags & CM_SCACHESYNC_LOCK) {
1198 /* Don't allow concurrent fiddling with lock lists */
1199 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1200 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1205 /* now the operations that don't correspond to making RPCs */
1206 if (flags & CM_SCACHESYNC_GETSTATUS) {
1207 /* we can use the status that's here, if we're not
1208 * bringing in new status.
1210 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1211 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1215 if (flags & CM_SCACHESYNC_SETSTATUS) {
1216 /* we can make a change to the local status, as long as
1217 * the status isn't changing now.
1219 * If we're fetching or storing a chunk of data, we can
1220 * change the status locally, since the fetch/store
1221 * operations don't change any of the data that we're
1224 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1225 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1226 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1230 if (flags & CM_SCACHESYNC_READ) {
1231 /* we're going to read the data, make sure that the
1232 * status is available, and that the data is here. It
1233 * is OK to read while storing the data back.
1235 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1236 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1239 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1240 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1243 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1244 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1248 if (flags & CM_SCACHESYNC_WRITE) {
1249 /* don't write unless the status is stable and the chunk
1252 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1253 CM_SCACHEFLAG_SIZESTORING)) {
1254 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1257 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1259 CM_BUF_CMWRITING))) {
1260 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1262 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1263 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1264 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1270 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1271 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1272 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1275 lock_ReleaseMutex(&bufp->mx);
1276 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1278 lock_ReleaseWrite(&scp->rw);
1279 lock_ObtainMutex(&bufp->mx);
1280 lock_ObtainWrite(&scp->rw);
1284 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1290 /* can't check access rights without a callback */
1291 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1293 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1294 return CM_ERROR_READONLY;
1296 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1297 if (~outRights & rights)
1298 return CM_ERROR_NOACCESS;
1301 /* we don't know the required access rights */
1302 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1303 code = cm_GetAccessRights(scp, userp, reqp);
1305 lock_ReleaseWrite(&scp->rw);
1306 lock_ObtainMutex(&bufp->mx);
1307 lock_ObtainWrite(&scp->rw);
1315 /* if we get here, we're happy */
1319 /* first check if we're not supposed to wait: fail
1320 * in this case, returning with everything still locked.
1322 if (flags & CM_SCACHESYNC_NOWAIT)
1323 return CM_ERROR_WOULDBLOCK;
1325 /* These are used for minidump debugging */
1326 sleep_scp_flags = scp->flags; /* so we know why we slept */
1327 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1328 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1330 /* wait here, then try again */
1331 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1332 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1334 scp->waitRequests++;
1335 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1336 scp, scp->waitCount, scp->waitRequests);
1338 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1339 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1340 scp->waitCount = scp->waitRequests = 1;
1343 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1347 lock_ReleaseMutex(&bufp->mx);
1348 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1350 lock_ObtainMutex(&bufp->mx);
1351 lock_ObtainWrite(&scp->rw);
1352 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1354 cm_UpdateServerPriority();
1357 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1358 scp, scp->waitCount, scp->waitRequests);
1359 if (scp->waitCount == 0) {
1360 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1361 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1362 scp->waitRequests = 0;
1364 } /* big while loop */
1366 /* now, update the recorded state for RPC-type calls */
1367 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1368 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1369 if (flags & CM_SCACHESYNC_STORESTATUS)
1370 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1371 if (flags & CM_SCACHESYNC_SETSIZE)
1372 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1373 if (flags & CM_SCACHESYNC_STORESIZE)
1374 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1375 if (flags & CM_SCACHESYNC_GETCALLBACK)
1376 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1377 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1378 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1379 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1380 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1381 if (flags & CM_SCACHESYNC_LOCK)
1382 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1384 /* now update the buffer pointer */
1385 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1386 /* ensure that the buffer isn't already in the I/O list */
1387 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1388 tbufp = osi_GetQData(qdp);
1389 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1392 /* queue a held reference to the buffer in the "reading" I/O list */
1393 qdp = osi_QDAlloc();
1394 osi_SetQData(qdp, bufp);
1397 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1398 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1401 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1402 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1403 "attempting to store extents on a non-file object");
1405 /* ensure that the buffer isn't already in the I/O list */
1406 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1407 tbufp = osi_GetQData(qdp);
1408 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1411 /* queue a held reference to the buffer in the "writing" I/O list */
1412 qdp = osi_QDAlloc();
1413 osi_SetQData(qdp, bufp);
1415 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1416 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1419 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1420 /* mark the buffer as being written to. */
1421 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1427 /* for those syncops that setup for RPCs.
1428 * Called with scache locked.
1430 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1432 osi_queueData_t *qdp;
1435 lock_AssertWrite(&scp->rw);
1437 /* now, update the recorded state for RPC-type calls */
1438 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1439 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1440 if (flags & CM_SCACHESYNC_STORESTATUS)
1441 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1442 if (flags & CM_SCACHESYNC_SETSIZE)
1443 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1444 if (flags & CM_SCACHESYNC_STORESIZE)
1445 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1446 if (flags & CM_SCACHESYNC_GETCALLBACK)
1447 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1448 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1449 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1450 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1451 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1452 if (flags & CM_SCACHESYNC_LOCK)
1453 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1455 /* now update the buffer pointer */
1456 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1459 /* ensure that the buffer is in the I/O list */
1460 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1461 tbufp = osi_GetQData(qdp);
1466 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1470 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1471 if (bufp->flags & CM_BUF_WAITING) {
1472 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1473 osi_Wakeup((LONG_PTR) &bufp);
1479 /* now update the buffer pointer */
1480 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1482 /* ensure that the buffer is in the I/O list */
1483 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1484 tbufp = osi_GetQData(qdp);
1489 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1493 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1494 if (bufp->flags & CM_BUF_WAITING) {
1495 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1496 osi_Wakeup((LONG_PTR) &bufp);
1502 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1503 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1504 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1507 /* and wakeup anyone who is waiting */
1508 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1509 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1510 osi_Wakeup((LONG_PTR) &scp->flags);
1514 /* merge in a response from an RPC. The scp must be locked, and the callback
1517 * Don't overwrite any status info that is dirty, since we could have a store
1518 * operation (such as store data) that merges some info in, and we don't want
1519 * to lose the local updates. Typically, there aren't many updates we do
1520 * locally, anyway, probably only mtime.
1522 * There is probably a bug in here where a chmod (which doesn't change
1523 * serverModTime) that occurs between two fetches, both of whose responses are
1524 * handled after the callback breaking is done, but only one of whose calls
1525 * started before that, can cause old info to be merged from the first call.
1527 void cm_MergeStatus(cm_scache_t *dscp,
1528 cm_scache_t *scp, AFSFetchStatus *statusp,
1529 AFSVolSync *volsyncp,
1530 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1532 afs_uint64 dataVersion;
1533 struct cm_volume *volp = NULL;
1534 struct cm_cell *cellp = NULL;
1536 lock_AssertWrite(&scp->rw);
1538 // yj: i want to create some fake status for the /afs directory and the
1539 // entries under that directory
1540 #ifdef AFS_FREELANCE_CLIENT
1541 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1542 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1543 if (scp == cm_data.rootSCachep) {
1544 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1545 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1546 statusp->Length = cm_fakeDirSize;
1547 statusp->Length_hi = 0;
1549 statusp->FileType = scp->fileType;
1550 statusp->Length = scp->length.LowPart;
1551 statusp->Length_hi = scp->length.HighPart;
1553 statusp->InterfaceVersion = 0x1;
1554 statusp->LinkCount = scp->linkCount;
1555 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1556 statusp->Author = 0x1;
1557 statusp->Owner = 0x0;
1558 statusp->CallerAccess = 0x9;
1559 statusp->AnonymousAccess = 0x9;
1560 statusp->UnixModeBits = 0777;
1561 statusp->ParentVnode = 0x1;
1562 statusp->ParentUnique = 0x1;
1563 statusp->ResidencyMask = 0;
1564 statusp->ClientModTime = FakeFreelanceModTime;
1565 statusp->ServerModTime = FakeFreelanceModTime;
1567 statusp->SyncCounter = 0;
1568 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1569 statusp->lockCount = 0;
1570 statusp->errorCode = 0;
1572 #endif /* AFS_FREELANCE_CLIENT */
1574 if (statusp->errorCode != 0) {
1575 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1576 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1578 scp->fileType = 0; /* unknown */
1580 scp->serverModTime = 0;
1581 scp->clientModTime = 0;
1582 scp->length.LowPart = 0;
1583 scp->length.HighPart = 0;
1584 scp->serverLength.LowPart = 0;
1585 scp->serverLength.HighPart = 0;
1589 scp->unixModeBits = 0;
1591 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1592 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1593 scp->fsLockCount = 0;
1596 scp->parentVnode = dscp->fid.vnode;
1597 scp->parentUnique = dscp->fid.unique;
1599 scp->parentVnode = 0;
1600 scp->parentUnique = 0;
1604 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1607 dataVersion = statusp->dataVersionHigh;
1609 dataVersion |= statusp->DataVersion;
1611 if (!(flags & CM_MERGEFLAG_FORCE) &&
1612 dataVersion < scp->dataVersion &&
1613 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1615 cellp = cm_FindCellByID(scp->fid.cell, 0);
1616 if (scp->cbServerp) {
1617 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1618 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1619 osi_Log2(afsd_logp, "old data from server %x volume %s",
1620 scp->cbServerp->addr.sin_addr.s_addr,
1621 volp ? volp->namep : "(unknown)");
1623 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1624 scp, scp->dataVersion, dataVersion);
1625 /* we have a number of data fetch/store operations running
1626 * concurrently, and we can tell which one executed last at the
1627 * server by its mtime.
1628 * Choose the one with the largest mtime, and ignore the rest.
1630 * These concurrent calls are incompatible with setting the
1631 * mtime, so we won't have a locally changed mtime here.
1633 * We could also have ACL info for a different user than usual,
1634 * in which case we have to do that part of the merge, anyway.
1635 * We won't have to worry about the info being old, since we
1636 * won't have concurrent calls
1637 * that change file status running from this machine.
1639 * Added 3/17/98: if we see data version regression on an RO
1640 * file, it's probably due to a server holding an out-of-date
1641 * replica, rather than to concurrent RPC's. Failures to
1642 * release replicas are now flagged by the volserver, but only
1643 * since AFS 3.4 5.22, so there are plenty of clients getting
1644 * out-of-date replicas out there.
1646 * If we discover an out-of-date replica, by this time it's too
1647 * late to go to another server and retry. Also, we can't
1648 * reject the merge, because then there is no way for
1649 * GetAccess to do its work, and the caller gets into an
1650 * infinite loop. So we just grin and bear it.
1652 if (!(scp->flags & CM_SCACHEFLAG_RO))
1656 if (cm_readonlyVolumeVersioning)
1657 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1659 scp->serverModTime = statusp->ServerModTime;
1661 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1662 scp->clientModTime = statusp->ClientModTime;
1664 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1665 scp->length.LowPart = statusp->Length;
1666 scp->length.HighPart = statusp->Length_hi;
1669 scp->serverLength.LowPart = statusp->Length;
1670 scp->serverLength.HighPart = statusp->Length_hi;
1672 scp->linkCount = statusp->LinkCount;
1673 scp->owner = statusp->Owner;
1674 scp->group = statusp->Group;
1675 scp->unixModeBits = statusp->UnixModeBits & 07777;
1677 if (statusp->FileType == File)
1678 scp->fileType = CM_SCACHETYPE_FILE;
1679 else if (statusp->FileType == Directory)
1680 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1681 else if (statusp->FileType == SymbolicLink) {
1682 if ((scp->unixModeBits & 0111) == 0)
1683 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1685 scp->fileType = CM_SCACHETYPE_SYMLINK;
1688 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1689 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1691 /* and other stuff */
1692 scp->parentVnode = statusp->ParentVnode;
1693 scp->parentUnique = statusp->ParentUnique;
1695 /* -1 is a write lock; any positive values are read locks */
1696 scp->fsLockCount = (afs_int32)statusp->lockCount;
1698 /* and merge in the private acl cache info, if this is more than the public
1699 * info; merge in the public stuff in any case.
1701 scp->anyAccess = statusp->AnonymousAccess;
1703 if (userp != NULL) {
1704 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1707 if (scp->dataVersion != 0 &&
1708 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1709 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1711 * We now know that all of the data buffers that we have associated
1712 * with this scp are invalid. Subsequent operations will go faster
1713 * if the buffers are removed from the hash tables.
1715 * We do not remove directory buffers if the dataVersion delta is 1 because
1716 * those version numbers will be updated as part of the directory operation.
1718 * We do not remove storedata buffers because they will still be valid.
1723 cm_buf_t *bp, *prevBp, *nextBp;
1725 lock_ObtainWrite(&buf_globalLock);
1726 i = BUF_FILEHASH(&scp->fid);
1727 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1729 nextBp = bp->fileHashp;
1731 * if the buffer belongs to this stat cache entry
1732 * and the buffer mutex can be obtained, check the
1733 * reference count and if it is zero, remove the buffer
1734 * from the hash tables. If there are references,
1735 * the buffer might be updated to the current version
1736 * so leave it in place.
1738 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1739 lock_TryMutex(&bp->mx)) {
1740 if (bp->refCount == 0 &&
1741 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1742 prevBp = bp->fileHashBackp;
1743 bp->fileHashBackp = bp->fileHashp = NULL;
1745 prevBp->fileHashp = nextBp;
1747 cm_data.buf_fileHashTablepp[i] = nextBp;
1749 nextBp->fileHashBackp = prevBp;
1751 j = BUF_HASH(&bp->fid, &bp->offset);
1752 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1753 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1758 *lbpp = bp->hashp; /* hash out */
1761 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1763 lock_ReleaseMutex(&bp->mx);
1766 lock_ReleaseWrite(&buf_globalLock);
1770 * If the dataVersion has changed, the mountPointStringp must be cleared
1771 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1772 * does not update a mountpoint or symlink by altering the contents of
1773 * the file data; but the Unix CM does.
1775 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA))
1776 scp->mountPointStringp[0] = '\0';
1778 /* We maintain a range of buffer dataVersion values which are considered
1779 * valid. This avoids the need to update the dataVersion on each buffer
1780 * object during an uncontested storeData operation. As a result this
1781 * merge status no longer has performance characteristics derived from
1782 * the size of the file.
1784 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1785 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1786 scp->bufDataVersionLow == 0)
1787 scp->bufDataVersionLow = dataVersion;
1789 scp->dataVersion = dataVersion;
1792 * If someone is waiting for status information, we can wake them up
1793 * now even though the entity that issued the FetchStatus may not
1794 * have completed yet.
1796 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1799 * We just successfully merged status on the stat cache object.
1800 * This means that the associated volume must be online.
1804 cellp = cm_FindCellByID(scp->fid.cell, 0);
1805 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1808 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1809 if (statep->state != vl_online) {
1810 lock_ObtainWrite(&volp->rw);
1811 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1812 statep->state = vl_online;
1813 lock_ReleaseWrite(&volp->rw);
1822 /* note that our stat cache info is incorrect, so force us eventually
1823 * to stat the file again. There may be dirty data associated with
1824 * this vnode, and we want to preserve that information.
1826 * This function works by simply simulating a loss of the callback.
1828 * This function must be called with the scache locked.
1830 void cm_DiscardSCache(cm_scache_t *scp)
1832 lock_AssertWrite(&scp->rw);
1833 if (scp->cbServerp) {
1834 cm_PutServer(scp->cbServerp);
1835 scp->cbServerp = NULL;
1838 scp->volumeCreationDate = 0;
1839 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL));
1840 cm_dnlcPurgedp(scp);
1841 cm_dnlcPurgevp(scp);
1842 cm_FreeAllACLEnts(scp);
1844 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1845 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1847 /* Force mount points and symlinks to be re-evaluated */
1848 scp->mountPointStringp[0] = '\0';
1851 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1853 afsFidp->Volume = fidp->volume;
1854 afsFidp->Vnode = fidp->vnode;
1855 afsFidp->Unique = fidp->unique;
1858 #ifdef DEBUG_REFCOUNT
1859 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1861 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1866 osi_assertx(scp != NULL, "null cm_scache_t");
1867 lock_AssertAny(&cm_scacheLock);
1868 refCount = InterlockedIncrement(&scp->refCount);
1869 #ifdef DEBUG_REFCOUNT
1870 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1871 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1875 #ifdef DEBUG_REFCOUNT
1876 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1878 void cm_HoldSCache(cm_scache_t *scp)
1883 osi_assertx(scp != NULL, "null cm_scache_t");
1884 lock_ObtainRead(&cm_scacheLock);
1885 refCount = InterlockedIncrement(&scp->refCount);
1886 #ifdef DEBUG_REFCOUNT
1887 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1888 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1890 lock_ReleaseRead(&cm_scacheLock);
1893 #ifdef DEBUG_REFCOUNT
1894 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1896 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1901 osi_assertx(scp != NULL, "null cm_scache_t");
1902 lock_AssertAny(&cm_scacheLock);
1904 refCount = InterlockedDecrement(&scp->refCount);
1905 #ifdef DEBUG_REFCOUNT
1907 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1909 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1910 #ifdef DEBUG_REFCOUNT
1911 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1912 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1915 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1919 lockstate = lock_GetRWLockState(&cm_scacheLock);
1920 if (lockstate != OSI_RWLOCK_WRITEHELD)
1921 lock_ReleaseRead(&cm_scacheLock);
1923 lock_ReleaseWrite(&cm_scacheLock);
1925 lock_ObtainWrite(&scp->rw);
1926 if (scp->flags & CM_SCACHEFLAG_DELETED)
1929 if (refCount == 0 && deleted) {
1930 lock_ObtainWrite(&cm_scacheLock);
1931 cm_RecycleSCache(scp, 0);
1932 if (lockstate != OSI_RWLOCK_WRITEHELD)
1933 lock_ConvertWToR(&cm_scacheLock);
1935 if (lockstate != OSI_RWLOCK_WRITEHELD)
1936 lock_ObtainRead(&cm_scacheLock);
1938 lock_ObtainWrite(&cm_scacheLock);
1940 lock_ReleaseWrite(&scp->rw);
1944 #ifdef DEBUG_REFCOUNT
1945 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1947 void cm_ReleaseSCache(cm_scache_t *scp)
1952 osi_assertx(scp != NULL, "null cm_scache_t");
1953 lock_ObtainRead(&cm_scacheLock);
1954 refCount = InterlockedDecrement(&scp->refCount);
1955 #ifdef DEBUG_REFCOUNT
1957 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1959 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1960 #ifdef DEBUG_REFCOUNT
1961 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1962 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1964 lock_ReleaseRead(&cm_scacheLock);
1966 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1968 lock_ObtainWrite(&scp->rw);
1969 if (scp->flags & CM_SCACHEFLAG_DELETED)
1972 lock_ObtainWrite(&cm_scacheLock);
1973 cm_RecycleSCache(scp, 0);
1974 lock_ReleaseWrite(&cm_scacheLock);
1976 lock_ReleaseWrite(&scp->rw);
1980 /* just look for the scp entry to get filetype */
1981 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1982 int cm_FindFileType(cm_fid_t *fidp)
1987 hash = CM_SCACHE_HASH(fidp);
1989 osi_assertx(fidp->cell != 0, "unassigned cell value");
1991 lock_ObtainWrite(&cm_scacheLock);
1992 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1993 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1994 lock_ReleaseWrite(&cm_scacheLock);
1995 return scp->fileType;
1998 lock_ReleaseWrite(&cm_scacheLock);
2002 /* dump all scp's that have reference count > 0 to a file.
2003 * cookie is used to identify this batch for easy parsing,
2004 * and it a string provided by a caller
2006 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2015 lock_ObtainRead(&cm_scacheLock);
2017 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2018 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2020 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2023 char *srvStr = NULL;
2024 afs_uint32 srvStrRpc = TRUE;
2028 if (scp->cbServerp) {
2029 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2030 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2031 srvStr = malloc(16); /* enough for 255.255.255.255 */
2033 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2037 if (scp->cbExpires) {
2042 cbt[strlen(cbt)-1] = '\0';
2045 if (scp->volumeCreationDate) {
2046 t = scp->volumeCreationDate;
2049 cdrot = strdup(cdrot);
2050 cdrot[strlen(cdrot)-1] = '\0';
2054 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2055 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2056 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2057 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2058 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2059 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2060 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2061 cdrot ? cdrot : "<none>", scp->refCount);
2062 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2064 if (scp->fileLocksH) {
2065 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2066 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2068 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2069 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2070 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2071 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2072 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2073 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2074 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2077 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2078 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2083 RpcStringFree(&srvStr);
2093 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2094 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2095 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2096 cookie, cm_data.scacheHashTableSize);
2097 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2099 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2101 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2103 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2104 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2105 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2109 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2110 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2112 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2113 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2115 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2116 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2117 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",
2118 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2119 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2120 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2123 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2124 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2127 lock_ReleaseRead(&cm_scacheLock);