repl5_mtnode_ext.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. if(ext->replica){
  101. object_release (ext->replica);
  102. ext->replica = NULL;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. void *
  109. multimaster_mtnode_extension_constructor (void *object, void *parent)
  110. {
  111. mapping_tree_node *node;
  112. const Slapi_DN *root;
  113. multimaster_mtnode_extension *ext;
  114. ext = (multimaster_mtnode_extension *)slapi_ch_calloc (1, sizeof (multimaster_mtnode_extension));
  115. node = (mapping_tree_node *)object;
  116. /* replica can be attached only to local public data */
  117. if (slapi_mapping_tree_node_is_set (node, SLAPI_MTN_LOCAL) &&
  118. !slapi_mapping_tree_node_is_set (node, SLAPI_MTN_PRIVATE))
  119. {
  120. root = slapi_get_mapping_tree_node_root (node);
  121. /* ONREPL - we don't create replica object here because
  122. we can't fully initialize replica here since backends
  123. are not yet started. Instead, replica objects are created
  124. during replication plugin startup */
  125. if (root)
  126. {
  127. /* for now just store node root in the root list */
  128. dl_add (root_list, slapi_sdn_dup (root));
  129. }
  130. }
  131. return ext;
  132. }
  133. void
  134. multimaster_mtnode_extension_destructor (void* ext, void *object, void *parent)
  135. {
  136. if (ext)
  137. {
  138. multimaster_mtnode_extension *mtnode_ext = (multimaster_mtnode_extension *)ext;
  139. if (mtnode_ext->replica)
  140. {
  141. object_release (mtnode_ext->replica);
  142. mtnode_ext->replica = NULL;
  143. }
  144. slapi_ch_free((void **)&ext);
  145. }
  146. }
  147. Object *
  148. replica_get_replica_from_dn (const Slapi_DN *dn)
  149. {
  150. mapping_tree_node *mtnode;
  151. multimaster_mtnode_extension *ext;
  152. if (dn == NULL)
  153. return NULL;
  154. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  155. if (mtnode == NULL)
  156. {
  157. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  158. "failed to locate mapping tree node for %s\n",
  159. slapi_sdn_get_dn (dn));
  160. return NULL;
  161. }
  162. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  163. if (ext == NULL)
  164. {
  165. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  166. "failed to locate replication extension of mapping tree node for %s\n",
  167. slapi_sdn_get_dn (dn));
  168. return NULL;
  169. }
  170. if (ext->replica)
  171. object_acquire (ext->replica);
  172. return ext->replica;
  173. }
  174. Object *replica_get_replica_for_op (Slapi_PBlock *pb)
  175. {
  176. Slapi_DN *sdn = NULL;
  177. Object *repl_obj = NULL;
  178. if (pb)
  179. {
  180. /* get replica generation for this operation */
  181. slapi_pblock_get (pb, SLAPI_TARGET_SDN, &sdn);
  182. if (NULL == sdn) {
  183. goto bail;
  184. }
  185. repl_obj = replica_get_replica_from_dn (sdn);
  186. }
  187. bail:
  188. return repl_obj;
  189. }
  190. Object *replica_get_for_backend (const char *be_name)
  191. {
  192. Slapi_Backend *be;
  193. const Slapi_DN *suffix;
  194. Object *r_obj;
  195. be = slapi_be_select_by_instance_name(be_name);
  196. if (NULL == be)
  197. return NULL;
  198. suffix = slapi_be_getsuffix(be, 0);
  199. r_obj = replica_get_replica_from_dn (suffix);
  200. return r_obj;
  201. }