repl5_mtnode_ext.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * 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. slapi_ch_free((void **)&ext);
  108. }
  109. }
  110. Object *
  111. replica_get_replica_from_dn (const Slapi_DN *dn)
  112. {
  113. mapping_tree_node *mtnode;
  114. multimaster_mtnode_extension *ext;
  115. if (dn == NULL)
  116. return NULL;
  117. mtnode = slapi_get_mapping_tree_node_by_dn(dn);
  118. if (mtnode == NULL)
  119. {
  120. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  121. "failed to locate mapping tree node for %s\n",
  122. slapi_sdn_get_dn (dn));
  123. return NULL;
  124. }
  125. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  126. if (ext == NULL)
  127. {
  128. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_get_replica_from_dn: "
  129. "failed to locate replication extension of mapping tree node for %s\n",
  130. slapi_sdn_get_dn (dn));
  131. return NULL;
  132. }
  133. if (ext->replica)
  134. object_acquire (ext->replica);
  135. return ext->replica;
  136. }
  137. Object *replica_get_replica_for_op (Slapi_PBlock *pb)
  138. {
  139. char *dn;
  140. Slapi_DN *sdn;
  141. Object *repl_obj = NULL;
  142. if (pb)
  143. {
  144. /* get replica generation for this operation */
  145. slapi_pblock_get (pb, SLAPI_TARGET_DN, &dn);
  146. sdn = slapi_sdn_new_dn_byref(dn);
  147. repl_obj = replica_get_replica_from_dn (sdn);
  148. slapi_sdn_free (&sdn);
  149. }
  150. return repl_obj;
  151. }
  152. Object *replica_get_for_backend (const char *be_name)
  153. {
  154. Slapi_Backend *be;
  155. const Slapi_DN *suffix;
  156. Object *r_obj;
  157. be = slapi_be_select_by_instance_name(be_name);
  158. if (NULL == be)
  159. return NULL;
  160. suffix = slapi_be_getsuffix(be, 0);
  161. r_obj = replica_get_replica_from_dn (suffix);
  162. return r_obj;
  163. }