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 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
72 /* call with cm_scacheLock write-locked and scp rw held */
73 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
79 lock_AssertWrite(&cm_scacheLock);
80 lock_AssertWrite(&scp->rw);
81 if (scp->flags & CM_SCACHEFLAG_INHASH) {
82 /* hash it out first */
83 i = CM_SCACHE_HASH(&scp->fid);
84 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
86 lscpp = &tscp->nextp, tscp = tscp->nextp) {
90 scp->flags &= ~CM_SCACHEFLAG_INHASH;
97 /* called with cm_scacheLock and scp write-locked */
98 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
101 /* destroy directory Bplus Tree */
103 LARGE_INTEGER start, end;
105 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
107 * We are not holding the dirlock and obtaining it
108 * requires that we drop the scp->rw. As a result
109 * we will leave the dirBplus tree intact but
110 * invalidate the version number so that whatever
111 * operation is currently active can safely complete
112 * but the contents will be ignored on the next
113 * directory operation.
115 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
119 QueryPerformanceCounter(&start);
121 freeBtree(scp->dirBplus);
122 scp->dirBplus = NULL;
123 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
124 QueryPerformanceCounter(&end);
127 lock_ReleaseWrite(&scp->dirlock);
129 bplus_free_time += (end.QuadPart - start.QuadPart);
134 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
135 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
137 if (scp->refCount != 0) {
141 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
142 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
144 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
149 cm_RemoveSCacheFromHashTable(scp);
152 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
153 osi_queueData_t *qdp;
156 while(qdp = scp->bufWritesp) {
157 bufp = osi_GetQData(qdp);
158 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
161 lock_ObtainMutex(&bufp->mx);
162 bufp->cmFlags &= ~CM_BUF_CMSTORING;
163 bufp->flags &= ~CM_BUF_DIRTY;
164 bufp->dirty_offset = 0;
165 bufp->dirty_length = 0;
166 bufp->flags |= CM_BUF_ERROR;
167 bufp->error = VNOVNODE;
168 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
169 bufp->dirtyCounter++;
170 if (bufp->flags & CM_BUF_WAITING) {
171 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
172 osi_Wakeup((long) &bufp);
174 lock_ReleaseMutex(&bufp->mx);
178 while(qdp = scp->bufReadsp) {
179 bufp = osi_GetQData(qdp);
180 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
183 lock_ObtainMutex(&bufp->mx);
184 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
185 bufp->flags &= ~CM_BUF_DIRTY;
186 bufp->dirty_offset = 0;
187 bufp->dirty_length = 0;
188 bufp->flags |= CM_BUF_ERROR;
189 bufp->error = VNOVNODE;
190 bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
191 bufp->dirtyCounter++;
192 if (bufp->flags & CM_BUF_WAITING) {
193 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
194 osi_Wakeup((long) &bufp);
196 lock_ReleaseMutex(&bufp->mx);
200 buf_CleanDirtyBuffers(scp);
202 /* look for things that shouldn't still be set */
203 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
204 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
208 /* invalidate so next merge works fine;
209 * also initialize some flags */
211 scp->flags &= ~(CM_SCACHEFLAG_STATD
212 | CM_SCACHEFLAG_DELETED
214 | CM_SCACHEFLAG_PURERO
215 | CM_SCACHEFLAG_OVERQUOTA
216 | CM_SCACHEFLAG_OUTOFSPACE
217 | CM_SCACHEFLAG_EACCESS);
218 scp->serverModTime = 0;
219 scp->dataVersion = CM_SCACHE_VERSION_BAD;
220 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
221 scp->bulkStatProgress = hzero;
223 scp->waitQueueT = NULL;
225 if (scp->cbServerp) {
226 cm_PutServer(scp->cbServerp);
227 scp->cbServerp = NULL;
230 scp->volumeCreationDate = 0;
238 /* remove from dnlc */
242 /* discard cached status; if non-zero, Close
243 * tried to store this to server but failed */
246 /* discard symlink info */
247 scp->mountPointStringp[0] = '\0';
248 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
249 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
251 /* reset locking info */
252 scp->fileLocksH = NULL;
253 scp->fileLocksT = NULL;
254 scp->serverLock = (-1);
255 scp->exclusiveLocks = 0;
256 scp->sharedLocks = 0;
257 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
258 scp->fsLockCount = 0;
260 /* not locked, but there can be no references to this guy
261 * while we hold the global refcount lock.
263 cm_FreeAllACLEnts(scp);
265 cm_ResetSCacheDirectory(scp, 0);
271 * called with cm_scacheLock write-locked; find a vnode to recycle.
272 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
273 * returns scp->mx held.
275 cm_scache_t *cm_GetNewSCache(void)
280 lock_AssertWrite(&cm_scacheLock);
282 /* first pass - look for deleted objects */
283 for ( scp = cm_data.scacheLRULastp;
285 scp = (cm_scache_t *) osi_QPrev(&scp->q))
287 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
288 "invalid cm_scache_t address");
290 if (scp->refCount == 0) {
291 if (scp->flags & CM_SCACHEFLAG_DELETED) {
292 if (!lock_TryWrite(&scp->rw))
295 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
296 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
298 /* we found an entry, so return it */
299 /* now remove from the LRU queue and put it back at the
300 * head of the LRU queue.
302 cm_AdjustScacheLRU(scp);
307 lock_ReleaseWrite(&scp->rw);
308 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
309 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
310 if (!lock_TryWrite(&scp->rw))
313 /* we found an entry, so return it */
314 /* now remove from the LRU queue and put it back at the
315 * head of the LRU queue.
317 cm_AdjustScacheLRU(scp);
324 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
327 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
328 /* There were no deleted scache objects that we could use. Try to find
329 * one that simply hasn't been used in a while.
331 for ( scp = cm_data.scacheLRULastp;
333 scp = (cm_scache_t *) osi_QPrev(&scp->q))
335 /* It is possible for the refCount to be zero and for there still
336 * to be outstanding dirty buffers. If there are dirty buffers,
337 * we must not recycle the scp. */
338 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
339 if (!buf_DirtyBuffersExist(&scp->fid)) {
340 if (!lock_TryWrite(&scp->rw))
343 if (!cm_RecycleSCache(scp, 0)) {
344 /* we found an entry, so return it */
345 /* now remove from the LRU queue and put it back at the
346 * head of the LRU queue.
348 cm_AdjustScacheLRU(scp);
353 lock_ReleaseWrite(&scp->rw);
355 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
359 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
364 /* if we get here, we should allocate a new scache entry. We either are below
365 * quota or we have a leak and need to allocate a new one to avoid panicing.
367 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
368 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
369 "invalid cm_scache_t address");
370 memset(scp, 0, sizeof(cm_scache_t));
371 scp->magic = CM_SCACHE_MAGIC;
372 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
373 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
374 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
376 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
378 scp->serverLock = -1;
380 /* and put it in the LRU queue */
381 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
382 if (!cm_data.scacheLRULastp)
383 cm_data.scacheLRULastp = scp;
384 cm_data.currentSCaches++;
385 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
387 scp->allNextp = cm_data.allSCachesp;
388 cm_data.allSCachesp = scp;
392 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
395 fidp->volume = volume;
397 fidp->unique = unique;
398 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
401 /* like strcmp, only for fids */
402 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
404 if (ap->hash != bp->hash)
406 if (ap->vnode != bp->vnode)
408 if (ap->volume != bp->volume)
410 if (ap->unique != bp->unique)
412 if (ap->cell != bp->cell)
417 void cm_fakeSCacheInit(int newFile)
420 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
421 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
422 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
423 cm_data.fakeSCache.cbExpires = (time_t)-1;
424 /* can leave clientModTime at 0 */
425 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
426 cm_data.fakeSCache.unixModeBits = 0777;
427 cm_data.fakeSCache.length.LowPart = 1000;
428 cm_data.fakeSCache.linkCount = 1;
429 cm_data.fakeSCache.refCount = 1;
430 cm_data.fakeSCache.serverLock = -1;
431 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
433 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
434 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
435 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
439 cm_ValidateSCache(void)
441 cm_scache_t * scp, *lscp;
444 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
445 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
446 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
447 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
451 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
453 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
454 if (scp->magic != CM_SCACHE_MAGIC) {
455 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
456 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
459 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
460 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
461 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
464 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
465 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
466 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
469 if (i > cm_data.currentSCaches ) {
470 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
471 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
474 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
475 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
476 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
481 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
482 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
483 if (scp->magic != CM_SCACHE_MAGIC) {
484 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
485 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
488 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
489 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
490 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
493 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
494 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
495 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
498 if (i > cm_data.currentSCaches ) {
499 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
500 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
503 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
504 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
505 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
510 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
511 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
513 hash = CM_SCACHE_HASH(&scp->fid);
514 if (scp->magic != CM_SCACHE_MAGIC) {
515 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
516 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
519 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
520 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
521 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
524 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
525 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
526 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
530 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
531 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
537 return cm_dnlcValidate();
541 cm_SuspendSCache(void)
546 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
549 * After this call all servers are marked down.
550 * Do not clear the callbacks, instead change the
551 * expiration time so that the callbacks will be expired
552 * when the servers are marked back up. However, we
553 * want the callbacks to be preserved as long as the
554 * servers are down. That way if the machine resumes
555 * without network, the stat cache item will still be
560 lock_ObtainWrite(&cm_scacheLock);
561 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
562 if (scp->cbServerp) {
563 if (scp->flags & CM_SCACHEFLAG_PURERO) {
564 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
566 if (volp->cbExpiresRO == scp->cbExpires)
567 volp->cbExpiresRO = now+1;
571 scp->cbExpires = now+1;
574 lock_ReleaseWrite(&cm_scacheLock);
578 cm_ShutdownSCache(void)
582 lock_ObtainWrite(&cm_scacheLock);
584 for ( scp = cm_data.allSCachesp; scp;
585 scp = scp->allNextp ) {
586 if (scp->randomACLp) {
587 lock_ReleaseWrite(&cm_scacheLock);
588 lock_ObtainWrite(&scp->rw);
589 lock_ObtainWrite(&cm_scacheLock);
590 cm_FreeAllACLEnts(scp);
591 lock_ReleaseWrite(&scp->rw);
594 if (scp->cbServerp) {
595 cm_PutServer(scp->cbServerp);
596 scp->cbServerp = NULL;
599 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
603 freeBtree(scp->dirBplus);
604 scp->dirBplus = NULL;
605 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
606 lock_FinalizeRWLock(&scp->dirlock);
608 lock_FinalizeRWLock(&scp->rw);
609 lock_FinalizeRWLock(&scp->bufCreateLock);
611 lock_ReleaseWrite(&cm_scacheLock);
613 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
615 return cm_dnlcShutdown();
618 void cm_InitSCache(int newFile, long maxSCaches)
620 static osi_once_t once;
622 if (osi_Once(&once)) {
623 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
625 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
626 cm_data.allSCachesp = NULL;
627 cm_data.currentSCaches = 0;
628 cm_data.maxSCaches = maxSCaches;
629 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
633 for ( scp = cm_data.allSCachesp; scp;
634 scp = scp->allNextp ) {
635 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
636 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
638 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
640 scp->cbServerp = NULL;
642 scp->volumeCreationDate = 0;
643 scp->fileLocksH = NULL;
644 scp->fileLocksT = NULL;
645 scp->serverLock = (-1);
646 scp->lastRefreshCycle = 0;
647 scp->exclusiveLocks = 0;
648 scp->sharedLocks = 0;
655 scp->dirBplus = NULL;
656 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
658 scp->waitQueueT = NULL;
659 scp->flags &= ~CM_SCACHEFLAG_WAITING;
662 cm_allFileLocks = NULL;
663 cm_freeFileLocks = NULL;
664 cm_lockRefreshCycle = 0;
665 cm_fakeSCacheInit(newFile);
666 cm_allFreeWaiters = NULL;
667 cm_dnlcInit(newFile);
672 /* version that doesn't bother creating the entry if we don't find it */
673 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
678 hash = CM_SCACHE_HASH(fidp);
680 if (fidp->cell == 0) {
684 lock_ObtainRead(&cm_scacheLock);
685 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
686 if (cm_FidCmp(fidp, &scp->fid) == 0) {
687 cm_HoldSCacheNoLock(scp);
688 lock_ConvertRToW(&cm_scacheLock);
689 cm_AdjustScacheLRU(scp);
690 lock_ReleaseWrite(&cm_scacheLock);
694 lock_ReleaseRead(&cm_scacheLock);
698 #ifdef DEBUG_REFCOUNT
699 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
700 cm_req_t *reqp, char * file, long line)
702 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
707 cm_scache_t *scp = NULL;
709 cm_volume_t *volp = NULL;
711 int special = 0; // yj: boolean variable to test if file is on root.afs
713 extern cm_fid_t cm_rootFid;
715 hash = CM_SCACHE_HASH(fidp);
718 return CM_ERROR_INVAL;
720 #ifdef AFS_FREELANCE_CLIENT
721 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
722 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
723 !(fidp->vnode==0x1 && fidp->unique==0x1));
724 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
725 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
726 fidp->vnode==0x1 && fidp->unique==0x1);
729 // yj: check if we have the scp, if so, we don't need
730 // to do anything else
731 lock_ObtainWrite(&cm_scacheLock);
732 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
733 if (cm_FidCmp(fidp, &scp->fid) == 0) {
734 #ifdef DEBUG_REFCOUNT
735 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
736 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
738 #ifdef AFS_FREELANCE_CLIENT
739 if (cm_freelanceEnabled && special &&
740 cm_data.fakeDirVersion != scp->dataVersion)
743 cm_HoldSCacheNoLock(scp);
745 cm_AdjustScacheLRU(scp);
746 lock_ReleaseWrite(&cm_scacheLock);
751 // yj: when we get here, it means we don't have an scp
752 // so we need to either load it or fake it, depending
753 // on whether the file is "special", see below.
755 // yj: if we're trying to get an scp for a file that's
756 // on root.afs of homecell, we want to handle it specially
757 // because we have to fill in the status stuff 'coz we
758 // don't want trybulkstat to fill it in for us
759 #ifdef AFS_FREELANCE_CLIENT
760 if (cm_freelanceEnabled && isRoot) {
761 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
762 /* freelance: if we are trying to get the root scp for the first
763 * time, we will just put in a place holder entry.
768 if (cm_freelanceEnabled && special) {
769 lock_ReleaseWrite(&cm_scacheLock);
770 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
772 if (cm_getLocalMountPointChange()) {
773 cm_clearLocalMountPointChange();
774 cm_reInitLocalMountPoints();
777 lock_ObtainWrite(&cm_scacheLock);
779 scp = cm_GetNewSCache(); /* returns scp->rw held */
781 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
782 lock_ReleaseWrite(&cm_scacheLock);
783 return CM_ERROR_WOULDBLOCK;
786 lock_ReleaseWrite(&cm_scacheLock);
787 lock_ObtainWrite(&scp->rw);
788 lock_ObtainWrite(&cm_scacheLock);
791 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
792 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
793 scp->dotdotFid.unique=1;
794 scp->dotdotFid.vnode=1;
795 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
796 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
797 scp->nextp = cm_data.scacheHashTablep[hash];
798 cm_data.scacheHashTablep[hash] = scp;
799 scp->flags |= CM_SCACHEFLAG_INHASH;
802 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
804 /* must be called after the scp->fid is set */
805 cm_FreelanceFetchMountPointString(scp);
806 cm_FreelanceFetchFileType(scp);
808 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
809 scp->length.HighPart = 0;
811 scp->unixModeBits=0777;
812 scp->clientModTime=FakeFreelanceModTime;
813 scp->serverModTime=FakeFreelanceModTime;
814 scp->parentUnique = 0x1;
815 scp->parentVnode=0x1;
817 scp->dataVersion=cm_data.fakeDirVersion;
818 scp->bufDataVersionLow=cm_data.fakeDirVersion;
819 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
821 lock_ReleaseWrite(&scp->rw);
822 lock_ReleaseWrite(&cm_scacheLock);
824 #ifdef DEBUG_REFCOUNT
825 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
826 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
831 #endif /* AFS_FREELANCE_CLIENT */
833 /* otherwise, we need to find the volume */
834 if (!cm_freelanceEnabled || !isRoot) {
835 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
836 cellp = cm_FindCellByID(fidp->cell, 0);
838 return CM_ERROR_NOSUCHCELL;
840 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
843 lock_ObtainWrite(&cm_scacheLock);
846 /* otherwise, we have the volume, now reverify that the scp doesn't
847 * exist, and proceed.
849 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
850 if (cm_FidCmp(fidp, &scp->fid) == 0) {
851 #ifdef DEBUG_REFCOUNT
852 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
853 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
855 cm_HoldSCacheNoLock(scp);
856 cm_AdjustScacheLRU(scp);
857 lock_ReleaseWrite(&cm_scacheLock);
865 /* now, if we don't have the fid, recycle something */
866 scp = cm_GetNewSCache(); /* returns scp->rw held */
868 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
869 lock_ReleaseWrite(&cm_scacheLock);
872 return CM_ERROR_WOULDBLOCK;
874 #ifdef DEBUG_REFCOUNT
875 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
877 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
879 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
882 if (!cm_freelanceEnabled || !isRoot) {
883 /* if this scache entry represents a volume root then we need
884 * to copy the dotdotFipd from the volume structure where the
885 * "master" copy is stored (defect 11489)
887 if (volp->vol[ROVOL].ID == fidp->volume) {
888 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
889 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
890 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
891 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
892 scp->flags |= CM_SCACHEFLAG_RO;
893 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
894 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
896 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
897 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
902 scp->nextp = cm_data.scacheHashTablep[hash];
903 cm_data.scacheHashTablep[hash] = scp;
904 scp->flags |= CM_SCACHEFLAG_INHASH;
905 lock_ReleaseWrite(&scp->rw);
907 #ifdef DEBUG_REFCOUNT
908 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
910 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
912 /* XXX - The following fields in the cm_scache are
919 /* now we have a held scache entry; just return it */
921 #ifdef DEBUG_REFCOUNT
922 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
923 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
925 lock_ReleaseWrite(&cm_scacheLock);
929 /* Returns a held reference to the scache's parent
931 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
936 cm_scache_t * pscp = NULL;
938 lock_ObtainWrite(&cm_scacheLock);
939 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
941 if (cm_FidCmp(&scp->fid, &parent_fid)) {
942 i = CM_SCACHE_HASH(&parent_fid);
943 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
944 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
945 cm_HoldSCacheNoLock(pscp);
951 lock_ReleaseWrite(&cm_scacheLock);
956 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
958 cm_scache_waiter_t * w;
960 lock_ObtainWrite(&cm_scacheLock);
961 if (cm_allFreeWaiters == NULL) {
962 w = malloc(sizeof(*w));
963 memset(w, 0, sizeof(*w));
965 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
966 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
969 w->threadId = thrd_Current();
971 cm_HoldSCacheNoLock(scp);
975 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
976 lock_ReleaseWrite(&cm_scacheLock);
978 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
981 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
983 cm_scache_waiter_t * w;
986 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
988 lock_ObtainRead(&cm_scacheLock);
989 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
991 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
992 if (w->flags == flags && w->bufp == bufp) {
997 osi_assertx(w != NULL, "null cm_scache_waiter_t");
998 this_is_me = (w->threadId == thrd_Current());
999 lock_ReleaseRead(&cm_scacheLock);
1002 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
1006 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
1008 lock_ObtainWrite(&cm_scacheLock);
1009 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
1010 cm_ReleaseSCacheNoLock(scp);
1011 memset(w, 0, sizeof(*w));
1012 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
1013 lock_ReleaseWrite(&cm_scacheLock);
1019 /* synchronize a fetch, store, read, write, fetch status or store status.
1020 * Called with scache mutex held, and returns with it held, but temporarily
1021 * drops it during the fetch.
1023 * At most one flag can be on in flags, if this is an RPC request.
1025 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
1027 * There are a lot of weird restrictions here; here's an attempt to explain the
1028 * rationale for the concurrency restrictions implemented in this function.
1030 * First, although the file server will break callbacks when *another* machine
1031 * modifies a file or status block, the client itself is responsible for
1032 * concurrency control on its own requests. Callback breaking events are rare,
1033 * and simply invalidate any concurrent new status info.
1035 * In the absence of callback breaking messages, we need to know how to
1036 * synchronize incoming responses describing updates to files. We synchronize
1037 * operations that update the data version by comparing the data versions.
1038 * However, updates that do not update the data, but only the status, can't be
1039 * synchronized with fetches or stores, since there's nothing to compare
1040 * to tell which operation executed first at the server.
1042 * Thus, we can allow multiple ops that change file data, or dir data, and
1043 * fetches. However, status storing ops have to be done serially.
1045 * Furthermore, certain data-changing ops are incompatible: we can't read or
1046 * write a buffer while doing a truncate. We can't read and write the same
1047 * buffer at the same time, or write while fetching or storing, or read while
1048 * fetching a buffer (this may change). We can't fetch and store at the same
1051 * With respect to status, we can't read and write at the same time, read while
1052 * fetching, write while fetching or storing, or fetch and store at the same time.
1054 * We can't allow a get callback RPC to run in concurrently with something that
1055 * will return updated status, since we could start a call, have the server
1056 * return status, have another machine make an update to the status (which
1057 * doesn't change serverModTime), have the original machine get a new callback,
1058 * and then have the original machine merge in the early, old info from the
1059 * first call. At this point, the easiest way to avoid this problem is to have
1060 * getcallback calls conflict with all others for the same vnode. Other calls
1061 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1062 * vnode must be careful not to merge in their status unless they have obtained
1063 * a callback from the start of their call.
1065 * Note added 1/23/96
1066 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1067 * Each such RPC passes a FileLength parameter, which the server uses to do
1068 * pre-truncation if necessary. So if two RPC's are processed out of order at
1069 * the server, the one with the smaller FileLength will be processed last,
1070 * possibly resulting in a bogus truncation. The simplest way to avoid this
1071 * is to serialize all StoreData RPC's. This is the reason we defined
1072 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1074 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1075 afs_uint32 rights, afs_uint32 flags)
1077 osi_queueData_t *qdp;
1080 afs_uint32 outRights;
1082 afs_uint32 sleep_scp_flags = 0;
1083 afs_uint32 sleep_buf_cmflags = 0;
1084 afs_uint32 sleep_scp_bufs = 0;
1087 lock_AssertWrite(&scp->rw);
1089 /* lookup this first */
1090 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1093 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1096 /* Do the access check. Now we don't really do the access check
1097 * atomically, since the caller doesn't expect the parent dir to be
1098 * returned locked, and that is what we'd have to do to prevent a
1099 * callback breaking message on the parent due to a setacl call from
1100 * being processed while we're running. So, instead, we check things
1101 * here, and if things look fine with the access, we proceed to finish
1102 * the rest of this check. Sort of a hack, but probably good enough.
1106 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1107 /* if we're bringing in a new status block, ensure that
1108 * we aren't already doing so, and that no one is
1109 * changing the status concurrently, either. We need
1110 * to do this, even if the status is of a different
1111 * type, since we don't have the ability to figure out,
1112 * in the AFS 3 protocols, which status-changing
1113 * operation ran first, or even which order a read and
1114 * a write occurred in.
1116 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1117 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1118 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1122 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1123 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1124 /* if we're going to make an RPC to change the status, make sure
1125 * that no one is bringing in or sending out the status.
1127 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1128 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1129 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1132 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1133 (scp->bufReadsp || scp->bufWritesp)) {
1134 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1138 if (flags & CM_SCACHESYNC_FETCHDATA) {
1139 /* if we're bringing in a new chunk of data, make sure that
1140 * nothing is happening to that chunk, and that we aren't
1141 * changing the basic file status info, either.
1143 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1144 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1145 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1148 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1149 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1153 if (flags & CM_SCACHESYNC_STOREDATA) {
1154 /* same as fetch data */
1155 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1156 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1157 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", 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 STOREDATA", scp, bufp);
1166 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1167 /* Don't allow concurrent StoreData RPC's */
1168 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1169 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1174 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1175 /* Don't allow more than one BKG store request */
1176 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1177 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1182 if (flags & CM_SCACHESYNC_LOCK) {
1183 /* Don't allow concurrent fiddling with lock lists */
1184 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1185 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1190 /* now the operations that don't correspond to making RPCs */
1191 if (flags & CM_SCACHESYNC_GETSTATUS) {
1192 /* we can use the status that's here, if we're not
1193 * bringing in new status.
1195 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1196 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1200 if (flags & CM_SCACHESYNC_SETSTATUS) {
1201 /* we can make a change to the local status, as long as
1202 * the status isn't changing now.
1204 * If we're fetching or storing a chunk of data, we can
1205 * change the status locally, since the fetch/store
1206 * operations don't change any of the data that we're
1209 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1210 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1211 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1215 if (flags & CM_SCACHESYNC_READ) {
1216 /* we're going to read the data, make sure that the
1217 * status is available, and that the data is here. It
1218 * is OK to read while storing the data back.
1220 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1221 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1224 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1225 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1228 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1229 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1233 if (flags & CM_SCACHESYNC_WRITE) {
1234 /* don't write unless the status is stable and the chunk
1237 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1238 CM_SCACHEFLAG_SIZESTORING)) {
1239 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1242 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1244 CM_BUF_CMWRITING))) {
1245 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1247 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1248 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1249 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1255 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1256 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1257 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1260 lock_ReleaseMutex(&bufp->mx);
1261 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1263 lock_ReleaseWrite(&scp->rw);
1264 lock_ObtainMutex(&bufp->mx);
1265 lock_ObtainWrite(&scp->rw);
1269 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1275 /* can't check access rights without a callback */
1276 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1278 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1279 return CM_ERROR_READONLY;
1281 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1282 if (~outRights & rights)
1283 return CM_ERROR_NOACCESS;
1286 /* we don't know the required access rights */
1287 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1288 code = cm_GetAccessRights(scp, userp, reqp);
1290 lock_ReleaseWrite(&scp->rw);
1291 lock_ObtainMutex(&bufp->mx);
1292 lock_ObtainWrite(&scp->rw);
1300 /* if we get here, we're happy */
1304 /* first check if we're not supposed to wait: fail
1305 * in this case, returning with everything still locked.
1307 if (flags & CM_SCACHESYNC_NOWAIT)
1308 return CM_ERROR_WOULDBLOCK;
1310 /* These are used for minidump debugging */
1311 sleep_scp_flags = scp->flags; /* so we know why we slept */
1312 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1313 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1315 /* wait here, then try again */
1316 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1317 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1319 scp->waitRequests++;
1320 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1321 scp, scp->waitCount, scp->waitRequests);
1323 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1324 scp->flags |= CM_SCACHEFLAG_WAITING;
1325 scp->waitCount = scp->waitRequests = 1;
1328 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1332 lock_ReleaseMutex(&bufp->mx);
1333 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1335 lock_ObtainMutex(&bufp->mx);
1336 lock_ObtainWrite(&scp->rw);
1337 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1339 cm_UpdateServerPriority();
1342 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1343 scp, scp->waitCount, scp->waitRequests);
1344 if (scp->waitCount == 0) {
1345 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1346 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1347 scp->waitRequests = 0;
1349 } /* big while loop */
1351 /* now, update the recorded state for RPC-type calls */
1352 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1353 scp->flags |= CM_SCACHEFLAG_FETCHING;
1354 if (flags & CM_SCACHESYNC_STORESTATUS)
1355 scp->flags |= CM_SCACHEFLAG_STORING;
1356 if (flags & CM_SCACHESYNC_SETSIZE)
1357 scp->flags |= CM_SCACHEFLAG_SIZESETTING;
1358 if (flags & CM_SCACHESYNC_STORESIZE)
1359 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1360 if (flags & CM_SCACHESYNC_GETCALLBACK)
1361 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1362 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1363 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1364 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1365 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1366 if (flags & CM_SCACHESYNC_LOCK)
1367 scp->flags |= CM_SCACHEFLAG_LOCKING;
1369 /* now update the buffer pointer */
1370 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1371 /* ensure that the buffer isn't already in the I/O list */
1372 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1373 tbufp = osi_GetQData(qdp);
1374 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1377 /* queue a held reference to the buffer in the "reading" I/O list */
1378 qdp = osi_QDAlloc();
1379 osi_SetQData(qdp, bufp);
1382 bufp->cmFlags |= CM_BUF_CMFETCHING;
1383 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1386 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1387 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1388 "attempting to store extents on a non-file object");
1390 /* ensure that the buffer isn't already in the I/O list */
1391 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1392 tbufp = osi_GetQData(qdp);
1393 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1396 /* queue a held reference to the buffer in the "writing" I/O list */
1397 qdp = osi_QDAlloc();
1398 osi_SetQData(qdp, bufp);
1400 bufp->cmFlags |= CM_BUF_CMSTORING;
1401 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1404 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1405 /* mark the buffer as being written to. */
1406 bufp->cmFlags |= CM_BUF_CMWRITING;
1412 /* for those syncops that setup for RPCs.
1413 * Called with scache locked.
1415 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1417 osi_queueData_t *qdp;
1420 lock_AssertWrite(&scp->rw);
1422 /* now, update the recorded state for RPC-type calls */
1423 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1424 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1425 if (flags & CM_SCACHESYNC_STORESTATUS)
1426 scp->flags &= ~CM_SCACHEFLAG_STORING;
1427 if (flags & CM_SCACHESYNC_SETSIZE)
1428 scp->flags &= ~CM_SCACHEFLAG_SIZESETTING;
1429 if (flags & CM_SCACHESYNC_STORESIZE)
1430 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1431 if (flags & CM_SCACHESYNC_GETCALLBACK)
1432 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1433 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1434 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1435 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1436 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1437 if (flags & CM_SCACHESYNC_LOCK)
1438 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1440 /* now update the buffer pointer */
1441 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1444 /* ensure that the buffer is in the I/O list */
1445 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1446 tbufp = osi_GetQData(qdp);
1451 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1455 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1456 if (bufp->flags & CM_BUF_WAITING) {
1457 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1458 osi_Wakeup((LONG_PTR) &bufp);
1464 /* now update the buffer pointer */
1465 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1467 /* ensure that the buffer is in the I/O list */
1468 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1469 tbufp = osi_GetQData(qdp);
1474 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1478 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1479 if (bufp->flags & CM_BUF_WAITING) {
1480 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1481 osi_Wakeup((LONG_PTR) &bufp);
1487 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1488 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1489 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1492 /* and wakeup anyone who is waiting */
1493 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1494 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1495 osi_Wakeup((LONG_PTR) &scp->flags);
1499 /* merge in a response from an RPC. The scp must be locked, and the callback
1502 * Don't overwrite any status info that is dirty, since we could have a store
1503 * operation (such as store data) that merges some info in, and we don't want
1504 * to lose the local updates. Typically, there aren't many updates we do
1505 * locally, anyway, probably only mtime.
1507 * There is probably a bug in here where a chmod (which doesn't change
1508 * serverModTime) that occurs between two fetches, both of whose responses are
1509 * handled after the callback breaking is done, but only one of whose calls
1510 * started before that, can cause old info to be merged from the first call.
1512 void cm_MergeStatus(cm_scache_t *dscp,
1513 cm_scache_t *scp, AFSFetchStatus *statusp,
1514 AFSVolSync *volsyncp,
1515 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1517 afs_uint64 dataVersion;
1518 struct cm_volume *volp = NULL;
1519 struct cm_cell *cellp = NULL;
1521 // yj: i want to create some fake status for the /afs directory and the
1522 // entries under that directory
1523 #ifdef AFS_FREELANCE_CLIENT
1524 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1525 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1526 if (scp == cm_data.rootSCachep) {
1527 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1528 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1529 statusp->Length = cm_fakeDirSize;
1530 statusp->Length_hi = 0;
1532 statusp->FileType = scp->fileType;
1533 statusp->Length = scp->length.LowPart;
1534 statusp->Length_hi = scp->length.HighPart;
1536 statusp->InterfaceVersion = 0x1;
1537 statusp->LinkCount = scp->linkCount;
1538 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1539 statusp->Author = 0x1;
1540 statusp->Owner = 0x0;
1541 statusp->CallerAccess = 0x9;
1542 statusp->AnonymousAccess = 0x9;
1543 statusp->UnixModeBits = 0777;
1544 statusp->ParentVnode = 0x1;
1545 statusp->ParentUnique = 0x1;
1546 statusp->ResidencyMask = 0;
1547 statusp->ClientModTime = FakeFreelanceModTime;
1548 statusp->ServerModTime = FakeFreelanceModTime;
1550 statusp->SyncCounter = 0;
1551 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1552 statusp->lockCount = 0;
1553 statusp->errorCode = 0;
1555 #endif /* AFS_FREELANCE_CLIENT */
1557 if (statusp->errorCode != 0) {
1558 scp->flags |= CM_SCACHEFLAG_EACCESS;
1559 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1561 scp->fileType = 0; /* unknown */
1563 scp->serverModTime = 0;
1564 scp->clientModTime = 0;
1565 scp->length.LowPart = 0;
1566 scp->length.HighPart = 0;
1567 scp->serverLength.LowPart = 0;
1568 scp->serverLength.HighPart = 0;
1572 scp->unixModeBits = 0;
1574 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1575 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1576 scp->fsLockCount = 0;
1579 scp->parentVnode = dscp->fid.vnode;
1580 scp->parentUnique = dscp->fid.unique;
1582 scp->parentVnode = 0;
1583 scp->parentUnique = 0;
1587 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1590 dataVersion = statusp->dataVersionHigh;
1592 dataVersion |= statusp->DataVersion;
1594 if (!(flags & CM_MERGEFLAG_FORCE) &&
1595 dataVersion < scp->dataVersion &&
1596 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1598 cellp = cm_FindCellByID(scp->fid.cell, 0);
1599 if (scp->cbServerp) {
1600 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1601 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1602 osi_Log2(afsd_logp, "old data from server %x volume %s",
1603 scp->cbServerp->addr.sin_addr.s_addr,
1604 volp ? volp->namep : "(unknown)");
1606 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1607 scp, scp->dataVersion, dataVersion);
1608 /* we have a number of data fetch/store operations running
1609 * concurrently, and we can tell which one executed last at the
1610 * server by its mtime.
1611 * Choose the one with the largest mtime, and ignore the rest.
1613 * These concurrent calls are incompatible with setting the
1614 * mtime, so we won't have a locally changed mtime here.
1616 * We could also have ACL info for a different user than usual,
1617 * in which case we have to do that part of the merge, anyway.
1618 * We won't have to worry about the info being old, since we
1619 * won't have concurrent calls
1620 * that change file status running from this machine.
1622 * Added 3/17/98: if we see data version regression on an RO
1623 * file, it's probably due to a server holding an out-of-date
1624 * replica, rather than to concurrent RPC's. Failures to
1625 * release replicas are now flagged by the volserver, but only
1626 * since AFS 3.4 5.22, so there are plenty of clients getting
1627 * out-of-date replicas out there.
1629 * If we discover an out-of-date replica, by this time it's too
1630 * late to go to another server and retry. Also, we can't
1631 * reject the merge, because then there is no way for
1632 * GetAccess to do its work, and the caller gets into an
1633 * infinite loop. So we just grin and bear it.
1635 if (!(scp->flags & CM_SCACHEFLAG_RO))
1639 if (cm_readonlyVolumeVersioning)
1640 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1642 scp->serverModTime = statusp->ServerModTime;
1644 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1645 scp->clientModTime = statusp->ClientModTime;
1647 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1648 scp->length.LowPart = statusp->Length;
1649 scp->length.HighPart = statusp->Length_hi;
1652 scp->serverLength.LowPart = statusp->Length;
1653 scp->serverLength.HighPart = statusp->Length_hi;
1655 scp->linkCount = statusp->LinkCount;
1656 scp->owner = statusp->Owner;
1657 scp->group = statusp->Group;
1658 scp->unixModeBits = statusp->UnixModeBits & 07777;
1660 if (statusp->FileType == File)
1661 scp->fileType = CM_SCACHETYPE_FILE;
1662 else if (statusp->FileType == Directory)
1663 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1664 else if (statusp->FileType == SymbolicLink) {
1665 if ((scp->unixModeBits & 0111) == 0)
1666 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1668 scp->fileType = CM_SCACHETYPE_SYMLINK;
1671 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1672 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1674 /* and other stuff */
1675 scp->parentVnode = statusp->ParentVnode;
1676 scp->parentUnique = statusp->ParentUnique;
1677 scp->fsLockCount = statusp->lockCount;
1679 /* and merge in the private acl cache info, if this is more than the public
1680 * info; merge in the public stuff in any case.
1682 scp->anyAccess = statusp->AnonymousAccess;
1684 if (userp != NULL) {
1685 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1688 if (scp->dataVersion != 0 &&
1689 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1690 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1692 * We now know that all of the data buffers that we have associated
1693 * with this scp are invalid. Subsequent operations will go faster
1694 * if the buffers are removed from the hash tables.
1696 * We do not remove directory buffers if the dataVersion delta is 1 because
1697 * those version numbers will be updated as part of the directory operation.
1699 * We do not remove storedata buffers because they will still be valid.
1704 cm_buf_t *bp, *prevBp, *nextBp;
1706 lock_ObtainWrite(&buf_globalLock);
1707 i = BUF_FILEHASH(&scp->fid);
1708 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1710 nextBp = bp->fileHashp;
1712 * if the buffer belongs to this stat cache entry
1713 * and the buffer mutex can be obtained, check the
1714 * reference count and if it is zero, remove the buffer
1715 * from the hash tables. If there are references,
1716 * the buffer might be updated to the current version
1717 * so leave it in place.
1719 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1720 lock_TryMutex(&bp->mx)) {
1721 if (bp->refCount == 0 &&
1722 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1723 prevBp = bp->fileHashBackp;
1724 bp->fileHashBackp = bp->fileHashp = NULL;
1726 prevBp->fileHashp = nextBp;
1728 cm_data.buf_fileHashTablepp[i] = nextBp;
1730 nextBp->fileHashBackp = prevBp;
1732 j = BUF_HASH(&bp->fid, &bp->offset);
1733 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1734 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1739 *lbpp = bp->hashp; /* hash out */
1742 bp->qFlags &= ~CM_BUF_QINHASH;
1744 lock_ReleaseMutex(&bp->mx);
1747 lock_ReleaseWrite(&buf_globalLock);
1751 * If the dataVersion has changed, the mountPointStringp must be cleared
1752 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1753 * does not update a mountpoint or symlink by altering the contents of
1754 * the file data; but the Unix CM does.
1756 if (scp->dataVersion != dataVersion)
1757 scp->mountPointStringp[0] = '\0';
1759 /* We maintain a range of buffer dataVersion values which are considered
1760 * valid. This avoids the need to update the dataVersion on each buffer
1761 * object during an uncontested storeData operation. As a result this
1762 * merge status no longer has performance characteristics derived from
1763 * the size of the file.
1765 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1766 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1767 scp->bufDataVersionLow == 0)
1768 scp->bufDataVersionLow = dataVersion;
1770 scp->dataVersion = dataVersion;
1773 * If someone is waiting for status information, we can wake them up
1774 * now even though the entity that issued the FetchStatus may not
1775 * have completed yet.
1777 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1780 * We just successfully merged status on the stat cache object.
1781 * This means that the associated volume must be online.
1785 cellp = cm_FindCellByID(scp->fid.cell, 0);
1786 cm_FindVolumeByID(cellp, scp->fid.volume, userp, reqp, 0, &volp);
1789 cm_vol_state_t *statep = cm_VolumeStateByID(volp, scp->fid.volume);
1790 if (statep->state != vl_online) {
1791 lock_ObtainWrite(&volp->rw);
1792 cm_VolumeStatusNotification(volp, statep->ID, statep->state, vl_online);
1793 statep->state = vl_online;
1794 lock_ReleaseWrite(&volp->rw);
1803 /* note that our stat cache info is incorrect, so force us eventually
1804 * to stat the file again. There may be dirty data associated with
1805 * this vnode, and we want to preserve that information.
1807 * This function works by simply simulating a loss of the callback.
1809 * This function must be called with the scache locked.
1811 void cm_DiscardSCache(cm_scache_t *scp)
1813 lock_AssertWrite(&scp->rw);
1814 if (scp->cbServerp) {
1815 cm_PutServer(scp->cbServerp);
1816 scp->cbServerp = NULL;
1819 scp->volumeCreationDate = 0;
1820 scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
1821 cm_dnlcPurgedp(scp);
1822 cm_dnlcPurgevp(scp);
1823 cm_FreeAllACLEnts(scp);
1825 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1826 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1828 /* Force mount points and symlinks to be re-evaluated */
1829 scp->mountPointStringp[0] = '\0';
1832 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1834 afsFidp->Volume = fidp->volume;
1835 afsFidp->Vnode = fidp->vnode;
1836 afsFidp->Unique = fidp->unique;
1839 #ifdef DEBUG_REFCOUNT
1840 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1842 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1847 osi_assertx(scp != NULL, "null cm_scache_t");
1848 lock_AssertAny(&cm_scacheLock);
1849 refCount = InterlockedIncrement(&scp->refCount);
1850 #ifdef DEBUG_REFCOUNT
1851 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1852 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1856 #ifdef DEBUG_REFCOUNT
1857 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1859 void cm_HoldSCache(cm_scache_t *scp)
1864 osi_assertx(scp != NULL, "null cm_scache_t");
1865 lock_ObtainRead(&cm_scacheLock);
1866 refCount = InterlockedIncrement(&scp->refCount);
1867 #ifdef DEBUG_REFCOUNT
1868 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1869 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1871 lock_ReleaseRead(&cm_scacheLock);
1874 #ifdef DEBUG_REFCOUNT
1875 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1877 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1882 osi_assertx(scp != NULL, "null cm_scache_t");
1883 lock_AssertAny(&cm_scacheLock);
1885 refCount = InterlockedDecrement(&scp->refCount);
1886 #ifdef DEBUG_REFCOUNT
1888 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1890 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1891 #ifdef DEBUG_REFCOUNT
1892 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1893 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1896 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1900 lockstate = lock_GetRWLockState(&cm_scacheLock);
1901 if (lockstate != OSI_RWLOCK_WRITEHELD)
1902 lock_ReleaseRead(&cm_scacheLock);
1904 lock_ReleaseWrite(&cm_scacheLock);
1906 lock_ObtainWrite(&scp->rw);
1907 if (scp->flags & CM_SCACHEFLAG_DELETED)
1910 if (refCount == 0 && deleted) {
1911 lock_ObtainWrite(&cm_scacheLock);
1912 cm_RecycleSCache(scp, 0);
1913 if (lockstate != OSI_RWLOCK_WRITEHELD)
1914 lock_ConvertWToR(&cm_scacheLock);
1916 if (lockstate != OSI_RWLOCK_WRITEHELD)
1917 lock_ObtainRead(&cm_scacheLock);
1919 lock_ObtainWrite(&cm_scacheLock);
1921 lock_ReleaseWrite(&scp->rw);
1925 #ifdef DEBUG_REFCOUNT
1926 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1928 void cm_ReleaseSCache(cm_scache_t *scp)
1933 osi_assertx(scp != NULL, "null cm_scache_t");
1934 lock_ObtainRead(&cm_scacheLock);
1935 refCount = InterlockedDecrement(&scp->refCount);
1936 #ifdef DEBUG_REFCOUNT
1938 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1940 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1941 #ifdef DEBUG_REFCOUNT
1942 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1943 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1945 lock_ReleaseRead(&cm_scacheLock);
1947 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1949 lock_ObtainWrite(&scp->rw);
1950 if (scp->flags & CM_SCACHEFLAG_DELETED)
1953 lock_ObtainWrite(&cm_scacheLock);
1954 cm_RecycleSCache(scp, 0);
1955 lock_ReleaseWrite(&cm_scacheLock);
1957 lock_ReleaseWrite(&scp->rw);
1961 /* just look for the scp entry to get filetype */
1962 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1963 int cm_FindFileType(cm_fid_t *fidp)
1968 hash = CM_SCACHE_HASH(fidp);
1970 osi_assertx(fidp->cell != 0, "unassigned cell value");
1972 lock_ObtainWrite(&cm_scacheLock);
1973 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1974 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1975 lock_ReleaseWrite(&cm_scacheLock);
1976 return scp->fileType;
1979 lock_ReleaseWrite(&cm_scacheLock);
1983 /* dump all scp's that have reference count > 0 to a file.
1984 * cookie is used to identify this batch for easy parsing,
1985 * and it a string provided by a caller
1987 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1996 lock_ObtainRead(&cm_scacheLock);
1998 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1999 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2001 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
2004 char *srvStr = NULL;
2005 afs_uint32 srvStrRpc = TRUE;
2009 if (scp->cbServerp) {
2010 if (!((scp->cbServerp->flags & CM_SERVERFLAG_UUID) &&
2011 UuidToString((UUID *)&scp->cbServerp->uuid, &srvStr) == RPC_S_OK)) {
2012 afs_asprintf(&srvStr, "%.0I", scp->cbServerp->addr.sin_addr.s_addr);
2016 if (scp->cbExpires) {
2021 cbt[strlen(cbt)-1] = '\0';
2024 if (scp->volumeCreationDate) {
2025 t = scp->volumeCreationDate;
2028 cdrot = strdup(cdrot);
2029 cdrot[strlen(cdrot)-1] = '\0';
2033 "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x "
2034 "mp='%s' Locks (server=0x%x shared=%d excl=%d clnt=%d) fsLockCount=%d linkCount=%d anyAccess=0x%x "
2035 "flags=0x%x cbServer='%s' cbExpires='%s' volumeCreationDate='%s' refCount=%u\r\n",
2036 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
2037 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp,
2038 scp->serverLock, scp->sharedLocks, scp->exclusiveLocks, scp->clientLocks, scp->fsLockCount,
2039 scp->linkCount, scp->anyAccess, scp->flags, srvStr ? srvStr : "<none>", cbt ? cbt : "<none>",
2040 cdrot ? cdrot : "<none>", scp->refCount);
2041 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2043 if (scp->fileLocksH) {
2044 sprintf(output, " %s - begin dumping scp locks\r\n", cookie);
2045 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2047 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
2048 cm_file_lock_t * lockp = (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
2049 sprintf(output, " %s lockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x "
2050 "key=0x%I64x flags=0x%x update=0x%I64u\r\n",
2051 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2052 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2053 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2056 sprintf(output, " %s - done dumping scp locks\r\n", cookie);
2057 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2062 RpcStringFree(&srvStr);
2072 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
2073 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2074 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
2075 cookie, cm_data.scacheHashTableSize);
2076 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2078 for (i = 0; i < cm_data.scacheHashTableSize; i++)
2080 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
2082 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
2083 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
2084 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2088 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
2089 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2091 sprintf(output, "%s - begin dumping all file locks\r\n", cookie);
2092 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2094 for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
2095 cm_file_lock_t * lockp = (cm_file_lock_t *)q;
2096 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",
2097 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
2098 lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
2099 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2102 sprintf(output, "%s - done dumping all file locks\r\n", cookie);
2103 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
2106 lock_ReleaseRead(&cm_scacheLock);