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