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 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 bufp->cmFlags &= ~CM_BUF_CMSTORING;
168 bufp->flags &= ~CM_BUF_DIRTY;
169 bufp->dirty_offset = 0;
170 bufp->dirty_length = 0;
171 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 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
190 bufp->flags &= ~CM_BUF_DIRTY;
191 bufp->dirty_offset = 0;
192 bufp->dirty_length = 0;
193 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 scp->flags &= ~(CM_SCACHEFLAG_STATD
217 | CM_SCACHEFLAG_DELETED
219 | CM_SCACHEFLAG_PURERO
220 | CM_SCACHEFLAG_OVERQUOTA
221 | CM_SCACHEFLAG_OUTOFSPACE
222 | CM_SCACHEFLAG_EACCESS);
223 scp->serverModTime = 0;
224 scp->dataVersion = CM_SCACHE_VERSION_BAD;
225 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
226 scp->bulkStatProgress = hzero;
228 scp->waitQueueT = NULL;
230 if (scp->cbServerp) {
231 cm_PutServer(scp->cbServerp);
232 scp->cbServerp = NULL;
235 scp->volumeCreationDate = 0;
243 /* remove from dnlc */
247 /* discard cached status; if non-zero, Close
248 * tried to store this to server but failed */
251 /* discard symlink info */
252 scp->mountPointStringp[0] = '\0';
253 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
254 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
256 /* reset locking info */
257 scp->fileLocksH = NULL;
258 scp->fileLocksT = NULL;
259 scp->serverLock = (-1);
260 scp->exclusiveLocks = 0;
261 scp->sharedLocks = 0;
262 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
263 scp->fsLockCount = 0;
265 /* not locked, but there can be no references to this guy
266 * while we hold the global refcount lock.
268 cm_FreeAllACLEnts(scp);
270 cm_ResetSCacheDirectory(scp, 0);
276 * called with cm_scacheLock write-locked; find a vnode to recycle.
277 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
278 * returns scp->mx held.
280 cm_scache_t *cm_GetNewSCache(void)
285 lock_AssertWrite(&cm_scacheLock);
287 /* first pass - look for deleted objects */
288 for ( scp = cm_data.scacheLRULastp;
290 scp = (cm_scache_t *) osi_QPrev(&scp->q))
292 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
293 "invalid cm_scache_t address");
295 if (scp->refCount == 0) {
296 if (scp->flags & CM_SCACHEFLAG_DELETED) {
297 if (!lock_TryWrite(&scp->rw))
300 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%p", scp);
301 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
303 /* we found an entry, so return it */
304 /* now remove from the LRU queue and put it back at the
305 * head of the LRU queue.
307 cm_AdjustScacheLRU(scp);
312 lock_ReleaseWrite(&scp->rw);
313 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%p", scp);
314 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
315 if (!lock_TryWrite(&scp->rw))
318 /* we found an entry, so return it */
319 /* now remove from the LRU queue and put it back at the
320 * head of the LRU queue.
322 cm_AdjustScacheLRU(scp);
329 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
332 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
333 /* There were no deleted scache objects that we could use. Try to find
334 * one that simply hasn't been used in a while.
336 for ( scp = cm_data.scacheLRULastp;
338 scp = (cm_scache_t *) osi_QPrev(&scp->q))
340 /* It is possible for the refCount to be zero and for there still
341 * to be outstanding dirty buffers. If there are dirty buffers,
342 * we must not recycle the scp. */
343 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
344 if (!buf_DirtyBuffersExist(&scp->fid)) {
345 if (!lock_TryWrite(&scp->rw))
348 if (!cm_RecycleSCache(scp, 0)) {
349 /* we found an entry, so return it */
350 /* now remove from the LRU queue and put it back at the
351 * head of the LRU queue.
353 cm_AdjustScacheLRU(scp);
358 lock_ReleaseWrite(&scp->rw);
360 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
364 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
369 /* if we get here, we should allocate a new scache entry. We either are below
370 * quota or we have a leak and need to allocate a new one to avoid panicing.
372 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
373 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
374 "invalid cm_scache_t address");
375 memset(scp, 0, sizeof(cm_scache_t));
376 scp->magic = CM_SCACHE_MAGIC;
377 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
378 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
379 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
381 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
383 scp->serverLock = -1;
385 /* and put it in the LRU queue */
386 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
387 if (!cm_data.scacheLRULastp)
388 cm_data.scacheLRULastp = scp;
389 cm_data.currentSCaches++;
390 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
392 scp->allNextp = cm_data.allSCachesp;
393 cm_data.allSCachesp = scp;
397 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
400 fidp->volume = volume;
402 fidp->unique = unique;
403 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
406 /* like strcmp, only for fids */
407 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
409 if (ap->hash != bp->hash)
411 if (ap->vnode != bp->vnode)
413 if (ap->volume != bp->volume)
415 if (ap->unique != bp->unique)
417 if (ap->cell != bp->cell)
422 void cm_fakeSCacheInit(int newFile)
425 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
426 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
427 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
428 cm_data.fakeSCache.cbExpires = (time_t)-1;
429 /* can leave clientModTime at 0 */
430 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
431 cm_data.fakeSCache.unixModeBits = 0777;
432 cm_data.fakeSCache.length.LowPart = 1000;
433 cm_data.fakeSCache.linkCount = 1;
434 cm_data.fakeSCache.refCount = 1;
435 cm_data.fakeSCache.serverLock = -1;
436 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
438 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
439 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
440 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
444 cm_ValidateSCache(void)
446 cm_scache_t * scp, *lscp;
449 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
450 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
451 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
452 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
456 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
458 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
459 if (scp->magic != CM_SCACHE_MAGIC) {
460 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
461 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
464 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
465 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
466 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
469 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
470 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
471 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
474 if (i > cm_data.currentSCaches ) {
475 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
476 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
479 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
480 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
481 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
486 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
487 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
488 if (scp->magic != CM_SCACHE_MAGIC) {
489 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
490 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
493 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
494 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
495 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
498 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
499 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
500 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
503 if (i > cm_data.currentSCaches ) {
504 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
505 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
508 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
509 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
510 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
515 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
516 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
518 hash = CM_SCACHE_HASH(&scp->fid);
519 if (scp->magic != CM_SCACHE_MAGIC) {
520 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
521 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
524 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
525 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
526 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
529 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
530 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
531 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
535 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
536 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
542 return cm_dnlcValidate();
546 cm_SuspendSCache(void)
551 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
554 * After this call all servers are marked down.
555 * Do not clear the callbacks, instead change the
556 * expiration time so that the callbacks will be expired
557 * when the servers are marked back up. However, we
558 * want the callbacks to be preserved as long as the
559 * servers are down. That way if the machine resumes
560 * without network, the stat cache item will still be
565 lock_ObtainWrite(&cm_scacheLock);
566 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
567 if (scp->cbServerp) {
568 if (scp->flags & CM_SCACHEFLAG_PURERO) {
569 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
571 if (volp->cbExpiresRO == scp->cbExpires)
572 volp->cbExpiresRO = now+1;
576 scp->cbExpires = now+1;
579 lock_ReleaseWrite(&cm_scacheLock);
583 cm_ShutdownSCache(void)
587 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
589 lock_ObtainWrite(&cm_scacheLock);
591 for ( scp = cm_data.allSCachesp; scp;
592 scp = scp->allNextp ) {
593 if (scp->randomACLp) {
594 lock_ReleaseWrite(&cm_scacheLock);
595 lock_ObtainWrite(&scp->rw);
596 lock_ObtainWrite(&cm_scacheLock);
597 cm_FreeAllACLEnts(scp);
598 lock_ReleaseWrite(&scp->rw);
601 if (scp->cbServerp) {
602 cm_PutServer(scp->cbServerp);
603 scp->cbServerp = NULL;
606 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
610 freeBtree(scp->dirBplus);
611 scp->dirBplus = NULL;
612 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
613 lock_FinalizeRWLock(&scp->dirlock);
615 lock_FinalizeRWLock(&scp->rw);
616 lock_FinalizeRWLock(&scp->bufCreateLock);
618 lock_ReleaseWrite(&cm_scacheLock);
620 return cm_dnlcShutdown();
623 void cm_InitSCache(int newFile, long maxSCaches)
625 static osi_once_t once;
627 if (osi_Once(&once)) {
628 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
630 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
631 cm_data.allSCachesp = NULL;
632 cm_data.currentSCaches = 0;
633 cm_data.maxSCaches = maxSCaches;
634 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
638 for ( scp = cm_data.allSCachesp; scp;
639 scp = scp->allNextp ) {
640 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
641 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
643 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
645 scp->cbServerp = NULL;
647 scp->volumeCreationDate = 0;
648 scp->fileLocksH = NULL;
649 scp->fileLocksT = NULL;
650 scp->serverLock = (-1);
651 scp->lastRefreshCycle = 0;
652 scp->exclusiveLocks = 0;
653 scp->sharedLocks = 0;
660 scp->dirBplus = NULL;
661 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
663 scp->waitQueueT = NULL;
664 scp->flags &= ~CM_SCACHEFLAG_WAITING;
667 cm_allFileLocks = NULL;
668 cm_freeFileLocks = NULL;
669 cm_lockRefreshCycle = 0;
670 cm_fakeSCacheInit(newFile);
671 cm_allFreeWaiters = NULL;
672 cm_dnlcInit(newFile);
677 /* version that doesn't bother creating the entry if we don't find it */
678 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
683 hash = CM_SCACHE_HASH(fidp);
685 if (fidp->cell == 0) {
689 lock_ObtainRead(&cm_scacheLock);
690 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
691 if (cm_FidCmp(fidp, &scp->fid) == 0) {
692 cm_HoldSCacheNoLock(scp);
693 lock_ConvertRToW(&cm_scacheLock);
694 cm_AdjustScacheLRU(scp);
695 lock_ReleaseWrite(&cm_scacheLock);
699 lock_ReleaseRead(&cm_scacheLock);
703 #ifdef DEBUG_REFCOUNT
704 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
705 cm_req_t *reqp, char * file, long line)
707 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
712 cm_scache_t *scp = NULL;
714 cm_volume_t *volp = NULL;
716 int special = 0; // yj: boolean variable to test if file is on root.afs
718 extern cm_fid_t cm_rootFid;
720 hash = CM_SCACHE_HASH(fidp);
723 return CM_ERROR_INVAL;
725 #ifdef AFS_FREELANCE_CLIENT
726 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
727 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
728 !(fidp->vnode==0x1 && fidp->unique==0x1));
729 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
730 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
731 fidp->vnode==0x1 && fidp->unique==0x1);
734 // yj: check if we have the scp, if so, we don't need
735 // to do anything else
736 lock_ObtainWrite(&cm_scacheLock);
737 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
738 if (cm_FidCmp(fidp, &scp->fid) == 0) {
739 #ifdef DEBUG_REFCOUNT
740 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
741 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
743 #ifdef AFS_FREELANCE_CLIENT
744 if (cm_freelanceEnabled && special &&
745 cm_data.fakeDirVersion != scp->dataVersion)
748 cm_HoldSCacheNoLock(scp);
750 cm_AdjustScacheLRU(scp);
751 lock_ReleaseWrite(&cm_scacheLock);
756 // yj: when we get here, it means we don't have an scp
757 // so we need to either load it or fake it, depending
758 // on whether the file is "special", see below.
760 // yj: if we're trying to get an scp for a file that's
761 // on root.afs of homecell, we want to handle it specially
762 // because we have to fill in the status stuff 'coz we
763 // don't want trybulkstat to fill it in for us
764 #ifdef AFS_FREELANCE_CLIENT
765 if (cm_freelanceEnabled && isRoot) {
766 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
767 /* freelance: if we are trying to get the root scp for the first
768 * time, we will just put in a place holder entry.
773 if (cm_freelanceEnabled && special) {
774 lock_ReleaseWrite(&cm_scacheLock);
775 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
777 if (cm_getLocalMountPointChange()) {
778 cm_clearLocalMountPointChange();
779 cm_reInitLocalMountPoints();
782 lock_ObtainWrite(&cm_scacheLock);
784 scp = cm_GetNewSCache(); /* returns scp->rw held */
786 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
787 lock_ReleaseWrite(&cm_scacheLock);
788 return CM_ERROR_WOULDBLOCK;
791 lock_ReleaseWrite(&cm_scacheLock);
792 lock_ObtainWrite(&scp->rw);
793 lock_ObtainWrite(&cm_scacheLock);
796 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
797 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
798 scp->dotdotFid.unique=1;
799 scp->dotdotFid.vnode=1;
800 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
801 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
802 scp->nextp = cm_data.scacheHashTablep[hash];
803 cm_data.scacheHashTablep[hash] = scp;
804 scp->flags |= CM_SCACHEFLAG_INHASH;
807 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
809 /* must be called after the scp->fid is set */
810 cm_FreelanceFetchMountPointString(scp);
811 cm_FreelanceFetchFileType(scp);
813 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
814 scp->length.HighPart = 0;
816 scp->unixModeBits=0777;
817 scp->clientModTime=FakeFreelanceModTime;
818 scp->serverModTime=FakeFreelanceModTime;
819 scp->parentUnique = 0x1;
820 scp->parentVnode=0x1;
822 scp->dataVersion=cm_data.fakeDirVersion;
823 scp->bufDataVersionLow=cm_data.fakeDirVersion;
824 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
826 lock_ReleaseWrite(&scp->rw);
827 lock_ReleaseWrite(&cm_scacheLock);
829 #ifdef DEBUG_REFCOUNT
830 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
831 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
836 #endif /* AFS_FREELANCE_CLIENT */
838 /* otherwise, we need to find the volume */
839 if (!cm_freelanceEnabled || !isRoot) {
840 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
841 cellp = cm_FindCellByID(fidp->cell, 0);
843 return CM_ERROR_NOSUCHCELL;
845 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
848 lock_ObtainWrite(&cm_scacheLock);
851 /* otherwise, we have the volume, now reverify that the scp doesn't
852 * exist, and proceed.
854 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
855 if (cm_FidCmp(fidp, &scp->fid) == 0) {
856 #ifdef DEBUG_REFCOUNT
857 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
858 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
860 cm_HoldSCacheNoLock(scp);
861 cm_AdjustScacheLRU(scp);
862 lock_ReleaseWrite(&cm_scacheLock);
870 /* now, if we don't have the fid, recycle something */
871 scp = cm_GetNewSCache(); /* returns scp->rw held */
873 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
874 lock_ReleaseWrite(&cm_scacheLock);
877 return CM_ERROR_WOULDBLOCK;
879 #ifdef DEBUG_REFCOUNT
880 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
882 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
884 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
887 if (!cm_freelanceEnabled || !isRoot) {
888 /* if this scache entry represents a volume root then we need
889 * to copy the dotdotFipd from the volume structure where the
890 * "master" copy is stored (defect 11489)
892 if (volp->vol[ROVOL].ID == fidp->volume) {
893 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
894 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
895 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
896 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
897 scp->flags |= CM_SCACHEFLAG_RO;
898 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
899 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
901 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
902 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
907 scp->nextp = cm_data.scacheHashTablep[hash];
908 cm_data.scacheHashTablep[hash] = scp;
909 scp->flags |= CM_SCACHEFLAG_INHASH;
910 lock_ReleaseWrite(&scp->rw);
912 #ifdef DEBUG_REFCOUNT
913 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
915 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
917 /* XXX - The following fields in the cm_scache are
924 /* now we have a held scache entry; just return it */
926 #ifdef DEBUG_REFCOUNT
927 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
928 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
930 lock_ReleaseWrite(&cm_scacheLock);
934 /* Returns a held reference to the scache's parent
936 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
941 cm_scache_t * pscp = NULL;
943 lock_ObtainWrite(&cm_scacheLock);
944 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
946 if (cm_FidCmp(&scp->fid, &parent_fid)) {
947 i = CM_SCACHE_HASH(&parent_fid);
948 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
949 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
950 cm_HoldSCacheNoLock(pscp);
956 lock_ReleaseWrite(&cm_scacheLock);
961 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
963 cm_scache_waiter_t * w;
965 lock_ObtainWrite(&cm_scacheLock);
966 if (cm_allFreeWaiters == NULL) {
967 w = malloc(sizeof(*w));
968 memset(w, 0, sizeof(*w));
970 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
971 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
974 w->threadId = thrd_Current();
976 cm_HoldSCacheNoLock(scp);
980 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
981 lock_ReleaseWrite(&cm_scacheLock);
983 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
986 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
988 cm_scache_waiter_t * w;
991 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
993 lock_ObtainRead(&cm_scacheLock);
994 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
996 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
997 if (w->flags == flags && w->bufp == bufp) {
1002 osi_assertx(w != NULL, "null cm_scache_waiter_t");
1003 this_is_me = (w->threadId == thrd_Current());
1004 lock_ReleaseRead(&cm_scacheLock);
1007 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1011 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1013 lock_ObtainWrite(&cm_scacheLock);
1014 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1015 cm_ReleaseSCacheNoLock(scp);
1016 memset(w, 0, sizeof(*w));
1017 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1018 lock_ReleaseWrite(&cm_scacheLock);
1024 /* synchronize a fetch, store, read, write, fetch status or store status.
1025 * Called with scache mutex held, and returns with it held, but temporarily
1026 * drops it during the fetch.
1028 * At most one flag can be on in flags, if this is an RPC request.
1030 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1032 * There are a lot of weird restrictions here; here's an attempt to explain the
1033 * rationale for the concurrency restrictions implemented in this function.
1035 * First, although the file server will break callbacks when *another* machine
1036 * modifies a file or status block, the client itself is responsible for
1037 * concurrency control on its own requests. Callback breaking events are rare,
1038 * and simply invalidate any concurrent new status info.
1040 * In the absence of callback breaking messages, we need to know how to
1041 * synchronize incoming responses describing updates to files. We synchronize
1042 * operations that update the data version by comparing the data versions.
1043 * However, updates that do not update the data, but only the status, can't be
1044 * synchronized with fetches or stores, since there's nothing to compare
1045 * to tell which operation executed first at the server.
1047 * Thus, we can allow multiple ops that change file data, or dir data, and
1048 * fetches. However, status storing ops have to be done serially.
1050 * Furthermore, certain data-changing ops are incompatible: we can't read or
1051 * write a buffer while doing a truncate. We can't read and write the same
1052 * buffer at the same time, or write while fetching or storing, or read while
1053 * fetching a buffer (this may change). We can't fetch and store at the same
1056 * With respect to status, we can't read and write at the same time, read while
1057 * fetching, write while fetching or storing, or fetch and store at the same time.
1059 * We can't allow a get callback RPC to run in concurrently with something that
1060 * will return updated status, since we could start a call, have the server
1061 * return status, have another machine make an update to the status (which
1062 * doesn't change serverModTime), have the original machine get a new callback,
1063 * and then have the original machine merge in the early, old info from the
1064 * first call. At this point, the easiest way to avoid this problem is to have
1065 * getcallback calls conflict with all others for the same vnode. Other calls
1066 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1067 * vnode must be careful not to merge in their status unless they have obtained
1068 * a callback from the start of their call.
1070 * Note added 1/23/96
1071 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1072 * Each such RPC passes a FileLength parameter, which the server uses to do
1073 * pre-truncation if necessary. So if two RPC's are processed out of order at
1074 * the server, the one with the smaller FileLength will be processed last,
1075 * possibly resulting in a bogus truncation. The simplest way to avoid this
1076 * is to serialize all StoreData RPC's. This is the reason we defined
1077 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1079 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1080 afs_uint32 rights, afs_uint32 flags)
1082 osi_queueData_t *qdp;
1085 afs_uint32 outRights;
1087 afs_uint32 sleep_scp_flags = 0;
1088 afs_uint32 sleep_buf_cmflags = 0;
1089 afs_uint32 sleep_scp_bufs = 0;
1092 lock_AssertWrite(&scp->rw);
1094 /* lookup this first */
1095 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1098 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1101 /* Do the access check. Now we don't really do the access check
1102 * atomically, since the caller doesn't expect the parent dir to be
1103 * returned locked, and that is what we'd have to do to prevent a
1104 * callback breaking message on the parent due to a setacl call from
1105 * being processed while we're running. So, instead, we check things
1106 * here, and if things look fine with the access, we proceed to finish
1107 * the rest of this check. Sort of a hack, but probably good enough.
1111 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1112 /* if we're bringing in a new status block, ensure that
1113 * we aren't already doing so, and that no one is
1114 * changing the status concurrently, either. We need
1115 * to do this, even if the status is of a different
1116 * type, since we don't have the ability to figure out,
1117 * in the AFS 3 protocols, which status-changing
1118 * operation ran first, or even which order a read and
1119 * a write occurred in.
1121 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1122 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1123 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1127 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1128 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1129 /* if we're going to make an RPC to change the status, make sure
1130 * that no one is bringing in or sending out the status.
1132 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1133 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1134 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1137 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1138 (scp->bufReadsp || scp->bufWritesp)) {
1139 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1143 if (flags & CM_SCACHESYNC_FETCHDATA) {
1144 /* if we're bringing in a new chunk of data, make sure that
1145 * nothing is happening to that chunk, and that we aren't
1146 * changing the basic file status info, either.
1148 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1149 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1150 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1153 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1154 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1158 if (flags & CM_SCACHESYNC_STOREDATA) {
1159 /* same as fetch data */
1160 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1161 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1162 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1165 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1166 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1171 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1172 /* Don't allow concurrent StoreData RPC's */
1173 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1174 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1179 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1180 /* Don't allow more than one BKG store request */
1181 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1182 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1187 if (flags & CM_SCACHESYNC_LOCK) {
1188 /* Don't allow concurrent fiddling with lock lists */
1189 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1190 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1195 /* now the operations that don't correspond to making RPCs */
1196 if (flags & CM_SCACHESYNC_GETSTATUS) {
1197 /* we can use the status that's here, if we're not
1198 * bringing in new status.
1200 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1201 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1205 if (flags & CM_SCACHESYNC_SETSTATUS) {
1206 /* we can make a change to the local status, as long as
1207 * the status isn't changing now.
1209 * If we're fetching or storing a chunk of data, we can
1210 * change the status locally, since the fetch/store
1211 * operations don't change any of the data that we're
1214 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1215 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1216 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1220 if (flags & CM_SCACHESYNC_READ) {
1221 /* we're going to read the data, make sure that the
1222 * status is available, and that the data is here. It
1223 * is OK to read while storing the data back.
1225 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1226 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1229 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1230 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1233 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1234 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1238 if (flags & CM_SCACHESYNC_WRITE) {
1239 /* don't write unless the status is stable and the chunk
1242 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1243 CM_SCACHEFLAG_SIZESTORING)) {
1244 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1247 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1249 CM_BUF_CMWRITING))) {
1250 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1252 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1253 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1254 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1260 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1261 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1262 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1265 lock_ReleaseMutex(&bufp->mx);
1266 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1268 lock_ReleaseWrite(&scp->rw);
1269 lock_ObtainMutex(&bufp->mx);
1270 lock_ObtainWrite(&scp->rw);
1274 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1280 /* can't check access rights without a callback */
1281 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1283 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1284 return CM_ERROR_READONLY;
1286 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1287 if (~outRights & rights)
1288 return CM_ERROR_NOACCESS;
1291 /* we don't know the required access rights */
1292 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1293 code = cm_GetAccessRights(scp, userp, reqp);
1295 lock_ReleaseWrite(&scp->rw);
1296 lock_ObtainMutex(&bufp->mx);
1297 lock_ObtainWrite(&scp->rw);
1305 /* if we get here, we're happy */
1309 /* first check if we're not supposed to wait: fail
1310 * in this case, returning with everything still locked.
1312 if (flags & CM_SCACHESYNC_NOWAIT)
1313 return CM_ERROR_WOULDBLOCK;
1315 /* These are used for minidump debugging */
1316 sleep_scp_flags = scp->flags; /* so we know why we slept */
1317 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1318 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1320 /* wait here, then try again */
1321 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1322 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1324 scp->waitRequests++;
1325 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1326 scp, scp->waitCount, scp->waitRequests);
1328 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1329 scp->flags |= CM_SCACHEFLAG_WAITING;
1330 scp->waitCount = scp->waitRequests = 1;
1333 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1337 lock_ReleaseMutex(&bufp->mx);
1338 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1340 lock_ObtainMutex(&bufp->mx);
1341 lock_ObtainWrite(&scp->rw);
1342 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1344 cm_UpdateServerPriority();
1347 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1348 scp, scp->waitCount, scp->waitRequests);
1349 if (scp->waitCount == 0) {
1350 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1351 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1352 scp->waitRequests = 0;
1354 } /* big while loop */
1356 /* now, update the recorded state for RPC-type calls */
1357 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1358 scp->flags |= CM_SCACHEFLAG_FETCHING;
1359 if (flags & CM_SCACHESYNC_STORESTATUS)
1360 scp->flags |= CM_SCACHEFLAG_STORING;
1361 if (flags & CM_SCACHESYNC_SETSIZE)
1362 scp->flags |= CM_SCACHEFLAG_SIZESETTING;
1363 if (flags & CM_SCACHESYNC_STORESIZE)
1364 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1365 if (flags & CM_SCACHESYNC_GETCALLBACK)
1366 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1367 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1368 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1369 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1370 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1371 if (flags & CM_SCACHESYNC_LOCK)
1372 scp->flags |= CM_SCACHEFLAG_LOCKING;
1374 /* now update the buffer pointer */
1375 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1376 /* ensure that the buffer isn't already in the I/O list */
1377 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1378 tbufp = osi_GetQData(qdp);
1379 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1382 /* queue a held reference to the buffer in the "reading" I/O list */
1383 qdp = osi_QDAlloc();
1384 osi_SetQData(qdp, bufp);
1387 bufp->cmFlags |= CM_BUF_CMFETCHING;
1388 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1391 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1392 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1393 "attempting to store extents on a non-file object");
1395 /* ensure that the buffer isn't already in the I/O list */
1396 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1397 tbufp = osi_GetQData(qdp);
1398 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1401 /* queue a held reference to the buffer in the "writing" I/O list */
1402 qdp = osi_QDAlloc();
1403 osi_SetQData(qdp, bufp);
1405 bufp->cmFlags |= CM_BUF_CMSTORING;
1406 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1409 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1410 /* mark the buffer as being written to. */
1411 bufp->cmFlags |= CM_BUF_CMWRITING;
1417 /* for those syncops that setup for RPCs.
1418 * Called with scache locked.
1420 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1422 osi_queueData_t *qdp;
1425 lock_AssertWrite(&scp->rw);
1427 /* now, update the recorded state for RPC-type calls */
1428 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1429 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1430 if (flags & CM_SCACHESYNC_STORESTATUS)
1431 scp->flags &= ~CM_SCACHEFLAG_STORING;
1432 if (flags & CM_SCACHESYNC_SETSIZE)
1433 scp->flags &= ~CM_SCACHEFLAG_SIZESETTING;
1434 if (flags & CM_SCACHESYNC_STORESIZE)
1435 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1436 if (flags & CM_SCACHESYNC_GETCALLBACK)
1437 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1438 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1439 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1440 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1441 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1442 if (flags & CM_SCACHESYNC_LOCK)
1443 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1445 /* now update the buffer pointer */
1446 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1449 /* ensure that the buffer is in the I/O list */
1450 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1451 tbufp = osi_GetQData(qdp);
1456 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1460 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1461 if (bufp->flags & CM_BUF_WAITING) {
1462 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1463 osi_Wakeup((LONG_PTR) &bufp);
1469 /* now update the buffer pointer */
1470 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1472 /* ensure that the buffer is in the I/O list */
1473 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1474 tbufp = osi_GetQData(qdp);
1479 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1483 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1484 if (bufp->flags & CM_BUF_WAITING) {
1485 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1486 osi_Wakeup((LONG_PTR) &bufp);
1492 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1493 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1494 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1497 /* and wakeup anyone who is waiting */
1498 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1499 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1500 osi_Wakeup((LONG_PTR) &scp->flags);
1504 /* merge in a response from an RPC. The scp must be locked, and the callback
1507 * Don't overwrite any status info that is dirty, since we could have a store
1508 * operation (such as store data) that merges some info in, and we don't want
1509 * to lose the local updates. Typically, there aren't many updates we do
1510 * locally, anyway, probably only mtime.
1512 * There is probably a bug in here where a chmod (which doesn't change
1513 * serverModTime) that occurs between two fetches, both of whose responses are
1514 * handled after the callback breaking is done, but only one of whose calls
1515 * started before that, can cause old info to be merged from the first call.
1517 void cm_MergeStatus(cm_scache_t *dscp,
1518 cm_scache_t *scp, AFSFetchStatus *statusp,
1519 AFSVolSync *volsyncp,
1520 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1522 afs_uint64 dataVersion;
1523 struct cm_volume *volp = NULL;
1524 struct cm_cell *cellp = NULL;
1526 // yj: i want to create some fake status for the /afs directory and the
1527 // entries under that directory
1528 #ifdef AFS_FREELANCE_CLIENT
1529 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1530 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1531 if (scp == cm_data.rootSCachep) {
1532 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1533 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1534 statusp->Length = cm_fakeDirSize;
1535 statusp->Length_hi = 0;
1537 statusp->FileType = scp->fileType;
1538 statusp->Length = scp->length.LowPart;
1539 statusp->Length_hi = scp->length.HighPart;
1541 statusp->InterfaceVersion = 0x1;
1542 statusp->LinkCount = scp->linkCount;
1543 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1544 statusp->Author = 0x1;
1545 statusp->Owner = 0x0;
1546 statusp->CallerAccess = 0x9;
1547 statusp->AnonymousAccess = 0x9;
1548 statusp->UnixModeBits = 0777;
1549 statusp->ParentVnode = 0x1;
1550 statusp->ParentUnique = 0x1;
1551 statusp->ResidencyMask = 0;
1552 statusp->ClientModTime = FakeFreelanceModTime;
1553 statusp->ServerModTime = FakeFreelanceModTime;
1555 statusp->SyncCounter = 0;
1556 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1557 statusp->lockCount = 0;
1558 statusp->errorCode = 0;
1560 #endif /* AFS_FREELANCE_CLIENT */
1562 if (statusp->errorCode != 0) {
1563 scp->flags |= CM_SCACHEFLAG_EACCESS;
1564 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1566 scp->fileType = 0; /* unknown */
1568 scp->serverModTime = 0;
1569 scp->clientModTime = 0;
1570 scp->length.LowPart = 0;
1571 scp->length.HighPart = 0;
1572 scp->serverLength.LowPart = 0;
1573 scp->serverLength.HighPart = 0;
1577 scp->unixModeBits = 0;
1579 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1580 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1581 scp->fsLockCount = 0;
1584 scp->parentVnode = dscp->fid.vnode;
1585 scp->parentUnique = dscp->fid.unique;
1587 scp->parentVnode = 0;
1588 scp->parentUnique = 0;
1592 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1595 dataVersion = statusp->dataVersionHigh;
1597 dataVersion |= statusp->DataVersion;
1599 if (!(flags & CM_MERGEFLAG_FORCE) &&
1600 dataVersion < scp->dataVersion &&
1601 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1603 cellp = cm_FindCellByID(scp->fid.cell, 0);
1604 if (scp->cbServerp) {
1605 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1606 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1607 osi_Log2(afsd_logp, "old data from server %x volume %s",
1608 scp->cbServerp->addr.sin_addr.s_addr,
1609 volp ? volp->namep : "(unknown)");
1611 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1612 scp, scp->dataVersion, dataVersion);
1613 /* we have a number of data fetch/store operations running
1614 * concurrently, and we can tell which one executed last at the
1615 * server by its mtime.
1616 * Choose the one with the largest mtime, and ignore the rest.
1618 * These concurrent calls are incompatible with setting the
1619 * mtime, so we won't have a locally changed mtime here.
1621 * We could also have ACL info for a different user than usual,
1622 * in which case we have to do that part of the merge, anyway.
1623 * We won't have to worry about the info being old, since we
1624 * won't have concurrent calls
1625 * that change file status running from this machine.
1627 * Added 3/17/98: if we see data version regression on an RO
1628 * file, it's probably due to a server holding an out-of-date
1629 * replica, rather than to concurrent RPC's. Failures to
1630 * release replicas are now flagged by the volserver, but only
1631 * since AFS 3.4 5.22, so there are plenty of clients getting
1632 * out-of-date replicas out there.
1634 * If we discover an out-of-date replica, by this time it's too
1635 * late to go to another server and retry. Also, we can't
1636 * reject the merge, because then there is no way for
1637 * GetAccess to do its work, and the caller gets into an
1638 * infinite loop. So we just grin and bear it.
1640 if (!(scp->flags & CM_SCACHEFLAG_RO))
1644 if (cm_readonlyVolumeVersioning)
1645 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1647 scp->serverModTime = statusp->ServerModTime;
1649 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1650 scp->clientModTime = statusp->ClientModTime;
1652 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1653 scp->length.LowPart = statusp->Length;
1654 scp->length.HighPart = statusp->Length_hi;
1657 scp->serverLength.LowPart = statusp->Length;
1658 scp->serverLength.HighPart = statusp->Length_hi;
1660 scp->linkCount = statusp->LinkCount;
1661 scp->owner = statusp->Owner;
1662 scp->group = statusp->Group;
1663 scp->unixModeBits = statusp->UnixModeBits & 07777;
1665 if (statusp->FileType == File)
1666 scp->fileType = CM_SCACHETYPE_FILE;
1667 else if (statusp->FileType == Directory)
1668 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1669 else if (statusp->FileType == SymbolicLink) {
1670 if ((scp->unixModeBits & 0111) == 0)
1671 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1673 scp->fileType = CM_SCACHETYPE_SYMLINK;
1676 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1677 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1679 /* and other stuff */
1680 scp->parentVnode = statusp->ParentVnode;
1681 scp->parentUnique = statusp->ParentUnique;
1682 scp->fsLockCount = statusp->lockCount;
1684 /* and merge in the private acl cache info, if this is more than the public
1685 * info; merge in the public stuff in any case.
1687 scp->anyAccess = statusp->AnonymousAccess;
1689 if (userp != NULL) {
1690 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1693 if (scp->dataVersion != 0 &&
1694 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1695 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1697 * We now know that all of the data buffers that we have associated
1698 * with this scp are invalid. Subsequent operations will go faster
1699 * if the buffers are removed from the hash tables.
1701 * We do not remove directory buffers if the dataVersion delta is 1 because
1702 * those version numbers will be updated as part of the directory operation.
1704 * We do not remove storedata buffers because they will still be valid.
1709 cm_buf_t *bp, *prevBp, *nextBp;
1711 lock_ObtainWrite(&buf_globalLock);
1712 i = BUF_FILEHASH(&scp->fid);
1713 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1715 nextBp = bp->fileHashp;
1717 * if the buffer belongs to this stat cache entry
1718 * and the buffer mutex can be obtained, check the
1719 * reference count and if it is zero, remove the buffer
1720 * from the hash tables. If there are references,
1721 * the buffer might be updated to the current version
1722 * so leave it in place.
1724 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1725 lock_TryMutex(&bp->mx)) {
1726 if (bp->refCount == 0 &&
1727 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1728 prevBp = bp->fileHashBackp;
1729 bp->fileHashBackp = bp->fileHashp = NULL;
1731 prevBp->fileHashp = nextBp;
1733 cm_data.buf_fileHashTablepp[i] = nextBp;
1735 nextBp->fileHashBackp = prevBp;
1737 j = BUF_HASH(&bp->fid, &bp->offset);
1738 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1739 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1744 *lbpp = bp->hashp; /* hash out */
1747 bp->qFlags &= ~CM_BUF_QINHASH;
1749 lock_ReleaseMutex(&bp->mx);
1752 lock_ReleaseWrite(&buf_globalLock);
1756 * If the dataVersion has changed, the mountPointStringp must be cleared
1757 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1758 * does not update a mountpoint or symlink by altering the contents of
1759 * the file data; but the Unix CM does.
1761 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA))
1762 scp->mountPointStringp[0] = '\0';
1764 /* We maintain a range of buffer dataVersion values which are considered
1765 * valid. This avoids the need to update the dataVersion on each buffer
1766 * object during an uncontested storeData operation. As a result this
1767 * merge status no longer has performance characteristics derived from
1768 * the size of the file.
1770 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1771 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1772 scp->bufDataVersionLow == 0)
1773 scp->bufDataVersionLow = dataVersion;
1775 scp->dataVersion = dataVersion;
1778 * If someone is waiting for status information, we can wake them up
1779 * now even though the entity that issued the FetchStatus may not
1780 * have completed yet.
1782 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1785 * We just successfully merged status on the stat cache object.
1786 * This means that the associated volume must be online.
1790 cellp = cm_FindCellByID(scp->fid.cell, 0);
1791 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1794 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1795 if (statep->state != vl_online) {
1796 lock_ObtainWrite(&volp->rw);
1797 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1798 statep->state = vl_online;
1799 lock_ReleaseWrite(&volp->rw);
1808 /* note that our stat cache info is incorrect, so force us eventually
1809 * to stat the file again. There may be dirty data associated with
1810 * this vnode, and we want to preserve that information.
1812 * This function works by simply simulating a loss of the callback.
1814 * This function must be called with the scache locked.
1816 void cm_DiscardSCache(cm_scache_t *scp)
1818 lock_AssertWrite(&scp->rw);
1819 if (scp->cbServerp) {
1820 cm_PutServer(scp->cbServerp);
1821 scp->cbServerp = NULL;
1824 scp->volumeCreationDate = 0;
1825 scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
1826 cm_dnlcPurgedp(scp);
1827 cm_dnlcPurgevp(scp);
1828 cm_FreeAllACLEnts(scp);
1830 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1831 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1833 /* Force mount points and symlinks to be re-evaluated */
1834 scp->mountPointStringp[0] = '\0';
1837 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1839 afsFidp->Volume = fidp->volume;
1840 afsFidp->Vnode = fidp->vnode;
1841 afsFidp->Unique = fidp->unique;
1844 #ifdef DEBUG_REFCOUNT
1845 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1847 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1852 osi_assertx(scp != NULL, "null cm_scache_t");
1853 lock_AssertAny(&cm_scacheLock);
1854 refCount = InterlockedIncrement(&scp->refCount);
1855 #ifdef DEBUG_REFCOUNT
1856 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1857 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1861 #ifdef DEBUG_REFCOUNT
1862 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1864 void cm_HoldSCache(cm_scache_t *scp)
1869 osi_assertx(scp != NULL, "null cm_scache_t");
1870 lock_ObtainRead(&cm_scacheLock);
1871 refCount = InterlockedIncrement(&scp->refCount);
1872 #ifdef DEBUG_REFCOUNT
1873 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1874 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1876 lock_ReleaseRead(&cm_scacheLock);
1879 #ifdef DEBUG_REFCOUNT
1880 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1882 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1887 osi_assertx(scp != NULL, "null cm_scache_t");
1888 lock_AssertAny(&cm_scacheLock);
1890 refCount = InterlockedDecrement(&scp->refCount);
1891 #ifdef DEBUG_REFCOUNT
1893 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1895 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1896 #ifdef DEBUG_REFCOUNT
1897 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1898 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1901 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1905 lockstate = lock_GetRWLockState(&cm_scacheLock);
1906 if (lockstate != OSI_RWLOCK_WRITEHELD)
1907 lock_ReleaseRead(&cm_scacheLock);
1909 lock_ReleaseWrite(&cm_scacheLock);
1911 lock_ObtainWrite(&scp->rw);
1912 if (scp->flags & CM_SCACHEFLAG_DELETED)
1915 if (refCount == 0 && deleted) {
1916 lock_ObtainWrite(&cm_scacheLock);
1917 cm_RecycleSCache(scp, 0);
1918 if (lockstate != OSI_RWLOCK_WRITEHELD)
1919 lock_ConvertWToR(&cm_scacheLock);
1921 if (lockstate != OSI_RWLOCK_WRITEHELD)
1922 lock_ObtainRead(&cm_scacheLock);
1924 lock_ObtainWrite(&cm_scacheLock);
1926 lock_ReleaseWrite(&scp->rw);
1930 #ifdef DEBUG_REFCOUNT
1931 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1933 void cm_ReleaseSCache(cm_scache_t *scp)
1938 osi_assertx(scp != NULL, "null cm_scache_t");
1939 lock_ObtainRead(&cm_scacheLock);
1940 refCount = InterlockedDecrement(&scp->refCount);
1941 #ifdef DEBUG_REFCOUNT
1943 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1945 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1946 #ifdef DEBUG_REFCOUNT
1947 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1948 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1950 lock_ReleaseRead(&cm_scacheLock);
1952 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1954 lock_ObtainWrite(&scp->rw);
1955 if (scp->flags & CM_SCACHEFLAG_DELETED)
1958 lock_ObtainWrite(&cm_scacheLock);
1959 cm_RecycleSCache(scp, 0);
1960 lock_ReleaseWrite(&cm_scacheLock);
1962 lock_ReleaseWrite(&scp->rw);
1966 /* just look for the scp entry to get filetype */
1967 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1968 int cm_FindFileType(cm_fid_t *fidp)
1973 hash = CM_SCACHE_HASH(fidp);
1975 osi_assertx(fidp->cell != 0, "unassigned cell value");
1977 lock_ObtainWrite(&cm_scacheLock);
1978 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1979 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1980 lock_ReleaseWrite(&cm_scacheLock);
1981 return scp->fileType;
1984 lock_ReleaseWrite(&cm_scacheLock);
1988 /* dump all scp's that have reference count > 0 to a file.
1989 * cookie is used to identify this batch for easy parsing,
1990 * and it a string provided by a caller
1992 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
2001 lock_ObtainRead(&cm_scacheLock);
2003 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
2004 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2006 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2009 char *srvStr = NULL;
2010 afs_uint32 srvStrRpc = TRUE;
2014 if (scp->cbServerp) {
2015 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2016 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2017 srvStr = malloc(16); /* enough for 255.255.255.255 */
2019 afs_inet_ntoa_r(scp->cbServerp->addr.sin_addr.s_addr, srvStr);
2023 if (scp->cbExpires) {
2028 cbt[strlen(cbt)-1] = '\0';
2031 if (scp->volumeCreationDate) {
2032 t = scp->volumeCreationDate;
2035 cdrot = strdup(cdrot);
2036 cdrot[strlen(cdrot)-1] = '\0';
2040 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2041 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2042 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2043 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2044 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2045 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2046 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2047 cdrot ? cdrot : "<none>", scp->refCount);
2048 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2050 if (scp->fileLocksH) {
2051 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2052 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2054 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2055 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2056 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2057 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2058 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2059 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2060 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2063 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2064 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2069 RpcStringFree(&srvStr);
2079 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2080 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2081 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2082 cookie, cm_data.scacheHashTableSize);
2083 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2085 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2087 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2089 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2090 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2091 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2095 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2096 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2098 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2099 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2101 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2102 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2103 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",
2104 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2105 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2106 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2109 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2110 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2113 lock_ReleaseRead(&cm_scacheLock);