1
0

repl5_replica_config.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_replica_config.c - replica configuration over ldap */
  13. #include <ctype.h> /* for isdigit() */
  14. #include "repl.h" /* ONREPL - this is bad */
  15. #include "repl5.h"
  16. #include "cl5_api.h"
  17. #include "cl5.h"
  18. #include "slap.h"
  19. /* CONFIG_BASE: no need to optimize */
  20. #define CONFIG_BASE "cn=mapping tree,cn=config"
  21. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  22. #define TASK_ATTR "nsds5Task"
  23. #define CL2LDIF_TASK "CL2LDIF"
  24. #define LDIF2CL_TASK "LDIF2CL"
  25. #define CLEANRUV "CLEANRUV"
  26. #define CLEANRUVLEN 8
  27. #define CLEANALLRUV "CLEANALLRUV"
  28. #define CLEANALLRUVLEN 11
  29. #define REPLICA_RDN "cn=replica"
  30. #define CLEANALLRUV_ID "CleanAllRUV Task"
  31. #define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
  32. int slapi_log_urp = SLAPI_LOG_REPL;
  33. static ReplicaId cleaned_rids[CLEANRIDSIZ + 1] = {0};
  34. static ReplicaId pre_cleaned_rids[CLEANRIDSIZ + 1] = {0};
  35. static ReplicaId aborted_rids[CLEANRIDSIZ + 1] = {0};
  36. static Slapi_RWLock *rid_lock = NULL;
  37. static Slapi_RWLock *abort_rid_lock = NULL;
  38. static PRLock *notify_lock = NULL;
  39. static PRCondVar *notify_cvar = NULL;
  40. /* Forward Declartions */
  41. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  42. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  43. static int replica_config_post_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  44. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  45. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  46. static int replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  47. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  48. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  49. static int replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  50. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  51. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  52. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  53. static int replica_execute_ldif2cl_task (Object *r, char *returntext);
  54. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  55. static int replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char *force_cleaning, char *returntext);
  56. static void replica_cleanallruv_thread(void *arg);
  57. static void replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data);
  58. static int check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task);
  59. static int check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn);
  60. static int replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *data, int check_result);
  61. static int replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload);
  62. static int replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task);
  63. static int replica_cleanallruv_replica_alive(Repl_Agmt *agmt);
  64. static int replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *ra, char *rid_text, Slapi_Task *task, char *force);
  65. static int get_cleanruv_task_count();
  66. static int get_abort_cleanruv_task_count();
  67. static int replica_cleanup_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  68. static int replica_task_done(Replica *replica);
  69. static void delete_cleaned_rid_config(cleanruv_data *data);
  70. static int replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task);
  71. static void check_replicas_are_done_cleaning(cleanruv_data *data);
  72. static void check_replicas_are_done_aborting(cleanruv_data *data );
  73. static CSN* replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn);
  74. static int replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn);
  75. static void preset_cleaned_rid(ReplicaId rid);
  76. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  77. static void replica_cleanall_ruv_destructor(Slapi_Task *task);
  78. static void replica_cleanall_ruv_abort_destructor(Slapi_Task *task);
  79. /*
  80. * Note: internal add/modify/delete operations should not be run while
  81. * s_configLock is held. E.g., slapi_modify_internal_pb via replica_task_done
  82. * in replica_config_post_modify.
  83. */
  84. static PRLock *s_configLock;
  85. static int
  86. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  87. {
  88. *returncode = LDAP_UNWILLING_TO_PERFORM;
  89. return SLAPI_DSE_CALLBACK_ERROR;
  90. }
  91. int
  92. replica_config_init()
  93. {
  94. s_configLock = PR_NewLock ();
  95. if (s_configLock == NULL)
  96. {
  97. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  98. "failed to create configuration lock; NSPR error - %d\n",
  99. PR_GetError ());
  100. return -1;
  101. }
  102. rid_lock = slapi_new_rwlock();
  103. if(rid_lock == NULL)
  104. {
  105. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  106. "failed to create rid_lock; NSPR error - %d\n", PR_GetError ());
  107. return -1;
  108. }
  109. abort_rid_lock = slapi_new_rwlock();
  110. if(abort_rid_lock == NULL)
  111. {
  112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  113. "failed to create abort_rid_lock; NSPR error - %d\n", PR_GetError ());
  114. return -1;
  115. }
  116. if ( ( notify_lock = PR_NewLock()) == NULL ) {
  117. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  118. "failed to create notify lock; NSPR error - %d\n", PR_GetError ());
  119. return -1;
  120. }
  121. if ( ( notify_cvar = PR_NewCondVar( notify_lock )) == NULL ) {
  122. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  123. "failed to create notify cond var; NSPR error - %d\n", PR_GetError ());
  124. return -1;
  125. }
  126. /* config DSE must be initialized before we get here */
  127. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  128. CONFIG_FILTER, replica_config_add, NULL);
  129. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  130. CONFIG_FILTER, replica_config_modify,NULL);
  131. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  132. CONFIG_FILTER, dont_allow_that, NULL);
  133. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  134. CONFIG_FILTER, replica_config_delete,NULL);
  135. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  136. CONFIG_FILTER, replica_config_search,NULL);
  137. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  138. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  139. CONFIG_FILTER, replica_config_post_modify,
  140. NULL);
  141. /* register the CLEANALLRUV & ABORT task */
  142. slapi_task_register_handler("cleanallruv", replica_cleanall_ruv_task);
  143. slapi_task_register_handler("abort cleanallruv", replica_cleanall_ruv_abort);
  144. return 0;
  145. }
  146. void
  147. replica_config_destroy ()
  148. {
  149. if (s_configLock)
  150. {
  151. PR_DestroyLock (s_configLock);
  152. s_configLock = NULL;
  153. }
  154. /* config DSE must be initialized before we get here */
  155. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  156. CONFIG_FILTER, replica_config_add);
  157. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  158. CONFIG_FILTER, replica_config_modify);
  159. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  160. CONFIG_FILTER, dont_allow_that);
  161. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  162. CONFIG_FILTER, replica_config_delete);
  163. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  164. CONFIG_FILTER, replica_config_search);
  165. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  166. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  167. CONFIG_FILTER, replica_config_post_modify);
  168. }
  169. static int
  170. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  171. int *returncode, char *errorbuf, void *arg)
  172. {
  173. Replica *r = NULL;
  174. multimaster_mtnode_extension *mtnode_ext;
  175. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  176. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  177. char *errortext = errorbuf ? errorbuf : buf;
  178. if (errorbuf)
  179. {
  180. errorbuf[0] = '\0';
  181. }
  182. *returncode = LDAP_SUCCESS;
  183. PR_Lock (s_configLock);
  184. /* add the dn to the dn hash so we can tell this replica is being configured */
  185. replica_add_by_dn(replica_root);
  186. mtnode_ext = _replica_config_get_mtnode_ext (e);
  187. PR_ASSERT (mtnode_ext);
  188. if (mtnode_ext->replica)
  189. {
  190. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  191. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  192. *returncode = LDAP_UNWILLING_TO_PERFORM;
  193. goto done;
  194. }
  195. /* create replica object */
  196. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  197. if (r == NULL)
  198. {
  199. *returncode = LDAP_OPERATIONS_ERROR;
  200. goto done;
  201. }
  202. /* Set the mapping tree node state, and the referrals from the RUV */
  203. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  204. if (!replica_is_legacy_consumer (r))
  205. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  206. /* ONREPL if replica is added as writable we need to execute protocol that
  207. introduces new writable replica to the topology */
  208. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  209. /* add replica object to the hash */
  210. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  211. /* delete the dn from the dn hash - done with configuration */
  212. replica_delete_by_dn(replica_root);
  213. done:
  214. PR_Unlock (s_configLock);
  215. /* slapi_ch_free accepts NULL pointer */
  216. slapi_ch_free ((void**)&replica_root);
  217. if (*returncode != LDAP_SUCCESS)
  218. {
  219. if (mtnode_ext->replica)
  220. object_release (mtnode_ext->replica);
  221. return SLAPI_DSE_CALLBACK_ERROR;
  222. }
  223. else
  224. return SLAPI_DSE_CALLBACK_OK;
  225. }
  226. static int
  227. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  228. int *returncode, char *returntext, void *arg)
  229. {
  230. int rc= 0;
  231. LDAPMod **mods;
  232. int i, apply_mods;
  233. multimaster_mtnode_extension *mtnode_ext;
  234. Replica *r = NULL;
  235. char *replica_root = NULL;
  236. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  237. char *errortext = returntext ? returntext : buf;
  238. char *config_attr, *config_attr_value;
  239. Slapi_Operation *op;
  240. void *identity;
  241. if (returntext)
  242. {
  243. returntext[0] = '\0';
  244. }
  245. *returncode = LDAP_SUCCESS;
  246. /* just let internal operations originated from replication plugin to go through */
  247. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  248. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  249. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  250. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  251. {
  252. *returncode = LDAP_SUCCESS;
  253. return SLAPI_DSE_CALLBACK_OK;
  254. }
  255. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  256. PR_Lock (s_configLock);
  257. mtnode_ext = _replica_config_get_mtnode_ext (e);
  258. PR_ASSERT (mtnode_ext);
  259. if (mtnode_ext->replica)
  260. object_acquire (mtnode_ext->replica);
  261. if (mtnode_ext->replica == NULL)
  262. {
  263. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  264. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  265. errortext);
  266. *returncode = LDAP_OPERATIONS_ERROR;
  267. goto done;
  268. }
  269. r = object_get_data (mtnode_ext->replica);
  270. PR_ASSERT (r);
  271. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  272. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  273. {
  274. /* we only allow the replica ID and type to be modified together e.g.
  275. if converting a read only replica to a master or vice versa -
  276. we will need to change both the replica ID and the type at the same
  277. time - we must disallow changing the replica ID if the type is not
  278. being changed and vice versa
  279. */
  280. char *new_repl_id = NULL;
  281. char *new_repl_type = NULL;
  282. if (*returncode != LDAP_SUCCESS)
  283. break;
  284. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  285. {
  286. if (*returncode != LDAP_SUCCESS)
  287. break;
  288. config_attr = (char *) mods[i]->mod_type;
  289. PR_ASSERT (config_attr);
  290. /* disallow modifications or removal of replica root,
  291. replica name and replica state attributes */
  292. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  293. strcasecmp (config_attr, attr_replicaName) == 0 ||
  294. strcasecmp (config_attr, attr_state) == 0)
  295. {
  296. *returncode = LDAP_UNWILLING_TO_PERFORM;
  297. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  298. config_attr);
  299. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  300. errortext);
  301. }
  302. /* this is a request to delete an attribute */
  303. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) || mods[i]->mod_bvalues == NULL
  304. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  305. {
  306. /*
  307. * Where possible/allowed return replica config settings to their
  308. * default values.
  309. */
  310. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  311. {
  312. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  313. }
  314. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  315. {
  316. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  317. }
  318. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  319. {
  320. replica_set_groupdn_checkinterval (r, -1);
  321. }
  322. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  323. {
  324. if (apply_mods) {
  325. replica_set_referrals(r, NULL);
  326. if (!replica_is_legacy_consumer (r)) {
  327. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  328. }
  329. }
  330. }
  331. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  332. {
  333. if (apply_mods)
  334. replica_set_legacy_consumer (r, PR_FALSE);
  335. }
  336. else if (strcasecmp (config_attr, type_replicaCleanRUV) == 0 ||
  337. strcasecmp (config_attr, type_replicaAbortCleanRUV) == 0)
  338. {
  339. /*
  340. * Nothing to do in this case, allow it, and continue.
  341. */
  342. continue;
  343. }
  344. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 )
  345. {
  346. if (apply_mods)
  347. replica_set_protocol_timeout(r, DEFAULT_PROTOCOL_TIMEOUT);
  348. }
  349. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  350. {
  351. if (apply_mods)
  352. replica_set_backoff_min(r, PROTOCOL_BACKOFF_MINIMUM);
  353. }
  354. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  355. {
  356. if (apply_mods)
  357. replica_set_backoff_max(r, PROTOCOL_BACKOFF_MAXIMUM);
  358. }
  359. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  360. {
  361. if (apply_mods)
  362. replica_set_precise_purging(r, 0);
  363. }
  364. else if (strcasecmp (config_attr, type_replicaReleaseTimeout) == 0 )
  365. {
  366. if (apply_mods)
  367. replica_set_release_timeout(r, 0);
  368. }
  369. else
  370. {
  371. *returncode = LDAP_UNWILLING_TO_PERFORM;
  372. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  373. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  374. }
  375. }
  376. else /* modify an attribute */
  377. {
  378. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  379. if (NULL == config_attr_value) {
  380. *returncode = LDAP_UNWILLING_TO_PERFORM;
  381. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attribute %s value is NULL.\n",
  382. config_attr);
  383. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  384. errortext);
  385. break;
  386. }
  387. if (strcasecmp (config_attr, attr_replicaBindDn) == 0) {
  388. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  389. }
  390. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  391. {
  392. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  393. }
  394. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  395. {
  396. int interval = atoi(config_attr_value);
  397. replica_set_groupdn_checkinterval (r, interval);
  398. }
  399. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  400. {
  401. slapi_ch_free_string(&new_repl_type);
  402. new_repl_type = slapi_ch_strdup(config_attr_value);
  403. }
  404. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  405. {
  406. slapi_ch_free_string(&new_repl_id);
  407. new_repl_id = slapi_ch_strdup(config_attr_value);
  408. }
  409. else if (strcasecmp (config_attr, attr_flags) == 0)
  410. {
  411. *returncode = replica_config_change_flags (r, config_attr_value, errortext, apply_mods);
  412. }
  413. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  414. {
  415. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value, errortext, apply_mods);
  416. }
  417. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  418. {
  419. if (apply_mods)
  420. {
  421. Slapi_Mod smod;
  422. Slapi_ValueSet *vs= slapi_valueset_new();
  423. slapi_mod_init_byref(&smod,mods[i]);
  424. slapi_valueset_set_from_smod(vs, &smod);
  425. replica_set_referrals (r, vs);
  426. slapi_mod_done(&smod);
  427. slapi_valueset_free(vs);
  428. if (!replica_is_legacy_consumer (r)) {
  429. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  430. }
  431. }
  432. }
  433. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  434. {
  435. if (apply_mods && config_attr_value[0])
  436. {
  437. PRUint32 delay;
  438. if (isdigit (config_attr_value[0]))
  439. {
  440. delay = (unsigned int)atoi(config_attr_value);
  441. replica_set_purge_delay(r, delay);
  442. }
  443. else
  444. *returncode = LDAP_OPERATIONS_ERROR;
  445. }
  446. }
  447. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  448. {
  449. if (apply_mods && config_attr_value[0])
  450. {
  451. long interval;
  452. interval = atol (config_attr_value);
  453. replica_set_tombstone_reap_interval (r, interval);
  454. }
  455. }
  456. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  457. {
  458. if (apply_mods && config_attr_value[0])
  459. {
  460. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  461. (strcasecmp (config_attr_value, "true") == 0) ||
  462. (strcasecmp (config_attr_value, "yes") == 0) ||
  463. (strcasecmp (config_attr_value, "1") == 0);
  464. replica_set_legacy_consumer (r, legacy);
  465. }
  466. }
  467. /* ignore modifiers attributes added by the server */
  468. else if (slapi_attr_is_last_mod(config_attr))
  469. {
  470. *returncode = LDAP_SUCCESS;
  471. }
  472. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 ){
  473. if (apply_mods)
  474. {
  475. PRUint64 ptimeout = 0;
  476. ptimeout = atoll(config_attr_value);
  477. if(ptimeout <= 0){
  478. *returncode = LDAP_UNWILLING_TO_PERFORM;
  479. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  480. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  481. config_attr, config_attr_value);
  482. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  483. break;
  484. }
  485. replica_set_protocol_timeout(r, ptimeout);
  486. }
  487. }
  488. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  489. {
  490. if (apply_mods)
  491. {
  492. PRUint64 val = atoll(config_attr_value);
  493. if(val <= 0){
  494. *returncode = LDAP_UNWILLING_TO_PERFORM;
  495. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  496. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  497. config_attr, config_attr_value);
  498. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  499. break;
  500. }
  501. replica_set_backoff_min(r, val);
  502. }
  503. }
  504. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  505. {
  506. if (apply_mods)
  507. {
  508. PRUint64 val = atoll(config_attr_value);
  509. if(val <= 0){
  510. *returncode = LDAP_UNWILLING_TO_PERFORM;
  511. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  512. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  513. config_attr, config_attr_value);
  514. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  515. break;
  516. }
  517. replica_set_backoff_max(r, val);
  518. }
  519. }
  520. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  521. {
  522. if (apply_mods)
  523. {
  524. if (config_attr_value[0]) {
  525. PRUint64 on_off = 0;
  526. if (strcasecmp(config_attr_value, "on") == 0){
  527. on_off = 1;
  528. } else if (strcasecmp(config_attr_value, "off") == 0){
  529. on_off = 0;
  530. } else{
  531. /* Invalid value */
  532. *returncode = LDAP_UNWILLING_TO_PERFORM;
  533. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  534. "Invalid value for %s: %s Value should be \"on\" or \"off\"\n",
  535. type_replicaPrecisePurge, config_attr_value);
  536. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  537. "Invalid value for %s: %s Value should be \"on\" or \"off\")\n",
  538. type_replicaPrecisePurge, config_attr_value);
  539. break;
  540. }
  541. replica_set_precise_purging(r, on_off);
  542. } else {
  543. replica_set_precise_purging(r, 0);
  544. }
  545. }
  546. }
  547. else if (strcasecmp (config_attr, type_replicaReleaseTimeout) == 0 )
  548. {
  549. if (apply_mods)
  550. {
  551. long val = atol(config_attr_value);
  552. if (val < 0){
  553. *returncode = LDAP_UNWILLING_TO_PERFORM;
  554. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  555. "attribute %s value (%s) is invalid, must be a number zero or greater.\n",
  556. config_attr, config_attr_value);
  557. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  558. "replica_config_modify: %s\n", errortext);
  559. break;
  560. } else {
  561. /* Set the timeout */
  562. replica_set_release_timeout(r, val);
  563. }
  564. }
  565. }
  566. else
  567. {
  568. *returncode = LDAP_UNWILLING_TO_PERFORM;
  569. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  570. "modification of attribute %s is not allowed in replica entry", config_attr);
  571. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  572. }
  573. }
  574. }
  575. if (new_repl_id || new_repl_type)
  576. {
  577. *returncode = replica_config_change_type_and_id(r, new_repl_type, new_repl_id, errortext, apply_mods);
  578. PR_Unlock (s_configLock);
  579. replica_update_state(0, (void *)replica_get_name(r));
  580. PR_Lock (s_configLock);
  581. slapi_ch_free_string(&new_repl_id);
  582. slapi_ch_free_string(&new_repl_type);
  583. agmtlist_notify_all(pb);
  584. }
  585. }
  586. done:
  587. if (mtnode_ext->replica)
  588. object_release (mtnode_ext->replica);
  589. /* slapi_ch_free accepts NULL pointer */
  590. slapi_ch_free_string(&replica_root);
  591. PR_Unlock (s_configLock);
  592. if (*returncode != LDAP_SUCCESS)
  593. {
  594. return SLAPI_DSE_CALLBACK_ERROR;
  595. }
  596. else
  597. {
  598. return SLAPI_DSE_CALLBACK_OK;
  599. }
  600. }
  601. static int
  602. replica_config_post_modify(Slapi_PBlock *pb,
  603. Slapi_Entry* entryBefore,
  604. Slapi_Entry* e,
  605. int *returncode,
  606. char *returntext,
  607. void *arg)
  608. {
  609. int rc= 0;
  610. LDAPMod **mods;
  611. int i, apply_mods;
  612. multimaster_mtnode_extension *mtnode_ext;
  613. char *replica_root = NULL;
  614. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  615. char *errortext = returntext ? returntext : buf;
  616. char *config_attr, *config_attr_value;
  617. Slapi_Operation *op;
  618. void *identity;
  619. int flag_need_cleanup = 0;
  620. if (returntext)
  621. {
  622. returntext[0] = '\0';
  623. }
  624. *returncode = LDAP_SUCCESS;
  625. /* just let internal operations originated from replication plugin to go through */
  626. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  627. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  628. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  629. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  630. {
  631. *returncode = LDAP_SUCCESS;
  632. return SLAPI_DSE_CALLBACK_OK;
  633. }
  634. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  635. PR_Lock (s_configLock);
  636. mtnode_ext = _replica_config_get_mtnode_ext (e);
  637. PR_ASSERT (mtnode_ext);
  638. if (mtnode_ext->replica)
  639. object_acquire (mtnode_ext->replica);
  640. if (mtnode_ext->replica == NULL)
  641. {
  642. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  643. "replica does not exist for %s", replica_root);
  644. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  645. "replica_config_post_modify: %s\n",
  646. errortext);
  647. *returncode = LDAP_OPERATIONS_ERROR;
  648. goto done;
  649. }
  650. PR_ASSERT (object_get_data (mtnode_ext->replica) != NULL);
  651. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  652. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  653. {
  654. /* we only allow the replica ID and type to be modified together e.g.
  655. if converting a read only replica to a master or vice versa -
  656. we will need to change both the replica ID and the type at the same
  657. time - we must disallow changing the replica ID if the type is not
  658. being changed and vice versa
  659. */
  660. if (*returncode != LDAP_SUCCESS)
  661. break;
  662. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  663. {
  664. if (*returncode != LDAP_SUCCESS)
  665. break;
  666. config_attr = (char *) mods[i]->mod_type;
  667. PR_ASSERT (config_attr);
  668. /* disallow modifications or removal of replica root,
  669. replica name and replica state attributes */
  670. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  671. strcasecmp (config_attr, attr_replicaName) == 0 ||
  672. strcasecmp (config_attr, attr_state) == 0)
  673. {
  674. *returncode = LDAP_UNWILLING_TO_PERFORM;
  675. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  676. "modification of %s attribute is not allowed",
  677. config_attr);
  678. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  679. "replica_config_post_modify: %s\n",
  680. errortext);
  681. }
  682. /* this is a request to delete an attribute */
  683. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) ||
  684. mods[i]->mod_bvalues == NULL ||
  685. mods[i]->mod_bvalues[0]->bv_val == NULL)
  686. {
  687. ;
  688. }
  689. else /* modify an attribute */
  690. {
  691. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  692. if (strcasecmp (config_attr, TASK_ATTR) == 0)
  693. {
  694. flag_need_cleanup = 1;
  695. }
  696. }
  697. }
  698. }
  699. done:
  700. PR_Unlock (s_configLock);
  701. /* slapi_ch_free accepts NULL pointer */
  702. slapi_ch_free_string (&replica_root);
  703. /* Call replica_cleanup_task after s_configLock is reliesed */
  704. if (flag_need_cleanup)
  705. {
  706. *returncode = replica_cleanup_task(mtnode_ext->replica,
  707. config_attr_value,
  708. errortext, apply_mods);
  709. }
  710. if (mtnode_ext->replica)
  711. object_release (mtnode_ext->replica);
  712. if (*returncode != LDAP_SUCCESS)
  713. {
  714. return SLAPI_DSE_CALLBACK_ERROR;
  715. }
  716. else
  717. {
  718. return SLAPI_DSE_CALLBACK_OK;
  719. }
  720. }
  721. static int
  722. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  723. int *returncode, char *returntext, void *arg)
  724. {
  725. multimaster_mtnode_extension *mtnode_ext;
  726. Replica *r;
  727. PR_Lock (s_configLock);
  728. mtnode_ext = _replica_config_get_mtnode_ext (e);
  729. PR_ASSERT (mtnode_ext);
  730. if (mtnode_ext->replica)
  731. {
  732. /* remove object from the hash */
  733. r = (Replica*)object_get_data (mtnode_ext->replica);
  734. PR_ASSERT (r);
  735. /* The changelog for this replica is no longer valid, so we should remove it. */
  736. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  737. "Warning: The changelog for replica %s is no longer valid since "
  738. "the replica config is being deleted. Removing the changelog.\n",
  739. slapi_sdn_get_dn(replica_get_root(r)));
  740. cl5DeleteDBSync(mtnode_ext->replica);
  741. replica_delete_by_name (replica_get_name (r));
  742. object_release (mtnode_ext->replica);
  743. mtnode_ext->replica = NULL;
  744. }
  745. PR_Unlock (s_configLock);
  746. *returncode = LDAP_SUCCESS;
  747. return SLAPI_DSE_CALLBACK_OK;
  748. }
  749. static void
  750. replica_config_search_last_modified(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  751. {
  752. Object *ruv_obj = NULL;
  753. RUV *ruv = NULL;
  754. Slapi_Value **values;
  755. if (replica == NULL)
  756. return;
  757. ruv_obj = replica_get_ruv(replica);
  758. ruv = object_get_data(ruv_obj);
  759. if ((values = ruv_last_modified_to_valuearray(ruv)) != NULL) {
  760. slapi_entry_add_values_sv(e, type_ruvElementUpdatetime, values);
  761. valuearray_free(&values);
  762. }
  763. object_release(ruv_obj);
  764. }
  765. static void
  766. replica_config_search_ruv(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  767. {
  768. Object *ruv_obj = NULL;
  769. RUV *ruv = NULL;
  770. Slapi_Value **values;
  771. if (replica == NULL)
  772. return;
  773. ruv_obj = replica_get_ruv(replica);
  774. ruv = object_get_data(ruv_obj);
  775. if ((values = ruv_to_valuearray(ruv)) != NULL) {
  776. slapi_entry_add_values_sv(e, type_ruvElement, values);
  777. valuearray_free(&values);
  778. }
  779. object_release(ruv_obj);
  780. /* now add all the repl agmts maxcsnsruv */
  781. add_agmt_maxcsns(e, replica);
  782. }
  783. /* Returns PR_TRUE if 'attr' is present in the search requested attributes
  784. * else it returns PR_FALSE
  785. */
  786. static PRBool
  787. search_requested_attr(Slapi_PBlock *pb, const char *attr)
  788. {
  789. char **attrs = NULL;
  790. int i;
  791. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  792. if ((attr == NULL) || (attrs == NULL))
  793. return PR_FALSE;
  794. for (i = 0; attrs[i] != NULL; i++) {
  795. if (strcasecmp(attrs[i], attr) == 0) {
  796. return PR_TRUE;
  797. }
  798. }
  799. return PR_FALSE;
  800. }
  801. static int
  802. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  803. char *returntext, void *arg)
  804. {
  805. multimaster_mtnode_extension *mtnode_ext;
  806. int changeCount = 0;
  807. PRBool reapActive = PR_FALSE;
  808. char val [64];
  809. /* add attribute that contains number of entries in the changelog for this replica */
  810. PR_Lock (s_configLock);
  811. mtnode_ext = _replica_config_get_mtnode_ext (e);
  812. PR_ASSERT (mtnode_ext);
  813. if (mtnode_ext->replica) {
  814. Replica *replica;
  815. object_acquire (mtnode_ext->replica);
  816. if (cl5GetState () == CL5_STATE_OPEN) {
  817. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  818. }
  819. replica = (Replica*)object_get_data (mtnode_ext->replica);
  820. if (replica) {
  821. reapActive = replica_get_tombstone_reap_active(replica);
  822. }
  823. /* Check if the in memory ruv is requested */
  824. if (search_requested_attr(pb, type_ruvElement)) {
  825. replica_config_search_ruv(pb, e, replica);
  826. }
  827. /* Check if the last update time is requested */
  828. if (search_requested_attr(pb, type_ruvElementUpdatetime)) {
  829. replica_config_search_last_modified(pb, e, replica);
  830. }
  831. object_release (mtnode_ext->replica);
  832. }
  833. sprintf (val, "%d", changeCount);
  834. slapi_entry_add_string (e, type_replicaChangeCount, val);
  835. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  836. PR_Unlock (s_configLock);
  837. return SLAPI_DSE_CALLBACK_OK;
  838. }
  839. static int
  840. replica_config_change_type_and_id (Replica *r, const char *new_type,
  841. const char *new_id, char *returntext,
  842. int apply_mods)
  843. {
  844. int type;
  845. ReplicaType oldtype;
  846. ReplicaId rid;
  847. ReplicaId oldrid;
  848. PR_ASSERT (r);
  849. oldtype = replica_get_type(r);
  850. oldrid = replica_get_rid(r);
  851. if (new_type == NULL) /* by default - replica is read-only */
  852. {
  853. type = REPLICA_TYPE_READONLY;
  854. }
  855. else
  856. {
  857. type = atoi (new_type);
  858. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  859. {
  860. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  861. return LDAP_OPERATIONS_ERROR;
  862. }
  863. }
  864. /* disallow changing type to itself just to permit a replica ID change */
  865. if (oldtype == type)
  866. {
  867. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  868. return LDAP_OPERATIONS_ERROR;
  869. }
  870. if (type == REPLICA_TYPE_READONLY)
  871. {
  872. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  873. }
  874. else if (!new_id)
  875. {
  876. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  877. return LDAP_UNWILLING_TO_PERFORM;
  878. }
  879. else
  880. {
  881. int temprid = atoi (new_id);
  882. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  883. {
  884. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  885. "attribute %s must have a value greater than 0 "
  886. "and less than %d",
  887. attr_replicaId, READ_ONLY_REPLICA_ID);
  888. return LDAP_UNWILLING_TO_PERFORM;
  889. }
  890. else
  891. {
  892. rid = (ReplicaId)temprid;
  893. }
  894. }
  895. /* error if old rid == new rid */
  896. if (oldrid == rid)
  897. {
  898. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  899. return LDAP_OPERATIONS_ERROR;
  900. }
  901. if (apply_mods)
  902. {
  903. Object *ruv_obj, *gen_obj;
  904. RUV *ruv;
  905. CSNGen *gen;
  906. ruv_obj = replica_get_ruv(r);
  907. if(ruv_obj){
  908. /* we need to rewrite the repl_csngen with the new rid */
  909. ruv = object_get_data (ruv_obj);
  910. gen_obj = replica_get_csngen (r);
  911. if(gen_obj){
  912. const char *purl = multimaster_get_local_purl();
  913. gen = (CSNGen*) object_get_data (gen_obj);
  914. csngen_rewrite_rid(gen, rid);
  915. if(purl && type == REPLICA_TYPE_UPDATABLE){
  916. ruv_add_replica(ruv, rid, purl);
  917. ruv_move_local_supplier_to_first(ruv, rid);
  918. replica_reset_csn_pl(r);
  919. }
  920. ruv_delete_replica(ruv, oldrid);
  921. replica_set_ruv_dirty(r);
  922. cl5CleanRUV(oldrid);
  923. replica_set_csn_assigned(r);
  924. }
  925. object_release(ruv_obj);
  926. }
  927. replica_set_type (r, type);
  928. replica_set_rid(r, rid);
  929. /* Set the mapping tree node, and the list of referrals */
  930. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  931. if (!replica_is_legacy_consumer(r))
  932. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  933. }
  934. return LDAP_SUCCESS;
  935. }
  936. static int
  937. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  938. int apply_mods)
  939. {
  940. PR_ASSERT (r);
  941. if (apply_mods)
  942. {
  943. Slapi_Mod smod;
  944. Slapi_ValueSet *vs= slapi_valueset_new();
  945. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  946. slapi_valueset_set_from_smod(vs, &smod);
  947. replica_set_updatedn(r, vs, mod->mod_op);
  948. slapi_mod_done(&smod);
  949. slapi_valueset_free(vs);
  950. }
  951. return LDAP_SUCCESS;
  952. }
  953. static int
  954. replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext,
  955. int apply_mods)
  956. {
  957. PR_ASSERT (r);
  958. if (apply_mods)
  959. {
  960. Slapi_Mod smod;
  961. Slapi_ValueSet *vs= slapi_valueset_new();
  962. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  963. slapi_valueset_set_from_smod(vs, &smod);
  964. replica_set_groupdn(r, vs, mod->mod_op);
  965. slapi_mod_done(&smod);
  966. slapi_valueset_free(vs);
  967. }
  968. return LDAP_SUCCESS;
  969. }
  970. static int replica_config_change_flags (Replica *r, const char *new_flags,
  971. char *returntext, int apply_mods)
  972. {
  973. PR_ASSERT (r);
  974. if (apply_mods)
  975. {
  976. PRUint32 flags;
  977. flags = atol (new_flags);
  978. replica_replace_flags (r, flags);
  979. }
  980. return LDAP_SUCCESS;
  981. }
  982. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  983. int apply_mods)
  984. {
  985. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  986. {
  987. if (apply_mods)
  988. {
  989. return replica_execute_cl2ldif_task (r, returntext);
  990. }
  991. else
  992. return LDAP_SUCCESS;
  993. }
  994. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  995. {
  996. if (apply_mods)
  997. {
  998. return replica_execute_ldif2cl_task (r, returntext);
  999. }
  1000. else
  1001. return LDAP_SUCCESS;
  1002. }
  1003. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  1004. {
  1005. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  1006. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1007. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  1008. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1009. return LDAP_OPERATIONS_ERROR;
  1010. }
  1011. if (apply_mods)
  1012. {
  1013. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  1014. }
  1015. else
  1016. return LDAP_SUCCESS;
  1017. }
  1018. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  1019. {
  1020. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  1021. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1022. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  1023. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1024. return LDAP_OPERATIONS_ERROR;
  1025. }
  1026. if (apply_mods)
  1027. {
  1028. Slapi_Task *empty_task = NULL;
  1029. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, empty_task, returntext, "no");
  1030. }
  1031. else
  1032. return LDAP_SUCCESS;
  1033. }
  1034. else
  1035. {
  1036. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  1037. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1038. "replica_execute_task: %s\n", returntext);
  1039. return LDAP_OPERATIONS_ERROR;
  1040. }
  1041. }
  1042. static int
  1043. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  1044. int apply_mods)
  1045. {
  1046. int rc = LDAP_SUCCESS;
  1047. if (apply_mods) {
  1048. Replica *replica = (Replica*)object_get_data (r);
  1049. if (NULL == replica) {
  1050. rc = LDAP_OPERATIONS_ERROR;
  1051. } else {
  1052. rc = replica_task_done(replica);
  1053. }
  1054. }
  1055. return rc;
  1056. }
  1057. static int
  1058. replica_task_done(Replica *replica)
  1059. {
  1060. int rc = LDAP_OPERATIONS_ERROR;
  1061. char *replica_dn = NULL;
  1062. Slapi_DN *replica_sdn = NULL;
  1063. Slapi_PBlock *pb = NULL;
  1064. LDAPMod *mods [2];
  1065. LDAPMod mod;
  1066. if (NULL == replica) {
  1067. return rc;
  1068. }
  1069. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  1070. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  1071. REPLICA_RDN,
  1072. slapi_sdn_get_dn(replica_get_root(replica)),
  1073. CONFIG_BASE);
  1074. if (NULL == replica_dn) {
  1075. return rc;
  1076. }
  1077. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1078. pb = slapi_pblock_new();
  1079. mods[0] = &mod;
  1080. mods[1] = NULL;
  1081. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1082. mod.mod_type = (char *)TASK_ATTR;
  1083. mod.mod_bvalues = NULL;
  1084. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  1085. NULL/* uniqueid */,
  1086. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1087. 0/* flags */);
  1088. slapi_modify_internal_pb (pb);
  1089. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1090. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  1091. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1092. "replica_task_done: "
  1093. "failed to remove (%s) attribute from (%s) entry; "
  1094. "LDAP error - %d\n",
  1095. TASK_ATTR, replica_dn, rc);
  1096. }
  1097. slapi_pblock_destroy (pb);
  1098. slapi_sdn_free(&replica_sdn);
  1099. return rc;
  1100. }
  1101. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  1102. {
  1103. int rc;
  1104. Object *rlist [2];
  1105. Replica *replica;
  1106. char fName [MAXPATHLEN];
  1107. char *clDir = NULL;
  1108. if (cl5GetState () != CL5_STATE_OPEN)
  1109. {
  1110. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1111. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1112. "replica_execute_cl2ldif_task: %s\n", returntext);
  1113. rc = LDAP_OPERATIONS_ERROR;
  1114. goto bail;
  1115. }
  1116. rlist[0] = r;
  1117. rlist[1] = NULL;
  1118. /* file is stored in the changelog directory and is named
  1119. <replica name>.ldif */
  1120. clDir = cl5GetDir ();
  1121. if (NULL == clDir) {
  1122. rc = LDAP_OPERATIONS_ERROR;
  1123. goto bail;
  1124. }
  1125. replica = (Replica*)object_get_data (r);
  1126. if (NULL == replica) {
  1127. rc = LDAP_OPERATIONS_ERROR;
  1128. goto bail;
  1129. }
  1130. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1131. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1132. "Beginning changelog export of replica \"%s\"\n",
  1133. replica_get_name(replica));
  1134. rc = cl5ExportLDIF (fName, rlist);
  1135. if (rc == CL5_SUCCESS) {
  1136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1137. "Finished changelog export of replica \"%s\"\n",
  1138. replica_get_name(replica));
  1139. rc = LDAP_SUCCESS;
  1140. } else {
  1141. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1142. "Failed changelog export replica %s; "
  1143. "changelog error - %d", replica_get_name(replica), rc);
  1144. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1145. "replica_execute_cl2ldif_task: %s\n", returntext);
  1146. rc = LDAP_OPERATIONS_ERROR;
  1147. }
  1148. bail:
  1149. slapi_ch_free_string (&clDir);
  1150. return rc;
  1151. }
  1152. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  1153. {
  1154. int rc, imprc = 0;
  1155. Object *rlist [2];
  1156. Replica *replica;
  1157. char fName [MAXPATHLEN];
  1158. char *clDir = NULL;
  1159. changelog5Config config;
  1160. if (cl5GetState () != CL5_STATE_OPEN)
  1161. {
  1162. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1163. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1164. "replica_execute_ldif2cl_task: %s\n", returntext);
  1165. rc = LDAP_OPERATIONS_ERROR;
  1166. goto bail;
  1167. }
  1168. rlist[0] = r;
  1169. rlist[1] = NULL;
  1170. /* file is stored in the changelog directory and is named
  1171. <replica name>.ldif */
  1172. clDir = cl5GetDir ();
  1173. if (NULL == clDir) {
  1174. rc = LDAP_OPERATIONS_ERROR;
  1175. goto bail;
  1176. }
  1177. replica = (Replica*)object_get_data (r);
  1178. if (NULL == replica) {
  1179. rc = LDAP_OPERATIONS_ERROR;
  1180. goto bail;
  1181. }
  1182. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1183. rc = cl5Close();
  1184. if (rc != CL5_SUCCESS)
  1185. {
  1186. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1187. "failed to close changelog to import changelog data; "
  1188. "changelog error - %d", rc);
  1189. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1190. "replica_execute_ldif2cl_task: %s\n", returntext);
  1191. rc = LDAP_OPERATIONS_ERROR;
  1192. goto bail;
  1193. }
  1194. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1195. "Beginning changelog import of replica \"%s\"\n",
  1196. replica_get_name(replica));
  1197. imprc = cl5ImportLDIF (clDir, fName, rlist);
  1198. if (CL5_SUCCESS == imprc)
  1199. {
  1200. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1201. "Finished changelog import of replica \"%s\"\n",
  1202. replica_get_name(replica));
  1203. }
  1204. else
  1205. {
  1206. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1207. "Failed changelog import replica %s; "
  1208. "changelog error - %d", replica_get_name(replica), rc);
  1209. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1210. "replica_execute_ldif2cl_task: %s\n", returntext);
  1211. imprc = LDAP_OPERATIONS_ERROR;
  1212. }
  1213. changelog5_read_config (&config);
  1214. /* restart changelog */
  1215. rc = cl5Open (config.dir, &config.dbconfig);
  1216. if (CL5_SUCCESS == rc)
  1217. {
  1218. rc = LDAP_SUCCESS;
  1219. }
  1220. else
  1221. {
  1222. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1223. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  1224. config.dir?config.dir:"null config dir");
  1225. rc = LDAP_OPERATIONS_ERROR;
  1226. }
  1227. bail:
  1228. slapi_ch_free_string(&clDir);
  1229. changelog5_config_done(&config);
  1230. /* if cl5ImportLDIF returned an error, report it first. */
  1231. return imprc?imprc:rc;
  1232. }
  1233. static multimaster_mtnode_extension *
  1234. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  1235. {
  1236. const char *replica_root;
  1237. Slapi_DN *sdn = NULL;
  1238. mapping_tree_node *mtnode;
  1239. multimaster_mtnode_extension *ext = NULL;
  1240. /* retirve root of the tree for which replica is configured */
  1241. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1242. if (replica_root == NULL)
  1243. {
  1244. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1245. "configuration entry %s missing %s attribute\n",
  1246. slapi_entry_get_dn((Slapi_Entry *)e),
  1247. attr_replicaRoot);
  1248. return NULL;
  1249. }
  1250. sdn = slapi_sdn_new_dn_passin (replica_root);
  1251. /* locate mapping tree node for the specified subtree */
  1252. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1253. if (mtnode == NULL)
  1254. {
  1255. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1256. "failed to locate mapping tree node for dn %s\n",
  1257. slapi_sdn_get_dn(sdn));
  1258. }
  1259. else
  1260. {
  1261. /* check if replica object already exists for the specified subtree */
  1262. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1263. }
  1264. slapi_sdn_free (&sdn);
  1265. return ext;
  1266. }
  1267. int
  1268. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1269. {
  1270. return replica_execute_cleanruv_task(r, rid, NULL);
  1271. }
  1272. static int
  1273. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1274. {
  1275. Object *RUVObj;
  1276. RUV *local_ruv = NULL;
  1277. Replica *replica = (Replica*)object_get_data (r);
  1278. int rc = 0;
  1279. PR_ASSERT (replica);
  1280. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1281. RUVObj = replica_get_ruv(replica);
  1282. PR_ASSERT(RUVObj);
  1283. local_ruv = (RUV*)object_get_data (RUVObj);
  1284. /* Need to check that :
  1285. * - rid is not the local one
  1286. * - rid is not the last one
  1287. */
  1288. if ((replica_get_rid(replica) == rid) ||
  1289. (ruv_replica_count(local_ruv) <= 1)) {
  1290. return LDAP_UNWILLING_TO_PERFORM;
  1291. }
  1292. rc = ruv_delete_replica(local_ruv, rid);
  1293. replica_set_ruv_dirty(replica);
  1294. if (replica_write_ruv(replica)) {
  1295. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: could not write RUV\n");
  1296. }
  1297. object_release(RUVObj);
  1298. /* Update Mapping Tree to reflect RUV changes */
  1299. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1300. /*
  1301. * Clean the changelog RUV's
  1302. */
  1303. cl5CleanRUV(rid);
  1304. /*
  1305. * Now purge the changelog
  1306. */
  1307. trigger_cl_purging(rid);
  1308. if (rc != RUV_SUCCESS){
  1309. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1310. return LDAP_OPERATIONS_ERROR;
  1311. }
  1312. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1313. return LDAP_SUCCESS;
  1314. }
  1315. const char *
  1316. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1317. {
  1318. Slapi_Attr *attr;
  1319. Slapi_Value *val = NULL;
  1320. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1321. return default_val;
  1322. slapi_attr_first_value(attr, &val);
  1323. return slapi_value_get_string(val);
  1324. }
  1325. static int
  1326. replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1327. int *returncode, char *returntext, void *arg)
  1328. {
  1329. Slapi_Task *task = NULL;
  1330. const Slapi_DN *task_dn;
  1331. Slapi_DN *dn = NULL;
  1332. ReplicaId rid = -1;
  1333. Object *r;
  1334. const char *force_cleaning;
  1335. const char *base_dn;
  1336. const char *rid_str;
  1337. int rc = SLAPI_DSE_CALLBACK_OK;
  1338. /* allocate new task now */
  1339. task = slapi_new_task(slapi_entry_get_ndn(e));
  1340. task_dn = slapi_entry_get_sdn(e);
  1341. if(task == NULL){
  1342. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: Failed to create new task\n");
  1343. rc = SLAPI_DSE_CALLBACK_ERROR;
  1344. goto out;
  1345. }
  1346. /* register our destructor for waiting the task is done */
  1347. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_destructor);
  1348. /*
  1349. * Get our task settings
  1350. */
  1351. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1352. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-id attribute");
  1353. cleanruv_log(task, -1, CLEANALLRUV_ID, "%s", returntext);
  1354. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1355. rc = SLAPI_DSE_CALLBACK_ERROR;
  1356. goto out;
  1357. }
  1358. rid = atoi(rid_str);
  1359. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1360. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-base-dn attribute");
  1361. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1362. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1363. rc = SLAPI_DSE_CALLBACK_ERROR;
  1364. goto out;
  1365. }
  1366. if ((force_cleaning = fetch_attr(e, "replica-force-cleaning", 0)) != NULL){
  1367. if(strcasecmp(force_cleaning,"yes") != 0 && strcasecmp(force_cleaning,"no") != 0){
  1368. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for replica-force-cleaning "
  1369. "(%s). Value must be \"yes\" or \"no\" for task - (%s)",
  1370. force_cleaning, slapi_sdn_get_dn(task_dn));
  1371. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1372. *returncode = LDAP_OPERATIONS_ERROR;
  1373. rc = SLAPI_DSE_CALLBACK_ERROR;
  1374. goto out;
  1375. }
  1376. } else {
  1377. force_cleaning = "no";
  1378. }
  1379. /*
  1380. * Check the rid
  1381. */
  1382. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1383. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1384. rid, slapi_sdn_get_dn(task_dn));
  1385. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1386. *returncode = LDAP_OPERATIONS_ERROR;
  1387. rc = SLAPI_DSE_CALLBACK_ERROR;
  1388. goto out;
  1389. }
  1390. if(is_cleaned_rid(rid)){
  1391. /* we are already cleaning this rid */
  1392. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being cleaned", rid);
  1393. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1394. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1395. rc = SLAPI_DSE_CALLBACK_ERROR;
  1396. goto out;
  1397. }
  1398. /*
  1399. * Get the replica object
  1400. */
  1401. dn = slapi_sdn_new_dn_byval(base_dn);
  1402. if((r = replica_get_replica_from_dn(dn)) == NULL){
  1403. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Could not find replica from dn(%s)",slapi_sdn_get_dn(dn));
  1404. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1405. *returncode = LDAP_OPERATIONS_ERROR;
  1406. rc = SLAPI_DSE_CALLBACK_ERROR;
  1407. goto out;
  1408. }
  1409. /* clean the RUV's */
  1410. rc = replica_execute_cleanall_ruv_task (r, rid, task, force_cleaning, returntext);
  1411. out:
  1412. if(rc){
  1413. cleanruv_log(task, rid, CLEANALLRUV_ID, "Task failed...(%d)", rc);
  1414. slapi_task_finish(task, *returncode);
  1415. } else {
  1416. rc = SLAPI_DSE_CALLBACK_OK;
  1417. }
  1418. slapi_sdn_free(&dn);
  1419. return rc;
  1420. }
  1421. /*
  1422. * CLEANALLRUV task
  1423. *
  1424. * [1] Get the maxcsn from the RUV of the rid we want to clean
  1425. * [2] Create the payload for the "cleanallruv" extended ops
  1426. * [3] Create "monitor" thread to do the real work.
  1427. *
  1428. */
  1429. static int
  1430. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char* force_cleaning, char *returntext)
  1431. {
  1432. struct berval *payload = NULL;
  1433. Slapi_Task *pre_task = NULL; /* this is supposed to be null for logging */
  1434. cleanruv_data *data = NULL;
  1435. PRThread *thread = NULL;
  1436. CSN *maxcsn = NULL;
  1437. Replica *replica;
  1438. char csnstr[CSN_STRSIZE];
  1439. char *ridstr = NULL;
  1440. char *basedn = NULL;
  1441. int rc = 0;
  1442. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,"Initiating CleanAllRUV Task...");
  1443. if(get_cleanruv_task_count() >= CLEANRIDSIZ){
  1444. /* we are already running the maximum number of tasks */
  1445. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,
  1446. "Exceeded maximum number of active CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1447. return LDAP_UNWILLING_TO_PERFORM;
  1448. }
  1449. /*
  1450. * Grab the replica
  1451. */
  1452. if(r){
  1453. replica = (Replica*)object_get_data (r);
  1454. } else {
  1455. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Replica object is NULL, aborting task");
  1456. return -1;
  1457. }
  1458. /*
  1459. * Check if this is a consumer
  1460. */
  1461. if(replica_get_type(replica) == REPLICA_TYPE_READONLY){
  1462. /* this is a consumer, send error */
  1463. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d), task can not be run on a consumer",rid);
  1464. if(task){
  1465. rc = -1;
  1466. slapi_task_finish(task, rc);
  1467. }
  1468. return -1;
  1469. }
  1470. /*
  1471. * Grab the max csn of the deleted replica
  1472. */
  1473. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Retrieving maxcsn...");
  1474. basedn = (char *)slapi_sdn_get_dn(replica_get_root(replica));
  1475. maxcsn = replica_cleanallruv_find_maxcsn(replica, rid, basedn);
  1476. if(maxcsn == NULL || csn_get_replicaid(maxcsn) == 0){
  1477. /*
  1478. * This is for consistency with extop csn creation, where
  1479. * we want the csn string to be "0000000000000000000" not ""
  1480. */
  1481. csn_free(&maxcsn);
  1482. maxcsn = csn_new();
  1483. csn_init_by_string(maxcsn, "");
  1484. }
  1485. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1486. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Found maxcsn (%s)",csnstr);
  1487. /*
  1488. * Create payload
  1489. */
  1490. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, basedn, csnstr, force_cleaning);
  1491. payload = create_cleanruv_payload(ridstr);
  1492. slapi_ch_free_string(&ridstr);
  1493. if(payload == NULL){
  1494. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  1495. rc = -1;
  1496. goto fail;
  1497. }
  1498. /*
  1499. * Launch the cleanallruv thread. Once all the replicas are cleaned it will release the rid
  1500. */
  1501. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1502. if (data == NULL) {
  1503. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to allocate cleanruv_data. Aborting task.");
  1504. rc = -1;
  1505. goto fail;
  1506. }
  1507. data->repl_obj = r;
  1508. data->replica = replica;
  1509. data->rid = rid;
  1510. data->task = task;
  1511. data->payload = payload;
  1512. data->sdn = NULL;
  1513. data->maxcsn = maxcsn;
  1514. data->repl_root = slapi_ch_strdup(basedn);
  1515. data->force = slapi_ch_strdup(force_cleaning);
  1516. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread,
  1517. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1518. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1519. if (thread == NULL) {
  1520. rc = -1;
  1521. slapi_ch_free_string(&data->force);
  1522. slapi_ch_free_string(&data->repl_root);
  1523. goto fail;
  1524. } else {
  1525. goto done;
  1526. }
  1527. fail:
  1528. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d)", rid);
  1529. if(task){
  1530. slapi_task_finish(task, rc);
  1531. }
  1532. if(payload){
  1533. ber_bvfree(payload);
  1534. }
  1535. csn_free(&maxcsn);
  1536. if(task) /* only the task acquires the r obj */
  1537. object_release (r);
  1538. done:
  1539. return rc;
  1540. }
  1541. void
  1542. replica_cleanallruv_thread_ext(void *arg)
  1543. {
  1544. replica_cleanallruv_thread(arg);
  1545. }
  1546. /*
  1547. * CLEANALLRUV Thread
  1548. *
  1549. * [1] Wait for the maxcsn to be covered
  1550. * [2] Make sure all the replicas are alive
  1551. * [3] Set the cleaned rid
  1552. * [4] Send the cleanAllRUV extop to all the replicas
  1553. * [5] Manually send the CLEANRUV task to replicas that do not support CLEANALLRUV
  1554. * [6] Wait for all the replicas to be cleaned.
  1555. * [7] Trigger cl trimming, release the rid, and remove all the "cleanallruv" attributes
  1556. * from the config.
  1557. */
  1558. static void
  1559. replica_cleanallruv_thread(void *arg)
  1560. {
  1561. cleanruv_data *data = arg;
  1562. Object *agmt_obj = NULL;
  1563. Object *ruv_obj = NULL;
  1564. Repl_Agmt *agmt = NULL;
  1565. RUV *ruv = NULL;
  1566. char csnstr[CSN_STRSIZE];
  1567. char *returntext = NULL;
  1568. char *rid_text = NULL;
  1569. int agmt_not_notified = 1;
  1570. int found_dirty_rid = 1;
  1571. int interval = 10;
  1572. int free_obj = 0;
  1573. int aborted = 0;
  1574. int rc = 0;
  1575. if (!data) {
  1576. return; /* no data */
  1577. }
  1578. if (data->task) {
  1579. slapi_task_inc_refcount(data->task);
  1580. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name,
  1581. "replica_cleanallruv_thread --> refcount incremented (%d).\n",
  1582. data->task->task_refcount);
  1583. }
  1584. /*
  1585. * Initialize our settings
  1586. */
  1587. if(data->replica == NULL && data->repl_obj == NULL){
  1588. /*
  1589. * This thread was initiated at startup because the process did not finish. Due
  1590. * to startup timing issues, we need to wait before grabbing the replica obj, as
  1591. * the backends might not be online yet.
  1592. */
  1593. PR_Lock( notify_lock );
  1594. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(10) );
  1595. PR_Unlock( notify_lock );
  1596. data->repl_obj = replica_get_replica_from_dn(data->sdn);
  1597. if(data->repl_obj == NULL){
  1598. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to retrieve repl object from dn(%s).", data->sdn);
  1599. aborted = 1;
  1600. goto done;
  1601. }
  1602. data->replica = (Replica*)object_get_data(data->repl_obj);
  1603. free_obj = 1;
  1604. } else if(data->replica == NULL && data->repl_obj){
  1605. data->replica = (Replica*)object_get_data(data->repl_obj);
  1606. } else if( data->repl_obj == NULL && data->replica){
  1607. data->repl_obj = object_new(data->replica, NULL);
  1608. free_obj = 1;
  1609. }
  1610. /* verify we have set our repl objects */
  1611. if(data->repl_obj == NULL || data->replica == NULL){
  1612. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to set the replica objects.");
  1613. aborted = 1;
  1614. goto done;
  1615. }
  1616. if(data->repl_root == NULL){
  1617. /* we must have resumed from start up, fill in the repl root */
  1618. data->repl_root = slapi_ch_strdup(slapi_sdn_get_dn(replica_get_root(data->replica)));
  1619. }
  1620. if(data->task){
  1621. slapi_task_begin(data->task, 1);
  1622. }
  1623. /*
  1624. * Presetting the rid prevents duplicate thread creation, but allows the db and changelog to still
  1625. * process updates from the rid. set_cleaned_rid() blocks updates, so we don't want to do that... yet.
  1626. */
  1627. preset_cleaned_rid(data->rid);
  1628. rid_text = slapi_ch_smprintf("%d", data->rid);
  1629. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1630. /*
  1631. * Add the cleanallruv task to the repl config - so we can handle restarts
  1632. */
  1633. add_cleaned_rid(data->rid, data->replica, csnstr, data->force); /* marks config that we started cleaning a rid */
  1634. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaning rid (%d)...", data->rid);
  1635. /*
  1636. * First, wait for the maxcsn to be covered
  1637. */
  1638. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting to process all the updates from the deleted replica...");
  1639. ruv_obj = replica_get_ruv(data->replica);
  1640. ruv = object_get_data (ruv_obj);
  1641. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1642. if(csn_get_replicaid(data->maxcsn) == 0 ||
  1643. ruv_covers_csn_cleanallruv(ruv,data->maxcsn) ||
  1644. strcasecmp(data->force,"yes") == 0)
  1645. {
  1646. /* We are caught up, now we can clean the ruv's */
  1647. break;
  1648. }
  1649. PR_Lock( notify_lock );
  1650. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(5) );
  1651. PR_Unlock( notify_lock );
  1652. }
  1653. object_release(ruv_obj);
  1654. /*
  1655. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1656. *
  1657. * Even if we are forcing the cleaning, the replicas still need to be up
  1658. */
  1659. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1660. if(strcasecmp(data->force, "no") == 0 && check_agmts_are_alive(data->replica, data->rid, data->task)){
  1661. /* error, aborted or shutdown */
  1662. aborted = 1;
  1663. goto done;
  1664. }
  1665. /*
  1666. * Make sure all the replicas have seen the max csn
  1667. */
  1668. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1669. if(strcasecmp(data->force,"no") == 0 && check_agmts_are_caught_up(data, csnstr)){
  1670. /* error, aborted or shutdown */
  1671. aborted = 1;
  1672. goto done;
  1673. }
  1674. /*
  1675. * Set the rid as notified - this blocks the changelog from sending out updates
  1676. * during this process, as well as prevents the db ruv from getting polluted.
  1677. */
  1678. set_cleaned_rid(data->rid);
  1679. /*
  1680. * Now send the cleanruv extended op to all the agreements
  1681. */
  1682. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1683. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1684. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1685. if(agmt_obj == NULL){
  1686. /* no agmts, just clean this replica */
  1687. break;
  1688. }
  1689. while (agmt_obj && !slapi_is_shutting_down()){
  1690. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1691. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1692. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1693. agmt_not_notified = 0;
  1694. continue;
  1695. }
  1696. if(replica_cleanallruv_send_extop(agmt, data, 1) == 0){
  1697. agmt_not_notified = 0;
  1698. } else {
  1699. agmt_not_notified = 1;
  1700. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Failed to send task to replica (%s)",agmt_get_long_name(agmt));
  1701. if(strcasecmp(data->force,"no") == 0){
  1702. break;
  1703. }
  1704. }
  1705. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1706. }
  1707. if(is_task_aborted(data->rid)){
  1708. aborted = 1;
  1709. goto done;
  1710. }
  1711. if(agmt_not_notified == 0 || strcasecmp(data->force, "yes") == 0){
  1712. break;
  1713. }
  1714. /*
  1715. * need to sleep between passes
  1716. */
  1717. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas have received the "
  1718. "cleanallruv extended op, retrying in %d seconds",interval);
  1719. if(!slapi_is_shutting_down()){
  1720. PR_Lock( notify_lock );
  1721. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1722. PR_Unlock( notify_lock );
  1723. }
  1724. if(interval < 14400){ /* 4 hour max */
  1725. interval = interval * 2;
  1726. } else {
  1727. interval = 14400;
  1728. }
  1729. }
  1730. /*
  1731. * Run the CLEANRUV task
  1732. */
  1733. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1734. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1735. /*
  1736. * Wait for all the replicas to be cleaned
  1737. */
  1738. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1739. "Waiting for all the replicas to be cleaned...");
  1740. interval = 10;
  1741. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1742. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1743. if(agmt_obj == NULL){
  1744. break;
  1745. }
  1746. while (agmt_obj && !slapi_is_shutting_down()){
  1747. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1748. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1749. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1750. found_dirty_rid = 0;
  1751. continue;
  1752. }
  1753. if(replica_cleanallruv_check_ruv(data->repl_root, agmt, rid_text, data->task, data->force) == 0){
  1754. found_dirty_rid = 0;
  1755. } else {
  1756. found_dirty_rid = 1;
  1757. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Replica is not cleaned yet (%s)",
  1758. agmt_get_long_name(agmt));
  1759. break;
  1760. }
  1761. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1762. }
  1763. /* If the task is abort or everyone is cleaned, break out */
  1764. if(is_task_aborted(data->rid)){
  1765. aborted = 1;
  1766. goto done;
  1767. }
  1768. if(found_dirty_rid == 0 || strcasecmp(data->force, "yes") == 0){
  1769. break;
  1770. }
  1771. /*
  1772. * Need to sleep between passes unless we are shutting down
  1773. */
  1774. if (!slapi_is_shutting_down()){
  1775. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1776. "retrying in %d seconds", interval);
  1777. PR_Lock( notify_lock );
  1778. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1779. PR_Unlock( notify_lock );
  1780. }
  1781. if(interval < 14400){ /* 4 hour max */
  1782. interval = interval * 2;
  1783. } else {
  1784. interval = 14400;
  1785. }
  1786. } /* while */
  1787. done:
  1788. /*
  1789. * If the replicas are cleaned, release the rid
  1790. */
  1791. if(!aborted && !slapi_is_shutting_down()){
  1792. delete_cleaned_rid_config(data);
  1793. /* make sure all the replicas have been "pre_cleaned" before finishing */
  1794. check_replicas_are_done_cleaning(data);
  1795. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1796. remove_cleaned_rid(data->rid);
  1797. } else {
  1798. /*
  1799. * Shutdown or abort
  1800. */
  1801. if(!is_task_aborted(data->rid) || slapi_is_shutting_down()){
  1802. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1803. } else {
  1804. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1805. delete_cleaned_rid_config(data);
  1806. remove_cleaned_rid(data->rid);
  1807. }
  1808. }
  1809. if(data->task){
  1810. slapi_task_finish(data->task, rc);
  1811. slapi_task_dec_refcount(data->task);
  1812. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread <-- refcount decremented.\n");
  1813. }
  1814. if(data->payload){
  1815. ber_bvfree(data->payload);
  1816. }
  1817. if(data->repl_obj && free_obj){
  1818. object_release(data->repl_obj);
  1819. }
  1820. csn_free(&data->maxcsn);
  1821. slapi_sdn_free(&data->sdn);
  1822. slapi_ch_free_string(&data->repl_root);
  1823. slapi_ch_free_string(&data->force);
  1824. slapi_ch_free_string(&rid_text);
  1825. slapi_ch_free((void **)&data);
  1826. }
  1827. static void
  1828. replica_cleanall_ruv_destructor(Slapi_Task *task)
  1829. {
  1830. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1831. "replica_cleanall_ruv_destructor -->\n" );
  1832. stop_ruv_cleaning();
  1833. if (task) {
  1834. while (slapi_task_get_refcount(task) > 0) {
  1835. /* Yield to wait for the fixup task finishes. */
  1836. DS_Sleep (PR_MillisecondsToInterval(100));
  1837. }
  1838. }
  1839. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1840. "replica_cleanall_ruv_destructor <--\n" );
  1841. }
  1842. static void
  1843. replica_cleanall_ruv_abort_destructor(Slapi_Task *task)
  1844. {
  1845. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1846. "replica_cleanall_ruv_abort_destructor -->\n" );
  1847. stop_ruv_cleaning();
  1848. if (task) {
  1849. while (slapi_task_get_refcount(task) > 0) {
  1850. /* Yield to wait for the fixup task finishes. */
  1851. DS_Sleep (PR_MillisecondsToInterval(100));
  1852. }
  1853. }
  1854. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1855. "replica_cleanall_ruv_abort_destructor <--\n" );
  1856. }
  1857. /*
  1858. * Loop over the agmts, and check if they are in the last phase of cleaning, meaning they have
  1859. * released cleanallruv data from the config
  1860. */
  1861. static void
  1862. check_replicas_are_done_cleaning(cleanruv_data *data )
  1863. {
  1864. Object *agmt_obj;
  1865. Repl_Agmt *agmt;
  1866. char csnstr[CSN_STRSIZE];
  1867. char *filter = NULL;
  1868. int not_all_cleaned = 1;
  1869. int interval = 10;
  1870. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1871. "Waiting for all the replicas to finish cleaning...");
  1872. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1873. filter = PR_smprintf("(%s=%d:%s:%s)", type_replicaCleanRUV,(int)data->rid, csnstr, data->force);
  1874. while(not_all_cleaned && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1875. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1876. if(agmt_obj == NULL){
  1877. not_all_cleaned = 0;
  1878. break;
  1879. }
  1880. while (agmt_obj && !slapi_is_shutting_down()){
  1881. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1882. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1883. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1884. not_all_cleaned = 0;
  1885. continue;
  1886. }
  1887. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1888. not_all_cleaned = 0;
  1889. } else {
  1890. not_all_cleaned = 1;
  1891. break;
  1892. }
  1893. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1894. }
  1895. if(not_all_cleaned == 0 ||
  1896. is_task_aborted(data->rid) ||
  1897. strcasecmp(data->force, "yes") == 0)
  1898. {
  1899. break;
  1900. }
  1901. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1902. "Not all replicas finished cleaning, retrying in %d seconds",
  1903. interval);
  1904. if(!slapi_is_shutting_down()){
  1905. PR_Lock( notify_lock );
  1906. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1907. PR_Unlock( notify_lock );
  1908. }
  1909. if(interval < 14400){ /* 4 hour max */
  1910. interval = interval * 2;
  1911. } else {
  1912. interval = 14400;
  1913. }
  1914. }
  1915. slapi_ch_free_string(&filter);
  1916. }
  1917. /*
  1918. * Search this replica for the nsds5ReplicaCleanruv attribute, we don't return
  1919. * an entry, we know it has finished cleaning.
  1920. */
  1921. static int
  1922. replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task)
  1923. {
  1924. Repl_Connection *conn = NULL;
  1925. ConnResult crc = 0;
  1926. struct berval *payload = NULL;
  1927. int msgid = 0;
  1928. int rc = -1;
  1929. if((conn = conn_new(agmt)) == NULL){
  1930. return rc;
  1931. }
  1932. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  1933. payload = create_cleanruv_payload(filter);
  1934. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_CHECK_STATUS_OID, payload, NULL, &msgid);
  1935. if(crc == CONN_OPERATION_SUCCESS){
  1936. struct berval *retsdata = NULL;
  1937. char *retoid = NULL;
  1938. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  1939. if (CONN_OPERATION_SUCCESS == crc ){
  1940. char *response = NULL;
  1941. decode_cleanruv_payload(retsdata, &response);
  1942. if(response == NULL){
  1943. /* this replica does not support cleanallruv */
  1944. rc = 0;
  1945. } else if(strcmp(response,CLEANRUV_FINISHED) == 0){
  1946. /* finished cleaning */
  1947. rc = 0;
  1948. }
  1949. if (NULL != retsdata)
  1950. ber_bvfree(retsdata);
  1951. slapi_ch_free_string(&response);
  1952. slapi_ch_free_string(&retoid);
  1953. }
  1954. }
  1955. }
  1956. conn_delete_internal_ext(conn);
  1957. if(payload)
  1958. ber_bvfree(payload);
  1959. return rc;
  1960. }
  1961. /*
  1962. * Loop over the agmts, and check if they are in the last phase of aborting, meaning they have
  1963. * released the abort cleanallruv data from the config
  1964. */
  1965. static void
  1966. check_replicas_are_done_aborting(cleanruv_data *data )
  1967. {
  1968. Object *agmt_obj;
  1969. Repl_Agmt *agmt;
  1970. char *filter = NULL;
  1971. int not_all_aborted = 1;
  1972. int interval = 10;
  1973. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Waiting for all the replicas to finish aborting...");
  1974. filter = PR_smprintf("(%s=%d:%s)", type_replicaAbortCleanRUV, data->rid, data->repl_root);
  1975. while(not_all_aborted && !slapi_is_shutting_down()){
  1976. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1977. if(agmt_obj == NULL){
  1978. not_all_aborted = 0;
  1979. break;
  1980. }
  1981. while (agmt_obj && !slapi_is_shutting_down()){
  1982. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1983. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1984. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1985. not_all_aborted = 0;
  1986. continue;
  1987. }
  1988. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1989. not_all_aborted = 0;
  1990. } else {
  1991. not_all_aborted = 1;
  1992. break;
  1993. }
  1994. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1995. }
  1996. if(not_all_aborted == 0){
  1997. break;
  1998. }
  1999. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Not all replicas finished aborting, retrying in %d seconds",interval);
  2000. if(!slapi_is_shutting_down()){
  2001. PR_Lock( notify_lock );
  2002. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2003. PR_Unlock( notify_lock );
  2004. }
  2005. if(interval < 14400){ /* 4 hour max */
  2006. interval = interval * 2;
  2007. } else {
  2008. interval = 14400;
  2009. }
  2010. }
  2011. slapi_ch_free_string(&filter);
  2012. }
  2013. /*
  2014. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  2015. */
  2016. static int
  2017. check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn)
  2018. {
  2019. Object *agmt_obj;
  2020. Repl_Agmt *agmt;
  2021. char *rid_text;
  2022. int not_all_caughtup = 1;
  2023. int interval = 10;
  2024. rid_text = slapi_ch_smprintf("%d", data->rid);
  2025. while(not_all_caughtup && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  2026. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2027. if(agmt_obj == NULL){
  2028. not_all_caughtup = 0;
  2029. break;
  2030. }
  2031. while (agmt_obj && !slapi_is_shutting_down()){
  2032. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2033. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2034. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2035. not_all_caughtup = 0;
  2036. continue;
  2037. }
  2038. if(replica_cleanallruv_check_maxcsn(agmt, data->repl_root, rid_text, maxcsn, data->task) == 0){
  2039. not_all_caughtup = 0;
  2040. } else {
  2041. not_all_caughtup = 1;
  2042. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replica not caught up (%s)",agmt_get_long_name(agmt));
  2043. break;
  2044. }
  2045. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2046. }
  2047. if(not_all_caughtup == 0 || is_task_aborted(data->rid) ){
  2048. break;
  2049. }
  2050. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  2051. "Not all replicas caught up, retrying in %d seconds",interval);
  2052. if(!slapi_is_shutting_down()){
  2053. PR_Lock( notify_lock );
  2054. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2055. PR_Unlock( notify_lock );
  2056. }
  2057. if(interval < 14400){ /* 4 hour max */
  2058. interval = interval * 2;
  2059. } else {
  2060. interval = 14400;
  2061. }
  2062. }
  2063. slapi_ch_free_string(&rid_text);
  2064. if(is_task_aborted(data->rid)){
  2065. return -1;
  2066. }
  2067. return not_all_caughtup;
  2068. }
  2069. /*
  2070. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  2071. */
  2072. static int
  2073. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  2074. {
  2075. Object *agmt_obj;
  2076. Repl_Agmt *agmt;
  2077. int not_all_alive = 1;
  2078. int interval = 10;
  2079. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  2080. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2081. if(agmt_obj == NULL){
  2082. not_all_alive = 0;
  2083. break;
  2084. }
  2085. while (agmt_obj && !slapi_is_shutting_down()){
  2086. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2087. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2088. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2089. not_all_alive = 0;
  2090. continue;
  2091. }
  2092. if(replica_cleanallruv_replica_alive(agmt) == 0){
  2093. not_all_alive = 0;
  2094. } else {
  2095. not_all_alive = 1;
  2096. cleanruv_log(task, rid, CLEANALLRUV_ID, "Replica not online (%s)",
  2097. agmt_get_long_name(agmt));
  2098. break;
  2099. }
  2100. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2101. }
  2102. if(not_all_alive == 0 || is_task_aborted(rid)){
  2103. break;
  2104. }
  2105. cleanruv_log(task, rid, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",
  2106. interval);
  2107. if(!slapi_is_shutting_down()){
  2108. PR_Lock( notify_lock );
  2109. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2110. PR_Unlock( notify_lock );
  2111. }
  2112. if(interval < 14400){ /* 4 hour max */
  2113. interval = interval * 2;
  2114. } else {
  2115. interval = 14400;
  2116. }
  2117. }
  2118. if(is_task_aborted(rid)){
  2119. return -1;
  2120. }
  2121. return not_all_alive;
  2122. }
  2123. /*
  2124. * Create the CLEANALLRUV extended op payload
  2125. */
  2126. struct berval *
  2127. create_cleanruv_payload(char *value)
  2128. {
  2129. struct berval *req_data = NULL;
  2130. BerElement *tmp_bere = NULL;
  2131. if ((tmp_bere = der_alloc()) == NULL){
  2132. goto error;
  2133. }
  2134. if (ber_printf(tmp_bere, "{s}", value) == -1){
  2135. goto error;
  2136. }
  2137. if (ber_flatten(tmp_bere, &req_data) == -1){
  2138. goto error;
  2139. }
  2140. goto done;
  2141. error:
  2142. if (NULL != req_data){
  2143. ber_bvfree(req_data);
  2144. req_data = NULL;
  2145. }
  2146. done:
  2147. if (NULL != tmp_bere){
  2148. ber_free(tmp_bere, 1);
  2149. tmp_bere = NULL;
  2150. }
  2151. return req_data;
  2152. }
  2153. /*
  2154. * Manually add the CLEANRUV task to replicas that do not support
  2155. * the CLEANALLRUV task.
  2156. */
  2157. static void
  2158. replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
  2159. {
  2160. Repl_Connection *conn;
  2161. ConnResult crc = 0;
  2162. LDAP *ld;
  2163. Slapi_DN *sdn;
  2164. struct berval *vals[2];
  2165. struct berval val;
  2166. LDAPMod *mods[2];
  2167. LDAPMod mod;
  2168. char *repl_dn = NULL;
  2169. char data[15];
  2170. int rc;
  2171. if((conn = conn_new(agmt)) == NULL){
  2172. return;
  2173. }
  2174. crc = conn_connect(conn);
  2175. if (CONN_OPERATION_SUCCESS != crc){
  2176. conn_delete_internal_ext(conn);
  2177. return;
  2178. }
  2179. ld = conn_get_ldap(conn);
  2180. if(ld == NULL){
  2181. conn_delete_internal_ext(conn);
  2182. return;
  2183. }
  2184. sdn = agmt_get_replarea(agmt);
  2185. if (!sdn) {
  2186. conn_delete_internal_ext(conn);
  2187. return;
  2188. }
  2189. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2190. mod.mod_type = "nsds5task";
  2191. mod.mod_bvalues = vals;
  2192. vals [0] = &val;
  2193. vals [1] = NULL;
  2194. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", clean_data->rid);
  2195. val.bv_val = data;
  2196. mods[0] = &mod;
  2197. mods[1] = NULL;
  2198. repl_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(sdn));
  2199. /*
  2200. * Add task to remote replica
  2201. */
  2202. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  2203. if(rc != LDAP_SUCCESS){
  2204. char *hostname = agmt_get_hostname(agmt);
  2205. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2206. "Failed to add CLEANRUV task (%s) to replica (%s). You will need "
  2207. "to manually run the CLEANRUV task on this replica (%s) error (%d)",
  2208. repl_dn, agmt_get_long_name(agmt), hostname, rc);
  2209. slapi_ch_free_string(&hostname);
  2210. }
  2211. slapi_ch_free_string(&repl_dn);
  2212. slapi_sdn_free(&sdn);
  2213. conn_delete_internal_ext(conn);
  2214. }
  2215. /*
  2216. * Check if the rid is in our list of "cleaned" rids
  2217. */
  2218. int
  2219. is_cleaned_rid(ReplicaId rid)
  2220. {
  2221. int i;
  2222. slapi_rwlock_rdlock(rid_lock);
  2223. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  2224. if(rid == cleaned_rids[i]){
  2225. slapi_rwlock_unlock(rid_lock);
  2226. return 1;
  2227. }
  2228. }
  2229. slapi_rwlock_unlock(rid_lock);
  2230. return 0;
  2231. }
  2232. int
  2233. is_pre_cleaned_rid(ReplicaId rid)
  2234. {
  2235. int i;
  2236. slapi_rwlock_rdlock(rid_lock);
  2237. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++){
  2238. if(rid == pre_cleaned_rids[i]){
  2239. slapi_rwlock_unlock(rid_lock);
  2240. return 1;
  2241. }
  2242. }
  2243. slapi_rwlock_unlock(rid_lock);
  2244. return 0;
  2245. }
  2246. int
  2247. is_task_aborted(ReplicaId rid)
  2248. {
  2249. int i;
  2250. if(rid == 0){
  2251. return 0;
  2252. }
  2253. slapi_rwlock_rdlock(abort_rid_lock);
  2254. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  2255. if(rid == aborted_rids[i]){
  2256. slapi_rwlock_unlock(abort_rid_lock);
  2257. return 1;
  2258. }
  2259. }
  2260. slapi_rwlock_unlock(abort_rid_lock);
  2261. return 0;
  2262. }
  2263. static void
  2264. preset_cleaned_rid(ReplicaId rid)
  2265. {
  2266. int i;
  2267. slapi_rwlock_wrlock(rid_lock);
  2268. for(i = 0; i < CLEANRIDSIZ; i++){
  2269. if(pre_cleaned_rids[i] == 0){
  2270. pre_cleaned_rids[i] = rid;
  2271. pre_cleaned_rids[i + 1] = 0;
  2272. break;
  2273. }
  2274. }
  2275. slapi_rwlock_unlock(rid_lock);
  2276. }
  2277. /*
  2278. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  2279. * This prevent the changelog from sending updates from this rid, and the local ruv
  2280. * will not be updated either.
  2281. */
  2282. void
  2283. set_cleaned_rid(ReplicaId rid)
  2284. {
  2285. int i;
  2286. slapi_rwlock_wrlock(rid_lock);
  2287. for(i = 0; i < CLEANRIDSIZ; i++){
  2288. if(cleaned_rids[i] == 0){
  2289. cleaned_rids[i] = rid;
  2290. cleaned_rids[i + 1] = 0;
  2291. }
  2292. }
  2293. slapi_rwlock_unlock(rid_lock);
  2294. }
  2295. /*
  2296. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  2297. */
  2298. void
  2299. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn, char *forcing)
  2300. {
  2301. Slapi_PBlock *pb;
  2302. struct berval *vals[2];
  2303. struct berval val;
  2304. LDAPMod *mods[2];
  2305. LDAPMod mod;
  2306. char data[CSN_STRSIZE + 10];
  2307. char *dn;
  2308. int rc;
  2309. if(r == NULL || maxcsn == NULL){
  2310. return;
  2311. }
  2312. /*
  2313. * Write the rid & maxcsn to the config entry
  2314. */
  2315. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s:%s", rid, maxcsn, forcing);
  2316. dn = replica_get_dn(r);
  2317. pb = slapi_pblock_new();
  2318. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2319. mod.mod_type = (char *)type_replicaCleanRUV;
  2320. mod.mod_bvalues = vals;
  2321. vals [0] = &val;
  2322. vals [1] = NULL;
  2323. val.bv_val = data;
  2324. mods[0] = &mod;
  2325. mods[1] = NULL;
  2326. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2327. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2328. slapi_modify_internal_pb (pb);
  2329. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2330. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2331. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  2332. "config (%d), rid (%d)\n", rc, rid);
  2333. }
  2334. slapi_ch_free_string(&dn);
  2335. slapi_pblock_destroy(pb);
  2336. }
  2337. /*
  2338. * Add aborted rid and repl root to config in case of a server restart
  2339. */
  2340. void
  2341. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  2342. {
  2343. Slapi_PBlock *pb;
  2344. struct berval *vals[2];
  2345. struct berval val;
  2346. LDAPMod *mods[2];
  2347. LDAPMod mod;
  2348. char *data;
  2349. char *dn;
  2350. int rc;
  2351. int i;
  2352. slapi_rwlock_wrlock(abort_rid_lock);
  2353. for(i = 0; i < CLEANRIDSIZ; i++){
  2354. if(aborted_rids[i] == 0){
  2355. aborted_rids[i] = rid;
  2356. aborted_rids[i + 1] = 0;
  2357. break;
  2358. }
  2359. }
  2360. slapi_rwlock_unlock(abort_rid_lock);
  2361. /*
  2362. * Write the rid to the config entry
  2363. */
  2364. dn = replica_get_dn(r);
  2365. pb = slapi_pblock_new();
  2366. data = PR_smprintf("%d:%s", rid, repl_root);
  2367. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2368. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2369. mod.mod_bvalues = vals;
  2370. vals [0] = &val;
  2371. vals [1] = NULL;
  2372. val.bv_val = data;
  2373. val.bv_len = strlen (data);
  2374. mods[0] = &mod;
  2375. mods[1] = NULL;
  2376. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2377. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2378. slapi_modify_internal_pb (pb);
  2379. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2380. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2381. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  2382. "replica config (%d), rid (%d)\n", rc, rid);
  2383. }
  2384. slapi_ch_free_string(&dn);
  2385. slapi_ch_free_string(&data);
  2386. slapi_pblock_destroy(pb);
  2387. }
  2388. void
  2389. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip){
  2390. Slapi_PBlock *pb;
  2391. LDAPMod *mods[2];
  2392. LDAPMod mod;
  2393. struct berval *vals[2];
  2394. struct berval val;
  2395. char *data;
  2396. char *dn;
  2397. int rc;
  2398. int i;
  2399. if(r == NULL)
  2400. return;
  2401. if(skip){
  2402. /* skip the deleting of the config, and just remove the in memory rid */
  2403. slapi_rwlock_wrlock(abort_rid_lock);
  2404. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  2405. for(; i < CLEANRIDSIZ; i++){
  2406. /* rewrite entire array */
  2407. aborted_rids[i] = aborted_rids[i + 1];
  2408. }
  2409. slapi_rwlock_unlock(abort_rid_lock);
  2410. } else {
  2411. /* only remove the config, leave the in-memory rid */
  2412. dn = replica_get_dn(r);
  2413. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  2414. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2415. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2416. mod.mod_bvalues = vals;
  2417. vals [0] = &val;
  2418. vals [1] = NULL;
  2419. val.bv_val = data;
  2420. val.bv_len = strlen (data);
  2421. mods[0] = &mod;
  2422. mods[1] = NULL;
  2423. pb = slapi_pblock_new();
  2424. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2425. slapi_modify_internal_pb (pb);
  2426. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2427. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2428. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  2429. "config (%d), rid (%d)\n", rc, rid);
  2430. }
  2431. slapi_pblock_destroy (pb);
  2432. slapi_ch_free_string(&dn);
  2433. slapi_ch_free_string(&data);
  2434. }
  2435. }
  2436. /*
  2437. * Just remove the dse.ldif config, but we need to keep the cleaned rids in memory until we know we are done
  2438. */
  2439. static void
  2440. delete_cleaned_rid_config(cleanruv_data *clean_data)
  2441. {
  2442. Slapi_PBlock *pb, *modpb;
  2443. Slapi_Entry **entries = NULL;
  2444. LDAPMod *mods[2];
  2445. LDAPMod mod;
  2446. struct berval *vals[5]= {0, 0, 0, 0, 0}; /* maximum of 4 tasks */
  2447. struct berval val[5];
  2448. char *iter = NULL;
  2449. char *dn = NULL;
  2450. int i, ii;
  2451. int rc = -1, ret, rid;
  2452. if(clean_data == NULL){
  2453. cleanruv_log(NULL, -1, CLEANALLRUV_ID, "delete_cleaned_rid_config: cleanruv data is NULL, "
  2454. "failed to clean the config.");
  2455. return;
  2456. }
  2457. /*
  2458. * Search the config for the exact attribute value to delete
  2459. */
  2460. pb = slapi_pblock_new();
  2461. if(clean_data->replica){
  2462. dn = replica_get_dn(clean_data->replica);
  2463. } else {
  2464. goto bail;
  2465. }
  2466. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "nsds5ReplicaCleanRUV=*", NULL, 0, NULL, NULL,
  2467. (void *)plugin_get_default_component_id(), 0);
  2468. slapi_search_internal_pb(pb);
  2469. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2470. if (ret != LDAP_SUCCESS){
  2471. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2472. "delete_cleaned_rid_config: internal search failed(%d).",ret);
  2473. goto bail;
  2474. } else {
  2475. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2476. if (entries == NULL || entries[0] == NULL){
  2477. /*
  2478. * No matching entries!
  2479. */
  2480. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2481. "delete_cleaned_rid_config: failed to find any "
  2482. "entries with nsds5ReplicaCleanRUV under (%s)", dn);
  2483. goto bail;
  2484. } else {
  2485. /*
  2486. * Clean all the matching entries
  2487. */
  2488. for(i = 0; entries[i] != NULL;i++){
  2489. char **attr_val = slapi_entry_attr_get_charray(entries[i], type_replicaCleanRUV);
  2490. char *edn = slapi_entry_get_dn(entries[i]);
  2491. int count = 0;
  2492. for (ii = 0; attr_val && attr_val[ii] && i < 5; ii++){
  2493. /* make a copy to retain the full value after toking */
  2494. char *aval = slapi_ch_strdup(attr_val[ii]);
  2495. rid = atoi(ldap_utf8strtok_r(attr_val[ii], ":", &iter));
  2496. if(rid == clean_data->rid){
  2497. val[count].bv_len = strlen(aval);
  2498. val[count].bv_val = aval;
  2499. vals[count] = &val[count];
  2500. count++;
  2501. } else {
  2502. slapi_ch_free_string(&aval);
  2503. }
  2504. }
  2505. slapi_ch_array_free(attr_val);
  2506. /*
  2507. * Now delete the attribute
  2508. */
  2509. vals[4] = NULL;
  2510. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2511. mod.mod_type = (char *)type_replicaCleanRUV;
  2512. mod.mod_bvalues = vals;
  2513. mods[0] = &mod;
  2514. mods[1] = NULL;
  2515. modpb = slapi_pblock_new();
  2516. slapi_modify_internal_set_pb(modpb, edn, mods, NULL, NULL,
  2517. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2518. slapi_modify_internal_pb (modpb);
  2519. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2520. slapi_pblock_destroy(modpb);
  2521. /* free the attr vals */
  2522. for (ii = 0; ii < count; ii++){
  2523. slapi_ch_free_string(&val[ii].bv_val);
  2524. }
  2525. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2526. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2527. "delete_cleaned_rid_config: failed to remove task data "
  2528. "from (%s) error (%d), rid (%d)", edn, rc, clean_data->rid);
  2529. goto bail;
  2530. }
  2531. }
  2532. }
  2533. }
  2534. bail:
  2535. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2536. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2537. "delete_cleaned_rid_config: failed to remove replica config "
  2538. "(%d), rid (%d)", rc, clean_data->rid);
  2539. }
  2540. slapi_free_search_results_internal(pb);
  2541. slapi_pblock_destroy(pb);
  2542. slapi_ch_free_string(&dn);
  2543. }
  2544. /*
  2545. * Remove the rid from our list, and the config
  2546. */
  2547. void
  2548. remove_cleaned_rid(ReplicaId rid)
  2549. {
  2550. int i;
  2551. /*
  2552. * Remove this rid, and optimize the array
  2553. */
  2554. slapi_rwlock_wrlock(rid_lock);
  2555. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  2556. for(; i < CLEANRIDSIZ; i++){
  2557. /* rewrite entire array */
  2558. cleaned_rids[i] = cleaned_rids[i + 1];
  2559. }
  2560. /* now do the preset cleaned rids */
  2561. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++); /* found rid, stop */
  2562. for(; i < CLEANRIDSIZ; i++){
  2563. /* rewrite entire array */
  2564. pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
  2565. }
  2566. slapi_rwlock_unlock(rid_lock);
  2567. }
  2568. /*
  2569. * Abort the CLEANALLRUV task
  2570. */
  2571. int
  2572. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2573. int *returncode, char *returntext, void *arg)
  2574. {
  2575. struct berval *payload = NULL;
  2576. cleanruv_data *data = NULL;
  2577. PRThread *thread = NULL;
  2578. Slapi_Task *task = NULL;
  2579. Slapi_DN *sdn = NULL;
  2580. Replica *replica;
  2581. ReplicaId rid = -1;
  2582. Object *r;
  2583. const char *certify_all;
  2584. const char *base_dn;
  2585. const char *rid_str;
  2586. char *ridstr = NULL;
  2587. int rc = SLAPI_DSE_CALLBACK_OK;
  2588. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  2589. /* we are already running the maximum number of tasks */
  2590. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2591. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
  2592. CLEANRIDSIZ);
  2593. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2594. *returncode = LDAP_OPERATIONS_ERROR;
  2595. return SLAPI_DSE_CALLBACK_ERROR;
  2596. }
  2597. /* allocate new task now */
  2598. task = slapi_new_task(slapi_entry_get_ndn(e));
  2599. /* register our destructor for waiting the task is done */
  2600. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_abort_destructor);
  2601. /*
  2602. * Get our task settings
  2603. */
  2604. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  2605. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-id\"");
  2606. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2607. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2608. rc = SLAPI_DSE_CALLBACK_ERROR;
  2609. goto out;
  2610. }
  2611. certify_all = fetch_attr(e, "replica-certify-all", 0);
  2612. /*
  2613. * Check the rid
  2614. */
  2615. rid = atoi(rid_str);
  2616. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2617. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  2618. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  2619. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"%s", returntext);
  2620. *returncode = LDAP_OPERATIONS_ERROR;
  2621. rc = SLAPI_DSE_CALLBACK_ERROR;
  2622. goto out;
  2623. }
  2624. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  2625. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-base-dn\"");
  2626. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2627. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2628. rc = SLAPI_DSE_CALLBACK_ERROR;
  2629. goto out;
  2630. }
  2631. if(!is_cleaned_rid(rid) && !is_pre_cleaned_rid(rid)){
  2632. /* we are not cleaning this rid */
  2633. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is not being cleaned, nothing to abort.", rid);
  2634. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2635. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2636. rc = SLAPI_DSE_CALLBACK_ERROR;
  2637. goto out;
  2638. }
  2639. if(is_task_aborted(rid)){
  2640. /* we are already aborting this rid */
  2641. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being aborted", rid);
  2642. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2643. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2644. rc = SLAPI_DSE_CALLBACK_ERROR;
  2645. goto out;
  2646. }
  2647. /*
  2648. * Get the replica object
  2649. */
  2650. sdn = slapi_sdn_new_dn_byval(base_dn);
  2651. if((r = replica_get_replica_from_dn(sdn)) == NULL){
  2652. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to find replica from dn(%s)", base_dn);
  2653. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2654. *returncode = LDAP_OPERATIONS_ERROR;
  2655. rc = SLAPI_DSE_CALLBACK_ERROR;
  2656. goto out;
  2657. }
  2658. /*
  2659. * Check certify value
  2660. */
  2661. if(certify_all){
  2662. if(strcasecmp(certify_all,"yes") && strcasecmp(certify_all,"no")){
  2663. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for \"replica-certify-all\", the value "
  2664. "must be \"yes\" or \"no\".");
  2665. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2666. *returncode = LDAP_OPERATIONS_ERROR;
  2667. rc = SLAPI_DSE_CALLBACK_ERROR;
  2668. goto out;
  2669. }
  2670. } else {
  2671. /*
  2672. * The default should be not to certify all the replicas, because
  2673. * we might be trying to abort a clean task that is "hanging" due
  2674. * to unreachable replicas. If the default is "yes" then the abort
  2675. * task will run into the same issue.
  2676. */
  2677. certify_all = "no";
  2678. }
  2679. /*
  2680. * Create payload
  2681. */
  2682. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, base_dn, certify_all);
  2683. payload = create_cleanruv_payload(ridstr);
  2684. if(payload == NULL){
  2685. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2686. *returncode = LDAP_OPERATIONS_ERROR;
  2687. rc = SLAPI_DSE_CALLBACK_ERROR;
  2688. goto out;
  2689. }
  2690. /*
  2691. * Stop the cleaning, and delete the rid
  2692. */
  2693. replica = (Replica*)object_get_data (r);
  2694. add_aborted_rid(rid, replica, (char *)base_dn);
  2695. stop_ruv_cleaning();
  2696. /*
  2697. * Prepare the abort struct and fire off the thread
  2698. */
  2699. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2700. if (data == NULL) {
  2701. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2702. *returncode = LDAP_OPERATIONS_ERROR;
  2703. rc = SLAPI_DSE_CALLBACK_ERROR;
  2704. goto out;
  2705. }
  2706. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2707. data->replica = replica;
  2708. data->task = task;
  2709. data->payload = payload;
  2710. data->rid = rid;
  2711. data->repl_root = slapi_ch_strdup(base_dn);
  2712. data->sdn = NULL;
  2713. data->certify = slapi_ch_strdup(certify_all);
  2714. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2715. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2716. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2717. if (thread == NULL) {
  2718. object_release(r);
  2719. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2720. *returncode = LDAP_OPERATIONS_ERROR;
  2721. slapi_ch_free_string(&data->certify);
  2722. rc = SLAPI_DSE_CALLBACK_ERROR;
  2723. }
  2724. out:
  2725. slapi_ch_free_string(&ridstr);
  2726. slapi_sdn_free(&sdn);
  2727. if(rc != SLAPI_DSE_CALLBACK_OK){
  2728. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2729. slapi_task_finish(task, rc);
  2730. }
  2731. return rc;
  2732. }
  2733. /*
  2734. * Abort CLEANALLRUV task thread
  2735. */
  2736. void
  2737. replica_abort_task_thread(void *arg)
  2738. {
  2739. cleanruv_data *data = (cleanruv_data *)arg;
  2740. Repl_Agmt *agmt;
  2741. Object *agmt_obj;
  2742. int agmt_not_notified = 1;
  2743. int interval = 10;
  2744. int release_it = 0;
  2745. int count = 0, rc = 0;
  2746. if (!data) {
  2747. return; /* no data */
  2748. }
  2749. if (data->task) {
  2750. slapi_task_inc_refcount(data->task);
  2751. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread --> refcount incremented.\n");
  2752. }
  2753. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Aborting task for rid(%d)...",data->rid);
  2754. /*
  2755. * Need to build the replica from the dn
  2756. */
  2757. if(data->replica == NULL && data->repl_obj == NULL){
  2758. /*
  2759. * This thread was initiated at startup because the process did not finish. Due
  2760. * to timing issues, we need to wait to grab the replica obj until we get here.
  2761. */
  2762. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2763. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to get replica object from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2764. goto done;
  2765. }
  2766. if(data->replica == NULL && data->repl_obj){
  2767. data->replica = (Replica*)object_get_data(data->repl_obj);
  2768. }
  2769. release_it = 1;
  2770. }
  2771. /*
  2772. * Now send the abort cleanruv extended op to all the agreements
  2773. */
  2774. while(agmt_not_notified && !slapi_is_shutting_down()){
  2775. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2776. if(agmt_obj == NULL){
  2777. agmt_not_notified = 0;
  2778. break;
  2779. }
  2780. while (agmt_obj && !slapi_is_shutting_down()){
  2781. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2782. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2783. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2784. agmt_not_notified = 0;
  2785. continue;
  2786. }
  2787. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2788. if(strcasecmp(data->certify,"yes") == 0){
  2789. /* we are verifying all the replicas receive the abort task */
  2790. agmt_not_notified = 1;
  2791. break;
  2792. } else {
  2793. /* we do not care if we could not reach a replica, just continue as if we did */
  2794. agmt_not_notified = 0;
  2795. }
  2796. } else {
  2797. /* success */
  2798. agmt_not_notified = 0;
  2799. }
  2800. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2801. } /* while loop for agmts */
  2802. if(agmt_not_notified == 0){
  2803. /* everybody has been contacted */
  2804. break;
  2805. }
  2806. /*
  2807. * Need to sleep between passes. unless we are shutting down
  2808. */
  2809. if (!slapi_is_shutting_down()){
  2810. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2811. PR_Lock( notify_lock );
  2812. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2813. PR_Unlock( notify_lock );
  2814. }
  2815. if(interval < 14400){ /* 4 hour max */
  2816. interval = interval * 2;
  2817. } else {
  2818. interval = 14400;
  2819. }
  2820. } /* while */
  2821. done:
  2822. if(agmt_not_notified || slapi_is_shutting_down()){
  2823. /* failure */
  2824. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2825. } else {
  2826. /*
  2827. * Wait for this server to stop its cleanallruv task(which removes the rid from the cleaned list)
  2828. */
  2829. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Waiting for CleanAllRUV task to abort...");
  2830. while(is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  2831. DS_Sleep(PR_SecondsToInterval(1));
  2832. count++;
  2833. if(count == 60){ /* it should not take this long */
  2834. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "CleanAllRUV task failed to abort. You might need to "
  2835. "rerun the task.");
  2836. rc = -1;
  2837. break;
  2838. }
  2839. }
  2840. /*
  2841. * Clean up the config
  2842. */
  2843. delete_aborted_rid(data->replica, data->rid, data->repl_root, 1); /* delete just the config, leave rid in memory */
  2844. if(strcasecmp(data->certify, "yes") == 0){
  2845. check_replicas_are_done_aborting(data);
  2846. }
  2847. delete_aborted_rid(data->replica, data->rid, data->repl_root, 0); /* remove the in-memory aborted rid */
  2848. if(rc == 0){
  2849. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Successfully aborted task for rid(%d)", data->rid);
  2850. } else {
  2851. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to abort task for rid(%d)",data->rid);
  2852. }
  2853. }
  2854. if(data->task){
  2855. slapi_task_finish(data->task, agmt_not_notified);
  2856. slapi_task_dec_refcount(data->task);
  2857. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread <-- refcount incremented.\n");
  2858. }
  2859. if(data->repl_obj && release_it)
  2860. object_release(data->repl_obj);
  2861. if(data->payload){
  2862. ber_bvfree(data->payload);
  2863. }
  2864. slapi_ch_free_string(&data->repl_root);
  2865. slapi_ch_free_string(&data->certify);
  2866. slapi_sdn_free(&data->sdn);
  2867. slapi_ch_free((void **)&data);
  2868. }
  2869. static int
  2870. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2871. {
  2872. Repl_Connection *conn = NULL;
  2873. ConnResult crc = 0;
  2874. int msgid = 0;
  2875. int rc = -1;
  2876. if((conn = conn_new(ra)) == NULL){
  2877. return rc;
  2878. }
  2879. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2880. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2881. /*
  2882. * success or failure, just return the error code
  2883. */
  2884. rc = crc;
  2885. if(rc){
  2886. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2887. "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2888. }
  2889. } else {
  2890. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2891. "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2892. rc = -1;
  2893. }
  2894. conn_delete_internal_ext(conn);
  2895. return rc;
  2896. }
  2897. static int
  2898. replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *clean_data, int check_result)
  2899. {
  2900. Repl_Connection *conn = NULL;
  2901. ConnResult crc = 0;
  2902. int msgid = 0;
  2903. int rc = -1;
  2904. if((conn = conn_new(ra)) == NULL){
  2905. return rc;
  2906. }
  2907. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2908. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, clean_data->payload, NULL, &msgid);
  2909. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2910. struct berval *retsdata = NULL;
  2911. char *retoid = NULL;
  2912. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2913. if (CONN_OPERATION_SUCCESS == crc ){
  2914. char *response = NULL;
  2915. decode_cleanruv_payload(retsdata, &response);
  2916. if(response && strcmp(response,CLEANRUV_ACCEPTED) == 0){
  2917. /* extop was accepted */
  2918. rc = 0;
  2919. } else {
  2920. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. "
  2921. "Sending replica CLEANRUV task...", slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2922. /*
  2923. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2924. * add the CLEANRUV task to the replica.
  2925. */
  2926. replica_send_cleanruv_task(ra, clean_data);
  2927. rc = 0;
  2928. }
  2929. if (NULL != retsdata)
  2930. ber_bvfree(retsdata);
  2931. slapi_ch_free_string(&retoid);
  2932. slapi_ch_free_string(&response);
  2933. }
  2934. } else {
  2935. /*
  2936. * success or failure, just return the error code
  2937. */
  2938. rc = crc;
  2939. }
  2940. }
  2941. conn_delete_internal_ext(conn);
  2942. return rc;
  2943. }
  2944. static CSN*
  2945. replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn)
  2946. {
  2947. Object *agmt_obj;
  2948. Repl_Agmt *agmt;
  2949. CSN *maxcsn = NULL, *topcsn = NULL;
  2950. char *rid_text = slapi_ch_smprintf("%d", rid);
  2951. char *csnstr = NULL;
  2952. /* start with the local maxcsn */
  2953. csnstr = replica_cleanallruv_get_local_maxcsn(rid, basedn);
  2954. if(csnstr){
  2955. topcsn = csn_new();
  2956. csn_init_by_string(topcsn, csnstr);
  2957. slapi_ch_free_string(&csnstr);
  2958. }
  2959. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2960. if(agmt_obj == NULL){ /* no agreements */
  2961. goto done;
  2962. }
  2963. while (agmt_obj && !slapi_is_shutting_down()){
  2964. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2965. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2966. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2967. continue;
  2968. }
  2969. if(replica_cleanallruv_get_replica_maxcsn(agmt, rid_text, basedn, &maxcsn) == 0){
  2970. if(maxcsn == NULL){
  2971. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2972. continue;
  2973. }
  2974. if(topcsn == NULL){
  2975. topcsn = maxcsn;
  2976. } else {
  2977. if(csn_compare(topcsn, maxcsn) < 0){
  2978. csn_free(&topcsn);
  2979. topcsn = maxcsn;
  2980. } else {
  2981. csn_free(&maxcsn);
  2982. }
  2983. }
  2984. }
  2985. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2986. }
  2987. done:
  2988. slapi_ch_free_string(&rid_text);
  2989. return topcsn;
  2990. }
  2991. static int
  2992. replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn)
  2993. {
  2994. Repl_Connection *conn = NULL;
  2995. ConnResult crc = -1;
  2996. struct berval *payload = NULL;
  2997. CSN *maxcsn = NULL;
  2998. char *data = NULL;
  2999. int msgid = 0;
  3000. if((conn = conn_new(agmt)) == NULL){
  3001. return -1;
  3002. }
  3003. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3004. payload = create_cleanruv_payload(data);
  3005. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3006. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3007. if(crc == CONN_OPERATION_SUCCESS){
  3008. struct berval *retsdata = NULL;
  3009. char *retoid = NULL;
  3010. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3011. if (CONN_OPERATION_SUCCESS == crc ){
  3012. char *remote_maxcsn = NULL;
  3013. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3014. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3015. maxcsn = csn_new();
  3016. csn_init_by_string(maxcsn, remote_maxcsn);
  3017. *csn = maxcsn;
  3018. } else {
  3019. /* no csn */
  3020. *csn = NULL;
  3021. }
  3022. slapi_ch_free_string(&retoid);
  3023. slapi_ch_free_string(&remote_maxcsn);
  3024. if (NULL != retsdata)
  3025. ber_bvfree(retsdata);
  3026. }
  3027. }
  3028. }
  3029. conn_delete_internal_ext(conn);
  3030. slapi_ch_free_string(&data);
  3031. if(payload)
  3032. ber_bvfree(payload);
  3033. return (int)crc;
  3034. }
  3035. static int
  3036. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task)
  3037. {
  3038. Repl_Connection *conn = NULL;
  3039. ConnResult crc = 0;
  3040. struct berval *payload = NULL;
  3041. char *data = NULL;
  3042. int msgid = 0;
  3043. int rc = -1;
  3044. if((conn = conn_new(agmt)) == NULL){
  3045. return -1;
  3046. }
  3047. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3048. payload = create_cleanruv_payload(data);
  3049. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3050. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3051. if(crc == CONN_OPERATION_SUCCESS){
  3052. struct berval *retsdata = NULL;
  3053. char *retoid = NULL;
  3054. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3055. if (CONN_OPERATION_SUCCESS == crc ){
  3056. char *remote_maxcsn = NULL;
  3057. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3058. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3059. CSN *max, *repl_max;
  3060. max = csn_new();
  3061. repl_max = csn_new();
  3062. csn_init_by_string(max, maxcsn);
  3063. csn_init_by_string(repl_max, remote_maxcsn);
  3064. if(csn_compare (repl_max, max) < 0){
  3065. /* we are not caught up yet, free, and return */
  3066. cleanruv_log(task, atoi(rid_text), CLEANALLRUV_ID,"Replica maxcsn (%s) is not caught up with deleted replica's maxcsn(%s)",
  3067. remote_maxcsn, maxcsn);
  3068. rc = -1;
  3069. } else {
  3070. /* ok this replica is caught up */
  3071. rc = 0;
  3072. }
  3073. csn_free(&max);
  3074. csn_free(&repl_max);
  3075. } else {
  3076. /* no remote_maxcsn - return success */
  3077. rc = 0;
  3078. }
  3079. slapi_ch_free_string(&retoid);
  3080. slapi_ch_free_string(&remote_maxcsn);
  3081. if (NULL != retsdata)
  3082. ber_bvfree(retsdata);
  3083. }
  3084. }
  3085. }
  3086. conn_delete_internal_ext(conn);
  3087. slapi_ch_free_string(&data);
  3088. if(payload)
  3089. ber_bvfree(payload);
  3090. return rc;
  3091. }
  3092. static int
  3093. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  3094. {
  3095. Repl_Connection *conn = NULL;
  3096. LDAP *ld = NULL;
  3097. LDAPMessage *result = NULL;
  3098. int rc = -1;
  3099. if((conn = conn_new(agmt)) == NULL){
  3100. return rc;
  3101. }
  3102. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3103. ld = conn_get_ldap(conn);
  3104. if(ld == NULL){
  3105. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "CleanAllRUV_task: failed to get LDAP "
  3106. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  3107. conn_delete_internal_ext(conn);
  3108. return -1;
  3109. }
  3110. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  3111. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  3112. {
  3113. rc = 0;
  3114. } else {
  3115. rc = -1;
  3116. }
  3117. if(result)
  3118. ldap_msgfree( result );
  3119. }
  3120. conn_delete_internal_ext(conn);
  3121. return rc;
  3122. }
  3123. static int
  3124. replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text, Slapi_Task *task, char *force)
  3125. {
  3126. Repl_Connection *conn = NULL;
  3127. ConnResult crc = 0;
  3128. struct berval *payload = NULL;
  3129. char *data = NULL;
  3130. int msgid = 0;
  3131. int rc = -1;
  3132. if((conn = conn_new(agmt)) == NULL){
  3133. if(strcasecmp(force, "yes") == 0){
  3134. return 0;
  3135. }
  3136. return rc;
  3137. }
  3138. data = slapi_ch_smprintf("%s:%s",rid_text, repl_root);
  3139. payload = create_cleanruv_payload(data);
  3140. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3141. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3142. if(crc == CONN_OPERATION_SUCCESS){
  3143. struct berval *retsdata = NULL;
  3144. char *retoid = NULL;
  3145. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3146. if (CONN_OPERATION_SUCCESS == crc ){
  3147. char *remote_maxcsn = NULL;
  3148. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3149. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3150. /* remote replica still has dirty RUV element */
  3151. rc = -1;
  3152. } else {
  3153. /* no maxcsn = we're clean */
  3154. rc = 0;
  3155. }
  3156. slapi_ch_free_string(&retoid);
  3157. slapi_ch_free_string(&remote_maxcsn);
  3158. if (NULL != retsdata)
  3159. ber_bvfree(retsdata);
  3160. }
  3161. }
  3162. } else {
  3163. if (strcasecmp(force, "yes") == 0){
  3164. /* We are forcing, we don't care that the replica is not online */
  3165. rc = 0;
  3166. }
  3167. }
  3168. conn_delete_internal_ext(conn);
  3169. slapi_ch_free_string(&data);
  3170. if(payload)
  3171. ber_bvfree(payload);
  3172. return rc;
  3173. }
  3174. static int
  3175. get_cleanruv_task_count()
  3176. {
  3177. int i, count = 0;
  3178. slapi_rwlock_wrlock(rid_lock);
  3179. for(i = 0; i < CLEANRIDSIZ; i++){
  3180. if(pre_cleaned_rids[i] != 0){
  3181. count++;
  3182. }
  3183. }
  3184. slapi_rwlock_unlock(rid_lock);
  3185. return count;
  3186. }
  3187. static int
  3188. get_abort_cleanruv_task_count()
  3189. {
  3190. int i, count = 0;
  3191. slapi_rwlock_wrlock(rid_lock);
  3192. for(i = 0; i < CLEANRIDSIZ; i++){
  3193. if(aborted_rids[i] != 0){
  3194. count++;
  3195. }
  3196. }
  3197. slapi_rwlock_unlock(rid_lock);
  3198. return count;
  3199. }
  3200. /*
  3201. * Notify sleeping CLEANALLRUV threads to stop
  3202. */
  3203. void
  3204. stop_ruv_cleaning()
  3205. {
  3206. if(notify_lock){
  3207. PR_Lock( notify_lock );
  3208. PR_NotifyCondVar( notify_cvar );
  3209. PR_Unlock( notify_lock );
  3210. }
  3211. }
  3212. /*
  3213. * Write our logging to the task and error log
  3214. */
  3215. void
  3216. cleanruv_log(Slapi_Task *task, int rid, char *task_type, char *fmt, ...)
  3217. {
  3218. va_list ap1;
  3219. va_list ap2;
  3220. va_list ap3;
  3221. va_list ap4;
  3222. char *errlog_fmt;
  3223. va_start(ap1, fmt);
  3224. va_start(ap2, fmt);
  3225. va_start(ap3, fmt);
  3226. va_start(ap4, fmt);
  3227. if(task){
  3228. slapi_task_log_notice_ext(task, fmt, ap1);
  3229. slapi_task_log_status_ext(task, fmt, ap2);
  3230. slapi_task_inc_progress(task);
  3231. }
  3232. errlog_fmt = PR_smprintf("%s (rid %d): %s \n",task_type, rid, fmt);
  3233. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  3234. slapi_ch_free_string(&errlog_fmt);
  3235. va_end(ap1);
  3236. va_end(ap2);
  3237. va_end(ap3);
  3238. va_end(ap4);
  3239. }
  3240. char *
  3241. replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn)
  3242. {
  3243. Slapi_PBlock *search_pb = NULL;
  3244. Slapi_Entry **entries = NULL;
  3245. char **ruv_elements = NULL;
  3246. char *maxcsn = NULL;
  3247. char *filter = NULL;
  3248. char *ridstr = NULL;
  3249. char *iter = NULL;
  3250. char *attrs[2];
  3251. char *ruv_part = NULL;
  3252. int part_count = 0;
  3253. int res, i;
  3254. /*
  3255. * Get the maxruv from the database tombstone entry
  3256. */
  3257. filter = "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))";
  3258. attrs[0] = "nsds50ruv";
  3259. attrs[1] = NULL;
  3260. ridstr = slapi_ch_smprintf("{replica %d ldap", rid);
  3261. search_pb = slapi_pblock_new();
  3262. slapi_search_internal_set_pb(search_pb, base_dn, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  3263. slapi_search_internal_pb (search_pb);
  3264. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3265. if ( LDAP_SUCCESS == res ) {
  3266. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3267. if (NULL == entries || entries[0] == NULL) {
  3268. /* Hmmm, no tombstone! Error out */
  3269. } else {
  3270. /* find the right ruv element, and find the maxcsn */
  3271. ruv_elements = slapi_entry_attr_get_charray(entries[0],attrs[0]);
  3272. for(i = 0; ruv_elements && ruv_elements[i] ; i++){
  3273. if(strstr(ruv_elements[i], ridstr)){
  3274. /* get the max csn */
  3275. ruv_part = ldap_utf8strtok_r(ruv_elements[i], " ", &iter);
  3276. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  3277. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  3278. }
  3279. if(part_count == 5 && ruv_part){/* we have the maxcsn */
  3280. maxcsn = slapi_ch_strdup(ruv_part);
  3281. break;
  3282. }
  3283. }
  3284. }
  3285. slapi_ch_array_free(ruv_elements);
  3286. }
  3287. } else {
  3288. /* internal search failed */
  3289. cleanruv_log(NULL, (int)rid, CLEANALLRUV_ID, "replica_cleanallruv_get_local_maxcsn: internal search failed (%d)\n", res);
  3290. }
  3291. slapi_free_search_results_internal(search_pb);
  3292. slapi_pblock_destroy(search_pb);
  3293. slapi_ch_free_string(&ridstr);
  3294. return maxcsn;
  3295. }