repl5_replica_config.c 109 KB

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