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>
25 /*extern void afsi_log(char *pattern, ...);*/
27 extern osi_hyper_t hzero;
30 osi_queue_t *cm_allFileLocks;
31 osi_queue_t *cm_freeFileLocks;
32 unsigned long cm_lockRefreshCycle;
34 /* lock for globals */
35 osi_rwlock_t cm_scacheLock;
37 /* Dummy scache entry for use with pioctl fids */
38 cm_scache_t cm_fakeSCache;
40 #ifdef AFS_FREELANCE_CLIENT
41 extern osi_mutex_t cm_Freelance_Lock;
44 /* must be called with cm_scacheLock write-locked! */
45 void cm_AdjustLRU(cm_scache_t *scp)
47 if (scp == cm_data.scacheLRULastp)
48 cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
49 osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
50 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
51 if (!cm_data.scacheLRULastp)
52 cm_data.scacheLRULastp = scp;
55 /* called with cm_scacheLock write-locked; find a vnode to recycle.
56 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
58 cm_scache_t *cm_GetNewSCache(void)
65 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
66 for (scp = cm_data.scacheLRULastp;
68 scp = (cm_scache_t *) osi_QPrev(&scp->q)) {
69 if (scp->refCount == 0)
74 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
75 /* we found an entry, so return it */
76 if (scp->flags & CM_SCACHEFLAG_INHASH) {
77 /* hash it out first */
78 i = CM_SCACHE_HASH(&scp->fid);
79 for (lscpp = &cm_data.hashTablep[i], tscp = cm_data.hashTablep[i];
81 lscpp = &tscp->nextp, tscp = tscp->nextp) {
84 scp->flags &= ~CM_SCACHEFLAG_INHASH;
88 osi_assertx(tscp, "afsd: scache hash screwup");
91 /* look for things that shouldn't still be set */
92 osi_assert(scp->bufWritesp == NULL);
93 osi_assert(scp->bufReadsp == NULL);
95 /* invalidate so next merge works fine;
96 * also initialize some flags */
97 scp->flags &= ~(CM_SCACHEFLAG_STATD
99 | CM_SCACHEFLAG_PURERO
100 | CM_SCACHEFLAG_OVERQUOTA
101 | CM_SCACHEFLAG_OUTOFSPACE);
102 scp->serverModTime = 0;
103 scp->dataVersion = 0;
104 scp->bulkStatProgress = hzero;
112 /* discard callback */
113 if (scp->cbServerp) {
114 cm_PutServer(scp->cbServerp);
115 scp->cbServerp = NULL;
119 /* remove from dnlc */
123 /* discard cached status; if non-zero, Close
124 * tried to store this to server but failed */
127 /* drop held volume ref */
129 cm_PutVolume(scp->volp);
133 /* discard symlink info */
134 scp->mountPointStringp[0] = 0;
135 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
136 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
138 /* reset locking info */
139 scp->fileLocksH = NULL;
140 scp->fileLocksT = NULL;
141 scp->serverLock = (-1);
142 scp->exclusiveLocks = 0;
143 scp->sharedLocks = 0;
145 /* not locked, but there can be no references to this guy
146 * while we hold the global refcount lock.
148 cm_FreeAllACLEnts(scp);
150 /* now remove from the LRU queue and put it back at the
151 * head of the LRU queue.
160 /* if we get here, we should allocate a new scache entry. We either are below
161 * quota or we have a leak and need to allocate a new one to avoid panicing.
163 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
164 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
165 memset(scp, 0, sizeof(cm_scache_t));
166 scp->magic = CM_SCACHE_MAGIC;
167 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
168 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
169 scp->serverLock = -1;
171 /* and put it in the LRU queue */
172 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
173 if (!cm_data.scacheLRULastp)
174 cm_data.scacheLRULastp = scp;
175 cm_data.currentSCaches++;
176 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
181 /* like strcmp, only for fids */
182 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
184 if (ap->vnode != bp->vnode)
186 if (ap->volume != bp->volume)
188 if (ap->unique != bp->unique)
190 if (ap->cell != bp->cell)
195 void cm_fakeSCacheInit(int newFile)
198 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
199 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
200 /* can leave clientModTime at 0 */
201 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
202 cm_data.fakeSCache.unixModeBits = 0777;
203 cm_data.fakeSCache.length.LowPart = 1000;
204 cm_data.fakeSCache.linkCount = 1;
205 cm_data.fakeSCache.refCount = 1;
207 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
211 cm_ValidateSCache(void)
213 cm_scache_t * scp, *lscp;
216 if ( cm_data.scacheLRUFirstp == NULL && cm_data.scacheLRULastp != NULL ||
217 cm_data.scacheLRUFirstp != NULL && cm_data.scacheLRULastp == NULL) {
218 afsi_log("cm_ValidateSCache failure: inconsistent LRU pointers");
219 fprintf(stderr, "cm_ValidateSCache failure: inconsistent LRU pointers\n");
223 for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
225 lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
226 if (scp->magic != CM_SCACHE_MAGIC) {
227 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
228 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
231 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
232 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
233 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
236 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
237 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
238 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
241 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
242 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
243 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
246 if (i > cm_data.currentSCaches ) {
247 afsi_log("cm_ValidateSCache failure: LRU First queue loops");
248 fprintf(stderr, "cm_ValidateSCache failure: LUR First queue loops\n");
251 if (lscp != (cm_scache_t *) osi_QPrev(&scp->q)) {
252 afsi_log("cm_ValidateSCache failure: QPrev(scp) != previous");
253 fprintf(stderr, "cm_ValidateSCache failure: QPrev(scp) != previous\n");
258 for ( scp = cm_data.scacheLRULastp, lscp = NULL, i = 0; scp;
259 lscp = scp, scp = (cm_scache_t *) osi_QPrev(&scp->q), i++ ) {
260 if (scp->magic != CM_SCACHE_MAGIC) {
261 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
262 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
265 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
266 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
267 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
270 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
271 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
272 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
275 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
276 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
277 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
280 if (i > cm_data.currentSCaches ) {
281 afsi_log("cm_ValidateSCache failure: LRU Last queue loops");
282 fprintf(stderr, "cm_ValidateSCache failure: LUR Last queue loops\n");
285 if (lscp != (cm_scache_t *) osi_QNext(&scp->q)) {
286 afsi_log("cm_ValidateSCache failure: QNext(scp) != next");
287 fprintf(stderr, "cm_ValidateSCache failure: QNext(scp) != next\n");
292 for ( i=0; i < cm_data.hashTableSize; i++ ) {
293 for ( scp = cm_data.hashTablep[i]; scp; scp = scp->nextp ) {
294 if (scp->magic != CM_SCACHE_MAGIC) {
295 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
296 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
299 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
300 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
301 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
304 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
305 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
306 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
309 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
310 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
311 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
317 return cm_dnlcValidate();
321 cm_ShutdownSCache(void)
325 for ( scp = cm_data.scacheLRULastp; scp;
326 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
327 if (scp->randomACLp) {
328 lock_ObtainMutex(&scp->mx);
329 cm_FreeAllACLEnts(scp);
330 lock_ReleaseMutex(&scp->mx);
332 lock_FinalizeMutex(&scp->mx);
333 lock_FinalizeRWLock(&scp->bufCreateLock);
336 return cm_dnlcShutdown();
339 void cm_InitSCache(int newFile, long maxSCaches)
341 static osi_once_t once;
343 if (osi_Once(&once)) {
344 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
346 memset(cm_data.hashTablep, 0, sizeof(cm_scache_t *) * cm_data.hashTableSize);
347 cm_data.currentSCaches = 0;
348 cm_data.maxSCaches = maxSCaches;
349 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
353 for ( scp = cm_data.scacheLRULastp; scp;
354 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
355 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
356 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
358 scp->cbServerp = NULL;
360 scp->fileLocksH = NULL;
361 scp->fileLocksT = NULL;
362 scp->serverLock = (-1);
363 scp->lastRefreshCycle = 0;
364 scp->exclusiveLocks = 0;
365 scp->sharedLocks = 0;
371 scp->flags &= ~CM_SCACHEFLAG_WAITING;
374 cm_allFileLocks = NULL;
375 cm_freeFileLocks = NULL;
376 cm_lockRefreshCycle = 0;
377 cm_fakeSCacheInit(newFile);
378 cm_dnlcInit(newFile);
383 /* version that doesn't bother creating the entry if we don't find it */
384 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
389 hash = CM_SCACHE_HASH(fidp);
391 osi_assert(fidp->cell != 0);
393 lock_ObtainWrite(&cm_scacheLock);
394 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
395 if (cm_FidCmp(fidp, &scp->fid) == 0) {
396 cm_HoldSCacheNoLock(scp);
398 lock_ReleaseWrite(&cm_scacheLock);
402 lock_ReleaseWrite(&cm_scacheLock);
406 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
412 cm_volume_t *volp = 0;
415 int special; // yj: boolean variable to test if file is on root.afs
417 extern cm_fid_t cm_rootFid;
419 hash = CM_SCACHE_HASH(fidp);
421 osi_assert(fidp->cell != 0);
423 if (fidp->cell== cm_data.rootFid.cell &&
424 fidp->volume==cm_data.rootFid.volume &&
425 fidp->vnode==0x0 && fidp->unique==0x0)
427 osi_Log0(afsd_logp,"cm_getSCache called with root cell/volume and vnode=0 and unique=0");
430 // yj: check if we have the scp, if so, we don't need
431 // to do anything else
432 lock_ObtainWrite(&cm_scacheLock);
433 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
434 if (cm_FidCmp(fidp, &scp->fid) == 0) {
435 cm_HoldSCacheNoLock(scp);
438 lock_ReleaseWrite(&cm_scacheLock);
443 // yj: when we get here, it means we don't have an scp
444 // so we need to either load it or fake it, depending
445 // on whether the file is "special", see below.
447 // yj: if we're trying to get an scp for a file that's
448 // on root.afs of homecell, we want to handle it specially
449 // because we have to fill in the status stuff 'coz we
450 // don't want trybulkstat to fill it in for us
451 #ifdef AFS_FREELANCE_CLIENT
452 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
453 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
454 !(fidp->vnode==0x1 && fidp->unique==0x1));
455 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
456 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
457 fidp->vnode==0x1 && fidp->unique==0x1);
458 if (cm_freelanceEnabled && isRoot) {
459 osi_Log0(afsd_logp,"cm_getSCache Freelance and isRoot");
460 /* freelance: if we are trying to get the root scp for the first
461 * time, we will just put in a place holder entry.
466 if (cm_freelanceEnabled && special) {
467 osi_Log0(afsd_logp,"cm_getSCache Freelance and special");
468 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2) {
469 lock_ObtainMutex(&cm_Freelance_Lock);
470 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
471 lock_ReleaseMutex(&cm_Freelance_Lock);
475 scp = cm_GetNewSCache();
477 lock_ObtainMutex(&scp->mx);
479 scp->volp = cm_data.rootSCachep->volp;
480 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
481 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
482 scp->dotdotFid.unique=1;
483 scp->dotdotFid.vnode=1;
484 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
485 scp->nextp=cm_data.hashTablep[hash];
486 cm_data.hashTablep[hash]=scp;
487 scp->flags |= CM_SCACHEFLAG_INHASH;
489 if (fidp->vnode > 1 && fidp->vnode <= cm_noLocalMountPoints + 2)
490 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
492 scp->fileType = CM_SCACHETYPE_INVALID;
494 lock_ObtainMutex(&cm_Freelance_Lock);
495 scp->length.LowPart = (DWORD)strlen(mp)+4;
496 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
497 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
498 lock_ReleaseMutex(&cm_Freelance_Lock);
501 scp->unixModeBits=0x1ff;
502 scp->clientModTime=FakeFreelanceModTime;
503 scp->serverModTime=FakeFreelanceModTime;
504 scp->parentUnique = 0x1;
505 scp->parentVnode=0x1;
507 scp->dataVersion=cm_data.fakeDirVersion;
508 scp->lockDataVersion=-1; /* no lock yet */
509 lock_ReleaseMutex(&scp->mx);
511 lock_ReleaseWrite(&cm_scacheLock);
515 #endif /* AFS_FREELANCE_CLIENT */
517 /* otherwise, we need to find the volume */
518 if (!cm_freelanceEnabled || !isRoot) {
519 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
520 cellp = cm_FindCellByID(fidp->cell);
522 return CM_ERROR_NOSUCHCELL;
524 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
527 lock_ObtainWrite(&cm_scacheLock);
530 /* otherwise, we have the volume, now reverify that the scp doesn't
531 * exist, and proceed.
533 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
534 if (cm_FidCmp(fidp, &scp->fid) == 0) {
535 cm_HoldSCacheNoLock(scp);
536 osi_assert(scp->volp == volp);
538 lock_ReleaseWrite(&cm_scacheLock);
546 /* now, if we don't have the fid, recycle something */
547 scp = cm_GetNewSCache();
548 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
549 lock_ObtainMutex(&scp->mx);
551 scp->volp = volp; /* a held reference */
553 if (!cm_freelanceEnabled || !isRoot) {
554 /* if this scache entry represents a volume root then we need
555 * to copy the dotdotFipd from the volume structure where the
556 * "master" copy is stored (defect 11489)
558 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
559 scp->dotdotFid = volp->dotdotFid;
562 if (volp->roID == fidp->volume)
563 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
564 else if (volp->bkID == fidp->volume)
565 scp->flags |= CM_SCACHEFLAG_RO;
567 scp->nextp = cm_data.hashTablep[hash];
568 cm_data.hashTablep[hash] = scp;
569 scp->flags |= CM_SCACHEFLAG_INHASH;
571 lock_ReleaseMutex(&scp->mx);
573 /* XXX - The following fields in the cm_scache are
579 lock_ReleaseWrite(&cm_scacheLock);
581 /* now we have a held scache entry; just return it */
586 /* Returns a held reference to the scache's parent
588 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
593 cm_scache_t * pscp = NULL;
595 lock_ObtainWrite(&cm_scacheLock);
596 parent_fid = scp->fid;
597 parent_fid.vnode = scp->parentVnode;
598 parent_fid.unique = scp->parentUnique;
600 if (cm_FidCmp(&scp->fid, &parent_fid)) {
601 for (i=0; i<cm_data.hashTableSize; i++) {
602 for (pscp = cm_data.hashTablep[i]; pscp; pscp = pscp->nextp) {
603 if (!cm_FidCmp(&pscp->fid, &parent_fid)) {
604 cm_HoldSCacheNoLock(pscp);
610 lock_ReleaseWrite(&cm_scacheLock);
615 /* synchronize a fetch, store, read, write, fetch status or store status.
616 * Called with scache mutex held, and returns with it held, but temporarily
617 * drops it during the fetch.
619 * At most one flag can be on in flags, if this is an RPC request.
621 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
623 * There are a lot of weird restrictions here; here's an attempt to explain the
624 * rationale for the concurrency restrictions implemented in this function.
626 * First, although the file server will break callbacks when *another* machine
627 * modifies a file or status block, the client itself is responsible for
628 * concurrency control on its own requests. Callback breaking events are rare,
629 * and simply invalidate any concurrent new status info.
631 * In the absence of callback breaking messages, we need to know how to
632 * synchronize incoming responses describing updates to files. We synchronize
633 * operations that update the data version by comparing the data versions.
634 * However, updates that do not update the data, but only the status, can't be
635 * synchronized with fetches or stores, since there's nothing to compare
636 * to tell which operation executed first at the server.
638 * Thus, we can allow multiple ops that change file data, or dir data, and
639 * fetches. However, status storing ops have to be done serially.
641 * Furthermore, certain data-changing ops are incompatible: we can't read or
642 * write a buffer while doing a truncate. We can't read and write the same
643 * buffer at the same time, or write while fetching or storing, or read while
644 * fetching a buffer (this may change). We can't fetch and store at the same
647 * With respect to status, we can't read and write at the same time, read while
648 * fetching, write while fetching or storing, or fetch and store at the same time.
650 * We can't allow a get callback RPC to run in concurrently with something that
651 * will return updated status, since we could start a call, have the server
652 * return status, have another machine make an update to the status (which
653 * doesn't change serverModTime), have the original machine get a new callback,
654 * and then have the original machine merge in the early, old info from the
655 * first call. At this point, the easiest way to avoid this problem is to have
656 * getcallback calls conflict with all others for the same vnode. Other calls
657 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
658 * vnode must be careful not to merge in their status unless they have obtained
659 * a callback from the start of their call.
662 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
663 * Each such RPC passes a FileLength parameter, which the server uses to do
664 * pre-truncation if necessary. So if two RPC's are processed out of order at
665 * the server, the one with the smaller FileLength will be processed last,
666 * possibly resulting in a bogus truncation. The simplest way to avoid this
667 * is to serialize all StoreData RPC's. This is the reason we defined
668 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
670 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *reqp,
671 afs_uint32 rights, afs_uint32 flags)
673 osi_queueData_t *qdp;
676 afs_uint32 outRights;
679 /* lookup this first */
680 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
682 /* some minor assertions */
683 if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
684 | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
685 | CM_SCACHESYNC_SETSIZE)) {
687 osi_assert(bufp->refCount > 0);
689 osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
693 else osi_assert(bufp == NULL);
695 /* Do the access check. Now we don't really do the access check
696 * atomically, since the caller doesn't expect the parent dir to be
697 * returned locked, and that is what we'd have to do to prevent a
698 * callback breaking message on the parent due to a setacl call from
699 * being processed while we're running. So, instead, we check things
700 * here, and if things look fine with the access, we proceed to finish
701 * the rest of this check. Sort of a hack, but probably good enough.
705 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
706 /* if we're bringing in a new status block, ensure that
707 * we aren't already doing so, and that no one is
708 * changing the status concurrently, either. We need
709 * to do this, even if the status is of a different
710 * type, since we don't have the ability to figure out,
711 * in the AFS 3 protocols, which status-changing
712 * operation ran first, or even which order a read and
713 * a write occurred in.
715 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
716 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
717 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
721 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
722 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
723 /* if we're going to make an RPC to change the status, make sure
724 * that no one is bringing in or sending out the status.
726 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
727 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
728 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
731 if (scp->bufReadsp || scp->bufWritesp) {
732 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
736 if (flags & CM_SCACHESYNC_FETCHDATA) {
737 /* if we're bringing in a new chunk of data, make sure that
738 * nothing is happening to that chunk, and that we aren't
739 * changing the basic file status info, either.
741 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
742 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
743 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
746 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
747 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp, bufp);
751 if (flags & CM_SCACHESYNC_STOREDATA) {
752 /* same as fetch data */
753 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
754 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
755 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
758 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
759 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp, bufp);
764 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
765 /* Don't allow concurrent StoreData RPC's */
766 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
767 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is DATASTORING want STOREDATA_EXCL", scp);
772 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
773 /* Don't allow more than one BKG store request */
774 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
775 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is ASYNCSTORING want ASYNCSTORE", scp);
780 if (flags & CM_SCACHESYNC_LOCK) {
781 /* Don't allow concurrent fiddling with lock lists */
782 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
783 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is LOCKING want LOCK", scp);
788 /* now the operations that don't correspond to making RPCs */
789 if (flags & CM_SCACHESYNC_GETSTATUS) {
790 /* we can use the status that's here, if we're not
791 * bringing in new status.
793 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
794 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want GETSTATUS", scp);
798 if (flags & CM_SCACHESYNC_SETSTATUS) {
799 /* we can make a change to the local status, as long as
800 * the status isn't changing now.
802 * If we're fetching or storing a chunk of data, we can
803 * change the status locally, since the fetch/store
804 * operations don't change any of the data that we're
807 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING)) {
808 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
812 if (flags & CM_SCACHESYNC_READ) {
813 /* we're going to read the data, make sure that the
814 * status is available, and that the data is here. It
815 * is OK to read while storing the data back.
817 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
818 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING want READ", scp);
821 if (bufp && ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == CM_BUF_CMFETCHING)) {
822 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING want READ", scp, bufp);
826 if (flags & CM_SCACHESYNC_WRITE) {
827 /* don't write unless the status is stable and the chunk
830 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
831 | CM_SCACHEFLAG_SIZESTORING)) {
832 osi_Log1(afsd_logp, "CM SyncOp scp 0x%p is FETCHING|STORING|SIZESTORING want WRITE", scp);
835 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
836 osi_Log2(afsd_logp, "CM SyncOp scp 0x%p bufp 0x%p is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp, bufp);
841 // yj: modified this so that callback only checked if we're
842 // not checking something on /afs
843 /* fix the conditional to match the one in cm_HaveCallback */
844 if ( (flags & CM_SCACHESYNC_NEEDCALLBACK)
845 #ifdef AFS_FREELANCE_CLIENT
846 && (!cm_freelanceEnabled ||
847 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
848 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
849 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
850 cm_fakeDirCallback < 2)
851 #endif /* AFS_FREELANCE_CLIENT */
853 if (!cm_HaveCallback(scp)) {
854 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
856 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
857 code = cm_GetCallback(scp, userp, reqp, 0);
859 lock_ReleaseMutex(&scp->mx);
860 lock_ObtainMutex(&bufp->mx);
861 lock_ObtainMutex(&scp->mx);
870 /* can't check access rights without a callback */
871 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
873 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
874 return CM_ERROR_READONLY;
876 if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
877 if (~outRights & rights)
878 return CM_ERROR_NOACCESS;
881 /* we don't know the required access rights */
882 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
883 code = cm_GetAccessRights(scp, userp, reqp);
887 lock_ReleaseMutex(&scp->mx);
888 lock_ObtainMutex(&bufp->mx);
889 lock_ObtainMutex(&scp->mx);
895 /* if we get here, we're happy */
899 /* first check if we're not supposed to wait: fail
900 * in this case, returning with everything still locked.
902 if (flags & CM_SCACHESYNC_NOWAIT)
903 return CM_ERROR_WOULDBLOCK;
905 /* wait here, then try again */
906 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%p", scp);
907 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
910 osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
911 scp, scp->waitCount, scp->waitRequests);
913 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
914 scp->flags |= CM_SCACHEFLAG_WAITING;
915 scp->waitCount = scp->waitRequests = 1;
918 lock_ReleaseMutex(&bufp->mx);
919 osi_SleepM((LONG_PTR) &scp->flags, &scp->mx);
921 smb_UpdateServerPriority();
924 lock_ObtainMutex(&bufp->mx);
925 lock_ObtainMutex(&scp->mx);
927 osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
928 scp, scp->waitCount, scp->waitRequests);
929 if (scp->waitCount == 0) {
930 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
931 scp->flags &= ~CM_SCACHEFLAG_WAITING;
932 scp->waitRequests = 0;
934 } /* big while loop */
936 /* now, update the recorded state for RPC-type calls */
937 if (flags & CM_SCACHESYNC_FETCHSTATUS)
938 scp->flags |= CM_SCACHEFLAG_FETCHING;
939 if (flags & CM_SCACHESYNC_STORESTATUS)
940 scp->flags |= CM_SCACHEFLAG_STORING;
941 if (flags & CM_SCACHESYNC_STORESIZE)
942 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
943 if (flags & CM_SCACHESYNC_GETCALLBACK)
944 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
945 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
946 scp->flags |= CM_SCACHEFLAG_DATASTORING;
947 if (flags & CM_SCACHESYNC_ASYNCSTORE)
948 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
949 if (flags & CM_SCACHESYNC_LOCK)
950 scp->flags |= CM_SCACHEFLAG_LOCKING;
952 /* now update the buffer pointer */
953 if (flags & CM_SCACHESYNC_FETCHDATA) {
954 /* ensure that the buffer isn't already in the I/O list */
956 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
957 tbufp = osi_GetQData(qdp);
958 osi_assert(tbufp != bufp);
962 /* queue a held reference to the buffer in the "reading" I/O list */
964 osi_SetQData(qdp, bufp);
967 bufp->cmFlags |= CM_BUF_CMFETCHING;
969 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
972 if (flags & CM_SCACHESYNC_STOREDATA) {
973 /* ensure that the buffer isn't already in the I/O list */
975 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
976 tbufp = osi_GetQData(qdp);
977 osi_assert(tbufp != bufp);
981 /* queue a held reference to the buffer in the "writing" I/O list */
983 osi_SetQData(qdp, bufp);
986 bufp->cmFlags |= CM_BUF_CMSTORING;
988 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
994 /* for those syncops that setup for RPCs.
995 * Called with scache locked.
997 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
999 osi_queueData_t *qdp;
1002 /* now, update the recorded state for RPC-type calls */
1003 if (flags & CM_SCACHESYNC_FETCHSTATUS)
1004 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
1005 if (flags & CM_SCACHESYNC_STORESTATUS)
1006 scp->flags &= ~CM_SCACHEFLAG_STORING;
1007 if (flags & CM_SCACHESYNC_STORESIZE)
1008 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
1009 if (flags & CM_SCACHESYNC_GETCALLBACK)
1010 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
1011 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
1012 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
1013 if (flags & CM_SCACHESYNC_ASYNCSTORE)
1014 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
1015 if (flags & CM_SCACHESYNC_LOCK)
1016 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
1018 /* now update the buffer pointer */
1019 if (flags & CM_SCACHESYNC_FETCHDATA) {
1020 /* ensure that the buffer isn't already in the I/O list */
1021 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1022 tbufp = osi_GetQData(qdp);
1023 if (tbufp == bufp) break;
1025 osi_assert(qdp != NULL);
1026 osi_assert(osi_GetQData(qdp) == bufp);
1027 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
1030 bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
1031 if (bufp->flags & CM_BUF_WAITING) {
1032 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1033 osi_Wakeup((LONG_PTR) &bufp);
1039 /* now update the buffer pointer */
1040 if (flags & CM_SCACHESYNC_STOREDATA) {
1041 /* ensure that the buffer isn't already in the I/O list */
1042 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1043 tbufp = osi_GetQData(qdp);
1044 if (tbufp == bufp) break;
1046 osi_assert(qdp != NULL);
1047 osi_assert(osi_GetQData(qdp) == bufp);
1048 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
1051 bufp->cmFlags &= ~CM_BUF_CMSTORING;
1052 if (bufp->flags & CM_BUF_WAITING) {
1053 osi_Log2(afsd_logp, "CM SyncOpDone Waking [scp 0x%p] bufp 0x%p", scp, bufp);
1054 osi_Wakeup((LONG_PTR) &bufp);
1060 /* and wakeup anyone who is waiting */
1061 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1062 osi_Log1(afsd_logp, "CM SyncOpDone Waking scp 0x%p", scp);
1063 osi_Wakeup((LONG_PTR) &scp->flags);
1067 /* merge in a response from an RPC. The scp must be locked, and the callback
1070 * Don't overwrite any status info that is dirty, since we could have a store
1071 * operation (such as store data) that merges some info in, and we don't want
1072 * to lose the local updates. Typically, there aren't many updates we do
1073 * locally, anyway, probably only mtime.
1075 * There is probably a bug in here where a chmod (which doesn't change
1076 * serverModTime) that occurs between two fetches, both of whose responses are
1077 * handled after the callback breaking is done, but only one of whose calls
1078 * started before that, can cause old info to be merged from the first call.
1080 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
1081 cm_user_t *userp, afs_uint32 flags)
1083 // yj: i want to create some fake status for the /afs directory and the
1084 // entries under that directory
1085 #ifdef AFS_FREELANCE_CLIENT
1086 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
1087 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
1088 statusp->InterfaceVersion = 0x1;
1089 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
1090 statusp->LinkCount = scp->linkCount;
1091 statusp->Length = cm_fakeDirSize;
1092 statusp->DataVersion = cm_data.fakeDirVersion;
1093 statusp->Author = 0x1;
1094 statusp->Owner = 0x0;
1095 statusp->CallerAccess = 0x9;
1096 statusp->AnonymousAccess = 0x9;
1097 statusp->UnixModeBits = 0x1ff;
1098 statusp->ParentVnode = 0x1;
1099 statusp->ParentUnique = 0x1;
1100 statusp->ResidencyMask = 0;
1101 statusp->ClientModTime = FakeFreelanceModTime;
1102 statusp->ServerModTime = FakeFreelanceModTime;
1104 statusp->SyncCounter = 0;
1105 statusp->dataVersionHigh = 0;
1107 #endif /* AFS_FREELANCE_CLIENT */
1109 if (!(flags & CM_MERGEFLAG_FORCE)
1110 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1111 struct cm_cell *cellp;
1113 cellp = cm_FindCellByID(scp->fid.cell);
1114 if (scp->cbServerp) {
1115 struct cm_volume *volp = NULL;
1117 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1118 (cm_req_t *) NULL, &volp);
1119 osi_Log2(afsd_logp, "old data from server %x volume %s",
1120 scp->cbServerp->addr.sin_addr.s_addr,
1121 volp ? volp->namep : "(unknown)");
1125 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1126 scp, scp->dataVersion, statusp->DataVersion);
1127 /* we have a number of data fetch/store operations running
1128 * concurrently, and we can tell which one executed last at the
1129 * server by its mtime.
1130 * Choose the one with the largest mtime, and ignore the rest.
1132 * These concurrent calls are incompatible with setting the
1133 * mtime, so we won't have a locally changed mtime here.
1135 * We could also have ACL info for a different user than usual,
1136 * in which case we have to do that part of the merge, anyway.
1137 * We won't have to worry about the info being old, since we
1138 * won't have concurrent calls
1139 * that change file status running from this machine.
1141 * Added 3/17/98: if we see data version regression on an RO
1142 * file, it's probably due to a server holding an out-of-date
1143 * replica, rather than to concurrent RPC's. Failures to
1144 * release replicas are now flagged by the volserver, but only
1145 * since AFS 3.4 5.22, so there are plenty of clients getting
1146 * out-of-date replicas out there.
1148 * If we discover an out-of-date replica, by this time it's too
1149 * late to go to another server and retry. Also, we can't
1150 * reject the merge, because then there is no way for
1151 * GetAccess to do its work, and the caller gets into an
1152 * infinite loop. So we just grin and bear it.
1154 if (!(scp->flags & CM_SCACHEFLAG_RO))
1157 scp->serverModTime = statusp->ServerModTime;
1159 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1160 scp->clientModTime = statusp->ClientModTime;
1162 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1163 scp->length.LowPart = statusp->Length;
1164 scp->length.HighPart = 0;
1167 scp->serverLength.LowPart = statusp->Length;
1168 scp->serverLength.HighPart = 0;
1170 scp->linkCount = statusp->LinkCount;
1171 scp->dataVersion = statusp->DataVersion;
1172 scp->owner = statusp->Owner;
1173 scp->group = statusp->Group;
1174 scp->unixModeBits = statusp->UnixModeBits & 07777;
1176 if (statusp->FileType == File)
1177 scp->fileType = CM_SCACHETYPE_FILE;
1178 else if (statusp->FileType == Directory)
1179 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1180 else if (statusp->FileType == SymbolicLink) {
1181 if ((scp->unixModeBits & 0111) == 0)
1182 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1184 scp->fileType = CM_SCACHETYPE_SYMLINK;
1187 osi_Log1(afsd_logp, "Merge, Invalid File Type, scp %x", scp);
1188 scp->fileType = 0; /* invalid */
1190 /* and other stuff */
1191 scp->parentVnode = statusp->ParentVnode;
1192 scp->parentUnique = statusp->ParentUnique;
1194 /* and merge in the private acl cache info, if this is more than the public
1195 * info; merge in the public stuff in any case.
1197 scp->anyAccess = statusp->AnonymousAccess;
1199 if (userp != NULL) {
1200 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1204 /* note that our stat cache info is incorrect, so force us eventually
1205 * to stat the file again. There may be dirty data associated with
1206 * this vnode, and we want to preserve that information.
1208 * This function works by simply simulating a loss of the callback.
1210 * This function must be called with the scache locked.
1212 void cm_DiscardSCache(cm_scache_t *scp)
1214 lock_AssertMutex(&scp->mx);
1215 if (scp->cbServerp) {
1216 cm_PutServer(scp->cbServerp);
1217 scp->cbServerp = NULL;
1220 scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
1221 cm_dnlcPurgedp(scp);
1222 cm_dnlcPurgevp(scp);
1223 cm_FreeAllACLEnts(scp);
1225 /* Force mount points and symlinks to be re-evaluated */
1226 scp->mountPointStringp[0] = '\0';
1229 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1231 afsFidp->Volume = fidp->volume;
1232 afsFidp->Vnode = fidp->vnode;
1233 afsFidp->Unique = fidp->unique;
1236 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1238 osi_assert(scp != 0);
1239 osi_assert(scp->refCount >= 0);
1243 void cm_HoldSCache(cm_scache_t *scp)
1245 osi_assert(scp != 0);
1246 lock_ObtainWrite(&cm_scacheLock);
1247 osi_assert(scp->refCount >= 0);
1249 lock_ReleaseWrite(&cm_scacheLock);
1252 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1254 osi_assert(scp != 0);
1255 osi_assert(scp->refCount-- >= 0);
1258 void cm_ReleaseSCache(cm_scache_t *scp)
1260 osi_assert(scp != 0);
1261 lock_ObtainWrite(&cm_scacheLock);
1262 osi_assert(scp->refCount != 0);
1264 lock_ReleaseWrite(&cm_scacheLock);
1267 /* just look for the scp entry to get filetype */
1268 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1269 int cm_FindFileType(cm_fid_t *fidp)
1274 hash = CM_SCACHE_HASH(fidp);
1276 osi_assert(fidp->cell != 0);
1278 lock_ObtainWrite(&cm_scacheLock);
1279 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
1280 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1281 lock_ReleaseWrite(&cm_scacheLock);
1282 return scp->fileType;
1285 lock_ReleaseWrite(&cm_scacheLock);
1289 /* dump all scp's that have reference count > 0 to a file.
1290 * cookie is used to identify this batch for easy parsing,
1291 * and it a string provided by a caller
1293 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1301 lock_ObtainRead(&cm_scacheLock);
1303 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1304 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1306 for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q))
1308 if (scp->refCount != 0)
1310 sprintf(output, "%s fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1311 cookie, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1313 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1317 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.hashTableSize=%d\n", cookie, cm_data.hashTableSize);
1318 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1320 for (i = 0; i < cm_data.hashTableSize; i++)
1322 for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp)
1324 if (scp->refCount != 0)
1326 sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1327 cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1328 scp->fid.unique, scp->refCount);
1329 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1334 sprintf(output, "%s - Done dumping scache.\n", cookie);
1335 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1338 lock_ReleaseRead(&cm_scacheLock);