2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
27 /*extern void afsi_log(char *pattern, ...);*/
29 extern osi_hyper_t hzero;
32 osi_queue_t *cm_allFileLocks;
33 osi_queue_t *cm_freeFileLocks;
34 unsigned long cm_lockRefreshCycle;
36 /* lock for globals */
37 osi_rwlock_t cm_scacheLock;
39 /* Dummy scache entry for use with pioctl fids */
40 cm_scache_t cm_fakeSCache;
42 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
44 #ifdef AFS_FREELANCE_CLIENT
45 extern osi_mutex_t cm_Freelance_Lock;
49 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
53 lock_ObtainWrite(&cm_data.rootSCachep->rw);
54 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
55 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
57 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
58 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
60 return cm_data.rootSCachep;
64 /* must be called with cm_scacheLock write-locked! */
65 void cm_AdjustScacheLRU(cm_scache_t *scp)
67 lock_AssertWrite(&cm_scacheLock);
68 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
69 if (scp->flags & CM_SCACHEFLAG_DELETED) {
70 /* Since it has been deleted make it the first to be recycled. */
71 osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
73 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
77 /* call with cm_scacheLock write-locked and scp rw held */
78 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
84 lock_AssertWrite(&cm_scacheLock);
85 lock_AssertWrite(&scp->rw);
86 if (scp->flags & CM_SCACHEFLAG_INHASH) {
87 /* hash it out first */
88 i = CM_SCACHE_HASH(&scp->fid);
89 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
91 lscpp = &tscp->nextp, tscp = tscp->nextp) {
95 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
102 /* called with cm_scacheLock and scp write-locked */
103 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
106 /* destroy directory Bplus Tree */
108 LARGE_INTEGER start, end;
110 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
112 * We are not holding the dirlock and obtaining it
113 * requires that we drop the scp->rw. As a result
114 * we will leave the dirBplus tree intact but
115 * invalidate the version number so that whatever
116 * operation is currently active can safely complete
117 * but the contents will be ignored on the next
118 * directory operation.
120 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
124 QueryPerformanceCounter(&start);
126 freeBtree(scp->dirBplus);
127 scp->dirBplus = NULL;
128 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
129 QueryPerformanceCounter(&end);
132 lock_ReleaseWrite(&scp->dirlock);
134 bplus_free_time += (end.QuadPart - start.QuadPart);
139 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
140 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
142 if (scp->refCount != 0) {
146 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
147 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
149 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
154 cm_RemoveSCacheFromHashTable(scp);
157 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
158 osi_queueData_t *qdp;
161 while(qdp = scp->bufWritesp) {
162 bufp = osi_GetQData(qdp);
163 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
166 lock_ObtainMutex(&bufp->mx);
167 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
168 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
169 bufp->dirty_offset = 0;
170 bufp->dirty_length = 0;
171 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
172 bufp->error = VNOVNODE;
173 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
174 bufp->dirtyCounter++;
175 if (bufp->flags & CM_BUF_WAITING) {
176 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
177 osi_Wakeup((long) &bufp);
179 lock_ReleaseMutex(&bufp->mx);
183 while(qdp = scp->bufReadsp) {
184 bufp = osi_GetQData(qdp);
185 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
188 lock_ObtainMutex(&bufp->mx);
189 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMFETCHING);
190 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
191 bufp->dirty_offset = 0;
192 bufp->dirty_length = 0;
193 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
194 bufp->error = VNOVNODE;
195 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
196 bufp->dirtyCounter++;
197 if (bufp->flags & CM_BUF_WAITING) {
198 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
199 osi_Wakeup((long) &bufp);
201 lock_ReleaseMutex(&bufp->mx);
205 buf_CleanDirtyBuffers(scp);
207 /* look for things that shouldn't still be set */
208 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
209 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
213 /* invalidate so next merge works fine;
214 * also initialize some flags */
216 _InterlockedAnd(&scp->flags,
217 ~(CM_SCACHEFLAG_STATD
218 | CM_SCACHEFLAG_DELETED
220 | CM_SCACHEFLAG_PURERO
221 | CM_SCACHEFLAG_OVERQUOTA
222 | CM_SCACHEFLAG_OUTOFSPACE
223 | CM_SCACHEFLAG_EACCESS));
224 scp->serverModTime = 0;
225 scp->dataVersion = CM_SCACHE_VERSION_BAD;
226 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
227 scp->bulkStatProgress = hzero;
229 scp->waitQueueT = NULL;
231 if (scp->cbServerp) {
232 cm_PutServer(scp->cbServerp);
233 scp->cbServerp = NULL;
236 scp->volumeCreationDate = 0;
244 /* remove from dnlc */
248 /* discard cached status; if non-zero, Close
249 * tried to store this to server but failed */
252 /* discard symlink info */
253 scp->mountPointStringp[0] = '\0';
254 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
255 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
257 /* reset locking info */
258 scp->fileLocksH = NULL;
259 scp->fileLocksT = NULL;
260 scp->serverLock = (-1);
261 scp->exclusiveLocks = 0;
262 scp->sharedLocks = 0;
263 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
264 scp->fsLockCount = 0;
266 /* not locked, but there can be no references to this guy
267 * while we hold the global refcount lock.
269 cm_FreeAllACLEnts(scp);
271 cm_ResetSCacheDirectory(scp, 0);
277 * called with cm_scacheLock write-locked; find a vnode to recycle.
278 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
279 * returns scp->mx held.
281 cm_scache_t *cm_GetNewSCache(void)
286 lock_AssertWrite(&cm_scacheLock);
288 /* first pass - look for deleted objects */
289 for ( scp = cm_data.scacheLRULastp;
291 scp = (cm_scache_t *) osi_QPrev(&scp->q))
293 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
294 "invalid cm_scache_t address");
296 if (scp->refCount == 0) {
297 if (scp->flags & CM_SCACHEFLAG_DELETED) {
298 if (!lock_TryWrite(&scp->rw))
301 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%p", scp);
302 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
304 /* we found an entry, so return it */
305 /* now remove from the LRU queue and put it back at the
306 * head of the LRU queue.
308 cm_AdjustScacheLRU(scp);
313 lock_ReleaseWrite(&scp->rw);
314 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%p", scp);
315 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
316 if (!lock_TryWrite(&scp->rw))
319 /* we found an entry, so return it */
320 /* now remove from the LRU queue and put it back at the
321 * head of the LRU queue.
323 cm_AdjustScacheLRU(scp);
330 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
333 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
334 /* There were no deleted scache objects that we could use. Try to find
335 * one that simply hasn't been used in a while.
337 for ( scp = cm_data.scacheLRULastp;
339 scp = (cm_scache_t *) osi_QPrev(&scp->q))
341 /* It is possible for the refCount to be zero and for there still
342 * to be outstanding dirty buffers. If there are dirty buffers,
343 * we must not recycle the scp. */
344 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
345 if (!buf_DirtyBuffersExist(&scp->fid)) {
346 if (!lock_TryWrite(&scp->rw))
349 if (!cm_RecycleSCache(scp, 0)) {
350 /* we found an entry, so return it */
351 /* now remove from the LRU queue and put it back at the
352 * head of the LRU queue.
354 cm_AdjustScacheLRU(scp);
359 lock_ReleaseWrite(&scp->rw);
361 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
365 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
370 /* if we get here, we should allocate a new scache entry. We either are below
371 * quota or we have a leak and need to allocate a new one to avoid panicing.
373 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
374 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
375 "invalid cm_scache_t address");
376 memset(scp, 0, sizeof(cm_scache_t));
377 scp->magic = CM_SCACHE_MAGIC;
378 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
379 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
380 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
382 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
384 scp->serverLock = -1;
386 /* and put it in the LRU queue */
387 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
388 if (!cm_data.scacheLRULastp)
389 cm_data.scacheLRULastp = scp;
390 cm_data.currentSCaches++;
391 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
393 scp->allNextp = cm_data.allSCachesp;
394 cm_data.allSCachesp = scp;
398 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
401 fidp->volume = volume;
403 fidp->unique = unique;
404 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
407 /* like strcmp, only for fids */
408 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
410 if (ap->hash != bp->hash)
412 if (ap->vnode != bp->vnode)
414 if (ap->volume != bp->volume)
416 if (ap->unique != bp->unique)
418 if (ap->cell != bp->cell)
423 void cm_fakeSCacheInit(int newFile)
426 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
427 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
428 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
429 cm_data.fakeSCache.cbExpires = (time_t)-1;
430 /* can leave clientModTime at 0 */
431 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
432 cm_data.fakeSCache.unixModeBits = 0777;
433 cm_data.fakeSCache.length.LowPart = 1000;
434 cm_data.fakeSCache.linkCount = 1;
435 cm_data.fakeSCache.refCount = 1;
436 cm_data.fakeSCache.serverLock = -1;
437 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
439 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
440 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
441 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
445 cm_ValidateSCache(void)
447 cm_scache_t * scp, *lscp;
450 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
451 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
452 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
453 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
457 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
459 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
460 if (scp->magic != CM_SCACHE_MAGIC) {
461 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
462 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
465 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
466 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
467 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
470 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
471 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
472 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
475 if (i > cm_data.currentSCaches ) {
476 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
477 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
480 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
481 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
482 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
487 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
488 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
489 if (scp->magic != CM_SCACHE_MAGIC) {
490 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
491 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
494 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
495 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
496 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
499 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
500 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
501 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
504 if (i > cm_data.currentSCaches ) {
505 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
506 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
509 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
510 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
511 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
516 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
517 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
519 hash = CM_SCACHE_HASH(&scp->fid);
520 if (scp->magic != CM_SCACHE_MAGIC) {
521 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
522 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
525 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
526 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
527 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
530 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
531 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
532 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
536 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
537 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
543 return cm_dnlcValidate();
547 cm_SuspendSCache(void)
552 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
555 * After this call all servers are marked down.
556 * Do not clear the callbacks, instead change the
557 * expiration time so that the callbacks will be expired
558 * when the servers are marked back up. However, we
559 * want the callbacks to be preserved as long as the
560 * servers are down. That way if the machine resumes
561 * without network, the stat cache item will still be
566 lock_ObtainWrite(&cm_scacheLock);
567 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
568 if (scp->cbServerp) {
569 if (scp->flags & CM_SCACHEFLAG_PURERO) {
570 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
572 if (volp->cbExpiresRO == scp->cbExpires)
573 volp->cbExpiresRO = now+1;
577 scp->cbExpires = now+1;
580 lock_ReleaseWrite(&cm_scacheLock);
584 cm_ShutdownSCache(void)
586 cm_scache_t * scp, * nextp;
588 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
590 lock_ObtainWrite(&cm_scacheLock);
592 for ( scp = cm_data.allSCachesp; scp;
594 nextp = scp->allNextp;
595 lock_ReleaseWrite(&cm_scacheLock);
597 lock_ObtainWrite(&scp->dirlock);
599 lock_ObtainWrite(&scp->rw);
600 lock_ObtainWrite(&cm_scacheLock);
602 if (scp->randomACLp) {
603 cm_FreeAllACLEnts(scp);
606 if (scp->cbServerp) {
607 cm_PutServer(scp->cbServerp);
608 scp->cbServerp = NULL;
611 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
612 lock_ReleaseWrite(&scp->rw);
616 freeBtree(scp->dirBplus);
617 scp->dirBplus = NULL;
618 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
619 lock_ReleaseWrite(&scp->dirlock);
620 lock_FinalizeRWLock(&scp->dirlock);
622 lock_FinalizeRWLock(&scp->rw);
623 lock_FinalizeRWLock(&scp->bufCreateLock);
625 lock_ReleaseWrite(&cm_scacheLock);
627 return cm_dnlcShutdown();
630 void cm_InitSCache(int newFile, long maxSCaches)
632 static osi_once_t once;
634 if (osi_Once(&once)) {
635 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
637 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
638 cm_data.allSCachesp = NULL;
639 cm_data.currentSCaches = 0;
640 cm_data.maxSCaches = maxSCaches;
641 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
645 for ( scp = cm_data.allSCachesp; scp;
646 scp = scp->allNextp ) {
647 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
648 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
650 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
652 scp->cbServerp = NULL;
654 scp->volumeCreationDate = 0;
655 scp->fileLocksH = NULL;
656 scp->fileLocksT = NULL;
657 scp->serverLock = (-1);
658 scp->lastRefreshCycle = 0;
659 scp->exclusiveLocks = 0;
660 scp->sharedLocks = 0;
667 scp->dirBplus = NULL;
668 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
670 scp->waitQueueT = NULL;
671 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
674 cm_allFileLocks = NULL;
675 cm_freeFileLocks = NULL;
676 cm_lockRefreshCycle = 0;
677 cm_fakeSCacheInit(newFile);
678 cm_allFreeWaiters = NULL;
679 cm_dnlcInit(newFile);
684 /* version that doesn't bother creating the entry if we don't find it */
685 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
690 hash = CM_SCACHE_HASH(fidp);
692 if (fidp->cell == 0) {
696 lock_ObtainRead(&cm_scacheLock);
697 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
698 if (cm_FidCmp(fidp, &scp->fid) == 0) {
699 cm_HoldSCacheNoLock(scp);
700 lock_ConvertRToW(&cm_scacheLock);
701 cm_AdjustScacheLRU(scp);
702 lock_ReleaseWrite(&cm_scacheLock);
706 lock_ReleaseRead(&cm_scacheLock);
710 #ifdef DEBUG_REFCOUNT
711 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
712 cm_req_t *reqp, char * file, long line)
714 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
719 cm_scache_t *scp = NULL;
721 cm_volume_t *volp = NULL;
723 int special = 0; // yj: boolean variable to test if file is on root.afs
725 extern cm_fid_t cm_rootFid;
727 hash = CM_SCACHE_HASH(fidp);
730 return CM_ERROR_INVAL;
732 #ifdef AFS_FREELANCE_CLIENT
733 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
734 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
735 !(fidp->vnode==0x1 && fidp->unique==0x1));
736 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
737 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
738 fidp->vnode==0x1 && fidp->unique==0x1);
741 // yj: check if we have the scp, if so, we don't need
742 // to do anything else
743 lock_ObtainWrite(&cm_scacheLock);
744 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
745 if (cm_FidCmp(fidp, &scp->fid) == 0) {
746 #ifdef DEBUG_REFCOUNT
747 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
748 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
750 #ifdef AFS_FREELANCE_CLIENT
751 if (cm_freelanceEnabled && special &&
752 cm_data.fakeDirVersion != scp->dataVersion)
755 cm_HoldSCacheNoLock(scp);
757 cm_AdjustScacheLRU(scp);
758 lock_ReleaseWrite(&cm_scacheLock);
763 // yj: when we get here, it means we don't have an scp
764 // so we need to either load it or fake it, depending
765 // on whether the file is "special", see below.
767 // yj: if we're trying to get an scp for a file that's
768 // on root.afs of homecell, we want to handle it specially
769 // because we have to fill in the status stuff 'coz we
770 // don't want trybulkstat to fill it in for us
771 #ifdef AFS_FREELANCE_CLIENT
772 if (cm_freelanceEnabled && isRoot) {
773 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
774 /* freelance: if we are trying to get the root scp for the first
775 * time, we will just put in a place holder entry.
780 if (cm_freelanceEnabled && special) {
781 lock_ReleaseWrite(&cm_scacheLock);
782 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
784 if (cm_getLocalMountPointChange()) {
785 cm_clearLocalMountPointChange();
786 cm_reInitLocalMountPoints();
789 lock_ObtainWrite(&cm_scacheLock);
791 scp = cm_GetNewSCache(); /* returns scp->rw held */
793 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
794 lock_ReleaseWrite(&cm_scacheLock);
795 return CM_ERROR_WOULDBLOCK;
798 lock_ReleaseWrite(&cm_scacheLock);
799 lock_ObtainWrite(&scp->rw);
800 lock_ObtainWrite(&cm_scacheLock);
803 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
804 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
805 scp->dotdotFid.unique=1;
806 scp->dotdotFid.vnode=1;
807 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
808 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
809 scp->nextp = cm_data.scacheHashTablep[hash];
810 cm_data.scacheHashTablep[hash] = scp;
811 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
814 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
816 /* must be called after the scp->fid is set */
817 cm_FreelanceFetchMountPointString(scp);
818 cm_FreelanceFetchFileType(scp);
820 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
821 scp->length.HighPart = 0;
823 scp->unixModeBits=0777;
824 scp->clientModTime=FakeFreelanceModTime;
825 scp->serverModTime=FakeFreelanceModTime;
826 scp->parentUnique = 0x1;
827 scp->parentVnode=0x1;
829 scp->dataVersion=cm_data.fakeDirVersion;
830 scp->bufDataVersionLow=cm_data.fakeDirVersion;
831 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
833 lock_ReleaseWrite(&scp->rw);
834 lock_ReleaseWrite(&cm_scacheLock);
836 #ifdef DEBUG_REFCOUNT
837 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
838 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
843 #endif /* AFS_FREELANCE_CLIENT */
845 /* otherwise, we need to find the volume */
846 if (!cm_freelanceEnabled || !isRoot) {
847 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
848 cellp = cm_FindCellByID(fidp->cell, 0);
850 return CM_ERROR_NOSUCHCELL;
852 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
855 lock_ObtainWrite(&cm_scacheLock);
858 /* otherwise, we have the volume, now reverify that the scp doesn't
859 * exist, and proceed.
861 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
862 if (cm_FidCmp(fidp, &scp->fid) == 0) {
863 #ifdef DEBUG_REFCOUNT
864 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
865 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
867 cm_HoldSCacheNoLock(scp);
868 cm_AdjustScacheLRU(scp);
869 lock_ReleaseWrite(&cm_scacheLock);
877 /* now, if we don't have the fid, recycle something */
878 scp = cm_GetNewSCache(); /* returns scp->rw held */
880 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
881 lock_ReleaseWrite(&cm_scacheLock);
884 return CM_ERROR_WOULDBLOCK;
886 #ifdef DEBUG_REFCOUNT
887 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
889 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
891 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
894 if (!cm_freelanceEnabled || !isRoot) {
895 /* if this scache entry represents a volume root then we need
896 * to copy the dotdotFipd from the volume structure where the
897 * "master" copy is stored (defect 11489)
899 if (volp->vol[ROVOL].ID == fidp->volume) {
900 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
901 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
902 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
903 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
904 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
905 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
906 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
908 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
909 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
914 scp->nextp = cm_data.scacheHashTablep[hash];
915 cm_data.scacheHashTablep[hash] = scp;
916 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
917 lock_ReleaseWrite(&scp->rw);
919 #ifdef DEBUG_REFCOUNT
920 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
922 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
924 /* XXX - The following fields in the cm_scache are
931 /* now we have a held scache entry; just return it */
933 #ifdef DEBUG_REFCOUNT
934 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
935 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
937 lock_ReleaseWrite(&cm_scacheLock);
941 /* Returns a held reference to the scache's parent
943 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
948 cm_scache_t * pscp = NULL;
950 lock_ObtainWrite(&cm_scacheLock);
951 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
953 if (cm_FidCmp(&scp->fid, &parent_fid)) {
954 i = CM_SCACHE_HASH(&parent_fid);
955 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
956 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
957 cm_HoldSCacheNoLock(pscp);
963 lock_ReleaseWrite(&cm_scacheLock);
968 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
970 cm_scache_waiter_t * w;
972 lock_ObtainWrite(&cm_scacheLock);
973 if (cm_allFreeWaiters == NULL) {
974 w = malloc(sizeof(*w));
975 memset(w, 0, sizeof(*w));
977 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
978 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
981 w->threadId = thrd_Current();
983 cm_HoldSCacheNoLock(scp);
987 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
988 lock_ReleaseWrite(&cm_scacheLock);
990 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
993 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
995 cm_scache_waiter_t * w;
998 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
1000 lock_ObtainRead(&cm_scacheLock);
1001 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
1003 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
1004 if (w->flags == flags && w->bufp == bufp) {
1009 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1010 this_is_me = (w->threadId == thrd_Current());
1011 lock_ReleaseRead(&cm_scacheLock);
1014 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1018 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1020 lock_ObtainWrite(&cm_scacheLock);
1021 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1022 cm_ReleaseSCacheNoLock(scp);
1023 memset(w, 0, sizeof(*w));
1024 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1025 lock_ReleaseWrite(&cm_scacheLock);
1031 /* synchronize a fetch, store, read, write, fetch status or store status.
1032 * Called with scache mutex held, and returns with it held, but temporarily
1033 * drops it during the fetch.
1035 * At most one flag can be on in flags, if this is an RPC request.
1037 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1039 * There are a lot of weird restrictions here; here's an attempt to explain the
1040 * rationale for the concurrency restrictions implemented in this function.
1042 * First, although the file server will break callbacks when *another* machine
1043 * modifies a file or status block, the client itself is responsible for
1044 * concurrency control on its own requests. Callback breaking events are rare,
1045 * and simply invalidate any concurrent new status info.
1047 * In the absence of callback breaking messages, we need to know how to
1048 * synchronize incoming responses describing updates to files. We synchronize
1049 * operations that update the data version by comparing the data versions.
1050 * However, updates that do not update the data, but only the status, can't be
1051 * synchronized with fetches or stores, since there's nothing to compare
1052 * to tell which operation executed first at the server.
1054 * Thus, we can allow multiple ops that change file data, or dir data, and
1055 * fetches. However, status storing ops have to be done serially.
1057 * Furthermore, certain data-changing ops are incompatible: we can't read or
1058 * write a buffer while doing a truncate. We can't read and write the same
1059 * buffer at the same time, or write while fetching or storing, or read while
1060 * fetching a buffer (this may change). We can't fetch and store at the same
1063 * With respect to status, we can't read and write at the same time, read while
1064 * fetching, write while fetching or storing, or fetch and store at the same time.
1066 * We can't allow a get callback RPC to run in concurrently with something that
1067 * will return updated status, since we could start a call, have the server
1068 * return status, have another machine make an update to the status (which
1069 * doesn't change serverModTime), have the original machine get a new callback,
1070 * and then have the original machine merge in the early, old info from the
1071 * first call. At this point, the easiest way to avoid this problem is to have
1072 * getcallback calls conflict with all others for the same vnode. Other calls
1073 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1074 * vnode must be careful not to merge in their status unless they have obtained
1075 * a callback from the start of their call.
1077 * Note added 1/23/96
1078 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1079 * Each such RPC passes a FileLength parameter, which the server uses to do
1080 * pre-truncation if necessary. So if two RPC's are processed out of order at
1081 * the server, the one with the smaller FileLength will be processed last,
1082 * possibly resulting in a bogus truncation. The simplest way to avoid this
1083 * is to serialize all StoreData RPC's. This is the reason we defined
1084 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1086 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1087 afs_uint32 rights, afs_uint32 flags)
1089 osi_queueData_t *qdp;
1092 afs_uint32 outRights;
1094 afs_uint32 sleep_scp_flags = 0;
1095 afs_uint32 sleep_buf_cmflags = 0;
1096 afs_uint32 sleep_scp_bufs = 0;
1099 lock_AssertWrite(&scp->rw);
1101 /* lookup this first */
1102 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1105 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1108 /* Do the access check. Now we don't really do the access check
1109 * atomically, since the caller doesn't expect the parent dir to be
1110 * returned locked, and that is what we'd have to do to prevent a
1111 * callback breaking message on the parent due to a setacl call from
1112 * being processed while we're running. So, instead, we check things
1113 * here, and if things look fine with the access, we proceed to finish
1114 * the rest of this check. Sort of a hack, but probably good enough.
1118 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1119 /* if we're bringing in a new status block, ensure that
1120 * we aren't already doing so, and that no one is
1121 * changing the status concurrently, either. We need
1122 * to do this, even if the status is of a different
1123 * type, since we don't have the ability to figure out,
1124 * in the AFS 3 protocols, which status-changing
1125 * operation ran first, or even which order a read and
1126 * a write occurred in.
1128 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1129 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1130 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1134 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1135 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1136 /* if we're going to make an RPC to change the status, make sure
1137 * that no one is bringing in or sending out the status.
1139 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1140 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1141 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1144 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1145 (scp->bufReadsp || scp->bufWritesp)) {
1146 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1150 if (flags & CM_SCACHESYNC_FETCHDATA) {
1151 /* if we're bringing in a new chunk of data, make sure that
1152 * nothing is happening to that chunk, and that we aren't
1153 * changing the basic file status info, either.
1155 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1156 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1157 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1160 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1161 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1165 if (flags & CM_SCACHESYNC_STOREDATA) {
1166 /* same as fetch data */
1167 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1168 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1169 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1172 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1173 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1178 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1179 /* Don't allow concurrent StoreData RPC's */
1180 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1181 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1186 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1187 /* Don't allow more than one BKG store request */
1188 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1189 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1194 if (flags & CM_SCACHESYNC_LOCK) {
1195 /* Don't allow concurrent fiddling with lock lists */
1196 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1197 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1202 /* now the operations that don't correspond to making RPCs */
1203 if (flags & CM_SCACHESYNC_GETSTATUS) {
1204 /* we can use the status that's here, if we're not
1205 * bringing in new status.
1207 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1208 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1212 if (flags & CM_SCACHESYNC_SETSTATUS) {
1213 /* we can make a change to the local status, as long as
1214 * the status isn't changing now.
1216 * If we're fetching or storing a chunk of data, we can
1217 * change the status locally, since the fetch/store
1218 * operations don't change any of the data that we're
1221 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1222 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1223 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1227 if (flags & CM_SCACHESYNC_READ) {
1228 /* we're going to read the data, make sure that the
1229 * status is available, and that the data is here. It
1230 * is OK to read while storing the data back.
1232 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1233 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1236 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1237 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1240 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1241 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1245 if (flags & CM_SCACHESYNC_WRITE) {
1246 /* don't write unless the status is stable and the chunk
1249 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1250 CM_SCACHEFLAG_SIZESTORING)) {
1251 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1254 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1256 CM_BUF_CMWRITING))) {
1257 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1259 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1260 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1261 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1267 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1268 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1269 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1272 lock_ReleaseMutex(&bufp->mx);
1273 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1275 lock_ReleaseWrite(&scp->rw);
1276 lock_ObtainMutex(&bufp->mx);
1277 lock_ObtainWrite(&scp->rw);
1281 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1287 /* can't check access rights without a callback */
1288 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1290 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1291 return CM_ERROR_READONLY;
1293 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1294 if (~outRights & rights)
1295 return CM_ERROR_NOACCESS;
1298 /* we don't know the required access rights */
1299 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1300 code = cm_GetAccessRights(scp, userp, reqp);
1302 lock_ReleaseWrite(&scp->rw);
1303 lock_ObtainMutex(&bufp->mx);
1304 lock_ObtainWrite(&scp->rw);
1312 /* if we get here, we're happy */
1316 /* first check if we're not supposed to wait: fail
1317 * in this case, returning with everything still locked.
1319 if (flags & CM_SCACHESYNC_NOWAIT)
1320 return CM_ERROR_WOULDBLOCK;
1322 /* These are used for minidump debugging */
1323 sleep_scp_flags = scp->flags; /* so we know why we slept */
1324 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1325 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1327 /* wait here, then try again */
1328 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1329 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1331 scp->waitRequests++;
1332 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1333 scp, scp->waitCount, scp->waitRequests);
1335 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1336 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1337 scp->waitCount = scp->waitRequests = 1;
1340 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1344 lock_ReleaseMutex(&bufp->mx);
1345 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1347 lock_ObtainMutex(&bufp->mx);
1348 lock_ObtainWrite(&scp->rw);
1349 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1351 cm_UpdateServerPriority();
1354 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1355 scp, scp->waitCount, scp->waitRequests);
1356 if (scp->waitCount == 0) {
1357 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1358 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1359 scp->waitRequests = 0;
1361 } /* big while loop */
1363 /* now, update the recorded state for RPC-type calls */
1364 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1365 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1366 if (flags & CM_SCACHESYNC_STORESTATUS)
1367 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1368 if (flags & CM_SCACHESYNC_SETSIZE)
1369 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1370 if (flags & CM_SCACHESYNC_STORESIZE)
1371 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1372 if (flags & CM_SCACHESYNC_GETCALLBACK)
1373 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1374 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1375 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1376 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1377 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1378 if (flags & CM_SCACHESYNC_LOCK)
1379 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1381 /* now update the buffer pointer */
1382 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1383 /* ensure that the buffer isn't already in the I/O list */
1384 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1385 tbufp = osi_GetQData(qdp);
1386 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1389 /* queue a held reference to the buffer in the "reading" I/O list */
1390 qdp = osi_QDAlloc();
1391 osi_SetQData(qdp, bufp);
1394 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1395 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1398 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1399 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1400 "attempting to store extents on a non-file object");
1402 /* ensure that the buffer isn't already in the I/O list */
1403 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1404 tbufp = osi_GetQData(qdp);
1405 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1408 /* queue a held reference to the buffer in the "writing" I/O list */
1409 qdp = osi_QDAlloc();
1410 osi_SetQData(qdp, bufp);
1412 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1413 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1416 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1417 /* mark the buffer as being written to. */
1418 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1424 /* for those syncops that setup for RPCs.
1425 * Called with scache locked.
1427 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1429 osi_queueData_t *qdp;
1432 lock_AssertWrite(&scp->rw);
1434 /* now, update the recorded state for RPC-type calls */
1435 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1436 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1437 if (flags & CM_SCACHESYNC_STORESTATUS)
1438 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1439 if (flags & CM_SCACHESYNC_SETSIZE)
1440 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1441 if (flags & CM_SCACHESYNC_STORESIZE)
1442 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1443 if (flags & CM_SCACHESYNC_GETCALLBACK)
1444 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1445 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1446 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1447 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1448 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1449 if (flags & CM_SCACHESYNC_LOCK)
1450 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1452 /* now update the buffer pointer */
1453 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1456 /* ensure that the buffer is in the I/O list */
1457 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1458 tbufp = osi_GetQData(qdp);
1463 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1467 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1468 if (bufp->flags & CM_BUF_WAITING) {
1469 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1470 osi_Wakeup((LONG_PTR) &bufp);
1476 /* now update the buffer pointer */
1477 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1479 /* ensure that the buffer is in the I/O list */
1480 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1481 tbufp = osi_GetQData(qdp);
1486 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1490 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1491 if (bufp->flags & CM_BUF_WAITING) {
1492 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1493 osi_Wakeup((LONG_PTR) &bufp);
1499 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1500 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1501 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1504 /* and wakeup anyone who is waiting */
1505 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1506 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1507 osi_Wakeup((LONG_PTR) &scp->flags);
1511 /* merge in a response from an RPC. The scp must be locked, and the callback
1514 * Don't overwrite any status info that is dirty, since we could have a store
1515 * operation (such as store data) that merges some info in, and we don't want
1516 * to lose the local updates. Typically, there aren't many updates we do
1517 * locally, anyway, probably only mtime.
1519 * There is probably a bug in here where a chmod (which doesn't change
1520 * serverModTime) that occurs between two fetches, both of whose responses are
1521 * handled after the callback breaking is done, but only one of whose calls
1522 * started before that, can cause old info to be merged from the first call.
1524 void cm_MergeStatus(cm_scache_t *dscp,
1525 cm_scache_t *scp, AFSFetchStatus *statusp,
1526 AFSVolSync *volsyncp,
1527 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1529 afs_uint64 dataVersion;
1530 struct cm_volume *volp = NULL;
1531 struct cm_cell *cellp = NULL;
1533 // yj: i want to create some fake status for the /afs directory and the
1534 // entries under that directory
1535 #ifdef AFS_FREELANCE_CLIENT
1536 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1537 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1538 if (scp == cm_data.rootSCachep) {
1539 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1540 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1541 statusp->Length = cm_fakeDirSize;
1542 statusp->Length_hi = 0;
1544 statusp->FileType = scp->fileType;
1545 statusp->Length = scp->length.LowPart;
1546 statusp->Length_hi = scp->length.HighPart;
1548 statusp->InterfaceVersion = 0x1;
1549 statusp->LinkCount = scp->linkCount;
1550 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1551 statusp->Author = 0x1;
1552 statusp->Owner = 0x0;
1553 statusp->CallerAccess = 0x9;
1554 statusp->AnonymousAccess = 0x9;
1555 statusp->UnixModeBits = 0777;
1556 statusp->ParentVnode = 0x1;
1557 statusp->ParentUnique = 0x1;
1558 statusp->ResidencyMask = 0;
1559 statusp->ClientModTime = FakeFreelanceModTime;
1560 statusp->ServerModTime = FakeFreelanceModTime;
1562 statusp->SyncCounter = 0;
1563 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1564 statusp->lockCount = 0;
1565 statusp->errorCode = 0;
1567 #endif /* AFS_FREELANCE_CLIENT */
1569 if (statusp->errorCode != 0) {
1570 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1571 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1573 scp->fileType = 0; /* unknown */
1575 scp->serverModTime = 0;
1576 scp->clientModTime = 0;
1577 scp->length.LowPart = 0;
1578 scp->length.HighPart = 0;
1579 scp->serverLength.LowPart = 0;
1580 scp->serverLength.HighPart = 0;
1584 scp->unixModeBits = 0;
1586 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1587 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1588 scp->fsLockCount = 0;
1591 scp->parentVnode = dscp->fid.vnode;
1592 scp->parentUnique = dscp->fid.unique;
1594 scp->parentVnode = 0;
1595 scp->parentUnique = 0;
1599 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1602 dataVersion = statusp->dataVersionHigh;
1604 dataVersion |= statusp->DataVersion;
1606 if (!(flags & CM_MERGEFLAG_FORCE) &&
1607 dataVersion < scp->dataVersion &&
1608 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1610 cellp = cm_FindCellByID(scp->fid.cell, 0);
1611 if (scp->cbServerp) {
1612 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1613 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1614 osi_Log2(afsd_logp, "old data from server %x volume %s",
1615 scp->cbServerp->addr.sin_addr.s_addr,
1616 volp ? volp->namep : "(unknown)");
1618 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1619 scp, scp->dataVersion, dataVersion);
1620 /* we have a number of data fetch/store operations running
1621 * concurrently, and we can tell which one executed last at the
1622 * server by its mtime.
1623 * Choose the one with the largest mtime, and ignore the rest.
1625 * These concurrent calls are incompatible with setting the
1626 * mtime, so we won't have a locally changed mtime here.
1628 * We could also have ACL info for a different user than usual,
1629 * in which case we have to do that part of the merge, anyway.
1630 * We won't have to worry about the info being old, since we
1631 * won't have concurrent calls
1632 * that change file status running from this machine.
1634 * Added 3/17/98: if we see data version regression on an RO
1635 * file, it's probably due to a server holding an out-of-date
1636 * replica, rather than to concurrent RPC's. Failures to
1637 * release replicas are now flagged by the volserver, but only
1638 * since AFS 3.4 5.22, so there are plenty of clients getting
1639 * out-of-date replicas out there.
1641 * If we discover an out-of-date replica, by this time it's too
1642 * late to go to another server and retry. Also, we can't
1643 * reject the merge, because then there is no way for
1644 * GetAccess to do its work, and the caller gets into an
1645 * infinite loop. So we just grin and bear it.
1647 if (!(scp->flags & CM_SCACHEFLAG_RO))
1651 if (cm_readonlyVolumeVersioning)
1652 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1654 scp->serverModTime = statusp->ServerModTime;
1656 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1657 scp->clientModTime = statusp->ClientModTime;
1659 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1660 scp->length.LowPart = statusp->Length;
1661 scp->length.HighPart = statusp->Length_hi;
1664 scp->serverLength.LowPart = statusp->Length;
1665 scp->serverLength.HighPart = statusp->Length_hi;
1667 scp->linkCount = statusp->LinkCount;
1668 scp->owner = statusp->Owner;
1669 scp->group = statusp->Group;
1670 scp->unixModeBits = statusp->UnixModeBits & 07777;
1672 if (statusp->FileType == File)
1673 scp->fileType = CM_SCACHETYPE_FILE;
1674 else if (statusp->FileType == Directory)
1675 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1676 else if (statusp->FileType == SymbolicLink) {
1677 if ((scp->unixModeBits & 0111) == 0)
1678 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1680 scp->fileType = CM_SCACHETYPE_SYMLINK;
1683 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1684 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1686 /* and other stuff */
1687 scp->parentVnode = statusp->ParentVnode;
1688 scp->parentUnique = statusp->ParentUnique;
1689 scp->fsLockCount = statusp->lockCount;
1691 /* and merge in the private acl cache info, if this is more than the public
1692 * info; merge in the public stuff in any case.
1694 scp->anyAccess = statusp->AnonymousAccess;
1696 if (userp != NULL) {
1697 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1700 if (scp->dataVersion != 0 &&
1701 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1702 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1704 * We now know that all of the data buffers that we have associated
1705 * with this scp are invalid. Subsequent operations will go faster
1706 * if the buffers are removed from the hash tables.
1708 * We do not remove directory buffers if the dataVersion delta is 1 because
1709 * those version numbers will be updated as part of the directory operation.
1711 * We do not remove storedata buffers because they will still be valid.
1716 cm_buf_t *bp, *prevBp, *nextBp;
1718 lock_ObtainWrite(&buf_globalLock);
1719 i = BUF_FILEHASH(&scp->fid);
1720 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1722 nextBp = bp->fileHashp;
1724 * if the buffer belongs to this stat cache entry
1725 * and the buffer mutex can be obtained, check the
1726 * reference count and if it is zero, remove the buffer
1727 * from the hash tables. If there are references,
1728 * the buffer might be updated to the current version
1729 * so leave it in place.
1731 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1732 lock_TryMutex(&bp->mx)) {
1733 if (bp->refCount == 0 &&
1734 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1735 prevBp = bp->fileHashBackp;
1736 bp->fileHashBackp = bp->fileHashp = NULL;
1738 prevBp->fileHashp = nextBp;
1740 cm_data.buf_fileHashTablepp[i] = nextBp;
1742 nextBp->fileHashBackp = prevBp;
1744 j = BUF_HASH(&bp->fid, &bp->offset);
1745 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1746 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1751 *lbpp = bp->hashp; /* hash out */
1754 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1756 lock_ReleaseMutex(&bp->mx);
1759 lock_ReleaseWrite(&buf_globalLock);
1763 * If the dataVersion has changed, the mountPointStringp must be cleared
1764 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1765 * does not update a mountpoint or symlink by altering the contents of
1766 * the file data; but the Unix CM does.
1768 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA))
1769 scp->mountPointStringp[0] = '\0';
1771 /* We maintain a range of buffer dataVersion values which are considered
1772 * valid. This avoids the need to update the dataVersion on each buffer
1773 * object during an uncontested storeData operation. As a result this
1774 * merge status no longer has performance characteristics derived from
1775 * the size of the file.
1777 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1778 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1779 scp->bufDataVersionLow == 0)
1780 scp->bufDataVersionLow = dataVersion;
1782 scp->dataVersion = dataVersion;
1785 * If someone is waiting for status information, we can wake them up
1786 * now even though the entity that issued the FetchStatus may not
1787 * have completed yet.
1789 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1792 * We just successfully merged status on the stat cache object.
1793 * This means that the associated volume must be online.
1797 cellp = cm_FindCellByID(scp->fid.cell, 0);
1798 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1801 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1802 if (statep->state != vl_online) {
1803 lock_ObtainWrite(&volp->rw);
1804 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1805 statep->state = vl_online;
1806 lock_ReleaseWrite(&volp->rw);
1815 /* note that our stat cache info is incorrect, so force us eventually
1816 * to stat the file again. There may be dirty data associated with
1817 * this vnode, and we want to preserve that information.
1819 * This function works by simply simulating a loss of the callback.
1821 * This function must be called with the scache locked.
1823 void cm_DiscardSCache(cm_scache_t *scp)
1825 lock_AssertWrite(&scp->rw);
1826 if (scp->cbServerp) {
1827 cm_PutServer(scp->cbServerp);
1828 scp->cbServerp = NULL;
1831 scp->volumeCreationDate = 0;
1832 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL));
1833 cm_dnlcPurgedp(scp);
1834 cm_dnlcPurgevp(scp);
1835 cm_FreeAllACLEnts(scp);
1837 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1838 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1840 /* Force mount points and symlinks to be re-evaluated */
1841 scp->mountPointStringp[0] = '\0';
1844 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1846 afsFidp->Volume = fidp->volume;
1847 afsFidp->Vnode = fidp->vnode;
1848 afsFidp->Unique = fidp->unique;
1851 #ifdef DEBUG_REFCOUNT
1852 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1854 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1859 osi_assertx(scp != NULL, "null cm_scache_t");
1860 lock_AssertAny(&cm_scacheLock);
1861 refCount = InterlockedIncrement(&scp->refCount);
1862 #ifdef DEBUG_REFCOUNT
1863 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1864 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1868 #ifdef DEBUG_REFCOUNT
1869 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1871 void cm_HoldSCache(cm_scache_t *scp)
1876 osi_assertx(scp != NULL, "null cm_scache_t");
1877 lock_ObtainRead(&cm_scacheLock);
1878 refCount = InterlockedIncrement(&scp->refCount);
1879 #ifdef DEBUG_REFCOUNT
1880 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1881 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1883 lock_ReleaseRead(&cm_scacheLock);
1886 #ifdef DEBUG_REFCOUNT
1887 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1889 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1894 osi_assertx(scp != NULL, "null cm_scache_t");
1895 lock_AssertAny(&cm_scacheLock);
1897 refCount = InterlockedDecrement(&scp->refCount);
1898 #ifdef DEBUG_REFCOUNT
1900 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1902 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1903 #ifdef DEBUG_REFCOUNT
1904 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1905 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1908 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1912 lockstate = lock_GetRWLockState(&cm_scacheLock);
1913 if (lockstate != OSI_RWLOCK_WRITEHELD)
1914 lock_ReleaseRead(&cm_scacheLock);
1916 lock_ReleaseWrite(&cm_scacheLock);
1918 lock_ObtainWrite(&scp->rw);
1919 if (scp->flags & CM_SCACHEFLAG_DELETED)
1922 if (refCount == 0 && deleted) {
1923 lock_ObtainWrite(&cm_scacheLock);
1924 cm_RecycleSCache(scp, 0);
1925 if (lockstate != OSI_RWLOCK_WRITEHELD)
1926 lock_ConvertWToR(&cm_scacheLock);
1928 if (lockstate != OSI_RWLOCK_WRITEHELD)
1929 lock_ObtainRead(&cm_scacheLock);
1931 lock_ObtainWrite(&cm_scacheLock);
1933 lock_ReleaseWrite(&scp->rw);
1937 #ifdef DEBUG_REFCOUNT
1938 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1940 void cm_ReleaseSCache(cm_scache_t *scp)
1945 osi_assertx(scp != NULL, "null cm_scache_t");
1946 lock_ObtainRead(&cm_scacheLock);
1947 refCount = InterlockedDecrement(&scp->refCount);
1948 #ifdef DEBUG_REFCOUNT
1950 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1952 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1953 #ifdef DEBUG_REFCOUNT
1954 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1955 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1957 lock_ReleaseRead(&cm_scacheLock);
1959 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1961 lock_ObtainWrite(&scp->rw);
1962 if (scp->flags & CM_SCACHEFLAG_DELETED)
1965 lock_ObtainWrite(&cm_scacheLock);
1966 cm_RecycleSCache(scp, 0);
1967 lock_ReleaseWrite(&cm_scacheLock);
1969 lock_ReleaseWrite(&scp->rw);
1973 /* just look for the scp entry to get filetype */
1974 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1975 int cm_FindFileType(cm_fid_t *fidp)
1980 hash = CM_SCACHE_HASH(fidp);
1982 osi_assertx(fidp->cell != 0, "unassigned cell value");
1984 lock_ObtainWrite(&cm_scacheLock);
1985 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1986 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1987 lock_ReleaseWrite(&cm_scacheLock);
1988 return scp->fileType;
1991 lock_ReleaseWrite(&cm_scacheLock);
1995 /* dump all scp's that have reference count > 0 to a file.
1996 * cookie is used to identify this batch for easy parsing,
1997 * and it a string provided by a caller
1999 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2008 lock_ObtainRead(&cm_scacheLock);
2010 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2011 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2013 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2016 char *srvStr = NULL;
2017 afs_uint32 srvStrRpc = TRUE;
2021 if (scp->cbServerp) {
2022 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2023 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2024 srvStr = malloc(16); /* enough for 255.255.255.255 */
2026 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2030 if (scp->cbExpires) {
2035 cbt[strlen(cbt)-1] = '\0';
2038 if (scp->volumeCreationDate) {
2039 t = scp->volumeCreationDate;
2042 cdrot = strdup(cdrot);
2043 cdrot[strlen(cdrot)-1] = '\0';
2047 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2048 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2049 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2050 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2051 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2052 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2053 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2054 cdrot ? cdrot : "<none>", scp->refCount);
2055 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2057 if (scp->fileLocksH) {
2058 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2059 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2061 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2062 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2063 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2064 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2065 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2066 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2067 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2070 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2071 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2076 RpcStringFree(&srvStr);
2086 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2087 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2088 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2089 cookie, cm_data.scacheHashTableSize);
2090 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2092 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2094 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2096 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2097 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2098 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2102 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2103 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2105 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2106 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2108 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2109 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2110 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",
2111 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2112 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2113 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2116 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2117 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2120 lock_ReleaseRead(&cm_scacheLock);