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;
168 /* discard callback */
169 if (scp->cbServerp) {
170 cm_PutServer(scp->cbServerp);
171 scp->cbServerp = NULL;
175 /* remove from dnlc */
179 /* discard cached status; if non-zero, Close
180 * tried to store this to server but failed */
183 /* drop held volume ref */
185 cm_PutVolume(scp->volp);
189 /* discard symlink info */
190 scp->mountPointStringp[0] = '\0';
191 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
192 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
194 /* reset locking info */
195 scp->fileLocksH = NULL;
196 scp->fileLocksT = NULL;
197 scp->serverLock = (-1);
198 scp->exclusiveLocks = 0;
199 scp->sharedLocks = 0;
201 /* not locked, but there can be no references to this guy
202 * while we hold the global refcount lock.
204 cm_FreeAllACLEnts(scp);
209 /* called with cm_scacheLock write-locked; find a vnode to recycle.
210 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
212 cm_scache_t *cm_GetNewSCache(void)
218 /* first pass - look for deleted objects */
219 for ( scp = cm_data.scacheLRULastp;
221 scp = (cm_scache_t *) osi_QPrev(&scp->q))
223 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
225 if (scp->refCount == 0) {
226 if (scp->flags & CM_SCACHEFLAG_DELETED) {
227 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
228 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
230 /* we found an entry, so return it */
231 /* now remove from the LRU queue and put it back at the
232 * head of the LRU queue.
234 cm_AdjustScacheLRU(scp);
239 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
240 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
241 /* we found an entry, so return it */
242 /* now remove from the LRU queue and put it back at the
243 * head of the LRU queue.
245 cm_AdjustScacheLRU(scp);
252 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
255 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
256 /* There were no deleted scache objects that we could use. Try to find
257 * one that simply hasn't been used in a while.
259 for ( scp = cm_data.scacheLRULastp;
261 scp = (cm_scache_t *) osi_QPrev(&scp->q))
263 /* It is possible for the refCount to be zero and for there still
264 * to be outstanding dirty buffers. If there are dirty buffers,
265 * we must not recycle the scp. */
266 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
267 if (!buf_DirtyBuffersExist(&scp->fid)) {
268 if (!cm_RecycleSCache(scp, 0)) {
269 /* we found an entry, so return it */
270 /* now remove from the LRU queue and put it back at the
271 * head of the LRU queue.
273 cm_AdjustScacheLRU(scp);
279 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
283 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
288 /* if we get here, we should allocate a new scache entry. We either are below
289 * quota or we have a leak and need to allocate a new one to avoid panicing.
291 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
292 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
293 memset(scp, 0, sizeof(cm_scache_t));
294 scp->magic = CM_SCACHE_MAGIC;
295 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
296 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
297 scp->serverLock = -1;
299 /* and put it in the LRU queue */
300 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
301 if (!cm_data.scacheLRULastp)
302 cm_data.scacheLRULastp = scp;
303 cm_data.currentSCaches++;
304 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
306 scp->allNextp = cm_data.allSCachesp;
307 cm_data.allSCachesp = scp;
311 /* like strcmp, only for fids */
312 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
314 if (ap->vnode != bp->vnode)
316 if (ap->volume != bp->volume)
318 if (ap->unique != bp->unique)
320 if (ap->cell != bp->cell)
325 void cm_fakeSCacheInit(int newFile)
328 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
329 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
330 /* can leave clientModTime at 0 */
331 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
332 cm_data.fakeSCache.unixModeBits = 0777;
333 cm_data.fakeSCache.length.LowPart = 1000;
334 cm_data.fakeSCache.linkCount = 1;
335 cm_data.fakeSCache.refCount = 1;
337 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
341 cm_ValidateSCache(void)
343 cm_scache_t * scp, *lscp;
346 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
347 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
348 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
349 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
353 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
355 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
356 if (scp->magic != CM_SCACHE_MAGIC) {
357 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
358 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
361 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
362 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
363 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
366 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
367 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
368 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
371 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
372 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
373 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
376 if (i > cm_data.currentSCaches ) {
377 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
378 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
381 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
382 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
383 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
388 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
389 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
390 if (scp->magic != CM_SCACHE_MAGIC) {
391 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
392 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
395 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
396 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
397 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
400 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
401 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
402 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
405 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
406 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
407 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
410 if (i > cm_data.currentSCaches ) {
411 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
412 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
415 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
416 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
417 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
422 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
423 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
424 if (scp->magic != CM_SCACHE_MAGIC) {
425 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
426 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
429 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
430 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
431 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
434 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
435 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
436 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
439 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
440 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
441 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
447 return cm_dnlcValidate();
451 cm_ShutdownSCache(void)
455 for ( scp = cm_data.allSCachesp; scp;
456 scp = scp->allNextp ) {
457 if (scp->randomACLp) {
458 lock_ObtainMutex(&scp->mx);
459 cm_FreeAllACLEnts(scp);
460 lock_ReleaseMutex(&scp->mx);
462 lock_FinalizeMutex(&scp->mx);
463 lock_FinalizeRWLock(&scp->bufCreateLock);
466 return cm_dnlcShutdown();
469 void cm_InitSCache(int newFile, long maxSCaches)
471 static osi_once_t once;
473 if (osi_Once(&once)) {
474 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
476 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
477 cm_data.allSCachesp = NULL;
478 cm_data.currentSCaches = 0;
479 cm_data.maxSCaches = maxSCaches;
480 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
484 for ( scp = cm_data.allSCachesp; scp;
485 scp = scp->allNextp ) {
486 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
487 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
489 scp->cbServerp = NULL;
491 scp->fileLocksH = NULL;
492 scp->fileLocksT = NULL;
493 scp->serverLock = (-1);
494 scp->lastRefreshCycle = 0;
495 scp->exclusiveLocks = 0;
496 scp->sharedLocks = 0;
502 scp->flags &= ~CM_SCACHEFLAG_WAITING;
505 cm_allFileLocks = NULL;
506 cm_freeFileLocks = NULL;
507 cm_lockRefreshCycle = 0;
508 cm_fakeSCacheInit(newFile);
509 cm_dnlcInit(newFile);
514 /* version that doesn't bother creating the entry if we don't find it */
515 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
520 hash = CM_SCACHE_HASH(fidp);
522 if (fidp->cell == 0) {
526 lock_ObtainWrite(&cm_scacheLock);
527 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
528 if (cm_FidCmp(fidp, &scp->fid) == 0) {
529 cm_HoldSCacheNoLock(scp);
530 cm_AdjustScacheLRU(scp);
531 lock_ReleaseWrite(&cm_scacheLock);
535 lock_ReleaseWrite(&cm_scacheLock);
539 #ifdef DEBUG_REFCOUNT
540 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
541 cm_req_t *reqp, char * file, long line)
543 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
550 cm_volume_t *volp = NULL;
553 int special; // yj: boolean variable to test if file is on root.afs
555 extern cm_fid_t cm_rootFid;
557 hash = CM_SCACHE_HASH(fidp);
559 osi_assert(fidp->cell != 0);
561 if (fidp->cell== cm_data.rootFid.cell &&
562 fidp->volume==cm_data.rootFid.volume &&
563 fidp->vnode==0x0 && fidp->unique==0x0)
565 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
568 // yj: check if we have the scp, if so, we don't need
569 // to do anything else
570 lock_ObtainWrite(&cm_scacheLock);
571 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
572 if (cm_FidCmp(fidp, &scp->fid) == 0) {
573 #ifdef DEBUG_REFCOUNT
574 afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
575 osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
577 cm_HoldSCacheNoLock(scp);
579 cm_AdjustScacheLRU(scp);
580 lock_ReleaseWrite(&cm_scacheLock);
585 // yj: when we get here, it means we don't have an scp
586 // so we need to either load it or fake it, depending
587 // on whether the file is "special", see below.
589 // yj: if we're trying to get an scp for a file that's
590 // on root.afs of homecell, we want to handle it specially
591 // because we have to fill in the status stuff 'coz we
592 // don't want trybulkstat to fill it in for us
593 #ifdef AFS_FREELANCE_CLIENT
594 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
595 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
596 !(fidp->vnode==0x1 && fidp->unique==0x1));
597 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
598 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
599 fidp->vnode==0x1 && fidp->unique==0x1);
600 if (cm_freelanceEnabled && isRoot) {
601 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
602 /* freelance: if we are trying to get the root scp for the first
603 * time, we will just put in a place holder entry.
608 if (cm_freelanceEnabled && special) {
609 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
610 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
611 lock_ObtainMutex(&cm_Freelance_Lock);
612 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
613 lock_ReleaseMutex(&cm_Freelance_Lock);
617 scp = cm_GetNewSCache();
619 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
620 lock_ReleaseWrite(&cm_scacheLock);
621 return CM_ERROR_WOULDBLOCK;
624 #if not_too_dangerous
625 /* dropping the cm_scacheLock allows more than one thread
626 * to obtain the same cm_scache_t from the LRU list. Since
627 * the refCount is known to be zero at this point we have to
628 * assume that no one else is using the one this is returned.
630 lock_ReleaseWrite(&cm_scacheLock);
631 lock_ObtainMutex(&scp->mx);
632 lock_ObtainWrite(&cm_scacheLock);
635 scp->volp = cm_data.rootSCachep->volp;
636 cm_GetVolume(scp->volp); /* grab an additional reference */
637 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
638 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
639 scp->dotdotFid.unique=1;
640 scp->dotdotFid.vnode=1;
641 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
642 scp->nextp=cm_data.scacheHashTablep[hash];
643 cm_data.scacheHashTablep[hash]=scp;
644 scp->flags |= CM_SCACHEFLAG_INHASH;
646 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
647 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
648 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
650 scp->fileType = CM_SCACHETYPE_INVALID;
652 lock_ObtainMutex(&cm_Freelance_Lock);
653 scp->length.LowPart = (DWORD)strlen(mp)+4;
654 scp->length.HighPart = 0;
655 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
656 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
657 lock_ReleaseMutex(&cm_Freelance_Lock);
660 scp->unixModeBits=0777;
661 scp->clientModTime=FakeFreelanceModTime;
662 scp->serverModTime=FakeFreelanceModTime;
663 scp->parentUnique = 0x1;
664 scp->parentVnode=0x1;
666 scp->dataVersion=cm_data.fakeDirVersion;
667 scp->lockDataVersion=-1; /* no lock yet */
668 #if not_too_dangerous
669 lock_ReleaseMutex(&scp->mx);
672 lock_ReleaseWrite(&cm_scacheLock);
673 #ifdef DEBUG_REFCOUNT
674 afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
675 osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
680 #endif /* AFS_FREELANCE_CLIENT */
682 /* otherwise, we need to find the volume */
683 if (!cm_freelanceEnabled || !isRoot) {
684 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
685 cellp = cm_FindCellByID(fidp->cell);
687 return CM_ERROR_NOSUCHCELL;
689 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
692 lock_ObtainWrite(&cm_scacheLock);
695 /* otherwise, we have the volume, now reverify that the scp doesn't
696 * exist, and proceed.
698 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
699 if (cm_FidCmp(fidp, &scp->fid) == 0) {
700 #ifdef DEBUG_REFCOUNT
701 afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
702 osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
704 cm_HoldSCacheNoLock(scp);
705 osi_assert(scp->volp == volp);
706 cm_AdjustScacheLRU(scp);
707 lock_ReleaseWrite(&cm_scacheLock);
715 /* now, if we don't have the fid, recycle something */
716 scp = cm_GetNewSCache();
718 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
719 lock_ReleaseWrite(&cm_scacheLock);
722 return CM_ERROR_WOULDBLOCK;
724 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
726 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
728 #if not_too_dangerous
729 /* dropping the cm_scacheLock allows more than one thread
730 * to obtain the same cm_scache_t from the LRU list. Since
731 * the refCount is known to be zero at this point we have to
732 * assume that no one else is using the one this is returned.
734 lock_ReleaseWrite(&cm_scacheLock);
735 lock_ObtainMutex(&scp->mx);
736 lock_ObtainWrite(&cm_scacheLock);
739 scp->volp = volp; /* a held reference */
741 if (!cm_freelanceEnabled || !isRoot) {
742 /* if this scache entry represents a volume root then we need
743 * to copy the dotdotFipd from the volume structure where the
744 * "master" copy is stored (defect 11489)
746 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
747 scp->dotdotFid = volp->dotdotFid;
750 if (volp->ro.ID == fidp->volume)
751 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
752 else if (volp->bk.ID == fidp->volume)
753 scp->flags |= CM_SCACHEFLAG_RO;
755 scp->nextp = cm_data.scacheHashTablep[hash];
756 cm_data.scacheHashTablep[hash] = scp;
757 scp->flags |= CM_SCACHEFLAG_INHASH;
759 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
760 #if not_too_dangerous
761 lock_ReleaseMutex(&scp->mx);
764 /* XXX - The following fields in the cm_scache are
770 lock_ReleaseWrite(&cm_scacheLock);
772 /* now we have a held scache entry; just return it */
774 #ifdef DEBUG_REFCOUNT
775 afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
776 osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
781 /* Returns a held reference to the scache's parent
783 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
788 cm_scache_t * pscp = NULL;
790 lock_ObtainRead(&cm_scacheLock);
791 parent_fid = scp->fid;
792 parent_fid.vnode = scp->parentVnode;
793 parent_fid.unique = scp->parentUnique;
795 if (cm_FidCmp(&scp->fid, &parent_fid)) {
796 i = CM_SCACHE_HASH(&parent_fid);
797 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
798 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
799 cm_HoldSCacheNoLock(pscp);
805 lock_ReleaseRead(&cm_scacheLock);
810 /* synchronize a fetch, store, read, write, fetch status or store status.
811 * Called with scache mutex held, and returns with it held, but temporarily
812 * drops it during the fetch.
814 * At most one flag can be on in flags, if this is an RPC request.
816 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
818 * There are a lot of weird restrictions here; here's an attempt to explain the
819 * rationale for the concurrency restrictions implemented in this function.
821 * First, although the file server will break callbacks when *another* machine
822 * modifies a file or status block, the client itself is responsible for
823 * concurrency control on its own requests. Callback breaking events are rare,
824 * and simply invalidate any concurrent new status info.
826 * In the absence of callback breaking messages, we need to know how to
827 * synchronize incoming responses describing updates to files. We synchronize
828 * operations that update the data version by comparing the data versions.
829 * However, updates that do not update the data, but only the status, can't be
830 * synchronized with fetches or stores, since there's nothing to compare
831 * to tell which operation executed first at the server.
833 * Thus, we can allow multiple ops that change file data, or dir data, and
834 * fetches. However, status storing ops have to be done serially.
836 * Furthermore, certain data-changing ops are incompatible: we can't read or
837 * write a buffer while doing a truncate. We can't read and write the same
838 * buffer at the same time, or write while fetching or storing, or read while
839 * fetching a buffer (this may change). We can't fetch and store at the same
842 * With respect to status, we can't read and write at the same time, read while
843 * fetching, write while fetching or storing, or fetch and store at the same time.
845 * We can't allow a get callback RPC to run in concurrently with something that
846 * will return updated status, since we could start a call, have the server
847 * return status, have another machine make an update to the status (which
848 * doesn't change serverModTime), have the original machine get a new callback,
849 * and then have the original machine merge in the early, old info from the
850 * first call. At this point, the easiest way to avoid this problem is to have
851 * getcallback calls conflict with all others for the same vnode. Other calls
852 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
853 * vnode must be careful not to merge in their status unless they have obtained
854 * a callback from the start of their call.
857 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
858 * Each such RPC passes a FileLength parameter, which the server uses to do
859 * pre-truncation if necessary. So if two RPC's are processed out of order at
860 * the server, the one with the smaller FileLength will be processed last,
861 * possibly resulting in a bogus truncation. The simplest way to avoid this
862 * is to serialize all StoreData RPC's. This is the reason we defined
863 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
865 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
866 afs_uint32 rights, afs_uint32 flags)
868 osi_queueData_t *qdp;
871 afs_uint32 outRights;
873 afs_uint32 sleep_scp_flags = 0;
874 afs_uint32 sleep_buf_cmflags = 0;
875 afs_uint32 sleep_scp_bufs = 0;
877 /* lookup this first */
878 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
881 osi_assert(bufp->refCount > 0);
884 /* Do the access check. Now we don't really do the access check
885 * atomically, since the caller doesn't expect the parent dir to be
886 * returned locked, and that is what we'd have to do to prevent a
887 * callback breaking message on the parent due to a setacl call from
888 * being processed while we're running. So, instead, we check things
889 * here, and if things look fine with the access, we proceed to finish
890 * the rest of this check. Sort of a hack, but probably good enough.
894 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
895 /* if we're bringing in a new status block, ensure that
896 * we aren't already doing so, and that no one is
897 * changing the status concurrently, either. We need
898 * to do this, even if the status is of a different
899 * type, since we don't have the ability to figure out,
900 * in the AFS 3 protocols, which status-changing
901 * operation ran first, or even which order a read and
902 * a write occurred in.
904 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
905 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
906 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
910 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
911 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
912 /* if we're going to make an RPC to change the status, make sure
913 * that no one is bringing in or sending out the status.
915 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
916 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
917 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
920 if (scp->bufReadsp || scp->bufWritesp) {
921 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
925 if (flags & CM_SCACHESYNC_FETCHDATA) {
926 /* if we're bringing in a new chunk of data, make sure that
927 * nothing is happening to that chunk, and that we aren't
928 * changing the basic file status info, either.
930 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
931 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
932 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
935 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
936 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
940 if (flags & CM_SCACHESYNC_STOREDATA) {
941 /* same as fetch data */
942 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
943 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
944 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
947 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
948 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp, bufp);
953 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
954 /* Don't allow concurrent StoreData RPC's */
955 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
956 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
961 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
962 /* Don't allow more than one BKG store request */
963 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
964 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
969 if (flags & CM_SCACHESYNC_LOCK) {
970 /* Don't allow concurrent fiddling with lock lists */
971 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
972 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
977 /* now the operations that don't correspond to making RPCs */
978 if (flags & CM_SCACHESYNC_GETSTATUS) {
979 /* we can use the status that's here, if we're not
980 * bringing in new status.
982 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
983 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
987 if (flags & CM_SCACHESYNC_SETSTATUS) {
988 /* we can make a change to the local status, as long as
989 * the status isn't changing now.
991 * If we're fetching or storing a chunk of data, we can
992 * change the status locally, since the fetch/store
993 * operations don't change any of the data that we're
996 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
997 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1001 if (flags & CM_SCACHESYNC_READ) {
1002 /* we're going to read the data, make sure that the
1003 * status is available, and that the data is here. It
1004 * is OK to read while storing the data back.
1006 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1007 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1010 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1011 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1015 if (flags & CM_SCACHESYNC_WRITE) {
1016 /* don't write unless the status is stable and the chunk
1019 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1020 | CM_SCACHEFLAG_SIZESTORING)) {
1021 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1024 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
1025 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
1030 // yj: modified this so that callback only checked if we're
1031 // not checking something on /afs
1032 /* fix the conditional to match the one in cm_HaveCallback */
1033 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1034 #ifdef AFS_FREELANCE_CLIENT
1035 && (!cm_freelanceEnabled ||
1036 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1037 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1038 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1039 cm_fakeDirCallback < 2)
1040 #endif /* AFS_FREELANCE_CLIENT */
1042 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1043 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1046 lock_ReleaseMutex(&bufp->mx);
1047 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1049 lock_ReleaseMutex(&scp->mx);
1050 lock_ObtainMutex(&bufp->mx);
1051 lock_ObtainMutex(&scp->mx);
1055 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1061 /* can't check access rights without a callback */
1062 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1064 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1065 return CM_ERROR_READONLY;
1067 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1068 if (~outRights & rights)
1069 return CM_ERROR_NOACCESS;
1072 /* we don't know the required access rights */
1073 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1074 code = cm_GetAccessRights(scp, userp, reqp);
1076 lock_ReleaseMutex(&scp->mx);
1077 lock_ObtainMutex(&bufp->mx);
1078 lock_ObtainMutex(&scp->mx);
1086 /* if we get here, we're happy */
1090 /* first check if we're not supposed to wait: fail
1091 * in this case, returning with everything still locked.
1093 if (flags & CM_SCACHESYNC_NOWAIT)
1094 return CM_ERROR_WOULDBLOCK;
1096 sleep_scp_flags = scp->flags; /* so we know why we slept */
1097 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1098 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1100 /* wait here, then try again */
1101 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1102 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1104 scp->waitRequests++;
1105 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1106 scp, scp->waitCount, scp->waitRequests);
1108 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1109 scp->flags |= CM_SCACHEFLAG_WAITING;
1110 scp->waitCount = scp->waitRequests = 1;
1113 lock_ReleaseMutex(&bufp->mx);
1114 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1116 smb_UpdateServerPriority();
1119 lock_ObtainMutex(&bufp->mx);
1120 lock_ObtainMutex(&scp->mx);
1122 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1123 scp, scp->waitCount, scp->waitRequests);
1124 if (scp->waitCount == 0) {
1125 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1126 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1127 scp->waitRequests = 0;
1129 } /* big while loop */
1131 /* now, update the recorded state for RPC-type calls */
1132 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1133 scp->flags |= CM_SCACHEFLAG_FETCHING;
1134 if (flags & CM_SCACHESYNC_STORESTATUS)
1135 scp->flags |= CM_SCACHEFLAG_STORING;
1136 if (flags & CM_SCACHESYNC_STORESIZE)
1137 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1138 if (flags & CM_SCACHESYNC_GETCALLBACK)
1139 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1140 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1141 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1142 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1143 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1144 if (flags & CM_SCACHESYNC_LOCK)
1145 scp->flags |= CM_SCACHEFLAG_LOCKING;
1147 /* now update the buffer pointer */
1148 if (flags & CM_SCACHESYNC_FETCHDATA) {
1149 /* ensure that the buffer isn't already in the I/O list */
1151 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1152 tbufp = osi_GetQData(qdp);
1153 osi_assert(tbufp != bufp);
1157 /* queue a held reference to the buffer in the "reading" I/O list */
1158 qdp = osi_QDAlloc();
1159 osi_SetQData(qdp, bufp);
1162 bufp->cmFlags |= CM_BUF_CMFETCHING;
1164 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1167 if (flags & CM_SCACHESYNC_STOREDATA) {
1168 /* ensure that the buffer isn't already in the I/O list */
1170 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1171 tbufp = osi_GetQData(qdp);
1172 osi_assert(tbufp != bufp);
1176 /* queue a held reference to the buffer in the "writing" I/O list */
1177 qdp = osi_QDAlloc();
1178 osi_SetQData(qdp, bufp);
1181 bufp->cmFlags |= CM_BUF_CMSTORING;
1183 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1189 /* for those syncops that setup for RPCs.
1190 * Called with scache locked.
1192 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1194 osi_queueData_t *qdp;
1197 lock_AssertMutex(&scp->mx);
1199 /* now, update the recorded state for RPC-type calls */
1200 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1201 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1202 if (flags & CM_SCACHESYNC_STORESTATUS)
1203 scp->flags &= ~CM_SCACHEFLAG_STORING;
1204 if (flags & CM_SCACHESYNC_STORESIZE)
1205 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1206 if (flags & CM_SCACHESYNC_GETCALLBACK)
1207 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1208 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1209 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1210 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1211 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1212 if (flags & CM_SCACHESYNC_LOCK)
1213 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1215 /* now update the buffer pointer */
1216 if (flags & CM_SCACHESYNC_FETCHDATA) {
1219 /* ensure that the buffer isn't already in the I/O list */
1220 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1221 tbufp = osi_GetQData(qdp);
1226 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1231 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1232 if (bufp->flags & CM_BUF_WAITING) {
1233 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1234 osi_Wakeup((LONG_PTR) &bufp);
1241 /* now update the buffer pointer */
1242 if (flags & CM_SCACHESYNC_STOREDATA) {
1244 /* ensure that the buffer isn't already in the I/O list */
1245 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1246 tbufp = osi_GetQData(qdp);
1251 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1256 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1257 if (bufp->flags & CM_BUF_WAITING) {
1258 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1259 osi_Wakeup((LONG_PTR) &bufp);
1266 /* and wakeup anyone who is waiting */
1267 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1268 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1269 osi_Wakeup((LONG_PTR) &scp->flags);
1273 /* merge in a response from an RPC. The scp must be locked, and the callback
1276 * Don't overwrite any status info that is dirty, since we could have a store
1277 * operation (such as store data) that merges some info in, and we don't want
1278 * to lose the local updates. Typically, there aren't many updates we do
1279 * locally, anyway, probably only mtime.
1281 * There is probably a bug in here where a chmod (which doesn't change
1282 * serverModTime) that occurs between two fetches, both of whose responses are
1283 * handled after the callback breaking is done, but only one of whose calls
1284 * started before that, can cause old info to be merged from the first call.
1286 void cm_MergeStatus(cm_scache_t *dscp,
1287 cm_scache_t *scp, AFSFetchStatus *statusp,
1288 AFSVolSync *volsyncp,
1289 cm_user_t *userp, afs_uint32 flags)
1291 // yj: i want to create some fake status for the /afs directory and the
1292 // entries under that directory
1293 #ifdef AFS_FREELANCE_CLIENT
1294 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1295 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1296 statusp->InterfaceVersion = 0x1;
1297 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1298 statusp->LinkCount = scp->linkCount;
1299 statusp->Length = cm_fakeDirSize;
1300 statusp->Length_hi = 0;
1301 statusp->DataVersion = cm_data.fakeDirVersion;
1302 statusp->Author = 0x1;
1303 statusp->Owner = 0x0;
1304 statusp->CallerAccess = 0x9;
1305 statusp->AnonymousAccess = 0x9;
1306 statusp->UnixModeBits = 0777;
1307 statusp->ParentVnode = 0x1;
1308 statusp->ParentUnique = 0x1;
1309 statusp->ResidencyMask = 0;
1310 statusp->ClientModTime = FakeFreelanceModTime;
1311 statusp->ServerModTime = FakeFreelanceModTime;
1313 statusp->SyncCounter = 0;
1314 statusp->dataVersionHigh = 0;
1315 statusp->errorCode = 0;
1317 #endif /* AFS_FREELANCE_CLIENT */
1319 if (statusp->errorCode != 0) {
1320 scp->flags |= CM_SCACHEFLAG_EACCESS;
1321 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1323 scp->fileType = 0; /* unknown */
1325 scp->serverModTime = 0;
1326 scp->clientModTime = 0;
1327 scp->length.LowPart = 0;
1328 scp->length.HighPart = 0;
1329 scp->serverLength.LowPart = 0;
1330 scp->serverLength.HighPart = 0;
1334 scp->unixModeBits = 0;
1336 scp->dataVersion = 0;
1339 scp->parentVnode = dscp->fid.vnode;
1340 scp->parentUnique = dscp->fid.unique;
1342 scp->parentVnode = 0;
1343 scp->parentUnique = 0;
1347 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1350 if (!(flags & CM_MERGEFLAG_FORCE)
1351 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1352 struct cm_cell *cellp;
1354 cellp = cm_FindCellByID(scp->fid.cell);
1355 if (scp->cbServerp) {
1356 struct cm_volume *volp = NULL;
1358 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1359 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1360 osi_Log2(afsd_logp, "old data from server %x volume %s",
1361 scp->cbServerp->addr.sin_addr.s_addr,
1362 volp ? volp->namep : "(unknown)");
1366 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1367 scp, scp->dataVersion, statusp->DataVersion);
1368 /* we have a number of data fetch/store operations running
1369 * concurrently, and we can tell which one executed last at the
1370 * server by its mtime.
1371 * Choose the one with the largest mtime, and ignore the rest.
1373 * These concurrent calls are incompatible with setting the
1374 * mtime, so we won't have a locally changed mtime here.
1376 * We could also have ACL info for a different user than usual,
1377 * in which case we have to do that part of the merge, anyway.
1378 * We won't have to worry about the info being old, since we
1379 * won't have concurrent calls
1380 * that change file status running from this machine.
1382 * Added 3/17/98: if we see data version regression on an RO
1383 * file, it's probably due to a server holding an out-of-date
1384 * replica, rather than to concurrent RPC's. Failures to
1385 * release replicas are now flagged by the volserver, but only
1386 * since AFS 3.4 5.22, so there are plenty of clients getting
1387 * out-of-date replicas out there.
1389 * If we discover an out-of-date replica, by this time it's too
1390 * late to go to another server and retry. Also, we can't
1391 * reject the merge, because then there is no way for
1392 * GetAccess to do its work, and the caller gets into an
1393 * infinite loop. So we just grin and bear it.
1395 if (!(scp->flags & CM_SCACHEFLAG_RO))
1399 scp->serverModTime = statusp->ServerModTime;
1401 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1402 scp->clientModTime = statusp->ClientModTime;
1404 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1405 scp->length.LowPart = statusp->Length;
1406 scp->length.HighPart = statusp->Length_hi;
1409 scp->serverLength.LowPart = statusp->Length;
1410 scp->serverLength.HighPart = statusp->Length_hi;
1412 scp->linkCount = statusp->LinkCount;
1413 scp->owner = statusp->Owner;
1414 scp->group = statusp->Group;
1415 scp->unixModeBits = statusp->UnixModeBits & 07777;
1417 if (statusp->FileType == File)
1418 scp->fileType = CM_SCACHETYPE_FILE;
1419 else if (statusp->FileType == Directory)
1420 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1421 else if (statusp->FileType == SymbolicLink) {
1422 if ((scp->unixModeBits & 0111) == 0)
1423 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1425 scp->fileType = CM_SCACHETYPE_SYMLINK;
1428 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1429 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1431 /* and other stuff */
1432 scp->parentVnode = statusp->ParentVnode;
1433 scp->parentUnique = statusp->ParentUnique;
1435 /* and merge in the private acl cache info, if this is more than the public
1436 * info; merge in the public stuff in any case.
1438 scp->anyAccess = statusp->AnonymousAccess;
1440 if (userp != NULL) {
1441 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1444 if ((flags & CM_MERGEFLAG_STOREDATA) &&
1445 statusp->DataVersion - scp->dataVersion == 1) {
1448 for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1450 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1451 bp->dataVersion == scp->dataVersion)
1452 bp->dataVersion = statusp->DataVersion;
1456 scp->dataVersion = statusp->DataVersion;
1459 /* note that our stat cache info is incorrect, so force us eventually
1460 * to stat the file again. There may be dirty data associated with
1461 * this vnode, and we want to preserve that information.
1463 * This function works by simply simulating a loss of the callback.
1465 * This function must be called with the scache locked.
1467 void cm_DiscardSCache(cm_scache_t *scp)
1469 lock_AssertMutex(&scp->mx);
1470 if (scp->cbServerp) {
1471 cm_PutServer(scp->cbServerp);
1472 scp->cbServerp = NULL;
1475 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1476 cm_dnlcPurgedp(scp);
1477 cm_dnlcPurgevp(scp);
1478 cm_FreeAllACLEnts(scp);
1480 /* Force mount points and symlinks to be re-evaluated */
1481 scp->mountPointStringp[0] = '\0';
1484 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1486 afsFidp->Volume = fidp->volume;
1487 afsFidp->Vnode = fidp->vnode;
1488 afsFidp->Unique = fidp->unique;
1491 #ifdef DEBUG_REFCOUNT
1492 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1494 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1497 osi_assert(scp != 0);
1499 #ifdef DEBUG_REFCOUNT
1500 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1501 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1505 #ifdef DEBUG_REFCOUNT
1506 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1508 void cm_HoldSCache(cm_scache_t *scp)
1511 osi_assert(scp != 0);
1512 lock_ObtainWrite(&cm_scacheLock);
1514 #ifdef DEBUG_REFCOUNT
1515 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1516 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1518 lock_ReleaseWrite(&cm_scacheLock);
1521 #ifdef DEBUG_REFCOUNT
1522 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1524 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1527 osi_assert(scp != NULL);
1528 if (scp->refCount == 0)
1529 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1530 osi_assert(scp->refCount-- >= 0);
1531 #ifdef DEBUG_REFCOUNT
1532 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1533 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1537 #ifdef DEBUG_REFCOUNT
1538 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1540 void cm_ReleaseSCache(cm_scache_t *scp)
1543 osi_assert(scp != NULL);
1544 lock_ObtainWrite(&cm_scacheLock);
1545 if (scp->refCount == 0)
1546 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1547 osi_assert(scp->refCount != 0);
1549 #ifdef DEBUG_REFCOUNT
1550 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1551 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1553 lock_ReleaseWrite(&cm_scacheLock);
1556 /* just look for the scp entry to get filetype */
1557 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1558 int cm_FindFileType(cm_fid_t *fidp)
1563 hash = CM_SCACHE_HASH(fidp);
1565 osi_assert(fidp->cell != 0);
1567 lock_ObtainWrite(&cm_scacheLock);
1568 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1569 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1570 lock_ReleaseWrite(&cm_scacheLock);
1571 return scp->fileType;
1574 lock_ReleaseWrite(&cm_scacheLock);
1578 /* dump all scp's that have reference count > 0 to a file.
1579 * cookie is used to identify this batch for easy parsing,
1580 * and it a string provided by a caller
1582 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1590 lock_ObtainRead(&cm_scacheLock);
1592 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1593 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1595 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1597 if (scp->refCount != 0)
1599 sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\r\n",
1600 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1602 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1606 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1607 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1609 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1611 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1613 if (scp->refCount != 0)
1615 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\r\n",
1616 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1617 scp->fid.unique, scp->refCount);
1618 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1623 sprintf(output, "%s - Done dumping scache.\r\n", cookie);
1624 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1627 lock_ReleaseRead(&cm_scacheLock);