repl5_mtnode_ext.c 5.2 KB

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