2 * Copyright 2000, International Business Machines Corporation and others.
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
13 #include <afsconfig.h>
14 #include "../afs/param.h"
18 #include "../afs/stds.h"
19 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include <netinet/in.h>
26 #include "../h/hashing.h"
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) */
33 #include "../afs/afsincludes.h" /* Afs-based standard headers */
34 #include "../afs/afs_stats.h" /* afs statistics */
36 #if defined(AFS_SUN56_ENV)
38 #include <inet/common.h>
39 #if defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
45 /* Exported variables */
46 afs_rwlock_t afs_xcell; /* allocation lock for cells */
48 afs_int32 afs_cellindex=0;
49 afs_uint32 afs_nextCellNum = 0x100;
52 /* Local variables. */
53 struct cell *afs_rootcell = 0;
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;
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;
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;
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;
81 static struct cell *afs_GetCellByName_int();
83 int afs_strcasecmp(s1, s2)
84 register char *s1, *s2;
91 if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20;
92 if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20;
104 if (afs_AfsdbHandlerPresent) {
105 afs_osi_Wakeup(&afs_AfsdbHandlerWait);
107 afs_AfsdbHandlerShutdown = 1;
108 afs_termState = AFSOP_STOP_RXEVENT;
112 int afs_AfsdbHandler(acellName, acellNameLen, kernelMsg)
115 afs_int32 *kernelMsg;
117 /* afs_syscall_call() has already grabbed the global lock */
119 if (afs_AfsdbHandlerShutdown) return -2;
120 afs_AfsdbHandlerPresent = 1;
122 if (afs_AfsdbHandler_ReqPending) {
125 hostCount = kernelMsg[0];
126 *afs_AfsdbHandler_Timeout = kernelMsg[1];
127 if (*afs_AfsdbHandler_Timeout) *afs_AfsdbHandler_Timeout += osi_Time();
129 *afs_AfsdbHandler_RealName = afs_osi_Alloc(strlen(acellName) + 1);
130 strcpy(*afs_AfsdbHandler_RealName, acellName);
132 for (i=0; i<MAXCELLHOSTS; i++) {
134 afs_AfsdbHandler_CellHosts[i] = 0;
136 afs_AfsdbHandler_CellHosts[i] = kernelMsg[2+i];
139 /* Request completed, wake up the relevant thread */
140 afs_AfsdbHandler_ReqPending = 0;
141 afs_AfsdbHandler_Completed = 1;
142 afs_osi_Wakeup(&afs_AfsdbLookupWait);
145 /* Wait for a request */
146 while (afs_AfsdbHandler_ReqPending == 0 && afs_termState != AFSOP_STOP_AFSDB)
147 afs_osi_Sleep(&afs_AfsdbHandlerWait);
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);
156 afs_termState = AFSOP_STOP_RXEVENT;
157 afs_osi_Wakeup(&afs_termState);
161 /* Copy the requested cell name into the request buffer */
162 strncpy(acellName, afs_AfsdbHandler_CellName, acellNameLen);
164 /* Return the lookup request to userspace */
170 int afs_GetCellHostsFromDns(acellName, acellHosts, timeout, realName)
172 afs_int32 *acellHosts;
179 if (!afs_AfsdbHandlerPresent) return ENOENT;
181 /* Initialize host list to empty in case the handler is gone */
184 if (!ISAFS_GLOCK()) {
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;
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;
200 /* Wake up the AFSDB handler */
201 afs_AfsdbHandler_Completed = 0;
202 afs_AfsdbHandler_ReqPending = 1;
203 afs_osi_Wakeup(&afs_AfsdbHandlerWait);
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);
209 /* Release the AFSDB handler and wake up others waiting for it */
210 afs_AfsdbHandlerInuse = 0;
211 afs_osi_Wakeup(&afs_AfsdbLookupWait);
213 if (grab_glock) AFS_GUNLOCK();
215 if (*acellHosts) return 0;
223 void afs_RefreshCell(tc)
224 register struct cell *tc;
226 afs_int32 cellHosts[MAXCELLHOSTS];
227 char *realName = NULL;
230 /* Don't need to do anything if no timeout or it's not expired */
231 if (!tc->timeout || tc->timeout > osi_Time()) return;
233 if (afs_GetCellHostsFromDns(tc->cellName, cellHosts, &timeout, &realName))
234 /* In case of lookup failure, keep old data */
237 /* Refresh the DB servers for the real cell; other values stay the same. */
238 afs_NewCell(realName, cellHosts, 0, (char *) 0, 0, 0, timeout, (char *) 0);
240 /* If this is an alias, update the alias entry too */
241 if (afs_strcasecmp(tc->cellName, realName))
242 afs_NewCell(tc->cellName, 0, CAlias, (char *) 0, 0, 0,
247 afs_osi_Free(realName, strlen(realName) + 1);
251 struct cell *afs_GetCellByName_Dns(acellName, locktype)
252 register char *acellName;
255 afs_int32 cellHosts[MAXCELLHOSTS];
256 char *realName = NULL;
259 if (afs_GetCellHostsFromDns(acellName, cellHosts, &timeout, &realName))
261 if (afs_NewCell(realName, cellHosts, CNoSUID, (char *) 0, 0, 0,
262 timeout, (char *) 0))
265 /* If this is an alias, create an entry for it too */
266 if (afs_strcasecmp(acellName, realName)) {
267 if (afs_NewCell(acellName, 0, CAlias, (char *) 0, 0, 0,
273 afs_osi_Free(realName, strlen(realName) + 1);
274 return afs_GetCellByName_int(acellName, locktype, 0);
278 afs_osi_Free(realName, strlen(realName) + 1);
279 return (struct cell *) 0;
283 static struct cell *afs_GetCellByName_int(acellName, locktype, trydns)
284 register char *acellName;
288 register struct cell *tc;
289 register struct afs_q *cq, *tq;
291 AFS_STATCNT(afs_GetCellByName);
292 ObtainWriteLock(&afs_xcell,100);
293 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
294 tc = QTOC(cq); tq = QNext(cq);
295 if (!afs_strcasecmp(tc->cellName, acellName)) {
297 QAdd(&CellLRU, &tc->lruq);
298 ReleaseWriteLock(&afs_xcell);
300 if (tc->states & CAlias) {
307 ReleaseWriteLock(&afs_xcell);
308 return trydns ? afs_GetCellByName_Dns(acellName, locktype)
311 } /*afs_GetCellByName_int*/
314 struct cell *afs_GetCellByName(acellName, locktype)
315 register char *acellName;
318 return afs_GetCellByName_int(acellName, locktype, 1);
320 } /*afs_GetCellByName*/
323 struct cell *afs_GetCell(acell, locktype)
324 register afs_int32 acell;
327 register struct cell *tc;
328 register struct afs_q *cq, *tq;
330 AFS_STATCNT(afs_GetCell);
331 if (acell == 1 && afs_rootcell) return afs_rootcell;
332 ObtainWriteLock(&afs_xcell,101);
333 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
334 tc = QTOC(cq); tq = QNext(cq);
335 if (tc->cell == acell) {
337 QAdd(&CellLRU, &tc->lruq);
338 ReleaseWriteLock(&afs_xcell);
343 ReleaseWriteLock(&afs_xcell);
344 return (struct cell *) 0;
349 struct cell *afs_GetCellByIndex(cellindex, locktype)
350 register afs_int32 cellindex;
353 register struct cell *tc;
354 register struct afs_q *cq, *tq;
356 AFS_STATCNT(afs_GetCellByIndex);
357 ObtainWriteLock(&afs_xcell,102);
358 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
359 tc = QTOC(cq); tq = QNext(cq);
360 if (tc->cellIndex == cellindex) {
362 QAdd(&CellLRU, &tc->lruq);
363 ReleaseWriteLock(&afs_xcell);
368 ReleaseWriteLock(&afs_xcell);
369 return (struct cell *) 0;
371 } /*afs_GetCellByIndex*/
374 afs_int32 afs_NewCell(acellName, acellHosts, aflags, linkedcname, fsport, vlport, timeout, aliasFor)
377 register afs_int32 *acellHosts;
379 u_short fsport, vlport;
383 register struct cell *tc, *tcl=0;
384 register afs_int32 i, newc=0, code=0;
385 register struct afs_q *cq, *tq;
387 AFS_STATCNT(afs_NewCell);
388 if (!(aflags & CAlias) && *acellHosts == 0)
389 /* need >= one host to gen cell # */
392 ObtainWriteLock(&afs_xcell,103);
394 /* Find the cell and mark its servers as not down but gone */
395 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
396 tc = QTOC(cq); tq = QNext(cq);
397 if (afs_strcasecmp(tc->cellName, acellName) == 0) {
398 /* if the cell we've found has the correct name but no timeout,
399 * and we're called with a non-zero timeout, bail out: never
400 * override static configuration entries with AFSDB ones. */
401 if (timeout && !tc->timeout) {
402 ReleaseWriteLock(&afs_xcell);
405 /* we don't want to keep pinging old vlservers which were down,
406 * since they don't matter any more. It's easier to do this than
407 * to remove the server from its various hash tables. */
408 for (i=0; i<MAXCELLHOSTS; i++) {
409 if (!tc->cellHosts[i]) break;
410 tc->cellHosts[i]->flags &= ~SRVR_ISDOWN;
411 tc->cellHosts[i]->flags |= SRVR_ISGONE;
417 if (cq != &CellLRU) {
421 tc = (struct cell *) afs_osi_Alloc(sizeof(struct cell));
422 QAdd(&CellLRU, &tc->lruq); /* put in lruq */
423 tc->cellName = (char *) afs_osi_Alloc(strlen(acellName)+1);
424 strcpy(tc->cellName, acellName);
425 tc->cellIndex = afs_cellindex++;
426 if (aflags & CPrimary) {
427 extern int afs_rootCellIndex;
428 tc->cell = 1; /* primary cell is always 1 */
430 afs_rootCellIndex = tc->cellIndex;
432 tc->cell = afs_nextCellNum++;
435 tc->lcellp = (struct cell *)0;
436 tc->fsport = (fsport ? fsport : AFS_FSPORT);
437 tc->vlport = (vlport ? vlport : AFS_VLPORT);
438 afs_stats_cmperf.numCellsVisible++;
442 if (aflags & CLinkedCell) {
447 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
448 tcl = QTOC(cq); tq = QNext(cq);
449 if (!afs_strcasecmp(tcl->cellName, linkedcname)) {
458 if (tcl->lcellp) { /* XXX Overwriting if one existed before! XXX */
459 tcl->lcellp->lcellp = (struct cell *)0;
460 tcl->lcellp->states &= ~CLinkedCell;
465 tc->states |= aflags;
466 tc->timeout = timeout;
468 memset((char *)tc->cellHosts, 0, sizeof(tc->cellHosts));
469 if (aflags & CAlias) {
470 struct cell *tca = NULL;
476 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
477 tca = QTOC(cq); tq = QNext(cq);
478 if (!afs_strcasecmp(tca->cellName, aliasFor))
489 for (i=0; i<MAXCELLHOSTS; i++) {
491 afs_uint32 temp = acellHosts[i];
493 ts = afs_GetServer(&temp, 1, 0, tc->vlport, WRITE_LOCK, (afsUUID *)0, 0);
495 ts->flags &= ~SRVR_ISGONE;
496 tc->cellHosts[i] = ts;
497 afs_PutServer(ts, WRITE_LOCK);
499 afs_SortServers(tc->cellHosts, MAXCELLHOSTS); /* randomize servers */
501 ReleaseWriteLock(&afs_xcell);
506 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
507 afs_osi_Free((char *)tc, sizeof(struct cell));
509 ReleaseWriteLock(&afs_xcell);
514 afs_RemoveCellEntry(struct server *srvp)
522 /* Remove the server structure from the cell list - if there */
523 ObtainWriteLock(&afs_xcell,200);
524 for (j=k=0; j<MAXCELLHOSTS; j++) {
525 if (!tc->cellHosts[j]) break;
526 if (tc->cellHosts[j] != srvp) {
527 tc->cellHosts[k++] = tc->cellHosts[j];
531 /* What do we do if we remove the last one? */
533 for (; k<MAXCELLHOSTS; k++) {
534 tc->cellHosts[k] = 0;
536 ReleaseWriteLock(&afs_xcell);