repl5_replica_config.c 123 KB

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