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>
24 /*extern void afsi_log(char *pattern, ...);*/
26 extern osi_hyper_t hzero;
29 osi_queue_t *cm_allFileLocks;
30 osi_queue_t *cm_freeFileLocks;
31 unsigned long cm_lockRefreshCycle;
33 /* lock for globals */
34 osi_rwlock_t cm_scacheLock;
36 /* Dummy scache entry for use with pioctl fids */
37 cm_scache_t cm_fakeSCache;
39 #ifdef AFS_FREELANCE_CLIENT
40 extern osi_mutex_t cm_Freelance_Lock;
43 /* must be called with cm_scacheLock write-locked! */
44 void cm_AdjustScacheLRU(cm_scache_t *scp)
46 if (scp == cm_data.scacheLRULastp)
47 cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
48 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
49 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
50 if (!cm_data.scacheLRULastp)
51 cm_data.scacheLRULastp = scp;
54 /* call with scache write-locked and mutex held */
55 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
61 if (scp->flags & CM_SCACHEFLAG_INHASH) {
62 /* hash it out first */
63 i = CM_SCACHE_HASH(&scp->fid);
64 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
66 lscpp = &tscp->nextp, tscp = tscp->nextp) {
69 scp->flags &= ~CM_SCACHEFLAG_INHASH;
76 /* called with cm_scacheLock write-locked; recycles an existing scp.
78 * this function ignores all of the locking hierarchy.
80 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
82 if (scp->refCount != 0) {
86 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
87 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
89 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
94 cm_RemoveSCacheFromHashTable(scp);
97 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
101 while(qdp = scp->bufWritesp) {
102 bufp = osi_GetQData(qdp);
103 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
106 lock_ObtainMutex(&bufp->mx);
107 bufp->cmFlags &= ~CM_BUF_CMSTORING;
108 bufp->flags &= ~CM_BUF_DIRTY;
109 bufp->dirty_offset = 0;
110 bufp->dirty_length = 0;
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);
123 while(qdp = scp->bufReadsp) {
124 bufp = osi_GetQData(qdp);
125 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
128 lock_ObtainMutex(&bufp->mx);
129 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
130 bufp->flags &= ~CM_BUF_DIRTY;
131 bufp->dirty_offset = 0;
132 bufp->dirty_length = 0;
133 bufp->flags |= CM_BUF_ERROR;
134 bufp->error = VNOVNODE;
135 bufp->dataVersion = -1; /* bad */
136 bufp->dirtyCounter++;
137 if (bufp->flags & CM_BUF_WAITING) {
138 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
139 osi_Wakeup((long) &bufp);
141 lock_ReleaseMutex(&bufp->mx);
145 buf_CleanDirtyBuffers(scp);
147 /* look for things that shouldn't still be set */
148 osi_assert(scp->bufWritesp == NULL);
149 osi_assert(scp->bufReadsp == NULL);
153 /* invalidate so next merge works fine;
154 * also initialize some flags */
156 scp->flags &= ~(CM_SCACHEFLAG_STATD
157 | CM_SCACHEFLAG_DELETED
159 | CM_SCACHEFLAG_PURERO
160 | CM_SCACHEFLAG_OVERQUOTA
161 | CM_SCACHEFLAG_OUTOFSPACE
162 | CM_SCACHEFLAG_EACCESS);
163 scp->serverModTime = 0;
164 scp->dataVersion = 0;
165 scp->bulkStatProgress = hzero;
168 if (scp->cbServerp) {
169 cm_PutServer(scp->cbServerp);
170 scp->cbServerp = NULL;
179 /* remove from dnlc */
183 /* discard cached status; if non-zero, Close
184 * tried to store this to server but failed */
187 /* drop held volume ref */
189 cm_PutVolume(scp->volp);
193 /* discard symlink info */
194 scp->mountPointStringp[0] = '\0';
195 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
196 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
198 /* reset locking info */
199 scp->fileLocksH = NULL;
200 scp->fileLocksT = NULL;
201 scp->serverLock = (-1);
202 scp->exclusiveLocks = 0;
203 scp->sharedLocks = 0;
205 /* not locked, but there can be no references to this guy
206 * while we hold the global refcount lock.
208 cm_FreeAllACLEnts(scp);
211 /* destroy directory Bplus Tree */
213 LARGE_INTEGER start, end;
214 QueryPerformanceCounter(&start);
216 freeBtree(scp->dirBplus);
217 scp->dirBplus = NULL;
218 QueryPerformanceCounter(&end);
220 bplus_free_time += (end.QuadPart - start.QuadPart);
227 /* called with cm_scacheLock write-locked; find a vnode to recycle.
228 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
230 cm_scache_t *cm_GetNewSCache(void)
236 /* first pass - look for deleted objects */
237 for ( scp = cm_data.scacheLRULastp;
239 scp = (cm_scache_t *) osi_QPrev(&scp->q))
241 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
243 if (scp->refCount == 0) {
244 if (scp->flags & CM_SCACHEFLAG_DELETED) {
245 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
246 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
248 /* we found an entry, so return it */
249 /* now remove from the LRU queue and put it back at the
250 * head of the LRU queue.
252 cm_AdjustScacheLRU(scp);
257 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
258 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
259 /* we found an entry, so return it */
260 /* now remove from the LRU queue and put it back at the
261 * head of the LRU queue.
263 cm_AdjustScacheLRU(scp);
270 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
273 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
274 /* There were no deleted scache objects that we could use. Try to find
275 * one that simply hasn't been used in a while.
277 for ( scp = cm_data.scacheLRULastp;
279 scp = (cm_scache_t *) osi_QPrev(&scp->q))
281 /* It is possible for the refCount to be zero and for there still
282 * to be outstanding dirty buffers. If there are dirty buffers,
283 * we must not recycle the scp. */
284 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
285 if (!buf_DirtyBuffersExist(&scp->fid)) {
286 if (!cm_RecycleSCache(scp, 0)) {
287 /* we found an entry, so return it */
288 /* now remove from the LRU queue and put it back at the
289 * head of the LRU queue.
291 cm_AdjustScacheLRU(scp);
297 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
301 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
306 /* if we get here, we should allocate a new scache entry. We either are below
307 * quota or we have a leak and need to allocate a new one to avoid panicing.
309 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
310 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep);
311 memset(scp, 0, sizeof(cm_scache_t));
312 scp->magic = CM_SCACHE_MAGIC;
313 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
314 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
316 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
318 scp->serverLock = -1;
320 /* and put it in the LRU queue */
321 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
322 if (!cm_data.scacheLRULastp)
323 cm_data.scacheLRULastp = scp;
324 cm_data.currentSCaches++;
325 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
327 scp->allNextp = cm_data.allSCachesp;
328 cm_data.allSCachesp = scp;
332 /* like strcmp, only for fids */
333 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
335 if (ap->vnode != bp->vnode)
337 if (ap->volume != bp->volume)
339 if (ap->unique != bp->unique)
341 if (ap->cell != bp->cell)
346 void cm_fakeSCacheInit(int newFile)
349 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
350 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
351 /* can leave clientModTime at 0 */
352 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
353 cm_data.fakeSCache.unixModeBits = 0777;
354 cm_data.fakeSCache.length.LowPart = 1000;
355 cm_data.fakeSCache.linkCount = 1;
356 cm_data.fakeSCache.refCount = 1;
358 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
362 cm_ValidateSCache(void)
364 cm_scache_t * scp, *lscp;
367 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
368 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
369 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
370 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
374 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
376 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
377 if (scp->magic != CM_SCACHE_MAGIC) {
378 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
379 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
382 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
383 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
384 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
387 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
388 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
389 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
392 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
393 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
394 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
397 if (i > cm_data.currentSCaches ) {
398 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
399 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
402 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
403 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
404 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
409 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
410 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
411 if (scp->magic != CM_SCACHE_MAGIC) {
412 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
413 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
416 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
417 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
418 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
421 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
422 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
423 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
426 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
427 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
428 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
431 if (i > cm_data.currentSCaches ) {
432 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
433 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
436 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
437 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
438 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
443 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
444 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
445 if (scp->magic != CM_SCACHE_MAGIC) {
446 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
447 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
450 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
451 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
452 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
455 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
456 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
457 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
460 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
461 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
462 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
468 return cm_dnlcValidate();
472 cm_SuspendSCache(void)
476 cm_GiveUpAllCallbacksAllServers();
478 lock_ObtainWrite(&cm_scacheLock);
479 for ( scp = cm_data.allSCachesp; scp;
480 scp = scp->allNextp ) {
481 if (scp->cbServerp &&
482 !(scp->cbServerp->flags & CM_SERVERFLAG_DOWN)) {
483 cm_PutServer(scp->cbServerp);
484 scp->cbServerp = NULL;
487 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
489 lock_ReleaseWrite(&cm_scacheLock);
493 cm_ShutdownSCache(void)
497 lock_ObtainWrite(&cm_scacheLock);
499 for ( scp = cm_data.allSCachesp; scp;
500 scp = scp->allNextp ) {
501 if (scp->randomACLp) {
502 lock_ObtainMutex(&scp->mx);
503 cm_FreeAllACLEnts(scp);
504 lock_ReleaseMutex(&scp->mx);
507 if (scp->cbServerp) {
508 cm_PutServer(scp->cbServerp);
509 scp->cbServerp = NULL;
512 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
516 freeBtree(scp->dirBplus);
517 scp->dirBplus = NULL;
518 scp->dirDataVersion = -1;
519 lock_FinalizeRWLock(&scp->dirlock);
521 lock_FinalizeMutex(&scp->mx);
522 lock_FinalizeRWLock(&scp->bufCreateLock);
524 lock_ReleaseWrite(&cm_scacheLock);
526 cm_GiveUpAllCallbacksAllServers();
528 return cm_dnlcShutdown();
531 void cm_InitSCache(int newFile, long maxSCaches)
533 static osi_once_t once;
535 if (osi_Once(&once)) {
536 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
538 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
539 cm_data.allSCachesp = NULL;
540 cm_data.currentSCaches = 0;
541 cm_data.maxSCaches = maxSCaches;
542 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
546 for ( scp = cm_data.allSCachesp; scp;
547 scp = scp->allNextp ) {
548 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
549 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
551 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
553 scp->cbServerp = NULL;
555 scp->fileLocksH = NULL;
556 scp->fileLocksT = NULL;
557 scp->serverLock = (-1);
558 scp->lastRefreshCycle = 0;
559 scp->exclusiveLocks = 0;
560 scp->sharedLocks = 0;
567 scp->dirBplus = NULL;
568 scp->dirDataVersion = -1;
570 scp->flags &= ~CM_SCACHEFLAG_WAITING;
573 cm_allFileLocks = NULL;
574 cm_freeFileLocks = NULL;
575 cm_lockRefreshCycle = 0;
576 cm_fakeSCacheInit(newFile);
577 cm_dnlcInit(newFile);
582 /* version that doesn't bother creating the entry if we don't find it */
583 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
588 hash = CM_SCACHE_HASH(fidp);
590 if (fidp->cell == 0) {
594 lock_ObtainWrite(&cm_scacheLock);
595 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
596 if (cm_FidCmp(fidp, &scp->fid) == 0) {
597 cm_HoldSCacheNoLock(scp);
598 cm_AdjustScacheLRU(scp);
599 lock_ReleaseWrite(&cm_scacheLock);
603 lock_ReleaseWrite(&cm_scacheLock);
607 #ifdef DEBUG_REFCOUNT
608 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
609 cm_req_t *reqp, char * file, long line)
611 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
618 cm_volume_t *volp = NULL;
621 int special; // yj: boolean variable to test if file is on root.afs
623 extern cm_fid_t cm_rootFid;
625 hash = CM_SCACHE_HASH(fidp);
627 osi_assert(fidp->cell != 0);
629 if (fidp->cell== cm_data.rootFid.cell &&
630 fidp->volume==cm_data.rootFid.volume &&
631 fidp->vnode==0x0 && fidp->unique==0x0)
633 osi_Log0(afsd_logp,"cm_GetSCache called with root cell/volume and vnode=0 and unique=0");
636 // yj: check if we have the scp, if so, we don't need
637 // to do anything else
638 lock_ObtainWrite(&cm_scacheLock);
639 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
640 if (cm_FidCmp(fidp, &scp->fid) == 0) {
641 #ifdef DEBUG_REFCOUNT
642 afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
643 osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
645 cm_HoldSCacheNoLock(scp);
647 cm_AdjustScacheLRU(scp);
648 lock_ReleaseWrite(&cm_scacheLock);
653 // yj: when we get here, it means we don't have an scp
654 // so we need to either load it or fake it, depending
655 // on whether the file is "special", see below.
657 // yj: if we're trying to get an scp for a file that's
658 // on root.afs of homecell, we want to handle it specially
659 // because we have to fill in the status stuff 'coz we
660 // don't want trybulkstat to fill it in for us
661 #ifdef AFS_FREELANCE_CLIENT
662 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
663 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
664 !(fidp->vnode==0x1 && fidp->unique==0x1));
665 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
666 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
667 fidp->vnode==0x1 && fidp->unique==0x1);
668 if (cm_freelanceEnabled && isRoot) {
669 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
670 /* freelance: if we are trying to get the root scp for the first
671 * time, we will just put in a place holder entry.
676 if (cm_freelanceEnabled && special) {
677 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
678 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
679 lock_ObtainMutex(&cm_Freelance_Lock);
680 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
681 lock_ReleaseMutex(&cm_Freelance_Lock);
685 scp = cm_GetNewSCache();
687 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
688 lock_ReleaseWrite(&cm_scacheLock);
689 return CM_ERROR_WOULDBLOCK;
692 #if not_too_dangerous
693 /* dropping the cm_scacheLock allows more than one thread
694 * to obtain the same cm_scache_t from the LRU list. Since
695 * the refCount is known to be zero at this point we have to
696 * assume that no one else is using the one this is returned.
698 lock_ReleaseWrite(&cm_scacheLock);
699 lock_ObtainMutex(&scp->mx);
700 lock_ObtainWrite(&cm_scacheLock);
703 scp->volp = cm_data.rootSCachep->volp;
704 cm_GetVolume(scp->volp); /* grab an additional reference */
705 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
706 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
707 scp->dotdotFid.unique=1;
708 scp->dotdotFid.vnode=1;
709 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
710 scp->nextp=cm_data.scacheHashTablep[hash];
711 cm_data.scacheHashTablep[hash]=scp;
712 scp->flags |= CM_SCACHEFLAG_INHASH;
714 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
715 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
716 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
718 scp->fileType = CM_SCACHETYPE_INVALID;
720 lock_ObtainMutex(&cm_Freelance_Lock);
721 scp->length.LowPart = (DWORD)strlen(mp)+4;
722 scp->length.HighPart = 0;
723 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
724 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
725 lock_ReleaseMutex(&cm_Freelance_Lock);
728 scp->unixModeBits=0777;
729 scp->clientModTime=FakeFreelanceModTime;
730 scp->serverModTime=FakeFreelanceModTime;
731 scp->parentUnique = 0x1;
732 scp->parentVnode=0x1;
734 scp->dataVersion=cm_data.fakeDirVersion;
735 scp->lockDataVersion=-1; /* no lock yet */
736 #if not_too_dangerous
737 lock_ReleaseMutex(&scp->mx);
740 lock_ReleaseWrite(&cm_scacheLock);
741 #ifdef DEBUG_REFCOUNT
742 afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
743 osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
748 #endif /* AFS_FREELANCE_CLIENT */
750 /* otherwise, we need to find the volume */
751 if (!cm_freelanceEnabled || !isRoot) {
752 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
753 cellp = cm_FindCellByID(fidp->cell);
755 return CM_ERROR_NOSUCHCELL;
757 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
760 lock_ObtainWrite(&cm_scacheLock);
763 /* otherwise, we have the volume, now reverify that the scp doesn't
764 * exist, and proceed.
766 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
767 if (cm_FidCmp(fidp, &scp->fid) == 0) {
768 #ifdef DEBUG_REFCOUNT
769 afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
770 osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
772 cm_HoldSCacheNoLock(scp);
773 osi_assert(scp->volp == volp);
774 cm_AdjustScacheLRU(scp);
775 lock_ReleaseWrite(&cm_scacheLock);
783 /* now, if we don't have the fid, recycle something */
784 scp = cm_GetNewSCache();
786 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
787 lock_ReleaseWrite(&cm_scacheLock);
790 return CM_ERROR_WOULDBLOCK;
792 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
794 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
796 #if not_too_dangerous
797 /* dropping the cm_scacheLock allows more than one thread
798 * to obtain the same cm_scache_t from the LRU list. Since
799 * the refCount is known to be zero at this point we have to
800 * assume that no one else is using the one this is returned.
802 lock_ReleaseWrite(&cm_scacheLock);
803 lock_ObtainMutex(&scp->mx);
804 lock_ObtainWrite(&cm_scacheLock);
807 scp->volp = volp; /* a held reference */
809 if (!cm_freelanceEnabled || !isRoot) {
810 /* if this scache entry represents a volume root then we need
811 * to copy the dotdotFipd from the volume structure where the
812 * "master" copy is stored (defect 11489)
814 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
815 scp->dotdotFid = volp->dotdotFid;
818 if (volp->ro.ID == fidp->volume)
819 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
820 else if (volp->bk.ID == fidp->volume)
821 scp->flags |= CM_SCACHEFLAG_RO;
823 scp->nextp = cm_data.scacheHashTablep[hash];
824 cm_data.scacheHashTablep[hash] = scp;
825 scp->flags |= CM_SCACHEFLAG_INHASH;
827 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
828 #if not_too_dangerous
829 lock_ReleaseMutex(&scp->mx);
832 /* XXX - The following fields in the cm_scache are
838 lock_ReleaseWrite(&cm_scacheLock);
840 /* now we have a held scache entry; just return it */
842 #ifdef DEBUG_REFCOUNT
843 afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
844 osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
849 /* Returns a held reference to the scache's parent
851 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
856 cm_scache_t * pscp = NULL;
858 lock_ObtainRead(&cm_scacheLock);
859 parent_fid = scp->fid;
860 parent_fid.vnode = scp->parentVnode;
861 parent_fid.unique = scp->parentUnique;
863 if (cm_FidCmp(&scp->fid, &parent_fid)) {
864 i = CM_SCACHE_HASH(&parent_fid);
865 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
866 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
867 cm_HoldSCacheNoLock(pscp);
873 lock_ReleaseRead(&cm_scacheLock);
878 /* synchronize a fetch, store, read, write, fetch status or store status.
879 * Called with scache mutex held, and returns with it held, but temporarily
880 * drops it during the fetch.
882 * At most one flag can be on in flags, if this is an RPC request.
884 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
886 * There are a lot of weird restrictions here; here's an attempt to explain the
887 * rationale for the concurrency restrictions implemented in this function.
889 * First, although the file server will break callbacks when *another* machine
890 * modifies a file or status block, the client itself is responsible for
891 * concurrency control on its own requests. Callback breaking events are rare,
892 * and simply invalidate any concurrent new status info.
894 * In the absence of callback breaking messages, we need to know how to
895 * synchronize incoming responses describing updates to files. We synchronize
896 * operations that update the data version by comparing the data versions.
897 * However, updates that do not update the data, but only the status, can't be
898 * synchronized with fetches or stores, since there's nothing to compare
899 * to tell which operation executed first at the server.
901 * Thus, we can allow multiple ops that change file data, or dir data, and
902 * fetches. However, status storing ops have to be done serially.
904 * Furthermore, certain data-changing ops are incompatible: we can't read or
905 * write a buffer while doing a truncate. We can't read and write the same
906 * buffer at the same time, or write while fetching or storing, or read while
907 * fetching a buffer (this may change). We can't fetch and store at the same
910 * With respect to status, we can't read and write at the same time, read while
911 * fetching, write while fetching or storing, or fetch and store at the same time.
913 * We can't allow a get callback RPC to run in concurrently with something that
914 * will return updated status, since we could start a call, have the server
915 * return status, have another machine make an update to the status (which
916 * doesn't change serverModTime), have the original machine get a new callback,
917 * and then have the original machine merge in the early, old info from the
918 * first call. At this point, the easiest way to avoid this problem is to have
919 * getcallback calls conflict with all others for the same vnode. Other calls
920 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
921 * vnode must be careful not to merge in their status unless they have obtained
922 * a callback from the start of their call.
925 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
926 * Each such RPC passes a FileLength parameter, which the server uses to do
927 * pre-truncation if necessary. So if two RPC's are processed out of order at
928 * the server, the one with the smaller FileLength will be processed last,
929 * possibly resulting in a bogus truncation. The simplest way to avoid this
930 * is to serialize all StoreData RPC's. This is the reason we defined
931 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
933 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
934 afs_uint32 rights, afs_uint32 flags)
936 osi_queueData_t *qdp;
939 afs_uint32 outRights;
941 afs_uint32 sleep_scp_flags = 0;
942 afs_uint32 sleep_buf_cmflags = 0;
943 afs_uint32 sleep_scp_bufs = 0;
945 /* lookup this first */
946 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
949 osi_assert(bufp->refCount > 0);
952 /* Do the access check. Now we don't really do the access check
953 * atomically, since the caller doesn't expect the parent dir to be
954 * returned locked, and that is what we'd have to do to prevent a
955 * callback breaking message on the parent due to a setacl call from
956 * being processed while we're running. So, instead, we check things
957 * here, and if things look fine with the access, we proceed to finish
958 * the rest of this check. Sort of a hack, but probably good enough.
962 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
963 /* if we're bringing in a new status block, ensure that
964 * we aren't already doing so, and that no one is
965 * changing the status concurrently, either. We need
966 * to do this, even if the status is of a different
967 * type, since we don't have the ability to figure out,
968 * in the AFS 3 protocols, which status-changing
969 * operation ran first, or even which order a read and
970 * a write occurred in.
972 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
973 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
974 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
978 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
979 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
980 /* if we're going to make an RPC to change the status, make sure
981 * that no one is bringing in or sending out the status.
983 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
984 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
985 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
988 if (scp->bufReadsp || scp->bufWritesp) {
989 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
993 if (flags & CM_SCACHESYNC_FETCHDATA) {
994 /* if we're bringing in a new chunk of data, make sure that
995 * nothing is happening to that chunk, and that we aren't
996 * changing the basic file status info, either.
998 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
999 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1000 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1003 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1004 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1008 if (flags & CM_SCACHESYNC_STOREDATA) {
1009 /* same as fetch data */
1010 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1011 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1012 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1015 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1016 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1021 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1022 /* Don't allow concurrent StoreData RPC's */
1023 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1024 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1029 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1030 /* Don't allow more than one BKG store request */
1031 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1032 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1037 if (flags & CM_SCACHESYNC_LOCK) {
1038 /* Don't allow concurrent fiddling with lock lists */
1039 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1040 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1045 /* now the operations that don't correspond to making RPCs */
1046 if (flags & CM_SCACHESYNC_GETSTATUS) {
1047 /* we can use the status that's here, if we're not
1048 * bringing in new status.
1050 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1051 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1055 if (flags & CM_SCACHESYNC_SETSTATUS) {
1056 /* we can make a change to the local status, as long as
1057 * the status isn't changing now.
1059 * If we're fetching or storing a chunk of data, we can
1060 * change the status locally, since the fetch/store
1061 * operations don't change any of the data that we're
1064 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1065 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1069 if (flags & CM_SCACHESYNC_READ) {
1070 /* we're going to read the data, make sure that the
1071 * status is available, and that the data is here. It
1072 * is OK to read while storing the data back.
1074 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1075 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1078 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1079 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1082 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1083 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1087 if (flags & CM_SCACHESYNC_WRITE) {
1088 /* don't write unless the status is stable and the chunk
1091 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1092 | CM_SCACHEFLAG_SIZESTORING)) {
1093 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1096 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1098 CM_BUF_CMWRITING))) {
1099 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1101 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1102 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1103 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1109 // yj: modified this so that callback only checked if we're
1110 // not checking something on /afs
1111 /* fix the conditional to match the one in cm_HaveCallback */
1112 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1113 #ifdef AFS_FREELANCE_CLIENT
1114 && (!cm_freelanceEnabled ||
1115 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1116 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1117 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1118 cm_fakeDirCallback < 2)
1119 #endif /* AFS_FREELANCE_CLIENT */
1121 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1122 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1125 lock_ReleaseMutex(&bufp->mx);
1126 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1128 lock_ReleaseMutex(&scp->mx);
1129 lock_ObtainMutex(&bufp->mx);
1130 lock_ObtainMutex(&scp->mx);
1134 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1140 /* can't check access rights without a callback */
1141 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
1143 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
1144 return CM_ERROR_READONLY;
1146 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
1147 if (~outRights & rights)
1148 return CM_ERROR_NOACCESS;
1151 /* we don't know the required access rights */
1152 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1153 code = cm_GetAccessRights(scp, userp, reqp);
1155 lock_ReleaseMutex(&scp->mx);
1156 lock_ObtainMutex(&bufp->mx);
1157 lock_ObtainMutex(&scp->mx);
1165 /* if we get here, we're happy */
1169 /* first check if we're not supposed to wait: fail
1170 * in this case, returning with everything still locked.
1172 if (flags & CM_SCACHESYNC_NOWAIT)
1173 return CM_ERROR_WOULDBLOCK;
1175 sleep_scp_flags = scp->flags; /* so we know why we slept */
1176 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1177 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1179 /* wait here, then try again */
1180 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1181 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1183 scp->waitRequests++;
1184 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1185 scp, scp->waitCount, scp->waitRequests);
1187 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1188 scp->flags |= CM_SCACHEFLAG_WAITING;
1189 scp->waitCount = scp->waitRequests = 1;
1192 lock_ReleaseMutex(&bufp->mx);
1193 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
1195 smb_UpdateServerPriority();
1198 lock_ObtainMutex(&bufp->mx);
1199 lock_ObtainMutex(&scp->mx);
1201 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1202 scp, scp->waitCount, scp->waitRequests);
1203 if (scp->waitCount == 0) {
1204 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1205 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1206 scp->waitRequests = 0;
1208 } /* big while loop */
1210 /* now, update the recorded state for RPC-type calls */
1211 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1212 scp->flags |= CM_SCACHEFLAG_FETCHING;
1213 if (flags & CM_SCACHESYNC_STORESTATUS)
1214 scp->flags |= CM_SCACHEFLAG_STORING;
1215 if (flags & CM_SCACHESYNC_STORESIZE)
1216 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1217 if (flags & CM_SCACHESYNC_GETCALLBACK)
1218 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1219 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1220 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1221 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1222 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1223 if (flags & CM_SCACHESYNC_LOCK)
1224 scp->flags |= CM_SCACHEFLAG_LOCKING;
1226 /* now update the buffer pointer */
1227 if (flags & CM_SCACHESYNC_FETCHDATA) {
1228 /* ensure that the buffer isn't already in the I/O list */
1230 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1231 tbufp = osi_GetQData(qdp);
1232 osi_assert(tbufp != bufp);
1236 /* queue a held reference to the buffer in the "reading" I/O list */
1237 qdp = osi_QDAlloc();
1238 osi_SetQData(qdp, bufp);
1241 bufp->cmFlags |= CM_BUF_CMFETCHING;
1243 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1246 if (flags & CM_SCACHESYNC_STOREDATA) {
1247 /* ensure that the buffer isn't already in the I/O list */
1249 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1250 tbufp = osi_GetQData(qdp);
1251 osi_assert(tbufp != bufp);
1255 /* queue a held reference to the buffer in the "writing" I/O list */
1256 qdp = osi_QDAlloc();
1257 osi_SetQData(qdp, bufp);
1260 bufp->cmFlags |= CM_BUF_CMSTORING;
1262 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1265 if (flags & CM_SCACHESYNC_WRITE) {
1266 /* mark the buffer as being written to. */
1268 bufp->cmFlags |= CM_BUF_CMWRITING;
1275 /* for those syncops that setup for RPCs.
1276 * Called with scache locked.
1278 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1280 osi_queueData_t *qdp;
1283 lock_AssertMutex(&scp->mx);
1285 /* now, update the recorded state for RPC-type calls */
1286 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1287 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1288 if (flags & CM_SCACHESYNC_STORESTATUS)
1289 scp->flags &= ~CM_SCACHEFLAG_STORING;
1290 if (flags & CM_SCACHESYNC_STORESIZE)
1291 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1292 if (flags & CM_SCACHESYNC_GETCALLBACK)
1293 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1294 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1295 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1296 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1297 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1298 if (flags & CM_SCACHESYNC_LOCK)
1299 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1301 /* now update the buffer pointer */
1302 if (flags & CM_SCACHESYNC_FETCHDATA) {
1305 /* ensure that the buffer isn't already in the I/O list */
1306 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1307 tbufp = osi_GetQData(qdp);
1312 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1317 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1318 if (bufp->flags & CM_BUF_WAITING) {
1319 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1320 osi_Wakeup((LONG_PTR) &bufp);
1327 /* now update the buffer pointer */
1328 if (flags & CM_SCACHESYNC_STOREDATA) {
1330 /* ensure that the buffer isn't already in the I/O list */
1331 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1332 tbufp = osi_GetQData(qdp);
1337 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1342 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1343 if (bufp->flags & CM_BUF_WAITING) {
1344 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1345 osi_Wakeup((LONG_PTR) &bufp);
1352 if (flags & CM_SCACHESYNC_WRITE) {
1354 osi_assert(bufp->cmFlags & CM_BUF_CMWRITING);
1356 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1360 /* and wakeup anyone who is waiting */
1361 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1362 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1363 osi_Wakeup((LONG_PTR) &scp->flags);
1367 /* merge in a response from an RPC. The scp must be locked, and the callback
1370 * Don't overwrite any status info that is dirty, since we could have a store
1371 * operation (such as store data) that merges some info in, and we don't want
1372 * to lose the local updates. Typically, there aren't many updates we do
1373 * locally, anyway, probably only mtime.
1375 * There is probably a bug in here where a chmod (which doesn't change
1376 * serverModTime) that occurs between two fetches, both of whose responses are
1377 * handled after the callback breaking is done, but only one of whose calls
1378 * started before that, can cause old info to be merged from the first call.
1380 void cm_MergeStatus(cm_scache_t *dscp,
1381 cm_scache_t *scp, AFSFetchStatus *statusp,
1382 AFSVolSync *volsyncp,
1383 cm_user_t *userp, afs_uint32 flags)
1385 // yj: i want to create some fake status for the /afs directory and the
1386 // entries under that directory
1387 #ifdef AFS_FREELANCE_CLIENT
1388 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1389 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1390 statusp->InterfaceVersion = 0x1;
1391 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1392 statusp->LinkCount = scp->linkCount;
1393 statusp->Length = cm_fakeDirSize;
1394 statusp->Length_hi = 0;
1395 statusp->DataVersion = cm_data.fakeDirVersion;
1396 statusp->Author = 0x1;
1397 statusp->Owner = 0x0;
1398 statusp->CallerAccess = 0x9;
1399 statusp->AnonymousAccess = 0x9;
1400 statusp->UnixModeBits = 0777;
1401 statusp->ParentVnode = 0x1;
1402 statusp->ParentUnique = 0x1;
1403 statusp->ResidencyMask = 0;
1404 statusp->ClientModTime = FakeFreelanceModTime;
1405 statusp->ServerModTime = FakeFreelanceModTime;
1407 statusp->SyncCounter = 0;
1408 statusp->dataVersionHigh = 0;
1409 statusp->errorCode = 0;
1411 #endif /* AFS_FREELANCE_CLIENT */
1413 if (statusp->errorCode != 0) {
1414 scp->flags |= CM_SCACHEFLAG_EACCESS;
1415 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1417 scp->fileType = 0; /* unknown */
1419 scp->serverModTime = 0;
1420 scp->clientModTime = 0;
1421 scp->length.LowPart = 0;
1422 scp->length.HighPart = 0;
1423 scp->serverLength.LowPart = 0;
1424 scp->serverLength.HighPart = 0;
1428 scp->unixModeBits = 0;
1430 scp->dataVersion = 0;
1433 scp->parentVnode = dscp->fid.vnode;
1434 scp->parentUnique = dscp->fid.unique;
1436 scp->parentVnode = 0;
1437 scp->parentUnique = 0;
1441 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1444 if (!(flags & CM_MERGEFLAG_FORCE)
1445 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1446 struct cm_cell *cellp;
1448 cellp = cm_FindCellByID(scp->fid.cell);
1449 if (scp->cbServerp) {
1450 struct cm_volume *volp = NULL;
1452 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1453 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1454 osi_Log2(afsd_logp, "old data from server %x volume %s",
1455 scp->cbServerp->addr.sin_addr.s_addr,
1456 volp ? volp->namep : "(unknown)");
1460 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1461 scp, scp->dataVersion, statusp->DataVersion);
1462 /* we have a number of data fetch/store operations running
1463 * concurrently, and we can tell which one executed last at the
1464 * server by its mtime.
1465 * Choose the one with the largest mtime, and ignore the rest.
1467 * These concurrent calls are incompatible with setting the
1468 * mtime, so we won't have a locally changed mtime here.
1470 * We could also have ACL info for a different user than usual,
1471 * in which case we have to do that part of the merge, anyway.
1472 * We won't have to worry about the info being old, since we
1473 * won't have concurrent calls
1474 * that change file status running from this machine.
1476 * Added 3/17/98: if we see data version regression on an RO
1477 * file, it's probably due to a server holding an out-of-date
1478 * replica, rather than to concurrent RPC's. Failures to
1479 * release replicas are now flagged by the volserver, but only
1480 * since AFS 3.4 5.22, so there are plenty of clients getting
1481 * out-of-date replicas out there.
1483 * If we discover an out-of-date replica, by this time it's too
1484 * late to go to another server and retry. Also, we can't
1485 * reject the merge, because then there is no way for
1486 * GetAccess to do its work, and the caller gets into an
1487 * infinite loop. So we just grin and bear it.
1489 if (!(scp->flags & CM_SCACHEFLAG_RO))
1493 scp->serverModTime = statusp->ServerModTime;
1495 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1496 scp->clientModTime = statusp->ClientModTime;
1498 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1499 scp->length.LowPart = statusp->Length;
1500 scp->length.HighPart = statusp->Length_hi;
1503 scp->serverLength.LowPart = statusp->Length;
1504 scp->serverLength.HighPart = statusp->Length_hi;
1506 scp->linkCount = statusp->LinkCount;
1507 scp->owner = statusp->Owner;
1508 scp->group = statusp->Group;
1509 scp->unixModeBits = statusp->UnixModeBits & 07777;
1511 if (statusp->FileType == File)
1512 scp->fileType = CM_SCACHETYPE_FILE;
1513 else if (statusp->FileType == Directory)
1514 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1515 else if (statusp->FileType == SymbolicLink) {
1516 if ((scp->unixModeBits & 0111) == 0)
1517 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1519 scp->fileType = CM_SCACHETYPE_SYMLINK;
1522 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1523 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1525 /* and other stuff */
1526 scp->parentVnode = statusp->ParentVnode;
1527 scp->parentUnique = statusp->ParentUnique;
1529 /* and merge in the private acl cache info, if this is more than the public
1530 * info; merge in the public stuff in any case.
1532 scp->anyAccess = statusp->AnonymousAccess;
1534 if (userp != NULL) {
1535 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1538 if ((flags & CM_MERGEFLAG_STOREDATA) &&
1539 statusp->DataVersion - scp->dataVersion == 1) {
1542 for (bp = cm_data.buf_fileHashTablepp[BUF_FILEHASH(&scp->fid)]; bp; bp=bp->fileHashp)
1544 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1545 bp->dataVersion == scp->dataVersion)
1546 bp->dataVersion = statusp->DataVersion;
1550 scp->dataVersion = statusp->DataVersion;
1553 /* note that our stat cache info is incorrect, so force us eventually
1554 * to stat the file again. There may be dirty data associated with
1555 * this vnode, and we want to preserve that information.
1557 * This function works by simply simulating a loss of the callback.
1559 * This function must be called with the scache locked.
1561 void cm_DiscardSCache(cm_scache_t *scp)
1563 lock_AssertMutex(&scp->mx);
1564 if (scp->cbServerp) {
1565 cm_PutServer(scp->cbServerp);
1566 scp->cbServerp = NULL;
1569 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1570 cm_dnlcPurgedp(scp);
1571 cm_dnlcPurgevp(scp);
1572 cm_FreeAllACLEnts(scp);
1574 /* Force mount points and symlinks to be re-evaluated */
1575 scp->mountPointStringp[0] = '\0';
1578 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1580 afsFidp->Volume = fidp->volume;
1581 afsFidp->Vnode = fidp->vnode;
1582 afsFidp->Unique = fidp->unique;
1585 #ifdef DEBUG_REFCOUNT
1586 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1588 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1591 osi_assert(scp != 0);
1593 #ifdef DEBUG_REFCOUNT
1594 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, scp->refCount);
1595 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, scp->refCount);
1599 #ifdef DEBUG_REFCOUNT
1600 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1602 void cm_HoldSCache(cm_scache_t *scp)
1605 osi_assert(scp != 0);
1606 lock_ObtainWrite(&cm_scacheLock);
1608 #ifdef DEBUG_REFCOUNT
1609 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, scp->refCount);
1610 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1612 lock_ReleaseWrite(&cm_scacheLock);
1615 #ifdef DEBUG_REFCOUNT
1616 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1618 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1621 osi_assert(scp != NULL);
1622 if (scp->refCount == 0)
1623 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1624 osi_assert(scp->refCount-- >= 0);
1625 #ifdef DEBUG_REFCOUNT
1626 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp,scp->refCount);
1627 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, scp->refCount);
1631 #ifdef DEBUG_REFCOUNT
1632 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1634 void cm_ReleaseSCache(cm_scache_t *scp)
1637 osi_assert(scp != NULL);
1638 lock_ObtainWrite(&cm_scacheLock);
1639 if (scp->refCount == 0)
1640 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1641 osi_assert(scp->refCount != 0);
1643 #ifdef DEBUG_REFCOUNT
1644 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp,scp->refCount);
1645 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, scp->refCount);
1647 lock_ReleaseWrite(&cm_scacheLock);
1650 /* just look for the scp entry to get filetype */
1651 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1652 int cm_FindFileType(cm_fid_t *fidp)
1657 hash = CM_SCACHE_HASH(fidp);
1659 osi_assert(fidp->cell != 0);
1661 lock_ObtainWrite(&cm_scacheLock);
1662 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1663 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1664 lock_ReleaseWrite(&cm_scacheLock);
1665 return scp->fileType;
1668 lock_ReleaseWrite(&cm_scacheLock);
1672 /* dump all scp's that have reference count > 0 to a file.
1673 * cookie is used to identify this batch for easy parsing,
1674 * and it a string provided by a caller
1676 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1684 lock_ObtainRead(&cm_scacheLock);
1686 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1687 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1689 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1691 sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) volp=0x%p type=%d dv=%d len=0x%I64x mp='%s' flags=0x%x cb=0x%x refCount=%u\r\n",
1692 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1693 scp->volp, scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp, scp->flags,
1694 (unsigned long)scp->cbExpires, scp->refCount);
1695 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1698 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1699 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1700 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1701 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1703 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1705 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1707 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1708 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1709 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1713 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1714 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1717 lock_ReleaseRead(&cm_scacheLock);