14dac4286f7aac7a3c558f3da24175e14f3675fd
[openafs.git] / src / WINNT / afsd / cm_callback.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/afs_args.h>
12 #include <afs/stds.h>
13
14 #ifndef DJGPP
15 #include <windows.h>
16 #include <winsock2.h>
17 #else
18 #include <sys/socket.h>
19 #endif /* !DJGPP */
20 #include <malloc.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <osi.h>
25
26 #include "afsd.h"
27
28 /*extern void afsi_log(char *pattern, ...);*/
29
30 /* read/write lock for all global storage in this module */
31 osi_rwlock_t cm_callbackLock;
32
33 #ifdef AFS_FREELANCE_CLIENT
34 extern osi_mutex_t cm_Freelance_Lock;
35 #endif
36
37 /* count of # of callback breaking messages received by this CM so far.  We use
38  * this count in determining whether there have been any callback breaks that
39  * apply to a call that returned a new callback.  If the counter doesn't
40  * increase during a call, then we know that no callbacks were broken during
41  * that call, and thus that the callback that was just returned is still valid.
42  */
43 long cm_callbackCount;
44
45 /* count of number of RPCs potentially returning a callback executing now.
46  * When this counter hits zero, we can clear out the racing revokes list, since
47  * at that time, we know that none of the just-executed callback revokes will
48  * apply to any future call that returns a callback (since the latter hasn't
49  * even started execution yet).
50  */
51 long cm_activeCallbackGrantingCalls;
52
53 /* list of callbacks that have been broken recently.  If a call returning a
54  * callback is executing and a callback revoke runs immediately after it at the
55  * server, the revoke may end up being processed before the response to the
56  * original callback granting call.  We detect this by keeping a list of
57  * callback revokes that have been received since we *started* the callback
58  * granting call, and discarding any callbacks received for the same file ID,
59  * even if the callback revoke was received before the callback grant.
60  */
61 cm_racingRevokes_t *cm_racingRevokesp;
62
63 /* record a (potentially) racing revoke for this file ID; null means for all
64  * file IDs, and is used by InitCallBackState.
65  *
66  * The cancelFlags describe whether we're just discarding callbacks for the same
67  * file ID, the same volume, or all from the same server.
68  *
69  * Called with no locks held.
70  */
71 void cm_RecordRacingRevoke(cm_fid_t *fidp, long cancelFlags)
72 {
73     cm_racingRevokes_t *rp;
74
75     lock_ObtainWrite(&cm_callbackLock);
76
77     osi_Log3(afsd_logp, "RecordRacingRevoke Volume %d Flags %lX activeCalls %d",
78              fidp->volume, cancelFlags, cm_activeCallbackGrantingCalls);
79
80     if (cm_activeCallbackGrantingCalls > 0) {
81         rp = malloc(sizeof(*rp));
82         memset(rp, 0, sizeof(*rp));
83         osi_QAdd((osi_queue_t **) &cm_racingRevokesp, &rp->q);
84         rp->flags |= (cancelFlags & CM_RACINGFLAG_ALL);
85         if (fidp) rp->fid = *fidp;
86         rp->callbackCount = ++cm_callbackCount;
87     }
88     lock_ReleaseWrite(&cm_callbackLock);
89 }
90
91 /*
92  * When we lose a callback, may have to send change notification replies.
93  * Do not call with a lock on the scp.
94  */
95 void cm_CallbackNotifyChange(cm_scache_t *scp)
96 {
97     osi_Log2(afsd_logp, "CallbackNotifyChange FileType %d Flags %lX",
98               scp->fileType, scp->flags);
99
100     if (scp->fileType == CM_SCACHETYPE_DIRECTORY) {
101         if (scp->flags & CM_SCACHEFLAG_ANYWATCH)
102             smb_NotifyChange(0,
103                              FILE_NOTIFY_GENERIC_DIRECTORY_FILTER,
104                              scp, NULL, NULL, TRUE);
105     } else {
106         cm_fid_t tfid;
107         cm_scache_t *dscp;
108
109         tfid.cell = scp->fid.cell;
110         tfid.volume = scp->fid.volume;
111         tfid.vnode = scp->parentVnode;
112         tfid.unique = scp->parentUnique;
113         dscp = cm_FindSCache(&tfid);
114         if (dscp &&
115              dscp->flags & CM_SCACHEFLAG_ANYWATCH)
116             smb_NotifyChange( 0,
117                               FILE_NOTIFY_GENERIC_FILE_FILTER,
118                               dscp,   NULL, NULL, TRUE);
119         if (dscp) cm_ReleaseSCache(dscp);
120     }
121 }
122
123 /* called with no locks held for every file ID that is revoked directly by
124  * a callback revoke call.  Does not have to handle volume callback breaks,
125  * since those have already been split out.
126  *
127  * The callp parameter is currently unused.
128  */
129 void cm_RevokeCallback(struct rx_call *callp, AFSFid *fidp)
130 {
131     cm_fid_t tfid;
132     cm_scache_t *scp;
133     long hash;
134         
135     /* don't bother setting cell, since we won't be checking it (to aid
136      * in working with multi-homed servers: we don't know the cell if we
137      * don't recognize the IP address).
138      */
139     tfid.cell = 0;
140     tfid.volume = fidp->Volume;
141     tfid.vnode = fidp->Vnode;
142     tfid.unique = fidp->Unique;
143     hash = CM_SCACHE_HASH(&tfid);
144
145     osi_Log3(afsd_logp, "RevokeCallback vol %d vn %d un %d",
146              fidp->Volume, fidp->Vnode, fidp->Unique);
147         
148     /* do this first, so that if we're executing a callback granting call
149      * at this moment, we kill it before it can be merged in.  Otherwise,
150      * it could complete while we're doing the scan below, and get missed
151      * by both the scan and by this code.
152      */
153     cm_RecordRacingRevoke(&tfid, 0);
154
155     lock_ObtainWrite(&cm_scacheLock);
156     /* do all in the hash bucket, since we don't know how many we'll find with
157      * varying cells.
158      */
159     for (scp = cm_hashTablep[hash]; scp; scp=scp->nextp) {
160         if (scp->fid.volume == tfid.volume &&
161              scp->fid.vnode == tfid.vnode &&
162              scp->fid.unique == tfid.unique) {
163             scp->refCount++;
164             lock_ReleaseWrite(&cm_scacheLock);
165             osi_Log1(afsd_logp, "Discarding SCache scp %x", scp);
166             lock_ObtainMutex(&scp->mx);
167             cm_DiscardSCache(scp);
168             lock_ReleaseMutex(&scp->mx);
169             cm_CallbackNotifyChange(scp);
170             lock_ObtainWrite(&cm_scacheLock);
171             scp->refCount--;
172         }
173     }
174     lock_ReleaseWrite(&cm_scacheLock);
175 }
176
177 /* called to revoke a volume callback, which is typically issued when a volume
178  * is moved from one server to another.
179  *
180  * Called with no locks held.
181  */
182 void cm_RevokeVolumeCallback(struct rx_call *callp, AFSFid *fidp)
183 {
184     long hash;
185     cm_scache_t *scp;
186     cm_fid_t tfid;
187
188     osi_Log1(afsd_logp, "RevokeVolumeCallback %d", fidp->Volume);
189
190     /* do this first, so that if we're executing a callback granting call
191      * at this moment, we kill it before it can be merged in.  Otherwise,
192      * it could complete while we're doing the scan below, and get missed
193      * by both the scan and by this code.
194      */
195     tfid.cell = tfid.vnode = tfid.unique = 0;
196     tfid.volume = fidp->Volume;
197     cm_RecordRacingRevoke(&tfid, CM_RACINGFLAG_CANCELVOL);
198
199
200     lock_ObtainWrite(&cm_scacheLock);
201     for (hash = 0; hash < cm_hashTableSize; hash++) {
202         for(scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
203             if (scp->fid.volume == fidp->Volume) {
204                 scp->refCount++;
205                 lock_ReleaseWrite(&cm_scacheLock);
206                 lock_ObtainMutex(&scp->mx);
207                 osi_Log1(afsd_logp, "Discarding SCache scp %x", scp);
208                 cm_DiscardSCache(scp);
209                 lock_ReleaseMutex(&scp->mx);
210                 cm_CallbackNotifyChange(scp);
211                 lock_ObtainWrite(&cm_scacheLock);
212                 scp->refCount--;
213             }
214         }       /* search one hash bucket */
215     }   /* search all hash buckets */
216
217     lock_ReleaseWrite(&cm_scacheLock);
218 }
219
220 /* handle incoming RPC callback breaking message.
221  * Called with no locks held.
222  */
223 SRXAFSCB_CallBack(struct rx_call *callp, AFSCBFids *fidsArrayp, AFSCBs *cbsArrayp)
224 {
225     int i;
226     AFSFid *tfidp;
227         
228     osi_Log0(afsd_logp, "SRXAFSCB_CallBack");
229
230     for (i=0; i < (long) fidsArrayp->AFSCBFids_len; i++) {
231         tfidp = &fidsArrayp->AFSCBFids_val[i];
232                 
233         if (tfidp->Volume == 0)
234             continue;   /* means don't do anything */
235         else if (tfidp->Vnode == 0)
236             cm_RevokeVolumeCallback(callp, tfidp);
237         else
238             cm_RevokeCallback(callp, tfidp);
239     }
240
241     return 0;
242 }
243
244 /* called with no locks by RPC system when a server indicates that it has never
245  * heard from us, or for other reasons has had to discard callbacks from us
246  * without telling us, e.g. a network partition.
247  */
248 SRXAFSCB_InitCallBackState(struct rx_call *callp)
249 {
250     struct sockaddr_in taddr;
251     cm_server_t *tsp;
252     cm_scache_t *scp;
253     int hash;
254     int discarded;
255
256     osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState");
257
258     if ((rx_ConnectionOf(callp)) && (rx_PeerOf(rx_ConnectionOf(callp)))) {
259         taddr.sin_family = AF_INET;
260         taddr.sin_addr.s_addr = rx_HostOf(rx_PeerOf(rx_ConnectionOf(callp)));
261
262         tsp = cm_FindServer(&taddr, CM_SERVER_FILE);
263
264         osi_Log1(afsd_logp, "Init Callback State server %x", tsp);
265         
266         /* record the callback in the racing revokes structure.  This
267          * shouldn't be necessary, since we shouldn't be making callback
268          * granting calls while we're going to get an initstate call,
269          * but there probably are some obscure races, so better safe
270          * than sorry.
271          *
272          * We do this first since we don't hold the cm_scacheLock and vnode
273          * locks over the entire callback scan operation below.  The
274          * big loop below is guaranteed to hit any callback already
275          * processed.  The call to RecordRacingRevoke is guaranteed
276          * to kill any callback that is currently being returned.
277          * Anything that sneaks past both must start
278          * after the call to RecordRacingRevoke.
279          */
280         cm_RecordRacingRevoke(NULL, CM_RACINGFLAG_CANCELALL);
281         
282         /* now search all vnodes looking for guys with this callback, if we
283          * found it, or guys with any callbacks, if we didn't find the server
284          * (that's how multihomed machines will appear and how we'll handle
285          * them, albeit a little inefficiently).  That is, we're discarding all
286          * callbacks from all hosts if we get an initstate call from an unknown
287          * host.  Since these calls are rare, and multihomed servers
288          * are "rare," hopefully this won't be a problem.
289          */
290         lock_ObtainWrite(&cm_scacheLock);
291         for (hash = 0; hash < cm_hashTableSize; hash++) {
292             for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
293                 scp->refCount++;
294                 lock_ReleaseWrite(&cm_scacheLock);
295                 lock_ObtainMutex(&scp->mx);
296                 discarded = 0;
297                 if (scp->cbServerp != NULL) {
298                     /* we have a callback, now decide if we should clear it */
299                     if (scp->cbServerp == tsp || tsp == NULL) {
300                         osi_Log1(afsd_logp, "Discarding SCache scp %x", scp);
301                         cm_DiscardSCache(scp);
302                         discarded = 1;
303                     }
304                 }
305                 lock_ReleaseMutex(&scp->mx);
306                 if (discarded)
307                     cm_CallbackNotifyChange(scp);
308                 lock_ObtainWrite(&cm_scacheLock);
309                 scp->refCount--;
310             }   /* search one hash bucket */
311         }       /* search all hash buckets */
312         
313         lock_ReleaseWrite(&cm_scacheLock);
314         
315         /* we're done with the server structure */
316         if (tsp) 
317             cm_PutServer(tsp);
318     }
319
320     return 0;
321 }
322
323 /* just returns if we're up */
324 SRXAFSCB_Probe(struct rx_call *callp)
325 {
326     osi_Log0(afsd_logp, "SRXAFSCB_Probe - not implemented");
327     return 0;
328 }
329
330 /* debug interface: not implemented */
331 SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
332 {
333     /* XXXX */
334     osi_Log0(afsd_logp, "SRXAFSCB_GetCE64 - not implemented");
335     return RXGEN_OPCODE;
336 }
337
338 /* debug interface: not implemented */
339 SRXAFSCB_GetLock(struct rx_call *callp, long index, AFSDBLock *lockp)
340 {
341     /* XXXX */
342     osi_Log0(afsd_logp, "SRXAFSCB_GetLock - not implemented");
343     return RXGEN_OPCODE;
344 }
345
346 /* debug interface: not implemented */
347 SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
348 {
349     /* XXXX */
350     osi_Log0(afsd_logp, "SRXAFSCB_GetCE - not implemented");
351     return RXGEN_OPCODE;
352 }
353
354 /* debug interface: not implemented */
355 SRXAFSCB_XStatsVersion(struct rx_call *callp, long *vp)
356 {
357     /* XXXX */
358     osi_Log0(afsd_logp, "SRXAFSCB_XStatsVersion - not implemented");
359     *vp = -1;
360     return RXGEN_OPCODE;
361 }
362
363 /* debug interface: not implemented */
364 SRXAFSCB_GetXStats(struct rx_call *callp, long cvn, long coln, long *srvp, long *timep,
365         AFSCB_CollData *datap)
366 {
367     /* XXXX */
368     osi_Log0(afsd_logp, "SRXAFSCB_GetXStats - not implemented");
369     return RXGEN_OPCODE;
370 }
371
372 /* debug interface: not implemented */
373 SRXAFSCB_InitCallBackState2(struct rx_call *callp, struct interfaceAddr* addr)
374 {
375     /* XXXX */
376     osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState2 - not implemented");
377     return RXGEN_OPCODE;
378 }
379
380 /* debug interface: not implemented */
381 SRXAFSCB_WhoAreYou(struct rx_call *callp, struct interfaceAddr* addr)
382 {
383     /* XXXX */
384     osi_Log0(afsd_logp, "SRXAFSCB_WhoAreYou - not implemented");
385     return RXGEN_OPCODE;
386 }
387
388 /* debug interface: not implemented */
389 SRXAFSCB_InitCallBackState3(struct rx_call *callp, afsUUID* serverUuid)
390 {
391     /* XXXX */
392     osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState3 - not implemented");
393     return RXGEN_OPCODE;
394 }
395
396 /* debug interface: not implemented */
397 SRXAFSCB_ProbeUuid(struct rx_call *callp, afsUUID* clientUuid)
398 {
399     /* XXXX */
400     osi_Log0(afsd_logp, "SRXAFSCB_ProbeUuid - not implemented");
401     return RXGEN_OPCODE;
402 }
403
404 /*------------------------------------------------------------------------
405  * EXPORTED SRXAFSCB_GetServerPrefs
406  *
407  * Description:
408  *      Routine to list server preferences used by this client.
409  *
410  * Arguments:
411  *      a_call  : Ptr to Rx call on which this request came in.
412  *      a_index : Input server index
413  *      a_srvr_addr  : Output server address (0xffffffff on last server)
414  *      a_srvr_rank  : Output server rank
415  *
416  * Returns:
417  *      0 on success
418  *
419  * Environment:
420  *      Nothing interesting.
421  *
422  * Side Effects:
423  *      As advertised.
424  *------------------------------------------------------------------------*/
425
426 int SRXAFSCB_GetServerPrefs(
427     struct rx_call *a_call,
428     afs_int32 a_index,
429     afs_int32 *a_srvr_addr,
430     afs_int32 *a_srvr_rank)
431 {
432     osi_Log0(afsd_logp, "SRXAFSCB_GetServerPrefs - not implemented");
433
434     *a_srvr_addr = 0xffffffff;
435     *a_srvr_rank = 0xffffffff;
436     return 0;
437 }
438
439 /*------------------------------------------------------------------------
440  * EXPORTED SRXAFSCB_GetCellServDB
441  *
442  * Description:
443  *      Routine to list cells configured for this client
444  *
445  * Arguments:
446  *      a_call  : Ptr to Rx call on which this request came in.
447  *      a_index : Input cell index
448  *      a_name  : Output cell name ("" on last cell)
449  *      a_hosts : Output cell database servers
450  *
451  * Returns:
452  *      0 on success
453  *
454  * Environment:
455  *      Nothing interesting.
456  *
457  * Side Effects:
458  *      As advertised.
459  *------------------------------------------------------------------------*/
460
461 int SRXAFSCB_GetCellServDB(
462     struct rx_call *a_call,
463     afs_int32 a_index,
464     char **a_name,
465     serverList *a_hosts)
466 {
467     char *t_name;
468
469     osi_Log0(afsd_logp, "SRXAFSCB_GetCellServDB - not implemented");
470
471     t_name = (char *)malloc(AFSNAMEMAX);
472     t_name[0] = '\0';
473     *a_name = t_name;
474     a_hosts->serverList_len = 0;
475     return 0;
476 }
477
478 /*------------------------------------------------------------------------
479  * EXPORTED SRXAFSCB_GetLocalCell
480  *
481  * Description:
482  *      Routine to return name of client's local cell
483  *
484  * Arguments:
485  *      a_call  : Ptr to Rx call on which this request came in.
486  *      a_name  : Output cell name
487  *
488  * Returns:
489  *      0 on success
490  *
491  * Environment:
492  *      Nothing interesting.
493  *
494  * Side Effects:
495  *      As advertised.
496  *------------------------------------------------------------------------*/
497
498 int SRXAFSCB_GetLocalCell(
499     struct rx_call *a_call,
500     char **a_name)
501 {
502     char *t_name;
503
504     osi_Log0(afsd_logp, "SRXAFSCB_GetLocalCell");
505
506     if (cm_rootCellp) {
507         t_name = (char *)malloc(strlen(cm_rootCellp->namep)+1);
508         strcpy(t_name, cm_rootCellp->namep);
509     } else {
510         t_name = (char *)malloc(1);
511         t_name[0] = '\0';
512     }
513     *a_name = t_name;
514     return 0;
515 }
516
517
518 /*
519  * afs_MarshallCacheConfig - marshall client cache configuration
520  *
521  * PARAMETERS
522  *
523  * IN callerVersion - the rpc stat version of the caller.
524  *
525  * IN config - client cache configuration.
526  *
527  * OUT ptr - buffer where configuration is marshalled.
528  *
529  * RETURN CODES
530  *
531  * Returns void.
532  */
533 static void afs_MarshallCacheConfig(
534     afs_uint32 callerVersion,
535     cm_initparams_v1 *config,
536     afs_uint32 *ptr)
537 {
538     /*
539      * We currently only support version 1.
540      */
541     *(ptr++) = config->nChunkFiles;
542     *(ptr++) = config->nStatCaches;
543     *(ptr++) = config->nDataCaches;
544     *(ptr++) = config->nVolumeCaches;
545     *(ptr++) = config->firstChunkSize;
546     *(ptr++) = config->otherChunkSize;
547     *(ptr++) = config->cacheSize;
548     *(ptr++) = config->setTime;
549     *(ptr++) = config->memCache;
550
551 }
552  
553
554 /*------------------------------------------------------------------------
555  * EXPORTED SRXAFSCB_GetCacheConfig
556  *
557  * Description:
558  *      Routine to return parameters used to initialize client cache.
559  *      Client may request any format version. Server may not return
560  *      format version greater than version requested by client.
561  *
562  * Arguments:
563  *      a_call:        Ptr to Rx call on which this request came in.
564  *      callerVersion: Data format version desired by the client.
565  *      serverVersion: Data format version of output data.
566  *      configCount:   Number bytes allocated for output data.
567  *      config:        Client cache configuration.
568  *
569  * Returns:
570  *      0 on success
571  *
572  * Environment:
573  *      Nothing interesting.
574  *
575  * Side Effects:
576  *      As advertised.
577  *------------------------------------------------------------------------*/
578
579 int SRXAFSCB_GetCacheConfig(a_call, callerVersion, serverVersion,
580                             configCount, config)
581 struct rx_call *a_call;
582 afs_uint32 callerVersion;
583 afs_uint32 *serverVersion;
584 afs_uint32 *configCount;
585 cacheConfig *config;
586 {
587     afs_uint32 *t_config;
588     size_t allocsize;
589     extern cm_initparams_v1 cm_initParams;
590
591     osi_Log0(afsd_logp, "SRXAFSCB_GetCacheConfig - version 1 only");
592
593     /*
594      * Currently only support version 1
595      */
596     allocsize = sizeof(cm_initparams_v1);
597     t_config = (afs_uint32 *)malloc(allocsize);
598
599     afs_MarshallCacheConfig(callerVersion, &cm_initParams, t_config);
600
601     *serverVersion = AFS_CLIENT_RETRIEVAL_FIRST_EDITION;
602     *configCount = allocsize;
603     config->cacheConfig_val = t_config;
604     config->cacheConfig_len = allocsize/sizeof(afs_uint32);
605
606     return 0;
607 }
608
609 /* called by afsd without any locks to initialize this module */
610 void cm_InitCallback(void)
611 {
612     lock_InitializeRWLock(&cm_callbackLock, "cm_callbackLock");
613     cm_activeCallbackGrantingCalls = 0;
614 }
615
616 /* called with locked scp; tells us whether we've got a callback.
617  * Expirations are checked by a background daemon so as to make
618  * this function as inexpensive as possible
619  */
620 int cm_HaveCallback(cm_scache_t *scp)
621 {
622 #ifdef AFS_FREELANCE_CLIENT
623     // yj: we handle callbacks specially for callbacks on the root directory
624     // Since it's local, we almost always say that we have callback on it
625     // The only time we send back a 0 is if we're need to initialize or
626     // reinitialize the fake directory
627
628     // There are 2 state variables cm_fakeGettingCallback and cm_fakeDirCallback
629     // cm_fakeGettingCallback is 1 if we're in the process of initialization and
630     // hence should return false. it's 0 otherwise
631     // cm_fakeDirCallback is 0 if we haven't loaded the fake directory, it's 1
632     // if the fake directory is loaded and this is the first time cm_HaveCallback
633     // is called since then. We return false in this case to allow cm_GetCallback
634     // to be called because cm_GetCallback has some initialization work to do.
635     // If cm_fakeDirCallback is 2, then it means that the fake directory is in
636     // good shape and we simply return true, provided no change is detected.
637     int fdc, fgc;
638
639     if (cm_freelanceEnabled && 
640          scp->fid.cell==AFS_FAKE_ROOT_CELL_ID && scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
641         /* if it's something on /afs */
642         if (!(scp->fid.vnode==0x1 && scp->fid.unique==0x1)) {
643             /* if it's not root.afs */
644             return 1;
645         }
646
647             lock_ObtainMutex(&cm_Freelance_Lock);
648             fdc = cm_fakeDirCallback;
649             fgc = cm_fakeGettingCallback;
650             lock_ReleaseMutex(&cm_Freelance_Lock);
651             
652             if (fdc==1) {       // first call since init
653                 return 0;
654             } else if (fdc==2 && !fgc) {        // we're in good shape
655                 if (cm_getLocalMountPointChange()) {    // check for changes
656                     cm_clearLocalMountPointChange(); // clear the changefile
657             lock_ReleaseMutex(&scp->mx);      // this is re-locked in reInitLocalMountPoints
658                     cm_reInitLocalMountPoints();        // start reinit
659             lock_ObtainMutex(&scp->mx);      // now get the lock back 
660                     return 0;
661                 }
662                 return 1;                       // no change
663             }
664             return 0;
665         }
666 #endif
667
668     if (scp->cbServerp != NULL)
669         return 1;
670     else 
671         return 0;
672 }
673
674 /* need to detect a broken callback that races with our obtaining a callback.
675  * Need to be able to do this even if we don't know the file ID of the file
676  * we're breaking the callback on at the time we start the acquisition of the
677  * callback (as in the case where we are creating a file).
678  *
679  * So, we start by writing down the count of the # of callbacks we've received
680  * so far, and bumping a global counter of the # of callback granting calls
681  * outstanding (all done under cm_callbackLock).
682  *
683  * When we're back from the call, we look at all of the callback revokes with
684  * counter numbers greater than the one we recorded in our caller's structure,
685  * and replay those that are higher than when we started the call.
686  * 
687  * We free all the structures in the queue when the count of the # of outstanding
688  * callback-granting calls drops to zero.
689  *
690  * We call this function with the scp locked, too, but in its current implementation,
691  * this knowledge is not used.
692  */
693 void cm_StartCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp)
694 {
695     lock_ObtainWrite(&cm_callbackLock);
696     cbrp->callbackCount = cm_callbackCount;
697     cm_activeCallbackGrantingCalls++;
698     cbrp->startTime = osi_Time();
699     cbrp->serverp = NULL;
700     lock_ReleaseWrite(&cm_callbackLock);
701 }
702
703 /* Called at the end of a callback-granting call, to remove the callback
704  * info from the scache entry, if necessary.
705  *
706  * Called with scp locked, so we can discard the callbacks easily with
707  * this locking hierarchy.
708  */
709 void cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
710                                 AFSCallBack *cbp, long flags)
711 {
712     cm_racingRevokes_t *revp;           /* where we are */
713     cm_racingRevokes_t *nrevp;          /* where we'll be next */
714     int freeFlag;
715     cm_server_t * serverp = 0;
716
717     lock_ObtainWrite(&cm_callbackLock);
718     if (flags & CM_CALLBACK_MAINTAINCOUNT) {
719         osi_assert(cm_activeCallbackGrantingCalls > 0);
720     }
721     else {
722         osi_assert(cm_activeCallbackGrantingCalls-- > 0);
723     }
724     if (cm_activeCallbackGrantingCalls == 0) 
725         freeFlag = 1;
726     else 
727         freeFlag = 0;
728
729     /* record the callback; we'll clear it below if we really lose it */
730     if (cbrp) {
731         if (scp) {
732             if (scp->cbServerp != cbrp->serverp) {
733                 serverp = scp->cbServerp;
734             }
735             scp->cbServerp = cbrp->serverp;
736             scp->cbExpires = cbrp->startTime + cbp->ExpirationTime;
737         } else {
738             serverp = cbrp->serverp;
739         }
740         cbrp->serverp = NULL;
741     }
742
743     /* a callback was actually revoked during our granting call, so
744      * run down the list of revoked fids, looking for ours.
745      * If activeCallbackGrantingCalls is zero, free the elements, too.
746      *
747      * May need to go through entire list just to do the freeing.
748      */
749     for (revp = cm_racingRevokesp; revp; revp = nrevp) {
750         nrevp = (cm_racingRevokes_t *) osi_QNext(&revp->q);
751         /* if this callback came in later than when we started the
752          * callback-granting call, and if this fid is the right fid,
753          * then clear the callback.
754          */
755         if (scp && cbrp && cbrp->callbackCount != cm_callbackCount
756              && revp->callbackCount > cbrp->callbackCount
757              && (( scp->fid.volume == revp->fid.volume &&
758                    scp->fid.vnode == revp->fid.vnode &&
759                    scp->fid.unique == revp->fid.unique)
760                   ||
761                   ((revp->flags & CM_RACINGFLAG_CANCELVOL) &&
762                     scp->fid.volume == revp->fid.volume)
763                   ||
764                   (revp->flags & CM_RACINGFLAG_CANCELALL))) {
765             /* this one matches */
766             osi_Log4(afsd_logp,
767                       "Racing revoke scp %x old cbc %d rev cbc %d cur cbc %d",
768                       scp,
769                       cbrp->callbackCount, revp->callbackCount,
770                       cm_callbackCount);
771             cm_DiscardSCache(scp);
772             /*
773              * Since we don't have a callback to preserve, it's
774              * OK to drop the lock and re-obtain it.
775              */
776             lock_ReleaseMutex(&scp->mx);
777             cm_CallbackNotifyChange(scp);
778             lock_ObtainMutex(&scp->mx);
779         }
780         if (freeFlag) free(revp);
781     }
782
783     /* if we freed the list, zap the pointer to it */
784     if (freeFlag) cm_racingRevokesp = NULL;
785
786     lock_ReleaseWrite(&cm_callbackLock);
787
788     if ( serverp ) {
789         lock_ObtainWrite(&cm_serverLock);
790         cm_FreeServer(serverp);
791         lock_ReleaseWrite(&cm_serverLock);
792     }
793 }
794
795 /* if flags is 1, we want to force the code to make one call, anyway.
796  * called with locked scp; returns with same.
797  */
798 long cm_GetCallback(cm_scache_t *scp, struct cm_user *userp,
799                     struct cm_req *reqp, long flags)
800 {
801     long code;
802     cm_conn_t *connp;
803     AFSFetchStatus afsStatus;
804     AFSVolSync volSync;
805     AFSCallBack callback;
806     AFSFid tfid;
807     cm_callbackRequest_t cbr;
808     int mustCall;
809     long sflags;
810     cm_fid_t sfid;
811     struct rx_connection * callp;
812
813     osi_Log2(afsd_logp, "GetCallback scp %x flags %lX", scp, flags);
814
815 #ifdef AFS_FREELANCE_CLIENT
816     // The case where a callback is needed on /afs is handled
817     // specially. We need to fetch the status by calling
818     // cm_MergeStatus and mark that cm_fakeDirCallback is 2
819     if (cm_freelanceEnabled) {
820         if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
821              scp->fid.volume==AFS_FAKE_ROOT_VOL_ID &&
822              scp->fid.unique==0x1 &&
823              scp->fid.vnode==0x1) {
824             
825             // Start by indicating that we're in the process
826             // of fetching the callback
827             lock_ObtainMutex(&cm_Freelance_Lock);
828             osi_Log0(afsd_logp,"cm_getGetCallback fakeGettingCallback=1");
829             cm_fakeGettingCallback = 1;
830             lock_ReleaseMutex(&cm_Freelance_Lock);
831
832             // Fetch the status info 
833             cm_MergeStatus(scp, &afsStatus, &volSync, userp, 0);
834
835             // Indicate that the callback is not done
836             lock_ObtainMutex(&cm_Freelance_Lock);
837             osi_Log0(afsd_logp,"cm_getGetCallback fakeDirCallback=2");
838             cm_fakeDirCallback = 2;
839
840             // Indicate that we're no longer fetching the callback
841             osi_Log0(afsd_logp,"cm_getGetCallback fakeGettingCallback=0");
842             cm_fakeGettingCallback = 0;
843             lock_ReleaseMutex(&cm_Freelance_Lock);
844
845             return 0;
846         }
847
848         if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID && scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
849             osi_Log0(afsd_logp,"cm_getcallback should NEVER EVER get here... ");
850         }
851     }
852 #endif /* AFS_FREELANCE_CLIENT */
853         
854     mustCall = (flags & 1);
855     cm_AFSFidFromFid(&tfid, &scp->fid);
856     while (1) {
857         if (!mustCall && cm_HaveCallback(scp)) return 0;
858
859         /* turn off mustCall, since it has now forced us past the check above */
860         mustCall = 0;
861
862         /* otherwise, we have to make an RPC to get the status */
863         sflags = CM_SCACHESYNC_FETCHSTATUS | CM_SCACHESYNC_GETCALLBACK;
864         cm_SyncOp(scp, NULL, NULL, NULL, 0, sflags);
865         cm_StartCallbackGrantingCall(scp, &cbr);
866         sfid = scp->fid;
867         lock_ReleaseMutex(&scp->mx);
868                 
869         /* now make the RPC */
870         osi_Log1(afsd_logp, "CALL FetchStatus vp %x", (long) scp);
871         do {
872             code = cm_Conn(&sfid, userp, reqp, &connp);
873             if (code) continue;
874
875             callp = cm_GetRxConn(connp);
876             code = RXAFS_FetchStatus(callp, &tfid,
877                                      &afsStatus, &callback, &volSync);
878             rx_PutConnection(callp);
879
880         } while (cm_Analyze(connp, userp, reqp, &sfid, &volSync, NULL,
881                             &cbr, code));
882         code = cm_MapRPCError(code, reqp);
883         osi_Log0(afsd_logp, "CALL FetchStatus DONE");
884
885         lock_ObtainMutex(&scp->mx);
886         cm_SyncOpDone(scp, NULL, sflags);
887         if (code == 0) {
888             cm_EndCallbackGrantingCall(scp, &cbr, &callback, 0);
889             cm_MergeStatus(scp, &afsStatus, &volSync, userp, 0);
890         }   
891         else
892             cm_EndCallbackGrantingCall(NULL, &cbr, NULL, 0);
893
894         /* now check to see if we got an error */
895         if (code) return code;
896     }
897 }
898
899 /* called periodically by cm_daemon to shut down use of expired callbacks */
900 void cm_CheckCBExpiration(void)
901 {
902     int i;
903     cm_scache_t *scp;
904     long now;
905         
906     osi_Log0(afsd_logp, "CheckCBExpiration");
907
908     now = osi_Time();
909     lock_ObtainWrite(&cm_scacheLock);
910     for (i=0; i<cm_hashTableSize; i++) {
911         for (scp = cm_hashTablep[i]; scp; scp=scp->nextp) {
912             scp->refCount++;
913             lock_ReleaseWrite(&cm_scacheLock);
914             if (scp->cbExpires > 0 && (scp->cbServerp == NULL || now > scp->cbExpires)) {
915                 osi_Log1(afsd_logp, "Callback Expiration Discarding SCache scp %x", scp);
916                 cm_CallbackNotifyChange(scp);
917                 lock_ObtainMutex(&scp->mx);
918                 cm_DiscardSCache(scp);
919                 lock_ReleaseMutex(&scp->mx);
920             }
921             lock_ObtainWrite(&cm_scacheLock);
922             osi_assert(scp->refCount-- > 0);
923         }
924     }
925     lock_ReleaseWrite(&cm_scacheLock);
926 }
927
928 /* debug interface: not implemented */
929 int SRXAFSCB_GetCellByNum(struct rx_call *a_call, afs_int32 a_cellnum,
930                           char **a_name, serverList *a_hosts)
931 {
932     /* XXXX */
933     osi_Log0(afsd_logp, "SRXAFSCB_GetCellByNum - not implemented");
934     return RXGEN_OPCODE;
935 }
936
937 /* debug interface: not implemented */
938 int SRXAFSCB_TellMeAboutYourself(struct rx_call *a_call, afs_int32 a_cellnum,
939                           char **a_name, serverList *a_hosts)
940 {
941     /* XXXX */
942     osi_Log0(afsd_logp, "SRXAFSCB_TellMeAboutYourself - not implemented");
943     return RXGEN_OPCODE;
944 }