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