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 <afs/param.h>
24 /*extern void afsi_log(char *pattern, ...);*/
26 extern osi_hyper_t hzero;
29 osi_queue_t *cm_allFileLocks;
30 osi_queue_t *cm_freeFileLocks;
31 unsigned long cm_lockRefreshCycle;
33 /* lock for globals */
34 osi_rwlock_t cm_scacheLock;
36 /* Dummy scache entry for use with pioctl fids */
37 cm_scache_t cm_fakeSCache;
39 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
41 #ifdef AFS_FREELANCE_CLIENT
42 extern osi_mutex_t cm_Freelance_Lock;
45 /* must be called with cm_scacheLock write-locked! */
46 void cm_AdjustScacheLRU(cm_scache_t *scp)
48 lock_AssertWrite(&cm_scacheLock);
49 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
50 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
53 /* call with cm_scacheLock write-locked and scp rw held */
54 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
60 lock_AssertWrite(&cm_scacheLock);
61 lock_AssertWrite(&scp->rw);
62 if (scp->flags & CM_SCACHEFLAG_INHASH) {
63 /* hash it out first */
64 i = CM_SCACHE_HASH(&scp->fid);
65 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
67 lscpp = &tscp->nextp, tscp = tscp->nextp) {
71 scp->flags &= ~CM_SCACHEFLAG_INHASH;
78 /* called with cm_scacheLock and scp write-locked */
79 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
82 /* destroy directory Bplus Tree */
84 LARGE_INTEGER start, end;
86 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
88 * We are not holding the dirlock and obtaining it
89 * requires that we drop the scp->rw. As a result
90 * we will leave the dirBplus tree intact but
91 * invalidate the version number so that whatever
92 * operation is currently active can safely complete
93 * but the contents will be ignored on the next
94 * directory operation.
96 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
100 QueryPerformanceCounter(&start);
102 freeBtree(scp->dirBplus);
103 scp->dirBplus = NULL;
104 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
105 QueryPerformanceCounter(&end);
108 lock_ReleaseWrite(&scp->dirlock);
110 bplus_free_time += (end.QuadPart - start.QuadPart);
115 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
116 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
118 if (scp->refCount != 0) {
122 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
123 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
125 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
130 cm_RemoveSCacheFromHashTable(scp);
133 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
134 osi_queueData_t *qdp;
137 while(qdp = scp->bufWritesp) {
138 bufp = osi_GetQData(qdp);
139 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
142 lock_ObtainMutex(&bufp->mx);
143 bufp->cmFlags &= ~CM_BUF_CMSTORING;
144 bufp->flags &= ~CM_BUF_DIRTY;
145 bufp->dirty_offset = 0;
146 bufp->dirty_length = 0;
147 bufp->flags |= CM_BUF_ERROR;
148 bufp->error = VNOVNODE;
149 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
150 bufp->dirtyCounter++;
151 if (bufp->flags & CM_BUF_WAITING) {
152 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
153 osi_Wakeup((long) &bufp);
155 lock_ReleaseMutex(&bufp->mx);
159 while(qdp = scp->bufReadsp) {
160 bufp = osi_GetQData(qdp);
161 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
164 lock_ObtainMutex(&bufp->mx);
165 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
166 bufp->flags &= ~CM_BUF_DIRTY;
167 bufp->dirty_offset = 0;
168 bufp->dirty_length = 0;
169 bufp->flags |= CM_BUF_ERROR;
170 bufp->error = VNOVNODE;
171 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
172 bufp->dirtyCounter++;
173 if (bufp->flags & CM_BUF_WAITING) {
174 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
175 osi_Wakeup((long) &bufp);
177 lock_ReleaseMutex(&bufp->mx);
181 buf_CleanDirtyBuffers(scp);
183 /* look for things that shouldn't still be set */
184 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
185 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
189 /* invalidate so next merge works fine;
190 * also initialize some flags */
192 scp->flags &= ~(CM_SCACHEFLAG_STATD
193 | CM_SCACHEFLAG_DELETED
195 | CM_SCACHEFLAG_PURERO
196 | CM_SCACHEFLAG_OVERQUOTA
197 | CM_SCACHEFLAG_OUTOFSPACE
198 | CM_SCACHEFLAG_EACCESS);
199 scp->serverModTime = 0;
200 scp->dataVersion = CM_SCACHE_VERSION_BAD;
201 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
202 scp->bulkStatProgress = hzero;
204 scp->waitQueueT = NULL;
206 if (scp->cbServerp) {
207 cm_PutServer(scp->cbServerp);
208 scp->cbServerp = NULL;
211 scp->volumeCreationDate = 0;
219 /* remove from dnlc */
223 /* discard cached status; if non-zero, Close
224 * tried to store this to server but failed */
227 /* discard symlink info */
228 scp->mountPointStringp[0] = '\0';
229 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
230 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
232 /* reset locking info */
233 scp->fileLocksH = NULL;
234 scp->fileLocksT = NULL;
235 scp->serverLock = (-1);
236 scp->exclusiveLocks = 0;
237 scp->sharedLocks = 0;
238 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
239 scp->fsLockCount = 0;
241 /* not locked, but there can be no references to this guy
242 * while we hold the global refcount lock.
244 cm_FreeAllACLEnts(scp);
246 cm_ResetSCacheDirectory(scp, 0);
252 * called with cm_scacheLock write-locked; find a vnode to recycle.
253 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
254 * returns scp->mx held.
256 cm_scache_t *cm_GetNewSCache(void)
261 lock_AssertWrite(&cm_scacheLock);
263 /* first pass - look for deleted objects */
264 for ( scp = cm_data.scacheLRULastp;
266 scp = (cm_scache_t *) osi_QPrev(&scp->q))
268 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
269 "invalid cm_scache_t address");
271 if (scp->refCount == 0) {
272 if (scp->flags & CM_SCACHEFLAG_DELETED) {
273 if (!lock_TryWrite(&scp->rw))
276 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
277 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
279 /* we found an entry, so return it */
280 /* now remove from the LRU queue and put it back at the
281 * head of the LRU queue.
283 cm_AdjustScacheLRU(scp);
288 lock_ReleaseWrite(&scp->rw);
289 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
290 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
291 if (!lock_TryWrite(&scp->rw))
294 /* we found an entry, so return it */
295 /* now remove from the LRU queue and put it back at the
296 * head of the LRU queue.
298 cm_AdjustScacheLRU(scp);
305 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
308 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
309 /* There were no deleted scache objects that we could use. Try to find
310 * one that simply hasn't been used in a while.
312 for ( scp = cm_data.scacheLRULastp;
314 scp = (cm_scache_t *) osi_QPrev(&scp->q))
316 /* It is possible for the refCount to be zero and for there still
317 * to be outstanding dirty buffers. If there are dirty buffers,
318 * we must not recycle the scp. */
319 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
320 if (!buf_DirtyBuffersExist(&scp->fid)) {
321 if (!lock_TryWrite(&scp->rw))
324 if (!cm_RecycleSCache(scp, 0)) {
325 /* we found an entry, so return it */
326 /* now remove from the LRU queue and put it back at the
327 * head of the LRU queue.
329 cm_AdjustScacheLRU(scp);
334 lock_ReleaseWrite(&scp->rw);
336 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
340 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
345 /* if we get here, we should allocate a new scache entry. We either are below
346 * quota or we have a leak and need to allocate a new one to avoid panicing.
348 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
349 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
350 "invalid cm_scache_t address");
351 memset(scp, 0, sizeof(cm_scache_t));
352 scp->magic = CM_SCACHE_MAGIC;
353 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
354 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
355 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
357 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
359 scp->serverLock = -1;
361 /* and put it in the LRU queue */
362 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
363 if (!cm_data.scacheLRULastp)
364 cm_data.scacheLRULastp = scp;
365 cm_data.currentSCaches++;
366 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
368 scp->allNextp = cm_data.allSCachesp;
369 cm_data.allSCachesp = scp;
373 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
376 fidp->volume = volume;
378 fidp->unique = unique;
379 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
382 /* like strcmp, only for fids */
383 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
385 if (ap->hash != bp->hash)
387 if (ap->vnode != bp->vnode)
389 if (ap->volume != bp->volume)
391 if (ap->unique != bp->unique)
393 if (ap->cell != bp->cell)
398 void cm_fakeSCacheInit(int newFile)
401 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
402 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
403 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
404 cm_data.fakeSCache.cbExpires = (time_t)-1;
405 /* can leave clientModTime at 0 */
406 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
407 cm_data.fakeSCache.unixModeBits = 0777;
408 cm_data.fakeSCache.length.LowPart = 1000;
409 cm_data.fakeSCache.linkCount = 1;
410 cm_data.fakeSCache.refCount = 1;
411 cm_data.fakeSCache.serverLock = -1;
412 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
414 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
415 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
416 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
420 cm_ValidateSCache(void)
422 cm_scache_t * scp, *lscp;
425 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
426 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
427 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
428 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
432 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
434 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
435 if (scp->magic != CM_SCACHE_MAGIC) {
436 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
437 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
440 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
441 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
442 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
445 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
446 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
447 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
450 if (i > cm_data.currentSCaches ) {
451 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
452 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
455 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
456 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
457 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
462 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
463 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
464 if (scp->magic != CM_SCACHE_MAGIC) {
465 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
466 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
469 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
470 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
471 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
474 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
475 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
476 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
479 if (i > cm_data.currentSCaches ) {
480 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
481 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
484 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
485 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
486 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
491 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
492 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
494 hash = CM_SCACHE_HASH(&scp->fid);
495 if (scp->magic != CM_SCACHE_MAGIC) {
496 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
497 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
500 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
501 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
502 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
505 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
506 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
507 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
511 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
512 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
518 return cm_dnlcValidate();
522 cm_SuspendSCache(void)
527 cm_GiveUpAllCallbacksAllServers(TRUE);
530 * After this call all servers are marked down.
531 * Do not clear the callbacks, instead change the
532 * expiration time so that the callbacks will be expired
533 * when the servers are marked back up. However, we
534 * want the callbacks to be preserved as long as the
535 * servers are down. That way if the machine resumes
536 * without network, the stat cache item will still be
541 lock_ObtainWrite(&cm_scacheLock);
542 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
543 if (scp->cbServerp) {
544 if (scp->flags & CM_SCACHEFLAG_PURERO) {
545 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
547 if (volp->cbExpiresRO == scp->cbExpires)
548 volp->cbExpiresRO = now+1;
552 scp->cbExpires = now+1;
555 lock_ReleaseWrite(&cm_scacheLock);
559 cm_ShutdownSCache(void)
563 lock_ObtainWrite(&cm_scacheLock);
565 for ( scp = cm_data.allSCachesp; scp;
566 scp = scp->allNextp ) {
567 if (scp->randomACLp) {
568 lock_ReleaseWrite(&cm_scacheLock);
569 lock_ObtainWrite(&scp->rw);
570 lock_ObtainWrite(&cm_scacheLock);
571 cm_FreeAllACLEnts(scp);
572 lock_ReleaseWrite(&scp->rw);
575 if (scp->cbServerp) {
576 cm_PutServer(scp->cbServerp);
577 scp->cbServerp = NULL;
580 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
584 freeBtree(scp->dirBplus);
585 scp->dirBplus = NULL;
586 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
587 lock_FinalizeRWLock(&scp->dirlock);
589 lock_FinalizeRWLock(&scp->rw);
590 lock_FinalizeRWLock(&scp->bufCreateLock);
592 lock_ReleaseWrite(&cm_scacheLock);
594 cm_GiveUpAllCallbacksAllServers(FALSE);
596 return cm_dnlcShutdown();
599 void cm_InitSCache(int newFile, long maxSCaches)
601 static osi_once_t once;
603 if (osi_Once(&once)) {
604 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
606 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
607 cm_data.allSCachesp = NULL;
608 cm_data.currentSCaches = 0;
609 cm_data.maxSCaches = maxSCaches;
610 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
614 for ( scp = cm_data.allSCachesp; scp;
615 scp = scp->allNextp ) {
616 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
617 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
619 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
621 scp->cbServerp = NULL;
623 scp->volumeCreationDate = 0;
624 scp->fileLocksH = NULL;
625 scp->fileLocksT = NULL;
626 scp->serverLock = (-1);
627 scp->lastRefreshCycle = 0;
628 scp->exclusiveLocks = 0;
629 scp->sharedLocks = 0;
636 scp->dirBplus = NULL;
637 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
639 scp->waitQueueT = NULL;
640 scp->flags &= ~CM_SCACHEFLAG_WAITING;
643 cm_allFileLocks = NULL;
644 cm_freeFileLocks = NULL;
645 cm_lockRefreshCycle = 0;
646 cm_fakeSCacheInit(newFile);
647 cm_allFreeWaiters = NULL;
648 cm_dnlcInit(newFile);
653 /* version that doesn't bother creating the entry if we don't find it */
654 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
659 hash = CM_SCACHE_HASH(fidp);
661 if (fidp->cell == 0) {
665 lock_ObtainRead(&cm_scacheLock);
666 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
667 if (cm_FidCmp(fidp, &scp->fid) == 0) {
668 cm_HoldSCacheNoLock(scp);
669 lock_ConvertRToW(&cm_scacheLock);
670 cm_AdjustScacheLRU(scp);
671 lock_ReleaseWrite(&cm_scacheLock);
675 lock_ReleaseRead(&cm_scacheLock);
679 #ifdef DEBUG_REFCOUNT
680 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
681 cm_req_t *reqp, char * file, long line)
683 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
688 cm_scache_t *scp = NULL;
690 cm_volume_t *volp = NULL;
692 int special = 0; // yj: boolean variable to test if file is on root.afs
694 extern cm_fid_t cm_rootFid;
696 hash = CM_SCACHE_HASH(fidp);
699 return CM_ERROR_INVAL;
701 #ifdef AFS_FREELANCE_CLIENT
702 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
703 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
704 !(fidp->vnode==0x1 && fidp->unique==0x1));
705 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
706 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
707 fidp->vnode==0x1 && fidp->unique==0x1);
710 // yj: check if we have the scp, if so, we don't need
711 // to do anything else
712 lock_ObtainWrite(&cm_scacheLock);
713 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
714 if (cm_FidCmp(fidp, &scp->fid) == 0) {
715 #ifdef DEBUG_REFCOUNT
716 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
717 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
719 #ifdef AFS_FREELANCE_CLIENT
720 if (cm_freelanceEnabled && special &&
721 cm_data.fakeDirVersion != scp->dataVersion)
724 cm_HoldSCacheNoLock(scp);
726 cm_AdjustScacheLRU(scp);
727 lock_ReleaseWrite(&cm_scacheLock);
732 // yj: when we get here, it means we don't have an scp
733 // so we need to either load it or fake it, depending
734 // on whether the file is "special", see below.
736 // yj: if we're trying to get an scp for a file that's
737 // on root.afs of homecell, we want to handle it specially
738 // because we have to fill in the status stuff 'coz we
739 // don't want trybulkstat to fill it in for us
740 #ifdef AFS_FREELANCE_CLIENT
741 if (cm_freelanceEnabled && isRoot) {
742 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
743 /* freelance: if we are trying to get the root scp for the first
744 * time, we will just put in a place holder entry.
749 if (cm_freelanceEnabled && special) {
750 lock_ReleaseWrite(&cm_scacheLock);
751 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
753 if (cm_getLocalMountPointChange()) { // check for changes
754 cm_clearLocalMountPointChange(); // clear the changefile
755 cm_reInitLocalMountPoints(); // start reinit
758 lock_ObtainWrite(&cm_scacheLock);
760 scp = cm_GetNewSCache(); /* returns scp->rw held */
762 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
763 lock_ReleaseWrite(&cm_scacheLock);
764 return CM_ERROR_WOULDBLOCK;
767 lock_ReleaseWrite(&cm_scacheLock);
768 lock_ObtainWrite(&scp->rw);
769 lock_ObtainWrite(&cm_scacheLock);
772 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
773 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
774 scp->dotdotFid.unique=1;
775 scp->dotdotFid.vnode=1;
776 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
777 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
778 scp->nextp = cm_data.scacheHashTablep[hash];
779 cm_data.scacheHashTablep[hash] = scp;
780 scp->flags |= CM_SCACHEFLAG_INHASH;
783 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
785 /* must be called after the scp->fid is set */
786 cm_FreelanceFetchMountPointString(scp);
787 cm_FreelanceFetchFileType(scp);
789 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
790 scp->length.HighPart = 0;
792 scp->unixModeBits=0777;
793 scp->clientModTime=FakeFreelanceModTime;
794 scp->serverModTime=FakeFreelanceModTime;
795 scp->parentUnique = 0x1;
796 scp->parentVnode=0x1;
798 scp->dataVersion=cm_data.fakeDirVersion;
799 scp->bufDataVersionLow=cm_data.fakeDirVersion;
800 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
802 lock_ReleaseWrite(&scp->rw);
803 lock_ReleaseWrite(&cm_scacheLock);
805 #ifdef DEBUG_REFCOUNT
806 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
807 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
812 #endif /* AFS_FREELANCE_CLIENT */
814 /* otherwise, we need to find the volume */
815 if (!cm_freelanceEnabled || !isRoot) {
816 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
817 cellp = cm_FindCellByID(fidp->cell, 0);
819 return CM_ERROR_NOSUCHCELL;
821 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
824 lock_ObtainWrite(&cm_scacheLock);
827 /* otherwise, we have the volume, now reverify that the scp doesn't
828 * exist, and proceed.
830 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
831 if (cm_FidCmp(fidp, &scp->fid) == 0) {
832 #ifdef DEBUG_REFCOUNT
833 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
834 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
836 cm_HoldSCacheNoLock(scp);
837 cm_AdjustScacheLRU(scp);
838 lock_ReleaseWrite(&cm_scacheLock);
846 /* now, if we don't have the fid, recycle something */
847 scp = cm_GetNewSCache(); /* returns scp->rw held */
849 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
850 lock_ReleaseWrite(&cm_scacheLock);
853 return CM_ERROR_WOULDBLOCK;
855 #ifdef DEBUG_REFCOUNT
856 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
858 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
860 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
863 if (!cm_freelanceEnabled || !isRoot) {
864 /* if this scache entry represents a volume root then we need
865 * to copy the dotdotFipd from the volume structure where the
866 * "master" copy is stored (defect 11489)
868 if (volp->vol[ROVOL].ID == fidp->volume) {
869 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
870 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
871 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
872 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
873 scp->flags |= CM_SCACHEFLAG_RO;
874 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
875 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
877 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
878 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
883 scp->nextp = cm_data.scacheHashTablep[hash];
884 cm_data.scacheHashTablep[hash] = scp;
885 scp->flags |= CM_SCACHEFLAG_INHASH;
886 lock_ReleaseWrite(&scp->rw);
888 #ifdef DEBUG_REFCOUNT
889 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
891 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
893 /* XXX - The following fields in the cm_scache are
900 /* now we have a held scache entry; just return it */
902 #ifdef DEBUG_REFCOUNT
903 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
904 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
906 lock_ReleaseWrite(&cm_scacheLock);
910 /* Returns a held reference to the scache's parent
912 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
917 cm_scache_t * pscp = NULL;
919 lock_ObtainWrite(&cm_scacheLock);
920 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
922 if (cm_FidCmp(&scp->fid, &parent_fid)) {
923 i = CM_SCACHE_HASH(&parent_fid);
924 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
925 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
926 cm_HoldSCacheNoLock(pscp);
932 lock_ReleaseWrite(&cm_scacheLock);
937 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
939 cm_scache_waiter_t * w;
941 lock_ObtainWrite(&cm_scacheLock);
942 if (cm_allFreeWaiters == NULL) {
943 w = malloc(sizeof(*w));
944 memset(w, 0, sizeof(*w));
946 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
947 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
950 w->threadId = thrd_Current();
952 cm_HoldSCacheNoLock(scp);
956 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
957 lock_ReleaseWrite(&cm_scacheLock);
959 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
962 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
964 cm_scache_waiter_t * w;
967 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
969 lock_ObtainRead(&cm_scacheLock);
970 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
972 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
973 if (w->flags == flags && w->bufp == bufp) {
978 osi_assertx(w != NULL, "null cm_scache_waiter_t");
979 this_is_me = (w->threadId == thrd_Current());
980 lock_ReleaseRead(&cm_scacheLock);
983 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
987 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
989 lock_ObtainWrite(&cm_scacheLock);
990 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
991 cm_ReleaseSCacheNoLock(scp);
992 memset(w, 0, sizeof(*w));
993 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
994 lock_ReleaseWrite(&cm_scacheLock);
1000 /* synchronize a fetch, store, read, write, fetch status or store status.
1001 * Called with scache mutex held, and returns with it held, but temporarily
1002 * drops it during the fetch.
1004 * At most one flag can be on in flags, if this is an RPC request.
1006 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1008 * There are a lot of weird restrictions here; here's an attempt to explain the
1009 * rationale for the concurrency restrictions implemented in this function.
1011 * First, although the file server will break callbacks when *another* machine
1012 * modifies a file or status block, the client itself is responsible for
1013 * concurrency control on its own requests. Callback breaking events are rare,
1014 * and simply invalidate any concurrent new status info.
1016 * In the absence of callback breaking messages, we need to know how to
1017 * synchronize incoming responses describing updates to files. We synchronize
1018 * operations that update the data version by comparing the data versions.
1019 * However, updates that do not update the data, but only the status, can't be
1020 * synchronized with fetches or stores, since there's nothing to compare
1021 * to tell which operation executed first at the server.
1023 * Thus, we can allow multiple ops that change file data, or dir data, and
1024 * fetches. However, status storing ops have to be done serially.
1026 * Furthermore, certain data-changing ops are incompatible: we can't read or
1027 * write a buffer while doing a truncate. We can't read and write the same
1028 * buffer at the same time, or write while fetching or storing, or read while
1029 * fetching a buffer (this may change). We can't fetch and store at the same
1032 * With respect to status, we can't read and write at the same time, read while
1033 * fetching, write while fetching or storing, or fetch and store at the same time.
1035 * We can't allow a get callback RPC to run in concurrently with something that
1036 * will return updated status, since we could start a call, have the server
1037 * return status, have another machine make an update to the status (which
1038 * doesn't change serverModTime), have the original machine get a new callback,
1039 * and then have the original machine merge in the early, old info from the
1040 * first call. At this point, the easiest way to avoid this problem is to have
1041 * getcallback calls conflict with all others for the same vnode. Other calls
1042 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1043 * vnode must be careful not to merge in their status unless they have obtained
1044 * a callback from the start of their call.
1046 * Note added 1/23/96
1047 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1048 * Each such RPC passes a FileLength parameter, which the server uses to do
1049 * pre-truncation if necessary. So if two RPC's are processed out of order at
1050 * the server, the one with the smaller FileLength will be processed last,
1051 * possibly resulting in a bogus truncation. The simplest way to avoid this
1052 * is to serialize all StoreData RPC's. This is the reason we defined
1053 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1055 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1056 afs_uint32 rights, afs_uint32 flags)
1058 osi_queueData_t *qdp;
1061 afs_uint32 outRights;
1063 afs_uint32 sleep_scp_flags = 0;
1064 afs_uint32 sleep_buf_cmflags = 0;
1065 afs_uint32 sleep_scp_bufs = 0;
1068 lock_AssertWrite(&scp->rw);
1070 /* lookup this first */
1071 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1074 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1077 /* Do the access check. Now we don't really do the access check
1078 * atomically, since the caller doesn't expect the parent dir to be
1079 * returned locked, and that is what we'd have to do to prevent a
1080 * callback breaking message on the parent due to a setacl call from
1081 * being processed while we're running. So, instead, we check things
1082 * here, and if things look fine with the access, we proceed to finish
1083 * the rest of this check. Sort of a hack, but probably good enough.
1087 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1088 /* if we're bringing in a new status block, ensure that
1089 * we aren't already doing so, and that no one is
1090 * changing the status concurrently, either. We need
1091 * to do this, even if the status is of a different
1092 * type, since we don't have the ability to figure out,
1093 * in the AFS 3 protocols, which status-changing
1094 * operation ran first, or even which order a read and
1095 * a write occurred in.
1097 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1098 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1099 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1103 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1104 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1105 /* if we're going to make an RPC to change the status, make sure
1106 * that no one is bringing in or sending out the status.
1108 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1109 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1110 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1113 if (scp->bufReadsp || scp->bufWritesp) {
1114 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1118 if (flags & CM_SCACHESYNC_FETCHDATA) {
1119 /* if we're bringing in a new chunk of data, make sure that
1120 * nothing is happening to that chunk, and that we aren't
1121 * changing the basic file status info, either.
1123 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1124 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1125 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1128 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1129 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1133 if (flags & CM_SCACHESYNC_STOREDATA) {
1134 /* same as fetch data */
1135 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1136 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1137 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1140 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1141 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1146 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1147 /* Don't allow concurrent StoreData RPC's */
1148 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1149 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1154 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1155 /* Don't allow more than one BKG store request */
1156 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1157 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1162 if (flags & CM_SCACHESYNC_LOCK) {
1163 /* Don't allow concurrent fiddling with lock lists */
1164 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1165 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1170 /* now the operations that don't correspond to making RPCs */
1171 if (flags & CM_SCACHESYNC_GETSTATUS) {
1172 /* we can use the status that's here, if we're not
1173 * bringing in new status.
1175 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1176 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1180 if (flags & CM_SCACHESYNC_SETSTATUS) {
1181 /* we can make a change to the local status, as long as
1182 * the status isn't changing now.
1184 * If we're fetching or storing a chunk of data, we can
1185 * change the status locally, since the fetch/store
1186 * operations don't change any of the data that we're
1189 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1190 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1194 if (flags & CM_SCACHESYNC_READ) {
1195 /* we're going to read the data, make sure that the
1196 * status is available, and that the data is here. It
1197 * is OK to read while storing the data back.
1199 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1200 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1203 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1204 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1207 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1208 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1212 if (flags & CM_SCACHESYNC_WRITE) {
1213 /* don't write unless the status is stable and the chunk
1216 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1217 | CM_SCACHEFLAG_SIZESTORING)) {
1218 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1221 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1223 CM_BUF_CMWRITING))) {
1224 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1226 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1227 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1228 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1234 // yj: modified this so that callback only checked if we're
1235 // not checking something on /afs
1236 /* fix the conditional to match the one in cm_HaveCallback */
1237 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1238 #ifdef AFS_FREELANCE_CLIENT
1239 && (!cm_freelanceEnabled ||
1240 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1241 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1242 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1243 cm_fakeDirCallback < 2)
1244 #endif /* AFS_FREELANCE_CLIENT */
1246 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1247 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1250 lock_ReleaseMutex(&bufp->mx);
1251 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1253 lock_ReleaseWrite(&scp->rw);
1254 lock_ObtainMutex(&bufp->mx);
1255 lock_ObtainWrite(&scp->rw);
1259 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1265 /* can't check access rights without a callback */
1266 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1268 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1269 return CM_ERROR_READONLY;
1271 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1272 if (~outRights & rights)
1273 return CM_ERROR_NOACCESS;
1276 /* we don't know the required access rights */
1277 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1278 code = cm_GetAccessRights(scp, userp, reqp);
1280 lock_ReleaseWrite(&scp->rw);
1281 lock_ObtainMutex(&bufp->mx);
1282 lock_ObtainWrite(&scp->rw);
1290 /* if we get here, we're happy */
1294 /* first check if we're not supposed to wait: fail
1295 * in this case, returning with everything still locked.
1297 if (flags & CM_SCACHESYNC_NOWAIT)
1298 return CM_ERROR_WOULDBLOCK;
1300 /* These are used for minidump debugging */
1301 sleep_scp_flags = scp->flags; /* so we know why we slept */
1302 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1303 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1305 /* wait here, then try again */
1306 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1307 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1309 scp->waitRequests++;
1310 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1311 scp, scp->waitCount, scp->waitRequests);
1313 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1314 scp->flags |= CM_SCACHEFLAG_WAITING;
1315 scp->waitCount = scp->waitRequests = 1;
1318 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1322 lock_ReleaseMutex(&bufp->mx);
1323 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1325 lock_ObtainMutex(&bufp->mx);
1326 lock_ObtainWrite(&scp->rw);
1327 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1329 cm_UpdateServerPriority();
1332 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1333 scp, scp->waitCount, scp->waitRequests);
1334 if (scp->waitCount == 0) {
1335 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1336 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1337 scp->waitRequests = 0;
1339 } /* big while loop */
1341 /* now, update the recorded state for RPC-type calls */
1342 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1343 scp->flags |= CM_SCACHEFLAG_FETCHING;
1344 if (flags & CM_SCACHESYNC_STORESTATUS)
1345 scp->flags |= CM_SCACHEFLAG_STORING;
1346 if (flags & CM_SCACHESYNC_STORESIZE)
1347 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1348 if (flags & CM_SCACHESYNC_GETCALLBACK)
1349 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1350 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1351 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1352 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1353 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1354 if (flags & CM_SCACHESYNC_LOCK)
1355 scp->flags |= CM_SCACHEFLAG_LOCKING;
1357 /* now update the buffer pointer */
1358 if (flags & CM_SCACHESYNC_FETCHDATA) {
1359 /* ensure that the buffer isn't already in the I/O list */
1361 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1362 tbufp = osi_GetQData(qdp);
1363 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1367 /* queue a held reference to the buffer in the "reading" I/O list */
1368 qdp = osi_QDAlloc();
1369 osi_SetQData(qdp, bufp);
1372 bufp->cmFlags |= CM_BUF_CMFETCHING;
1374 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1377 if (flags & CM_SCACHESYNC_STOREDATA) {
1378 /* ensure that the buffer isn't already in the I/O list */
1380 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1381 tbufp = osi_GetQData(qdp);
1382 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1386 /* queue a held reference to the buffer in the "writing" I/O list */
1387 qdp = osi_QDAlloc();
1388 osi_SetQData(qdp, bufp);
1391 bufp->cmFlags |= CM_BUF_CMSTORING;
1393 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1396 if (flags & CM_SCACHESYNC_WRITE) {
1397 /* mark the buffer as being written to. */
1399 bufp->cmFlags |= CM_BUF_CMWRITING;
1406 /* for those syncops that setup for RPCs.
1407 * Called with scache locked.
1409 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1411 osi_queueData_t *qdp;
1414 lock_AssertWrite(&scp->rw);
1416 /* now, update the recorded state for RPC-type calls */
1417 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1418 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1419 if (flags & CM_SCACHESYNC_STORESTATUS)
1420 scp->flags &= ~CM_SCACHEFLAG_STORING;
1421 if (flags & CM_SCACHESYNC_STORESIZE)
1422 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1423 if (flags & CM_SCACHESYNC_GETCALLBACK)
1424 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1425 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1426 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1427 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1428 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1429 if (flags & CM_SCACHESYNC_LOCK)
1430 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1432 /* now update the buffer pointer */
1433 if (flags & CM_SCACHESYNC_FETCHDATA) {
1436 /* ensure that the buffer isn't already in the I/O list */
1437 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1438 tbufp = osi_GetQData(qdp);
1443 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1448 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1449 if (bufp->flags & CM_BUF_WAITING) {
1450 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1451 osi_Wakeup((LONG_PTR) &bufp);
1458 /* now update the buffer pointer */
1459 if (flags & CM_SCACHESYNC_STOREDATA) {
1461 /* ensure that the buffer isn't already in the I/O list */
1462 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1463 tbufp = osi_GetQData(qdp);
1468 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1473 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1474 if (bufp->flags & CM_BUF_WAITING) {
1475 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1476 osi_Wakeup((LONG_PTR) &bufp);
1483 if (flags & CM_SCACHESYNC_WRITE) {
1485 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1487 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1491 /* and wakeup anyone who is waiting */
1492 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1493 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1494 osi_Wakeup((LONG_PTR) &scp->flags);
1498 /* merge in a response from an RPC. The scp must be locked, and the callback
1501 * Don't overwrite any status info that is dirty, since we could have a store
1502 * operation (such as store data) that merges some info in, and we don't want
1503 * to lose the local updates. Typically, there aren't many updates we do
1504 * locally, anyway, probably only mtime.
1506 * There is probably a bug in here where a chmod (which doesn't change
1507 * serverModTime) that occurs between two fetches, both of whose responses are
1508 * handled after the callback breaking is done, but only one of whose calls
1509 * started before that, can cause old info to be merged from the first call.
1511 void cm_MergeStatus(cm_scache_t *dscp,
1512 cm_scache_t *scp, AFSFetchStatus *statusp,
1513 AFSVolSync *volsyncp,
1514 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1516 afs_uint64 dataVersion;
1517 struct cm_volume *volp = NULL;
1518 struct cm_cell *cellp = NULL;
1520 // yj: i want to create some fake status for the /afs directory and the
1521 // entries under that directory
1522 #ifdef AFS_FREELANCE_CLIENT
1523 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1524 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1525 if (scp == cm_data.rootSCachep) {
1526 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1527 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1528 statusp->Length = cm_fakeDirSize;
1529 statusp->Length_hi = 0;
1531 statusp->FileType = scp->fileType;
1532 statusp->Length = scp->length.LowPart;
1533 statusp->Length_hi = scp->length.HighPart;
1535 statusp->InterfaceVersion = 0x1;
1536 statusp->LinkCount = scp->linkCount;
1537 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1538 statusp->Author = 0x1;
1539 statusp->Owner = 0x0;
1540 statusp->CallerAccess = 0x9;
1541 statusp->AnonymousAccess = 0x9;
1542 statusp->UnixModeBits = 0777;
1543 statusp->ParentVnode = 0x1;
1544 statusp->ParentUnique = 0x1;
1545 statusp->ResidencyMask = 0;
1546 statusp->ClientModTime = FakeFreelanceModTime;
1547 statusp->ServerModTime = FakeFreelanceModTime;
1549 statusp->SyncCounter = 0;
1550 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1551 statusp->lockCount = 0;
1552 statusp->errorCode = 0;
1554 #endif /* AFS_FREELANCE_CLIENT */
1556 if (statusp->errorCode != 0) {
1557 scp->flags |= CM_SCACHEFLAG_EACCESS;
1558 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1560 scp->fileType = 0; /* unknown */
1562 scp->serverModTime = 0;
1563 scp->clientModTime = 0;
1564 scp->length.LowPart = 0;
1565 scp->length.HighPart = 0;
1566 scp->serverLength.LowPart = 0;
1567 scp->serverLength.HighPart = 0;
1571 scp->unixModeBits = 0;
1573 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1574 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1575 scp->fsLockCount = 0;
1578 scp->parentVnode = dscp->fid.vnode;
1579 scp->parentUnique = dscp->fid.unique;
1581 scp->parentVnode = 0;
1582 scp->parentUnique = 0;
1586 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1589 dataVersion = statusp->dataVersionHigh;
1591 dataVersion |= statusp->DataVersion;
1593 if (!(flags & CM_MERGEFLAG_FORCE) &&
1594 dataVersion < scp->dataVersion &&
1595 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1597 cellp = cm_FindCellByID(scp->fid.cell, 0);
1598 if (scp->cbServerp) {
1599 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1600 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1601 osi_Log2(afsd_logp, "old data from server %x volume %s",
1602 scp->cbServerp->addr.sin_addr.s_addr,
1603 volp ? volp->namep : "(unknown)");
1605 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1606 scp, scp->dataVersion, dataVersion);
1607 /* we have a number of data fetch/store operations running
1608 * concurrently, and we can tell which one executed last at the
1609 * server by its mtime.
1610 * Choose the one with the largest mtime, and ignore the rest.
1612 * These concurrent calls are incompatible with setting the
1613 * mtime, so we won't have a locally changed mtime here.
1615 * We could also have ACL info for a different user than usual,
1616 * in which case we have to do that part of the merge, anyway.
1617 * We won't have to worry about the info being old, since we
1618 * won't have concurrent calls
1619 * that change file status running from this machine.
1621 * Added 3/17/98: if we see data version regression on an RO
1622 * file, it's probably due to a server holding an out-of-date
1623 * replica, rather than to concurrent RPC's. Failures to
1624 * release replicas are now flagged by the volserver, but only
1625 * since AFS 3.4 5.22, so there are plenty of clients getting
1626 * out-of-date replicas out there.
1628 * If we discover an out-of-date replica, by this time it's too
1629 * late to go to another server and retry. Also, we can't
1630 * reject the merge, because then there is no way for
1631 * GetAccess to do its work, and the caller gets into an
1632 * infinite loop. So we just grin and bear it.
1634 if (!(scp->flags & CM_SCACHEFLAG_RO))
1638 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1640 scp->serverModTime = statusp->ServerModTime;
1642 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1643 scp->clientModTime = statusp->ClientModTime;
1645 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1646 scp->length.LowPart = statusp->Length;
1647 scp->length.HighPart = statusp->Length_hi;
1650 scp->serverLength.LowPart = statusp->Length;
1651 scp->serverLength.HighPart = statusp->Length_hi;
1653 scp->linkCount = statusp->LinkCount;
1654 scp->owner = statusp->Owner;
1655 scp->group = statusp->Group;
1656 scp->unixModeBits = statusp->UnixModeBits & 07777;
1658 if (statusp->FileType == File)
1659 scp->fileType = CM_SCACHETYPE_FILE;
1660 else if (statusp->FileType == Directory)
1661 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1662 else if (statusp->FileType == SymbolicLink) {
1663 if ((scp->unixModeBits & 0111) == 0)
1664 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1666 scp->fileType = CM_SCACHETYPE_SYMLINK;
1669 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1670 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1672 /* and other stuff */
1673 scp->parentVnode = statusp->ParentVnode;
1674 scp->parentUnique = statusp->ParentUnique;
1675 scp->fsLockCount = statusp->lockCount;
1677 /* and merge in the private acl cache info, if this is more than the public
1678 * info; merge in the public stuff in any case.
1680 scp->anyAccess = statusp->AnonymousAccess;
1682 if (userp != NULL) {
1683 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1686 if (scp->dataVersion != 0 &&
1687 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1688 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1690 * We now know that all of the data buffers that we have associated
1691 * with this scp are invalid. Subsequent operations will go faster
1692 * if the buffers are removed from the hash tables.
1694 * We do not remove directory buffers if the dataVersion delta is 1 because
1695 * those version numbers will be updated as part of the directory operation.
1697 * We do not remove storedata buffers because they will still be valid.
1702 cm_buf_t *bp, *prevBp, *nextBp;
1704 lock_ObtainWrite(&buf_globalLock);
1705 i = BUF_FILEHASH(&scp->fid);
1706 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1708 nextBp = bp->fileHashp;
1710 * if the buffer belongs to this stat cache entry
1711 * and the buffer mutex can be obtained, check the
1712 * reference count and if it is zero, remove the buffer
1713 * from the hash tables. If there are references,
1714 * the buffer might be updated to the current version
1715 * so leave it in place.
1717 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1718 lock_TryMutex(&bp->mx)) {
1719 if (bp->refCount == 0 &&
1720 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1721 prevBp = bp->fileHashBackp;
1722 bp->fileHashBackp = bp->fileHashp = NULL;
1724 prevBp->fileHashp = nextBp;
1726 cm_data.buf_fileHashTablepp[i] = nextBp;
1728 nextBp->fileHashBackp = prevBp;
1730 j = BUF_HASH(&bp->fid, &bp->offset);
1731 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1732 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1737 *lbpp = bp->hashp; /* hash out */
1740 bp->flags &= ~CM_BUF_INHASH;
1742 lock_ReleaseMutex(&bp->mx);
1745 lock_ReleaseWrite(&buf_globalLock);
1748 /* We maintain a range of buffer dataVersion values which are considered
1749 * valid. This avoids the need to update the dataVersion on each buffer
1750 * object during an uncontested storeData operation. As a result this
1751 * merge status no longer has performance characteristics derived from
1752 * the size of the file.
1754 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1755 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1756 scp->bufDataVersionLow == 0)
1757 scp->bufDataVersionLow = dataVersion;
1759 scp->dataVersion = dataVersion;
1762 * If someone is waiting for status information, we can wake them up
1763 * now even though the entity that issued the FetchStatus may not
1764 * have completed yet.
1766 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1769 * We just successfully merged status on the stat cache object.
1770 * This means that the associated volume must be online.
1774 cellp = cm_FindCellByID(scp->fid.cell, 0);
1775 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1778 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1779 if (statep->state != vl_online) {
1780 lock_ObtainWrite(&volp->rw);
1781 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1782 statep->state = vl_online;
1783 lock_ReleaseWrite(&volp->rw);
1792 /* note that our stat cache info is incorrect, so force us eventually
1793 * to stat the file again. There may be dirty data associated with
1794 * this vnode, and we want to preserve that information.
1796 * This function works by simply simulating a loss of the callback.
1798 * This function must be called with the scache locked.
1800 void cm_DiscardSCache(cm_scache_t *scp)
1802 lock_AssertWrite(&scp->rw);
1803 if (scp->cbServerp) {
1804 cm_PutServer(scp->cbServerp);
1805 scp->cbServerp = NULL;
1808 scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
1809 cm_dnlcPurgedp(scp);
1810 cm_dnlcPurgevp(scp);
1811 cm_FreeAllACLEnts(scp);
1813 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1814 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1816 /* Force mount points and symlinks to be re-evaluated */
1817 scp->mountPointStringp[0] = '\0';
1820 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1822 afsFidp->Volume = fidp->volume;
1823 afsFidp->Vnode = fidp->vnode;
1824 afsFidp->Unique = fidp->unique;
1827 #ifdef DEBUG_REFCOUNT
1828 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1830 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1835 osi_assertx(scp != NULL, "null cm_scache_t");
1836 lock_AssertAny(&cm_scacheLock);
1837 refCount = InterlockedIncrement(&scp->refCount);
1838 #ifdef DEBUG_REFCOUNT
1839 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1840 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1844 #ifdef DEBUG_REFCOUNT
1845 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1847 void cm_HoldSCache(cm_scache_t *scp)
1852 osi_assertx(scp != NULL, "null cm_scache_t");
1853 lock_ObtainRead(&cm_scacheLock);
1854 refCount = InterlockedIncrement(&scp->refCount);
1855 #ifdef DEBUG_REFCOUNT
1856 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1857 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1859 lock_ReleaseRead(&cm_scacheLock);
1862 #ifdef DEBUG_REFCOUNT
1863 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1865 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1870 osi_assertx(scp != NULL, "null cm_scache_t");
1871 lock_AssertAny(&cm_scacheLock);
1873 refCount = InterlockedDecrement(&scp->refCount);
1874 #ifdef DEBUG_REFCOUNT
1876 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1878 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1879 #ifdef DEBUG_REFCOUNT
1880 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1881 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1884 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1888 lockstate = lock_GetRWLockState(&cm_scacheLock);
1889 if (lockstate != OSI_RWLOCK_WRITEHELD)
1890 lock_ReleaseRead(&cm_scacheLock);
1892 lock_ReleaseWrite(&cm_scacheLock);
1894 lock_ObtainWrite(&scp->rw);
1895 if (scp->flags & CM_SCACHEFLAG_DELETED)
1898 if (refCount == 0 && deleted) {
1899 lock_ObtainWrite(&cm_scacheLock);
1900 cm_RecycleSCache(scp, 0);
1901 if (lockstate != OSI_RWLOCK_WRITEHELD)
1902 lock_ConvertWToR(&cm_scacheLock);
1904 if (lockstate != OSI_RWLOCK_WRITEHELD)
1905 lock_ObtainRead(&cm_scacheLock);
1907 lock_ObtainWrite(&cm_scacheLock);
1909 lock_ReleaseWrite(&scp->rw);
1913 #ifdef DEBUG_REFCOUNT
1914 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1916 void cm_ReleaseSCache(cm_scache_t *scp)
1921 osi_assertx(scp != NULL, "null cm_scache_t");
1922 lock_ObtainRead(&cm_scacheLock);
1923 refCount = InterlockedDecrement(&scp->refCount);
1924 #ifdef DEBUG_REFCOUNT
1926 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1928 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1929 #ifdef DEBUG_REFCOUNT
1930 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1931 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1933 lock_ReleaseRead(&cm_scacheLock);
1935 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1937 lock_ObtainWrite(&scp->rw);
1938 if (scp->flags & CM_SCACHEFLAG_DELETED)
1941 lock_ObtainWrite(&cm_scacheLock);
1942 cm_RecycleSCache(scp, 0);
1943 lock_ReleaseWrite(&cm_scacheLock);
1945 lock_ReleaseWrite(&scp->rw);
1949 /* just look for the scp entry to get filetype */
1950 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1951 int cm_FindFileType(cm_fid_t *fidp)
1956 hash = CM_SCACHE_HASH(fidp);
1958 osi_assertx(fidp->cell != 0, "unassigned cell value");
1960 lock_ObtainWrite(&cm_scacheLock);
1961 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1962 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1963 lock_ReleaseWrite(&cm_scacheLock);
1964 return scp->fileType;
1967 lock_ReleaseWrite(&cm_scacheLock);
1971 /* dump all scp's that have reference count > 0 to a file.
1972 * cookie is used to identify this batch for easy parsing,
1973 * and it a string provided by a caller
1975 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1984 lock_ObtainRead(&cm_scacheLock);
1986 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1987 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1989 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1992 char *srvStr = NULL;
1993 afs_uint32 srvStrRpc = TRUE;
1997 if (scp->cbServerp) {
1998 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
1999 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2000 afs_asprintf(&srvStr, "%.0I", scp->cbServerp->addr.sin_addr.s_addr);
2004 if (scp->cbExpires) {
2009 cbt[strlen(cbt)-1] = '\0';
2012 if (scp->volumeCreationDate) {
2013 t = scp->volumeCreationDate;
2016 cdrot = strdup(cdrot);
2017 cdrot[strlen(cdrot)-1] = '\0';
2021 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2022 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2023 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2024 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2025 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2026 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2027 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2028 cdrot ? cdrot : "<none>", scp->refCount);
2029 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2031 if (scp->fileLocksH) {
2032 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2033 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2035 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2036 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2037 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2038 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2039 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2040 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2041 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2044 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2045 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2050 RpcStringFree(&srvStr);
2060 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2061 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2062 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2063 cookie, cm_data.scacheHashTableSize);
2064 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2066 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2068 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2070 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2071 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2072 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2076 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2077 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2079 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2080 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2082 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2083 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2084 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",
2085 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2086 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2087 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2090 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2091 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2094 lock_ReleaseRead(&cm_scacheLock);