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))) {
968 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING 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))) {
980 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING 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);
1047 if (flags & CM_SCACHESYNC_WRITE) {
1048 /* don't write unless the status is stable and the chunk
1051 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1052 | CM_SCACHEFLAG_SIZESTORING)) {
1053 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1056 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
1057 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
1062 // yj: modified this so that callback only checked if we're
1063 // not checking something on /afs
1064 /* fix the conditional to match the one in cm_HaveCallback */
1065 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1066 #ifdef AFS_FREELANCE_CLIENT
1067 && (!cm_freelanceEnabled ||
1068 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1069 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1070 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1071 cm_fakeDirCallback < 2)
1072 #endif /* AFS_FREELANCE_CLIENT */
1074 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1075 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1078 lock_ReleaseMutex(&bufp->mx);
1079 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1081 lock_ReleaseMutex(&scp->mx);
1082 lock_ObtainMutex(&bufp->mx);
1083 lock_ObtainMutex(&scp->mx);
1087 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1093 /* can't check access rights without a callback */
1094 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1096 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1097 return CM_ERROR_READONLY;
1099 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1100 if (~outRights & rights)
1101 return CM_ERROR_NOACCESS;
1104 /* we don't know the required access rights */
1105 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1106 code = cm_GetAccessRights(scp, userp, reqp);
1108 lock_ReleaseMutex(&scp->mx);
1109 lock_ObtainMutex(&bufp->mx);
1110 lock_ObtainMutex(&scp->mx);
1118 /* if we get here, we're happy */
1122 /* first check if we're not supposed to wait: fail
1123 * in this case, returning with everything still locked.
1125 if (flags & CM_SCACHESYNC_NOWAIT)
1126 return CM_ERROR_WOULDBLOCK;
1128 sleep_scp_flags = scp->flags; /* so we know why we slept */
1129 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1130 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1132 /* wait here, then try again */
1133 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1134 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1136 scp->waitRequests++;
1137 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1138 scp, scp->waitCount, scp->waitRequests);
1140 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1141 scp->flags |= CM_SCACHEFLAG_WAITING;
1142 scp->waitCount = scp->waitRequests = 1;
1145 lock_ReleaseMutex(&bufp->mx);
1146 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1148 smb_UpdateServerPriority();
1151 lock_ObtainMutex(&bufp->mx);
1152 lock_ObtainMutex(&scp->mx);
1154 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1155 scp, scp->waitCount, scp->waitRequests);
1156 if (scp->waitCount == 0) {
1157 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1158 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1159 scp->waitRequests = 0;
1161 } /* big while loop */
1163 /* now, update the recorded state for RPC-type calls */
1164 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1165 scp->flags |= CM_SCACHEFLAG_FETCHING;
1166 if (flags & CM_SCACHESYNC_STORESTATUS)
1167 scp->flags |= CM_SCACHEFLAG_STORING;
1168 if (flags & CM_SCACHESYNC_STORESIZE)
1169 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1170 if (flags & CM_SCACHESYNC_GETCALLBACK)
1171 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1172 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1173 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1174 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1175 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1176 if (flags & CM_SCACHESYNC_LOCK)
1177 scp->flags |= CM_SCACHEFLAG_LOCKING;
1179 /* now update the buffer pointer */
1180 if (flags & CM_SCACHESYNC_FETCHDATA) {
1181 /* ensure that the buffer isn't already in the I/O list */
1183 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1184 tbufp = osi_GetQData(qdp);
1185 osi_assert(tbufp != bufp);
1189 /* queue a held reference to the buffer in the "reading" I/O list */
1190 qdp = osi_QDAlloc();
1191 osi_SetQData(qdp, bufp);
1194 bufp->cmFlags |= CM_BUF_CMFETCHING;
1196 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1199 if (flags & CM_SCACHESYNC_STOREDATA) {
1200 /* ensure that the buffer isn't already in the I/O list */
1202 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1203 tbufp = osi_GetQData(qdp);
1204 osi_assert(tbufp != bufp);
1208 /* queue a held reference to the buffer in the "writing" I/O list */
1209 qdp = osi_QDAlloc();
1210 osi_SetQData(qdp, bufp);
1213 bufp->cmFlags |= CM_BUF_CMSTORING;
1215 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1221 /* for those syncops that setup for RPCs.
1222 * Called with scache locked.
1224 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1226 osi_queueData_t *qdp;
1229 lock_AssertMutex(&scp->mx);
1231 /* now, update the recorded state for RPC-type calls */
1232 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1233 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1234 if (flags & CM_SCACHESYNC_STORESTATUS)
1235 scp->flags &= ~CM_SCACHEFLAG_STORING;
1236 if (flags & CM_SCACHESYNC_STORESIZE)
1237 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1238 if (flags & CM_SCACHESYNC_GETCALLBACK)
1239 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1240 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1241 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1242 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1243 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1244 if (flags & CM_SCACHESYNC_LOCK)
1245 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1247 /* now update the buffer pointer */
1248 if (flags & CM_SCACHESYNC_FETCHDATA) {
1251 /* ensure that the buffer isn't already in the I/O list */
1252 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1253 tbufp = osi_GetQData(qdp);
1258 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1263 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1264 if (bufp->flags & CM_BUF_WAITING) {
1265 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1266 osi_Wakeup((LONG_PTR) &bufp);
1273 /* now update the buffer pointer */
1274 if (flags & CM_SCACHESYNC_STOREDATA) {
1276 /* ensure that the buffer isn't already in the I/O list */
1277 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1278 tbufp = osi_GetQData(qdp);
1283 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1288 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1289 if (bufp->flags & CM_BUF_WAITING) {
1290 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1291 osi_Wakeup((LONG_PTR) &bufp);
1298 /* and wakeup anyone who is waiting */
1299 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1300 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1301 osi_Wakeup((LONG_PTR) &scp->flags);
1305 /* merge in a response from an RPC. The scp must be locked, and the callback
1308 * Don't overwrite any status info that is dirty, since we could have a store
1309 * operation (such as store data) that merges some info in, and we don't want
1310 * to lose the local updates. Typically, there aren't many updates we do
1311 * locally, anyway, probably only mtime.
1313 * There is probably a bug in here where a chmod (which doesn't change
1314 * serverModTime) that occurs between two fetches, both of whose responses are
1315 * handled after the callback breaking is done, but only one of whose calls
1316 * started before that, can cause old info to be merged from the first call.
1318 void cm_MergeStatus(cm_scache_t *dscp,
1319 cm_scache_t *scp, AFSFetchStatus *statusp,
1320 AFSVolSync *volsyncp,
1321 cm_user_t *userp, afs_uint32 flags)
1323 // yj: i want to create some fake status for the /afs directory and the
1324 // entries under that directory
1325 #ifdef AFS_FREELANCE_CLIENT
1326 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1327 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1328 statusp->InterfaceVersion = 0x1;
1329 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1330 statusp->LinkCount = scp->linkCount;
1331 statusp->Length = cm_fakeDirSize;
1332 statusp->Length_hi = 0;
1333 statusp->DataVersion = cm_data.fakeDirVersion;
1334 statusp->Author = 0x1;
1335 statusp->Owner = 0x0;
1336 statusp->CallerAccess = 0x9;
1337 statusp->AnonymousAccess = 0x9;
1338 statusp->UnixModeBits = 0777;
1339 statusp->ParentVnode = 0x1;
1340 statusp->ParentUnique = 0x1;
1341 statusp->ResidencyMask = 0;
1342 statusp->ClientModTime = FakeFreelanceModTime;
1343 statusp->ServerModTime = FakeFreelanceModTime;
1345 statusp->SyncCounter = 0;
1346 statusp->dataVersionHigh = 0;
1347 statusp->errorCode = 0;
1349 #endif /* AFS_FREELANCE_CLIENT */
1351 if (statusp->errorCode != 0) {
1352 scp->flags |= CM_SCACHEFLAG_EACCESS;
1353 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1355 scp->fileType = 0; /* unknown */
1357 scp->serverModTime = 0;
1358 scp->clientModTime = 0;
1359 scp->length.LowPart = 0;
1360 scp->length.HighPart = 0;
1361 scp->serverLength.LowPart = 0;
1362 scp->serverLength.HighPart = 0;
1366 scp->unixModeBits = 0;
1368 scp->dataVersion = 0;
1371 scp->parentVnode = dscp->fid.vnode;
1372 scp->parentUnique = dscp->fid.unique;
1374 scp->parentVnode = 0;
1375 scp->parentUnique = 0;
1379 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1382 if (!(flags & CM_MERGEFLAG_FORCE)
1383 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1384 struct cm_cell *cellp;
1386 cellp = cm_FindCellByID(scp->fid.cell);
1387 if (scp->cbServerp) {
1388 struct cm_volume *volp = NULL;
1390 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1391 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1392 osi_Log2(afsd_logp, "old data from server %x volume %s",
1393 scp->cbServerp->addr.sin_addr.s_addr,
1394 volp ? volp->namep : "(unknown)");
1398 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1399 scp, scp->dataVersion, statusp->DataVersion);
1400 /* we have a number of data fetch/store operations running
1401 * concurrently, and we can tell which one executed last at the
1402 * server by its mtime.
1403 * Choose the one with the largest mtime, and ignore the rest.
1405 * These concurrent calls are incompatible with setting the
1406 * mtime, so we won't have a locally changed mtime here.
1408 * We could also have ACL info for a different user than usual,
1409 * in which case we have to do that part of the merge, anyway.
1410 * We won't have to worry about the info being old, since we
1411 * won't have concurrent calls
1412 * that change file status running from this machine.
1414 * Added 3/17/98: if we see data version regression on an RO
1415 * file, it's probably due to a server holding an out-of-date
1416 * replica, rather than to concurrent RPC's. Failures to
1417 * release replicas are now flagged by the volserver, but only
1418 * since AFS 3.4 5.22, so there are plenty of clients getting
1419 * out-of-date replicas out there.
1421 * If we discover an out-of-date replica, by this time it's too
1422 * late to go to another server and retry. Also, we can't
1423 * reject the merge, because then there is no way for
1424 * GetAccess to do its work, and the caller gets into an
1425 * infinite loop. So we just grin and bear it.
1427 if (!(scp->flags & CM_SCACHEFLAG_RO))
1431 scp->serverModTime = statusp->ServerModTime;
1433 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1434 scp->clientModTime = statusp->ClientModTime;
1436 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1437 scp->length.LowPart = statusp->Length;
1438 scp->length.HighPart = statusp->Length_hi;
1441 scp->serverLength.LowPart = statusp->Length;
1442 scp->serverLength.HighPart = statusp->Length_hi;
1444 scp->linkCount = statusp->LinkCount;
1445 scp->owner = statusp->Owner;
1446 scp->group = statusp->Group;
1447 scp->unixModeBits = statusp->UnixModeBits & 07777;
1449 if (statusp->FileType == File)
1450 scp->fileType = CM_SCACHETYPE_FILE;
1451 else if (statusp->FileType == Directory)
1452 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1453 else if (statusp->FileType == SymbolicLink) {
1454 if ((scp->unixModeBits & 0111) == 0)
1455 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1457 scp->fileType = CM_SCACHETYPE_SYMLINK;
1460 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1461 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1463 /* and other stuff */
1464 scp->parentVnode = statusp->ParentVnode;
1465 scp->parentUnique = statusp->ParentUnique;
1467 /* and merge in the private acl cache info, if this is more than the public
1468 * info; merge in the public stuff in any case.
1470 scp->anyAccess = statusp->AnonymousAccess;
1472 if (userp != NULL) {
1473 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1476 if ((flags & CM_MERGEFLAG_STOREDATA) &&
1477 statusp->DataVersion - scp->dataVersion == 1) {
1480 for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1482 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1483 bp->dataVersion == scp->dataVersion)
1484 bp->dataVersion = statusp->DataVersion;
1488 scp->dataVersion = statusp->DataVersion;
1491 /* note that our stat cache info is incorrect, so force us eventually
1492 * to stat the file again. There may be dirty data associated with
1493 * this vnode, and we want to preserve that information.
1495 * This function works by simply simulating a loss of the callback.
1497 * This function must be called with the scache locked.
1499 void cm_DiscardSCache(cm_scache_t *scp)
1501 lock_AssertMutex(&scp->mx);
1502 if (scp->cbServerp) {
1503 cm_PutServer(scp->cbServerp);
1504 scp->cbServerp = NULL;
1507 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1508 cm_dnlcPurgedp(scp);
1509 cm_dnlcPurgevp(scp);
1510 cm_FreeAllACLEnts(scp);
1512 /* Force mount points and symlinks to be re-evaluated */
1513 scp->mountPointStringp[0] = '\0';
1516 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1518 afsFidp->Volume = fidp->volume;
1519 afsFidp->Vnode = fidp->vnode;
1520 afsFidp->Unique = fidp->unique;
1523 #ifdef DEBUG_REFCOUNT
1524 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1526 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1529 osi_assert(scp != 0);
1531 #ifdef DEBUG_REFCOUNT
1532 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1533 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1537 #ifdef DEBUG_REFCOUNT
1538 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1540 void cm_HoldSCache(cm_scache_t *scp)
1543 osi_assert(scp != 0);
1544 lock_ObtainWrite(&cm_scacheLock);
1546 #ifdef DEBUG_REFCOUNT
1547 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1548 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1550 lock_ReleaseWrite(&cm_scacheLock);
1553 #ifdef DEBUG_REFCOUNT
1554 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1556 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1559 osi_assert(scp != NULL);
1560 if (scp->refCount == 0)
1561 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1562 osi_assert(scp->refCount-- >= 0);
1563 #ifdef DEBUG_REFCOUNT
1564 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1565 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1569 #ifdef DEBUG_REFCOUNT
1570 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1572 void cm_ReleaseSCache(cm_scache_t *scp)
1575 osi_assert(scp != NULL);
1576 lock_ObtainWrite(&cm_scacheLock);
1577 if (scp->refCount == 0)
1578 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1579 osi_assert(scp->refCount != 0);
1581 #ifdef DEBUG_REFCOUNT
1582 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1583 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1585 lock_ReleaseWrite(&cm_scacheLock);
1588 /* just look for the scp entry to get filetype */
1589 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1590 int cm_FindFileType(cm_fid_t *fidp)
1595 hash = CM_SCACHE_HASH(fidp);
1597 osi_assert(fidp->cell != 0);
1599 lock_ObtainWrite(&cm_scacheLock);
1600 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1601 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1602 lock_ReleaseWrite(&cm_scacheLock);
1603 return scp->fileType;
1606 lock_ReleaseWrite(&cm_scacheLock);
1610 /* dump all scp's that have reference count > 0 to a file.
1611 * cookie is used to identify this batch for easy parsing,
1612 * and it a string provided by a caller
1614 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1622 lock_ObtainRead(&cm_scacheLock);
1624 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1625 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1627 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1629 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",
1630 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1631 scp->volp, scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp, scp->flags,
1632 (unsigned long)scp->cbExpires, scp->refCount);
1633 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1636 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1637 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1638 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1639 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1641 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1643 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1645 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1646 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1647 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1651 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1652 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1655 lock_ReleaseRead(&cm_scacheLock);