2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
24 /*extern void afsi_log(char *pattern, ...);*/
26 extern osi_hyper_t hzero;
29 osi_queue_t *cm_allFileLocks;
30 osi_queue_t *cm_freeFileLocks;
31 unsigned long cm_lockRefreshCycle;
33 /* lock for globals */
34 osi_rwlock_t cm_scacheLock;
36 /* Dummy scache entry for use with pioctl fids */
37 cm_scache_t cm_fakeSCache;
39 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
41 #ifdef AFS_FREELANCE_CLIENT
42 extern osi_mutex_t cm_Freelance_Lock;
45 /* must be called with cm_scacheLock write-locked! */
46 void cm_AdjustScacheLRU(cm_scache_t *scp)
48 if (scp == cm_data.scacheLRULastp)
49 cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
50 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
51 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
52 if (!cm_data.scacheLRULastp)
53 cm_data.scacheLRULastp = scp;
56 /* call with scache write-locked and mutex held */
57 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
63 if (scp->flags & CM_SCACHEFLAG_INHASH) {
64 /* hash it out first */
65 i = CM_SCACHE_HASH(&scp->fid);
66 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
68 lscpp = &tscp->nextp, tscp = tscp->nextp) {
71 scp->flags &= ~CM_SCACHEFLAG_INHASH;
78 /* called with cm_scacheLock write-locked; recycles an existing scp.
80 * this function ignores all of the locking hierarchy.
82 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
84 if (scp->refCount != 0) {
88 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
89 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
91 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
96 cm_RemoveSCacheFromHashTable(scp);
99 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
100 osi_queueData_t *qdp;
103 while(qdp = scp->bufWritesp) {
104 bufp = osi_GetQData(qdp);
105 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
108 lock_ObtainMutex(&bufp->mx);
109 bufp->cmFlags &= ~CM_BUF_CMSTORING;
110 bufp->flags &= ~CM_BUF_DIRTY;
111 bufp->dirty_offset = 0;
112 bufp->dirty_length = 0;
113 bufp->flags |= CM_BUF_ERROR;
114 bufp->error = VNOVNODE;
115 bufp->dataVersion = -1; /* bad */
116 bufp->dirtyCounter++;
117 if (bufp->flags & CM_BUF_WAITING) {
118 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
119 osi_Wakeup((long) &bufp);
121 lock_ReleaseMutex(&bufp->mx);
125 while(qdp = scp->bufReadsp) {
126 bufp = osi_GetQData(qdp);
127 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
130 lock_ObtainMutex(&bufp->mx);
131 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
132 bufp->flags &= ~CM_BUF_DIRTY;
133 bufp->dirty_offset = 0;
134 bufp->dirty_length = 0;
135 bufp->flags |= CM_BUF_ERROR;
136 bufp->error = VNOVNODE;
137 bufp->dataVersion = -1; /* bad */
138 bufp->dirtyCounter++;
139 if (bufp->flags & CM_BUF_WAITING) {
140 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
141 osi_Wakeup((long) &bufp);
143 lock_ReleaseMutex(&bufp->mx);
147 buf_CleanDirtyBuffers(scp);
149 /* look for things that shouldn't still be set */
150 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
151 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
155 /* invalidate so next merge works fine;
156 * also initialize some flags */
158 scp->flags &= ~(CM_SCACHEFLAG_STATD
159 | CM_SCACHEFLAG_DELETED
161 | CM_SCACHEFLAG_PURERO
162 | CM_SCACHEFLAG_OVERQUOTA
163 | CM_SCACHEFLAG_OUTOFSPACE
164 | CM_SCACHEFLAG_EACCESS);
165 scp->serverModTime = 0;
166 scp->dataVersion = 0;
167 scp->bulkStatProgress = hzero;
169 scp->waitQueueT = NULL;
171 if (scp->cbServerp) {
172 cm_PutServer(scp->cbServerp);
173 scp->cbServerp = NULL;
182 /* remove from dnlc */
186 /* discard cached status; if non-zero, Close
187 * tried to store this to server but failed */
190 /* drop held volume ref */
192 cm_PutVolume(scp->volp);
196 /* discard symlink info */
197 scp->mountPointStringp[0] = '\0';
198 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
199 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
201 /* reset locking info */
202 scp->fileLocksH = NULL;
203 scp->fileLocksT = NULL;
204 scp->serverLock = (-1);
205 scp->exclusiveLocks = 0;
206 scp->sharedLocks = 0;
207 scp->lockDataVersion = -1;
209 /* not locked, but there can be no references to this guy
210 * while we hold the global refcount lock.
212 cm_FreeAllACLEnts(scp);
215 /* destroy directory Bplus Tree */
217 LARGE_INTEGER start, end;
218 QueryPerformanceCounter(&start);
220 freeBtree(scp->dirBplus);
221 scp->dirBplus = NULL;
222 QueryPerformanceCounter(&end);
224 bplus_free_time += (end.QuadPart - start.QuadPart);
231 /* called with cm_scacheLock write-locked; find a vnode to recycle.
232 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
234 cm_scache_t *cm_GetNewSCache(void)
240 /* first pass - look for deleted objects */
241 for ( scp = cm_data.scacheLRULastp;
243 scp = (cm_scache_t *) osi_QPrev(&scp->q))
245 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
246 "invalid cm_scache_t address");
248 if (scp->refCount == 0) {
249 if (scp->flags & CM_SCACHEFLAG_DELETED) {
250 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
251 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
253 /* we found an entry, so return it */
254 /* now remove from the LRU queue and put it back at the
255 * head of the LRU queue.
257 cm_AdjustScacheLRU(scp);
262 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
263 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
264 /* we found an entry, so return it */
265 /* now remove from the LRU queue and put it back at the
266 * head of the LRU queue.
268 cm_AdjustScacheLRU(scp);
275 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
278 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
279 /* There were no deleted scache objects that we could use. Try to find
280 * one that simply hasn't been used in a while.
282 for ( scp = cm_data.scacheLRULastp;
284 scp = (cm_scache_t *) osi_QPrev(&scp->q))
286 /* It is possible for the refCount to be zero and for there still
287 * to be outstanding dirty buffers. If there are dirty buffers,
288 * we must not recycle the scp. */
289 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
290 if (!buf_DirtyBuffersExist(&scp->fid)) {
291 if (!cm_RecycleSCache(scp, 0)) {
292 /* we found an entry, so return it */
293 /* now remove from the LRU queue and put it back at the
294 * head of the LRU queue.
296 cm_AdjustScacheLRU(scp);
302 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
306 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
311 /* if we get here, we should allocate a new scache entry. We either are below
312 * quota or we have a leak and need to allocate a new one to avoid panicing.
314 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
315 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
316 "invalid cm_scache_t address");
317 memset(scp, 0, sizeof(cm_scache_t));
318 scp->magic = CM_SCACHE_MAGIC;
319 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
320 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
322 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
324 scp->serverLock = -1;
326 /* and put it in the LRU queue */
327 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
328 if (!cm_data.scacheLRULastp)
329 cm_data.scacheLRULastp = scp;
330 cm_data.currentSCaches++;
331 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
333 scp->allNextp = cm_data.allSCachesp;
334 cm_data.allSCachesp = scp;
338 /* like strcmp, only for fids */
339 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
341 if (ap->vnode != bp->vnode)
343 if (ap->volume != bp->volume)
345 if (ap->unique != bp->unique)
347 if (ap->cell != bp->cell)
352 void cm_fakeSCacheInit(int newFile)
355 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
356 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
357 /* can leave clientModTime at 0 */
358 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
359 cm_data.fakeSCache.unixModeBits = 0777;
360 cm_data.fakeSCache.length.LowPart = 1000;
361 cm_data.fakeSCache.linkCount = 1;
362 cm_data.fakeSCache.refCount = 1;
364 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
368 cm_ValidateSCache(void)
370 cm_scache_t * scp, *lscp;
373 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
374 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
375 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
376 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
380 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
382 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
383 if (scp->magic != CM_SCACHE_MAGIC) {
384 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
385 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
388 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
389 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
390 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
393 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
394 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
395 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
398 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
399 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
400 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
403 if (i > cm_data.currentSCaches ) {
404 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
405 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
408 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
409 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
410 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
415 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
416 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
417 if (scp->magic != CM_SCACHE_MAGIC) {
418 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
419 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
422 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
423 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
424 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
427 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
428 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
429 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
432 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
433 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
434 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_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 ) {
451 if (scp->magic != CM_SCACHE_MAGIC) {
452 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
453 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
456 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
457 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
458 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
461 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
462 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
463 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
466 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
467 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
468 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
474 return cm_dnlcValidate();
478 cm_SuspendSCache(void)
483 cm_GiveUpAllCallbacksAllServers(TRUE);
486 * After this call all servers are marked down.
487 * Do not clear the callbacks, instead change the
488 * expiration time so that the callbacks will be expired
489 * when the servers are marked back up. However, we
490 * want the callbacks to be preserved as long as the
491 * servers are down. That way if the machine resumes
492 * without network, the stat cache item will still be
497 lock_ObtainWrite(&cm_scacheLock);
498 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
499 if (scp->cbServerp) {
500 if (scp->flags & CM_SCACHEFLAG_PURERO && scp->volp) {
501 if (scp->volp->cbExpiresRO == scp->cbExpires) {
502 scp->volp->cbExpiresRO = now+1;
505 scp->cbExpires = now+1;
508 lock_ReleaseWrite(&cm_scacheLock);
512 cm_ShutdownSCache(void)
516 lock_ObtainWrite(&cm_scacheLock);
518 for ( scp = cm_data.allSCachesp; scp;
519 scp = scp->allNextp ) {
520 if (scp->randomACLp) {
521 lock_ObtainMutex(&scp->mx);
522 cm_FreeAllACLEnts(scp);
523 lock_ReleaseMutex(&scp->mx);
526 if (scp->cbServerp) {
527 cm_PutServer(scp->cbServerp);
528 scp->cbServerp = NULL;
531 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
535 freeBtree(scp->dirBplus);
536 scp->dirBplus = NULL;
537 scp->dirDataVersion = -1;
538 lock_FinalizeRWLock(&scp->dirlock);
540 lock_FinalizeMutex(&scp->mx);
541 lock_FinalizeRWLock(&scp->bufCreateLock);
543 lock_ReleaseWrite(&cm_scacheLock);
545 cm_GiveUpAllCallbacksAllServers(FALSE);
547 return cm_dnlcShutdown();
550 void cm_InitSCache(int newFile, long maxSCaches)
552 static osi_once_t once;
554 if (osi_Once(&once)) {
555 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
557 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
558 cm_data.allSCachesp = NULL;
559 cm_data.currentSCaches = 0;
560 cm_data.maxSCaches = maxSCaches;
561 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
565 for ( scp = cm_data.allSCachesp; scp;
566 scp = scp->allNextp ) {
567 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
568 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
570 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
572 scp->cbServerp = NULL;
574 scp->fileLocksH = NULL;
575 scp->fileLocksT = NULL;
576 scp->serverLock = (-1);
577 scp->lastRefreshCycle = 0;
578 scp->exclusiveLocks = 0;
579 scp->sharedLocks = 0;
586 scp->dirBplus = NULL;
587 scp->dirDataVersion = -1;
589 scp->waitQueueT = NULL;
590 scp->flags &= ~CM_SCACHEFLAG_WAITING;
593 cm_allFileLocks = NULL;
594 cm_freeFileLocks = NULL;
595 cm_lockRefreshCycle = 0;
596 cm_fakeSCacheInit(newFile);
597 cm_allFreeWaiters = NULL;
598 cm_dnlcInit(newFile);
603 /* version that doesn't bother creating the entry if we don't find it */
604 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
609 hash = CM_SCACHE_HASH(fidp);
611 if (fidp->cell == 0) {
615 lock_ObtainWrite(&cm_scacheLock);
616 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
617 if (cm_FidCmp(fidp, &scp->fid) == 0) {
618 cm_HoldSCacheNoLock(scp);
619 cm_AdjustScacheLRU(scp);
620 lock_ReleaseWrite(&cm_scacheLock);
624 lock_ReleaseWrite(&cm_scacheLock);
628 #ifdef DEBUG_REFCOUNT
629 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
630 cm_req_t *reqp, char * file, long line)
632 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
639 cm_volume_t *volp = NULL;
642 int special; // yj: boolean variable to test if file is on root.afs
644 extern cm_fid_t cm_rootFid;
646 hash = CM_SCACHE_HASH(fidp);
648 osi_assertx(fidp->cell != 0, "unassigned cell value");
650 if (fidp->cell== cm_data.rootFid.cell &&
651 fidp->volume==cm_data.rootFid.volume &&
652 fidp->vnode==0x0 && fidp->unique==0x0)
654 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
657 // yj: check if we have the scp, if so, we don't need
658 // to do anything else
659 lock_ObtainWrite(&cm_scacheLock);
660 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
661 if (cm_FidCmp(fidp, &scp->fid) == 0) {
662 #ifdef DEBUG_REFCOUNT
663 afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
664 osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
666 cm_HoldSCacheNoLock(scp);
668 cm_AdjustScacheLRU(scp);
669 lock_ReleaseWrite(&cm_scacheLock);
674 // yj: when we get here, it means we don't have an scp
675 // so we need to either load it or fake it, depending
676 // on whether the file is "special", see below.
678 // yj: if we're trying to get an scp for a file that's
679 // on root.afs of homecell, we want to handle it specially
680 // because we have to fill in the status stuff 'coz we
681 // don't want trybulkstat to fill it in for us
682 #ifdef AFS_FREELANCE_CLIENT
683 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
684 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
685 !(fidp->vnode==0x1 && fidp->unique==0x1));
686 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
687 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
688 fidp->vnode==0x1 && fidp->unique==0x1);
689 if (cm_freelanceEnabled && isRoot) {
690 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
691 /* freelance: if we are trying to get the root scp for the first
692 * time, we will just put in a place holder entry.
697 if (cm_freelanceEnabled && special) {
698 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
699 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
700 lock_ObtainMutex(&cm_Freelance_Lock);
701 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
702 lock_ReleaseMutex(&cm_Freelance_Lock);
706 scp = cm_GetNewSCache();
708 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
709 lock_ReleaseWrite(&cm_scacheLock);
710 return CM_ERROR_WOULDBLOCK;
713 #if not_too_dangerous
714 /* dropping the cm_scacheLock allows more than one thread
715 * to obtain the same cm_scache_t from the LRU list. Since
716 * the refCount is known to be zero at this point we have to
717 * assume that no one else is using the one this is returned.
719 lock_ReleaseWrite(&cm_scacheLock);
720 lock_ObtainMutex(&scp->mx);
721 lock_ObtainWrite(&cm_scacheLock);
724 scp->volp = cm_data.rootSCachep->volp;
725 cm_GetVolume(scp->volp); /* grab an additional reference */
726 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
727 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
728 scp->dotdotFid.unique=1;
729 scp->dotdotFid.vnode=1;
730 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
731 scp->nextp=cm_data.scacheHashTablep[hash];
732 cm_data.scacheHashTablep[hash]=scp;
733 scp->flags |= CM_SCACHEFLAG_INHASH;
735 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
736 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
737 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
739 scp->fileType = CM_SCACHETYPE_INVALID;
741 lock_ObtainMutex(&cm_Freelance_Lock);
742 scp->length.LowPart = (DWORD)strlen(mp)+4;
743 scp->length.HighPart = 0;
744 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
745 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
746 lock_ReleaseMutex(&cm_Freelance_Lock);
749 scp->unixModeBits=0777;
750 scp->clientModTime=FakeFreelanceModTime;
751 scp->serverModTime=FakeFreelanceModTime;
752 scp->parentUnique = 0x1;
753 scp->parentVnode=0x1;
755 scp->dataVersion=cm_data.fakeDirVersion;
756 scp->lockDataVersion=-1; /* no lock yet */
757 #if not_too_dangerous
758 lock_ReleaseMutex(&scp->mx);
761 lock_ReleaseWrite(&cm_scacheLock);
762 #ifdef DEBUG_REFCOUNT
763 afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
764 osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
769 #endif /* AFS_FREELANCE_CLIENT */
771 /* otherwise, we need to find the volume */
772 if (!cm_freelanceEnabled || !isRoot) {
773 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
774 cellp = cm_FindCellByID(fidp->cell);
776 return CM_ERROR_NOSUCHCELL;
778 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
781 lock_ObtainWrite(&cm_scacheLock);
784 /* otherwise, we have the volume, now reverify that the scp doesn't
785 * exist, and proceed.
787 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
788 if (cm_FidCmp(fidp, &scp->fid) == 0) {
789 #ifdef DEBUG_REFCOUNT
790 afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
791 osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
793 cm_HoldSCacheNoLock(scp);
794 osi_assertx(scp->volp == volp, "cm_scache_t volume has unexpected value");
795 cm_AdjustScacheLRU(scp);
796 lock_ReleaseWrite(&cm_scacheLock);
804 /* now, if we don't have the fid, recycle something */
805 scp = cm_GetNewSCache();
807 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
808 lock_ReleaseWrite(&cm_scacheLock);
811 return CM_ERROR_WOULDBLOCK;
813 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
815 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
817 #if not_too_dangerous
818 /* dropping the cm_scacheLock allows more than one thread
819 * to obtain the same cm_scache_t from the LRU list. Since
820 * the refCount is known to be zero at this point we have to
821 * assume that no one else is using the one this is returned.
823 lock_ReleaseWrite(&cm_scacheLock);
824 lock_ObtainMutex(&scp->mx);
825 lock_ObtainWrite(&cm_scacheLock);
828 scp->volp = volp; /* a held reference */
830 if (!cm_freelanceEnabled || !isRoot) {
831 /* if this scache entry represents a volume root then we need
832 * to copy the dotdotFipd from the volume structure where the
833 * "master" copy is stored (defect 11489)
835 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
836 scp->dotdotFid = volp->dotdotFid;
839 if (volp->ro.ID == fidp->volume)
840 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
841 else if (volp->bk.ID == fidp->volume)
842 scp->flags |= CM_SCACHEFLAG_RO;
844 scp->nextp = cm_data.scacheHashTablep[hash];
845 cm_data.scacheHashTablep[hash] = scp;
846 scp->flags |= CM_SCACHEFLAG_INHASH;
848 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
849 #if not_too_dangerous
850 lock_ReleaseMutex(&scp->mx);
853 /* XXX - The following fields in the cm_scache are
859 lock_ReleaseWrite(&cm_scacheLock);
861 /* now we have a held scache entry; just return it */
863 #ifdef DEBUG_REFCOUNT
864 afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
865 osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
870 /* Returns a held reference to the scache's parent
872 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
877 cm_scache_t * pscp = NULL;
879 lock_ObtainRead(&cm_scacheLock);
880 parent_fid = scp->fid;
881 parent_fid.vnode = scp->parentVnode;
882 parent_fid.unique = scp->parentUnique;
884 if (cm_FidCmp(&scp->fid, &parent_fid)) {
885 i = CM_SCACHE_HASH(&parent_fid);
886 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
887 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
888 cm_HoldSCacheNoLock(pscp);
894 lock_ReleaseRead(&cm_scacheLock);
899 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
901 cm_scache_waiter_t * w;
903 lock_ObtainWrite(&cm_scacheLock);
904 if (cm_allFreeWaiters == NULL) {
905 w = malloc(sizeof(*w));
906 memset(w, 0, sizeof(*w));
908 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
909 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
912 w->threadId = thrd_Current();
914 cm_HoldSCacheNoLock(scp);
918 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
919 lock_ReleaseWrite(&cm_scacheLock);
921 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
924 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
926 cm_scache_waiter_t * w;
929 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
931 lock_ObtainRead(&cm_scacheLock);
932 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
934 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
935 if (w->flags == flags && w->bufp == bufp) {
940 osi_assertx(w != NULL, "null cm_scache_waiter_t");
941 this_is_me = (w->threadId == thrd_Current());
942 lock_ReleaseRead(&cm_scacheLock);
945 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
949 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
951 lock_ObtainWrite(&cm_scacheLock);
952 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
953 cm_ReleaseSCacheNoLock(scp);
954 memset(w, 0, sizeof(*w));
955 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
956 lock_ReleaseWrite(&cm_scacheLock);
962 /* synchronize a fetch, store, read, write, fetch status or store status.
963 * Called with scache mutex held, and returns with it held, but temporarily
964 * drops it during the fetch.
966 * At most one flag can be on in flags, if this is an RPC request.
968 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
970 * There are a lot of weird restrictions here; here's an attempt to explain the
971 * rationale for the concurrency restrictions implemented in this function.
973 * First, although the file server will break callbacks when *another* machine
974 * modifies a file or status block, the client itself is responsible for
975 * concurrency control on its own requests. Callback breaking events are rare,
976 * and simply invalidate any concurrent new status info.
978 * In the absence of callback breaking messages, we need to know how to
979 * synchronize incoming responses describing updates to files. We synchronize
980 * operations that update the data version by comparing the data versions.
981 * However, updates that do not update the data, but only the status, can't be
982 * synchronized with fetches or stores, since there's nothing to compare
983 * to tell which operation executed first at the server.
985 * Thus, we can allow multiple ops that change file data, or dir data, and
986 * fetches. However, status storing ops have to be done serially.
988 * Furthermore, certain data-changing ops are incompatible: we can't read or
989 * write a buffer while doing a truncate. We can't read and write the same
990 * buffer at the same time, or write while fetching or storing, or read while
991 * fetching a buffer (this may change). We can't fetch and store at the same
994 * With respect to status, we can't read and write at the same time, read while
995 * fetching, write while fetching or storing, or fetch and store at the same time.
997 * We can't allow a get callback RPC to run in concurrently with something that
998 * will return updated status, since we could start a call, have the server
999 * return status, have another machine make an update to the status (which
1000 * doesn't change serverModTime), have the original machine get a new callback,
1001 * and then have the original machine merge in the early, old info from the
1002 * first call. At this point, the easiest way to avoid this problem is to have
1003 * getcallback calls conflict with all others for the same vnode. Other calls
1004 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1005 * vnode must be careful not to merge in their status unless they have obtained
1006 * a callback from the start of their call.
1008 * Note added 1/23/96
1009 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1010 * Each such RPC passes a FileLength parameter, which the server uses to do
1011 * pre-truncation if necessary. So if two RPC's are processed out of order at
1012 * the server, the one with the smaller FileLength will be processed last,
1013 * possibly resulting in a bogus truncation. The simplest way to avoid this
1014 * is to serialize all StoreData RPC's. This is the reason we defined
1015 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1017 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1018 afs_uint32 rights, afs_uint32 flags)
1020 osi_queueData_t *qdp;
1023 afs_uint32 outRights;
1025 afs_uint32 sleep_scp_flags = 0;
1026 afs_uint32 sleep_buf_cmflags = 0;
1027 afs_uint32 sleep_scp_bufs = 0;
1030 /* lookup this first */
1031 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1034 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1037 /* Do the access check. Now we don't really do the access check
1038 * atomically, since the caller doesn't expect the parent dir to be
1039 * returned locked, and that is what we'd have to do to prevent a
1040 * callback breaking message on the parent due to a setacl call from
1041 * being processed while we're running. So, instead, we check things
1042 * here, and if things look fine with the access, we proceed to finish
1043 * the rest of this check. Sort of a hack, but probably good enough.
1047 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1048 /* if we're bringing in a new status block, ensure that
1049 * we aren't already doing so, and that no one is
1050 * changing the status concurrently, either. We need
1051 * to do this, even if the status is of a different
1052 * type, since we don't have the ability to figure out,
1053 * in the AFS 3 protocols, which status-changing
1054 * operation ran first, or even which order a read and
1055 * a write occurred in.
1057 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1058 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1059 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1063 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1064 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1065 /* if we're going to make an RPC to change the status, make sure
1066 * that no one is bringing in or sending out the status.
1068 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1069 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1070 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1073 if (scp->bufReadsp || scp->bufWritesp) {
1074 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1078 if (flags & CM_SCACHESYNC_FETCHDATA) {
1079 /* if we're bringing in a new chunk of data, make sure that
1080 * nothing is happening to that chunk, and that we aren't
1081 * changing the basic file status info, either.
1083 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1084 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1085 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1088 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1089 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1093 if (flags & CM_SCACHESYNC_STOREDATA) {
1094 /* same as fetch data */
1095 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1096 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1097 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", 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 STOREDATA", scp, bufp);
1106 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1107 /* Don't allow concurrent StoreData RPC's */
1108 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1109 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1114 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1115 /* Don't allow more than one BKG store request */
1116 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1117 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1122 if (flags & CM_SCACHESYNC_LOCK) {
1123 /* Don't allow concurrent fiddling with lock lists */
1124 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1125 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1130 /* now the operations that don't correspond to making RPCs */
1131 if (flags & CM_SCACHESYNC_GETSTATUS) {
1132 /* we can use the status that's here, if we're not
1133 * bringing in new status.
1135 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1136 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1140 if (flags & CM_SCACHESYNC_SETSTATUS) {
1141 /* we can make a change to the local status, as long as
1142 * the status isn't changing now.
1144 * If we're fetching or storing a chunk of data, we can
1145 * change the status locally, since the fetch/store
1146 * operations don't change any of the data that we're
1149 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1150 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1154 if (flags & CM_SCACHESYNC_READ) {
1155 /* we're going to read the data, make sure that the
1156 * status is available, and that the data is here. It
1157 * is OK to read while storing the data back.
1159 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1160 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1163 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1164 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1167 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1168 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1172 if (flags & CM_SCACHESYNC_WRITE) {
1173 /* don't write unless the status is stable and the chunk
1176 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1177 | CM_SCACHEFLAG_SIZESTORING)) {
1178 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1181 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1183 CM_BUF_CMWRITING))) {
1184 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1186 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1187 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1188 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1194 // yj: modified this so that callback only checked if we're
1195 // not checking something on /afs
1196 /* fix the conditional to match the one in cm_HaveCallback */
1197 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1198 #ifdef AFS_FREELANCE_CLIENT
1199 && (!cm_freelanceEnabled ||
1200 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1201 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1202 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1203 cm_fakeDirCallback < 2)
1204 #endif /* AFS_FREELANCE_CLIENT */
1206 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1207 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1210 lock_ReleaseMutex(&bufp->mx);
1211 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1213 lock_ReleaseMutex(&scp->mx);
1214 lock_ObtainMutex(&bufp->mx);
1215 lock_ObtainMutex(&scp->mx);
1219 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1225 /* can't check access rights without a callback */
1226 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1228 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1229 return CM_ERROR_READONLY;
1231 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1232 if (~outRights & rights)
1233 return CM_ERROR_NOACCESS;
1236 /* we don't know the required access rights */
1237 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1238 code = cm_GetAccessRights(scp, userp, reqp);
1240 lock_ReleaseMutex(&scp->mx);
1241 lock_ObtainMutex(&bufp->mx);
1242 lock_ObtainMutex(&scp->mx);
1250 /* if we get here, we're happy */
1254 /* first check if we're not supposed to wait: fail
1255 * in this case, returning with everything still locked.
1257 if (flags & CM_SCACHESYNC_NOWAIT)
1258 return CM_ERROR_WOULDBLOCK;
1260 /* These are used for minidump debugging */
1261 sleep_scp_flags = scp->flags; /* so we know why we slept */
1262 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1263 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1265 /* wait here, then try again */
1266 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1267 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1269 scp->waitRequests++;
1270 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1271 scp, scp->waitCount, scp->waitRequests);
1273 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1274 scp->flags |= CM_SCACHEFLAG_WAITING;
1275 scp->waitCount = scp->waitRequests = 1;
1279 lock_ReleaseMutex(&bufp->mx);
1281 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1284 if (wakeupCycle++ != 0)
1285 lock_ObtainMutex(&scp->mx);
1286 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1287 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1289 smb_UpdateServerPriority();
1292 lock_ObtainMutex(&bufp->mx);
1293 lock_ObtainMutex(&scp->mx);
1295 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1296 scp, scp->waitCount, scp->waitRequests);
1297 if (scp->waitCount == 0) {
1298 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1299 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1300 scp->waitRequests = 0;
1302 } /* big while loop */
1304 /* now, update the recorded state for RPC-type calls */
1305 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1306 scp->flags |= CM_SCACHEFLAG_FETCHING;
1307 if (flags & CM_SCACHESYNC_STORESTATUS)
1308 scp->flags |= CM_SCACHEFLAG_STORING;
1309 if (flags & CM_SCACHESYNC_STORESIZE)
1310 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1311 if (flags & CM_SCACHESYNC_GETCALLBACK)
1312 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1313 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1314 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1315 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1316 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1317 if (flags & CM_SCACHESYNC_LOCK)
1318 scp->flags |= CM_SCACHEFLAG_LOCKING;
1320 /* now update the buffer pointer */
1321 if (flags & CM_SCACHESYNC_FETCHDATA) {
1322 /* ensure that the buffer isn't already in the I/O list */
1324 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1325 tbufp = osi_GetQData(qdp);
1326 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1330 /* queue a held reference to the buffer in the "reading" I/O list */
1331 qdp = osi_QDAlloc();
1332 osi_SetQData(qdp, bufp);
1335 bufp->cmFlags |= CM_BUF_CMFETCHING;
1337 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1340 if (flags & CM_SCACHESYNC_STOREDATA) {
1341 /* ensure that the buffer isn't already in the I/O list */
1343 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1344 tbufp = osi_GetQData(qdp);
1345 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1349 /* queue a held reference to the buffer in the "writing" I/O list */
1350 qdp = osi_QDAlloc();
1351 osi_SetQData(qdp, bufp);
1354 bufp->cmFlags |= CM_BUF_CMSTORING;
1356 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1359 if (flags & CM_SCACHESYNC_WRITE) {
1360 /* mark the buffer as being written to. */
1362 bufp->cmFlags |= CM_BUF_CMWRITING;
1369 /* for those syncops that setup for RPCs.
1370 * Called with scache locked.
1372 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1374 osi_queueData_t *qdp;
1377 lock_AssertMutex(&scp->mx);
1379 /* now, update the recorded state for RPC-type calls */
1380 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1381 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1382 if (flags & CM_SCACHESYNC_STORESTATUS)
1383 scp->flags &= ~CM_SCACHEFLAG_STORING;
1384 if (flags & CM_SCACHESYNC_STORESIZE)
1385 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1386 if (flags & CM_SCACHESYNC_GETCALLBACK)
1387 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1388 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1389 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1390 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1391 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1392 if (flags & CM_SCACHESYNC_LOCK)
1393 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1395 /* now update the buffer pointer */
1396 if (flags & CM_SCACHESYNC_FETCHDATA) {
1399 /* ensure that the buffer isn't already in the I/O list */
1400 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1401 tbufp = osi_GetQData(qdp);
1406 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1411 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1412 if (bufp->flags & CM_BUF_WAITING) {
1413 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1414 osi_Wakeup((LONG_PTR) &bufp);
1421 /* now update the buffer pointer */
1422 if (flags & CM_SCACHESYNC_STOREDATA) {
1424 /* ensure that the buffer isn't already in the I/O list */
1425 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1426 tbufp = osi_GetQData(qdp);
1431 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1436 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1437 if (bufp->flags & CM_BUF_WAITING) {
1438 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1439 osi_Wakeup((LONG_PTR) &bufp);
1446 if (flags & CM_SCACHESYNC_WRITE) {
1448 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1450 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1454 /* and wakeup anyone who is waiting */
1455 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1456 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1457 osi_Wakeup((LONG_PTR) &scp->flags);
1461 /* merge in a response from an RPC. The scp must be locked, and the callback
1464 * Don't overwrite any status info that is dirty, since we could have a store
1465 * operation (such as store data) that merges some info in, and we don't want
1466 * to lose the local updates. Typically, there aren't many updates we do
1467 * locally, anyway, probably only mtime.
1469 * There is probably a bug in here where a chmod (which doesn't change
1470 * serverModTime) that occurs between two fetches, both of whose responses are
1471 * handled after the callback breaking is done, but only one of whose calls
1472 * started before that, can cause old info to be merged from the first call.
1474 void cm_MergeStatus(cm_scache_t *dscp,
1475 cm_scache_t *scp, AFSFetchStatus *statusp,
1476 AFSVolSync *volsyncp,
1477 cm_user_t *userp, afs_uint32 flags)
1479 afs_uint64 dataVersion;
1481 // yj: i want to create some fake status for the /afs directory and the
1482 // entries under that directory
1483 #ifdef AFS_FREELANCE_CLIENT
1484 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1485 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1486 statusp->InterfaceVersion = 0x1;
1487 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1488 statusp->LinkCount = scp->linkCount;
1489 statusp->Length = cm_fakeDirSize;
1490 statusp->Length_hi = 0;
1491 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1492 statusp->Author = 0x1;
1493 statusp->Owner = 0x0;
1494 statusp->CallerAccess = 0x9;
1495 statusp->AnonymousAccess = 0x9;
1496 statusp->UnixModeBits = 0777;
1497 statusp->ParentVnode = 0x1;
1498 statusp->ParentUnique = 0x1;
1499 statusp->ResidencyMask = 0;
1500 statusp->ClientModTime = FakeFreelanceModTime;
1501 statusp->ServerModTime = FakeFreelanceModTime;
1503 statusp->SyncCounter = 0;
1504 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1505 statusp->errorCode = 0;
1507 #endif /* AFS_FREELANCE_CLIENT */
1509 if (statusp->errorCode != 0) {
1510 scp->flags |= CM_SCACHEFLAG_EACCESS;
1511 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1513 scp->fileType = 0; /* unknown */
1515 scp->serverModTime = 0;
1516 scp->clientModTime = 0;
1517 scp->length.LowPart = 0;
1518 scp->length.HighPart = 0;
1519 scp->serverLength.LowPart = 0;
1520 scp->serverLength.HighPart = 0;
1524 scp->unixModeBits = 0;
1526 scp->dataVersion = 0;
1529 scp->parentVnode = dscp->fid.vnode;
1530 scp->parentUnique = dscp->fid.unique;
1532 scp->parentVnode = 0;
1533 scp->parentUnique = 0;
1537 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1540 dataVersion = statusp->dataVersionHigh;
1542 dataVersion |= statusp->DataVersion;
1544 if (!(flags & CM_MERGEFLAG_FORCE) && dataVersion < scp->dataVersion) {
1545 struct cm_cell *cellp;
1547 cellp = cm_FindCellByID(scp->fid.cell);
1548 if (scp->cbServerp) {
1549 struct cm_volume *volp = NULL;
1551 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1552 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1553 osi_Log2(afsd_logp, "old data from server %x volume %s",
1554 scp->cbServerp->addr.sin_addr.s_addr,
1555 volp ? volp->namep : "(unknown)");
1559 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %I64d, RPC dv %I64d",
1560 scp, scp->dataVersion, dataVersion);
1561 /* we have a number of data fetch/store operations running
1562 * concurrently, and we can tell which one executed last at the
1563 * server by its mtime.
1564 * Choose the one with the largest mtime, and ignore the rest.
1566 * These concurrent calls are incompatible with setting the
1567 * mtime, so we won't have a locally changed mtime here.
1569 * We could also have ACL info for a different user than usual,
1570 * in which case we have to do that part of the merge, anyway.
1571 * We won't have to worry about the info being old, since we
1572 * won't have concurrent calls
1573 * that change file status running from this machine.
1575 * Added 3/17/98: if we see data version regression on an RO
1576 * file, it's probably due to a server holding an out-of-date
1577 * replica, rather than to concurrent RPC's. Failures to
1578 * release replicas are now flagged by the volserver, but only
1579 * since AFS 3.4 5.22, so there are plenty of clients getting
1580 * out-of-date replicas out there.
1582 * If we discover an out-of-date replica, by this time it's too
1583 * late to go to another server and retry. Also, we can't
1584 * reject the merge, because then there is no way for
1585 * GetAccess to do its work, and the caller gets into an
1586 * infinite loop. So we just grin and bear it.
1588 if (!(scp->flags & CM_SCACHEFLAG_RO))
1592 scp->serverModTime = statusp->ServerModTime;
1594 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1595 scp->clientModTime = statusp->ClientModTime;
1597 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1598 scp->length.LowPart = statusp->Length;
1599 scp->length.HighPart = statusp->Length_hi;
1602 scp->serverLength.LowPart = statusp->Length;
1603 scp->serverLength.HighPart = statusp->Length_hi;
1605 scp->linkCount = statusp->LinkCount;
1606 scp->owner = statusp->Owner;
1607 scp->group = statusp->Group;
1608 scp->unixModeBits = statusp->UnixModeBits & 07777;
1610 if (statusp->FileType == File)
1611 scp->fileType = CM_SCACHETYPE_FILE;
1612 else if (statusp->FileType == Directory)
1613 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1614 else if (statusp->FileType == SymbolicLink) {
1615 if ((scp->unixModeBits & 0111) == 0)
1616 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1618 scp->fileType = CM_SCACHETYPE_SYMLINK;
1621 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1622 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1624 /* and other stuff */
1625 scp->parentVnode = statusp->ParentVnode;
1626 scp->parentUnique = statusp->ParentUnique;
1628 /* and merge in the private acl cache info, if this is more than the public
1629 * info; merge in the public stuff in any case.
1631 scp->anyAccess = statusp->AnonymousAccess;
1633 if (userp != NULL) {
1634 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1637 if ((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion == 1) {
1640 for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1642 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1643 bp->dataVersion == scp->dataVersion)
1644 bp->dataVersion = dataVersion;
1648 scp->dataVersion = dataVersion;
1651 /* note that our stat cache info is incorrect, so force us eventually
1652 * to stat the file again. There may be dirty data associated with
1653 * this vnode, and we want to preserve that information.
1655 * This function works by simply simulating a loss of the callback.
1657 * This function must be called with the scache locked.
1659 void cm_DiscardSCache(cm_scache_t *scp)
1661 lock_AssertMutex(&scp->mx);
1662 if (scp->cbServerp) {
1663 cm_PutServer(scp->cbServerp);
1664 scp->cbServerp = NULL;
1667 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1668 cm_dnlcPurgedp(scp);
1669 cm_dnlcPurgevp(scp);
1670 cm_FreeAllACLEnts(scp);
1672 /* Force mount points and symlinks to be re-evaluated */
1673 scp->mountPointStringp[0] = '\0';
1676 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1678 afsFidp->Volume = fidp->volume;
1679 afsFidp->Vnode = fidp->vnode;
1680 afsFidp->Unique = fidp->unique;
1683 #ifdef DEBUG_REFCOUNT
1684 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1686 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1689 osi_assertx(scp != NULL, "null cm_scache_t");
1691 #ifdef DEBUG_REFCOUNT
1692 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1693 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1697 #ifdef DEBUG_REFCOUNT
1698 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1700 void cm_HoldSCache(cm_scache_t *scp)
1703 osi_assertx(scp != NULL, "null cm_scache_t");
1704 lock_ObtainWrite(&cm_scacheLock);
1706 #ifdef DEBUG_REFCOUNT
1707 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1708 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1710 lock_ReleaseWrite(&cm_scacheLock);
1713 #ifdef DEBUG_REFCOUNT
1714 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1716 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1719 osi_assertx(scp != NULL, "null cm_scache_t");
1720 if (scp->refCount == 0)
1721 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1722 osi_assertx(scp->refCount-- >= 0, "cm_scache_t refCount 0");
1723 #ifdef DEBUG_REFCOUNT
1724 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1725 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1729 #ifdef DEBUG_REFCOUNT
1730 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1732 void cm_ReleaseSCache(cm_scache_t *scp)
1735 osi_assertx(scp != NULL, "null cm_scache_t");
1736 lock_ObtainWrite(&cm_scacheLock);
1737 if (scp->refCount == 0)
1738 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1739 osi_assertx(scp->refCount != 0, "cm_scache_t refCount 0");
1741 #ifdef DEBUG_REFCOUNT
1742 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1743 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1745 lock_ReleaseWrite(&cm_scacheLock);
1748 /* just look for the scp entry to get filetype */
1749 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1750 int cm_FindFileType(cm_fid_t *fidp)
1755 hash = CM_SCACHE_HASH(fidp);
1757 osi_assertx(fidp->cell != 0, "unassigned cell value");
1759 lock_ObtainWrite(&cm_scacheLock);
1760 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1761 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1762 lock_ReleaseWrite(&cm_scacheLock);
1763 return scp->fileType;
1766 lock_ReleaseWrite(&cm_scacheLock);
1770 /* dump all scp's that have reference count > 0 to a file.
1771 * cookie is used to identify this batch for easy parsing,
1772 * and it a string provided by a caller
1774 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1782 lock_ObtainRead(&cm_scacheLock);
1784 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1785 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1787 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1789 sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) volp=0x%p type=%d dv=%d len=0x%I64x mp='%s' flags=0x%x cb=0x%x refCount=%u\r\n",
1790 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1791 scp->volp, scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp, scp->flags,
1792 (unsigned long)scp->cbExpires, scp->refCount);
1793 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1796 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1797 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1798 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1799 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1801 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1803 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1805 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1806 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1807 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1811 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1812 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1815 lock_ReleaseRead(&cm_scacheLock);