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 osi_queue_t * cm_allFreeWaiters; /* protected by cm_scacheLock */
41 #ifdef AFS_FREELANCE_CLIENT
42 extern osi_mutex_t cm_Freelance_Lock;
45 /* must be called with cm_scacheLock write-locked! */
46 void cm_AdjustScacheLRU(cm_scache_t *scp)
48 lock_AssertWrite(&cm_scacheLock);
49 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
50 osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
53 /* call with cm_scacheLock write-locked and scp rw held */
54 void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
60 lock_AssertWrite(&cm_scacheLock);
61 lock_AssertWrite(&scp->rw);
62 if (scp->flags & CM_SCACHEFLAG_INHASH) {
63 /* hash it out first */
64 i = CM_SCACHE_HASH(&scp->fid);
65 for (lscpp = &cm_data.scacheHashTablep[i], tscp = cm_data.scacheHashTablep[i];
67 lscpp = &tscp->nextp, tscp = tscp->nextp) {
71 scp->flags &= ~CM_SCACHEFLAG_INHASH;
78 /* called with cm_scacheLock write-locked; recycles an existing scp.
80 * this function ignores all of the locking hierarchy.
82 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
84 if (scp->refCount != 0) {
88 if (scp->flags & CM_SCACHEFLAG_SMB_FID) {
89 osi_Log1(afsd_logp,"cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected scp 0x%p", scp);
91 osi_panic("cm_RecycleSCache CM_SCACHEFLAG_SMB_FID detected",__FILE__,__LINE__);
96 lock_ObtainWrite(&scp->rw);
97 cm_RemoveSCacheFromHashTable(scp);
98 lock_ReleaseWrite(&scp->rw);
101 if (flags & CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS) {
102 osi_queueData_t *qdp;
105 while(qdp = scp->bufWritesp) {
106 bufp = osi_GetQData(qdp);
107 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
110 lock_ObtainMutex(&bufp->mx);
111 bufp->cmFlags &= ~CM_BUF_CMSTORING;
112 bufp->flags &= ~CM_BUF_DIRTY;
113 bufp->dirty_offset = 0;
114 bufp->dirty_length = 0;
115 bufp->flags |= CM_BUF_ERROR;
116 bufp->error = VNOVNODE;
117 bufp->dataVersion = -1; /* bad */
118 bufp->dirtyCounter++;
119 if (bufp->flags & CM_BUF_WAITING) {
120 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
121 osi_Wakeup((long) &bufp);
123 lock_ReleaseMutex(&bufp->mx);
127 while(qdp = scp->bufReadsp) {
128 bufp = osi_GetQData(qdp);
129 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
132 lock_ObtainMutex(&bufp->mx);
133 bufp->cmFlags &= ~CM_BUF_CMFETCHING;
134 bufp->flags &= ~CM_BUF_DIRTY;
135 bufp->dirty_offset = 0;
136 bufp->dirty_length = 0;
137 bufp->flags |= CM_BUF_ERROR;
138 bufp->error = VNOVNODE;
139 bufp->dataVersion = -1; /* bad */
140 bufp->dirtyCounter++;
141 if (bufp->flags & CM_BUF_WAITING) {
142 osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
143 osi_Wakeup((long) &bufp);
145 lock_ReleaseMutex(&bufp->mx);
149 buf_CleanDirtyBuffers(scp);
151 /* look for things that shouldn't still be set */
152 osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
153 osi_assertx(scp->bufReadsp == NULL, "non-null cm_scache_t bufReadsp");
157 /* invalidate so next merge works fine;
158 * also initialize some flags */
160 scp->flags &= ~(CM_SCACHEFLAG_STATD
161 | CM_SCACHEFLAG_DELETED
163 | CM_SCACHEFLAG_PURERO
164 | CM_SCACHEFLAG_OVERQUOTA
165 | CM_SCACHEFLAG_OUTOFSPACE
166 | CM_SCACHEFLAG_EACCESS);
167 scp->serverModTime = 0;
168 scp->dataVersion = 0;
169 scp->bufDataVersionLow = 0;
170 scp->bulkStatProgress = hzero;
172 scp->waitQueueT = NULL;
174 if (scp->cbServerp) {
175 cm_PutServer(scp->cbServerp);
176 scp->cbServerp = NULL;
186 /* remove from dnlc */
190 /* discard cached status; if non-zero, Close
191 * tried to store this to server but failed */
194 /* discard symlink info */
195 scp->mountPointStringp[0] = '\0';
196 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
197 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
199 /* reset locking info */
200 scp->fileLocksH = NULL;
201 scp->fileLocksT = NULL;
202 scp->serverLock = (-1);
203 scp->exclusiveLocks = 0;
204 scp->sharedLocks = 0;
205 scp->lockDataVersion = -1;
207 /* not locked, but there can be no references to this guy
208 * while we hold the global refcount lock.
210 cm_FreeAllACLEnts(scp);
213 /* destroy directory Bplus Tree */
215 LARGE_INTEGER start, end;
216 QueryPerformanceCounter(&start);
218 freeBtree(scp->dirBplus);
219 scp->dirBplus = NULL;
220 QueryPerformanceCounter(&end);
222 bplus_free_time += (end.QuadPart - start.QuadPart);
229 /* called with cm_scacheLock write-locked; find a vnode to recycle.
230 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
232 cm_scache_t *cm_GetNewSCache(void)
237 lock_AssertWrite(&cm_scacheLock);
239 /* first pass - look for deleted objects */
240 for ( scp = cm_data.scacheLRULastp;
242 scp = (cm_scache_t *) osi_QPrev(&scp->q))
244 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
245 "invalid cm_scache_t address");
247 if (scp->refCount == 0) {
248 if (scp->flags & CM_SCACHEFLAG_DELETED) {
249 osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
250 if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
252 /* we found an entry, so return it */
253 /* now remove from the LRU queue and put it back at the
254 * head of the LRU queue.
256 cm_AdjustScacheLRU(scp);
261 osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
262 } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
263 /* we found an entry, so return it */
264 /* now remove from the LRU queue and put it back at the
265 * head of the LRU queue.
267 cm_AdjustScacheLRU(scp);
274 osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
277 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
278 /* There were no deleted scache objects that we could use. Try to find
279 * one that simply hasn't been used in a while.
281 for ( scp = cm_data.scacheLRULastp;
283 scp = (cm_scache_t *) osi_QPrev(&scp->q))
285 /* It is possible for the refCount to be zero and for there still
286 * to be outstanding dirty buffers. If there are dirty buffers,
287 * we must not recycle the scp. */
288 if (scp->refCount == 0 && scp->bufReadsp == NULL && scp->bufWritesp == NULL) {
289 if (!buf_DirtyBuffersExist(&scp->fid)) {
290 if (!cm_RecycleSCache(scp, 0)) {
291 /* we found an entry, so return it */
292 /* now remove from the LRU queue and put it back at the
293 * head of the LRU queue.
295 cm_AdjustScacheLRU(scp);
301 osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
305 osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
310 /* if we get here, we should allocate a new scache entry. We either are below
311 * quota or we have a leak and need to allocate a new one to avoid panicing.
313 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
314 osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
315 "invalid cm_scache_t address");
316 memset(scp, 0, sizeof(cm_scache_t));
317 scp->magic = CM_SCACHE_MAGIC;
318 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw");
319 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
321 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
323 scp->serverLock = -1;
325 /* and put it in the LRU queue */
326 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
327 if (!cm_data.scacheLRULastp)
328 cm_data.scacheLRULastp = scp;
329 cm_data.currentSCaches++;
330 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
332 scp->allNextp = cm_data.allSCachesp;
333 cm_data.allSCachesp = scp;
337 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
340 fidp->volume = volume;
342 fidp->unique = unique;
343 fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
346 /* like strcmp, only for fids */
347 __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
349 if (ap->hash != bp->hash)
351 if (ap->vnode != bp->vnode)
353 if (ap->volume != bp->volume)
355 if (ap->unique != bp->unique)
357 if (ap->cell != bp->cell)
362 void cm_fakeSCacheInit(int newFile)
365 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
366 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
367 /* can leave clientModTime at 0 */
368 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
369 cm_data.fakeSCache.unixModeBits = 0777;
370 cm_data.fakeSCache.length.LowPart = 1000;
371 cm_data.fakeSCache.linkCount = 1;
372 cm_data.fakeSCache.refCount = 1;
374 lock_InitializeRWLock(&cm_data.fakeSCache.rw, "cm_scache_t rw");
378 cm_ValidateSCache(void)
380 cm_scache_t * scp, *lscp;
383 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
384 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
385 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
386 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
390 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
392 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
393 if (scp->magic != CM_SCACHE_MAGIC) {
394 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
395 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
398 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
399 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
400 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
403 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
404 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
405 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
408 if (i > cm_data.currentSCaches ) {
409 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
410 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
413 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
414 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
415 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
420 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
421 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
422 if (scp->magic != CM_SCACHE_MAGIC) {
423 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
424 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
427 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
428 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
429 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
432 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
433 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
434 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
437 if (i > cm_data.currentSCaches ) {
438 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
439 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
442 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
443 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
444 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
449 for ( i=0; i < cm_data.scacheHashTableSize; i++ ) {
450 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
452 hash = CM_SCACHE_HASH(&scp->fid);
453 if (scp->magic != CM_SCACHE_MAGIC) {
454 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
455 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
458 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
459 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
460 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
463 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
464 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
465 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
469 afsi_log("cm_ValidateSCache failure: scp hash != hash index");
470 fprintf(stderr, "cm_ValidateSCache failure: scp hash != hash index\n");
476 return cm_dnlcValidate();
480 cm_SuspendSCache(void)
485 cm_GiveUpAllCallbacksAllServers(TRUE);
488 * After this call all servers are marked down.
489 * Do not clear the callbacks, instead change the
490 * expiration time so that the callbacks will be expired
491 * when the servers are marked back up. However, we
492 * want the callbacks to be preserved as long as the
493 * servers are down. That way if the machine resumes
494 * without network, the stat cache item will still be
499 lock_ObtainWrite(&cm_scacheLock);
500 for ( scp = cm_data.allSCachesp; scp; scp = scp->allNextp ) {
501 if (scp->cbServerp) {
502 if (scp->flags & CM_SCACHEFLAG_PURERO) {
503 cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
505 if (volp->cbExpiresRO == scp->cbExpires)
506 volp->cbExpiresRO = now+1;
510 scp->cbExpires = now+1;
513 lock_ReleaseWrite(&cm_scacheLock);
517 cm_ShutdownSCache(void)
521 lock_ObtainWrite(&cm_scacheLock);
523 for ( scp = cm_data.allSCachesp; scp;
524 scp = scp->allNextp ) {
525 if (scp->randomACLp) {
526 lock_ObtainWrite(&scp->rw);
527 cm_FreeAllACLEnts(scp);
528 lock_ReleaseWrite(&scp->rw);
531 if (scp->cbServerp) {
532 cm_PutServer(scp->cbServerp);
533 scp->cbServerp = NULL;
536 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
540 freeBtree(scp->dirBplus);
541 scp->dirBplus = NULL;
542 scp->dirDataVersion = -1;
543 lock_FinalizeRWLock(&scp->dirlock);
545 lock_FinalizeRWLock(&scp->rw);
546 lock_FinalizeRWLock(&scp->bufCreateLock);
548 lock_ReleaseWrite(&cm_scacheLock);
550 cm_GiveUpAllCallbacksAllServers(FALSE);
552 return cm_dnlcShutdown();
555 void cm_InitSCache(int newFile, long maxSCaches)
557 static osi_once_t once;
559 if (osi_Once(&once)) {
560 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
562 memset(cm_data.scacheHashTablep, 0, sizeof(cm_scache_t *) * cm_data.scacheHashTableSize);
563 cm_data.allSCachesp = NULL;
564 cm_data.currentSCaches = 0;
565 cm_data.maxSCaches = maxSCaches;
566 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
570 for ( scp = cm_data.allSCachesp; scp;
571 scp = scp->allNextp ) {
572 lock_InitializeRWLock(&scp->rw, "cm_scache_t rw");
573 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
575 lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
577 scp->cbServerp = NULL;
579 scp->fileLocksH = NULL;
580 scp->fileLocksT = NULL;
581 scp->serverLock = (-1);
582 scp->lastRefreshCycle = 0;
583 scp->exclusiveLocks = 0;
584 scp->sharedLocks = 0;
591 scp->dirBplus = NULL;
592 scp->dirDataVersion = -1;
594 scp->waitQueueT = NULL;
595 scp->flags &= ~CM_SCACHEFLAG_WAITING;
598 cm_allFileLocks = NULL;
599 cm_freeFileLocks = NULL;
600 cm_lockRefreshCycle = 0;
601 cm_fakeSCacheInit(newFile);
602 cm_allFreeWaiters = NULL;
603 cm_dnlcInit(newFile);
608 /* version that doesn't bother creating the entry if we don't find it */
609 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
614 hash = CM_SCACHE_HASH(fidp);
616 if (fidp->cell == 0) {
620 lock_ObtainRead(&cm_scacheLock);
621 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
622 if (cm_FidCmp(fidp, &scp->fid) == 0) {
623 cm_HoldSCacheNoLock(scp);
624 lock_ConvertRToW(&cm_scacheLock);
625 cm_AdjustScacheLRU(scp);
626 lock_ReleaseWrite(&cm_scacheLock);
630 lock_ReleaseRead(&cm_scacheLock);
634 #ifdef DEBUG_REFCOUNT
635 long cm_GetSCacheDbg(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
636 cm_req_t *reqp, char * file, long line)
638 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
643 cm_scache_t *scp = NULL;
645 cm_volume_t *volp = NULL;
647 int special = 0; // yj: boolean variable to test if file is on root.afs
649 extern cm_fid_t cm_rootFid;
651 hash = CM_SCACHE_HASH(fidp);
653 osi_assertx(fidp->cell != 0, "unassigned cell value");
655 #ifdef AFS_FREELANCE_CLIENT
656 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
657 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
658 !(fidp->vnode==0x1 && fidp->unique==0x1));
659 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
660 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
661 fidp->vnode==0x1 && fidp->unique==0x1);
664 // yj: check if we have the scp, if so, we don't need
665 // to do anything else
666 lock_ObtainWrite(&cm_scacheLock);
667 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
668 if (cm_FidCmp(fidp, &scp->fid) == 0) {
669 #ifdef DEBUG_REFCOUNT
670 afsi_log("%s:%d cm_GetSCache (1) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
671 osi_Log1(afsd_logp,"cm_GetSCache (1) outScpp 0x%p", scp);
673 #ifdef AFS_FREELANCE_CLIENT
674 if (cm_freelanceEnabled && special &&
675 cm_data.fakeDirVersion != scp->dataVersion)
678 cm_HoldSCacheNoLock(scp);
680 cm_AdjustScacheLRU(scp);
681 lock_ReleaseWrite(&cm_scacheLock);
686 // yj: when we get here, it means we don't have an scp
687 // so we need to either load it or fake it, depending
688 // on whether the file is "special", see below.
690 // yj: if we're trying to get an scp for a file that's
691 // on root.afs of homecell, we want to handle it specially
692 // because we have to fill in the status stuff 'coz we
693 // don't want trybulkstat to fill it in for us
694 #ifdef AFS_FREELANCE_CLIENT
695 if (cm_freelanceEnabled && isRoot) {
696 osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
697 /* freelance: if we are trying to get the root scp for the first
698 * time, we will just put in a place holder entry.
703 if (cm_freelanceEnabled && special) {
704 char mp[MOUNTPOINTLEN] = "";
707 lock_ReleaseWrite(&cm_scacheLock);
708 osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
710 if (cm_getLocalMountPointChange()) { // check for changes
711 cm_clearLocalMountPointChange(); // clear the changefile
712 cm_reInitLocalMountPoints(); // start reinit
715 lock_ObtainMutex(&cm_Freelance_Lock);
716 if (fidp->vnode >= 2 && fidp->vnode - 2 < cm_noLocalMountPoints) {
717 strncpy(mp,(cm_localMountPoints+fidp->vnode-2)->mountPointStringp, MOUNTPOINTLEN);
718 mp[MOUNTPOINTLEN-1] = '\0';
719 if ( !strnicmp(mp, "msdfs:", strlen("msdfs:")) )
720 fileType = CM_SCACHETYPE_DFSLINK;
722 fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
724 fileType = CM_SCACHETYPE_INVALID;
727 lock_ReleaseMutex(&cm_Freelance_Lock);
728 lock_ObtainWrite(&cm_scacheLock);
730 scp = cm_GetNewSCache();
732 osi_Log0(afsd_logp,"cm_GetSCache unable to obtain *new* scache entry");
733 lock_ReleaseWrite(&cm_scacheLock);
734 return CM_ERROR_WOULDBLOCK;
737 #if not_too_dangerous
738 /* dropping the cm_scacheLock allows more than one thread
739 * to obtain the same cm_scache_t from the LRU list. Since
740 * the refCount is known to be zero at this point we have to
741 * assume that no one else is using the one this is returned.
743 lock_ReleaseWrite(&cm_scacheLock);
744 lock_ObtainWrite(&scp->rw);
745 lock_ObtainWrite(&cm_scacheLock);
748 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
749 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
750 scp->dotdotFid.unique=1;
751 scp->dotdotFid.vnode=1;
752 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
753 if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
754 scp->nextp = cm_data.scacheHashTablep[hash];
755 cm_data.scacheHashTablep[hash] = scp;
756 scp->flags |= CM_SCACHEFLAG_INHASH;
759 osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
760 scp->fileType = fileType;
761 scp->length.LowPart = (DWORD)strlen(mp)+4;
762 scp->length.HighPart = 0;
763 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
765 scp->unixModeBits=0777;
766 scp->clientModTime=FakeFreelanceModTime;
767 scp->serverModTime=FakeFreelanceModTime;
768 scp->parentUnique = 0x1;
769 scp->parentVnode=0x1;
771 scp->dataVersion=cm_data.fakeDirVersion;
772 scp->bufDataVersionLow=cm_data.fakeDirVersion;
773 scp->lockDataVersion=-1; /* no lock yet */
774 #if not_too_dangerous
775 lock_ReleaseWrite(&scp->rw);
778 lock_ReleaseWrite(&cm_scacheLock);
779 #ifdef DEBUG_REFCOUNT
780 afsi_log("%s:%d cm_GetSCache (2) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
781 osi_Log1(afsd_logp,"cm_GetSCache (2) outScpp 0x%p", scp);
786 #endif /* AFS_FREELANCE_CLIENT */
788 /* otherwise, we need to find the volume */
789 if (!cm_freelanceEnabled || !isRoot) {
790 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
791 cellp = cm_FindCellByID(fidp->cell, 0);
793 return CM_ERROR_NOSUCHCELL;
795 code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
798 lock_ObtainWrite(&cm_scacheLock);
801 /* otherwise, we have the volume, now reverify that the scp doesn't
802 * exist, and proceed.
804 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
805 if (cm_FidCmp(fidp, &scp->fid) == 0) {
806 #ifdef DEBUG_REFCOUNT
807 afsi_log("%s:%d cm_GetSCache (3) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
808 osi_Log1(afsd_logp,"cm_GetSCache (3) outScpp 0x%p", scp);
810 cm_HoldSCacheNoLock(scp);
811 cm_AdjustScacheLRU(scp);
812 lock_ReleaseWrite(&cm_scacheLock);
820 /* now, if we don't have the fid, recycle something */
821 scp = cm_GetNewSCache();
823 osi_Log0(afsd_logp,"cm_GetNewSCache unable to obtain *new* scache entry");
824 lock_ReleaseWrite(&cm_scacheLock);
827 return CM_ERROR_WOULDBLOCK;
829 osi_Log2(afsd_logp,"cm_GetNewSCache returns scp 0x%x flags 0x%x", scp, scp->flags);
831 osi_assertx(!(scp->flags & CM_SCACHEFLAG_INHASH), "CM_SCACHEFLAG_INHASH set");
833 #if not_too_dangerous
834 /* dropping the cm_scacheLock allows more than one thread
835 * to obtain the same cm_scache_t from the LRU list. Since
836 * the refCount is known to be zero at this point we have to
837 * assume that no one else is using the one this is returned.
839 lock_ReleaseWrite(&cm_scacheLock);
840 lock_ObtainWrite(&scp->rw);
841 lock_ObtainWrite(&cm_scacheLock);
844 if (!cm_freelanceEnabled || !isRoot) {
845 /* if this scache entry represents a volume root then we need
846 * to copy the dotdotFipd from the volume structure where the
847 * "master" copy is stored (defect 11489)
849 if (volp->vol[ROVOL].ID == fidp->volume) {
850 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
851 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
852 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
853 } else if (volp->vol[BACKVOL].ID == fidp->volume) {
854 scp->flags |= CM_SCACHEFLAG_RO;
855 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
856 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
858 if (scp->fid.vnode == 1 && scp->fid.unique == 1)
859 scp->dotdotFid = cm_VolumeStateByType(volp, RWVOL)->dotdotFid;
864 scp->nextp = cm_data.scacheHashTablep[hash];
865 cm_data.scacheHashTablep[hash] = scp;
866 scp->flags |= CM_SCACHEFLAG_INHASH;
868 osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
869 #if not_too_dangerous
870 lock_ReleaseWrite(&scp->rw);
873 /* XXX - The following fields in the cm_scache are
879 lock_ReleaseWrite(&cm_scacheLock);
881 /* now we have a held scache entry; just return it */
883 #ifdef DEBUG_REFCOUNT
884 afsi_log("%s:%d cm_GetSCache (4) outScpp 0x%p ref %d", file, line, scp, scp->refCount);
885 osi_Log1(afsd_logp,"cm_GetSCache (4) outScpp 0x%p", scp);
890 /* Returns a held reference to the scache's parent
892 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
897 cm_scache_t * pscp = NULL;
899 lock_ObtainWrite(&cm_scacheLock);
900 cm_SetFid(&parent_fid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
902 if (cm_FidCmp(&scp->fid, &parent_fid)) {
903 i = CM_SCACHE_HASH(&parent_fid);
904 for (pscp = cm_data.scacheHashTablep[i]; pscp; pscp = pscp->nextp) {
905 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
906 cm_HoldSCacheNoLock(pscp);
912 lock_ReleaseWrite(&cm_scacheLock);
917 void cm_SyncOpAddToWaitQueue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
919 cm_scache_waiter_t * w;
921 lock_ObtainWrite(&cm_scacheLock);
922 if (cm_allFreeWaiters == NULL) {
923 w = malloc(sizeof(*w));
924 memset(w, 0, sizeof(*w));
926 w = (cm_scache_waiter_t *) cm_allFreeWaiters;
927 osi_QRemove(&cm_allFreeWaiters, (osi_queue_t *) w);
930 w->threadId = thrd_Current();
932 cm_HoldSCacheNoLock(scp);
936 osi_QAddT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
937 lock_ReleaseWrite(&cm_scacheLock);
939 osi_Log2(afsd_logp, "cm_SyncOpAddToWaitQueue : Adding thread to wait queue scp 0x%p w 0x%p", scp, w);
942 int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
944 cm_scache_waiter_t * w;
947 osi_Log0(afsd_logp, "cm_SyncOpCheckContinue checking for continuation");
949 lock_ObtainRead(&cm_scacheLock);
950 for (w = (cm_scache_waiter_t *)scp->waitQueueH;
952 w = (cm_scache_waiter_t *)osi_QNext((osi_queue_t *) w)) {
953 if (w->flags == flags && w->bufp == bufp) {
958 osi_assertx(w != NULL, "null cm_scache_waiter_t");
959 this_is_me = (w->threadId == thrd_Current());
960 lock_ReleaseRead(&cm_scacheLock);
963 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue MISS: Waiter 0x%p", w);
967 osi_Log1(afsd_logp, "cm_SyncOpCheckContinue HIT: Waiter 0x%p", w);
969 lock_ObtainWrite(&cm_scacheLock);
970 osi_QRemoveHT(&scp->waitQueueH, &scp->waitQueueT, (osi_queue_t *) w);
971 cm_ReleaseSCacheNoLock(scp);
972 memset(w, 0, sizeof(*w));
973 osi_QAdd(&cm_allFreeWaiters, (osi_queue_t *) w);
974 lock_ReleaseWrite(&cm_scacheLock);
980 /* synchronize a fetch, store, read, write, fetch status or store status.
981 * Called with scache mutex held, and returns with it held, but temporarily
982 * drops it during the fetch.
984 * At most one flag can be on in flags, if this is an RPC request.
986 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
988 * There are a lot of weird restrictions here; here's an attempt to explain the
989 * rationale for the concurrency restrictions implemented in this function.
991 * First, although the file server will break callbacks when *another* machine
992 * modifies a file or status block, the client itself is responsible for
993 * concurrency control on its own requests. Callback breaking events are rare,
994 * and simply invalidate any concurrent new status info.
996 * In the absence of callback breaking messages, we need to know how to
997 * synchronize incoming responses describing updates to files. We synchronize
998 * operations that update the data version by comparing the data versions.
999 * However, updates that do not update the data, but only the status, can't be
1000 * synchronized with fetches or stores, since there's nothing to compare
1001 * to tell which operation executed first at the server.
1003 * Thus, we can allow multiple ops that change file data, or dir data, and
1004 * fetches. However, status storing ops have to be done serially.
1006 * Furthermore, certain data-changing ops are incompatible: we can't read or
1007 * write a buffer while doing a truncate. We can't read and write the same
1008 * buffer at the same time, or write while fetching or storing, or read while
1009 * fetching a buffer (this may change). We can't fetch and store at the same
1012 * With respect to status, we can't read and write at the same time, read while
1013 * fetching, write while fetching or storing, or fetch and store at the same time.
1015 * We can't allow a get callback RPC to run in concurrently with something that
1016 * will return updated status, since we could start a call, have the server
1017 * return status, have another machine make an update to the status (which
1018 * doesn't change serverModTime), have the original machine get a new callback,
1019 * and then have the original machine merge in the early, old info from the
1020 * first call. At this point, the easiest way to avoid this problem is to have
1021 * getcallback calls conflict with all others for the same vnode. Other calls
1022 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
1023 * vnode must be careful not to merge in their status unless they have obtained
1024 * a callback from the start of their call.
1026 * Note added 1/23/96
1027 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
1028 * Each such RPC passes a FileLength parameter, which the server uses to do
1029 * pre-truncation if necessary. So if two RPC's are processed out of order at
1030 * the server, the one with the smaller FileLength will be processed last,
1031 * possibly resulting in a bogus truncation. The simplest way to avoid this
1032 * is to serialize all StoreData RPC's. This is the reason we defined
1033 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
1035 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
1036 afs_uint32 rights, afs_uint32 flags)
1038 osi_queueData_t *qdp;
1041 afs_uint32 outRights;
1043 afs_uint32 sleep_scp_flags = 0;
1044 afs_uint32 sleep_buf_cmflags = 0;
1045 afs_uint32 sleep_scp_bufs = 0;
1047 int getAccessRights = 1;
1049 lock_AssertWrite(&scp->rw);
1051 /* lookup this first */
1052 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
1055 osi_assertx(bufp->refCount > 0, "cm_buf_t refCount 0");
1058 /* Do the access check. Now we don't really do the access check
1059 * atomically, since the caller doesn't expect the parent dir to be
1060 * returned locked, and that is what we'd have to do to prevent a
1061 * callback breaking message on the parent due to a setacl call from
1062 * being processed while we're running. So, instead, we check things
1063 * here, and if things look fine with the access, we proceed to finish
1064 * the rest of this check. Sort of a hack, but probably good enough.
1068 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
1069 /* if we're bringing in a new status block, ensure that
1070 * we aren't already doing so, and that no one is
1071 * changing the status concurrently, either. We need
1072 * to do this, even if the status is of a different
1073 * type, since we don't have the ability to figure out,
1074 * in the AFS 3 protocols, which status-changing
1075 * operation ran first, or even which order a read and
1076 * a write occurred in.
1078 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1079 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1080 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
1084 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
1085 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
1086 /* if we're going to make an RPC to change the status, make sure
1087 * that no one is bringing in or sending out the status.
1089 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
1090 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1091 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1094 if (scp->bufReadsp || scp->bufWritesp) {
1095 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
1099 if (flags & CM_SCACHESYNC_FETCHDATA) {
1100 /* if we're bringing in a new chunk of data, make sure that
1101 * nothing is happening to that chunk, and that we aren't
1102 * changing the basic file status info, either.
1104 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1105 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1106 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
1109 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1110 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want FETCHDATA", scp, bufp);
1114 if (flags & CM_SCACHESYNC_STOREDATA) {
1115 /* same as fetch data */
1116 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1117 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
1118 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
1121 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMWRITING))) {
1122 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING|BUF_CMWRITING want STOREDATA", scp, bufp);
1127 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
1128 /* Don't allow concurrent StoreData RPC's */
1129 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
1130 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
1135 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
1136 /* Don't allow more than one BKG store request */
1137 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
1138 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
1143 if (flags & CM_SCACHESYNC_LOCK) {
1144 /* Don't allow concurrent fiddling with lock lists */
1145 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
1146 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
1151 /* now the operations that don't correspond to making RPCs */
1152 if (flags & CM_SCACHESYNC_GETSTATUS) {
1153 /* we can use the status that's here, if we're not
1154 * bringing in new status.
1156 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
1157 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
1161 if (flags & CM_SCACHESYNC_SETSTATUS) {
1162 /* we can make a change to the local status, as long as
1163 * the status isn't changing now.
1165 * If we're fetching or storing a chunk of data, we can
1166 * change the status locally, since the fetch/store
1167 * operations don't change any of the data that we're
1170 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
1171 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
1175 if (flags & CM_SCACHESYNC_READ) {
1176 /* we're going to read the data, make sure that the
1177 * status is available, and that the data is here. It
1178 * is OK to read while storing the data back.
1180 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
1181 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
1184 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
1185 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
1188 if (bufp && (bufp->cmFlags & CM_BUF_CMWRITING)) {
1189 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMWRITING want READ", scp, bufp);
1193 if (flags & CM_SCACHESYNC_WRITE) {
1194 /* don't write unless the status is stable and the chunk
1197 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
1198 | CM_SCACHEFLAG_SIZESTORING)) {
1199 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
1202 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING |
1204 CM_BUF_CMWRITING))) {
1205 osi_Log3(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is %s want WRITE",
1207 ((bufp->cmFlags & CM_BUF_CMFETCHING) ? "CM_BUF_CMFETCHING":
1208 ((bufp->cmFlags & CM_BUF_CMSTORING) ? "CM_BUF_CMSTORING" :
1209 ((bufp->cmFlags & CM_BUF_CMWRITING) ? "CM_BUF_CMWRITING" :
1215 // yj: modified this so that callback only checked if we're
1216 // not checking something on /afs
1217 /* fix the conditional to match the one in cm_HaveCallback */
1218 if ((flags & CM_SCACHESYNC_NEEDCALLBACK)
1219 #ifdef AFS_FREELANCE_CLIENT
1220 && (!cm_freelanceEnabled ||
1221 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
1222 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
1223 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
1224 cm_fakeDirCallback < 2)
1225 #endif /* AFS_FREELANCE_CLIENT */
1227 if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
1228 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
1231 lock_ReleaseMutex(&bufp->mx);
1232 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
1234 lock_ReleaseWrite(&scp->rw);
1235 lock_ObtainMutex(&bufp->mx);
1236 lock_ObtainWrite(&scp->rw);
1240 flags &= ~CM_SCACHESYNC_FORCECB; /* only force once */
1246 /* can't check access rights without a callback */
1247 osi_assertx(flags & CM_SCACHESYNC_NEEDCALLBACK, "!CM_SCACHESYNC_NEEDCALLBACK");
1249 if ((rights & (PRSFS_WRITE|PRSFS_DELETE)) && (scp->flags & CM_SCACHEFLAG_RO))
1250 return CM_ERROR_READONLY;
1252 if (cm_HaveAccessRights(scp, userp, rights, &outRights) || !getAccessRights) {
1253 if (~outRights & rights)
1254 return CM_ERROR_NOACCESS;
1257 /* we don't know the required access rights */
1258 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
1259 code = cm_GetAccessRights(scp, userp, reqp);
1261 lock_ReleaseWrite(&scp->rw);
1262 lock_ObtainMutex(&bufp->mx);
1263 lock_ObtainWrite(&scp->rw);
1267 getAccessRights = 0; /* do not repeat */
1272 /* if we get here, we're happy */
1276 /* first check if we're not supposed to wait: fail
1277 * in this case, returning with everything still locked.
1279 if (flags & CM_SCACHESYNC_NOWAIT)
1280 return CM_ERROR_WOULDBLOCK;
1282 /* These are used for minidump debugging */
1283 sleep_scp_flags = scp->flags; /* so we know why we slept */
1284 sleep_buf_cmflags = bufp ? bufp->cmFlags : 0;
1285 sleep_scp_bufs = (scp->bufReadsp ? 1 : 0) | (scp->bufWritesp ? 2 : 0);
1287 /* wait here, then try again */
1288 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
1289 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
1291 scp->waitRequests++;
1292 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
1293 scp, scp->waitCount, scp->waitRequests);
1295 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
1296 scp->flags |= CM_SCACHEFLAG_WAITING;
1297 scp->waitCount = scp->waitRequests = 1;
1300 cm_SyncOpAddToWaitQueue(scp, flags, bufp);
1304 lock_ReleaseMutex(&bufp->mx);
1305 osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
1307 lock_ObtainMutex(&bufp->mx);
1308 lock_ObtainWrite(&scp->rw);
1309 } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
1311 smb_UpdateServerPriority();
1314 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
1315 scp, scp->waitCount, scp->waitRequests);
1316 if (scp->waitCount == 0) {
1317 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
1318 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1319 scp->waitRequests = 0;
1321 } /* big while loop */
1323 /* now, update the recorded state for RPC-type calls */
1324 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1325 scp->flags |= CM_SCACHEFLAG_FETCHING;
1326 if (flags & CM_SCACHESYNC_STORESTATUS)
1327 scp->flags |= CM_SCACHEFLAG_STORING;
1328 if (flags & CM_SCACHESYNC_STORESIZE)
1329 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
1330 if (flags & CM_SCACHESYNC_GETCALLBACK)
1331 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
1332 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1333 scp->flags |= CM_SCACHEFLAG_DATASTORING;
1334 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1335 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
1336 if (flags & CM_SCACHESYNC_LOCK)
1337 scp->flags |= CM_SCACHEFLAG_LOCKING;
1339 /* now update the buffer pointer */
1340 if (flags & CM_SCACHESYNC_FETCHDATA) {
1341 /* ensure that the buffer isn't already in the I/O list */
1343 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1344 tbufp = osi_GetQData(qdp);
1345 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1349 /* queue a held reference to the buffer in the "reading" I/O list */
1350 qdp = osi_QDAlloc();
1351 osi_SetQData(qdp, bufp);
1354 bufp->cmFlags |= CM_BUF_CMFETCHING;
1356 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1359 if (flags & CM_SCACHESYNC_STOREDATA) {
1360 /* ensure that the buffer isn't already in the I/O list */
1362 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1363 tbufp = osi_GetQData(qdp);
1364 osi_assertx(tbufp != bufp, "unexpected cm_buf_t value");
1368 /* queue a held reference to the buffer in the "writing" I/O list */
1369 qdp = osi_QDAlloc();
1370 osi_SetQData(qdp, bufp);
1373 bufp->cmFlags |= CM_BUF_CMSTORING;
1375 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1378 if (flags & CM_SCACHESYNC_WRITE) {
1379 /* mark the buffer as being written to. */
1381 bufp->cmFlags |= CM_BUF_CMWRITING;
1388 /* for those syncops that setup for RPCs.
1389 * Called with scache locked.
1391 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
1393 osi_queueData_t *qdp;
1396 lock_AssertWrite(&scp->rw);
1398 /* now, update the recorded state for RPC-type calls */
1399 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1400 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1401 if (flags & CM_SCACHESYNC_STORESTATUS)
1402 scp->flags &= ~CM_SCACHEFLAG_STORING;
1403 if (flags & CM_SCACHESYNC_STORESIZE)
1404 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1405 if (flags & CM_SCACHESYNC_GETCALLBACK)
1406 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1407 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1408 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1409 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1410 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1411 if (flags & CM_SCACHESYNC_LOCK)
1412 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1414 /* now update the buffer pointer */
1415 if (flags & CM_SCACHESYNC_FETCHDATA) {
1418 /* ensure that the buffer isn't already in the I/O list */
1419 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1420 tbufp = osi_GetQData(qdp);
1425 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1430 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1431 if (bufp->flags & CM_BUF_WAITING) {
1432 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1433 osi_Wakeup((LONG_PTR) &bufp);
1440 /* now update the buffer pointer */
1441 if (flags & CM_SCACHESYNC_STOREDATA) {
1443 /* ensure that the buffer isn't already in the I/O list */
1444 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1445 tbufp = osi_GetQData(qdp);
1450 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1455 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1456 if (bufp->flags & CM_BUF_WAITING) {
1457 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1458 osi_Wakeup((LONG_PTR) &bufp);
1465 if (flags & CM_SCACHESYNC_WRITE) {
1467 osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
1469 bufp->cmFlags &= ~CM_BUF_CMWRITING;
1473 /* and wakeup anyone who is waiting */
1474 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1475 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1476 osi_Wakeup((LONG_PTR) &scp->flags);
1480 /* merge in a response from an RPC. The scp must be locked, and the callback
1483 * Don't overwrite any status info that is dirty, since we could have a store
1484 * operation (such as store data) that merges some info in, and we don't want
1485 * to lose the local updates. Typically, there aren't many updates we do
1486 * locally, anyway, probably only mtime.
1488 * There is probably a bug in here where a chmod (which doesn't change
1489 * serverModTime) that occurs between two fetches, both of whose responses are
1490 * handled after the callback breaking is done, but only one of whose calls
1491 * started before that, can cause old info to be merged from the first call.
1493 void cm_MergeStatus(cm_scache_t *dscp,
1494 cm_scache_t *scp, AFSFetchStatus *statusp,
1495 AFSVolSync *volsyncp,
1496 cm_user_t *userp, afs_uint32 flags)
1498 afs_uint64 dataVersion;
1500 // yj: i want to create some fake status for the /afs directory and the
1501 // entries under that directory
1502 #ifdef AFS_FREELANCE_CLIENT
1503 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1504 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1505 statusp->InterfaceVersion = 0x1;
1506 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1507 statusp->LinkCount = scp->linkCount;
1508 statusp->Length = cm_fakeDirSize;
1509 statusp->Length_hi = 0;
1510 statusp->DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1511 statusp->Author = 0x1;
1512 statusp->Owner = 0x0;
1513 statusp->CallerAccess = 0x9;
1514 statusp->AnonymousAccess = 0x9;
1515 statusp->UnixModeBits = 0777;
1516 statusp->ParentVnode = 0x1;
1517 statusp->ParentUnique = 0x1;
1518 statusp->ResidencyMask = 0;
1519 statusp->ClientModTime = FakeFreelanceModTime;
1520 statusp->ServerModTime = FakeFreelanceModTime;
1522 statusp->SyncCounter = 0;
1523 statusp->dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1524 statusp->errorCode = 0;
1526 #endif /* AFS_FREELANCE_CLIENT */
1528 if (statusp->errorCode != 0) {
1529 scp->flags |= CM_SCACHEFLAG_EACCESS;
1530 osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
1532 scp->fileType = 0; /* unknown */
1534 scp->serverModTime = 0;
1535 scp->clientModTime = 0;
1536 scp->length.LowPart = 0;
1537 scp->length.HighPart = 0;
1538 scp->serverLength.LowPart = 0;
1539 scp->serverLength.HighPart = 0;
1543 scp->unixModeBits = 0;
1545 scp->dataVersion = 0;
1546 scp->bufDataVersionLow = 0;
1549 scp->parentVnode = dscp->fid.vnode;
1550 scp->parentUnique = dscp->fid.unique;
1552 scp->parentVnode = 0;
1553 scp->parentUnique = 0;
1557 scp->flags &= ~CM_SCACHEFLAG_EACCESS;
1560 dataVersion = statusp->dataVersionHigh;
1562 dataVersion |= statusp->DataVersion;
1564 if (!(flags & CM_MERGEFLAG_FORCE) && dataVersion < scp->dataVersion) {
1565 struct cm_cell *cellp;
1567 cellp = cm_FindCellByID(scp->fid.cell, 0);
1568 if (scp->cbServerp) {
1569 struct cm_volume *volp = NULL;
1571 cm_FindVolumeByID(cellp, scp->fid.volume, userp,
1572 (cm_req_t *) NULL, CM_GETVOL_FLAG_CREATE, &volp);
1573 osi_Log2(afsd_logp, "old data from server %x volume %s",
1574 scp->cbServerp->addr.sin_addr.s_addr,
1575 volp ? volp->namep : "(unknown)");
1579 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1580 scp, scp->dataVersion, dataVersion);
1581 /* we have a number of data fetch/store operations running
1582 * concurrently, and we can tell which one executed last at the
1583 * server by its mtime.
1584 * Choose the one with the largest mtime, and ignore the rest.
1586 * These concurrent calls are incompatible with setting the
1587 * mtime, so we won't have a locally changed mtime here.
1589 * We could also have ACL info for a different user than usual,
1590 * in which case we have to do that part of the merge, anyway.
1591 * We won't have to worry about the info being old, since we
1592 * won't have concurrent calls
1593 * that change file status running from this machine.
1595 * Added 3/17/98: if we see data version regression on an RO
1596 * file, it's probably due to a server holding an out-of-date
1597 * replica, rather than to concurrent RPC's. Failures to
1598 * release replicas are now flagged by the volserver, but only
1599 * since AFS 3.4 5.22, so there are plenty of clients getting
1600 * out-of-date replicas out there.
1602 * If we discover an out-of-date replica, by this time it's too
1603 * late to go to another server and retry. Also, we can't
1604 * reject the merge, because then there is no way for
1605 * GetAccess to do its work, and the caller gets into an
1606 * infinite loop. So we just grin and bear it.
1608 if (!(scp->flags & CM_SCACHEFLAG_RO))
1612 scp->serverModTime = statusp->ServerModTime;
1614 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1615 scp->clientModTime = statusp->ClientModTime;
1617 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1618 scp->length.LowPart = statusp->Length;
1619 scp->length.HighPart = statusp->Length_hi;
1622 scp->serverLength.LowPart = statusp->Length;
1623 scp->serverLength.HighPart = statusp->Length_hi;
1625 scp->linkCount = statusp->LinkCount;
1626 scp->owner = statusp->Owner;
1627 scp->group = statusp->Group;
1628 scp->unixModeBits = statusp->UnixModeBits & 07777;
1630 if (statusp->FileType == File)
1631 scp->fileType = CM_SCACHETYPE_FILE;
1632 else if (statusp->FileType == Directory)
1633 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1634 else if (statusp->FileType == SymbolicLink) {
1635 if ((scp->unixModeBits & 0111) == 0)
1636 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1638 scp->fileType = CM_SCACHETYPE_SYMLINK;
1641 osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
1642 scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
1644 /* and other stuff */
1645 scp->parentVnode = statusp->ParentVnode;
1646 scp->parentUnique = statusp->ParentUnique;
1648 /* and merge in the private acl cache info, if this is more than the public
1649 * info; merge in the public stuff in any case.
1651 scp->anyAccess = statusp->AnonymousAccess;
1653 if (userp != NULL) {
1654 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1657 if (scp->dataVersion != 0 &&
1658 (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
1659 (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
1661 * We now know that all of the data buffers that we have associated
1662 * with this scp are invalid. Subsequent operations will go faster
1663 * if the buffers are removed from the hash tables.
1665 * We do not remove directory buffers if the dataVersion delta is 1 because
1666 * those version numbers will be updated as part of the directory operation.
1668 * We do not remove storedata buffers because they will still be valid.
1673 cm_buf_t *bp, *prevBp, *nextBp;
1675 lock_ObtainWrite(&buf_globalLock);
1676 i = BUF_FILEHASH(&scp->fid);
1677 for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
1679 nextBp = bp->fileHashp;
1681 * if the buffer belongs to this stat cache entry
1682 * and the buffer mutex can be obtained, check the
1683 * reference count and if it is zero, remove the buffer
1684 * from the hash tables. If there are references,
1685 * the buffer might be updated to the current version
1686 * so leave it in place.
1688 if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
1689 lock_TryMutex(&bp->mx)) {
1690 if (bp->refCount == 0 &&
1691 !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
1692 prevBp = bp->fileHashBackp;
1693 bp->fileHashBackp = bp->fileHashp = NULL;
1695 prevBp->fileHashp = nextBp;
1697 cm_data.buf_fileHashTablepp[i] = nextBp;
1699 nextBp->fileHashBackp = prevBp;
1701 j = BUF_HASH(&bp->fid, &bp->offset);
1702 lbpp = &(cm_data.buf_scacheHashTablepp[j]);
1703 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
1708 *lbpp = bp->hashp; /* hash out */
1711 bp->flags &= ~CM_BUF_INHASH;
1713 lock_ReleaseMutex(&bp->mx);
1716 lock_ReleaseWrite(&buf_globalLock);
1719 /* We maintain a range of buffer dataVersion values which are considered
1720 * valid. This avoids the need to update the dataVersion on each buffer
1721 * object during an uncontested storeData operation. As a result this
1722 * merge status no longer has performance characteristics derived from
1723 * the size of the file.
1725 if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
1726 (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
1727 scp->bufDataVersionLow == 0)
1728 scp->bufDataVersionLow = dataVersion;
1730 scp->dataVersion = dataVersion;
1733 * If someone is waiting for status information, we can wake them up
1734 * now even though the entity that issued the FetchStatus may not
1735 * have completed yet.
1737 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1740 /* note that our stat cache info is incorrect, so force us eventually
1741 * to stat the file again. There may be dirty data associated with
1742 * this vnode, and we want to preserve that information.
1744 * This function works by simply simulating a loss of the callback.
1746 * This function must be called with the scache locked.
1748 void cm_DiscardSCache(cm_scache_t *scp)
1750 lock_AssertWrite(&scp->rw);
1751 if (scp->cbServerp) {
1752 cm_PutServer(scp->cbServerp);
1753 scp->cbServerp = NULL;
1756 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1757 cm_dnlcPurgedp(scp);
1758 cm_dnlcPurgevp(scp);
1759 cm_FreeAllACLEnts(scp);
1761 if (scp->fileType == CM_SCACHETYPE_DFSLINK)
1762 cm_VolStatus_Invalidate_DFS_Mapping(scp);
1764 /* Force mount points and symlinks to be re-evaluated */
1765 scp->mountPointStringp[0] = '\0';
1768 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1770 afsFidp->Volume = fidp->volume;
1771 afsFidp->Vnode = fidp->vnode;
1772 afsFidp->Unique = fidp->unique;
1775 #ifdef DEBUG_REFCOUNT
1776 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1778 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1783 osi_assertx(scp != NULL, "null cm_scache_t");
1784 lock_AssertAny(&cm_scacheLock);
1785 refCount = InterlockedIncrement(&scp->refCount);
1786 #ifdef DEBUG_REFCOUNT
1787 osi_Log2(afsd_logp,"cm_HoldSCacheNoLock scp 0x%p ref %d",scp, refCount);
1788 afsi_log("%s:%d cm_HoldSCacheNoLock scp 0x%p, ref %d", file, line, scp, refCount);
1792 #ifdef DEBUG_REFCOUNT
1793 void cm_HoldSCacheDbg(cm_scache_t *scp, char * file, long line)
1795 void cm_HoldSCache(cm_scache_t *scp)
1800 osi_assertx(scp != NULL, "null cm_scache_t");
1801 lock_ObtainRead(&cm_scacheLock);
1802 refCount = InterlockedIncrement(&scp->refCount);
1803 #ifdef DEBUG_REFCOUNT
1804 osi_Log2(afsd_logp,"cm_HoldSCache scp 0x%p ref %d",scp, refCount);
1805 afsi_log("%s:%d cm_HoldSCache scp 0x%p ref %d", file, line, scp, refCount);
1807 lock_ReleaseRead(&cm_scacheLock);
1810 #ifdef DEBUG_REFCOUNT
1811 void cm_ReleaseSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
1813 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1819 osi_assertx(scp != NULL, "null cm_scache_t");
1820 lock_AssertAny(&cm_scacheLock);
1822 lockstate = lock_GetRWLockState(&cm_scacheLock);
1823 refCount = InterlockedDecrement(&scp->refCount);
1824 #ifdef DEBUG_REFCOUNT
1826 osi_Log1(afsd_logp,"cm_ReleaseSCacheNoLock about to panic scp 0x%x",scp);
1828 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1829 #ifdef DEBUG_REFCOUNT
1830 osi_Log2(afsd_logp,"cm_ReleaseSCacheNoLock scp 0x%p ref %d",scp, refCount);
1831 afsi_log("%s:%d cm_ReleaseSCacheNoLock scp 0x%p ref %d", file, line, scp, refCount);
1834 if (refCount == 0 && (scp->flags & CM_SCACHEFLAG_DELETED)) {
1836 if (lockstate != OSI_RWLOCK_WRITEHELD)
1837 lock_ConvertRToW(&cm_scacheLock);
1838 lock_ObtainWrite(&scp->rw);
1839 if (scp->flags & CM_SCACHEFLAG_DELETED)
1841 lock_ReleaseWrite(&scp->rw);
1842 if (refCount == 0 && deleted)
1843 cm_RecycleSCache(scp, 0);
1844 if (lockstate != OSI_RWLOCK_WRITEHELD)
1845 lock_ConvertWToR(&cm_scacheLock);
1849 #ifdef DEBUG_REFCOUNT
1850 void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
1852 void cm_ReleaseSCache(cm_scache_t *scp)
1857 osi_assertx(scp != NULL, "null cm_scache_t");
1858 lock_ObtainRead(&cm_scacheLock);
1859 refCount = InterlockedDecrement(&scp->refCount);
1860 #ifdef DEBUG_REFCOUNT
1862 osi_Log1(afsd_logp,"cm_ReleaseSCache about to panic scp 0x%x",scp);
1864 osi_assertx(refCount >= 0, "cm_scache_t refCount 0");
1865 #ifdef DEBUG_REFCOUNT
1866 osi_Log2(afsd_logp,"cm_ReleaseSCache scp 0x%p ref %d",scp, refCount);
1867 afsi_log("%s:%d cm_ReleaseSCache scp 0x%p ref %d", file, line, scp, refCount);
1870 if (scp->flags & CM_SCACHEFLAG_DELETED) {
1872 lock_ObtainWrite(&scp->rw);
1873 if (scp->flags & CM_SCACHEFLAG_DELETED)
1875 lock_ReleaseWrite(&scp->rw);
1877 lock_ConvertRToW(&cm_scacheLock);
1878 cm_RecycleSCache(scp, 0);
1879 lock_ConvertWToR(&cm_scacheLock);
1883 lock_ReleaseRead(&cm_scacheLock);
1886 /* just look for the scp entry to get filetype */
1887 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1888 int cm_FindFileType(cm_fid_t *fidp)
1893 hash = CM_SCACHE_HASH(fidp);
1895 osi_assertx(fidp->cell != 0, "unassigned cell value");
1897 lock_ObtainWrite(&cm_scacheLock);
1898 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
1899 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1900 lock_ReleaseWrite(&cm_scacheLock);
1901 return scp->fileType;
1904 lock_ReleaseWrite(&cm_scacheLock);
1908 /* dump all scp's that have reference count > 0 to a file.
1909 * cookie is used to identify this batch for easy parsing,
1910 * and it a string provided by a caller
1912 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1920 lock_ObtainRead(&cm_scacheLock);
1922 sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1923 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1925 for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
1927 sprintf(output, "%s scp=0x%p, fid (cell=%d, volume=%d, vnode=%d, unique=%d) type=%d dv=%I64d len=0x%I64x mp='%s' flags=0x%x cb=0x%x refCount=%u\r\n",
1928 cookie, scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1929 scp->fileType, scp->dataVersion, scp->length.QuadPart, scp->mountPointStringp, scp->flags,
1930 (unsigned long)scp->cbExpires, scp->refCount);
1931 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1934 sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
1935 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1936 sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n", cookie, cm_data.scacheHashTableSize);
1937 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1939 for (i = 0; i < cm_data.scacheHashTableSize; i++)
1941 for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
1943 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
1944 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
1945 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1949 sprintf(output, "%s - Done dumping cm_data.scacheHashTable\r\n", cookie);
1950 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1953 lock_ReleaseRead(&cm_scacheLock);