1
0

repl5_replica_config.c 113 KB

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