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>
23 /*extern void afsi_log(char *pattern, ...);*/
25 extern osi_hyper_t hzero;
28 osi_queue_t *cm_allFileLocks;
29 osi_queue_t *cm_freeFileLocks;
30 unsigned long cm_lockRefreshCycle;
32 /* lock for globals */
33 osi_rwlock_t cm_scacheLock;
35 /* Dummy scache entry for use with pioctl fids */
36 cm_scache_t cm_fakeSCache;
38 #ifdef AFS_FREELANCE_CLIENT
39 extern osi_mutex_t cm_Freelance_Lock;
42 /* must be called with cm_scacheLock write-locked! */
43 void cm_AdjustScacheLRU(cm_scache_t *scp)
45 if (scp == cm_data.scacheLRULastp)
46 cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
47 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
48 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
49 if (!cm_data.scacheLRULastp)
50 cm_data.scacheLRULastp = scp;
53 /* call with scache write-locked and mutex held */
54 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
60 if (scp->flags & CM_SCACHEFLAG_INHASH) {
61 /* hash it out first */
62 i = CM_SCACHE_HASH(&scp->fid);
63 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
65 lscpp = &tscp->nextp, tscp = tscp->nextp) {
68 scp->flags &= ~CM_SCACHEFLAG_INHASH;
75 /* called with cm_scacheLock write-locked; recycles an existing scp.
77 * this function ignores all of the locking hierarchy.
79 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
81 if (scp->refCount != 0) {
85 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
86 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
88 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
93 cm_RemoveSCacheFromHashTable(scp);
96 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
100 while(qdp = scp->bufWritesp) {
101 bufp = osi_GetQData(qdp);
102 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
105 lock_ObtainMutex(&bufp->mx);
106 bufp->cmFlags &= ~CM_BUF_CMSTORING;
107 bufp->flags &= ~CM_BUF_DIRTY;
108 bufp->flags |= CM_BUF_ERROR;
109 bufp->error = VNOVNODE;
110 bufp->dataVersion = -1; /* bad */
111 bufp->dirtyCounter++;
112 if (bufp->flags & CM_BUF_WAITING) {
113 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
114 osi_Wakeup((long) &bufp);
116 lock_ReleaseMutex(&bufp->mx);
120 while(qdp = scp->bufReadsp) {
121 bufp = osi_GetQData(qdp);
122 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
125 lock_ObtainMutex(&bufp->mx);
126 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
127 bufp->flags &= ~CM_BUF_DIRTY;
128 bufp->flags |= CM_BUF_ERROR;
129 bufp->error = VNOVNODE;
130 bufp->dataVersion = -1; /* bad */
131 bufp->dirtyCounter++;
132 if (bufp->flags & CM_BUF_WAITING) {
133 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
134 osi_Wakeup((long) &bufp);
136 lock_ReleaseMutex(&bufp->mx);
140 buf_CleanDirtyBuffers(scp);
142 /* look for things that shouldn't still be set */
143 osi_assert(scp->bufWritesp == NULL);
144 osi_assert(scp->bufReadsp == NULL);
148 /* invalidate so next merge works fine;
149 * also initialize some flags */
151 scp->flags &= ~(CM_SCACHEFLAG_STATD
152 | CM_SCACHEFLAG_DELETED
154 | CM_SCACHEFLAG_PURERO
155 | CM_SCACHEFLAG_OVERQUOTA
156 | CM_SCACHEFLAG_OUTOFSPACE
157 | CM_SCACHEFLAG_EACCESS);
158 scp->serverModTime = 0;
159 scp->dataVersion = 0;
160 scp->bulkStatProgress = hzero;
163 if (scp->cbServerp) {
164 cm_PutServer(scp->cbServerp);
165 scp->cbServerp = NULL;
174 /* remove from dnlc */
178 /* discard cached status; if non-zero, Close
179 * tried to store this to server but failed */
182 /* drop held volume ref */
184 cm_PutVolume(scp->volp);
188 /* discard symlink info */
189 scp->mountPointStringp[0] = '\0';
190 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
191 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
193 /* reset locking info */
194 scp->fileLocksH = NULL;
195 scp->fileLocksT = NULL;
196 scp->serverLock = (-1);
197 scp->exclusiveLocks = 0;
198 scp->sharedLocks = 0;
200 /* not locked, but there can be no references to this guy
201 * while we hold the global refcount lock.
203 cm_FreeAllACLEnts(scp);
208 /* called with cm_scacheLock write-locked; find a vnode to recycle.
209 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
211 cm_scache_t *cm_GetNewSCache(void)
217 /* first pass - look for deleted objects */
218 for ( scp = cm_data.scacheLRULastp;
220 scp = (cm_scache_t *) osi_QPrev(&scp->q))
222 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
224 if (scp->refCount == 0) {
225 if (scp->flags & CM_SCACHEFLAG_DELETED) {
226 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
227 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
229 /* we found an entry, so return it */
230 /* now remove from the LRU queue and put it back at the
231 * head of the LRU queue.
233 cm_AdjustScacheLRU(scp);
238 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
239 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
240 /* we found an entry, so return it */
241 /* now remove from the LRU queue and put it back at the
242 * head of the LRU queue.
244 cm_AdjustScacheLRU(scp);
251 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
254 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
255 /* There were no deleted scache objects that we could use. Try to find
256 * one that simply hasn't been used in a while.
258 for ( scp = cm_data.scacheLRULastp;
260 scp = (cm_scache_t *) osi_QPrev(&scp->q))
262 /* It is possible for the refCount to be zero and for there still
263 * to be outstanding dirty buffers. If there are dirty buffers,
264 * we must not recycle the scp. */
265 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
266 if (!buf_DirtyBuffersExist(&scp->fid)) {
267 if (!cm_RecycleSCache(scp, 0)) {
268 /* we found an entry, so return it */
269 /* now remove from the LRU queue and put it back at the
270 * head of the LRU queue.
272 cm_AdjustScacheLRU(scp);
278 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
282 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
287 /* if we get here, we should allocate a new scache entry. We either are below
288 * quota or we have a leak and need to allocate a new one to avoid panicing.
290 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
291 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
292 memset(scp, 0, sizeof(cm_scache_t));
293 scp->magic = CM_SCACHE_MAGIC;
294 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
295 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
296 scp->serverLock = -1;
298 /* and put it in the LRU queue */
299 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
300 if (!cm_data.scacheLRULastp)
301 cm_data.scacheLRULastp = scp;
302 cm_data.currentSCaches++;
303 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
305 scp->allNextp = cm_data.allSCachesp;
306 cm_data.allSCachesp = scp;
310 /* like strcmp, only for fids */
311 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
313 if (ap->vnode != bp->vnode)
315 if (ap->volume != bp->volume)
317 if (ap->unique != bp->unique)
319 if (ap->cell != bp->cell)
324 void cm_fakeSCacheInit(int newFile)
327 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
328 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
329 /* can leave clientModTime at 0 */
330 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
331 cm_data.fakeSCache.unixModeBits = 0777;
332 cm_data.fakeSCache.length.LowPart = 1000;
333 cm_data.fakeSCache.linkCount = 1;
334 cm_data.fakeSCache.refCount = 1;
336 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
340 cm_ValidateSCache(void)
342 cm_scache_t * scp, *lscp;
345 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
346 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
347 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
348 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
352 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
354 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
355 if (scp->magic != CM_SCACHE_MAGIC) {
356 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
357 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
360 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
361 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
362 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
365 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
366 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
367 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
370 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
371 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
372 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
375 if (i > cm_data.currentSCaches ) {
376 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
377 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
380 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
381 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
382 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
387 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
388 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
389 if (scp->magic != CM_SCACHE_MAGIC) {
390 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
391 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
394 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
395 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
396 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
399 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
400 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
401 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
404 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
405 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
406 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
409 if (i > cm_data.currentSCaches ) {
410 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
411 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
414 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
415 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
416 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
421 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
422 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
423 if (scp->magic != CM_SCACHE_MAGIC) {
424 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
425 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
428 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
429 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
430 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
433 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
434 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
435 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
438 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
439 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
440 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
446 return cm_dnlcValidate();
450 cm_SuspendSCache(void)
454 cm_GiveUpAllCallbacksAllServers();
456 lock_ObtainWrite(&cm_scacheLock);
457 for ( scp = cm_data.allSCachesp; scp;
458 scp = scp->allNextp ) {
459 if (scp->cbServerp) {
460 cm_PutServer(scp->cbServerp);
461 scp->cbServerp = NULL;
464 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
466 lock_ReleaseWrite(&cm_scacheLock);
470 cm_ShutdownSCache(void)
474 lock_ObtainWrite(&cm_scacheLock);
476 for ( scp = cm_data.allSCachesp; scp;
477 scp = scp->allNextp ) {
478 if (scp->randomACLp) {
479 lock_ObtainMutex(&scp->mx);
480 cm_FreeAllACLEnts(scp);
481 lock_ReleaseMutex(&scp->mx);
484 if (scp->cbServerp) {
485 cm_PutServer(scp->cbServerp);
486 scp->cbServerp = NULL;
489 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
491 lock_FinalizeMutex(&scp->mx);
492 lock_FinalizeRWLock(&scp->bufCreateLock);
494 lock_ReleaseWrite(&cm_scacheLock);
496 cm_GiveUpAllCallbacksAllServers();
498 return cm_dnlcShutdown();
501 void cm_InitSCache(int newFile, long maxSCaches)
503 static osi_once_t once;
505 if (osi_Once(&once)) {
506 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
508 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
509 cm_data.allSCachesp = NULL;
510 cm_data.currentSCaches = 0;
511 cm_data.maxSCaches = maxSCaches;
512 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
516 for ( scp = cm_data.allSCachesp; scp;
517 scp = scp->allNextp ) {
518 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
519 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
521 scp->cbServerp = NULL;
523 scp->fileLocksH = NULL;
524 scp->fileLocksT = NULL;
525 scp->serverLock = (-1);
526 scp->lastRefreshCycle = 0;
527 scp->exclusiveLocks = 0;
528 scp->sharedLocks = 0;
534 scp->flags &= ~CM_SCACHEFLAG_WAITING;
537 cm_allFileLocks = NULL;
538 cm_freeFileLocks = NULL;
539 cm_lockRefreshCycle = 0;
540 cm_fakeSCacheInit(newFile);
541 cm_dnlcInit(newFile);
546 /* version that doesn't bother creating the entry if we don't find it */
547 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
552 hash = CM_SCACHE_HASH(fidp);
554 if (fidp->cell == 0) {
558 lock_ObtainWrite(&cm_scacheLock);
559 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
560 if (cm_FidCmp(fidp, &scp->fid) == 0) {
561 cm_HoldSCacheNoLock(scp);
562 cm_AdjustScacheLRU(scp);
563 lock_ReleaseWrite(&cm_scacheLock);
567 lock_ReleaseWrite(&cm_scacheLock);
571 #ifdef DEBUG_REFCOUNT
572 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
573 cm_req_t *reqp, char * file, long line)
575 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
582 cm_volume_t *volp = NULL;
585 int special; // yj: boolean variable to test if file is on root.afs
587 extern cm_fid_t cm_rootFid;
589 hash = CM_SCACHE_HASH(fidp);
591 osi_assert(fidp->cell != 0);
593 if (fidp->cell== cm_data.rootFid.cell &&
594 fidp->volume==cm_data.rootFid.volume &&
595 fidp->vnode==0x0 && fidp->unique==0x0)
597 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
600 // yj: check if we have the scp, if so, we don't need
601 // to do anything else
602 lock_ObtainWrite(&cm_scacheLock);
603 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
604 if (cm_FidCmp(fidp, &scp->fid) == 0) {
605 #ifdef DEBUG_REFCOUNT
606 afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
607 osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
609 cm_HoldSCacheNoLock(scp);
611 cm_AdjustScacheLRU(scp);
612 lock_ReleaseWrite(&cm_scacheLock);
617 // yj: when we get here, it means we don't have an scp
618 // so we need to either load it or fake it, depending
619 // on whether the file is "special", see below.
621 // yj: if we're trying to get an scp for a file that's
622 // on root.afs of homecell, we want to handle it specially
623 // because we have to fill in the status stuff 'coz we
624 // don't want trybulkstat to fill it in for us
625 #ifdef AFS_FREELANCE_CLIENT
626 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
627 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
628 !(fidp->vnode==0x1 && fidp->unique==0x1));
629 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
630 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
631 fidp->vnode==0x1 && fidp->unique==0x1);
632 if (cm_freelanceEnabled && isRoot) {
633 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
634 /* freelance: if we are trying to get the root scp for the first
635 * time, we will just put in a place holder entry.
640 if (cm_freelanceEnabled && special) {
641 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
642 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
643 lock_ObtainMutex(&cm_Freelance_Lock);
644 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
645 lock_ReleaseMutex(&cm_Freelance_Lock);
649 scp = cm_GetNewSCache();
651 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
652 lock_ReleaseWrite(&cm_scacheLock);
653 return CM_ERROR_WOULDBLOCK;
656 #if not_too_dangerous
657 /* dropping the cm_scacheLock allows more than one thread
658 * to obtain the same cm_scache_t from the LRU list. Since
659 * the refCount is known to be zero at this point we have to
660 * assume that no one else is using the one this is returned.
662 lock_ReleaseWrite(&cm_scacheLock);
663 lock_ObtainMutex(&scp->mx);
664 lock_ObtainWrite(&cm_scacheLock);
667 scp->volp = cm_data.rootSCachep->volp;
668 cm_GetVolume(scp->volp); /* grab an additional reference */
669 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
670 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
671 scp->dotdotFid.unique=1;
672 scp->dotdotFid.vnode=1;
673 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
674 scp->nextp=cm_data.scacheHashTablep[hash];
675 cm_data.scacheHashTablep[hash]=scp;
676 scp->flags |= CM_SCACHEFLAG_INHASH;
678 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
679 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
680 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
682 scp->fileType = CM_SCACHETYPE_INVALID;
684 lock_ObtainMutex(&cm_Freelance_Lock);
685 scp->length.LowPart = (DWORD)strlen(mp)+4;
686 scp->length.HighPart = 0;
687 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
688 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
689 lock_ReleaseMutex(&cm_Freelance_Lock);
692 scp->unixModeBits=0777;
693 scp->clientModTime=FakeFreelanceModTime;
694 scp->serverModTime=FakeFreelanceModTime;
695 scp->parentUnique = 0x1;
696 scp->parentVnode=0x1;
698 scp->dataVersion=cm_data.fakeDirVersion;
699 scp->lockDataVersion=-1; /* no lock yet */
700 #if not_too_dangerous
701 lock_ReleaseMutex(&scp->mx);
704 lock_ReleaseWrite(&cm_scacheLock);
705 #ifdef DEBUG_REFCOUNT
706 afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
707 osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
712 #endif /* AFS_FREELANCE_CLIENT */
714 /* otherwise, we need to find the volume */
715 if (!cm_freelanceEnabled || !isRoot) {
716 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
717 cellp = cm_FindCellByID(fidp->cell);
719 return CM_ERROR_NOSUCHCELL;
721 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
724 lock_ObtainWrite(&cm_scacheLock);
727 /* otherwise, we have the volume, now reverify that the scp doesn't
728 * exist, and proceed.
730 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
731 if (cm_FidCmp(fidp, &scp->fid) == 0) {
732 #ifdef DEBUG_REFCOUNT
733 afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
734 osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
736 cm_HoldSCacheNoLock(scp);
737 osi_assert(scp->volp == volp);
738 cm_AdjustScacheLRU(scp);
739 lock_ReleaseWrite(&cm_scacheLock);
747 /* now, if we don't have the fid, recycle something */
748 scp = cm_GetNewSCache();
750 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
751 lock_ReleaseWrite(&cm_scacheLock);
754 return CM_ERROR_WOULDBLOCK;
756 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
758 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
760 #if not_too_dangerous
761 /* dropping the cm_scacheLock allows more than one thread
762 * to obtain the same cm_scache_t from the LRU list. Since
763 * the refCount is known to be zero at this point we have to
764 * assume that no one else is using the one this is returned.
766 lock_ReleaseWrite(&cm_scacheLock);
767 lock_ObtainMutex(&scp->mx);
768 lock_ObtainWrite(&cm_scacheLock);
771 scp->volp = volp; /* a held reference */
773 if (!cm_freelanceEnabled || !isRoot) {
774 /* if this scache entry represents a volume root then we need
775 * to copy the dotdotFipd from the volume structure where the
776 * "master" copy is stored (defect 11489)
778 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
779 scp->dotdotFid = volp->dotdotFid;
782 if (volp->ro.ID == fidp->volume)
783 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
784 else if (volp->bk.ID == fidp->volume)
785 scp->flags |= CM_SCACHEFLAG_RO;
787 scp->nextp = cm_data.scacheHashTablep[hash];
788 cm_data.scacheHashTablep[hash] = scp;
789 scp->flags |= CM_SCACHEFLAG_INHASH;
791 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
792 #if not_too_dangerous
793 lock_ReleaseMutex(&scp->mx);
796 /* XXX - The following fields in the cm_scache are
802 lock_ReleaseWrite(&cm_scacheLock);
804 /* now we have a held scache entry; just return it */
806 #ifdef DEBUG_REFCOUNT
807 afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
808 osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
813 /* Returns a held reference to the scache's parent
815 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
820 cm_scache_t * pscp = NULL;
822 lock_ObtainRead(&cm_scacheLock);
823 parent_fid = scp->fid;
824 parent_fid.vnode = scp->parentVnode;
825 parent_fid.unique = scp->parentUnique;
827 if (cm_FidCmp(&scp->fid, &parent_fid)) {
828 i = CM_SCACHE_HASH(&parent_fid);
829 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
830 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
831 cm_HoldSCacheNoLock(pscp);
837 lock_ReleaseRead(&cm_scacheLock);
842 /* synchronize a fetch, store, read, write, fetch status or store status.
843 * Called with scache mutex held, and returns with it held, but temporarily
844 * drops it during the fetch.
846 * At most one flag can be on in flags, if this is an RPC request.
848 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
850 * There are a lot of weird restrictions here; here's an attempt to explain the
851 * rationale for the concurrency restrictions implemented in this function.
853 * First, although the file server will break callbacks when *another* machine
854 * modifies a file or status block, the client itself is responsible for
855 * concurrency control on its own requests. Callback breaking events are rare,
856 * and simply invalidate any concurrent new status info.
858 * In the absence of callback breaking messages, we need to know how to
859 * synchronize incoming responses describing updates to files. We synchronize
860 * operations that update the data version by comparing the data versions.
861 * However, updates that do not update the data, but only the status, can't be
862 * synchronized with fetches or stores, since there's nothing to compare
863 * to tell which operation executed first at the server.
865 * Thus, we can allow multiple ops that change file data, or dir data, and
866 * fetches. However, status storing ops have to be done serially.
868 * Furthermore, certain data-changing ops are incompatible: we can't read or
869 * write a buffer while doing a truncate. We can't read and write the same
870 * buffer at the same time, or write while fetching or storing, or read while
871 * fetching a buffer (this may change). We can't fetch and store at the same
874 * With respect to status, we can't read and write at the same time, read while
875 * fetching, write while fetching or storing, or fetch and store at the same time.
877 * We can't allow a get callback RPC to run in concurrently with something that
878 * will return updated status, since we could start a call, have the server
879 * return status, have another machine make an update to the status (which
880 * doesn't change serverModTime), have the original machine get a new callback,
881 * and then have the original machine merge in the early, old info from the
882 * first call. At this point, the easiest way to avoid this problem is to have
883 * getcallback calls conflict with all others for the same vnode. Other calls
884 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
885 * vnode must be careful not to merge in their status unless they have obtained
886 * a callback from the start of their call.
889 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
890 * Each such RPC passes a FileLength parameter, which the server uses to do
891 * pre-truncation if necessary. So if two RPC's are processed out of order at
892 * the server, the one with the smaller FileLength will be processed last,
893 * possibly resulting in a bogus truncation. The simplest way to avoid this
894 * is to serialize all StoreData RPC's. This is the reason we defined
895 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
897 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
898 afs_uint32 rights, afs_uint32 flags)
900 osi_queueData_t *qdp;
903 afs_uint32 outRights;
905 afs_uint32 sleep_scp_flags = 0;
906 afs_uint32 sleep_buf_cmflags = 0;
907 afs_uint32 sleep_scp_bufs = 0;
909 /* lookup this first */
910 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
913 osi_assert(bufp->refCount > 0);
916 /* Do the access check. Now we don't really do the access check
917 * atomically, since the caller doesn't expect the parent dir to be
918 * returned locked, and that is what we'd have to do to prevent a
919 * callback breaking message on the parent due to a setacl call from
920 * being processed while we're running. So, instead, we check things
921 * here, and if things look fine with the access, we proceed to finish
922 * the rest of this check. Sort of a hack, but probably good enough.
926 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
927 /* if we're bringing in a new status block, ensure that
928 * we aren't already doing so, and that no one is
929 * changing the status concurrently, either. We need
930 * to do this, even if the status is of a different
931 * type, since we don't have the ability to figure out,
932 * in the AFS 3 protocols, which status-changing
933 * operation ran first, or even which order a read and
934 * a write occurred in.
936 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
937 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
938 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
942 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
943 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
944 /* if we're going to make an RPC to change the status, make sure
945 * that no one is bringing in or sending out the status.
947 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
948 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
949 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
952 if (scp->bufReadsp || scp->bufWritesp) {
953 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
957 if (flags & CM_SCACHESYNC_FETCHDATA) {
958 /* if we're bringing in a new chunk of data, make sure that
959 * nothing is happening to that chunk, and that we aren't
960 * changing the basic file status info, either.
962 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
963 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
964 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
967 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
968 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
972 if (flags & CM_SCACHESYNC_STOREDATA) {
973 /* same as fetch data */
974 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
975 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
976 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
979 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
980 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
985 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
986 /* Don't allow concurrent StoreData RPC's */
987 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
988 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
993 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
994 /* Don't allow more than one BKG store request */
995 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
996 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1001 if (flags & CM_SCACHESYNC_LOCK) {
1002 /* Don't allow concurrent fiddling with lock lists */
1003 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1004 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1009 /* now the operations that don't correspond to making RPCs */
1010 if (flags & CM_SCACHESYNC_GETSTATUS) {
1011 /* we can use the status that's here, if we're not
1012 * bringing in new status.
1014 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1015 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1019 if (flags & CM_SCACHESYNC_SETSTATUS) {
1020 /* we can make a change to the local status, as long as
1021 * the status isn't changing now.
1023 * If we're fetching or storing a chunk of data, we can
1024 * change the status locally, since the fetch/store
1025 * operations don't change any of the data that we're
1028 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1029 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1033 if (flags & CM_SCACHESYNC_READ) {
1034 /* we're going to read the data, make sure that the
1035 * status is available, and that the data is here. It
1036 * is OK to read while storing the data back.
1038 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1039 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1042 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1043 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1046 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1047 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1051 if (flags & CM_SCACHESYNC_WRITE) {
1052 /* don't write unless the status is stable and the chunk
1055 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1056 | CM_SCACHEFLAG_SIZESTORING)) {
1057 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1060 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1062 CM_BUF_CMWRITING))) {
1063 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1065 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1066 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1067 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1073 // yj: modified this so that callback only checked if we're
1074 // not checking something on /afs
1075 /* fix the conditional to match the one in cm_HaveCallback */
1076 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1077 #ifdef AFS_FREELANCE_CLIENT
1078 && (!cm_freelanceEnabled ||
1079 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1080 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1081 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1082 cm_fakeDirCallback < 2)
1083 #endif /* AFS_FREELANCE_CLIENT */
1085 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1086 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1089 lock_ReleaseMutex(&bufp->mx);
1090 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1092 lock_ReleaseMutex(&scp->mx);
1093 lock_ObtainMutex(&bufp->mx);
1094 lock_ObtainMutex(&scp->mx);
1098 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1104 /* can't check access rights without a callback */
1105 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1107 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1108 return CM_ERROR_READONLY;
1110 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1111 if (~outRights & rights)
1112 return CM_ERROR_NOACCESS;
1115 /* we don't know the required access rights */
1116 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1117 code = cm_GetAccessRights(scp, userp, reqp);
1119 lock_ReleaseMutex(&scp->mx);
1120 lock_ObtainMutex(&bufp->mx);
1121 lock_ObtainMutex(&scp->mx);
1129 /* if we get here, we're happy */
1133 /* first check if we're not supposed to wait: fail
1134 * in this case, returning with everything still locked.
1136 if (flags & CM_SCACHESYNC_NOWAIT)
1137 return CM_ERROR_WOULDBLOCK;
1139 sleep_scp_flags = scp->flags; /* so we know why we slept */
1140 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1141 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1143 /* wait here, then try again */
1144 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1145 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1147 scp->waitRequests++;
1148 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1149 scp, scp->waitCount, scp->waitRequests);
1151 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1152 scp->flags |= CM_SCACHEFLAG_WAITING;
1153 scp->waitCount = scp->waitRequests = 1;
1156 lock_ReleaseMutex(&bufp->mx);
1157 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1159 smb_UpdateServerPriority();
1162 lock_ObtainMutex(&bufp->mx);
1163 lock_ObtainMutex(&scp->mx);
1165 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1166 scp, scp->waitCount, scp->waitRequests);
1167 if (scp->waitCount == 0) {
1168 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1169 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1170 scp->waitRequests = 0;
1172 } /* big while loop */
1174 /* now, update the recorded state for RPC-type calls */
1175 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1176 scp->flags |= CM_SCACHEFLAG_FETCHING;
1177 if (flags & CM_SCACHESYNC_STORESTATUS)
1178 scp->flags |= CM_SCACHEFLAG_STORING;
1179 if (flags & CM_SCACHESYNC_STORESIZE)
1180 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1181 if (flags & CM_SCACHESYNC_GETCALLBACK)
1182 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1183 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1184 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1185 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1186 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1187 if (flags & CM_SCACHESYNC_LOCK)
1188 scp->flags |= CM_SCACHEFLAG_LOCKING;
1190 /* now update the buffer pointer */
1191 if (flags & CM_SCACHESYNC_FETCHDATA) {
1192 /* ensure that the buffer isn't already in the I/O list */
1194 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1195 tbufp = osi_GetQData(qdp);
1196 osi_assert(tbufp != bufp);
1200 /* queue a held reference to the buffer in the "reading" I/O list */
1201 qdp = osi_QDAlloc();
1202 osi_SetQData(qdp, bufp);
1205 bufp->cmFlags |= CM_BUF_CMFETCHING;
1207 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1210 if (flags & CM_SCACHESYNC_STOREDATA) {
1211 /* ensure that the buffer isn't already in the I/O list */
1213 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1214 tbufp = osi_GetQData(qdp);
1215 osi_assert(tbufp != bufp);
1219 /* queue a held reference to the buffer in the "writing" I/O list */
1220 qdp = osi_QDAlloc();
1221 osi_SetQData(qdp, bufp);
1224 bufp->cmFlags |= CM_BUF_CMSTORING;
1226 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1229 if (flags & CM_SCACHESYNC_WRITE) {
1230 /* mark the buffer as being written to. */
1232 bufp->cmFlags |= CM_BUF_CMWRITING;
1239 /* for those syncops that setup for RPCs.
1240 * Called with scache locked.
1242 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1244 osi_queueData_t *qdp;
1247 lock_AssertMutex(&scp->mx);
1249 /* now, update the recorded state for RPC-type calls */
1250 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1251 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1252 if (flags & CM_SCACHESYNC_STORESTATUS)
1253 scp->flags &= ~CM_SCACHEFLAG_STORING;
1254 if (flags & CM_SCACHESYNC_STORESIZE)
1255 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1256 if (flags & CM_SCACHESYNC_GETCALLBACK)
1257 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1258 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1259 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1260 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1261 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1262 if (flags & CM_SCACHESYNC_LOCK)
1263 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1265 /* now update the buffer pointer */
1266 if (flags & CM_SCACHESYNC_FETCHDATA) {
1269 /* ensure that the buffer isn't already in the I/O list */
1270 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1271 tbufp = osi_GetQData(qdp);
1276 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1281 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1282 if (bufp->flags & CM_BUF_WAITING) {
1283 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1284 osi_Wakeup((LONG_PTR) &bufp);
1291 /* now update the buffer pointer */
1292 if (flags & CM_SCACHESYNC_STOREDATA) {
1294 /* ensure that the buffer isn't already in the I/O list */
1295 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1296 tbufp = osi_GetQData(qdp);
1301 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1306 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1307 if (bufp->flags & CM_BUF_WAITING) {
1308 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1309 osi_Wakeup((LONG_PTR) &bufp);
1316 if (flags & CM_SCACHESYNC_WRITE) {
1318 osi_assert(bufp->cmFlags & CM_BUF_CMWRITING);
1320 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1324 /* and wakeup anyone who is waiting */
1325 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1326 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1327 osi_Wakeup((LONG_PTR) &scp->flags);
1331 /* merge in a response from an RPC. The scp must be locked, and the callback
1334 * Don't overwrite any status info that is dirty, since we could have a store
1335 * operation (such as store data) that merges some info in, and we don't want
1336 * to lose the local updates. Typically, there aren't many updates we do
1337 * locally, anyway, probably only mtime.
1339 * There is probably a bug in here where a chmod (which doesn't change
1340 * serverModTime) that occurs between two fetches, both of whose responses are
1341 * handled after the callback breaking is done, but only one of whose calls
1342 * started before that, can cause old info to be merged from the first call.
1344 void cm_MergeStatus(cm_scache_t *dscp,
1345 cm_scache_t *scp, AFSFetchStatus *statusp,
1346 AFSVolSync *volsyncp,
1347 cm_user_t *userp, afs_uint32 flags)
1349 // yj: i want to create some fake status for the /afs directory and the
1350 // entries under that directory
1351 #ifdef AFS_FREELANCE_CLIENT
1352 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1353 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1354 statusp->InterfaceVersion = 0x1;
1355 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1356 statusp->LinkCount = scp->linkCount;
1357 statusp->Length = cm_fakeDirSize;
1358 statusp->Length_hi = 0;
1359 statusp->DataVersion = cm_data.fakeDirVersion;
1360 statusp->Author = 0x1;
1361 statusp->Owner = 0x0;
1362 statusp->CallerAccess = 0x9;
1363 statusp->AnonymousAccess = 0x9;
1364 statusp->UnixModeBits = 0777;
1365 statusp->ParentVnode = 0x1;
1366 statusp->ParentUnique = 0x1;
1367 statusp->ResidencyMask = 0;
1368 statusp->ClientModTime = FakeFreelanceModTime;
1369 statusp->ServerModTime = FakeFreelanceModTime;
1371 statusp->SyncCounter = 0;
1372 statusp->dataVersionHigh = 0;
1373 statusp->errorCode = 0;
1375 #endif /* AFS_FREELANCE_CLIENT */
1377 if (statusp->errorCode != 0) {
1378 scp->flags |= CM_SCACHEFLAG_EACCESS;
1379 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1381 scp->fileType = 0; /* unknown */
1383 scp->serverModTime = 0;
1384 scp->clientModTime = 0;
1385 scp->length.LowPart = 0;
1386 scp->length.HighPart = 0;
1387 scp->serverLength.LowPart = 0;
1388 scp->serverLength.HighPart = 0;
1392 scp->unixModeBits = 0;
1394 scp->dataVersion = 0;
1397 scp->parentVnode = dscp->fid.vnode;
1398 scp->parentUnique = dscp->fid.unique;
1400 scp->parentVnode = 0;
1401 scp->parentUnique = 0;
1405 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1408 if (!(flags & CM_MERGEFLAG_FORCE)
1409 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1410 struct cm_cell *cellp;
1412 cellp = cm_FindCellByID(scp->fid.cell);
1413 if (scp->cbServerp) {
1414 struct cm_volume *volp = NULL;
1416 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1417 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1418 osi_Log2(afsd_logp, "old data from server %x volume %s",
1419 scp->cbServerp->addr.sin_addr.s_addr,
1420 volp ? volp->namep : "(unknown)");
1424 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1425 scp, scp->dataVersion, statusp->DataVersion);
1426 /* we have a number of data fetch/store operations running
1427 * concurrently, and we can tell which one executed last at the
1428 * server by its mtime.
1429 * Choose the one with the largest mtime, and ignore the rest.
1431 * These concurrent calls are incompatible with setting the
1432 * mtime, so we won't have a locally changed mtime here.
1434 * We could also have ACL info for a different user than usual,
1435 * in which case we have to do that part of the merge, anyway.
1436 * We won't have to worry about the info being old, since we
1437 * won't have concurrent calls
1438 * that change file status running from this machine.
1440 * Added 3/17/98: if we see data version regression on an RO
1441 * file, it's probably due to a server holding an out-of-date
1442 * replica, rather than to concurrent RPC's. Failures to
1443 * release replicas are now flagged by the volserver, but only
1444 * since AFS 3.4 5.22, so there are plenty of clients getting
1445 * out-of-date replicas out there.
1447 * If we discover an out-of-date replica, by this time it's too
1448 * late to go to another server and retry. Also, we can't
1449 * reject the merge, because then there is no way for
1450 * GetAccess to do its work, and the caller gets into an
1451 * infinite loop. So we just grin and bear it.
1453 if (!(scp->flags & CM_SCACHEFLAG_RO))
1457 scp->serverModTime = statusp->ServerModTime;
1459 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1460 scp->clientModTime = statusp->ClientModTime;
1462 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1463 scp->length.LowPart = statusp->Length;
1464 scp->length.HighPart = statusp->Length_hi;
1467 scp->serverLength.LowPart = statusp->Length;
1468 scp->serverLength.HighPart = statusp->Length_hi;
1470 scp->linkCount = statusp->LinkCount;
1471 scp->owner = statusp->Owner;
1472 scp->group = statusp->Group;
1473 scp->unixModeBits = statusp->UnixModeBits & 07777;
1475 if (statusp->FileType == File)
1476 scp->fileType = CM_SCACHETYPE_FILE;
1477 else if (statusp->FileType == Directory)
1478 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1479 else if (statusp->FileType == SymbolicLink) {
1480 if ((scp->unixModeBits & 0111) == 0)
1481 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1483 scp->fileType = CM_SCACHETYPE_SYMLINK;
1486 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1487 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1489 /* and other stuff */
1490 scp->parentVnode = statusp->ParentVnode;
1491 scp->parentUnique = statusp->ParentUnique;
1493 /* and merge in the private acl cache info, if this is more than the public
1494 * info; merge in the public stuff in any case.
1496 scp->anyAccess = statusp->AnonymousAccess;
1498 if (userp != NULL) {
1499 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1502 if ((flags & CM_MERGEFLAG_STOREDATA) &&
1503 statusp->DataVersion - scp->dataVersion == 1) {
1506 for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1508 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1509 bp->dataVersion == scp->dataVersion)
1510 bp->dataVersion = statusp->DataVersion;
1514 scp->dataVersion = statusp->DataVersion;
1517 /* note that our stat cache info is incorrect, so force us eventually
1518 * to stat the file again. There may be dirty data associated with
1519 * this vnode, and we want to preserve that information.
1521 * This function works by simply simulating a loss of the callback.
1523 * This function must be called with the scache locked.
1525 void cm_DiscardSCache(cm_scache_t *scp)
1527 lock_AssertMutex(&scp->mx);
1528 if (scp->cbServerp) {
1529 cm_PutServer(scp->cbServerp);
1530 scp->cbServerp = NULL;
1533 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1534 cm_dnlcPurgedp(scp);
1535 cm_dnlcPurgevp(scp);
1536 cm_FreeAllACLEnts(scp);
1538 /* Force mount points and symlinks to be re-evaluated */
1539 scp->mountPointStringp[0] = '\0';
1542 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1544 afsFidp->Volume = fidp->volume;
1545 afsFidp->Vnode = fidp->vnode;
1546 afsFidp->Unique = fidp->unique;
1549 #ifdef DEBUG_REFCOUNT
1550 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1552 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1555 osi_assert(scp != 0);
1557 #ifdef DEBUG_REFCOUNT
1558 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1559 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1563 #ifdef DEBUG_REFCOUNT
1564 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1566 void cm_HoldSCache(cm_scache_t *scp)
1569 osi_assert(scp != 0);
1570 lock_ObtainWrite(&cm_scacheLock);
1572 #ifdef DEBUG_REFCOUNT
1573 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1574 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1576 lock_ReleaseWrite(&cm_scacheLock);
1579 #ifdef DEBUG_REFCOUNT
1580 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1582 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1585 osi_assert(scp != NULL);
1586 if (scp->refCount == 0)
1587 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1588 osi_assert(scp->refCount-- >= 0);
1589 #ifdef DEBUG_REFCOUNT
1590 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1591 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1595 #ifdef DEBUG_REFCOUNT
1596 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1598 void cm_ReleaseSCache(cm_scache_t *scp)
1601 osi_assert(scp != NULL);
1602 lock_ObtainWrite(&cm_scacheLock);
1603 if (scp->refCount == 0)
1604 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1605 osi_assert(scp->refCount != 0);
1607 #ifdef DEBUG_REFCOUNT
1608 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1609 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1611 lock_ReleaseWrite(&cm_scacheLock);
1614 /* just look for the scp entry to get filetype */
1615 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1616 int cm_FindFileType(cm_fid_t *fidp)
1621 hash = CM_SCACHE_HASH(fidp);
1623 osi_assert(fidp->cell != 0);
1625 lock_ObtainWrite(&cm_scacheLock);
1626 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1627 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1628 lock_ReleaseWrite(&cm_scacheLock);
1629 return scp->fileType;
1632 lock_ReleaseWrite(&cm_scacheLock);
1636 /* dump all scp's that have reference count > 0 to a file.
1637 * cookie is used to identify this batch for easy parsing,
1638 * and it a string provided by a caller
1640 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1648 lock_ObtainRead(&cm_scacheLock);
1650 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1651 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1653 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1655 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",
1656 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1657 scp->volp, scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp, scp->flags,
1658 (unsigned long)scp->cbExpires, scp->refCount);
1659 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1662 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1663 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1664 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1665 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1667 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1669 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1671 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1672 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1673 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1677 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1678 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1681 lock_ReleaseRead(&cm_scacheLock);