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_AdjustLRU(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 /* called with cm_scacheLock write-locked; recycles an existing scp. */
54 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
60 lock_ObtainMutex(&scp->mx);
61 if (scp->flags & CM_SCACHEFLAG_INHASH) {
62 /* hash it out first */
63 i = CM_SCACHE_HASH(&scp->fid);
64 for (lscpp = &cm_data.hashTablep[i], tscp = cm_data.hashTablep[i];
66 lscpp = &tscp->nextp, tscp = tscp->nextp) {
69 scp->flags &= ~CM_SCACHEFLAG_INHASH;
73 osi_assertx(tscp, "afsd: scache hash screwup");
76 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
80 while(qdp = scp->bufWritesp) {
81 bufp = osi_GetQData(qdp);
82 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
85 lock_ReleaseMutex(&scp->mx);
86 lock_ObtainMutex(&bufp->mx);
87 bufp->cmFlags &= ~CM_BUF_CMSTORING;
88 bufp->flags &= ~CM_BUF_DIRTY;
89 bufp->flags |= CM_BUF_ERROR;
90 bufp->error = VNOVNODE;
91 bufp->dataVersion = -1; /* bad */
93 if (bufp->flags & CM_BUF_WAITING) {
94 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
95 osi_Wakeup((long) &bufp);
97 lock_ReleaseMutex(&bufp->mx);
99 lock_ObtainMutex(&scp->mx);
102 while(qdp = scp->bufReadsp) {
103 bufp = osi_GetQData(qdp);
104 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
107 lock_ReleaseMutex(&scp->mx);
108 lock_ObtainMutex(&bufp->mx);
109 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
110 bufp->flags &= ~CM_BUF_DIRTY;
111 bufp->flags |= CM_BUF_ERROR;
112 bufp->error = VNOVNODE;
113 bufp->dataVersion = -1; /* bad */
114 bufp->dirtyCounter++;
115 if (bufp->flags & CM_BUF_WAITING) {
116 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
117 osi_Wakeup((long) &bufp);
119 lock_ReleaseMutex(&bufp->mx);
121 lock_ObtainMutex(&scp->mx);
124 buf_CleanDirtyBuffers(scp);
126 /* look for things that shouldn't still be set */
127 osi_assert(scp->bufWritesp == NULL);
128 osi_assert(scp->bufReadsp == NULL);
131 /* invalidate so next merge works fine;
132 * also initialize some flags */
133 scp->flags &= ~(CM_SCACHEFLAG_STATD
134 | CM_SCACHEFLAG_DELETED
136 | CM_SCACHEFLAG_PURERO
137 | CM_SCACHEFLAG_OVERQUOTA
138 | CM_SCACHEFLAG_OUTOFSPACE);
139 scp->serverModTime = 0;
140 scp->dataVersion = 0;
141 scp->bulkStatProgress = hzero;
149 /* discard callback */
150 if (scp->cbServerp) {
151 cm_PutServer(scp->cbServerp);
152 scp->cbServerp = NULL;
156 /* remove from dnlc */
160 /* discard cached status; if non-zero, Close
161 * tried to store this to server but failed */
164 /* drop held volume ref */
166 cm_PutVolume(scp->volp);
170 /* discard symlink info */
171 scp->mountPointStringp[0] = 0;
172 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
173 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
175 /* reset locking info */
176 scp->fileLocksH = NULL;
177 scp->fileLocksT = NULL;
178 scp->serverLock = (-1);
179 scp->exclusiveLocks = 0;
180 scp->sharedLocks = 0;
182 /* not locked, but there can be no references to this guy
183 * while we hold the global refcount lock.
185 cm_FreeAllACLEnts(scp);
187 osi_Wakeup((long)&scp->flags);
189 lock_ReleaseMutex(&scp->mx);
194 /* called with cm_scacheLock write-locked; find a vnode to recycle.
195 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
197 cm_scache_t *cm_GetNewSCache(void)
202 /* first pass - look for deleted objects */
203 for ( scp = cm_data.scacheLRULastp;
205 scp = (cm_scache_t *) osi_QPrev(&scp->q))
207 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
209 if (scp->refCount == 0) {
210 if (scp->flags & CM_SCACHEFLAG_DELETED) {
211 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
212 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
214 /* we found an entry, so return it */
215 /* now remove from the LRU queue and put it back at the
216 * head of the LRU queue.
223 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
224 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
225 /* we found an entry, so return it */
226 /* now remove from the LRU queue and put it back at the
227 * head of the LRU queue.
236 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
238 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
239 /* There were no deleted scache objects that we could use. Try to find
240 * one that simply hasn't been used in a while.
243 for ( scp = cm_data.scacheLRULastp;
245 scp = (cm_scache_t *) osi_QPrev(&scp->q))
247 /* It is possible for the refCount to be zero and for there still
248 * to be outstanding dirty buffers. If there are dirty buffers,
249 * we must not recycle the scp. */
250 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
251 if (!buf_DirtyBuffersExist(&scp->fid)) {
252 if (!cm_RecycleSCache(scp, 0)) {
253 /* we found an entry, so return it */
254 /* now remove from the LRU queue and put it back at the
255 * head of the LRU queue.
263 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
267 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
269 /* If get here it means that every scache is either in use or has dirty buffers.
270 * We used to panic. Now we will give up our lock and wait.
273 lock_ReleaseWrite(&cm_scacheLock);
275 lock_ObtainWrite(&cm_scacheLock);
282 /* if we get here, we should allocate a new scache entry. We either are below
283 * quota or we have a leak and need to allocate a new one to avoid panicing.
285 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
286 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
287 memset(scp, 0, sizeof(cm_scache_t));
288 scp->magic = CM_SCACHE_MAGIC;
289 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
290 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
291 scp->serverLock = -1;
293 /* and put it in the LRU queue */
294 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
295 if (!cm_data.scacheLRULastp)
296 cm_data.scacheLRULastp = scp;
297 cm_data.currentSCaches++;
298 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
303 /* like strcmp, only for fids */
304 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
306 if (ap->vnode != bp->vnode)
308 if (ap->volume != bp->volume)
310 if (ap->unique != bp->unique)
312 if (ap->cell != bp->cell)
317 void cm_fakeSCacheInit(int newFile)
320 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
321 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
322 /* can leave clientModTime at 0 */
323 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
324 cm_data.fakeSCache.unixModeBits = 0777;
325 cm_data.fakeSCache.length.LowPart = 1000;
326 cm_data.fakeSCache.linkCount = 1;
327 cm_data.fakeSCache.refCount = 1;
329 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
333 cm_ValidateSCache(void)
335 cm_scache_t * scp, *lscp;
338 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
339 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
340 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
341 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
345 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
347 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
348 if (scp->magic != CM_SCACHE_MAGIC) {
349 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
350 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
353 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
354 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
355 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
358 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
359 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
360 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
363 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
364 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
365 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
368 if (i > cm_data.currentSCaches ) {
369 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
370 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
373 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
374 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
375 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
380 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
381 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
382 if (scp->magic != CM_SCACHE_MAGIC) {
383 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
384 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
387 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
388 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
389 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
392 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
393 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
394 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
397 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
398 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
399 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
402 if (i > cm_data.currentSCaches ) {
403 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
404 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
407 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
408 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
409 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
414 for ( i=0; i < cm_data.hashTableSize; i++ ) {
415 for ( scp = cm_data.hashTablep[i]; scp; scp = scp->nextp ) {
416 if (scp->magic != CM_SCACHE_MAGIC) {
417 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
418 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
421 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
422 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
423 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
426 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
427 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
428 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
431 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
432 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
433 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
439 return cm_dnlcValidate();
443 cm_ShutdownSCache(void)
447 for ( scp = cm_data.scacheLRULastp; scp;
448 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
449 if (scp->randomACLp) {
450 lock_ObtainMutex(&scp->mx);
451 cm_FreeAllACLEnts(scp);
452 lock_ReleaseMutex(&scp->mx);
454 lock_FinalizeMutex(&scp->mx);
455 lock_FinalizeRWLock(&scp->bufCreateLock);
458 return cm_dnlcShutdown();
461 void cm_InitSCache(int newFile, long maxSCaches)
463 static osi_once_t once;
465 if (osi_Once(&once)) {
466 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
468 memset(cm_data.hashTablep, 0, sizeof(cm_scache_t *) * cm_data.hashTableSize);
469 cm_data.currentSCaches = 0;
470 cm_data.maxSCaches = maxSCaches;
471 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
475 for ( scp = cm_data.scacheLRULastp; scp;
476 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
477 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
478 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
480 scp->cbServerp = NULL;
482 scp->fileLocksH = NULL;
483 scp->fileLocksT = NULL;
484 scp->serverLock = (-1);
485 scp->lastRefreshCycle = 0;
486 scp->exclusiveLocks = 0;
487 scp->sharedLocks = 0;
493 scp->flags &= ~CM_SCACHEFLAG_WAITING;
496 cm_allFileLocks = NULL;
497 cm_freeFileLocks = NULL;
498 cm_lockRefreshCycle = 0;
499 cm_fakeSCacheInit(newFile);
500 cm_dnlcInit(newFile);
505 /* version that doesn't bother creating the entry if we don't find it */
506 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
511 hash = CM_SCACHE_HASH(fidp);
513 if (fidp->cell == 0) {
520 lock_ObtainWrite(&cm_scacheLock);
521 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
522 if (cm_FidCmp(fidp, &scp->fid) == 0) {
523 cm_HoldSCacheNoLock(scp);
525 lock_ReleaseWrite(&cm_scacheLock);
529 lock_ReleaseWrite(&cm_scacheLock);
533 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
539 cm_volume_t *volp = 0;
542 int special; // yj: boolean variable to test if file is on root.afs
544 extern cm_fid_t cm_rootFid;
546 hash = CM_SCACHE_HASH(fidp);
548 osi_assert(fidp->cell != 0);
550 if (fidp->cell== cm_data.rootFid.cell &&
551 fidp->volume==cm_data.rootFid.volume &&
552 fidp->vnode==0x0 && fidp->unique==0x0)
554 osi_Log0(afsd_logp,"cm_getSCache called with root cell/volume and vnode=0 and unique=0");
557 // yj: check if we have the scp, if so, we don't need
558 // to do anything else
559 lock_ObtainWrite(&cm_scacheLock);
560 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
561 if (cm_FidCmp(fidp, &scp->fid) == 0) {
562 cm_HoldSCacheNoLock(scp);
565 lock_ReleaseWrite(&cm_scacheLock);
570 // yj: when we get here, it means we don't have an scp
571 // so we need to either load it or fake it, depending
572 // on whether the file is "special", see below.
574 // yj: if we're trying to get an scp for a file that's
575 // on root.afs of homecell, we want to handle it specially
576 // because we have to fill in the status stuff 'coz we
577 // don't want trybulkstat to fill it in for us
578 #ifdef AFS_FREELANCE_CLIENT
579 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
580 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
581 !(fidp->vnode==0x1 && fidp->unique==0x1));
582 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
583 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
584 fidp->vnode==0x1 && fidp->unique==0x1);
585 if (cm_freelanceEnabled && isRoot) {
586 osi_Log0(afsd_logp,"cm_getSCache Freelance and isRoot");
587 /* freelance: if we are trying to get the root scp for the first
588 * time, we will just put in a place holder entry.
593 if (cm_freelanceEnabled && special) {
594 osi_Log0(afsd_logp,"cm_getSCache Freelance and special");
595 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
596 lock_ObtainMutex(&cm_Freelance_Lock);
597 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
598 lock_ReleaseMutex(&cm_Freelance_Lock);
602 scp = cm_GetNewSCache();
604 osi_Log0(afsd_logp,"cm_getSCache unable to obtain *new* scache entry");
605 lock_ReleaseWrite(&cm_scacheLock);
606 return CM_ERROR_WOULDBLOCK;
609 lock_ObtainMutex(&scp->mx);
611 scp->volp = cm_data.rootSCachep->volp;
612 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
613 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
614 scp->dotdotFid.unique=1;
615 scp->dotdotFid.vnode=1;
616 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
617 scp->nextp=cm_data.hashTablep[hash];
618 cm_data.hashTablep[hash]=scp;
619 scp->flags |= CM_SCACHEFLAG_INHASH;
621 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
622 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
624 scp->fileType = CM_SCACHETYPE_INVALID;
626 lock_ObtainMutex(&cm_Freelance_Lock);
627 scp->length.LowPart = (DWORD)strlen(mp)+4;
628 scp->length.HighPart = 0;
629 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
630 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
631 lock_ReleaseMutex(&cm_Freelance_Lock);
634 scp->unixModeBits=0x1ff;
635 scp->clientModTime=FakeFreelanceModTime;
636 scp->serverModTime=FakeFreelanceModTime;
637 scp->parentUnique = 0x1;
638 scp->parentVnode=0x1;
640 scp->dataVersion=cm_data.fakeDirVersion;
641 scp->lockDataVersion=-1; /* no lock yet */
642 lock_ReleaseMutex(&scp->mx);
644 lock_ReleaseWrite(&cm_scacheLock);
648 #endif /* AFS_FREELANCE_CLIENT */
650 /* otherwise, we need to find the volume */
651 if (!cm_freelanceEnabled || !isRoot) {
652 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
653 cellp = cm_FindCellByID(fidp->cell);
655 return CM_ERROR_NOSUCHCELL;
657 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
660 lock_ObtainWrite(&cm_scacheLock);
663 /* otherwise, we have the volume, now reverify that the scp doesn't
664 * exist, and proceed.
666 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
667 if (cm_FidCmp(fidp, &scp->fid) == 0) {
668 cm_HoldSCacheNoLock(scp);
669 osi_assert(scp->volp == volp);
671 lock_ReleaseWrite(&cm_scacheLock);
679 /* now, if we don't have the fid, recycle something */
680 scp = cm_GetNewSCache();
682 osi_Log0(afsd_logp,"cm_getSCache unable to obtain *new* scache entry");
683 lock_ReleaseWrite(&cm_scacheLock);
684 return CM_ERROR_WOULDBLOCK;
687 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
688 lock_ObtainMutex(&scp->mx);
690 scp->volp = volp; /* a held reference */
692 if (!cm_freelanceEnabled || !isRoot) {
693 /* if this scache entry represents a volume root then we need
694 * to copy the dotdotFipd from the volume structure where the
695 * "master" copy is stored (defect 11489)
697 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
698 scp->dotdotFid = volp->dotdotFid;
701 if (volp->roID == fidp->volume)
702 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
703 else if (volp->bkID == fidp->volume)
704 scp->flags |= CM_SCACHEFLAG_RO;
706 scp->nextp = cm_data.hashTablep[hash];
707 cm_data.hashTablep[hash] = scp;
708 scp->flags |= CM_SCACHEFLAG_INHASH;
710 lock_ReleaseMutex(&scp->mx);
712 /* XXX - The following fields in the cm_scache are
718 lock_ReleaseWrite(&cm_scacheLock);
720 /* now we have a held scache entry; just return it */
725 /* Returns a held reference to the scache's parent
727 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
732 cm_scache_t * pscp = NULL;
734 lock_ObtainWrite(&cm_scacheLock);
735 parent_fid = scp->fid;
736 parent_fid.vnode = scp->parentVnode;
737 parent_fid.unique = scp->parentUnique;
739 if (cm_FidCmp(&scp->fid, &parent_fid)) {
740 for (i=0; i<cm_data.hashTableSize; i++) {
741 for (pscp = cm_data.hashTablep[i]; pscp; pscp = pscp->nextp) {
742 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
743 cm_HoldSCacheNoLock(pscp);
749 lock_ReleaseWrite(&cm_scacheLock);
754 /* synchronize a fetch, store, read, write, fetch status or store status.
755 * Called with scache mutex held, and returns with it held, but temporarily
756 * drops it during the fetch.
758 * At most one flag can be on in flags, if this is an RPC request.
760 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
762 * There are a lot of weird restrictions here; here's an attempt to explain the
763 * rationale for the concurrency restrictions implemented in this function.
765 * First, although the file server will break callbacks when *another* machine
766 * modifies a file or status block, the client itself is responsible for
767 * concurrency control on its own requests. Callback breaking events are rare,
768 * and simply invalidate any concurrent new status info.
770 * In the absence of callback breaking messages, we need to know how to
771 * synchronize incoming responses describing updates to files. We synchronize
772 * operations that update the data version by comparing the data versions.
773 * However, updates that do not update the data, but only the status, can't be
774 * synchronized with fetches or stores, since there's nothing to compare
775 * to tell which operation executed first at the server.
777 * Thus, we can allow multiple ops that change file data, or dir data, and
778 * fetches. However, status storing ops have to be done serially.
780 * Furthermore, certain data-changing ops are incompatible: we can't read or
781 * write a buffer while doing a truncate. We can't read and write the same
782 * buffer at the same time, or write while fetching or storing, or read while
783 * fetching a buffer (this may change). We can't fetch and store at the same
786 * With respect to status, we can't read and write at the same time, read while
787 * fetching, write while fetching or storing, or fetch and store at the same time.
789 * We can't allow a get callback RPC to run in concurrently with something that
790 * will return updated status, since we could start a call, have the server
791 * return status, have another machine make an update to the status (which
792 * doesn't change serverModTime), have the original machine get a new callback,
793 * and then have the original machine merge in the early, old info from the
794 * first call. At this point, the easiest way to avoid this problem is to have
795 * getcallback calls conflict with all others for the same vnode. Other calls
796 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
797 * vnode must be careful not to merge in their status unless they have obtained
798 * a callback from the start of their call.
801 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
802 * Each such RPC passes a FileLength parameter, which the server uses to do
803 * pre-truncation if necessary. So if two RPC's are processed out of order at
804 * the server, the one with the smaller FileLength will be processed last,
805 * possibly resulting in a bogus truncation. The simplest way to avoid this
806 * is to serialize all StoreData RPC's. This is the reason we defined
807 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
809 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
810 afs_uint32 rights, afs_uint32 flags)
812 osi_queueData_t *qdp;
815 afs_uint32 outRights;
818 /* lookup this first */
819 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
821 /* some minor assertions */
822 if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
823 | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
824 | CM_SCACHESYNC_SETSIZE)) {
826 osi_assert(bufp->refCount > 0);
828 osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
832 else osi_assert(bufp == NULL);
834 /* Do the access check. Now we don't really do the access check
835 * atomically, since the caller doesn't expect the parent dir to be
836 * returned locked, and that is what we'd have to do to prevent a
837 * callback breaking message on the parent due to a setacl call from
838 * being processed while we're running. So, instead, we check things
839 * here, and if things look fine with the access, we proceed to finish
840 * the rest of this check. Sort of a hack, but probably good enough.
844 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
845 /* if we're bringing in a new status block, ensure that
846 * we aren't already doing so, and that no one is
847 * changing the status concurrently, either. We need
848 * to do this, even if the status is of a different
849 * type, since we don't have the ability to figure out,
850 * in the AFS 3 protocols, which status-changing
851 * operation ran first, or even which order a read and
852 * a write occurred in.
854 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
855 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
856 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
860 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
861 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
862 /* if we're going to make an RPC to change the status, make sure
863 * that no one is bringing in or sending out the status.
865 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
866 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
867 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
870 if (scp->bufReadsp || scp->bufWritesp) {
871 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
875 if (flags & CM_SCACHESYNC_FETCHDATA) {
876 /* if we're bringing in a new chunk of data, make sure that
877 * nothing is happening to that chunk, and that we aren't
878 * changing the basic file status info, either.
880 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
881 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
882 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
885 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
886 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
890 if (flags & CM_SCACHESYNC_STOREDATA) {
891 /* same as fetch data */
892 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
893 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
894 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
897 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
898 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp, bufp);
903 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
904 /* Don't allow concurrent StoreData RPC's */
905 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
906 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
911 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
912 /* Don't allow more than one BKG store request */
913 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
914 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
919 if (flags & CM_SCACHESYNC_LOCK) {
920 /* Don't allow concurrent fiddling with lock lists */
921 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
922 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
927 /* now the operations that don't correspond to making RPCs */
928 if (flags & CM_SCACHESYNC_GETSTATUS) {
929 /* we can use the status that's here, if we're not
930 * bringing in new status.
932 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
933 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
937 if (flags & CM_SCACHESYNC_SETSTATUS) {
938 /* we can make a change to the local status, as long as
939 * the status isn't changing now.
941 * If we're fetching or storing a chunk of data, we can
942 * change the status locally, since the fetch/store
943 * operations don't change any of the data that we're
946 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
947 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
951 if (flags & CM_SCACHESYNC_READ) {
952 /* we're going to read the data, make sure that the
953 * status is available, and that the data is here. It
954 * is OK to read while storing the data back.
956 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
957 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
960 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
961 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
965 if (flags & CM_SCACHESYNC_WRITE) {
966 /* don't write unless the status is stable and the chunk
969 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
970 | CM_SCACHEFLAG_SIZESTORING)) {
971 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
974 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
975 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
980 // yj: modified this so that callback only checked if we're
981 // not checking something on /afs
982 /* fix the conditional to match the one in cm_HaveCallback */
983 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
984 #ifdef AFS_FREELANCE_CLIENT
985 && (!cm_freelanceEnabled ||
986 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
987 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
988 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
989 cm_fakeDirCallback < 2)
990 #endif /* AFS_FREELANCE_CLIENT */
992 if (!cm_HaveCallback(scp)) {
993 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
996 lock_ReleaseMutex(&bufp->mx);
997 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
999 lock_ReleaseMutex(&scp->mx);
1000 lock_ObtainMutex(&bufp->mx);
1001 lock_ObtainMutex(&scp->mx);
1010 /* can't check access rights without a callback */
1011 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1013 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1014 return CM_ERROR_READONLY;
1016 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1017 if (~outRights & rights)
1018 return CM_ERROR_NOACCESS;
1021 /* we don't know the required access rights */
1022 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1023 code = cm_GetAccessRights(scp, userp, reqp);
1025 lock_ReleaseMutex(&scp->mx);
1026 lock_ObtainMutex(&bufp->mx);
1027 lock_ObtainMutex(&scp->mx);
1035 /* if we get here, we're happy */
1039 /* first check if we're not supposed to wait: fail
1040 * in this case, returning with everything still locked.
1042 if (flags & CM_SCACHESYNC_NOWAIT)
1043 return CM_ERROR_WOULDBLOCK;
1045 /* wait here, then try again */
1046 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1047 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1049 scp->waitRequests++;
1050 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1051 scp, scp->waitCount, scp->waitRequests);
1053 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1054 scp->flags |= CM_SCACHEFLAG_WAITING;
1055 scp->waitCount = scp->waitRequests = 1;
1058 lock_ReleaseMutex(&bufp->mx);
1059 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1061 smb_UpdateServerPriority();
1064 lock_ObtainMutex(&bufp->mx);
1065 lock_ObtainMutex(&scp->mx);
1067 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1068 scp, scp->waitCount, scp->waitRequests);
1069 if (scp->waitCount == 0) {
1070 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1071 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1072 scp->waitRequests = 0;
1074 } /* big while loop */
1076 /* now, update the recorded state for RPC-type calls */
1077 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1078 scp->flags |= CM_SCACHEFLAG_FETCHING;
1079 if (flags & CM_SCACHESYNC_STORESTATUS)
1080 scp->flags |= CM_SCACHEFLAG_STORING;
1081 if (flags & CM_SCACHESYNC_STORESIZE)
1082 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1083 if (flags & CM_SCACHESYNC_GETCALLBACK)
1084 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1085 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1086 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1087 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1088 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1089 if (flags & CM_SCACHESYNC_LOCK)
1090 scp->flags |= CM_SCACHEFLAG_LOCKING;
1092 /* now update the buffer pointer */
1093 if (flags & CM_SCACHESYNC_FETCHDATA) {
1094 /* ensure that the buffer isn't already in the I/O list */
1096 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1097 tbufp = osi_GetQData(qdp);
1098 osi_assert(tbufp != bufp);
1102 /* queue a held reference to the buffer in the "reading" I/O list */
1103 qdp = osi_QDAlloc();
1104 osi_SetQData(qdp, bufp);
1107 bufp->cmFlags |= CM_BUF_CMFETCHING;
1109 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1112 if (flags & CM_SCACHESYNC_STOREDATA) {
1113 /* ensure that the buffer isn't already in the I/O list */
1115 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1116 tbufp = osi_GetQData(qdp);
1117 osi_assert(tbufp != bufp);
1121 /* queue a held reference to the buffer in the "writing" I/O list */
1122 qdp = osi_QDAlloc();
1123 osi_SetQData(qdp, bufp);
1126 bufp->cmFlags |= CM_BUF_CMSTORING;
1128 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1134 /* for those syncops that setup for RPCs.
1135 * Called with scache locked.
1137 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1139 osi_queueData_t *qdp;
1142 /* now, update the recorded state for RPC-type calls */
1143 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1144 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1145 if (flags & CM_SCACHESYNC_STORESTATUS)
1146 scp->flags &= ~CM_SCACHEFLAG_STORING;
1147 if (flags & CM_SCACHESYNC_STORESIZE)
1148 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1149 if (flags & CM_SCACHESYNC_GETCALLBACK)
1150 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1151 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1152 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1153 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1154 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1155 if (flags & CM_SCACHESYNC_LOCK)
1156 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1158 /* now update the buffer pointer */
1159 if (flags & CM_SCACHESYNC_FETCHDATA) {
1160 /* ensure that the buffer isn't already in the I/O list */
1161 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1162 tbufp = osi_GetQData(qdp);
1167 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1172 if (bufp->cmFlags & CM_BUF_CMFETCHING)
1174 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1175 if (bufp->flags & CM_BUF_WAITING) {
1176 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1177 osi_Wakeup((LONG_PTR) &bufp);
1184 /* now update the buffer pointer */
1185 if (flags & CM_SCACHESYNC_STOREDATA) {
1186 /* ensure that the buffer isn't already in the I/O list */
1187 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1188 tbufp = osi_GetQData(qdp);
1193 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1198 if (bufp->cmFlags & CM_BUF_CMSTORING)
1200 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1201 if (bufp->flags & CM_BUF_WAITING) {
1202 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1203 osi_Wakeup((LONG_PTR) &bufp);
1210 /* and wakeup anyone who is waiting */
1211 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1212 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1213 osi_Wakeup((LONG_PTR) &scp->flags);
1217 /* merge in a response from an RPC. The scp must be locked, and the callback
1220 * Don't overwrite any status info that is dirty, since we could have a store
1221 * operation (such as store data) that merges some info in, and we don't want
1222 * to lose the local updates. Typically, there aren't many updates we do
1223 * locally, anyway, probably only mtime.
1225 * There is probably a bug in here where a chmod (which doesn't change
1226 * serverModTime) that occurs between two fetches, both of whose responses are
1227 * handled after the callback breaking is done, but only one of whose calls
1228 * started before that, can cause old info to be merged from the first call.
1230 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
1231 cm_user_t *userp, afs_uint32 flags)
1233 // yj: i want to create some fake status for the /afs directory and the
1234 // entries under that directory
1235 #ifdef AFS_FREELANCE_CLIENT
1236 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1237 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1238 statusp->InterfaceVersion = 0x1;
1239 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1240 statusp->LinkCount = scp->linkCount;
1241 statusp->Length = cm_fakeDirSize;
1242 statusp->Length_hi = 0;
1243 statusp->DataVersion = cm_data.fakeDirVersion;
1244 statusp->Author = 0x1;
1245 statusp->Owner = 0x0;
1246 statusp->CallerAccess = 0x9;
1247 statusp->AnonymousAccess = 0x9;
1248 statusp->UnixModeBits = 0x1ff;
1249 statusp->ParentVnode = 0x1;
1250 statusp->ParentUnique = 0x1;
1251 statusp->ResidencyMask = 0;
1252 statusp->ClientModTime = FakeFreelanceModTime;
1253 statusp->ServerModTime = FakeFreelanceModTime;
1255 statusp->SyncCounter = 0;
1256 statusp->dataVersionHigh = 0;
1257 statusp->errorCode = 0;
1259 #endif /* AFS_FREELANCE_CLIENT */
1261 if (statusp->errorCode != 0) {
1262 scp->flags |= CM_SCACHEFLAG_EACCESS;
1263 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1266 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1269 if (!(flags & CM_MERGEFLAG_FORCE)
1270 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1271 struct cm_cell *cellp;
1273 cellp = cm_FindCellByID(scp->fid.cell);
1274 if (scp->cbServerp) {
1275 struct cm_volume *volp = NULL;
1277 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1278 (cm_req_t *) NULL, &volp);
1279 osi_Log2(afsd_logp, "old data from server %x volume %s",
1280 scp->cbServerp->addr.sin_addr.s_addr,
1281 volp ? volp->namep : "(unknown)");
1285 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1286 scp, scp->dataVersion, statusp->DataVersion);
1287 /* we have a number of data fetch/store operations running
1288 * concurrently, and we can tell which one executed last at the
1289 * server by its mtime.
1290 * Choose the one with the largest mtime, and ignore the rest.
1292 * These concurrent calls are incompatible with setting the
1293 * mtime, so we won't have a locally changed mtime here.
1295 * We could also have ACL info for a different user than usual,
1296 * in which case we have to do that part of the merge, anyway.
1297 * We won't have to worry about the info being old, since we
1298 * won't have concurrent calls
1299 * that change file status running from this machine.
1301 * Added 3/17/98: if we see data version regression on an RO
1302 * file, it's probably due to a server holding an out-of-date
1303 * replica, rather than to concurrent RPC's. Failures to
1304 * release replicas are now flagged by the volserver, but only
1305 * since AFS 3.4 5.22, so there are plenty of clients getting
1306 * out-of-date replicas out there.
1308 * If we discover an out-of-date replica, by this time it's too
1309 * late to go to another server and retry. Also, we can't
1310 * reject the merge, because then there is no way for
1311 * GetAccess to do its work, and the caller gets into an
1312 * infinite loop. So we just grin and bear it.
1314 if (!(scp->flags & CM_SCACHEFLAG_RO))
1317 scp->serverModTime = statusp->ServerModTime;
1319 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1320 scp->clientModTime = statusp->ClientModTime;
1322 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1323 scp->length.LowPart = statusp->Length;
1324 scp->length.HighPart = statusp->Length_hi;
1327 scp->serverLength.LowPart = statusp->Length;
1328 scp->serverLength.HighPart = statusp->Length_hi;
1330 scp->linkCount = statusp->LinkCount;
1331 scp->dataVersion = statusp->DataVersion;
1332 scp->owner = statusp->Owner;
1333 scp->group = statusp->Group;
1334 scp->unixModeBits = statusp->UnixModeBits & 07777;
1336 if (statusp->FileType == File)
1337 scp->fileType = CM_SCACHETYPE_FILE;
1338 else if (statusp->FileType == Directory)
1339 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1340 else if (statusp->FileType == SymbolicLink) {
1341 if ((scp->unixModeBits & 0111) == 0)
1342 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1344 scp->fileType = CM_SCACHETYPE_SYMLINK;
1347 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1348 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1350 /* and other stuff */
1351 scp->parentVnode = statusp->ParentVnode;
1352 scp->parentUnique = statusp->ParentUnique;
1354 /* and merge in the private acl cache info, if this is more than the public
1355 * info; merge in the public stuff in any case.
1357 scp->anyAccess = statusp->AnonymousAccess;
1359 if (userp != NULL) {
1360 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1364 /* note that our stat cache info is incorrect, so force us eventually
1365 * to stat the file again. There may be dirty data associated with
1366 * this vnode, and we want to preserve that information.
1368 * This function works by simply simulating a loss of the callback.
1370 * This function must be called with the scache locked.
1372 void cm_DiscardSCache(cm_scache_t *scp)
1374 lock_AssertMutex(&scp->mx);
1375 if (scp->cbServerp) {
1376 cm_PutServer(scp->cbServerp);
1377 scp->cbServerp = NULL;
1380 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1381 cm_dnlcPurgedp(scp);
1382 cm_dnlcPurgevp(scp);
1383 cm_FreeAllACLEnts(scp);
1385 /* Force mount points and symlinks to be re-evaluated */
1386 scp->mountPointStringp[0] = '\0';
1389 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1391 afsFidp->Volume = fidp->volume;
1392 afsFidp->Vnode = fidp->vnode;
1393 afsFidp->Unique = fidp->unique;
1396 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1398 osi_assert(scp != 0);
1399 osi_assert(scp->refCount >= 0);
1403 void cm_HoldSCache(cm_scache_t *scp)
1405 osi_assert(scp != 0);
1406 lock_ObtainWrite(&cm_scacheLock);
1407 osi_assert(scp->refCount >= 0);
1409 lock_ReleaseWrite(&cm_scacheLock);
1412 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1414 osi_assert(scp != 0);
1415 osi_assert(scp->refCount-- >= 0);
1418 void cm_ReleaseSCache(cm_scache_t *scp)
1420 osi_assert(scp != 0);
1421 lock_ObtainWrite(&cm_scacheLock);
1422 osi_assert(scp->refCount != 0);
1424 lock_ReleaseWrite(&cm_scacheLock);
1427 /* just look for the scp entry to get filetype */
1428 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1429 int cm_FindFileType(cm_fid_t *fidp)
1434 hash = CM_SCACHE_HASH(fidp);
1436 osi_assert(fidp->cell != 0);
1438 lock_ObtainWrite(&cm_scacheLock);
1439 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
1440 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1441 lock_ReleaseWrite(&cm_scacheLock);
1442 return scp->fileType;
1445 lock_ReleaseWrite(&cm_scacheLock);
1449 /* dump all scp's that have reference count > 0 to a file.
1450 * cookie is used to identify this batch for easy parsing,
1451 * and it a string provided by a caller
1453 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1461 lock_ObtainRead(&cm_scacheLock);
1463 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1464 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1466 for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q))
1468 if (scp->refCount != 0)
1470 sprintf(output, "%s fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1471 cookie, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1473 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1477 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.hashTableSize=%d\n", cookie, cm_data.hashTableSize);
1478 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1480 for (i = 0; i < cm_data.hashTableSize; i++)
1482 for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp)
1484 if (scp->refCount != 0)
1486 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1487 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1488 scp->fid.unique, scp->refCount);
1489 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1494 sprintf(output, "%s - Done dumping scache.\n", cookie);
1495 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1498 lock_ReleaseRead(&cm_scacheLock);