7416249d9fa0b5cda229a72649c16121e4218414
[openafs.git] / src / afs / afs_cell.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  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "../afs/param.h"
15
16 RCSID("$Header$");
17
18 #include "../afs/stds.h"
19 #include "../afs/sysincludes.h" /* Standard vendor system headers */
20
21 #if !defined(UKERNEL)
22 #include <net/if.h>
23 #include <netinet/in.h>
24
25 #ifdef AFS_SGI62_ENV
26 #include "../h/hashing.h"
27 #endif
28 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
29 #include <netinet/in_var.h>
30 #endif /* ! ASF_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
32
33 #include "../afs/afsincludes.h" /* Afs-based standard headers */
34 #include "../afs/afs_stats.h"   /* afs statistics */
35
36 #if     defined(AFS_SUN56_ENV)
37 #include <inet/led.h>
38 #include <inet/common.h>
39 #if     defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
41 #endif
42 #include <inet/ip.h>
43 #endif
44
45 /* Exported variables */
46 afs_rwlock_t afs_xcell;                 /* allocation lock for cells */
47 struct afs_q CellLRU;
48 afs_int32 afs_cellindex=0;
49 afs_uint32 afs_nextCellNum = 0x100;
50
51
52 /* Local variables. */
53 struct cell *afs_rootcell = 0;
54
55 /* Handler waiting for request from client */
56 static char afs_AfsdbHandlerWait;
57 /* Client waiting for handler to become available or finish request */
58 static char afs_AfsdbLookupWait;
59
60 /* Set to 1 when we've seen the userspace AFSDB process at least once */
61 char afs_AfsdbHandlerPresent = 0;
62 /* Set to 1 when there is a client interacting with the AFSDB handler.
63  * Protects the in and out variables below.  Protected by GLOCK. */
64 char afs_AfsdbHandlerInuse = 0;
65 /* Set to 1 when AFSDB has been shut down */
66 char afs_AfsdbHandlerShutdown = 0;
67
68 /* Input to handler from the client: cell name to look up */
69 char *afs_AfsdbHandler_CellName;
70 /* Outputs from handler to client: cell hosts, TTL, and real cell name */
71 afs_int32 *afs_AfsdbHandler_CellHosts;
72 int *afs_AfsdbHandler_Timeout;
73 char **afs_AfsdbHandler_RealName;
74
75 /* Client sets ReqPending to 1 whenever it queues a request for it */
76 char afs_AfsdbHandler_ReqPending = 0;
77 /* Handler sets Completed to 1 when it completes the client request */
78 char afs_AfsdbHandler_Completed = 0;
79
80
81 struct cell *afs_GetCellByName2();
82
83 int afs_strcasecmp(s1, s2)
84     register char *s1, *s2;
85 {
86     while (*s1 && *s2) {
87         register char c1, c2;
88
89         c1 = *s1++;
90         c2 = *s2++;
91         if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20;
92         if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20;
93         if (c1 != c2)
94             return c1-c2;
95     }
96
97     return *s1 - *s2;
98 }
99
100
101 #ifdef AFS_AFSDB_ENV
102 void afs_StopAfsdb()
103 {
104     if (afs_AfsdbHandlerPresent) {
105         afs_osi_Wakeup(&afs_AfsdbHandlerWait);
106     } else {
107         afs_AfsdbHandlerShutdown = 1;
108         afs_termState = AFSOP_STOP_RXEVENT;
109     }
110 }
111
112 int afs_AfsdbHandler(acellName, acellNameLen, kernelMsg)
113     char *acellName;
114     int acellNameLen;
115     afs_int32 *kernelMsg;
116 {
117     /* afs_syscall_call() has already grabbed the global lock */
118
119     if (afs_AfsdbHandlerShutdown) return -2;
120     afs_AfsdbHandlerPresent = 1;
121
122     if (afs_AfsdbHandler_ReqPending) {
123         int i, hostCount;
124
125         hostCount = kernelMsg[0];
126         *afs_AfsdbHandler_Timeout = kernelMsg[1];
127         if (*afs_AfsdbHandler_Timeout) *afs_AfsdbHandler_Timeout += osi_Time();
128
129         *afs_AfsdbHandler_RealName = afs_osi_Alloc(strlen(acellName) + 1);
130         strcpy(*afs_AfsdbHandler_RealName, acellName);
131
132         for (i=0; i<MAXCELLHOSTS; i++) {
133             if (i >= hostCount)
134                 afs_AfsdbHandler_CellHosts[i] = 0;
135             else
136                 afs_AfsdbHandler_CellHosts[i] = kernelMsg[2+i];
137         }
138
139         /* Request completed, wake up the relevant thread */
140         afs_AfsdbHandler_ReqPending = 0;
141         afs_AfsdbHandler_Completed = 1;
142         afs_osi_Wakeup(&afs_AfsdbLookupWait);
143     }
144
145     /* Wait for a request */
146     while (afs_AfsdbHandler_ReqPending == 0 && afs_termState != AFSOP_STOP_AFSDB)
147         afs_osi_Sleep(&afs_AfsdbHandlerWait);
148
149     /* Check if we're shutting down */
150     if (afs_termState == AFSOP_STOP_AFSDB) {
151         /* Inform anyone waiting for us that we're going away */
152         afs_AfsdbHandlerShutdown = 1;
153         afs_AfsdbHandlerPresent = 0;
154         afs_osi_Wakeup(&afs_AfsdbLookupWait);
155
156         afs_termState = AFSOP_STOP_RXEVENT;
157         afs_osi_Wakeup(&afs_termState);
158         return -2;
159     }
160
161     /* Copy the requested cell name into the request buffer */
162     strncpy(acellName, afs_AfsdbHandler_CellName, acellNameLen);
163
164     /* Return the lookup request to userspace */    
165     return 0;
166 }
167 #endif
168
169
170 int afs_GetCellHostsFromDns(acellName, acellHosts, timeout, realName)
171     char *acellName;
172     afs_int32 *acellHosts;
173     int *timeout;
174     char **realName;
175 {
176 #ifdef AFS_AFSDB_ENV
177     char grab_glock = 0;
178
179     if (!afs_AfsdbHandlerPresent) return ENOENT;
180
181     /* Initialize host list to empty in case the handler is gone */
182     *acellHosts = 0;
183
184     if (!ISAFS_GLOCK()) {
185         grab_glock = 1;
186         AFS_GLOCK();
187     }
188
189     /* Wait until the AFSDB handler is available, and grab it */
190     while (afs_AfsdbHandlerInuse)
191         afs_osi_Sleep(&afs_AfsdbLookupWait);
192     afs_AfsdbHandlerInuse = 1;
193
194     /* Set up parameters for the handler */
195     afs_AfsdbHandler_CellName = acellName;
196     afs_AfsdbHandler_CellHosts = acellHosts;
197     afs_AfsdbHandler_Timeout = timeout;
198     afs_AfsdbHandler_RealName = realName;
199
200     /* Wake up the AFSDB handler */
201     afs_AfsdbHandler_Completed = 0;
202     afs_AfsdbHandler_ReqPending = 1;
203     afs_osi_Wakeup(&afs_AfsdbHandlerWait);
204
205     /* Wait for the handler to get back to us with the reply */
206     while (afs_AfsdbHandlerPresent && !afs_AfsdbHandler_Completed)
207         afs_osi_Sleep(&afs_AfsdbLookupWait);
208
209     /* Release the AFSDB handler and wake up others waiting for it */
210     afs_AfsdbHandlerInuse = 0;
211     afs_osi_Wakeup(&afs_AfsdbLookupWait);
212
213     if (grab_glock) AFS_GUNLOCK();
214
215     if (*acellHosts) return 0;
216     return ENOENT;
217 #else
218     return ENOENT;
219 #endif
220 }
221
222
223 void afs_RefreshCell(ac)
224     register struct cell *ac;
225 {
226     afs_int32 cellHosts[MAXCELLHOSTS];
227     char *realName = NULL;
228     struct cell *tc;
229     int timeout;
230
231     /* Don't need to do anything if no timeout or it's not expired */
232     if (!ac->timeout || ac->timeout > osi_Time()) return;
233
234     if (afs_GetCellHostsFromDns(ac->cellName, cellHosts, &timeout, &realName))
235         /* In case of lookup failure, keep old data */
236         goto done;
237
238     /* Refresh the DB servers for the real cell; other values stay the same. */
239     afs_NewCell(realName, cellHosts, 0, (char *) 0, 0, 0, timeout, (char *) 0);
240
241     /* If this is an alias, update the alias entry too */
242     if (afs_strcasecmp(ac->cellName, realName)) {
243         /*
244          * Look up the entry we just updated, to compensate for
245          * uppercase-vs-lowercase lossage with DNS.
246          */
247         tc = afs_GetCellByName2(realName, READ_LOCK, 0 /* no AFSDB */);
248
249         if (tc) {
250             afs_NewCell(ac->cellName, 0, CAlias, (char *) 0, 0, 0,
251                         timeout, tc->cellName);
252             afs_PutCell(tc, READ_LOCK);
253         }
254     }
255
256 done:
257     if (realName)
258         afs_osi_Free(realName, strlen(realName) + 1);
259 }
260
261
262 struct cell *afs_GetCellByName_Dns(acellName, locktype)
263     register char *acellName;
264     afs_int32 locktype;
265 {
266     afs_int32 cellHosts[MAXCELLHOSTS];
267     char *realName = NULL;
268     struct cell *tc;
269     int timeout;
270
271     if (afs_GetCellHostsFromDns(acellName, cellHosts, &timeout, &realName))
272         goto bad;
273     if (afs_NewCell(realName, cellHosts, CNoSUID, (char *) 0, 0, 0,
274                     timeout, (char *) 0))
275         goto bad;
276
277     /* If this is an alias, create an entry for it too */
278     if (afs_strcasecmp(acellName, realName)) {
279         /*
280          * Look up the entry we just updated, to compensate for
281          * uppercase-vs-lowercase lossage with DNS.
282          */
283         tc = afs_GetCellByName2(realName, READ_LOCK, 0 /* no AFSDB */);
284         if (!tc)
285             goto bad;
286
287         if (afs_NewCell(acellName, 0, CAlias, (char *) 0, 0, 0,
288                         timeout, tc->cellName)) {
289             afs_PutCell(tc, READ_LOCK);
290             goto bad;
291         }
292
293         afs_PutCell(tc, READ_LOCK);
294     }
295
296     if (realName)
297         afs_osi_Free(realName, strlen(realName) + 1);
298     return afs_GetCellByName2(acellName, locktype, 0);
299
300 bad:
301     if (realName)
302         afs_osi_Free(realName, strlen(realName) + 1);
303     return (struct cell *) 0;
304 }
305
306
307 struct cell *afs_GetCellByName2(acellName, locktype, trydns)
308     register char *acellName;
309     afs_int32 locktype;
310     char trydns;
311 {
312     register struct cell *tc;
313     register struct afs_q *cq, *tq;
314     int didAlias = 0;
315
316     AFS_STATCNT(afs_GetCellByName);
317 retry:
318     ObtainWriteLock(&afs_xcell,100);
319     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
320         tc = QTOC(cq); tq = QNext(cq);
321         if (!afs_strcasecmp(tc->cellName, acellName)) {
322             QRemove(&tc->lruq);
323             QAdd(&CellLRU, &tc->lruq);
324             ReleaseWriteLock(&afs_xcell);
325             afs_RefreshCell(tc);
326             if ((tc->states & CAlias) && (didAlias == 0)) {
327                 acellName = tc->realName;
328                 if (!acellName) return (struct cell *) 0;
329                 didAlias = 1;
330                 goto retry;
331             }
332             return tc;
333         }
334     }
335     ReleaseWriteLock(&afs_xcell);
336     return trydns ? afs_GetCellByName_Dns(acellName, locktype)
337                   : (struct cell *) 0;
338
339 } /*afs_GetCellByName2*/
340
341
342 struct cell *afs_GetCellByName(acellName, locktype)
343     register char *acellName;
344     afs_int32 locktype;
345 {
346     return afs_GetCellByName2(acellName, locktype, 1);
347
348 } /*afs_GetCellByName*/
349
350
351 struct cell *afs_GetCell(acell, locktype)
352     register afs_int32 acell;
353     afs_int32 locktype;
354 {
355     register struct cell *tc;
356     register struct afs_q *cq, *tq;
357
358     AFS_STATCNT(afs_GetCell);
359     if (acell == 1 && afs_rootcell) return afs_rootcell;
360     ObtainWriteLock(&afs_xcell,101);
361     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
362         tc = QTOC(cq); tq = QNext(cq);
363         if (tc->cell == acell) {
364             QRemove(&tc->lruq);
365             QAdd(&CellLRU, &tc->lruq);
366             ReleaseWriteLock(&afs_xcell);
367             afs_RefreshCell(tc);
368             return tc;
369         }
370     }
371     ReleaseWriteLock(&afs_xcell);
372     return (struct cell *) 0;
373
374 } /*afs_GetCell*/
375
376
377 struct cell *afs_GetCellByIndex(cellindex, locktype, refresh)
378     register afs_int32 cellindex;
379     afs_int32 locktype;
380     afs_int32 refresh;
381 {
382     register struct cell *tc;
383     register struct afs_q *cq, *tq;
384
385     AFS_STATCNT(afs_GetCellByIndex);
386     ObtainWriteLock(&afs_xcell,102);
387     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
388         tc = QTOC(cq); tq = QNext(cq);
389         if (tc->cellIndex == cellindex) {
390             QRemove(&tc->lruq);
391             QAdd(&CellLRU, &tc->lruq);
392             ReleaseWriteLock(&afs_xcell);
393             if (refresh) afs_RefreshCell(tc);
394             return tc;
395         }
396     }
397     ReleaseWriteLock(&afs_xcell);
398     return (struct cell *) 0;
399
400 } /*afs_GetCellByIndex*/
401
402
403 afs_int32 afs_NewCell(acellName, acellHosts, aflags, linkedcname, fsport, vlport, timeout, aliasFor)
404     int aflags;
405     char *acellName;
406     register afs_int32 *acellHosts;
407     char *linkedcname;
408     u_short fsport, vlport;
409     int timeout;
410     char *aliasFor;
411 {
412     register struct cell *tc, *tcl=0;
413     register afs_int32 i, newc=0, code=0;
414     register struct afs_q *cq, *tq;
415
416     AFS_STATCNT(afs_NewCell);
417     if (!(aflags & CAlias) && *acellHosts == 0)
418         /* need >= one host to gen cell # */
419         return EINVAL;
420
421     ObtainWriteLock(&afs_xcell,103);
422
423     /* Find the cell and mark its servers as not down but gone */
424     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
425         tc = QTOC(cq); tq = QNext(cq);
426         if (afs_strcasecmp(tc->cellName, acellName) == 0) {
427             /* if the cell we've found has the correct name but no timeout,
428              * and we're called with a non-zero timeout, bail out:  never
429              * override static configuration entries with AFSDB ones. */
430             if (timeout && !tc->timeout) {
431                 ReleaseWriteLock(&afs_xcell);
432                 return 0;
433             }
434             /* we don't want to keep pinging old vlservers which were down,
435              * since they don't matter any more.  It's easier to do this than
436              * to remove the server from its various hash tables. */
437             for (i=0; i<MAXCELLHOSTS; i++) {
438                 if (!tc->cellHosts[i]) break;
439                 tc->cellHosts[i]->flags &= ~SRVR_ISDOWN;
440                 tc->cellHosts[i]->flags |= SRVR_ISGONE;
441             }
442             break;
443         }
444     }
445
446     if (cq != &CellLRU) {
447         aflags &= ~CNoSUID;
448     }
449     else {
450         tc = (struct cell *) afs_osi_Alloc(sizeof(struct cell));
451         memset((char *)tc, 0, sizeof(*tc));
452         QAdd(&CellLRU, &tc->lruq);                      /* put in lruq */
453         tc->cellName = (char *) afs_osi_Alloc(strlen(acellName)+1);
454         strcpy(tc->cellName, acellName);
455         tc->cellIndex = afs_cellindex++;
456         if (aflags & CPrimary) {
457             extern int afs_rootCellIndex;
458             tc->cell = 1;       /* primary cell is always 1 */
459             afs_rootcell = tc;
460             afs_rootCellIndex = tc->cellIndex;
461         } else {
462             tc->cell = afs_nextCellNum++;
463         }
464         tc->states = 0;
465         tc->lcellp = (struct cell *)0;
466         tc->fsport = (fsport ? fsport : AFS_FSPORT);
467         tc->vlport = (vlport ? vlport : AFS_VLPORT);
468         afs_stats_cmperf.numCellsVisible++;
469         newc++;
470     }
471
472     if (aflags & CLinkedCell) {
473         if (!linkedcname) {
474             code = EINVAL;
475             goto bad;
476         }
477         for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
478             tcl = QTOC(cq); tq = QNext(cq);
479             if (!afs_strcasecmp(tcl->cellName, linkedcname)) {
480                 break;
481             }
482             tcl = 0;
483         }
484         if (!tcl) {
485             code = ENOENT;
486             goto bad;
487         }
488         if (tcl->lcellp) {      /* XXX Overwriting if one existed before! XXX */
489             tcl->lcellp->lcellp = (struct cell *)0;
490             tcl->lcellp->states &= ~CLinkedCell;
491         }
492         tc->lcellp = tcl;
493         tcl->lcellp = tc;
494     }
495     tc->states |= aflags;
496     tc->timeout = timeout;
497
498     /* Allow converting an alias into a real cell */
499     if (!(aflags & CAlias)) tc->states &= ~CAlias;
500  
501     memset((char *)tc->cellHosts, 0, sizeof(tc->cellHosts));
502     if (aflags & CAlias) {
503         if (!aliasFor) {
504             code = EINVAL;
505             goto bad;
506         }
507         if (tc->realName) afs_osi_Free(tc->realName, strlen(tc->realName)+1);
508         tc->realName = (char *) afs_osi_Alloc(strlen(aliasFor)+1);
509         strcpy(tc->realName, aliasFor);
510         goto done;
511     }
512
513     for (i=0; i<MAXCELLHOSTS; i++) {
514         struct server *ts;
515         afs_uint32 temp = acellHosts[i];
516         if (!temp) break;
517         ts = afs_GetServer(&temp, 1, 0, tc->vlport, WRITE_LOCK, (afsUUID *)0, 0);
518         ts->cell = tc;
519         ts->flags &= ~SRVR_ISGONE;
520         tc->cellHosts[i] = ts;
521         afs_PutServer(ts, WRITE_LOCK);
522     }
523     afs_SortServers(tc->cellHosts, MAXCELLHOSTS);       /* randomize servers */
524 done:
525     ReleaseWriteLock(&afs_xcell);
526     return 0;
527 bad:
528     if (newc) {
529         QRemove(&tc->lruq);
530         afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
531         afs_osi_Free((char *)tc, sizeof(struct cell));
532     }
533     ReleaseWriteLock(&afs_xcell);
534     return code;
535
536 } /*afs_NewCell*/
537
538 afs_RemoveCellEntry(struct server *srvp)
539 {
540   struct cell *tc;
541   afs_int32 j, k;
542
543   tc = srvp->cell;
544   if (!tc) return;
545
546   /* Remove the server structure from the cell list - if there */
547   ObtainWriteLock(&afs_xcell,200);
548   for (j=k=0; j<MAXCELLHOSTS; j++) {
549      if (!tc->cellHosts[j]) break;
550      if (tc->cellHosts[j] != srvp) {
551         tc->cellHosts[k++] = tc->cellHosts[j];
552      }
553   }
554   if (k == 0) {
555      /* What do we do if we remove the last one? */
556   }
557   for (; k<MAXCELLHOSTS; k++) {
558      tc->cellHosts[k] = 0;
559   }
560   ReleaseWriteLock(&afs_xcell);
561 }
562