1
0

repl5_mtnode_ext.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_replica.c */
  42. #include "repl.h" /* ONREPL - this is bad */
  43. #include "repl5.h"
  44. #include "cl5_api.h"
  45. /* global data */
  46. static DataList *root_list;
  47. /*
  48. * Mapping tree node extension management. Node stores replica object
  49. */
  50. void
  51. multimaster_mtnode_extension_init ()
  52. {
  53. /* Initialize list that store node roots. It is used during
  54. plugin startup to create replica objects */
  55. root_list = dl_new ();
  56. dl_init (root_list, 0);
  57. }
  58. void
  59. multimaster_mtnode_extension_destroy ()
  60. {
  61. dl_cleanup (root_list, (FREEFN)slapi_sdn_free);
  62. dl_free (&root_list);
  63. }
  64. /* This function loops over the list of node roots, constructing replica objects
  65. where exist */
  66. void
  67. multimaster_mtnode_construct_replicas ()
  68. {
  69. Slapi_DN *root;
  70. int cookie;
  71. Replica *r;
  72. mapping_tree_node *mtnode;
  73. multimaster_mtnode_extension *ext;
  74. for (root = dl_get_first (root_list, &cookie); root;
  75. root = dl_get_next (root_list, &cookie))
  76. {
  77. r = replica_new(root);
  78. if (r)
  79. {
  80. mtnode = slapi_get_mapping_tree_node_by_dn(root);
  81. if (mtnode == NULL)
  82. {
  83. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  84. "multimaster_mtnode_construct_replicas: "
  85. "failed to locate mapping tree node for %s\n",
  86. slapi_sdn_get_dn (root));
  87. continue;
  88. }
  89. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  90. if (ext == NULL)
  91. {
  92. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "multimaster_mtnode_construct_replicas: "
  93. "failed to locate replication extension of mapping tree node for %s\n",
  94. slapi_sdn_get_dn (root));
  95. continue;
  96. }
  97. ext->replica = object_new(r, replica_destroy);
  98. if (replica_add_by_name (replica_get_name (r), ext->replica) != 0)
  99. {
  100. object_release (ext->replica);
  101. ext->replica = NULL;
  102. }
  103. }
  104. }
  105. }
  106. void *
  107. multimaster_mtnode_extension_constructor (void *object, void *parent)
  108. {
  109. mapping_tree_node *node;
  110. const Slapi_DN *root;
  111. multimaster_mtnode_extension *ext;
  112. ext = (multimaster_mtnode_extension *)slapi_ch_calloc (1, sizeof (multimaster_mtnode_extension));
  113. node = (mapping_tree_node *)object;
  114. /* replica can be attached only to local public data */
  115. if (slapi_mapping_tree_node_is_set (node, SLAPI_MTN_LOCAL) &&
  116. !slapi_mapping_tree_node_is_set (node, SLAPI_MTN_PRIVATE))
  117. {
  118. root = slapi_get_mapping_tree_node_root (node);
  119. /* ONREPL - we don't create replica object here because
  120. we can't fully initialize replica here since backends
  121. are not yet started. Instead, replica objects are created
  122. during replication plugin startup */
  123. if (root)
  124. {
  125. /* for now just store node root in the root list */
  126. dl_add (root_list, slapi_sdn_dup (root));
  127. }
  128. }
  129. return ext;
  130. }
  131. void
  132. multimaster_mtnode_extension_destructor (void* ext, void *object, void *parent)
  133. {
  134. if (ext)
  135. {
  136. multimaster_mtnode_extension *mtnode_ext = (multimaster_mtnode_extension *)ext;
  137. if (mtnode_ext->replica)
  138. {
  139. object_release (mtnode_ext->replica);
  140. mtnode_ext->replica = NULL;
  141. }
  142. slapi_ch_free((void **)&ext);
  143. }
  144. }
  145. Object *
  146. replica_get_replica_from_dn (const Slapi_DN *dn)
  147. {
  148. mapping_tree_node *mtnode;
  149. multimaster_mtnode_extension *ext;
  150. if (dn == NULL)
  151. return NULL;
  152. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  153. if (mtnode == NULL)
  154. {
  155. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  156. "failed to locate mapping tree node for %s\n",
  157. slapi_sdn_get_dn (dn));
  158. return NULL;
  159. }
  160. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  161. if (ext == NULL)
  162. {
  163. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  164. "failed to locate replication extension of mapping tree node for %s\n",
  165. slapi_sdn_get_dn (dn));
  166. return NULL;
  167. }
  168. if (ext->replica)
  169. object_acquire (ext->replica);
  170. return ext->replica;
  171. }
  172. Object *replica_get_replica_for_op (Slapi_PBlock *pb)
  173. {
  174. char *dn;
  175. Slapi_DN *sdn;
  176. Object *repl_obj = NULL;
  177. if (pb)
  178. {
  179. /* get replica generation for this operation */
  180. slapi_pblock_get (pb, SLAPI_TARGET_DN, &dn);
  181. sdn = slapi_sdn_new_dn_byref(dn);
  182. repl_obj = replica_get_replica_from_dn (sdn);
  183. slapi_sdn_free (&sdn);
  184. }
  185. return repl_obj;
  186. }
  187. Object *replica_get_for_backend (const char *be_name)
  188. {
  189. Slapi_Backend *be;
  190. const Slapi_DN *suffix;
  191. Object *r_obj;
  192. be = slapi_be_select_by_instance_name(be_name);
  193. if (NULL == be)
  194. return NULL;
  195. suffix = slapi_be_getsuffix(be, 0);
  196. r_obj = replica_get_replica_from_dn (suffix);
  197. return r_obj;
  198. }