repl5_ruv.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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) 2010 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_ruv.c - implementation of replica update vector */
  43. /*
  44. * The replica update vector is stored in the nsds50ruv attribute. The LDIF
  45. * representation of the ruv is:
  46. * nsds50ruv: {replicageneration} <gen-id-for-this-replica>
  47. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  48. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  49. * ...
  50. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  51. *
  52. * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
  53. * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
  54. * ...
  55. *
  56. * For readability, ruv_dump appends nsruvReplicaLastModified to nsds50ruv:
  57. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn> [<lastModifiedTime>]]
  58. */
  59. #include <string.h>
  60. #include <ctype.h> /* For isdigit() */
  61. #include "csnpl.h"
  62. #include "repl5_ruv.h"
  63. #include "repl_shared.h"
  64. #include "repl5.h"
  65. #define RIDSTR_SIZE 16 /* string large enough to hold replicaid*/
  66. #define RUVSTR_SIZE 256 /* string large enough to hold ruv and lastmodifiedtime */
  67. /* Data Structures */
  68. /* replica */
  69. typedef struct ruvElement
  70. {
  71. ReplicaId rid; /* replica id for this element */
  72. CSN *csn; /* largest csn that we know about that originated at the master */
  73. CSN *min_csn; /* smallest csn that originated at the master */
  74. char *replica_purl; /* Partial URL for replica */
  75. CSNPL *csnpl; /* list of operations in progress */
  76. time_t last_modified; /* timestamp the modification of csn */
  77. } RUVElement;
  78. /* replica update vector */
  79. struct _ruv
  80. {
  81. char *replGen; /* replicated area generation: identifies replica
  82. in space and in time */
  83. DataList *elements; /* replicas */
  84. Slapi_RWLock *lock; /* concurrency control */
  85. };
  86. /* forward declarations */
  87. static int ruvInit (RUV **ruv, int initCount);
  88. static void ruvFreeReplica (void **data);
  89. static RUVElement* ruvGetReplica (const RUV *ruv, ReplicaId rid);
  90. static RUVElement* ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl);
  91. static RUVElement* ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl);
  92. static RUVElement* ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index);
  93. static int ruvReplicaCompare (const void *el1, const void *el2);
  94. static RUVElement *get_ruvelement_from_berval(const struct berval *bval);
  95. static char *get_replgen_from_berval(const struct berval *bval);
  96. static const char * const prefix_replicageneration = "{replicageneration}";
  97. static const char * const prefix_ruvcsn = "{replica "; /* intentionally missing '}' */
  98. /* API implementation */
  99. /*
  100. * Allocate a new ruv and set its replica generation to the given generation.
  101. */
  102. int
  103. ruv_init_new(const char *replGen, ReplicaId rid, const char *purl, RUV **ruv)
  104. {
  105. int rc;
  106. RUVElement *replica;
  107. if (ruv == NULL || replGen == NULL)
  108. {
  109. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_init_new: NULL argument\n");
  110. return RUV_BAD_DATA;
  111. }
  112. rc = ruvInit (ruv, 0);
  113. if (rc != RUV_SUCCESS)
  114. return rc;
  115. (*ruv)->replGen = slapi_ch_strdup (replGen);
  116. /* we want to add the local writable replica to the RUV before any csns are created */
  117. /* this is so that it can be referred to even before it accepted any changes */
  118. if (purl)
  119. {
  120. replica = ruvAddReplicaNoCSN (*ruv, rid, purl);
  121. if (replica == NULL)
  122. return RUV_MEMORY_ERROR;
  123. }
  124. return RUV_SUCCESS;
  125. }
  126. int ruv_private_new(RUV **ruv, RUV *clone )
  127. {
  128. int rc;
  129. rc = ruvInit (ruv, dl_get_count(clone->elements) );
  130. if (rc != RUV_SUCCESS)
  131. return rc;
  132. (*ruv)->replGen = slapi_ch_strdup (clone->replGen);
  133. return RUV_SUCCESS;
  134. }
  135. /*
  136. * Create a new RUV and initialize its contents from the provided Slapi_Attr.
  137. * Returns:
  138. * RUV_BAD_DATA if the values in the attribute were malformed.
  139. * RUV_SUCCESS if all went well
  140. */
  141. int
  142. ruv_init_from_slapi_attr(Slapi_Attr *attr, RUV **ruv)
  143. {
  144. ReplicaId dummy = 0;
  145. return (ruv_init_from_slapi_attr_and_check_purl(attr, ruv, &dummy));
  146. }
  147. /*
  148. * Create a new RUV and initialize its contents from the provided Slapi_Attr.
  149. * Returns:
  150. * RUV_BAD_DATA if the values in the attribute were malformed.
  151. * RUV_SUCCESS if all went well
  152. * contain_purl is 0 if the ruv doesn't contain the local purl
  153. * contain_purl is != 0 if the ruv contains the local purl (contains the rid)
  154. */
  155. int
  156. ruv_init_from_slapi_attr_and_check_purl(Slapi_Attr *attr, RUV **ruv, ReplicaId *contain_purl)
  157. {
  158. int return_value;
  159. PR_ASSERT(NULL != attr && NULL != ruv);
  160. if (NULL == ruv || NULL == attr)
  161. {
  162. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  163. "ruv_init_from_slapi_attr: NULL argument\n");
  164. return_value = RUV_BAD_DATA;
  165. }
  166. else
  167. {
  168. int rc;
  169. int numvalues;
  170. slapi_attr_get_numvalues(attr, &numvalues);
  171. if ((rc = ruvInit(ruv, numvalues)) != RUV_SUCCESS)
  172. {
  173. return_value = rc;
  174. }
  175. else
  176. {
  177. int hint;
  178. Slapi_Value *value;
  179. const struct berval *bval;
  180. const char *purl = NULL;
  181. return_value = RUV_SUCCESS;
  182. purl = multimaster_get_local_purl();
  183. *contain_purl = 0;
  184. for (hint = slapi_attr_first_value(attr, &value);
  185. hint != -1; hint = slapi_attr_next_value(attr, hint, &value))
  186. {
  187. bval = slapi_value_get_berval(value);
  188. if (NULL != bval && NULL != bval->bv_val)
  189. {
  190. if (strncmp(bval->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
  191. if (NULL == (*ruv)->replGen)
  192. {
  193. (*ruv)->replGen = get_replgen_from_berval(bval);
  194. } else {
  195. /* Twice replicageneration is wrong, just log and ignore */
  196. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  197. "ruv_init_from_slapi_attr: %s is present more than once\n",
  198. prefix_replicageneration);
  199. }
  200. }
  201. else
  202. {
  203. RUVElement *ruve = get_ruvelement_from_berval(bval);
  204. if (NULL != ruve)
  205. {
  206. /* Is the local purl already in the ruv ? */
  207. if ( (*contain_purl==0) && ruve->replica_purl && purl && (strncmp(ruve->replica_purl, purl, strlen(purl))==0) )
  208. {
  209. *contain_purl = ruve->rid;
  210. }
  211. dl_add ((*ruv)->elements, ruve);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. return return_value;
  219. }
  220. /*
  221. * Same as ruv_init_from_slapi_attr, but takes an array of pointers to bervals.
  222. * I wish this wasn't a cut-n-paste of the above function, but I don't see a
  223. * clean way to define one API in terms of the other.
  224. */
  225. int
  226. ruv_init_from_bervals(struct berval **vals, RUV **ruv)
  227. {
  228. int return_value;
  229. PR_ASSERT(NULL != vals && NULL != ruv);
  230. if (NULL == ruv || NULL == vals)
  231. {
  232. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  233. "ruv_init_from_slapi_value: NULL argument\n");
  234. return_value = RUV_BAD_DATA;
  235. }
  236. else
  237. {
  238. int i, rc;
  239. i = 0;
  240. while (vals[i] != NULL)
  241. {
  242. i++;
  243. }
  244. if ((rc = ruvInit (ruv, i)) != RUV_SUCCESS)
  245. {
  246. return_value = rc;
  247. }
  248. else
  249. {
  250. return_value = RUV_SUCCESS;
  251. for (i = 0; NULL != vals[i]; i++)
  252. {
  253. if (NULL != vals[i]->bv_val)
  254. {
  255. if (strncmp(vals[i]->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
  256. if (NULL == (*ruv)->replGen)
  257. {
  258. (*ruv)->replGen = get_replgen_from_berval(vals[i]);
  259. } else {
  260. /* Twice replicageneration is wrong, just log and ignore */
  261. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  262. "ruv_init_from_slapi_value: %s is present more than once\n",
  263. prefix_replicageneration);
  264. }
  265. }
  266. else
  267. {
  268. RUVElement *ruve = get_ruvelement_from_berval(vals[i]);
  269. if (NULL != ruve)
  270. {
  271. dl_add ((*ruv)->elements, ruve);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return return_value;
  279. }
  280. RUV*
  281. ruv_dup (const RUV *ruv)
  282. {
  283. int rc;
  284. RUVElement *replica, *dupReplica;
  285. int cookie;
  286. RUV *dupRUV = NULL;
  287. if (ruv == NULL)
  288. return NULL;
  289. slapi_rwlock_rdlock (ruv->lock);
  290. rc = ruvInit (&dupRUV, dl_get_count (ruv->elements));
  291. if (rc != RUV_SUCCESS || dupRUV == NULL)
  292. goto done;
  293. dupRUV->replGen = slapi_ch_strdup (ruv->replGen);
  294. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  295. replica = dl_get_next (ruv->elements, &cookie))
  296. {
  297. dupReplica = (RUVElement *)slapi_ch_calloc (1, sizeof (*dupReplica));
  298. dupReplica->rid = replica->rid;
  299. if (replica->csn)
  300. dupReplica->csn = csn_dup (replica->csn);
  301. if (replica->min_csn)
  302. dupReplica->min_csn = csn_dup (replica->min_csn);
  303. if (replica->replica_purl)
  304. dupReplica->replica_purl = slapi_ch_strdup (replica->replica_purl);
  305. dupReplica->last_modified = replica->last_modified;
  306. /* ONREPL - we don't make copy of the pernding list. For now
  307. we don't need it. */
  308. dl_add (dupRUV->elements, dupReplica);
  309. }
  310. done:
  311. slapi_rwlock_unlock (ruv->lock);
  312. return dupRUV;
  313. }
  314. void
  315. ruv_destroy (RUV **ruv)
  316. {
  317. if (ruv != NULL && *ruv != NULL)
  318. {
  319. if ((*ruv)->elements)
  320. {
  321. dl_cleanup ((*ruv)->elements, ruvFreeReplica);
  322. dl_free (&(*ruv)->elements);
  323. }
  324. /* slapi_ch_free accepts NULL pointer */
  325. slapi_ch_free ((void **)&((*ruv)->replGen));
  326. if ((*ruv)->lock)
  327. {
  328. slapi_destroy_rwlock ((*ruv)->lock);
  329. }
  330. slapi_ch_free ((void**)ruv);
  331. }
  332. }
  333. /*
  334. * [610948]
  335. * copy elements in srcruv to destruv
  336. * destruv is the live wrapper, which could be referred by other threads.
  337. * srcruv is cleaned up after copied.
  338. */
  339. void
  340. ruv_copy_and_destroy (RUV **srcruv, RUV **destruv)
  341. {
  342. DataList *elemp = NULL;
  343. char *replgp = NULL;
  344. if (NULL == srcruv || NULL == *srcruv || NULL == destruv)
  345. {
  346. return;
  347. }
  348. if (NULL == *destruv)
  349. {
  350. *destruv = *srcruv;
  351. *srcruv = NULL;
  352. }
  353. else
  354. {
  355. slapi_rwlock_wrlock((*destruv)->lock);
  356. elemp = (*destruv)->elements;
  357. (*destruv)->elements = (*srcruv)->elements;
  358. if (elemp)
  359. {
  360. dl_cleanup (elemp, ruvFreeReplica);
  361. dl_free (&elemp);
  362. }
  363. /* slapi_ch_free accepts NULL pointer */
  364. replgp = (*destruv)->replGen;
  365. (*destruv)->replGen = (*srcruv)->replGen;
  366. slapi_ch_free ((void **)&replgp);
  367. if ((*srcruv)->lock)
  368. {
  369. slapi_destroy_rwlock ((*srcruv)->lock);
  370. }
  371. slapi_ch_free ((void**)srcruv);
  372. slapi_rwlock_unlock((*destruv)->lock);
  373. }
  374. PR_ASSERT (*destruv != NULL && *srcruv == NULL);
  375. }
  376. int
  377. ruv_delete_replica (RUV *ruv, ReplicaId rid)
  378. {
  379. int return_value;
  380. if (ruv == NULL)
  381. {
  382. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_delete_replica: NULL argument\n");
  383. return_value = RUV_BAD_DATA;
  384. }
  385. else
  386. {
  387. /* check for duplicates */
  388. slapi_rwlock_wrlock (ruv->lock);
  389. dl_delete (ruv->elements, (const void*)&rid, ruvReplicaCompare, ruvFreeReplica);
  390. slapi_rwlock_unlock (ruv->lock);
  391. return_value = RUV_SUCCESS;
  392. }
  393. return return_value;
  394. }
  395. int
  396. ruv_add_replica (RUV *ruv, ReplicaId rid, const char *replica_purl)
  397. {
  398. RUVElement* replica;
  399. PR_ASSERT (ruv && replica_purl);
  400. slapi_rwlock_wrlock (ruv->lock);
  401. replica = ruvGetReplica (ruv, rid);
  402. if (replica == NULL)
  403. {
  404. replica = ruvAddReplicaNoCSN (ruv, rid, replica_purl);
  405. }
  406. slapi_rwlock_unlock (ruv->lock);
  407. if (replica)
  408. return RUV_SUCCESS;
  409. else
  410. return RUV_MEMORY_ERROR;
  411. }
  412. int
  413. ruv_replace_replica_purl (RUV *ruv, ReplicaId rid, const char *replica_purl)
  414. {
  415. RUVElement* replica;
  416. int rc = RUV_NOTFOUND;
  417. PR_ASSERT (ruv && replica_purl);
  418. slapi_rwlock_wrlock (ruv->lock);
  419. replica = ruvGetReplica (ruv, rid);
  420. if (replica != NULL)
  421. {
  422. if (replica->replica_purl == NULL || strcmp(replica->replica_purl, replica_purl)) { /* purl updated */
  423. /* Replace replica_purl in RUV since supplier has been updated. */
  424. slapi_ch_free_string(&replica->replica_purl);
  425. replica->replica_purl = slapi_ch_strdup(replica_purl);
  426. /* Also, reset csn and min_csn. */
  427. replica->csn = replica->min_csn = NULL;
  428. }
  429. rc = RUV_SUCCESS;
  430. }
  431. slapi_rwlock_unlock (ruv->lock);
  432. return rc;
  433. }
  434. int
  435. ruv_add_index_replica (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  436. {
  437. RUVElement* replica;
  438. PR_ASSERT (ruv && replica_purl);
  439. slapi_rwlock_wrlock (ruv->lock);
  440. replica = ruvGetReplica (ruv, rid);
  441. if (replica == NULL)
  442. {
  443. replica = ruvAddIndexReplicaNoCSN (ruv, rid, replica_purl, index);
  444. }
  445. slapi_rwlock_unlock (ruv->lock);
  446. if (replica)
  447. return RUV_SUCCESS;
  448. else
  449. return RUV_MEMORY_ERROR;
  450. }
  451. PRBool
  452. ruv_contains_replica (const RUV *ruv, ReplicaId rid)
  453. {
  454. RUVElement *replica;
  455. if (ruv == NULL)
  456. return PR_FALSE;
  457. slapi_rwlock_rdlock (ruv->lock);
  458. replica = ruvGetReplica (ruv, rid);
  459. slapi_rwlock_unlock (ruv->lock);
  460. return replica != NULL;
  461. }
  462. #define GET_LARGEST_CSN 231
  463. #define GET_SMALLEST_CSN 232
  464. static int
  465. get_csn_internal(const RUV *ruv, ReplicaId rid, CSN **csn, int whichone)
  466. {
  467. RUVElement *replica;
  468. int return_value = RUV_SUCCESS;
  469. if (ruv == NULL || csn == NULL)
  470. {
  471. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_largest_csn_for_replica: NULL argument\n");
  472. return_value = RUV_BAD_DATA;
  473. }
  474. else
  475. {
  476. *csn = NULL;
  477. /* prevent element from being destroyed while we get its data */
  478. slapi_rwlock_rdlock (ruv->lock);
  479. replica = ruvGetReplica (ruv, rid);
  480. /* replica without min csn is treated as a non-existent replica */
  481. if (replica == NULL || replica->min_csn == NULL)
  482. {
  483. return_value = RUV_NOTFOUND;
  484. }
  485. else
  486. {
  487. switch (whichone)
  488. {
  489. case GET_LARGEST_CSN:
  490. *csn = replica->csn ? csn_dup (replica->csn) : NULL;
  491. break;
  492. case GET_SMALLEST_CSN:
  493. *csn = replica->min_csn ? csn_dup (replica->min_csn) : NULL;
  494. break;
  495. default:
  496. *csn = NULL;
  497. }
  498. }
  499. slapi_rwlock_unlock (ruv->lock);
  500. }
  501. return return_value;
  502. }
  503. int
  504. ruv_get_largest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
  505. {
  506. return get_csn_internal(ruv, rid, csn, GET_LARGEST_CSN);
  507. }
  508. int
  509. ruv_get_smallest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
  510. {
  511. return get_csn_internal(ruv, rid, csn, GET_SMALLEST_CSN);
  512. }
  513. const char *
  514. ruv_get_purl_for_replica(const RUV *ruv, ReplicaId rid)
  515. {
  516. RUVElement *replica;
  517. const char *return_value = NULL;
  518. slapi_rwlock_rdlock (ruv->lock);
  519. replica = ruvGetReplica (ruv, rid);
  520. if (replica != NULL)
  521. {
  522. return_value = replica->replica_purl;
  523. }
  524. slapi_rwlock_unlock (ruv->lock);
  525. return return_value;
  526. }
  527. static int
  528. set_min_csn_nolock(RUV *ruv, const CSN *min_csn, const char *replica_purl)
  529. {
  530. int return_value;
  531. ReplicaId rid = csn_get_replicaid (min_csn);
  532. RUVElement *replica = ruvGetReplica (ruv, rid);
  533. if (NULL == replica)
  534. {
  535. replica = ruvAddReplica (ruv, min_csn, replica_purl);
  536. if (replica)
  537. return_value = RUV_SUCCESS;
  538. else
  539. return_value = RUV_MEMORY_ERROR;
  540. }
  541. else
  542. {
  543. if (replica->min_csn == NULL || csn_compare (min_csn, replica->min_csn) < 0)
  544. {
  545. csn_free(&replica->min_csn);
  546. replica->min_csn = csn_dup(min_csn);
  547. }
  548. return_value = RUV_SUCCESS;
  549. }
  550. return return_value;
  551. }
  552. static int
  553. set_max_csn_nolock(RUV *ruv, const CSN *max_csn, const char *replica_purl)
  554. {
  555. int return_value;
  556. ReplicaId rid = csn_get_replicaid (max_csn);
  557. RUVElement *replica = ruvGetReplica (ruv, rid);
  558. if (NULL == replica)
  559. {
  560. replica = ruvAddReplica (ruv, max_csn, replica_purl);
  561. if (replica)
  562. return_value = RUV_SUCCESS;
  563. else
  564. return_value = RUV_MEMORY_ERROR;
  565. }
  566. else
  567. {
  568. if (replica_purl && replica->replica_purl == NULL)
  569. replica->replica_purl = slapi_ch_strdup (replica_purl);
  570. csn_free(&replica->csn);
  571. replica->csn = csn_dup(max_csn);
  572. replica->last_modified = current_time();
  573. return_value = RUV_SUCCESS;
  574. }
  575. return return_value;
  576. }
  577. int
  578. ruv_set_min_csn(RUV *ruv, const CSN *min_csn, const char *replica_purl)
  579. {
  580. int return_value;
  581. slapi_rwlock_wrlock (ruv->lock);
  582. return_value = set_min_csn_nolock(ruv, min_csn, replica_purl);
  583. slapi_rwlock_unlock (ruv->lock);
  584. return return_value;
  585. }
  586. int
  587. ruv_set_max_csn(RUV *ruv, const CSN *max_csn, const char *replica_purl)
  588. {
  589. int return_value;
  590. slapi_rwlock_wrlock (ruv->lock);
  591. return_value = set_max_csn_nolock(ruv, max_csn, replica_purl);
  592. slapi_rwlock_unlock (ruv->lock);
  593. return return_value;
  594. }
  595. int
  596. ruv_set_csns(RUV *ruv, const CSN *csn, const char *replica_purl)
  597. {
  598. RUVElement *replica;
  599. ReplicaId rid;
  600. int return_value;
  601. if (ruv == NULL || csn == NULL)
  602. {
  603. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_set_csns: NULL argument\n");
  604. return_value = RUV_BAD_DATA;
  605. }
  606. else
  607. {
  608. rid = csn_get_replicaid (csn);
  609. /* prevent element from being destroyed while we get its data */
  610. slapi_rwlock_wrlock (ruv->lock);
  611. replica = ruvGetReplica (ruv, rid);
  612. if (replica == NULL) /* add new replica */
  613. {
  614. replica = ruvAddReplica (ruv, csn, replica_purl);
  615. if (replica)
  616. return_value = RUV_SUCCESS;
  617. else
  618. return_value = RUV_MEMORY_ERROR;
  619. }
  620. else
  621. {
  622. if (csn_compare (csn, replica->csn) > 0)
  623. {
  624. if (replica->csn != NULL)
  625. {
  626. csn_init_by_csn ( replica->csn, csn );
  627. }
  628. else
  629. {
  630. replica->csn = csn_dup(csn);
  631. }
  632. replica->last_modified = current_time();
  633. if (replica_purl && (NULL == replica->replica_purl ||
  634. strcmp(replica->replica_purl, replica_purl) != 0))
  635. {
  636. /* slapi_ch_free accepts NULL pointer */
  637. slapi_ch_free((void **)&replica->replica_purl);
  638. replica->replica_purl = slapi_ch_strdup(replica_purl);
  639. }
  640. }
  641. /* XXXggood only need to worry about this if real min csn not committed to changelog yet */
  642. if (csn_compare (csn, replica->min_csn) < 0)
  643. {
  644. csn_free(&replica->min_csn);
  645. replica->min_csn = csn_dup(csn);
  646. }
  647. return_value = RUV_SUCCESS;
  648. }
  649. slapi_rwlock_unlock (ruv->lock);
  650. }
  651. return return_value;
  652. }
  653. /* This function, for each replica keeps the smallest CSN its seen so far.
  654. Used for initial setup of changelog purge vector */
  655. int
  656. ruv_set_csns_keep_smallest(RUV *ruv, const CSN *csn)
  657. {
  658. RUVElement *replica;
  659. ReplicaId rid;
  660. int return_value;
  661. if (ruv == NULL || csn == NULL)
  662. {
  663. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  664. "ruv_set_csns_keep_smallest: NULL argument\n");
  665. return_value = RUV_BAD_DATA;
  666. }
  667. else
  668. {
  669. rid = csn_get_replicaid (csn);
  670. /* prevent element from being destroyed while we get its data */
  671. slapi_rwlock_wrlock (ruv->lock);
  672. replica = ruvGetReplica (ruv, rid);
  673. if (replica == NULL) /* add new replica */
  674. {
  675. replica = ruvAddReplica (ruv, csn, NULL);
  676. if (replica)
  677. return_value = RUV_SUCCESS;
  678. else
  679. return_value = RUV_MEMORY_ERROR;
  680. }
  681. else
  682. {
  683. if (csn_compare (csn, replica->csn) < 0)
  684. {
  685. csn_free(&replica->csn);
  686. replica->csn = csn_dup(csn);
  687. replica->last_modified = current_time();
  688. }
  689. return_value = RUV_SUCCESS;
  690. }
  691. slapi_rwlock_unlock (ruv->lock);
  692. }
  693. return return_value;
  694. }
  695. void
  696. ruv_set_replica_generation(RUV *ruv, const char *csnstr)
  697. {
  698. if (NULL != csnstr && NULL != ruv)
  699. {
  700. slapi_rwlock_wrlock (ruv->lock);
  701. if (NULL != ruv->replGen)
  702. {
  703. slapi_ch_free((void **)&ruv->replGen);
  704. }
  705. ruv->replGen = slapi_ch_strdup(csnstr);
  706. slapi_rwlock_unlock (ruv->lock);
  707. }
  708. }
  709. char *
  710. ruv_get_replica_generation(const RUV *ruv)
  711. {
  712. char *return_str = NULL;
  713. if (!ruv) {
  714. return return_str;
  715. }
  716. slapi_rwlock_rdlock (ruv->lock);
  717. if (ruv != NULL && ruv->replGen != NULL)
  718. {
  719. return_str = slapi_ch_strdup(ruv->replGen);
  720. }
  721. slapi_rwlock_unlock (ruv->lock);
  722. return return_str;
  723. }
  724. static PRBool
  725. ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
  726. {
  727. RUVElement *replica;
  728. ReplicaId rid;
  729. PRBool return_value;
  730. if (ruv == NULL || csn == NULL)
  731. {
  732. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: NULL argument\n");
  733. return_value = PR_FALSE;
  734. }
  735. else
  736. {
  737. rid = csn_get_replicaid(csn);
  738. replica = ruvGetReplica (ruv, rid);
  739. if (replica == NULL)
  740. {
  741. /*
  742. * We don't know anything about this replica change in the cl, mark it to be zapped.
  743. * This could of been a previously cleaned ruv, but the server was restarted before
  744. * the change could be trimmed.
  745. *
  746. * Only the change log trimming calls this function with "strict" set. So we'll return success
  747. * if strict is set.
  748. */
  749. if(strict){
  750. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found.\n", rid);
  751. return_value = PR_TRUE;
  752. } else {
  753. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found.\n", rid);
  754. return_value = PR_FALSE;
  755. }
  756. }
  757. else
  758. {
  759. if (strict)
  760. {
  761. return_value = (csn_compare (csn, replica->csn) < 0);
  762. }
  763. else
  764. {
  765. return_value = (csn_compare (csn, replica->csn) <= 0);
  766. }
  767. }
  768. }
  769. return return_value;
  770. }
  771. PRBool
  772. ruv_covers_csn(const RUV *ruv, const CSN *csn)
  773. {
  774. PRBool rc;
  775. slapi_rwlock_rdlock (ruv->lock);
  776. rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
  777. slapi_rwlock_unlock (ruv->lock);
  778. return rc;
  779. }
  780. PRBool
  781. ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
  782. {
  783. PRBool rc;
  784. slapi_rwlock_rdlock (ruv->lock);
  785. rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
  786. slapi_rwlock_unlock (ruv->lock);
  787. return rc;
  788. }
  789. /*
  790. * Used by the cleanallruv task
  791. *
  792. * We want to return TRUE if replica is NULL,
  793. * and we want to use "csn_compare() <="
  794. */
  795. PRBool
  796. ruv_covers_csn_cleanallruv(const RUV *ruv, const CSN *csn)
  797. {
  798. RUVElement *replica;
  799. ReplicaId rid;
  800. PRBool return_value;
  801. if (ruv == NULL || csn == NULL){
  802. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn_cleanallruv: NULL argument\n");
  803. return_value = PR_FALSE;
  804. } else {
  805. rid = csn_get_replicaid(csn);
  806. replica = ruvGetReplica (ruv, rid);
  807. if (replica == NULL){
  808. /* already cleaned */
  809. return_value = PR_TRUE;
  810. } else {
  811. return_value = (csn_compare (csn, replica->csn) <= 0);
  812. }
  813. }
  814. return return_value;
  815. }
  816. /*
  817. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  818. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  819. */
  820. static int
  821. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max, ReplicaId rid)
  822. {
  823. int return_value;
  824. if (ruv == NULL || csn == NULL)
  825. {
  826. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  827. return_value = RUV_BAD_DATA;
  828. }
  829. else
  830. {
  831. CSN *found = NULL;
  832. RUVElement *replica;
  833. int cookie;
  834. slapi_rwlock_rdlock (ruv->lock);
  835. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  836. replica = dl_get_next (ruv->elements, &cookie))
  837. {
  838. /*
  839. * Skip replica whose maxcsn is NULL otherwise
  840. * the code will return different min_csn if
  841. * the sequence of the replicas is altered.
  842. *
  843. * don't use READ_ONLY replicas for computing the value of
  844. * "found", as they seem to have NULL csn and min_csn
  845. */
  846. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  847. {
  848. continue;
  849. }
  850. if(rid){ /* we are only interested in this rid's maxcsn */
  851. if(replica->rid == rid){
  852. found = replica->csn;
  853. break;
  854. }
  855. } else {
  856. if (found == NULL ||
  857. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  858. ( get_the_max && csn_compare(found, replica->csn)<0))
  859. {
  860. found = replica->csn;
  861. }
  862. }
  863. }
  864. if (found == NULL)
  865. {
  866. *csn = NULL;
  867. }
  868. else
  869. {
  870. *csn = csn_dup (found);
  871. }
  872. slapi_rwlock_unlock (ruv->lock);
  873. return_value = RUV_SUCCESS;
  874. }
  875. return return_value;
  876. }
  877. int
  878. ruv_get_rid_max_csn(const RUV *ruv, CSN **csn, ReplicaId rid){
  879. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, rid);
  880. }
  881. int
  882. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  883. {
  884. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, 0 /* rid */);
  885. }
  886. int
  887. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  888. {
  889. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */, 0 /* rid */);
  890. }
  891. int
  892. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  893. {
  894. int cookie;
  895. RUVElement *elem;
  896. int rc = 0;
  897. ruv_enum_data enum_data = {0};
  898. if (ruv == NULL || fn == NULL)
  899. {
  900. /* ONREPL - log error */
  901. return -1;
  902. }
  903. slapi_rwlock_rdlock (ruv->lock);
  904. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  905. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  906. {
  907. /* we only return elements that contains both minimal and maximal CSNs */
  908. if (elem->csn && elem->min_csn)
  909. {
  910. enum_data.csn = elem->csn;
  911. enum_data.min_csn = elem->min_csn;
  912. rc = fn (&enum_data, arg);
  913. if (rc != 0)
  914. break;
  915. }
  916. }
  917. slapi_rwlock_unlock (ruv->lock);
  918. return rc;
  919. }
  920. void
  921. ruv_element_to_string(RUVElement *ruvelem, struct berval *bv, char *buf, size_t bufsize)
  922. {
  923. char csnStr1[CSN_STRSIZE];
  924. char csnStr2[CSN_STRSIZE];
  925. const char *fmtstr = "%s%d%s%s}%s%s%s%s";
  926. if (buf && bufsize) {
  927. PR_snprintf(buf, bufsize, fmtstr,
  928. prefix_ruvcsn, ruvelem->rid,
  929. ruvelem->replica_purl == NULL ? "" : " ",
  930. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  931. ruvelem->min_csn == NULL ? "" : " ",
  932. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  933. ruvelem->csn == NULL ? "" : " ",
  934. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  935. } else {
  936. bv->bv_val = slapi_ch_smprintf(fmtstr,
  937. prefix_ruvcsn, ruvelem->rid,
  938. ruvelem->replica_purl == NULL ? "" : " ",
  939. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  940. ruvelem->min_csn == NULL ? "" : " ",
  941. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  942. ruvelem->csn == NULL ? "" : " ",
  943. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  944. bv->bv_len = strlen(bv->bv_val);
  945. }
  946. }
  947. /*
  948. * Convert a replica update vector to a NULL-terminated array
  949. * of bervals. The caller is responsible for freeing the bervals.
  950. */
  951. int
  952. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  953. {
  954. struct berval **returned_bervals = NULL;
  955. int return_value;
  956. if (ruv == NULL || bvals == NULL)
  957. {
  958. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  959. return_value = RUV_BAD_DATA;
  960. }
  961. else
  962. {
  963. int count;
  964. int i;
  965. RUVElement *replica;
  966. int cookie;
  967. slapi_rwlock_rdlock (ruv->lock);
  968. count = dl_get_count (ruv->elements) + 2;
  969. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  970. returned_bervals[count - 1] = NULL;
  971. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  972. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  973. prefix_replicageneration, ruv->replGen);
  974. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  975. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  976. i++, replica = dl_get_next (ruv->elements, &cookie))
  977. {
  978. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  979. ruv_element_to_string(replica, returned_bervals[i], NULL, 0);
  980. }
  981. slapi_rwlock_unlock (ruv->lock);
  982. return_value = RUV_SUCCESS;
  983. *bvals = returned_bervals;
  984. }
  985. return return_value;
  986. }
  987. void
  988. ruv_get_cleaned_rids(RUV *ruv, ReplicaId *rids)
  989. {
  990. RUVElement *replica;
  991. int cookie;
  992. int i = 0;
  993. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  994. replica = dl_get_next (ruv->elements, &cookie))
  995. {
  996. if(is_cleaned_rid(replica->rid)){
  997. rids[i++] = replica->rid;
  998. }
  999. }
  1000. }
  1001. int
  1002. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  1003. {
  1004. int return_value;
  1005. if (ruv == NULL || smod == NULL)
  1006. {
  1007. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  1008. return_value = RUV_BAD_DATA;
  1009. }
  1010. else
  1011. {
  1012. struct berval val;
  1013. RUVElement *replica;
  1014. int cookie;
  1015. #define B_SIZ 1024
  1016. char buf[B_SIZ];
  1017. slapi_rwlock_rdlock (ruv->lock);
  1018. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  1019. slapi_mod_set_type (smod, type_ruvElement);
  1020. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1021. PR_snprintf(buf, sizeof(buf), "%s %s", prefix_replicageneration, ruv->replGen);
  1022. val.bv_val = buf;
  1023. val.bv_len = strlen(buf);
  1024. slapi_mod_add_value(smod, &val);
  1025. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1026. replica = dl_get_next (ruv->elements, &cookie))
  1027. {
  1028. ruv_element_to_string(replica, NULL, buf, sizeof(buf));
  1029. val.bv_val = buf;
  1030. val.bv_len = strlen(buf);
  1031. slapi_mod_add_value(smod, &val);
  1032. }
  1033. slapi_rwlock_unlock (ruv->lock);
  1034. return_value = RUV_SUCCESS;
  1035. }
  1036. return return_value;
  1037. }
  1038. int
  1039. ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
  1040. {
  1041. int return_value;
  1042. if (ruv == NULL || smod == NULL)
  1043. {
  1044. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod: NULL argument\n");
  1045. return_value = RUV_BAD_DATA;
  1046. }
  1047. else
  1048. {
  1049. struct berval val;
  1050. RUVElement *replica;
  1051. int cookie;
  1052. char buf[B_SIZ];
  1053. slapi_rwlock_rdlock (ruv->lock);
  1054. slapi_mod_init (smod, dl_get_count (ruv->elements));
  1055. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  1056. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1057. val.bv_val = buf;
  1058. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1059. replica = dl_get_next (ruv->elements, &cookie))
  1060. {
  1061. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  1062. replica->replica_purl == NULL ? "" : " ",
  1063. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1064. replica->last_modified);
  1065. val.bv_len = strlen(buf);
  1066. slapi_mod_add_value(smod, &val);
  1067. }
  1068. slapi_rwlock_unlock (ruv->lock);
  1069. return_value = RUV_SUCCESS;
  1070. }
  1071. return return_value;
  1072. }
  1073. /*
  1074. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  1075. PRBool
  1076. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  1077. {
  1078. PRBool return_value = PR_TRUE;
  1079. RUVElement *replica;
  1080. int cookie;
  1081. /* compare replica generations first */
  1082. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  1083. return PR_FALSE;
  1084. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1085. return PR_FALSE;
  1086. /* replica generation is the same, now compare element by element */
  1087. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1088. NULL != replica;
  1089. replica = dl_get_next (covered_ruv->elements, &cookie))
  1090. {
  1091. if (replica->csn &&
  1092. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1093. {
  1094. return_value = PR_FALSE;
  1095. /* Don't break here - may leave something referenced? */
  1096. }
  1097. }
  1098. return return_value;
  1099. }
  1100. /*
  1101. * This compares two ruvs to see if they are compatible. This is
  1102. * used, for example, when the data is reloaded, to see if the ruv
  1103. * from the database is compatible with the ruv from the changelog.
  1104. * If the replica generation is empty or does not match, the data
  1105. * is not compatible.
  1106. * If the maxcsns are not compatible, the ruvs are not compatible.
  1107. * However, if the first ruv has replica IDs that the second RUV
  1108. * does not have, and this is the only difference, the application
  1109. * may allow that with a warning.
  1110. */
  1111. int
  1112. ruv_compare_ruv(const RUV *ruv1, const char *ruv1name, const RUV *ruv2, const char *ruv2name, int strict, int loglevel)
  1113. {
  1114. int rc = 0;
  1115. int ii = 0;
  1116. const RUV *ruvalist[] = {ruv1, ruv2};
  1117. const RUV *ruvblist[] = {ruv2, ruv1};
  1118. int missinglist[2] = {0, 0};
  1119. const char *ruvanames[] = {ruv1name, ruv2name};
  1120. const char *ruvbnames[] = {ruv2name, ruv1name};
  1121. const int nitems = 2;
  1122. /* compare replica generations first */
  1123. if (ruv1->replGen == NULL || ruv2->replGen == NULL) {
  1124. slapi_log_error(loglevel, repl_plugin_name,
  1125. "ruv_compare_ruv: RUV [%s] is missing the replica generation\n",
  1126. ruv1->replGen ? ruv2name : ruv1name);
  1127. return RUV_COMP_NO_GENERATION;
  1128. }
  1129. if (strcasecmp (ruv1->replGen, ruv2->replGen)) {
  1130. slapi_log_error(loglevel, repl_plugin_name,
  1131. "ruv_compare_ruv: RUV [%s] replica generation [%s] does not match RUV [%s] [%s]\n",
  1132. ruv1name, ruv1->replGen, ruv2name, ruv2->replGen);
  1133. return RUV_COMP_GENERATION_DIFFERS;
  1134. }
  1135. /* replica generation is the same, now compare element by element */
  1136. for (ii = 0; ii < nitems; ++ii) {
  1137. int cookie;
  1138. const RUV *ruva = ruvalist[ii];
  1139. const RUV *ruvb = ruvblist[ii];
  1140. int *missing = &missinglist[ii];
  1141. const char *ruvaname = ruvanames[ii];
  1142. const char *ruvbname = ruvbnames[ii];
  1143. RUVElement *replicab;
  1144. for (replicab = dl_get_first (ruvb->elements, &cookie);
  1145. NULL != replicab;
  1146. replicab = dl_get_next (ruvb->elements, &cookie)) {
  1147. if (replicab->csn) {
  1148. ReplicaId rid = csn_get_replicaid(replicab->csn);
  1149. RUVElement *replicaa = ruvGetReplica(ruva, rid);
  1150. char csnstra[CSN_STRSIZE];
  1151. char csnstrb[CSN_STRSIZE];
  1152. char ruvelem[1024];
  1153. ruv_element_to_string(replicab, NULL, ruvelem, sizeof(ruvelem));
  1154. csn_as_string(replicab->csn, PR_FALSE, csnstrb);
  1155. if (replicaa == NULL) {
  1156. (*missing)++;
  1157. slapi_log_error(loglevel, repl_plugin_name,
  1158. "ruv_compare_ruv: RUV [%s] does not contain element [%s] "
  1159. "which is present in RUV [%s]\n",
  1160. ruvaname, ruvelem, ruvbname);
  1161. } else if (strict && (csn_compare (replicab->csn, replicaa->csn) >= 0)) {
  1162. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1163. slapi_log_error(loglevel, repl_plugin_name,
  1164. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1165. "than or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
  1166. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1167. rc = RUV_COMP_CSN_DIFFERS;
  1168. } else if (csn_compare (replicab->csn, replicaa->csn) > 0) {
  1169. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1170. slapi_log_error(loglevel, repl_plugin_name,
  1171. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1172. "than the max CSN [%s] from RUV [%s] for element [%s]\n",
  1173. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1174. rc = RUV_COMP_CSN_DIFFERS;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. if (!rc) {
  1180. if (missinglist[0]) {
  1181. rc = RUV_COMP_RUV1_MISSING;
  1182. } else if (missinglist[1]) {
  1183. rc = RUV_COMP_RUV2_MISSING;
  1184. }
  1185. }
  1186. return rc;
  1187. }
  1188. PRInt32
  1189. ruv_replica_count (const RUV *ruv)
  1190. {
  1191. if (ruv == NULL)
  1192. return 0;
  1193. else
  1194. {
  1195. int count;
  1196. slapi_rwlock_rdlock (ruv->lock);
  1197. count = dl_get_count (ruv->elements);
  1198. slapi_rwlock_unlock (ruv->lock);
  1199. return count;
  1200. }
  1201. }
  1202. /*
  1203. * Extract all the referral URL's from the RUV (but self URL),
  1204. * returning them in an array of strings, that
  1205. * the caller must free.
  1206. */
  1207. char **
  1208. ruv_get_referrals(const RUV *ruv)
  1209. {
  1210. char **r= NULL;
  1211. int n;
  1212. const char *mypurl = multimaster_get_local_purl();
  1213. slapi_rwlock_rdlock (ruv->lock);
  1214. n = ruv_replica_count(ruv);
  1215. if(n>0)
  1216. {
  1217. RUVElement *replica;
  1218. int cookie;
  1219. int i= 0;
  1220. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1221. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1222. replica = dl_get_next (ruv->elements, &cookie))
  1223. {
  1224. /* Add URL into referrals if doesn't match self URL */
  1225. if((replica->replica_purl!=NULL) &&
  1226. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1227. (unsigned char *)mypurl) != 0))
  1228. {
  1229. r[i]= slapi_ch_strdup(replica->replica_purl);
  1230. i++;
  1231. }
  1232. }
  1233. }
  1234. slapi_rwlock_unlock (ruv->lock);
  1235. return r; /* Caller must free this */
  1236. }
  1237. void
  1238. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1239. {
  1240. RUVElement *replica;
  1241. int cookie;
  1242. char csnstr1[CSN_STRSIZE];
  1243. char csnstr2[CSN_STRSIZE];
  1244. char buff[RUVSTR_SIZE];
  1245. int len = sizeof (buff);
  1246. PR_ASSERT(NULL != ruv);
  1247. if (!slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1248. return;
  1249. }
  1250. slapi_rwlock_rdlock (ruv->lock);
  1251. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1252. ruv_name ? ruv_name : type_ruvElement,
  1253. ruv->replGen == NULL ? "" : ruv->replGen);
  1254. if (prFile)
  1255. {
  1256. slapi_write_buffer (prFile, buff, strlen(buff));
  1257. }
  1258. else
  1259. {
  1260. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1261. }
  1262. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1263. replica = dl_get_next (ruv->elements, &cookie))
  1264. {
  1265. /* prefix_ruvcsn = "{replica " */
  1266. PR_snprintf (buff, len, "%s: %s%d%s%s}%s%s%s%s\n",
  1267. ruv_name ? ruv_name : type_ruvElement,
  1268. prefix_ruvcsn, replica->rid,
  1269. replica->replica_purl == NULL ? "" : " ",
  1270. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1271. replica->min_csn == NULL ? "" : " ",
  1272. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1273. replica->csn == NULL ? "" : " ",
  1274. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1275. if (strlen (csnstr1) > 0) {
  1276. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1277. replica->last_modified);
  1278. }
  1279. if (prFile)
  1280. {
  1281. slapi_write_buffer (prFile, buff, strlen(buff));
  1282. }
  1283. else
  1284. {
  1285. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1286. }
  1287. }
  1288. slapi_rwlock_unlock (ruv->lock);
  1289. }
  1290. /* this function notifies the ruv that there are operations in progress so that
  1291. they can be added to the pending list for the appropriate client. */
  1292. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1293. {
  1294. RUVElement* replica;
  1295. char csn_str[CSN_STRSIZE];
  1296. int rc = RUV_SUCCESS;
  1297. int rid = csn_get_replicaid (csn);
  1298. PR_ASSERT (ruv && csn);
  1299. /* locate ruvElement */
  1300. slapi_rwlock_wrlock (ruv->lock);
  1301. if(is_cleaned_rid(rid)){
  1302. /* return success because we want to consume the update, but not perform it */
  1303. rc = RUV_COVERS_CSN;
  1304. goto done;
  1305. }
  1306. replica = ruvGetReplica (ruv, rid);
  1307. if (replica == NULL)
  1308. {
  1309. replica = ruvAddReplicaNoCSN (ruv, rid, NULL/*purl*/);
  1310. if (replica == NULL)
  1311. {
  1312. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1313. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1314. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1315. }
  1316. rc = RUV_MEMORY_ERROR;
  1317. goto done;
  1318. }
  1319. }
  1320. /* check first that this csn is not already covered by this RUV */
  1321. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1322. {
  1323. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1324. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1325. "the csn %s has already be seen - ignoring\n",
  1326. csn_as_string (csn, PR_FALSE, csn_str));
  1327. }
  1328. rc = RUV_COVERS_CSN;
  1329. goto done;
  1330. }
  1331. rc = csnplInsert (replica->csnpl, csn);
  1332. if (rc == 1) /* we already seen this csn */
  1333. {
  1334. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1335. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1336. "the csn %s has already be seen - ignoring\n",
  1337. csn_as_string (csn, PR_FALSE, csn_str));
  1338. }
  1339. rc = RUV_COVERS_CSN;
  1340. }
  1341. else if(rc != 0)
  1342. {
  1343. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1344. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1345. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1346. }
  1347. rc = RUV_UNKNOWN_ERROR;
  1348. }
  1349. else
  1350. {
  1351. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1352. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1353. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1354. }
  1355. rc = RUV_SUCCESS;
  1356. }
  1357. done:
  1358. slapi_rwlock_unlock (ruv->lock);
  1359. return rc;
  1360. }
  1361. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1362. {
  1363. RUVElement* replica;
  1364. int rc = RUV_SUCCESS;
  1365. PR_ASSERT (ruv && csn);
  1366. /* locate ruvElement */
  1367. slapi_rwlock_wrlock (ruv->lock);
  1368. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1369. if (replica == NULL)
  1370. {
  1371. /* ONREPL - log error */
  1372. rc = RUV_NOTFOUND;
  1373. goto done;
  1374. }
  1375. rc = csnplRemove (replica->csnpl, csn);
  1376. if (rc != 0)
  1377. rc = RUV_NOTFOUND;
  1378. else
  1379. rc = RUV_SUCCESS;
  1380. done:
  1381. slapi_rwlock_unlock (ruv->lock);
  1382. return rc;
  1383. }
  1384. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1385. {
  1386. int rc=RUV_SUCCESS;
  1387. char csn_str[CSN_STRSIZE];
  1388. CSN *max_csn;
  1389. CSN *first_csn = NULL;
  1390. RUVElement *replica;
  1391. PR_ASSERT (ruv && csn);
  1392. slapi_rwlock_wrlock (ruv->lock);
  1393. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1394. if (replica == NULL)
  1395. {
  1396. /* we should have a ruv element at this point because it would have
  1397. been added by ruv_add_inprogress function */
  1398. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1399. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1400. goto done;
  1401. }
  1402. if (csnplCommit(replica->csnpl, csn) != 0)
  1403. {
  1404. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1405. csn_as_string(csn, PR_FALSE, csn_str));
  1406. rc = RUV_CSNPL_ERROR;
  1407. goto done;
  1408. }
  1409. else
  1410. {
  1411. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1412. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1413. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1414. }
  1415. }
  1416. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1417. {
  1418. #ifdef DEBUG
  1419. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1420. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1421. #endif
  1422. /* replica object sets min csn for local replica */
  1423. if (!isLocal && replica->min_csn == NULL) {
  1424. /* bug 559223 - it seems that, under huge stress, a server might pass
  1425. * through this code when more than 1 change has already been sent and commited into
  1426. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1427. * generated by this replica, we need to set the first_csn as the min csn in the
  1428. * ruv */
  1429. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1430. }
  1431. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1432. /* It is possible that first_csn points to max_csn.
  1433. We need to free it once */
  1434. if (max_csn != first_csn) {
  1435. csn_free(&first_csn);
  1436. }
  1437. csn_free(&max_csn);
  1438. }
  1439. done:
  1440. slapi_rwlock_unlock (ruv->lock);
  1441. return rc;
  1442. }
  1443. /* Helper functions */
  1444. static int
  1445. ruvInit (RUV **ruv, int initCount)
  1446. {
  1447. PR_ASSERT (ruv);
  1448. if (ruv == NULL) {
  1449. return RUV_NSPR_ERROR;
  1450. }
  1451. /* allocate new RUV */
  1452. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1453. /* allocate elements */
  1454. (*ruv)->elements = dl_new (); /* never returns NULL */
  1455. dl_init ((*ruv)->elements, initCount);
  1456. /* create lock */
  1457. (*ruv)->lock = slapi_new_rwlock();
  1458. if ((*ruv)->lock == NULL)
  1459. {
  1460. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1461. "ruvInit: failed to create lock\n");
  1462. dl_free(&(*ruv)->elements);
  1463. slapi_ch_free((void**)ruv);
  1464. return RUV_NSPR_ERROR;
  1465. }
  1466. return RUV_SUCCESS;
  1467. }
  1468. static void
  1469. ruvFreeReplica (void **data)
  1470. {
  1471. RUVElement *element = *(RUVElement**)data;
  1472. if (NULL != element)
  1473. {
  1474. if (NULL != element->csn)
  1475. {
  1476. csn_free (&element->csn);
  1477. }
  1478. if (NULL != element->min_csn)
  1479. {
  1480. csn_free (&element->min_csn);
  1481. }
  1482. /* slapi_ch_free accepts NULL pointer */
  1483. slapi_ch_free((void **)&element->replica_purl);
  1484. if (element->csnpl)
  1485. {
  1486. csnplFree (&(element->csnpl));
  1487. }
  1488. slapi_ch_free ((void **)&element);
  1489. }
  1490. }
  1491. static RUVElement*
  1492. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1493. {
  1494. RUVElement *replica;
  1495. PR_ASSERT (NULL != ruv);
  1496. PR_ASSERT (NULL != csn);
  1497. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1498. if (replica == NULL)
  1499. {
  1500. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1501. "ruvAddReplica: memory allocation failed\n");
  1502. return NULL;
  1503. }
  1504. replica->rid = csn_get_replicaid (csn);
  1505. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1506. replica->csn = csn_dup (csn);
  1507. replica->last_modified = current_time();
  1508. replica->min_csn = csn_dup (csn);
  1509. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1510. replica->csnpl = csnplNew ();
  1511. dl_add (ruv->elements, replica);
  1512. return replica;
  1513. }
  1514. static RUVElement*
  1515. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1516. {
  1517. RUVElement *replica;
  1518. PR_ASSERT (NULL != ruv);
  1519. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1520. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1521. if (replica == NULL)
  1522. {
  1523. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1524. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1525. return NULL;
  1526. }
  1527. replica->rid = rid;
  1528. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1529. replica->csnpl = csnplNew ();
  1530. dl_add (ruv->elements, replica);
  1531. return replica;
  1532. }
  1533. static RUVElement*
  1534. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1535. {
  1536. RUVElement *replica;
  1537. PR_ASSERT (NULL != ruv);
  1538. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1539. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1540. if (replica == NULL)
  1541. {
  1542. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1543. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1544. return NULL;
  1545. }
  1546. replica->rid = rid;
  1547. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1548. replica->csnpl = csnplNew ();
  1549. dl_add_index (ruv->elements, replica, index);
  1550. return replica;
  1551. }
  1552. static RUVElement *
  1553. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1554. {
  1555. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1556. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1557. }
  1558. static int
  1559. ruvReplicaCompare (const void *el1, const void *el2)
  1560. {
  1561. RUVElement *replica = (RUVElement*)el1;
  1562. ReplicaId *rid1 = (ReplicaId*) el2;
  1563. if (replica == NULL || rid1 == NULL)
  1564. return -1;
  1565. if (*rid1 == replica->rid)
  1566. return 0;
  1567. if (*rid1 < replica->rid)
  1568. return -1;
  1569. else
  1570. return 1;
  1571. }
  1572. /*
  1573. * Given a berval that points to a string of the form:
  1574. * "{dbgen} generation-id", return a copy of the
  1575. * "generation-id" part in a null-terminated string.
  1576. * Returns NULL if the berval is malformed.
  1577. */
  1578. static char *
  1579. get_replgen_from_berval(const struct berval *bval)
  1580. {
  1581. char *ret_string = NULL;
  1582. if (NULL != bval && NULL != bval->bv_val &&
  1583. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1584. strncasecmp(bval->bv_val, prefix_replicageneration,
  1585. strlen(prefix_replicageneration)) == 0)
  1586. {
  1587. unsigned int index = strlen(prefix_replicageneration);
  1588. /* Skip any whitespace */
  1589. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1590. if (index < bval->bv_len)
  1591. {
  1592. unsigned int ret_len = bval->bv_len - index;
  1593. ret_string = slapi_ch_malloc(ret_len + 1);
  1594. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1595. ret_string[ret_len] = '\0';
  1596. }
  1597. }
  1598. return ret_string;
  1599. }
  1600. /*
  1601. * Given a berval that points to a string of the form:
  1602. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1603. * partial URL and the CSNs into an RUVElement, and return
  1604. * a pointer to the copy. Returns NULL if the berval is
  1605. * malformed.
  1606. */
  1607. static RUVElement *
  1608. get_ruvelement_from_berval(const struct berval *bval)
  1609. {
  1610. RUVElement *ret_ruve = NULL;
  1611. char *purl = NULL;
  1612. ReplicaId rid = 0;
  1613. char ridbuff [RIDSTR_SIZE];
  1614. int i;
  1615. if (NULL == bval || NULL == bval->bv_val ||
  1616. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1617. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1618. {
  1619. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1620. "get_ruvelement_from_berval: invalid berval\n");
  1621. goto loser;
  1622. }
  1623. {
  1624. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1625. unsigned int urlend;
  1626. unsigned int mincsnbegin;
  1627. /* replica id must be here */
  1628. i = 0;
  1629. while (isdigit (bval->bv_val[urlbegin]))
  1630. {
  1631. ridbuff [i] = bval->bv_val[urlbegin];
  1632. i++;
  1633. urlbegin ++;
  1634. }
  1635. if (i == 0) /* replicaid is missing */
  1636. goto loser;
  1637. ridbuff[i] = '\0';
  1638. rid = atoi (ridbuff);
  1639. if (bval->bv_val[urlbegin] == '}')
  1640. {
  1641. /* No purl in this value */
  1642. purl = NULL;
  1643. mincsnbegin = urlbegin + 1;
  1644. }
  1645. else
  1646. {
  1647. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1648. urlend = urlbegin;
  1649. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1650. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1651. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1652. purl[urlend - urlbegin] = '\0';
  1653. mincsnbegin = urlend;
  1654. }
  1655. /* Skip any whitespace before the first (minimum) CSN */
  1656. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1657. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1658. if (mincsnbegin >= bval->bv_len)
  1659. {
  1660. /* Missing the entire content*/
  1661. if (purl == NULL)
  1662. goto loser;
  1663. else /* we have just purl - no changes from the replica has been seen */
  1664. {
  1665. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1666. ret_ruve->rid = rid;
  1667. ret_ruve->replica_purl = purl;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ((bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1673. && (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 10))
  1674. {
  1675. /* Malformed - incorrect length for 2 CSNs + space AND
  1676. * incorrect length for 2 CSNs + 2 spaces +
  1677. * last_modified (see ruv_dump) */
  1678. goto loser;
  1679. }
  1680. else
  1681. {
  1682. char mincsnstr[CSN_STRSIZE];
  1683. char maxcsnstr[CSN_STRSIZE];
  1684. memset(mincsnstr, '\0', CSN_STRSIZE);
  1685. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1686. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1687. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1688. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1689. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1690. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1691. ret_ruve->rid = rid;
  1692. ret_ruve->replica_purl = purl;
  1693. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1694. {
  1695. goto loser;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. /* initialize csn pending list */
  1701. ret_ruve->csnpl = csnplNew ();
  1702. if (ret_ruve->csnpl == NULL)
  1703. {
  1704. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1705. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1706. goto loser;
  1707. }
  1708. return ret_ruve;
  1709. loser:
  1710. /* slapi_ch_free accepts NULL pointer */
  1711. slapi_ch_free((void **)&purl);
  1712. if (NULL != ret_ruve)
  1713. {
  1714. if (NULL != ret_ruve->min_csn)
  1715. {
  1716. csn_free(&ret_ruve->min_csn);
  1717. }
  1718. if (NULL != ret_ruve->csn)
  1719. {
  1720. csn_free(&ret_ruve->csn);
  1721. }
  1722. slapi_ch_free((void **)&ret_ruve);
  1723. }
  1724. return NULL;
  1725. }
  1726. int
  1727. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1728. {
  1729. RUVElement * elem = NULL;
  1730. int rc = RUV_NOTFOUND;
  1731. PR_ASSERT(ruv);
  1732. slapi_rwlock_wrlock (ruv->lock);
  1733. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1734. if (elem) {
  1735. dl_add_index(ruv->elements, elem, 1);
  1736. rc = RUV_SUCCESS;
  1737. }
  1738. slapi_rwlock_unlock (ruv->lock);
  1739. return rc;
  1740. }
  1741. int
  1742. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1743. {
  1744. RUVElement * first = NULL;
  1745. int cookie;
  1746. int rc;
  1747. PR_ASSERT(ruv);
  1748. slapi_rwlock_rdlock (ruv->lock);
  1749. first = dl_get_first(ruv->elements, &cookie);
  1750. if ( first == NULL )
  1751. {
  1752. rc = RUV_MEMORY_ERROR;
  1753. }
  1754. else
  1755. {
  1756. *rid = first->rid;
  1757. *replica_purl = first->replica_purl;
  1758. rc = RUV_SUCCESS;
  1759. }
  1760. slapi_rwlock_unlock (ruv->lock);
  1761. return rc;
  1762. }
  1763. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1764. {
  1765. int cookie;
  1766. RUVElement *elem = NULL;
  1767. PR_ASSERT(ruv);
  1768. slapi_rwlock_rdlock (ruv->lock);
  1769. for (elem = dl_get_first (ruv->elements, &cookie);
  1770. elem;
  1771. elem = dl_get_next (ruv->elements, &cookie))
  1772. {
  1773. if (elem->rid == rid){
  1774. slapi_rwlock_unlock (ruv->lock);
  1775. return 1;
  1776. }
  1777. }
  1778. slapi_rwlock_unlock (ruv->lock);
  1779. return 0;
  1780. }
  1781. PRBool ruv_has_csns(const RUV *ruv)
  1782. {
  1783. PRBool retval = PR_TRUE;
  1784. CSN *mincsn = NULL;
  1785. CSN *maxcsn = NULL;
  1786. ruv_get_min_csn(ruv, &mincsn);
  1787. ruv_get_max_csn(ruv, &maxcsn);
  1788. if (mincsn) {
  1789. csn_free(&mincsn);
  1790. csn_free(&maxcsn);
  1791. } else if (maxcsn) {
  1792. csn_free(&maxcsn);
  1793. } else {
  1794. retval = PR_FALSE; /* both min and max are false */
  1795. }
  1796. return retval;
  1797. }
  1798. PRBool ruv_has_both_csns(const RUV *ruv)
  1799. {
  1800. PRBool retval = PR_TRUE;
  1801. CSN *mincsn = NULL;
  1802. CSN *maxcsn = NULL;
  1803. ruv_get_min_csn(ruv, &mincsn);
  1804. ruv_get_max_csn(ruv, &maxcsn);
  1805. if (mincsn) {
  1806. csn_free(&mincsn);
  1807. csn_free(&maxcsn);
  1808. } else if (maxcsn) {
  1809. csn_free(&maxcsn);
  1810. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1811. } else {
  1812. retval = PR_FALSE; /* both min and max are false */
  1813. }
  1814. return retval;
  1815. }
  1816. /* Check if the first ruv is newer than the second one */
  1817. PRBool
  1818. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1819. {
  1820. RUV *sruv, *cruv;
  1821. RUVElement *sreplica, *creplica;
  1822. int scookie, ccookie;
  1823. int is_newer = PR_FALSE;
  1824. if ( sruvobj == NULL ) {
  1825. return 0;
  1826. }
  1827. if ( cruvobj == NULL ) {
  1828. return 1;
  1829. }
  1830. sruv = (RUV *) object_get_data ( sruvobj );
  1831. cruv = (RUV *) object_get_data ( cruvobj );
  1832. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1833. sreplica = dl_get_next (sruv->elements, &scookie))
  1834. {
  1835. /* A hub may have a dummy ruv with rid 65535 */
  1836. if ( sreplica->csn == NULL ) continue;
  1837. if ( cruv->elements == NULL )
  1838. {
  1839. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1840. "ruv_is_newer, consumer RUV has no elements\n");
  1841. is_newer=PR_FALSE;
  1842. break;
  1843. }
  1844. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1845. creplica = dl_get_next (cruv->elements, &ccookie))
  1846. {
  1847. if ( sreplica->rid == creplica->rid ) {
  1848. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1849. is_newer = PR_TRUE;
  1850. }
  1851. break;
  1852. }
  1853. }
  1854. if ( creplica == NULL || is_newer ) {
  1855. is_newer = PR_TRUE;
  1856. break;
  1857. }
  1858. }
  1859. return is_newer;
  1860. }
  1861. void
  1862. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1863. {
  1864. CSN *max = NULL;
  1865. if (ruv != NULL)
  1866. {
  1867. ruv_get_max_csn(ruv, &max);
  1868. if (csn_compare(max, csn))
  1869. {
  1870. ruv_set_max_csn(ruv, csn, NULL);
  1871. }
  1872. csn_free(&max);
  1873. }
  1874. }
  1875. /* This routine is used to iterate the elements in a RUV and set each vector's
  1876. + * minimal CSN to a dummy with just the rid set, e.g. 00000000000000010000 */
  1877. void
  1878. ruv_insert_dummy_min_csn (RUV *ruv)
  1879. {
  1880. RUVElement *r;
  1881. int cookie;
  1882. for (r = dl_get_first (ruv->elements, &cookie); r;
  1883. r = dl_get_next (ruv->elements, &cookie)) {
  1884. if (r->csn && !r->min_csn) {
  1885. CSN *dummycsn = csn_new();
  1886. csn_init(dummycsn);
  1887. csn_set_replicaid(dummycsn, csn_get_replicaid(r->csn));
  1888. r->min_csn = dummycsn;
  1889. }
  1890. }
  1891. }
  1892. #ifdef TESTING /* Some unit tests for code in this file */
  1893. static void
  1894. ruv_dump_internal(RUV *ruv)
  1895. {
  1896. RUVElement *replica;
  1897. int cookie;
  1898. char csnstr1[CSN_STRSIZE];
  1899. char csnstr2[CSN_STRSIZE];
  1900. PR_ASSERT(NULL != ruv);
  1901. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1902. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1903. replica = dl_get_next (ruv->elements, &cookie))
  1904. {
  1905. printf("{replica%s%s} %s %s\n",
  1906. replica->replica_purl == NULL ? "" : " ",
  1907. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1908. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1909. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1910. }
  1911. }
  1912. void
  1913. ruv_test()
  1914. {
  1915. const struct berval *vals[5];
  1916. struct berval val0, val1, val2, val3;
  1917. RUV *ruv;
  1918. Slapi_Attr *attr;
  1919. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1920. int rc;
  1921. char csnstr[CSN_STRSIZE];
  1922. char *gen;
  1923. CSN *newcsn;
  1924. ReplicaId *ids;
  1925. int nids;
  1926. Slapi_Mod smods;
  1927. PRBool covers;
  1928. vals[0] = &val0;
  1929. vals[1] = &val1;
  1930. vals[2] = &val2;
  1931. vals[3] = &val3;
  1932. vals[4] = NULL;
  1933. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1934. val0.bv_len = strlen(val0.bv_val);
  1935. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1936. val1.bv_len = strlen(val1.bv_val);
  1937. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1938. val2.bv_len = strlen(val2.bv_val);
  1939. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1940. val3.bv_len = strlen(val3.bv_val);
  1941. rc = ruv_init_from_bervals(vals, &ruv);
  1942. ruv_dump_internal(ruv);
  1943. attr = slapi_attr_new();
  1944. attr = slapi_attr_init(attr, "ruvelement");
  1945. sv0 = slapi_value_new();
  1946. sv1 = slapi_value_new();
  1947. sv2 = slapi_value_new();
  1948. sv3 = slapi_value_new();
  1949. slapi_value_init_berval(sv0, &val0);
  1950. slapi_value_init_berval(sv1, &val1);
  1951. slapi_value_init_berval(sv2, &val2);
  1952. slapi_value_init_berval(sv3, &val3);
  1953. slapi_attr_add_value(attr, sv0);
  1954. slapi_attr_add_value(attr, sv1);
  1955. slapi_attr_add_value(attr, sv2);
  1956. slapi_attr_add_value(attr, sv3);
  1957. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1958. ruv_dump_internal(ruv);
  1959. rc = ruv_delete_replica(ruv, 0xFF);
  1960. /* Should delete one replica */
  1961. ruv_dump_internal(ruv);
  1962. rc = ruv_delete_replica(ruv, 0xAA);
  1963. /* No such replica - should not do anything */
  1964. ruv_dump_internal(ruv);
  1965. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1966. if (NULL != newcsn)
  1967. {
  1968. csn_as_string(newcsn, PR_FALSE, csnstr);
  1969. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1970. }
  1971. else
  1972. {
  1973. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1974. }
  1975. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1976. if (NULL != newcsn)
  1977. {
  1978. csn_as_string(newcsn, PR_FALSE, csnstr);
  1979. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1980. }
  1981. else
  1982. {
  1983. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1984. }
  1985. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1986. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1987. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1988. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1989. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1990. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1991. /* Should update replica FE's CSN */
  1992. ruv_dump_internal(ruv);
  1993. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1994. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1995. /* Should get a new replica in the list with min == max csn */
  1996. ruv_dump_internal(ruv);
  1997. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1998. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1999. /* Should update replica 0xFD so new CSN is newer than min CSN */
  2000. ruv_dump_internal(ruv);
  2001. gen = ruv_get_replica_generation(ruv);
  2002. printf("replica generation is \"%s\"\n", gen);
  2003. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  2004. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  2005. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  2006. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  2007. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  2008. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  2009. ruv_dump_internal(ruv);
  2010. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  2011. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  2012. ruv_dump_internal(ruv);
  2013. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  2014. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  2015. ruv_dump_internal(ruv);
  2016. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  2017. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  2018. ruv_dump_internal(ruv);
  2019. /* ONREPL test ruv enumeration */
  2020. rc = ruv_to_smod(ruv, &smods);
  2021. ruv_destroy(&ruv);
  2022. }
  2023. #endif /* TESTING */