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;
32 /* lock for globals */
33 osi_rwlock_t cm_scacheLock;
35 /* Dummy scache entry for use with pioctl fids */
36 cm_scache_t cm_fakeSCache;
38 #ifdef AFS_FREELANCE_CLIENT
39 extern osi_mutex_t cm_Freelance_Lock;
42 /* must be called with cm_scacheLock write-locked! */
43 void cm_AdjustLRU(cm_scache_t *scp)
45 if (scp == cm_data.scacheLRULastp)
46 cm_data.scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
47 osi_QRemove((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
48 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
49 if (!cm_data.scacheLRULastp)
50 cm_data.scacheLRULastp = scp;
53 /* called with cm_scacheLock write-locked; find a vnode to recycle.
54 * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
56 cm_scache_t *cm_GetNewSCache(void)
63 if (cm_data.currentSCaches >= cm_data.maxSCaches) {
64 for (scp = cm_data.scacheLRULastp;
66 scp = (cm_scache_t *) osi_QPrev(&scp->q)) {
67 if (scp->refCount == 0)
72 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
73 /* we found an entry, so return it */
74 if (scp->flags & CM_SCACHEFLAG_INHASH) {
75 /* hash it out first */
76 i = CM_SCACHE_HASH(&scp->fid);
77 for (lscpp = &cm_data.hashTablep[i], tscp = cm_data.hashTablep[i];
79 lscpp = &tscp->nextp, tscp = tscp->nextp) {
82 scp->flags &= ~CM_SCACHEFLAG_INHASH;
86 osi_assertx(tscp, "afsd: scache hash screwup");
89 /* look for things that shouldn't still be set */
90 osi_assert(scp->bufWritesp == NULL);
91 osi_assert(scp->bufReadsp == NULL);
93 /* invalidate so next merge works fine;
94 * also initialize some flags */
95 scp->flags &= ~(CM_SCACHEFLAG_STATD
97 | CM_SCACHEFLAG_PURERO
98 | CM_SCACHEFLAG_OVERQUOTA
99 | CM_SCACHEFLAG_OUTOFSPACE);
100 scp->serverModTime = 0;
101 scp->dataVersion = 0;
102 scp->bulkStatProgress = hzero;
104 /* discard callback */
105 if (scp->cbServerp) {
106 cm_PutServer(scp->cbServerp);
107 scp->cbServerp = NULL;
111 /* remove from dnlc */
115 /* discard cached status; if non-zero, Close
116 * tried to store this to server but failed */
119 /* drop held volume ref */
121 cm_PutVolume(scp->volp);
125 /* discard symlink info */
126 scp->mountPointStringp[0] = 0;
127 memset(&scp->mountRootFid, 0, sizeof(cm_fid_t));
128 memset(&scp->dotdotFid, 0, sizeof(cm_fid_t));
130 /* not locked, but there can be no references to this guy
131 * while we hold the global refcount lock.
133 cm_FreeAllACLEnts(scp);
135 /* now remove from the LRU queue and put it back at the
136 * head of the LRU queue.
145 /* if we get here, we should allocate a new scache entry. We either are below
146 * quota or we have a leak and need to allocate a new one to avoid panicing.
148 scp = cm_data.scacheBaseAddress + cm_data.currentSCaches;
149 osi_assert(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.hashTablep);
150 memset(scp, 0, sizeof(cm_scache_t));
151 scp->magic = CM_SCACHE_MAGIC;
152 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
153 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
155 /* and put it in the LRU queue */
156 osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
157 if (!cm_data.scacheLRULastp)
158 cm_data.scacheLRULastp = scp;
159 cm_data.currentSCaches++;
160 cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
165 /* like strcmp, only for fids */
166 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
168 if (ap->vnode != bp->vnode)
170 if (ap->volume != bp->volume)
172 if (ap->unique != bp->unique)
174 if (ap->cell != bp->cell)
179 void cm_fakeSCacheInit(int newFile)
182 memset(&cm_data.fakeSCache, 0, sizeof(cm_scache_t));
183 cm_data.fakeSCache.cbServerp = (struct cm_server *)(-1);
184 /* can leave clientModTime at 0 */
185 cm_data.fakeSCache.fileType = CM_SCACHETYPE_FILE;
186 cm_data.fakeSCache.unixModeBits = 0777;
187 cm_data.fakeSCache.length.LowPart = 1000;
188 cm_data.fakeSCache.linkCount = 1;
189 cm_data.fakeSCache.refCount = 1;
191 lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
195 cm_ValidateSCache(void)
200 for ( scp = cm_data.scacheLRUFirstp; scp;
201 scp = (cm_scache_t *) osi_QNext(&scp->q) ) {
202 if (scp->magic != CM_SCACHE_MAGIC) {
203 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
204 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
207 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
208 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
209 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
212 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
213 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
214 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
217 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
218 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
219 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
224 for ( scp = cm_data.scacheLRULastp; scp;
225 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
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");
248 for ( i=0; i < cm_data.hashTableSize; i++ ) {
249 for ( scp = cm_data.hashTablep[i]; scp; scp = scp->nextp ) {
250 if (scp->magic != CM_SCACHE_MAGIC) {
251 afsi_log("cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC");
252 fprintf(stderr, "cm_ValidateSCache failure: scp->magic != CM_SCACHE_MAGIC\n");
255 if (scp->nextp && scp->nextp->magic != CM_SCACHE_MAGIC) {
256 afsi_log("cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC");
257 fprintf(stderr, "cm_ValidateSCache failure: scp->nextp->magic != CM_SCACHE_MAGIC\n");
260 if (scp->randomACLp && scp->randomACLp->magic != CM_ACLENT_MAGIC) {
261 afsi_log("cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC");
262 fprintf(stderr, "cm_ValidateSCache failure: scp->randomACLp->magic != CM_ACLENT_MAGIC\n");
265 if (scp->volp && scp->volp->magic != CM_VOLUME_MAGIC) {
266 afsi_log("cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC");
267 fprintf(stderr, "cm_ValidateSCache failure: scp->volp->magic != CM_VOLUME_MAGIC\n");
273 return cm_dnlcValidate();
277 cm_ShutdownSCache(void)
281 for ( scp = cm_data.scacheLRULastp; scp;
282 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
283 if (scp->randomACLp) {
284 lock_ObtainWrite(&scp->mx);
285 cm_FreeAllACLEnts(scp);
286 lock_ReleaseWrite(&scp->mx);
288 lock_FinalizeMutex(&scp->mx);
289 lock_FinalizeRWLock(&scp->bufCreateLock);
292 return cm_dnlcShutdown();
295 void cm_InitSCache(int newFile, long maxSCaches)
297 static osi_once_t once;
299 if (osi_Once(&once)) {
300 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
302 memset(cm_data.hashTablep, 0, sizeof(cm_scache_t *) * cm_data.hashTableSize);
303 cm_data.currentSCaches = 0;
304 cm_data.maxSCaches = maxSCaches;
305 cm_data.scacheLRUFirstp = cm_data.scacheLRULastp = NULL;
309 for ( scp = cm_data.scacheLRULastp; scp;
310 scp = (cm_scache_t *) osi_QPrev(&scp->q) ) {
311 lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
312 lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
314 scp->cbServerp = NULL;
316 scp->fileLocks = NULL;
323 cm_allFileLocks = NULL;
324 cm_fakeSCacheInit(newFile);
325 cm_dnlcInit(newFile);
330 /* version that doesn't bother creating the entry if we don't find it */
331 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
336 hash = CM_SCACHE_HASH(fidp);
338 osi_assert(fidp->cell != 0);
340 lock_ObtainWrite(&cm_scacheLock);
341 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
342 if (cm_FidCmp(fidp, &scp->fid) == 0) {
343 cm_HoldSCacheNoLock(scp);
345 lock_ReleaseWrite(&cm_scacheLock);
349 lock_ReleaseWrite(&cm_scacheLock);
353 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
359 cm_volume_t *volp = 0;
362 int special; // yj: boolean variable to test if file is on root.afs
364 extern cm_fid_t cm_rootFid;
366 hash = CM_SCACHE_HASH(fidp);
368 osi_assert(fidp->cell != 0);
370 if (fidp->cell== cm_data.rootFid.cell &&
371 fidp->volume==cm_data.rootFid.volume &&
372 fidp->vnode==0x0 && fidp->unique==0x0)
374 osi_Log0(afsd_logp,"cm_getSCache called with root cell/volume and vnode=0 and unique=0");
377 // yj: check if we have the scp, if so, we don't need
378 // to do anything else
379 lock_ObtainWrite(&cm_scacheLock);
380 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
381 if (cm_FidCmp(fidp, &scp->fid) == 0) {
382 cm_HoldSCacheNoLock(scp);
385 lock_ReleaseWrite(&cm_scacheLock);
390 // yj: when we get here, it means we don't have an scp
391 // so we need to either load it or fake it, depending
392 // on whether the file is "special", see below.
394 // yj: if we're trying to get an scp for a file that's
395 // on root.afs of homecell, we want to handle it specially
396 // because we have to fill in the status stuff 'coz we
397 // don't want trybulkstat to fill it in for us
398 #ifdef AFS_FREELANCE_CLIENT
399 special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
400 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
401 !(fidp->vnode==0x1 && fidp->unique==0x1));
402 isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
403 fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
404 fidp->vnode==0x1 && fidp->unique==0x1);
405 if (cm_freelanceEnabled && isRoot) {
406 osi_Log0(afsd_logp,"cm_getSCache Freelance and isRoot");
407 /* freelance: if we are trying to get the root scp for the first
408 * time, we will just put in a place holder entry.
413 if (cm_freelanceEnabled && special) {
414 osi_Log0(afsd_logp,"cm_getSCache Freelance and special");
415 if (fidp->vnode > 1 && fidp->vnode <= cm_localMountPoints + 2) {
416 lock_ObtainMutex(&cm_Freelance_Lock);
417 mp =(cm_localMountPoints+fidp->vnode-2)->mountPointStringp;
418 lock_ReleaseMutex(&cm_Freelance_Lock);
422 scp = cm_GetNewSCache();
425 scp->volp = cm_data.rootSCachep->volp;
426 scp->dotdotFid.cell=AFS_FAKE_ROOT_CELL_ID;
427 scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
428 scp->dotdotFid.unique=1;
429 scp->dotdotFid.vnode=1;
430 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
431 scp->nextp=cm_data.hashTablep[hash];
432 cm_data.hashTablep[hash]=scp;
433 scp->flags |= CM_SCACHEFLAG_INHASH;
435 if (fidp->vnode > 1 && fidp->vnode <= cm_localMountPoints + 2)
436 scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
438 scp->fileType = CM_SCACHETYPE_INVALID;
440 lock_ObtainMutex(&cm_Freelance_Lock);
441 scp->length.LowPart = strlen(mp)+4;
442 strncpy(scp->mountPointStringp,mp,MOUNTPOINTLEN);
443 scp->mountPointStringp[MOUNTPOINTLEN-1] = '\0';
444 lock_ReleaseMutex(&cm_Freelance_Lock);
447 scp->unixModeBits=0x1ff;
448 scp->clientModTime=FakeFreelanceModTime;
449 scp->serverModTime=FakeFreelanceModTime;
450 scp->parentUnique = 0x1;
451 scp->parentVnode=0x1;
453 scp->dataVersion=cm_data.fakeDirVersion;
455 lock_ReleaseWrite(&cm_scacheLock);
456 /*afsi_log(" getscache done");*/
460 #endif /* AFS_FREELANCE_CLIENT */
462 /* otherwise, we need to find the volume */
463 if (!cm_freelanceEnabled || !isRoot) {
464 lock_ReleaseWrite(&cm_scacheLock); /* for perf. reasons */
465 cellp = cm_FindCellByID(fidp->cell);
467 return CM_ERROR_NOSUCHCELL;
469 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
472 lock_ObtainWrite(&cm_scacheLock);
475 /* otherwise, we have the volume, now reverify that the scp doesn't
476 * exist, and proceed.
478 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
479 if (cm_FidCmp(fidp, &scp->fid) == 0) {
480 cm_HoldSCacheNoLock(scp);
481 osi_assert(scp->volp == volp);
483 lock_ReleaseWrite(&cm_scacheLock);
491 /* now, if we don't have the fid, recycle something */
492 scp = cm_GetNewSCache();
493 osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
495 scp->volp = volp; /* a held reference */
497 if (!cm_freelanceEnabled || !isRoot) {
498 /* if this scache entry represents a volume root then we need
499 * to copy the dotdotFipd from the volume structure where the
500 * "master" copy is stored (defect 11489)
502 if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
503 scp->dotdotFid = volp->dotdotFid;
506 if (volp->roID == fidp->volume)
507 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
508 else if (volp->bkID == fidp->volume)
509 scp->flags |= CM_SCACHEFLAG_RO;
511 scp->nextp = cm_data.hashTablep[hash];
512 cm_data.hashTablep[hash] = scp;
513 scp->flags |= CM_SCACHEFLAG_INHASH;
516 /* XXX - The following fields in the cm_scache are
522 lock_ReleaseWrite(&cm_scacheLock);
524 /* now we have a held scache entry; just return it */
529 /* synchronize a fetch, store, read, write, fetch status or store status.
530 * Called with scache mutex held, and returns with it held, but temporarily
531 * drops it during the fetch.
533 * At most one flag can be on in flags, if this is an RPC request.
535 * Also, if we're fetching or storing data, we must ensure that we have a buffer.
537 * There are a lot of weird restrictions here; here's an attempt to explain the
538 * rationale for the concurrency restrictions implemented in this function.
540 * First, although the file server will break callbacks when *another* machine
541 * modifies a file or status block, the client itself is responsible for
542 * concurrency control on its own requests. Callback breaking events are rare,
543 * and simply invalidate any concurrent new status info.
545 * In the absence of callback breaking messages, we need to know how to
546 * synchronize incoming responses describing updates to files. We synchronize
547 * operations that update the data version by comparing the data versions.
548 * However, updates that do not update the data, but only the status, can't be
549 * synchronized with fetches or stores, since there's nothing to compare
550 * to tell which operation executed first at the server.
552 * Thus, we can allow multiple ops that change file data, or dir data, and
553 * fetches. However, status storing ops have to be done serially.
555 * Furthermore, certain data-changing ops are incompatible: we can't read or
556 * write a buffer while doing a truncate. We can't read and write the same
557 * buffer at the same time, or write while fetching or storing, or read while
558 * fetching a buffer (this may change). We can't fetch and store at the same
561 * With respect to status, we can't read and write at the same time, read while
562 * fetching, write while fetching or storing, or fetch and store at the same time.
564 * We can't allow a get callback RPC to run in concurrently with something that
565 * will return updated status, since we could start a call, have the server
566 * return status, have another machine make an update to the status (which
567 * doesn't change serverModTime), have the original machine get a new callback,
568 * and then have the original machine merge in the early, old info from the
569 * first call. At this point, the easiest way to avoid this problem is to have
570 * getcallback calls conflict with all others for the same vnode. Other calls
571 * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
572 * vnode must be careful not to merge in their status unless they have obtained
573 * a callback from the start of their call.
576 * Concurrent StoreData RPC's can cause trouble if the file is being extended.
577 * Each such RPC passes a FileLength parameter, which the server uses to do
578 * pre-truncation if necessary. So if two RPC's are processed out of order at
579 * the server, the one with the smaller FileLength will be processed last,
580 * possibly resulting in a bogus truncation. The simplest way to avoid this
581 * is to serialize all StoreData RPC's. This is the reason we defined
582 * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
584 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *up, cm_req_t *reqp,
585 long rights, long flags)
587 osi_queueData_t *qdp;
593 /* lookup this first */
594 bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
596 /* some minor assertions */
597 if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
598 | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
599 | CM_SCACHESYNC_SETSIZE)) {
601 osi_assert(bufp->refCount > 0);
603 osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
607 else osi_assert(bufp == NULL);
609 /* Do the access check. Now we don't really do the access check
610 * atomically, since the caller doesn't expect the parent dir to be
611 * returned locked, and that is what we'd have to do to prevent a
612 * callback breaking message on the parent due to a setacl call from
613 * being processed while we're running. So, instead, we check things
614 * here, and if things look fine with the access, we proceed to finish
615 * the rest of this check. Sort of a hack, but probably good enough.
619 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
620 /* if we're bringing in a new status block, ensure that
621 * we aren't already doing so, and that no one is
622 * changing the status concurrently, either. We need
623 * to do this, even if the status is of a different
624 * type, since we don't have the ability to figure out,
625 * in the AFS 3 protocols, which status-changing
626 * operation ran first, or even which order a read and
627 * a write occurred in.
629 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
630 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
631 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHSTATUS", scp);
635 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
636 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
637 /* if we're going to make an RPC to change the status, make sure
638 * that no one is bringing in or sending out the status.
640 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING |
641 CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
642 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING|GETCALLBACK want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
645 if (scp->bufReadsp || scp->bufWritesp) {
646 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is bufRead|bufWrite want STORESIZE|STORESTATUS|SETSIZE|GETCALLBACK", scp);
650 if (flags & CM_SCACHESYNC_FETCHDATA) {
651 /* if we're bringing in a new chunk of data, make sure that
652 * nothing is happening to that chunk, and that we aren't
653 * changing the basic file status info, either.
655 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
656 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
657 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING|GETCALLBACK want FETCHDATA", scp);
660 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
661 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is BUF_CMFETCHING|BUF_CMSTORING want FETCHDATA", scp);
665 if (flags & CM_SCACHESYNC_STOREDATA) {
666 /* same as fetch data */
667 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
668 | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK)) {
669 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING|GETCALLBACK want STOREDATA", scp);
672 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
673 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is BUF_CMFETCHING|BUF_CMSTORING want STOREDATA", scp);
678 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
679 /* Don't allow concurrent StoreData RPC's */
680 if (scp->flags & CM_SCACHEFLAG_DATASTORING) {
681 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is DATASTORING want STOREDATA_EXCL", scp);
686 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
687 /* Don't allow more than one BKG store request */
688 if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING) {
689 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is ASYNCSTORING want ASYNCSTORE", scp);
694 if (flags & CM_SCACHESYNC_LOCK) {
695 /* Don't allow concurrent fiddling with lock lists */
696 if (scp->flags & CM_SCACHEFLAG_LOCKING) {
697 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is LOCKING want LOCK", scp);
702 /* now the operations that don't correspond to making RPCs */
703 if (flags & CM_SCACHESYNC_GETSTATUS) {
704 /* we can use the status that's here, if we're not
705 * bringing in new status.
707 if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
708 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING want GETSTATUS", scp);
712 if (flags & CM_SCACHESYNC_SETSTATUS) {
713 /* we can make a change to the local status, as long as
714 * the status isn't changing now.
716 * If we're fetching or storing a chunk of data, we can
717 * change the status locally, since the fetch/store
718 * operations don't change any of the data that we're
721 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
722 | CM_SCACHEFLAG_SIZESTORING)) {
723 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING want SETSTATUS", scp);
727 if (flags & CM_SCACHESYNC_READ) {
728 /* we're going to read the data, make sure that the
729 * status is available, and that the data is here. It
730 * is OK to read while storing the data back.
732 if (scp->flags & CM_SCACHEFLAG_FETCHING) {
733 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING want READ", scp);
736 if (bufp && ((bufp->cmFlags
738 | CM_BUF_CMFULLYFETCHED))
739 == CM_BUF_CMFETCHING)) {
740 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is BUF_CMFETCHING want READ", scp);
744 if (flags & CM_SCACHESYNC_WRITE) {
745 /* don't write unless the status is stable and the chunk
748 if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
749 | CM_SCACHEFLAG_SIZESTORING)) {
750 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING|STORING|SIZESTORING want WRITE", scp);
753 if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING))) {
754 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is BUF_CMFETCHING|BUF_CMSTORING want WRITE", scp);
759 // yj: modified this so that callback only checked if we're
760 // not checking something on /afs
761 /* fix the conditional to match the one in cm_HaveCallback */
762 if ( (flags & CM_SCACHESYNC_NEEDCALLBACK)
763 #ifdef AFS_FREELANCE_CLIENT
764 && (!cm_freelanceEnabled ||
765 !(scp->fid.vnode==0x1 && scp->fid.unique==0x1) ||
766 scp->fid.cell!=AFS_FAKE_ROOT_CELL_ID ||
767 scp->fid.volume!=AFS_FAKE_ROOT_VOL_ID ||
768 cm_fakeDirCallback < 2)
769 #endif /* AFS_FREELANCE_CLIENT */
771 if (!cm_HaveCallback(scp)) {
772 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp %x",
774 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
775 code = cm_GetCallback(scp, up, reqp, 0);
777 lock_ReleaseMutex(&scp->mx);
778 lock_ObtainMutex(&bufp->mx);
779 lock_ObtainMutex(&scp->mx);
788 /* can't check access rights without a callback */
789 osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
791 if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
792 return CM_ERROR_READONLY;
794 if (cm_HaveAccessRights(scp, up, rights, &outRights)) {
795 if (~outRights & rights) return CM_ERROR_NOACCESS;
798 /* we don't know the required access rights */
799 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
800 code = cm_GetAccessRights(scp, up, reqp);
804 lock_ReleaseMutex(&scp->mx);
805 lock_ObtainMutex(&bufp->mx);
806 lock_ObtainMutex(&scp->mx);
812 /* if we get here, we're happy */
816 /* first check if we're not supposed to wait: fail
817 * in this case, returning with everything still locked.
819 if (flags & CM_SCACHESYNC_NOWAIT)
820 return CM_ERROR_WOULDBLOCK;
822 /* wait here, then try again */
823 osi_Log1(afsd_logp, "CM SyncOp sleeping scp 0x%x", scp);
824 if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
825 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%x", scp);
827 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%x", scp);
828 scp->flags |= CM_SCACHEFLAG_WAITING;
831 lock_ReleaseMutex(&bufp->mx);
832 osi_SleepM((long) &scp->flags, &scp->mx);
834 lock_ObtainMutex(&bufp->mx);
835 lock_ObtainMutex(&scp->mx);
836 osi_Log1(afsd_logp, "CM SyncOp woke! scp 0x%x", scp);
837 } /* big while loop */
839 /* now, update the recorded state for RPC-type calls */
840 if (flags & CM_SCACHESYNC_FETCHSTATUS)
841 scp->flags |= CM_SCACHEFLAG_FETCHING;
842 if (flags & CM_SCACHESYNC_STORESTATUS)
843 scp->flags |= CM_SCACHEFLAG_STORING;
844 if (flags & CM_SCACHESYNC_STORESIZE)
845 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
846 if (flags & CM_SCACHESYNC_GETCALLBACK)
847 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
848 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
849 scp->flags |= CM_SCACHEFLAG_DATASTORING;
850 if (flags & CM_SCACHESYNC_ASYNCSTORE)
851 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
852 if (flags & CM_SCACHESYNC_LOCK)
853 scp->flags |= CM_SCACHEFLAG_LOCKING;
855 /* now update the buffer pointer */
856 if (flags & CM_SCACHESYNC_FETCHDATA) {
857 /* ensure that the buffer isn't already in the I/O list */
859 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
860 tbufp = osi_GetQData(qdp);
861 osi_assert(tbufp != bufp);
865 /* queue a held reference to the buffer in the "reading" I/O list */
867 osi_SetQData(qdp, bufp);
870 bufp->cmFlags |= CM_BUF_CMFETCHING;
872 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
875 if (flags & CM_SCACHESYNC_STOREDATA) {
876 /* ensure that the buffer isn't already in the I/O list */
878 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
879 tbufp = osi_GetQData(qdp);
880 osi_assert(tbufp != bufp);
884 /* queue a held reference to the buffer in the "writing" I/O list */
886 osi_SetQData(qdp, bufp);
889 bufp->cmFlags |= CM_BUF_CMSTORING;
891 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
897 /* for those syncops that setup for RPCs.
898 * Called with scache locked.
900 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, long flags)
902 osi_queueData_t *qdp;
905 /* now, update the recorded state for RPC-type calls */
906 if (flags & CM_SCACHESYNC_FETCHSTATUS)
907 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
908 if (flags & CM_SCACHESYNC_STORESTATUS)
909 scp->flags &= ~CM_SCACHEFLAG_STORING;
910 if (flags & CM_SCACHESYNC_STORESIZE)
911 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
912 if (flags & CM_SCACHESYNC_GETCALLBACK)
913 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
914 if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
915 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
916 if (flags & CM_SCACHESYNC_ASYNCSTORE)
917 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
918 if (flags & CM_SCACHESYNC_LOCK)
919 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
921 /* now update the buffer pointer */
922 if (flags & CM_SCACHESYNC_FETCHDATA) {
923 /* ensure that the buffer isn't already in the I/O list */
924 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
925 tbufp = osi_GetQData(qdp);
926 if (tbufp == bufp) break;
928 osi_assert(qdp != NULL);
929 osi_assert(osi_GetQData(qdp) == bufp);
930 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
934 ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
939 /* now update the buffer pointer */
940 if (flags & CM_SCACHESYNC_STOREDATA) {
941 /* ensure that the buffer isn't already in the I/O list */
942 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
943 tbufp = osi_GetQData(qdp);
944 if (tbufp == bufp) break;
946 osi_assert(qdp != NULL);
947 osi_assert(osi_GetQData(qdp) == bufp);
948 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
951 bufp->cmFlags &= ~CM_BUF_CMSTORING;
956 /* and wakeup anyone who is waiting */
957 if (scp->flags & CM_SCACHEFLAG_WAITING) {
958 osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%x", scp);
959 scp->flags &= ~CM_SCACHEFLAG_WAITING;
960 osi_Wakeup((long) &scp->flags);
964 /* merge in a response from an RPC. The scp must be locked, and the callback
967 * Don't overwrite any status info that is dirty, since we could have a store
968 * operation (such as store data) that merges some info in, and we don't want
969 * to lose the local updates. Typically, there aren't many updates we do
970 * locally, anyway, probably only mtime.
972 * There is probably a bug in here where a chmod (which doesn't change
973 * serverModTime) that occurs between two fetches, both of whose responses are
974 * handled after the callback breaking is done, but only one of whose calls
975 * started before that, can cause old info to be merged from the first call.
977 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
978 cm_user_t *userp, int flags)
980 // yj: i want to create some fake status for the /afs directory and the
981 // entries under that directory
982 #ifdef AFS_FREELANCE_CLIENT
983 if (cm_freelanceEnabled && scp == cm_data.rootSCachep) {
984 osi_Log0(afsd_logp,"cm_MergeStatus Freelance cm_data.rootSCachep");
985 statusp->InterfaceVersion = 0x1;
986 statusp->FileType = CM_SCACHETYPE_DIRECTORY;
987 statusp->LinkCount = scp->linkCount;
988 statusp->Length = cm_fakeDirSize;
989 statusp->DataVersion = cm_data.fakeDirVersion;
990 statusp->Author = 0x1;
991 statusp->Owner = 0x0;
992 statusp->CallerAccess = 0x9;
993 statusp->AnonymousAccess = 0x9;
994 statusp->UnixModeBits = 0x1ff;
995 statusp->ParentVnode = 0x1;
996 statusp->ParentUnique = 0x1;
997 statusp->ResidencyMask = 0;
998 statusp->ClientModTime = FakeFreelanceModTime;
999 statusp->ServerModTime = FakeFreelanceModTime;
1001 statusp->SyncCounter = 0;
1002 statusp->dataVersionHigh = 0;
1004 #endif /* AFS_FREELANCE_CLIENT */
1006 if (!(flags & CM_MERGEFLAG_FORCE)
1007 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1008 struct cm_cell *cellp;
1009 struct cm_volume *volp;
1011 cellp = cm_FindCellByID(scp->fid.cell);
1012 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1013 (cm_req_t *) NULL, &volp);
1015 osi_Log2(afsd_logp, "old data from server %x volume %s",
1016 scp->cbServerp->addr.sin_addr.s_addr,
1018 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
1019 scp, scp->dataVersion, statusp->DataVersion);
1020 /* we have a number of data fetch/store operations running
1021 * concurrently, and we can tell which one executed last at the
1022 * server by its mtime.
1023 * Choose the one with the largest mtime, and ignore the rest.
1025 * These concurrent calls are incompatible with setting the
1026 * mtime, so we won't have a locally changed mtime here.
1028 * We could also have ACL info for a different user than usual,
1029 * in which case we have to do that part of the merge, anyway.
1030 * We won't have to worry about the info being old, since we
1031 * won't have concurrent calls
1032 * that change file status running from this machine.
1034 * Added 3/17/98: if we see data version regression on an RO
1035 * file, it's probably due to a server holding an out-of-date
1036 * replica, rather than to concurrent RPC's. Failures to
1037 * release replicas are now flagged by the volserver, but only
1038 * since AFS 3.4 5.22, so there are plenty of clients getting
1039 * out-of-date replicas out there.
1041 * If we discover an out-of-date replica, by this time it's too
1042 * late to go to another server and retry. Also, we can't
1043 * reject the merge, because then there is no way for
1044 * GetAccess to do its work, and the caller gets into an
1045 * infinite loop. So we just grin and bear it.
1047 if (!(scp->flags & CM_SCACHEFLAG_RO))
1050 scp->serverModTime = statusp->ServerModTime;
1052 if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1053 scp->clientModTime = statusp->ClientModTime;
1055 if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1056 scp->length.LowPart = statusp->Length;
1057 scp->length.HighPart = 0;
1060 scp->serverLength.LowPart = statusp->Length;
1061 scp->serverLength.HighPart = 0;
1063 scp->linkCount = statusp->LinkCount;
1064 scp->dataVersion = statusp->DataVersion;
1065 scp->owner = statusp->Owner;
1066 scp->group = statusp->Group;
1067 scp->unixModeBits = statusp->UnixModeBits & 07777;
1069 if (statusp->FileType == File)
1070 scp->fileType = CM_SCACHETYPE_FILE;
1071 else if (statusp->FileType == Directory)
1072 scp->fileType = CM_SCACHETYPE_DIRECTORY;
1073 else if (statusp->FileType == SymbolicLink) {
1074 if ((scp->unixModeBits & 0111) == 0)
1075 scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
1077 scp->fileType = CM_SCACHETYPE_SYMLINK;
1080 osi_Log1(afsd_logp, "Merge, Invalid File Type, scp %x", scp);
1081 scp->fileType = 0; /* invalid */
1083 /* and other stuff */
1084 scp->parentVnode = statusp->ParentVnode;
1085 scp->parentUnique = statusp->ParentUnique;
1087 /* and merge in the private acl cache info, if this is more than the public
1088 * info; merge in the public stuff in any case.
1090 scp->anyAccess = statusp->AnonymousAccess;
1092 if (userp != NULL) {
1093 cm_AddACLCache(scp, userp, statusp->CallerAccess);
1097 /* note that our stat cache info is incorrect, so force us eventually
1098 * to stat the file again. There may be dirty data associated with
1099 * this vnode, and we want to preserve that information.
1101 * This function works by simply simulating a loss of the callback.
1103 * This function must be called with the scache locked.
1105 void cm_DiscardSCache(cm_scache_t *scp)
1107 lock_AssertMutex(&scp->mx);
1108 if (scp->cbServerp) {
1109 cm_PutServer(scp->cbServerp);
1110 scp->cbServerp = NULL;
1113 cm_dnlcPurgedp(scp);
1114 cm_dnlcPurgevp(scp);
1115 cm_FreeAllACLEnts(scp);
1118 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1120 afsFidp->Volume = fidp->volume;
1121 afsFidp->Vnode = fidp->vnode;
1122 afsFidp->Unique = fidp->unique;
1125 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1127 osi_assert(scp != 0);
1128 osi_assert(scp->refCount >= 0);
1132 void cm_HoldSCache(cm_scache_t *scp)
1134 osi_assert(scp != 0);
1135 lock_ObtainWrite(&cm_scacheLock);
1136 osi_assert(scp->refCount >= 0);
1138 lock_ReleaseWrite(&cm_scacheLock);
1141 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1143 osi_assert(scp != 0);
1144 osi_assert(scp->refCount-- >= 0);
1147 void cm_ReleaseSCache(cm_scache_t *scp)
1149 osi_assert(scp != 0);
1150 lock_ObtainWrite(&cm_scacheLock);
1151 osi_assert(scp->refCount != 0);
1153 lock_ReleaseWrite(&cm_scacheLock);
1156 /* just look for the scp entry to get filetype */
1157 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
1158 int cm_FindFileType(cm_fid_t *fidp)
1163 hash = CM_SCACHE_HASH(fidp);
1165 osi_assert(fidp->cell != 0);
1167 lock_ObtainWrite(&cm_scacheLock);
1168 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
1169 if (cm_FidCmp(fidp, &scp->fid) == 0) {
1170 lock_ReleaseWrite(&cm_scacheLock);
1171 return scp->fileType;
1174 lock_ReleaseWrite(&cm_scacheLock);
1178 /* dump all scp's that have reference count > 0 to a file.
1179 * cookie is used to identify this batch for easy parsing,
1180 * and it a string provided by a caller
1182 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1190 lock_ObtainRead(&cm_scacheLock);
1192 sprintf(output, "%s - dumping scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
1193 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1195 for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q))
1197 if (scp->refCount != 0)
1199 sprintf(output, "%s fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1200 cookie, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
1202 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1206 sprintf(output, "%s - dumping cm_data.hashTable - cm_data.hashTableSize=%d\n", cookie, cm_data.hashTableSize);
1207 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1209 for (i = 0; i < cm_data.hashTableSize; i++)
1211 for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp)
1213 if (scp->refCount != 0)
1215 sprintf(output, "%s scp=0x%08X, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d) refCount=%u\n",
1216 cookie, (void *)scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode,
1217 scp->fid.unique, scp->refCount);
1218 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1223 sprintf(output, "%s - Done dumping scache.\n", cookie);
1224 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1227 lock_ReleaseRead(&cm_scacheLock);