repl5_mtnode_ext.c 5.0 KB

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