repl5_mtnode_ext.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_replica.c */
  13. #include "repl.h" /* ONREPL - this is bad */
  14. #include "repl5.h"
  15. #include "cl5_api.h"
  16. /* global data */
  17. static DataList *root_list;
  18. /*
  19. * Mapping tree node extension management. Node stores replica object
  20. */
  21. void
  22. multimaster_mtnode_extension_init ()
  23. {
  24. /* Initialize list that store node roots. It is used during
  25. plugin startup to create replica objects */
  26. root_list = dl_new ();
  27. dl_init (root_list, 0);
  28. }
  29. void
  30. multimaster_mtnode_free_replica_object(const Slapi_DN *root) {
  31. mapping_tree_node *mtnode;
  32. multimaster_mtnode_extension *ext;
  33. /* In some cases, root can be an empty SDN */
  34. /* Othertimes, a bug is setting root to 0x8, and I can't see where... */
  35. if (root != NULL) {
  36. mtnode = slapi_get_mapping_tree_node_by_dn(root);
  37. if (mtnode != NULL) {
  38. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  39. if (ext != NULL && ext->replica != NULL) {
  40. object_release(ext->replica);
  41. }
  42. }
  43. }
  44. }
  45. void
  46. multimaster_mtnode_extension_destroy ()
  47. {
  48. /* First iterate over the list to free the replica infos */
  49. /* dl_cleanup (root_list, (FREEFN)multimaster_mtnode_free_replica_object); */
  50. dl_cleanup (root_list, (FREEFN)slapi_sdn_free);
  51. dl_free (&root_list);
  52. }
  53. /* This function loops over the list of node roots, constructing replica objects
  54. where exist */
  55. void
  56. multimaster_mtnode_construct_replicas ()
  57. {
  58. Slapi_DN *root;
  59. int cookie;
  60. Replica *r;
  61. mapping_tree_node *mtnode;
  62. multimaster_mtnode_extension *ext;
  63. for (root = dl_get_first (root_list, &cookie); root;
  64. root = dl_get_next (root_list, &cookie))
  65. {
  66. r = replica_new(root);
  67. if (r)
  68. {
  69. mtnode = slapi_get_mapping_tree_node_by_dn(root);
  70. if (mtnode == NULL)
  71. {
  72. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  73. "multimaster_mtnode_construct_replicas: "
  74. "failed to locate mapping tree node for %s\n",
  75. slapi_sdn_get_dn (root));
  76. continue;
  77. }
  78. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  79. if (ext == NULL)
  80. {
  81. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "multimaster_mtnode_construct_replicas: "
  82. "failed to locate replication extension of mapping tree node for %s\n",
  83. slapi_sdn_get_dn (root));
  84. continue;
  85. }
  86. ext->replica = object_new(r, replica_destroy);
  87. if (replica_add_by_name (replica_get_name (r), ext->replica) != 0)
  88. {
  89. if(ext->replica){
  90. object_release (ext->replica);
  91. ext->replica = NULL;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. void *
  98. multimaster_mtnode_extension_constructor (void *object, void *parent)
  99. {
  100. mapping_tree_node *node;
  101. const Slapi_DN *root;
  102. multimaster_mtnode_extension *ext;
  103. ext = (multimaster_mtnode_extension *)slapi_ch_calloc (1, sizeof (multimaster_mtnode_extension));
  104. node = (mapping_tree_node *)object;
  105. /* replica can be attached only to local public data */
  106. if (slapi_mapping_tree_node_is_set (node, SLAPI_MTN_LOCAL) &&
  107. !slapi_mapping_tree_node_is_set (node, SLAPI_MTN_PRIVATE))
  108. {
  109. root = slapi_get_mapping_tree_node_root (node);
  110. /* ONREPL - we don't create replica object here because
  111. we can't fully initialize replica here since backends
  112. are not yet started. Instead, replica objects are created
  113. during replication plugin startup */
  114. if (root != NULL && !slapi_sdn_isempty(root))
  115. {
  116. /* for now just store node root in the root list */
  117. dl_add (root_list, slapi_sdn_dup (root));
  118. }
  119. }
  120. return ext;
  121. }
  122. void
  123. multimaster_mtnode_extension_destructor (void* ext, void *object, void *parent)
  124. {
  125. if (ext)
  126. {
  127. multimaster_mtnode_extension *mtnode_ext = (multimaster_mtnode_extension *)ext;
  128. if (mtnode_ext->replica)
  129. {
  130. object_release (mtnode_ext->replica);
  131. mtnode_ext->replica = NULL;
  132. }
  133. slapi_ch_free((void **)&ext);
  134. }
  135. }
  136. Object *
  137. replica_get_replica_from_dn (const Slapi_DN *dn)
  138. {
  139. mapping_tree_node *mtnode;
  140. multimaster_mtnode_extension *ext;
  141. if (dn == NULL)
  142. return NULL;
  143. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  144. if (mtnode == NULL)
  145. {
  146. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  147. "failed to locate mapping tree node for %s\n",
  148. slapi_sdn_get_dn (dn));
  149. return NULL;
  150. }
  151. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  152. if (ext == NULL)
  153. {
  154. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  155. "failed to locate replication extension of mapping tree node for %s\n",
  156. slapi_sdn_get_dn (dn));
  157. return NULL;
  158. }
  159. if (ext->replica)
  160. object_acquire (ext->replica);
  161. return ext->replica;
  162. }
  163. Object *replica_get_replica_for_op (Slapi_PBlock *pb)
  164. {
  165. Slapi_DN *sdn = NULL;
  166. Object *repl_obj = NULL;
  167. if (pb)
  168. {
  169. /* get replica generation for this operation */
  170. slapi_pblock_get (pb, SLAPI_TARGET_SDN, &sdn);
  171. if (NULL == sdn) {
  172. goto bail;
  173. }
  174. repl_obj = replica_get_replica_from_dn (sdn);
  175. }
  176. bail:
  177. return repl_obj;
  178. }
  179. Object *replica_get_for_backend (const char *be_name)
  180. {
  181. Slapi_Backend *be;
  182. const Slapi_DN *suffix;
  183. Object *r_obj;
  184. be = slapi_be_select_by_instance_name(be_name);
  185. if (NULL == be)
  186. return NULL;
  187. suffix = slapi_be_getsuffix(be, 0);
  188. r_obj = replica_get_replica_from_dn (suffix);
  189. return r_obj;
  190. }