1
0

repl5_replica.c 100 KB

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