summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/replication/repl5_mtnode_ext.c
blob: b6c01d6edc9da4d7efda90fbc79ae03a756b34b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/* repl5_replica.c */

#include "repl.h"   /* ONREPL - this is bad */
#include "repl5.h" 
#include "cl5_api.h"

/* global data */
static DataList *root_list;

/*
 * Mapping tree node extension management. Node stores replica object
 */

void
multimaster_mtnode_extension_init ()
{
	/* Initialize list that store node roots. It is used during
       plugin startup to create replica objects */
	root_list = dl_new ();
	dl_init (root_list, 0);
}

void
multimaster_mtnode_extension_destroy ()
{
	dl_cleanup (root_list, (FREEFN)slapi_sdn_free);
	dl_free (&root_list);
}

/* This function loops over the list of node roots, constructing replica objects 
   where exist */
void
multimaster_mtnode_construct_replicas ()
{
	Slapi_DN *root;
	int cookie;
	Replica *r;	
	mapping_tree_node *mtnode;
	multimaster_mtnode_extension *ext;

	for (root = dl_get_first (root_list, &cookie); root;
		 root = dl_get_next (root_list, &cookie))
	{
		r = replica_new(root);
        if (r)
        {

			mtnode = slapi_get_mapping_tree_node_by_dn(root);    
			if (mtnode == NULL)
			{
				slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, 
								"multimaster_mtnode_construct_replicas: "
								"failed to locate mapping tree node for %s\n",
								slapi_sdn_get_dn (root));	        
				continue;
			}
    
			ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
			if (ext == NULL)
			{
				slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "multimaster_mtnode_construct_replicas: "
								"failed to locate replication extension of mapping tree node for %s\n",
								slapi_sdn_get_dn (root));	        
				continue;
			}

            ext->replica = object_new(r, replica_destroy);
            if (replica_add_by_name (replica_get_name (r), ext->replica) != 0)
            {
                object_release (ext->replica);    
                ext->replica = NULL;
            }
        }
	}
}

void *
multimaster_mtnode_extension_constructor (void *object, void *parent)
{
    mapping_tree_node *node;
    const Slapi_DN *root;
    multimaster_mtnode_extension *ext;

    ext = (multimaster_mtnode_extension *)slapi_ch_calloc (1, sizeof (multimaster_mtnode_extension));

    node = (mapping_tree_node *)object;

    /* replica can be attached only to local public data */
    if (slapi_mapping_tree_node_is_set (node, SLAPI_MTN_LOCAL) &&
        !slapi_mapping_tree_node_is_set (node, SLAPI_MTN_PRIVATE))
    {        
        root = slapi_get_mapping_tree_node_root (node);
		/* ONREPL - we don't create replica object here because
		   we can't fully initialize replica here since backends
		   are not yet started. Instead, replica objects are created
		   during replication plugin startup */
        if (root)
        {
			/* for now just store node root in the root list */
            dl_add (root_list, slapi_sdn_dup (root));
        }
    }

    return ext;
}

void
multimaster_mtnode_extension_destructor (void* ext, void *object, void *parent)
{
    if (ext)
    {
        multimaster_mtnode_extension *mtnode_ext = (multimaster_mtnode_extension *)ext;
        if (mtnode_ext->replica)
        {
            object_release (mtnode_ext->replica);
            mtnode_ext->replica = NULL;
        }
    }
}

Object *
replica_get_replica_from_dn (const Slapi_DN *dn)
{
    mapping_tree_node *mtnode;
    multimaster_mtnode_extension *ext;

    if (dn == NULL)
        return NULL;

    mtnode = slapi_get_mapping_tree_node_by_dn(dn);    
    if (mtnode == NULL)
    {
        slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
                        "failed to locate mapping tree node for %s\n",
                        slapi_sdn_get_dn (dn));	        
        return NULL;
    }

    ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
    if (ext == NULL)
    {
        slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
                        "failed to locate replication extension of mapping tree node for %s\n",
                        slapi_sdn_get_dn (dn));	        
        return NULL;
    }

    if (ext->replica)
        object_acquire (ext->replica);

    return ext->replica;
}

Object *replica_get_replica_for_op (Slapi_PBlock *pb)
{
    char *dn;
    Slapi_DN *sdn;
    Object *repl_obj = NULL;

    if (pb)
    {
        /* get replica generation for this operation */
        slapi_pblock_get (pb, SLAPI_TARGET_DN, &dn);
        sdn = slapi_sdn_new_dn_byref(dn);
        repl_obj = replica_get_replica_from_dn (sdn);

        slapi_sdn_free (&sdn);
    }

    return repl_obj;
}

Object *replica_get_for_backend (const char *be_name)
{
    Slapi_Backend *be;
    const Slapi_DN *suffix;
    Object *r_obj;

    be = slapi_be_select_by_instance_name(be_name);
    if (NULL == be)
		return NULL;

    suffix = slapi_be_getsuffix(be, 0);    

    r_obj = replica_get_replica_from_dn (suffix);
    
    return r_obj;
}