repl5_replica_config.c 110 KB

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