759a31f342a0a84beafcf2894ec0b7eb5b462da2
[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 osi_hyper_t hzero;
26
27 /* hash table stuff */
28 cm_scache_t **cm_hashTablep;
29 long cm_hashTableSize;
30 long cm_maxSCaches;
31 long cm_currentSCaches;
32
33 /* LRU stuff */
34 cm_scache_t *cm_scacheLRUFirstp;
35 cm_scache_t *cm_scacheLRULastp;
36
37 /* File locks */
38 osi_queue_t *cm_allFileLocks;
39
40 /* lock for globals */
41 osi_rwlock_t cm_scacheLock;
42
43 /* Dummy scache entry for use with pioctl fids */
44 cm_scache_t cm_fakeSCache;
45
46 /* must be called with cm_scacheLock write-locked! */
47 void cm_AdjustLRU(cm_scache_t *scp)
48 {
49         if (scp == cm_scacheLRULastp)
50                 cm_scacheLRULastp = (cm_scache_t *) osi_QPrev(&scp->q);
51         osi_QRemove((osi_queue_t **) &cm_scacheLRUFirstp, &scp->q);
52         osi_QAdd((osi_queue_t **) &cm_scacheLRUFirstp, &scp->q);
53         if (!cm_scacheLRULastp) cm_scacheLRULastp = scp;
54 }
55
56 /* called with cm_scacheLock write-locked; find a vnode to recycle.
57  * Can allocate a new one if desperate, or if below quota (cm_maxSCaches).
58  */
59 cm_scache_t *cm_GetNewSCache(void)
60 {
61         cm_scache_t *scp;
62         int i;
63         cm_scache_t **lscpp;
64         cm_scache_t *tscp;
65
66         if (cm_currentSCaches >= cm_maxSCaches) {
67                 for (scp = cm_scacheLRULastp;
68                      scp;
69                      scp = (cm_scache_t *) osi_QPrev(&scp->q)) {
70                   if (scp->refCount == 0) break;
71                 }
72                 
73                 if (scp) {
74                         /* we found an entry, so return it */
75                         if (scp->flags & CM_SCACHEFLAG_INHASH) {
76                                 /* hash it out first */
77                                 i = CM_SCACHE_HASH(&scp->fid);
78                                 lscpp = &cm_hashTablep[i];
79                                 for (tscp = *lscpp;
80                                      tscp;
81                                      lscpp = &tscp->nextp, tscp = *lscpp) {
82                                   if (tscp == scp) break;
83                                 }
84                                 osi_assertx(tscp, "afsd: scache hash screwup");
85                                 *lscpp = scp->nextp;
86                                 scp->flags &= ~CM_SCACHEFLAG_INHASH;
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                         scp->cbServerp = NULL;
106                         scp->cbExpires = 0;
107
108                         /* remove from dnlc */
109                         cm_dnlcPurgedp(scp);
110                         cm_dnlcPurgevp(scp);
111
112                         /* discard cached status; if non-zero, Close
113                          * tried to store this to server but failed */
114                         scp->mask = 0;
115
116                         /* drop held volume ref */
117                         if (scp->volp) {
118                                 cm_PutVolume(scp->volp);
119                                 scp->volp = NULL;
120                         }
121
122                         /* discard symlink info */
123                         if (scp->mountPointStringp) {
124                                 free(scp->mountPointStringp);
125                                 scp->mountPointStringp = NULL;
126                         }
127                         if (scp->mountRootFidp) {
128                                 free(scp->mountRootFidp);
129                                 scp->mountRootFidp = NULL;
130                         }
131                         if (scp->dotdotFidp) {
132                                 free(scp->dotdotFidp);
133                                 scp->dotdotFidp = NULL;
134                         }
135                         
136                         /* not locked, but there can be no references to this guy
137                          * while we hold the global refcount lock.
138                          */
139                         cm_FreeAllACLEnts(scp);
140                         
141                         /* now remove from the LRU queue and put it back at the
142                          * head of the LRU queue.
143                          */
144                         cm_AdjustLRU(scp);
145                         
146                         /* and we're done */
147                         return scp;
148                 }
149         }
150         
151         /* if we get here, we should allocate a new scache entry.  We either are below
152          * quota or we have a leak and need to allocate a new one to avoid panicing.
153          */
154         scp = malloc(sizeof(*scp));
155         memset(scp, 0, sizeof(*scp));
156         lock_InitializeMutex(&scp->mx, "cm_scache_t mutex");
157         lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
158         
159         /* and put it in the LRU queue */
160         osi_QAdd((osi_queue_t **) &cm_scacheLRUFirstp, &scp->q);
161         if (!cm_scacheLRULastp) cm_scacheLRULastp = scp;
162         cm_currentSCaches++;
163         cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
164         cm_dnlcPurgevp(scp); 
165         return scp;
166 }
167
168 /* like strcmp, only for fids */
169 int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
170 {
171         if (ap->vnode != bp->vnode) return 1;
172         if (ap->volume != bp->volume) return 1;
173         if (ap->unique != bp->unique) return 1;
174         if (ap->cell != bp->cell) return 1;
175         return 0;
176 }
177
178 void cm_fakeSCacheInit()
179 {
180         memset(&cm_fakeSCache, 0, sizeof(cm_fakeSCache));
181         lock_InitializeMutex(&cm_fakeSCache.mx, "cm_scache_t mutex");
182         cm_fakeSCache.cbServerp = (struct cm_server *)(-1);
183         /* can leave clientModTime at 0 */
184         cm_fakeSCache.fileType = CM_SCACHETYPE_FILE;
185         cm_fakeSCache.unixModeBits = 0777;
186         cm_fakeSCache.length.LowPart = 1000;
187         cm_fakeSCache.linkCount = 1;
188 }
189
190 void cm_InitSCache(long maxSCaches)
191 {
192         static osi_once_t once;
193         
194         if (osi_Once(&once)) {
195                 lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock");
196                 cm_hashTableSize = maxSCaches / 2;
197                 cm_hashTablep = malloc(sizeof(cm_scache_t *) * cm_hashTableSize);
198                 memset(cm_hashTablep, 0, sizeof(cm_scache_t *) * cm_hashTableSize);
199                 cm_allFileLocks = NULL;
200                 cm_currentSCaches = 0;
201                 cm_maxSCaches = maxSCaches;
202                 cm_fakeSCacheInit();
203                 cm_dnlcInit();
204                 osi_EndOnce(&once);
205         }
206 }
207
208 /* version that doesn't bother creating the entry if we don't find it */
209 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
210 {
211         long hash;
212         cm_scache_t *scp;
213         
214         hash = CM_SCACHE_HASH(fidp);
215         
216         osi_assert(fidp->cell != 0);
217
218         lock_ObtainWrite(&cm_scacheLock);
219         for(scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
220                 if (cm_FidCmp(fidp, &scp->fid) == 0) {
221                         scp->refCount++;
222                         cm_AdjustLRU(scp);
223                         lock_ReleaseWrite(&cm_scacheLock);
224                         return scp;
225                 }
226         }
227         lock_ReleaseWrite(&cm_scacheLock);
228         return NULL;
229 }
230
231 long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
232         cm_req_t *reqp)
233 {
234         long hash;
235         cm_scache_t *scp;
236         long code;
237         cm_volume_t *volp;
238         cm_cell_t *cellp;
239         
240         hash = CM_SCACHE_HASH(fidp);
241         
242         osi_assert(fidp->cell != 0);
243
244         lock_ObtainWrite(&cm_scacheLock);
245         for(scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
246                 if (cm_FidCmp(fidp, &scp->fid) == 0) {
247                         scp->refCount++;
248                         *outScpp = scp;
249                         cm_AdjustLRU(scp);
250                         lock_ReleaseWrite(&cm_scacheLock);
251                         return 0;
252                 }
253         }
254         
255         /* otherwise, we need to find the volume */
256         lock_ReleaseWrite(&cm_scacheLock);      /* for perf. reasons */
257         cellp = cm_FindCellByID(fidp->cell);
258         if (!cellp) return CM_ERROR_NOSUCHCELL;
259
260         code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
261         if (code) return code;
262         
263         /* otherwise, we have the volume, now reverify that the scp doesn't
264          * exist, and proceed.
265          */
266         lock_ObtainWrite(&cm_scacheLock);
267         for(scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
268                 if (cm_FidCmp(fidp, &scp->fid) == 0) {
269                         scp->refCount++;
270                         cm_AdjustLRU(scp);
271                         lock_ReleaseWrite(&cm_scacheLock);
272                         cm_PutVolume(volp);
273                         *outScpp = scp;
274                         return 0;
275                 }
276         }
277         
278         /* now, if we don't have the fid, recycle something */
279         scp = cm_GetNewSCache();
280         osi_assert(!(scp->flags & CM_SCACHEFLAG_INHASH));
281         scp->fid = *fidp;
282         scp->volp = volp;       /* a held reference */
283
284         /* if this scache entry represents a volume root then we need 
285          * to copy the dotdotFipd from the volume structure where the 
286          * "master" copy is stored (defect 11489)
287          */
288         if(scp->fid.vnode == 1 && scp->fid.unique == 1 && volp->dotdotFidp) {
289                 if (scp->dotdotFidp == (cm_fid_t *) NULL)
290                         scp->dotdotFidp = (cm_fid_t *) malloc(sizeof(cm_fid_t));
291                 *(scp->dotdotFidp) = *volp->dotdotFidp;
292         }
293
294         if (volp->roID == fidp->volume)
295                 scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
296         else if (volp->bkID == fidp->volume)
297                 scp->flags |= CM_SCACHEFLAG_RO;
298         scp->nextp = cm_hashTablep[hash];
299         cm_hashTablep[hash] = scp;
300         scp->flags |= CM_SCACHEFLAG_INHASH;
301         scp->refCount = 1;
302         lock_ReleaseWrite(&cm_scacheLock);
303         
304         /* now we have a held scache entry; just return it */
305         *outScpp = scp;
306         
307         return 0;
308 }
309
310 /* synchronize a fetch, store, read, write, fetch status or store status.
311  * Called with scache mutex held, and returns with it held, but temporarily
312  * drops it during the fetch.
313  * 
314  * At most one flag can be on in flags, if this is an RPC request.
315  *
316  * Also, if we're fetching or storing data, we must ensure that we have a buffer.
317  *
318  * There are a lot of weird restrictions here; here's an attempt to explain the
319  * rationale for the concurrency restrictions implemented in this function.
320  *
321  * First, although the file server will break callbacks when *another* machine
322  * modifies a file or status block, the client itself is responsible for
323  * concurrency control on its own requests.  Callback breaking events are rare,
324  * and simply invalidate any concurrent new status info.
325  *
326  * In the absence of callback breaking messages, we need to know how to
327  * synchronize incoming responses describing updates to files.  We synchronize
328  * operations that update the data version by comparing the data versions.
329  * However, updates that do not update the data, but only the status, can't be
330  * synchronized with fetches or stores, since there's nothing to compare
331  * to tell which operation executed first at the server.
332  *
333  * Thus, we can allow multiple ops that change file data, or dir data, and
334  * fetches.  However, status storing ops have to be done serially.
335  *
336  * Furthermore, certain data-changing ops are incompatible: we can't read or
337  * write a buffer while doing a truncate.  We can't read and write the same
338  * buffer at the same time, or write while fetching or storing, or read while
339  * fetching a buffer (this may change).  We can't fetch and store at the same
340  * time, either.
341  *
342  * With respect to status, we can't read and write at the same time, read while
343  * fetching, write while fetching or storing, or fetch and store at the same time.
344  *
345  * We can't allow a get callback RPC to run in concurrently with something that
346  * will return updated status, since we could start a call, have the server
347  * return status, have another machine make an update to the status (which
348  * doesn't change serverModTime), have the original machine get a new callback,
349  * and then have the original machine merge in the early, old info from the
350  * first call.  At this point, the easiest way to avoid this problem is to have
351  * getcallback calls conflict with all others for the same vnode.  Other calls
352  * to cm_MergeStatus that aren't associated with calls to cm_SyncOp on the same
353  * vnode must be careful not to merge in their status unless they have obtained
354  * a callback from the start of their call.
355  *
356  * Note added 1/23/96
357  * Concurrent StoreData RPC's can cause trouble if the file is being extended.
358  * Each such RPC passes a FileLength parameter, which the server uses to do
359  * pre-truncation if necessary.  So if two RPC's are processed out of order at
360  * the server, the one with the smaller FileLength will be processed last,
361  * possibly resulting in a bogus truncation.  The simplest way to avoid this
362  * is to serialize all StoreData RPC's.  This is the reason we defined
363  * CM_SCACHESYNC_STOREDATA_EXCL and CM_SCACHEFLAG_DATASTORING.
364  */
365 long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *up, cm_req_t *reqp,
366         long rights, long flags)
367 {
368         osi_queueData_t *qdp;
369         long code;
370         cm_buf_t *tbufp;
371         long outRights;
372         int bufLocked;
373
374         /* lookup this first */
375         bufLocked = flags & CM_SCACHESYNC_BUFLOCKED;
376
377         /* some minor assertions */
378         if (flags & (CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_FETCHDATA
379                         | CM_SCACHESYNC_READ | CM_SCACHESYNC_WRITE
380                         | CM_SCACHESYNC_SETSIZE)) {
381                 if (bufp) {
382                         osi_assert(bufp->refCount > 0);
383                         /*
384                         osi_assert(cm_FidCmp(&bufp->fid, &scp->fid) == 0);
385                          */
386                 }
387         }
388         else osi_assert(bufp == NULL);
389
390         /* Do the access check.  Now we don't really do the access check
391          * atomically, since the caller doesn't expect the parent dir to be
392          * returned locked, and that is what we'd have to do to prevent a
393          * callback breaking message on the parent due to a setacl call from
394          * being processed while we're running.  So, instead, we check things
395          * here, and if things look fine with the access, we proceed to finish
396          * the rest of this check.  Sort of a hack, but probably good enough.
397          */
398
399         while (1) {
400                 if (flags & CM_SCACHESYNC_FETCHSTATUS) {
401                         /* if we're bringing in a new status block, ensure that
402                          * we aren't already doing so, and that no one is
403                          * changing the status concurrently, either.  We need
404                          * to do this, even if the status is of a different
405                          * type, since we don't have the ability to figure out,
406                          * in the AFS 3 protocols, which status-changing
407                          * operation ran first, or even which order a read and
408                          * a write occurred in.
409                          */
410                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
411                                           | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK))
412                                 goto sleep;
413                 }
414                 if (flags & (CM_SCACHESYNC_STORESIZE | CM_SCACHESYNC_STORESTATUS
415                                 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_GETCALLBACK)) {
416                         /* if we're going to make an RPC to change the status, make sure
417                          * that no one is bringing in or sending out the status.
418                          */
419                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
420                                           | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK))
421                                 goto sleep;
422                         if (scp->bufReadsp || scp->bufWritesp) goto sleep;
423                 }
424                 if (flags & CM_SCACHESYNC_FETCHDATA) {
425                         /* if we're bringing in a new chunk of data, make sure that
426                          * nothing is happening to that chunk, and that we aren't
427                          * changing the basic file status info, either.
428                          */
429                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
430                                           | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK))
431                                 goto sleep;
432                         if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)))
433                                 goto sleep;
434                 }
435                 if (flags & CM_SCACHESYNC_STOREDATA) {
436                         /* same as fetch data */
437                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
438                                           | CM_SCACHEFLAG_SIZESTORING | CM_SCACHEFLAG_GETCALLBACK))
439                                 goto sleep;
440                         if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)))
441                                 goto sleep;
442                 }
443
444                 if (flags & CM_SCACHESYNC_STOREDATA_EXCL) {
445                         /* Don't allow concurrent StoreData RPC's */
446                         if (scp->flags & CM_SCACHEFLAG_DATASTORING)
447                                 goto sleep;
448                 }
449
450                 if (flags & CM_SCACHESYNC_ASYNCSTORE) {
451                         /* Don't allow more than one BKG store request */
452                         if (scp->flags & CM_SCACHEFLAG_ASYNCSTORING)
453                                 goto sleep;
454                 }
455
456                 if (flags & CM_SCACHESYNC_LOCK) {
457                         /* Don't allow concurrent fiddling with lock lists */
458                         if (scp->flags & CM_SCACHEFLAG_LOCKING)
459                                 goto sleep;
460                 }
461
462                 /* now the operations that don't correspond to making RPCs */
463                 if (flags & CM_SCACHESYNC_GETSTATUS) {
464                         /* we can use the status that's here, if we're not
465                          * bringing in new status.
466                          */
467                         if (scp->flags & (CM_SCACHEFLAG_FETCHING))
468                                 goto sleep;
469                 }
470                 if (flags & CM_SCACHESYNC_SETSTATUS) {
471                         /* we can make a change to the local status, as long as
472                          * the status isn't changing now.
473                          *
474                          * If we're fetching or storing a chunk of data, we can
475                          * change the status locally, since the fetch/store
476                          * operations don't change any of the data that we're
477                          * changing here.
478                          */
479                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
480                                           | CM_SCACHEFLAG_SIZESTORING))
481                                 goto sleep;
482                 }
483                 if (flags & CM_SCACHESYNC_READ) {
484                         /* we're going to read the data, make sure that the
485                          * status is available, and that the data is here.  It
486                          * is OK to read while storing the data back.
487                          */
488                         if (scp->flags & CM_SCACHEFLAG_FETCHING)
489                                 goto sleep;
490                         if (bufp && ((bufp->cmFlags
491                                          & (CM_BUF_CMFETCHING
492                                              | CM_BUF_CMFULLYFETCHED))
493                                         == CM_BUF_CMFETCHING))
494                                 goto sleep;
495                 }
496                 if (flags & CM_SCACHESYNC_WRITE) {
497                         /* don't write unless the status is stable and the chunk
498                          * is stable.
499                          */
500                         if (scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING
501                                           | CM_SCACHEFLAG_SIZESTORING))
502                                 goto sleep;
503                         if (bufp && (bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)))
504                                 goto sleep;
505                 }
506
507                 if (flags & CM_SCACHESYNC_NEEDCALLBACK) {
508                         if (!cm_HaveCallback(scp)) {
509                                 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp %x",
510                                         (long) scp);
511                                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
512                                 code = cm_GetCallback(scp, up, reqp, 0);
513                                 if (bufLocked) {
514                                         lock_ReleaseMutex(&scp->mx);
515                                         lock_ObtainMutex(&bufp->mx);
516                                         lock_ObtainMutex(&scp->mx);
517                                 }
518                                 if (code) return code;
519                                 continue;
520                         }
521                 }
522                 
523                 if (rights) {
524                         /* can't check access rights without a callback */
525                         osi_assert(flags & CM_SCACHESYNC_NEEDCALLBACK);
526
527                         if ((rights & PRSFS_WRITE) && (scp->flags & CM_SCACHEFLAG_RO))
528                                 return CM_ERROR_READONLY;
529
530                         if (cm_HaveAccessRights(scp, up, rights, &outRights)) {
531                                 if (~outRights & rights) return CM_ERROR_NOACCESS;
532                         }
533                         else {
534                                 /* we don't know the required access rights */
535                                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
536                                 code = cm_GetAccessRights(scp, up, reqp);
537                                 if (code) return code;
538                                 if (bufLocked) {
539                                         lock_ReleaseMutex(&scp->mx);
540                                         lock_ObtainMutex(&bufp->mx);
541                                         lock_ObtainMutex(&scp->mx);
542                                 }
543                                 continue;
544                         }
545                 }
546
547                 /* if we get here, we're happy */
548                 break;
549
550 sleep:
551                 /* first check if we're not supposed to wait: fail 
552                  * in this case, returning with everything still locked.
553                  */
554                 if (flags & CM_SCACHESYNC_NOWAIT) return CM_ERROR_WOULDBLOCK;
555
556                 /* wait here, then try again */
557                 osi_Log1(afsd_logp, "CM SyncOp sleeping scp %x", (long) scp);
558                 scp->flags |= CM_SCACHEFLAG_WAITING;
559                 if (bufLocked) lock_ReleaseMutex(&bufp->mx);
560                 osi_SleepM((long) &scp->flags, &scp->mx);
561                 osi_Log0(afsd_logp, "CM SyncOp woke!");
562                 if (bufLocked) lock_ObtainMutex(&bufp->mx);
563                 lock_ObtainMutex(&scp->mx);
564         } /* big while loop */
565         
566         /* now, update the recorded state for RPC-type calls */
567         if (flags & CM_SCACHESYNC_FETCHSTATUS)
568                 scp->flags |= CM_SCACHEFLAG_FETCHING;
569         if (flags & CM_SCACHESYNC_STORESTATUS)
570                 scp->flags |= CM_SCACHEFLAG_STORING;
571         if (flags & CM_SCACHESYNC_STORESIZE)
572                 scp->flags |= CM_SCACHEFLAG_SIZESTORING;
573         if (flags & CM_SCACHESYNC_GETCALLBACK)
574                 scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
575         if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
576                 scp->flags |= CM_SCACHEFLAG_DATASTORING;
577         if (flags & CM_SCACHESYNC_ASYNCSTORE)
578                 scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
579         if (flags & CM_SCACHESYNC_LOCK)
580                 scp->flags |= CM_SCACHEFLAG_LOCKING;
581
582         /* now update the buffer pointer */
583         if (flags & CM_SCACHESYNC_FETCHDATA) {
584                 /* ensure that the buffer isn't already in the I/O list */
585                 if (bufp) {
586                         for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
587                                 tbufp = osi_GetQData(qdp);
588                                 osi_assert(tbufp != bufp);
589                         }
590                 }
591                 
592                 /* queue a held reference to the buffer in the "reading" I/O list */
593                 qdp = osi_QDAlloc();
594                 osi_SetQData(qdp, bufp);
595                 if (bufp) {
596                         buf_Hold(bufp);
597                         bufp->cmFlags |= CM_BUF_CMFETCHING;
598                 }
599                 osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
600         }
601
602         if (flags & CM_SCACHESYNC_STOREDATA) {
603                 /* ensure that the buffer isn't already in the I/O list */
604                 if (bufp) {
605                         for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
606                                 tbufp = osi_GetQData(qdp);
607                                 osi_assert(tbufp != bufp);
608                         }
609                 }
610                 
611                 /* queue a held reference to the buffer in the "writing" I/O list */
612                 qdp = osi_QDAlloc();
613                 osi_SetQData(qdp, bufp);
614                 if (bufp) {
615                         buf_Hold(bufp);
616                         bufp->cmFlags |= CM_BUF_CMSTORING;
617                 }
618                 osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
619         }
620         
621         return 0;
622 }
623
624 /* for those syncops that setup for RPCs.
625  * Called with scache locked.
626  */
627 void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, long flags)
628 {
629         osi_queueData_t *qdp;
630         cm_buf_t *tbufp;
631
632         /* now, update the recorded state for RPC-type calls */
633         if (flags & CM_SCACHESYNC_FETCHSTATUS)
634                 scp->flags &= ~CM_SCACHEFLAG_FETCHING;
635         if (flags & CM_SCACHESYNC_STORESTATUS)
636                 scp->flags &= ~CM_SCACHEFLAG_STORING;
637         if (flags & CM_SCACHESYNC_STORESIZE)
638                 scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
639         if (flags & CM_SCACHESYNC_GETCALLBACK)
640                 scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
641         if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
642                 scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
643         if (flags & CM_SCACHESYNC_ASYNCSTORE)
644                 scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
645         if (flags & CM_SCACHESYNC_LOCK)
646                 scp->flags &= ~CM_SCACHEFLAG_LOCKING;
647
648         /* now update the buffer pointer */
649         if (flags & CM_SCACHESYNC_FETCHDATA) {
650                 /* ensure that the buffer isn't already in the I/O list */
651                 for(qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
652                         tbufp = osi_GetQData(qdp);
653                         if (tbufp == bufp) break;
654                 }
655                 osi_assert(qdp != NULL);
656                 osi_assert(osi_GetQData(qdp) == bufp);
657                 osi_QRemove((osi_queue_t **) &scp->bufReadsp, &qdp->q);
658                 osi_QDFree(qdp);
659                 if (bufp) {
660                         bufp->cmFlags &=
661                           ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
662                         buf_Release(bufp);
663                 }
664         }
665
666         /* now update the buffer pointer */
667         if (flags & CM_SCACHESYNC_STOREDATA) {
668                 /* ensure that the buffer isn't already in the I/O list */
669                 for(qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
670                         tbufp = osi_GetQData(qdp);
671                         if (tbufp == bufp) break;
672                 }
673                 osi_assert(qdp != NULL);
674                 osi_assert(osi_GetQData(qdp) == bufp);
675                 osi_QRemove((osi_queue_t **) &scp->bufWritesp, &qdp->q);
676                 osi_QDFree(qdp);
677                 if (bufp) {
678                         bufp->cmFlags &= ~CM_BUF_CMSTORING;
679                         buf_Release(bufp);
680                 }
681         }
682         
683         /* and wakeup anyone who is waiting */
684         if (scp->flags & CM_SCACHEFLAG_WAITING) {
685                 scp->flags &= ~CM_SCACHEFLAG_WAITING;
686                 osi_Wakeup((long) &scp->flags);
687         }
688 }
689
690 /* merge in a response from an RPC.  The scp must be locked, and the callback
691  * is optional.
692  *
693  * Don't overwrite any status info that is dirty, since we could have a store
694  * operation (such as store data) that merges some info in, and we don't want
695  * to lose the local updates.  Typically, there aren't many updates we do
696  * locally, anyway, probably only mtime.
697  *
698  * There is probably a bug in here where a chmod (which doesn't change
699  * serverModTime) that occurs between two fetches, both of whose responses are
700  * handled after the callback breaking is done, but only one of whose calls
701  * started before that, can cause old info to be merged from the first call.
702  */
703 void cm_MergeStatus(cm_scache_t *scp, AFSFetchStatus *statusp, AFSVolSync *volp,
704         cm_user_t *userp, int flags)
705 {
706         if (!(flags & CM_MERGEFLAG_FORCE)
707                 && statusp->DataVersion < (unsigned long) scp->dataVersion) {
708                 struct cm_cell *cellp;
709                 struct cm_volume *volp;
710
711                 cellp = cm_FindCellByID(scp->fid.cell);
712                 cm_GetVolumeByID(cellp, scp->fid.volume, userp,
713                                  (cm_req_t *) NULL, &volp);
714                 if (scp->cbServerp)
715                         osi_Log2(afsd_logp, "old data from server %x volume %s",
716                                  scp->cbServerp->addr.sin_addr.s_addr,
717                                  volp->namep);
718                 osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
719                          scp, scp->dataVersion, statusp->DataVersion);
720                 /* we have a number of data fetch/store operations running
721                  * concurrently, and we can tell which one executed last at the
722                  * server by its mtime.
723                  * Choose the one with the largest mtime, and ignore the rest.
724                  *
725                  * These concurrent calls are incompatible with setting the
726                  * mtime, so we won't have a locally changed mtime here.
727                  *
728                  * We could also have ACL info for a different user than usual,
729                  * in which case we have to do that part of the merge, anyway.
730                  * We won't have to worry about the info being old, since we
731                  * won't have concurrent calls
732                  * that change file status running from this machine.
733                  *
734                  * Added 3/17/98:  if we see data version regression on an RO
735                  * file, it's probably due to a server holding an out-of-date
736                  * replica, rather than to concurrent RPC's.  Failures to
737                  * release replicas are now flagged by the volserver, but only
738                  * since AFS 3.4 5.22, so there are plenty of clients getting
739                  * out-of-date replicas out there.
740                  *
741                  * If we discover an out-of-date replica, by this time it's too
742                  * late to go to another server and retry.  Also, we can't
743                  * reject the merge, because then there is no way for
744                  * GetAccess to do its work, and the caller gets into an
745                  * infinite loop.  So we just grin and bear it.
746                  */
747                 if (!(scp->flags & CM_SCACHEFLAG_RO))
748                         return;
749         }
750         scp->serverModTime = statusp->ServerModTime;
751
752         if (!(scp->mask & CM_SCACHEMASK_CLIENTMODTIME)) {
753                 scp->clientModTime = statusp->ClientModTime;
754         }
755         if (!(scp->mask & CM_SCACHEMASK_LENGTH)) {
756                 scp->length.LowPart = statusp->Length;
757                 scp->length.HighPart = 0;
758         }
759
760         scp->serverLength.LowPart = statusp->Length;
761         scp->serverLength.HighPart = 0;
762
763         scp->linkCount = statusp->LinkCount;
764         scp->dataVersion = statusp->DataVersion;
765         scp->owner = statusp->Owner;
766         scp->group = statusp->Group;
767         scp->unixModeBits = statusp->UnixModeBits & 07777;
768         
769         if (statusp->FileType == File)
770                 scp->fileType = CM_SCACHETYPE_FILE;
771         else if (statusp->FileType == Directory)
772                 scp->fileType = CM_SCACHETYPE_DIRECTORY;
773         else if (statusp->FileType == SymbolicLink) {
774                 if ((scp->unixModeBits & 0111) == 0)
775                         scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
776                 else
777                         scp->fileType = CM_SCACHETYPE_SYMLINK;
778         }
779         else scp->fileType = 0; /* invalid */
780
781         /* and other stuff */
782         scp->parentVnode = statusp->ParentVnode;
783         scp->parentUnique = statusp->ParentUnique;
784         
785         /* and merge in the private acl cache info, if this is more than the public
786          * info; merge in the public stuff in any case.
787          */
788         scp->anyAccess = statusp->AnonymousAccess;
789
790         if (userp != NULL) {
791                 cm_AddACLCache(scp, userp, statusp->CallerAccess);
792         }
793 }
794
795 /* note that our stat cache info is incorrect, so force us eventually
796  * to stat the file again.  There may be dirty data associated with
797  * this vnode, and we want to preserve that information.
798  *
799  * This function works by simply simulating a loss of the callback.
800  *
801  * This function must be called with the scache locked.
802  */
803 void cm_DiscardSCache(cm_scache_t *scp)
804 {
805         lock_AssertMutex(&scp->mx);
806         scp->cbServerp = NULL;
807         scp->cbExpires = 0;
808         cm_dnlcPurgedp(scp);
809         cm_FreeAllACLEnts(scp);
810 }
811
812 void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
813 {
814         afsFidp->Volume = fidp->volume;
815         afsFidp->Vnode = fidp->vnode;
816         afsFidp->Unique = fidp->unique;
817 }
818
819 void cm_HoldSCache(cm_scache_t *scp)
820 {
821         lock_ObtainWrite(&cm_scacheLock);
822         osi_assert(scp->refCount > 0);
823         scp->refCount++;
824         lock_ReleaseWrite(&cm_scacheLock);
825 }
826
827 void cm_ReleaseSCache(cm_scache_t *scp)
828 {
829         lock_ObtainWrite(&cm_scacheLock);
830         osi_assert(scp->refCount-- > 0);
831         lock_ReleaseWrite(&cm_scacheLock);
832 }
833
834 /* just look for the scp entry to get filetype */
835 /* doesn't need to be perfectly accurate, so locking doesn't matter too much */
836 int cm_FindFileType(cm_fid_t *fidp)
837 {
838         long hash;
839         cm_scache_t *scp;
840         
841         hash = CM_SCACHE_HASH(fidp);
842         
843         osi_assert(fidp->cell != 0);
844
845         lock_ObtainWrite(&cm_scacheLock);
846         for(scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
847                 if (cm_FidCmp(fidp, &scp->fid) == 0) {
848                   /*scp->refCount++;*/
849                   /*cm_AdjustLRU(scp);*/
850                   lock_ReleaseWrite(&cm_scacheLock);
851                   return scp->fileType;
852                 }
853         }
854         lock_ReleaseWrite(&cm_scacheLock);
855         return NULL;
856 }