repl5_replica_config.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_replica_config.c - replica configuration over ldap */
  42. #include <ctype.h> /* for isdigit() */
  43. #include "repl.h" /* ONREPL - this is bad */
  44. #include "repl5.h"
  45. #include "cl5_api.h"
  46. #include "cl5.h"
  47. /* CONFIG_BASE: no need to optimize */
  48. #define CONFIG_BASE "cn=mapping tree,cn=config"
  49. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  50. #define TASK_ATTR "nsds5Task"
  51. #define CL2LDIF_TASK "CL2LDIF"
  52. #define LDIF2CL_TASK "LDIF2CL"
  53. #define CLEANRUV "CLEANRUV"
  54. #define CLEANRUVLEN 8
  55. #define CLEANALLRUV "CLEANALLRUV"
  56. #define CLEANALLRUVLEN 11
  57. #define REPLICA_RDN "cn=replica"
  58. #define CLEANALLRUV_ID "CleanAllRUV Task"
  59. #define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
  60. int slapi_log_urp = SLAPI_LOG_REPL;
  61. static ReplicaId cleaned_rids[CLEANRIDSIZ + 1] = {0};
  62. static ReplicaId aborted_rids[CLEANRIDSIZ + 1] = {0};
  63. static Slapi_RWLock *rid_lock = NULL;
  64. static Slapi_RWLock *abort_rid_lock = NULL;
  65. static PRLock *notify_lock = NULL;
  66. static PRCondVar *notify_cvar = NULL;
  67. /* Forward Declartions */
  68. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  69. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  70. static int replica_config_post_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  71. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  72. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  73. static int replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  74. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  75. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  76. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  77. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  78. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  79. static int replica_execute_ldif2cl_task (Object *r, char *returntext);
  80. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  81. static int replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, char *returntext);
  82. static void replica_cleanallruv_thread(void *arg);
  83. static void replica_send_cleanruv_task(Repl_Agmt *agmt, ReplicaId rid, Slapi_Task *task);
  84. static int check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task);
  85. static int check_agmts_are_caught_up(Replica *replica, ReplicaId rid, char *maxcsn, Slapi_Task *task);
  86. static int replica_cleanallruv_send_extop(Repl_Agmt *ra, ReplicaId rid, Slapi_Task *task, struct berval *payload, int check_result);
  87. static int replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload);
  88. static int replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *rid_text, char *maxcsn, Slapi_Task *task);
  89. static int replica_cleanallruv_replica_alive(Repl_Agmt *agmt);
  90. static int replica_cleanallruv_check_ruv(Repl_Agmt *ra, char *rid_text, Slapi_Task *task);
  91. static int get_cleanruv_task_count();
  92. static int get_abort_cleanruv_task_count();
  93. static int replica_cleanup_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  94. static int replica_task_done(Replica *replica);
  95. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  96. /*
  97. * Note: internal add/modify/delete operations should not be run while
  98. * s_configLock is held. E.g., slapi_modify_internal_pb via replica_task_done
  99. * in replica_config_post_modify.
  100. */
  101. static PRLock *s_configLock;
  102. static int
  103. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  104. {
  105. *returncode = LDAP_UNWILLING_TO_PERFORM;
  106. return SLAPI_DSE_CALLBACK_ERROR;
  107. }
  108. int
  109. replica_config_init()
  110. {
  111. s_configLock = PR_NewLock ();
  112. if (s_configLock == NULL)
  113. {
  114. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  115. "failed to create configuration lock; NSPR error - %d\n",
  116. PR_GetError ());
  117. return -1;
  118. }
  119. rid_lock = slapi_new_rwlock();
  120. if(rid_lock == NULL)
  121. {
  122. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  123. "failed to create rid_lock; NSPR error - %d\n", PR_GetError ());
  124. return -1;
  125. }
  126. abort_rid_lock = slapi_new_rwlock();
  127. if(abort_rid_lock == NULL)
  128. {
  129. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  130. "failed to create abort_rid_lock; NSPR error - %d\n", PR_GetError ());
  131. return -1;
  132. }
  133. if ( ( notify_lock = PR_NewLock()) == NULL ) {
  134. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  135. "failed to create notify lock; NSPR error - %d\n", PR_GetError ());
  136. return -1;
  137. }
  138. if ( ( notify_cvar = PR_NewCondVar( notify_lock )) == NULL ) {
  139. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  140. "failed to create notify cond var; NSPR error - %d\n", PR_GetError ());
  141. return -1;
  142. }
  143. /* config DSE must be initialized before we get here */
  144. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  145. CONFIG_FILTER, replica_config_add, NULL);
  146. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  147. CONFIG_FILTER, replica_config_modify,NULL);
  148. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  149. CONFIG_FILTER, dont_allow_that, NULL);
  150. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  151. CONFIG_FILTER, replica_config_delete,NULL);
  152. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  153. CONFIG_FILTER, replica_config_search,NULL);
  154. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  155. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  156. CONFIG_FILTER, replica_config_post_modify,
  157. NULL);
  158. /* register the CLEANALLRUV & ABORT task */
  159. slapi_task_register_handler("cleanallruv", replica_cleanall_ruv_task);
  160. slapi_task_register_handler("abort cleanallruv", replica_cleanall_ruv_abort);
  161. return 0;
  162. }
  163. void
  164. replica_config_destroy ()
  165. {
  166. if (s_configLock)
  167. {
  168. PR_DestroyLock (s_configLock);
  169. s_configLock = NULL;
  170. }
  171. /* config DSE must be initialized before we get here */
  172. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  173. CONFIG_FILTER, replica_config_add);
  174. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  175. CONFIG_FILTER, replica_config_modify);
  176. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  177. CONFIG_FILTER, dont_allow_that);
  178. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  179. CONFIG_FILTER, replica_config_delete);
  180. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  181. CONFIG_FILTER, replica_config_search);
  182. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  183. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  184. CONFIG_FILTER, replica_config_post_modify);
  185. }
  186. static int
  187. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  188. int *returncode, char *errorbuf, void *arg)
  189. {
  190. Replica *r = NULL;
  191. multimaster_mtnode_extension *mtnode_ext;
  192. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  193. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  194. char *errortext = errorbuf ? errorbuf : buf;
  195. if (errorbuf)
  196. {
  197. errorbuf[0] = '\0';
  198. }
  199. *returncode = LDAP_SUCCESS;
  200. PR_Lock (s_configLock);
  201. /* add the dn to the dn hash so we can tell this replica is being configured */
  202. replica_add_by_dn(replica_root);
  203. mtnode_ext = _replica_config_get_mtnode_ext (e);
  204. PR_ASSERT (mtnode_ext);
  205. if (mtnode_ext->replica)
  206. {
  207. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  208. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  209. *returncode = LDAP_UNWILLING_TO_PERFORM;
  210. goto done;
  211. }
  212. /* create replica object */
  213. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  214. if (r == NULL)
  215. {
  216. *returncode = LDAP_OPERATIONS_ERROR;
  217. goto done;
  218. }
  219. /* Set the mapping tree node state, and the referrals from the RUV */
  220. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  221. if (!replica_is_legacy_consumer (r))
  222. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  223. /* ONREPL if replica is added as writable we need to execute protocol that
  224. introduces new writable replica to the topology */
  225. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  226. /* add replica object to the hash */
  227. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  228. /* delete the dn from the dn hash - done with configuration */
  229. replica_delete_by_dn(replica_root);
  230. done:
  231. PR_Unlock (s_configLock);
  232. /* slapi_ch_free accepts NULL pointer */
  233. slapi_ch_free ((void**)&replica_root);
  234. if (*returncode != LDAP_SUCCESS)
  235. {
  236. if (mtnode_ext->replica)
  237. object_release (mtnode_ext->replica);
  238. return SLAPI_DSE_CALLBACK_ERROR;
  239. }
  240. else
  241. return SLAPI_DSE_CALLBACK_OK;
  242. }
  243. static int
  244. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  245. int *returncode, char *returntext, void *arg)
  246. {
  247. int rc= 0;
  248. LDAPMod **mods;
  249. int i, apply_mods;
  250. multimaster_mtnode_extension *mtnode_ext;
  251. Replica *r = NULL;
  252. char *replica_root = NULL;
  253. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  254. char *errortext = returntext ? returntext : buf;
  255. char *config_attr, *config_attr_value;
  256. Slapi_Operation *op;
  257. void *identity;
  258. if (returntext)
  259. {
  260. returntext[0] = '\0';
  261. }
  262. *returncode = LDAP_SUCCESS;
  263. /* just let internal operations originated from replication plugin to go through */
  264. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  265. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  266. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  267. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  268. {
  269. *returncode = LDAP_SUCCESS;
  270. return SLAPI_DSE_CALLBACK_OK;
  271. }
  272. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  273. PR_Lock (s_configLock);
  274. mtnode_ext = _replica_config_get_mtnode_ext (e);
  275. PR_ASSERT (mtnode_ext);
  276. if (mtnode_ext->replica)
  277. object_acquire (mtnode_ext->replica);
  278. if (mtnode_ext->replica == NULL)
  279. {
  280. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  281. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  282. errortext);
  283. *returncode = LDAP_OPERATIONS_ERROR;
  284. goto done;
  285. }
  286. r = object_get_data (mtnode_ext->replica);
  287. PR_ASSERT (r);
  288. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  289. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  290. {
  291. /* we only allow the replica ID and type to be modified together e.g.
  292. if converting a read only replica to a master or vice versa -
  293. we will need to change both the replica ID and the type at the same
  294. time - we must disallow changing the replica ID if the type is not
  295. being changed and vice versa
  296. */
  297. char *new_repl_id = NULL;
  298. char *new_repl_type = NULL;
  299. if (*returncode != LDAP_SUCCESS)
  300. break;
  301. for (i = 0; (mods[i] && (LDAP_SUCCESS == rc)); i++)
  302. {
  303. if (*returncode != LDAP_SUCCESS)
  304. break;
  305. config_attr = (char *) mods[i]->mod_type;
  306. PR_ASSERT (config_attr);
  307. /* disallow modifications or removal of replica root,
  308. replica name and replica state attributes */
  309. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  310. strcasecmp (config_attr, attr_replicaName) == 0 ||
  311. strcasecmp (config_attr, attr_state) == 0)
  312. {
  313. *returncode = LDAP_UNWILLING_TO_PERFORM;
  314. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  315. config_attr);
  316. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  317. errortext);
  318. }
  319. /* this is a request to delete an attribute */
  320. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) || mods[i]->mod_bvalues == NULL
  321. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  322. {
  323. /* currently, you can only remove referral,
  324. legacy consumer or bind dn attribute */
  325. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  326. {
  327. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  328. }
  329. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  330. {
  331. if (apply_mods) {
  332. replica_set_referrals(r, NULL);
  333. if (!replica_is_legacy_consumer (r)) {
  334. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  335. }
  336. }
  337. }
  338. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  339. {
  340. if (apply_mods)
  341. replica_set_legacy_consumer (r, PR_FALSE);
  342. }
  343. else
  344. {
  345. *returncode = LDAP_UNWILLING_TO_PERFORM;
  346. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  347. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  348. }
  349. }
  350. else /* modify an attribute */
  351. {
  352. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  353. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  354. {
  355. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  356. }
  357. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  358. {
  359. new_repl_type = slapi_ch_strdup(config_attr_value);
  360. }
  361. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  362. {
  363. new_repl_id = slapi_ch_strdup(config_attr_value);
  364. }
  365. else if (strcasecmp (config_attr, attr_flags) == 0)
  366. {
  367. *returncode = replica_config_change_flags (r, config_attr_value, errortext, apply_mods);
  368. }
  369. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  370. {
  371. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value, errortext, apply_mods);
  372. }
  373. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  374. {
  375. if (apply_mods)
  376. {
  377. Slapi_Mod smod;
  378. Slapi_ValueSet *vs= slapi_valueset_new();
  379. slapi_mod_init_byref(&smod,mods[i]);
  380. slapi_valueset_set_from_smod(vs, &smod);
  381. replica_set_referrals (r, vs);
  382. slapi_mod_done(&smod);
  383. slapi_valueset_free(vs);
  384. if (!replica_is_legacy_consumer (r)) {
  385. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  386. }
  387. }
  388. }
  389. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  390. {
  391. if (apply_mods && config_attr_value && config_attr_value[0])
  392. {
  393. PRUint32 delay;
  394. if (isdigit (config_attr_value[0]))
  395. {
  396. delay = (unsigned int)atoi(config_attr_value);
  397. replica_set_purge_delay(r, delay);
  398. }
  399. else
  400. *returncode = LDAP_OPERATIONS_ERROR;
  401. }
  402. }
  403. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  404. {
  405. if (apply_mods && config_attr_value && config_attr_value[0])
  406. {
  407. long interval;
  408. interval = atol (config_attr_value);
  409. replica_set_tombstone_reap_interval (r, interval);
  410. }
  411. }
  412. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  413. {
  414. if (apply_mods)
  415. {
  416. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  417. (strcasecmp (config_attr_value, "true") == 0) ||
  418. (strcasecmp (config_attr_value, "yes") == 0) ||
  419. (strcasecmp (config_attr_value, "1") == 0);
  420. replica_set_legacy_consumer (r, legacy);
  421. }
  422. }
  423. /* ignore modifiers attributes added by the server */
  424. else if (strcasecmp (config_attr, "modifytimestamp") == 0 ||
  425. strcasecmp (config_attr, "modifiersname") == 0)
  426. {
  427. *returncode = LDAP_SUCCESS;
  428. }
  429. else
  430. {
  431. *returncode = LDAP_UNWILLING_TO_PERFORM;
  432. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  433. "modification of attribute %s is not allowed in replica entry", config_attr);
  434. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  435. }
  436. }
  437. }
  438. if (new_repl_id || new_repl_type)
  439. {
  440. *returncode = replica_config_change_type_and_id(r, new_repl_type, new_repl_id, errortext, apply_mods);
  441. slapi_ch_free_string(&new_repl_id);
  442. slapi_ch_free_string(&new_repl_type);
  443. }
  444. }
  445. done:
  446. if (mtnode_ext->replica)
  447. object_release (mtnode_ext->replica);
  448. /* slapi_ch_free accepts NULL pointer */
  449. slapi_ch_free_string(&replica_root);
  450. PR_Unlock (s_configLock);
  451. if (*returncode != LDAP_SUCCESS)
  452. {
  453. return SLAPI_DSE_CALLBACK_ERROR;
  454. }
  455. else
  456. {
  457. return SLAPI_DSE_CALLBACK_OK;
  458. }
  459. }
  460. static int
  461. replica_config_post_modify(Slapi_PBlock *pb,
  462. Slapi_Entry* entryBefore,
  463. Slapi_Entry* e,
  464. int *returncode,
  465. char *returntext,
  466. void *arg)
  467. {
  468. int rc= 0;
  469. LDAPMod **mods;
  470. int i, apply_mods;
  471. multimaster_mtnode_extension *mtnode_ext;
  472. Replica *r = NULL;
  473. char *replica_root = NULL;
  474. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  475. char *errortext = returntext ? returntext : buf;
  476. char *config_attr, *config_attr_value;
  477. Slapi_Operation *op;
  478. void *identity;
  479. int flag_need_cleanup = 0;
  480. if (returntext)
  481. {
  482. returntext[0] = '\0';
  483. }
  484. *returncode = LDAP_SUCCESS;
  485. /* just let internal operations originated from replication plugin to go through */
  486. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  487. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  488. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  489. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  490. {
  491. *returncode = LDAP_SUCCESS;
  492. return SLAPI_DSE_CALLBACK_OK;
  493. }
  494. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  495. PR_Lock (s_configLock);
  496. mtnode_ext = _replica_config_get_mtnode_ext (e);
  497. PR_ASSERT (mtnode_ext);
  498. if (mtnode_ext->replica)
  499. object_acquire (mtnode_ext->replica);
  500. if (mtnode_ext->replica == NULL)
  501. {
  502. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  503. "replica does not exist for %s", replica_root);
  504. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  505. "replica_config_post_modify: %s\n",
  506. errortext);
  507. *returncode = LDAP_OPERATIONS_ERROR;
  508. goto done;
  509. }
  510. r = object_get_data (mtnode_ext->replica);
  511. PR_ASSERT (r);
  512. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  513. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  514. {
  515. /* we only allow the replica ID and type to be modified together e.g.
  516. if converting a read only replica to a master or vice versa -
  517. we will need to change both the replica ID and the type at the same
  518. time - we must disallow changing the replica ID if the type is not
  519. being changed and vice versa
  520. */
  521. if (*returncode != LDAP_SUCCESS)
  522. break;
  523. for (i = 0; (mods[i] && (LDAP_SUCCESS == rc)); i++)
  524. {
  525. if (*returncode != LDAP_SUCCESS)
  526. break;
  527. config_attr = (char *) mods[i]->mod_type;
  528. PR_ASSERT (config_attr);
  529. /* disallow modifications or removal of replica root,
  530. replica name and replica state attributes */
  531. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  532. strcasecmp (config_attr, attr_replicaName) == 0 ||
  533. strcasecmp (config_attr, attr_state) == 0)
  534. {
  535. *returncode = LDAP_UNWILLING_TO_PERFORM;
  536. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  537. "modification of %s attribute is not allowed",
  538. config_attr);
  539. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  540. "replica_config_post_modify: %s\n",
  541. errortext);
  542. }
  543. /* this is a request to delete an attribute */
  544. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) ||
  545. mods[i]->mod_bvalues == NULL ||
  546. mods[i]->mod_bvalues[0]->bv_val == NULL)
  547. {
  548. ;
  549. }
  550. else /* modify an attribute */
  551. {
  552. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  553. if (strcasecmp (config_attr, TASK_ATTR) == 0)
  554. {
  555. flag_need_cleanup = 1;
  556. }
  557. }
  558. }
  559. }
  560. done:
  561. PR_Unlock (s_configLock);
  562. /* slapi_ch_free accepts NULL pointer */
  563. slapi_ch_free_string (&replica_root);
  564. /* Call replica_cleanup_task after s_configLock is reliesed */
  565. if (flag_need_cleanup)
  566. {
  567. *returncode = replica_cleanup_task(mtnode_ext->replica,
  568. config_attr_value,
  569. errortext, apply_mods);
  570. }
  571. if (mtnode_ext->replica)
  572. object_release (mtnode_ext->replica);
  573. if (*returncode != LDAP_SUCCESS)
  574. {
  575. return SLAPI_DSE_CALLBACK_ERROR;
  576. }
  577. else
  578. {
  579. return SLAPI_DSE_CALLBACK_OK;
  580. }
  581. }
  582. static int
  583. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  584. int *returncode, char *returntext, void *arg)
  585. {
  586. multimaster_mtnode_extension *mtnode_ext;
  587. Replica *r;
  588. PR_Lock (s_configLock);
  589. mtnode_ext = _replica_config_get_mtnode_ext (e);
  590. PR_ASSERT (mtnode_ext);
  591. if (mtnode_ext->replica)
  592. {
  593. /* remove object from the hash */
  594. r = (Replica*)object_get_data (mtnode_ext->replica);
  595. PR_ASSERT (r);
  596. /* The changelog for this replica is no longer valid, so we should remove it. */
  597. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  598. "Warning: The changelog for replica %s is no longer valid since "
  599. "the replica config is being deleted. Removing the changelog.\n",
  600. slapi_sdn_get_dn(replica_get_root(r)));
  601. cl5DeleteDBSync(mtnode_ext->replica);
  602. replica_delete_by_name (replica_get_name (r));
  603. object_release (mtnode_ext->replica);
  604. mtnode_ext->replica = NULL;
  605. }
  606. PR_Unlock (s_configLock);
  607. *returncode = LDAP_SUCCESS;
  608. return SLAPI_DSE_CALLBACK_OK;
  609. }
  610. static int
  611. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  612. char *returntext, void *arg)
  613. {
  614. multimaster_mtnode_extension *mtnode_ext;
  615. int changeCount = 0;
  616. PRBool reapActive = PR_FALSE;
  617. char val [64];
  618. /* add attribute that contains number of entries in the changelog for this replica */
  619. PR_Lock (s_configLock);
  620. mtnode_ext = _replica_config_get_mtnode_ext (e);
  621. PR_ASSERT (mtnode_ext);
  622. if (mtnode_ext->replica) {
  623. Replica *replica;
  624. object_acquire (mtnode_ext->replica);
  625. if (cl5GetState () == CL5_STATE_OPEN) {
  626. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  627. }
  628. replica = (Replica*)object_get_data (mtnode_ext->replica);
  629. if (replica) {
  630. reapActive = replica_get_tombstone_reap_active(replica);
  631. }
  632. object_release (mtnode_ext->replica);
  633. }
  634. sprintf (val, "%d", changeCount);
  635. slapi_entry_add_string (e, type_replicaChangeCount, val);
  636. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  637. PR_Unlock (s_configLock);
  638. return SLAPI_DSE_CALLBACK_OK;
  639. }
  640. static int
  641. replica_config_change_type_and_id (Replica *r, const char *new_type,
  642. const char *new_id, char *returntext,
  643. int apply_mods)
  644. {
  645. int type;
  646. ReplicaType oldtype;
  647. ReplicaId rid;
  648. ReplicaId oldrid;
  649. PR_ASSERT (r);
  650. oldtype = replica_get_type(r);
  651. oldrid = replica_get_rid(r);
  652. if (new_type == NULL) /* by default - replica is read-only */
  653. {
  654. type = REPLICA_TYPE_READONLY;
  655. }
  656. else
  657. {
  658. type = atoi (new_type);
  659. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  660. {
  661. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  662. return LDAP_OPERATIONS_ERROR;
  663. }
  664. }
  665. /* disallow changing type to itself just to permit a replica ID change */
  666. if (oldtype == type)
  667. {
  668. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  669. return LDAP_OPERATIONS_ERROR;
  670. }
  671. if (type == REPLICA_TYPE_READONLY)
  672. {
  673. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  674. }
  675. else if (!new_id)
  676. {
  677. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  678. return LDAP_UNWILLING_TO_PERFORM;
  679. }
  680. else
  681. {
  682. int temprid = atoi (new_id);
  683. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  684. {
  685. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  686. "attribute %s must have a value greater than 0 "
  687. "and less than %d",
  688. attr_replicaId, READ_ONLY_REPLICA_ID);
  689. return LDAP_UNWILLING_TO_PERFORM;
  690. }
  691. else
  692. {
  693. rid = (ReplicaId)temprid;
  694. }
  695. }
  696. /* error if old rid == new rid */
  697. if (oldrid == rid)
  698. {
  699. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  700. return LDAP_OPERATIONS_ERROR;
  701. }
  702. if (apply_mods)
  703. {
  704. replica_set_type (r, type);
  705. replica_set_rid(r, rid);
  706. /* Set the mapping tree node, and the list of referrals */
  707. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  708. if (!replica_is_legacy_consumer(r))
  709. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  710. }
  711. return LDAP_SUCCESS;
  712. }
  713. static int
  714. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  715. int apply_mods)
  716. {
  717. PR_ASSERT (r);
  718. if (apply_mods)
  719. {
  720. Slapi_Mod smod;
  721. Slapi_ValueSet *vs= slapi_valueset_new();
  722. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  723. slapi_valueset_set_from_smod(vs, &smod);
  724. replica_set_updatedn(r, vs, mod->mod_op);
  725. slapi_mod_done(&smod);
  726. slapi_valueset_free(vs);
  727. }
  728. return LDAP_SUCCESS;
  729. }
  730. static int replica_config_change_flags (Replica *r, const char *new_flags,
  731. char *returntext, int apply_mods)
  732. {
  733. PR_ASSERT (r);
  734. if (apply_mods)
  735. {
  736. PRUint32 flags;
  737. flags = atol (new_flags);
  738. replica_replace_flags (r, flags);
  739. }
  740. return LDAP_SUCCESS;
  741. }
  742. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  743. int apply_mods)
  744. {
  745. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  746. {
  747. if (apply_mods)
  748. {
  749. return replica_execute_cl2ldif_task (r, returntext);
  750. }
  751. else
  752. return LDAP_SUCCESS;
  753. }
  754. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  755. {
  756. if (apply_mods)
  757. {
  758. return replica_execute_ldif2cl_task (r, returntext);
  759. }
  760. else
  761. return LDAP_SUCCESS;
  762. }
  763. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  764. {
  765. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  766. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  767. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  768. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  769. return LDAP_OPERATIONS_ERROR;
  770. }
  771. if (apply_mods)
  772. {
  773. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  774. }
  775. else
  776. return LDAP_SUCCESS;
  777. }
  778. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  779. {
  780. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  781. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  782. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  783. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  784. return LDAP_OPERATIONS_ERROR;
  785. }
  786. if (apply_mods)
  787. {
  788. Slapi_Task *empty_task = NULL;
  789. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, empty_task, returntext);
  790. }
  791. else
  792. return LDAP_SUCCESS;
  793. }
  794. else
  795. {
  796. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  797. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  798. "replica_execute_task: %s\n", returntext);
  799. return LDAP_OPERATIONS_ERROR;
  800. }
  801. }
  802. static int
  803. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  804. int apply_mods)
  805. {
  806. int rc = LDAP_SUCCESS;
  807. if (apply_mods) {
  808. Replica *replica = (Replica*)object_get_data (r);
  809. if (NULL == replica) {
  810. rc = LDAP_OPERATIONS_ERROR;
  811. } else {
  812. rc = replica_task_done(replica);
  813. }
  814. }
  815. return rc;
  816. }
  817. static int
  818. replica_task_done(Replica *replica)
  819. {
  820. int rc = LDAP_OPERATIONS_ERROR;
  821. char *replica_dn = NULL;
  822. Slapi_DN *replica_sdn = NULL;
  823. Slapi_PBlock *pb = NULL;
  824. LDAPMod *mods [2];
  825. LDAPMod mod;
  826. if (NULL == replica) {
  827. return rc;
  828. }
  829. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  830. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  831. REPLICA_RDN,
  832. slapi_sdn_get_dn(replica_get_root(replica)),
  833. CONFIG_BASE);
  834. if (NULL == replica_dn) {
  835. return rc;
  836. }
  837. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  838. pb = slapi_pblock_new();
  839. mods[0] = &mod;
  840. mods[1] = NULL;
  841. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  842. mod.mod_type = (char *)TASK_ATTR;
  843. mod.mod_bvalues = NULL;
  844. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  845. NULL/* uniqueid */,
  846. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  847. 0/* flags */);
  848. slapi_modify_internal_pb (pb);
  849. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  850. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  851. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  852. "replica_task_done: "
  853. "failed to remove (%s) attribute from (%s) entry; "
  854. "LDAP error - %d\n",
  855. TASK_ATTR, replica_dn, rc);
  856. }
  857. slapi_pblock_destroy (pb);
  858. slapi_sdn_free(&replica_sdn);
  859. return rc;
  860. }
  861. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  862. {
  863. int rc;
  864. Object *rlist [2];
  865. Replica *replica;
  866. char fName [MAXPATHLEN];
  867. char *clDir = NULL;
  868. if (cl5GetState () != CL5_STATE_OPEN)
  869. {
  870. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  871. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  872. "replica_execute_cl2ldif_task: %s\n", returntext);
  873. rc = LDAP_OPERATIONS_ERROR;
  874. goto bail;
  875. }
  876. rlist[0] = r;
  877. rlist[1] = NULL;
  878. /* file is stored in the changelog directory and is named
  879. <replica name>.ldif */
  880. clDir = cl5GetDir ();
  881. if (NULL == clDir) {
  882. rc = LDAP_OPERATIONS_ERROR;
  883. goto bail;
  884. }
  885. replica = (Replica*)object_get_data (r);
  886. if (NULL == replica) {
  887. rc = LDAP_OPERATIONS_ERROR;
  888. goto bail;
  889. }
  890. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  891. slapi_ch_free_string (&clDir);
  892. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  893. "Beginning changelog export of replica \"%s\"\n",
  894. replica_get_name(replica));
  895. rc = cl5ExportLDIF (fName, rlist);
  896. if (rc == CL5_SUCCESS) {
  897. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  898. "Finished changelog export of replica \"%s\"\n",
  899. replica_get_name(replica));
  900. rc = LDAP_SUCCESS;
  901. } else {
  902. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  903. "Failed changelog export replica %s; "
  904. "changelog error - %d", replica_get_name(replica), rc);
  905. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  906. "replica_execute_cl2ldif_task: %s\n", returntext);
  907. rc = LDAP_OPERATIONS_ERROR;
  908. }
  909. bail:
  910. return rc;
  911. }
  912. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  913. {
  914. int rc, imprc = 0;
  915. Object *rlist [2];
  916. Replica *replica;
  917. char fName [MAXPATHLEN];
  918. char *clDir = NULL;
  919. changelog5Config config;
  920. if (cl5GetState () != CL5_STATE_OPEN)
  921. {
  922. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  923. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  924. "replica_execute_ldif2cl_task: %s\n", returntext);
  925. rc = LDAP_OPERATIONS_ERROR;
  926. goto bail;
  927. }
  928. rlist[0] = r;
  929. rlist[1] = NULL;
  930. /* file is stored in the changelog directory and is named
  931. <replica name>.ldif */
  932. clDir = cl5GetDir ();
  933. if (NULL == clDir) {
  934. rc = LDAP_OPERATIONS_ERROR;
  935. goto bail;
  936. }
  937. replica = (Replica*)object_get_data (r);
  938. if (NULL == replica) {
  939. rc = LDAP_OPERATIONS_ERROR;
  940. goto bail;
  941. }
  942. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  943. rc = cl5Close();
  944. if (rc != CL5_SUCCESS)
  945. {
  946. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  947. "failed to close changelog to import changelog data; "
  948. "changelog error - %d", rc);
  949. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  950. "replica_execute_ldif2cl_task: %s\n", returntext);
  951. rc = LDAP_OPERATIONS_ERROR;
  952. goto bail;
  953. }
  954. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  955. "Beginning changelog import of replica \"%s\"\n",
  956. replica_get_name(replica));
  957. imprc = cl5ImportLDIF (clDir, fName, rlist);
  958. slapi_ch_free_string (&clDir);
  959. if (CL5_SUCCESS == imprc)
  960. {
  961. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  962. "Finished changelog import of replica \"%s\"\n",
  963. replica_get_name(replica));
  964. }
  965. else
  966. {
  967. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  968. "Failed changelog import replica %s; "
  969. "changelog error - %d", replica_get_name(replica), rc);
  970. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  971. "replica_execute_ldif2cl_task: %s\n", returntext);
  972. imprc = LDAP_OPERATIONS_ERROR;
  973. }
  974. changelog5_read_config (&config);
  975. /* restart changelog */
  976. rc = cl5Open (config.dir, &config.dbconfig);
  977. if (CL5_SUCCESS == rc)
  978. {
  979. rc = LDAP_SUCCESS;
  980. }
  981. else
  982. {
  983. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  984. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  985. config.dir?config.dir:"null config dir");
  986. rc = LDAP_OPERATIONS_ERROR;
  987. }
  988. bail:
  989. changelog5_config_done(&config);
  990. /* if cl5ImportLDIF returned an error, report it first. */
  991. return imprc?imprc:rc;
  992. }
  993. static multimaster_mtnode_extension *
  994. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  995. {
  996. const char *replica_root;
  997. Slapi_DN *sdn = NULL;
  998. mapping_tree_node *mtnode;
  999. multimaster_mtnode_extension *ext = NULL;
  1000. /* retirve root of the tree for which replica is configured */
  1001. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1002. if (replica_root == NULL)
  1003. {
  1004. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1005. "configuration entry %s missing %s attribute\n",
  1006. slapi_entry_get_dn((Slapi_Entry *)e),
  1007. attr_replicaRoot);
  1008. return NULL;
  1009. }
  1010. sdn = slapi_sdn_new_dn_passin (replica_root);
  1011. /* locate mapping tree node for the specified subtree */
  1012. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1013. if (mtnode == NULL)
  1014. {
  1015. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1016. "failed to locate mapping tree node for dn %s\n",
  1017. slapi_sdn_get_dn(sdn));
  1018. }
  1019. else
  1020. {
  1021. /* check if replica object already exists for the specified subtree */
  1022. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1023. }
  1024. slapi_sdn_free (&sdn);
  1025. return ext;
  1026. }
  1027. int
  1028. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1029. {
  1030. return replica_execute_cleanruv_task(r, rid, NULL);
  1031. }
  1032. static int
  1033. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1034. {
  1035. Object *RUVObj;
  1036. RUV *local_ruv = NULL;
  1037. Replica *replica = (Replica*)object_get_data (r);
  1038. int rc = 0;
  1039. PR_ASSERT (replica);
  1040. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1041. RUVObj = replica_get_ruv(replica);
  1042. PR_ASSERT(RUVObj);
  1043. local_ruv = (RUV*)object_get_data (RUVObj);
  1044. /* Need to check that :
  1045. * - rid is not the local one
  1046. * - rid is not the last one
  1047. */
  1048. if ((replica_get_rid(replica) == rid) ||
  1049. (ruv_replica_count(local_ruv) <= 1)) {
  1050. return LDAP_UNWILLING_TO_PERFORM;
  1051. }
  1052. rc = ruv_delete_replica(local_ruv, rid);
  1053. replica_set_ruv_dirty(replica);
  1054. replica_write_ruv(replica);
  1055. object_release(RUVObj);
  1056. /* Update Mapping Tree to reflect RUV changes */
  1057. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1058. /*
  1059. * Clean the changelog RUV's
  1060. */
  1061. cl5CleanRUV(rid);
  1062. if (rc != RUV_SUCCESS){
  1063. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1064. return LDAP_OPERATIONS_ERROR;
  1065. }
  1066. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1067. return LDAP_SUCCESS;
  1068. }
  1069. const char *
  1070. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1071. {
  1072. Slapi_Attr *attr;
  1073. Slapi_Value *val = NULL;
  1074. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1075. return default_val;
  1076. slapi_attr_first_value(attr, &val);
  1077. return slapi_value_get_string(val);
  1078. }
  1079. static int
  1080. replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1081. int *returncode, char *returntext, void *arg)
  1082. {
  1083. Slapi_Task *task = NULL;
  1084. const Slapi_DN *task_dn;
  1085. Slapi_DN *dn = NULL;
  1086. Object *r;
  1087. const char *base_dn;
  1088. const char *rid_str;
  1089. ReplicaId rid;
  1090. int rc = SLAPI_DSE_CALLBACK_OK;
  1091. /* allocate new task now */
  1092. task = slapi_new_task(slapi_entry_get_ndn(e));
  1093. if(task == NULL){
  1094. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: Failed to create new task\n");
  1095. rc = SLAPI_DSE_CALLBACK_ERROR;
  1096. goto out;
  1097. }
  1098. /*
  1099. * Get our task settings
  1100. */
  1101. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1102. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1103. rc = SLAPI_DSE_CALLBACK_ERROR;
  1104. goto out;
  1105. }
  1106. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1107. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1108. rc = SLAPI_DSE_CALLBACK_ERROR;
  1109. goto out;
  1110. }
  1111. task_dn = slapi_entry_get_sdn(e);
  1112. /*
  1113. * Check the rid
  1114. */
  1115. rid = atoi(rid_str);
  1116. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1117. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1118. rid, slapi_sdn_get_dn(task_dn));
  1119. cleanruv_log(task, CLEANALLRUV_ID, "%s", returntext);
  1120. rc = LDAP_OPERATIONS_ERROR;
  1121. goto out;
  1122. }
  1123. /*
  1124. * Get the replica object
  1125. */
  1126. dn = slapi_sdn_new_dn_byval(base_dn);
  1127. if((r = replica_get_replica_from_dn(dn)) == NULL){
  1128. *returncode = LDAP_OPERATIONS_ERROR ;
  1129. rc = SLAPI_DSE_CALLBACK_ERROR;
  1130. goto out;
  1131. }
  1132. /* clean the RUV's */
  1133. rc = replica_execute_cleanall_ruv_task (r, rid, task, returntext);
  1134. out:
  1135. if(rc){
  1136. cleanruv_log(task, CLEANALLRUV_ID, "Task failed...(%d)", rc);
  1137. *returncode = rc;
  1138. slapi_task_finish(task, *returncode);
  1139. rc = SLAPI_DSE_CALLBACK_ERROR;
  1140. } else {
  1141. rc = SLAPI_DSE_CALLBACK_OK;
  1142. }
  1143. slapi_sdn_free(&dn);
  1144. return rc;
  1145. }
  1146. /*
  1147. * CLEANALLRUV task
  1148. *
  1149. * [1] Get the maxcsn from the RUV of the rid we want to clean
  1150. * [2] Create the payload for the "cleanallruv" extended ops
  1151. * [3] Create "monitor" thread to do the real work.
  1152. *
  1153. */
  1154. static int
  1155. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, char *returntext)
  1156. {
  1157. PRThread *thread = NULL;
  1158. Slapi_Task *pre_task = NULL; /* this is supposed to be null for logging */
  1159. Replica *replica;
  1160. Object *ruv_obj;
  1161. cleanruv_data *data = NULL;
  1162. CSN *maxcsn = NULL;
  1163. const RUV *ruv;
  1164. struct berval *payload = NULL;
  1165. char *ridstr = NULL;
  1166. char csnstr[CSN_STRSIZE];
  1167. int rc = 0;
  1168. if(get_cleanruv_task_count() >= CLEANRIDSIZ){
  1169. /* we are already running the maximum number of tasks */
  1170. cleanruv_log(pre_task, CLEANALLRUV_ID,
  1171. "Exceeded maximum number of active CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1172. returntext = PR_smprintf("Exceeded maximum number of active CLEANALLRUV tasks(%d), "
  1173. "you must wait for one to finish.", CLEANRIDSIZ);
  1174. return LDAP_UNWILLING_TO_PERFORM;
  1175. }
  1176. /*
  1177. * Grab the replica
  1178. */
  1179. if(r){
  1180. replica = (Replica*)object_get_data (r);
  1181. } else {
  1182. cleanruv_log(pre_task, CLEANALLRUV_ID, "Replica is NULL, aborting task");
  1183. rc = -1;
  1184. goto fail;
  1185. }
  1186. /*
  1187. * Check if this is a consumer
  1188. */
  1189. if(replica_get_type(replica) == REPLICA_TYPE_READONLY){
  1190. /* this is a consumer, send error */
  1191. cleanruv_log(pre_task, CLEANALLRUV_ID, "Failed to clean rid (%d), task can not be run on a consumer",rid);
  1192. if(task){
  1193. rc = -1;
  1194. slapi_task_finish(task, rc);
  1195. }
  1196. return -1;
  1197. }
  1198. /*
  1199. * Grab the max csn of the deleted replica
  1200. */
  1201. ruv_obj = replica_get_ruv(replica);
  1202. ruv = object_get_data (ruv_obj);
  1203. if(ruv_get_rid_max_csn(ruv, &maxcsn, rid) == RUV_BAD_DATA){
  1204. /* no maxcsn, can not proceed */
  1205. cleanruv_log(pre_task, CLEANALLRUV_ID, "Could not find maxcsn for rid (%d)", rid);
  1206. rc = -1;
  1207. object_release(ruv_obj);
  1208. goto fail;
  1209. } else {
  1210. object_release(ruv_obj);
  1211. if(maxcsn == NULL || csn_get_replicaid(maxcsn) == 0){
  1212. /*
  1213. * This is for consistency with extop csn creation, where
  1214. * we want the csn string to be "0000000000000000000" not ""
  1215. */
  1216. csn_free(&maxcsn);
  1217. maxcsn = csn_new();
  1218. csn_init_by_string(maxcsn, "");
  1219. }
  1220. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1221. }
  1222. /*
  1223. * Create payload
  1224. */
  1225. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, slapi_sdn_get_dn(replica_get_root(replica)), csnstr);
  1226. payload = create_ruv_payload(ridstr);
  1227. slapi_ch_free_string(&ridstr);
  1228. if(payload == NULL){
  1229. cleanruv_log(pre_task, CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  1230. rc = -1;
  1231. goto fail;
  1232. }
  1233. /*
  1234. * Launch the cleanallruv thread. Once all the replicas are cleaned it will release the rid
  1235. */
  1236. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1237. if (data == NULL) {
  1238. cleanruv_log(pre_task, CLEANALLRUV_ID, "Failed to allocate cleanruv_data. Aborting task.");
  1239. rc = -1;
  1240. goto fail;
  1241. }
  1242. data->repl_obj = r;
  1243. data->replica = replica;
  1244. data->rid = rid;
  1245. data->task = task;
  1246. data->maxcsn = maxcsn;
  1247. data->payload = payload;
  1248. data->sdn = NULL;
  1249. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread,
  1250. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1251. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1252. if (thread == NULL) {
  1253. rc = -1;
  1254. goto fail;
  1255. } else {
  1256. goto done;
  1257. }
  1258. fail:
  1259. cleanruv_log(pre_task, CLEANALLRUV_ID, "Failed to clean rid (%d)",rid);
  1260. if(task){
  1261. slapi_task_finish(task, rc);
  1262. }
  1263. if(payload){
  1264. ber_bvfree(payload);
  1265. }
  1266. csn_free(&maxcsn);
  1267. object_release (r);
  1268. done:
  1269. return rc;
  1270. }
  1271. void
  1272. replica_cleanallruv_thread_ext(void *arg)
  1273. {
  1274. replica_cleanallruv_thread(arg);
  1275. }
  1276. /*
  1277. * CLEANALLRUV Thread
  1278. *
  1279. * [1] Wait for the maxcsn to be covered
  1280. * [2] Make sure all the replicas are alive
  1281. * [3] Set the cleaned rid
  1282. * [4] Send the cleanAllRUV extop to all the replicas
  1283. * [5] Manually send the CLEANRUV task to replicas that do not support CLEANALLRUV
  1284. * [6] Wait for all the replicas to be cleaned.
  1285. * [7] Trigger cl trimming, release the rid, and remove all the "cleanallruv" attributes
  1286. * from the config.
  1287. */
  1288. static void
  1289. replica_cleanallruv_thread(void *arg)
  1290. {
  1291. Object *ruv_obj = NULL;
  1292. Object *agmt_obj = NULL;
  1293. Repl_Agmt *agmt = NULL;
  1294. RUV *ruv = NULL;
  1295. cleanruv_data *data = arg;
  1296. char csnstr[CSN_STRSIZE];
  1297. char *returntext = NULL;
  1298. char *rid_text = NULL;
  1299. int found_dirty_rid = 1;
  1300. int agmt_not_notified = 1;
  1301. int interval = 10;
  1302. int free_obj = 0;
  1303. int rc = 0;
  1304. /*
  1305. * Initialize our settings
  1306. */
  1307. if(data->replica == NULL && data->repl_obj == NULL){
  1308. /*
  1309. * This thread was initiated at startup because the process did not finish. Due
  1310. * to timing issues, we need to wait to grab the replica obj until we get here.
  1311. */
  1312. data->repl_obj = replica_get_replica_from_dn(data->sdn);
  1313. data->replica = (Replica*)object_get_data(data->repl_obj);
  1314. free_obj = 1;
  1315. }
  1316. if(data->replica == NULL && data->repl_obj){
  1317. data->replica = (Replica*)object_get_data(data->repl_obj);
  1318. }
  1319. if( data->replica && data->repl_obj == NULL){
  1320. data->repl_obj = object_new(data->replica, NULL);
  1321. free_obj = 1;
  1322. }
  1323. if(data->task){
  1324. slapi_task_begin(data->task, 1);
  1325. }
  1326. rid_text = slapi_ch_smprintf("{replica %d ldap", data->rid);
  1327. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1328. /*
  1329. * Add the cleanallruv task to the repl config - so we can handle restarts
  1330. */
  1331. cleanruv_log(data->task, CLEANALLRUV_ID, "Cleaning rid (%d)...", data->rid);
  1332. add_cleaned_rid(data->rid, data->replica, csnstr); /* marks config that we started cleaning a rid */
  1333. /*
  1334. * First, wait for the maxcsn to be covered
  1335. */
  1336. cleanruv_log(data->task, CLEANALLRUV_ID, "Waiting to process all the updates from the deleted replica...");
  1337. ruv_obj = replica_get_ruv(data->replica);
  1338. ruv = object_get_data (ruv_obj);
  1339. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1340. if(csn_get_replicaid(data->maxcsn) == 0 || ruv_covers_csn(ruv,data->maxcsn)){
  1341. /* We are caught up, now we can clean the ruv's */
  1342. break;
  1343. }
  1344. DS_Sleep(PR_SecondsToInterval(5));
  1345. }
  1346. object_release(ruv_obj);
  1347. /*
  1348. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1349. */
  1350. cleanruv_log(data->task, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1351. if(check_agmts_are_alive(data->replica, data->rid, data->task)){
  1352. /* error, aborted or shutdown */
  1353. goto done;
  1354. }
  1355. /*
  1356. * Make sure all the replicas have seen the max csn
  1357. */
  1358. cleanruv_log(data->task, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1359. if(check_agmts_are_caught_up(data->replica, data->rid, csnstr, data->task)){
  1360. /* error, aborted or shutdown */
  1361. goto done;
  1362. }
  1363. /*
  1364. * Set the rid as notified - this blocks the changelog from sending out updates
  1365. * during this process, as well as prevents the db ruv from getting polluted.
  1366. */
  1367. set_cleaned_rid(data->rid);
  1368. /*
  1369. * Now send the cleanruv extended op to all the agreements
  1370. */
  1371. cleanruv_log(data->task, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1372. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1373. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1374. if(agmt_obj == NULL){
  1375. /* no agmts, just clean this replica */
  1376. break;
  1377. }
  1378. while (agmt_obj){
  1379. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1380. if(!agmt_is_enabled(agmt)){ /* skip disabled replicas */
  1381. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1382. continue;
  1383. }
  1384. if(replica_cleanallruv_send_extop(agmt, data->rid, data->task, data->payload, 1) == 0){
  1385. agmt_not_notified = 0;
  1386. } else {
  1387. agmt_not_notified = 1;
  1388. break;
  1389. }
  1390. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1391. }
  1392. if(is_task_aborted(data->rid)){
  1393. goto done;
  1394. }
  1395. if(agmt_not_notified == 0){
  1396. break;
  1397. }
  1398. /*
  1399. * need to sleep between passes
  1400. */
  1401. cleanruv_log(data->task, CLEANALLRUV_ID, "Not all replicas have received the "
  1402. "cleanallruv extended op, retrying in %d seconds",interval);
  1403. PR_Lock( notify_lock );
  1404. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1405. PR_Unlock( notify_lock );
  1406. if(interval < 14400){ /* 4 hour max */
  1407. interval = interval * 2;
  1408. } else {
  1409. interval = 14400;
  1410. }
  1411. }
  1412. /*
  1413. * Run the CLEANRUV task
  1414. */
  1415. cleanruv_log(data->task, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1416. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1417. /*
  1418. * Wait for all the replicas to be cleaned
  1419. */
  1420. cleanruv_log(data->task, CLEANALLRUV_ID,"Waiting for all the replicas to be cleaned...");
  1421. interval = 10;
  1422. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1423. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1424. if(agmt_obj == NULL){
  1425. break;
  1426. }
  1427. while (agmt_obj && !slapi_is_shutting_down()){
  1428. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1429. if(!agmt_is_enabled(agmt)){ /* skip disabled replicas */
  1430. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1431. continue;
  1432. }
  1433. if(replica_cleanallruv_check_ruv(agmt, rid_text, data->task) == 0){
  1434. found_dirty_rid = 0;
  1435. } else {
  1436. found_dirty_rid = 1;
  1437. break;
  1438. }
  1439. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1440. }
  1441. /* If the task is abort or everyone is cleaned, break out */
  1442. if(is_task_aborted(data->rid)){
  1443. goto done;
  1444. }
  1445. if(found_dirty_rid == 0){
  1446. break;
  1447. }
  1448. /*
  1449. * need to sleep between passes
  1450. */
  1451. cleanruv_log(data->task, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1452. "retrying in %d seconds", interval);
  1453. PR_Lock( notify_lock );
  1454. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1455. PR_Unlock( notify_lock );
  1456. if(interval < 14400){ /* 4 hour max */
  1457. interval = interval * 2;
  1458. } else {
  1459. interval = 14400;
  1460. }
  1461. } /* while */
  1462. done:
  1463. /*
  1464. * If the replicas are cleaned, release the rid, and trim the changelog
  1465. */
  1466. if(!found_dirty_rid){
  1467. trigger_cl_trimming(data->rid);
  1468. delete_cleaned_rid(data->replica, data->rid, data->maxcsn);
  1469. cleanruv_log(data->task, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1470. slapi_task_finish(data->task, rc);
  1471. } else {
  1472. /*
  1473. * Shutdown or abort
  1474. */
  1475. if(!is_task_aborted(data->rid)){
  1476. cleanruv_log(data->task, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1477. } else {
  1478. cleanruv_log(data->task, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1479. }
  1480. if(data->task){
  1481. slapi_task_finish(data->task, rc);
  1482. }
  1483. }
  1484. if(data->payload){
  1485. ber_bvfree(data->payload);
  1486. }
  1487. if(data->repl_obj && free_obj){
  1488. object_release(data->repl_obj);
  1489. }
  1490. slapi_sdn_free(&data->sdn);
  1491. slapi_ch_free_string(&rid_text);
  1492. csn_free(&data->maxcsn);
  1493. slapi_ch_free((void **)&data);
  1494. }
  1495. /*
  1496. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  1497. */
  1498. static int
  1499. check_agmts_are_caught_up(Replica *replica, ReplicaId rid, char *maxcsn, Slapi_Task *task)
  1500. {
  1501. Object *agmt_obj;
  1502. Repl_Agmt *agmt;
  1503. char *rid_text;
  1504. int not_all_caughtup = 1;
  1505. int interval = 10;
  1506. rid_text = slapi_ch_smprintf("{replica %d ldap", rid);
  1507. while(not_all_caughtup && !is_task_aborted(rid) && !slapi_is_shutting_down()){
  1508. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  1509. if(agmt_obj == NULL){
  1510. not_all_caughtup = 0;
  1511. break;
  1512. }
  1513. while (agmt_obj){
  1514. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1515. if(!agmt_is_enabled(agmt)){ /* skip disabled replicas */
  1516. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1517. continue;
  1518. }
  1519. if(replica_cleanallruv_check_maxcsn(agmt, rid_text, maxcsn, task) == 0){
  1520. not_all_caughtup = 0;
  1521. } else {
  1522. not_all_caughtup = 1;
  1523. break;
  1524. }
  1525. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1526. } /* agmt while */
  1527. if(not_all_caughtup == 0 || is_task_aborted(rid) ){
  1528. break;
  1529. }
  1530. cleanruv_log(task, CLEANALLRUV_ID, "Not all replicas caught up, retrying in %d seconds",interval);
  1531. PR_Lock( notify_lock );
  1532. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1533. PR_Unlock( notify_lock );
  1534. if(interval < 14400){ /* 4 hour max */
  1535. interval = interval * 2;
  1536. } else {
  1537. interval = 14400;
  1538. }
  1539. }
  1540. slapi_ch_free_string(&rid_text);
  1541. if(is_task_aborted(rid)){
  1542. return -1;
  1543. }
  1544. return not_all_caughtup;
  1545. }
  1546. /*
  1547. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  1548. */
  1549. static int
  1550. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  1551. {
  1552. Object *agmt_obj;
  1553. Repl_Agmt *agmt;
  1554. int not_all_alive = 1;
  1555. int interval = 10;
  1556. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  1557. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  1558. if(agmt_obj == NULL){
  1559. not_all_alive = 0;
  1560. break;
  1561. }
  1562. while (agmt_obj){
  1563. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1564. if(!agmt_is_enabled(agmt)){ /* skip disabled replicas */
  1565. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1566. continue;
  1567. }
  1568. if(replica_cleanallruv_replica_alive(agmt) == 0){
  1569. not_all_alive = 0;
  1570. } else {
  1571. not_all_alive = 1;
  1572. break;
  1573. }
  1574. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  1575. }
  1576. if(not_all_alive == 0 || is_task_aborted(rid)){
  1577. break;
  1578. }
  1579. cleanruv_log(task, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",interval);
  1580. PR_Lock( notify_lock );
  1581. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1582. PR_Unlock( notify_lock );
  1583. if(interval < 14400){ /* 4 hour max */
  1584. interval = interval * 2;
  1585. } else {
  1586. interval = 14400;
  1587. }
  1588. }
  1589. if(is_task_aborted(rid)){
  1590. return -1;
  1591. }
  1592. return not_all_alive;
  1593. }
  1594. /*
  1595. * Create the CLEANALLRUV extended op payload
  1596. */
  1597. struct berval *
  1598. create_ruv_payload(char *value)
  1599. {
  1600. struct berval *req_data = NULL;
  1601. BerElement *tmp_bere = NULL;
  1602. if ((tmp_bere = der_alloc()) == NULL){
  1603. goto error;
  1604. }
  1605. if (ber_printf(tmp_bere, "{s", value) == -1){
  1606. goto error;
  1607. }
  1608. if (ber_printf(tmp_bere, "}") == -1){
  1609. goto error;
  1610. }
  1611. if (ber_flatten(tmp_bere, &req_data) == -1){
  1612. goto error;
  1613. }
  1614. goto done;
  1615. error:
  1616. if (NULL != req_data){
  1617. ber_bvfree(req_data);
  1618. req_data = NULL;
  1619. }
  1620. done:
  1621. if (NULL != tmp_bere){
  1622. ber_free(tmp_bere, 1);
  1623. tmp_bere = NULL;
  1624. }
  1625. return req_data;
  1626. }
  1627. /*
  1628. * Manually add the CLEANRUV task to replicas that do not support
  1629. * the CLEANALLRUV task.
  1630. */
  1631. static void
  1632. replica_send_cleanruv_task(Repl_Agmt *agmt, ReplicaId rid, Slapi_Task *task)
  1633. {
  1634. Repl_Connection *conn;
  1635. ConnResult crc = 0;
  1636. LDAP *ld;
  1637. Slapi_DN *dn;
  1638. struct berval *vals[2];
  1639. struct berval val;
  1640. LDAPMod *mods[2];
  1641. LDAPMod mod;
  1642. char *repl_dn = NULL;
  1643. char data[15];
  1644. int rc;
  1645. if((conn = conn_new(agmt)) == NULL){
  1646. return;
  1647. }
  1648. crc = conn_connect(conn);
  1649. if (CONN_OPERATION_SUCCESS != crc){
  1650. conn_delete_internal_ext(conn);
  1651. return;
  1652. }
  1653. ld = conn_get_ldap(conn);
  1654. if(ld == NULL){
  1655. conn_delete_internal_ext(conn);
  1656. return;
  1657. }
  1658. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", rid);
  1659. dn = agmt_get_replarea(agmt);
  1660. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  1661. mod.mod_type = "nsds5task";
  1662. mod.mod_bvalues = vals;
  1663. vals [0] = &val;
  1664. vals [1] = NULL;
  1665. val.bv_val = data;
  1666. mods[0] = &mod;
  1667. mods[1] = NULL;
  1668. repl_dn = PR_smprintf("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(dn));
  1669. /*
  1670. * Add task to remote replica
  1671. */
  1672. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  1673. if(rc != LDAP_SUCCESS){
  1674. cleanruv_log(task, CLEANALLRUV_ID, "Failed to add CLEANRUV task replica "
  1675. "(%s). You will need to manually run the CLEANRUV task on this replica (%s) error (%d)",
  1676. agmt_get_long_name(agmt), agmt_get_hostname(agmt), rc);
  1677. }
  1678. slapi_ch_free_string(&repl_dn);
  1679. conn_delete_internal_ext(conn);
  1680. }
  1681. /*
  1682. * Check if the rid is in our list of "cleaned" rids
  1683. */
  1684. int
  1685. is_cleaned_rid(ReplicaId rid)
  1686. {
  1687. int i;
  1688. slapi_rwlock_rdlock(rid_lock);
  1689. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  1690. if(rid == cleaned_rids[i]){
  1691. slapi_rwlock_unlock(rid_lock);
  1692. return 1;
  1693. }
  1694. }
  1695. slapi_rwlock_unlock(rid_lock);
  1696. return 0;
  1697. }
  1698. int
  1699. is_task_aborted(ReplicaId rid)
  1700. {
  1701. int i;
  1702. if(rid == 0){
  1703. return 0;
  1704. }
  1705. slapi_rwlock_rdlock(abort_rid_lock);
  1706. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  1707. if(rid == aborted_rids[i]){
  1708. slapi_rwlock_unlock(abort_rid_lock);
  1709. return 1;
  1710. }
  1711. }
  1712. slapi_rwlock_unlock(abort_rid_lock);
  1713. return 0;
  1714. }
  1715. /*
  1716. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  1717. * This prevent the changelog from sending updates from this rid, and the local ruv
  1718. * will not be updated either.
  1719. */
  1720. void
  1721. set_cleaned_rid(ReplicaId rid)
  1722. {
  1723. int i;
  1724. slapi_rwlock_wrlock(rid_lock);
  1725. for(i = 0; i < CLEANRIDSIZ; i++){
  1726. if(cleaned_rids[i] == 0){
  1727. cleaned_rids[i] = rid;
  1728. cleaned_rids[i + 1] = 0;
  1729. }
  1730. }
  1731. slapi_rwlock_unlock(rid_lock);
  1732. }
  1733. /*
  1734. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  1735. */
  1736. void
  1737. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn)
  1738. {
  1739. Slapi_PBlock *pb;
  1740. struct berval *vals[2];
  1741. struct berval val;
  1742. LDAPMod *mods[2];
  1743. LDAPMod mod;
  1744. char data[CSN_STRSIZE + 10];
  1745. char *dn;
  1746. int rc;
  1747. /*
  1748. * Write the rid & maxcsn to the config entry
  1749. */
  1750. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s", rid, maxcsn);
  1751. dn = replica_get_dn(r);
  1752. pb = slapi_pblock_new();
  1753. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  1754. mod.mod_type = (char *)type_replicaCleanRUV;
  1755. mod.mod_bvalues = vals;
  1756. vals [0] = &val;
  1757. vals [1] = NULL;
  1758. val.bv_val = data;
  1759. mods[0] = &mod;
  1760. mods[1] = NULL;
  1761. replica_add_cleanruv_data(r, val.bv_val);
  1762. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  1763. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1764. slapi_modify_internal_pb (pb);
  1765. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1766. if (rc != LDAP_SUCCESS){
  1767. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  1768. "config (%d), rid (%d)\n", rc, rid);
  1769. }
  1770. slapi_ch_free_string(&dn);
  1771. slapi_pblock_destroy(pb);
  1772. }
  1773. /*
  1774. * Add aborted rid and repl root to config in case of a server restart
  1775. */
  1776. void
  1777. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  1778. {
  1779. Slapi_PBlock *pb;
  1780. struct berval *vals[2];
  1781. struct berval val;
  1782. LDAPMod *mods[2];
  1783. LDAPMod mod;
  1784. char *data;
  1785. char *dn;
  1786. int rc;
  1787. int i;
  1788. slapi_rwlock_wrlock(abort_rid_lock);
  1789. for(i = 0; i < CLEANRIDSIZ; i++){
  1790. if(aborted_rids[i] == 0){
  1791. aborted_rids[i] = rid;
  1792. aborted_rids[i + 1] = 0;
  1793. break;
  1794. }
  1795. }
  1796. slapi_rwlock_unlock(abort_rid_lock);
  1797. /*
  1798. * Write the rid to the config entry
  1799. */
  1800. dn = replica_get_dn(r);
  1801. pb = slapi_pblock_new();
  1802. data = PR_smprintf("%d:%s", rid, repl_root);
  1803. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  1804. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  1805. mod.mod_bvalues = vals;
  1806. vals [0] = &val;
  1807. vals [1] = NULL;
  1808. val.bv_val = data;
  1809. val.bv_len = strlen (data);
  1810. mods[0] = &mod;
  1811. mods[1] = NULL;
  1812. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  1813. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1814. slapi_modify_internal_pb (pb);
  1815. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1816. if (rc != LDAP_SUCCESS){
  1817. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  1818. "replica config (%d), rid (%d)\n", rc, rid);
  1819. }
  1820. slapi_ch_free_string(&dn);
  1821. slapi_ch_free_string(&data);
  1822. slapi_pblock_destroy(pb);
  1823. }
  1824. void
  1825. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root){
  1826. Slapi_PBlock *pb;
  1827. LDAPMod *mods[2];
  1828. LDAPMod mod;
  1829. struct berval *vals[2];
  1830. struct berval val;
  1831. char *data;
  1832. char *dn;
  1833. int rc;
  1834. int i;
  1835. if(r == NULL)
  1836. return;
  1837. /*
  1838. * Remove this rid, and optimize the array
  1839. */
  1840. slapi_rwlock_wrlock(abort_rid_lock);
  1841. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  1842. for(; i < CLEANRIDSIZ; i++){
  1843. /* rewrite entire array */
  1844. aborted_rids[i] = aborted_rids[i + 1];
  1845. }
  1846. slapi_rwlock_unlock(abort_rid_lock);
  1847. /*
  1848. * Prepare the mods for the config entry
  1849. */
  1850. dn = replica_get_dn(r);
  1851. pb = slapi_pblock_new();
  1852. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  1853. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  1854. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  1855. mod.mod_bvalues = vals;
  1856. vals [0] = &val;
  1857. vals [1] = NULL;
  1858. val.bv_val = data;
  1859. val.bv_len = strlen (data);
  1860. mods[0] = &mod;
  1861. mods[1] = NULL;
  1862. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1863. slapi_modify_internal_pb (pb);
  1864. slapi_pblock_destroy (pb);
  1865. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1866. if (rc != LDAP_SUCCESS){
  1867. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  1868. "config (%d), rid (%d)\n", rc, rid);
  1869. }
  1870. slapi_ch_free_string(&dn);
  1871. slapi_ch_free_string(&data);
  1872. }
  1873. /*
  1874. * Remove the rid from our list, and the config
  1875. */
  1876. void
  1877. delete_cleaned_rid(Replica *r, ReplicaId rid, CSN *maxcsn)
  1878. {
  1879. Slapi_PBlock *pb;
  1880. Object *agmt_obj;
  1881. Repl_Agmt *agmt;
  1882. LDAPMod *mods[2];
  1883. LDAPMod mod;
  1884. struct berval *vals[2];
  1885. struct berval val;
  1886. char *dn;
  1887. char data[CSN_STRSIZE + 10];
  1888. char csnstr[CSN_STRSIZE];
  1889. int rc;
  1890. int i;
  1891. if(r == NULL || maxcsn == NULL)
  1892. return;
  1893. /*
  1894. * Remove this rid, and optimize the array
  1895. */
  1896. slapi_rwlock_wrlock(rid_lock);
  1897. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  1898. for(; i < CLEANRIDSIZ; i++){
  1899. /* rewrite entire array */
  1900. cleaned_rids[i] = cleaned_rids[i + 1];
  1901. }
  1902. slapi_rwlock_unlock(rid_lock);
  1903. /*
  1904. * Prepare the mods for the config entry
  1905. */
  1906. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1907. val.bv_len = PR_snprintf(data, sizeof(data), "%d:%s", (int)rid, csnstr);
  1908. dn = replica_get_dn(r);
  1909. pb = slapi_pblock_new();
  1910. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  1911. mod.mod_type = (char *)type_replicaCleanRUV;
  1912. mod.mod_bvalues = vals;
  1913. vals [0] = &val;
  1914. vals [1] = NULL;
  1915. val.bv_val = data;
  1916. mods[0] = &mod;
  1917. mods[1] = NULL;
  1918. replica_remove_cleanruv_data(r, data);
  1919. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1920. slapi_modify_internal_pb (pb);
  1921. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1922. if (rc != LDAP_SUCCESS){
  1923. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to remove replica config "
  1924. "(%d), rid (%d)\n", rc, rid);
  1925. }
  1926. slapi_pblock_destroy (pb);
  1927. slapi_ch_free_string(&dn);
  1928. /*
  1929. * Now release the cleaned rid from the repl agmts
  1930. */
  1931. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  1932. while (agmt_obj){
  1933. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1934. if(!agmt_is_enabled(agmt)){
  1935. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  1936. continue;
  1937. }
  1938. agmt_set_cleanruv_data(agmt, rid, CLEANRUV_RELEASED);
  1939. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  1940. }
  1941. }
  1942. /*
  1943. * Abort the CLEANALLRUV task
  1944. */
  1945. int
  1946. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1947. int *returncode, char *returntext, void *arg)
  1948. {
  1949. PRThread *thread = NULL;
  1950. struct berval *payload = NULL;
  1951. Slapi_Task *task = NULL;
  1952. Replica *replica;
  1953. ReplicaId rid;
  1954. cleanruv_data *data = NULL;
  1955. const Slapi_DN *dn;
  1956. Object *r;
  1957. CSN *maxcsn;
  1958. const char *base_dn;
  1959. const char *rid_str;
  1960. char *ridstr;
  1961. int rc = SLAPI_DSE_CALLBACK_OK;
  1962. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  1963. /* we are already running the maximum number of tasks */
  1964. cleanruv_log(task, ABORT_CLEANALLRUV_ID,
  1965. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1966. returntext = PR_smprintf("Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d), "
  1967. "you must wait for one to finish.", CLEANRIDSIZ);
  1968. *returncode = LDAP_OPERATIONS_ERROR;
  1969. return SLAPI_DSE_CALLBACK_ERROR;
  1970. }
  1971. /* allocate new task now */
  1972. task = slapi_new_task(slapi_entry_get_ndn(e));
  1973. /*
  1974. * Get our task settings
  1975. */
  1976. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1977. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"Missing required attr \"replica-id\"");
  1978. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1979. rc = SLAPI_DSE_CALLBACK_ERROR;
  1980. goto out;
  1981. }
  1982. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1983. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"Missing required attr \"replica-base-dn\"");
  1984. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1985. rc = SLAPI_DSE_CALLBACK_ERROR;
  1986. goto out;
  1987. }
  1988. /*
  1989. * Check the rid
  1990. */
  1991. rid = atoi(rid_str);
  1992. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1993. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1994. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  1995. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"%s", returntext);
  1996. *returncode = LDAP_OPERATIONS_ERROR;
  1997. rc = SLAPI_DSE_CALLBACK_ERROR;
  1998. goto out;
  1999. }
  2000. /*
  2001. * Get the replica object
  2002. */
  2003. dn = slapi_sdn_new_dn_byval(base_dn);
  2004. if((r = replica_get_replica_from_dn(dn)) == NULL){
  2005. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"Failed to find replica from dn(%s)", base_dn);
  2006. *returncode = LDAP_OPERATIONS_ERROR;
  2007. rc = SLAPI_DSE_CALLBACK_ERROR;
  2008. goto out;
  2009. }
  2010. /*
  2011. * Create payload
  2012. */
  2013. ridstr = slapi_ch_smprintf("%d:%s", rid, base_dn);
  2014. payload = create_ruv_payload(ridstr);
  2015. if(payload == NULL){
  2016. cleanruv_log(task, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2017. *returncode = LDAP_OPERATIONS_ERROR;
  2018. rc = SLAPI_DSE_CALLBACK_ERROR;
  2019. goto out;
  2020. }
  2021. /*
  2022. * Stop the cleaning, and delete the rid
  2023. */
  2024. replica = (Replica*)object_get_data (r);
  2025. maxcsn = replica_get_cleanruv_maxcsn(replica, rid);
  2026. delete_cleaned_rid(replica, rid, maxcsn);
  2027. add_aborted_rid(rid, replica, (char *)base_dn);
  2028. stop_ruv_cleaning();
  2029. /*
  2030. * Prepare the abort struct and fire off the thread
  2031. */
  2032. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2033. if (data == NULL) {
  2034. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2035. *returncode = LDAP_OPERATIONS_ERROR;
  2036. rc = SLAPI_DSE_CALLBACK_ERROR;
  2037. goto out;
  2038. }
  2039. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2040. data->replica = replica;
  2041. data->task = task;
  2042. data->payload = payload;
  2043. data->rid = rid;
  2044. data->repl_root = slapi_ch_strdup(base_dn);
  2045. data->sdn = NULL;
  2046. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2047. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2048. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2049. if (thread == NULL) {
  2050. object_release(r);
  2051. cleanruv_log(task, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2052. *returncode = LDAP_OPERATIONS_ERROR;
  2053. rc = SLAPI_DSE_CALLBACK_ERROR;
  2054. }
  2055. out:
  2056. csn_free(&maxcsn);
  2057. slapi_ch_free_string(&ridstr);
  2058. if(rc != SLAPI_DSE_CALLBACK_OK){
  2059. cleanruv_log(task, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2060. slapi_task_finish(task, rc);
  2061. }
  2062. return rc;
  2063. }
  2064. /*
  2065. * Abort CLEANALLRUV task thread
  2066. */
  2067. void
  2068. replica_abort_task_thread(void *arg)
  2069. {
  2070. cleanruv_data *data = (cleanruv_data *)arg;
  2071. Repl_Agmt *agmt;
  2072. Object *agmt_obj;
  2073. int agmt_not_notified = 1;
  2074. int interval = 10;
  2075. int release_it = 0;
  2076. /*
  2077. * Need to build the replica from the dn
  2078. */
  2079. if(data->replica == NULL && data->repl_obj == NULL){
  2080. /*
  2081. * This thread was initiated at startup because the process did not finish. Due
  2082. * to timing issues, we need to wait to grab the replica obj until we get here.
  2083. */
  2084. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2085. cleanruv_log(data->task, ABORT_CLEANALLRUV_ID, "Failed to get replica from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2086. goto done;
  2087. }
  2088. if(data->replica == NULL && data->repl_obj){
  2089. data->replica = (Replica*)object_get_data(data->repl_obj);
  2090. }
  2091. release_it = 1;
  2092. }
  2093. /*
  2094. * Now send the cleanruv extended op to all the agreements
  2095. */
  2096. while(agmt_not_notified && !slapi_is_shutting_down()){
  2097. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2098. while (agmt_obj){
  2099. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2100. if(!agmt_is_enabled(agmt)){ /* skip disabled replicas */
  2101. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2102. continue;
  2103. }
  2104. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2105. agmt_not_notified = 1;
  2106. break;
  2107. } else {
  2108. /* success */
  2109. agmt_not_notified = 0;
  2110. }
  2111. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2112. } /* while loop for agmts */
  2113. if(agmt_not_notified == 0){
  2114. /* everybody has been contacted */
  2115. break;
  2116. }
  2117. /*
  2118. * need to sleep between passes
  2119. */
  2120. cleanruv_log(data->task, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2121. PR_Lock( notify_lock );
  2122. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2123. PR_Unlock( notify_lock );
  2124. if(interval < 14400){ /* 4 hour max */
  2125. interval = interval * 2;
  2126. } else {
  2127. interval = 14400;
  2128. }
  2129. } /* while */
  2130. done:
  2131. if(agmt_not_notified){
  2132. /* failure */
  2133. cleanruv_log(data->task, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2134. } else {
  2135. /*
  2136. * Clean up the config
  2137. */
  2138. delete_aborted_rid(data->replica, data->rid, data->repl_root);
  2139. cleanruv_log(data->task, ABORT_CLEANALLRUV_ID, "Successfully aborted cleanAllRUV task for rid(%d)", data->rid);
  2140. }
  2141. if(data->task){
  2142. slapi_task_finish(data->task, agmt_not_notified);
  2143. }
  2144. if(data->repl_obj && release_it)
  2145. object_release(data->repl_obj);
  2146. if(data->payload){
  2147. ber_bvfree(data->payload);
  2148. }
  2149. slapi_ch_free_string(&data->repl_root);
  2150. slapi_sdn_free(&data->sdn);
  2151. slapi_ch_free((void **)&data);
  2152. }
  2153. static int
  2154. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2155. {
  2156. Repl_Connection *conn = NULL;
  2157. ConnResult crc = 0;
  2158. int msgid = 0;
  2159. int rc = 0;
  2160. if((conn = conn_new(ra)) == NULL){
  2161. return -1;
  2162. }
  2163. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2164. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2165. /*
  2166. * success or failure, just return the error code
  2167. */
  2168. rc = crc;
  2169. if(rc){
  2170. cleanruv_log(task, ABORT_CLEANALLRUV_ID, "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2171. }
  2172. } else {
  2173. cleanruv_log(task, ABORT_CLEANALLRUV_ID, "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2174. rc = -1;
  2175. }
  2176. conn_delete_internal_ext(conn);
  2177. return rc;
  2178. }
  2179. static int
  2180. replica_cleanallruv_send_extop(Repl_Agmt *ra, ReplicaId rid, Slapi_Task *task, struct berval *payload, int check_result)
  2181. {
  2182. Repl_Connection *conn = NULL;
  2183. ConnResult crc = 0;
  2184. int msgid = 0;
  2185. int rc = 0;
  2186. if((conn = conn_new(ra)) == NULL){
  2187. return -1;
  2188. }
  2189. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2190. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, payload, NULL, &msgid);
  2191. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2192. struct berval *retsdata = NULL;
  2193. char *retoid = NULL;
  2194. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2195. if (CONN_OPERATION_SUCCESS == crc ){
  2196. struct berval **ruv_bervals = NULL;
  2197. struct berval *data = NULL;
  2198. char *data_guid = NULL;
  2199. decode_repl_ext_response(retsdata, &rc, &ruv_bervals, &data_guid, &data);
  2200. /* just free everything, we only wanted "rc" */
  2201. slapi_ch_free_string(&data_guid);
  2202. if(data)
  2203. ber_bvfree(data);
  2204. if (ruv_bervals)
  2205. ber_bvecfree(ruv_bervals);
  2206. if(rc == 0 ){ /* rc == 1 is success */
  2207. cleanruv_log(task, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. Sending replica CLEANRUV task...",
  2208. slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2209. /*
  2210. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2211. * add the CLEANRUV task to the replica.
  2212. */
  2213. replica_send_cleanruv_task(ra, rid, task);
  2214. } else {
  2215. /* extop was accepted */
  2216. rc = 0;
  2217. }
  2218. if (NULL != retoid)
  2219. ldap_memfree(retoid);
  2220. if (NULL != retsdata)
  2221. ber_bvfree(retsdata);
  2222. }
  2223. agmt_set_cleanruv_data(ra, rid, CLEANRUV_NOTIFIED);
  2224. } else {
  2225. /*
  2226. * success or failure, just return the error code
  2227. */
  2228. rc = crc;
  2229. }
  2230. } else {
  2231. rc =-1;
  2232. }
  2233. conn_delete_internal_ext(conn);
  2234. return rc;
  2235. }
  2236. static int
  2237. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *rid_text, char *maxcsn, Slapi_Task *task)
  2238. {
  2239. Repl_Connection *conn = NULL;
  2240. LDAP *ld;
  2241. Slapi_DN *dn = agmt_get_replarea(agmt);
  2242. struct berval **vals;
  2243. LDAPMessage *result = NULL, *entry = NULL;
  2244. BerElement *ber;
  2245. char *attrs[2];
  2246. char *attr = NULL;
  2247. char *iter = NULL;
  2248. char *ruv_part = NULL;
  2249. int found_rid = 0;
  2250. int part_count = 0;
  2251. int rc = 0, i;
  2252. if((conn = conn_new(agmt)) == NULL){
  2253. return -1;
  2254. }
  2255. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2256. attrs[0] = "nsds50ruv";
  2257. attrs[1] = NULL;
  2258. ld = conn_get_ldap(conn);
  2259. if(ld == NULL){
  2260. conn_delete_internal_ext(conn);
  2261. return -1;
  2262. }
  2263. rc = ldap_search_ext_s(ld, slapi_sdn_get_dn(dn), LDAP_SCOPE_SUBTREE,
  2264. "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))",
  2265. attrs, 0, NULL, NULL, NULL, 0, &result);
  2266. slapi_sdn_free(&dn);
  2267. if(rc != LDAP_SUCCESS){
  2268. cleanruv_log(task, CLEANALLRUV_ID,"Failed to contact "
  2269. "agmt (%s) error (%d), will retry later.", agmt_get_long_name(agmt), rc);
  2270. conn_delete_internal_ext(conn);
  2271. return -1;
  2272. }
  2273. entry = ldap_first_entry( ld, result );
  2274. if ( entry != NULL ) {
  2275. for ( attr = ldap_first_attribute( ld, entry, &ber ); attr != NULL; attr = ldap_next_attribute( ld, entry, ber ) ){
  2276. /* make sure the attribute is nsds50ruv */
  2277. if(strcasecmp(attr,"nsds50ruv") != 0){
  2278. ldap_memfree( attr );
  2279. continue;
  2280. }
  2281. found_rid = 0;
  2282. if ((vals = ldap_get_values_len( ld, entry, attr)) != NULL ) {
  2283. for ( i = 0; vals[i] && vals[i]->bv_val; i++ ) {
  2284. /* look for this replica */
  2285. if(strstr(vals[i]->bv_val, rid_text)){
  2286. found_rid = 1;
  2287. /* get the max csn compare it to our known max csn */
  2288. ruv_part = ldap_utf8strtok_r(vals[i]->bv_val, " ", &iter);
  2289. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  2290. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  2291. }
  2292. if(part_count == 5){
  2293. /* we have the maxcsn */
  2294. if(strcmp(ruv_part, maxcsn)){
  2295. /* we are not caught up yet, free, and return */
  2296. ldap_value_free_len(vals);
  2297. ldap_memfree( attr );
  2298. ldap_msgfree( result );
  2299. if(ber){
  2300. ber_free( ber, 0 );
  2301. }
  2302. conn_delete_internal_ext(conn);
  2303. return -1;
  2304. } else {
  2305. /* ok this replica has all the updates from the deleted replica */
  2306. rc = 0;
  2307. }
  2308. } else {
  2309. /* there is no maxcsn for this rid - treat it as caught up */
  2310. rc = 0;
  2311. }
  2312. }
  2313. }
  2314. if(!found_rid){
  2315. /* must have been cleaned already */
  2316. rc = 0;
  2317. }
  2318. ldap_value_free_len(vals);
  2319. }
  2320. ldap_memfree( attr );
  2321. }
  2322. if ( ber != NULL ) {
  2323. ber_free( ber, 0 );
  2324. }
  2325. }
  2326. if(result)
  2327. ldap_msgfree( result );
  2328. } else {
  2329. rc = -1;
  2330. }
  2331. conn_delete_internal_ext(conn);
  2332. return rc;
  2333. }
  2334. static int
  2335. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  2336. {
  2337. Repl_Connection *conn = NULL;
  2338. LDAP *ld = NULL;
  2339. LDAPMessage *result = NULL;
  2340. int rc = 0;
  2341. if((conn = conn_new(agmt)) == NULL){
  2342. return -1;
  2343. }
  2344. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2345. ld = conn_get_ldap(conn);
  2346. if(ld == NULL){
  2347. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanAllRUV_task: failed to get LDAP "
  2348. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  2349. conn_delete_internal_ext(conn);
  2350. return -1;
  2351. }
  2352. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  2353. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  2354. {
  2355. rc = 0;
  2356. } else {
  2357. rc = -1;
  2358. }
  2359. if(result)
  2360. ldap_msgfree( result );
  2361. } else {
  2362. rc = -1;
  2363. }
  2364. conn_delete_internal_ext(conn);
  2365. return rc;
  2366. }
  2367. static int
  2368. replica_cleanallruv_check_ruv(Repl_Agmt *ra, char *rid_text, Slapi_Task *task)
  2369. {
  2370. Repl_Connection *conn = NULL;
  2371. BerElement *ber = NULL;
  2372. struct berval **vals = NULL;
  2373. LDAPMessage *result = NULL, *entry = NULL;
  2374. LDAP *ld = NULL;
  2375. Slapi_DN *dn = agmt_get_replarea(ra);
  2376. char *attrs[2];
  2377. char *attr = NULL;
  2378. int rc = 0, i;
  2379. if((conn = conn_new(ra)) == NULL){
  2380. return -1;
  2381. }
  2382. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2383. attrs[0] = "nsds50ruv";
  2384. attrs[1] = NULL;
  2385. ld = conn_get_ldap(conn);
  2386. if(ld == NULL){
  2387. cleanruv_log(task, CLEANALLRUV_ID,"Failed to get LDAP handle from "
  2388. "the replication agmt (%s). Moving on to the next agmt.",agmt_get_long_name(ra));
  2389. rc = -1;
  2390. goto done;
  2391. }
  2392. rc = ldap_search_ext_s(ld, slapi_sdn_get_dn(dn), LDAP_SCOPE_SUBTREE,
  2393. "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))",
  2394. attrs, 0, NULL, NULL, NULL, 0, &result);
  2395. slapi_sdn_free(&dn);
  2396. if(rc != LDAP_SUCCESS){
  2397. cleanruv_log(task, CLEANALLRUV_ID,"Failed to contact "
  2398. "agmt (%s) error (%d), will retry later.", agmt_get_long_name(ra), rc);
  2399. rc = -1;
  2400. goto done;
  2401. }
  2402. entry = ldap_first_entry( ld, result );
  2403. if ( entry != NULL ) {
  2404. for ( attr = ldap_first_attribute( ld, entry, &ber ); attr != NULL; attr = ldap_next_attribute( ld, entry, ber ) ){
  2405. /* make sure the attribute is nsds50ruv */
  2406. if(strcasecmp(attr,"nsds50ruv") != 0){
  2407. ldap_memfree( attr );
  2408. continue;
  2409. }
  2410. if ((vals = ldap_get_values_len( ld, entry, attr)) != NULL ) {
  2411. for ( i = 0; vals[i] && vals[i]->bv_val; i++ ) {
  2412. /* look for this replica */
  2413. if(strstr(vals[i]->bv_val, rid_text)){
  2414. /* rid has not been cleaned yet, free and return */
  2415. rc = -1;
  2416. ldap_value_free_len(vals);
  2417. ldap_memfree( attr );
  2418. if ( ber != NULL ) {
  2419. ber_free( ber, 0 );
  2420. ber = NULL;
  2421. }
  2422. goto done;
  2423. } else {
  2424. rc = 0;
  2425. }
  2426. }
  2427. ldap_value_free_len(vals);
  2428. }
  2429. ldap_memfree( attr );
  2430. } /* for loop */
  2431. if ( ber != NULL ) {
  2432. ber_free( ber, 0 );
  2433. }
  2434. }
  2435. done:
  2436. if(result)
  2437. ldap_msgfree( result );
  2438. } else {
  2439. return -1;
  2440. }
  2441. conn_delete_internal_ext(conn);
  2442. return rc;
  2443. }
  2444. static int
  2445. get_cleanruv_task_count()
  2446. {
  2447. int i, count = 0;
  2448. slapi_rwlock_wrlock(rid_lock);
  2449. for(i = 0; i < CLEANRIDSIZ; i++){
  2450. if(cleaned_rids[i] != 0){
  2451. count++;
  2452. }
  2453. }
  2454. slapi_rwlock_unlock(rid_lock);
  2455. return count;
  2456. }
  2457. static int
  2458. get_abort_cleanruv_task_count()
  2459. {
  2460. int i, count = 0;
  2461. slapi_rwlock_wrlock(rid_lock);
  2462. for(i = 0; i < CLEANRIDSIZ; i++){
  2463. if(aborted_rids[i] != 0){
  2464. count++;
  2465. }
  2466. }
  2467. slapi_rwlock_unlock(rid_lock);
  2468. return count;
  2469. }
  2470. /*
  2471. * Notify sleeping CLEANALLRUV threads to stop
  2472. */
  2473. void
  2474. stop_ruv_cleaning()
  2475. {
  2476. if(notify_lock){
  2477. PR_Lock( notify_lock );
  2478. PR_NotifyCondVar( notify_cvar );
  2479. PR_Unlock( notify_lock );
  2480. }
  2481. }
  2482. /*
  2483. * Write our logging to the task and error log
  2484. */
  2485. void
  2486. cleanruv_log(Slapi_Task *task, char *task_type, char *fmt, ...)
  2487. {
  2488. va_list ap1;
  2489. va_list ap2;
  2490. va_list ap3;
  2491. va_list ap4;
  2492. char *errlog_fmt;
  2493. va_start(ap1, fmt);
  2494. va_start(ap2, fmt);
  2495. va_start(ap3, fmt);
  2496. va_start(ap4, fmt);
  2497. if(task){
  2498. slapi_task_log_notice_ext(task, fmt, ap1);
  2499. slapi_task_log_status_ext(task, fmt, ap2);
  2500. slapi_task_inc_progress(task);
  2501. }
  2502. errlog_fmt = PR_smprintf("%s: %s\n",task_type, fmt);
  2503. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  2504. slapi_ch_free_string(&errlog_fmt);
  2505. va_end(ap1);
  2506. va_end(ap2);
  2507. va_end(ap3);
  2508. va_end(ap4);
  2509. }