repl5_replica.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465
  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.c */
  42. #include "slapi-plugin.h"
  43. #include "repl.h" /* ONREPL - this is bad */
  44. #include "repl5.h"
  45. #include "repl_shared.h"
  46. #include "csnpl.h"
  47. #include "cl5_api.h"
  48. /* from proto-slap.h */
  49. int g_get_shutdown();
  50. #define RUV_SAVE_INTERVAL (30 * 1000) /* 30 seconds */
  51. #define START_UPDATE_DELAY 2 /* 2 second */
  52. #define REPLICA_RDN "cn=replica"
  53. #define CHANGELOG_RDN "cn=legacy changelog"
  54. /*
  55. * A replica is a locally-held copy of a portion of the DIT.
  56. */
  57. struct replica {
  58. Slapi_DN *repl_root; /* top of the replicated area */
  59. char *repl_name; /* unique replica name */
  60. PRBool new_name; /* new name was generated - need to be saved */
  61. ReplicaUpdateDNList updatedn_list; /* list of dns with which a supplier should bind
  62. to update this replica */
  63. ReplicaType repl_type; /* is this replica read-only ? */
  64. PRBool legacy_consumer; /* if true, this replica is supplied by 4.0 consumer */
  65. char* legacy_purl; /* partial url of the legacy supplier */
  66. ReplicaId repl_rid; /* replicaID */
  67. Object *repl_ruv; /* replica update vector */
  68. PRBool repl_ruv_dirty; /* Dirty flag for ruv */
  69. CSNPL *min_csn_pl; /* Pending list for minimal CSN */
  70. void *csn_pl_reg_id; /* registration assignment for csn callbacks */
  71. unsigned long repl_state_flags; /* state flags */
  72. PRUint32 repl_flags; /* persistent, externally visible flags */
  73. PRLock *repl_lock; /* protects entire structure */
  74. Slapi_Eq_Context repl_eqcxt_rs; /* context to cancel event that saves ruv */
  75. Slapi_Eq_Context repl_eqcxt_tr; /* context to cancel event that reaps tombstones */
  76. Object *repl_csngen; /* CSN generator for this replica */
  77. PRBool repl_csn_assigned; /* Flag set when new csn is assigned. */
  78. PRUint32 repl_purge_delay; /* When purgeable, CSNs are held on to for this many extra seconds */
  79. PRBool tombstone_reap_stop; /* TRUE when the tombstone reaper should stop */
  80. PRBool tombstone_reap_active; /* TRUE when the tombstone reaper is running */
  81. long tombstone_reap_interval; /* Time in seconds between tombstone reaping */
  82. Slapi_ValueSet *repl_referral; /* A list of administrator provided referral URLs */
  83. PRBool state_update_inprogress; /* replica state is being updated */
  84. PRLock *agmt_lock; /* protects agreement creation, start and stop */
  85. char *locking_purl; /* supplier who has exclusive access */
  86. };
  87. typedef struct reap_callback_data
  88. {
  89. int rc;
  90. unsigned long num_entries;
  91. unsigned long num_purged_entries;
  92. CSN *purge_csn;
  93. PRBool *tombstone_reap_stop;
  94. } reap_callback_data;
  95. /* Forward declarations of helper functions*/
  96. static Slapi_Entry* _replica_get_config_entry (const Slapi_DN *root);
  97. static int _replica_check_validity (const Replica *r);
  98. static int _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext);
  99. static int _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext);
  100. static int _replica_configure_ruv (Replica *r, PRBool isLocked);
  101. static void _replica_update_state (time_t when, void *arg);
  102. static char * _replica_get_config_dn (const Slapi_DN *root);
  103. static char * _replica_type_as_string (const Replica *r);
  104. /* DBDB, I think this is probably bogus : */
  105. static int replica_create_ruv_tombstone(Replica *r);
  106. static void assign_csn_callback(const CSN *csn, void *data);
  107. static void abort_csn_callback(const CSN *csn, void *data);
  108. static void eq_cb_reap_tombstones(time_t when, void *arg);
  109. static CSN *_replica_get_purge_csn_nolock (const Replica *r);
  110. static void replica_get_referrals_nolock (const Replica *r, char ***referrals);
  111. static void replica_clear_legacy_referrals (const Slapi_DN *repl_root_sdn, char **referrals, const char *state);
  112. static void replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr);
  113. static int replica_log_ruv_elements_nolock (const Replica *r);
  114. static void replica_replace_ruv_tombstone(Replica *r);
  115. static void start_agreements_for_replica (Replica *r, PRBool start);
  116. /* Allocates new replica and reads its state and state of its component from
  117. * various parts of the DIT.
  118. */
  119. Replica *
  120. replica_new(const Slapi_DN *root)
  121. {
  122. Replica *r = NULL;
  123. Slapi_Entry *e = NULL;
  124. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  125. char ebuf[BUFSIZ];
  126. PR_ASSERT (root);
  127. /* check if there is a replica associated with the tree */
  128. e = _replica_get_config_entry (root);
  129. if (e)
  130. {
  131. errorbuf[0] = '\0';
  132. r = replica_new_from_entry(e, errorbuf,
  133. PR_FALSE /* not a newly added entry */);
  134. if (NULL == r)
  135. {
  136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to "
  137. "configure replica %s: %s\n",
  138. escape_string(slapi_sdn_get_dn(root), ebuf),
  139. errorbuf);
  140. }
  141. slapi_entry_free (e);
  142. }
  143. return r;
  144. }
  145. /* constructs the replica object from the newly added entry */
  146. Replica *
  147. replica_new_from_entry (Slapi_Entry *e, char *errortext, PRBool is_add_operation)
  148. {
  149. int rc = 0;
  150. Replica *r;
  151. char *repl_name = NULL;
  152. if (e == NULL)
  153. {
  154. if (NULL != errortext)
  155. {
  156. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "NULL entry");
  157. }
  158. return NULL;
  159. }
  160. r = (Replica *)slapi_ch_calloc(1, sizeof(Replica));
  161. if ((r->repl_lock = PR_NewLock()) == NULL)
  162. {
  163. if (NULL != errortext)
  164. {
  165. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  166. }
  167. rc = -1;
  168. goto done;
  169. }
  170. if ((r->agmt_lock = PR_NewLock()) == NULL)
  171. {
  172. if (NULL != errortext)
  173. {
  174. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  175. }
  176. rc = -1;
  177. goto done;
  178. }
  179. /* read parameters from the replica config entry */
  180. rc = _replica_init_from_config (r, e, errortext);
  181. if (rc != 0)
  182. {
  183. goto done;
  184. }
  185. /* configure ruv */
  186. rc = _replica_configure_ruv (r, PR_FALSE);
  187. if (rc != 0)
  188. {
  189. goto done;
  190. }
  191. /* If smallest csn exists in RUV for our local replica, it's ok to begin iteration */
  192. PR_ASSERT (object_get_data (r->repl_ruv));
  193. if (is_add_operation)
  194. {
  195. /*
  196. * This is called by an ldap add operation.
  197. * Update the entry to contain information generated
  198. * during replica initialization
  199. */
  200. rc = _replica_update_entry (r, e, errortext);
  201. }
  202. else
  203. {
  204. /*
  205. * Entry is already in dse.ldif - update it on the disk
  206. * (done by the update state event scheduled below)
  207. */
  208. }
  209. if (rc != 0)
  210. goto done;
  211. /* ONREPL - the state update can occur before the entry is added to the DIT.
  212. In that case the updated would fail but nothing bad would happen. The next
  213. scheduled update would save the state */
  214. repl_name = slapi_ch_strdup (r->repl_name);
  215. r->repl_eqcxt_rs = slapi_eq_repeat(_replica_update_state, repl_name,
  216. current_time () + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  217. if (r->tombstone_reap_interval > 0)
  218. {
  219. /*
  220. * Reap Tombstone should be started some time after the plugin started.
  221. * This will allow the server to fully start before consuming resources.
  222. */
  223. repl_name = slapi_ch_strdup (r->repl_name);
  224. r->repl_eqcxt_tr = slapi_eq_repeat(eq_cb_reap_tombstones, repl_name,
  225. current_time() + r->tombstone_reap_interval,
  226. 1000 * r->tombstone_reap_interval);
  227. }
  228. if (r->legacy_consumer)
  229. {
  230. char ebuf[BUFSIZ];
  231. legacy_consumer_init_referrals (r);
  232. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_new_from_entry: "
  233. "replica for %s was configured as legacy consumer\n",
  234. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  235. }
  236. done:
  237. if (rc != 0 && r)
  238. {
  239. replica_destroy ((void**)&r);
  240. }
  241. return r;
  242. }
  243. void
  244. replica_flush(Replica *r)
  245. {
  246. PR_ASSERT(NULL != r);
  247. if (NULL != r)
  248. {
  249. PR_Lock(r->repl_lock);
  250. /* Make sure we dump the CSNGen state */
  251. r->repl_csn_assigned = PR_TRUE;
  252. PR_Unlock(r->repl_lock);
  253. /* This function take the Lock Inside */
  254. /* And also write the RUV */
  255. _replica_update_state((time_t)0, r->repl_name);
  256. }
  257. }
  258. /*
  259. * Deallocate a replica. arg should point to the address of a
  260. * pointer that points to a replica structure.
  261. */
  262. void
  263. replica_destroy(void **arg)
  264. {
  265. Replica *r;
  266. void *repl_name;
  267. if (arg == NULL)
  268. return;
  269. r = *((Replica **)arg);
  270. PR_ASSERT(r);
  271. slapi_log_error (SLAPI_LOG_REPL, NULL, "replica_destroy\n");
  272. /*
  273. * The function will not be called unless the refcnt of its
  274. * wrapper object is 0. Hopefully this refcnt could sync up
  275. * this destruction and the events such as tombstone reap
  276. * and ruv updates.
  277. */
  278. if (r->repl_eqcxt_rs)
  279. {
  280. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  281. slapi_ch_free (&repl_name);
  282. slapi_eq_cancel(r->repl_eqcxt_rs);
  283. r->repl_eqcxt_rs = NULL;
  284. }
  285. if (r->repl_eqcxt_tr)
  286. {
  287. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  288. slapi_ch_free (&repl_name);
  289. slapi_eq_cancel(r->repl_eqcxt_tr);
  290. r->repl_eqcxt_tr = NULL;
  291. }
  292. if (r->repl_root)
  293. {
  294. slapi_sdn_free(&r->repl_root);
  295. }
  296. slapi_ch_free_string(&r->locking_purl);
  297. if (r->updatedn_list)
  298. {
  299. replica_updatedn_list_free(r->updatedn_list);
  300. r->updatedn_list = NULL;
  301. }
  302. /* slapi_ch_free accepts NULL pointer */
  303. slapi_ch_free ((void**)&r->repl_name);
  304. slapi_ch_free ((void**)&r->legacy_purl);
  305. if (r->repl_lock)
  306. {
  307. PR_DestroyLock(r->repl_lock);
  308. r->repl_lock = NULL;
  309. }
  310. if (r->agmt_lock)
  311. {
  312. PR_DestroyLock(r->agmt_lock);
  313. r->agmt_lock = NULL;
  314. }
  315. if(NULL != r->repl_ruv)
  316. {
  317. object_release(r->repl_ruv);
  318. }
  319. if(NULL != r->repl_csngen)
  320. {
  321. if (r->csn_pl_reg_id)
  322. {
  323. csngen_unregister_callbacks((CSNGen *)object_get_data (r->repl_csngen), r->csn_pl_reg_id);
  324. }
  325. object_release(r->repl_csngen);
  326. }
  327. if (NULL != r->repl_referral)
  328. {
  329. slapi_valueset_free(r->repl_referral);
  330. }
  331. if (NULL != r->min_csn_pl)
  332. {
  333. csnplFree(&r->min_csn_pl);;
  334. }
  335. slapi_ch_free((void **)arg);
  336. }
  337. /*
  338. * Attempt to obtain exclusive access to replica (advisory only)
  339. *
  340. * Returns PR_TRUE if exclusive access was granted,
  341. * PR_FALSE otherwise
  342. * The parameter isInc tells whether or not the replica is being
  343. * locked for an incremental update session - if the replica is
  344. * successfully locked, this value is used - if the replica is already
  345. * in use, this value will be set to TRUE or FALSE, depending on what
  346. * type of update session has the replica in use currently
  347. * locking_purl is the supplier who is attempting to acquire access
  348. * current_purl is the supplier who already has access, if any
  349. */
  350. PRBool
  351. replica_get_exclusive_access(Replica *r, PRBool *isInc, PRUint64 connid, int opid,
  352. const char *locking_purl,
  353. char **current_purl)
  354. {
  355. char ebuf[BUFSIZ];
  356. PRBool rval = PR_TRUE;
  357. PR_ASSERT(r);
  358. PR_Lock(r->repl_lock);
  359. if (r->repl_state_flags & REPLICA_IN_USE)
  360. {
  361. if (isInc)
  362. *isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  363. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  364. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  365. "Replica in use locking_purl=%s\n",
  366. connid, opid,
  367. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  368. r->locking_purl ? r->locking_purl : "unknown");
  369. rval = PR_FALSE;
  370. if (current_purl)
  371. {
  372. *current_purl = slapi_ch_strdup(r->locking_purl);
  373. }
  374. }
  375. else
  376. {
  377. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  378. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Acquired replica\n",
  379. connid, opid,
  380. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  381. r->repl_state_flags |= REPLICA_IN_USE;
  382. if (isInc && *isInc)
  383. {
  384. r->repl_state_flags |= REPLICA_INCREMENTAL_IN_PROGRESS;
  385. }
  386. else
  387. {
  388. /* if connid or opid != 0, it's a total update */
  389. /* Both set to 0 means we're disabling replication */
  390. if (connid || opid)
  391. {
  392. r->repl_state_flags |= REPLICA_TOTAL_IN_PROGRESS;
  393. }
  394. }
  395. slapi_ch_free_string(&r->locking_purl);
  396. r->locking_purl = slapi_ch_strdup(locking_purl);
  397. }
  398. PR_Unlock(r->repl_lock);
  399. return rval;
  400. }
  401. /*
  402. * Relinquish exclusive access to the replica
  403. */
  404. void
  405. replica_relinquish_exclusive_access(Replica *r, PRUint64 connid, int opid)
  406. {
  407. char ebuf[BUFSIZ];
  408. PRBool isInc;
  409. PR_ASSERT(r);
  410. PR_Lock(r->repl_lock);
  411. isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  412. /* check to see if the replica is in use and log a warning if not */
  413. if (!(r->repl_state_flags & REPLICA_IN_USE))
  414. {
  415. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  416. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  417. "Replica not in use\n",
  418. connid, opid,
  419. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  420. } else {
  421. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  422. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  423. "Released replica\n",
  424. connid, opid,
  425. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  426. slapi_ch_free_string(&r->locking_purl);
  427. r->repl_state_flags &= ~(REPLICA_IN_USE);
  428. if (isInc)
  429. r->repl_state_flags &= ~(REPLICA_INCREMENTAL_IN_PROGRESS);
  430. else
  431. r->repl_state_flags &= ~(REPLICA_TOTAL_IN_PROGRESS);
  432. }
  433. PR_Unlock(r->repl_lock);
  434. }
  435. /*
  436. * Returns root of the replicated area
  437. */
  438. PRBool
  439. replica_get_tombstone_reap_active(const Replica *r)
  440. {
  441. PR_ASSERT(r);
  442. return(r->tombstone_reap_active);
  443. }
  444. /*
  445. * Returns root of the replicated area
  446. */
  447. const Slapi_DN *
  448. replica_get_root(const Replica *r) /* ONREPL - should we return copy instead? */
  449. {
  450. PR_ASSERT(r);
  451. /* replica root never changes so we don't have to lock */
  452. return(r->repl_root);
  453. }
  454. /*
  455. * Returns normalized dn of the root of the replicated area
  456. */
  457. const char *
  458. replica_get_name(const Replica *r) /* ONREPL - should we return copy instead? */
  459. {
  460. PR_ASSERT(r);
  461. /* replica name never changes so we don't have to lock */
  462. return(r->repl_name);
  463. }
  464. /*
  465. * Returns replicaid of this replica
  466. */
  467. ReplicaId
  468. replica_get_rid (const Replica *r)
  469. {
  470. ReplicaId rid;
  471. PR_ASSERT(r);
  472. PR_Lock(r->repl_lock);
  473. rid = r->repl_rid;
  474. PR_Unlock(r->repl_lock);
  475. return rid;
  476. }
  477. /*
  478. * Sets replicaid of this replica - should only be used when also changing the type
  479. */
  480. void
  481. replica_set_rid (Replica *r, ReplicaId rid)
  482. {
  483. PR_ASSERT(r);
  484. PR_Lock(r->repl_lock);
  485. r->repl_rid = rid;
  486. PR_Unlock(r->repl_lock);
  487. }
  488. /* Returns true if replica was initialized through ORC or import;
  489. * otherwise, false. An uninitialized replica should return
  490. * LDAP_UNWILLING_TO_PERFORM to all client requests
  491. */
  492. PRBool
  493. replica_is_initialized (const Replica *r)
  494. {
  495. PR_ASSERT(r);
  496. return (r->repl_ruv != NULL);
  497. }
  498. /*
  499. * Returns refcounted object that contains RUV. The caller should release the
  500. * object once it is no longer used. To release, call object_release
  501. */
  502. Object *
  503. replica_get_ruv (const Replica *r)
  504. {
  505. Object *ruv = NULL;
  506. PR_ASSERT(r);
  507. PR_Lock(r->repl_lock);
  508. PR_ASSERT (r->repl_ruv);
  509. object_acquire (r->repl_ruv);
  510. ruv = r->repl_ruv;
  511. PR_Unlock(r->repl_lock);
  512. return ruv;
  513. }
  514. /*
  515. * Sets RUV vector. This function should be called during replica
  516. * (re)initialization. During normal operation, the RUV is read from
  517. * the root of the replicated in the replica_new call
  518. */
  519. void
  520. replica_set_ruv (Replica *r, RUV *ruv)
  521. {
  522. PR_ASSERT(r && ruv);
  523. PR_Lock(r->repl_lock);
  524. if(NULL != r->repl_ruv)
  525. {
  526. object_release(r->repl_ruv);
  527. }
  528. /* if the local replica is not in the RUV and it is writable - add it
  529. and reinitialize min_csn pending list */
  530. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  531. {
  532. CSN *csn = NULL;
  533. if (r->min_csn_pl)
  534. csnplFree (&r->min_csn_pl);
  535. if (ruv_contains_replica (ruv, r->repl_rid))
  536. {
  537. ruv_get_smallest_csn_for_replica(ruv, r->repl_rid, &csn);
  538. if (csn)
  539. csn_free (&csn);
  540. else
  541. r->min_csn_pl = csnplNew ();
  542. /* We need to make sure the local ruv element is the 1st. */
  543. ruv_move_local_supplier_to_first(ruv, r->repl_rid);
  544. }
  545. else
  546. {
  547. r->min_csn_pl = csnplNew ();
  548. /* To be sure that the local is in first */
  549. ruv_add_index_replica(ruv, r->repl_rid, multimaster_get_local_purl(), 1);
  550. }
  551. }
  552. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  553. r->repl_ruv_dirty = PR_TRUE;
  554. PR_Unlock(r->repl_lock);
  555. }
  556. /*
  557. * Update one particular CSN in an RUV. This is meant to be called
  558. * whenever (a) the server has processed a client operation and
  559. * needs to update its CSN, or (b) the server is completing an
  560. * inbound replication session operation, and needs to update its
  561. * local RUV.
  562. */
  563. void
  564. replica_update_ruv(Replica *r, const CSN *updated_csn, const char *replica_purl)
  565. {
  566. char csn_str[CSN_STRSIZE];
  567. char ebuf[BUFSIZ];
  568. PR_ASSERT(NULL != r);
  569. PR_ASSERT(NULL != updated_csn);
  570. #ifdef DEBUG
  571. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  572. "replica_update_ruv: csn %s\n",
  573. csn_as_string(updated_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  574. #endif
  575. if (NULL == r)
  576. {
  577. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: replica "
  578. "is NULL\n");
  579. }
  580. else if (NULL == updated_csn)
  581. {
  582. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: csn "
  583. "is NULL when updating replica %s\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  584. }
  585. else
  586. {
  587. RUV *ruv;
  588. PR_Lock(r->repl_lock);
  589. if (r->repl_ruv != NULL)
  590. {
  591. ruv = object_get_data(r->repl_ruv);
  592. if (NULL != ruv)
  593. {
  594. ReplicaId rid = csn_get_replicaid(updated_csn);
  595. if (rid == r->repl_rid)
  596. {
  597. if (NULL != r->min_csn_pl)
  598. {
  599. CSN *min_csn;
  600. PRBool committed;
  601. (void)csnplCommit(r->min_csn_pl, updated_csn);
  602. min_csn = csnplGetMinCSN(r->min_csn_pl, &committed);
  603. if (NULL != min_csn)
  604. {
  605. if (committed)
  606. {
  607. ruv_set_min_csn(ruv, min_csn, replica_purl);
  608. csnplFree(&r->min_csn_pl);
  609. }
  610. csn_free(&min_csn);
  611. }
  612. }
  613. }
  614. /* Update max csn for local and remote replicas */
  615. if (ruv_update_ruv (ruv, updated_csn, replica_purl, rid == r->repl_rid)
  616. != RUV_SUCCESS)
  617. {
  618. slapi_log_error(SLAPI_LOG_FATAL,
  619. repl_plugin_name, "replica_update_ruv: unable "
  620. "to update RUV for replica %s, csn = %s\n",
  621. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  622. csn_as_string(updated_csn, PR_FALSE, csn_str));
  623. }
  624. r->repl_ruv_dirty = PR_TRUE;
  625. }
  626. else
  627. {
  628. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  629. "replica_update_ruv: unable to get RUV object for replica "
  630. "%s\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  631. }
  632. }
  633. else
  634. {
  635. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: "
  636. "unable to initialize RUV for replica %s\n",
  637. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  638. }
  639. PR_Unlock(r->repl_lock);
  640. }
  641. }
  642. /*
  643. * Returns refcounted object that contains csn generator. The caller should release the
  644. * object once it is no longer used. To release, call object_release
  645. */
  646. Object *
  647. replica_get_csngen (const Replica *r)
  648. {
  649. Object *csngen;
  650. PR_ASSERT(r);
  651. PR_Lock(r->repl_lock);
  652. object_acquire (r->repl_csngen);
  653. csngen = r->repl_csngen;
  654. PR_Unlock(r->repl_lock);
  655. return csngen;
  656. }
  657. /*
  658. * Returns the replica type.
  659. */
  660. ReplicaType
  661. replica_get_type (const Replica *r)
  662. {
  663. PR_ASSERT(r);
  664. return r->repl_type;
  665. }
  666. /*
  667. * Sets the replica type.
  668. */
  669. void
  670. replica_set_type (Replica *r, ReplicaType type)
  671. {
  672. PR_ASSERT(r);
  673. PR_Lock(r->repl_lock);
  674. r->repl_type = type;
  675. PR_Unlock(r->repl_lock);
  676. }
  677. /*
  678. * Returns PR_TRUE if this replica is a consumer of 4.0 server
  679. * and PR_FALSE otherwise
  680. */
  681. PRBool
  682. replica_is_legacy_consumer (const Replica *r)
  683. {
  684. PR_ASSERT(r);
  685. return r->legacy_consumer;
  686. }
  687. /*
  688. * Sets the replica type.
  689. */
  690. void
  691. replica_set_legacy_consumer (Replica *r, PRBool legacy_consumer)
  692. {
  693. int legacy2mmr;
  694. Slapi_DN *repl_root_sdn = NULL;
  695. char **referrals = NULL;
  696. char *replstate = NULL;
  697. PR_ASSERT(r);
  698. PR_Lock(r->repl_lock);
  699. legacy2mmr = r->legacy_consumer && !legacy_consumer;
  700. /* making the server a regular 5.0 replica */
  701. if (legacy2mmr)
  702. {
  703. slapi_ch_free ((void**)&r->legacy_purl);
  704. /* Remove copiedFrom/copyingFrom attributes from the root entry */
  705. /* set the right state in the mapping tree */
  706. if (r->repl_type == REPLICA_TYPE_READONLY)
  707. {
  708. replica_get_referrals_nolock (r, &referrals);
  709. replstate = STATE_UPDATE_REFERRAL;
  710. }
  711. else /* updateable */
  712. {
  713. replstate = STATE_BACKEND;
  714. }
  715. }
  716. r->legacy_consumer = legacy_consumer;
  717. repl_root_sdn = slapi_sdn_dup(r->repl_root);
  718. PR_Unlock(r->repl_lock);
  719. if (legacy2mmr)
  720. {
  721. replica_clear_legacy_referrals(repl_root_sdn, referrals, replstate);
  722. /* Also change state of the mapping tree node and/or referrals */
  723. replica_remove_legacy_attr (repl_root_sdn, type_copiedFrom);
  724. replica_remove_legacy_attr (repl_root_sdn, type_copyingFrom);
  725. }
  726. charray_free(referrals);
  727. slapi_sdn_free(&repl_root_sdn);
  728. }
  729. /* Gets partial url of the legacy supplier - applicable for legacy consumer only */
  730. char *
  731. replica_get_legacy_purl (const Replica *r)
  732. {
  733. char *purl;
  734. PR_Lock (r->repl_lock);
  735. PR_ASSERT (r->legacy_consumer);
  736. purl = slapi_ch_strdup (r->legacy_purl);
  737. PR_Unlock (r->repl_lock);
  738. return purl;
  739. }
  740. void
  741. replica_set_legacy_purl (Replica *r, const char *purl)
  742. {
  743. PR_Lock (r->repl_lock);
  744. PR_ASSERT (r->legacy_consumer);
  745. /* slapi_ch_free accepts NULL pointer */
  746. slapi_ch_free ((void**)&r->legacy_purl);
  747. r->legacy_purl = slapi_ch_strdup (purl);
  748. PR_Unlock (r->repl_lock);
  749. }
  750. /*
  751. * Returns true if sdn is the same as updatedn and false otherwise
  752. */
  753. PRBool
  754. replica_is_updatedn (const Replica *r, const Slapi_DN *sdn)
  755. {
  756. PRBool result;
  757. PR_ASSERT (r);
  758. PR_Lock(r->repl_lock);
  759. if (sdn == NULL)
  760. {
  761. result = (r->updatedn_list == NULL);
  762. }
  763. else if (r->updatedn_list == NULL)
  764. {
  765. result = PR_FALSE;
  766. }
  767. else
  768. {
  769. result = replica_updatedn_list_ismember(r->updatedn_list, sdn);
  770. }
  771. PR_Unlock(r->repl_lock);
  772. return result;
  773. }
  774. /*
  775. * Sets updatedn list for this replica
  776. */
  777. void
  778. replica_set_updatedn (Replica *r, const Slapi_ValueSet *vs, int mod_op)
  779. {
  780. PR_ASSERT (r);
  781. PR_Lock(r->repl_lock);
  782. if (!r->updatedn_list)
  783. r->updatedn_list = replica_updatedn_list_new(NULL);
  784. if (SLAPI_IS_MOD_DELETE(mod_op) || vs == NULL ||
  785. (0 == slapi_valueset_count(vs))) /* null value also causes list deletion */
  786. replica_updatedn_list_delete(r->updatedn_list, vs);
  787. else if (SLAPI_IS_MOD_REPLACE(mod_op))
  788. replica_updatedn_list_replace(r->updatedn_list, vs);
  789. else if (SLAPI_IS_MOD_ADD(mod_op))
  790. replica_updatedn_list_add(r->updatedn_list, vs);
  791. PR_Unlock(r->repl_lock);
  792. }
  793. /* gets current replica generation for this replica */
  794. char *replica_get_generation (const Replica *r)
  795. {
  796. int rc = 0;
  797. char *gen = NULL;
  798. if (r)
  799. {
  800. PR_Lock(r->repl_lock);
  801. PR_ASSERT (r->repl_ruv);
  802. if (rc == 0)
  803. gen = ruv_get_replica_generation ((RUV*)object_get_data (r->repl_ruv));
  804. PR_Unlock(r->repl_lock);
  805. }
  806. return gen;
  807. }
  808. PRBool replica_is_flag_set (const Replica *r, PRUint32 flag)
  809. {
  810. if (r)
  811. return (r->repl_flags & flag);
  812. else
  813. return PR_FALSE;
  814. }
  815. void replica_set_flag (Replica *r, PRUint32 flag, PRBool clear)
  816. {
  817. if (r == NULL)
  818. return;
  819. PR_Lock(r->repl_lock);
  820. if (clear)
  821. {
  822. r->repl_flags &= ~flag;
  823. }
  824. else
  825. {
  826. r->repl_flags |= flag;
  827. }
  828. PR_Unlock(r->repl_lock);
  829. }
  830. void replica_replace_flags (Replica *r, PRUint32 flags)
  831. {
  832. if (r)
  833. {
  834. PR_Lock(r->repl_lock);
  835. r->repl_flags = flags;
  836. PR_Unlock(r->repl_lock);
  837. }
  838. }
  839. void
  840. replica_get_referrals(const Replica *r, char ***referrals)
  841. {
  842. PR_Lock(r->repl_lock);
  843. replica_get_referrals_nolock (r, referrals);
  844. PR_Unlock(r->repl_lock);
  845. }
  846. void
  847. replica_set_referrals(Replica *r,const Slapi_ValueSet *vs)
  848. {
  849. int ii = 0;
  850. Slapi_Value *vv = NULL;
  851. if (r->repl_referral == NULL)
  852. {
  853. r->repl_referral = slapi_valueset_new();
  854. }
  855. else
  856. {
  857. slapi_valueset_done(r->repl_referral);
  858. }
  859. slapi_valueset_set_valueset(r->repl_referral, vs);
  860. /* make sure the DN is included in the referral LDAP URL */
  861. if (r->repl_referral)
  862. {
  863. Slapi_ValueSet *newvs = slapi_valueset_new();
  864. const char *repl_root = slapi_sdn_get_dn(r->repl_root);
  865. ii = slapi_valueset_first_value(r->repl_referral, &vv);
  866. while (vv)
  867. {
  868. const char *ref = slapi_value_get_string(vv);
  869. LDAPURLDesc *lud = NULL;
  870. (void)slapi_ldap_url_parse(ref, &lud, 0, NULL);
  871. /* see if the dn is already in the referral URL */
  872. if (!lud || !lud->lud_dn) {
  873. /* add the dn */
  874. Slapi_Value *newval = NULL;
  875. int len = strlen(ref);
  876. char *tmpref = NULL;
  877. int need_slash = 0;
  878. if (ref[len-1] != '/') {
  879. need_slash = 1;
  880. }
  881. tmpref = slapi_ch_smprintf("%s%s%s", ref, (need_slash ? "/" : ""),
  882. repl_root);
  883. newval = slapi_value_new_string(tmpref);
  884. slapi_ch_free_string(&tmpref); /* sv_new_string makes a copy */
  885. slapi_valueset_add_value(newvs, newval);
  886. slapi_value_free(&newval); /* s_vs_add_value makes a copy */
  887. }
  888. if (lud)
  889. ldap_free_urldesc(lud);
  890. ii = slapi_valueset_next_value(r->repl_referral, ii, &vv);
  891. }
  892. if (slapi_valueset_count(newvs) > 0) {
  893. slapi_valueset_done(r->repl_referral);
  894. slapi_valueset_set_valueset(r->repl_referral, newvs);
  895. }
  896. slapi_valueset_free(newvs); /* s_vs_set_vs makes a copy */
  897. }
  898. }
  899. int
  900. replica_update_csngen_state_ext (Replica *r, const RUV *ruv, const CSN *extracsn)
  901. {
  902. int rc = 0;
  903. CSNGen *gen;
  904. CSN *csn = NULL;
  905. PR_ASSERT (r && ruv);
  906. rc = ruv_get_max_csn(ruv, &csn);
  907. if (rc != RUV_SUCCESS)
  908. {
  909. return -1;
  910. }
  911. if ((csn == NULL) && (extracsn == NULL)) /* ruv contains no csn and no extra - we are done */
  912. {
  913. return 0;
  914. }
  915. if (csn_compare(extracsn, csn) > 0) /* extracsn > csn */
  916. {
  917. csn_free (&csn); /* free */
  918. csn = (CSN*)extracsn; /* use this csn to do the update */
  919. }
  920. PR_Lock(r->repl_lock);
  921. gen = (CSNGen *)object_get_data (r->repl_csngen);
  922. PR_ASSERT (gen);
  923. rc = csngen_adjust_time (gen, csn);
  924. /* rc will be either CSN_SUCCESS (0) or clock skew */
  925. /* done: */
  926. PR_Unlock(r->repl_lock);
  927. if (csn != extracsn) /* do not free the given csn */
  928. {
  929. csn_free (&csn);
  930. }
  931. return rc;
  932. }
  933. int
  934. replica_update_csngen_state (Replica *r, const RUV *ruv)
  935. {
  936. return replica_update_csngen_state_ext(r, ruv, NULL);
  937. }
  938. /*
  939. * dumps replica state for debugging purpose
  940. */
  941. void
  942. replica_dump(Replica *r)
  943. {
  944. char *updatedn_list = NULL;
  945. PR_ASSERT (r);
  946. PR_Lock(r->repl_lock);
  947. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "Replica state:\n");
  948. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica root: %s\n",
  949. slapi_sdn_get_ndn (r->repl_root));
  950. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica type: %s\n",
  951. _replica_type_as_string (r));
  952. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica id: %d\n", r->repl_rid);
  953. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tflags: %d\n", r->repl_flags);
  954. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tstate flags: %lu\n", r->repl_state_flags);
  955. if (r->updatedn_list)
  956. updatedn_list = replica_updatedn_list_to_string(r->updatedn_list, "\n\t\t");
  957. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tupdate dn: %s\n",
  958. updatedn_list? updatedn_list : "not configured");
  959. slapi_ch_free_string(&updatedn_list);
  960. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\truv: %s configured and is %sdirty\n",
  961. r->repl_ruv ? "" : "not", r->repl_ruv_dirty ? "" : "not ");
  962. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tCSN generator: %s configured\n",
  963. r->repl_csngen ? "" : "not");
  964. /* JCMREPL - Dump Referrals */
  965. PR_Unlock(r->repl_lock);
  966. }
  967. /*
  968. * Return the CSN of the purge point. Any CSNs smaller than the
  969. * purge point can be safely removed from entries within this
  970. * this replica. Returns an allocated CSN that must be freed by
  971. * the caller, or NULL if purging is disabled.
  972. */
  973. CSN *
  974. replica_get_purge_csn(const Replica *r)
  975. {
  976. CSN *csn;
  977. PR_Lock(r->repl_lock);
  978. csn= _replica_get_purge_csn_nolock(r);
  979. PR_Unlock(r->repl_lock);
  980. return csn;
  981. }
  982. /*
  983. * This function logs a dummy entry for the smallest csn in the RUV.
  984. * This is necessary because, to get the next change, we need to position
  985. * changelog on the previous change. So this function insures that we always have one.
  986. */
  987. /* ONREPL we will need to change this function to log all the
  988. * ruv elements not just the smallest when changelog iteration
  989. * algoritm changes to iterate replica by replica
  990. */
  991. int
  992. replica_log_ruv_elements (const Replica *r)
  993. {
  994. int rc = 0;
  995. PR_ASSERT (r);
  996. PR_Lock(r->repl_lock);
  997. rc = replica_log_ruv_elements_nolock (r);
  998. PR_Unlock(r->repl_lock);
  999. return rc;
  1000. }
  1001. void
  1002. consumer5_set_mapping_tree_state_for_replica(const Replica *r, RUV *supplierRuv)
  1003. {
  1004. const Slapi_DN *repl_root_sdn= replica_get_root(r);
  1005. char **ruv_referrals= NULL;
  1006. char **replica_referrals= NULL;
  1007. RUV *ruv;
  1008. int state_backend = -1;
  1009. const char *mtn_state = NULL;
  1010. PR_Lock (r->repl_lock);
  1011. if ( supplierRuv == NULL )
  1012. {
  1013. ruv = (RUV*)object_get_data (r->repl_ruv);
  1014. PR_ASSERT (ruv);
  1015. ruv_referrals= ruv_get_referrals(ruv); /* ruv_referrals has to be free'd */
  1016. }
  1017. else
  1018. {
  1019. ruv_referrals = ruv_get_referrals(supplierRuv);
  1020. }
  1021. replica_get_referrals_nolock (r, &replica_referrals); /* replica_referrals has to be free'd */
  1022. /* JCMREPL - What if there's a Total update in progress? */
  1023. if( (r->repl_type==REPLICA_TYPE_READONLY) || (r->legacy_consumer) )
  1024. {
  1025. state_backend = 0;
  1026. }
  1027. else if (r->repl_type==REPLICA_TYPE_UPDATABLE)
  1028. {
  1029. state_backend = 1;
  1030. }
  1031. /* Unlock to avoid changing MTN state under repl lock */
  1032. PR_Unlock (r->repl_lock);
  1033. if(state_backend == 0 )
  1034. {
  1035. /* Read-Only - The mapping tree should be refering all update operations. */
  1036. mtn_state = STATE_UPDATE_REFERRAL;
  1037. }
  1038. else if (state_backend == 1)
  1039. {
  1040. /* Updatable - The mapping tree should be accepting all update operations. */
  1041. mtn_state = STATE_BACKEND;
  1042. }
  1043. /* JCMREPL - Check the return code. */
  1044. repl_set_mtn_state_and_referrals(repl_root_sdn, mtn_state, NULL,
  1045. ruv_referrals, replica_referrals);
  1046. charray_free(ruv_referrals);
  1047. charray_free(replica_referrals);
  1048. }
  1049. void
  1050. replica_set_enabled (Replica *r, PRBool enable)
  1051. {
  1052. char *repl_name = NULL;
  1053. PR_ASSERT (r);
  1054. PR_Lock (r->repl_lock);
  1055. if (enable)
  1056. {
  1057. if (r->repl_eqcxt_rs == NULL) /* event is not already registered */
  1058. {
  1059. repl_name = slapi_ch_strdup (r->repl_name);
  1060. r->repl_eqcxt_rs = slapi_eq_repeat(_replica_update_state, repl_name,
  1061. current_time() + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  1062. }
  1063. }
  1064. else /* disable */
  1065. {
  1066. if (r->repl_eqcxt_rs) /* event is still registerd */
  1067. {
  1068. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  1069. slapi_ch_free ((void**)&repl_name);
  1070. slapi_eq_cancel(r->repl_eqcxt_rs);
  1071. r->repl_eqcxt_rs = NULL;
  1072. }
  1073. }
  1074. PR_Unlock (r->repl_lock);
  1075. }
  1076. /* This function is generally called when replica's data store
  1077. is reloaded. It retrieves new RUV from the datastore. If new
  1078. RUV does not exist or if it is not as up to date as the purge RUV
  1079. of the corresponding changelog file, we need to remove */
  1080. /* the function minimizes the use of replica lock where ever possible.
  1081. Locking replica lock while calling changelog functions
  1082. causes a deadlock because changelog calls replica functions that
  1083. that lock the same lock */
  1084. int
  1085. replica_reload_ruv (Replica *r)
  1086. {
  1087. int rc = 0;
  1088. Object *old_ruv_obj = NULL, *new_ruv_obj = NULL;
  1089. RUV *upper_bound_ruv = NULL;
  1090. RUV *new_ruv = NULL;
  1091. Object *r_obj;
  1092. PR_ASSERT (r);
  1093. PR_Lock (r->repl_lock);
  1094. old_ruv_obj = r->repl_ruv;
  1095. r->repl_ruv = NULL;
  1096. rc = _replica_configure_ruv (r, PR_TRUE);
  1097. PR_Unlock (r->repl_lock);
  1098. if (rc != 0)
  1099. {
  1100. return rc;
  1101. }
  1102. /* check if there is a changelog and whether this replica logs changes */
  1103. if (cl5GetState () == CL5_STATE_OPEN && r->repl_flags & REPLICA_LOG_CHANGES)
  1104. {
  1105. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1106. the existing changelog if its upper bound is the same as replica's RUV.
  1107. This is because if changelog has changes not in RUV, they will be
  1108. eventually sent to the consumer's which will cause a state mismatch
  1109. (because the supplier does not actually contain the changes in its data store.
  1110. If, on the other hand, the changelog is not as up to date as the supplier,
  1111. it is not really useful since out of sync consumer's can't be brought
  1112. up to date using this changelog and hence will need to be reinitialized */
  1113. /* replace ruv to make sure we work with the correct changelog file */
  1114. PR_Lock (r->repl_lock);
  1115. new_ruv_obj = r->repl_ruv;
  1116. r->repl_ruv = old_ruv_obj;
  1117. PR_Unlock (r->repl_lock);
  1118. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1119. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1120. {
  1121. return -1;
  1122. }
  1123. if (upper_bound_ruv)
  1124. {
  1125. new_ruv = object_get_data (new_ruv_obj);
  1126. PR_ASSERT (new_ruv);
  1127. /* ONREPL - there are more efficient ways to establish RUV equality.
  1128. However, because this is not in the critical path and we at most
  1129. have 2 elements in the RUV, this will not effect performance */
  1130. if (!ruv_covers_ruv (new_ruv, upper_bound_ruv) ||
  1131. !ruv_covers_ruv (upper_bound_ruv, new_ruv))
  1132. {
  1133. char ebuf[BUFSIZ];
  1134. /* create a temporary replica object to conform to the interface */
  1135. r_obj = object_new (r, NULL);
  1136. /* We can't use existing changelog - remove existing file */
  1137. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_reload_ruv: "
  1138. "Warning: new data for replica %s does not match the data in the changelog.\n"
  1139. " Recreating the changelog file. This could affect replication with replica's "
  1140. " consumers in which case the consumers should be reinitialized.\n",
  1141. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1142. rc = cl5DeleteDBSync (r_obj);
  1143. /* reinstate new ruv */
  1144. PR_Lock (r->repl_lock);
  1145. r->repl_ruv = new_ruv_obj;
  1146. object_release (r_obj);
  1147. if (rc == CL5_SUCCESS)
  1148. {
  1149. /* log changes to mark starting point for replication */
  1150. rc = replica_log_ruv_elements_nolock (r);
  1151. }
  1152. PR_Unlock (r->repl_lock);
  1153. }
  1154. else
  1155. {
  1156. /* we just need to reinstate new ruv */
  1157. PR_Lock (r->repl_lock);
  1158. r->repl_ruv = new_ruv_obj;
  1159. PR_Unlock (r->repl_lock);
  1160. }
  1161. }
  1162. else /* upper bound vector is not there - we have no changes logged */
  1163. {
  1164. /* reinstate new ruv */
  1165. PR_Lock (r->repl_lock);
  1166. r->repl_ruv = new_ruv_obj;
  1167. /* just log elements of the current RUV. This is to have
  1168. a starting point for iteration through the changes */
  1169. rc = replica_log_ruv_elements_nolock (r);
  1170. PR_Unlock (r->repl_lock);
  1171. }
  1172. }
  1173. if (rc == 0)
  1174. {
  1175. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1176. /* reset mapping tree referrals based on new local RUV */
  1177. }
  1178. if (old_ruv_obj)
  1179. object_release (old_ruv_obj);
  1180. if (upper_bound_ruv)
  1181. ruv_destroy (&upper_bound_ruv);
  1182. return rc;
  1183. }
  1184. /* this function is called during server startup for each replica
  1185. to check whether the replica's data was reloaded offline and
  1186. whether replica's changelog needs to be reinitialized */
  1187. /* the function does not use replica lock but all functions it calls are
  1188. thread safe. Locking replica lock while calling changelog functions
  1189. causes a deadlock because changelog calls replica functions that
  1190. that lock the same lock */
  1191. int replica_check_for_data_reload (Replica *r, void *arg)
  1192. {
  1193. int rc = 0;
  1194. RUV *upper_bound_ruv = NULL;
  1195. RUV *r_ruv = NULL;
  1196. Object *r_obj, *ruv_obj;
  1197. int cl_cover_be, be_cover_cl;
  1198. PR_ASSERT (r);
  1199. /* check that we have a changelog and if this replica logs changes */
  1200. if (cl5GetState () == CL5_STATE_OPEN && r->repl_flags & REPLICA_LOG_CHANGES)
  1201. {
  1202. /* Compare new ruv to the purge ruv. If the new contains csns which
  1203. are smaller than those in purge ruv, we need to remove old and
  1204. create new changelog file for this replica. This is because we
  1205. will not have sufficient changes to incrementally update a consumer
  1206. to the current state of the supplier. */
  1207. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1208. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1209. {
  1210. return -1;
  1211. }
  1212. if (upper_bound_ruv)
  1213. {
  1214. ruv_obj = replica_get_ruv (r);
  1215. r_ruv = object_get_data (ruv_obj);
  1216. PR_ASSERT (r_ruv);
  1217. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1218. the existing changelog if its upper bound is the same as replica's RUV.
  1219. This is because if changelog has changes not in RUV, they will be
  1220. eventually sent to the consumer's which will cause a state mismatch
  1221. (because the supplier does not actually contain the changes in its data store.
  1222. If, on the other hand, the changelog is not as up to date as the supplier,
  1223. it is not really useful since out of sync consumer's can't be brought
  1224. up to date using this changelog and hence will need to be reinitialized */
  1225. /*
  1226. * Actually we can ignore the scenario that the changelog's upper
  1227. * bound ruv covers data store's ruv for two reasons: (1) a change
  1228. * is always written to the changelog after it is committed to the
  1229. * data store; (2) a change will be ignored if the server has seen
  1230. * it before - this happens frequently at the beginning of replication
  1231. * sessions.
  1232. */
  1233. be_cover_cl = ruv_covers_ruv (r_ruv, upper_bound_ruv);
  1234. cl_cover_be = ruv_covers_ruv (upper_bound_ruv, r_ruv);
  1235. if (!cl_cover_be)
  1236. {
  1237. /* the data was reloaded and we can no longer use existing changelog */
  1238. char ebuf[BUFSIZ];
  1239. /* create a temporary replica object to conform to the interface */
  1240. r_obj = object_new (r, NULL);
  1241. /* We can't use existing changelog - remove existing file */
  1242. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_check_for_data_reload: "
  1243. "Warning: data for replica %s was reloaded and it no longer matches the data "
  1244. "in the changelog (replica data %s changelog). Recreating the changelog file. This could affect replication "
  1245. "with replica's consumers in which case the consumers should be reinitialized.\n",
  1246. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  1247. ((!be_cover_cl) ? "<>" : ">") );
  1248. rc = cl5DeleteDBSync (r_obj);
  1249. object_release (r_obj);
  1250. if (rc == CL5_SUCCESS)
  1251. {
  1252. /* log changes to mark starting point for replication */
  1253. rc = replica_log_ruv_elements (r);
  1254. }
  1255. }
  1256. object_release (ruv_obj);
  1257. }
  1258. else /* we have no changes currently logged for this replica */
  1259. {
  1260. /* log changes to mark starting point for replication */
  1261. rc = replica_log_ruv_elements (r);
  1262. }
  1263. }
  1264. if (rc == 0)
  1265. {
  1266. /* reset mapping tree referrals based on new local RUV */
  1267. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1268. }
  1269. if (upper_bound_ruv)
  1270. ruv_destroy (&upper_bound_ruv);
  1271. return rc;
  1272. }
  1273. /* Helper functions */
  1274. /* reads replica configuration entry. The entry is the child of the
  1275. mapping tree node for the replica's backend */
  1276. static Slapi_Entry*
  1277. _replica_get_config_entry (const Slapi_DN *root)
  1278. {
  1279. int rc = 0;
  1280. char *dn = NULL;
  1281. Slapi_Entry **entries;
  1282. Slapi_Entry *e = NULL;
  1283. Slapi_PBlock *pb = NULL;
  1284. dn = _replica_get_config_dn (root);
  1285. if (NULL == dn) {
  1286. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1287. "_replica_get_config_entry: failed to get the config dn for %s\n",
  1288. slapi_sdn_get_dn (root));
  1289. return NULL;
  1290. }
  1291. pb = slapi_pblock_new ();
  1292. slapi_search_internal_set_pb (pb, dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, NULL,
  1293. NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1294. slapi_search_internal_pb (pb);
  1295. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1296. if (rc == 0)
  1297. {
  1298. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1299. e = slapi_entry_dup (entries [0]);
  1300. }
  1301. slapi_free_search_results_internal(pb);
  1302. slapi_pblock_destroy (pb);
  1303. slapi_ch_free_string(&dn);
  1304. return e;
  1305. }
  1306. static int
  1307. _replica_check_validity (const Replica *r)
  1308. {
  1309. PR_ASSERT (r);
  1310. if (r->repl_root == NULL || r->repl_type == 0 || r->repl_rid == 0 ||
  1311. r->repl_csngen == NULL || r->repl_name == NULL)
  1312. {
  1313. return -1;
  1314. }
  1315. else
  1316. {
  1317. return 0;
  1318. }
  1319. }
  1320. /* replica configuration entry has the following format:
  1321. dn: cn=replica,<mapping tree node dn>
  1322. objectclass: top
  1323. objectclass: nsds5Replica
  1324. objectclass: extensibleObject
  1325. nsds5ReplicaRoot: <root of the replica>
  1326. nsds5ReplicaId: <replica id>
  1327. nsds5ReplicaType: <type of the replica: primary, read-write or read-only>
  1328. nsState: <state of the csn generator> missing the first time replica is started
  1329. nsds5ReplicaBindDN: <supplier update dn> consumers only
  1330. nsds5ReplicaReferral: <referral URL to updatable replica> consumers only
  1331. nsds5ReplicaPurgeDelay: <time, in seconds, to keep purgeable CSNs, 0 == keep forever>
  1332. nsds5ReplicaTombstonePurgeInterval: <time, in seconds, between tombstone purge runs, 0 == don't reap>
  1333. nsds5ReplicaLegacyConsumer: <TRUE | FALSE>
  1334. richm: changed slapi entry from const to editable - if the replica id is supplied for a read
  1335. only replica, we ignore it and replace the value with the READ_ONLY_REPLICA_ID
  1336. */
  1337. static int
  1338. _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext)
  1339. {
  1340. int rc;
  1341. Slapi_Attr *attr;
  1342. char *val;
  1343. CSNGen *gen;
  1344. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  1345. char *errormsg = errortext? errortext : buf;
  1346. Slapi_Attr *a = NULL;
  1347. char dnescape[BUFSIZ]; /* for escape_string */
  1348. PR_ASSERT (r && e);
  1349. /* get replica root */
  1350. val = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1351. if (val == NULL)
  1352. {
  1353. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE, "failed to retrieve %s attribute from (%s)\n",
  1354. attr_replicaRoot,
  1355. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e), dnescape));
  1356. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1357. errormsg);
  1358. return -1;
  1359. }
  1360. r->repl_root = slapi_sdn_new_dn_passin (val);
  1361. /* get replica type */
  1362. val = slapi_entry_attr_get_charptr (e, attr_replicaType);
  1363. if (val)
  1364. {
  1365. r->repl_type = atoi(val);
  1366. slapi_ch_free ((void**)&val);
  1367. }
  1368. else
  1369. {
  1370. r->repl_type = REPLICA_TYPE_READONLY;
  1371. }
  1372. /* get legacy consumer flag */
  1373. val = slapi_entry_attr_get_charptr (e, type_replicaLegacyConsumer);
  1374. if (val)
  1375. {
  1376. if (strcasecmp (val, "on") == 0 || strcasecmp (val, "yes") == 0 ||
  1377. strcasecmp (val, "true") == 0 || strcasecmp (val, "1") == 0)
  1378. {
  1379. r->legacy_consumer = PR_TRUE;
  1380. }
  1381. else
  1382. {
  1383. r->legacy_consumer = PR_FALSE;
  1384. }
  1385. slapi_ch_free ((void**)&val);
  1386. }
  1387. else
  1388. {
  1389. r->legacy_consumer = PR_FALSE;
  1390. }
  1391. /* get replica flags */
  1392. r->repl_flags = slapi_entry_attr_get_ulong(e, attr_flags);
  1393. /* get replicaid */
  1394. /* the replica id is ignored for read only replicas and is set to the
  1395. special value READ_ONLY_REPLICA_ID */
  1396. if (r->repl_type == REPLICA_TYPE_READONLY)
  1397. {
  1398. r->repl_rid = READ_ONLY_REPLICA_ID;
  1399. slapi_entry_attr_set_uint(e, attr_replicaId, (unsigned int)READ_ONLY_REPLICA_ID);
  1400. }
  1401. /* a replica id is required for updatable and primary replicas */
  1402. else if (r->repl_type == REPLICA_TYPE_UPDATABLE ||
  1403. r->repl_type == REPLICA_TYPE_PRIMARY)
  1404. {
  1405. if ((val = slapi_entry_attr_get_charptr (e, attr_replicaId)))
  1406. {
  1407. int temprid = atoi (val);
  1408. slapi_ch_free ((void**)&val);
  1409. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  1410. {
  1411. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1412. "attribute %s must have a value greater than 0 "
  1413. "and less than %d: entry %s",
  1414. attr_replicaId, READ_ONLY_REPLICA_ID,
  1415. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1416. dnescape));
  1417. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1418. "_replica_init_from_config: %s\n",
  1419. errormsg);
  1420. return -1;
  1421. }
  1422. else
  1423. {
  1424. r->repl_rid = (ReplicaId)temprid;
  1425. }
  1426. }
  1427. else
  1428. {
  1429. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1430. "failed to retrieve required %s attribute from %s",
  1431. attr_replicaId,
  1432. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1433. dnescape));
  1434. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1435. "_replica_init_from_config: %s\n",
  1436. errormsg);
  1437. return -1;
  1438. }
  1439. }
  1440. attr = NULL;
  1441. rc = slapi_entry_attr_find(e, attr_state, &attr);
  1442. gen = csngen_new (r->repl_rid, attr);
  1443. if (gen == NULL)
  1444. {
  1445. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1446. "failed to create csn generator for replica (%s)",
  1447. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1448. dnescape));
  1449. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1450. "_replica_init_from_config: %s\n",
  1451. errormsg);
  1452. return -1;
  1453. }
  1454. r->repl_csngen = object_new((void*)gen, (FNFree)csngen_free);
  1455. /* Hook generator so we can maintain min/max CSN info */
  1456. r->csn_pl_reg_id = csngen_register_callbacks(gen, assign_csn_callback, r, abort_csn_callback, r);
  1457. /* get replication bind dn */
  1458. r->updatedn_list = replica_updatedn_list_new(e);
  1459. /* get replica name */
  1460. val = slapi_entry_attr_get_charptr (e, attr_replicaName);
  1461. if (val) {
  1462. r->repl_name = val;
  1463. }
  1464. else
  1465. {
  1466. rc = slapi_uniqueIDGenerateString (&r->repl_name);
  1467. if (rc != UID_SUCCESS)
  1468. {
  1469. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1470. "failed to assign replica name for replica (%s); "
  1471. "uuid generator error - %d ",
  1472. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e), dnescape),
  1473. rc);
  1474. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1475. errormsg);
  1476. return -1;
  1477. }
  1478. else
  1479. r->new_name = PR_TRUE;
  1480. }
  1481. /* get the list of referrals */
  1482. slapi_entry_attr_find( e, attr_replicaReferral, &attr );
  1483. if(attr!=NULL)
  1484. {
  1485. slapi_attr_get_valueset(attr, &r->repl_referral);
  1486. }
  1487. /*
  1488. * Set the purge offset (default 7 days). This is the extra
  1489. * time we allow purgeable CSNs to stick around, in case a
  1490. * replica regresses. Could also be useful when LCUP happens,
  1491. * since we don't know about LCUP replicas, and they can just
  1492. * turn up whenever they want to.
  1493. */
  1494. if (slapi_entry_attr_find(e, type_replicaPurgeDelay, &a) == -1)
  1495. {
  1496. /* No purge delay provided, so use default */
  1497. r->repl_purge_delay = 60 * 60 * 24 * 7; /* One week, in seconds */
  1498. }
  1499. else
  1500. {
  1501. r->repl_purge_delay = slapi_entry_attr_get_uint(e, type_replicaPurgeDelay);
  1502. }
  1503. if (slapi_entry_attr_find(e, type_replicaTombstonePurgeInterval, &a) == -1)
  1504. {
  1505. /* No reap interval provided, so use default */
  1506. r->tombstone_reap_interval = 3600 * 24; /* One day */
  1507. }
  1508. else
  1509. {
  1510. r->tombstone_reap_interval = slapi_entry_attr_get_int(e, type_replicaTombstonePurgeInterval);
  1511. }
  1512. r->tombstone_reap_stop = r->tombstone_reap_active = PR_FALSE;
  1513. return (_replica_check_validity (r));
  1514. }
  1515. /* This function updates the entry to contain information generated
  1516. during replica initialization.
  1517. Returns 0 if successful and -1 otherwise */
  1518. static int
  1519. _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext)
  1520. {
  1521. int rc;
  1522. Slapi_Mod smod;
  1523. Slapi_Value *val;
  1524. PR_ASSERT (r);
  1525. /* add attribute that stores state of csn generator */
  1526. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  1527. if (rc != CSN_SUCCESS)
  1528. {
  1529. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to get csn generator's state; csn error - %d", rc);
  1530. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1531. "_replica_update_entry: %s\n", errortext);
  1532. return -1;
  1533. }
  1534. val = slapi_value_new_berval(slapi_mod_get_first_value(&smod));
  1535. rc = slapi_entry_add_value (e, slapi_mod_get_type (&smod), val);
  1536. slapi_value_free(&val);
  1537. slapi_mod_done (&smod);
  1538. if (rc != 0)
  1539. {
  1540. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to update replica entry");
  1541. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1542. "_replica_update_entry: %s\n", errortext);
  1543. return -1;
  1544. }
  1545. /* add attribute that stores replica name */
  1546. rc = slapi_entry_add_string (e, attr_replicaName, r->repl_name);
  1547. if (rc != 0)
  1548. {
  1549. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to update replica entry");
  1550. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1551. "_replica_update_entry: %s\n", errortext);
  1552. return -1;
  1553. }
  1554. else
  1555. r->new_name = PR_FALSE;
  1556. return 0;
  1557. }
  1558. /* DN format: cn=replica,cn=\"<root>\",cn=mapping tree,cn=config */
  1559. static char*
  1560. _replica_get_config_dn (const Slapi_DN *root)
  1561. {
  1562. char *dn;
  1563. const char *mp_base = slapi_get_mapping_tree_config_root ();
  1564. PR_ASSERT (root);
  1565. /* This function converts the old style DN to the new style. */
  1566. dn = slapi_create_dn_string("%s,cn=\"%s\",%s",
  1567. REPLICA_RDN, slapi_sdn_get_dn (root), mp_base);
  1568. return dn;
  1569. }
  1570. /* This function retrieves RUV from the root of the replicated tree.
  1571. * The attribute can be missing if
  1572. * (1) this replica is the first supplier and replica generation has not been assigned
  1573. * or
  1574. * (2) this is a consumer that has not been yet initialized
  1575. * In either case, replica_set_ruv should be used to further initialize the replica.
  1576. * Returns 0 on success, -1 on failure. If 0 is returned, the RUV is present in the replica.
  1577. */
  1578. static int
  1579. _replica_configure_ruv (Replica *r, PRBool isLocked)
  1580. {
  1581. Slapi_PBlock *pb = NULL;
  1582. char *attrs[2];
  1583. int rc;
  1584. int return_value = -1;
  1585. Slapi_Entry **entries = NULL;
  1586. Slapi_Attr *attr;
  1587. RUV *ruv = NULL;
  1588. CSN *csn = NULL;
  1589. ReplicaId rid = 0;
  1590. char ebuf[BUFSIZ];
  1591. /* read ruv state from the ruv tombstone entry */
  1592. pb = slapi_pblock_new();
  1593. attrs[0] = (char*)type_ruvElement;
  1594. attrs[1] = NULL;
  1595. slapi_search_internal_set_pb(
  1596. pb,
  1597. slapi_sdn_get_dn(r->repl_root),
  1598. LDAP_SCOPE_BASE,
  1599. "objectclass=*",
  1600. attrs,
  1601. 0, /* attrsonly */
  1602. NULL, /* controls */
  1603. RUV_STORAGE_ENTRY_UNIQUEID,
  1604. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1605. OP_FLAG_REPLICATED); /* flags */
  1606. slapi_search_internal_pb (pb);
  1607. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1608. if (rc == LDAP_SUCCESS)
  1609. {
  1610. /* get RUV attributes and construct the RUV */
  1611. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1612. if (NULL == entries || NULL == entries[0])
  1613. {
  1614. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1615. "_replica_configure_ruv: replica ruv tombstone entry for "
  1616. "replica %s not found\n",
  1617. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1618. goto done;
  1619. }
  1620. rc = slapi_entry_attr_find(entries[0], type_ruvElement, &attr);
  1621. if (rc != 0) /* ruv attribute is missing - this not allowed */
  1622. {
  1623. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1624. "_replica_configure_ruv: replica ruv tombstone entry for "
  1625. "replica %s does not contain %s\n",
  1626. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), type_ruvElement);
  1627. goto done;
  1628. }
  1629. /* Check in the tombstone we have retrieved if the local purl is
  1630. already present:
  1631. rid == 0: the local purl is not present
  1632. rid != 0: the local purl is present ==> nothing to do
  1633. */
  1634. ruv_init_from_slapi_attr_and_check_purl (attr, &ruv, &rid);
  1635. if (ruv)
  1636. {
  1637. char *generation = NULL;
  1638. generation = ruv_get_replica_generation(ruv);
  1639. if (NULL != generation)
  1640. {
  1641. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  1642. /* Is the local purl in the ruv? (the port or the host could have
  1643. changed)
  1644. */
  1645. /* A consumer only doesn't have its purl in its ruv */
  1646. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  1647. {
  1648. int need_update = 0;
  1649. if (rid == 0)
  1650. {
  1651. /* We can not have more than 1 ruv with the same rid
  1652. so we replace it */
  1653. const char *purl = NULL;
  1654. purl = multimaster_get_local_purl();
  1655. ruv_delete_replica(ruv, r->repl_rid);
  1656. ruv_add_index_replica(ruv, r->repl_rid, purl, 1);
  1657. need_update = 1; /* ruv changed, so write tombstone */
  1658. }
  1659. else /* bug 540844: make sure the local supplier rid is first in the ruv */
  1660. {
  1661. /* make sure local supplier is first in list */
  1662. ReplicaId first_rid = 0;
  1663. char *first_purl = NULL;
  1664. ruv_get_first_id_and_purl(ruv, &first_rid, &first_purl);
  1665. /* if the local supplier is not first in the list . . . */
  1666. if (rid != first_rid)
  1667. {
  1668. /* . . . move the local supplier to the beginning of the list */
  1669. ruv_move_local_supplier_to_first(ruv, rid);
  1670. need_update = 1; /* must update tombstone also */
  1671. }
  1672. }
  1673. /* Update also the directory entry */
  1674. if (need_update) {
  1675. /* richm 20010821 bug 556498
  1676. replica_replace_ruv_tombstone acquires the repl_lock, so release
  1677. the lock then reacquire it if locked */
  1678. if (isLocked) PR_Unlock(r->repl_lock);
  1679. replica_replace_ruv_tombstone(r);
  1680. if (isLocked) PR_Lock(r->repl_lock);
  1681. }
  1682. }
  1683. slapi_ch_free((void **)&generation);
  1684. return_value = 0;
  1685. }
  1686. else
  1687. {
  1688. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1689. "RUV for replica %s is missing replica generation\n",
  1690. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1691. goto done;
  1692. }
  1693. }
  1694. else
  1695. {
  1696. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1697. "Unable to convert %s attribute in entry %s to a replica update vector.\n",
  1698. type_ruvElement, escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1699. goto done;
  1700. }
  1701. }
  1702. else /* search failed */
  1703. {
  1704. if (LDAP_NO_SUCH_OBJECT == rc)
  1705. {
  1706. /* The entry doesn't exist: create it */
  1707. rc = replica_create_ruv_tombstone(r);
  1708. if (LDAP_SUCCESS != rc)
  1709. {
  1710. /*
  1711. * XXXggood - the following error appears on startup if we try
  1712. * to initialize replica RUVs before the backend instance is up.
  1713. * It's alarming to see this error, and we should suppress it
  1714. * (or avoid trying to configure it) if the backend instance is
  1715. * not yet online.
  1716. */
  1717. /*
  1718. * XXXrichm - you can also get this error when the backend is in
  1719. * read only mode c.f. bug 539782
  1720. */
  1721. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1722. "_replica_configure_ruv: failed to create replica ruv tombstone "
  1723. "entry (%s); LDAP error - %d\n",
  1724. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1725. goto done;
  1726. }
  1727. else
  1728. {
  1729. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1730. "_replica_configure_ruv: No ruv tombstone found for replica %s. "
  1731. "Created a new one\n",
  1732. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1733. return_value = 0;
  1734. }
  1735. }
  1736. else
  1737. {
  1738. /* see if the suffix is disabled */
  1739. char *state = slapi_mtn_get_state(r->repl_root);
  1740. if (state && !strcasecmp(state, "disabled"))
  1741. {
  1742. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1743. "_replica_configure_ruv: replication disabled for "
  1744. "entry (%s); LDAP error - %d\n",
  1745. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1746. slapi_ch_free_string(&state);
  1747. goto done;
  1748. }
  1749. else if (!r->repl_ruv) /* other error */
  1750. {
  1751. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1752. "_replica_configure_ruv: replication broken for "
  1753. "entry (%s); LDAP error - %d\n",
  1754. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1755. slapi_ch_free_string(&state);
  1756. goto done;
  1757. }
  1758. else /* some error but continue anyway? */
  1759. {
  1760. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1761. "_replica_configure_ruv: Error %d reading tombstone for replica %s.\n",
  1762. rc, escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1763. return_value = 0;
  1764. }
  1765. slapi_ch_free_string(&state);
  1766. }
  1767. }
  1768. if (NULL != r->min_csn_pl)
  1769. {
  1770. csnplFree (&r->min_csn_pl);
  1771. }
  1772. /* create pending list for min csn if necessary */
  1773. if (ruv_get_smallest_csn_for_replica ((RUV*)object_get_data (r->repl_ruv),
  1774. r->repl_rid, &csn) == RUV_SUCCESS)
  1775. {
  1776. csn_free (&csn);
  1777. r->min_csn_pl = NULL;
  1778. }
  1779. else
  1780. {
  1781. /*
  1782. * The local replica has not generated any of its own CSNs yet.
  1783. * We need to watch CSNs being generated and note the first
  1784. * locally-generated CSN that's committed. Once that event occurs,
  1785. * the RUV is suitable for iteration over locally generated
  1786. * changes.
  1787. */
  1788. r->min_csn_pl = csnplNew();
  1789. }
  1790. done:
  1791. if (NULL != pb)
  1792. {
  1793. slapi_free_search_results_internal(pb);
  1794. slapi_pblock_destroy (pb);
  1795. }
  1796. if (return_value != 0)
  1797. {
  1798. if (ruv)
  1799. ruv_destroy (&ruv);
  1800. }
  1801. return return_value;
  1802. }
  1803. /* NOTE - this is the only non-api function that performs locking because
  1804. it is called by the event queue */
  1805. static void
  1806. _replica_update_state (time_t when, void *arg)
  1807. {
  1808. int rc;
  1809. const char *replica_name = (const char *)arg;
  1810. Object *replica_object = NULL;
  1811. Replica *r;
  1812. Slapi_Mod smod;
  1813. LDAPMod *mods[3];
  1814. Slapi_PBlock *pb = NULL;
  1815. char *dn = NULL;
  1816. struct berval *vals[2];
  1817. struct berval val;
  1818. LDAPMod mod;
  1819. if (NULL == replica_name)
  1820. return;
  1821. /*
  1822. * replica_get_by_name() will acquire the replica object
  1823. * and that could prevent the replica from being destroyed
  1824. * until the object_release is called.
  1825. */
  1826. replica_object = replica_get_by_name(replica_name);
  1827. if (NULL == replica_object)
  1828. {
  1829. return;
  1830. }
  1831. /* We have a reference, so replica won't vanish on us. */
  1832. r = (Replica *)object_get_data(replica_object);
  1833. if (NULL == r)
  1834. {
  1835. goto done;
  1836. }
  1837. PR_Lock(r->repl_lock);
  1838. /* replica state is currently being updated
  1839. or no CSN was assigned - bail out */
  1840. if (r->state_update_inprogress)
  1841. {
  1842. PR_Unlock(r->repl_lock);
  1843. goto done;
  1844. }
  1845. /* This might be a consumer */
  1846. if (!r->repl_csn_assigned)
  1847. {
  1848. /* EY: the consumer needs to flush ruv to disk. */
  1849. PR_Unlock(r->repl_lock);
  1850. replica_write_ruv(r);
  1851. goto done;
  1852. }
  1853. /* ONREPL update csn generator state of an updatable replica only */
  1854. /* ONREPL state always changes because we update time every second and
  1855. we write state to the disk less frequently */
  1856. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  1857. if (rc != 0)
  1858. {
  1859. PR_Unlock(r->repl_lock);
  1860. goto done;
  1861. }
  1862. r->state_update_inprogress = PR_TRUE;
  1863. r->repl_csn_assigned = PR_FALSE;
  1864. dn = _replica_get_config_dn (r->repl_root);
  1865. if (NULL == dn) {
  1866. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1867. "_replica_update_state: failed to get the config dn for %s\n",
  1868. slapi_sdn_get_dn (r->repl_root));
  1869. PR_Unlock(r->repl_lock);
  1870. goto done;
  1871. }
  1872. pb = slapi_pblock_new();
  1873. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  1874. /* we don't want to held lock during operations since it causes lock contention
  1875. and sometimes deadlock. So releasing lock here */
  1876. PR_Unlock(r->repl_lock);
  1877. /* replica repl_name and new_name attributes do not get changed once
  1878. the replica is configured - so it is ok that they are outside replica lock */
  1879. /* write replica name if it has not been written before */
  1880. if (r->new_name)
  1881. {
  1882. mods[1] = &mod;
  1883. mod.mod_op = LDAP_MOD_REPLACE|LDAP_MOD_BVALUES;
  1884. mod.mod_type = (char*)attr_replicaName;
  1885. mod.mod_bvalues = vals;
  1886. vals [0] = &val;
  1887. vals [1] = NULL;
  1888. val.bv_val = r->repl_name;
  1889. val.bv_len = strlen (val.bv_val);
  1890. mods[2] = NULL;
  1891. }
  1892. else
  1893. {
  1894. mods[1] = NULL;
  1895. }
  1896. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  1897. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1898. slapi_modify_internal_pb (pb);
  1899. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1900. if (rc != LDAP_SUCCESS)
  1901. {
  1902. char ebuf[BUFSIZ];
  1903. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_update_state: "
  1904. "failed to update state of csn generator for replica %s: LDAP "
  1905. "error - %d\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1906. }
  1907. else
  1908. {
  1909. r->new_name = PR_FALSE;
  1910. }
  1911. /* update RUV - performs its own locking */
  1912. replica_write_ruv (r);
  1913. /* since this is the only place this value is changed and we are
  1914. guaranteed that only one thread enters the function, its ok
  1915. to change it outside replica lock */
  1916. r->state_update_inprogress = PR_FALSE;
  1917. slapi_ch_free ((void**)&dn);
  1918. slapi_pblock_destroy (pb);
  1919. slapi_mod_done (&smod);
  1920. done:
  1921. if (replica_object)
  1922. object_release (replica_object);
  1923. }
  1924. void
  1925. replica_write_ruv (Replica *r)
  1926. {
  1927. int rc;
  1928. Slapi_Mod smod;
  1929. Slapi_Mod smod_last_modified;
  1930. LDAPMod *mods [3];
  1931. Slapi_PBlock *pb;
  1932. PR_ASSERT(r);
  1933. PR_Lock(r->repl_lock);
  1934. if (!r->repl_ruv_dirty)
  1935. {
  1936. PR_Unlock(r->repl_lock);
  1937. return;
  1938. }
  1939. PR_ASSERT (r->repl_ruv);
  1940. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  1941. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  1942. PR_Unlock (r->repl_lock);
  1943. mods [0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  1944. mods [1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  1945. mods [2] = NULL;
  1946. pb = slapi_pblock_new();
  1947. /* replica name never changes so it is ok to reference it outside the lock */
  1948. slapi_modify_internal_set_pb(
  1949. pb,
  1950. slapi_sdn_get_dn(r->repl_root), /* only used to select be */
  1951. mods,
  1952. NULL, /* controls */
  1953. RUV_STORAGE_ENTRY_UNIQUEID,
  1954. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1955. /* Add OP_FLAG_TOMBSTONE_ENTRY so that this doesn't get logged in the Retro ChangeLog */
  1956. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_TOMBSTONE_ENTRY |
  1957. OP_FLAG_REPL_RUV );
  1958. slapi_modify_internal_pb (pb);
  1959. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1960. /* ruv does not exist - create one */
  1961. PR_Lock(r->repl_lock);
  1962. if (rc == LDAP_SUCCESS)
  1963. {
  1964. r->repl_ruv_dirty = PR_FALSE;
  1965. }
  1966. else if (rc == LDAP_NO_SUCH_OBJECT)
  1967. {
  1968. /* this includes an internal operation - but since this only happens
  1969. during server startup - its ok that we have lock around it */
  1970. rc = _replica_configure_ruv (r, PR_TRUE);
  1971. if (rc == 0)
  1972. r->repl_ruv_dirty = PR_FALSE;
  1973. }
  1974. else /* error */
  1975. {
  1976. char ebuf[BUFSIZ];
  1977. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1978. "replica_write_ruv: failed to update RUV tombstone for %s; "
  1979. "LDAP error - %d\n",
  1980. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1981. PR_ASSERT (0);
  1982. }
  1983. PR_Unlock(r->repl_lock);
  1984. slapi_mod_done (&smod);
  1985. slapi_mod_done (&smod_last_modified);
  1986. slapi_pblock_destroy (pb);
  1987. }
  1988. const CSN *
  1989. _get_deletion_csn(Slapi_Entry *e)
  1990. {
  1991. const CSN *deletion_csn = NULL;
  1992. PR_ASSERT(NULL != e);
  1993. if (NULL != e)
  1994. {
  1995. Slapi_Attr *oc_attr = NULL;
  1996. if (entry_attr_find_wsi(e, SLAPI_ATTR_OBJECTCLASS, &oc_attr) == ATTRIBUTE_PRESENT)
  1997. {
  1998. Slapi_Value *tombstone_value = NULL;
  1999. struct berval v;
  2000. v.bv_val = SLAPI_ATTR_VALUE_TOMBSTONE;
  2001. v.bv_len = strlen(SLAPI_ATTR_VALUE_TOMBSTONE);
  2002. if (attr_value_find_wsi(oc_attr, &v, &tombstone_value) == VALUE_PRESENT)
  2003. {
  2004. deletion_csn = value_get_csn(tombstone_value, CSN_TYPE_VALUE_UPDATED);
  2005. }
  2006. }
  2007. }
  2008. return deletion_csn;
  2009. }
  2010. static void
  2011. _delete_tombstone(const char *tombstone_dn, const char *uniqueid)
  2012. {
  2013. PR_ASSERT(NULL != tombstone_dn && NULL != uniqueid);
  2014. if (NULL == tombstone_dn || NULL == uniqueid)
  2015. {
  2016. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_delete_tombstone: "
  2017. "NULL tombstone_dn or uniqueid provided.\n");
  2018. }
  2019. else
  2020. {
  2021. int ldaprc;
  2022. Slapi_PBlock *pb = slapi_pblock_new();
  2023. slapi_delete_internal_set_pb(pb, tombstone_dn, NULL, /* controls */
  2024. uniqueid, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2025. OP_FLAG_TOMBSTONE_ENTRY);
  2026. slapi_delete_internal_pb(pb);
  2027. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ldaprc);
  2028. if (LDAP_SUCCESS != ldaprc)
  2029. {
  2030. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2031. "_delete_tombstone: unable to delete tombstone %s, "
  2032. "uniqueid %s: %s.\n", tombstone_dn, uniqueid,
  2033. ldap_err2string(ldaprc));
  2034. }
  2035. slapi_pblock_destroy(pb);
  2036. }
  2037. }
  2038. static
  2039. void get_reap_result (int rc, void *cb_data)
  2040. {
  2041. PR_ASSERT (cb_data);
  2042. ((reap_callback_data*)cb_data)->rc = rc;
  2043. }
  2044. static
  2045. int process_reap_entry (Slapi_Entry *entry, void *cb_data)
  2046. {
  2047. char ebuf[BUFSIZ];
  2048. char deletion_csn_str[CSN_STRSIZE];
  2049. char purge_csn_str[CSN_STRSIZE];
  2050. unsigned long *num_entriesp = &((reap_callback_data *)cb_data)->num_entries;
  2051. unsigned long *num_purged_entriesp = &((reap_callback_data *)cb_data)->num_purged_entries;
  2052. CSN *purge_csn = ((reap_callback_data *)cb_data)->purge_csn;
  2053. /* this is a pointer into the actual value in the Replica object - so that
  2054. if the value is set in the replica, we will know about it immediately */
  2055. PRBool *tombstone_reap_stop = ((reap_callback_data *)cb_data)->tombstone_reap_stop;
  2056. const CSN *deletion_csn = NULL;
  2057. /* abort reaping if we've been told to stop or we're shutting down */
  2058. if (*tombstone_reap_stop || g_get_shutdown()) {
  2059. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2060. "_replica_reap_tombstones: the tombstone reap process "
  2061. " has been stopped\n");
  2062. return -1;
  2063. }
  2064. /* we only ask for the objectclass in the search - the deletion csn is in the
  2065. objectclass attribute values - if we need more attributes returned by the
  2066. search in the future, see _replica_reap_tombstones below and add more to the
  2067. attrs array */
  2068. deletion_csn = _get_deletion_csn(entry);
  2069. if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
  2070. (!is_ruv_tombstone_entry(entry))) {
  2071. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2072. "_replica_reap_tombstones: removing tombstone %s "
  2073. "because its deletion csn (%s) is less than the "
  2074. "purge csn (%s).\n",
  2075. escape_string(slapi_entry_get_dn(entry), ebuf),
  2076. csn_as_string(deletion_csn, PR_FALSE, deletion_csn_str),
  2077. csn_as_string(purge_csn, PR_FALSE, purge_csn_str));
  2078. _delete_tombstone(slapi_entry_get_dn(entry),
  2079. slapi_entry_get_uniqueid(entry));
  2080. (*num_purged_entriesp)++;
  2081. }
  2082. else {
  2083. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2084. "_replica_reap_tombstones: NOT removing tombstone "
  2085. "%s\n", escape_string(slapi_entry_get_dn(entry),ebuf));
  2086. }
  2087. (*num_entriesp)++;
  2088. return 0;
  2089. }
  2090. /* This does the actual work of searching for tombstones and deleting them.
  2091. This must be called in a separate thread because it may take a long time.
  2092. */
  2093. static void
  2094. _replica_reap_tombstones(void *arg)
  2095. {
  2096. const char *replica_name = (const char *)arg;
  2097. Slapi_PBlock *pb = NULL;
  2098. Object *replica_object = NULL;
  2099. Replica *replica = NULL;
  2100. CSN *purge_csn = NULL;
  2101. char ebuf[BUFSIZ];
  2102. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2103. "Info: Beginning tombstone reap for replica %s.\n",
  2104. replica_name ? replica_name : "(null)");
  2105. if (NULL == replica_name)
  2106. {
  2107. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2108. "Warning: Replica name is null in tombstone reap\n");
  2109. goto done;
  2110. }
  2111. /*
  2112. * replica_get_by_name() will acquire the replica object
  2113. * and that could prevent the replica from being destroyed
  2114. * until the object_release is called.
  2115. */
  2116. replica_object = replica_get_by_name(replica_name);
  2117. if (NULL == replica_object)
  2118. {
  2119. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2120. "Warning: Replica object %s is null in tombstone reap\n", replica_name);
  2121. goto done;
  2122. }
  2123. /* We have a reference, so replica won't vanish on us. */
  2124. replica = (Replica *)object_get_data(replica_object);
  2125. if (NULL == replica)
  2126. {
  2127. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2128. "Warning: Replica %s is null in tombstone reap\n", replica_name);
  2129. goto done;
  2130. }
  2131. if (replica->tombstone_reap_stop)
  2132. {
  2133. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2134. "Info: Replica %s reap stop flag is set for tombstone reap\n", replica_name);
  2135. goto done;
  2136. }
  2137. purge_csn = replica_get_purge_csn(replica);
  2138. if (NULL != purge_csn)
  2139. {
  2140. LDAPControl **ctrls;
  2141. int oprc;
  2142. reap_callback_data cb_data;
  2143. char **attrs = NULL;
  2144. /* we just need the objectclass - for the deletion csn
  2145. and the dn and nsuniqueid - for possible deletion
  2146. saves time to return only 2 attrs
  2147. */
  2148. charray_add(&attrs, slapi_ch_strdup("objectclass"));
  2149. charray_add(&attrs, slapi_ch_strdup("nsuniqueid"));
  2150. ctrls = (LDAPControl **)slapi_ch_calloc (3, sizeof (LDAPControl *));
  2151. ctrls[0] = create_managedsait_control();
  2152. ctrls[1] = create_backend_control(replica->repl_root);
  2153. ctrls[2] = NULL;
  2154. pb = slapi_pblock_new();
  2155. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(replica->repl_root),
  2156. LDAP_SCOPE_SUBTREE, "(&(objectclass=nstombstone)(nscpentrydn=*))",
  2157. attrs, 0, ctrls, NULL,
  2158. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  2159. cb_data.rc = 0;
  2160. cb_data.num_entries = 0UL;
  2161. cb_data.num_purged_entries = 0UL;
  2162. cb_data.purge_csn = purge_csn;
  2163. /* set the cb data pointer to point to the actual memory address in
  2164. the actual Replica object - so that when the value in the Replica
  2165. is set, the reap process will know about it immediately */
  2166. cb_data.tombstone_reap_stop = &(replica->tombstone_reap_stop);
  2167. slapi_search_internal_callback_pb (pb, &cb_data /* callback data */,
  2168. get_reap_result /* result callback */,
  2169. process_reap_entry /* entry callback */,
  2170. NULL /* referral callback*/);
  2171. charray_free(attrs);
  2172. oprc = cb_data.rc;
  2173. if (LDAP_SUCCESS != oprc)
  2174. {
  2175. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2176. "_replica_reap_tombstones: failed when searching for "
  2177. "tombstones in replica %s: %s. Will try again in %ld "
  2178. "seconds.\n", escape_string(slapi_sdn_get_dn(replica->repl_root),ebuf),
  2179. ldap_err2string(oprc), replica->tombstone_reap_interval);
  2180. }
  2181. else
  2182. {
  2183. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2184. "_replica_reap_tombstones: purged %ld of %ld tombstones "
  2185. "in replica %s. Will try again in %ld "
  2186. "seconds.\n", cb_data.num_purged_entries, cb_data.num_entries,
  2187. escape_string(slapi_sdn_get_dn(replica->repl_root),ebuf),
  2188. replica->tombstone_reap_interval);
  2189. }
  2190. }
  2191. else
  2192. {
  2193. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2194. "Info: No purge CSN for tombstone reap for replica %s.\n",
  2195. replica_name);
  2196. }
  2197. done:
  2198. if (replica)
  2199. {
  2200. PR_Lock(replica->repl_lock);
  2201. replica->tombstone_reap_active = PR_FALSE;
  2202. PR_Unlock(replica->repl_lock);
  2203. }
  2204. if (NULL != purge_csn)
  2205. {
  2206. csn_free(&purge_csn);
  2207. }
  2208. if (NULL != pb)
  2209. {
  2210. slapi_free_search_results_internal(pb);
  2211. slapi_pblock_destroy(pb);
  2212. }
  2213. if (NULL != replica_object)
  2214. {
  2215. object_release(replica_object);
  2216. replica_object = NULL;
  2217. replica = NULL;
  2218. }
  2219. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2220. "Info: Finished tombstone reap for replica %s.\n",
  2221. replica_name ? replica_name : "(null)");
  2222. }
  2223. /*
  2224. We don't want to run the reaper function directly from the event
  2225. queue since it may hog the event queue, starving other events.
  2226. See bug 604441
  2227. The function eq_cb_reap_tombstones will fire off the actual thread
  2228. that does the real work.
  2229. */
  2230. static void
  2231. eq_cb_reap_tombstones(time_t when, void *arg)
  2232. {
  2233. const char *replica_name = (const char *)arg;
  2234. Object *replica_object = NULL;
  2235. Replica *replica = NULL;
  2236. if (NULL != replica_name)
  2237. {
  2238. /*
  2239. * replica_get_by_name() will acquire the replica object
  2240. * and that could prevent the replica from being destroyed
  2241. * until the object_release is called.
  2242. */
  2243. replica_object = replica_get_by_name(replica_name);
  2244. if (NULL != replica_object)
  2245. {
  2246. /* We have a reference, so replica won't vanish on us. */
  2247. replica = (Replica *)object_get_data(replica_object);
  2248. if (replica)
  2249. {
  2250. PR_Lock(replica->repl_lock);
  2251. /* No action if purge is disabled or the previous purge is not done yet */
  2252. if (replica->tombstone_reap_interval != 0 &&
  2253. replica->tombstone_reap_active == PR_FALSE)
  2254. {
  2255. /* set the flag here to minimize race conditions */
  2256. replica->tombstone_reap_active = PR_TRUE;
  2257. if (PR_CreateThread(PR_USER_THREAD,
  2258. _replica_reap_tombstones, (void *)replica_name,
  2259. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  2260. SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  2261. {
  2262. replica->tombstone_reap_active = PR_FALSE;
  2263. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2264. "Error: unable to create the tombstone reap thread for replica %s. "
  2265. "Possible system resources problem\n",
  2266. replica_name);
  2267. }
  2268. }
  2269. /* reap thread will wait until this lock is released */
  2270. PR_Unlock(replica->repl_lock);
  2271. }
  2272. object_release(replica_object);
  2273. replica_object = NULL;
  2274. replica = NULL;
  2275. }
  2276. }
  2277. }
  2278. static char *
  2279. _replica_type_as_string (const Replica *r)
  2280. {
  2281. switch (r->repl_type)
  2282. {
  2283. case REPLICA_TYPE_PRIMARY: return "primary";
  2284. case REPLICA_TYPE_READONLY: return "read-only";
  2285. case REPLICA_TYPE_UPDATABLE: return "updatable";
  2286. default: return "unknown";
  2287. }
  2288. }
  2289. static const char *root_glue =
  2290. "dn: %s\n"
  2291. "objectclass: top\n"
  2292. "objectclass: nsTombstone\n"
  2293. "objectclass: extensibleobject\n"
  2294. "nsuniqueid: %s\n";
  2295. static int
  2296. replica_create_ruv_tombstone(Replica *r)
  2297. {
  2298. int return_value = LDAP_LOCAL_ERROR;
  2299. char *root_entry_str;
  2300. Slapi_Entry *e = NULL;
  2301. const char *purl = NULL;
  2302. RUV *ruv;
  2303. struct berval **bvals = NULL;
  2304. Slapi_PBlock *pb = NULL;
  2305. int rc;
  2306. char ebuf[BUFSIZ];
  2307. PR_ASSERT(NULL != r && NULL != r->repl_root);
  2308. root_entry_str = slapi_ch_smprintf(root_glue, slapi_sdn_get_ndn(r->repl_root),
  2309. RUV_STORAGE_ENTRY_UNIQUEID);
  2310. e = slapi_str2entry(root_entry_str, SLAPI_STR2ENTRY_TOMBSTONE_CHECK);
  2311. if (e == NULL)
  2312. goto done;
  2313. /* Add ruv */
  2314. if (r->repl_ruv == NULL)
  2315. {
  2316. CSNGen *gen;
  2317. CSN *csn;
  2318. char csnstr [CSN_STRSIZE];
  2319. /* first attempt to write RUV tombstone - need to create RUV */
  2320. gen = (CSNGen *)object_get_data(r->repl_csngen);
  2321. PR_ASSERT (gen);
  2322. if (csngen_new_csn(gen, &csn, PR_FALSE /* notify */) == CSN_SUCCESS)
  2323. {
  2324. (void)csn_as_string(csn, PR_FALSE, csnstr);
  2325. csn_free(&csn);
  2326. /* if this is an updateable replica - add its own
  2327. element to the RUV so that referrals work correctly */
  2328. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  2329. purl = multimaster_get_local_purl();
  2330. if (ruv_init_new(csnstr, r->repl_rid, purl, &ruv) == RUV_SUCCESS)
  2331. {
  2332. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  2333. r->repl_ruv_dirty = PR_TRUE;
  2334. return_value = LDAP_SUCCESS;
  2335. }
  2336. else
  2337. {
  2338. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2339. "Cannot create new replica update vector for %s\n",
  2340. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  2341. goto done;
  2342. }
  2343. }
  2344. else
  2345. {
  2346. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2347. "Cannot obtain CSN for new replica update vector for %s\n",
  2348. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  2349. goto done;
  2350. }
  2351. }
  2352. else /* failed to write the entry because DB was not initialized - retry */
  2353. {
  2354. ruv = (RUV*) object_get_data (r->repl_ruv);
  2355. PR_ASSERT (ruv);
  2356. }
  2357. PR_ASSERT (r->repl_ruv);
  2358. rc = ruv_to_bervals(ruv, &bvals);
  2359. if (rc != RUV_SUCCESS)
  2360. {
  2361. goto done;
  2362. }
  2363. /* ONREPL this is depricated function but there is currently no better API to use */
  2364. rc = slapi_entry_add_values(e, type_ruvElement, bvals);
  2365. if (rc != 0)
  2366. {
  2367. goto done;
  2368. }
  2369. pb = slapi_pblock_new();
  2370. slapi_add_entry_internal_set_pb(
  2371. pb,
  2372. e,
  2373. NULL /* controls */,
  2374. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2375. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP |
  2376. OP_FLAG_REPL_RUV);
  2377. slapi_add_internal_pb(pb);
  2378. e = NULL; /* add consumes e, upon success or failure */
  2379. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2380. if (return_value == LDAP_SUCCESS)
  2381. r->repl_ruv_dirty = PR_FALSE;
  2382. done:
  2383. slapi_entry_free (e);
  2384. if (bvals)
  2385. ber_bvecfree(bvals);
  2386. if (pb)
  2387. slapi_pblock_destroy(pb);
  2388. slapi_ch_free((void **) &root_entry_str);
  2389. return return_value;
  2390. }
  2391. static void
  2392. assign_csn_callback(const CSN *csn, void *data)
  2393. {
  2394. Replica *r = (Replica *)data;
  2395. Object *ruv_obj;
  2396. RUV *ruv;
  2397. PR_ASSERT(NULL != csn);
  2398. PR_ASSERT(NULL != r);
  2399. ruv_obj = replica_get_ruv (r);
  2400. PR_ASSERT (ruv_obj);
  2401. ruv = (RUV*)object_get_data (ruv_obj);
  2402. PR_ASSERT (ruv);
  2403. PR_Lock(r->repl_lock);
  2404. r->repl_csn_assigned = PR_TRUE;
  2405. if (NULL != r->min_csn_pl)
  2406. {
  2407. if (csnplInsert(r->min_csn_pl, csn) != 0)
  2408. {
  2409. char ebuf[BUFSIZ];
  2410. char csn_str[CSN_STRSIZE]; /* For logging only */
  2411. /* Ack, we can't keep track of min csn. Punt. */
  2412. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "assign_csn_callback: "
  2413. "failed to insert csn %s for replica %s\n",
  2414. csn_as_string(csn, PR_FALSE, csn_str),
  2415. escape_string(slapi_sdn_get_dn(r->repl_root), ebuf));
  2416. csnplFree(&r->min_csn_pl);
  2417. }
  2418. }
  2419. ruv_add_csn_inprogress (ruv, csn);
  2420. PR_Unlock(r->repl_lock);
  2421. object_release (ruv_obj);
  2422. }
  2423. static void
  2424. abort_csn_callback(const CSN *csn, void *data)
  2425. {
  2426. Replica *r = (Replica *)data;
  2427. Object *ruv_obj;
  2428. RUV *ruv;
  2429. int rc;
  2430. PR_ASSERT(NULL != csn);
  2431. PR_ASSERT(NULL != data);
  2432. ruv_obj = replica_get_ruv (r);
  2433. PR_ASSERT (ruv_obj);
  2434. ruv = (RUV*)object_get_data (ruv_obj);
  2435. PR_ASSERT (ruv);
  2436. PR_Lock(r->repl_lock);
  2437. if (NULL != r->min_csn_pl)
  2438. {
  2439. rc = csnplRemove(r->min_csn_pl, csn);
  2440. PR_ASSERT(rc == 0);
  2441. }
  2442. ruv_cancel_csn_inprogress (ruv, csn);
  2443. PR_Unlock(r->repl_lock);
  2444. object_release (ruv_obj);
  2445. }
  2446. static CSN *
  2447. _replica_get_purge_csn_nolock(const Replica *r)
  2448. {
  2449. CSN *purge_csn = NULL;
  2450. CSN **csns = NULL;
  2451. RUV *ruv;
  2452. int i;
  2453. if (r->repl_purge_delay > 0)
  2454. {
  2455. /* get a sorted list of all maxcsns in ruv in ascend order */
  2456. object_acquire(r->repl_ruv);
  2457. ruv = object_get_data(r->repl_ruv);
  2458. csns = cl5BuildCSNList (ruv, NULL);
  2459. object_release(r->repl_ruv);
  2460. if (csns == NULL)
  2461. return NULL;
  2462. /* locate the most recent maxcsn in the csn list */
  2463. for (i = 0; csns[i]; i++);
  2464. purge_csn = csn_dup (csns[i-1]);
  2465. /* set purge_csn to the most recent maxcsn - purge_delay */
  2466. if ((csn_get_time(purge_csn) - r->repl_purge_delay) > 0)
  2467. csn_set_time(purge_csn, csn_get_time(purge_csn) - r->repl_purge_delay);
  2468. }
  2469. if (csns)
  2470. cl5DestroyCSNList (&csns);
  2471. return purge_csn;
  2472. }
  2473. static void
  2474. replica_get_referrals_nolock (const Replica *r, char ***referrals)
  2475. {
  2476. if(referrals!=NULL)
  2477. {
  2478. int hint;
  2479. int i= 0;
  2480. Slapi_Value *v= NULL;
  2481. if (NULL == r->repl_referral)
  2482. {
  2483. *referrals = NULL;
  2484. }
  2485. else
  2486. {
  2487. /* richm: +1 for trailing NULL */
  2488. *referrals= (char**)slapi_ch_calloc(sizeof(char*),1+slapi_valueset_count(r->repl_referral));
  2489. hint= slapi_valueset_first_value( r->repl_referral, &v );
  2490. while(v!=NULL)
  2491. {
  2492. const char *s= slapi_value_get_string(v);
  2493. if(s!=NULL && s[0]!='\0')
  2494. {
  2495. (*referrals)[i]= slapi_ch_strdup(s);
  2496. i++;
  2497. }
  2498. hint= slapi_valueset_next_value( r->repl_referral, hint, &v);
  2499. }
  2500. (*referrals)[i] = NULL;
  2501. }
  2502. }
  2503. }
  2504. static void
  2505. replica_clear_legacy_referrals(const Slapi_DN *repl_root_sdn,
  2506. char **referrals, const char *state)
  2507. {
  2508. repl_set_mtn_state_and_referrals(repl_root_sdn, state, NULL, NULL, referrals);
  2509. }
  2510. static void
  2511. replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr)
  2512. {
  2513. Slapi_PBlock *pb;
  2514. Slapi_Mods smods;
  2515. LDAPControl **ctrls;
  2516. int rc;
  2517. pb = slapi_pblock_new ();
  2518. slapi_mods_init(&smods, 1);
  2519. slapi_mods_add(&smods, LDAP_MOD_DELETE, attr, 0, NULL);
  2520. ctrls = (LDAPControl**)slapi_ch_malloc (2 * sizeof (LDAPControl*));
  2521. ctrls[0] = create_managedsait_control ();
  2522. ctrls[1] = NULL;
  2523. /* remove copiedFrom/copyingFrom first */
  2524. slapi_modify_internal_set_pb (pb, slapi_sdn_get_dn (repl_root_sdn),
  2525. slapi_mods_get_ldapmods_passout (&smods), ctrls,
  2526. NULL /*uniqueid */,
  2527. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION) ,
  2528. 0 /* operation_flags */);
  2529. slapi_modify_internal_pb (pb);
  2530. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2531. if (rc != LDAP_SUCCESS)
  2532. {
  2533. char ebuf[BUFSIZ];
  2534. /* this is not a fatal error because the attribute may not be there */
  2535. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_remove_legacy_attr: "
  2536. "failed to remove legacy attribute %s for replica %s; LDAP error - %d\n",
  2537. attr, escape_string(slapi_sdn_get_dn(repl_root_sdn),ebuf), rc);
  2538. }
  2539. slapi_mods_done (&smods);
  2540. slapi_pblock_destroy (pb);
  2541. }
  2542. static int
  2543. replica_log_ruv_elements_nolock (const Replica *r)
  2544. {
  2545. int rc = 0;
  2546. slapi_operation_parameters op_params;
  2547. RUV *ruv;
  2548. char *repl_gen;
  2549. CSN *csn = NULL;
  2550. ruv = (RUV*) object_get_data (r->repl_ruv);
  2551. PR_ASSERT (ruv);
  2552. if ((ruv_get_min_csn(ruv, &csn) == RUV_SUCCESS) && csn)
  2553. {
  2554. /* we log it as a delete operation to have the least number of fields
  2555. to set. the entry can be identified by a special target uniqueid and
  2556. special target dn */
  2557. memset (&op_params, 0, sizeof (op_params));
  2558. op_params.operation_type = SLAPI_OPERATION_DELETE;
  2559. op_params.target_address.dn = START_ITERATION_ENTRY_DN;
  2560. op_params.target_address.uniqueid = START_ITERATION_ENTRY_UNIQUEID;
  2561. op_params.csn = csn;
  2562. repl_gen = ruv_get_replica_generation (ruv);
  2563. rc = cl5WriteOperation(r->repl_name, repl_gen, &op_params, PR_FALSE);
  2564. if (rc == CL5_SUCCESS)
  2565. rc = 0;
  2566. else
  2567. rc = -1;
  2568. slapi_ch_free ((void**)&repl_gen);
  2569. csn_free (&csn);
  2570. }
  2571. return rc;
  2572. }
  2573. void
  2574. replica_set_purge_delay(Replica *r, PRUint32 purge_delay)
  2575. {
  2576. PR_ASSERT(r);
  2577. PR_Lock(r->repl_lock);
  2578. r->repl_purge_delay = purge_delay;
  2579. PR_Unlock(r->repl_lock);
  2580. }
  2581. void
  2582. replica_set_tombstone_reap_interval (Replica *r, long interval)
  2583. {
  2584. char *repl_name;
  2585. PR_Lock(r->repl_lock);
  2586. /*
  2587. * Leave the event there to purge the existing tombstones
  2588. * if we are about to turn off tombstone creation
  2589. */
  2590. if (interval > 0 && r->repl_eqcxt_tr && r->tombstone_reap_interval != interval)
  2591. {
  2592. int found;
  2593. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  2594. slapi_ch_free ((void**)&repl_name);
  2595. found = slapi_eq_cancel (r->repl_eqcxt_tr);
  2596. slapi_log_error (SLAPI_LOG_REPL, NULL,
  2597. "tombstone_reap event (interval=%ld) was %s\n",
  2598. r->tombstone_reap_interval, (found ? "cancelled" : "not found"));
  2599. r->repl_eqcxt_tr = NULL;
  2600. }
  2601. r->tombstone_reap_interval = interval;
  2602. if ( interval > 0 && r->repl_eqcxt_tr == NULL )
  2603. {
  2604. repl_name = slapi_ch_strdup (r->repl_name);
  2605. r->repl_eqcxt_tr = slapi_eq_repeat (eq_cb_reap_tombstones, repl_name,
  2606. current_time() + r->tombstone_reap_interval,
  2607. 1000 * r->tombstone_reap_interval);
  2608. slapi_log_error (SLAPI_LOG_REPL, NULL,
  2609. "tombstone_reap event (interval=%ld) was %s\n",
  2610. r->tombstone_reap_interval, (r->repl_eqcxt_tr ? "scheduled" : "not scheduled successfully"));
  2611. }
  2612. PR_Unlock(r->repl_lock);
  2613. }
  2614. /* Update the tombstone entry to reflect the content of the ruv */
  2615. static void
  2616. replica_replace_ruv_tombstone(Replica *r)
  2617. {
  2618. Slapi_PBlock *pb = NULL;
  2619. char *dn;
  2620. int rc;
  2621. Slapi_Mod smod;
  2622. Slapi_Mod smod_last_modified;
  2623. LDAPMod *mods [3];
  2624. PR_ASSERT(NULL != r && NULL != r->repl_root);
  2625. PR_Lock(r->repl_lock);
  2626. PR_ASSERT (r->repl_ruv);
  2627. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  2628. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  2629. dn = _replica_get_config_dn (r->repl_root);
  2630. if (NULL == dn) {
  2631. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2632. "replica_replace_ruv_tombstone: "
  2633. "failed to get the config dn for %s\n",
  2634. slapi_sdn_get_dn (r->repl_root));
  2635. PR_Unlock(r->repl_lock);
  2636. goto bail;
  2637. }
  2638. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  2639. mods[1] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  2640. PR_Unlock (r->repl_lock);
  2641. mods [2] = NULL;
  2642. pb = slapi_pblock_new();
  2643. slapi_modify_internal_set_pb(
  2644. pb,
  2645. (char*)slapi_sdn_get_dn (r->repl_root), /* only used to select be */
  2646. mods,
  2647. NULL, /* controls */
  2648. RUV_STORAGE_ENTRY_UNIQUEID,
  2649. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2650. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_REPL_RUV);
  2651. slapi_modify_internal_pb (pb);
  2652. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2653. if (rc != LDAP_SUCCESS)
  2654. {
  2655. if ((rc != LDAP_NO_SUCH_OBJECT) || !replica_is_state_flag_set(r, REPLICA_IN_USE))
  2656. {
  2657. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_replace_ruv_tombstone: "
  2658. "failed to update replication update vector for replica %s: LDAP "
  2659. "error - %d\n", (char*)slapi_sdn_get_dn (r->repl_root), rc);
  2660. }
  2661. }
  2662. slapi_ch_free ((void**)&dn);
  2663. slapi_pblock_destroy (pb);
  2664. bail:
  2665. slapi_mod_done (&smod);
  2666. slapi_mod_done (&smod_last_modified);
  2667. }
  2668. void
  2669. replica_update_ruv_consumer(Replica *r, RUV *supplier_ruv)
  2670. {
  2671. ReplicaId supplier_id = 0;
  2672. char *supplier_purl = NULL;
  2673. if ( ruv_get_first_id_and_purl(supplier_ruv, &supplier_id, &supplier_purl) == RUV_SUCCESS )
  2674. {
  2675. RUV *local_ruv = NULL;
  2676. PR_Lock(r->repl_lock);
  2677. local_ruv = (RUV*)object_get_data (r->repl_ruv);
  2678. PR_ASSERT (local_ruv);
  2679. if ( ruv_local_contains_supplier(local_ruv, supplier_id) == 0 )
  2680. {
  2681. if ( r->repl_type == REPLICA_TYPE_UPDATABLE )
  2682. {
  2683. /* Add the new ruv right after the consumer own purl */
  2684. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 2);
  2685. }
  2686. else
  2687. {
  2688. /* This is a consumer only, add it first */
  2689. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 1);
  2690. }
  2691. }
  2692. else
  2693. {
  2694. /* Replace it */
  2695. ruv_replace_replica_purl(local_ruv, supplier_id, supplier_purl);
  2696. }
  2697. PR_Unlock(r->repl_lock);
  2698. /* Update also the directory entry */
  2699. replica_replace_ruv_tombstone(r);
  2700. }
  2701. }
  2702. void
  2703. replica_set_ruv_dirty(Replica *r)
  2704. {
  2705. PR_ASSERT(r);
  2706. PR_Lock(r->repl_lock);
  2707. r->repl_ruv_dirty = PR_TRUE;
  2708. PR_Unlock(r->repl_lock);
  2709. }
  2710. PRBool
  2711. replica_is_state_flag_set(Replica *r, PRInt32 flag)
  2712. {
  2713. PR_ASSERT(r);
  2714. if (r)
  2715. return (r->repl_state_flags & flag);
  2716. else
  2717. return PR_FALSE;
  2718. }
  2719. void
  2720. replica_set_state_flag (Replica *r, PRUint32 flag, PRBool clear)
  2721. {
  2722. if (r == NULL)
  2723. return;
  2724. PR_Lock(r->repl_lock);
  2725. if (clear)
  2726. {
  2727. r->repl_state_flags &= ~flag;
  2728. }
  2729. else
  2730. {
  2731. r->repl_state_flags |= flag;
  2732. }
  2733. PR_Unlock(r->repl_lock);
  2734. }
  2735. /**
  2736. * Use this to tell the tombstone reap process to stop. This will
  2737. * typically be used when we (consumer) get a request to do a
  2738. * total update.
  2739. */
  2740. void
  2741. replica_set_tombstone_reap_stop(Replica *r, PRBool val)
  2742. {
  2743. if (r == NULL)
  2744. return;
  2745. PR_Lock(r->repl_lock);
  2746. r->tombstone_reap_stop = val;
  2747. PR_Unlock(r->repl_lock);
  2748. }
  2749. /* replica just came back online, probably after data was reloaded */
  2750. void
  2751. replica_enable_replication (Replica *r)
  2752. {
  2753. int rc;
  2754. PR_ASSERT(r);
  2755. /* prevent creation of new agreements until the replica is enabled */
  2756. PR_Lock(r->agmt_lock);
  2757. /* retrieve new ruv */
  2758. rc = replica_reload_ruv (r);
  2759. if (rc) {
  2760. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_enable_replication: "
  2761. "reloading ruv failed\n");
  2762. /* What to do ? */
  2763. }
  2764. /* Replica came back online, Check if the total update was terminated.
  2765. If flag is still set, it was not terminated, therefore the data is
  2766. very likely to be incorrect, and we should not restart Replication threads...
  2767. */
  2768. if (!replica_is_state_flag_set(r, REPLICA_TOTAL_IN_PROGRESS)){
  2769. /* restart outbound replication */
  2770. start_agreements_for_replica (r, PR_TRUE);
  2771. /* enable ruv state update */
  2772. replica_set_enabled (r, PR_TRUE);
  2773. }
  2774. /* mark the replica as being available for updates */
  2775. replica_relinquish_exclusive_access(r, 0, 0);
  2776. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_TRUE /* clear */);
  2777. PR_Unlock(r->agmt_lock);
  2778. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_enable_replication: "
  2779. "replica %s is relinquished\n",
  2780. slapi_sdn_get_ndn (replica_get_root (r)));
  2781. }
  2782. /* replica is about to be taken offline */
  2783. void
  2784. replica_disable_replication (Replica *r, Object *r_obj)
  2785. {
  2786. char *current_purl = NULL;
  2787. char *p_locking_purl = NULL;
  2788. char *locking_purl = NULL;
  2789. ReplicaId junkrid;
  2790. PRBool isInc = PR_FALSE; /* get exclusive access, but not for inc update */
  2791. RUV *repl_ruv = NULL;
  2792. /* prevent creation of new agreements until the replica is disabled */
  2793. PR_Lock(r->agmt_lock);
  2794. /* stop ruv update */
  2795. replica_set_enabled (r, PR_FALSE);
  2796. /* disable outbound replication */
  2797. start_agreements_for_replica (r, PR_FALSE);
  2798. /* close the corresponding changelog file */
  2799. /* close_changelog_for_replica (r_obj); */
  2800. /* mark the replica as being unavailable for updates */
  2801. /* If an incremental update is in progress, we want to wait until it is
  2802. finished until we get exclusive access to the replica, because we have
  2803. to make sure no operations are in progress - it messes up replication
  2804. when a restore is in progress but we are still adding replicated entries
  2805. from a supplier
  2806. */
  2807. repl_ruv = (RUV*) object_get_data (r->repl_ruv);
  2808. ruv_get_first_id_and_purl(repl_ruv, &junkrid, &p_locking_purl);
  2809. locking_purl = slapi_ch_strdup(p_locking_purl);
  2810. p_locking_purl = NULL;
  2811. repl_ruv = NULL;
  2812. while (!replica_get_exclusive_access(r, &isInc, 0, 0, "replica_disable_replication",
  2813. &current_purl)) {
  2814. if (!isInc) /* already locked, but not by inc update - break */
  2815. break;
  2816. isInc = PR_FALSE;
  2817. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2818. "replica_disable_replication: "
  2819. "replica %s is already locked by (%s) for incoming "
  2820. "incremental update; sleeping 100ms\n",
  2821. slapi_sdn_get_ndn (replica_get_root (r)),
  2822. current_purl ? current_purl : "unknown");
  2823. slapi_ch_free_string(&current_purl);
  2824. DS_Sleep(PR_MillisecondsToInterval(100));
  2825. }
  2826. slapi_ch_free_string(&current_purl);
  2827. slapi_ch_free_string(&locking_purl);
  2828. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_FALSE);
  2829. PR_Unlock(r->agmt_lock);
  2830. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_disable_replication: "
  2831. "replica %s is acquired\n",
  2832. slapi_sdn_get_ndn (replica_get_root (r)));
  2833. }
  2834. static void
  2835. start_agreements_for_replica (Replica *r, PRBool start)
  2836. {
  2837. Object *agmt_obj;
  2838. Repl_Agmt *agmt;
  2839. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  2840. while (agmt_obj)
  2841. {
  2842. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2843. PR_ASSERT (agmt);
  2844. if (start)
  2845. agmt_start (agmt);
  2846. else /* stop */
  2847. agmt_stop (agmt);
  2848. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2849. }
  2850. }
  2851. int replica_start_agreement(Replica *r, Repl_Agmt *ra)
  2852. {
  2853. int ret = 0;
  2854. if (r == NULL) return -1;
  2855. PR_Lock(r->agmt_lock);
  2856. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED)) {
  2857. ret = agmt_start(ra); /* Start the replication agreement */
  2858. }
  2859. PR_Unlock(r->agmt_lock);
  2860. return ret;
  2861. }
  2862. int windows_replica_start_agreement(Replica *r, Repl_Agmt *ra)
  2863. {
  2864. int ret = 0;
  2865. if (r == NULL) return -1;
  2866. PR_Lock(r->agmt_lock);
  2867. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED)) {
  2868. ret = windows_agmt_start(ra); /* Start the replication agreement */
  2869. /* ret = windows_agmt_start(ra); Start the replication agreement */
  2870. }
  2871. PR_Unlock(r->agmt_lock);
  2872. return ret;
  2873. }
  2874. /*
  2875. * A callback function registed as op->o_csngen_handler and
  2876. * called by backend ops to generate opcsn.
  2877. */
  2878. CSN *
  2879. replica_generate_next_csn ( Slapi_PBlock *pb, const CSN *basecsn )
  2880. {
  2881. CSN *opcsn = NULL;
  2882. Object *replica_obj;
  2883. replica_obj = replica_get_replica_for_op (pb);
  2884. if (NULL != replica_obj)
  2885. {
  2886. Replica *replica = (Replica*) object_get_data (replica_obj);
  2887. if ( NULL != replica )
  2888. {
  2889. Slapi_Operation *op;
  2890. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  2891. if ( replica->repl_type != REPLICA_TYPE_READONLY ||
  2892. operation_is_flag_set (op, OP_FLAG_LEGACY_REPLICATION_DN ))
  2893. {
  2894. Object *gen_obj = replica_get_csngen (replica);
  2895. if (NULL != gen_obj)
  2896. {
  2897. CSNGen *gen = (CSNGen*) object_get_data (gen_obj);
  2898. if (NULL != gen)
  2899. {
  2900. /* The new CSN should be greater than the base CSN */
  2901. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  2902. if (csn_compare (opcsn, basecsn) <= 0)
  2903. {
  2904. char opcsnstr[CSN_STRSIZE], basecsnstr[CSN_STRSIZE];
  2905. char opcsn2str[CSN_STRSIZE];
  2906. csn_as_string (opcsn, PR_FALSE, opcsnstr);
  2907. csn_as_string (basecsn, PR_FALSE, basecsnstr);
  2908. csn_free ( &opcsn );
  2909. csngen_adjust_time (gen, basecsn);
  2910. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  2911. csn_as_string (opcsn, PR_FALSE, opcsn2str);
  2912. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  2913. "replica_generate_next_csn: "
  2914. "opcsn=%s <= basecsn=%s, adjusted opcsn=%s\n",
  2915. opcsnstr, basecsnstr, opcsn2str);
  2916. }
  2917. /*
  2918. * Insert opcsn into the csn pending list.
  2919. * This is the notify effect in csngen_new_csn().
  2920. */
  2921. assign_csn_callback (opcsn, (void *)replica);
  2922. }
  2923. object_release (gen_obj);
  2924. }
  2925. }
  2926. }
  2927. object_release (replica_obj);
  2928. }
  2929. return opcsn;
  2930. }
  2931. /*
  2932. * A callback function registed as op->o_replica_attr_handler and
  2933. * called by backend ops to get replica attributes.
  2934. */
  2935. int
  2936. replica_get_attr ( Slapi_PBlock *pb, const char* type, void *value )
  2937. {
  2938. int rc = -1;
  2939. Object *replica_obj;
  2940. replica_obj = replica_get_replica_for_op (pb);
  2941. if (NULL != replica_obj)
  2942. {
  2943. Replica *replica = (Replica*) object_get_data (replica_obj);
  2944. if ( NULL != replica )
  2945. {
  2946. if (strcasecmp (type, type_replicaTombstonePurgeInterval) == 0)
  2947. {
  2948. *((int*)value) = replica->tombstone_reap_interval;
  2949. rc = 0;
  2950. }
  2951. else if (strcasecmp (type, type_replicaPurgeDelay) == 0)
  2952. {
  2953. *((int*)value) = replica->repl_purge_delay;
  2954. rc = 0;
  2955. }
  2956. }
  2957. object_release (replica_obj);
  2958. }
  2959. return rc;
  2960. }