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 "../afs/param.h" /* Should be always first */
14 #include <afsconfig.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 static char afs_AfsdbHandlerWait;
56 static char afs_AfsdbLookupWait;
58 char afs_AfsdbHandlerPresent = 0;
59 char afs_AfsdbHandlerInuse = 0;
61 char *afs_AfsdbHandler_CellName;
62 afs_int32 *afs_AfsdbHandler_CellHosts;
63 int *afs_AfsdbHandler_Timeout;
65 char afs_AfsdbHandler_ReqPending;
66 char afs_AfsdbHandler_Completed;
69 struct cell *afs_GetCellByName_int();
71 int afs_strcasecmp(s1, s2)
72 register char *s1, *s2;
79 if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20;
80 if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20;
90 int afs_AfsdbHandler(acellName, acellNameLen, kernelMsg)
95 /* afs_syscall_call() has already grabbed the global lock */
97 afs_AfsdbHandlerPresent = 1;
99 if (afs_AfsdbHandler_ReqPending) {
102 hostCount = kernelMsg[0];
103 *afs_AfsdbHandler_Timeout = kernelMsg[1];
104 if (*afs_AfsdbHandler_Timeout) *afs_AfsdbHandler_Timeout += osi_Time();
106 for (i=0; i<MAXCELLHOSTS; i++) {
108 afs_AfsdbHandler_CellHosts[i] = 0;
110 afs_AfsdbHandler_CellHosts[i] = kernelMsg[2+i];
113 /* Request completed, wake up the relevant thread */
114 afs_AfsdbHandler_ReqPending = 0;
115 afs_AfsdbHandler_Completed = 1;
116 afs_osi_Wakeup(&afs_AfsdbLookupWait);
119 /* Wait for a request */
120 while (afs_AfsdbHandler_ReqPending == 0)
121 afs_osi_Sleep(&afs_AfsdbHandlerWait);
123 /* Copy the requested cell name into the request buffer */
124 strncpy(acellName, afs_AfsdbHandler_CellName, acellNameLen);
126 /* Return the lookup request to userspace */
132 int afs_GetCellHostsFromDns(acellName, acellHosts, timeout)
134 afs_int32 *acellHosts;
140 if (!afs_AfsdbHandlerPresent) return ENOENT;
142 if (!ISAFS_GLOCK()) {
147 /* Wait until the AFSDB handler is available, and grab it */
148 while (afs_AfsdbHandlerInuse)
149 afs_osi_Sleep(&afs_AfsdbLookupWait);
150 afs_AfsdbHandlerInuse = 1;
152 /* Set up parameters for the handler */
153 afs_AfsdbHandler_CellName = acellName;
154 afs_AfsdbHandler_CellHosts = acellHosts;
155 afs_AfsdbHandler_Timeout = timeout;
157 /* Wake up the AFSDB handler */
158 afs_AfsdbHandler_Completed = 0;
159 afs_AfsdbHandler_ReqPending = 1;
160 afs_osi_Wakeup(&afs_AfsdbHandlerWait);
162 /* Wait for the handler to get back to us with the reply */
163 while (!afs_AfsdbHandler_Completed)
164 afs_osi_Sleep(&afs_AfsdbLookupWait);
166 /* Release the AFSDB handler and wake up others waiting for it */
167 afs_AfsdbHandlerInuse = 0;
168 afs_osi_Wakeup(&afs_AfsdbLookupWait);
170 if (grab_glock) AFS_GUNLOCK();
172 if (*acellHosts) return 0;
180 void afs_RefreshCell(tc)
181 register struct cell *tc;
183 afs_int32 acellHosts[MAXCELLHOSTS];
186 /* Don't need to do anything if no timeout or it's not expired */
187 if (!tc->timeout || tc->timeout > osi_Time()) return;
189 if (!afs_GetCellHostsFromDns(tc->cellName, acellHosts, &timeout)) {
190 afs_NewCell(tc->cellName, acellHosts, tc->states,
191 tc->lcellp ? tc->lcellp->cellName : (char *) 0,
192 tc->fsport, tc->vlport, timeout);
195 /* In case of a DNS failure, keep old cell data.. */
200 struct cell *afs_GetCellByName_Dns(acellName, locktype)
201 register char *acellName;
204 afs_int32 acellHosts[MAXCELLHOSTS];
207 if (afs_GetCellHostsFromDns(acellName, acellHosts, &timeout))
208 return (struct cell *) 0;
209 if (afs_NewCell(acellName, acellHosts, CNoSUID, (char *) 0, 0, 0, timeout))
210 return (struct cell *) 0;
212 return afs_GetCellByName_int(acellName, locktype, 0);
216 struct cell *afs_GetCellByName_int(acellName, locktype, trydns)
217 register char *acellName;
221 register struct cell *tc;
222 register struct afs_q *cq, *tq;
224 AFS_STATCNT(afs_GetCellByName);
225 ObtainWriteLock(&afs_xcell,100);
226 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
227 tc = QTOC(cq); tq = QNext(cq);
228 if (!afs_strcasecmp(tc->cellName, acellName)) {
230 QAdd(&CellLRU, &tc->lruq);
231 ReleaseWriteLock(&afs_xcell);
236 ReleaseWriteLock(&afs_xcell);
237 return trydns ? afs_GetCellByName_Dns(acellName, locktype)
240 } /*afs_GetCellByName_int*/
243 struct cell *afs_GetCellByName(acellName, locktype)
244 register char *acellName;
247 return afs_GetCellByName_int(acellName, locktype, 1);
249 } /*afs_GetCellByName*/
252 struct cell *afs_GetCell(acell, locktype)
253 register afs_int32 acell;
256 register struct cell *tc;
257 register struct afs_q *cq, *tq;
259 AFS_STATCNT(afs_GetCell);
260 if (acell == 1 && afs_rootcell) return afs_rootcell;
261 ObtainWriteLock(&afs_xcell,101);
262 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
263 tc = QTOC(cq); tq = QNext(cq);
264 if (tc->cell == acell) {
266 QAdd(&CellLRU, &tc->lruq);
267 ReleaseWriteLock(&afs_xcell);
272 ReleaseWriteLock(&afs_xcell);
273 return (struct cell *) 0;
278 struct cell *afs_GetCellByIndex(cellindex, locktype)
279 register afs_int32 cellindex;
282 register struct cell *tc;
283 register struct afs_q *cq, *tq;
285 AFS_STATCNT(afs_GetCellByIndex);
286 ObtainWriteLock(&afs_xcell,102);
287 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
288 tc = QTOC(cq); tq = QNext(cq);
289 if (tc->cellIndex == cellindex) {
291 QAdd(&CellLRU, &tc->lruq);
292 ReleaseWriteLock(&afs_xcell);
297 ReleaseWriteLock(&afs_xcell);
298 return (struct cell *) 0;
300 } /*afs_GetCellByIndex*/
303 afs_int32 afs_NewCell(acellName, acellHosts, aflags, linkedcname, fsport, vlport, timeout)
306 register afs_int32 *acellHosts;
308 u_short fsport, vlport;
311 register struct cell *tc, *tcl=0;
312 register afs_int32 i, newc=0, code=0;
313 register struct afs_q *cq, *tq;
315 AFS_STATCNT(afs_NewCell);
316 if (*acellHosts == 0)
317 /* need >= one host to gen cell # */
320 ObtainWriteLock(&afs_xcell,103);
322 /* Find the cell and mark its servers as not down but gone */
323 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
324 tc = QTOC(cq); tq = QNext(cq);
325 if (afs_strcasecmp(tc->cellName, acellName) == 0) {
326 /* we don't want to keep pinging old vlservers which were down,
327 * since they don't matter any more. It's easier to do this than
328 * to remove the server from its various hash tables. */
329 for (i=0; i<MAXCELLHOSTS; i++) {
330 if (!tc->cellHosts[i]) break;
331 tc->cellHosts[i]->flags &= ~SRVR_ISDOWN;
332 tc->cellHosts[i]->flags |= SRVR_ISGONE;
338 if (cq != &CellLRU) {
342 tc = (struct cell *) afs_osi_Alloc(sizeof(struct cell));
343 QAdd(&CellLRU, &tc->lruq); /* put in lruq */
344 tc->cellName = (char *) afs_osi_Alloc(strlen(acellName)+1);
345 strcpy(tc->cellName, acellName);
346 tc->cellIndex = afs_cellindex++;
347 if (aflags & CPrimary) {
348 extern int afs_rootCellIndex;
349 tc->cell = 1; /* primary cell is always 1 */
351 afs_rootCellIndex = tc->cellIndex;
353 tc->cell = afs_nextCellNum++;
356 tc->lcellp = (struct cell *)0;
357 tc->fsport = (fsport ? fsport : AFS_FSPORT);
358 tc->vlport = (vlport ? vlport : AFS_VLPORT);
359 afs_stats_cmperf.numCellsVisible++;
363 if (aflags & CLinkedCell) {
368 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
369 tcl = QTOC(cq); tq = QNext(cq);
370 if (!afs_strcasecmp(tcl->cellName, linkedcname)) {
379 if (tcl->lcellp) { /* XXX Overwriting if one existed before! XXX */
380 tcl->lcellp->lcellp = (struct cell *)0;
381 tcl->lcellp->states &= ~CLinkedCell;
386 tc->states |= aflags;
387 tc->timeout = timeout;
389 bzero((char *)tc->cellHosts, sizeof(tc->cellHosts));
390 for (i=0; i<MAXCELLHOSTS; i++) {
392 afs_uint32 temp = acellHosts[i];
394 ts = afs_GetServer(&temp, 1, 0, tc->vlport, WRITE_LOCK, (afsUUID *)0, 0);
396 ts->flags &= ~SRVR_ISGONE;
397 tc->cellHosts[i] = ts;
398 afs_PutServer(ts, WRITE_LOCK);
400 afs_SortServers(tc->cellHosts, MAXCELLHOSTS); /* randomize servers */
401 ReleaseWriteLock(&afs_xcell);
406 afs_osi_Free(tc->cellName, strlen(tc->cellName)+1);
407 afs_osi_Free((char *)tc, sizeof(struct cell));
409 ReleaseWriteLock(&afs_xcell);
414 afs_RemoveCellEntry(struct server *srvp)
422 /* Remove the server structure from the cell list - if there */
423 ObtainWriteLock(&afs_xcell,200);
424 for (j=k=0; j<MAXCELLHOSTS; j++) {
425 if (!tc->cellHosts[j]) break;
426 if (tc->cellHosts[j] != srvp) {
427 tc->cellHosts[k++] = tc->cellHosts[j];
431 /* What do we do if we remove the last one? */
433 for (; k<MAXCELLHOSTS; k++) {
434 tc->cellHosts[k] = 0;
436 ReleaseWriteLock(&afs_xcell);