repl5_mtnode_ext.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "repl5.h"
  14. #include "cl5_api.h"
  15. /* global data */
  16. static DataList *root_list;
  17. /*
  18. * Mapping tree node extension management. Node stores replica object
  19. */
  20. void
  21. multisupplier_mtnode_extension_init()
  22. {
  23. /* Initialize list that store node roots. It is used during
  24. plugin startup to create replica objects */
  25. root_list = dl_new();
  26. dl_init(root_list, 0);
  27. }
  28. void
  29. multisupplier_mtnode_extension_destroy()
  30. {
  31. /* First iterate over the list to free the replica infos */
  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. multisupplier_mtnode_construct_replicas()
  39. {
  40. Slapi_DN *root;
  41. int cookie;
  42. Replica *r;
  43. mapping_tree_node *mtnode;
  44. multisupplier_mtnode_extension *ext;
  45. for (root = dl_get_first(root_list, &cookie); root;
  46. root = dl_get_next(root_list, &cookie)) {
  47. r = replica_new(root);
  48. if (r) {
  49. mtnode = slapi_get_mapping_tree_node_by_dn(root);
  50. if (mtnode == NULL) {
  51. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  52. "multisupplier_mtnode_construct_replicas: "
  53. "failed to locate mapping tree node for %s\n",
  54. slapi_sdn_get_dn(root));
  55. continue;
  56. }
  57. ext = (multisupplier_mtnode_extension *)repl_con_get_ext(REPL_CON_EXT_MTNODE, mtnode);
  58. if (ext == NULL) {
  59. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "multisupplier_mtnode_construct_replicas: "
  60. "failed to locate replication extension of mapping tree node for %s\n",
  61. slapi_sdn_get_dn(root));
  62. continue;
  63. }
  64. ext->replica = object_new(r, replica_destroy);
  65. if (replica_add_by_name(replica_get_name(r), r) != 0) {
  66. if (ext->replica) {
  67. object_release(ext->replica);
  68. ext->replica = NULL;
  69. }
  70. }
  71. /* Wait a few seconds for everything to startup before resuming any replication tasks */
  72. slapi_eq_once_rel(replica_check_for_tasks, (void *)replica_get_root(r),
  73. slapi_current_rel_time_t() + 5);
  74. }
  75. }
  76. }
  77. void *
  78. multisupplier_mtnode_extension_constructor(void *object, void *parent __attribute__((unused)))
  79. {
  80. mapping_tree_node *node;
  81. const Slapi_DN *root;
  82. multisupplier_mtnode_extension *ext;
  83. ext = (multisupplier_mtnode_extension *)slapi_ch_calloc(1, sizeof(multisupplier_mtnode_extension));
  84. node = (mapping_tree_node *)object;
  85. /* replica can be attached only to local public data */
  86. if (slapi_mapping_tree_node_is_set(node, SLAPI_MTN_LOCAL) &&
  87. !slapi_mapping_tree_node_is_set(node, SLAPI_MTN_PRIVATE)) {
  88. root = slapi_get_mapping_tree_node_root(node);
  89. /* ONREPL - we don't create replica object here because
  90. we can't fully initialize replica here since backends
  91. are not yet started. Instead, replica objects are created
  92. during replication plugin startup */
  93. if (root != NULL && !slapi_sdn_isempty(root)) {
  94. /* for now just store node root in the root list */
  95. dl_add(root_list, slapi_sdn_dup(root));
  96. }
  97. }
  98. return ext;
  99. }
  100. void
  101. multisupplier_mtnode_extension_destructor(void *ext, void *object __attribute__((unused)), void *parent __attribute__((unused)))
  102. {
  103. if (ext) {
  104. multisupplier_mtnode_extension *mtnode_ext = (multisupplier_mtnode_extension *)ext;
  105. if (mtnode_ext->replica) {
  106. object_release(mtnode_ext->replica);
  107. mtnode_ext->replica = NULL;
  108. }
  109. slapi_ch_free((void **)&ext);
  110. }
  111. }
  112. Replica *
  113. replica_get_replica_from_dn(const Slapi_DN *dn)
  114. {
  115. mapping_tree_node *mtnode;
  116. multisupplier_mtnode_extension *ext;
  117. Replica *r = NULL;
  118. if (dn == NULL)
  119. return NULL;
  120. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  121. if (mtnode == NULL) {
  122. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  123. "failed to locate mapping tree node for %s\n",
  124. slapi_sdn_get_dn(dn));
  125. return NULL;
  126. }
  127. ext = (multisupplier_mtnode_extension *)repl_con_get_ext(REPL_CON_EXT_MTNODE, mtnode);
  128. if (ext == NULL) {
  129. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  130. "failed to locate replication extension of mapping tree node for %s\n",
  131. slapi_sdn_get_dn(dn));
  132. return NULL;
  133. }
  134. if (ext->replica) {
  135. r = (Replica *)object_get_data(ext->replica);
  136. }
  137. return r;
  138. }
  139. Replica *
  140. replica_get_replica_from_root(const char *repl_root)
  141. {
  142. Replica *replica =NULL;
  143. Slapi_DN *repl_sdn = slapi_sdn_new_dn_byval(repl_root);
  144. replica = replica_get_replica_from_dn(repl_sdn);
  145. slapi_sdn_free(&repl_sdn);
  146. return replica;
  147. }
  148. Replica *
  149. replica_get_replica_for_op(Slapi_PBlock *pb)
  150. {
  151. Slapi_DN *sdn = NULL;
  152. Replica *replica = NULL;
  153. if (pb) {
  154. /* get replica generation for this operation */
  155. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  156. if (NULL == sdn) {
  157. goto bail;
  158. }
  159. replica = replica_get_replica_from_dn(sdn);
  160. }
  161. bail:
  162. return replica;
  163. }
  164. Replica *
  165. replica_get_for_backend(const char *be_name)
  166. {
  167. Slapi_Backend *be;
  168. const Slapi_DN *suffix;
  169. be = slapi_be_select_by_instance_name(be_name);
  170. if (NULL == be)
  171. return NULL;
  172. suffix = slapi_be_getsuffix(be, 0);
  173. return replica_get_replica_from_dn(suffix);
  174. }