windows-afsd-minor-20050330
[openafs.git] / src / WINNT / afsd / cm_scache.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #include <winsock2.h>
16 #include <nb30.h>
17 #endif /* !DJGPP */
18 #include <malloc.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <osi.h>
22
23 #include "afsd.h"
24
25 /*extern void afsi_log(char *pattern, ...);*/
26
27 extern osi_hyper_t hzero;
28
29 /* File locks */
30 osi_queue_t *cm_allFileLocks;
31
32 /* lock for globals */
33 osi_rwlock_t cm_scacheLock;
34
35 /* Dummy scache entry for use with pioctl fids */
36 cm_scache_t cm_fakeSCache;
37
38 #ifdef AFS_FREELANCE_CLIENT
39 extern osi_mutex_t cm_Freelance_Lock;
40 #endif
41
42 /* must be called with cm_scacheLock write-locked! */
43 void cm_AdjustLRU(cm_scache_t *scp)
44 {
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;
51 }
52
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).
55  */
56 cm_scache_t *cm_GetNewSCache(void)
57 {
58     cm_scache_t *scp;
59     int i;
60     cm_scache_t **lscpp;
61     cm_scache_t *tscp;
62
63     if (cm_data.currentSCaches >= cm_data.maxSCaches) {
64         for (scp = cm_data.scacheLRULastp;
65               scp;
66               scp = (cm_scache_t *) osi_QPrev(&scp->q)) {
67             if (scp->refCount == 0) 
68                 break;
69         }
70                 
71         if (scp) {
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];
78                       tscp;
79                       lscpp = &tscp->nextp, tscp = tscp->nextp) {
80                     if (tscp == scp) {
81                         *lscpp = scp->nextp;
82                         scp->flags &= ~CM_SCACHEFLAG_INHASH;
83                         break;
84                     }
85                 }
86                 osi_assertx(tscp, "afsd: scache hash screwup");
87             }
88
89             /* look for things that shouldn't still be set */
90             osi_assert(scp->bufWritesp == NULL);
91             osi_assert(scp->bufReadsp == NULL);
92
93             /* invalidate so next merge works fine;
94             * also initialize some flags */
95             scp->flags &= ~(CM_SCACHEFLAG_STATD
96                              | CM_SCACHEFLAG_RO
97                              | CM_SCACHEFLAG_PURERO
98                              | CM_SCACHEFLAG_OVERQUOTA
99                              | CM_SCACHEFLAG_OUTOFSPACE);
100             scp->serverModTime = 0;
101             scp->dataVersion = 0;
102             scp->bulkStatProgress = hzero;
103
104             /* discard callback */
105             if (scp->cbServerp) {
106                 cm_PutServer(scp->cbServerp);
107                 scp->cbServerp = NULL;
108             }
109             scp->cbExpires = 0;
110
111             /* remove from dnlc */
112             cm_dnlcPurgedp(scp);
113             cm_dnlcPurgevp(scp);
114
115             /* discard cached status; if non-zero, Close
116              * tried to store this to server but failed */
117             scp->mask = 0;
118
119             /* drop held volume ref */
120             if (scp->volp) {
121                 cm_PutVolume(scp->volp);
122                 scp->volp = NULL;
123             }
124
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));
129
130             /* not locked, but there can be no references to this guy
131              * while we hold the global refcount lock.
132              */
133             cm_FreeAllACLEnts(scp);
134
135             /* now remove from the LRU queue and put it back at the
136              * head of the LRU queue.
137              */
138             cm_AdjustLRU(scp);
139
140             /* and we're done */
141             return scp;
142         }
143     }
144         
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.
147      */
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");
154
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 */
161     cm_dnlcPurgevp(scp); 
162     return scp;
163 }       
164
165 /* like strcmp, only for fids */
166 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
167 {
168     if (ap->vnode != bp->vnode) 
169         return 1;
170     if (ap->volume != bp->volume) 
171         return 1;
172     if (ap->unique != bp->unique) 
173         return 1;
174     if (ap->cell != bp->cell) 
175         return 1;
176     return 0;
177 }
178
179 void cm_fakeSCacheInit(int newFile)
180 {
181     if ( 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;
190     }
191     lock_InitializeMutex(&cm_data.fakeSCache.mx, "cm_scache_t mutex");
192 }       
193
194 long
195 cm_ValidateSCache(void)
196 {
197     cm_scache_t * scp;
198     long i;
199
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");
205             return -1;
206         }
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");
210             return -2;
211         }
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");
215             return -3;
216         }
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");
220             return -4;
221         }
222     }
223
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");
229             return -5;
230         }
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");
234             return -6;
235         }
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");
239             return -7;
240         }
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");
244             return -8;
245         }
246     }
247
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");
253                 return -9;
254             }
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");
258                 return -10;
259             }
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");
263                 return -11;
264             }
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");
268                 return -12;
269             }
270         }
271     }
272
273     return cm_dnlcValidate();
274 }
275
276 long
277 cm_ShutdownSCache(void)
278 {
279     cm_scache_t * scp;
280
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);
287         }
288         lock_FinalizeMutex(&scp->mx);
289         lock_FinalizeRWLock(&scp->bufCreateLock);
290     }
291
292     return cm_dnlcShutdown();
293 }
294
295 void cm_InitSCache(int newFile, long maxSCaches)
296 {
297     static osi_once_t once;
298         
299     if (osi_Once(&once)) {
300         lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
301         if ( newFile ) {
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;
306         } else {
307             cm_scache_t * scp;
308
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");
313
314                 scp->cbServerp = NULL;
315                 scp->cbExpires = 0;
316                 scp->fileLocks = NULL;
317                 scp->openReads = 0;
318                 scp->openWrites = 0;
319                 scp->openShares = 0;
320                 scp->openExcls = 0;
321             }
322         }
323         cm_allFileLocks = NULL;
324         cm_fakeSCacheInit(newFile);
325         cm_dnlcInit(newFile);
326         osi_EndOnce(&once);
327     }
328 }
329
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)
332 {
333     long hash;
334     cm_scache_t *scp;
335
336     hash = CM_SCACHE_HASH(fidp);
337         
338     osi_assert(fidp->cell != 0);
339
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);
344             cm_AdjustLRU(scp);
345             lock_ReleaseWrite(&cm_scacheLock);
346             return scp;
347         }
348     }
349     lock_ReleaseWrite(&cm_scacheLock);
350     return NULL;
351 }
352
353 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
354                   cm_req_t *reqp)
355 {
356     long hash;
357     cm_scache_t *scp;
358     long code;
359     cm_volume_t *volp = 0;
360     cm_cell_t *cellp;
361     char* mp = 0;
362     int special; // yj: boolean variable to test if file is on root.afs
363     int isRoot;
364     extern cm_fid_t cm_rootFid;
365         
366     hash = CM_SCACHE_HASH(fidp);
367         
368     osi_assert(fidp->cell != 0);
369
370     if (fidp->cell== cm_data.rootFid.cell && 
371          fidp->volume==cm_data.rootFid.volume &&
372          fidp->vnode==0x0 && fidp->unique==0x0)
373     {
374         osi_Log0(afsd_logp,"cm_getSCache called with root cell/volume and vnode=0 and unique=0");
375     }
376
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);
383             *outScpp = scp;
384             cm_AdjustLRU(scp);
385             lock_ReleaseWrite(&cm_scacheLock);
386             return 0;
387         }
388     }
389         
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.
393
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. 
409          */
410         volp = NULL;
411     }
412           
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);
419         } else {
420             mp = "";
421         }
422         scp = cm_GetNewSCache();
423                 
424         scp->fid = *fidp;
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;
434         scp->refCount = 1;
435         if (fidp->vnode > 1 && fidp->vnode <= cm_localMountPoints + 2)
436             scp->fileType = (cm_localMountPoints+fidp->vnode-2)->fileType;
437         else 
438             scp->fileType = CM_SCACHETYPE_INVALID;
439
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);
445
446         scp->owner=0x0;
447         scp->unixModeBits=0x1ff;
448         scp->clientModTime=FakeFreelanceModTime;
449         scp->serverModTime=FakeFreelanceModTime;
450         scp->parentUnique = 0x1;
451         scp->parentVnode=0x1;
452         scp->group=0;
453         scp->dataVersion=cm_data.fakeDirVersion;
454         *outScpp = scp;
455         lock_ReleaseWrite(&cm_scacheLock);
456         /*afsi_log("   getscache done");*/
457         return 0;
458     }
459     // end of yj code
460 #endif /* AFS_FREELANCE_CLIENT */
461
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);
466         if (!cellp) 
467             return CM_ERROR_NOSUCHCELL;
468
469         code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
470         if (code) 
471             return code;
472         lock_ObtainWrite(&cm_scacheLock);
473     }
474         
475     /* otherwise, we have the volume, now reverify that the scp doesn't
476      * exist, and proceed.
477      */
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);
482             cm_AdjustLRU(scp);
483             lock_ReleaseWrite(&cm_scacheLock);
484             if (volp)
485                 cm_PutVolume(volp);
486             *outScpp = scp;
487             return 0;
488         }
489     }
490         
491     /* now, if we don't have the fid, recycle something */
492     scp = cm_GetNewSCache();
493     osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
494     scp->fid = *fidp;
495     scp->volp = volp;   /* a held reference */
496
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)
501          */
502         if (scp->fid.vnode == 1 && scp->fid.unique == 1) {
503             scp->dotdotFid = volp->dotdotFid;
504         }
505           
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;
510     }
511     scp->nextp = cm_data.hashTablep[hash];
512     cm_data.hashTablep[hash] = scp;
513     scp->flags |= CM_SCACHEFLAG_INHASH;
514     scp->refCount = 1;
515
516     /* XXX - The following fields in the cm_scache are 
517      * uninitialized:
518      *   fileType
519      *   parentVnode
520      *   parentUnique
521      */
522     lock_ReleaseWrite(&cm_scacheLock);
523         
524     /* now we have a held scache entry; just return it */
525     *outScpp = scp;
526     return 0;
527 }
528
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.
532  * 
533  * At most one flag can be on in flags, if this is an RPC request.
534  *
535  * Also, if we're fetching or storing data, we must ensure that we have a buffer.
536  *
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.
539  *
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.
544  *
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.
551  *
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.
554  *
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
559  * time, either.
560  *
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.
563  *
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.
574  *
575  * Note added 1/23/96
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.
583  */
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)
586 {
587     osi_queueData_t *qdp;
588     long code;
589     cm_buf_t *tbufp;
590     long outRights;
591     int bufLocked;
592
593     /* lookup this first */
594     bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
595
596     /* some minor assertions */
597     if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
598                   | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
599                   | CM_SCACHESYNC_SETSIZE)) {
600         if (bufp) {
601             osi_assert(bufp->refCount > 0);
602             /*
603                osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
604              */
605         }
606     }
607     else osi_assert(bufp == NULL);
608
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.
616      */
617
618     while (1) {
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.
628              */
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);
632                 goto sleep;
633             }
634         }
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.
639              */
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);
643                 goto sleep;
644             }
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);
647                 goto sleep;
648             }
649         }
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.
654              */
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);
658                 goto sleep;
659             }
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);
662                 goto sleep;
663             }
664         }
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);
670                 goto sleep;
671             }
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);
674                 goto sleep;
675             }
676         }
677
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);
682                 goto sleep;
683             }
684         }
685
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);
690                 goto sleep;
691             }
692         }
693
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);
698                 goto sleep;
699             }
700         }
701
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.
706              */
707             if (scp->flags & (CM_SCACHEFLAG_FETCHING)) {
708                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING want GETSTATUS", scp);
709                 goto sleep;
710             }
711         }
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.
715              *
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
719              * changing here.
720              */
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);
724                 goto sleep;
725             }
726         }
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.
731              */
732             if (scp->flags & CM_SCACHEFLAG_FETCHING) {
733                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is FETCHING want READ", scp);
734                 goto sleep;
735             }
736             if (bufp && ((bufp->cmFlags
737                            & (CM_BUF_CMFETCHING
738                                | CM_BUF_CMFULLYFETCHED))
739                           == CM_BUF_CMFETCHING)) {
740                 osi_Log1(afsd_logp, "CM SyncOp scp 0x%x is BUF_CMFETCHING want READ", scp);
741                 goto sleep;
742             }
743         }
744         if (flags & CM_SCACHESYNC_WRITE) {
745             /* don't write unless the status is stable and the chunk
746              * is stable.
747              */
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);
751                 goto sleep;
752             }
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);
755                 goto sleep;
756             }
757         }
758
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 */
770              ) {
771             if (!cm_HaveCallback(scp)) {
772                 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp %x",
773                           (long) scp);
774                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
775                 code = cm_GetCallback(scp, up, reqp, 0);
776                 if (bufLocked) {
777                     lock_ReleaseMutex(&scp->mx);
778                     lock_ObtainMutex(&bufp->mx);
779                     lock_ObtainMutex(&scp->mx);
780                 }
781                 if (code) 
782                     return code;
783                 continue;
784             }
785         }
786
787         if (rights) {
788             /* can't check access rights without a callback */
789             osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
790
791             if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
792                 return CM_ERROR_READONLY;
793
794             if (cm_HaveAccessRights(scp, up, rights, &outRights)) {
795                 if (~outRights & rights) return CM_ERROR_NOACCESS;
796             }
797             else {
798                 /* we don't know the required access rights */
799                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
800                 code = cm_GetAccessRights(scp, up, reqp);
801                 if (code) 
802                     return code;
803                 if (bufLocked) {
804                     lock_ReleaseMutex(&scp->mx);
805                     lock_ObtainMutex(&bufp->mx);
806                     lock_ObtainMutex(&scp->mx);
807                 }
808                 continue;
809             }
810         }
811
812         /* if we get here, we're happy */
813         break;
814
815       sleep:
816         /* first check if we're not supposed to wait: fail 
817          * in this case, returning with everything still locked.
818          */
819         if (flags & CM_SCACHESYNC_NOWAIT) 
820             return CM_ERROR_WOULDBLOCK;
821
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);
826         } else {
827             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%x", scp);
828             scp->flags |= CM_SCACHEFLAG_WAITING;
829         }
830         if (bufLocked) 
831             lock_ReleaseMutex(&bufp->mx);
832         osi_SleepM((long) &scp->flags, &scp->mx);
833         if (bufLocked) 
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 */
838         
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;
854
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 */
858         if (bufp) {
859             for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
860                 tbufp = osi_GetQData(qdp);
861                 osi_assert(tbufp != bufp);
862             }
863         }
864
865         /* queue a held reference to the buffer in the "reading" I/O list */
866         qdp = osi_QDAlloc();
867         osi_SetQData(qdp, bufp);
868         if (bufp) {
869             buf_Hold(bufp);
870             bufp->cmFlags |= CM_BUF_CMFETCHING;
871         }
872         osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
873     }
874
875     if (flags & CM_SCACHESYNC_STOREDATA) {
876         /* ensure that the buffer isn't already in the I/O list */
877         if (bufp) {
878             for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
879                 tbufp = osi_GetQData(qdp);
880                 osi_assert(tbufp != bufp);
881             }
882         }
883
884         /* queue a held reference to the buffer in the "writing" I/O list */
885         qdp = osi_QDAlloc();
886         osi_SetQData(qdp, bufp);
887         if (bufp) {
888             buf_Hold(bufp);
889             bufp->cmFlags |= CM_BUF_CMSTORING;
890         }
891         osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
892     }
893
894     return 0;
895 }
896
897 /* for those syncops that setup for RPCs.
898  * Called with scache locked.
899  */
900 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, long flags)
901 {
902     osi_queueData_t *qdp;
903     cm_buf_t *tbufp;
904
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;
920
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;
927         }
928         osi_assert(qdp != NULL);
929         osi_assert(osi_GetQData(qdp) == bufp);
930         osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
931         osi_QDFree(qdp);
932         if (bufp) {
933             bufp->cmFlags &=
934                 ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
935             buf_Release(bufp);
936         }
937     }
938
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;
945         }
946         osi_assert(qdp != NULL);
947         osi_assert(osi_GetQData(qdp) == bufp);
948         osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
949         osi_QDFree(qdp);
950         if (bufp) {
951             bufp->cmFlags &= ~CM_BUF_CMSTORING;
952             buf_Release(bufp);
953         }
954     }
955
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);
961     }
962 }       
963
964 /* merge in a response from an RPC.  The scp must be locked, and the callback
965  * is optional.
966  *
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.
971  *
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.
976  */
977 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
978                     cm_user_t *userp, int flags)
979 {
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;
1000         statusp->Group = 0;
1001         statusp->SyncCounter = 0;
1002         statusp->dataVersionHigh = 0;
1003     }
1004 #endif /* AFS_FREELANCE_CLIENT */
1005
1006     if (!(flags & CM_MERGEFLAG_FORCE)
1007          && statusp->DataVersion < (unsigned long) scp->dataVersion) {
1008         struct cm_cell *cellp;
1009         struct cm_volume *volp;
1010
1011         cellp = cm_FindCellByID(scp->fid.cell);
1012         cm_GetVolumeByID(cellp, scp->fid.volume, userp,
1013                           (cm_req_t *) NULL, &volp);
1014         if (scp->cbServerp)
1015             osi_Log2(afsd_logp, "old data from server %x volume %s",
1016                       scp->cbServerp->addr.sin_addr.s_addr,
1017                       volp->namep);
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.
1024          *
1025          * These concurrent calls are incompatible with setting the
1026          * mtime, so we won't have a locally changed mtime here.
1027          *
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.
1033          *
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.
1040          *
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.
1046          */
1047         if (!(scp->flags & CM_SCACHEFLAG_RO))
1048             return;
1049     }       
1050     scp->serverModTime = statusp->ServerModTime;
1051
1052     if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
1053         scp->clientModTime = statusp->ClientModTime;
1054     }
1055     if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
1056         scp->length.LowPart = statusp->Length;
1057         scp->length.HighPart = 0;
1058     }
1059
1060     scp->serverLength.LowPart = statusp->Length;
1061     scp->serverLength.HighPart = 0;
1062
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;
1068
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;
1076         else
1077             scp->fileType = CM_SCACHETYPE_SYMLINK;
1078     }       
1079     else {
1080         osi_Log1(afsd_logp, "Merge, Invalid File Type, scp %x", scp);
1081         scp->fileType = 0;      /* invalid */
1082     }
1083     /* and other stuff */
1084     scp->parentVnode = statusp->ParentVnode;
1085     scp->parentUnique = statusp->ParentUnique;
1086         
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.
1089      */
1090     scp->anyAccess = statusp->AnonymousAccess;
1091
1092     if (userp != NULL) {
1093         cm_AddACLCache(scp, userp, statusp->CallerAccess);
1094     }
1095 }
1096
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.
1100  *
1101  * This function works by simply simulating a loss of the callback.
1102  *
1103  * This function must be called with the scache locked.
1104  */
1105 void cm_DiscardSCache(cm_scache_t *scp)
1106 {
1107     lock_AssertMutex(&scp->mx);
1108     if (scp->cbServerp) {
1109         cm_PutServer(scp->cbServerp);
1110         scp->cbServerp = NULL;
1111     }
1112     scp->cbExpires = 0;
1113     cm_dnlcPurgedp(scp);
1114     cm_dnlcPurgevp(scp);
1115     cm_FreeAllACLEnts(scp);
1116 }
1117
1118 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
1119 {
1120     afsFidp->Volume = fidp->volume;
1121     afsFidp->Vnode = fidp->vnode;
1122     afsFidp->Unique = fidp->unique;
1123 }       
1124
1125 void cm_HoldSCacheNoLock(cm_scache_t *scp)
1126 {
1127     osi_assert(scp != 0);
1128     osi_assert(scp->refCount >= 0);
1129     scp->refCount++;
1130 }
1131
1132 void cm_HoldSCache(cm_scache_t *scp)
1133 {
1134     osi_assert(scp != 0);
1135     lock_ObtainWrite(&cm_scacheLock);
1136     osi_assert(scp->refCount >= 0);
1137     scp->refCount++;
1138     lock_ReleaseWrite(&cm_scacheLock);
1139 }
1140
1141 void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
1142 {
1143     osi_assert(scp != 0);
1144     osi_assert(scp->refCount-- >= 0);
1145 }
1146
1147 void cm_ReleaseSCache(cm_scache_t *scp)
1148 {
1149     osi_assert(scp != 0);
1150     lock_ObtainWrite(&cm_scacheLock);
1151     osi_assert(scp->refCount != 0);
1152     scp->refCount--;
1153     lock_ReleaseWrite(&cm_scacheLock);
1154 }
1155
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)
1159 {
1160     long hash;
1161     cm_scache_t *scp;
1162         
1163     hash = CM_SCACHE_HASH(fidp);
1164         
1165     osi_assert(fidp->cell != 0);
1166
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;
1172         }
1173     }
1174     lock_ReleaseWrite(&cm_scacheLock);
1175     return 0;
1176 }
1177
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 
1181  */
1182 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
1183 {
1184     int zilch;
1185     cm_scache_t *scp;
1186     char output[1024];
1187     int i;
1188   
1189     if (lock)
1190         lock_ObtainRead(&cm_scacheLock);
1191   
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);
1194   
1195     for (scp = cm_data.scacheLRULastp; scp; scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
1196     {
1197         if (scp->refCount != 0)
1198         {
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, 
1201                     scp->refCount);
1202             WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1203         }
1204     }
1205   
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);
1208   
1209     for (i = 0; i < cm_data.hashTableSize; i++)
1210     {
1211         for(scp = cm_data.hashTablep[i]; scp; scp=scp->nextp) 
1212         {
1213             if (scp->refCount != 0)
1214             {
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);
1219             }
1220         }
1221     }
1222
1223     sprintf(output, "%s - Done dumping scache.\n", cookie);
1224     WriteFile(outputFile, output, strlen(output), &zilch, NULL);
1225   
1226     if (lock)
1227         lock_ReleaseRead(&cm_scacheLock);       
1228     return (0);     
1229 }
1230