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;
218 /* remove from dnlc */
222 /* discard cached status; if non-zero, Close
223 * tried to store this to server but failed */
226 /* discard symlink info */
227 scp->mountPointStringp[0] = '\0';
228 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
229 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
231 /* reset locking info */
232 scp->fileLocksH = NULL;
233 scp->fileLocksT = NULL;
234 scp->serverLock = (-1);
235 scp->exclusiveLocks = 0;
236 scp->sharedLocks = 0;
237 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
239 /* not locked, but there can be no references to this guy
240 * while we hold the global refcount lock.
242 cm_FreeAllACLEnts(scp);
244 cm_ResetSCacheDirectory(scp, 0);
250 * called with cm_scacheLock write-locked; find a vnode to recycle.
251 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
252 * returns scp->mx held.
254 cm_scache_t *cm_GetNewSCache(void)
259 lock_AssertWrite(&cm_scacheLock);
261 /* first pass - look for deleted objects */
262 for ( scp = cm_data.scacheLRULastp;
264 scp = (cm_scache_t *) osi_QPrev(&scp->q))
266 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
267 "invalid cm_scache_t address");
269 if (scp->refCount == 0) {
270 if (scp->flags & CM_SCACHEFLAG_DELETED) {
271 if (!lock_TryWrite(&scp->rw))
274 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
275 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
277 /* we found an entry, so return it */
278 /* now remove from the LRU queue and put it back at the
279 * head of the LRU queue.
281 cm_AdjustScacheLRU(scp);
286 lock_ReleaseWrite(&scp->rw);
287 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
288 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
289 if (!lock_TryWrite(&scp->rw))
292 /* we found an entry, so return it */
293 /* now remove from the LRU queue and put it back at the
294 * head of the LRU queue.
296 cm_AdjustScacheLRU(scp);
303 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
306 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
307 /* There were no deleted scache objects that we could use. Try to find
308 * one that simply hasn't been used in a while.
310 for ( scp = cm_data.scacheLRULastp;
312 scp = (cm_scache_t *) osi_QPrev(&scp->q))
314 /* It is possible for the refCount to be zero and for there still
315 * to be outstanding dirty buffers. If there are dirty buffers,
316 * we must not recycle the scp. */
317 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
318 if (!buf_DirtyBuffersExist(&scp->fid)) {
319 if (!lock_TryWrite(&scp->rw))
322 if (!cm_RecycleSCache(scp, 0)) {
323 /* we found an entry, so return it */
324 /* now remove from the LRU queue and put it back at the
325 * head of the LRU queue.
327 cm_AdjustScacheLRU(scp);
332 lock_ReleaseWrite(&scp->rw);
334 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
338 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
343 /* if we get here, we should allocate a new scache entry. We either are below
344 * quota or we have a leak and need to allocate a new one to avoid panicing.
346 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
347 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
348 "invalid cm_scache_t address");
349 memset(scp, 0, sizeof(cm_scache_t));
350 scp->magic = CM_SCACHE_MAGIC;
351 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
352 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
353 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
355 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
357 scp->serverLock = -1;
359 /* and put it in the LRU queue */
360 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
361 if (!cm_data.scacheLRULastp)
362 cm_data.scacheLRULastp = scp;
363 cm_data.currentSCaches++;
364 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
366 scp->allNextp = cm_data.allSCachesp;
367 cm_data.allSCachesp = scp;
371 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
374 fidp->volume = volume;
376 fidp->unique = unique;
377 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
380 /* like strcmp, only for fids */
381 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
383 if (ap->hash != bp->hash)
385 if (ap->vnode != bp->vnode)
387 if (ap->volume != bp->volume)
389 if (ap->unique != bp->unique)
391 if (ap->cell != bp->cell)
396 void cm_fakeSCacheInit(int newFile)
399 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
400 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
401 /* can leave clientModTime at 0 */
402 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
403 cm_data.fakeSCache.unixModeBits = 0777;
404 cm_data.fakeSCache.length.LowPart = 1000;
405 cm_data.fakeSCache.linkCount = 1;
406 cm_data.fakeSCache.refCount = 1;
408 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
412 cm_ValidateSCache(void)
414 cm_scache_t * scp, *lscp;
417 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
418 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
419 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
420 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
424 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
426 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
427 if (scp->magic != CM_SCACHE_MAGIC) {
428 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
429 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
432 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
433 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
434 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
437 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
438 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
439 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
442 if (i > cm_data.currentSCaches ) {
443 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
444 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
447 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
448 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
449 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
454 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
455 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
456 if (scp->magic != CM_SCACHE_MAGIC) {
457 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
458 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
461 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
462 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
463 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
466 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
467 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
468 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
471 if (i > cm_data.currentSCaches ) {
472 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
473 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
476 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
477 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
478 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
483 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
484 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
486 hash = CM_SCACHE_HASH(&scp->fid);
487 if (scp->magic != CM_SCACHE_MAGIC) {
488 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
489 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
492 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
493 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
494 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
497 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
498 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
499 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
503 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
504 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
510 return cm_dnlcValidate();
514 cm_SuspendSCache(void)
519 cm_GiveUpAllCallbacksAllServers(TRUE);
522 * After this call all servers are marked down.
523 * Do not clear the callbacks, instead change the
524 * expiration time so that the callbacks will be expired
525 * when the servers are marked back up. However, we
526 * want the callbacks to be preserved as long as the
527 * servers are down. That way if the machine resumes
528 * without network, the stat cache item will still be
533 lock_ObtainWrite(&cm_scacheLock);
534 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
535 if (scp->cbServerp) {
536 if (scp->flags & CM_SCACHEFLAG_PURERO) {
537 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
539 if (volp->cbExpiresRO == scp->cbExpires)
540 volp->cbExpiresRO = now+1;
544 scp->cbExpires = now+1;
547 lock_ReleaseWrite(&cm_scacheLock);
551 cm_ShutdownSCache(void)
555 lock_ObtainWrite(&cm_scacheLock);
557 for ( scp = cm_data.allSCachesp; scp;
558 scp = scp->allNextp ) {
559 if (scp->randomACLp) {
560 lock_ReleaseWrite(&cm_scacheLock);
561 lock_ObtainWrite(&scp->rw);
562 lock_ObtainWrite(&cm_scacheLock);
563 cm_FreeAllACLEnts(scp);
564 lock_ReleaseWrite(&scp->rw);
567 if (scp->cbServerp) {
568 cm_PutServer(scp->cbServerp);
569 scp->cbServerp = NULL;
572 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
576 freeBtree(scp->dirBplus);
577 scp->dirBplus = NULL;
578 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
579 lock_FinalizeRWLock(&scp->dirlock);
581 lock_FinalizeRWLock(&scp->rw);
582 lock_FinalizeRWLock(&scp->bufCreateLock);
584 lock_ReleaseWrite(&cm_scacheLock);
586 cm_GiveUpAllCallbacksAllServers(FALSE);
588 return cm_dnlcShutdown();
591 void cm_InitSCache(int newFile, long maxSCaches)
593 static osi_once_t once;
595 if (osi_Once(&once)) {
596 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
598 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
599 cm_data.allSCachesp = NULL;
600 cm_data.currentSCaches = 0;
601 cm_data.maxSCaches = maxSCaches;
602 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
606 for ( scp = cm_data.allSCachesp; scp;
607 scp = scp->allNextp ) {
608 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
609 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
611 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
613 scp->cbServerp = NULL;
615 scp->fileLocksH = NULL;
616 scp->fileLocksT = NULL;
617 scp->serverLock = (-1);
618 scp->lastRefreshCycle = 0;
619 scp->exclusiveLocks = 0;
620 scp->sharedLocks = 0;
627 scp->dirBplus = NULL;
628 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
630 scp->waitQueueT = NULL;
631 scp->flags &= ~CM_SCACHEFLAG_WAITING;
634 cm_allFileLocks = NULL;
635 cm_freeFileLocks = NULL;
636 cm_lockRefreshCycle = 0;
637 cm_fakeSCacheInit(newFile);
638 cm_allFreeWaiters = NULL;
639 cm_dnlcInit(newFile);
644 /* version that doesn't bother creating the entry if we don't find it */
645 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
650 hash = CM_SCACHE_HASH(fidp);
652 if (fidp->cell == 0) {
656 lock_ObtainRead(&cm_scacheLock);
657 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
658 if (cm_FidCmp(fidp, &scp->fid) == 0) {
659 cm_HoldSCacheNoLock(scp);
660 lock_ConvertRToW(&cm_scacheLock);
661 cm_AdjustScacheLRU(scp);
662 lock_ReleaseWrite(&cm_scacheLock);
666 lock_ReleaseRead(&cm_scacheLock);
670 #ifdef DEBUG_REFCOUNT
671 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
672 cm_req_t *reqp, char * file, long line)
674 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
679 cm_scache_t *scp = NULL;
681 cm_volume_t *volp = NULL;
683 int special = 0; // yj: boolean variable to test if file is on root.afs
685 extern cm_fid_t cm_rootFid;
687 hash = CM_SCACHE_HASH(fidp);
689 osi_assertx(fidp->cell != 0, "unassigned cell value");
691 #ifdef AFS_FREELANCE_CLIENT
692 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
693 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
694 !(fidp->vnode==0x1 && fidp->unique==0x1));
695 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
696 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
697 fidp->vnode==0x1 && fidp->unique==0x1);
700 // yj: check if we have the scp, if so, we don't need
701 // to do anything else
702 lock_ObtainWrite(&cm_scacheLock);
703 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
704 if (cm_FidCmp(fidp, &scp->fid) == 0) {
705 #ifdef DEBUG_REFCOUNT
706 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
707 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
709 #ifdef AFS_FREELANCE_CLIENT
710 if (cm_freelanceEnabled && special &&
711 cm_data.fakeDirVersion != scp->dataVersion)
714 cm_HoldSCacheNoLock(scp);
716 cm_AdjustScacheLRU(scp);
717 lock_ReleaseWrite(&cm_scacheLock);
722 // yj: when we get here, it means we don't have an scp
723 // so we need to either load it or fake it, depending
724 // on whether the file is "special", see below.
726 // yj: if we're trying to get an scp for a file that's
727 // on root.afs of homecell, we want to handle it specially
728 // because we have to fill in the status stuff 'coz we
729 // don't want trybulkstat to fill it in for us
730 #ifdef AFS_FREELANCE_CLIENT
731 if (cm_freelanceEnabled && isRoot) {
732 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
733 /* freelance: if we are trying to get the root scp for the first
734 * time, we will just put in a place holder entry.
739 if (cm_freelanceEnabled && special) {
740 lock_ReleaseWrite(&cm_scacheLock);
741 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
743 if (cm_getLocalMountPointChange()) { // check for changes
744 cm_clearLocalMountPointChange(); // clear the changefile
745 cm_reInitLocalMountPoints(); // start reinit
748 lock_ObtainWrite(&cm_scacheLock);
750 scp = cm_GetNewSCache(); /* returns scp->rw held */
752 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
753 lock_ReleaseWrite(&cm_scacheLock);
754 return CM_ERROR_WOULDBLOCK;
757 lock_ReleaseWrite(&cm_scacheLock);
758 lock_ObtainWrite(&scp->rw);
759 lock_ObtainWrite(&cm_scacheLock);
762 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
763 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
764 scp->dotdotFid.unique=1;
765 scp->dotdotFid.vnode=1;
766 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
767 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
768 scp->nextp = cm_data.scacheHashTablep[hash];
769 cm_data.scacheHashTablep[hash] = scp;
770 scp->flags |= CM_SCACHEFLAG_INHASH;
773 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
775 /* must be called after the scp->fid is set */
776 cm_FreelanceFetchMountPointString(scp);
777 cm_FreelanceFetchFileType(scp);
779 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
780 scp->length.HighPart = 0;
782 scp->unixModeBits=0777;
783 scp->clientModTime=FakeFreelanceModTime;
784 scp->serverModTime=FakeFreelanceModTime;
785 scp->parentUnique = 0x1;
786 scp->parentVnode=0x1;
788 scp->dataVersion=cm_data.fakeDirVersion;
789 scp->bufDataVersionLow=cm_data.fakeDirVersion;
790 scp->lockDataVersion=-1; /* no lock yet */
791 lock_ReleaseWrite(&scp->rw);
792 lock_ReleaseWrite(&cm_scacheLock);
794 #ifdef DEBUG_REFCOUNT
795 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
796 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
801 #endif /* AFS_FREELANCE_CLIENT */
803 /* otherwise, we need to find the volume */
804 if (!cm_freelanceEnabled || !isRoot) {
805 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
806 cellp = cm_FindCellByID(fidp->cell, 0);
808 return CM_ERROR_NOSUCHCELL;
810 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
813 lock_ObtainWrite(&cm_scacheLock);
816 /* otherwise, we have the volume, now reverify that the scp doesn't
817 * exist, and proceed.
819 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
820 if (cm_FidCmp(fidp, &scp->fid) == 0) {
821 #ifdef DEBUG_REFCOUNT
822 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
823 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
825 cm_HoldSCacheNoLock(scp);
826 cm_AdjustScacheLRU(scp);
827 lock_ReleaseWrite(&cm_scacheLock);
835 /* now, if we don't have the fid, recycle something */
836 scp = cm_GetNewSCache(); /* returns scp->rw held */
838 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
839 lock_ReleaseWrite(&cm_scacheLock);
842 return CM_ERROR_WOULDBLOCK;
844 #ifdef DEBUG_REFCOUNT
845 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
847 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
849 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
852 if (!cm_freelanceEnabled || !isRoot) {
853 /* if this scache entry represents a volume root then we need
854 * to copy the dotdotFipd from the volume structure where the
855 * "master" copy is stored (defect 11489)
857 if (volp->vol[ROVOL].ID == fidp->volume) {
858 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
859 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
860 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
861 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
862 scp->flags |= CM_SCACHEFLAG_RO;
863 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
864 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
866 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
867 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
872 scp->nextp = cm_data.scacheHashTablep[hash];
873 cm_data.scacheHashTablep[hash] = scp;
874 scp->flags |= CM_SCACHEFLAG_INHASH;
875 lock_ReleaseWrite(&scp->rw);
877 #ifdef DEBUG_REFCOUNT
878 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
880 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
882 /* XXX - The following fields in the cm_scache are
889 /* now we have a held scache entry; just return it */
891 #ifdef DEBUG_REFCOUNT
892 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
893 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
895 lock_ReleaseWrite(&cm_scacheLock);
899 /* Returns a held reference to the scache's parent
901 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
906 cm_scache_t * pscp = NULL;
908 lock_ObtainWrite(&cm_scacheLock);
909 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
911 if (cm_FidCmp(&scp->fid, &parent_fid)) {
912 i = CM_SCACHE_HASH(&parent_fid);
913 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
914 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
915 cm_HoldSCacheNoLock(pscp);
921 lock_ReleaseWrite(&cm_scacheLock);
926 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
928 cm_scache_waiter_t * w;
930 lock_ObtainWrite(&cm_scacheLock);
931 if (cm_allFreeWaiters == NULL) {
932 w = malloc(sizeof(*w));
933 memset(w, 0, sizeof(*w));
935 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
936 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
939 w->threadId = thrd_Current();
941 cm_HoldSCacheNoLock(scp);
945 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
946 lock_ReleaseWrite(&cm_scacheLock);
948 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
951 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
953 cm_scache_waiter_t * w;
956 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
958 lock_ObtainRead(&cm_scacheLock);
959 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
961 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
962 if (w->flags == flags && w->bufp == bufp) {
967 osi_assertx(w != NULL, "null cm_scache_waiter_t");
968 this_is_me = (w->threadId == thrd_Current());
969 lock_ReleaseRead(&cm_scacheLock);
972 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
976 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
978 lock_ObtainWrite(&cm_scacheLock);
979 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
980 cm_ReleaseSCacheNoLock(scp);
981 memset(w, 0, sizeof(*w));
982 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
983 lock_ReleaseWrite(&cm_scacheLock);
989 /* synchronize a fetch, store, read, write, fetch status or store status.
990 * Called with scache mutex held, and returns with it held, but temporarily
991 * drops it during the fetch.
993 * At most one flag can be on in flags, if this is an RPC request.
995 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
997 * There are a lot of weird restrictions here; here's an attempt to explain the
998 * rationale for the concurrency restrictions implemented in this function.
1000 * First, although the file server will break callbacks when *another* machine
1001 * modifies a file or status block, the client itself is responsible for
1002 * concurrency control on its own requests. Callback breaking events are rare,
1003 * and simply invalidate any concurrent new status info.
1005 * In the absence of callback breaking messages, we need to know how to
1006 * synchronize incoming responses describing updates to files. We synchronize
1007 * operations that update the data version by comparing the data versions.
1008 * However, updates that do not update the data, but only the status, can't be
1009 * synchronized with fetches or stores, since there's nothing to compare
1010 * to tell which operation executed first at the server.
1012 * Thus, we can allow multiple ops that change file data, or dir data, and
1013 * fetches. However, status storing ops have to be done serially.
1015 * Furthermore, certain data-changing ops are incompatible: we can't read or
1016 * write a buffer while doing a truncate. We can't read and write the same
1017 * buffer at the same time, or write while fetching or storing, or read while
1018 * fetching a buffer (this may change). We can't fetch and store at the same
1021 * With respect to status, we can't read and write at the same time, read while
1022 * fetching, write while fetching or storing, or fetch and store at the same time.
1024 * We can't allow a get callback RPC to run in concurrently with something that
1025 * will return updated status, since we could start a call, have the server
1026 * return status, have another machine make an update to the status (which
1027 * doesn't change serverModTime), have the original machine get a new callback,
1028 * and then have the original machine merge in the early, old info from the
1029 * first call. At this point, the easiest way to avoid this problem is to have
1030 * getcallback calls conflict with all others for the same vnode. Other calls
1031 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1032 * vnode must be careful not to merge in their status unless they have obtained
1033 * a callback from the start of their call.
1035 * Note added 1/23/96
1036 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1037 * Each such RPC passes a FileLength parameter, which the server uses to do
1038 * pre-truncation if necessary. So if two RPC's are processed out of order at
1039 * the server, the one with the smaller FileLength will be processed last,
1040 * possibly resulting in a bogus truncation. The simplest way to avoid this
1041 * is to serialize all StoreData RPC's. This is the reason we defined
1042 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1044 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1045 afs_uint32 rights, afs_uint32 flags)
1047 osi_queueData_t *qdp;
1050 afs_uint32 outRights;
1052 afs_uint32 sleep_scp_flags = 0;
1053 afs_uint32 sleep_buf_cmflags = 0;
1054 afs_uint32 sleep_scp_bufs = 0;
1057 lock_AssertWrite(&scp->rw);
1059 /* lookup this first */
1060 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1063 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1066 /* Do the access check. Now we don't really do the access check
1067 * atomically, since the caller doesn't expect the parent dir to be
1068 * returned locked, and that is what we'd have to do to prevent a
1069 * callback breaking message on the parent due to a setacl call from
1070 * being processed while we're running. So, instead, we check things
1071 * here, and if things look fine with the access, we proceed to finish
1072 * the rest of this check. Sort of a hack, but probably good enough.
1076 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1077 /* if we're bringing in a new status block, ensure that
1078 * we aren't already doing so, and that no one is
1079 * changing the status concurrently, either. We need
1080 * to do this, even if the status is of a different
1081 * type, since we don't have the ability to figure out,
1082 * in the AFS 3 protocols, which status-changing
1083 * operation ran first, or even which order a read and
1084 * a write occurred in.
1086 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1087 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1088 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1092 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1093 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1094 /* if we're going to make an RPC to change the status, make sure
1095 * that no one is bringing in or sending out the status.
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 STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1102 if (scp->bufReadsp || scp->bufWritesp) {
1103 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1107 if (flags & CM_SCACHESYNC_FETCHDATA) {
1108 /* if we're bringing in a new chunk of data, make sure that
1109 * nothing is happening to that chunk, and that we aren't
1110 * changing the basic file status info, either.
1112 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1113 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1114 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1117 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1118 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1122 if (flags & CM_SCACHESYNC_STOREDATA) {
1123 /* same as fetch data */
1124 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1125 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1126 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1129 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1130 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1135 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1136 /* Don't allow concurrent StoreData RPC's */
1137 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1138 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1143 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1144 /* Don't allow more than one BKG store request */
1145 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1146 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1151 if (flags & CM_SCACHESYNC_LOCK) {
1152 /* Don't allow concurrent fiddling with lock lists */
1153 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1154 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1159 /* now the operations that don't correspond to making RPCs */
1160 if (flags & CM_SCACHESYNC_GETSTATUS) {
1161 /* we can use the status that's here, if we're not
1162 * bringing in new status.
1164 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1165 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1169 if (flags & CM_SCACHESYNC_SETSTATUS) {
1170 /* we can make a change to the local status, as long as
1171 * the status isn't changing now.
1173 * If we're fetching or storing a chunk of data, we can
1174 * change the status locally, since the fetch/store
1175 * operations don't change any of the data that we're
1178 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1179 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1183 if (flags & CM_SCACHESYNC_READ) {
1184 /* we're going to read the data, make sure that the
1185 * status is available, and that the data is here. It
1186 * is OK to read while storing the data back.
1188 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1189 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1192 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1193 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1196 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1197 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1201 if (flags & CM_SCACHESYNC_WRITE) {
1202 /* don't write unless the status is stable and the chunk
1205 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1206 | CM_SCACHEFLAG_SIZESTORING)) {
1207 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1210 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1212 CM_BUF_CMWRITING))) {
1213 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1215 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1216 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1217 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1223 // yj: modified this so that callback only checked if we're
1224 // not checking something on /afs
1225 /* fix the conditional to match the one in cm_HaveCallback */
1226 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1227 #ifdef AFS_FREELANCE_CLIENT
1228 && (!cm_freelanceEnabled ||
1229 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1230 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1231 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1232 cm_fakeDirCallback < 2)
1233 #endif /* AFS_FREELANCE_CLIENT */
1235 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1236 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1239 lock_ReleaseMutex(&bufp->mx);
1240 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1242 lock_ReleaseWrite(&scp->rw);
1243 lock_ObtainMutex(&bufp->mx);
1244 lock_ObtainWrite(&scp->rw);
1248 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1254 /* can't check access rights without a callback */
1255 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1257 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1258 return CM_ERROR_READONLY;
1260 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1261 if (~outRights & rights)
1262 return CM_ERROR_NOACCESS;
1265 /* we don't know the required access rights */
1266 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1267 code = cm_GetAccessRights(scp, userp, reqp);
1269 lock_ReleaseWrite(&scp->rw);
1270 lock_ObtainMutex(&bufp->mx);
1271 lock_ObtainWrite(&scp->rw);
1279 /* if we get here, we're happy */
1283 /* first check if we're not supposed to wait: fail
1284 * in this case, returning with everything still locked.
1286 if (flags & CM_SCACHESYNC_NOWAIT)
1287 return CM_ERROR_WOULDBLOCK;
1289 /* These are used for minidump debugging */
1290 sleep_scp_flags = scp->flags; /* so we know why we slept */
1291 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1292 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1294 /* wait here, then try again */
1295 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1296 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1298 scp->waitRequests++;
1299 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1300 scp, scp->waitCount, scp->waitRequests);
1302 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1303 scp->flags |= CM_SCACHEFLAG_WAITING;
1304 scp->waitCount = scp->waitRequests = 1;
1307 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1311 lock_ReleaseMutex(&bufp->mx);
1312 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1314 lock_ObtainMutex(&bufp->mx);
1315 lock_ObtainWrite(&scp->rw);
1316 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1318 smb_UpdateServerPriority();
1321 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1322 scp, scp->waitCount, scp->waitRequests);
1323 if (scp->waitCount == 0) {
1324 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1325 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1326 scp->waitRequests = 0;
1328 } /* big while loop */
1330 /* now, update the recorded state for RPC-type calls */
1331 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1332 scp->flags |= CM_SCACHEFLAG_FETCHING;
1333 if (flags & CM_SCACHESYNC_STORESTATUS)
1334 scp->flags |= CM_SCACHEFLAG_STORING;
1335 if (flags & CM_SCACHESYNC_STORESIZE)
1336 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1337 if (flags & CM_SCACHESYNC_GETCALLBACK)
1338 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1339 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1340 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1341 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1342 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1343 if (flags & CM_SCACHESYNC_LOCK)
1344 scp->flags |= CM_SCACHEFLAG_LOCKING;
1346 /* now update the buffer pointer */
1347 if (flags & CM_SCACHESYNC_FETCHDATA) {
1348 /* ensure that the buffer isn't already in the I/O list */
1350 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1351 tbufp = osi_GetQData(qdp);
1352 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1356 /* queue a held reference to the buffer in the "reading" I/O list */
1357 qdp = osi_QDAlloc();
1358 osi_SetQData(qdp, bufp);
1361 bufp->cmFlags |= CM_BUF_CMFETCHING;
1363 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1366 if (flags & CM_SCACHESYNC_STOREDATA) {
1367 /* ensure that the buffer isn't already in the I/O list */
1369 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1370 tbufp = osi_GetQData(qdp);
1371 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1375 /* queue a held reference to the buffer in the "writing" I/O list */
1376 qdp = osi_QDAlloc();
1377 osi_SetQData(qdp, bufp);
1380 bufp->cmFlags |= CM_BUF_CMSTORING;
1382 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1385 if (flags & CM_SCACHESYNC_WRITE) {
1386 /* mark the buffer as being written to. */
1388 bufp->cmFlags |= CM_BUF_CMWRITING;
1395 /* for those syncops that setup for RPCs.
1396 * Called with scache locked.
1398 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1400 osi_queueData_t *qdp;
1403 lock_AssertWrite(&scp->rw);
1405 /* now, update the recorded state for RPC-type calls */
1406 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1407 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1408 if (flags & CM_SCACHESYNC_STORESTATUS)
1409 scp->flags &= ~CM_SCACHEFLAG_STORING;
1410 if (flags & CM_SCACHESYNC_STORESIZE)
1411 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1412 if (flags & CM_SCACHESYNC_GETCALLBACK)
1413 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1414 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1415 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1416 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1417 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1418 if (flags & CM_SCACHESYNC_LOCK)
1419 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1421 /* now update the buffer pointer */
1422 if (flags & CM_SCACHESYNC_FETCHDATA) {
1425 /* ensure that the buffer isn't already in the I/O list */
1426 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1427 tbufp = osi_GetQData(qdp);
1432 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1437 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1438 if (bufp->flags & CM_BUF_WAITING) {
1439 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1440 osi_Wakeup((LONG_PTR) &bufp);
1447 /* now update the buffer pointer */
1448 if (flags & CM_SCACHESYNC_STOREDATA) {
1450 /* ensure that the buffer isn't already in the I/O list */
1451 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1452 tbufp = osi_GetQData(qdp);
1457 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1462 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1463 if (bufp->flags & CM_BUF_WAITING) {
1464 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1465 osi_Wakeup((LONG_PTR) &bufp);
1472 if (flags & CM_SCACHESYNC_WRITE) {
1474 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1476 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1480 /* and wakeup anyone who is waiting */
1481 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1482 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1483 osi_Wakeup((LONG_PTR) &scp->flags);
1487 /* merge in a response from an RPC. The scp must be locked, and the callback
1490 * Don't overwrite any status info that is dirty, since we could have a store
1491 * operation (such as store data) that merges some info in, and we don't want
1492 * to lose the local updates. Typically, there aren't many updates we do
1493 * locally, anyway, probably only mtime.
1495 * There is probably a bug in here where a chmod (which doesn't change
1496 * serverModTime) that occurs between two fetches, both of whose responses are
1497 * handled after the callback breaking is done, but only one of whose calls
1498 * started before that, can cause old info to be merged from the first call.
1500 void cm_MergeStatus(cm_scache_t *dscp,
1501 cm_scache_t *scp, AFSFetchStatus *statusp,
1502 AFSVolSync *volsyncp,
1503 cm_user_t *userp, afs_uint32 flags)
1505 afs_uint64 dataVersion;
1507 // yj: i want to create some fake status for the /afs directory and the
1508 // entries under that directory
1509 #ifdef AFS_FREELANCE_CLIENT
1510 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1511 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1512 statusp->InterfaceVersion = 0x1;
1513 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1514 statusp->LinkCount = scp->linkCount;
1515 statusp->Length = cm_fakeDirSize;
1516 statusp->Length_hi = 0;
1517 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1518 statusp->Author = 0x1;
1519 statusp->Owner = 0x0;
1520 statusp->CallerAccess = 0x9;
1521 statusp->AnonymousAccess = 0x9;
1522 statusp->UnixModeBits = 0777;
1523 statusp->ParentVnode = 0x1;
1524 statusp->ParentUnique = 0x1;
1525 statusp->ResidencyMask = 0;
1526 statusp->ClientModTime = FakeFreelanceModTime;
1527 statusp->ServerModTime = FakeFreelanceModTime;
1529 statusp->SyncCounter = 0;
1530 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1531 statusp->errorCode = 0;
1533 #endif /* AFS_FREELANCE_CLIENT */
1535 if (statusp->errorCode != 0) {
1536 scp->flags |= CM_SCACHEFLAG_EACCESS;
1537 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1539 scp->fileType = 0; /* unknown */
1541 scp->serverModTime = 0;
1542 scp->clientModTime = 0;
1543 scp->length.LowPart = 0;
1544 scp->length.HighPart = 0;
1545 scp->serverLength.LowPart = 0;
1546 scp->serverLength.HighPart = 0;
1550 scp->unixModeBits = 0;
1552 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1553 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1556 scp->parentVnode = dscp->fid.vnode;
1557 scp->parentUnique = dscp->fid.unique;
1559 scp->parentVnode = 0;
1560 scp->parentUnique = 0;
1564 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1567 dataVersion = statusp->dataVersionHigh;
1569 dataVersion |= statusp->DataVersion;
1571 if (!(flags & CM_MERGEFLAG_FORCE) &&
1572 dataVersion < scp->dataVersion &&
1573 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1574 struct cm_cell *cellp;
1576 cellp = cm_FindCellByID(scp->fid.cell, 0);
1577 if (scp->cbServerp) {
1578 struct cm_volume *volp = NULL;
1583 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1584 &req, CM_GETVOL_FLAG_CREATE, &volp);
1585 osi_Log2(afsd_logp, "old data from server %x volume %s",
1586 scp->cbServerp->addr.sin_addr.s_addr,
1587 volp ? volp->namep : "(unknown)");
1591 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1592 scp, scp->dataVersion, dataVersion);
1593 /* we have a number of data fetch/store operations running
1594 * concurrently, and we can tell which one executed last at the
1595 * server by its mtime.
1596 * Choose the one with the largest mtime, and ignore the rest.
1598 * These concurrent calls are incompatible with setting the
1599 * mtime, so we won't have a locally changed mtime here.
1601 * We could also have ACL info for a different user than usual,
1602 * in which case we have to do that part of the merge, anyway.
1603 * We won't have to worry about the info being old, since we
1604 * won't have concurrent calls
1605 * that change file status running from this machine.
1607 * Added 3/17/98: if we see data version regression on an RO
1608 * file, it's probably due to a server holding an out-of-date
1609 * replica, rather than to concurrent RPC's. Failures to
1610 * release replicas are now flagged by the volserver, but only
1611 * since AFS 3.4 5.22, so there are plenty of clients getting
1612 * out-of-date replicas out there.
1614 * If we discover an out-of-date replica, by this time it's too
1615 * late to go to another server and retry. Also, we can't
1616 * reject the merge, because then there is no way for
1617 * GetAccess to do its work, and the caller gets into an
1618 * infinite loop. So we just grin and bear it.
1620 if (!(scp->flags & CM_SCACHEFLAG_RO))
1624 scp->serverModTime = statusp->ServerModTime;
1626 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1627 scp->clientModTime = statusp->ClientModTime;
1629 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1630 scp->length.LowPart = statusp->Length;
1631 scp->length.HighPart = statusp->Length_hi;
1634 scp->serverLength.LowPart = statusp->Length;
1635 scp->serverLength.HighPart = statusp->Length_hi;
1637 scp->linkCount = statusp->LinkCount;
1638 scp->owner = statusp->Owner;
1639 scp->group = statusp->Group;
1640 scp->unixModeBits = statusp->UnixModeBits & 07777;
1642 if (statusp->FileType == File)
1643 scp->fileType = CM_SCACHETYPE_FILE;
1644 else if (statusp->FileType == Directory)
1645 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1646 else if (statusp->FileType == SymbolicLink) {
1647 if ((scp->unixModeBits & 0111) == 0)
1648 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1650 scp->fileType = CM_SCACHETYPE_SYMLINK;
1653 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1654 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1656 /* and other stuff */
1657 scp->parentVnode = statusp->ParentVnode;
1658 scp->parentUnique = statusp->ParentUnique;
1660 /* and merge in the private acl cache info, if this is more than the public
1661 * info; merge in the public stuff in any case.
1663 scp->anyAccess = statusp->AnonymousAccess;
1665 if (userp != NULL) {
1666 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1669 if (scp->dataVersion != 0 &&
1670 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1671 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1673 * We now know that all of the data buffers that we have associated
1674 * with this scp are invalid. Subsequent operations will go faster
1675 * if the buffers are removed from the hash tables.
1677 * We do not remove directory buffers if the dataVersion delta is 1 because
1678 * those version numbers will be updated as part of the directory operation.
1680 * We do not remove storedata buffers because they will still be valid.
1685 cm_buf_t *bp, *prevBp, *nextBp;
1687 lock_ObtainWrite(&buf_globalLock);
1688 i = BUF_FILEHASH(&scp->fid);
1689 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1691 nextBp = bp->fileHashp;
1693 * if the buffer belongs to this stat cache entry
1694 * and the buffer mutex can be obtained, check the
1695 * reference count and if it is zero, remove the buffer
1696 * from the hash tables. If there are references,
1697 * the buffer might be updated to the current version
1698 * so leave it in place.
1700 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1701 lock_TryMutex(&bp->mx)) {
1702 if (bp->refCount == 0 &&
1703 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1704 prevBp = bp->fileHashBackp;
1705 bp->fileHashBackp = bp->fileHashp = NULL;
1707 prevBp->fileHashp = nextBp;
1709 cm_data.buf_fileHashTablepp[i] = nextBp;
1711 nextBp->fileHashBackp = prevBp;
1713 j = BUF_HASH(&bp->fid, &bp->offset);
1714 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1715 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1720 *lbpp = bp->hashp; /* hash out */
1723 bp->flags &= ~CM_BUF_INHASH;
1725 lock_ReleaseMutex(&bp->mx);
1728 lock_ReleaseWrite(&buf_globalLock);
1731 /* We maintain a range of buffer dataVersion values which are considered
1732 * valid. This avoids the need to update the dataVersion on each buffer
1733 * object during an uncontested storeData operation. As a result this
1734 * merge status no longer has performance characteristics derived from
1735 * the size of the file.
1737 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1738 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1739 scp->bufDataVersionLow == 0)
1740 scp->bufDataVersionLow = dataVersion;
1742 scp->dataVersion = dataVersion;
1745 * If someone is waiting for status information, we can wake them up
1746 * now even though the entity that issued the FetchStatus may not
1747 * have completed yet.
1749 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1752 /* note that our stat cache info is incorrect, so force us eventually
1753 * to stat the file again. There may be dirty data associated with
1754 * this vnode, and we want to preserve that information.
1756 * This function works by simply simulating a loss of the callback.
1758 * This function must be called with the scache locked.
1760 void cm_DiscardSCache(cm_scache_t *scp)
1762 lock_AssertWrite(&scp->rw);
1763 if (scp->cbServerp) {
1764 cm_PutServer(scp->cbServerp);
1765 scp->cbServerp = NULL;
1768 scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
1769 cm_dnlcPurgedp(scp);
1770 cm_dnlcPurgevp(scp);
1771 cm_FreeAllACLEnts(scp);
1773 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1774 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1776 /* Force mount points and symlinks to be re-evaluated */
1777 scp->mountPointStringp[0] = '\0';
1780 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1782 afsFidp->Volume = fidp->volume;
1783 afsFidp->Vnode = fidp->vnode;
1784 afsFidp->Unique = fidp->unique;
1787 #ifdef DEBUG_REFCOUNT
1788 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1790 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1795 osi_assertx(scp != NULL, "null cm_scache_t");
1796 lock_AssertAny(&cm_scacheLock);
1797 refCount = InterlockedIncrement(&scp->refCount);
1798 #ifdef DEBUG_REFCOUNT
1799 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1800 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1804 #ifdef DEBUG_REFCOUNT
1805 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1807 void cm_HoldSCache(cm_scache_t *scp)
1812 osi_assertx(scp != NULL, "null cm_scache_t");
1813 lock_ObtainRead(&cm_scacheLock);
1814 refCount = InterlockedIncrement(&scp->refCount);
1815 #ifdef DEBUG_REFCOUNT
1816 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1817 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1819 lock_ReleaseRead(&cm_scacheLock);
1822 #ifdef DEBUG_REFCOUNT
1823 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1825 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1830 osi_assertx(scp != NULL, "null cm_scache_t");
1831 lock_AssertAny(&cm_scacheLock);
1833 refCount = InterlockedDecrement(&scp->refCount);
1834 #ifdef DEBUG_REFCOUNT
1836 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1838 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1839 #ifdef DEBUG_REFCOUNT
1840 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1841 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1844 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1848 lockstate = lock_GetRWLockState(&cm_scacheLock);
1849 if (lockstate != OSI_RWLOCK_WRITEHELD)
1850 lock_ReleaseRead(&cm_scacheLock);
1852 lock_ReleaseWrite(&cm_scacheLock);
1854 lock_ObtainWrite(&scp->rw);
1855 if (scp->flags & CM_SCACHEFLAG_DELETED)
1858 if (refCount == 0 && deleted) {
1859 lock_ObtainWrite(&cm_scacheLock);
1860 cm_RecycleSCache(scp, 0);
1861 if (lockstate != OSI_RWLOCK_WRITEHELD)
1862 lock_ConvertWToR(&cm_scacheLock);
1864 if (lockstate != OSI_RWLOCK_WRITEHELD)
1865 lock_ObtainRead(&cm_scacheLock);
1867 lock_ObtainWrite(&cm_scacheLock);
1869 lock_ReleaseWrite(&scp->rw);
1873 #ifdef DEBUG_REFCOUNT
1874 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1876 void cm_ReleaseSCache(cm_scache_t *scp)
1881 osi_assertx(scp != NULL, "null cm_scache_t");
1882 lock_ObtainRead(&cm_scacheLock);
1883 refCount = InterlockedDecrement(&scp->refCount);
1884 #ifdef DEBUG_REFCOUNT
1886 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1888 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1889 #ifdef DEBUG_REFCOUNT
1890 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1891 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1893 lock_ReleaseRead(&cm_scacheLock);
1895 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1897 lock_ObtainWrite(&scp->rw);
1898 if (scp->flags & CM_SCACHEFLAG_DELETED)
1901 lock_ObtainWrite(&cm_scacheLock);
1902 cm_RecycleSCache(scp, 0);
1903 lock_ReleaseWrite(&cm_scacheLock);
1905 lock_ReleaseWrite(&scp->rw);
1909 /* just look for the scp entry to get filetype */
1910 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1911 int cm_FindFileType(cm_fid_t *fidp)
1916 hash = CM_SCACHE_HASH(fidp);
1918 osi_assertx(fidp->cell != 0, "unassigned cell value");
1920 lock_ObtainWrite(&cm_scacheLock);
1921 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1922 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1923 lock_ReleaseWrite(&cm_scacheLock);
1924 return scp->fileType;
1927 lock_ReleaseWrite(&cm_scacheLock);
1931 /* dump all scp's that have reference count > 0 to a file.
1932 * cookie is used to identify this batch for easy parsing,
1933 * and it a string provided by a caller
1935 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1944 lock_ObtainRead(&cm_scacheLock);
1946 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1947 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1949 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1951 sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) flags=0x%x cb=0x%x refCount=%u\r\n",
1952 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1953 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
1954 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks,
1955 scp->flags, (unsigned long)scp->cbExpires, scp->refCount);
1956 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1958 if (scp->fileLocksH) {
1959 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
1960 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1962 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
1963 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
1964 sprintf(output, " %s lockp=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",
1965 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
1966 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
1967 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1970 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
1971 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1975 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1976 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1977 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1978 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1980 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1982 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1984 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1985 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1986 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1990 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1991 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1993 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
1994 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1996 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
1997 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
1998 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",
1999 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2000 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2001 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2004 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2005 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2008 lock_ReleaseRead(&cm_scacheLock);