repl5_replica_config.c 105 KB

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