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 /* 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.hashTablep[i], tscp = cm_data.hashTablep[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 */
150 scp->flags &= ~(CM_SCACHEFLAG_STATD
151 | CM_SCACHEFLAG_DELETED
153 | CM_SCACHEFLAG_PURERO
154 | CM_SCACHEFLAG_OVERQUOTA
155 | CM_SCACHEFLAG_OUTOFSPACE
156 | CM_SCACHEFLAG_EACCESS);
157 scp->serverModTime = 0;
158 scp->dataVersion = 0;
159 scp->bulkStatProgress = hzero;
167 /* discard callback */
168 if (scp->cbServerp) {
169 cm_PutServer(scp->cbServerp);
170 scp->cbServerp = NULL;
174 /* remove from dnlc */
178 /* discard cached status; if non-zero, Close
179 * tried to store this to server but failed */
182 /* drop held volume ref */
184 cm_PutVolume(scp->volp);
188 /* discard symlink info */
189 scp->mountPointStringp[0] = 0;
190 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
191 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
193 /* reset locking info */
194 scp->fileLocksH = NULL;
195 scp->fileLocksT = NULL;
196 scp->serverLock = (-1);
197 scp->exclusiveLocks = 0;
198 scp->sharedLocks = 0;
200 /* not locked, but there can be no references to this guy
201 * while we hold the global refcount lock.
203 cm_FreeAllACLEnts(scp);
208 /* called with cm_scacheLock write-locked; find a vnode to recycle.
209 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
211 cm_scache_t *cm_GetNewSCache(void)
217 /* first pass - look for deleted objects */
218 for ( scp = cm_data.scacheLRULastp;
220 scp = (cm_scache_t *) osi_QPrev(&scp->q))
222 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
224 if (scp->refCount == 0) {
225 if (scp->flags & CM_SCACHEFLAG_DELETED) {
226 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
227 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
229 /* we found an entry, so return it */
230 /* now remove from the LRU queue and put it back at the
231 * head of the LRU queue.
238 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
239 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
240 /* we found an entry, so return it */
241 /* now remove from the LRU queue and put it back at the
242 * head of the LRU queue.
251 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
254 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
255 /* There were no deleted scache objects that we could use. Try to find
256 * one that simply hasn't been used in a while.
258 for ( scp = cm_data.scacheLRULastp;
260 scp = (cm_scache_t *) osi_QPrev(&scp->q))
262 /* It is possible for the refCount to be zero and for there still
263 * to be outstanding dirty buffers. If there are dirty buffers,
264 * we must not recycle the scp. */
265 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
266 if (!buf_DirtyBuffersExist(&scp->fid)) {
267 if (!cm_RecycleSCache(scp, 0)) {
268 /* we found an entry, so return it */
269 /* now remove from the LRU queue and put it back at the
270 * head of the LRU queue.
278 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
282 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
287 /* if we get here, we should allocate a new scache entry. We either are below
288 * quota or we have a leak and need to allocate a new one to avoid panicing.
290 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
291 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
292 memset(scp, 0, sizeof(cm_scache_t));
293 scp->magic = CM_SCACHE_MAGIC;
294 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
295 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
296 scp->serverLock = -1;
298 /* and put it in the LRU queue */
299 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
300 if (!cm_data.scacheLRULastp)
301 cm_data.scacheLRULastp = scp;
302 cm_data.currentSCaches++;
303 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
308 /* like strcmp, only for fids */
309 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
311 if (ap->vnode != bp->vnode)
313 if (ap->volume != bp->volume)
315 if (ap->unique != bp->unique)
317 if (ap->cell != bp->cell)
322 void cm_fakeSCacheInit(int newFile)
325 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
326 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
327 /* can leave clientModTime at 0 */
328 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
329 cm_data.fakeSCache.unixModeBits = 0777;
330 cm_data.fakeSCache.length.LowPart = 1000;
331 cm_data.fakeSCache.linkCount = 1;
332 cm_data.fakeSCache.refCount = 1;
334 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
338 cm_ValidateSCache(void)
340 cm_scache_t * scp, *lscp;
343 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
344 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
345 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
346 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
350 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
352 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
353 if (scp->magic != CM_SCACHE_MAGIC) {
354 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
355 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
358 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
359 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
360 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
363 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
364 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
365 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
368 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
369 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
370 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
373 if (i > cm_data.currentSCaches ) {
374 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
375 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
378 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
379 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
380 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
385 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
386 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
387 if (scp->magic != CM_SCACHE_MAGIC) {
388 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
389 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
392 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
393 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
394 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
397 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
398 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
399 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
402 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
403 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
404 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
407 if (i > cm_data.currentSCaches ) {
408 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
409 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
412 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
413 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
414 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
419 for ( i=0; i < cm_data.hashTableSize; i++ ) {
420 for ( scp = cm_data.hashTablep[i]; scp; scp = scp->nextp ) {
421 if (scp->magic != CM_SCACHE_MAGIC) {
422 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
423 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
426 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
427 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
428 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
431 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
432 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
433 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
436 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
437 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
438 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
444 return cm_dnlcValidate();
448 cm_ShutdownSCache(void)
452 for ( scp = cm_data.scacheLRULastp; scp;
453 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
454 if (scp->randomACLp) {
455 lock_ObtainMutex(&scp->mx);
456 cm_FreeAllACLEnts(scp);
457 lock_ReleaseMutex(&scp->mx);
459 lock_FinalizeMutex(&scp->mx);
460 lock_FinalizeRWLock(&scp->bufCreateLock);
463 return cm_dnlcShutdown();
466 void cm_InitSCache(int newFile, long maxSCaches)
468 static osi_once_t once;
470 if (osi_Once(&once)) {
471 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
473 memset(cm_data.hashTablep, 0, sizeof(cm_scache_t *) * cm_data.hashTableSize);
474 cm_data.currentSCaches = 0;
475 cm_data.maxSCaches = maxSCaches;
476 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
480 for ( scp = cm_data.scacheLRULastp; scp;
481 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
482 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
483 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
485 scp->cbServerp = NULL;
487 scp->fileLocksH = NULL;
488 scp->fileLocksT = NULL;
489 scp->serverLock = (-1);
490 scp->lastRefreshCycle = 0;
491 scp->exclusiveLocks = 0;
492 scp->sharedLocks = 0;
498 scp->flags &= ~CM_SCACHEFLAG_WAITING;
501 cm_allFileLocks = NULL;
502 cm_freeFileLocks = NULL;
503 cm_lockRefreshCycle = 0;
504 cm_fakeSCacheInit(newFile);
505 cm_dnlcInit(newFile);
510 /* version that doesn't bother creating the entry if we don't find it */
511 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
516 hash = CM_SCACHE_HASH(fidp);
518 if (fidp->cell == 0) {
522 lock_ObtainWrite(&cm_scacheLock);
523 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
524 if (cm_FidCmp(fidp, &scp->fid) == 0) {
525 cm_HoldSCacheNoLock(scp);
527 lock_ReleaseWrite(&cm_scacheLock);
531 lock_ReleaseWrite(&cm_scacheLock);
535 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
541 cm_volume_t *volp = 0;
544 int special; // yj: boolean variable to test if file is on root.afs
546 extern cm_fid_t cm_rootFid;
548 hash = CM_SCACHE_HASH(fidp);
550 osi_assert(fidp->cell != 0);
552 if (fidp->cell== cm_data.rootFid.cell &&
553 fidp->volume==cm_data.rootFid.volume &&
554 fidp->vnode==0x0 && fidp->unique==0x0)
556 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
559 // yj: check if we have the scp, if so, we don't need
560 // to do anything else
561 lock_ObtainWrite(&cm_scacheLock);
562 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
563 if (cm_FidCmp(fidp, &scp->fid) == 0) {
564 cm_HoldSCacheNoLock(scp);
567 lock_ReleaseWrite(&cm_scacheLock);
572 // yj: when we get here, it means we don't have an scp
573 // so we need to either load it or fake it, depending
574 // on whether the file is "special", see below.
576 // yj: if we're trying to get an scp for a file that's
577 // on root.afs of homecell, we want to handle it specially
578 // because we have to fill in the status stuff 'coz we
579 // don't want trybulkstat to fill it in for us
580 #ifdef AFS_FREELANCE_CLIENT
581 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
582 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
583 !(fidp->vnode==0x1 && fidp->unique==0x1));
584 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
585 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
586 fidp->vnode==0x1 && fidp->unique==0x1);
587 if (cm_freelanceEnabled && isRoot) {
588 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
589 /* freelance: if we are trying to get the root scp for the first
590 * time, we will just put in a place holder entry.
595 if (cm_freelanceEnabled && special) {
596 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
597 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
598 lock_ObtainMutex(&cm_Freelance_Lock);
599 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
600 lock_ReleaseMutex(&cm_Freelance_Lock);
604 scp = cm_GetNewSCache();
606 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
607 lock_ReleaseWrite(&cm_scacheLock);
608 return CM_ERROR_WOULDBLOCK;
611 #if not_too_dangerous
612 /* dropping the cm_scacheLock is dangerous */
613 lock_ReleaseWrite(&cm_scacheLock);
614 lock_ObtainMutex(&scp->mx);
615 lock_ObtainWrite(&cm_scacheLock);
618 scp->volp = cm_data.rootSCachep->volp;
619 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
620 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
621 scp->dotdotFid.unique=1;
622 scp->dotdotFid.vnode=1;
623 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
624 scp->nextp=cm_data.hashTablep[hash];
625 cm_data.hashTablep[hash]=scp;
626 scp->flags |= CM_SCACHEFLAG_INHASH;
628 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
629 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
630 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
632 scp->fileType = CM_SCACHETYPE_INVALID;
634 lock_ObtainMutex(&cm_Freelance_Lock);
635 scp->length.LowPart = (DWORD)strlen(mp)+4;
636 scp->length.HighPart = 0;
637 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
638 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
639 lock_ReleaseMutex(&cm_Freelance_Lock);
642 scp->unixModeBits=0x1ff;
643 scp->clientModTime=FakeFreelanceModTime;
644 scp->serverModTime=FakeFreelanceModTime;
645 scp->parentUnique = 0x1;
646 scp->parentVnode=0x1;
648 scp->dataVersion=cm_data.fakeDirVersion;
649 scp->lockDataVersion=-1; /* no lock yet */
650 #if not_too_dangerous
651 lock_ReleaseMutex(&scp->mx);
654 lock_ReleaseWrite(&cm_scacheLock);
658 #endif /* AFS_FREELANCE_CLIENT */
660 /* otherwise, we need to find the volume */
661 if (!cm_freelanceEnabled || !isRoot) {
662 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
663 cellp = cm_FindCellByID(fidp->cell);
665 return CM_ERROR_NOSUCHCELL;
667 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
670 lock_ObtainWrite(&cm_scacheLock);
673 /* otherwise, we have the volume, now reverify that the scp doesn't
674 * exist, and proceed.
676 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
677 if (cm_FidCmp(fidp, &scp->fid) == 0) {
678 cm_HoldSCacheNoLock(scp);
679 osi_assert(scp->volp == volp);
681 lock_ReleaseWrite(&cm_scacheLock);
689 /* now, if we don't have the fid, recycle something */
690 scp = cm_GetNewSCache();
692 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
693 lock_ReleaseWrite(&cm_scacheLock);
694 return CM_ERROR_WOULDBLOCK;
696 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
698 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
700 lock_ReleaseWrite(&cm_scacheLock);
701 lock_ObtainMutex(&scp->mx);
702 lock_ObtainWrite(&cm_scacheLock);
704 scp->volp = volp; /* a held reference */
706 if (!cm_freelanceEnabled || !isRoot) {
707 /* if this scache entry represents a volume root then we need
708 * to copy the dotdotFipd from the volume structure where the
709 * "master" copy is stored (defect 11489)
711 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
712 scp->dotdotFid = volp->dotdotFid;
715 if (volp->roID == fidp->volume)
716 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
717 else if (volp->bkID == fidp->volume)
718 scp->flags |= CM_SCACHEFLAG_RO;
720 scp->nextp = cm_data.hashTablep[hash];
721 cm_data.hashTablep[hash] = scp;
722 scp->flags |= CM_SCACHEFLAG_INHASH;
724 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
725 lock_ReleaseMutex(&scp->mx);
727 /* XXX - The following fields in the cm_scache are
733 lock_ReleaseWrite(&cm_scacheLock);
735 /* now we have a held scache entry; just return it */
740 /* Returns a held reference to the scache's parent
742 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
747 cm_scache_t * pscp = NULL;
749 lock_ObtainRead(&cm_scacheLock);
750 parent_fid = scp->fid;
751 parent_fid.vnode = scp->parentVnode;
752 parent_fid.unique = scp->parentUnique;
754 if (cm_FidCmp(&scp->fid, &parent_fid)) {
755 for (i=0; i<cm_data.hashTableSize; i++) {
756 for (pscp = cm_data.hashTablep[i]; pscp; pscp = pscp->nextp) {
757 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
758 cm_HoldSCacheNoLock(pscp);
764 lock_ReleaseRead(&cm_scacheLock);
769 /* synchronize a fetch, store, read, write, fetch status or store status.
770 * Called with scache mutex held, and returns with it held, but temporarily
771 * drops it during the fetch.
773 * At most one flag can be on in flags, if this is an RPC request.
775 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
777 * There are a lot of weird restrictions here; here's an attempt to explain the
778 * rationale for the concurrency restrictions implemented in this function.
780 * First, although the file server will break callbacks when *another* machine
781 * modifies a file or status block, the client itself is responsible for
782 * concurrency control on its own requests. Callback breaking events are rare,
783 * and simply invalidate any concurrent new status info.
785 * In the absence of callback breaking messages, we need to know how to
786 * synchronize incoming responses describing updates to files. We synchronize
787 * operations that update the data version by comparing the data versions.
788 * However, updates that do not update the data, but only the status, can't be
789 * synchronized with fetches or stores, since there's nothing to compare
790 * to tell which operation executed first at the server.
792 * Thus, we can allow multiple ops that change file data, or dir data, and
793 * fetches. However, status storing ops have to be done serially.
795 * Furthermore, certain data-changing ops are incompatible: we can't read or
796 * write a buffer while doing a truncate. We can't read and write the same
797 * buffer at the same time, or write while fetching or storing, or read while
798 * fetching a buffer (this may change). We can't fetch and store at the same
801 * With respect to status, we can't read and write at the same time, read while
802 * fetching, write while fetching or storing, or fetch and store at the same time.
804 * We can't allow a get callback RPC to run in concurrently with something that
805 * will return updated status, since we could start a call, have the server
806 * return status, have another machine make an update to the status (which
807 * doesn't change serverModTime), have the original machine get a new callback,
808 * and then have the original machine merge in the early, old info from the
809 * first call. At this point, the easiest way to avoid this problem is to have
810 * getcallback calls conflict with all others for the same vnode. Other calls
811 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
812 * vnode must be careful not to merge in their status unless they have obtained
813 * a callback from the start of their call.
816 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
817 * Each such RPC passes a FileLength parameter, which the server uses to do
818 * pre-truncation if necessary. So if two RPC's are processed out of order at
819 * the server, the one with the smaller FileLength will be processed last,
820 * possibly resulting in a bogus truncation. The simplest way to avoid this
821 * is to serialize all StoreData RPC's. This is the reason we defined
822 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
824 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
825 afs_uint32 rights, afs_uint32 flags)
827 osi_queueData_t *qdp;
830 afs_uint32 outRights;
832 afs_uint32 sleep_scp_flags = 0;
833 afs_uint32 sleep_buf_cmflags = 0;
834 afs_uint32 sleep_scp_bufs = 0;
836 /* lookup this first */
837 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
840 osi_assert(bufp->refCount > 0);
843 /* Do the access check. Now we don't really do the access check
844 * atomically, since the caller doesn't expect the parent dir to be
845 * returned locked, and that is what we'd have to do to prevent a
846 * callback breaking message on the parent due to a setacl call from
847 * being processed while we're running. So, instead, we check things
848 * here, and if things look fine with the access, we proceed to finish
849 * the rest of this check. Sort of a hack, but probably good enough.
853 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
854 /* if we're bringing in a new status block, ensure that
855 * we aren't already doing so, and that no one is
856 * changing the status concurrently, either. We need
857 * to do this, even if the status is of a different
858 * type, since we don't have the ability to figure out,
859 * in the AFS 3 protocols, which status-changing
860 * operation ran first, or even which order a read and
861 * a write occurred in.
863 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
864 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
865 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
869 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
870 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
871 /* if we're going to make an RPC to change the status, make sure
872 * that no one is bringing in or sending out the status.
874 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
875 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
876 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
879 if (scp->bufReadsp || scp->bufWritesp) {
880 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
884 if (flags & CM_SCACHESYNC_FETCHDATA) {
885 /* if we're bringing in a new chunk of data, make sure that
886 * nothing is happening to that chunk, and that we aren't
887 * changing the basic file status info, either.
889 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
890 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
891 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
894 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
895 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
899 if (flags & CM_SCACHESYNC_STOREDATA) {
900 /* same as fetch data */
901 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
902 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
903 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
906 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
907 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp, bufp);
912 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
913 /* Don't allow concurrent StoreData RPC's */
914 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
915 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
920 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
921 /* Don't allow more than one BKG store request */
922 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
923 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
928 if (flags & CM_SCACHESYNC_LOCK) {
929 /* Don't allow concurrent fiddling with lock lists */
930 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
931 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
936 /* now the operations that don't correspond to making RPCs */
937 if (flags & CM_SCACHESYNC_GETSTATUS) {
938 /* we can use the status that's here, if we're not
939 * bringing in new status.
941 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
942 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
946 if (flags & CM_SCACHESYNC_SETSTATUS) {
947 /* we can make a change to the local status, as long as
948 * the status isn't changing now.
950 * If we're fetching or storing a chunk of data, we can
951 * change the status locally, since the fetch/store
952 * operations don't change any of the data that we're
955 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
956 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
960 if (flags & CM_SCACHESYNC_READ) {
961 /* we're going to read the data, make sure that the
962 * status is available, and that the data is here. It
963 * is OK to read while storing the data back.
965 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
966 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
969 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
970 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
974 if (flags & CM_SCACHESYNC_WRITE) {
975 /* don't write unless the status is stable and the chunk
978 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
979 | CM_SCACHEFLAG_SIZESTORING)) {
980 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
983 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
984 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
989 // yj: modified this so that callback only checked if we're
990 // not checking something on /afs
991 /* fix the conditional to match the one in cm_HaveCallback */
992 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
993 #ifdef AFS_FREELANCE_CLIENT
994 && (!cm_freelanceEnabled ||
995 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
996 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
997 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
998 cm_fakeDirCallback < 2)
999 #endif /* AFS_FREELANCE_CLIENT */
1001 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1002 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1005 lock_ReleaseMutex(&bufp->mx);
1006 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1008 lock_ReleaseMutex(&scp->mx);
1009 lock_ObtainMutex(&bufp->mx);
1010 lock_ObtainMutex(&scp->mx);
1014 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1020 /* can't check access rights without a callback */
1021 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1023 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1024 return CM_ERROR_READONLY;
1026 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1027 if (~outRights & rights)
1028 return CM_ERROR_NOACCESS;
1031 /* we don't know the required access rights */
1032 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1033 code = cm_GetAccessRights(scp, userp, reqp);
1035 lock_ReleaseMutex(&scp->mx);
1036 lock_ObtainMutex(&bufp->mx);
1037 lock_ObtainMutex(&scp->mx);
1045 /* if we get here, we're happy */
1049 /* first check if we're not supposed to wait: fail
1050 * in this case, returning with everything still locked.
1052 if (flags & CM_SCACHESYNC_NOWAIT)
1053 return CM_ERROR_WOULDBLOCK;
1055 sleep_scp_flags = scp->flags; /* so we know why we slept */
1056 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1057 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1059 /* wait here, then try again */
1060 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1061 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1063 scp->waitRequests++;
1064 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1065 scp, scp->waitCount, scp->waitRequests);
1067 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1068 scp->flags |= CM_SCACHEFLAG_WAITING;
1069 scp->waitCount = scp->waitRequests = 1;
1072 lock_ReleaseMutex(&bufp->mx);
1073 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1075 smb_UpdateServerPriority();
1078 lock_ObtainMutex(&bufp->mx);
1079 lock_ObtainMutex(&scp->mx);
1081 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1082 scp, scp->waitCount, scp->waitRequests);
1083 if (scp->waitCount == 0) {
1084 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1085 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1086 scp->waitRequests = 0;
1088 } /* big while loop */
1090 /* now, update the recorded state for RPC-type calls */
1091 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1092 scp->flags |= CM_SCACHEFLAG_FETCHING;
1093 if (flags & CM_SCACHESYNC_STORESTATUS)
1094 scp->flags |= CM_SCACHEFLAG_STORING;
1095 if (flags & CM_SCACHESYNC_STORESIZE)
1096 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1097 if (flags & CM_SCACHESYNC_GETCALLBACK)
1098 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1099 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1100 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1101 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1102 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1103 if (flags & CM_SCACHESYNC_LOCK)
1104 scp->flags |= CM_SCACHEFLAG_LOCKING;
1106 /* now update the buffer pointer */
1107 if (flags & CM_SCACHESYNC_FETCHDATA) {
1108 /* ensure that the buffer isn't already in the I/O list */
1110 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1111 tbufp = osi_GetQData(qdp);
1112 osi_assert(tbufp != bufp);
1116 /* queue a held reference to the buffer in the "reading" I/O list */
1117 qdp = osi_QDAlloc();
1118 osi_SetQData(qdp, bufp);
1121 bufp->cmFlags |= CM_BUF_CMFETCHING;
1123 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1126 if (flags & CM_SCACHESYNC_STOREDATA) {
1127 /* ensure that the buffer isn't already in the I/O list */
1129 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1130 tbufp = osi_GetQData(qdp);
1131 osi_assert(tbufp != bufp);
1135 /* queue a held reference to the buffer in the "writing" I/O list */
1136 qdp = osi_QDAlloc();
1137 osi_SetQData(qdp, bufp);
1140 bufp->cmFlags |= CM_BUF_CMSTORING;
1142 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1148 /* for those syncops that setup for RPCs.
1149 * Called with scache locked.
1151 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1153 osi_queueData_t *qdp;
1156 lock_AssertMutex(&scp->mx);
1158 /* now, update the recorded state for RPC-type calls */
1159 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1160 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1161 if (flags & CM_SCACHESYNC_STORESTATUS)
1162 scp->flags &= ~CM_SCACHEFLAG_STORING;
1163 if (flags & CM_SCACHESYNC_STORESIZE)
1164 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1165 if (flags & CM_SCACHESYNC_GETCALLBACK)
1166 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1167 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1168 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1169 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1170 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1171 if (flags & CM_SCACHESYNC_LOCK)
1172 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1174 /* now update the buffer pointer */
1175 if (flags & CM_SCACHESYNC_FETCHDATA) {
1176 /* ensure that the buffer isn't already in the I/O list */
1177 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1178 tbufp = osi_GetQData(qdp);
1183 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1188 if (bufp->cmFlags & CM_BUF_CMFETCHING)
1190 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1191 if (bufp->flags & CM_BUF_WAITING) {
1192 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1193 osi_Wakeup((LONG_PTR) &bufp);
1200 /* now update the buffer pointer */
1201 if (flags & CM_SCACHESYNC_STOREDATA) {
1202 /* ensure that the buffer isn't already in the I/O list */
1203 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1204 tbufp = osi_GetQData(qdp);
1209 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1214 if (bufp->cmFlags & CM_BUF_CMSTORING)
1216 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1217 if (bufp->flags & CM_BUF_WAITING) {
1218 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1219 osi_Wakeup((LONG_PTR) &bufp);
1226 /* and wakeup anyone who is waiting */
1227 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1228 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1229 osi_Wakeup((LONG_PTR) &scp->flags);
1233 /* merge in a response from an RPC. The scp must be locked, and the callback
1236 * Don't overwrite any status info that is dirty, since we could have a store
1237 * operation (such as store data) that merges some info in, and we don't want
1238 * to lose the local updates. Typically, there aren't many updates we do
1239 * locally, anyway, probably only mtime.
1241 * There is probably a bug in here where a chmod (which doesn't change
1242 * serverModTime) that occurs between two fetches, both of whose responses are
1243 * handled after the callback breaking is done, but only one of whose calls
1244 * started before that, can cause old info to be merged from the first call.
1246 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
1247 cm_user_t *userp, afs_uint32 flags)
1249 // yj: i want to create some fake status for the /afs directory and the
1250 // entries under that directory
1251 #ifdef AFS_FREELANCE_CLIENT
1252 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1253 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1254 statusp->InterfaceVersion = 0x1;
1255 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1256 statusp->LinkCount = scp->linkCount;
1257 statusp->Length = cm_fakeDirSize;
1258 statusp->Length_hi = 0;
1259 statusp->DataVersion = cm_data.fakeDirVersion;
1260 statusp->Author = 0x1;
1261 statusp->Owner = 0x0;
1262 statusp->CallerAccess = 0x9;
1263 statusp->AnonymousAccess = 0x9;
1264 statusp->UnixModeBits = 0x1ff;
1265 statusp->ParentVnode = 0x1;
1266 statusp->ParentUnique = 0x1;
1267 statusp->ResidencyMask = 0;
1268 statusp->ClientModTime = FakeFreelanceModTime;
1269 statusp->ServerModTime = FakeFreelanceModTime;
1271 statusp->SyncCounter = 0;
1272 statusp->dataVersionHigh = 0;
1273 statusp->errorCode = 0;
1275 #endif /* AFS_FREELANCE_CLIENT */
1277 if (statusp->errorCode != 0) {
1278 scp->flags |= CM_SCACHEFLAG_EACCESS;
1279 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1282 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1285 if (!(flags & CM_MERGEFLAG_FORCE)
1286 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1287 struct cm_cell *cellp;
1289 cellp = cm_FindCellByID(scp->fid.cell);
1290 if (scp->cbServerp) {
1291 struct cm_volume *volp = NULL;
1293 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1294 (cm_req_t *) NULL, &volp);
1295 osi_Log2(afsd_logp, "old data from server %x volume %s",
1296 scp->cbServerp->addr.sin_addr.s_addr,
1297 volp ? volp->namep : "(unknown)");
1301 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1302 scp, scp->dataVersion, statusp->DataVersion);
1303 /* we have a number of data fetch/store operations running
1304 * concurrently, and we can tell which one executed last at the
1305 * server by its mtime.
1306 * Choose the one with the largest mtime, and ignore the rest.
1308 * These concurrent calls are incompatible with setting the
1309 * mtime, so we won't have a locally changed mtime here.
1311 * We could also have ACL info for a different user than usual,
1312 * in which case we have to do that part of the merge, anyway.
1313 * We won't have to worry about the info being old, since we
1314 * won't have concurrent calls
1315 * that change file status running from this machine.
1317 * Added 3/17/98: if we see data version regression on an RO
1318 * file, it's probably due to a server holding an out-of-date
1319 * replica, rather than to concurrent RPC's. Failures to
1320 * release replicas are now flagged by the volserver, but only
1321 * since AFS 3.4 5.22, so there are plenty of clients getting
1322 * out-of-date replicas out there.
1324 * If we discover an out-of-date replica, by this time it's too
1325 * late to go to another server and retry. Also, we can't
1326 * reject the merge, because then there is no way for
1327 * GetAccess to do its work, and the caller gets into an
1328 * infinite loop. So we just grin and bear it.
1330 if (!(scp->flags & CM_SCACHEFLAG_RO))
1333 scp->serverModTime = statusp->ServerModTime;
1335 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1336 scp->clientModTime = statusp->ClientModTime;
1338 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1339 scp->length.LowPart = statusp->Length;
1340 scp->length.HighPart = statusp->Length_hi;
1343 scp->serverLength.LowPart = statusp->Length;
1344 scp->serverLength.HighPart = statusp->Length_hi;
1346 scp->linkCount = statusp->LinkCount;
1347 scp->dataVersion = statusp->DataVersion;
1348 scp->owner = statusp->Owner;
1349 scp->group = statusp->Group;
1350 scp->unixModeBits = statusp->UnixModeBits & 07777;
1352 if (statusp->FileType == File)
1353 scp->fileType = CM_SCACHETYPE_FILE;
1354 else if (statusp->FileType == Directory)
1355 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1356 else if (statusp->FileType == SymbolicLink) {
1357 if ((scp->unixModeBits & 0111) == 0)
1358 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1360 scp->fileType = CM_SCACHETYPE_SYMLINK;
1363 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1364 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1366 /* and other stuff */
1367 scp->parentVnode = statusp->ParentVnode;
1368 scp->parentUnique = statusp->ParentUnique;
1370 /* and merge in the private acl cache info, if this is more than the public
1371 * info; merge in the public stuff in any case.
1373 scp->anyAccess = statusp->AnonymousAccess;
1375 if (userp != NULL) {
1376 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1380 /* note that our stat cache info is incorrect, so force us eventually
1381 * to stat the file again. There may be dirty data associated with
1382 * this vnode, and we want to preserve that information.
1384 * This function works by simply simulating a loss of the callback.
1386 * This function must be called with the scache locked.
1388 void cm_DiscardSCache(cm_scache_t *scp)
1390 lock_AssertMutex(&scp->mx);
1391 if (scp->cbServerp) {
1392 cm_PutServer(scp->cbServerp);
1393 scp->cbServerp = NULL;
1396 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1397 cm_dnlcPurgedp(scp);
1398 cm_dnlcPurgevp(scp);
1399 cm_FreeAllACLEnts(scp);
1401 /* Force mount points and symlinks to be re-evaluated */
1402 scp->mountPointStringp[0] = '\0';
1405 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1407 afsFidp->Volume = fidp->volume;
1408 afsFidp->Vnode = fidp->vnode;
1409 afsFidp->Unique = fidp->unique;
1412 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1414 osi_assert(scp != 0);
1416 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%x ref %d",scp, scp->refCount);
1419 void cm_HoldSCache(cm_scache_t *scp)
1421 osi_assert(scp != 0);
1422 lock_ObtainWrite(&cm_scacheLock);
1424 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%x ref %d",scp, scp->refCount);
1425 lock_ReleaseWrite(&cm_scacheLock);
1428 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1430 osi_assert(scp != NULL);
1431 if (scp->refCount == 0)
1432 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1433 osi_assert(scp->refCount-- >= 0);
1434 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%x ref %d",scp,scp->refCount);
1437 void cm_ReleaseSCache(cm_scache_t *scp)
1439 osi_assert(scp != NULL);
1440 lock_ObtainWrite(&cm_scacheLock);
1441 if (scp->refCount == 0)
1442 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1443 osi_assert(scp->refCount != 0);
1445 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%x ref %d",scp,scp->refCount);
1446 lock_ReleaseWrite(&cm_scacheLock);
1449 /* just look for the scp entry to get filetype */
1450 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1451 int cm_FindFileType(cm_fid_t *fidp)
1456 hash = CM_SCACHE_HASH(fidp);
1458 osi_assert(fidp->cell != 0);
1460 lock_ObtainWrite(&cm_scacheLock);
1461 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
1462 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1463 lock_ReleaseWrite(&cm_scacheLock);
1464 return scp->fileType;
1467 lock_ReleaseWrite(&cm_scacheLock);
1471 /* dump all scp's that have reference count > 0 to a file.
1472 * cookie is used to identify this batch for easy parsing,
1473 * and it a string provided by a caller
1475 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1483 lock_ObtainRead(&cm_scacheLock);
1485 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1486 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1488 for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q))
1490 if (scp->refCount != 0)
1492 sprintf(output, "%s fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1493 cookie, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1495 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1499 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.hashTableSize=%d\n", cookie, cm_data.hashTableSize);
1500 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1502 for (i = 0; i < cm_data.hashTableSize; i++)
1504 for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp)
1506 if (scp->refCount != 0)
1508 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1509 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1510 scp->fid.unique, scp->refCount);
1511 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1516 sprintf(output, "%s - Done dumping scache.\n", cookie);
1517 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1520 lock_ReleaseRead(&cm_scacheLock);