repl5_ruv.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  791. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  792. */
  793. static int
  794. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max, ReplicaId rid)
  795. {
  796. int return_value;
  797. if (ruv == NULL || csn == NULL)
  798. {
  799. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  800. return_value = RUV_BAD_DATA;
  801. }
  802. else
  803. {
  804. CSN *found = NULL;
  805. RUVElement *replica;
  806. int cookie;
  807. slapi_rwlock_rdlock (ruv->lock);
  808. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  809. replica = dl_get_next (ruv->elements, &cookie))
  810. {
  811. /*
  812. * Skip replica whose maxcsn is NULL otherwise
  813. * the code will return different min_csn if
  814. * the sequence of the replicas is altered.
  815. *
  816. * don't use READ_ONLY replicas for computing the value of
  817. * "found", as they seem to have NULL csn and min_csn
  818. */
  819. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  820. {
  821. continue;
  822. }
  823. if(rid){ /* we are only interested in this rid's maxcsn */
  824. if(replica->rid == rid){
  825. found = replica->csn;
  826. break;
  827. }
  828. } else {
  829. if (found == NULL ||
  830. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  831. ( get_the_max && csn_compare(found, replica->csn)<0))
  832. {
  833. found = replica->csn;
  834. }
  835. }
  836. }
  837. if (found == NULL)
  838. {
  839. *csn = NULL;
  840. }
  841. else
  842. {
  843. *csn = csn_dup (found);
  844. }
  845. slapi_rwlock_unlock (ruv->lock);
  846. return_value = RUV_SUCCESS;
  847. }
  848. return return_value;
  849. }
  850. int
  851. ruv_get_rid_max_csn(const RUV *ruv, CSN **csn, ReplicaId rid){
  852. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, rid);
  853. }
  854. int
  855. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  856. {
  857. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, 0 /* rid */);
  858. }
  859. int
  860. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  861. {
  862. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */, 0 /* rid */);
  863. }
  864. int
  865. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  866. {
  867. int cookie;
  868. RUVElement *elem;
  869. int rc = 0;
  870. ruv_enum_data enum_data = {0};
  871. if (ruv == NULL || fn == NULL)
  872. {
  873. /* ONREPL - log error */
  874. return -1;
  875. }
  876. slapi_rwlock_rdlock (ruv->lock);
  877. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  878. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  879. {
  880. /* we only return elements that contains both minimal and maximal CSNs */
  881. if (elem->csn && elem->min_csn)
  882. {
  883. enum_data.csn = elem->csn;
  884. enum_data.min_csn = elem->min_csn;
  885. rc = fn (&enum_data, arg);
  886. if (rc != 0)
  887. break;
  888. }
  889. }
  890. slapi_rwlock_unlock (ruv->lock);
  891. return rc;
  892. }
  893. void
  894. ruv_element_to_string(RUVElement *ruvelem, struct berval *bv, char *buf, size_t bufsize)
  895. {
  896. char csnStr1[CSN_STRSIZE];
  897. char csnStr2[CSN_STRSIZE];
  898. const char *fmtstr = "%s%d%s%s}%s%s%s%s";
  899. if (buf && bufsize) {
  900. PR_snprintf(buf, bufsize, fmtstr,
  901. prefix_ruvcsn, ruvelem->rid,
  902. ruvelem->replica_purl == NULL ? "" : " ",
  903. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  904. ruvelem->min_csn == NULL ? "" : " ",
  905. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  906. ruvelem->csn == NULL ? "" : " ",
  907. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  908. } else {
  909. bv->bv_val = slapi_ch_smprintf(fmtstr,
  910. prefix_ruvcsn, ruvelem->rid,
  911. ruvelem->replica_purl == NULL ? "" : " ",
  912. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  913. ruvelem->min_csn == NULL ? "" : " ",
  914. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  915. ruvelem->csn == NULL ? "" : " ",
  916. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  917. bv->bv_len = strlen(bv->bv_val);
  918. }
  919. }
  920. /*
  921. * Convert a replica update vector to a NULL-terminated array
  922. * of bervals. The caller is responsible for freeing the bervals.
  923. */
  924. int
  925. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  926. {
  927. struct berval **returned_bervals = NULL;
  928. int return_value;
  929. if (ruv == NULL || bvals == NULL)
  930. {
  931. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  932. return_value = RUV_BAD_DATA;
  933. }
  934. else
  935. {
  936. int count;
  937. int i;
  938. RUVElement *replica;
  939. int cookie;
  940. slapi_rwlock_rdlock (ruv->lock);
  941. count = dl_get_count (ruv->elements) + 2;
  942. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  943. returned_bervals[count - 1] = NULL;
  944. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  945. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  946. prefix_replicageneration, ruv->replGen);
  947. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  948. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  949. i++, replica = dl_get_next (ruv->elements, &cookie))
  950. {
  951. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  952. ruv_element_to_string(replica, returned_bervals[i], NULL, 0);
  953. }
  954. slapi_rwlock_unlock (ruv->lock);
  955. return_value = RUV_SUCCESS;
  956. *bvals = returned_bervals;
  957. }
  958. return return_value;
  959. }
  960. void
  961. ruv_get_cleaned_rids(RUV *ruv, ReplicaId *rids)
  962. {
  963. RUVElement *replica;
  964. int cookie;
  965. int i = 0;
  966. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  967. replica = dl_get_next (ruv->elements, &cookie))
  968. {
  969. if(is_cleaned_rid(replica->rid)){
  970. rids[i++] = replica->rid;
  971. }
  972. }
  973. }
  974. int
  975. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  976. {
  977. int return_value;
  978. if (ruv == NULL || smod == NULL)
  979. {
  980. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  981. return_value = RUV_BAD_DATA;
  982. }
  983. else
  984. {
  985. struct berval val;
  986. RUVElement *replica;
  987. int cookie;
  988. #define B_SIZ 1024
  989. char buf[B_SIZ];
  990. slapi_rwlock_rdlock (ruv->lock);
  991. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  992. slapi_mod_set_type (smod, type_ruvElement);
  993. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  994. PR_snprintf(buf, sizeof(buf), "%s %s", prefix_replicageneration, ruv->replGen);
  995. val.bv_val = buf;
  996. val.bv_len = strlen(buf);
  997. slapi_mod_add_value(smod, &val);
  998. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  999. replica = dl_get_next (ruv->elements, &cookie))
  1000. {
  1001. ruv_element_to_string(replica, NULL, buf, sizeof(buf));
  1002. val.bv_val = buf;
  1003. val.bv_len = strlen(buf);
  1004. slapi_mod_add_value(smod, &val);
  1005. }
  1006. slapi_rwlock_unlock (ruv->lock);
  1007. return_value = RUV_SUCCESS;
  1008. }
  1009. return return_value;
  1010. }
  1011. int
  1012. ruv_last_modified_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_last_modified_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. char buf[B_SIZ];
  1026. slapi_rwlock_rdlock (ruv->lock);
  1027. slapi_mod_init (smod, dl_get_count (ruv->elements));
  1028. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  1029. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1030. val.bv_val = buf;
  1031. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1032. replica = dl_get_next (ruv->elements, &cookie))
  1033. {
  1034. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  1035. replica->replica_purl == NULL ? "" : " ",
  1036. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1037. replica->last_modified);
  1038. val.bv_len = strlen(buf);
  1039. slapi_mod_add_value(smod, &val);
  1040. }
  1041. slapi_rwlock_unlock (ruv->lock);
  1042. return_value = RUV_SUCCESS;
  1043. }
  1044. return return_value;
  1045. }
  1046. /*
  1047. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  1048. PRBool
  1049. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  1050. {
  1051. PRBool return_value = PR_TRUE;
  1052. RUVElement *replica;
  1053. int cookie;
  1054. /* compare replica generations first */
  1055. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  1056. return PR_FALSE;
  1057. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1058. return PR_FALSE;
  1059. /* replica generation is the same, now compare element by element */
  1060. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1061. NULL != replica;
  1062. replica = dl_get_next (covered_ruv->elements, &cookie))
  1063. {
  1064. if (replica->csn &&
  1065. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1066. {
  1067. return_value = PR_FALSE;
  1068. /* Don't break here - may leave something referenced? */
  1069. }
  1070. }
  1071. return return_value;
  1072. }
  1073. /*
  1074. * This compares two ruvs to see if they are compatible. This is
  1075. * used, for example, when the data is reloaded, to see if the ruv
  1076. * from the database is compatible with the ruv from the changelog.
  1077. * If the replica generation is empty or does not match, the data
  1078. * is not compatible.
  1079. * If the maxcsns are not compatible, the ruvs are not compatible.
  1080. * However, if the first ruv has replica IDs that the second RUV
  1081. * does not have, and this is the only difference, the application
  1082. * may allow that with a warning.
  1083. */
  1084. int
  1085. ruv_compare_ruv(const RUV *ruv1, const char *ruv1name, const RUV *ruv2, const char *ruv2name, int strict, int loglevel)
  1086. {
  1087. int rc = 0;
  1088. int ii = 0;
  1089. const RUV *ruvalist[] = {ruv1, ruv2};
  1090. const RUV *ruvblist[] = {ruv2, ruv1};
  1091. int missinglist[2] = {0, 0};
  1092. const char *ruvanames[] = {ruv1name, ruv2name};
  1093. const char *ruvbnames[] = {ruv2name, ruv1name};
  1094. const int nitems = 2;
  1095. /* compare replica generations first */
  1096. if (ruv1->replGen == NULL || ruv2->replGen == NULL) {
  1097. slapi_log_error(loglevel, repl_plugin_name,
  1098. "ruv_compare_ruv: RUV [%s] is missing the replica generation\n",
  1099. ruv1->replGen ? ruv2name : ruv1name);
  1100. return RUV_COMP_NO_GENERATION;
  1101. }
  1102. if (strcasecmp (ruv1->replGen, ruv2->replGen)) {
  1103. slapi_log_error(loglevel, repl_plugin_name,
  1104. "ruv_compare_ruv: RUV [%s] replica generation [%s] does not match RUV [%s] [%s]\n",
  1105. ruv1name, ruv1->replGen, ruv2name, ruv2->replGen);
  1106. return RUV_COMP_GENERATION_DIFFERS;
  1107. }
  1108. /* replica generation is the same, now compare element by element */
  1109. for (ii = 0; ii < nitems; ++ii) {
  1110. int cookie;
  1111. const RUV *ruva = ruvalist[ii];
  1112. const RUV *ruvb = ruvblist[ii];
  1113. int *missing = &missinglist[ii];
  1114. const char *ruvaname = ruvanames[ii];
  1115. const char *ruvbname = ruvbnames[ii];
  1116. RUVElement *replicab;
  1117. for (replicab = dl_get_first (ruvb->elements, &cookie);
  1118. NULL != replicab;
  1119. replicab = dl_get_next (ruvb->elements, &cookie)) {
  1120. if (replicab->csn) {
  1121. ReplicaId rid = csn_get_replicaid(replicab->csn);
  1122. RUVElement *replicaa = ruvGetReplica(ruva, rid);
  1123. char csnstra[CSN_STRSIZE];
  1124. char csnstrb[CSN_STRSIZE];
  1125. char ruvelem[1024];
  1126. ruv_element_to_string(replicab, NULL, ruvelem, sizeof(ruvelem));
  1127. csn_as_string(replicab->csn, PR_FALSE, csnstrb);
  1128. if (replicaa == NULL) {
  1129. (*missing)++;
  1130. slapi_log_error(loglevel, repl_plugin_name,
  1131. "ruv_compare_ruv: RUV [%s] does not contain element [%s] "
  1132. "which is present in RUV [%s]\n",
  1133. ruvaname, ruvelem, ruvbname);
  1134. } else if (strict && (csn_compare (replicab->csn, replicaa->csn) >= 0)) {
  1135. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1136. slapi_log_error(loglevel, repl_plugin_name,
  1137. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1138. "than or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
  1139. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1140. rc = RUV_COMP_CSN_DIFFERS;
  1141. } else if (csn_compare (replicab->csn, replicaa->csn) > 0) {
  1142. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1143. slapi_log_error(loglevel, repl_plugin_name,
  1144. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1145. "than the max CSN [%s] from RUV [%s] for element [%s]\n",
  1146. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1147. rc = RUV_COMP_CSN_DIFFERS;
  1148. }
  1149. }
  1150. }
  1151. }
  1152. if (!rc) {
  1153. if (missinglist[0]) {
  1154. rc = RUV_COMP_RUV1_MISSING;
  1155. } else if (missinglist[1]) {
  1156. rc = RUV_COMP_RUV2_MISSING;
  1157. }
  1158. }
  1159. return rc;
  1160. }
  1161. PRInt32
  1162. ruv_replica_count (const RUV *ruv)
  1163. {
  1164. if (ruv == NULL)
  1165. return 0;
  1166. else
  1167. {
  1168. int count;
  1169. slapi_rwlock_rdlock (ruv->lock);
  1170. count = dl_get_count (ruv->elements);
  1171. slapi_rwlock_unlock (ruv->lock);
  1172. return count;
  1173. }
  1174. }
  1175. /*
  1176. * Extract all the referral URL's from the RUV (but self URL),
  1177. * returning them in an array of strings, that
  1178. * the caller must free.
  1179. */
  1180. char **
  1181. ruv_get_referrals(const RUV *ruv)
  1182. {
  1183. char **r= NULL;
  1184. int n;
  1185. const char *mypurl = multimaster_get_local_purl();
  1186. slapi_rwlock_rdlock (ruv->lock);
  1187. n = ruv_replica_count(ruv);
  1188. if(n>0)
  1189. {
  1190. RUVElement *replica;
  1191. int cookie;
  1192. int i= 0;
  1193. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1194. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1195. replica = dl_get_next (ruv->elements, &cookie))
  1196. {
  1197. /* Add URL into referrals if doesn't match self URL */
  1198. if((replica->replica_purl!=NULL) &&
  1199. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1200. (unsigned char *)mypurl) != 0))
  1201. {
  1202. r[i]= slapi_ch_strdup(replica->replica_purl);
  1203. i++;
  1204. }
  1205. }
  1206. }
  1207. slapi_rwlock_unlock (ruv->lock);
  1208. return r; /* Caller must free this */
  1209. }
  1210. void
  1211. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1212. {
  1213. RUVElement *replica;
  1214. int cookie;
  1215. char csnstr1[CSN_STRSIZE];
  1216. char csnstr2[CSN_STRSIZE];
  1217. char buff[RUVSTR_SIZE];
  1218. int len = sizeof (buff);
  1219. PR_ASSERT(NULL != ruv);
  1220. if (!slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1221. return;
  1222. }
  1223. slapi_rwlock_rdlock (ruv->lock);
  1224. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1225. ruv_name ? ruv_name : type_ruvElement,
  1226. ruv->replGen == NULL ? "" : ruv->replGen);
  1227. if (prFile)
  1228. {
  1229. slapi_write_buffer (prFile, buff, strlen(buff));
  1230. }
  1231. else
  1232. {
  1233. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1234. }
  1235. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1236. replica = dl_get_next (ruv->elements, &cookie))
  1237. {
  1238. /* prefix_ruvcsn = "{replica " */
  1239. PR_snprintf (buff, len, "%s: %s%d%s%s}%s%s%s%s\n",
  1240. ruv_name ? ruv_name : type_ruvElement,
  1241. prefix_ruvcsn, replica->rid,
  1242. replica->replica_purl == NULL ? "" : " ",
  1243. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1244. replica->min_csn == NULL ? "" : " ",
  1245. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1246. replica->csn == NULL ? "" : " ",
  1247. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1248. if (strlen (csnstr1) > 0) {
  1249. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1250. replica->last_modified);
  1251. }
  1252. if (prFile)
  1253. {
  1254. slapi_write_buffer (prFile, buff, strlen(buff));
  1255. }
  1256. else
  1257. {
  1258. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1259. }
  1260. }
  1261. slapi_rwlock_unlock (ruv->lock);
  1262. }
  1263. /* this function notifies the ruv that there are operations in progress so that
  1264. they can be added to the pending list for the appropriate client. */
  1265. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1266. {
  1267. RUVElement* replica;
  1268. char csn_str[CSN_STRSIZE];
  1269. int rc = RUV_SUCCESS;
  1270. int rid = csn_get_replicaid (csn);
  1271. PR_ASSERT (ruv && csn);
  1272. /* locate ruvElement */
  1273. slapi_rwlock_wrlock (ruv->lock);
  1274. if(is_cleaned_rid(rid)){
  1275. /* return success because we want to consume the update, but not perform it */
  1276. rc = RUV_COVERS_CSN;
  1277. goto done;
  1278. }
  1279. replica = ruvGetReplica (ruv, rid);
  1280. if (replica == NULL)
  1281. {
  1282. replica = ruvAddReplicaNoCSN (ruv, rid, NULL/*purl*/);
  1283. if (replica == NULL)
  1284. {
  1285. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1286. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1287. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1288. }
  1289. rc = RUV_MEMORY_ERROR;
  1290. goto done;
  1291. }
  1292. }
  1293. /* check first that this csn is not already covered by this RUV */
  1294. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1295. {
  1296. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1297. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1298. "the csn %s has already be seen - ignoring\n",
  1299. csn_as_string (csn, PR_FALSE, csn_str));
  1300. }
  1301. rc = RUV_COVERS_CSN;
  1302. goto done;
  1303. }
  1304. rc = csnplInsert (replica->csnpl, csn);
  1305. if (rc == 1) /* we already seen this csn */
  1306. {
  1307. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1308. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1309. "the csn %s has already be seen - ignoring\n",
  1310. csn_as_string (csn, PR_FALSE, csn_str));
  1311. }
  1312. rc = RUV_COVERS_CSN;
  1313. }
  1314. else if(rc != 0)
  1315. {
  1316. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1317. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1318. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1319. }
  1320. rc = RUV_UNKNOWN_ERROR;
  1321. }
  1322. else
  1323. {
  1324. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1325. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1326. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1327. }
  1328. rc = RUV_SUCCESS;
  1329. }
  1330. done:
  1331. slapi_rwlock_unlock (ruv->lock);
  1332. return rc;
  1333. }
  1334. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1335. {
  1336. RUVElement* replica;
  1337. int rc = RUV_SUCCESS;
  1338. PR_ASSERT (ruv && csn);
  1339. /* locate ruvElement */
  1340. slapi_rwlock_wrlock (ruv->lock);
  1341. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1342. if (replica == NULL)
  1343. {
  1344. /* ONREPL - log error */
  1345. rc = RUV_NOTFOUND;
  1346. goto done;
  1347. }
  1348. rc = csnplRemove (replica->csnpl, csn);
  1349. if (rc != 0)
  1350. rc = RUV_NOTFOUND;
  1351. else
  1352. rc = RUV_SUCCESS;
  1353. done:
  1354. slapi_rwlock_unlock (ruv->lock);
  1355. return rc;
  1356. }
  1357. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1358. {
  1359. int rc=RUV_SUCCESS;
  1360. char csn_str[CSN_STRSIZE];
  1361. CSN *max_csn;
  1362. CSN *first_csn = NULL;
  1363. RUVElement *replica;
  1364. PR_ASSERT (ruv && csn);
  1365. slapi_rwlock_wrlock (ruv->lock);
  1366. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1367. if (replica == NULL)
  1368. {
  1369. /* we should have a ruv element at this point because it would have
  1370. been added by ruv_add_inprogress function */
  1371. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1372. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1373. goto done;
  1374. }
  1375. if (csnplCommit(replica->csnpl, csn) != 0)
  1376. {
  1377. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1378. csn_as_string(csn, PR_FALSE, csn_str));
  1379. rc = RUV_CSNPL_ERROR;
  1380. goto done;
  1381. }
  1382. else
  1383. {
  1384. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1385. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1386. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1387. }
  1388. }
  1389. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1390. {
  1391. #ifdef DEBUG
  1392. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1393. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1394. #endif
  1395. /* replica object sets min csn for local replica */
  1396. if (!isLocal && replica->min_csn == NULL) {
  1397. /* bug 559223 - it seems that, under huge stress, a server might pass
  1398. * through this code when more than 1 change has already been sent and commited into
  1399. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1400. * generated by this replica, we need to set the first_csn as the min csn in the
  1401. * ruv */
  1402. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1403. }
  1404. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1405. /* It is possible that first_csn points to max_csn.
  1406. We need to free it once */
  1407. if (max_csn != first_csn) {
  1408. csn_free(&first_csn);
  1409. }
  1410. csn_free(&max_csn);
  1411. }
  1412. done:
  1413. slapi_rwlock_unlock (ruv->lock);
  1414. return rc;
  1415. }
  1416. /* Helper functions */
  1417. static int
  1418. ruvInit (RUV **ruv, int initCount)
  1419. {
  1420. PR_ASSERT (ruv);
  1421. if (ruv == NULL) {
  1422. return RUV_NSPR_ERROR;
  1423. }
  1424. /* allocate new RUV */
  1425. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1426. /* allocate elements */
  1427. (*ruv)->elements = dl_new (); /* never returns NULL */
  1428. dl_init ((*ruv)->elements, initCount);
  1429. /* create lock */
  1430. (*ruv)->lock = slapi_new_rwlock();
  1431. if ((*ruv)->lock == NULL)
  1432. {
  1433. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1434. "ruvInit: failed to create lock\n");
  1435. dl_free(&(*ruv)->elements);
  1436. slapi_ch_free((void**)ruv);
  1437. return RUV_NSPR_ERROR;
  1438. }
  1439. return RUV_SUCCESS;
  1440. }
  1441. static void
  1442. ruvFreeReplica (void **data)
  1443. {
  1444. RUVElement *element = *(RUVElement**)data;
  1445. if (NULL != element)
  1446. {
  1447. if (NULL != element->csn)
  1448. {
  1449. csn_free (&element->csn);
  1450. }
  1451. if (NULL != element->min_csn)
  1452. {
  1453. csn_free (&element->min_csn);
  1454. }
  1455. /* slapi_ch_free accepts NULL pointer */
  1456. slapi_ch_free((void **)&element->replica_purl);
  1457. if (element->csnpl)
  1458. {
  1459. csnplFree (&(element->csnpl));
  1460. }
  1461. slapi_ch_free ((void **)&element);
  1462. }
  1463. }
  1464. static RUVElement*
  1465. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1466. {
  1467. RUVElement *replica;
  1468. PR_ASSERT (NULL != ruv);
  1469. PR_ASSERT (NULL != csn);
  1470. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1471. if (replica == NULL)
  1472. {
  1473. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1474. "ruvAddReplica: memory allocation failed\n");
  1475. return NULL;
  1476. }
  1477. replica->rid = csn_get_replicaid (csn);
  1478. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1479. replica->csn = csn_dup (csn);
  1480. replica->last_modified = current_time();
  1481. replica->min_csn = csn_dup (csn);
  1482. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1483. replica->csnpl = csnplNew ();
  1484. dl_add (ruv->elements, replica);
  1485. return replica;
  1486. }
  1487. static RUVElement*
  1488. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1489. {
  1490. RUVElement *replica;
  1491. PR_ASSERT (NULL != ruv);
  1492. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1493. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1494. if (replica == NULL)
  1495. {
  1496. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1497. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1498. return NULL;
  1499. }
  1500. replica->rid = rid;
  1501. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1502. replica->csnpl = csnplNew ();
  1503. dl_add (ruv->elements, replica);
  1504. return replica;
  1505. }
  1506. static RUVElement*
  1507. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1508. {
  1509. RUVElement *replica;
  1510. PR_ASSERT (NULL != ruv);
  1511. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1512. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1513. if (replica == NULL)
  1514. {
  1515. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1516. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1517. return NULL;
  1518. }
  1519. replica->rid = rid;
  1520. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1521. replica->csnpl = csnplNew ();
  1522. dl_add_index (ruv->elements, replica, index);
  1523. return replica;
  1524. }
  1525. static RUVElement *
  1526. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1527. {
  1528. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1529. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1530. }
  1531. static int
  1532. ruvReplicaCompare (const void *el1, const void *el2)
  1533. {
  1534. RUVElement *replica = (RUVElement*)el1;
  1535. ReplicaId *rid1 = (ReplicaId*) el2;
  1536. if (replica == NULL || rid1 == NULL)
  1537. return -1;
  1538. if (*rid1 == replica->rid)
  1539. return 0;
  1540. if (*rid1 < replica->rid)
  1541. return -1;
  1542. else
  1543. return 1;
  1544. }
  1545. /*
  1546. * Given a berval that points to a string of the form:
  1547. * "{dbgen} generation-id", return a copy of the
  1548. * "generation-id" part in a null-terminated string.
  1549. * Returns NULL if the berval is malformed.
  1550. */
  1551. static char *
  1552. get_replgen_from_berval(const struct berval *bval)
  1553. {
  1554. char *ret_string = NULL;
  1555. if (NULL != bval && NULL != bval->bv_val &&
  1556. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1557. strncasecmp(bval->bv_val, prefix_replicageneration,
  1558. strlen(prefix_replicageneration)) == 0)
  1559. {
  1560. unsigned int index = strlen(prefix_replicageneration);
  1561. /* Skip any whitespace */
  1562. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1563. if (index < bval->bv_len)
  1564. {
  1565. unsigned int ret_len = bval->bv_len - index;
  1566. ret_string = slapi_ch_malloc(ret_len + 1);
  1567. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1568. ret_string[ret_len] = '\0';
  1569. }
  1570. }
  1571. return ret_string;
  1572. }
  1573. /*
  1574. * Given a berval that points to a string of the form:
  1575. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1576. * partial URL and the CSNs into an RUVElement, and return
  1577. * a pointer to the copy. Returns NULL if the berval is
  1578. * malformed.
  1579. */
  1580. static RUVElement *
  1581. get_ruvelement_from_berval(const struct berval *bval)
  1582. {
  1583. RUVElement *ret_ruve = NULL;
  1584. char *purl = NULL;
  1585. ReplicaId rid = 0;
  1586. char ridbuff [RIDSTR_SIZE];
  1587. int i;
  1588. if (NULL == bval || NULL == bval->bv_val ||
  1589. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1590. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1591. {
  1592. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1593. "get_ruvelement_from_berval: invalid berval\n");
  1594. goto loser;
  1595. }
  1596. {
  1597. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1598. unsigned int urlend;
  1599. unsigned int mincsnbegin;
  1600. /* replica id must be here */
  1601. i = 0;
  1602. while (isdigit (bval->bv_val[urlbegin]))
  1603. {
  1604. ridbuff [i] = bval->bv_val[urlbegin];
  1605. i++;
  1606. urlbegin ++;
  1607. }
  1608. if (i == 0) /* replicaid is missing */
  1609. goto loser;
  1610. ridbuff[i] = '\0';
  1611. rid = atoi (ridbuff);
  1612. if (bval->bv_val[urlbegin] == '}')
  1613. {
  1614. /* No purl in this value */
  1615. purl = NULL;
  1616. mincsnbegin = urlbegin + 1;
  1617. }
  1618. else
  1619. {
  1620. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1621. urlend = urlbegin;
  1622. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1623. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1624. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1625. purl[urlend - urlbegin] = '\0';
  1626. mincsnbegin = urlend;
  1627. }
  1628. /* Skip any whitespace before the first (minimum) CSN */
  1629. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1630. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1631. if (mincsnbegin >= bval->bv_len)
  1632. {
  1633. /* Missing the entire content*/
  1634. if (purl == NULL)
  1635. goto loser;
  1636. else /* we have just purl - no changes from the replica has been seen */
  1637. {
  1638. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1639. ret_ruve->rid = rid;
  1640. ret_ruve->replica_purl = purl;
  1641. }
  1642. }
  1643. else
  1644. {
  1645. if ((bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1646. && (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 10))
  1647. {
  1648. /* Malformed - incorrect length for 2 CSNs + space AND
  1649. * incorrect length for 2 CSNs + 2 spaces +
  1650. * last_modified (see ruv_dump) */
  1651. goto loser;
  1652. }
  1653. else
  1654. {
  1655. char mincsnstr[CSN_STRSIZE];
  1656. char maxcsnstr[CSN_STRSIZE];
  1657. memset(mincsnstr, '\0', CSN_STRSIZE);
  1658. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1659. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1660. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1661. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1662. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1663. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1664. ret_ruve->rid = rid;
  1665. ret_ruve->replica_purl = purl;
  1666. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1667. {
  1668. goto loser;
  1669. }
  1670. }
  1671. }
  1672. }
  1673. /* initialize csn pending list */
  1674. ret_ruve->csnpl = csnplNew ();
  1675. if (ret_ruve->csnpl == NULL)
  1676. {
  1677. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1678. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1679. goto loser;
  1680. }
  1681. return ret_ruve;
  1682. loser:
  1683. /* slapi_ch_free accepts NULL pointer */
  1684. slapi_ch_free((void **)&purl);
  1685. if (NULL != ret_ruve)
  1686. {
  1687. if (NULL != ret_ruve->min_csn)
  1688. {
  1689. csn_free(&ret_ruve->min_csn);
  1690. }
  1691. if (NULL != ret_ruve->csn)
  1692. {
  1693. csn_free(&ret_ruve->csn);
  1694. }
  1695. slapi_ch_free((void **)&ret_ruve);
  1696. }
  1697. return NULL;
  1698. }
  1699. int
  1700. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1701. {
  1702. RUVElement * elem = NULL;
  1703. int rc = RUV_NOTFOUND;
  1704. PR_ASSERT(ruv);
  1705. slapi_rwlock_wrlock (ruv->lock);
  1706. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1707. if (elem) {
  1708. dl_add_index(ruv->elements, elem, 1);
  1709. rc = RUV_SUCCESS;
  1710. }
  1711. slapi_rwlock_unlock (ruv->lock);
  1712. return rc;
  1713. }
  1714. int
  1715. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1716. {
  1717. RUVElement * first = NULL;
  1718. int cookie;
  1719. int rc;
  1720. PR_ASSERT(ruv);
  1721. slapi_rwlock_rdlock (ruv->lock);
  1722. first = dl_get_first(ruv->elements, &cookie);
  1723. if ( first == NULL )
  1724. {
  1725. rc = RUV_MEMORY_ERROR;
  1726. }
  1727. else
  1728. {
  1729. *rid = first->rid;
  1730. *replica_purl = first->replica_purl;
  1731. rc = RUV_SUCCESS;
  1732. }
  1733. slapi_rwlock_unlock (ruv->lock);
  1734. return rc;
  1735. }
  1736. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1737. {
  1738. int cookie;
  1739. RUVElement *elem = NULL;
  1740. PR_ASSERT(ruv);
  1741. slapi_rwlock_rdlock (ruv->lock);
  1742. for (elem = dl_get_first (ruv->elements, &cookie);
  1743. elem;
  1744. elem = dl_get_next (ruv->elements, &cookie))
  1745. {
  1746. if (elem->rid == rid){
  1747. slapi_rwlock_unlock (ruv->lock);
  1748. return 1;
  1749. }
  1750. }
  1751. slapi_rwlock_unlock (ruv->lock);
  1752. return 0;
  1753. }
  1754. PRBool ruv_has_csns(const RUV *ruv)
  1755. {
  1756. PRBool retval = PR_TRUE;
  1757. CSN *mincsn = NULL;
  1758. CSN *maxcsn = NULL;
  1759. ruv_get_min_csn(ruv, &mincsn);
  1760. ruv_get_max_csn(ruv, &maxcsn);
  1761. if (mincsn) {
  1762. csn_free(&mincsn);
  1763. csn_free(&maxcsn);
  1764. } else if (maxcsn) {
  1765. csn_free(&maxcsn);
  1766. } else {
  1767. retval = PR_FALSE; /* both min and max are false */
  1768. }
  1769. return retval;
  1770. }
  1771. PRBool ruv_has_both_csns(const RUV *ruv)
  1772. {
  1773. PRBool retval = PR_TRUE;
  1774. CSN *mincsn = NULL;
  1775. CSN *maxcsn = NULL;
  1776. ruv_get_min_csn(ruv, &mincsn);
  1777. ruv_get_max_csn(ruv, &maxcsn);
  1778. if (mincsn) {
  1779. csn_free(&mincsn);
  1780. csn_free(&maxcsn);
  1781. } else if (maxcsn) {
  1782. csn_free(&maxcsn);
  1783. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1784. } else {
  1785. retval = PR_FALSE; /* both min and max are false */
  1786. }
  1787. return retval;
  1788. }
  1789. /* Check if the first ruv is newer than the second one */
  1790. PRBool
  1791. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1792. {
  1793. RUV *sruv, *cruv;
  1794. RUVElement *sreplica, *creplica;
  1795. int scookie, ccookie;
  1796. int is_newer = PR_FALSE;
  1797. if ( sruvobj == NULL ) {
  1798. return 0;
  1799. }
  1800. if ( cruvobj == NULL ) {
  1801. return 1;
  1802. }
  1803. sruv = (RUV *) object_get_data ( sruvobj );
  1804. cruv = (RUV *) object_get_data ( cruvobj );
  1805. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1806. sreplica = dl_get_next (sruv->elements, &scookie))
  1807. {
  1808. /* A hub may have a dummy ruv with rid 65535 */
  1809. if ( sreplica->csn == NULL ) continue;
  1810. if ( cruv->elements == NULL )
  1811. {
  1812. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1813. "ruv_is_newer, consumer RUV has no elements\n");
  1814. is_newer=PR_FALSE;
  1815. break;
  1816. }
  1817. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1818. creplica = dl_get_next (cruv->elements, &ccookie))
  1819. {
  1820. if ( sreplica->rid == creplica->rid ) {
  1821. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1822. is_newer = PR_TRUE;
  1823. }
  1824. break;
  1825. }
  1826. }
  1827. if ( creplica == NULL || is_newer ) {
  1828. is_newer = PR_TRUE;
  1829. break;
  1830. }
  1831. }
  1832. return is_newer;
  1833. }
  1834. void
  1835. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1836. {
  1837. CSN *max = NULL;
  1838. if (ruv != NULL)
  1839. {
  1840. ruv_get_max_csn(ruv, &max);
  1841. if (csn_compare(max, csn))
  1842. {
  1843. ruv_set_max_csn(ruv, csn, NULL);
  1844. }
  1845. csn_free(&max);
  1846. }
  1847. }
  1848. /* This routine is used to iterate the elements in a RUV and set each vector's
  1849. + * minimal CSN to a dummy with just the rid set, e.g. 00000000000000010000 */
  1850. void
  1851. ruv_insert_dummy_min_csn (RUV *ruv)
  1852. {
  1853. RUVElement *r;
  1854. int cookie;
  1855. for (r = dl_get_first (ruv->elements, &cookie); r;
  1856. r = dl_get_next (ruv->elements, &cookie)) {
  1857. if (r->csn && !r->min_csn) {
  1858. CSN *dummycsn = csn_new();
  1859. csn_init(dummycsn);
  1860. csn_set_replicaid(dummycsn, csn_get_replicaid(r->csn));
  1861. r->min_csn = dummycsn;
  1862. }
  1863. }
  1864. }
  1865. #ifdef TESTING /* Some unit tests for code in this file */
  1866. static void
  1867. ruv_dump_internal(RUV *ruv)
  1868. {
  1869. RUVElement *replica;
  1870. int cookie;
  1871. char csnstr1[CSN_STRSIZE];
  1872. char csnstr2[CSN_STRSIZE];
  1873. PR_ASSERT(NULL != ruv);
  1874. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1875. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1876. replica = dl_get_next (ruv->elements, &cookie))
  1877. {
  1878. printf("{replica%s%s} %s %s\n",
  1879. replica->replica_purl == NULL ? "" : " ",
  1880. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1881. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1882. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1883. }
  1884. }
  1885. void
  1886. ruv_test()
  1887. {
  1888. const struct berval *vals[5];
  1889. struct berval val0, val1, val2, val3;
  1890. RUV *ruv;
  1891. Slapi_Attr *attr;
  1892. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1893. int rc;
  1894. char csnstr[CSN_STRSIZE];
  1895. char *gen;
  1896. CSN *newcsn;
  1897. ReplicaId *ids;
  1898. int nids;
  1899. Slapi_Mod smods;
  1900. PRBool covers;
  1901. vals[0] = &val0;
  1902. vals[1] = &val1;
  1903. vals[2] = &val2;
  1904. vals[3] = &val3;
  1905. vals[4] = NULL;
  1906. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1907. val0.bv_len = strlen(val0.bv_val);
  1908. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1909. val1.bv_len = strlen(val1.bv_val);
  1910. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1911. val2.bv_len = strlen(val2.bv_val);
  1912. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1913. val3.bv_len = strlen(val3.bv_val);
  1914. rc = ruv_init_from_bervals(vals, &ruv);
  1915. ruv_dump_internal(ruv);
  1916. attr = slapi_attr_new();
  1917. attr = slapi_attr_init(attr, "ruvelement");
  1918. sv0 = slapi_value_new();
  1919. sv1 = slapi_value_new();
  1920. sv2 = slapi_value_new();
  1921. sv3 = slapi_value_new();
  1922. slapi_value_init_berval(sv0, &val0);
  1923. slapi_value_init_berval(sv1, &val1);
  1924. slapi_value_init_berval(sv2, &val2);
  1925. slapi_value_init_berval(sv3, &val3);
  1926. slapi_attr_add_value(attr, sv0);
  1927. slapi_attr_add_value(attr, sv1);
  1928. slapi_attr_add_value(attr, sv2);
  1929. slapi_attr_add_value(attr, sv3);
  1930. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1931. ruv_dump_internal(ruv);
  1932. rc = ruv_delete_replica(ruv, 0xFF);
  1933. /* Should delete one replica */
  1934. ruv_dump_internal(ruv);
  1935. rc = ruv_delete_replica(ruv, 0xAA);
  1936. /* No such replica - should not do anything */
  1937. ruv_dump_internal(ruv);
  1938. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1939. if (NULL != newcsn)
  1940. {
  1941. csn_as_string(newcsn, PR_FALSE, csnstr);
  1942. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1943. }
  1944. else
  1945. {
  1946. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1947. }
  1948. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1949. if (NULL != newcsn)
  1950. {
  1951. csn_as_string(newcsn, PR_FALSE, csnstr);
  1952. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1953. }
  1954. else
  1955. {
  1956. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1957. }
  1958. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1959. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1960. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1961. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1962. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1963. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1964. /* Should update replica FE's CSN */
  1965. ruv_dump_internal(ruv);
  1966. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1967. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1968. /* Should get a new replica in the list with min == max csn */
  1969. ruv_dump_internal(ruv);
  1970. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1971. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1972. /* Should update replica 0xFD so new CSN is newer than min CSN */
  1973. ruv_dump_internal(ruv);
  1974. gen = ruv_get_replica_generation(ruv);
  1975. printf("replica generation is \"%s\"\n", gen);
  1976. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  1977. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  1978. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  1979. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  1980. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  1981. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1982. ruv_dump_internal(ruv);
  1983. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  1984. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1985. ruv_dump_internal(ruv);
  1986. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  1987. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1988. ruv_dump_internal(ruv);
  1989. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  1990. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1991. ruv_dump_internal(ruv);
  1992. /* ONREPL test ruv enumeration */
  1993. rc = ruv_to_smod(ruv, &smods);
  1994. ruv_destroy(&ruv);
  1995. }
  1996. #endif /* TESTING */