cell-info-do-not-delete-20040604
[openafs.git] / src / WINNT / afsd / cm_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 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #include <nb30.h>
16 #include <winsock2.h>
17 #endif /* !DJGPP */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <malloc.h>
21 #include <osi.h>
22 #include <string.h>
23
24 #include "afsd.h"
25
26 osi_rwlock_t cm_cellLock;
27
28 cm_cell_t *cm_allCellsp;
29
30 /* function called as callback proc from cm_SearchCellFile.  Return 0 to
31  * continue processing.
32  */
33 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep)
34 {
35         cm_server_t *tsp;
36         cm_serverRef_t *tsrp;
37     cm_cell_t *cellp;
38         
39         cellp = rockp;
40
41         /* if this server was previously created by fs setserverprefs */
42         if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
43         {
44                 if ( !tsp->cellp )
45                         tsp->cellp = cellp;
46         }
47         else
48                 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
49
50         /* Insert the vlserver into a sorted list, sorted by server rank */
51         tsrp = cm_NewServerRef(tsp);
52         cm_InsertServerList(&cellp->vlServersp, tsrp);
53
54         return 0;
55 }
56
57 /* load up a cell structure from the cell database, afsdcell.ini */
58 cm_cell_t *cm_GetCell(char *namep, long flags)
59 {
60   return cm_GetCell_Gen(namep, NULL, flags);
61 }
62
63 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, long flags)
64 {
65         cm_cell_t *cp;
66     long code;
67     static cellCounter = 1;             /* locked by cm_cellLock */
68         int ttl;
69         char fullname[200]="";
70
71         lock_ObtainWrite(&cm_cellLock);
72         for (cp = cm_allCellsp; cp; cp=cp->nextp) {
73                 if (strcmp(namep, cp->namep) == 0) {
74             strcpy(fullname, cp->namep);
75             break;
76                 }
77     }
78
79         if ((!cp && (flags & CM_FLAG_CREATE))
80 #ifdef AFS_AFSDB_ENV
81          /* if it's from DNS, see if it has expired */
82          || (cp && (cp->flags & CM_CELLFLAG_DNS) 
83          && ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID) || (time(0) > cp->timeout)))
84 #endif
85           ) {
86         int dns_expired = 0;
87                 if (!cp) {
88             cp = malloc(sizeof(*cp));
89             memset(cp, 0, sizeof(*cp));
90         } 
91         else {
92             dns_expired = 1;
93             /* must empty cp->vlServersp */
94             cm_FreeServerList(&cp->vlServersp);
95             cp->vlServersp = NULL;
96         }
97
98         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
99                 if (code) {
100             afsi_log("in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
101                       namep, code, fullname);
102
103 #ifdef AFS_AFSDB_ENV
104             if (cm_dnsEnabled /*&& cm_DomainValid(namep)*/) {
105                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
106                 if ( code ) {
107                     afsi_log("in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
108                              namep, code, fullname);
109                     if (dns_expired) {
110                         cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
111                         cp = NULL;  /* set cp to NULL to indicate error */
112                     } 
113                 }
114                 else {   /* got cell from DNS */
115                     cp->flags |= CM_CELLFLAG_DNS;
116                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
117                     cp->timeout = time(0) + ttl;
118                 }
119             }
120 #endif
121             if (cp && code) {     /* free newly allocated memory */
122                 free(cp);
123                 cp = NULL;
124                 goto done;
125             }
126                 }
127
128                 /* randomise among those vlservers having the same rank*/ 
129                 cm_RandomizeServer(&cp->vlServersp);
130
131 #ifdef AFS_AFSDB_ENV
132         if (dns_expired) {
133             /* we want to preserve the full name and mutex.
134              * also, cp is already in the cm_allCellsp list
135              */
136             goto done;
137         }
138 #endif /* AFS_AFSDB_ENV */
139
140         /* otherwise we found the cell, and so we're nearly done */
141         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
142
143                 /* copy in name */
144         cp->namep = malloc(strlen(fullname)+1);
145         strcpy(cp->namep, fullname);
146
147                 /* thread on global list */
148         cp->nextp = cm_allCellsp;
149         cm_allCellsp = cp;
150                 
151         cp->cellID = cellCounter++;
152     }
153
154   done:
155     /* fullname is not valid if cp == NULL */
156         if (cp && newnamep)
157         strcpy(newnamep, fullname);
158         lock_ReleaseWrite(&cm_cellLock);
159     return cp;
160 }
161
162 cm_cell_t *cm_FindCellByID(long cellID)
163 {
164         cm_cell_t *cp;
165         int ttl;
166     int code;
167
168         lock_ObtainWrite(&cm_cellLock);
169         for(cp = cm_allCellsp; cp; cp=cp->nextp) {
170                 if (cellID == cp->cellID) 
171             break;
172     }
173
174 #ifdef AFS_AFSDB_ENV
175         /* if it's from DNS, see if it has expired */
176         if (cp && cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) && 
177         ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID) || (time(0) > cp->timeout))) {
178         /* must empty cp->vlServersp */
179         cm_FreeServerList(&cp->vlServersp);
180         cp->vlServersp = NULL;
181
182         code = cm_SearchCellByDNS(cp->namep, NULL, &ttl, cm_AddCellProc, cp);
183         if (code == 0) {   /* got cell from DNS */
184             cp->flags |= CM_CELLFLAG_DNS;
185             cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
186 #ifdef DEBUG
187             fprintf(stderr, "cell %s: ttl=%d\n", cp->namep, ttl);
188 #endif
189             cp->timeout = time(0) + ttl;
190         } else {
191             cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
192             cp = NULL;      /* return NULL to indicate failure */
193         }
194         /* if we fail to find it this time, we'll just do nothing and leave the
195          * current entry alone 
196          */
197         }
198 #endif /* AFS_AFSDB_ENV */
199
200         lock_ReleaseWrite(&cm_cellLock);        
201     return cp;
202 }
203
204 void cm_InitCell(void)
205 {
206         static osi_once_t once;
207         
208     if (osi_Once(&once)) {
209                 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
210         cm_allCellsp = NULL;
211                 osi_EndOnce(&once);
212     }
213 }
214 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
215 {
216         cm_cell_t *cp;
217         int code;
218
219         cp = tsp->cellp;        /* cell that this vlserver belongs to */
220         osi_assert(cp);
221
222         lock_ObtainMutex(&cp->mx);
223         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
224
225         if ( !code )            /* if the server list was rearranged */
226             cm_RandomizeServer(&cp->vlServersp);
227
228         lock_ReleaseMutex(&cp->mx);
229 }
230