ctdb-daemon: Factor out new function ctdb_node_list_to_map()
authorMartin Schwenke <martin@meltin.net>
Fri, 20 Feb 2015 01:31:37 +0000 (12:31 +1100)
committerAmitay Isaacs <amitay@samba.org>
Tue, 7 Apr 2015 05:43:13 +0000 (07:43 +0200)
Change ctdb_control_getnodemap() to use this.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/server/ctdb_recover.c

index b8a9f3353e6846353517d656aacd9d51bd79f43b..90211be8267fd422b2dc610e341d824768127170 100644 (file)
@@ -118,31 +118,47 @@ ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indat
        return 0;
 }
 
-int 
-ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
+static struct ctdb_node_map *
+ctdb_node_list_to_map(struct ctdb_node **nodes, uint32_t num_nodes,
+                     TALLOC_CTX *mem_ctx)
 {
-       uint32_t i, num_nodes;
+       uint32_t i;
+       size_t size;
        struct ctdb_node_map *node_map;
 
-       CHECK_CONTROL_DATA_SIZE(0);
-
-       num_nodes = ctdb->num_nodes;
-
-       outdata->dsize = offsetof(struct ctdb_node_map, nodes) + num_nodes*sizeof(struct ctdb_node_and_flags);
-       outdata->dptr  = (unsigned char *)talloc_zero_size(outdata, outdata->dsize);
-       if (!outdata->dptr) {
-               DEBUG(DEBUG_ALERT, (__location__ " Failed to allocate nodemap array\n"));
-               exit(1);
+       size = offsetof(struct ctdb_node_map, nodes) + num_nodes*sizeof(struct ctdb_node_and_flags);
+       node_map  = (struct ctdb_node_map *)talloc_zero_size(mem_ctx, size);
+       if (node_map == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Failed to allocate nodemap array\n"));
+               return NULL;
        }
 
-       node_map = (struct ctdb_node_map *)outdata->dptr;
        node_map->num = num_nodes;
        for (i=0; i<num_nodes; i++) {
-               node_map->nodes[i].addr = ctdb->nodes[i]->address;
-               node_map->nodes[i].pnn   = ctdb->nodes[i]->pnn;
-               node_map->nodes[i].flags = ctdb->nodes[i]->flags;
+               node_map->nodes[i].addr  = nodes[i]->address;
+               node_map->nodes[i].pnn   = nodes[i]->pnn;
+               node_map->nodes[i].flags = nodes[i]->flags;
        }
 
+       return node_map;
+}
+
+
+
+int
+ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
+{
+       CHECK_CONTROL_DATA_SIZE(0);
+
+       outdata->dptr  = (unsigned char *)ctdb_node_list_to_map(ctdb->nodes,
+                                                               ctdb->num_nodes,
+                                                               outdata);
+       if (outdata->dptr == NULL) {
+               return -1;
+       }
+
+       outdata->dsize = talloc_get_size(outdata->dptr);
+
        return 0;
 }