libafs: Do not write-lock afs_xserver on ICBS
[openafs.git] / src / afs / afs_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 /*
11  * afs_callback.c:
12  *      Exported routines (and their private support) to implement
13  *      the callback RPC interface.
14  */
15
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19
20 #include "afs/sysincludes.h"    /*Standard vendor system headers */
21 #include "afsincludes.h"        /*AFS-based standard headers */
22 #include "afs/afs_stats.h"      /*Cache Manager stats */
23 #include "afs/afs_args.h"
24
25 afs_int32 afs_allCBs = 0;       /*Break callbacks on all objects */
26 afs_int32 afs_oddCBs = 0;       /*Break callbacks on dirs */
27 afs_int32 afs_evenCBs = 0;      /*Break callbacks received on files */
28 afs_int32 afs_allZaps = 0;      /*Objects entries deleted */
29 afs_int32 afs_oddZaps = 0;      /*Dir cache entries deleted */
30 afs_int32 afs_evenZaps = 0;     /*File cache entries deleted */
31 afs_int32 afs_connectBacks = 0;
32
33 /*
34  * Some debugging aids.
35  */
36 static struct ltable {
37     char *name;
38     char *addr;
39 } ltable[] = {
40     {
41     "afs_xvcache", (char *)&afs_xvcache}, {
42     "afs_xdcache", (char *)&afs_xdcache}, {
43     "afs_xserver", (char *)&afs_xserver}, {
44     "afs_xvcb", (char *)&afs_xvcb}, {
45     "afs_xbrs", (char *)&afs_xbrs}, {
46     "afs_xcell", (char *)&afs_xcell}, {
47     "afs_xconn", (char *)&afs_xconn}, {
48     "afs_xuser", (char *)&afs_xuser}, {
49     "afs_xvolume", (char *)&afs_xvolume}, {
50     "puttofile", (char *)&afs_puttofileLock}, {
51     "afs_ftf", (char *)&afs_ftf}, {
52     "afs_xcbhash", (char *)&afs_xcbhash}, {
53     "afs_xaxs", (char *)&afs_xaxs}, {
54     "afs_xinterface", (char *)&afs_xinterface},
55 #ifndef UKERNEL
56     {
57     "afs_xosi", (char *)&afs_xosi},
58 #endif
59     {
60       "afs_xsrvAddr", (char *)&afs_xsrvAddr},
61     {
62       "afs_xvreclaim", (char *)&afs_xvreclaim},
63     { "afsdb_client_lock", (char *)&afsdb_client_lock},
64     { "afsdb_req_lock", (char *)&afsdb_req_lock},
65     { "afs_discon_lock", (char *)&afs_discon_lock},
66     { "afs_disconDirtyLock", (char *)&afs_disconDirtyLock},
67     { "afs_discon_vc_dirty", (char *)&afs_xvcdirty},
68 };
69 unsigned long lastCallBack_vnode;
70 unsigned int lastCallBack_dv;
71 osi_timeval_t lastCallBack_time;
72
73 /* these are for storing alternate interface addresses */
74 struct interfaceAddr afs_cb_interface;
75
76 /*------------------------------------------------------------------------
77  * EXPORTED SRXAFSCB_GetCE
78  *
79  * Description:
80  *      Routine called by the server-side callback RPC interface to
81  *      implement pulling out the contents of the i'th cache entry.
82  *
83  * Arguments:
84  *      a_call   : Ptr to Rx call on which this request came in.
85  *      a_index  : Index of desired cache entry.
86  *      a_result : Ptr to a buffer for the given cache entry.
87  *
88  * Returns:
89  *      0 if everything went fine,
90  *      1 if we were given a bad index.
91  *
92  * Environment:
93  *      Nothing interesting.
94  *
95  * Side Effects:
96  *      As advertised.
97  *------------------------------------------------------------------------*/
98
99 int
100 SRXAFSCB_GetCE(struct rx_call *a_call, afs_int32 a_index,
101                struct AFSDBCacheEntry *a_result)
102 {
103
104     int i;              /*Loop variable */
105     struct vcache *tvc; /*Ptr to current cache entry */
106     int code;                   /*Return code */
107     XSTATS_DECLS;
108
109     RX_AFS_GLOCK();
110
111     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_GETCE);
112
113     AFS_STATCNT(SRXAFSCB_GetCE);
114     for (i = 0; i < VCSIZE; i++) {
115         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
116             if (a_index == 0)
117                 goto searchDone;
118             a_index--;
119         }                       /*Zip through current hash chain */
120     }                           /*Zip through hash chains */
121
122   searchDone:
123     if (tvc == NULL) {
124         /*Past EOF */
125         code = 1;
126         goto fcnDone;
127     }
128
129     /*
130      * Copy out the located entry.
131      */
132     a_result->addr = afs_data_pointer_to_int32(tvc);
133     a_result->cell = tvc->f.fid.Cell;
134     a_result->netFid.Volume = tvc->f.fid.Fid.Volume;
135     a_result->netFid.Vnode = tvc->f.fid.Fid.Vnode;
136     a_result->netFid.Unique = tvc->f.fid.Fid.Unique;
137     a_result->lock.waitStates = tvc->lock.wait_states;
138     a_result->lock.exclLocked = tvc->lock.excl_locked;
139     a_result->lock.readersReading = tvc->lock.readers_reading;
140     a_result->lock.numWaiting = tvc->lock.num_waiting;
141 #if defined(INSTRUMENT_LOCKS)
142     a_result->lock.pid_last_reader = MyPidxx2Pid(tvc->lock.pid_last_reader);
143     a_result->lock.pid_writer = MyPidxx2Pid(tvc->lock.pid_writer);
144     a_result->lock.src_indicator = tvc->lock.src_indicator;
145 #else
146     /* On osf20 , the vcache does not maintain these three fields */
147     a_result->lock.pid_last_reader = 0;
148     a_result->lock.pid_writer = 0;
149     a_result->lock.src_indicator = 0;
150 #endif /* INSTRUMENT_LOCKS */
151 #ifdef AFS_64BIT_CLIENT
152     a_result->Length = (afs_int32) tvc->f.m.Length & 0xffffffff;
153 #else /* AFS_64BIT_CLIENT */
154     a_result->Length = tvc->f.m.Length;
155 #endif /* AFS_64BIT_CLIENT */
156     a_result->DataVersion = hgetlo(tvc->f.m.DataVersion);
157     a_result->callback = afs_data_pointer_to_int32(tvc->callback);      /* XXXX Now a pointer; change it XXXX */
158     a_result->cbExpires = tvc->cbExpires;
159     if (tvc->f.states & CVInit) {
160         a_result->refCount = 1;
161     } else {
162 #ifdef AFS_DARWIN80_ENV
163     a_result->refCount = vnode_isinuse(AFSTOV(tvc),0)?1:0; /* XXX fix */
164 #else
165     a_result->refCount = VREFCOUNT(tvc);
166 #endif
167     }
168     a_result->opens = tvc->opens;
169     a_result->writers = tvc->execsOrWriters;
170     a_result->mvstat = tvc->mvstat;
171     a_result->states = tvc->f.states;
172     code = 0;
173
174     /*
175      * Return our results.
176      */
177   fcnDone:
178     XSTATS_END_TIME;
179
180     RX_AFS_GUNLOCK();
181
182     return (code);
183
184 }                               /*SRXAFSCB_GetCE */
185
186 int
187 SRXAFSCB_GetCE64(struct rx_call *a_call, afs_int32 a_index,
188                  struct AFSDBCacheEntry64 *a_result)
189 {
190     int i;              /*Loop variable */
191     struct vcache *tvc; /*Ptr to current cache entry */
192     int code;                   /*Return code */
193     XSTATS_DECLS;
194
195     RX_AFS_GLOCK();
196
197     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_GETCE);
198
199     AFS_STATCNT(SRXAFSCB_GetCE64);
200     for (i = 0; i < VCSIZE; i++) {
201         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
202             if (a_index == 0)
203                 goto searchDone;
204             a_index--;
205         }                       /*Zip through current hash chain */
206     }                           /*Zip through hash chains */
207
208   searchDone:
209     if (tvc == NULL) {
210         /*Past EOF */
211         code = 1;
212         goto fcnDone;
213     }
214
215     /*
216      * Copy out the located entry.
217      */
218     a_result->addr = afs_data_pointer_to_int32(tvc);
219     a_result->cell = tvc->f.fid.Cell;
220     a_result->netFid.Volume = tvc->f.fid.Fid.Volume;
221     a_result->netFid.Vnode = tvc->f.fid.Fid.Vnode;
222     a_result->netFid.Unique = tvc->f.fid.Fid.Unique;
223     a_result->lock.waitStates = tvc->lock.wait_states;
224     a_result->lock.exclLocked = tvc->lock.excl_locked;
225     a_result->lock.readersReading = tvc->lock.readers_reading;
226     a_result->lock.numWaiting = tvc->lock.num_waiting;
227 #if defined(INSTRUMENT_LOCKS)
228     a_result->lock.pid_last_reader = MyPidxx2Pid(tvc->lock.pid_last_reader);
229     a_result->lock.pid_writer = MyPidxx2Pid(tvc->lock.pid_writer);
230     a_result->lock.src_indicator = tvc->lock.src_indicator;
231 #else
232     /* On osf20 , the vcache does not maintain these three fields */
233     a_result->lock.pid_last_reader = 0;
234     a_result->lock.pid_writer = 0;
235     a_result->lock.src_indicator = 0;
236 #endif /* INSTRUMENT_LOCKS */
237     a_result->Length = tvc->f.m.Length;
238     a_result->DataVersion = hgetlo(tvc->f.m.DataVersion);
239     a_result->callback = afs_data_pointer_to_int32(tvc->callback);      /* XXXX Now a pointer; change it XXXX */
240     a_result->cbExpires = tvc->cbExpires;
241     if (tvc->f.states & CVInit) {
242         a_result->refCount = 1;
243     } else {
244 #ifdef AFS_DARWIN80_ENV
245     a_result->refCount = vnode_isinuse(AFSTOV(tvc),0)?1:0; /* XXX fix */
246 #else
247     a_result->refCount = VREFCOUNT(tvc);
248 #endif
249     }
250     a_result->opens = tvc->opens;
251     a_result->writers = tvc->execsOrWriters;
252     a_result->mvstat = tvc->mvstat;
253     a_result->states = tvc->f.states;
254     code = 0;
255
256     /*
257      * Return our results.
258      */
259   fcnDone:
260     XSTATS_END_TIME;
261
262     RX_AFS_GUNLOCK();
263
264     return (code);
265
266 }                               /*SRXAFSCB_GetCE64 */
267
268
269 /*------------------------------------------------------------------------
270  * EXPORTED SRXAFSCB_GetLock
271  *
272  * Description:
273  *      Routine called by the server-side callback RPC interface to
274  *      implement pulling out the contents of a lock in the lock
275  *      table.
276  *
277  * Arguments:
278  *      a_call   : Ptr to Rx call on which this request came in.
279  *      a_index  : Index of desired lock.
280  *      a_result : Ptr to a buffer for the given lock.
281  *
282  * Returns:
283  *      0 if everything went fine,
284  *      1 if we were given a bad index.
285  *
286  * Environment:
287  *      Nothing interesting.
288  *
289  * Side Effects:
290  *      As advertised.
291  *------------------------------------------------------------------------*/
292
293 int
294 SRXAFSCB_GetLock(struct rx_call *a_call, afs_int32 a_index,
295                  struct AFSDBLock *a_result)
296 {
297     struct ltable *tl;          /*Ptr to lock table entry */
298     int nentries;               /*Num entries in table */
299     int code;                   /*Return code */
300     XSTATS_DECLS;
301
302     RX_AFS_GLOCK();
303
304     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_GETLOCK);
305
306     AFS_STATCNT(SRXAFSCB_GetLock);
307     nentries = sizeof(ltable) / sizeof(struct ltable);
308     if (a_index < 0 || a_index >= nentries+afs_cellindex) {
309         /*
310          * Past EOF
311          */
312         code = 1;
313     } else if (a_index >= nentries) {
314         struct cell *tc = afs_GetCellByIndex(a_index-nentries, 0);
315         strcpy(a_result->name, tc->cellName);
316         a_result->lock.waitStates =
317             ((struct afs_lock *)&(tc->lock))->wait_states;
318         a_result->lock.exclLocked =
319             ((struct afs_lock *)&(tc->lock))->excl_locked;
320         a_result->lock.readersReading =
321             ((struct afs_lock *)&(tc->lock))->readers_reading;
322         a_result->lock.numWaiting =
323             ((struct afs_lock *)&(tc->lock))->num_waiting;
324 #ifdef INSTRUMENT_LOCKS
325         a_result->lock.pid_last_reader =
326             MyPidxx2Pid(((struct afs_lock *)&(tc->lock))->pid_last_reader);
327         a_result->lock.pid_writer =
328             MyPidxx2Pid(((struct afs_lock *)&(tc->lock))->pid_writer);
329         a_result->lock.src_indicator =
330             ((struct afs_lock *)&(tc->lock))->src_indicator;
331 #else
332         a_result->lock.pid_last_reader = 0;
333         a_result->lock.pid_writer = 0;
334         a_result->lock.src_indicator = 0;
335 #endif
336         code = 0;
337     } else {
338         /*
339          * Found it - copy out its contents.
340          */
341         tl = &ltable[a_index];
342         strcpy(a_result->name, tl->name);
343         a_result->lock.waitStates =
344             ((struct afs_lock *)(tl->addr))->wait_states;
345         a_result->lock.exclLocked =
346             ((struct afs_lock *)(tl->addr))->excl_locked;
347         a_result->lock.readersReading =
348             ((struct afs_lock *)(tl->addr))->readers_reading;
349         a_result->lock.numWaiting =
350             ((struct afs_lock *)(tl->addr))->num_waiting;
351 #ifdef INSTRUMENT_LOCKS
352         a_result->lock.pid_last_reader =
353             MyPidxx2Pid(((struct afs_lock *)(tl->addr))->pid_last_reader);
354         a_result->lock.pid_writer =
355             MyPidxx2Pid(((struct afs_lock *)(tl->addr))->pid_writer);
356         a_result->lock.src_indicator =
357             ((struct afs_lock *)(tl->addr))->src_indicator;
358 #else
359         a_result->lock.pid_last_reader = 0;
360         a_result->lock.pid_writer = 0;
361         a_result->lock.src_indicator = 0;
362 #endif
363         code = 0;
364     }
365
366     XSTATS_END_TIME;
367
368     RX_AFS_GUNLOCK();
369
370     return (code);
371
372 }                               /*SRXAFSCB_GetLock */
373
374
375 /*------------------------------------------------------------------------
376  * static ClearCallBack
377  *
378  * Description:
379  *      Clear out callback information for the specified file, or
380  *      even a whole volume.  Used to worry about callback was from
381  *      within the particular cell or not.  Now we don't bother with
382  *      that anymore; it's not worth the time.
383  *
384  * Arguments:
385  *      a_conn : Ptr to Rx connection involved.
386  *      a_fid  : Ptr to AFS fid being cleared.
387  *
388  * Returns:
389  *      0 (always)
390  *
391  * Environment:
392  *      Nothing interesting.
393  *
394  * Side Effects:
395  *      As advertised.
396
397 Appears to need to be called with GLOCK held, as the icl_Event4 stuff asserts otherwise
398
399  *------------------------------------------------------------------------*/
400
401 static int
402 ClearCallBack(struct rx_connection *a_conn,
403               struct AFSFid *a_fid)
404 {
405     struct vcache *tvc;
406     int i;
407     struct VenusFid localFid;
408     struct volume *tv;
409 #ifdef AFS_DARWIN80_ENV
410     vnode_t vp;
411 #endif
412
413     AFS_STATCNT(ClearCallBack);
414
415     AFS_ASSERT_GLOCK();
416
417     /*
418      * XXXX Don't hold any server locks here because of callback protocol XXX
419      */
420     localFid.Cell = 0;
421     localFid.Fid.Volume = a_fid->Volume;
422     localFid.Fid.Vnode = a_fid->Vnode;
423     localFid.Fid.Unique = a_fid->Unique;
424
425     /*
426      * Volume ID of zero means don't do anything.
427      */
428     if (a_fid->Volume != 0) {
429         if (a_fid->Vnode == 0) {
430                 struct afs_q *tq, *uq;
431             /*
432              * Clear callback for the whole volume.  Zip through the
433              * hash chain, nullifying entries whose volume ID matches.
434              */
435 loop1:
436                 ObtainReadLock(&afs_xvcache);
437                 i = VCHashV(&localFid);
438                 for (tq = afs_vhashTV[i].prev; tq != &afs_vhashTV[i]; tq = uq) {
439                     uq = QPrev(tq);
440                     tvc = QTOVH(tq);
441                     if (tvc->f.fid.Fid.Volume == a_fid->Volume) {
442                         tvc->callback = NULL;
443                         if (!localFid.Cell)
444                             localFid.Cell = tvc->f.fid.Cell;
445                         tvc->dchint = NULL;     /* invalidate hints */
446                         if (tvc->f.states & CVInit) {
447                             ReleaseReadLock(&afs_xvcache);
448                             afs_osi_Sleep(&tvc->f.states);
449                             goto loop1;
450                         }
451 #if     defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)  || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV)
452                         AFS_FAST_HOLD(tvc);
453 #else
454 #ifdef AFS_DARWIN80_ENV
455                         if (tvc->f.states & CDeadVnode) {
456                             if (!(tvc->f.states & CBulkFetching)) {
457                                 ReleaseReadLock(&afs_xvcache);
458                                 afs_osi_Sleep(&tvc->f.states);
459                                 goto loop1;
460                             }
461                         }
462                         vp = AFSTOV(tvc);
463                         if (vnode_get(vp))
464                             continue;
465                         if (vnode_ref(vp)) {
466                             AFS_GUNLOCK();
467                             vnode_put(vp);
468                             AFS_GLOCK();
469                             continue;
470                         }
471                         if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
472                             AFS_GUNLOCK();
473                             vnode_recycle(AFSTOV(tvc));
474                             AFS_GLOCK();
475                         }
476 #else
477                         AFS_FAST_HOLD(tvc);
478 #endif
479 #endif
480                         ReleaseReadLock(&afs_xvcache);
481                         ObtainWriteLock(&afs_xcbhash, 449);
482                         afs_DequeueCallback(tvc);
483                         tvc->f.states &= ~(CStatd | CUnique | CBulkFetching);
484                         afs_allCBs++;
485                         if (tvc->f.fid.Fid.Vnode & 1)
486                             afs_oddCBs++;
487                         else
488                             afs_evenCBs++;
489                         ReleaseWriteLock(&afs_xcbhash);
490                         if ((tvc->f.fid.Fid.Vnode & 1 || (vType(tvc) == VDIR)))
491                             osi_dnlc_purgedp(tvc);
492                         afs_Trace3(afs_iclSetp, CM_TRACE_CALLBACK,
493                                    ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
494                                    tvc->f.states, ICL_TYPE_INT32,
495                                    a_fid->Volume);
496 #ifdef AFS_DARWIN80_ENV
497                         vnode_put(AFSTOV(tvc));
498 #endif
499                         ObtainReadLock(&afs_xvcache);
500                         uq = QPrev(tq);
501                         AFS_FAST_RELE(tvc);
502                     } else if ((tvc->f.states & CMValid)
503                                && (tvc->mvid->Fid.Volume == a_fid->Volume)) {
504                         tvc->f.states &= ~CMValid;
505                         if (!localFid.Cell)
506                             localFid.Cell = tvc->mvid->Cell;
507                     }
508                 }
509                 ReleaseReadLock(&afs_xvcache);
510
511             /*
512              * XXXX Don't hold any locks here XXXX
513              */
514             tv = afs_FindVolume(&localFid, 0);
515             if (tv) {
516                 afs_ResetVolumeInfo(tv);
517                 afs_PutVolume(tv, 0);
518                 /* invalidate mtpoint? */
519             }
520         } /*Clear callbacks for whole volume */
521         else {
522             /*
523              * Clear callbacks just for the one file.
524              */
525             struct vcache *uvc;
526             afs_allCBs++;
527             if (a_fid->Vnode & 1)
528                 afs_oddCBs++;   /*Could do this on volume basis, too */
529             else
530                 afs_evenCBs++;  /*A particular fid was specified */
531 loop2:
532             ObtainReadLock(&afs_xvcache);
533             i = VCHash(&localFid);
534             for (tvc = afs_vhashT[i]; tvc; tvc = uvc) {
535                 uvc = tvc->hnext;
536                 if (tvc->f.fid.Fid.Vnode == a_fid->Vnode
537                     && tvc->f.fid.Fid.Volume == a_fid->Volume
538                     && tvc->f.fid.Fid.Unique == a_fid->Unique) {
539                     tvc->callback = NULL;
540                     tvc->dchint = NULL; /* invalidate hints */
541                     if (tvc->f.states & CVInit) {
542                         ReleaseReadLock(&afs_xvcache);
543                         afs_osi_Sleep(&tvc->f.states);
544                         goto loop2;
545                     }
546 #if     defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)  || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV)
547                     AFS_FAST_HOLD(tvc);
548 #else
549 #ifdef AFS_DARWIN80_ENV
550                     if (tvc->f.states & CDeadVnode) {
551                         if (!(tvc->f.states & CBulkFetching)) {
552                             ReleaseReadLock(&afs_xvcache);
553                             afs_osi_Sleep(&tvc->f.states);
554                             goto loop2;
555                         }
556                     }
557                     vp = AFSTOV(tvc);
558                     if (vnode_get(vp))
559                         continue;
560                     if (vnode_ref(vp)) {
561                         AFS_GUNLOCK();
562                         vnode_put(vp);
563                         AFS_GLOCK();
564                         continue;
565                     }
566                     if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
567                         AFS_GUNLOCK();
568                         vnode_recycle(AFSTOV(tvc));
569                         AFS_GLOCK();
570                     }
571 #else
572                     AFS_FAST_HOLD(tvc);
573 #endif
574 #endif
575                     ReleaseReadLock(&afs_xvcache);
576                     ObtainWriteLock(&afs_xcbhash, 450);
577                     afs_DequeueCallback(tvc);
578                     tvc->f.states &= ~(CStatd | CUnique | CBulkFetching);
579                     ReleaseWriteLock(&afs_xcbhash);
580                     if ((tvc->f.fid.Fid.Vnode & 1 || (vType(tvc) == VDIR)))
581                         osi_dnlc_purgedp(tvc);
582                     afs_Trace3(afs_iclSetp, CM_TRACE_CALLBACK,
583                                ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
584                                tvc->f.states, ICL_TYPE_LONG, 0);
585 #ifdef CBDEBUG
586                     lastCallBack_vnode = afid->Vnode;
587                     lastCallBack_dv = tvc->mstat.DataVersion.low;
588                     osi_GetuTime(&lastCallBack_time);
589 #endif /* CBDEBUG */
590 #ifdef AFS_DARWIN80_ENV
591                     vnode_put(AFSTOV(tvc));
592 #endif
593                     ObtainReadLock(&afs_xvcache);
594                     uvc = tvc->hnext;
595                     AFS_FAST_RELE(tvc);
596                 }
597             }                   /*Walk through hash table */
598             ReleaseReadLock(&afs_xvcache);
599         }                       /*Clear callbacks for one file */
600     }
601
602     /*Fid has non-zero volume ID */
603     /*
604      * Always return a predictable value.
605      */
606     return (0);
607
608 }                               /*ClearCallBack */
609
610
611 /*------------------------------------------------------------------------
612  * EXPORTED SRXAFSCB_CallBack
613  *
614  * Description:
615  *      Routine called by the server-side callback RPC interface to
616  *      implement passing in callback information.
617  *      table.
618  *
619  * Arguments:
620  *      a_call      : Ptr to Rx call on which this request came in.
621  *      a_fids      : Ptr to array of fids involved.
622  *      a_callbacks : Ptr to matching callback info for the fids.
623  *
624  * Returns:
625  *      0 (always).
626  *
627  * Environment:
628  *      Nothing interesting.
629  *
630  * Side Effects:
631  *      As advertised.
632  *------------------------------------------------------------------------*/
633
634 int
635 SRXAFSCB_CallBack(struct rx_call *a_call, struct AFSCBFids *a_fids,
636                   struct AFSCBs *a_callbacks)
637 {
638     int i;              /*Loop variable */
639     struct AFSFid *tfid;        /*Ptr to current fid */
640     struct rx_connection *tconn;        /*Call's connection */
641     int code = 0;
642     XSTATS_DECLS;
643
644     RX_AFS_GLOCK();
645
646     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_CALLBACK);
647
648     AFS_STATCNT(SRXAFSCB_CallBack);
649     if (!(tconn = rx_ConnectionOf(a_call)))
650         return (0);
651     tfid = (struct AFSFid *)a_fids->AFSCBFids_val;
652
653     /*
654      * For now, we ignore callbacks, since the File Server only *breaks*
655      * callbacks at present.
656      */
657     for (i = 0; i < a_fids->AFSCBFids_len; i++)
658         ClearCallBack(tconn, &tfid[i]);
659
660     XSTATS_END_TIME;
661
662     RX_AFS_GUNLOCK();
663
664     return (0);
665
666 }                               /*SRXAFSCB_CallBack */
667
668
669 /*------------------------------------------------------------------------
670  * EXPORTED SRXAFSCB_Probe
671  *
672  * Description:
673  *      Routine called by the server-side callback RPC interface to
674  *      implement ``probing'' the Cache Manager, just making sure it's
675  *      still there.
676  *
677  * Arguments:
678  *      a_call : Ptr to Rx call on which this request came in.
679  *
680  * Returns:
681  *      0 (always).
682  *
683  * Environment:
684  *      Nothing interesting.
685  *
686  * Side Effects:
687  *      As advertised.
688  *------------------------------------------------------------------------*/
689
690 int
691 SRXAFSCB_Probe(struct rx_call *a_call)
692 {
693     int code = 0;
694     XSTATS_DECLS;
695
696     RX_AFS_GLOCK();
697     AFS_STATCNT(SRXAFSCB_Probe);
698
699     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_PROBE);
700     XSTATS_END_TIME;
701
702     RX_AFS_GUNLOCK();
703
704     return (0);
705
706 }                               /*SRXAFSCB_Probe */
707
708
709 /*------------------------------------------------------------------------
710  * EXPORTED SRXAFSCB_InitCallBackState
711  *
712  * Description:
713  *      Routine called by the server-side callback RPC interface to
714  *      implement clearing all callbacks from this host.
715  *
716  * Arguments:
717  *      a_call : Ptr to Rx call on which this request came in.
718  *
719  * Returns:
720  *      0 (always).
721  *
722  * Environment:
723  *      Nothing interesting.
724  *
725  * Side Effects:
726  *      As advertised.
727  *------------------------------------------------------------------------*/
728
729 int
730 SRXAFSCB_InitCallBackState(struct rx_call *a_call)
731 {
732     int i;
733     struct vcache *tvc;
734     struct rx_connection *tconn;
735     struct rx_peer *peer;
736     struct server *ts;
737     int code = 0;
738     XSTATS_DECLS;
739
740     RX_AFS_GLOCK();
741
742     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_INITCALLBACKSTATE);
743     AFS_STATCNT(SRXAFSCB_InitCallBackState);
744
745     /*
746      * Find the address of the host making this call
747      */
748     if ((tconn = rx_ConnectionOf(a_call)) && (peer = rx_PeerOf(tconn))) {
749
750         afs_allCBs++;
751         afs_oddCBs++;           /*Including any missed via create race */
752         afs_evenCBs++;          /*Including any missed via create race */
753
754         ts = afs_FindServer(rx_HostOf(peer), rx_PortOf(peer), (afsUUID *) 0,
755                             0);
756         if (ts) {
757             for (i = 0; i < VCSIZE; i++)
758                 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
759                     if (tvc->callback == ts) {
760                         ObtainWriteLock(&afs_xcbhash, 451);
761                         afs_DequeueCallback(tvc);
762                         tvc->callback = NULL;
763                         tvc->f.states &= ~(CStatd | CUnique | CBulkFetching);
764                         ReleaseWriteLock(&afs_xcbhash);
765                     }
766                 }
767
768             /* capabilities need be requested again */
769             ts->flags &= ~SCAPS_KNOWN;
770         }
771
772
773
774         /* find any volumes residing on this server and flush their state */
775         {
776             struct volume *tv;
777             int j;
778
779             for (i = 0; i < NVOLS; i++)
780                 for (tv = afs_volumes[i]; tv; tv = tv->next) {
781                     for (j = 0; j < AFS_MAXHOSTS; j++)
782                         if (tv->serverHost[j] == ts)
783                             afs_ResetVolumeInfo(tv);
784                 }
785         }
786         osi_dnlc_purge();       /* may be a little bit extreme */
787     }
788
789     XSTATS_END_TIME;
790
791     RX_AFS_GUNLOCK();
792
793     return (0);
794
795 }                               /*SRXAFSCB_InitCallBackState */
796
797
798 /*------------------------------------------------------------------------
799  * EXPORTED SRXAFSCB_XStatsVersion
800  *
801  * Description:
802  *      Routine called by the server-side callback RPC interface to
803  *      implement pulling out the xstat version number for the Cache
804  *      Manager.
805  *
806  * Arguments:
807  *      a_versionP : Ptr to the version number variable to set.
808  *
809  * Returns:
810  *      0 (always)
811  *
812  * Environment:
813  *      Nothing interesting.
814  *
815  * Side Effects:
816  *      As advertised.
817  *------------------------------------------------------------------------*/
818
819 int
820 SRXAFSCB_XStatsVersion(struct rx_call *a_call, afs_int32 * a_versionP)
821 {
822     int code = 0;
823
824     XSTATS_DECLS;
825
826     RX_AFS_GLOCK();
827     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_XSTATSVERSION);
828
829     *a_versionP = AFSCB_XSTAT_VERSION;
830
831     XSTATS_END_TIME;
832
833     RX_AFS_GUNLOCK();
834
835     return (0);
836 }                               /*SRXAFSCB_XStatsVersion */
837
838
839 /*------------------------------------------------------------------------
840  * EXPORTED SRXAFSCB_GetXStats
841  *
842  * Description:
843  *      Routine called by the server-side callback RPC interface to
844  *      implement getting the given data collection from the extended
845  *      Cache Manager statistics.
846  *
847  * Arguments:
848  *      a_call              : Ptr to Rx call on which this request came in.
849  *      a_clientVersionNum  : Client version number.
850  *      a_opCode            : Desired operation.
851  *      a_serverVersionNumP : Ptr to version number to set.
852  *      a_timeP             : Ptr to time value (seconds) to set.
853  *      a_dataArray         : Ptr to variable array structure to return
854  *                            stuff in.
855  *
856  * Returns:
857  *      0 (always).
858  *
859  * Environment:
860  *      Nothing interesting.
861  *
862  * Side Effects:
863  *      As advertised.
864  *------------------------------------------------------------------------*/
865
866 int
867 SRXAFSCB_GetXStats(struct rx_call *a_call, afs_int32 a_clientVersionNum,
868                    afs_int32 a_collectionNumber, afs_int32 * a_srvVersionNumP,
869                    afs_int32 * a_timeP, AFSCB_CollData * a_dataP)
870 {
871     int code;           /*Return value */
872     afs_int32 *dataBuffP;       /*Ptr to data to be returned */
873     afs_int32 dataBytes;        /*Bytes in data buffer */
874     XSTATS_DECLS;
875
876     RX_AFS_GLOCK();
877
878     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_GETXSTATS);
879
880     /*
881      * Record the time of day and the server version number.
882      */
883     *a_srvVersionNumP = AFSCB_XSTAT_VERSION;
884     *a_timeP = osi_Time();
885
886     /*
887      * Stuff the appropriate data in there (assume victory)
888      */
889     code = 0;
890
891 #ifdef AFS_NOSTATS
892     /*
893      * We're not keeping stats, so just return successfully with
894      * no data.
895      */
896     a_dataP->AFSCB_CollData_len = 0;
897     a_dataP->AFSCB_CollData_val = NULL;
898 #else
899     switch (a_collectionNumber) {
900     case AFSCB_XSTATSCOLL_CALL_INFO:
901         /*
902          * Pass back all the call-count-related data.
903          *
904          * >>> We are forced to allocate a separate area in which to
905          * >>> put this stuff in by the RPC stub generator, since it
906          * >>> will be freed at the tail end of the server stub code.
907          */
908         dataBytes = sizeof(struct afs_CMStats);
909         dataBuffP = (afs_int32 *) afs_osi_Alloc(dataBytes);
910         osi_Assert(dataBuffP != NULL);
911         memcpy((char *)dataBuffP, (char *)&afs_cmstats, dataBytes);
912         a_dataP->AFSCB_CollData_len = dataBytes >> 2;
913         a_dataP->AFSCB_CollData_val = dataBuffP;
914         break;
915
916     case AFSCB_XSTATSCOLL_PERF_INFO:
917         /*
918          * Update and then pass back all the performance-related data.
919          * Note: the only performance fields that need to be computed
920          * at this time are the number of accesses for this collection
921          * and the current server record info.
922          *
923          * >>> We are forced to allocate a separate area in which to
924          * >>> put this stuff in by the RPC stub generator, since it
925          * >>> will be freed at the tail end of the server stub code.
926          */
927         afs_stats_cmperf.numPerfCalls++;
928         afs_CountServers();
929         dataBytes = sizeof(afs_stats_cmperf);
930         dataBuffP = (afs_int32 *) afs_osi_Alloc(dataBytes);
931         osi_Assert(dataBuffP != NULL);
932         memcpy((char *)dataBuffP, (char *)&afs_stats_cmperf, dataBytes);
933         a_dataP->AFSCB_CollData_len = dataBytes >> 2;
934         a_dataP->AFSCB_CollData_val = dataBuffP;
935         break;
936
937     case AFSCB_XSTATSCOLL_FULL_PERF_INFO:
938         /*
939          * Pass back the full range of performance and statistical
940          * data available.  We have to bring the normal performance
941          * data collection up to date, then copy that data into
942          * the full collection.
943          *
944          * >>> We are forced to allocate a separate area in which to
945          * >>> put this stuff in by the RPC stub generator, since it
946          * >>> will be freed at the tail end of the server stub code.
947          */
948         afs_stats_cmperf.numPerfCalls++;
949         afs_CountServers();
950         memcpy((char *)(&(afs_stats_cmfullperf.perf)),
951                (char *)(&afs_stats_cmperf), sizeof(struct afs_stats_CMPerf));
952         afs_stats_cmfullperf.numFullPerfCalls++;
953
954         dataBytes = sizeof(afs_stats_cmfullperf);
955         dataBuffP = (afs_int32 *) afs_osi_Alloc(dataBytes);
956         osi_Assert(dataBuffP != NULL);
957         memcpy((char *)dataBuffP, (char *)(&afs_stats_cmfullperf), dataBytes);
958         a_dataP->AFSCB_CollData_len = dataBytes >> 2;
959         a_dataP->AFSCB_CollData_val = dataBuffP;
960         break;
961
962     default:
963         /*
964          * Illegal collection number.
965          */
966         a_dataP->AFSCB_CollData_len = 0;
967         a_dataP->AFSCB_CollData_val = NULL;
968         code = 1;
969     }                           /*Switch on collection number */
970 #endif /* AFS_NOSTATS */
971
972     XSTATS_END_TIME;
973
974     RX_AFS_GUNLOCK();
975
976     return (code);
977
978 }                               /*SRXAFSCB_GetXStats */
979
980
981 /*------------------------------------------------------------------------
982  * EXPORTED afs_RXCallBackServer
983  *
984  * Description:
985  *      Body of the thread supporting callback services.
986  *
987  * Arguments:
988  *      None.
989  *
990  * Returns:
991  *      0 (always).
992  *
993  * Environment:
994  *      Nothing interesting.
995  *
996  * Side Effects:
997  *      As advertised.
998  *------------------------------------------------------------------------*/
999
1000 int
1001 afs_RXCallBackServer(void)
1002 {
1003     AFS_STATCNT(afs_RXCallBackServer);
1004
1005     while (1) {
1006         if (afs_server)
1007             break;
1008         afs_osi_Sleep(&afs_server);
1009     }
1010
1011     /*
1012      * Donate this process to Rx.
1013      */
1014     rx_ServerProc(NULL);
1015     return (0);
1016
1017 }                               /*afs_RXCallBackServer */
1018
1019
1020 /*------------------------------------------------------------------------
1021  * EXPORTED shutdown_CB
1022  *
1023  * Description:
1024  *      Zero out important Cache Manager data structures.
1025  *
1026  * Arguments:
1027  *      None.
1028  *
1029  * Returns:
1030  *      0 (always).
1031  *
1032  * Environment:
1033  *      Nothing interesting.
1034  *
1035  * Side Effects:
1036  *      As advertised.
1037  *------------------------------------------------------------------------*/
1038
1039 int
1040 shutdown_CB(void)
1041 {
1042     AFS_STATCNT(shutdown_CB);
1043
1044     if (afs_cold_shutdown) {
1045         afs_oddCBs = afs_evenCBs = afs_allCBs = afs_allZaps = afs_oddZaps =
1046             afs_evenZaps = afs_connectBacks = 0;
1047     }
1048
1049     return (0);
1050
1051 }                               /*shutdown_CB */
1052
1053 /*------------------------------------------------------------------------
1054  * EXPORTED SRXAFSCB_InitCallBackState2
1055  *
1056  * Description:
1057  *      This routine was used in the AFS 3.5 beta release, but not anymore.
1058  *      It has since been replaced by SRXAFSCB_InitCallBackState3.
1059  *
1060  * Arguments:
1061  *      a_call : Ptr to Rx call on which this request came in.
1062  *
1063  * Returns:
1064  *      RXGEN_OPCODE (always).
1065  *
1066  * Environment:
1067  *      Nothing interesting.
1068  *
1069  * Side Effects:
1070  *      None
1071  *------------------------------------------------------------------------*/
1072
1073 int
1074 SRXAFSCB_InitCallBackState2(struct rx_call *a_call,
1075                             struct interfaceAddr *addr)
1076 {
1077     return RXGEN_OPCODE;
1078 }
1079
1080 /*------------------------------------------------------------------------
1081  * EXPORTED SRXAFSCB_WhoAreYou
1082  *
1083  * Description:
1084  *      Routine called by the server-side callback RPC interface to
1085  *      obtain a unique identifier for the client. The server uses
1086  *      this identifier to figure out whether or not two RX connections
1087  *      are from the same client, and to find out which addresses go
1088  *      with which clients.
1089  *
1090  * Arguments:
1091  *      a_call : Ptr to Rx call on which this request came in.
1092  *      addr: Ptr to return the list of interfaces for this client.
1093  *
1094  * Returns:
1095  *      0 (Always)
1096  *
1097  * Environment:
1098  *      Nothing interesting.
1099  *
1100  * Side Effects:
1101  *      As advertised.
1102  *------------------------------------------------------------------------*/
1103
1104 int
1105 SRXAFSCB_WhoAreYou(struct rx_call *a_call, struct interfaceAddr *addr)
1106 {
1107     int i;
1108     int code = 0;
1109
1110     RX_AFS_GLOCK();
1111
1112     AFS_STATCNT(SRXAFSCB_WhoAreYou);
1113
1114     memset(addr, 0, sizeof(*addr));
1115
1116     ObtainReadLock(&afs_xinterface);
1117
1118     /* return all network interface addresses */
1119     addr->numberOfInterfaces = afs_cb_interface.numberOfInterfaces;
1120     addr->uuid = afs_cb_interface.uuid;
1121     for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
1122         addr->addr_in[i] = ntohl(afs_cb_interface.addr_in[i]);
1123         addr->subnetmask[i] = ntohl(afs_cb_interface.subnetmask[i]);
1124         addr->mtu[i] = ntohl(afs_cb_interface.mtu[i]);
1125     }
1126
1127     ReleaseReadLock(&afs_xinterface);
1128
1129     RX_AFS_GUNLOCK();
1130
1131     return code;
1132 }
1133
1134
1135 /*------------------------------------------------------------------------
1136  * EXPORTED SRXAFSCB_InitCallBackState3
1137  *
1138  * Description:
1139  *      Routine called by the server-side callback RPC interface to
1140  *      implement clearing all callbacks from this host.
1141  *
1142  * Arguments:
1143  *      a_call : Ptr to Rx call on which this request came in.
1144  *
1145  * Returns:
1146  *      0 (always).
1147  *
1148  * Environment:
1149  *      Nothing interesting.
1150  *
1151  * Side Effects:
1152  *      As advertised.
1153  *------------------------------------------------------------------------*/
1154
1155 int
1156 SRXAFSCB_InitCallBackState3(struct rx_call *a_call, afsUUID * a_uuid)
1157 {
1158     int code;
1159
1160     /*
1161      * TBD: Lookup the server by the UUID instead of its IP address.
1162      */
1163     code = SRXAFSCB_InitCallBackState(a_call);
1164
1165     return code;
1166 }
1167
1168
1169 /*------------------------------------------------------------------------
1170  * EXPORTED SRXAFSCB_ProbeUuid
1171  *
1172  * Description:
1173  *      Routine called by the server-side callback RPC interface to
1174  *      implement ``probing'' the Cache Manager, just making sure it's
1175  *      still there is still the same client it used to be.
1176  *
1177  * Arguments:
1178  *      a_call : Ptr to Rx call on which this request came in.
1179  *      a_uuid : Ptr to UUID that must match the client's UUID.
1180  *
1181  * Returns:
1182  *      0 if a_uuid matches the UUID for this client
1183  *      Non-zero otherwize
1184  *
1185  * Environment:
1186  *      Nothing interesting.
1187  *
1188  * Side Effects:
1189  *      As advertised.
1190  *------------------------------------------------------------------------*/
1191
1192 int
1193 SRXAFSCB_ProbeUuid(struct rx_call *a_call, afsUUID * a_uuid)
1194 {
1195     int code = 0;
1196     XSTATS_DECLS;
1197
1198     RX_AFS_GLOCK();
1199     AFS_STATCNT(SRXAFSCB_Probe);
1200
1201     XSTATS_START_CMTIME(AFS_STATS_CM_RPCIDX_PROBE);
1202     if (!afs_uuid_equal(a_uuid, &afs_cb_interface.uuid))
1203         code = 1;               /* failure */
1204     XSTATS_END_TIME;
1205
1206     RX_AFS_GUNLOCK();
1207
1208     return code;
1209 }
1210
1211
1212 /*------------------------------------------------------------------------
1213  * EXPORTED SRXAFSCB_GetServerPrefs
1214  *
1215  * Description:
1216  *      Routine to list server preferences used by this client.
1217  *
1218  * Arguments:
1219  *      a_call  : Ptr to Rx call on which this request came in.
1220  *      a_index : Input server index
1221  *      a_srvr_addr  : Output server address in host byte order
1222  *                     (0xffffffff on last server)
1223  *      a_srvr_rank  : Output server rank
1224  *
1225  * Returns:
1226  *      0 on success
1227  *
1228  * Environment:
1229  *      Nothing interesting.
1230  *
1231  * Side Effects:
1232  *      As advertised.
1233  *------------------------------------------------------------------------*/
1234
1235 int
1236 SRXAFSCB_GetServerPrefs(struct rx_call *a_call, afs_int32 a_index,
1237                         afs_int32 * a_srvr_addr, afs_int32 * a_srvr_rank)
1238 {
1239     int i, j;
1240     struct srvAddr *sa;
1241
1242     RX_AFS_GLOCK();
1243     AFS_STATCNT(SRXAFSCB_GetServerPrefs);
1244
1245     ObtainReadLock(&afs_xserver);
1246
1247     /* Search the hash table for the server with this index */
1248     *a_srvr_addr = 0xffffffff;
1249     *a_srvr_rank = 0xffffffff;
1250     for (i = 0, j = 0; j < NSERVERS && i <= a_index; j++) {
1251         for (sa = afs_srvAddrs[j]; sa && i <= a_index; sa = sa->next_bkt, i++) {
1252             if (i == a_index) {
1253                 *a_srvr_addr = ntohl(sa->sa_ip);
1254                 *a_srvr_rank = sa->sa_iprank;
1255             }
1256         }
1257     }
1258
1259     ReleaseReadLock(&afs_xserver);
1260
1261     RX_AFS_GUNLOCK();
1262
1263     return 0;
1264 }
1265
1266
1267 /*------------------------------------------------------------------------
1268  * EXPORTED SRXAFSCB_GetCellServDB
1269  *
1270  * Description:
1271  *      Routine to list cells configured for this client
1272  *
1273  * Arguments:
1274  *      a_call  : Ptr to Rx call on which this request came in.
1275  *      a_index : Input cell index
1276  *      a_name  : Output cell name ("" on last cell)
1277  *      a_hosts : Output cell database servers in host byte order.
1278  *
1279  * Returns:
1280  *      0 on success
1281  *
1282  * Environment:
1283  *      Nothing interesting.
1284  *
1285  * Side Effects:
1286  *      As advertised.
1287  *------------------------------------------------------------------------*/
1288
1289 int
1290 SRXAFSCB_GetCellServDB(struct rx_call *a_call, afs_int32 a_index,
1291                        char **a_name, serverList * a_hosts)
1292 {
1293     afs_int32 i, j = 0;
1294     struct cell *tcell;
1295     char *t_name, *p_name = NULL;
1296
1297     RX_AFS_GLOCK();
1298     AFS_STATCNT(SRXAFSCB_GetCellServDB);
1299
1300     tcell = afs_GetCellByIndex(a_index, READ_LOCK);
1301
1302     if (!tcell) {
1303         i = 0;
1304         a_hosts->serverList_val = 0;
1305         a_hosts->serverList_len = 0;
1306     } else {
1307         p_name = tcell->cellName;
1308         for (j = 0; j < AFSMAXCELLHOSTS && tcell->cellHosts[j]; j++);
1309         i = strlen(p_name);
1310         a_hosts->serverList_val = afs_osi_Alloc(j * sizeof(afs_int32));
1311         osi_Assert(a_hosts->serverList_val != NULL);
1312         a_hosts->serverList_len = j;
1313         for (j = 0; j < AFSMAXCELLHOSTS && tcell->cellHosts[j]; j++)
1314             a_hosts->serverList_val[j] =
1315                 ntohl(tcell->cellHosts[j]->addr->sa_ip);
1316         afs_PutCell(tcell, READ_LOCK);
1317     }
1318
1319     t_name = afs_osi_Alloc(i + 1);
1320     if (t_name == NULL) {
1321         afs_osi_Free(a_hosts->serverList_val, (j * sizeof(afs_int32)));
1322         RX_AFS_GUNLOCK();
1323         return ENOMEM;
1324     }
1325
1326     t_name[i] = '\0';
1327     if (p_name)
1328         memcpy(t_name, p_name, i);
1329
1330     RX_AFS_GUNLOCK();
1331
1332     *a_name = t_name;
1333     return 0;
1334 }
1335
1336
1337 /*------------------------------------------------------------------------
1338  * EXPORTED SRXAFSCB_GetLocalCell
1339  *
1340  * Description:
1341  *      Routine to return name of client's local cell
1342  *
1343  * Arguments:
1344  *      a_call  : Ptr to Rx call on which this request came in.
1345  *      a_name  : Output cell name
1346  *
1347  * Returns:
1348  *      0 on success
1349  *
1350  * Environment:
1351  *      Nothing interesting.
1352  *
1353  * Side Effects:
1354  *      As advertised.
1355  *------------------------------------------------------------------------*/
1356
1357 int
1358 SRXAFSCB_GetLocalCell(struct rx_call *a_call, char **a_name)
1359 {
1360     int plen;
1361     struct cell *tcell;
1362     char *t_name, *p_name = NULL;
1363
1364     RX_AFS_GLOCK();
1365     AFS_STATCNT(SRXAFSCB_GetLocalCell);
1366
1367     /* Search the list for the primary cell. Cell number 1 is only
1368      * the primary cell is when no other cell is explicitly marked as
1369      * the primary cell.  */
1370     tcell = afs_GetPrimaryCell(READ_LOCK);
1371     if (tcell)
1372         p_name = tcell->cellName;
1373     if (p_name)
1374         plen = strlen(p_name);
1375     else
1376         plen = 0;
1377     t_name = afs_osi_Alloc(plen + 1);
1378     if (t_name == NULL) {
1379         if (tcell)
1380             afs_PutCell(tcell, READ_LOCK);
1381         RX_AFS_GUNLOCK();
1382         return ENOMEM;
1383     }
1384
1385     t_name[plen] = '\0';
1386     if (p_name)
1387         memcpy(t_name, p_name, plen);
1388
1389     RX_AFS_GUNLOCK();
1390
1391     *a_name = t_name;
1392     if (tcell)
1393         afs_PutCell(tcell, READ_LOCK);
1394     return 0;
1395 }
1396
1397
1398 /*
1399  * afs_MarshallCacheConfig - marshall client cache configuration
1400  *
1401  * PARAMETERS
1402  *
1403  * IN callerVersion - the rpc stat version of the caller.
1404  *
1405  * IN config - client cache configuration.
1406  *
1407  * OUT ptr - buffer where configuration is marshalled.
1408  *
1409  * RETURN CODES
1410  *
1411  * Returns void.
1412  */
1413 static void
1414 afs_MarshallCacheConfig(afs_uint32 callerVersion, cm_initparams_v1 * config,
1415                         afs_uint32 * ptr)
1416 {
1417     AFS_STATCNT(afs_MarshallCacheConfig);
1418     /*
1419      * We currently only support version 1.
1420      */
1421     *(ptr++) = config->nChunkFiles;
1422     *(ptr++) = config->nStatCaches;
1423     *(ptr++) = config->nDataCaches;
1424     *(ptr++) = config->nVolumeCaches;
1425     *(ptr++) = config->firstChunkSize;
1426     *(ptr++) = config->otherChunkSize;
1427     *(ptr++) = config->cacheSize;
1428     *(ptr++) = config->setTime;
1429     *(ptr++) = config->memCache;
1430 }
1431
1432
1433 /*------------------------------------------------------------------------
1434  * EXPORTED SRXAFSCB_GetCacheConfig
1435  *
1436  * Description:
1437  *      Routine to return parameters used to initialize client cache.
1438  *      Client may request any format version. Server may not return
1439  *      format version greater than version requested by client.
1440  *
1441  * Arguments:
1442  *      a_call:        Ptr to Rx call on which this request came in.
1443  *      callerVersion: Data format version desired by the client.
1444  *      serverVersion: Data format version of output data.
1445  *      configCount:   Number bytes allocated for output data.
1446  *      config:        Client cache configuration.
1447  *
1448  * Returns:
1449  *      0 on success
1450  *
1451  * Environment:
1452  *      Nothing interesting.
1453  *
1454  * Side Effects:
1455  *      As advertised.
1456  *------------------------------------------------------------------------*/
1457
1458 int
1459 SRXAFSCB_GetCacheConfig(struct rx_call *a_call, afs_uint32 callerVersion,
1460                         afs_uint32 * serverVersion, afs_uint32 * configCount,
1461                         cacheConfig * config)
1462 {
1463     afs_uint32 *t_config;
1464     size_t allocsize;
1465     cm_initparams_v1 cm_config;
1466
1467     RX_AFS_GLOCK();
1468     AFS_STATCNT(SRXAFSCB_GetCacheConfig);
1469
1470     /*
1471      * Currently only support version 1
1472      */
1473     allocsize = sizeof(cm_initparams_v1);
1474     t_config = afs_osi_Alloc(allocsize);
1475     if (t_config == NULL) {
1476         RX_AFS_GUNLOCK();
1477         return ENOMEM;
1478     }
1479
1480     cm_config.nChunkFiles = cm_initParams.cmi_nChunkFiles;
1481     cm_config.nStatCaches = cm_initParams.cmi_nStatCaches;
1482     cm_config.nDataCaches = cm_initParams.cmi_nDataCaches;
1483     cm_config.nVolumeCaches = cm_initParams.cmi_nVolumeCaches;
1484     cm_config.firstChunkSize = cm_initParams.cmi_firstChunkSize;
1485     cm_config.otherChunkSize = cm_initParams.cmi_otherChunkSize;
1486     cm_config.cacheSize = cm_initParams.cmi_cacheSize;
1487     cm_config.setTime = cm_initParams.cmi_setTime;
1488     cm_config.memCache = cm_initParams.cmi_memCache;
1489
1490     afs_MarshallCacheConfig(callerVersion, &cm_config, t_config);
1491
1492     *serverVersion = AFS_CLIENT_RETRIEVAL_FIRST_EDITION;
1493     *configCount = allocsize;
1494     config->cacheConfig_val = t_config;
1495     config->cacheConfig_len = allocsize / sizeof(afs_uint32);
1496
1497     RX_AFS_GUNLOCK();
1498
1499     return 0;
1500 }
1501
1502 /*------------------------------------------------------------------------
1503  * EXPORTED SRXAFSCB_FetchData
1504  *
1505  * Description:
1506  *      Routine to do third party move from a remioserver to the original
1507  *      issuer of an ArchiveData request. Presently supported only by the
1508  *      "fs" command, not by the AFS client.
1509  *
1510  * Arguments:
1511  *      rxcall:        Ptr to Rx call on which this request came in.
1512  *      Fid:           pointer to AFSFid structure.
1513  *      Fd:            File descriptor inside fs command.
1514  *      Position:      Offset in the file.
1515  *      Length:        Data length to transfer.
1516  *      TotalLength:   Pointer to total file length field
1517  *
1518  * Returns:
1519  *      0 on success
1520  *
1521  * Environment:
1522  *      Nothing interesting.
1523  *
1524  * Side Effects:
1525  *------------------------------------------------------------------------*/
1526 int
1527 SRXAFSCB_FetchData(struct rx_call *rxcall, struct AFSFid *Fid, afs_int32 Fd,
1528                    afs_int64 Position, afs_int64 Length,
1529                    afs_int64 * TotalLength)
1530 {
1531     return ENOSYS;
1532 }
1533
1534 /*------------------------------------------------------------------------
1535  * EXPORTED SRXAFSCB_StoreData
1536  *
1537  * Description:
1538  *      Routine to do third party move from a remioserver to the original
1539  *      issuer of a RetrieveData request. Presently supported only by the
1540  *      "fs" command, not by the AFS client.
1541  *
1542  * Arguments:
1543  *      rxcall:        Ptr to Rx call on which this request came in.
1544  *      Fid:           pointer to AFSFid structure.
1545  *      Fd:            File descriptor inside fs command.
1546  *      Position:      Offset in the file.
1547  *      Length:        Data length to transfer.
1548  *      TotalLength:   Pointer to total file length field
1549  *
1550  * Returns:
1551  *      0 on success
1552  *
1553  * Environment:
1554  *      Nothing interesting.
1555  *
1556  * Side Effects:
1557  *      As advertised.
1558  *------------------------------------------------------------------------*/
1559 int
1560 SRXAFSCB_StoreData(struct rx_call *rxcall, struct AFSFid *Fid, afs_int32 Fd,
1561                    afs_int64 Position, afs_int64 Length,
1562                    afs_int64 * TotalLength)
1563 {
1564     return ENOSYS;
1565 }
1566
1567 /*------------------------------------------------------------------------
1568  * EXPORTED SRXAFSCB_GetCellByNum
1569  *
1570  * Description:
1571  *      Routine to get information about a cell specified by its
1572  *      cell number (returned by GetCE/GetCE64).
1573  *
1574  * Arguments:
1575  *      a_call    : Ptr to Rx call on which this request came in.
1576  *      a_cellnum : Input cell number
1577  *      a_name    : Output cell name (one zero byte when no such cell).
1578  *      a_hosts   : Output cell database servers in host byte order.
1579  *
1580  * Returns:
1581  *      0 on success
1582  *
1583  * Environment:
1584  *      Nothing interesting.
1585  *
1586  * Side Effects:
1587  *      As advertised.
1588  *------------------------------------------------------------------------*/
1589
1590 int
1591 SRXAFSCB_GetCellByNum(struct rx_call *a_call, afs_int32 a_cellnum,
1592                       char **a_name, serverList * a_hosts)
1593 {
1594     afs_int32 i, sn;
1595     struct cell *tcell;
1596
1597     RX_AFS_GLOCK();
1598     AFS_STATCNT(SRXAFSCB_GetCellByNum);
1599
1600     a_hosts->serverList_val = 0;
1601     a_hosts->serverList_len = 0;
1602
1603     tcell = afs_GetCellStale(a_cellnum, READ_LOCK);
1604     if (!tcell) {
1605         *a_name = afs_strdup("");
1606         RX_AFS_GUNLOCK();
1607         return 0;
1608     }
1609
1610     ObtainReadLock(&tcell->lock);
1611     *a_name = afs_strdup(tcell->cellName);
1612
1613     for (sn = 0; sn < AFSMAXCELLHOSTS && tcell->cellHosts[sn]; sn++);
1614     a_hosts->serverList_len = sn;
1615     a_hosts->serverList_val = afs_osi_Alloc(sn * sizeof(afs_int32));
1616     osi_Assert(a_hosts->serverList_val != NULL);
1617
1618     for (i = 0; i < sn; i++)
1619         a_hosts->serverList_val[i] = ntohl(tcell->cellHosts[i]->addr->sa_ip);
1620     ReleaseReadLock(&tcell->lock);
1621     afs_PutCell(tcell, READ_LOCK);
1622
1623     RX_AFS_GUNLOCK();
1624     return 0;
1625 }
1626
1627 int
1628 SRXAFSCB_TellMeAboutYourself(struct rx_call *a_call,
1629                              struct interfaceAddr *addr,
1630                              Capabilities * capabilities)
1631 {
1632     int i;
1633     int code = 0;
1634     afs_uint32 *dataBuffP;
1635     afs_int32 dataBytes;
1636
1637     RX_AFS_GLOCK();
1638
1639     AFS_STATCNT(SRXAFSCB_WhoAreYou);
1640
1641     ObtainReadLock(&afs_xinterface);
1642
1643     /* return all network interface addresses */
1644     addr->numberOfInterfaces = afs_cb_interface.numberOfInterfaces;
1645     addr->uuid = afs_cb_interface.uuid;
1646     for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
1647         addr->addr_in[i] = ntohl(afs_cb_interface.addr_in[i]);
1648         addr->subnetmask[i] = ntohl(afs_cb_interface.subnetmask[i]);
1649         addr->mtu[i] = ntohl(afs_cb_interface.mtu[i]);
1650     }
1651
1652     ReleaseReadLock(&afs_xinterface);
1653
1654     RX_AFS_GUNLOCK();
1655
1656     dataBytes = 1 * sizeof(afs_uint32);
1657     dataBuffP = afs_osi_Alloc(dataBytes);
1658     osi_Assert(dataBuffP != NULL);
1659     dataBuffP[0] = CLIENT_CAPABILITY_ERRORTRANS;
1660     capabilities->Capabilities_len = dataBytes / sizeof(afs_uint32);
1661     capabilities->Capabilities_val = dataBuffP;
1662
1663     return code;
1664 }
1665
1666
1667 #if 0 && defined(AFS_LINUX24_ENV)
1668 extern struct vcache *afs_globalVp;
1669
1670 int recurse_dcache_parent(parent, a_index, addr, inode, flags, time, fileName)
1671      struct dentry * parent;
1672     afs_int32 a_index;
1673     afs_int32 *addr;
1674     afs_int32 *inode;
1675     afs_int32 *flags;
1676     afs_int32 *time;
1677     char ** fileName;
1678 {
1679         struct dentry *this_parent = parent;
1680         struct list_head *next;
1681         int found = 0;
1682         struct dentry *dentry;
1683
1684 repeat:
1685         next = this_parent->d_subdirs.next;
1686 resume:
1687         while (next != &this_parent->d_subdirs) {
1688                 struct list_head *tmp = next;
1689                 dentry = list_entry(tmp, struct dentry, d_child);
1690                 if (a_index == 0)
1691                   goto searchdone3;
1692                 a_index--;
1693                 next = tmp->next;
1694                 /*
1695                  * Descend a level if the d_subdirs list is non-empty.
1696                  */
1697                 if (!list_empty(&dentry->d_subdirs)) {
1698                         this_parent = dentry;
1699                         goto repeat;
1700                 }
1701         }
1702         /*
1703          * All done at this level ... ascend and resume the search.
1704          */
1705         if (this_parent != parent) {
1706                 next = this_parent->d_child.next;
1707                 this_parent = this_parent->d_parent;
1708                 goto resume;
1709         }
1710         goto ret;
1711
1712  searchdone3:
1713     if (d_unhashed(dentry))
1714       *flags = 1;
1715     else
1716       *flags = 0;
1717
1718     *fileName = afs_strdup(dentry->d_name.name?dentry->d_name.name:"");
1719     *inode = ITOAFS(dentry->d_inode);
1720     *addr = atomic_read(&(dentry)->d_count);
1721     *time = dentry->d_time;
1722
1723     return 0;
1724  ret:
1725     return 1;
1726 }
1727 #endif
1728
1729 int
1730 SRXAFSCB_GetDE(struct rx_call *a_call, afs_int32 a_index, afs_int32 *addr,
1731                afs_int32 *inode, afs_int32 *flags, afs_int32 *time,
1732                char ** fileName)
1733 { /*SRXAFSCB_GetDE*/
1734     int code = 0;                               /*Return code*/
1735 #if 0 && defined(AFS_LINUX24_ENV)
1736     int i;                      /*Loop variable*/
1737     struct vcache *tvc = afs_globalVp;
1738     struct dentry *dentry;
1739     struct list_head *cur, *head = &(AFSTOI(tvc))->i_dentry;
1740
1741 #ifdef RX_ENABLE_LOCKS
1742     AFS_GLOCK();
1743 #endif /* RX_ENABLE_LOCKS */
1744
1745 #if defined(AFS_LINUX24_ENV)
1746     spin_lock(&dcache_lock);
1747 #endif
1748
1749     cur = head;
1750     while ((cur = cur->next) != head) {
1751       dentry = list_entry(cur, struct dentry, d_alias);
1752
1753       dget_locked(dentry);
1754
1755 #if defined(AFS_LINUX24_ENV)
1756       spin_unlock(&dcache_lock);
1757 #endif
1758       if (a_index == 0)
1759         goto searchdone2;
1760       a_index--;
1761
1762       if (recurse_dcache_parent(dentry, a_index, addr, inode, flags, time, fileName) == 0) {
1763         dput(dentry);
1764         code = 0;
1765         goto fcnDone;
1766       }
1767       dput(dentry);
1768     }
1769  searchdone2:
1770     if (cur == head) {
1771         /*Past EOF*/
1772         code = 1;
1773         *fileName = afs_strdup("");
1774         goto fcnDone;
1775     }
1776
1777     if (d_unhashed(dentry))
1778       *flags = 1;
1779     else
1780       *flags = 0;
1781
1782     *fileName = afs_strdup(dentry->d_name.name?dentry->d_name.name:"");
1783     *inode = ITOAFS(dentry->d_inode);
1784     *addr = atomic_read(&(dentry)->d_count);
1785     *time = dentry->d_time;
1786
1787     dput(dentry);
1788     code = 0;
1789
1790 fcnDone:
1791
1792 #ifdef RX_ENABLE_LOCKS
1793     AFS_GUNLOCK();
1794 #endif /* RX_ENABLE_LOCKS */
1795 #endif
1796     return(code);
1797
1798 } /*SRXAFSCB_GetDE*/