repl5_mtnode_ext.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /* repl5_replica.c */
  39. #include "repl.h" /* ONREPL - this is bad */
  40. #include "repl5.h"
  41. #include "cl5_api.h"
  42. /* global data */
  43. static DataList *root_list;
  44. /*
  45. * Mapping tree node extension management. Node stores replica object
  46. */
  47. void
  48. multimaster_mtnode_extension_init ()
  49. {
  50. /* Initialize list that store node roots. It is used during
  51. plugin startup to create replica objects */
  52. root_list = dl_new ();
  53. dl_init (root_list, 0);
  54. }
  55. void
  56. multimaster_mtnode_extension_destroy ()
  57. {
  58. dl_cleanup (root_list, (FREEFN)slapi_sdn_free);
  59. dl_free (&root_list);
  60. }
  61. /* This function loops over the list of node roots, constructing replica objects
  62. where exist */
  63. void
  64. multimaster_mtnode_construct_replicas ()
  65. {
  66. Slapi_DN *root;
  67. int cookie;
  68. Replica *r;
  69. mapping_tree_node *mtnode;
  70. multimaster_mtnode_extension *ext;
  71. for (root = dl_get_first (root_list, &cookie); root;
  72. root = dl_get_next (root_list, &cookie))
  73. {
  74. r = replica_new(root);
  75. if (r)
  76. {
  77. mtnode = slapi_get_mapping_tree_node_by_dn(root);
  78. if (mtnode == NULL)
  79. {
  80. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  81. "multimaster_mtnode_construct_replicas: "
  82. "failed to locate mapping tree node for %s\n",
  83. slapi_sdn_get_dn (root));
  84. continue;
  85. }
  86. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  87. if (ext == NULL)
  88. {
  89. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "multimaster_mtnode_construct_replicas: "
  90. "failed to locate replication extension of mapping tree node for %s\n",
  91. slapi_sdn_get_dn (root));
  92. continue;
  93. }
  94. ext->replica = object_new(r, replica_destroy);
  95. if (replica_add_by_name (replica_get_name (r), ext->replica) != 0)
  96. {
  97. object_release (ext->replica);
  98. ext->replica = NULL;
  99. }
  100. }
  101. }
  102. }
  103. void *
  104. multimaster_mtnode_extension_constructor (void *object, void *parent)
  105. {
  106. mapping_tree_node *node;
  107. const Slapi_DN *root;
  108. multimaster_mtnode_extension *ext;
  109. ext = (multimaster_mtnode_extension *)slapi_ch_calloc (1, sizeof (multimaster_mtnode_extension));
  110. node = (mapping_tree_node *)object;
  111. /* replica can be attached only to local public data */
  112. if (slapi_mapping_tree_node_is_set (node, SLAPI_MTN_LOCAL) &&
  113. !slapi_mapping_tree_node_is_set (node, SLAPI_MTN_PRIVATE))
  114. {
  115. root = slapi_get_mapping_tree_node_root (node);
  116. /* ONREPL - we don't create replica object here because
  117. we can't fully initialize replica here since backends
  118. are not yet started. Instead, replica objects are created
  119. during replication plugin startup */
  120. if (root)
  121. {
  122. /* for now just store node root in the root list */
  123. dl_add (root_list, slapi_sdn_dup (root));
  124. }
  125. }
  126. return ext;
  127. }
  128. void
  129. multimaster_mtnode_extension_destructor (void* ext, void *object, void *parent)
  130. {
  131. if (ext)
  132. {
  133. multimaster_mtnode_extension *mtnode_ext = (multimaster_mtnode_extension *)ext;
  134. if (mtnode_ext->replica)
  135. {
  136. object_release (mtnode_ext->replica);
  137. mtnode_ext->replica = NULL;
  138. }
  139. slapi_ch_free((void **)&ext);
  140. }
  141. }
  142. Object *
  143. replica_get_replica_from_dn (const Slapi_DN *dn)
  144. {
  145. mapping_tree_node *mtnode;
  146. multimaster_mtnode_extension *ext;
  147. if (dn == NULL)
  148. return NULL;
  149. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  150. if (mtnode == NULL)
  151. {
  152. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  153. "failed to locate mapping tree node for %s\n",
  154. slapi_sdn_get_dn (dn));
  155. return NULL;
  156. }
  157. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  158. if (ext == NULL)
  159. {
  160. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  161. "failed to locate replication extension of mapping tree node for %s\n",
  162. slapi_sdn_get_dn (dn));
  163. return NULL;
  164. }
  165. if (ext->replica)
  166. object_acquire (ext->replica);
  167. return ext->replica;
  168. }
  169. Object *replica_get_replica_for_op (Slapi_PBlock *pb)
  170. {
  171. char *dn;
  172. Slapi_DN *sdn;
  173. Object *repl_obj = NULL;
  174. if (pb)
  175. {
  176. /* get replica generation for this operation */
  177. slapi_pblock_get (pb, SLAPI_TARGET_DN, &dn);
  178. sdn = slapi_sdn_new_dn_byref(dn);
  179. repl_obj = replica_get_replica_from_dn (sdn);
  180. slapi_sdn_free (&sdn);
  181. }
  182. return repl_obj;
  183. }
  184. Object *replica_get_for_backend (const char *be_name)
  185. {
  186. Slapi_Backend *be;
  187. const Slapi_DN *suffix;
  188. Object *r_obj;
  189. be = slapi_be_select_by_instance_name(be_name);
  190. if (NULL == be)
  191. return NULL;
  192. suffix = slapi_be_getsuffix(be, 0);
  193. r_obj = replica_get_replica_from_dn (suffix);
  194. return r_obj;
  195. }