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__);
158 /* invalidate so next merge works fine;
159 * also initialize some flags */
161 _InterlockedAnd(&scp->flags,
162 ~(CM_SCACHEFLAG_STATD
163 | CM_SCACHEFLAG_DELETED
165 | CM_SCACHEFLAG_PURERO
166 | CM_SCACHEFLAG_OVERQUOTA
167 | CM_SCACHEFLAG_OUTOFSPACE
168 | CM_SCACHEFLAG_EACCESS));
169 scp->serverModTime = 0;
170 scp->dataVersion = CM_SCACHE_VERSION_BAD;
171 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
172 scp->bulkStatProgress = hzero;
174 scp->waitQueueT = NULL;
176 if (scp->cbServerp) {
177 cm_PutServer(scp->cbServerp);
178 scp->cbServerp = NULL;
181 scp->volumeCreationDate = 0;
189 /* remove from dnlc */
193 /* discard cached status; if non-zero, Close
194 * tried to store this to server but failed */
197 /* discard symlink info */
198 scp->mountPointStringp[0] = '\0';
199 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
200 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
202 /* reset locking info */
203 scp->fileLocksH = NULL;
204 scp->fileLocksT = NULL;
205 scp->serverLock = (-1);
206 scp->exclusiveLocks = 0;
207 scp->sharedLocks = 0;
208 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
209 scp->fsLockCount = 0;
211 /* not locked, but there can be no references to this guy
212 * while we hold the global refcount lock.
214 cm_FreeAllACLEnts(scp);
216 cm_ResetSCacheDirectory(scp, 0);
222 * called with cm_scacheLock write-locked; find a vnode to recycle.
223 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
224 * returns scp->rw write-locked.
226 cm_scache_t *cm_GetNewSCache(void)
231 lock_AssertWrite(&cm_scacheLock);
232 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
233 /* There were no deleted scache objects that we could use. Try to find
234 * one that simply hasn't been used in a while.
236 for ( scp = cm_data.scacheLRULastp;
238 scp = (cm_scache_t *) osi_QPrev(&scp->q))
240 /* It is possible for the refCount to be zero and for there still
241 * to be outstanding dirty buffers. If there are dirty buffers,
242 * we must not recycle the scp. */
243 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
244 if (!buf_DirtyBuffersExist(&scp->fid)) {
245 if (!lock_TryWrite(&scp->rw))
248 if (!cm_RecycleSCache(scp, 0)) {
249 /* we found an entry, so return it */
250 /* now remove from the LRU queue and put it back at the
251 * head of the LRU queue.
253 cm_AdjustScacheLRU(scp);
258 lock_ReleaseWrite(&scp->rw);
260 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
264 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
269 /* if we get here, we should allocate a new scache entry. We either are below
270 * quota or we have a leak and need to allocate a new one to avoid panicing.
272 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
273 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
274 "invalid cm_scache_t address");
275 memset(scp, 0, sizeof(cm_scache_t));
276 scp->magic = CM_SCACHE_MAGIC;
277 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
278 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
279 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
281 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
283 scp->serverLock = -1;
285 /* and put it in the LRU queue */
286 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
287 if (!cm_data.scacheLRULastp)
288 cm_data.scacheLRULastp = scp;
289 cm_data.currentSCaches++;
290 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
292 scp->allNextp = cm_data.allSCachesp;
293 cm_data.allSCachesp = scp;
297 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
300 fidp->volume = volume;
302 fidp->unique = unique;
303 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
306 /* like strcmp, only for fids */
307 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
309 if (ap->hash != bp->hash)
311 if (ap->vnode != bp->vnode)
313 if (ap->volume != bp->volume)
315 if (ap->unique != bp->unique)
317 if (ap->cell != bp->cell)
322 void cm_fakeSCacheInit(int newFile)
325 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
326 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
327 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
328 cm_data.fakeSCache.cbExpires = (time_t)-1;
329 /* can leave clientModTime at 0 */
330 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
331 cm_data.fakeSCache.unixModeBits = 0777;
332 cm_data.fakeSCache.length.LowPart = 1000;
333 cm_data.fakeSCache.linkCount = 1;
334 cm_data.fakeSCache.refCount = 1;
335 cm_data.fakeSCache.serverLock = -1;
336 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
338 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
339 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
340 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
344 cm_ValidateSCache(void)
346 cm_scache_t * scp, *lscp;
349 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
350 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
351 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
352 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
356 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
358 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
359 if (scp->magic != CM_SCACHE_MAGIC) {
360 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
361 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
364 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
365 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
366 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
369 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
370 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
371 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
374 if (i > cm_data.currentSCaches ) {
375 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
376 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
379 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
380 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
381 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
386 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
387 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
388 if (scp->magic != CM_SCACHE_MAGIC) {
389 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
390 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
393 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
394 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
395 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
398 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
399 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
400 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
403 if (i > cm_data.currentSCaches ) {
404 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
405 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
408 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
409 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
410 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
415 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
416 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
418 hash = CM_SCACHE_HASH(&scp->fid);
419 if (scp->magic != CM_SCACHE_MAGIC) {
420 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
421 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
424 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
425 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
426 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
429 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
430 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
431 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
435 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
436 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
442 return cm_dnlcValidate();
446 cm_SuspendSCache(void)
451 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
454 * After this call all servers are marked down.
455 * Do not clear the callbacks, instead change the
456 * expiration time so that the callbacks will be expired
457 * when the servers are marked back up. However, we
458 * want the callbacks to be preserved as long as the
459 * servers are down. That way if the machine resumes
460 * without network, the stat cache item will still be
465 lock_ObtainWrite(&cm_scacheLock);
466 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
467 if (scp->cbServerp) {
468 if (scp->flags & CM_SCACHEFLAG_PURERO) {
469 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
471 if (volp->cbExpiresRO == scp->cbExpires)
472 volp->cbExpiresRO = now+1;
476 scp->cbExpires = now+1;
479 lock_ReleaseWrite(&cm_scacheLock);
483 cm_ShutdownSCache(void)
485 cm_scache_t * scp, * nextp;
487 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
489 lock_ObtainWrite(&cm_scacheLock);
491 for ( scp = cm_data.allSCachesp; scp;
493 nextp = scp->allNextp;
494 lock_ReleaseWrite(&cm_scacheLock);
496 lock_ObtainWrite(&scp->dirlock);
498 lock_ObtainWrite(&scp->rw);
499 lock_ObtainWrite(&cm_scacheLock);
501 if (scp->randomACLp) {
502 cm_FreeAllACLEnts(scp);
505 if (scp->cbServerp) {
506 cm_PutServer(scp->cbServerp);
507 scp->cbServerp = NULL;
510 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
511 lock_ReleaseWrite(&scp->rw);
515 freeBtree(scp->dirBplus);
516 scp->dirBplus = NULL;
517 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
518 lock_ReleaseWrite(&scp->dirlock);
519 lock_FinalizeRWLock(&scp->dirlock);
521 lock_FinalizeRWLock(&scp->rw);
522 lock_FinalizeRWLock(&scp->bufCreateLock);
524 lock_ReleaseWrite(&cm_scacheLock);
526 return cm_dnlcShutdown();
529 void cm_InitSCache(int newFile, long maxSCaches)
531 static osi_once_t once;
533 if (osi_Once(&once)) {
534 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
536 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
537 cm_data.allSCachesp = NULL;
538 cm_data.currentSCaches = 0;
539 cm_data.maxSCaches = maxSCaches;
540 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
544 for ( scp = cm_data.allSCachesp; scp;
545 scp = scp->allNextp ) {
546 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
547 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
549 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
551 scp->cbServerp = NULL;
553 scp->volumeCreationDate = 0;
554 scp->fileLocksH = NULL;
555 scp->fileLocksT = NULL;
556 scp->serverLock = (-1);
557 scp->lastRefreshCycle = 0;
558 scp->exclusiveLocks = 0;
559 scp->sharedLocks = 0;
566 scp->dirBplus = NULL;
567 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
569 scp->waitQueueT = NULL;
570 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
573 cm_allFileLocks = NULL;
574 cm_freeFileLocks = NULL;
575 cm_lockRefreshCycle = 0;
576 cm_fakeSCacheInit(newFile);
577 cm_allFreeWaiters = NULL;
578 cm_dnlcInit(newFile);
583 /* version that doesn't bother creating the entry if we don't find it */
584 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
589 hash = CM_SCACHE_HASH(fidp);
591 if (fidp->cell == 0) {
595 lock_ObtainRead(&cm_scacheLock);
596 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
597 if (cm_FidCmp(fidp, &scp->fid) == 0) {
598 cm_HoldSCacheNoLock(scp);
599 lock_ConvertRToW(&cm_scacheLock);
600 cm_AdjustScacheLRU(scp);
601 lock_ReleaseWrite(&cm_scacheLock);
605 lock_ReleaseRead(&cm_scacheLock);
609 #ifdef DEBUG_REFCOUNT
610 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
611 cm_req_t *reqp, char * file, long line)
613 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
618 cm_scache_t *scp = NULL;
620 cm_volume_t *volp = NULL;
622 int special = 0; // yj: boolean variable to test if file is on root.afs
624 extern cm_fid_t cm_rootFid;
626 hash = CM_SCACHE_HASH(fidp);
629 return CM_ERROR_INVAL;
631 #ifdef AFS_FREELANCE_CLIENT
632 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
633 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
634 !(fidp->vnode==0x1 && fidp->unique==0x1));
635 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
636 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
637 fidp->vnode==0x1 && fidp->unique==0x1);
640 // yj: check if we have the scp, if so, we don't need
641 // to do anything else
642 lock_ObtainWrite(&cm_scacheLock);
643 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
644 if (cm_FidCmp(fidp, &scp->fid) == 0) {
645 #ifdef DEBUG_REFCOUNT
646 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
647 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
649 #ifdef AFS_FREELANCE_CLIENT
650 if (cm_freelanceEnabled && special &&
651 cm_data.fakeDirVersion != scp->dataVersion)
654 cm_HoldSCacheNoLock(scp);
656 cm_AdjustScacheLRU(scp);
657 lock_ReleaseWrite(&cm_scacheLock);
662 // yj: when we get here, it means we don't have an scp
663 // so we need to either load it or fake it, depending
664 // on whether the file is "special", see below.
666 // yj: if we're trying to get an scp for a file that's
667 // on root.afs of homecell, we want to handle it specially
668 // because we have to fill in the status stuff 'coz we
669 // don't want trybulkstat to fill it in for us
670 #ifdef AFS_FREELANCE_CLIENT
671 if (cm_freelanceEnabled && isRoot) {
672 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
673 /* freelance: if we are trying to get the root scp for the first
674 * time, we will just put in a place holder entry.
679 if (cm_freelanceEnabled && special) {
680 lock_ReleaseWrite(&cm_scacheLock);
681 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
683 if (cm_getLocalMountPointChange()) {
684 cm_clearLocalMountPointChange();
685 cm_reInitLocalMountPoints();
688 lock_ObtainWrite(&cm_scacheLock);
690 scp = cm_GetNewSCache(); /* returns scp->rw held */
692 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
693 lock_ReleaseWrite(&cm_scacheLock);
694 return CM_ERROR_WOULDBLOCK;
697 lock_ReleaseWrite(&cm_scacheLock);
698 lock_ObtainWrite(&scp->rw);
699 lock_ObtainWrite(&cm_scacheLock);
702 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
703 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
704 scp->dotdotFid.unique=1;
705 scp->dotdotFid.vnode=1;
706 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
707 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
708 scp->nextp = cm_data.scacheHashTablep[hash];
709 cm_data.scacheHashTablep[hash] = scp;
710 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
713 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
715 /* must be called after the scp->fid is set */
716 cm_FreelanceFetchMountPointString(scp);
717 cm_FreelanceFetchFileType(scp);
719 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
720 scp->length.HighPart = 0;
722 scp->unixModeBits=0777;
723 scp->clientModTime=FakeFreelanceModTime;
724 scp->serverModTime=FakeFreelanceModTime;
725 scp->parentUnique = 0x1;
726 scp->parentVnode=0x1;
728 scp->dataVersion=cm_data.fakeDirVersion;
729 scp->bufDataVersionLow=cm_data.fakeDirVersion;
730 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
732 lock_ReleaseWrite(&scp->rw);
733 lock_ReleaseWrite(&cm_scacheLock);
735 #ifdef DEBUG_REFCOUNT
736 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
737 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
742 #endif /* AFS_FREELANCE_CLIENT */
744 /* otherwise, we need to find the volume */
745 if (!cm_freelanceEnabled || !isRoot) {
746 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
747 cellp = cm_FindCellByID(fidp->cell, 0);
749 return CM_ERROR_NOSUCHCELL;
751 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
754 lock_ObtainWrite(&cm_scacheLock);
757 /* otherwise, we have the volume, now reverify that the scp doesn't
758 * exist, and proceed.
760 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
761 if (cm_FidCmp(fidp, &scp->fid) == 0) {
762 #ifdef DEBUG_REFCOUNT
763 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
764 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
766 cm_HoldSCacheNoLock(scp);
767 cm_AdjustScacheLRU(scp);
768 lock_ReleaseWrite(&cm_scacheLock);
776 /* now, if we don't have the fid, recycle something */
777 scp = cm_GetNewSCache(); /* returns scp->rw held */
779 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
780 lock_ReleaseWrite(&cm_scacheLock);
783 return CM_ERROR_WOULDBLOCK;
785 #ifdef DEBUG_REFCOUNT
786 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
788 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
790 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
793 if (!cm_freelanceEnabled || !isRoot) {
794 /* if this scache entry represents a volume root then we need
795 * to copy the dotdotFipd from the volume structure where the
796 * "master" copy is stored (defect 11489)
798 if (volp->vol[ROVOL].ID == fidp->volume) {
799 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
800 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
801 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
802 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
803 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
804 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
805 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
807 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
808 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
813 scp->nextp = cm_data.scacheHashTablep[hash];
814 cm_data.scacheHashTablep[hash] = scp;
815 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
816 lock_ReleaseWrite(&scp->rw);
818 #ifdef DEBUG_REFCOUNT
819 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
821 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
823 /* XXX - The following fields in the cm_scache are
830 /* now we have a held scache entry; just return it */
832 #ifdef DEBUG_REFCOUNT
833 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
834 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
836 lock_ReleaseWrite(&cm_scacheLock);
840 /* Returns a held reference to the scache's parent
842 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
847 cm_scache_t * pscp = NULL;
849 lock_ObtainWrite(&cm_scacheLock);
850 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
852 if (cm_FidCmp(&scp->fid, &parent_fid)) {
853 i = CM_SCACHE_HASH(&parent_fid);
854 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
855 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
856 cm_HoldSCacheNoLock(pscp);
862 lock_ReleaseWrite(&cm_scacheLock);
867 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
869 cm_scache_waiter_t * w;
871 lock_ObtainWrite(&cm_scacheLock);
872 if (cm_allFreeWaiters == NULL) {
873 w = malloc(sizeof(*w));
874 memset(w, 0, sizeof(*w));
876 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
877 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
880 w->threadId = thrd_Current();
882 cm_HoldSCacheNoLock(scp);
886 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
887 lock_ReleaseWrite(&cm_scacheLock);
889 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
892 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
894 cm_scache_waiter_t * w;
897 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
899 lock_ObtainRead(&cm_scacheLock);
900 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
902 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
903 if (w->flags == flags && w->bufp == bufp) {
908 osi_assertx(w != NULL, "null cm_scache_waiter_t");
909 this_is_me = (w->threadId == thrd_Current());
910 lock_ReleaseRead(&cm_scacheLock);
913 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
917 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
919 lock_ObtainWrite(&cm_scacheLock);
920 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
921 cm_ReleaseSCacheNoLock(scp);
922 memset(w, 0, sizeof(*w));
923 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
924 lock_ReleaseWrite(&cm_scacheLock);
930 /* synchronize a fetch, store, read, write, fetch status or store status.
931 * Called with scache mutex held, and returns with it held, but temporarily
932 * drops it during the fetch.
934 * At most one flag can be on in flags, if this is an RPC request.
936 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
938 * There are a lot of weird restrictions here; here's an attempt to explain the
939 * rationale for the concurrency restrictions implemented in this function.
941 * First, although the file server will break callbacks when *another* machine
942 * modifies a file or status block, the client itself is responsible for
943 * concurrency control on its own requests. Callback breaking events are rare,
944 * and simply invalidate any concurrent new status info.
946 * In the absence of callback breaking messages, we need to know how to
947 * synchronize incoming responses describing updates to files. We synchronize
948 * operations that update the data version by comparing the data versions.
949 * However, updates that do not update the data, but only the status, can't be
950 * synchronized with fetches or stores, since there's nothing to compare
951 * to tell which operation executed first at the server.
953 * Thus, we can allow multiple ops that change file data, or dir data, and
954 * fetches. However, status storing ops have to be done serially.
956 * Furthermore, certain data-changing ops are incompatible: we can't read or
957 * write a buffer while doing a truncate. We can't read and write the same
958 * buffer at the same time, or write while fetching or storing, or read while
959 * fetching a buffer (this may change). We can't fetch and store at the same
962 * With respect to status, we can't read and write at the same time, read while
963 * fetching, write while fetching or storing, or fetch and store at the same time.
965 * We can't allow a get callback RPC to run in concurrently with something that
966 * will return updated status, since we could start a call, have the server
967 * return status, have another machine make an update to the status (which
968 * doesn't change serverModTime), have the original machine get a new callback,
969 * and then have the original machine merge in the early, old info from the
970 * first call. At this point, the easiest way to avoid this problem is to have
971 * getcallback calls conflict with all others for the same vnode. Other calls
972 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
973 * vnode must be careful not to merge in their status unless they have obtained
974 * a callback from the start of their call.
977 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
978 * Each such RPC passes a FileLength parameter, which the server uses to do
979 * pre-truncation if necessary. So if two RPC's are processed out of order at
980 * the server, the one with the smaller FileLength will be processed last,
981 * possibly resulting in a bogus truncation. The simplest way to avoid this
982 * is to serialize all StoreData RPC's. This is the reason we defined
983 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
985 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
986 afs_uint32 rights, afs_uint32 flags)
988 osi_queueData_t *qdp;
991 afs_uint32 outRights;
993 afs_uint32 sleep_scp_flags = 0;
994 afs_uint32 sleep_buf_cmflags = 0;
995 afs_uint32 sleep_scp_bufs = 0;
998 lock_AssertWrite(&scp->rw);
1000 /* lookup this first */
1001 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1004 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1007 /* Do the access check. Now we don't really do the access check
1008 * atomically, since the caller doesn't expect the parent dir to be
1009 * returned locked, and that is what we'd have to do to prevent a
1010 * callback breaking message on the parent due to a setacl call from
1011 * being processed while we're running. So, instead, we check things
1012 * here, and if things look fine with the access, we proceed to finish
1013 * the rest of this check. Sort of a hack, but probably good enough.
1017 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1018 /* if we're bringing in a new status block, ensure that
1019 * we aren't already doing so, and that no one is
1020 * changing the status concurrently, either. We need
1021 * to do this, even if the status is of a different
1022 * type, since we don't have the ability to figure out,
1023 * in the AFS 3 protocols, which status-changing
1024 * operation ran first, or even which order a read and
1025 * a write occurred in.
1027 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1028 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1029 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1033 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1034 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1035 /* if we're going to make an RPC to change the status, make sure
1036 * that no one is bringing in or sending out the status.
1038 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1039 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1040 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1043 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1044 (scp->bufReadsp || scp->bufWritesp)) {
1045 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1049 if (flags & CM_SCACHESYNC_FETCHDATA) {
1050 /* if we're bringing in a new chunk of data, make sure that
1051 * nothing is happening to that chunk, and that we aren't
1052 * changing the basic file status info, either.
1054 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1055 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1056 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1059 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1060 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1064 if (flags & CM_SCACHESYNC_STOREDATA) {
1065 /* same as fetch data */
1066 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1067 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1068 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1071 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1072 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1077 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1078 /* Don't allow concurrent StoreData RPC's */
1079 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1080 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1085 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1086 /* Don't allow more than one BKG store request */
1087 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1088 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1093 if (flags & CM_SCACHESYNC_LOCK) {
1094 /* Don't allow concurrent fiddling with lock lists */
1095 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1096 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1101 /* now the operations that don't correspond to making RPCs */
1102 if (flags & CM_SCACHESYNC_GETSTATUS) {
1103 /* we can use the status that's here, if we're not
1104 * bringing in new status.
1106 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1107 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1111 if (flags & CM_SCACHESYNC_SETSTATUS) {
1112 /* we can make a change to the local status, as long as
1113 * the status isn't changing now.
1115 * If we're fetching or storing a chunk of data, we can
1116 * change the status locally, since the fetch/store
1117 * operations don't change any of the data that we're
1120 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1121 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1122 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1126 if (flags & CM_SCACHESYNC_READ) {
1127 /* we're going to read the data, make sure that the
1128 * status is available, and that the data is here. It
1129 * is OK to read while storing the data back.
1131 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1132 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1135 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1136 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1139 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1140 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1144 if (flags & CM_SCACHESYNC_WRITE) {
1145 /* don't write unless the status is stable and the chunk
1148 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1149 CM_SCACHEFLAG_SIZESTORING)) {
1150 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1153 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1155 CM_BUF_CMWRITING))) {
1156 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1158 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1159 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1160 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1166 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1167 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1168 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1171 lock_ReleaseMutex(&bufp->mx);
1172 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1174 lock_ReleaseWrite(&scp->rw);
1175 lock_ObtainMutex(&bufp->mx);
1176 lock_ObtainWrite(&scp->rw);
1180 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1186 /* can't check access rights without a callback */
1187 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1189 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1190 return CM_ERROR_READONLY;
1192 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1193 if (~outRights & rights)
1194 return CM_ERROR_NOACCESS;
1197 /* we don't know the required access rights */
1198 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1199 code = cm_GetAccessRights(scp, userp, reqp);
1201 lock_ReleaseWrite(&scp->rw);
1202 lock_ObtainMutex(&bufp->mx);
1203 lock_ObtainWrite(&scp->rw);
1211 /* if we get here, we're happy */
1215 /* first check if we're not supposed to wait: fail
1216 * in this case, returning with everything still locked.
1218 if (flags & CM_SCACHESYNC_NOWAIT)
1219 return CM_ERROR_WOULDBLOCK;
1221 /* These are used for minidump debugging */
1222 sleep_scp_flags = scp->flags; /* so we know why we slept */
1223 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1224 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1226 /* wait here, then try again */
1227 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1228 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1230 scp->waitRequests++;
1231 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1232 scp, scp->waitCount, scp->waitRequests);
1234 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1235 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1236 scp->waitCount = scp->waitRequests = 1;
1239 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1243 lock_ReleaseMutex(&bufp->mx);
1244 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1246 lock_ObtainMutex(&bufp->mx);
1247 lock_ObtainWrite(&scp->rw);
1248 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1250 cm_UpdateServerPriority();
1253 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1254 scp, scp->waitCount, scp->waitRequests);
1255 if (scp->waitCount == 0) {
1256 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1257 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1258 scp->waitRequests = 0;
1260 } /* big while loop */
1262 /* now, update the recorded state for RPC-type calls */
1263 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1264 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1265 if (flags & CM_SCACHESYNC_STORESTATUS)
1266 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1267 if (flags & CM_SCACHESYNC_SETSIZE)
1268 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1269 if (flags & CM_SCACHESYNC_STORESIZE)
1270 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1271 if (flags & CM_SCACHESYNC_GETCALLBACK)
1272 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1273 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1274 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1275 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1276 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1277 if (flags & CM_SCACHESYNC_LOCK)
1278 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1280 /* now update the buffer pointer */
1281 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1282 /* ensure that the buffer isn't already in the I/O list */
1283 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1284 tbufp = osi_GetQData(qdp);
1285 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1288 /* queue a held reference to the buffer in the "reading" I/O list */
1289 qdp = osi_QDAlloc();
1290 osi_SetQData(qdp, bufp);
1293 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1294 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1297 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1298 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1299 "attempting to store extents on a non-file object");
1301 /* ensure that the buffer isn't already in the I/O list */
1302 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1303 tbufp = osi_GetQData(qdp);
1304 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1307 /* queue a held reference to the buffer in the "writing" I/O list */
1308 qdp = osi_QDAlloc();
1309 osi_SetQData(qdp, bufp);
1311 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1312 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1315 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1316 /* mark the buffer as being written to. */
1317 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1323 /* for those syncops that setup for RPCs.
1324 * Called with scache locked.
1326 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1328 osi_queueData_t *qdp;
1331 lock_AssertWrite(&scp->rw);
1333 /* now, update the recorded state for RPC-type calls */
1334 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1335 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1336 if (flags & CM_SCACHESYNC_STORESTATUS)
1337 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1338 if (flags & CM_SCACHESYNC_SETSIZE)
1339 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1340 if (flags & CM_SCACHESYNC_STORESIZE)
1341 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1342 if (flags & CM_SCACHESYNC_GETCALLBACK)
1343 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1344 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1345 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1346 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1347 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1348 if (flags & CM_SCACHESYNC_LOCK)
1349 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1351 /* now update the buffer pointer */
1352 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1355 /* ensure that the buffer is in the I/O list */
1356 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1357 tbufp = osi_GetQData(qdp);
1362 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1366 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1367 if (bufp->flags & CM_BUF_WAITING) {
1368 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1369 osi_Wakeup((LONG_PTR) &bufp);
1375 /* now update the buffer pointer */
1376 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1378 /* ensure that the buffer is in the I/O list */
1379 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1380 tbufp = osi_GetQData(qdp);
1385 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1389 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1390 if (bufp->flags & CM_BUF_WAITING) {
1391 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1392 osi_Wakeup((LONG_PTR) &bufp);
1398 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1399 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1400 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1403 /* and wakeup anyone who is waiting */
1404 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1405 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1406 osi_Wakeup((LONG_PTR) &scp->flags);
1410 /* merge in a response from an RPC. The scp must be locked, and the callback
1413 * Don't overwrite any status info that is dirty, since we could have a store
1414 * operation (such as store data) that merges some info in, and we don't want
1415 * to lose the local updates. Typically, there aren't many updates we do
1416 * locally, anyway, probably only mtime.
1418 * There is probably a bug in here where a chmod (which doesn't change
1419 * serverModTime) that occurs between two fetches, both of whose responses are
1420 * handled after the callback breaking is done, but only one of whose calls
1421 * started before that, can cause old info to be merged from the first call.
1423 void cm_MergeStatus(cm_scache_t *dscp,
1424 cm_scache_t *scp, AFSFetchStatus *statusp,
1425 AFSVolSync *volsyncp,
1426 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1428 afs_uint64 dataVersion;
1429 struct cm_volume *volp = NULL;
1430 struct cm_cell *cellp = NULL;
1432 lock_AssertWrite(&scp->rw);
1434 // yj: i want to create some fake status for the /afs directory and the
1435 // entries under that directory
1436 #ifdef AFS_FREELANCE_CLIENT
1437 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1438 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1439 if (scp == cm_data.rootSCachep) {
1440 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1441 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1442 statusp->Length = cm_fakeDirSize;
1443 statusp->Length_hi = 0;
1445 statusp->FileType = scp->fileType;
1446 statusp->Length = scp->length.LowPart;
1447 statusp->Length_hi = scp->length.HighPart;
1449 statusp->InterfaceVersion = 0x1;
1450 statusp->LinkCount = scp->linkCount;
1451 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1452 statusp->Author = 0x1;
1453 statusp->Owner = 0x0;
1454 statusp->CallerAccess = 0x9;
1455 statusp->AnonymousAccess = 0x9;
1456 statusp->UnixModeBits = 0777;
1457 statusp->ParentVnode = 0x1;
1458 statusp->ParentUnique = 0x1;
1459 statusp->ResidencyMask = 0;
1460 statusp->ClientModTime = FakeFreelanceModTime;
1461 statusp->ServerModTime = FakeFreelanceModTime;
1463 statusp->SyncCounter = 0;
1464 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1465 statusp->lockCount = 0;
1466 statusp->errorCode = 0;
1468 #endif /* AFS_FREELANCE_CLIENT */
1470 if (statusp->errorCode != 0) {
1471 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1472 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1474 scp->fileType = 0; /* unknown */
1476 scp->serverModTime = 0;
1477 scp->clientModTime = 0;
1478 scp->length.LowPart = 0;
1479 scp->length.HighPart = 0;
1480 scp->serverLength.LowPart = 0;
1481 scp->serverLength.HighPart = 0;
1485 scp->unixModeBits = 0;
1487 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1488 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1489 scp->fsLockCount = 0;
1492 scp->parentVnode = dscp->fid.vnode;
1493 scp->parentUnique = dscp->fid.unique;
1495 scp->parentVnode = 0;
1496 scp->parentUnique = 0;
1500 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1503 dataVersion = statusp->dataVersionHigh;
1505 dataVersion |= statusp->DataVersion;
1507 if (!(flags & CM_MERGEFLAG_FORCE) &&
1508 dataVersion < scp->dataVersion &&
1509 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1511 cellp = cm_FindCellByID(scp->fid.cell, 0);
1512 if (scp->cbServerp) {
1513 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1514 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1515 osi_Log2(afsd_logp, "old data from server %x volume %s",
1516 scp->cbServerp->addr.sin_addr.s_addr,
1517 volp ? volp->namep : "(unknown)");
1519 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1520 scp, scp->dataVersion, dataVersion);
1521 /* we have a number of data fetch/store operations running
1522 * concurrently, and we can tell which one executed last at the
1523 * server by its mtime.
1524 * Choose the one with the largest mtime, and ignore the rest.
1526 * These concurrent calls are incompatible with setting the
1527 * mtime, so we won't have a locally changed mtime here.
1529 * We could also have ACL info for a different user than usual,
1530 * in which case we have to do that part of the merge, anyway.
1531 * We won't have to worry about the info being old, since we
1532 * won't have concurrent calls
1533 * that change file status running from this machine.
1535 * Added 3/17/98: if we see data version regression on an RO
1536 * file, it's probably due to a server holding an out-of-date
1537 * replica, rather than to concurrent RPC's. Failures to
1538 * release replicas are now flagged by the volserver, but only
1539 * since AFS 3.4 5.22, so there are plenty of clients getting
1540 * out-of-date replicas out there.
1542 * If we discover an out-of-date replica, by this time it's too
1543 * late to go to another server and retry. Also, we can't
1544 * reject the merge, because then there is no way for
1545 * GetAccess to do its work, and the caller gets into an
1546 * infinite loop. So we just grin and bear it.
1548 if (!(scp->flags & CM_SCACHEFLAG_RO))
1552 if (cm_readonlyVolumeVersioning)
1553 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1555 scp->serverModTime = statusp->ServerModTime;
1557 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1558 scp->clientModTime = statusp->ClientModTime;
1560 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1561 scp->length.LowPart = statusp->Length;
1562 scp->length.HighPart = statusp->Length_hi;
1565 scp->serverLength.LowPart = statusp->Length;
1566 scp->serverLength.HighPart = statusp->Length_hi;
1568 scp->linkCount = statusp->LinkCount;
1569 scp->owner = statusp->Owner;
1570 scp->group = statusp->Group;
1571 scp->unixModeBits = statusp->UnixModeBits & 07777;
1573 if (statusp->FileType == File)
1574 scp->fileType = CM_SCACHETYPE_FILE;
1575 else if (statusp->FileType == Directory)
1576 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1577 else if (statusp->FileType == SymbolicLink) {
1578 if ((scp->unixModeBits & 0111) == 0)
1579 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1581 scp->fileType = CM_SCACHETYPE_SYMLINK;
1584 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1585 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1587 /* and other stuff */
1588 scp->parentVnode = statusp->ParentVnode;
1589 scp->parentUnique = statusp->ParentUnique;
1591 /* -1 is a write lock; any positive values are read locks */
1592 scp->fsLockCount = (afs_int32)statusp->lockCount;
1594 /* and merge in the private acl cache info, if this is more than the public
1595 * info; merge in the public stuff in any case.
1597 scp->anyAccess = statusp->AnonymousAccess;
1599 if (userp != NULL) {
1600 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1603 if (scp->dataVersion != 0 &&
1604 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1605 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1607 * We now know that all of the data buffers that we have associated
1608 * with this scp are invalid. Subsequent operations will go faster
1609 * if the buffers are removed from the hash tables.
1611 * We do not remove directory buffers if the dataVersion delta is 1 because
1612 * those version numbers will be updated as part of the directory operation.
1614 * We do not remove storedata buffers because they will still be valid.
1619 cm_buf_t *bp, *prevBp, *nextBp;
1621 lock_ObtainWrite(&buf_globalLock);
1622 i = BUF_FILEHASH(&scp->fid);
1623 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1625 nextBp = bp->fileHashp;
1627 * if the buffer belongs to this stat cache entry
1628 * and the buffer mutex can be obtained, check the
1629 * reference count and if it is zero, remove the buffer
1630 * from the hash tables. If there are references,
1631 * the buffer might be updated to the current version
1632 * so leave it in place.
1634 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1635 lock_TryMutex(&bp->mx)) {
1636 if (bp->refCount == 0 &&
1637 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1638 prevBp = bp->fileHashBackp;
1639 bp->fileHashBackp = bp->fileHashp = NULL;
1641 prevBp->fileHashp = nextBp;
1643 cm_data.buf_fileHashTablepp[i] = nextBp;
1645 nextBp->fileHashBackp = prevBp;
1647 j = BUF_HASH(&bp->fid, &bp->offset);
1648 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1649 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1654 /* we better find it */
1655 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1657 *lbpp = bp->hashp; /* hash out */
1660 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1662 lock_ReleaseMutex(&bp->mx);
1665 lock_ReleaseWrite(&buf_globalLock);
1669 * If the dataVersion has changed, the mountPointStringp must be cleared
1670 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1671 * does not update a mountpoint or symlink by altering the contents of
1672 * the file data; but the Unix CM does.
1674 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA))
1675 scp->mountPointStringp[0] = '\0';
1677 /* We maintain a range of buffer dataVersion values which are considered
1678 * valid. This avoids the need to update the dataVersion on each buffer
1679 * object during an uncontested storeData operation. As a result this
1680 * merge status no longer has performance characteristics derived from
1681 * the size of the file.
1683 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1684 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1685 scp->bufDataVersionLow == 0)
1686 scp->bufDataVersionLow = dataVersion;
1688 scp->dataVersion = dataVersion;
1691 * If someone is waiting for status information, we can wake them up
1692 * now even though the entity that issued the FetchStatus may not
1693 * have completed yet.
1695 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1698 * We just successfully merged status on the stat cache object.
1699 * This means that the associated volume must be online.
1703 cellp = cm_FindCellByID(scp->fid.cell, 0);
1704 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1707 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1708 if (statep->state != vl_online) {
1709 lock_ObtainWrite(&volp->rw);
1710 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1711 statep->state = vl_online;
1712 lock_ReleaseWrite(&volp->rw);
1721 /* note that our stat cache info is incorrect, so force us eventually
1722 * to stat the file again. There may be dirty data associated with
1723 * this vnode, and we want to preserve that information.
1725 * This function works by simply simulating a loss of the callback.
1727 * This function must be called with the scache locked.
1729 void cm_DiscardSCache(cm_scache_t *scp)
1731 lock_AssertWrite(&scp->rw);
1732 if (scp->cbServerp) {
1733 cm_PutServer(scp->cbServerp);
1734 scp->cbServerp = NULL;
1737 scp->volumeCreationDate = 0;
1738 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL));
1739 cm_dnlcPurgedp(scp);
1740 cm_dnlcPurgevp(scp);
1741 cm_FreeAllACLEnts(scp);
1743 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1744 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1746 /* Force mount points and symlinks to be re-evaluated */
1747 scp->mountPointStringp[0] = '\0';
1750 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1752 afsFidp->Volume = fidp->volume;
1753 afsFidp->Vnode = fidp->vnode;
1754 afsFidp->Unique = fidp->unique;
1757 #ifdef DEBUG_REFCOUNT
1758 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1760 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1765 osi_assertx(scp != NULL, "null cm_scache_t");
1766 lock_AssertAny(&cm_scacheLock);
1767 refCount = InterlockedIncrement(&scp->refCount);
1768 #ifdef DEBUG_REFCOUNT
1769 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1770 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1774 #ifdef DEBUG_REFCOUNT
1775 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1777 void cm_HoldSCache(cm_scache_t *scp)
1782 osi_assertx(scp != NULL, "null cm_scache_t");
1783 lock_ObtainRead(&cm_scacheLock);
1784 refCount = InterlockedIncrement(&scp->refCount);
1785 #ifdef DEBUG_REFCOUNT
1786 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1787 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1789 lock_ReleaseRead(&cm_scacheLock);
1792 #ifdef DEBUG_REFCOUNT
1793 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1795 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1800 osi_assertx(scp != NULL, "null cm_scache_t");
1801 lock_AssertAny(&cm_scacheLock);
1803 refCount = InterlockedDecrement(&scp->refCount);
1804 #ifdef DEBUG_REFCOUNT
1806 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1808 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1809 #ifdef DEBUG_REFCOUNT
1810 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1811 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1814 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1818 lockstate = lock_GetRWLockState(&cm_scacheLock);
1819 if (lockstate != OSI_RWLOCK_WRITEHELD)
1820 lock_ReleaseRead(&cm_scacheLock);
1822 lock_ReleaseWrite(&cm_scacheLock);
1824 lock_ObtainWrite(&scp->rw);
1825 if (scp->flags & CM_SCACHEFLAG_DELETED)
1828 if (refCount == 0 && deleted) {
1829 lock_ObtainWrite(&cm_scacheLock);
1830 cm_RecycleSCache(scp, 0);
1831 if (lockstate != OSI_RWLOCK_WRITEHELD)
1832 lock_ConvertWToR(&cm_scacheLock);
1834 if (lockstate != OSI_RWLOCK_WRITEHELD)
1835 lock_ObtainRead(&cm_scacheLock);
1837 lock_ObtainWrite(&cm_scacheLock);
1839 lock_ReleaseWrite(&scp->rw);
1843 #ifdef DEBUG_REFCOUNT
1844 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1846 void cm_ReleaseSCache(cm_scache_t *scp)
1851 osi_assertx(scp != NULL, "null cm_scache_t");
1852 lock_ObtainRead(&cm_scacheLock);
1853 refCount = InterlockedDecrement(&scp->refCount);
1854 #ifdef DEBUG_REFCOUNT
1856 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1858 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1859 #ifdef DEBUG_REFCOUNT
1860 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1861 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1863 lock_ReleaseRead(&cm_scacheLock);
1865 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1867 lock_ObtainWrite(&scp->rw);
1868 if (scp->flags & CM_SCACHEFLAG_DELETED)
1871 lock_ObtainWrite(&cm_scacheLock);
1872 cm_RecycleSCache(scp, 0);
1873 lock_ReleaseWrite(&cm_scacheLock);
1875 lock_ReleaseWrite(&scp->rw);
1879 /* just look for the scp entry to get filetype */
1880 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1881 int cm_FindFileType(cm_fid_t *fidp)
1886 hash = CM_SCACHE_HASH(fidp);
1888 osi_assertx(fidp->cell != 0, "unassigned cell value");
1890 lock_ObtainWrite(&cm_scacheLock);
1891 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1892 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1893 lock_ReleaseWrite(&cm_scacheLock);
1894 return scp->fileType;
1897 lock_ReleaseWrite(&cm_scacheLock);
1901 /* dump all scp's that have reference count > 0 to a file.
1902 * cookie is used to identify this batch for easy parsing,
1903 * and it a string provided by a caller
1905 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1914 lock_ObtainRead(&cm_scacheLock);
1916 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1917 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1919 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1922 char *srvStr = NULL;
1923 afs_uint32 srvStrRpc = TRUE;
1927 if (scp->cbServerp) {
1928 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
1929 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
1930 srvStr = malloc(16); /* enough for 255.255.255.255 */
1932 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
1936 if (scp->cbExpires) {
1941 cbt[strlen(cbt)-1] = '\0';
1944 if (scp->volumeCreationDate) {
1945 t = scp->volumeCreationDate;
1948 cdrot = strdup(cdrot);
1949 cdrot[strlen(cdrot)-1] = '\0';
1953 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
1954 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
1955 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
1956 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1957 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
1958 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
1959 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
1960 cdrot ? cdrot : "<none>", scp->refCount);
1961 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1963 if (scp->fileLocksH) {
1964 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
1965 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1967 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
1968 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
1969 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
1970 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
1971 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
1972 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
1973 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1976 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
1977 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1982 RpcStringFree(&srvStr);
1992 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1993 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1994 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
1995 cookie, cm_data.scacheHashTableSize);
1996 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1998 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2000 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2002 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2003 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2004 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2008 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2009 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2011 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2012 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2014 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2015 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2016 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",
2017 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2018 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2019 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2022 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2023 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2026 lock_ReleaseRead(&cm_scacheLock);