patch-from-shadow-to-jaltman-bkbox-20031120
[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) && (time(0) > cp->timeout))
83 #endif
84           ) {
85                 if (!cp) cp = malloc(sizeof(*cp));
86         memset(cp, 0, sizeof(*cp));
87         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
88                 if (code) {
89 #ifdef AFS_AFSDB_ENV
90             if (cm_dnsEnabled /*&& cm_DomainValid(namep)*/)
91                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
92 #endif
93             if (code) {
94                 free(cp);
95                 cp = NULL;
96                 goto done;
97             }
98 #ifdef AFS_AFSDB_ENV
99             else {   /* got cell from DNS */
100                 cp->flags |= CM_CELLFLAG_DNS;
101                 cp->timeout = time(0) + ttl;
102             }
103 #endif
104                 }
105
106                 /* randomise among those vlservers having the same rank*/ 
107                 cm_RandomizeServer(&cp->vlServersp);
108
109         /* otherwise we found the cell, and so we're nearly done */
110         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
111
112                 /* copy in name */
113         cp->namep = malloc(strlen(fullname)+1);
114         strcpy(cp->namep, fullname);
115
116                 /* thread on global list */
117         cp->nextp = cm_allCellsp;
118         cm_allCellsp = cp;
119                 
120         cp->cellID = cellCounter++;
121     }
122
123   done:
124     /* fullname is not valid if cp == NULL */
125         if (cp && newnamep)
126           strcpy(newnamep, fullname);
127         lock_ReleaseWrite(&cm_cellLock);
128         return cp;
129 }
130
131 cm_cell_t *cm_FindCellByID(long cellID)
132 {
133         cm_cell_t *cp;
134         int ttl;
135      int code;
136
137         lock_ObtainWrite(&cm_cellLock);
138         for(cp = cm_allCellsp; cp; cp=cp->nextp) {
139                 if (cellID == cp->cellID) break;
140         }
141
142 #ifdef AFS_AFSDB_ENV
143         /* if it's from DNS, see if it has expired */
144         if (cp && cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) && (time(0) > cp->timeout)) {
145           code = cm_SearchCellByDNS(cp->namep, NULL, &ttl, cm_AddCellProc, cp);
146           if (code == 0) {   /* got cell from DNS */
147             cp->flags |= CM_CELLFLAG_DNS;
148 #ifdef DEBUG
149             fprintf(stderr, "cell %s: ttl=%d\n", cp->namep, ttl);
150 #endif
151             cp->timeout = time(0) + ttl;
152           }
153           /* if we fail to find it this time, we'll just do nothing and leave the
154              current entry alone */
155         }
156 #endif /* AFS_AFSDB_ENV */
157
158         lock_ReleaseWrite(&cm_cellLock);        
159         
160         return cp;
161 }
162
163 void cm_InitCell(void)
164 {
165         static osi_once_t once;
166         
167         if (osi_Once(&once)) {
168                 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
169                 cm_allCellsp = NULL;
170                 osi_EndOnce(&once);
171         }
172 }
173 void cm_ChangeRankCellVLServer(cm_server_t       *tsp)
174 {
175         cm_cell_t *cp;
176         int code;
177
178         cp = tsp->cellp;        /* cell that this vlserver belongs to */
179         osi_assert(cp);
180
181         lock_ObtainMutex(&cp->mx);
182         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
183
184         if ( !code )            /* if the server list was rearranged */
185             cm_RandomizeServer(&cp->vlServersp);
186
187         lock_ReleaseMutex(&cp->mx);
188 }
189