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>
26 #include <afs/unified_afs.h>
28 /*extern void afsi_log(char *pattern, ...);*/
30 extern osi_hyper_t hzero;
33 osi_queue_t *cm_allFileLocks;
34 osi_queue_t *cm_freeFileLocks;
35 unsigned long cm_lockRefreshCycle;
37 /* lock for globals */
38 osi_rwlock_t cm_scacheLock;
40 /* Dummy scache entry for use with pioctl fids */
41 cm_scache_t cm_fakeSCache;
43 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
45 #ifdef AFS_FREELANCE_CLIENT
46 extern osi_mutex_t cm_Freelance_Lock;
50 cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp)
54 lock_ObtainWrite(&cm_data.rootSCachep->rw);
55 code = cm_SyncOp(cm_data.rootSCachep, NULL, userp, reqp, 0,
56 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
58 cm_SyncOpDone(cm_data.rootSCachep, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
59 lock_ReleaseWrite(&cm_data.rootSCachep->rw);
61 return cm_data.rootSCachep;
65 /* must be called with cm_scacheLock write-locked! */
66 void cm_AdjustScacheLRU(cm_scache_t *scp)
68 lock_AssertWrite(&cm_scacheLock);
69 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
70 if (scp->flags & CM_SCACHEFLAG_DELETED) {
71 /* Since it has been deleted make it the first to be recycled. */
72 osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
74 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
78 /* call with cm_scacheLock write-locked and scp rw held */
79 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
85 lock_AssertWrite(&cm_scacheLock);
86 lock_AssertWrite(&scp->rw);
87 if (scp->flags & CM_SCACHEFLAG_INHASH) {
88 /* hash it out first */
89 i = CM_SCACHE_HASH(&scp->fid);
90 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
92 lscpp = &tscp->nextp, tscp = tscp->nextp) {
96 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
103 /* called with cm_scacheLock and scp write-locked */
104 void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
107 /* destroy directory Bplus Tree */
109 LARGE_INTEGER start, end;
111 if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
113 * We are not holding the dirlock and obtaining it
114 * requires that we drop the scp->rw. As a result
115 * we will leave the dirBplus tree intact but
116 * invalidate the version number so that whatever
117 * operation is currently active can safely complete
118 * but the contents will be ignored on the next
119 * directory operation.
121 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
125 QueryPerformanceCounter(&start);
127 freeBtree(scp->dirBplus);
128 scp->dirBplus = NULL;
129 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
130 QueryPerformanceCounter(&end);
133 lock_ReleaseWrite(&scp->dirlock);
135 bplus_free_time += (end.QuadPart - start.QuadPart);
140 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
141 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
143 lock_AssertWrite(&cm_scacheLock);
144 lock_AssertWrite(&scp->rw);
146 if (scp->refCount != 0) {
150 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
151 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
153 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
158 if (scp->redirBufCount != 0) {
162 cm_RemoveSCacheFromHashTable(scp);
164 /* invalidate so next merge works fine;
165 * also initialize some flags */
167 _InterlockedAnd(&scp->flags,
168 ~(CM_SCACHEFLAG_STATD
169 | CM_SCACHEFLAG_DELETED
171 | CM_SCACHEFLAG_PURERO
172 | CM_SCACHEFLAG_OVERQUOTA
173 | CM_SCACHEFLAG_OUTOFSPACE
174 | CM_SCACHEFLAG_EACCESS));
175 scp->serverModTime = 0;
176 scp->dataVersion = CM_SCACHE_VERSION_BAD;
177 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
178 scp->bulkStatProgress = hzero;
180 scp->waitQueueT = NULL;
182 if (scp->cbServerp) {
183 cm_PutServer(scp->cbServerp);
184 scp->cbServerp = NULL;
187 scp->volumeCreationDate = 0;
195 /* remove from dnlc */
199 /* discard cached status; if non-zero, Close
200 * tried to store this to server but failed */
203 /* discard symlink info */
204 scp->mountPointStringp[0] = '\0';
205 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
206 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
208 /* reset locking info */
209 scp->fileLocksH = NULL;
210 scp->fileLocksT = NULL;
211 scp->serverLock = (-1);
212 scp->exclusiveLocks = 0;
213 scp->sharedLocks = 0;
214 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
215 scp->fsLockCount = 0;
217 /* not locked, but there can be no references to this guy
218 * while we hold the global refcount lock.
220 cm_FreeAllACLEnts(scp);
222 cm_ResetSCacheDirectory(scp, 0);
228 * called with cm_scacheLock write-locked; find a vnode to recycle.
229 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
230 * returns scp->rw write-locked.
232 cm_scache_t *cm_GetNewSCache(void)
237 lock_AssertWrite(&cm_scacheLock);
238 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
239 /* There were no deleted scache objects that we could use. Try to find
240 * one that simply hasn't been used in a while.
242 for (retry = 0 ; retry < 2; retry++) {
243 for ( scp = cm_data.scacheLRULastp;
245 scp = (cm_scache_t *) osi_QPrev(&scp->q))
247 /* It is possible for the refCount to be zero and for there still
248 * to be outstanding dirty buffers. If there are dirty buffers,
249 * we must not recycle the scp.
251 * If the object is in use by the redirector, then avoid recycling
252 * it unless we have to.
254 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
255 if (!buf_DirtyBuffersExist(&scp->fid) && !buf_RDRBuffersExist(&scp->fid)) {
259 if (!lock_TryWrite(&scp->rw))
262 /* Found a likely candidate. Save type and fid in case we succeed */
264 fileType = scp->fileType;
266 if (!cm_RecycleSCache(scp, 0)) {
267 /* we found an entry, so return it.
268 * remove from the LRU queue and put it back at the
269 * head of the LRU queue.
271 cm_AdjustScacheLRU(scp);
273 if (RDR_Initialized) {
275 * We drop the cm_scacheLock because it may be required to
276 * satisfy an ioctl request from the redirector. It should
277 * be safe to hold the scp->rw lock here because at this
278 * point (a) the object has just been recycled so the fid
279 * is nul and there are no requests that could possibly
280 * be issued by the redirector that would depend upon it.
282 lock_ReleaseWrite(&cm_scacheLock);
283 RDR_InvalidateObject( fid.cell, fid.volume, fid.vnode,
284 fid.unique, fid.hash,
285 fileType, AFS_INVALIDATE_EXPIRED);
286 lock_ObtainWrite(&cm_scacheLock);
292 lock_ReleaseWrite(&scp->rw);
294 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%p", scp);
298 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
303 /* if we get here, we should allocate a new scache entry. We either are below
304 * quota or we have a leak and need to allocate a new one to avoid panicing.
306 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
307 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
308 "invalid cm_scache_t address");
309 memset(scp, 0, sizeof(cm_scache_t));
310 scp->magic = CM_SCACHE_MAGIC;
311 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
312 osi_assertx(lock_TryWrite(&scp->rw), "cm_scache_t rw held after allocation");
313 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
315 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
317 scp->serverLock = -1;
319 /* and put it in the LRU queue */
320 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
321 if (!cm_data.scacheLRULastp)
322 cm_data.scacheLRULastp = scp;
323 cm_data.currentSCaches++;
324 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
326 scp->allNextp = cm_data.allSCachesp;
327 cm_data.allSCachesp = scp;
331 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
334 fidp->volume = volume;
336 fidp->unique = unique;
337 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
340 /* like strcmp, only for fids */
341 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
343 if (ap->hash != bp->hash)
345 if (ap->vnode != bp->vnode)
347 if (ap->volume != bp->volume)
349 if (ap->unique != bp->unique)
351 if (ap->cell != bp->cell)
356 void cm_fakeSCacheInit(int newFile)
359 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
360 cm_data.fakeSCache.magic = CM_SCACHE_MAGIC;
361 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
362 cm_data.fakeSCache.cbExpires = (time_t)-1;
363 /* can leave clientModTime at 0 */
364 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
365 cm_data.fakeSCache.unixModeBits = 0777;
366 cm_data.fakeSCache.length.LowPart = 1000;
367 cm_data.fakeSCache.linkCount = 1;
368 cm_data.fakeSCache.refCount = 1;
369 cm_data.fakeSCache.serverLock = -1;
370 cm_data.fakeSCache.dataVersion = CM_SCACHE_VERSION_BAD;
372 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
373 lock_InitializeRWLock(&cm_data.fakeSCache.bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
374 lock_InitializeRWLock(&cm_data.fakeSCache.dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
378 cm_ValidateSCache(void)
380 cm_scache_t * scp, *lscp;
383 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
384 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
385 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
386 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
390 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
392 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
393 if (scp->magic != CM_SCACHE_MAGIC) {
394 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
395 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
398 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
399 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
400 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
403 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
404 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
405 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
408 if (i > cm_data.currentSCaches ) {
409 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
410 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
413 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
414 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
415 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
420 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
421 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
422 if (scp->magic != CM_SCACHE_MAGIC) {
423 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
424 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
427 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
428 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
429 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
432 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
433 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
434 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
437 if (i > cm_data.currentSCaches ) {
438 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
439 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
442 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
443 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
444 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
449 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
450 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
452 hash = CM_SCACHE_HASH(&scp->fid);
453 if (scp->magic != CM_SCACHE_MAGIC) {
454 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
455 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
458 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
459 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
460 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
463 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
464 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
465 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
469 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
470 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
476 return cm_dnlcValidate();
480 cm_SuspendSCache(void)
485 cm_GiveUpAllCallbacksAllServersMulti(TRUE);
488 * After this call all servers are marked down.
489 * Do not clear the callbacks, instead change the
490 * expiration time so that the callbacks will be expired
491 * when the servers are marked back up. However, we
492 * want the callbacks to be preserved as long as the
493 * servers are down. That way if the machine resumes
494 * without network, the stat cache item will still be
499 lock_ObtainWrite(&cm_scacheLock);
500 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
501 if (scp->cbServerp) {
502 if (scp->flags & CM_SCACHEFLAG_PURERO) {
503 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
505 if (volp->cbExpiresRO == scp->cbExpires)
506 volp->cbExpiresRO = now+1;
510 scp->cbExpires = now+1;
513 lock_ReleaseWrite(&cm_scacheLock);
517 cm_ShutdownSCache(void)
519 cm_scache_t * scp, * nextp;
521 cm_GiveUpAllCallbacksAllServersMulti(FALSE);
523 lock_ObtainWrite(&cm_scacheLock);
525 for ( scp = cm_data.allSCachesp; scp;
527 nextp = scp->allNextp;
528 lock_ReleaseWrite(&cm_scacheLock);
530 lock_ObtainWrite(&scp->dirlock);
532 lock_ObtainWrite(&scp->rw);
533 lock_ObtainWrite(&cm_scacheLock);
535 if (scp->randomACLp) {
536 cm_FreeAllACLEnts(scp);
539 if (scp->cbServerp) {
540 cm_PutServer(scp->cbServerp);
541 scp->cbServerp = NULL;
544 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
545 lock_ReleaseWrite(&scp->rw);
549 freeBtree(scp->dirBplus);
550 scp->dirBplus = NULL;
551 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
552 lock_ReleaseWrite(&scp->dirlock);
553 lock_FinalizeRWLock(&scp->dirlock);
555 lock_FinalizeRWLock(&scp->rw);
556 lock_FinalizeRWLock(&scp->bufCreateLock);
558 lock_ReleaseWrite(&cm_scacheLock);
560 return cm_dnlcShutdown();
563 void cm_InitSCache(int newFile, long maxSCaches)
565 static osi_once_t once;
567 if (osi_Once(&once)) {
568 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
570 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
571 cm_data.allSCachesp = NULL;
572 cm_data.currentSCaches = 0;
573 cm_data.maxSCaches = maxSCaches;
574 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
578 for ( scp = cm_data.allSCachesp; scp;
579 scp = scp->allNextp ) {
580 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw", LOCK_HIERARCHY_SCACHE);
581 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock", LOCK_HIERARCHY_SCACHE_BUFCREATE);
583 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock", LOCK_HIERARCHY_SCACHE_DIRLOCK);
585 scp->cbServerp = NULL;
587 scp->volumeCreationDate = 0;
588 scp->fileLocksH = NULL;
589 scp->fileLocksT = NULL;
590 scp->serverLock = (-1);
591 scp->lastRefreshCycle = 0;
592 scp->exclusiveLocks = 0;
593 scp->sharedLocks = 0;
600 scp->dirBplus = NULL;
601 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
603 scp->waitQueueT = NULL;
604 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_WAITING | CM_SCACHEFLAG_RDR_IN_USE));
606 scp->redirBufCount = 0;
607 scp->redirQueueT = NULL;
608 scp->redirQueueH = NULL;
611 cm_allFileLocks = NULL;
612 cm_freeFileLocks = NULL;
613 cm_lockRefreshCycle = 0;
614 cm_fakeSCacheInit(newFile);
615 cm_allFreeWaiters = NULL;
616 cm_dnlcInit(newFile);
621 /* version that doesn't bother creating the entry if we don't find it */
622 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
627 hash = CM_SCACHE_HASH(fidp);
629 if (fidp->cell == 0) {
633 lock_ObtainRead(&cm_scacheLock);
634 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
635 if (cm_FidCmp(fidp, &scp->fid) == 0) {
636 cm_HoldSCacheNoLock(scp);
637 lock_ConvertRToW(&cm_scacheLock);
638 cm_AdjustScacheLRU(scp);
639 lock_ReleaseWrite(&cm_scacheLock);
643 lock_ReleaseRead(&cm_scacheLock);
647 #ifdef DEBUG_REFCOUNT
648 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
649 cm_req_t *reqp, char * file, long line)
651 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
656 cm_scache_t *scp = NULL;
658 cm_volume_t *volp = NULL;
660 int special = 0; // yj: boolean variable to test if file is on root.afs
662 extern cm_fid_t cm_rootFid;
664 hash = CM_SCACHE_HASH(fidp);
667 return CM_ERROR_INVAL;
669 #ifdef AFS_FREELANCE_CLIENT
670 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
671 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
672 !(fidp->vnode==0x1 && fidp->unique==0x1));
673 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
674 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
675 fidp->vnode==0x1 && fidp->unique==0x1);
678 // yj: check if we have the scp, if so, we don't need
679 // to do anything else
680 lock_ObtainWrite(&cm_scacheLock);
681 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
682 if (cm_FidCmp(fidp, &scp->fid) == 0) {
683 #ifdef DEBUG_REFCOUNT
684 afsi_log("%s:%d cm_GetSCache (1) scp 0x%p ref %d", file, line, scp, scp->refCount);
685 osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
687 #ifdef AFS_FREELANCE_CLIENT
688 if (cm_freelanceEnabled && special &&
689 cm_data.fakeDirVersion != scp->dataVersion)
692 cm_HoldSCacheNoLock(scp);
694 cm_AdjustScacheLRU(scp);
695 lock_ReleaseWrite(&cm_scacheLock);
700 // yj: when we get here, it means we don't have an scp
701 // so we need to either load it or fake it, depending
702 // on whether the file is "special", see below.
704 // yj: if we're trying to get an scp for a file that's
705 // on root.afs of homecell, we want to handle it specially
706 // because we have to fill in the status stuff 'coz we
707 // don't want trybulkstat to fill it in for us
708 #ifdef AFS_FREELANCE_CLIENT
709 if (cm_freelanceEnabled && isRoot) {
710 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
711 /* freelance: if we are trying to get the root scp for the first
712 * time, we will just put in a place holder entry.
717 if (cm_freelanceEnabled && special) {
718 lock_ReleaseWrite(&cm_scacheLock);
719 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
721 if (cm_getLocalMountPointChange()) {
722 cm_clearLocalMountPointChange();
723 cm_reInitLocalMountPoints();
726 lock_ObtainWrite(&cm_scacheLock);
728 scp = cm_GetNewSCache(); /* returns scp->rw held */
730 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
731 lock_ReleaseWrite(&cm_scacheLock);
732 return CM_ERROR_WOULDBLOCK;
735 lock_ReleaseWrite(&cm_scacheLock);
736 lock_ObtainWrite(&scp->rw);
737 lock_ObtainWrite(&cm_scacheLock);
740 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
741 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
742 scp->dotdotFid.unique=1;
743 scp->dotdotFid.vnode=1;
744 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
745 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
746 scp->nextp = cm_data.scacheHashTablep[hash];
747 cm_data.scacheHashTablep[hash] = scp;
748 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
751 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
753 /* must be called after the scp->fid is set */
754 cm_FreelanceFetchMountPointString(scp);
755 cm_FreelanceFetchFileType(scp);
757 scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
758 scp->length.HighPart = 0;
760 scp->unixModeBits=0777;
761 scp->clientModTime=FakeFreelanceModTime;
762 scp->serverModTime=FakeFreelanceModTime;
763 scp->parentUnique = 0x1;
764 scp->parentVnode=0x1;
766 scp->dataVersion=cm_data.fakeDirVersion;
767 scp->bufDataVersionLow=cm_data.fakeDirVersion;
768 scp->lockDataVersion=CM_SCACHE_VERSION_BAD; /* no lock yet */
770 lock_ReleaseWrite(&scp->rw);
771 lock_ReleaseWrite(&cm_scacheLock);
773 #ifdef DEBUG_REFCOUNT
774 afsi_log("%s:%d cm_GetSCache (2) scp 0x%p ref %d", file, line, scp, scp->refCount);
775 osi_Log1(afsd_logp,"cm_GetSCache (2) scp 0x%p", scp);
780 #endif /* AFS_FREELANCE_CLIENT */
782 /* otherwise, we need to find the volume */
783 if (!cm_freelanceEnabled || !isRoot) {
784 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
785 cellp = cm_FindCellByID(fidp->cell, 0);
787 return CM_ERROR_NOSUCHCELL;
789 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
792 lock_ObtainWrite(&cm_scacheLock);
795 /* otherwise, we have the volume, now reverify that the scp doesn't
796 * exist, and proceed.
798 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
799 if (cm_FidCmp(fidp, &scp->fid) == 0) {
800 #ifdef DEBUG_REFCOUNT
801 afsi_log("%s:%d cm_GetSCache (3) scp 0x%p ref %d", file, line, scp, scp->refCount);
802 osi_Log1(afsd_logp,"cm_GetSCache (3) scp 0x%p", scp);
804 cm_HoldSCacheNoLock(scp);
805 cm_AdjustScacheLRU(scp);
806 lock_ReleaseWrite(&cm_scacheLock);
814 /* now, if we don't have the fid, recycle something */
815 scp = cm_GetNewSCache(); /* returns scp->rw held */
817 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
818 lock_ReleaseWrite(&cm_scacheLock);
821 return CM_ERROR_WOULDBLOCK;
823 #ifdef DEBUG_REFCOUNT
824 afsi_log("%s:%d cm_GetNewSCache returns scp 0x%p flags 0x%x", file, line, scp, scp->flags);
826 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%p flags 0x%x", scp, scp->flags);
828 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
831 if (!cm_freelanceEnabled || !isRoot) {
832 /* if this scache entry represents a volume root then we need
833 * to copy the dotdotFipd from the volume structure where the
834 * "master" copy is stored (defect 11489)
836 if (volp->vol[ROVOL].ID == fidp->volume) {
837 _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
838 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
839 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
840 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
841 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
842 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
843 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
845 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
846 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
851 scp->nextp = cm_data.scacheHashTablep[hash];
852 cm_data.scacheHashTablep[hash] = scp;
853 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
854 lock_ReleaseWrite(&scp->rw);
856 #ifdef DEBUG_REFCOUNT
857 afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
859 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
861 /* XXX - The following fields in the cm_scache are
868 /* now we have a held scache entry; just return it */
870 #ifdef DEBUG_REFCOUNT
871 afsi_log("%s:%d cm_GetSCache (4) scp 0x%p ref %d", file, line, scp, scp->refCount);
872 osi_Log1(afsd_logp,"cm_GetSCache (4) scp 0x%p", scp);
874 lock_ReleaseWrite(&cm_scacheLock);
878 /* Returns a held reference to the scache's parent
880 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
885 cm_scache_t * pscp = NULL;
887 lock_ObtainWrite(&cm_scacheLock);
888 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
890 if (cm_FidCmp(&scp->fid, &parent_fid)) {
891 i = CM_SCACHE_HASH(&parent_fid);
892 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
893 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
894 cm_HoldSCacheNoLock(pscp);
900 lock_ReleaseWrite(&cm_scacheLock);
905 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
907 cm_scache_waiter_t * w;
909 lock_ObtainWrite(&cm_scacheLock);
910 if (cm_allFreeWaiters == NULL) {
911 w = malloc(sizeof(*w));
912 memset(w, 0, sizeof(*w));
914 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
915 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
918 w->threadId = thrd_Current();
920 cm_HoldSCacheNoLock(scp);
924 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
925 lock_ReleaseWrite(&cm_scacheLock);
927 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
930 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
932 cm_scache_waiter_t * w;
935 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
937 lock_ObtainRead(&cm_scacheLock);
938 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
940 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
941 if (w->flags == flags && w->bufp == bufp) {
946 osi_assertx(w != NULL, "null cm_scache_waiter_t");
947 this_is_me = (w->threadId == thrd_Current());
948 lock_ReleaseRead(&cm_scacheLock);
951 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
955 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
957 lock_ObtainWrite(&cm_scacheLock);
958 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
959 cm_ReleaseSCacheNoLock(scp);
960 memset(w, 0, sizeof(*w));
961 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
962 lock_ReleaseWrite(&cm_scacheLock);
968 /* synchronize a fetch, store, read, write, fetch status or store status.
969 * Called with scache mutex held, and returns with it held, but temporarily
970 * drops it during the fetch.
972 * At most one flag can be on in flags, if this is an RPC request.
974 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
976 * There are a lot of weird restrictions here; here's an attempt to explain the
977 * rationale for the concurrency restrictions implemented in this function.
979 * First, although the file server will break callbacks when *another* machine
980 * modifies a file or status block, the client itself is responsible for
981 * concurrency control on its own requests. Callback breaking events are rare,
982 * and simply invalidate any concurrent new status info.
984 * In the absence of callback breaking messages, we need to know how to
985 * synchronize incoming responses describing updates to files. We synchronize
986 * operations that update the data version by comparing the data versions.
987 * However, updates that do not update the data, but only the status, can't be
988 * synchronized with fetches or stores, since there's nothing to compare
989 * to tell which operation executed first at the server.
991 * Thus, we can allow multiple ops that change file data, or dir data, and
992 * fetches. However, status storing ops have to be done serially.
994 * Furthermore, certain data-changing ops are incompatible: we can't read or
995 * write a buffer while doing a truncate. We can't read and write the same
996 * buffer at the same time, or write while fetching or storing, or read while
997 * fetching a buffer (this may change). We can't fetch and store at the same
1000 * With respect to status, we can't read and write at the same time, read while
1001 * fetching, write while fetching or storing, or fetch and store at the same time.
1003 * We can't allow a get callback RPC to run in concurrently with something that
1004 * will return updated status, since we could start a call, have the server
1005 * return status, have another machine make an update to the status (which
1006 * doesn't change serverModTime), have the original machine get a new callback,
1007 * and then have the original machine merge in the early, old info from the
1008 * first call. At this point, the easiest way to avoid this problem is to have
1009 * getcallback calls conflict with all others for the same vnode. Other calls
1010 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1011 * vnode must be careful not to merge in their status unless they have obtained
1012 * a callback from the start of their call.
1014 * Note added 1/23/96
1015 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1016 * Each such RPC passes a FileLength parameter, which the server uses to do
1017 * pre-truncation if necessary. So if two RPC's are processed out of order at
1018 * the server, the one with the smaller FileLength will be processed last,
1019 * possibly resulting in a bogus truncation. The simplest way to avoid this
1020 * is to serialize all StoreData RPC's. This is the reason we defined
1021 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1023 * CM_SCACHESYNC_BULKREAD is used to permit synchronization of multiple bulk
1024 * readers which may be requesting overlapping ranges.
1026 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1027 afs_uint32 rights, afs_uint32 flags)
1029 osi_queueData_t *qdp;
1032 afs_uint32 outRights;
1034 afs_uint32 sleep_scp_flags = 0;
1035 afs_uint32 sleep_buf_cmflags = 0;
1036 afs_uint32 sleep_scp_bufs = 0;
1039 lock_AssertWrite(&scp->rw);
1041 /* lookup this first */
1042 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1045 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1048 /* Do the access check. Now we don't really do the access check
1049 * atomically, since the caller doesn't expect the parent dir to be
1050 * returned locked, and that is what we'd have to do to prevent a
1051 * callback breaking message on the parent due to a setacl call from
1052 * being processed while we're running. So, instead, we check things
1053 * here, and if things look fine with the access, we proceed to finish
1054 * the rest of this check. Sort of a hack, but probably good enough.
1058 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1059 /* if we're bringing in a new status block, ensure that
1060 * we aren't already doing so, and that no one is
1061 * changing the status concurrently, either. We need
1062 * to do this, even if the status is of a different
1063 * type, since we don't have the ability to figure out,
1064 * in the AFS 3 protocols, which status-changing
1065 * operation ran first, or even which order a read and
1066 * a write occurred in.
1068 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1069 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1070 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1074 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1075 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1076 /* if we're going to make an RPC to change the status, make sure
1077 * that no one is bringing in or sending out the status.
1079 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1080 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1081 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1084 if ((!bufp || bufp && scp->fileType == CM_SCACHETYPE_FILE) &&
1085 (scp->bufReadsp || scp->bufWritesp)) {
1086 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1090 if (flags & CM_SCACHESYNC_FETCHDATA) {
1091 /* if we're bringing in a new chunk of data, make sure that
1092 * nothing is happening to that chunk, and that we aren't
1093 * changing the basic file status info, either.
1095 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1096 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1097 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1100 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1101 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1105 if (flags & CM_SCACHESYNC_STOREDATA) {
1106 /* same as fetch data */
1107 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1108 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1109 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1112 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1113 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1118 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1119 /* Don't allow concurrent StoreData RPC's */
1120 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1121 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1126 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1127 /* Don't allow more than one BKG store request */
1128 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1129 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1134 if (flags & CM_SCACHESYNC_LOCK) {
1135 /* Don't allow concurrent fiddling with lock lists */
1136 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1137 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1142 /* now the operations that don't correspond to making RPCs */
1143 if (flags & CM_SCACHESYNC_GETSTATUS) {
1144 /* we can use the status that's here, if we're not
1145 * bringing in new status.
1147 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1148 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1152 if (flags & CM_SCACHESYNC_SETSTATUS) {
1153 /* we can make a change to the local status, as long as
1154 * the status isn't changing now.
1156 * If we're fetching or storing a chunk of data, we can
1157 * change the status locally, since the fetch/store
1158 * operations don't change any of the data that we're
1161 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1162 CM_SCACHEFLAG_SIZESETTING | CM_SCACHEFLAG_SIZESTORING)) {
1163 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want SETSTATUS", scp);
1167 if (flags & CM_SCACHESYNC_READ) {
1168 /* we're going to read the data, make sure that the
1169 * status is available, and that the data is here. It
1170 * is OK to read while storing the data back.
1172 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1173 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1176 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1177 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1180 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1181 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1185 if (flags & CM_SCACHESYNC_WRITE) {
1186 /* don't write unless the status is stable and the chunk
1189 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESETTING |
1190 CM_SCACHEFLAG_SIZESTORING)) {
1191 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESETTING|SIZESTORING want WRITE", scp);
1194 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1196 CM_BUF_CMWRITING))) {
1197 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1199 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1200 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1201 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1207 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)) {
1208 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1209 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1212 lock_ReleaseMutex(&bufp->mx);
1213 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1215 lock_ReleaseWrite(&scp->rw);
1216 lock_ObtainMutex(&bufp->mx);
1217 lock_ObtainWrite(&scp->rw);
1221 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1227 /* can't check access rights without a callback */
1228 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1230 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1231 return CM_ERROR_READONLY;
1233 if (cm_HaveAccessRights(scp, userp, reqp, rights, &outRights)) {
1234 if (~outRights & rights)
1235 return CM_ERROR_NOACCESS;
1238 /* we don't know the required access rights */
1239 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1240 code = cm_GetAccessRights(scp, userp, reqp);
1242 lock_ReleaseWrite(&scp->rw);
1243 lock_ObtainMutex(&bufp->mx);
1244 lock_ObtainWrite(&scp->rw);
1252 if (flags & CM_SCACHESYNC_BULKREAD) {
1253 /* Don't allow concurrent fiddling with lock lists */
1254 if (scp->flags & CM_SCACHEFLAG_BULKREADING) {
1255 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is BULKREADING want BULKREAD", scp);
1260 /* if we get here, we're happy */
1264 /* first check if we're not supposed to wait: fail
1265 * in this case, returning with everything still locked.
1267 if (flags & CM_SCACHESYNC_NOWAIT)
1268 return CM_ERROR_WOULDBLOCK;
1270 /* These are used for minidump debugging */
1271 sleep_scp_flags = scp->flags; /* so we know why we slept */
1272 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1273 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1275 /* wait here, then try again */
1276 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1277 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1279 scp->waitRequests++;
1280 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1281 scp, scp->waitCount, scp->waitRequests);
1283 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1284 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
1285 scp->waitCount = scp->waitRequests = 1;
1288 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1292 lock_ReleaseMutex(&bufp->mx);
1293 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1295 lock_ObtainMutex(&bufp->mx);
1296 lock_ObtainWrite(&scp->rw);
1297 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1299 cm_UpdateServerPriority();
1302 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1303 scp, scp->waitCount, scp->waitRequests);
1304 if (scp->waitCount == 0) {
1305 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1306 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
1307 scp->waitRequests = 0;
1309 } /* big while loop */
1311 /* now, update the recorded state for RPC-type calls */
1312 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1313 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
1314 if (flags & CM_SCACHESYNC_STORESTATUS)
1315 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
1316 if (flags & CM_SCACHESYNC_SETSIZE)
1317 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
1318 if (flags & CM_SCACHESYNC_STORESIZE)
1319 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
1320 if (flags & CM_SCACHESYNC_GETCALLBACK)
1321 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
1322 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1323 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
1324 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1325 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
1326 if (flags & CM_SCACHESYNC_LOCK)
1327 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
1328 if (flags & CM_SCACHESYNC_BULKREAD)
1329 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_BULKREADING);
1331 /* now update the buffer pointer */
1332 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1333 /* ensure that the buffer isn't already in the I/O list */
1334 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1335 tbufp = osi_GetQData(qdp);
1336 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1339 /* queue a held reference to the buffer in the "reading" I/O list */
1340 qdp = osi_QDAlloc();
1341 osi_SetQData(qdp, bufp);
1344 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
1345 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1348 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1349 osi_assertx(scp->fileType == CM_SCACHETYPE_FILE,
1350 "attempting to store extents on a non-file object");
1352 /* ensure that the buffer isn't already in the I/O list */
1353 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1354 tbufp = osi_GetQData(qdp);
1355 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1358 /* queue a held reference to the buffer in the "writing" I/O list */
1359 qdp = osi_QDAlloc();
1360 osi_SetQData(qdp, bufp);
1362 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
1363 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1366 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1367 /* mark the buffer as being written to. */
1368 _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
1374 /* for those syncops that setup for RPCs.
1375 * Called with scache locked.
1377 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1379 osi_queueData_t *qdp;
1382 lock_AssertWrite(&scp->rw);
1384 /* now, update the recorded state for RPC-type calls */
1385 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1386 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
1387 if (flags & CM_SCACHESYNC_STORESTATUS)
1388 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
1389 if (flags & CM_SCACHESYNC_SETSIZE)
1390 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
1391 if (flags & CM_SCACHESYNC_STORESIZE)
1392 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
1393 if (flags & CM_SCACHESYNC_GETCALLBACK)
1394 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
1395 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1396 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
1397 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1398 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
1399 if (flags & CM_SCACHESYNC_LOCK)
1400 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
1401 if (flags & CM_SCACHESYNC_BULKREAD)
1402 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_BULKREADING);
1404 /* now update the buffer pointer */
1405 if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
1408 /* ensure that the buffer is in the I/O list */
1409 for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1410 tbufp = osi_GetQData(qdp);
1415 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1419 _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
1420 if (bufp->flags & CM_BUF_WAITING) {
1421 osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1422 osi_Wakeup((LONG_PTR) &bufp);
1428 /* now update the buffer pointer */
1429 if (bufp && (flags & CM_SCACHESYNC_STOREDATA)) {
1431 /* ensure that the buffer is in the I/O list */
1432 for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1433 tbufp = osi_GetQData(qdp);
1438 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1442 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
1443 if (bufp->flags & CM_BUF_WAITING) {
1444 osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1445 osi_Wakeup((LONG_PTR) &bufp);
1451 if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
1452 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1453 _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
1456 /* and wakeup anyone who is waiting */
1457 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1458 osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
1459 osi_Wakeup((LONG_PTR) &scp->flags);
1463 /* merge in a response from an RPC. The scp must be locked, and the callback
1466 * Don't overwrite any status info that is dirty, since we could have a store
1467 * operation (such as store data) that merges some info in, and we don't want
1468 * to lose the local updates. Typically, there aren't many updates we do
1469 * locally, anyway, probably only mtime.
1471 * There is probably a bug in here where a chmod (which doesn't change
1472 * serverModTime) that occurs between two fetches, both of whose responses are
1473 * handled after the callback breaking is done, but only one of whose calls
1474 * started before that, can cause old info to be merged from the first call.
1476 void cm_MergeStatus(cm_scache_t *dscp,
1477 cm_scache_t *scp, AFSFetchStatus *statusp,
1478 AFSVolSync *volsyncp,
1479 cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
1481 afs_uint64 dataVersion;
1482 struct cm_volume *volp = NULL;
1483 struct cm_cell *cellp = NULL;
1485 lock_AssertWrite(&scp->rw);
1487 // yj: i want to create some fake status for the /afs directory and the
1488 // entries under that directory
1489 #ifdef AFS_FREELANCE_CLIENT
1490 if (cm_freelanceEnabled && scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1491 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
1492 if (scp == cm_data.rootSCachep) {
1493 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1494 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1495 statusp->Length = cm_fakeDirSize;
1496 statusp->Length_hi = 0;
1498 statusp->FileType = scp->fileType;
1499 statusp->Length = scp->length.LowPart;
1500 statusp->Length_hi = scp->length.HighPart;
1502 statusp->InterfaceVersion = 0x1;
1503 statusp->LinkCount = scp->linkCount;
1504 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1505 statusp->Author = 0x1;
1506 statusp->Owner = 0x0;
1507 statusp->CallerAccess = 0x9;
1508 statusp->AnonymousAccess = 0x9;
1509 statusp->UnixModeBits = 0777;
1510 statusp->ParentVnode = 0x1;
1511 statusp->ParentUnique = 0x1;
1512 statusp->ResidencyMask = 0;
1513 statusp->ClientModTime = FakeFreelanceModTime;
1514 statusp->ServerModTime = FakeFreelanceModTime;
1516 statusp->SyncCounter = 0;
1517 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1518 statusp->lockCount = 0;
1519 statusp->errorCode = 0;
1521 #endif /* AFS_FREELANCE_CLIENT */
1523 if (statusp->errorCode != 0) {
1524 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1525 switch (statusp->errorCode) {
1530 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
1532 osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
1534 if (scp->fid.vnode & 0x1)
1535 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1537 scp->fileType = 0; /* unknown */
1539 scp->serverModTime = 0;
1540 scp->clientModTime = 0;
1541 scp->length.LowPart = 0;
1542 scp->length.HighPart = 0;
1543 scp->serverLength.LowPart = 0;
1544 scp->serverLength.HighPart = 0;
1548 scp->unixModeBits = 0;
1550 scp->dataVersion = CM_SCACHE_VERSION_BAD;
1551 scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
1552 scp->fsLockCount = 0;
1555 scp->parentVnode = dscp->fid.vnode;
1556 scp->parentUnique = dscp->fid.unique;
1558 scp->parentVnode = 0;
1559 scp->parentUnique = 0;
1563 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
1566 dataVersion = statusp->dataVersionHigh;
1568 dataVersion |= statusp->DataVersion;
1570 if (!(flags & CM_MERGEFLAG_FORCE) &&
1571 dataVersion < scp->dataVersion &&
1572 scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1574 cellp = cm_FindCellByID(scp->fid.cell, 0);
1575 if (scp->cbServerp) {
1576 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1577 reqp, CM_GETVOL_FLAG_CREATE, &volp);
1578 osi_Log2(afsd_logp, "old data from server %x volume %s",
1579 scp->cbServerp->addr.sin_addr.s_addr,
1580 volp ? volp->namep : "(unknown)");
1582 osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
1583 scp, scp->dataVersion, dataVersion);
1584 /* we have a number of data fetch/store operations running
1585 * concurrently, and we can tell which one executed last at the
1586 * server by its mtime.
1587 * Choose the one with the largest mtime, and ignore the rest.
1589 * These concurrent calls are incompatible with setting the
1590 * mtime, so we won't have a locally changed mtime here.
1592 * We could also have ACL info for a different user than usual,
1593 * in which case we have to do that part of the merge, anyway.
1594 * We won't have to worry about the info being old, since we
1595 * won't have concurrent calls
1596 * that change file status running from this machine.
1598 * Added 3/17/98: if we see data version regression on an RO
1599 * file, it's probably due to a server holding an out-of-date
1600 * replica, rather than to concurrent RPC's. Failures to
1601 * release replicas are now flagged by the volserver, but only
1602 * since AFS 3.4 5.22, so there are plenty of clients getting
1603 * out-of-date replicas out there.
1605 * If we discover an out-of-date replica, by this time it's too
1606 * late to go to another server and retry. Also, we can't
1607 * reject the merge, because then there is no way for
1608 * GetAccess to do its work, and the caller gets into an
1609 * infinite loop. So we just grin and bear it.
1611 if (!(scp->flags & CM_SCACHEFLAG_RO))
1615 if (cm_readonlyVolumeVersioning)
1616 scp->volumeCreationDate = volsyncp->spare1; /* volume creation date */
1618 scp->serverModTime = statusp->ServerModTime;
1620 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1621 scp->clientModTime = statusp->ClientModTime;
1623 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1624 scp->length.LowPart = statusp->Length;
1625 scp->length.HighPart = statusp->Length_hi;
1628 scp->serverLength.LowPart = statusp->Length;
1629 scp->serverLength.HighPart = statusp->Length_hi;
1631 scp->linkCount = statusp->LinkCount;
1632 scp->owner = statusp->Owner;
1633 scp->group = statusp->Group;
1634 scp->unixModeBits = statusp->UnixModeBits & 07777;
1636 if (statusp->FileType == File)
1637 scp->fileType = CM_SCACHETYPE_FILE;
1638 else if (statusp->FileType == Directory)
1639 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1640 else if (statusp->FileType == SymbolicLink) {
1641 if ((scp->unixModeBits & 0111) == 0)
1642 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1644 scp->fileType = CM_SCACHETYPE_SYMLINK;
1647 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
1648 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1650 /* and other stuff */
1651 scp->parentVnode = statusp->ParentVnode;
1652 scp->parentUnique = statusp->ParentUnique;
1654 /* -1 is a write lock; any positive values are read locks */
1655 scp->fsLockCount = (afs_int32)statusp->lockCount;
1657 /* and merge in the private acl cache info, if this is more than the public
1658 * info; merge in the public stuff in any case.
1660 scp->anyAccess = statusp->AnonymousAccess;
1662 if (userp != NULL) {
1663 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1666 if (scp->dataVersion != 0 &&
1667 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1668 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1670 * We now know that all of the data buffers that we have associated
1671 * with this scp are invalid. Subsequent operations will go faster
1672 * if the buffers are removed from the hash tables.
1674 * We do not remove directory buffers if the dataVersion delta is 1 because
1675 * those version numbers will be updated as part of the directory operation.
1677 * We do not remove storedata buffers because they will still be valid.
1682 cm_buf_t *bp, *prevBp, *nextBp;
1684 lock_ObtainWrite(&buf_globalLock);
1685 i = BUF_FILEHASH(&scp->fid);
1686 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1688 nextBp = bp->fileHashp;
1690 * if the buffer belongs to this stat cache entry
1691 * and the buffer mutex can be obtained, check the
1692 * reference count and if it is zero, remove the buffer
1693 * from the hash tables. If there are references,
1694 * the buffer might be updated to the current version
1695 * so leave it in place.
1697 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1698 lock_TryMutex(&bp->mx)) {
1699 if (bp->refCount == 0 &&
1700 !(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) &&
1701 !(bp->qFlags & CM_BUF_QREDIR)) {
1702 prevBp = bp->fileHashBackp;
1703 bp->fileHashBackp = bp->fileHashp = NULL;
1705 prevBp->fileHashp = nextBp;
1707 cm_data.buf_fileHashTablepp[i] = nextBp;
1709 nextBp->fileHashBackp = prevBp;
1711 j = BUF_HASH(&bp->fid, &bp->offset);
1712 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1713 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = tbp->hashp) {
1718 /* we better find it */
1719 osi_assertx(tbp != NULL, "cm_MergeStatus: buf_scacheHashTablepp table screwup");
1721 *lbpp = bp->hashp; /* hash out */
1724 _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
1726 lock_ReleaseMutex(&bp->mx);
1729 lock_ReleaseWrite(&buf_globalLock);
1733 * If the dataVersion has changed, the mountPointStringp must be cleared
1734 * in order to force a re-evaluation by cm_HandleLink(). The Windows CM
1735 * does not update a mountpoint or symlink by altering the contents of
1736 * the file data; but the Unix CM does.
1738 if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA)) {
1739 scp->mountPointStringp[0] = '\0';
1741 osi_Log5(afsd_logp, "cm_MergeStatus data version change scp 0x%p cell %u vol %u vn %u uniq %u",
1742 scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1744 osi_Log4(afsd_logp, ".... oldDV 0x%x:%x -> newDV 0x%x:%x",
1745 (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
1746 (afs_uint32)(scp->dataVersion & 0xFFFFFFFF),
1747 (afs_uint32)((dataVersion >> 32) & 0xFFFFFFFF),
1748 (afs_uint32)(dataVersion & 0xFFFFFFFF));
1751 /* We maintain a range of buffer dataVersion values which are considered
1752 * valid. This avoids the need to update the dataVersion on each buffer
1753 * object during an uncontested storeData operation. As a result this
1754 * merge status no longer has performance characteristics derived from
1755 * the size of the file.
1757 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1758 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1759 scp->bufDataVersionLow == 0)
1760 scp->bufDataVersionLow = dataVersion;
1762 if (RDR_Initialized && scp->dataVersion != CM_SCACHE_VERSION_BAD) {
1763 if ( ( !(reqp->flags & CM_REQ_SOURCE_REDIR) || !(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA))) &&
1764 scp->dataVersion != dataVersion ) {
1765 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1766 scp->fid.unique, scp->fid.hash,
1767 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
1768 } else if ( (reqp->flags & CM_REQ_SOURCE_REDIR) && (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) &&
1769 dataVersion - scp->dataVersion > 1) {
1770 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1771 scp->fid.unique, scp->fid.hash,
1772 scp->fileType, AFS_INVALIDATE_DATA_VERSION);
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 _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL | CM_SCACHEFLAG_RDR_IN_USE));
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 = fileq_to_cm_file_lock_t(q);
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);