repl5_ruv.c 62 KB

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