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;
46 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
50 lock_ObtainWrite(&cm_data.rootSCachep->rw);
51 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
52 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
54 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
55 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
57 return cm_data.rootSCachep;
61 /* must be called with cm_scacheLock write-locked! */
62 void cm_AdjustScacheLRU(cm_scache_t *scp)
64 lock_AssertWrite(&cm_scacheLock);
65 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
66 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
69 /* call with cm_scacheLock write-locked and scp rw held */
70 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
76 lock_AssertWrite(&cm_scacheLock);
77 lock_AssertWrite(&scp->rw);
78 if (scp->flags & CM_SCACHEFLAG_INHASH) {
79 /* hash it out first */
80 i = CM_SCACHE_HASH(&scp->fid);
81 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
83 lscpp = &tscp->nextp, tscp = tscp->nextp) {
87 scp->flags &= ~CM_SCACHEFLAG_INHASH;
94 /* called with cm_scacheLock and scp write-locked */
95 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
98 /* destroy directory Bplus Tree */
100 LARGE_INTEGER start, end;
102 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
104 * We are not holding the dirlock and obtaining it
105 * requires that we drop the scp->rw. As a result
106 * we will leave the dirBplus tree intact but
107 * invalidate the version number so that whatever
108 * operation is currently active can safely complete
109 * but the contents will be ignored on the next
110 * directory operation.
112 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
116 QueryPerformanceCounter(&start);
118 freeBtree(scp->dirBplus);
119 scp->dirBplus = NULL;
120 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
121 QueryPerformanceCounter(&end);
124 lock_ReleaseWrite(&scp->dirlock);
126 bplus_free_time += (end.QuadPart - start.QuadPart);
131 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
132 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
134 if (scp->refCount != 0) {
138 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
139 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
141 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
146 cm_RemoveSCacheFromHashTable(scp);
149 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
150 osi_queueData_t *qdp;
153 while(qdp = scp->bufWritesp) {
154 bufp = osi_GetQData(qdp);
155 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
158 lock_ObtainMutex(&bufp->mx);
159 bufp->cmFlags &= ~CM_BUF_CMSTORING;
160 bufp->flags &= ~CM_BUF_DIRTY;
161 bufp->dirty_offset = 0;
162 bufp->dirty_length = 0;
163 bufp->flags |= CM_BUF_ERROR;
164 bufp->error = VNOVNODE;
165 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
166 bufp->dirtyCounter++;
167 if (bufp->flags & CM_BUF_WAITING) {
168 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
169 osi_Wakeup((long) &bufp);
171 lock_ReleaseMutex(&bufp->mx);
175 while(qdp = scp->bufReadsp) {
176 bufp = osi_GetQData(qdp);
177 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
180 lock_ObtainMutex(&bufp->mx);
181 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
182 bufp->flags &= ~CM_BUF_DIRTY;
183 bufp->dirty_offset = 0;
184 bufp->dirty_length = 0;
185 bufp->flags |= CM_BUF_ERROR;
186 bufp->error = VNOVNODE;
187 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
188 bufp->dirtyCounter++;
189 if (bufp->flags & CM_BUF_WAITING) {
190 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
191 osi_Wakeup((long) &bufp);
193 lock_ReleaseMutex(&bufp->mx);
197 buf_CleanDirtyBuffers(scp);
199 /* look for things that shouldn't still be set */
200 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
201 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
205 /* invalidate so next merge works fine;
206 * also initialize some flags */
208 scp->flags &= ~(CM_SCACHEFLAG_STATD
209 | CM_SCACHEFLAG_DELETED
211 | CM_SCACHEFLAG_PURERO
212 | CM_SCACHEFLAG_OVERQUOTA
213 | CM_SCACHEFLAG_OUTOFSPACE
214 | CM_SCACHEFLAG_EACCESS);
215 scp->serverModTime = 0;
216 scp->dataVersion = CM_SCACHE_VERSION_BAD;
217 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
218 scp->bulkStatProgress = hzero;
220 scp->waitQueueT = NULL;
222 if (scp->cbServerp) {
223 cm_PutServer(scp->cbServerp);
224 scp->cbServerp = NULL;
227 scp->volumeCreationDate = 0;
235 /* remove from dnlc */
239 /* discard cached status; if non-zero, Close
240 * tried to store this to server but failed */
243 /* discard symlink info */
244 scp->mountPointStringp[0] = '\0';
245 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
246 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
248 /* reset locking info */
249 scp->fileLocksH = NULL;
250 scp->fileLocksT = NULL;
251 scp->serverLock = (-1);
252 scp->exclusiveLocks = 0;
253 scp->sharedLocks = 0;
254 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
255 scp->fsLockCount = 0;
257 /* not locked, but there can be no references to this guy
258 * while we hold the global refcount lock.
260 cm_FreeAllACLEnts(scp);
262 cm_ResetSCacheDirectory(scp, 0);
268 * called with cm_scacheLock write-locked; find a vnode to recycle.
269 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
270 * returns scp->mx held.
272 cm_scache_t *cm_GetNewSCache(void)
277 lock_AssertWrite(&cm_scacheLock);
279 /* first pass - look for deleted objects */
280 for ( scp = cm_data.scacheLRULastp;
282 scp = (cm_scache_t *) osi_QPrev(&scp->q))
284 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
285 "invalid cm_scache_t address");
287 if (scp->refCount == 0) {
288 if (scp->flags & CM_SCACHEFLAG_DELETED) {
289 if (!lock_TryWrite(&scp->rw))
292 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
293 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
295 /* we found an entry, so return it */
296 /* now remove from the LRU queue and put it back at the
297 * head of the LRU queue.
299 cm_AdjustScacheLRU(scp);
304 lock_ReleaseWrite(&scp->rw);
305 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
306 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
307 if (!lock_TryWrite(&scp->rw))
310 /* we found an entry, so return it */
311 /* now remove from the LRU queue and put it back at the
312 * head of the LRU queue.
314 cm_AdjustScacheLRU(scp);
321 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
324 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
325 /* There were no deleted scache objects that we could use. Try to find
326 * one that simply hasn't been used in a while.
328 for ( scp = cm_data.scacheLRULastp;
330 scp = (cm_scache_t *) osi_QPrev(&scp->q))
332 /* It is possible for the refCount to be zero and for there still
333 * to be outstanding dirty buffers. If there are dirty buffers,
334 * we must not recycle the scp. */
335 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
336 if (!buf_DirtyBuffersExist(&scp->fid)) {
337 if (!lock_TryWrite(&scp->rw))
340 if (!cm_RecycleSCache(scp, 0)) {
341 /* we found an entry, so return it */
342 /* now remove from the LRU queue and put it back at the
343 * head of the LRU queue.
345 cm_AdjustScacheLRU(scp);
350 lock_ReleaseWrite(&scp->rw);
352 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
356 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
361 /* if we get here, we should allocate a new scache entry. We either are below
362 * quota or we have a leak and need to allocate a new one to avoid panicing.
364 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
365 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
366 "invalid cm_scache_t address");
367 memset(scp, 0, sizeof(cm_scache_t));
368 scp->magic = CM_SCACHE_MAGIC;
369 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
370 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
371 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
373 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
375 scp->serverLock = -1;
377 /* and put it in the LRU queue */
378 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
379 if (!cm_data.scacheLRULastp)
380 cm_data.scacheLRULastp = scp;
381 cm_data.currentSCaches++;
382 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
384 scp->allNextp = cm_data.allSCachesp;
385 cm_data.allSCachesp = scp;
389 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
392 fidp->volume = volume;
394 fidp->unique = unique;
395 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
398 /* like strcmp, only for fids */
399 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
401 if (ap->hash != bp->hash)
403 if (ap->vnode != bp->vnode)
405 if (ap->volume != bp->volume)
407 if (ap->unique != bp->unique)
409 if (ap->cell != bp->cell)
414 void cm_fakeSCacheInit(int newFile)
417 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
418 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
419 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
420 cm_data.fakeSCache.cbExpires = (time_t)-1;
421 /* can leave clientModTime at 0 */
422 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
423 cm_data.fakeSCache.unixModeBits = 0777;
424 cm_data.fakeSCache.length.LowPart = 1000;
425 cm_data.fakeSCache.linkCount = 1;
426 cm_data.fakeSCache.refCount = 1;
427 cm_data.fakeSCache.serverLock = -1;
428 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
430 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
431 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
432 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
436 cm_ValidateSCache(void)
438 cm_scache_t * scp, *lscp;
441 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
442 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
443 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
444 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
448 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
450 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
451 if (scp->magic != CM_SCACHE_MAGIC) {
452 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
453 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
456 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
457 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
458 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
461 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
462 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
463 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
466 if (i > cm_data.currentSCaches ) {
467 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
468 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
471 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
472 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
473 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
478 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
479 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
480 if (scp->magic != CM_SCACHE_MAGIC) {
481 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
482 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
485 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
486 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
487 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
490 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
491 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
492 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
495 if (i > cm_data.currentSCaches ) {
496 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
497 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
500 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
501 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
502 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
507 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
508 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
510 hash = CM_SCACHE_HASH(&scp->fid);
511 if (scp->magic != CM_SCACHE_MAGIC) {
512 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
513 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
516 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
517 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
518 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
521 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
522 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
523 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
527 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
528 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
534 return cm_dnlcValidate();
538 cm_SuspendSCache(void)
543 cm_GiveUpAllCallbacksAllServers(TRUE);
546 * After this call all servers are marked down.
547 * Do not clear the callbacks, instead change the
548 * expiration time so that the callbacks will be expired
549 * when the servers are marked back up. However, we
550 * want the callbacks to be preserved as long as the
551 * servers are down. That way if the machine resumes
552 * without network, the stat cache item will still be
557 lock_ObtainWrite(&cm_scacheLock);
558 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
559 if (scp->cbServerp) {
560 if (scp->flags & CM_SCACHEFLAG_PURERO) {
561 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
563 if (volp->cbExpiresRO == scp->cbExpires)
564 volp->cbExpiresRO = now+1;
568 scp->cbExpires = now+1;
571 lock_ReleaseWrite(&cm_scacheLock);
575 cm_ShutdownSCache(void)
579 lock_ObtainWrite(&cm_scacheLock);
581 for ( scp = cm_data.allSCachesp; scp;
582 scp = scp->allNextp ) {
583 if (scp->randomACLp) {
584 lock_ReleaseWrite(&cm_scacheLock);
585 lock_ObtainWrite(&scp->rw);
586 lock_ObtainWrite(&cm_scacheLock);
587 cm_FreeAllACLEnts(scp);
588 lock_ReleaseWrite(&scp->rw);
591 if (scp->cbServerp) {
592 cm_PutServer(scp->cbServerp);
593 scp->cbServerp = NULL;
596 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
600 freeBtree(scp->dirBplus);
601 scp->dirBplus = NULL;
602 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
603 lock_FinalizeRWLock(&scp->dirlock);
605 lock_FinalizeRWLock(&scp->rw);
606 lock_FinalizeRWLock(&scp->bufCreateLock);
608 lock_ReleaseWrite(&cm_scacheLock);
610 cm_GiveUpAllCallbacksAllServers(FALSE);
612 return cm_dnlcShutdown();
615 void cm_InitSCache(int newFile, long maxSCaches)
617 static osi_once_t once;
619 if (osi_Once(&once)) {
620 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
622 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
623 cm_data.allSCachesp = NULL;
624 cm_data.currentSCaches = 0;
625 cm_data.maxSCaches = maxSCaches;
626 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
630 for ( scp = cm_data.allSCachesp; scp;
631 scp = scp->allNextp ) {
632 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
633 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
635 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
637 scp->cbServerp = NULL;
639 scp->volumeCreationDate = 0;
640 scp->fileLocksH = NULL;
641 scp->fileLocksT = NULL;
642 scp->serverLock = (-1);
643 scp->lastRefreshCycle = 0;
644 scp->exclusiveLocks = 0;
645 scp->sharedLocks = 0;
652 scp->dirBplus = NULL;
653 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
655 scp->waitQueueT = NULL;
656 scp->flags &= ~CM_SCACHEFLAG_WAITING;
659 cm_allFileLocks = NULL;
660 cm_freeFileLocks = NULL;
661 cm_lockRefreshCycle = 0;
662 cm_fakeSCacheInit(newFile);
663 cm_allFreeWaiters = NULL;
664 cm_dnlcInit(newFile);
669 /* version that doesn't bother creating the entry if we don't find it */
670 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
675 hash = CM_SCACHE_HASH(fidp);
677 if (fidp->cell == 0) {
681 lock_ObtainRead(&cm_scacheLock);
682 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
683 if (cm_FidCmp(fidp, &scp->fid) == 0) {
684 cm_HoldSCacheNoLock(scp);
685 lock_ConvertRToW(&cm_scacheLock);
686 cm_AdjustScacheLRU(scp);
687 lock_ReleaseWrite(&cm_scacheLock);
691 lock_ReleaseRead(&cm_scacheLock);
695 #ifdef DEBUG_REFCOUNT
696 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
697 cm_req_t *reqp, char * file, long line)
699 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
704 cm_scache_t *scp = NULL;
706 cm_volume_t *volp = NULL;
708 int special = 0; // yj: boolean variable to test if file is on root.afs
710 extern cm_fid_t cm_rootFid;
712 hash = CM_SCACHE_HASH(fidp);
715 return CM_ERROR_INVAL;
717 #ifdef AFS_FREELANCE_CLIENT
718 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
719 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
720 !(fidp->vnode==0x1 && fidp->unique==0x1));
721 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
722 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
723 fidp->vnode==0x1 && fidp->unique==0x1);
726 // yj: check if we have the scp, if so, we don't need
727 // to do anything else
728 lock_ObtainWrite(&cm_scacheLock);
729 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
730 if (cm_FidCmp(fidp, &scp->fid) == 0) {
731 #ifdef DEBUG_REFCOUNT
732 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
733 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
735 #ifdef AFS_FREELANCE_CLIENT
736 if (cm_freelanceEnabled && special &&
737 cm_data.fakeDirVersion != scp->dataVersion)
740 cm_HoldSCacheNoLock(scp);
742 cm_AdjustScacheLRU(scp);
743 lock_ReleaseWrite(&cm_scacheLock);
748 // yj: when we get here, it means we don't have an scp
749 // so we need to either load it or fake it, depending
750 // on whether the file is "special", see below.
752 // yj: if we're trying to get an scp for a file that's
753 // on root.afs of homecell, we want to handle it specially
754 // because we have to fill in the status stuff 'coz we
755 // don't want trybulkstat to fill it in for us
756 #ifdef AFS_FREELANCE_CLIENT
757 if (cm_freelanceEnabled && isRoot) {
758 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
759 /* freelance: if we are trying to get the root scp for the first
760 * time, we will just put in a place holder entry.
765 if (cm_freelanceEnabled && special) {
766 lock_ReleaseWrite(&cm_scacheLock);
767 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
769 if (cm_getLocalMountPointChange()) {
770 cm_clearLocalMountPointChange();
771 cm_reInitLocalMountPoints();
774 lock_ObtainWrite(&cm_scacheLock);
776 scp = cm_GetNewSCache(); /* returns scp->rw held */
778 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
779 lock_ReleaseWrite(&cm_scacheLock);
780 return CM_ERROR_WOULDBLOCK;
783 lock_ReleaseWrite(&cm_scacheLock);
784 lock_ObtainWrite(&scp->rw);
785 lock_ObtainWrite(&cm_scacheLock);
788 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
789 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
790 scp->dotdotFid.unique=1;
791 scp->dotdotFid.vnode=1;
792 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
793 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
794 scp->nextp = cm_data.scacheHashTablep[hash];
795 cm_data.scacheHashTablep[hash] = scp;
796 scp->flags |= CM_SCACHEFLAG_INHASH;
799 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
801 /* must be called after the scp->fid is set */
802 cm_FreelanceFetchMountPointString(scp);
803 cm_FreelanceFetchFileType(scp);
805 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
806 scp->length.HighPart = 0;
808 scp->unixModeBits=0777;
809 scp->clientModTime=FakeFreelanceModTime;
810 scp->serverModTime=FakeFreelanceModTime;
811 scp->parentUnique = 0x1;
812 scp->parentVnode=0x1;
814 scp->dataVersion=cm_data.fakeDirVersion;
815 scp->bufDataVersionLow=cm_data.fakeDirVersion;
816 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
818 lock_ReleaseWrite(&scp->rw);
819 lock_ReleaseWrite(&cm_scacheLock);
821 #ifdef DEBUG_REFCOUNT
822 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
823 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
828 #endif /* AFS_FREELANCE_CLIENT */
830 /* otherwise, we need to find the volume */
831 if (!cm_freelanceEnabled || !isRoot) {
832 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
833 cellp = cm_FindCellByID(fidp->cell, 0);
835 return CM_ERROR_NOSUCHCELL;
837 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
840 lock_ObtainWrite(&cm_scacheLock);
843 /* otherwise, we have the volume, now reverify that the scp doesn't
844 * exist, and proceed.
846 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
847 if (cm_FidCmp(fidp, &scp->fid) == 0) {
848 #ifdef DEBUG_REFCOUNT
849 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
850 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
852 cm_HoldSCacheNoLock(scp);
853 cm_AdjustScacheLRU(scp);
854 lock_ReleaseWrite(&cm_scacheLock);
862 /* now, if we don't have the fid, recycle something */
863 scp = cm_GetNewSCache(); /* returns scp->rw held */
865 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
866 lock_ReleaseWrite(&cm_scacheLock);
869 return CM_ERROR_WOULDBLOCK;
871 #ifdef DEBUG_REFCOUNT
872 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
874 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
876 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
879 if (!cm_freelanceEnabled || !isRoot) {
880 /* if this scache entry represents a volume root then we need
881 * to copy the dotdotFipd from the volume structure where the
882 * "master" copy is stored (defect 11489)
884 if (volp->vol[ROVOL].ID == fidp->volume) {
885 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
886 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
887 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
888 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
889 scp->flags |= CM_SCACHEFLAG_RO;
890 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
891 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
893 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
894 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
899 scp->nextp = cm_data.scacheHashTablep[hash];
900 cm_data.scacheHashTablep[hash] = scp;
901 scp->flags |= CM_SCACHEFLAG_INHASH;
902 lock_ReleaseWrite(&scp->rw);
904 #ifdef DEBUG_REFCOUNT
905 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
907 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
909 /* XXX - The following fields in the cm_scache are
916 /* now we have a held scache entry; just return it */
918 #ifdef DEBUG_REFCOUNT
919 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
920 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
922 lock_ReleaseWrite(&cm_scacheLock);
926 /* Returns a held reference to the scache's parent
928 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
933 cm_scache_t * pscp = NULL;
935 lock_ObtainWrite(&cm_scacheLock);
936 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
938 if (cm_FidCmp(&scp->fid, &parent_fid)) {
939 i = CM_SCACHE_HASH(&parent_fid);
940 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
941 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
942 cm_HoldSCacheNoLock(pscp);
948 lock_ReleaseWrite(&cm_scacheLock);
953 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
955 cm_scache_waiter_t * w;
957 lock_ObtainWrite(&cm_scacheLock);
958 if (cm_allFreeWaiters == NULL) {
959 w = malloc(sizeof(*w));
960 memset(w, 0, sizeof(*w));
962 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
963 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
966 w->threadId = thrd_Current();
968 cm_HoldSCacheNoLock(scp);
972 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
973 lock_ReleaseWrite(&cm_scacheLock);
975 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
978 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
980 cm_scache_waiter_t * w;
983 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
985 lock_ObtainRead(&cm_scacheLock);
986 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
988 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
989 if (w->flags == flags && w->bufp == bufp) {
994 osi_assertx(w != NULL, "null cm_scache_waiter_t");
995 this_is_me = (w->threadId == thrd_Current());
996 lock_ReleaseRead(&cm_scacheLock);
999 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1003 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1005 lock_ObtainWrite(&cm_scacheLock);
1006 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1007 cm_ReleaseSCacheNoLock(scp);
1008 memset(w, 0, sizeof(*w));
1009 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1010 lock_ReleaseWrite(&cm_scacheLock);
1016 /* synchronize a fetch, store, read, write, fetch status or store status.
1017 * Called with scache mutex held, and returns with it held, but temporarily
1018 * drops it during the fetch.
1020 * At most one flag can be on in flags, if this is an RPC request.
1022 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1024 * There are a lot of weird restrictions here; here's an attempt to explain the
1025 * rationale for the concurrency restrictions implemented in this function.
1027 * First, although the file server will break callbacks when *another* machine
1028 * modifies a file or status block, the client itself is responsible for
1029 * concurrency control on its own requests. Callback breaking events are rare,
1030 * and simply invalidate any concurrent new status info.
1032 * In the absence of callback breaking messages, we need to know how to
1033 * synchronize incoming responses describing updates to files. We synchronize
1034 * operations that update the data version by comparing the data versions.
1035 * However, updates that do not update the data, but only the status, can't be
1036 * synchronized with fetches or stores, since there's nothing to compare
1037 * to tell which operation executed first at the server.
1039 * Thus, we can allow multiple ops that change file data, or dir data, and
1040 * fetches. However, status storing ops have to be done serially.
1042 * Furthermore, certain data-changing ops are incompatible: we can't read or
1043 * write a buffer while doing a truncate. We can't read and write the same
1044 * buffer at the same time, or write while fetching or storing, or read while
1045 * fetching a buffer (this may change). We can't fetch and store at the same
1048 * With respect to status, we can't read and write at the same time, read while
1049 * fetching, write while fetching or storing, or fetch and store at the same time.
1051 * We can't allow a get callback RPC to run in concurrently with something that
1052 * will return updated status, since we could start a call, have the server
1053 * return status, have another machine make an update to the status (which
1054 * doesn't change serverModTime), have the original machine get a new callback,
1055 * and then have the original machine merge in the early, old info from the
1056 * first call. At this point, the easiest way to avoid this problem is to have
1057 * getcallback calls conflict with all others for the same vnode. Other calls
1058 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1059 * vnode must be careful not to merge in their status unless they have obtained
1060 * a callback from the start of their call.
1062 * Note added 1/23/96
1063 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1064 * Each such RPC passes a FileLength parameter, which the server uses to do
1065 * pre-truncation if necessary. So if two RPC's are processed out of order at
1066 * the server, the one with the smaller FileLength will be processed last,
1067 * possibly resulting in a bogus truncation. The simplest way to avoid this
1068 * is to serialize all StoreData RPC's. This is the reason we defined
1069 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1071 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1072 afs_uint32 rights, afs_uint32 flags)
1074 osi_queueData_t *qdp;
1077 afs_uint32 outRights;
1079 afs_uint32 sleep_scp_flags = 0;
1080 afs_uint32 sleep_buf_cmflags = 0;
1081 afs_uint32 sleep_scp_bufs = 0;
1084 lock_AssertWrite(&scp->rw);
1086 /* lookup this first */
1087 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1090 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1093 /* Do the access check. Now we don't really do the access check
1094 * atomically, since the caller doesn't expect the parent dir to be
1095 * returned locked, and that is what we'd have to do to prevent a
1096 * callback breaking message on the parent due to a setacl call from
1097 * being processed while we're running. So, instead, we check things
1098 * here, and if things look fine with the access, we proceed to finish
1099 * the rest of this check. Sort of a hack, but probably good enough.
1103 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1104 /* if we're bringing in a new status block, ensure that
1105 * we aren't already doing so, and that no one is
1106 * changing the status concurrently, either. We need
1107 * to do this, even if the status is of a different
1108 * type, since we don't have the ability to figure out,
1109 * in the AFS 3 protocols, which status-changing
1110 * operation ran first, or even which order a read and
1111 * a write occurred in.
1113 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1114 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1115 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1119 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1120 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1121 /* if we're going to make an RPC to change the status, make sure
1122 * that no one is bringing in or sending out the status.
1124 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1125 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1126 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1129 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1130 (scp->bufReadsp || scp->bufWritesp)) {
1131 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1135 if (flags & CM_SCACHESYNC_FETCHDATA) {
1136 /* if we're bringing in a new chunk of data, make sure that
1137 * nothing is happening to that chunk, and that we aren't
1138 * changing the basic file status info, either.
1140 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1141 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1142 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1145 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1146 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1150 if (flags & CM_SCACHESYNC_STOREDATA) {
1151 /* same as fetch data */
1152 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1153 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1154 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1157 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1158 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1163 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1164 /* Don't allow concurrent StoreData RPC's */
1165 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1166 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1171 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1172 /* Don't allow more than one BKG store request */
1173 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1174 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1179 if (flags & CM_SCACHESYNC_LOCK) {
1180 /* Don't allow concurrent fiddling with lock lists */
1181 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1182 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1187 /* now the operations that don't correspond to making RPCs */
1188 if (flags & CM_SCACHESYNC_GETSTATUS) {
1189 /* we can use the status that's here, if we're not
1190 * bringing in new status.
1192 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1193 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1197 if (flags & CM_SCACHESYNC_SETSTATUS) {
1198 /* we can make a change to the local status, as long as
1199 * the status isn't changing now.
1201 * If we're fetching or storing a chunk of data, we can
1202 * change the status locally, since the fetch/store
1203 * operations don't change any of the data that we're
1206 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1207 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1208 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1212 if (flags & CM_SCACHESYNC_READ) {
1213 /* we're going to read the data, make sure that the
1214 * status is available, and that the data is here. It
1215 * is OK to read while storing the data back.
1217 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1218 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1221 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1222 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1225 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1226 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1230 if (flags & CM_SCACHESYNC_WRITE) {
1231 /* don't write unless the status is stable and the chunk
1234 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1235 CM_SCACHEFLAG_SIZESTORING)) {
1236 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1239 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1241 CM_BUF_CMWRITING))) {
1242 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1244 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1245 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1246 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1252 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1253 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1254 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1257 lock_ReleaseMutex(&bufp->mx);
1258 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1260 lock_ReleaseWrite(&scp->rw);
1261 lock_ObtainMutex(&bufp->mx);
1262 lock_ObtainWrite(&scp->rw);
1266 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1272 /* can't check access rights without a callback */
1273 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1275 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1276 return CM_ERROR_READONLY;
1278 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1279 if (~outRights & rights)
1280 return CM_ERROR_NOACCESS;
1283 /* we don't know the required access rights */
1284 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1285 code = cm_GetAccessRights(scp, userp, reqp);
1287 lock_ReleaseWrite(&scp->rw);
1288 lock_ObtainMutex(&bufp->mx);
1289 lock_ObtainWrite(&scp->rw);
1297 /* if we get here, we're happy */
1301 /* first check if we're not supposed to wait: fail
1302 * in this case, returning with everything still locked.
1304 if (flags & CM_SCACHESYNC_NOWAIT)
1305 return CM_ERROR_WOULDBLOCK;
1307 /* These are used for minidump debugging */
1308 sleep_scp_flags = scp->flags; /* so we know why we slept */
1309 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1310 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1312 /* wait here, then try again */
1313 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1314 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1316 scp->waitRequests++;
1317 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1318 scp, scp->waitCount, scp->waitRequests);
1320 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1321 scp->flags |= CM_SCACHEFLAG_WAITING;
1322 scp->waitCount = scp->waitRequests = 1;
1325 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1329 lock_ReleaseMutex(&bufp->mx);
1330 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1332 lock_ObtainMutex(&bufp->mx);
1333 lock_ObtainWrite(&scp->rw);
1334 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1336 cm_UpdateServerPriority();
1339 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1340 scp, scp->waitCount, scp->waitRequests);
1341 if (scp->waitCount == 0) {
1342 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1343 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1344 scp->waitRequests = 0;
1346 } /* big while loop */
1348 /* now, update the recorded state for RPC-type calls */
1349 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1350 scp->flags |= CM_SCACHEFLAG_FETCHING;
1351 if (flags & CM_SCACHESYNC_STORESTATUS)
1352 scp->flags |= CM_SCACHEFLAG_STORING;
1353 if (flags & CM_SCACHESYNC_SETSIZE)
1354 scp->flags |= CM_SCACHEFLAG_SIZESETTING;
1355 if (flags & CM_SCACHESYNC_STORESIZE)
1356 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1357 if (flags & CM_SCACHESYNC_GETCALLBACK)
1358 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1359 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1360 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1361 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1362 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1363 if (flags & CM_SCACHESYNC_LOCK)
1364 scp->flags |= CM_SCACHEFLAG_LOCKING;
1366 /* now update the buffer pointer */
1367 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1368 /* ensure that the buffer isn't already in the I/O list */
1369 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1370 tbufp = osi_GetQData(qdp);
1371 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1374 /* queue a held reference to the buffer in the "reading" I/O list */
1375 qdp = osi_QDAlloc();
1376 osi_SetQData(qdp, bufp);
1379 bufp->cmFlags |= CM_BUF_CMFETCHING;
1380 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1383 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1384 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1385 "attempting to store extents on a non-file object");
1387 /* ensure that the buffer isn't already in the I/O list */
1388 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1389 tbufp = osi_GetQData(qdp);
1390 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1393 /* queue a held reference to the buffer in the "writing" I/O list */
1394 qdp = osi_QDAlloc();
1395 osi_SetQData(qdp, bufp);
1397 bufp->cmFlags |= CM_BUF_CMSTORING;
1398 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1401 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1402 /* mark the buffer as being written to. */
1403 bufp->cmFlags |= CM_BUF_CMWRITING;
1409 /* for those syncops that setup for RPCs.
1410 * Called with scache locked.
1412 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1414 osi_queueData_t *qdp;
1417 lock_AssertWrite(&scp->rw);
1419 /* now, update the recorded state for RPC-type calls */
1420 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1421 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1422 if (flags & CM_SCACHESYNC_STORESTATUS)
1423 scp->flags &= ~CM_SCACHEFLAG_STORING;
1424 if (flags & CM_SCACHESYNC_SETSIZE)
1425 scp->flags &= ~CM_SCACHEFLAG_SIZESETTING;
1426 if (flags & CM_SCACHESYNC_STORESIZE)
1427 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1428 if (flags & CM_SCACHESYNC_GETCALLBACK)
1429 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1430 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1431 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1432 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1433 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1434 if (flags & CM_SCACHESYNC_LOCK)
1435 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1437 /* now update the buffer pointer */
1438 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1441 /* ensure that the buffer is in the I/O list */
1442 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1443 tbufp = osi_GetQData(qdp);
1448 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1452 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1453 if (bufp->flags & CM_BUF_WAITING) {
1454 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1455 osi_Wakeup((LONG_PTR) &bufp);
1461 /* now update the buffer pointer */
1462 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1464 /* ensure that the buffer is in the I/O list */
1465 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1466 tbufp = osi_GetQData(qdp);
1471 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1475 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1476 if (bufp->flags & CM_BUF_WAITING) {
1477 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1478 osi_Wakeup((LONG_PTR) &bufp);
1484 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1485 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1486 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1489 /* and wakeup anyone who is waiting */
1490 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1491 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1492 osi_Wakeup((LONG_PTR) &scp->flags);
1496 /* merge in a response from an RPC. The scp must be locked, and the callback
1499 * Don't overwrite any status info that is dirty, since we could have a store
1500 * operation (such as store data) that merges some info in, and we don't want
1501 * to lose the local updates. Typically, there aren't many updates we do
1502 * locally, anyway, probably only mtime.
1504 * There is probably a bug in here where a chmod (which doesn't change
1505 * serverModTime) that occurs between two fetches, both of whose responses are
1506 * handled after the callback breaking is done, but only one of whose calls
1507 * started before that, can cause old info to be merged from the first call.
1509 void cm_MergeStatus(cm_scache_t *dscp,
1510 cm_scache_t *scp, AFSFetchStatus *statusp,
1511 AFSVolSync *volsyncp,
1512 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1514 afs_uint64 dataVersion;
1515 struct cm_volume *volp = NULL;
1516 struct cm_cell *cellp = NULL;
1518 // yj: i want to create some fake status for the /afs directory and the
1519 // entries under that directory
1520 #ifdef AFS_FREELANCE_CLIENT
1521 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1522 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1523 if (scp == cm_data.rootSCachep) {
1524 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1525 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1526 statusp->Length = cm_fakeDirSize;
1527 statusp->Length_hi = 0;
1529 statusp->FileType = scp->fileType;
1530 statusp->Length = scp->length.LowPart;
1531 statusp->Length_hi = scp->length.HighPart;
1533 statusp->InterfaceVersion = 0x1;
1534 statusp->LinkCount = scp->linkCount;
1535 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1536 statusp->Author = 0x1;
1537 statusp->Owner = 0x0;
1538 statusp->CallerAccess = 0x9;
1539 statusp->AnonymousAccess = 0x9;
1540 statusp->UnixModeBits = 0777;
1541 statusp->ParentVnode = 0x1;
1542 statusp->ParentUnique = 0x1;
1543 statusp->ResidencyMask = 0;
1544 statusp->ClientModTime = FakeFreelanceModTime;
1545 statusp->ServerModTime = FakeFreelanceModTime;
1547 statusp->SyncCounter = 0;
1548 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1549 statusp->lockCount = 0;
1550 statusp->errorCode = 0;
1552 #endif /* AFS_FREELANCE_CLIENT */
1554 if (statusp->errorCode != 0) {
1555 scp->flags |= CM_SCACHEFLAG_EACCESS;
1556 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1558 scp->fileType = 0; /* unknown */
1560 scp->serverModTime = 0;
1561 scp->clientModTime = 0;
1562 scp->length.LowPart = 0;
1563 scp->length.HighPart = 0;
1564 scp->serverLength.LowPart = 0;
1565 scp->serverLength.HighPart = 0;
1569 scp->unixModeBits = 0;
1571 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1572 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1573 scp->fsLockCount = 0;
1576 scp->parentVnode = dscp->fid.vnode;
1577 scp->parentUnique = dscp->fid.unique;
1579 scp->parentVnode = 0;
1580 scp->parentUnique = 0;
1584 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1587 dataVersion = statusp->dataVersionHigh;
1589 dataVersion |= statusp->DataVersion;
1591 if (!(flags & CM_MERGEFLAG_FORCE) &&
1592 dataVersion < scp->dataVersion &&
1593 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1595 cellp = cm_FindCellByID(scp->fid.cell, 0);
1596 if (scp->cbServerp) {
1597 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1598 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1599 osi_Log2(afsd_logp, "old data from server %x volume %s",
1600 scp->cbServerp->addr.sin_addr.s_addr,
1601 volp ? volp->namep : "(unknown)");
1603 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1604 scp, scp->dataVersion, dataVersion);
1605 /* we have a number of data fetch/store operations running
1606 * concurrently, and we can tell which one executed last at the
1607 * server by its mtime.
1608 * Choose the one with the largest mtime, and ignore the rest.
1610 * These concurrent calls are incompatible with setting the
1611 * mtime, so we won't have a locally changed mtime here.
1613 * We could also have ACL info for a different user than usual,
1614 * in which case we have to do that part of the merge, anyway.
1615 * We won't have to worry about the info being old, since we
1616 * won't have concurrent calls
1617 * that change file status running from this machine.
1619 * Added 3/17/98: if we see data version regression on an RO
1620 * file, it's probably due to a server holding an out-of-date
1621 * replica, rather than to concurrent RPC's. Failures to
1622 * release replicas are now flagged by the volserver, but only
1623 * since AFS 3.4 5.22, so there are plenty of clients getting
1624 * out-of-date replicas out there.
1626 * If we discover an out-of-date replica, by this time it's too
1627 * late to go to another server and retry. Also, we can't
1628 * reject the merge, because then there is no way for
1629 * GetAccess to do its work, and the caller gets into an
1630 * infinite loop. So we just grin and bear it.
1632 if (!(scp->flags & CM_SCACHEFLAG_RO))
1636 if (cm_readonlyVolumeVersioning)
1637 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1639 scp->serverModTime = statusp->ServerModTime;
1641 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1642 scp->clientModTime = statusp->ClientModTime;
1644 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1645 scp->length.LowPart = statusp->Length;
1646 scp->length.HighPart = statusp->Length_hi;
1649 scp->serverLength.LowPart = statusp->Length;
1650 scp->serverLength.HighPart = statusp->Length_hi;
1652 scp->linkCount = statusp->LinkCount;
1653 scp->owner = statusp->Owner;
1654 scp->group = statusp->Group;
1655 scp->unixModeBits = statusp->UnixModeBits & 07777;
1657 if (statusp->FileType == File)
1658 scp->fileType = CM_SCACHETYPE_FILE;
1659 else if (statusp->FileType == Directory)
1660 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1661 else if (statusp->FileType == SymbolicLink) {
1662 if ((scp->unixModeBits & 0111) == 0)
1663 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1665 scp->fileType = CM_SCACHETYPE_SYMLINK;
1668 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1669 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1671 /* and other stuff */
1672 scp->parentVnode = statusp->ParentVnode;
1673 scp->parentUnique = statusp->ParentUnique;
1674 scp->fsLockCount = statusp->lockCount;
1676 /* and merge in the private acl cache info, if this is more than the public
1677 * info; merge in the public stuff in any case.
1679 scp->anyAccess = statusp->AnonymousAccess;
1681 if (userp != NULL) {
1682 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1685 if (scp->dataVersion != 0 &&
1686 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1687 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1689 * We now know that all of the data buffers that we have associated
1690 * with this scp are invalid. Subsequent operations will go faster
1691 * if the buffers are removed from the hash tables.
1693 * We do not remove directory buffers if the dataVersion delta is 1 because
1694 * those version numbers will be updated as part of the directory operation.
1696 * We do not remove storedata buffers because they will still be valid.
1701 cm_buf_t *bp, *prevBp, *nextBp;
1703 lock_ObtainWrite(&buf_globalLock);
1704 i = BUF_FILEHASH(&scp->fid);
1705 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1707 nextBp = bp->fileHashp;
1709 * if the buffer belongs to this stat cache entry
1710 * and the buffer mutex can be obtained, check the
1711 * reference count and if it is zero, remove the buffer
1712 * from the hash tables. If there are references,
1713 * the buffer might be updated to the current version
1714 * so leave it in place.
1716 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1717 lock_TryMutex(&bp->mx)) {
1718 if (bp->refCount == 0 &&
1719 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1720 prevBp = bp->fileHashBackp;
1721 bp->fileHashBackp = bp->fileHashp = NULL;
1723 prevBp->fileHashp = nextBp;
1725 cm_data.buf_fileHashTablepp[i] = nextBp;
1727 nextBp->fileHashBackp = prevBp;
1729 j = BUF_HASH(&bp->fid, &bp->offset);
1730 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1731 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1736 *lbpp = bp->hashp; /* hash out */
1739 bp->qFlags &= ~CM_BUF_QINHASH;
1741 lock_ReleaseMutex(&bp->mx);
1744 lock_ReleaseWrite(&buf_globalLock);
1747 /* We maintain a range of buffer dataVersion values which are considered
1748 * valid. This avoids the need to update the dataVersion on each buffer
1749 * object during an uncontested storeData operation. As a result this
1750 * merge status no longer has performance characteristics derived from
1751 * the size of the file.
1753 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1754 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1755 scp->bufDataVersionLow == 0)
1756 scp->bufDataVersionLow = dataVersion;
1758 scp->dataVersion = dataVersion;
1761 * If someone is waiting for status information, we can wake them up
1762 * now even though the entity that issued the FetchStatus may not
1763 * have completed yet.
1765 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1768 * We just successfully merged status on the stat cache object.
1769 * This means that the associated volume must be online.
1773 cellp = cm_FindCellByID(scp->fid.cell, 0);
1774 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1777 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1778 if (statep->state != vl_online) {
1779 lock_ObtainWrite(&volp->rw);
1780 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1781 statep->state = vl_online;
1782 lock_ReleaseWrite(&volp->rw);
1791 /* note that our stat cache info is incorrect, so force us eventually
1792 * to stat the file again. There may be dirty data associated with
1793 * this vnode, and we want to preserve that information.
1795 * This function works by simply simulating a loss of the callback.
1797 * This function must be called with the scache locked.
1799 void cm_DiscardSCache(cm_scache_t *scp)
1801 lock_AssertWrite(&scp->rw);
1802 if (scp->cbServerp) {
1803 cm_PutServer(scp->cbServerp);
1804 scp->cbServerp = NULL;
1807 scp->volumeCreationDate = 0;
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);