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.
55 * this function ignores all of the locking hierarchy.
57 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
63 if (scp->refCount != 0) {
67 if (scp->flags & CM_SCACHEFLAG_INHASH) {
68 /* hash it out first */
69 i = CM_SCACHE_HASH(&scp->fid);
70 for (lscpp = &cm_data.hashTablep[i], tscp = cm_data.hashTablep[i];
72 lscpp = &tscp->nextp, tscp = tscp->nextp) {
75 scp->flags &= ~CM_SCACHEFLAG_INHASH;
82 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
86 while(qdp = scp->bufWritesp) {
87 bufp = osi_GetQData(qdp);
88 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
91 lock_ObtainMutex(&bufp->mx);
92 bufp->cmFlags &= ~CM_BUF_CMSTORING;
93 bufp->flags &= ~CM_BUF_DIRTY;
94 bufp->flags |= CM_BUF_ERROR;
95 bufp->error = VNOVNODE;
96 bufp->dataVersion = -1; /* bad */
98 if (bufp->flags & CM_BUF_WAITING) {
99 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
100 osi_Wakeup((long) &bufp);
102 lock_ReleaseMutex(&bufp->mx);
106 while(qdp = scp->bufReadsp) {
107 bufp = osi_GetQData(qdp);
108 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
111 lock_ObtainMutex(&bufp->mx);
112 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
113 bufp->flags &= ~CM_BUF_DIRTY;
114 bufp->flags |= CM_BUF_ERROR;
115 bufp->error = VNOVNODE;
116 bufp->dataVersion = -1; /* bad */
117 bufp->dirtyCounter++;
118 if (bufp->flags & CM_BUF_WAITING) {
119 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
120 osi_Wakeup((long) &bufp);
122 lock_ReleaseMutex(&bufp->mx);
126 buf_CleanDirtyBuffers(scp);
128 /* look for things that shouldn't still be set */
129 osi_assert(scp->bufWritesp == NULL);
130 osi_assert(scp->bufReadsp == NULL);
134 /* invalidate so next merge works fine;
135 * also initialize some flags */
136 scp->flags &= ~(CM_SCACHEFLAG_STATD
137 | CM_SCACHEFLAG_DELETED
139 | CM_SCACHEFLAG_PURERO
140 | CM_SCACHEFLAG_OVERQUOTA
141 | CM_SCACHEFLAG_OUTOFSPACE);
142 scp->serverModTime = 0;
143 scp->dataVersion = 0;
144 scp->bulkStatProgress = hzero;
152 /* discard callback */
153 if (scp->cbServerp) {
154 cm_PutServer(scp->cbServerp);
155 scp->cbServerp = NULL;
159 /* remove from dnlc */
163 /* discard cached status; if non-zero, Close
164 * tried to store this to server but failed */
167 /* drop held volume ref */
169 cm_PutVolume(scp->volp);
173 /* discard symlink info */
174 scp->mountPointStringp[0] = 0;
175 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
176 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
178 /* reset locking info */
179 scp->fileLocksH = NULL;
180 scp->fileLocksT = NULL;
181 scp->serverLock = (-1);
182 scp->exclusiveLocks = 0;
183 scp->sharedLocks = 0;
185 /* not locked, but there can be no references to this guy
186 * while we hold the global refcount lock.
188 cm_FreeAllACLEnts(scp);
193 /* called with cm_scacheLock write-locked; find a vnode to recycle.
194 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
196 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");
239 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
240 /* There were no deleted scache objects that we could use. Try to find
241 * 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);
272 /* if we get here, we should allocate a new scache entry. We either are below
273 * quota or we have a leak and need to allocate a new one to avoid panicing.
275 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
276 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
277 memset(scp, 0, sizeof(cm_scache_t));
278 scp->magic = CM_SCACHE_MAGIC;
279 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
280 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
281 scp->serverLock = -1;
283 /* and put it in the LRU queue */
284 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
285 if (!cm_data.scacheLRULastp)
286 cm_data.scacheLRULastp = scp;
287 cm_data.currentSCaches++;
288 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
293 /* like strcmp, only for fids */
294 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
296 if (ap->vnode != bp->vnode)
298 if (ap->volume != bp->volume)
300 if (ap->unique != bp->unique)
302 if (ap->cell != bp->cell)
307 void cm_fakeSCacheInit(int newFile)
310 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
311 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
312 /* can leave clientModTime at 0 */
313 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
314 cm_data.fakeSCache.unixModeBits = 0777;
315 cm_data.fakeSCache.length.LowPart = 1000;
316 cm_data.fakeSCache.linkCount = 1;
317 cm_data.fakeSCache.refCount = 1;
319 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
323 cm_ValidateSCache(void)
325 cm_scache_t * scp, *lscp;
328 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
329 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
330 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
331 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
335 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
337 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
338 if (scp->magic != CM_SCACHE_MAGIC) {
339 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
340 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
343 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
344 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
345 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
348 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
349 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
350 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
353 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
354 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
355 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
358 if (i > cm_data.currentSCaches ) {
359 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
360 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
363 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
364 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
365 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
370 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
371 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
372 if (scp->magic != CM_SCACHE_MAGIC) {
373 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
374 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
377 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
378 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
379 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
382 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
383 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
384 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
387 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
388 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
389 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
392 if (i > cm_data.currentSCaches ) {
393 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
394 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
397 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
398 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
399 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
404 for ( i=0; i < cm_data.hashTableSize; i++ ) {
405 for ( scp = cm_data.hashTablep[i]; scp; scp = scp->nextp ) {
406 if (scp->magic != CM_SCACHE_MAGIC) {
407 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
408 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
411 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
412 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
413 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
416 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
417 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
418 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
421 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
422 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
423 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
429 return cm_dnlcValidate();
433 cm_ShutdownSCache(void)
437 for ( scp = cm_data.scacheLRULastp; scp;
438 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
439 if (scp->randomACLp) {
440 lock_ObtainMutex(&scp->mx);
441 cm_FreeAllACLEnts(scp);
442 lock_ReleaseMutex(&scp->mx);
444 lock_FinalizeMutex(&scp->mx);
445 lock_FinalizeRWLock(&scp->bufCreateLock);
448 return cm_dnlcShutdown();
451 void cm_InitSCache(int newFile, long maxSCaches)
453 static osi_once_t once;
455 if (osi_Once(&once)) {
456 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
458 memset(cm_data.hashTablep, 0, sizeof(cm_scache_t *) * cm_data.hashTableSize);
459 cm_data.currentSCaches = 0;
460 cm_data.maxSCaches = maxSCaches;
461 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
465 for ( scp = cm_data.scacheLRULastp; scp;
466 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
467 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
468 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
470 scp->cbServerp = NULL;
472 scp->fileLocksH = NULL;
473 scp->fileLocksT = NULL;
474 scp->serverLock = (-1);
475 scp->lastRefreshCycle = 0;
476 scp->exclusiveLocks = 0;
477 scp->sharedLocks = 0;
483 scp->flags &= ~CM_SCACHEFLAG_WAITING;
486 cm_allFileLocks = NULL;
487 cm_freeFileLocks = NULL;
488 cm_lockRefreshCycle = 0;
489 cm_fakeSCacheInit(newFile);
490 cm_dnlcInit(newFile);
495 /* version that doesn't bother creating the entry if we don't find it */
496 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
501 hash = CM_SCACHE_HASH(fidp);
503 if (fidp->cell == 0) {
507 lock_ObtainWrite(&cm_scacheLock);
508 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
509 if (cm_FidCmp(fidp, &scp->fid) == 0) {
510 cm_HoldSCacheNoLock(scp);
512 lock_ReleaseWrite(&cm_scacheLock);
516 lock_ReleaseWrite(&cm_scacheLock);
520 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
526 cm_volume_t *volp = 0;
529 int special; // yj: boolean variable to test if file is on root.afs
531 extern cm_fid_t cm_rootFid;
533 hash = CM_SCACHE_HASH(fidp);
535 osi_assert(fidp->cell != 0);
537 if (fidp->cell== cm_data.rootFid.cell &&
538 fidp->volume==cm_data.rootFid.volume &&
539 fidp->vnode==0x0 && fidp->unique==0x0)
541 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
544 // yj: check if we have the scp, if so, we don't need
545 // to do anything else
546 lock_ObtainWrite(&cm_scacheLock);
547 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
548 if (cm_FidCmp(fidp, &scp->fid) == 0) {
549 cm_HoldSCacheNoLock(scp);
552 lock_ReleaseWrite(&cm_scacheLock);
557 // yj: when we get here, it means we don't have an scp
558 // so we need to either load it or fake it, depending
559 // on whether the file is "special", see below.
561 // yj: if we're trying to get an scp for a file that's
562 // on root.afs of homecell, we want to handle it specially
563 // because we have to fill in the status stuff 'coz we
564 // don't want trybulkstat to fill it in for us
565 #ifdef AFS_FREELANCE_CLIENT
566 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
567 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
568 !(fidp->vnode==0x1 && fidp->unique==0x1));
569 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
570 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
571 fidp->vnode==0x1 && fidp->unique==0x1);
572 if (cm_freelanceEnabled && isRoot) {
573 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
574 /* freelance: if we are trying to get the root scp for the first
575 * time, we will just put in a place holder entry.
580 if (cm_freelanceEnabled && special) {
581 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
582 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
583 lock_ObtainMutex(&cm_Freelance_Lock);
584 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
585 lock_ReleaseMutex(&cm_Freelance_Lock);
589 scp = cm_GetNewSCache();
591 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
592 lock_ReleaseWrite(&cm_scacheLock);
593 return CM_ERROR_WOULDBLOCK;
596 lock_ReleaseWrite(&cm_scacheLock);
597 lock_ObtainMutex(&scp->mx);
598 lock_ObtainWrite(&cm_scacheLock);
600 scp->volp = cm_data.rootSCachep->volp;
601 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
602 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
603 scp->dotdotFid.unique=1;
604 scp->dotdotFid.vnode=1;
605 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
606 scp->nextp=cm_data.hashTablep[hash];
607 cm_data.hashTablep[hash]=scp;
608 scp->flags |= CM_SCACHEFLAG_INHASH;
610 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
611 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
612 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
614 scp->fileType = CM_SCACHETYPE_INVALID;
616 lock_ObtainMutex(&cm_Freelance_Lock);
617 scp->length.LowPart = (DWORD)strlen(mp)+4;
618 scp->length.HighPart = 0;
619 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
620 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
621 lock_ReleaseMutex(&cm_Freelance_Lock);
624 scp->unixModeBits=0x1ff;
625 scp->clientModTime=FakeFreelanceModTime;
626 scp->serverModTime=FakeFreelanceModTime;
627 scp->parentUnique = 0x1;
628 scp->parentVnode=0x1;
630 scp->dataVersion=cm_data.fakeDirVersion;
631 scp->lockDataVersion=-1; /* no lock yet */
632 lock_ReleaseMutex(&scp->mx);
634 lock_ReleaseWrite(&cm_scacheLock);
638 #endif /* AFS_FREELANCE_CLIENT */
640 /* otherwise, we need to find the volume */
641 if (!cm_freelanceEnabled || !isRoot) {
642 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
643 cellp = cm_FindCellByID(fidp->cell);
645 return CM_ERROR_NOSUCHCELL;
647 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
650 lock_ObtainWrite(&cm_scacheLock);
653 /* otherwise, we have the volume, now reverify that the scp doesn't
654 * exist, and proceed.
656 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
657 if (cm_FidCmp(fidp, &scp->fid) == 0) {
658 cm_HoldSCacheNoLock(scp);
659 osi_assert(scp->volp == volp);
661 lock_ReleaseWrite(&cm_scacheLock);
669 /* now, if we don't have the fid, recycle something */
670 scp = cm_GetNewSCache();
672 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
673 lock_ReleaseWrite(&cm_scacheLock);
674 return CM_ERROR_WOULDBLOCK;
676 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
678 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
679 lock_ReleaseWrite(&cm_scacheLock);
680 lock_ObtainMutex(&scp->mx);
681 lock_ObtainWrite(&cm_scacheLock);
683 scp->volp = volp; /* a held reference */
685 if (!cm_freelanceEnabled || !isRoot) {
686 /* if this scache entry represents a volume root then we need
687 * to copy the dotdotFipd from the volume structure where the
688 * "master" copy is stored (defect 11489)
690 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
691 scp->dotdotFid = volp->dotdotFid;
694 if (volp->roID == fidp->volume)
695 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
696 else if (volp->bkID == fidp->volume)
697 scp->flags |= CM_SCACHEFLAG_RO;
699 scp->nextp = cm_data.hashTablep[hash];
700 cm_data.hashTablep[hash] = scp;
701 scp->flags |= CM_SCACHEFLAG_INHASH;
703 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
704 lock_ReleaseMutex(&scp->mx);
706 /* XXX - The following fields in the cm_scache are
712 lock_ReleaseWrite(&cm_scacheLock);
714 /* now we have a held scache entry; just return it */
719 /* Returns a held reference to the scache's parent
721 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
726 cm_scache_t * pscp = NULL;
728 lock_ObtainRead(&cm_scacheLock);
729 parent_fid = scp->fid;
730 parent_fid.vnode = scp->parentVnode;
731 parent_fid.unique = scp->parentUnique;
733 if (cm_FidCmp(&scp->fid, &parent_fid)) {
734 for (i=0; i<cm_data.hashTableSize; i++) {
735 for (pscp = cm_data.hashTablep[i]; pscp; pscp = pscp->nextp) {
736 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
737 cm_HoldSCacheNoLock(pscp);
743 lock_ReleaseRead(&cm_scacheLock);
748 /* synchronize a fetch, store, read, write, fetch status or store status.
749 * Called with scache mutex held, and returns with it held, but temporarily
750 * drops it during the fetch.
752 * At most one flag can be on in flags, if this is an RPC request.
754 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
756 * There are a lot of weird restrictions here; here's an attempt to explain the
757 * rationale for the concurrency restrictions implemented in this function.
759 * First, although the file server will break callbacks when *another* machine
760 * modifies a file or status block, the client itself is responsible for
761 * concurrency control on its own requests. Callback breaking events are rare,
762 * and simply invalidate any concurrent new status info.
764 * In the absence of callback breaking messages, we need to know how to
765 * synchronize incoming responses describing updates to files. We synchronize
766 * operations that update the data version by comparing the data versions.
767 * However, updates that do not update the data, but only the status, can't be
768 * synchronized with fetches or stores, since there's nothing to compare
769 * to tell which operation executed first at the server.
771 * Thus, we can allow multiple ops that change file data, or dir data, and
772 * fetches. However, status storing ops have to be done serially.
774 * Furthermore, certain data-changing ops are incompatible: we can't read or
775 * write a buffer while doing a truncate. We can't read and write the same
776 * buffer at the same time, or write while fetching or storing, or read while
777 * fetching a buffer (this may change). We can't fetch and store at the same
780 * With respect to status, we can't read and write at the same time, read while
781 * fetching, write while fetching or storing, or fetch and store at the same time.
783 * We can't allow a get callback RPC to run in concurrently with something that
784 * will return updated status, since we could start a call, have the server
785 * return status, have another machine make an update to the status (which
786 * doesn't change serverModTime), have the original machine get a new callback,
787 * and then have the original machine merge in the early, old info from the
788 * first call. At this point, the easiest way to avoid this problem is to have
789 * getcallback calls conflict with all others for the same vnode. Other calls
790 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
791 * vnode must be careful not to merge in their status unless they have obtained
792 * a callback from the start of their call.
795 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
796 * Each such RPC passes a FileLength parameter, which the server uses to do
797 * pre-truncation if necessary. So if two RPC's are processed out of order at
798 * the server, the one with the smaller FileLength will be processed last,
799 * possibly resulting in a bogus truncation. The simplest way to avoid this
800 * is to serialize all StoreData RPC's. This is the reason we defined
801 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
803 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
804 afs_uint32 rights, afs_uint32 flags)
806 osi_queueData_t *qdp;
809 afs_uint32 outRights;
811 afs_uint32 sleep_scp_flags = 0;
812 afs_uint32 sleep_buf_cmflags = 0;
813 afs_uint32 sleep_scp_bufs = 0;
815 /* lookup this first */
816 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
818 /* some minor assertions */
819 if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
820 | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
821 | CM_SCACHESYNC_SETSIZE)) {
823 osi_assert(bufp->refCount > 0);
825 osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
829 else osi_assert(bufp == NULL);
831 /* Do the access check. Now we don't really do the access check
832 * atomically, since the caller doesn't expect the parent dir to be
833 * returned locked, and that is what we'd have to do to prevent a
834 * callback breaking message on the parent due to a setacl call from
835 * being processed while we're running. So, instead, we check things
836 * here, and if things look fine with the access, we proceed to finish
837 * the rest of this check. Sort of a hack, but probably good enough.
841 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
842 /* if we're bringing in a new status block, ensure that
843 * we aren't already doing so, and that no one is
844 * changing the status concurrently, either. We need
845 * to do this, even if the status is of a different
846 * type, since we don't have the ability to figure out,
847 * in the AFS 3 protocols, which status-changing
848 * operation ran first, or even which order a read and
849 * a write occurred in.
851 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
852 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
853 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
857 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
858 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
859 /* if we're going to make an RPC to change the status, make sure
860 * that no one is bringing in or sending out the status.
862 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
863 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
864 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
867 if (scp->bufReadsp || scp->bufWritesp) {
868 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
872 if (flags & CM_SCACHESYNC_FETCHDATA) {
873 /* if we're bringing in a new chunk of data, make sure that
874 * nothing is happening to that chunk, and that we aren't
875 * changing the basic file status info, either.
877 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
878 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
879 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
882 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
883 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
887 if (flags & CM_SCACHESYNC_STOREDATA) {
888 /* same as fetch data */
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 STOREDATA", 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 STOREDATA", scp, bufp);
900 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
901 /* Don't allow concurrent StoreData RPC's */
902 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
903 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
908 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
909 /* Don't allow more than one BKG store request */
910 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
911 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
916 if (flags & CM_SCACHESYNC_LOCK) {
917 /* Don't allow concurrent fiddling with lock lists */
918 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
919 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
924 /* now the operations that don't correspond to making RPCs */
925 if (flags & CM_SCACHESYNC_GETSTATUS) {
926 /* we can use the status that's here, if we're not
927 * bringing in new status.
929 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
930 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
934 if (flags & CM_SCACHESYNC_SETSTATUS) {
935 /* we can make a change to the local status, as long as
936 * the status isn't changing now.
938 * If we're fetching or storing a chunk of data, we can
939 * change the status locally, since the fetch/store
940 * operations don't change any of the data that we're
943 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
944 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
948 if (flags & CM_SCACHESYNC_READ) {
949 /* we're going to read the data, make sure that the
950 * status is available, and that the data is here. It
951 * is OK to read while storing the data back.
953 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
954 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
957 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
958 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
962 if (flags & CM_SCACHESYNC_WRITE) {
963 /* don't write unless the status is stable and the chunk
966 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
967 | CM_SCACHEFLAG_SIZESTORING)) {
968 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
971 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
972 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
977 // yj: modified this so that callback only checked if we're
978 // not checking something on /afs
979 /* fix the conditional to match the one in cm_HaveCallback */
980 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
981 #ifdef AFS_FREELANCE_CLIENT
982 && (!cm_freelanceEnabled ||
983 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
984 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
985 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
986 cm_fakeDirCallback < 2)
987 #endif /* AFS_FREELANCE_CLIENT */
989 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
990 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
993 lock_ReleaseMutex(&bufp->mx);
994 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
996 lock_ReleaseMutex(&scp->mx);
997 lock_ObtainMutex(&bufp->mx);
998 lock_ObtainMutex(&scp->mx);
1002 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1008 /* can't check access rights without a callback */
1009 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1011 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1012 return CM_ERROR_READONLY;
1014 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1015 if (~outRights & rights)
1016 return CM_ERROR_NOACCESS;
1019 /* we don't know the required access rights */
1020 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1021 code = cm_GetAccessRights(scp, userp, reqp);
1023 lock_ReleaseMutex(&scp->mx);
1024 lock_ObtainMutex(&bufp->mx);
1025 lock_ObtainMutex(&scp->mx);
1033 /* if we get here, we're happy */
1037 /* first check if we're not supposed to wait: fail
1038 * in this case, returning with everything still locked.
1040 if (flags & CM_SCACHESYNC_NOWAIT)
1041 return CM_ERROR_WOULDBLOCK;
1043 sleep_scp_flags = scp->flags; /* so we know why we slept */
1044 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1045 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1047 /* wait here, then try again */
1048 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1049 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1051 scp->waitRequests++;
1052 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1053 scp, scp->waitCount, scp->waitRequests);
1055 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1056 scp->flags |= CM_SCACHEFLAG_WAITING;
1057 scp->waitCount = scp->waitRequests = 1;
1060 lock_ReleaseMutex(&bufp->mx);
1061 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1063 smb_UpdateServerPriority();
1066 lock_ObtainMutex(&bufp->mx);
1067 lock_ObtainMutex(&scp->mx);
1069 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1070 scp, scp->waitCount, scp->waitRequests);
1071 if (scp->waitCount == 0) {
1072 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1073 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1074 scp->waitRequests = 0;
1076 } /* big while loop */
1078 /* now, update the recorded state for RPC-type calls */
1079 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1080 scp->flags |= CM_SCACHEFLAG_FETCHING;
1081 if (flags & CM_SCACHESYNC_STORESTATUS)
1082 scp->flags |= CM_SCACHEFLAG_STORING;
1083 if (flags & CM_SCACHESYNC_STORESIZE)
1084 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1085 if (flags & CM_SCACHESYNC_GETCALLBACK)
1086 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1087 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1088 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1089 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1090 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1091 if (flags & CM_SCACHESYNC_LOCK)
1092 scp->flags |= CM_SCACHEFLAG_LOCKING;
1094 /* now update the buffer pointer */
1095 if (flags & CM_SCACHESYNC_FETCHDATA) {
1096 /* ensure that the buffer isn't already in the I/O list */
1098 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1099 tbufp = osi_GetQData(qdp);
1100 osi_assert(tbufp != bufp);
1104 /* queue a held reference to the buffer in the "reading" I/O list */
1105 qdp = osi_QDAlloc();
1106 osi_SetQData(qdp, bufp);
1109 bufp->cmFlags |= CM_BUF_CMFETCHING;
1111 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1114 if (flags & CM_SCACHESYNC_STOREDATA) {
1115 /* ensure that the buffer isn't already in the I/O list */
1117 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1118 tbufp = osi_GetQData(qdp);
1119 osi_assert(tbufp != bufp);
1123 /* queue a held reference to the buffer in the "writing" I/O list */
1124 qdp = osi_QDAlloc();
1125 osi_SetQData(qdp, bufp);
1128 bufp->cmFlags |= CM_BUF_CMSTORING;
1130 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1136 /* for those syncops that setup for RPCs.
1137 * Called with scache locked.
1139 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1141 osi_queueData_t *qdp;
1144 lock_AssertMutex(&scp->mx);
1146 /* now, update the recorded state for RPC-type calls */
1147 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1148 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1149 if (flags & CM_SCACHESYNC_STORESTATUS)
1150 scp->flags &= ~CM_SCACHEFLAG_STORING;
1151 if (flags & CM_SCACHESYNC_STORESIZE)
1152 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1153 if (flags & CM_SCACHESYNC_GETCALLBACK)
1154 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1155 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1156 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1157 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1158 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1159 if (flags & CM_SCACHESYNC_LOCK)
1160 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1162 /* now update the buffer pointer */
1163 if (flags & CM_SCACHESYNC_FETCHDATA) {
1164 /* ensure that the buffer isn't already in the I/O list */
1165 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1166 tbufp = osi_GetQData(qdp);
1171 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1176 if (bufp->cmFlags & CM_BUF_CMFETCHING)
1178 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1179 if (bufp->flags & CM_BUF_WAITING) {
1180 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1181 osi_Wakeup((LONG_PTR) &bufp);
1188 /* now update the buffer pointer */
1189 if (flags & CM_SCACHESYNC_STOREDATA) {
1190 /* ensure that the buffer isn't already in the I/O list */
1191 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1192 tbufp = osi_GetQData(qdp);
1197 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1202 if (bufp->cmFlags & CM_BUF_CMSTORING)
1204 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1205 if (bufp->flags & CM_BUF_WAITING) {
1206 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1207 osi_Wakeup((LONG_PTR) &bufp);
1214 /* and wakeup anyone who is waiting */
1215 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1216 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1217 osi_Wakeup((LONG_PTR) &scp->flags);
1221 /* merge in a response from an RPC. The scp must be locked, and the callback
1224 * Don't overwrite any status info that is dirty, since we could have a store
1225 * operation (such as store data) that merges some info in, and we don't want
1226 * to lose the local updates. Typically, there aren't many updates we do
1227 * locally, anyway, probably only mtime.
1229 * There is probably a bug in here where a chmod (which doesn't change
1230 * serverModTime) that occurs between two fetches, both of whose responses are
1231 * handled after the callback breaking is done, but only one of whose calls
1232 * started before that, can cause old info to be merged from the first call.
1234 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
1235 cm_user_t *userp, afs_uint32 flags)
1237 // yj: i want to create some fake status for the /afs directory and the
1238 // entries under that directory
1239 #ifdef AFS_FREELANCE_CLIENT
1240 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1241 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1242 statusp->InterfaceVersion = 0x1;
1243 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1244 statusp->LinkCount = scp->linkCount;
1245 statusp->Length = cm_fakeDirSize;
1246 statusp->Length_hi = 0;
1247 statusp->DataVersion = cm_data.fakeDirVersion;
1248 statusp->Author = 0x1;
1249 statusp->Owner = 0x0;
1250 statusp->CallerAccess = 0x9;
1251 statusp->AnonymousAccess = 0x9;
1252 statusp->UnixModeBits = 0x1ff;
1253 statusp->ParentVnode = 0x1;
1254 statusp->ParentUnique = 0x1;
1255 statusp->ResidencyMask = 0;
1256 statusp->ClientModTime = FakeFreelanceModTime;
1257 statusp->ServerModTime = FakeFreelanceModTime;
1259 statusp->SyncCounter = 0;
1260 statusp->dataVersionHigh = 0;
1261 statusp->errorCode = 0;
1263 #endif /* AFS_FREELANCE_CLIENT */
1265 if (statusp->errorCode != 0) {
1266 scp->flags |= CM_SCACHEFLAG_EACCESS;
1267 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1270 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1273 if (!(flags & CM_MERGEFLAG_FORCE)
1274 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1275 struct cm_cell *cellp;
1277 cellp = cm_FindCellByID(scp->fid.cell);
1278 if (scp->cbServerp) {
1279 struct cm_volume *volp = NULL;
1281 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1282 (cm_req_t *) NULL, &volp);
1283 osi_Log2(afsd_logp, "old data from server %x volume %s",
1284 scp->cbServerp->addr.sin_addr.s_addr,
1285 volp ? volp->namep : "(unknown)");
1289 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1290 scp, scp->dataVersion, statusp->DataVersion);
1291 /* we have a number of data fetch/store operations running
1292 * concurrently, and we can tell which one executed last at the
1293 * server by its mtime.
1294 * Choose the one with the largest mtime, and ignore the rest.
1296 * These concurrent calls are incompatible with setting the
1297 * mtime, so we won't have a locally changed mtime here.
1299 * We could also have ACL info for a different user than usual,
1300 * in which case we have to do that part of the merge, anyway.
1301 * We won't have to worry about the info being old, since we
1302 * won't have concurrent calls
1303 * that change file status running from this machine.
1305 * Added 3/17/98: if we see data version regression on an RO
1306 * file, it's probably due to a server holding an out-of-date
1307 * replica, rather than to concurrent RPC's. Failures to
1308 * release replicas are now flagged by the volserver, but only
1309 * since AFS 3.4 5.22, so there are plenty of clients getting
1310 * out-of-date replicas out there.
1312 * If we discover an out-of-date replica, by this time it's too
1313 * late to go to another server and retry. Also, we can't
1314 * reject the merge, because then there is no way for
1315 * GetAccess to do its work, and the caller gets into an
1316 * infinite loop. So we just grin and bear it.
1318 if (!(scp->flags & CM_SCACHEFLAG_RO))
1321 scp->serverModTime = statusp->ServerModTime;
1323 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1324 scp->clientModTime = statusp->ClientModTime;
1326 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1327 scp->length.LowPart = statusp->Length;
1328 scp->length.HighPart = statusp->Length_hi;
1331 scp->serverLength.LowPart = statusp->Length;
1332 scp->serverLength.HighPart = statusp->Length_hi;
1334 scp->linkCount = statusp->LinkCount;
1335 scp->dataVersion = statusp->DataVersion;
1336 scp->owner = statusp->Owner;
1337 scp->group = statusp->Group;
1338 scp->unixModeBits = statusp->UnixModeBits & 07777;
1340 if (statusp->FileType == File)
1341 scp->fileType = CM_SCACHETYPE_FILE;
1342 else if (statusp->FileType == Directory)
1343 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1344 else if (statusp->FileType == SymbolicLink) {
1345 if ((scp->unixModeBits & 0111) == 0)
1346 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1348 scp->fileType = CM_SCACHETYPE_SYMLINK;
1351 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1352 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1354 /* and other stuff */
1355 scp->parentVnode = statusp->ParentVnode;
1356 scp->parentUnique = statusp->ParentUnique;
1358 /* and merge in the private acl cache info, if this is more than the public
1359 * info; merge in the public stuff in any case.
1361 scp->anyAccess = statusp->AnonymousAccess;
1363 if (userp != NULL) {
1364 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1368 /* note that our stat cache info is incorrect, so force us eventually
1369 * to stat the file again. There may be dirty data associated with
1370 * this vnode, and we want to preserve that information.
1372 * This function works by simply simulating a loss of the callback.
1374 * This function must be called with the scache locked.
1376 void cm_DiscardSCache(cm_scache_t *scp)
1378 lock_AssertMutex(&scp->mx);
1379 if (scp->cbServerp) {
1380 cm_PutServer(scp->cbServerp);
1381 scp->cbServerp = NULL;
1384 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1385 cm_dnlcPurgedp(scp);
1386 cm_dnlcPurgevp(scp);
1387 cm_FreeAllACLEnts(scp);
1389 /* Force mount points and symlinks to be re-evaluated */
1390 scp->mountPointStringp[0] = '\0';
1393 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1395 afsFidp->Volume = fidp->volume;
1396 afsFidp->Vnode = fidp->vnode;
1397 afsFidp->Unique = fidp->unique;
1400 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1402 osi_assert(scp != 0);
1404 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%x ref %d",scp, scp->refCount);
1407 void cm_HoldSCache(cm_scache_t *scp)
1409 osi_assert(scp != 0);
1410 lock_ObtainWrite(&cm_scacheLock);
1412 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%x ref %d",scp, scp->refCount);
1413 lock_ReleaseWrite(&cm_scacheLock);
1416 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1418 osi_assert(scp != NULL);
1419 if (scp->refCount == 0)
1420 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1421 osi_assert(scp->refCount-- >= 0);
1422 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%x ref %d",scp,scp->refCount);
1425 void cm_ReleaseSCache(cm_scache_t *scp)
1427 osi_assert(scp != NULL);
1428 lock_ObtainWrite(&cm_scacheLock);
1429 if (scp->refCount == 0)
1430 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1431 osi_assert(scp->refCount != 0);
1433 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%x ref %d",scp,scp->refCount);
1434 lock_ReleaseWrite(&cm_scacheLock);
1437 /* just look for the scp entry to get filetype */
1438 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1439 int cm_FindFileType(cm_fid_t *fidp)
1444 hash = CM_SCACHE_HASH(fidp);
1446 osi_assert(fidp->cell != 0);
1448 lock_ObtainWrite(&cm_scacheLock);
1449 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
1450 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1451 lock_ReleaseWrite(&cm_scacheLock);
1452 return scp->fileType;
1455 lock_ReleaseWrite(&cm_scacheLock);
1459 /* dump all scp's that have reference count > 0 to a file.
1460 * cookie is used to identify this batch for easy parsing,
1461 * and it a string provided by a caller
1463 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1471 lock_ObtainRead(&cm_scacheLock);
1473 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1474 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1476 for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q))
1478 if (scp->refCount != 0)
1480 sprintf(output, "%s fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1481 cookie, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1483 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1487 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.hashTableSize=%d\n", cookie, cm_data.hashTableSize);
1488 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1490 for (i = 0; i < cm_data.hashTableSize; i++)
1492 for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp)
1494 if (scp->refCount != 0)
1496 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1497 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1498 scp->fid.unique, scp->refCount);
1499 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1504 sprintf(output, "%s - Done dumping scache.\n", cookie);
1505 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1508 lock_ReleaseRead(&cm_scacheLock);