merge from ronnie
[metze/ctdb/wip.git] / server / ctdb_serverids.c
1 /* 
2    ctdb_control protocol code to manage server ids
3
4    Copyright (C) Ronnie Sahlberg 2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "../include/ctdb_private.h"
21 #include "../common/rb_tree.h"
22
23
24 #define SERVER_ID_KEY_SIZE 3
25 static uint32_t *get_server_id_key(struct ctdb_server_id *server_id)
26 {
27         static uint32_t key[SERVER_ID_KEY_SIZE];
28
29         key[0] = server_id->type;
30         key[1] = server_id->pnn;
31         key[2] = server_id->server_id;
32
33         return &key[0];
34 }
35
36 /* add a server_id to the tree.
37    if we had already 'data' in the tree then this is a duplicate and we can
38    just talloc_free the structure in parm and leave data in the tree.
39    othervise if this is a new node we return parm and that is inserted
40    into the tree.
41 */
42 static void *add_server_id_callback(void *parm, void *data)
43 {
44         if (data) {
45                 talloc_free(parm);
46                 return data;
47         }
48         return parm;
49 }
50
51 /*
52   register a server id
53   a serverid that is registered with ctdb will be automatically unregistered
54   once the client domain socket dissappears.
55  */
56 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
57                                  uint32_t client_id,
58                                  TDB_DATA indata)
59 {
60         struct ctdb_server_id *server_id;
61         struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
62
63
64         if (client == NULL) {
65                 DEBUG(DEBUG_ERR,(__location__ " Could not find client parent structure. You can not send this control to a remote node\n"));
66                 return 1;
67         }
68
69         /* hang the server_id structure off client before storing it in the
70            tree so that is will be automatically destroyed when client
71            is destroyed. 
72            when the structure is free'd it will be automatically
73            removed from the tree
74         */
75         server_id = talloc_zero(client, struct ctdb_server_id);
76         CTDB_NO_MEMORY(ctdb, server_id);
77         memcpy(server_id, indata.dptr, sizeof(struct ctdb_server_id));
78
79         trbt_insertarray32_callback(ctdb->server_ids, SERVER_ID_KEY_SIZE,
80                 get_server_id_key(server_id), 
81                 add_server_id_callback, server_id);
82
83         return 0;
84 }
85
86
87 /*
88   check whether a server id exists
89  */
90 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
91                                  TDB_DATA indata)
92 {
93         struct ctdb_server_id *server_id = (struct ctdb_server_id *)indata.dptr;
94
95         return trbt_lookuparray32(ctdb->server_ids, 
96                                   SERVER_ID_KEY_SIZE,
97                                   get_server_id_key(server_id)) == NULL? 0 : 1;
98 }
99
100 /*
101   unregisters a server id
102  */
103 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
104                                  TDB_DATA indata)
105 {
106         struct ctdb_server_id *server_id = (struct ctdb_server_id *)indata.dptr;
107
108         talloc_free(trbt_lookuparray32(ctdb->server_ids, 
109                         SERVER_ID_KEY_SIZE,
110                         get_server_id_key(server_id)));
111         return 0;
112 }
113
114
115
116
117 struct count_server_ids {
118         int count;
119         struct ctdb_server_id_list *list;
120 };
121
122 static void server_id_count(void *param, void *data)
123 {
124         struct count_server_ids *svid = talloc_get_type(param, 
125                                                 struct count_server_ids);
126
127         if (svid == NULL) {
128                 DEBUG(DEBUG_ERR, (__location__ " Got null pointer for svid\n"));
129                 return;
130         }
131
132         svid->count++;
133 }
134
135 static void server_id_store(void *param, void *data)
136 {
137         struct count_server_ids *svid = talloc_get_type(param, 
138                                                 struct count_server_ids);
139         struct ctdb_server_id *server_id = talloc_get_type(data, 
140                                                 struct ctdb_server_id);
141
142         if (svid == NULL) {
143                 DEBUG(DEBUG_ERR, (__location__ " Got null pointer for svid\n"));
144                 return;
145         }
146
147         if (svid->count >= svid->list->num) {
148                 DEBUG(DEBUG_ERR, (__location__ " size of server id tree changed during traverse\n"));
149                 return;
150         }
151
152         memcpy(&svid->list->server_ids[svid->count], server_id, sizeof(struct ctdb_server_id));
153         svid->count++;
154 }
155
156 /* 
157    returns a list of all registered server ids for a node
158 */
159 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, TDB_DATA *outdata)
160 {
161         struct count_server_ids *svid;
162
163
164         svid = talloc_zero(outdata, struct count_server_ids);
165         CTDB_NO_MEMORY(ctdb, svid);
166
167
168         /* first we must count how many entries we have */
169         trbt_traversearray32(ctdb->server_ids, SERVER_ID_KEY_SIZE,
170                         server_id_count, svid);
171
172
173         outdata->dsize = offsetof(struct ctdb_server_id_list, 
174                                 server_ids)
175                         + sizeof(struct ctdb_server_id) * svid->count;
176         outdata->dptr  = talloc_size(outdata, outdata->dsize);
177         CTDB_NO_MEMORY(ctdb, outdata->dptr);
178
179
180         /* now fill the structure in */
181         svid->list = (struct ctdb_server_id_list *)(outdata->dptr);
182         svid->list->num = svid->count;
183         svid->count=0;
184         trbt_traversearray32(ctdb->server_ids, SERVER_ID_KEY_SIZE,
185                         server_id_store, svid);
186
187
188         return 0;
189 }