repl5_replica_config.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_replica_config.c - replica configuration over ldap */
  42. #include <ctype.h> /* for isdigit() */
  43. #include "repl.h" /* ONREPL - this is bad */
  44. #include "repl5.h"
  45. #include "cl5_api.h"
  46. #include "cl5.h"
  47. #include "slap.h"
  48. /* CONFIG_BASE: no need to optimize */
  49. #define CONFIG_BASE "cn=mapping tree,cn=config"
  50. #define CONFIG_FILTER "(objectclass=nsDS5Replica)"
  51. #define TASK_ATTR "nsds5Task"
  52. #define CL2LDIF_TASK "CL2LDIF"
  53. #define LDIF2CL_TASK "LDIF2CL"
  54. #define CLEANRUV "CLEANRUV"
  55. #define CLEANRUVLEN 8
  56. #define CLEANALLRUV "CLEANALLRUV"
  57. #define CLEANALLRUVLEN 11
  58. #define REPLICA_RDN "cn=replica"
  59. #define CLEANALLRUV_ID "CleanAllRUV Task"
  60. #define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
  61. int slapi_log_urp = SLAPI_LOG_REPL;
  62. static ReplicaId cleaned_rids[CLEANRIDSIZ + 1] = {0};
  63. static ReplicaId pre_cleaned_rids[CLEANRIDSIZ + 1] = {0};
  64. static ReplicaId aborted_rids[CLEANRIDSIZ + 1] = {0};
  65. static Slapi_RWLock *rid_lock = NULL;
  66. static Slapi_RWLock *abort_rid_lock = NULL;
  67. static PRLock *notify_lock = NULL;
  68. static PRCondVar *notify_cvar = NULL;
  69. /* Forward Declartions */
  70. static int replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  71. static int replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  72. static int replica_config_post_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  73. static int replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  74. static int replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  75. static int replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  76. static int replica_config_change_type_and_id (Replica *r, const char *new_type, const char *new_id, char *returntext, int apply_mods);
  77. static int replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  78. static int replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext, int apply_mods);
  79. static int replica_config_change_flags (Replica *r, const char *new_flags, char *returntext, int apply_mods);
  80. static int replica_execute_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  81. static int replica_execute_cl2ldif_task (Object *r, char *returntext);
  82. static int replica_execute_ldif2cl_task (Object *r, char *returntext);
  83. static int replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext);
  84. static int replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char *force_cleaning, char *returntext);
  85. static void replica_cleanallruv_thread(void *arg);
  86. static void replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data);
  87. static int check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task);
  88. static int check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn);
  89. static int replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *data, int check_result);
  90. static int replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload);
  91. static int replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task);
  92. static int replica_cleanallruv_replica_alive(Repl_Agmt *agmt);
  93. static int replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *ra, char *rid_text, Slapi_Task *task);
  94. static int get_cleanruv_task_count();
  95. static int get_abort_cleanruv_task_count();
  96. static int replica_cleanup_task (Object *r, const char *task_name, char *returntext, int apply_mods);
  97. static int replica_task_done(Replica *replica);
  98. static void delete_cleaned_rid_config(cleanruv_data *data);
  99. static int replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task);
  100. static void check_replicas_are_done_cleaning(cleanruv_data *data);
  101. static void check_replicas_are_done_aborting(cleanruv_data *data );
  102. static CSN* replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn);
  103. static int replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn);
  104. static void preset_cleaned_rid(ReplicaId rid);
  105. static multimaster_mtnode_extension * _replica_config_get_mtnode_ext (const Slapi_Entry *e);
  106. static void replica_cleanall_ruv_destructor(Slapi_Task *task);
  107. static void replica_cleanall_ruv_abort_destructor(Slapi_Task *task);
  108. /*
  109. * Note: internal add/modify/delete operations should not be run while
  110. * s_configLock is held. E.g., slapi_modify_internal_pb via replica_task_done
  111. * in replica_config_post_modify.
  112. */
  113. static PRLock *s_configLock;
  114. static int
  115. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  116. {
  117. *returncode = LDAP_UNWILLING_TO_PERFORM;
  118. return SLAPI_DSE_CALLBACK_ERROR;
  119. }
  120. int
  121. replica_config_init()
  122. {
  123. s_configLock = PR_NewLock ();
  124. if (s_configLock == NULL)
  125. {
  126. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  127. "failed to create configuration lock; NSPR error - %d\n",
  128. PR_GetError ());
  129. return -1;
  130. }
  131. rid_lock = slapi_new_rwlock();
  132. if(rid_lock == NULL)
  133. {
  134. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  135. "failed to create rid_lock; NSPR error - %d\n", PR_GetError ());
  136. return -1;
  137. }
  138. abort_rid_lock = slapi_new_rwlock();
  139. if(abort_rid_lock == NULL)
  140. {
  141. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  142. "failed to create abort_rid_lock; NSPR error - %d\n", PR_GetError ());
  143. return -1;
  144. }
  145. if ( ( notify_lock = PR_NewLock()) == NULL ) {
  146. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  147. "failed to create notify lock; NSPR error - %d\n", PR_GetError ());
  148. return -1;
  149. }
  150. if ( ( notify_cvar = PR_NewCondVar( notify_lock )) == NULL ) {
  151. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_init: "
  152. "failed to create notify cond var; NSPR error - %d\n", PR_GetError ());
  153. return -1;
  154. }
  155. /* config DSE must be initialized before we get here */
  156. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  157. CONFIG_FILTER, replica_config_add, NULL);
  158. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  159. CONFIG_FILTER, replica_config_modify,NULL);
  160. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  161. CONFIG_FILTER, dont_allow_that, NULL);
  162. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  163. CONFIG_FILTER, replica_config_delete,NULL);
  164. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  165. CONFIG_FILTER, replica_config_search,NULL);
  166. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  167. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  168. CONFIG_FILTER, replica_config_post_modify,
  169. NULL);
  170. /* register the CLEANALLRUV & ABORT task */
  171. slapi_task_register_handler("cleanallruv", replica_cleanall_ruv_task);
  172. slapi_task_register_handler("abort cleanallruv", replica_cleanall_ruv_abort);
  173. return 0;
  174. }
  175. void
  176. replica_config_destroy ()
  177. {
  178. if (s_configLock)
  179. {
  180. PR_DestroyLock (s_configLock);
  181. s_configLock = NULL;
  182. }
  183. /* config DSE must be initialized before we get here */
  184. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  185. CONFIG_FILTER, replica_config_add);
  186. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  187. CONFIG_FILTER, replica_config_modify);
  188. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  189. CONFIG_FILTER, dont_allow_that);
  190. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  191. CONFIG_FILTER, replica_config_delete);
  192. slapi_config_remove_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  193. CONFIG_FILTER, replica_config_search);
  194. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  195. CONFIG_BASE, LDAP_SCOPE_SUBTREE,
  196. CONFIG_FILTER, replica_config_post_modify);
  197. }
  198. static int
  199. replica_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  200. int *returncode, char *errorbuf, void *arg)
  201. {
  202. Replica *r = NULL;
  203. multimaster_mtnode_extension *mtnode_ext;
  204. char *replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  205. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  206. char *errortext = errorbuf ? errorbuf : buf;
  207. if (errorbuf)
  208. {
  209. errorbuf[0] = '\0';
  210. }
  211. *returncode = LDAP_SUCCESS;
  212. PR_Lock (s_configLock);
  213. /* add the dn to the dn hash so we can tell this replica is being configured */
  214. replica_add_by_dn(replica_root);
  215. mtnode_ext = _replica_config_get_mtnode_ext (e);
  216. PR_ASSERT (mtnode_ext);
  217. if (mtnode_ext->replica)
  218. {
  219. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica already configured for %s", replica_root);
  220. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: %s\n", errortext);
  221. *returncode = LDAP_UNWILLING_TO_PERFORM;
  222. goto done;
  223. }
  224. /* create replica object */
  225. r = replica_new_from_entry (e, errortext, PR_TRUE /* is a newly added entry */);
  226. if (r == NULL)
  227. {
  228. *returncode = LDAP_OPERATIONS_ERROR;
  229. goto done;
  230. }
  231. /* Set the mapping tree node state, and the referrals from the RUV */
  232. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  233. if (!replica_is_legacy_consumer (r))
  234. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  235. /* ONREPL if replica is added as writable we need to execute protocol that
  236. introduces new writable replica to the topology */
  237. mtnode_ext->replica = object_new (r, replica_destroy); /* Refcnt is 1 */
  238. /* add replica object to the hash */
  239. *returncode = replica_add_by_name (replica_get_name (r), mtnode_ext->replica); /* Increments object refcnt */
  240. /* delete the dn from the dn hash - done with configuration */
  241. replica_delete_by_dn(replica_root);
  242. done:
  243. PR_Unlock (s_configLock);
  244. /* slapi_ch_free accepts NULL pointer */
  245. slapi_ch_free ((void**)&replica_root);
  246. if (*returncode != LDAP_SUCCESS)
  247. {
  248. if (mtnode_ext->replica)
  249. object_release (mtnode_ext->replica);
  250. return SLAPI_DSE_CALLBACK_ERROR;
  251. }
  252. else
  253. return SLAPI_DSE_CALLBACK_OK;
  254. }
  255. static int
  256. replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  257. int *returncode, char *returntext, void *arg)
  258. {
  259. int rc= 0;
  260. LDAPMod **mods;
  261. int i, apply_mods;
  262. multimaster_mtnode_extension *mtnode_ext;
  263. Replica *r = NULL;
  264. char *replica_root = NULL;
  265. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  266. char *errortext = returntext ? returntext : buf;
  267. char *config_attr, *config_attr_value;
  268. Slapi_Operation *op;
  269. void *identity;
  270. if (returntext)
  271. {
  272. returntext[0] = '\0';
  273. }
  274. *returncode = LDAP_SUCCESS;
  275. /* just let internal operations originated from replication plugin to go through */
  276. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  277. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  278. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  279. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  280. {
  281. *returncode = LDAP_SUCCESS;
  282. return SLAPI_DSE_CALLBACK_OK;
  283. }
  284. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  285. PR_Lock (s_configLock);
  286. mtnode_ext = _replica_config_get_mtnode_ext (e);
  287. PR_ASSERT (mtnode_ext);
  288. if (mtnode_ext->replica)
  289. object_acquire (mtnode_ext->replica);
  290. if (mtnode_ext->replica == NULL)
  291. {
  292. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "replica does not exist for %s", replica_root);
  293. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  294. errortext);
  295. *returncode = LDAP_OPERATIONS_ERROR;
  296. goto done;
  297. }
  298. r = object_get_data (mtnode_ext->replica);
  299. PR_ASSERT (r);
  300. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  301. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  302. {
  303. /* we only allow the replica ID and type to be modified together e.g.
  304. if converting a read only replica to a master or vice versa -
  305. we will need to change both the replica ID and the type at the same
  306. time - we must disallow changing the replica ID if the type is not
  307. being changed and vice versa
  308. */
  309. char *new_repl_id = NULL;
  310. char *new_repl_type = NULL;
  311. if (*returncode != LDAP_SUCCESS)
  312. break;
  313. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  314. {
  315. if (*returncode != LDAP_SUCCESS)
  316. break;
  317. config_attr = (char *) mods[i]->mod_type;
  318. PR_ASSERT (config_attr);
  319. /* disallow modifications or removal of replica root,
  320. replica name and replica state attributes */
  321. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  322. strcasecmp (config_attr, attr_replicaName) == 0 ||
  323. strcasecmp (config_attr, attr_state) == 0)
  324. {
  325. *returncode = LDAP_UNWILLING_TO_PERFORM;
  326. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "modification of %s attribute is not allowed",
  327. config_attr);
  328. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  329. errortext);
  330. }
  331. /* this is a request to delete an attribute */
  332. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) || mods[i]->mod_bvalues == NULL
  333. || mods[i]->mod_bvalues[0]->bv_val == NULL)
  334. {
  335. /*
  336. * Where possible/allowed return replica config settings to their
  337. * default values.
  338. */
  339. if (strcasecmp (config_attr, attr_replicaBindDn) == 0)
  340. {
  341. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  342. }
  343. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  344. {
  345. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  346. }
  347. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  348. {
  349. replica_set_groupdn_checkinterval (r, -1);
  350. }
  351. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  352. {
  353. if (apply_mods) {
  354. replica_set_referrals(r, NULL);
  355. if (!replica_is_legacy_consumer (r)) {
  356. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  357. }
  358. }
  359. }
  360. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  361. {
  362. if (apply_mods)
  363. replica_set_legacy_consumer (r, PR_FALSE);
  364. }
  365. else if (strcasecmp (config_attr, type_replicaCleanRUV) == 0 ||
  366. strcasecmp (config_attr, type_replicaAbortCleanRUV) == 0)
  367. {
  368. /*
  369. * Nothing to do in this case, allow it, and continue.
  370. */
  371. continue;
  372. }
  373. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 )
  374. {
  375. if (apply_mods)
  376. replica_set_protocol_timeout(r, DEFAULT_PROTOCOL_TIMEOUT);
  377. }
  378. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  379. {
  380. if (apply_mods)
  381. replica_set_backoff_min(r, PROTOCOL_BACKOFF_MINIMUM);
  382. }
  383. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  384. {
  385. if (apply_mods)
  386. replica_set_backoff_max(r, PROTOCOL_BACKOFF_MAXIMUM);
  387. }
  388. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  389. {
  390. if (apply_mods)
  391. replica_set_precise_purging(r, 0);
  392. }
  393. else
  394. {
  395. *returncode = LDAP_UNWILLING_TO_PERFORM;
  396. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "deletion of %s attribute is not allowed", config_attr);
  397. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  398. }
  399. }
  400. else /* modify an attribute */
  401. {
  402. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  403. if (NULL == config_attr_value) {
  404. *returncode = LDAP_UNWILLING_TO_PERFORM;
  405. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE, "attribute %s value is NULL.\n",
  406. config_attr);
  407. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n",
  408. errortext);
  409. break;
  410. }
  411. if (strcasecmp (config_attr, attr_replicaBindDn) == 0) {
  412. *returncode = replica_config_change_updatedn (r, mods[i], errortext, apply_mods);
  413. }
  414. else if (strcasecmp (config_attr, attr_replicaBindDnGroup) == 0)
  415. {
  416. *returncode = replica_config_change_updatedngroup (r, mods[i], errortext, apply_mods);
  417. }
  418. else if (strcasecmp (config_attr, attr_replicaBindDnGroupCheckInterval) == 0)
  419. {
  420. int interval = atoi(config_attr_value);
  421. replica_set_groupdn_checkinterval (r, interval);
  422. }
  423. else if (strcasecmp (config_attr, attr_replicaType) == 0)
  424. {
  425. slapi_ch_free_string(&new_repl_type);
  426. new_repl_type = slapi_ch_strdup(config_attr_value);
  427. }
  428. else if (strcasecmp (config_attr, attr_replicaId) == 0)
  429. {
  430. slapi_ch_free_string(&new_repl_id);
  431. new_repl_id = slapi_ch_strdup(config_attr_value);
  432. }
  433. else if (strcasecmp (config_attr, attr_flags) == 0)
  434. {
  435. *returncode = replica_config_change_flags (r, config_attr_value, errortext, apply_mods);
  436. }
  437. else if (strcasecmp (config_attr, TASK_ATTR) == 0)
  438. {
  439. *returncode = replica_execute_task (mtnode_ext->replica, config_attr_value, errortext, apply_mods);
  440. }
  441. else if (strcasecmp (config_attr, attr_replicaReferral) == 0)
  442. {
  443. if (apply_mods)
  444. {
  445. Slapi_Mod smod;
  446. Slapi_ValueSet *vs= slapi_valueset_new();
  447. slapi_mod_init_byref(&smod,mods[i]);
  448. slapi_valueset_set_from_smod(vs, &smod);
  449. replica_set_referrals (r, vs);
  450. slapi_mod_done(&smod);
  451. slapi_valueset_free(vs);
  452. if (!replica_is_legacy_consumer (r)) {
  453. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  454. }
  455. }
  456. }
  457. else if (strcasecmp (config_attr, type_replicaPurgeDelay) == 0)
  458. {
  459. if (apply_mods && config_attr_value[0])
  460. {
  461. PRUint32 delay;
  462. if (isdigit (config_attr_value[0]))
  463. {
  464. delay = (unsigned int)atoi(config_attr_value);
  465. replica_set_purge_delay(r, delay);
  466. }
  467. else
  468. *returncode = LDAP_OPERATIONS_ERROR;
  469. }
  470. }
  471. else if (strcasecmp (config_attr, type_replicaTombstonePurgeInterval) == 0)
  472. {
  473. if (apply_mods && config_attr_value[0])
  474. {
  475. long interval;
  476. interval = atol (config_attr_value);
  477. replica_set_tombstone_reap_interval (r, interval);
  478. }
  479. }
  480. else if (strcasecmp (config_attr, type_replicaLegacyConsumer) == 0)
  481. {
  482. if (apply_mods && config_attr_value[0])
  483. {
  484. PRBool legacy = (strcasecmp (config_attr_value, "on") == 0) ||
  485. (strcasecmp (config_attr_value, "true") == 0) ||
  486. (strcasecmp (config_attr_value, "yes") == 0) ||
  487. (strcasecmp (config_attr_value, "1") == 0);
  488. replica_set_legacy_consumer (r, legacy);
  489. }
  490. }
  491. /* ignore modifiers attributes added by the server */
  492. else if (slapi_attr_is_last_mod(config_attr))
  493. {
  494. *returncode = LDAP_SUCCESS;
  495. }
  496. else if (strcasecmp (config_attr, type_replicaProtocolTimeout) == 0 ){
  497. if (apply_mods)
  498. {
  499. PRUint64 ptimeout = 0;
  500. ptimeout = atoll(config_attr_value);
  501. if(ptimeout <= 0){
  502. *returncode = LDAP_UNWILLING_TO_PERFORM;
  503. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  504. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  505. config_attr, config_attr_value);
  506. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  507. break;
  508. }
  509. replica_set_protocol_timeout(r, ptimeout);
  510. }
  511. }
  512. else if(strcasecmp (config_attr, type_replicaBackoffMin) == 0 )
  513. {
  514. if (apply_mods)
  515. {
  516. PRUint64 val = atoll(config_attr_value);
  517. if(val <= 0){
  518. *returncode = LDAP_UNWILLING_TO_PERFORM;
  519. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  520. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  521. config_attr, config_attr_value);
  522. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  523. break;
  524. }
  525. replica_set_backoff_min(r, val);
  526. }
  527. }
  528. else if (strcasecmp (config_attr, type_replicaBackoffMax) == 0 )
  529. {
  530. if (apply_mods)
  531. {
  532. PRUint64 val = atoll(config_attr_value);
  533. if(val <= 0){
  534. *returncode = LDAP_UNWILLING_TO_PERFORM;
  535. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  536. "attribute %s value (%s) is invalid, must be a number greater than zero.\n",
  537. config_attr, config_attr_value);
  538. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  539. break;
  540. }
  541. replica_set_backoff_max(r, val);
  542. }
  543. }
  544. else if (strcasecmp (config_attr, type_replicaPrecisePurge) == 0 )
  545. {
  546. if (apply_mods)
  547. {
  548. if (apply_mods && config_attr_value[0])
  549. {
  550. PRUint64 on_off = 0;
  551. if (strcasecmp(config_attr_value, "on") == 0){
  552. on_off = 1;
  553. } else if (strcasecmp(config_attr_value, "off") == 0){
  554. on_off = 0;
  555. } else{
  556. /* Invalid value */
  557. *returncode = LDAP_UNWILLING_TO_PERFORM;
  558. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  559. "Invalid value for %s: %s Value should be \"on\" or \"off\"\n",
  560. type_replicaPrecisePurge, config_attr_value);
  561. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  562. "Invalid value for %s: %s Value should be \"on\" or \"off\")\n",
  563. type_replicaPrecisePurge, config_attr_value);
  564. break;
  565. }
  566. replica_set_precise_purging(r, on_off);
  567. } else if (apply_mods) {
  568. replica_set_precise_purging(r, 0);
  569. }
  570. }
  571. }
  572. else
  573. {
  574. *returncode = LDAP_UNWILLING_TO_PERFORM;
  575. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  576. "modification of attribute %s is not allowed in replica entry", config_attr);
  577. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_modify: %s\n", errortext);
  578. }
  579. }
  580. }
  581. if (new_repl_id || new_repl_type)
  582. {
  583. *returncode = replica_config_change_type_and_id(r, new_repl_type, new_repl_id, errortext, apply_mods);
  584. PR_Unlock (s_configLock);
  585. replica_update_state(0, (void *)replica_get_name(r));
  586. PR_Lock (s_configLock);
  587. slapi_ch_free_string(&new_repl_id);
  588. slapi_ch_free_string(&new_repl_type);
  589. agmtlist_notify_all(pb);
  590. }
  591. }
  592. done:
  593. if (mtnode_ext->replica)
  594. object_release (mtnode_ext->replica);
  595. /* slapi_ch_free accepts NULL pointer */
  596. slapi_ch_free_string(&replica_root);
  597. PR_Unlock (s_configLock);
  598. if (*returncode != LDAP_SUCCESS)
  599. {
  600. return SLAPI_DSE_CALLBACK_ERROR;
  601. }
  602. else
  603. {
  604. return SLAPI_DSE_CALLBACK_OK;
  605. }
  606. }
  607. static int
  608. replica_config_post_modify(Slapi_PBlock *pb,
  609. Slapi_Entry* entryBefore,
  610. Slapi_Entry* e,
  611. int *returncode,
  612. char *returntext,
  613. void *arg)
  614. {
  615. int rc= 0;
  616. LDAPMod **mods;
  617. int i, apply_mods;
  618. multimaster_mtnode_extension *mtnode_ext;
  619. char *replica_root = NULL;
  620. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  621. char *errortext = returntext ? returntext : buf;
  622. char *config_attr, *config_attr_value;
  623. Slapi_Operation *op;
  624. void *identity;
  625. int flag_need_cleanup = 0;
  626. if (returntext)
  627. {
  628. returntext[0] = '\0';
  629. }
  630. *returncode = LDAP_SUCCESS;
  631. /* just let internal operations originated from replication plugin to go through */
  632. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  633. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &identity);
  634. if (operation_is_flag_set(op, OP_FLAG_INTERNAL) &&
  635. (identity == repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION)))
  636. {
  637. *returncode = LDAP_SUCCESS;
  638. return SLAPI_DSE_CALLBACK_OK;
  639. }
  640. replica_root = (char*)slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  641. PR_Lock (s_configLock);
  642. mtnode_ext = _replica_config_get_mtnode_ext (e);
  643. PR_ASSERT (mtnode_ext);
  644. if (mtnode_ext->replica)
  645. object_acquire (mtnode_ext->replica);
  646. if (mtnode_ext->replica == NULL)
  647. {
  648. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  649. "replica does not exist for %s", replica_root);
  650. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  651. "replica_config_post_modify: %s\n",
  652. errortext);
  653. *returncode = LDAP_OPERATIONS_ERROR;
  654. goto done;
  655. }
  656. PR_ASSERT (object_get_data (mtnode_ext->replica) != NULL);
  657. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  658. for (apply_mods = 0; apply_mods <= 1; apply_mods++)
  659. {
  660. /* we only allow the replica ID and type to be modified together e.g.
  661. if converting a read only replica to a master or vice versa -
  662. we will need to change both the replica ID and the type at the same
  663. time - we must disallow changing the replica ID if the type is not
  664. being changed and vice versa
  665. */
  666. if (*returncode != LDAP_SUCCESS)
  667. break;
  668. for (i = 0; mods && (mods[i] && (LDAP_SUCCESS == rc)); i++)
  669. {
  670. if (*returncode != LDAP_SUCCESS)
  671. break;
  672. config_attr = (char *) mods[i]->mod_type;
  673. PR_ASSERT (config_attr);
  674. /* disallow modifications or removal of replica root,
  675. replica name and replica state attributes */
  676. if (strcasecmp (config_attr, attr_replicaRoot) == 0 ||
  677. strcasecmp (config_attr, attr_replicaName) == 0 ||
  678. strcasecmp (config_attr, attr_state) == 0)
  679. {
  680. *returncode = LDAP_UNWILLING_TO_PERFORM;
  681. PR_snprintf (errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  682. "modification of %s attribute is not allowed",
  683. config_attr);
  684. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  685. "replica_config_post_modify: %s\n",
  686. errortext);
  687. }
  688. /* this is a request to delete an attribute */
  689. else if ((mods[i]->mod_op & LDAP_MOD_DELETE) ||
  690. mods[i]->mod_bvalues == NULL ||
  691. mods[i]->mod_bvalues[0]->bv_val == NULL)
  692. {
  693. ;
  694. }
  695. else /* modify an attribute */
  696. {
  697. config_attr_value = (char *) mods[i]->mod_bvalues[0]->bv_val;
  698. if (strcasecmp (config_attr, TASK_ATTR) == 0)
  699. {
  700. flag_need_cleanup = 1;
  701. }
  702. }
  703. }
  704. }
  705. done:
  706. PR_Unlock (s_configLock);
  707. /* slapi_ch_free accepts NULL pointer */
  708. slapi_ch_free_string (&replica_root);
  709. /* Call replica_cleanup_task after s_configLock is reliesed */
  710. if (flag_need_cleanup)
  711. {
  712. *returncode = replica_cleanup_task(mtnode_ext->replica,
  713. config_attr_value,
  714. errortext, apply_mods);
  715. }
  716. if (mtnode_ext->replica)
  717. object_release (mtnode_ext->replica);
  718. if (*returncode != LDAP_SUCCESS)
  719. {
  720. return SLAPI_DSE_CALLBACK_ERROR;
  721. }
  722. else
  723. {
  724. return SLAPI_DSE_CALLBACK_OK;
  725. }
  726. }
  727. static int
  728. replica_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  729. int *returncode, char *returntext, void *arg)
  730. {
  731. multimaster_mtnode_extension *mtnode_ext;
  732. Replica *r;
  733. PR_Lock (s_configLock);
  734. mtnode_ext = _replica_config_get_mtnode_ext (e);
  735. PR_ASSERT (mtnode_ext);
  736. if (mtnode_ext->replica)
  737. {
  738. /* remove object from the hash */
  739. r = (Replica*)object_get_data (mtnode_ext->replica);
  740. PR_ASSERT (r);
  741. /* The changelog for this replica is no longer valid, so we should remove it. */
  742. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_delete: "
  743. "Warning: The changelog for replica %s is no longer valid since "
  744. "the replica config is being deleted. Removing the changelog.\n",
  745. slapi_sdn_get_dn(replica_get_root(r)));
  746. cl5DeleteDBSync(mtnode_ext->replica);
  747. replica_delete_by_name (replica_get_name (r));
  748. object_release (mtnode_ext->replica);
  749. mtnode_ext->replica = NULL;
  750. }
  751. PR_Unlock (s_configLock);
  752. *returncode = LDAP_SUCCESS;
  753. return SLAPI_DSE_CALLBACK_OK;
  754. }
  755. static void
  756. replica_config_search_last_modified(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  757. {
  758. Object *ruv_obj = NULL;
  759. RUV *ruv = NULL;
  760. Slapi_Value **values;
  761. if (replica == NULL)
  762. return;
  763. ruv_obj = replica_get_ruv(replica);
  764. ruv = object_get_data(ruv_obj);
  765. if ((values = ruv_last_modified_to_valuearray(ruv)) != NULL) {
  766. slapi_entry_add_values_sv(e, type_ruvElementUpdatetime, values);
  767. valuearray_free(&values);
  768. }
  769. object_release(ruv_obj);
  770. }
  771. static void
  772. replica_config_search_ruv(Slapi_PBlock *pb, Slapi_Entry* e, Replica *replica)
  773. {
  774. Object *ruv_obj = NULL;
  775. RUV *ruv = NULL;
  776. Slapi_Value **values;
  777. if (replica == NULL)
  778. return;
  779. ruv_obj = replica_get_ruv(replica);
  780. ruv = object_get_data(ruv_obj);
  781. if ((values = ruv_to_valuearray(ruv)) != NULL) {
  782. slapi_entry_add_values_sv(e, type_ruvElement, values);
  783. valuearray_free(&values);
  784. }
  785. object_release(ruv_obj);
  786. /* now add all the repl agmts maxcsnsruv */
  787. add_agmt_maxcsns(e, replica);
  788. }
  789. /* Returns PR_TRUE if 'attr' is present in the search requested attributes
  790. * else it returns PR_FALSE
  791. */
  792. static PRBool
  793. search_requested_attr(Slapi_PBlock *pb, const char *attr)
  794. {
  795. char **attrs = NULL;
  796. int i;
  797. slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
  798. if ((attr == NULL) || (attrs == NULL))
  799. return PR_FALSE;
  800. for (i = 0; attrs[i] != NULL; i++) {
  801. if (strcasecmp(attrs[i], attr) == 0) {
  802. return PR_TRUE;
  803. }
  804. }
  805. return PR_FALSE;
  806. }
  807. static int
  808. replica_config_search (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode,
  809. char *returntext, void *arg)
  810. {
  811. multimaster_mtnode_extension *mtnode_ext;
  812. int changeCount = 0;
  813. PRBool reapActive = PR_FALSE;
  814. char val [64];
  815. /* add attribute that contains number of entries in the changelog for this replica */
  816. PR_Lock (s_configLock);
  817. mtnode_ext = _replica_config_get_mtnode_ext (e);
  818. PR_ASSERT (mtnode_ext);
  819. if (mtnode_ext->replica) {
  820. Replica *replica;
  821. object_acquire (mtnode_ext->replica);
  822. if (cl5GetState () == CL5_STATE_OPEN) {
  823. changeCount = cl5GetOperationCount (mtnode_ext->replica);
  824. }
  825. replica = (Replica*)object_get_data (mtnode_ext->replica);
  826. if (replica) {
  827. reapActive = replica_get_tombstone_reap_active(replica);
  828. }
  829. /* Check if the in memory ruv is requested */
  830. if (search_requested_attr(pb, type_ruvElement)) {
  831. replica_config_search_ruv(pb, e, replica);
  832. }
  833. /* Check if the last update time is requested */
  834. if (search_requested_attr(pb, type_ruvElementUpdatetime)) {
  835. replica_config_search_last_modified(pb, e, replica);
  836. }
  837. object_release (mtnode_ext->replica);
  838. }
  839. sprintf (val, "%d", changeCount);
  840. slapi_entry_add_string (e, type_replicaChangeCount, val);
  841. slapi_entry_attr_set_int(e, "nsds5replicaReapActive", (int)reapActive);
  842. PR_Unlock (s_configLock);
  843. return SLAPI_DSE_CALLBACK_OK;
  844. }
  845. static int
  846. replica_config_change_type_and_id (Replica *r, const char *new_type,
  847. const char *new_id, char *returntext,
  848. int apply_mods)
  849. {
  850. int type;
  851. ReplicaType oldtype;
  852. ReplicaId rid;
  853. ReplicaId oldrid;
  854. PR_ASSERT (r);
  855. oldtype = replica_get_type(r);
  856. oldrid = replica_get_rid(r);
  857. if (new_type == NULL) /* by default - replica is read-only */
  858. {
  859. type = REPLICA_TYPE_READONLY;
  860. }
  861. else
  862. {
  863. type = atoi (new_type);
  864. if (type <= REPLICA_TYPE_UNKNOWN || type >= REPLICA_TYPE_END)
  865. {
  866. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "invalid replica type %d", type);
  867. return LDAP_OPERATIONS_ERROR;
  868. }
  869. }
  870. /* disallow changing type to itself just to permit a replica ID change */
  871. if (oldtype == type)
  872. {
  873. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica type is already %d - not changing", type);
  874. return LDAP_OPERATIONS_ERROR;
  875. }
  876. if (type == REPLICA_TYPE_READONLY)
  877. {
  878. rid = READ_ONLY_REPLICA_ID; /* default rid for read only */
  879. }
  880. else if (!new_id)
  881. {
  882. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "a replica ID is required when changing replica type to read-write");
  883. return LDAP_UNWILLING_TO_PERFORM;
  884. }
  885. else
  886. {
  887. int temprid = atoi (new_id);
  888. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  889. {
  890. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  891. "attribute %s must have a value greater than 0 "
  892. "and less than %d",
  893. attr_replicaId, READ_ONLY_REPLICA_ID);
  894. return LDAP_UNWILLING_TO_PERFORM;
  895. }
  896. else
  897. {
  898. rid = (ReplicaId)temprid;
  899. }
  900. }
  901. /* error if old rid == new rid */
  902. if (oldrid == rid)
  903. {
  904. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "replica ID is already %d - not changing", rid);
  905. return LDAP_OPERATIONS_ERROR;
  906. }
  907. if (apply_mods)
  908. {
  909. Object *ruv_obj, *gen_obj;
  910. RUV *ruv;
  911. CSNGen *gen;
  912. ruv_obj = replica_get_ruv(r);
  913. if(ruv_obj){
  914. /* we need to rewrite the repl_csngen with the new rid */
  915. ruv = object_get_data (ruv_obj);
  916. gen_obj = replica_get_csngen (r);
  917. if(gen_obj){
  918. const char *purl = multimaster_get_local_purl();
  919. gen = (CSNGen*) object_get_data (gen_obj);
  920. csngen_rewrite_rid(gen, rid);
  921. if(purl && type == REPLICA_TYPE_UPDATABLE){
  922. ruv_add_replica(ruv, rid, purl);
  923. replica_reset_csn_pl(r);
  924. }
  925. ruv_delete_replica(ruv, oldrid);
  926. replica_set_ruv_dirty(r);
  927. cl5CleanRUV(oldrid);
  928. replica_set_csn_assigned(r);
  929. }
  930. object_release(ruv_obj);
  931. }
  932. replica_set_type (r, type);
  933. replica_set_rid(r, rid);
  934. /* Set the mapping tree node, and the list of referrals */
  935. /* if this server is a 4.0 consumer the referrals are set by legacy plugin */
  936. if (!replica_is_legacy_consumer(r))
  937. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  938. }
  939. return LDAP_SUCCESS;
  940. }
  941. static int
  942. replica_config_change_updatedn (Replica *r, const LDAPMod *mod, char *returntext,
  943. int apply_mods)
  944. {
  945. PR_ASSERT (r);
  946. if (apply_mods)
  947. {
  948. Slapi_Mod smod;
  949. Slapi_ValueSet *vs= slapi_valueset_new();
  950. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  951. slapi_valueset_set_from_smod(vs, &smod);
  952. replica_set_updatedn(r, vs, mod->mod_op);
  953. slapi_mod_done(&smod);
  954. slapi_valueset_free(vs);
  955. }
  956. return LDAP_SUCCESS;
  957. }
  958. static int
  959. replica_config_change_updatedngroup (Replica *r, const LDAPMod *mod, char *returntext,
  960. int apply_mods)
  961. {
  962. PR_ASSERT (r);
  963. if (apply_mods)
  964. {
  965. Slapi_Mod smod;
  966. Slapi_ValueSet *vs= slapi_valueset_new();
  967. slapi_mod_init_byref(&smod, (LDAPMod *)mod); /* cast away const */
  968. slapi_valueset_set_from_smod(vs, &smod);
  969. replica_set_groupdn(r, vs, mod->mod_op);
  970. slapi_mod_done(&smod);
  971. slapi_valueset_free(vs);
  972. }
  973. return LDAP_SUCCESS;
  974. }
  975. static int replica_config_change_flags (Replica *r, const char *new_flags,
  976. char *returntext, int apply_mods)
  977. {
  978. PR_ASSERT (r);
  979. if (apply_mods)
  980. {
  981. PRUint32 flags;
  982. flags = atol (new_flags);
  983. replica_replace_flags (r, flags);
  984. }
  985. return LDAP_SUCCESS;
  986. }
  987. static int replica_execute_task (Object *r, const char *task_name, char *returntext,
  988. int apply_mods)
  989. {
  990. if (strcasecmp (task_name, CL2LDIF_TASK) == 0)
  991. {
  992. if (apply_mods)
  993. {
  994. return replica_execute_cl2ldif_task (r, returntext);
  995. }
  996. else
  997. return LDAP_SUCCESS;
  998. }
  999. else if (strcasecmp (task_name, LDIF2CL_TASK) == 0)
  1000. {
  1001. if (apply_mods)
  1002. {
  1003. return replica_execute_ldif2cl_task (r, returntext);
  1004. }
  1005. else
  1006. return LDAP_SUCCESS;
  1007. }
  1008. else if (strncasecmp (task_name, CLEANRUV, CLEANRUVLEN) == 0)
  1009. {
  1010. int temprid = atoi(&(task_name[CLEANRUVLEN]));
  1011. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1012. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - %s", temprid, task_name);
  1013. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1014. return LDAP_OPERATIONS_ERROR;
  1015. }
  1016. if (apply_mods)
  1017. {
  1018. return replica_execute_cleanruv_task (r, (ReplicaId)temprid, returntext);
  1019. }
  1020. else
  1021. return LDAP_SUCCESS;
  1022. }
  1023. else if (strncasecmp (task_name, CLEANALLRUV, CLEANALLRUVLEN) == 0)
  1024. {
  1025. int temprid = atoi(&(task_name[CLEANALLRUVLEN]));
  1026. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1027. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)", temprid, task_name);
  1028. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_execute_task: %s\n", returntext);
  1029. return LDAP_OPERATIONS_ERROR;
  1030. }
  1031. if (apply_mods)
  1032. {
  1033. Slapi_Task *empty_task = NULL;
  1034. return replica_execute_cleanall_ruv_task(r, (ReplicaId)temprid, empty_task, returntext, "no");
  1035. }
  1036. else
  1037. return LDAP_SUCCESS;
  1038. }
  1039. else
  1040. {
  1041. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "unsupported replica task - %s", task_name);
  1042. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1043. "replica_execute_task: %s\n", returntext);
  1044. return LDAP_OPERATIONS_ERROR;
  1045. }
  1046. }
  1047. static int
  1048. replica_cleanup_task (Object *r, const char *task_name, char *returntext,
  1049. int apply_mods)
  1050. {
  1051. int rc = LDAP_SUCCESS;
  1052. if (apply_mods) {
  1053. Replica *replica = (Replica*)object_get_data (r);
  1054. if (NULL == replica) {
  1055. rc = LDAP_OPERATIONS_ERROR;
  1056. } else {
  1057. rc = replica_task_done(replica);
  1058. }
  1059. }
  1060. return rc;
  1061. }
  1062. static int
  1063. replica_task_done(Replica *replica)
  1064. {
  1065. int rc = LDAP_OPERATIONS_ERROR;
  1066. char *replica_dn = NULL;
  1067. Slapi_DN *replica_sdn = NULL;
  1068. Slapi_PBlock *pb = NULL;
  1069. LDAPMod *mods [2];
  1070. LDAPMod mod;
  1071. if (NULL == replica) {
  1072. return rc;
  1073. }
  1074. /* dn: cn=replica,cn=dc\3Dexample\2Cdc\3Dcom,cn=mapping tree,cn=config */
  1075. replica_dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  1076. REPLICA_RDN,
  1077. slapi_sdn_get_dn(replica_get_root(replica)),
  1078. CONFIG_BASE);
  1079. if (NULL == replica_dn) {
  1080. return rc;
  1081. }
  1082. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1083. pb = slapi_pblock_new();
  1084. mods[0] = &mod;
  1085. mods[1] = NULL;
  1086. mod.mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
  1087. mod.mod_type = (char *)TASK_ATTR;
  1088. mod.mod_bvalues = NULL;
  1089. slapi_modify_internal_set_pb_ext(pb, replica_sdn, mods, NULL/* controls */,
  1090. NULL/* uniqueid */,
  1091. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1092. 0/* flags */);
  1093. slapi_modify_internal_pb (pb);
  1094. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1095. if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_ATTRIBUTE)) {
  1096. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1097. "replica_task_done: "
  1098. "failed to remove (%s) attribute from (%s) entry; "
  1099. "LDAP error - %d\n",
  1100. TASK_ATTR, replica_dn, rc);
  1101. }
  1102. slapi_pblock_destroy (pb);
  1103. slapi_sdn_free(&replica_sdn);
  1104. return rc;
  1105. }
  1106. static int replica_execute_cl2ldif_task (Object *r, char *returntext)
  1107. {
  1108. int rc;
  1109. Object *rlist [2];
  1110. Replica *replica;
  1111. char fName [MAXPATHLEN];
  1112. char *clDir = NULL;
  1113. if (cl5GetState () != CL5_STATE_OPEN)
  1114. {
  1115. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1116. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1117. "replica_execute_cl2ldif_task: %s\n", returntext);
  1118. rc = LDAP_OPERATIONS_ERROR;
  1119. goto bail;
  1120. }
  1121. rlist[0] = r;
  1122. rlist[1] = NULL;
  1123. /* file is stored in the changelog directory and is named
  1124. <replica name>.ldif */
  1125. clDir = cl5GetDir ();
  1126. if (NULL == clDir) {
  1127. rc = LDAP_OPERATIONS_ERROR;
  1128. goto bail;
  1129. }
  1130. replica = (Replica*)object_get_data (r);
  1131. if (NULL == replica) {
  1132. rc = LDAP_OPERATIONS_ERROR;
  1133. goto bail;
  1134. }
  1135. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1137. "Beginning changelog export of replica \"%s\"\n",
  1138. replica_get_name(replica));
  1139. rc = cl5ExportLDIF (fName, rlist);
  1140. if (rc == CL5_SUCCESS) {
  1141. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1142. "Finished changelog export of replica \"%s\"\n",
  1143. replica_get_name(replica));
  1144. rc = LDAP_SUCCESS;
  1145. } else {
  1146. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1147. "Failed changelog export replica %s; "
  1148. "changelog error - %d", replica_get_name(replica), rc);
  1149. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1150. "replica_execute_cl2ldif_task: %s\n", returntext);
  1151. rc = LDAP_OPERATIONS_ERROR;
  1152. }
  1153. bail:
  1154. slapi_ch_free_string (&clDir);
  1155. return rc;
  1156. }
  1157. static int replica_execute_ldif2cl_task (Object *r, char *returntext)
  1158. {
  1159. int rc, imprc = 0;
  1160. Object *rlist [2];
  1161. Replica *replica;
  1162. char fName [MAXPATHLEN];
  1163. char *clDir = NULL;
  1164. changelog5Config config;
  1165. if (cl5GetState () != CL5_STATE_OPEN)
  1166. {
  1167. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "changelog is not open");
  1168. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1169. "replica_execute_ldif2cl_task: %s\n", returntext);
  1170. rc = LDAP_OPERATIONS_ERROR;
  1171. goto bail;
  1172. }
  1173. rlist[0] = r;
  1174. rlist[1] = NULL;
  1175. /* file is stored in the changelog directory and is named
  1176. <replica name>.ldif */
  1177. clDir = cl5GetDir ();
  1178. if (NULL == clDir) {
  1179. rc = LDAP_OPERATIONS_ERROR;
  1180. goto bail;
  1181. }
  1182. replica = (Replica*)object_get_data (r);
  1183. if (NULL == replica) {
  1184. rc = LDAP_OPERATIONS_ERROR;
  1185. goto bail;
  1186. }
  1187. PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
  1188. rc = cl5Close();
  1189. if (rc != CL5_SUCCESS)
  1190. {
  1191. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1192. "failed to close changelog to import changelog data; "
  1193. "changelog error - %d", rc);
  1194. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1195. "replica_execute_ldif2cl_task: %s\n", returntext);
  1196. rc = LDAP_OPERATIONS_ERROR;
  1197. goto bail;
  1198. }
  1199. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1200. "Beginning changelog import of replica \"%s\"\n",
  1201. replica_get_name(replica));
  1202. imprc = cl5ImportLDIF (clDir, fName, rlist);
  1203. if (CL5_SUCCESS == imprc)
  1204. {
  1205. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1206. "Finished changelog import of replica \"%s\"\n",
  1207. replica_get_name(replica));
  1208. }
  1209. else
  1210. {
  1211. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1212. "Failed changelog import replica %s; "
  1213. "changelog error - %d", replica_get_name(replica), rc);
  1214. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1215. "replica_execute_ldif2cl_task: %s\n", returntext);
  1216. imprc = LDAP_OPERATIONS_ERROR;
  1217. }
  1218. changelog5_read_config (&config);
  1219. /* restart changelog */
  1220. rc = cl5Open (config.dir, &config.dbconfig);
  1221. if (CL5_SUCCESS == rc)
  1222. {
  1223. rc = LDAP_SUCCESS;
  1224. }
  1225. else
  1226. {
  1227. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1228. "replica_execute_ldif2cl_task: failed to start changelog at %s\n",
  1229. config.dir?config.dir:"null config dir");
  1230. rc = LDAP_OPERATIONS_ERROR;
  1231. }
  1232. bail:
  1233. slapi_ch_free_string(&clDir);
  1234. changelog5_config_done(&config);
  1235. /* if cl5ImportLDIF returned an error, report it first. */
  1236. return imprc?imprc:rc;
  1237. }
  1238. static multimaster_mtnode_extension *
  1239. _replica_config_get_mtnode_ext (const Slapi_Entry *e)
  1240. {
  1241. const char *replica_root;
  1242. Slapi_DN *sdn = NULL;
  1243. mapping_tree_node *mtnode;
  1244. multimaster_mtnode_extension *ext = NULL;
  1245. /* retirve root of the tree for which replica is configured */
  1246. replica_root = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1247. if (replica_root == NULL)
  1248. {
  1249. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1250. "configuration entry %s missing %s attribute\n",
  1251. slapi_entry_get_dn((Slapi_Entry *)e),
  1252. attr_replicaRoot);
  1253. return NULL;
  1254. }
  1255. sdn = slapi_sdn_new_dn_passin (replica_root);
  1256. /* locate mapping tree node for the specified subtree */
  1257. mtnode = slapi_get_mapping_tree_node_by_dn (sdn);
  1258. if (mtnode == NULL)
  1259. {
  1260. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_config_add: "
  1261. "failed to locate mapping tree node for dn %s\n",
  1262. slapi_sdn_get_dn(sdn));
  1263. }
  1264. else
  1265. {
  1266. /* check if replica object already exists for the specified subtree */
  1267. ext = (multimaster_mtnode_extension *)repl_con_get_ext (REPL_CON_EXT_MTNODE, mtnode);
  1268. }
  1269. slapi_sdn_free (&sdn);
  1270. return ext;
  1271. }
  1272. int
  1273. replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid)
  1274. {
  1275. return replica_execute_cleanruv_task(r, rid, NULL);
  1276. }
  1277. static int
  1278. replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not used */)
  1279. {
  1280. Object *RUVObj;
  1281. RUV *local_ruv = NULL;
  1282. Replica *replica = (Replica*)object_get_data (r);
  1283. int rc = 0;
  1284. PR_ASSERT (replica);
  1285. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: cleaning rid (%d)...\n",(int)rid);
  1286. RUVObj = replica_get_ruv(replica);
  1287. PR_ASSERT(RUVObj);
  1288. local_ruv = (RUV*)object_get_data (RUVObj);
  1289. /* Need to check that :
  1290. * - rid is not the local one
  1291. * - rid is not the last one
  1292. */
  1293. if ((replica_get_rid(replica) == rid) ||
  1294. (ruv_replica_count(local_ruv) <= 1)) {
  1295. return LDAP_UNWILLING_TO_PERFORM;
  1296. }
  1297. rc = ruv_delete_replica(local_ruv, rid);
  1298. replica_set_ruv_dirty(replica);
  1299. if (replica_write_ruv(replica)) {
  1300. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: could not write RUV\n");
  1301. }
  1302. object_release(RUVObj);
  1303. /* Update Mapping Tree to reflect RUV changes */
  1304. consumer5_set_mapping_tree_state_for_replica(replica, NULL);
  1305. /*
  1306. * Clean the changelog RUV's
  1307. */
  1308. cl5CleanRUV(rid);
  1309. if (rc != RUV_SUCCESS){
  1310. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanruv_task: task failed(%d)\n",rc);
  1311. return LDAP_OPERATIONS_ERROR;
  1312. }
  1313. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "cleanruv_task: finished successfully\n");
  1314. return LDAP_SUCCESS;
  1315. }
  1316. const char *
  1317. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1318. {
  1319. Slapi_Attr *attr;
  1320. Slapi_Value *val = NULL;
  1321. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1322. return default_val;
  1323. slapi_attr_first_value(attr, &val);
  1324. return slapi_value_get_string(val);
  1325. }
  1326. static int
  1327. replica_cleanall_ruv_task(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1328. int *returncode, char *returntext, void *arg)
  1329. {
  1330. Slapi_Task *task = NULL;
  1331. const Slapi_DN *task_dn;
  1332. Slapi_DN *dn = NULL;
  1333. ReplicaId rid = -1;
  1334. Object *r;
  1335. const char *force_cleaning;
  1336. const char *base_dn;
  1337. const char *rid_str;
  1338. int rc = SLAPI_DSE_CALLBACK_OK;
  1339. /* allocate new task now */
  1340. task = slapi_new_task(slapi_entry_get_ndn(e));
  1341. task_dn = slapi_entry_get_sdn(e);
  1342. if(task == NULL){
  1343. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV_task: Failed to create new task\n");
  1344. rc = SLAPI_DSE_CALLBACK_ERROR;
  1345. goto out;
  1346. }
  1347. /* register our destructor for waiting the task is done */
  1348. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_destructor);
  1349. /*
  1350. * Get our task settings
  1351. */
  1352. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  1353. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-id attribute");
  1354. cleanruv_log(task, -1, CLEANALLRUV_ID, "%s", returntext);
  1355. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1356. rc = SLAPI_DSE_CALLBACK_ERROR;
  1357. goto out;
  1358. }
  1359. rid = atoi(rid_str);
  1360. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  1361. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing replica-base-dn attribute");
  1362. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1363. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1364. rc = SLAPI_DSE_CALLBACK_ERROR;
  1365. goto out;
  1366. }
  1367. if ((force_cleaning = fetch_attr(e, "replica-force-cleaning", 0)) != NULL){
  1368. if(strcasecmp(force_cleaning,"yes") != 0 && strcasecmp(force_cleaning,"no") != 0){
  1369. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for replica-force-cleaning "
  1370. "(%s). Value must be \"yes\" or \"no\" for task - (%s)",
  1371. force_cleaning, slapi_sdn_get_dn(task_dn));
  1372. cleanruv_log(task, (int)rid, CLEANALLRUV_ID, "%s", returntext);
  1373. *returncode = LDAP_OPERATIONS_ERROR;
  1374. rc = SLAPI_DSE_CALLBACK_ERROR;
  1375. goto out;
  1376. }
  1377. } else {
  1378. force_cleaning = "no";
  1379. }
  1380. /*
  1381. * Check the rid
  1382. */
  1383. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1384. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  1385. rid, slapi_sdn_get_dn(task_dn));
  1386. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1387. *returncode = LDAP_OPERATIONS_ERROR;
  1388. rc = SLAPI_DSE_CALLBACK_ERROR;
  1389. goto out;
  1390. }
  1391. if(is_cleaned_rid(rid)){
  1392. /* we are already cleaning this rid */
  1393. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being cleaned", rid);
  1394. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1395. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1396. rc = SLAPI_DSE_CALLBACK_ERROR;
  1397. goto out;
  1398. }
  1399. /*
  1400. * Get the replica object
  1401. */
  1402. dn = slapi_sdn_new_dn_byval(base_dn);
  1403. if((r = replica_get_replica_from_dn(dn)) == NULL){
  1404. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Could not find replica from dn(%s)",slapi_sdn_get_dn(dn));
  1405. cleanruv_log(task, rid, CLEANALLRUV_ID, "%s", returntext);
  1406. *returncode = LDAP_OPERATIONS_ERROR;
  1407. rc = SLAPI_DSE_CALLBACK_ERROR;
  1408. goto out;
  1409. }
  1410. /* clean the RUV's */
  1411. rc = replica_execute_cleanall_ruv_task (r, rid, task, force_cleaning, returntext);
  1412. out:
  1413. if(rc){
  1414. cleanruv_log(task, rid, CLEANALLRUV_ID, "Task failed...(%d)", rc);
  1415. slapi_task_finish(task, *returncode);
  1416. } else {
  1417. rc = SLAPI_DSE_CALLBACK_OK;
  1418. }
  1419. slapi_sdn_free(&dn);
  1420. return rc;
  1421. }
  1422. /*
  1423. * CLEANALLRUV task
  1424. *
  1425. * [1] Get the maxcsn from the RUV of the rid we want to clean
  1426. * [2] Create the payload for the "cleanallruv" extended ops
  1427. * [3] Create "monitor" thread to do the real work.
  1428. *
  1429. */
  1430. static int
  1431. replica_execute_cleanall_ruv_task (Object *r, ReplicaId rid, Slapi_Task *task, const char* force_cleaning, char *returntext)
  1432. {
  1433. struct berval *payload = NULL;
  1434. Slapi_Task *pre_task = NULL; /* this is supposed to be null for logging */
  1435. cleanruv_data *data = NULL;
  1436. PRThread *thread = NULL;
  1437. CSN *maxcsn = NULL;
  1438. Replica *replica;
  1439. char csnstr[CSN_STRSIZE];
  1440. char *ridstr = NULL;
  1441. char *basedn = NULL;
  1442. int rc = 0;
  1443. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,"Initiating CleanAllRUV Task...");
  1444. if(get_cleanruv_task_count() >= CLEANRIDSIZ){
  1445. /* we are already running the maximum number of tasks */
  1446. cleanruv_log(pre_task, rid, CLEANALLRUV_ID,
  1447. "Exceeded maximum number of active CLEANALLRUV tasks(%d)",CLEANRIDSIZ);
  1448. return LDAP_UNWILLING_TO_PERFORM;
  1449. }
  1450. /*
  1451. * Grab the replica
  1452. */
  1453. if(r){
  1454. replica = (Replica*)object_get_data (r);
  1455. } else {
  1456. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Replica object is NULL, aborting task");
  1457. return -1;
  1458. }
  1459. /*
  1460. * Check if this is a consumer
  1461. */
  1462. if(replica_get_type(replica) == REPLICA_TYPE_READONLY){
  1463. /* this is a consumer, send error */
  1464. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d), task can not be run on a consumer",rid);
  1465. if(task){
  1466. rc = -1;
  1467. slapi_task_finish(task, rc);
  1468. }
  1469. return -1;
  1470. }
  1471. /*
  1472. * Grab the max csn of the deleted replica
  1473. */
  1474. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Retrieving maxcsn...");
  1475. basedn = (char *)slapi_sdn_get_dn(replica_get_root(replica));
  1476. maxcsn = replica_cleanallruv_find_maxcsn(replica, rid, basedn);
  1477. if(maxcsn == NULL || csn_get_replicaid(maxcsn) == 0){
  1478. /*
  1479. * This is for consistency with extop csn creation, where
  1480. * we want the csn string to be "0000000000000000000" not ""
  1481. */
  1482. csn_free(&maxcsn);
  1483. maxcsn = csn_new();
  1484. csn_init_by_string(maxcsn, "");
  1485. }
  1486. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1487. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Found maxcsn (%s)",csnstr);
  1488. /*
  1489. * Create payload
  1490. */
  1491. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, basedn, csnstr, force_cleaning);
  1492. payload = create_cleanruv_payload(ridstr);
  1493. slapi_ch_free_string(&ridstr);
  1494. if(payload == NULL){
  1495. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  1496. rc = -1;
  1497. goto fail;
  1498. }
  1499. /*
  1500. * Launch the cleanallruv thread. Once all the replicas are cleaned it will release the rid
  1501. */
  1502. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1503. if (data == NULL) {
  1504. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to allocate cleanruv_data. Aborting task.");
  1505. rc = -1;
  1506. goto fail;
  1507. }
  1508. data->repl_obj = r;
  1509. data->replica = replica;
  1510. data->rid = rid;
  1511. data->task = task;
  1512. data->payload = payload;
  1513. data->sdn = NULL;
  1514. data->maxcsn = maxcsn;
  1515. data->repl_root = slapi_ch_strdup(basedn);
  1516. data->force = slapi_ch_strdup(force_cleaning);
  1517. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread,
  1518. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1519. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1520. if (thread == NULL) {
  1521. rc = -1;
  1522. slapi_ch_free_string(&data->force);
  1523. slapi_ch_free_string(&data->repl_root);
  1524. goto fail;
  1525. } else {
  1526. goto done;
  1527. }
  1528. fail:
  1529. cleanruv_log(pre_task, rid, CLEANALLRUV_ID, "Failed to clean rid (%d)", rid);
  1530. if(task){
  1531. slapi_task_finish(task, rc);
  1532. }
  1533. if(payload){
  1534. ber_bvfree(payload);
  1535. }
  1536. csn_free(&maxcsn);
  1537. if(task) /* only the task acquires the r obj */
  1538. object_release (r);
  1539. done:
  1540. return rc;
  1541. }
  1542. void
  1543. replica_cleanallruv_thread_ext(void *arg)
  1544. {
  1545. replica_cleanallruv_thread(arg);
  1546. }
  1547. /*
  1548. * CLEANALLRUV Thread
  1549. *
  1550. * [1] Wait for the maxcsn to be covered
  1551. * [2] Make sure all the replicas are alive
  1552. * [3] Set the cleaned rid
  1553. * [4] Send the cleanAllRUV extop to all the replicas
  1554. * [5] Manually send the CLEANRUV task to replicas that do not support CLEANALLRUV
  1555. * [6] Wait for all the replicas to be cleaned.
  1556. * [7] Trigger cl trimming, release the rid, and remove all the "cleanallruv" attributes
  1557. * from the config.
  1558. */
  1559. static void
  1560. replica_cleanallruv_thread(void *arg)
  1561. {
  1562. cleanruv_data *data = arg;
  1563. Object *agmt_obj = NULL;
  1564. Object *ruv_obj = NULL;
  1565. Repl_Agmt *agmt = NULL;
  1566. RUV *ruv = NULL;
  1567. char csnstr[CSN_STRSIZE];
  1568. char *returntext = NULL;
  1569. char *rid_text = NULL;
  1570. int agmt_not_notified = 1;
  1571. int found_dirty_rid = 1;
  1572. int interval = 10;
  1573. int free_obj = 0;
  1574. int aborted = 0;
  1575. int rc = 0;
  1576. if (!data) {
  1577. return; /* no data */
  1578. }
  1579. if (data->task) {
  1580. slapi_task_inc_refcount(data->task);
  1581. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread --> refcount incremented.\n");
  1582. }
  1583. /*
  1584. * Initialize our settings
  1585. */
  1586. if(data->replica == NULL && data->repl_obj == NULL){
  1587. /*
  1588. * This thread was initiated at startup because the process did not finish. Due
  1589. * to startup timing issues, we need to wait before grabbing the replica obj, as
  1590. * the backends might not be online yet.
  1591. */
  1592. PR_Lock( notify_lock );
  1593. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(10) );
  1594. PR_Unlock( notify_lock );
  1595. data->repl_obj = replica_get_replica_from_dn(data->sdn);
  1596. if(data->repl_obj == NULL){
  1597. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to retrieve repl object from dn(%s).", data->sdn);
  1598. aborted = 1;
  1599. goto done;
  1600. }
  1601. data->replica = (Replica*)object_get_data(data->repl_obj);
  1602. free_obj = 1;
  1603. } else if(data->replica == NULL && data->repl_obj){
  1604. data->replica = (Replica*)object_get_data(data->repl_obj);
  1605. } else if( data->repl_obj == NULL && data->replica){
  1606. data->repl_obj = object_new(data->replica, NULL);
  1607. free_obj = 1;
  1608. }
  1609. /* verify we have set our repl objects */
  1610. if(data->repl_obj == NULL || data->replica == NULL){
  1611. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Unable to set the replica objects.");
  1612. aborted = 1;
  1613. goto done;
  1614. }
  1615. if(data->repl_root == NULL){
  1616. /* we must have resumed from start up, fill in the repl root */
  1617. data->repl_root = slapi_ch_strdup(slapi_sdn_get_dn(replica_get_root(data->replica)));
  1618. }
  1619. if(data->task){
  1620. slapi_task_begin(data->task, 1);
  1621. }
  1622. /*
  1623. * Presetting the rid prevents duplicate thread creation, but allows the db and changelog to still
  1624. * process updates from the rid. set_cleaned_rid() blocks updates, so we don't want to do that... yet.
  1625. */
  1626. preset_cleaned_rid(data->rid);
  1627. rid_text = slapi_ch_smprintf("%d", data->rid);
  1628. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1629. /*
  1630. * Add the cleanallruv task to the repl config - so we can handle restarts
  1631. */
  1632. add_cleaned_rid(data->rid, data->replica, csnstr, data->force); /* marks config that we started cleaning a rid */
  1633. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Cleaning rid (%d)...", data->rid);
  1634. /*
  1635. * First, wait for the maxcsn to be covered
  1636. */
  1637. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting to process all the updates from the deleted replica...");
  1638. ruv_obj = replica_get_ruv(data->replica);
  1639. ruv = object_get_data (ruv_obj);
  1640. while(data->maxcsn && !is_task_aborted(data->rid) && !is_cleaned_rid(data->rid) && !slapi_is_shutting_down()){
  1641. if(csn_get_replicaid(data->maxcsn) == 0 || ruv_covers_csn_cleanallruv(ruv,data->maxcsn) || strcasecmp(data->force,"yes") == 0){
  1642. /* We are caught up, now we can clean the ruv's */
  1643. break;
  1644. }
  1645. PR_Lock( notify_lock );
  1646. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(5) );
  1647. PR_Unlock( notify_lock );
  1648. }
  1649. object_release(ruv_obj);
  1650. /*
  1651. * Next, make sure all the replicas are up and running before sending off the clean ruv tasks
  1652. *
  1653. * Even if we are forcing the cleaning, the replicas still need to be up
  1654. */
  1655. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to be online...");
  1656. if(check_agmts_are_alive(data->replica, data->rid, data->task)){
  1657. /* error, aborted or shutdown */
  1658. aborted = 1;
  1659. goto done;
  1660. }
  1661. /*
  1662. * Make sure all the replicas have seen the max csn
  1663. */
  1664. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Waiting for all the replicas to receive all the deleted replica updates...");
  1665. if(strcasecmp(data->force,"no") == 0 && check_agmts_are_caught_up(data, csnstr)){
  1666. /* error, aborted or shutdown */
  1667. aborted = 1;
  1668. goto done;
  1669. }
  1670. /*
  1671. * Set the rid as notified - this blocks the changelog from sending out updates
  1672. * during this process, as well as prevents the db ruv from getting polluted.
  1673. */
  1674. set_cleaned_rid(data->rid);
  1675. /*
  1676. * Now send the cleanruv extended op to all the agreements
  1677. */
  1678. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Sending cleanAllRUV task to all the replicas...");
  1679. while(agmt_not_notified && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1680. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1681. if(agmt_obj == NULL){
  1682. /* no agmts, just clean this replica */
  1683. break;
  1684. }
  1685. while (agmt_obj){
  1686. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1687. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1688. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1689. agmt_not_notified = 0;
  1690. continue;
  1691. }
  1692. if(replica_cleanallruv_send_extop(agmt, data, 1) == 0){
  1693. agmt_not_notified = 0;
  1694. } else {
  1695. agmt_not_notified = 1;
  1696. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Failed to send task to replica (%s)",agmt_get_long_name(agmt));
  1697. break;
  1698. }
  1699. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1700. }
  1701. if(is_task_aborted(data->rid)){
  1702. aborted = 1;
  1703. goto done;
  1704. }
  1705. if(agmt_not_notified == 0){
  1706. break;
  1707. }
  1708. /*
  1709. * need to sleep between passes
  1710. */
  1711. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas have received the "
  1712. "cleanallruv extended op, retrying in %d seconds",interval);
  1713. PR_Lock( notify_lock );
  1714. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1715. PR_Unlock( notify_lock );
  1716. if(interval < 14400){ /* 4 hour max */
  1717. interval = interval * 2;
  1718. } else {
  1719. interval = 14400;
  1720. }
  1721. }
  1722. /*
  1723. * Run the CLEANRUV task
  1724. */
  1725. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Cleaning local ruv's...");
  1726. replica_execute_cleanruv_task (data->repl_obj, data->rid, returntext);
  1727. /*
  1728. * Wait for all the replicas to be cleaned
  1729. */
  1730. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  1731. "Waiting for all the replicas to be cleaned...");
  1732. interval = 10;
  1733. while(found_dirty_rid && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1734. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1735. if(agmt_obj == NULL){
  1736. break;
  1737. }
  1738. while (agmt_obj && !slapi_is_shutting_down()){
  1739. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1740. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1741. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1742. found_dirty_rid = 0;
  1743. continue;
  1744. }
  1745. if(replica_cleanallruv_check_ruv(data->repl_root, agmt, rid_text, data->task) == 0){
  1746. found_dirty_rid = 0;
  1747. } else {
  1748. found_dirty_rid = 1;
  1749. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Replica is not cleaned yet (%s)",
  1750. agmt_get_long_name(agmt));
  1751. break;
  1752. }
  1753. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1754. }
  1755. /* If the task is abort or everyone is cleaned, break out */
  1756. if(is_task_aborted(data->rid)){
  1757. aborted = 1;
  1758. goto done;
  1759. }
  1760. if(found_dirty_rid == 0){
  1761. break;
  1762. }
  1763. /*
  1764. * need to sleep between passes
  1765. */
  1766. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replicas have not been cleaned yet, "
  1767. "retrying in %d seconds", interval);
  1768. PR_Lock( notify_lock );
  1769. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1770. PR_Unlock( notify_lock );
  1771. if(interval < 14400){ /* 4 hour max */
  1772. interval = interval * 2;
  1773. } else {
  1774. interval = 14400;
  1775. }
  1776. } /* while */
  1777. done:
  1778. /*
  1779. * If the replicas are cleaned, release the rid, and trim the changelog
  1780. */
  1781. if(!aborted){
  1782. trigger_cl_trimming(data->rid);
  1783. delete_cleaned_rid_config(data);
  1784. /* make sure all the replicas have been "pre_cleaned" before finishing */
  1785. check_replicas_are_done_cleaning(data);
  1786. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Successfully cleaned rid(%d).", data->rid);
  1787. remove_cleaned_rid(data->rid);
  1788. } else {
  1789. /*
  1790. * Shutdown or abort
  1791. */
  1792. if(!is_task_aborted(data->rid)){
  1793. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Server shutting down. Process will resume at server startup");
  1794. } else {
  1795. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,"Task aborted for rid(%d).",data->rid);
  1796. delete_cleaned_rid_config(data);
  1797. remove_cleaned_rid(data->rid);
  1798. }
  1799. }
  1800. if(data->task){
  1801. slapi_task_finish(data->task, rc);
  1802. slapi_task_dec_refcount(data->task);
  1803. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_cleanallruv_thread <-- refcount decremented.\n");
  1804. }
  1805. if(data->payload){
  1806. ber_bvfree(data->payload);
  1807. }
  1808. if(data->repl_obj && free_obj){
  1809. object_release(data->repl_obj);
  1810. }
  1811. csn_free(&data->maxcsn);
  1812. slapi_sdn_free(&data->sdn);
  1813. slapi_ch_free_string(&data->repl_root);
  1814. slapi_ch_free_string(&data->force);
  1815. slapi_ch_free_string(&rid_text);
  1816. slapi_ch_free((void **)&data);
  1817. }
  1818. static void
  1819. replica_cleanall_ruv_destructor(Slapi_Task *task)
  1820. {
  1821. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1822. "replica_cleanall_ruv_destructor -->\n" );
  1823. if (task) {
  1824. while (slapi_task_get_refcount(task) > 0) {
  1825. /* Yield to wait for the fixup task finishes. */
  1826. DS_Sleep (PR_MillisecondsToInterval(100));
  1827. }
  1828. }
  1829. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1830. "replica_cleanall_ruv_destructor <--\n" );
  1831. }
  1832. static void
  1833. replica_cleanall_ruv_abort_destructor(Slapi_Task *task)
  1834. {
  1835. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1836. "replica_cleanall_ruv_abort_destructor -->\n" );
  1837. if (task) {
  1838. while (slapi_task_get_refcount(task) > 0) {
  1839. /* Yield to wait for the fixup task finishes. */
  1840. DS_Sleep (PR_MillisecondsToInterval(100));
  1841. }
  1842. }
  1843. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  1844. "replica_cleanall_ruv_abort_destructor <--\n" );
  1845. }
  1846. /*
  1847. * Loop over the agmts, and check if they are in the last phase of cleaning, meaning they have
  1848. * released cleanallruv data from the config
  1849. */
  1850. static void
  1851. check_replicas_are_done_cleaning(cleanruv_data *data )
  1852. {
  1853. Object *agmt_obj;
  1854. Repl_Agmt *agmt;
  1855. char csnstr[CSN_STRSIZE];
  1856. char *filter = NULL;
  1857. int not_all_cleaned = 1;
  1858. int interval = 10;
  1859. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Waiting for all the replicas to finish cleaning...");
  1860. csn_as_string(data->maxcsn, PR_FALSE, csnstr);
  1861. filter = PR_smprintf("(%s=%d:%s:%s)", type_replicaCleanRUV,(int)data->rid, csnstr, data->force);
  1862. while(not_all_cleaned && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  1863. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1864. if(agmt_obj == NULL){
  1865. not_all_cleaned = 0;
  1866. break;
  1867. }
  1868. while (agmt_obj && !slapi_is_shutting_down()){
  1869. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1870. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1871. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1872. not_all_cleaned = 0;
  1873. continue;
  1874. }
  1875. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1876. not_all_cleaned = 0;
  1877. } else {
  1878. not_all_cleaned = 1;
  1879. break;
  1880. }
  1881. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1882. }
  1883. if(not_all_cleaned == 0 || is_task_aborted(data->rid) ){
  1884. break;
  1885. }
  1886. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Not all replicas finished cleaning, retrying in %d seconds",interval);
  1887. PR_Lock( notify_lock );
  1888. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1889. PR_Unlock( notify_lock );
  1890. if(interval < 14400){ /* 4 hour max */
  1891. interval = interval * 2;
  1892. } else {
  1893. interval = 14400;
  1894. }
  1895. }
  1896. slapi_ch_free_string(&filter);
  1897. }
  1898. /*
  1899. * Search this replica for the nsds5ReplicaCleanruv attribute, we don't return
  1900. * an entry, we know it has finished cleaning.
  1901. */
  1902. static int
  1903. replica_cleanallruv_is_finished(Repl_Agmt *agmt, char *filter, Slapi_Task *task)
  1904. {
  1905. Repl_Connection *conn = NULL;
  1906. ConnResult crc = 0;
  1907. struct berval *payload = NULL;
  1908. int msgid = 0;
  1909. int rc = -1;
  1910. if((conn = conn_new(agmt)) == NULL){
  1911. return rc;
  1912. }
  1913. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  1914. payload = create_cleanruv_payload(filter);
  1915. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_CHECK_STATUS_OID, payload, NULL, &msgid);
  1916. if(crc == CONN_OPERATION_SUCCESS){
  1917. struct berval *retsdata = NULL;
  1918. char *retoid = NULL;
  1919. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  1920. if (CONN_OPERATION_SUCCESS == crc ){
  1921. char *response = NULL;
  1922. decode_cleanruv_payload(retsdata, &response);
  1923. if(response == NULL){
  1924. /* this replica does not support cleanallruv */
  1925. rc = 0;
  1926. } else if(strcmp(response,CLEANRUV_FINISHED) == 0){
  1927. /* finished cleaning */
  1928. rc = 0;
  1929. }
  1930. if (NULL != retsdata)
  1931. ber_bvfree(retsdata);
  1932. slapi_ch_free_string(&response);
  1933. slapi_ch_free_string(&retoid);
  1934. }
  1935. }
  1936. }
  1937. conn_delete_internal_ext(conn);
  1938. if(payload)
  1939. ber_bvfree(payload);
  1940. return rc;
  1941. }
  1942. /*
  1943. * Loop over the agmts, and check if they are in the last phase of aborting, meaning they have
  1944. * released the abort cleanallruv data from the config
  1945. */
  1946. static void
  1947. check_replicas_are_done_aborting(cleanruv_data *data )
  1948. {
  1949. Object *agmt_obj;
  1950. Repl_Agmt *agmt;
  1951. char *filter = NULL;
  1952. int not_all_aborted = 1;
  1953. int interval = 10;
  1954. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Waiting for all the replicas to finish aborting...");
  1955. filter = PR_smprintf("(%s=%d:%s)", type_replicaAbortCleanRUV, data->rid, data->repl_root);
  1956. while(not_all_aborted && !slapi_is_shutting_down()){
  1957. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  1958. if(agmt_obj == NULL){
  1959. not_all_aborted = 0;
  1960. break;
  1961. }
  1962. while (agmt_obj && !slapi_is_shutting_down()){
  1963. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  1964. if(get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  1965. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1966. not_all_aborted = 0;
  1967. continue;
  1968. }
  1969. if(replica_cleanallruv_is_finished(agmt, filter, data->task) == 0){
  1970. not_all_aborted = 0;
  1971. } else {
  1972. not_all_aborted = 1;
  1973. break;
  1974. }
  1975. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  1976. }
  1977. if(not_all_aborted == 0){
  1978. break;
  1979. }
  1980. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Not all replicas finished aborting, retrying in %d seconds",interval);
  1981. PR_Lock( notify_lock );
  1982. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  1983. PR_Unlock( notify_lock );
  1984. if(interval < 14400){ /* 4 hour max */
  1985. interval = interval * 2;
  1986. } else {
  1987. interval = 14400;
  1988. }
  1989. }
  1990. slapi_ch_free_string(&filter);
  1991. }
  1992. /*
  1993. * Waits for all the repl agmts to be have have the maxcsn. Returns error only on abort or shutdown
  1994. */
  1995. static int
  1996. check_agmts_are_caught_up(cleanruv_data *data, char *maxcsn)
  1997. {
  1998. Object *agmt_obj;
  1999. Repl_Agmt *agmt;
  2000. char *rid_text;
  2001. int not_all_caughtup = 1;
  2002. int interval = 10;
  2003. rid_text = slapi_ch_smprintf("%d", data->rid);
  2004. while(not_all_caughtup && !is_task_aborted(data->rid) && !slapi_is_shutting_down()){
  2005. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2006. if(agmt_obj == NULL){
  2007. not_all_caughtup = 0;
  2008. break;
  2009. }
  2010. while (agmt_obj){
  2011. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2012. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2013. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2014. not_all_caughtup = 0;
  2015. continue;
  2016. }
  2017. if(replica_cleanallruv_check_maxcsn(agmt, data->repl_root, rid_text, maxcsn, data->task) == 0){
  2018. not_all_caughtup = 0;
  2019. } else {
  2020. not_all_caughtup = 1;
  2021. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, "Replica not caught up (%s)",agmt_get_long_name(agmt));
  2022. break;
  2023. }
  2024. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2025. }
  2026. if(not_all_caughtup == 0 || is_task_aborted(data->rid) ){
  2027. break;
  2028. }
  2029. cleanruv_log(data->task, data->rid, CLEANALLRUV_ID,
  2030. "Not all replicas caught up, retrying in %d seconds",interval);
  2031. PR_Lock( notify_lock );
  2032. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2033. PR_Unlock( notify_lock );
  2034. if(interval < 14400){ /* 4 hour max */
  2035. interval = interval * 2;
  2036. } else {
  2037. interval = 14400;
  2038. }
  2039. }
  2040. slapi_ch_free_string(&rid_text);
  2041. if(is_task_aborted(data->rid)){
  2042. return -1;
  2043. }
  2044. return not_all_caughtup;
  2045. }
  2046. /*
  2047. * Waits for all the repl agmts to be online. Returns error only on abort or shutdown
  2048. */
  2049. static int
  2050. check_agmts_are_alive(Replica *replica, ReplicaId rid, Slapi_Task *task)
  2051. {
  2052. Object *agmt_obj;
  2053. Repl_Agmt *agmt;
  2054. int not_all_alive = 1;
  2055. int interval = 10;
  2056. while(not_all_alive && is_task_aborted(rid) == 0 && !slapi_is_shutting_down()){
  2057. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2058. if(agmt_obj == NULL){
  2059. not_all_alive = 0;
  2060. break;
  2061. }
  2062. while (agmt_obj){
  2063. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2064. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2065. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2066. not_all_alive = 0;
  2067. continue;
  2068. }
  2069. if(replica_cleanallruv_replica_alive(agmt) == 0){
  2070. not_all_alive = 0;
  2071. } else {
  2072. not_all_alive = 1;
  2073. cleanruv_log(task, rid, CLEANALLRUV_ID, "Replica not online (%s)",
  2074. agmt_get_long_name(agmt));
  2075. break;
  2076. }
  2077. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2078. }
  2079. if(not_all_alive == 0 || is_task_aborted(rid)){
  2080. break;
  2081. }
  2082. cleanruv_log(task, rid, CLEANALLRUV_ID, "Not all replicas online, retrying in %d seconds...",
  2083. interval);
  2084. PR_Lock( notify_lock );
  2085. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2086. PR_Unlock( notify_lock );
  2087. if(interval < 14400){ /* 4 hour max */
  2088. interval = interval * 2;
  2089. } else {
  2090. interval = 14400;
  2091. }
  2092. }
  2093. if(is_task_aborted(rid)){
  2094. return -1;
  2095. }
  2096. return not_all_alive;
  2097. }
  2098. /*
  2099. * Create the CLEANALLRUV extended op payload
  2100. */
  2101. struct berval *
  2102. create_cleanruv_payload(char *value)
  2103. {
  2104. struct berval *req_data = NULL;
  2105. BerElement *tmp_bere = NULL;
  2106. if ((tmp_bere = der_alloc()) == NULL){
  2107. goto error;
  2108. }
  2109. if (ber_printf(tmp_bere, "{s}", value) == -1){
  2110. goto error;
  2111. }
  2112. if (ber_flatten(tmp_bere, &req_data) == -1){
  2113. goto error;
  2114. }
  2115. goto done;
  2116. error:
  2117. if (NULL != req_data){
  2118. ber_bvfree(req_data);
  2119. req_data = NULL;
  2120. }
  2121. done:
  2122. if (NULL != tmp_bere){
  2123. ber_free(tmp_bere, 1);
  2124. tmp_bere = NULL;
  2125. }
  2126. return req_data;
  2127. }
  2128. /*
  2129. * Manually add the CLEANRUV task to replicas that do not support
  2130. * the CLEANALLRUV task.
  2131. */
  2132. static void
  2133. replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
  2134. {
  2135. Repl_Connection *conn;
  2136. ConnResult crc = 0;
  2137. LDAP *ld;
  2138. Slapi_DN *sdn;
  2139. struct berval *vals[2];
  2140. struct berval val;
  2141. LDAPMod *mods[2];
  2142. LDAPMod mod;
  2143. char *repl_dn = NULL;
  2144. char data[15];
  2145. int rc;
  2146. if((conn = conn_new(agmt)) == NULL){
  2147. return;
  2148. }
  2149. crc = conn_connect(conn);
  2150. if (CONN_OPERATION_SUCCESS != crc){
  2151. conn_delete_internal_ext(conn);
  2152. return;
  2153. }
  2154. ld = conn_get_ldap(conn);
  2155. if(ld == NULL){
  2156. conn_delete_internal_ext(conn);
  2157. return;
  2158. }
  2159. val.bv_len = PR_snprintf(data, sizeof(data), "CLEANRUV%d", clean_data->rid);
  2160. sdn = agmt_get_replarea(agmt);
  2161. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2162. mod.mod_type = "nsds5task";
  2163. mod.mod_bvalues = vals;
  2164. vals [0] = &val;
  2165. vals [1] = NULL;
  2166. val.bv_val = data;
  2167. mods[0] = &mod;
  2168. mods[1] = NULL;
  2169. repl_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", slapi_sdn_get_dn(sdn));
  2170. /*
  2171. * Add task to remote replica
  2172. */
  2173. rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
  2174. if(rc != LDAP_SUCCESS){
  2175. char *hostname = agmt_get_hostname(agmt);
  2176. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2177. "Failed to add CLEANRUV task (%s) to replica (%s). You will need "
  2178. "to manually run the CLEANRUV task on this replica (%s) error (%d)",
  2179. repl_dn, agmt_get_long_name(agmt), hostname, rc);
  2180. slapi_ch_free_string(&hostname);
  2181. }
  2182. slapi_ch_free_string(&repl_dn);
  2183. slapi_sdn_free(&sdn);
  2184. conn_delete_internal_ext(conn);
  2185. }
  2186. /*
  2187. * Check if the rid is in our list of "cleaned" rids
  2188. */
  2189. int
  2190. is_cleaned_rid(ReplicaId rid)
  2191. {
  2192. int i;
  2193. slapi_rwlock_rdlock(rid_lock);
  2194. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++){
  2195. if(rid == cleaned_rids[i]){
  2196. slapi_rwlock_unlock(rid_lock);
  2197. return 1;
  2198. }
  2199. }
  2200. slapi_rwlock_unlock(rid_lock);
  2201. return 0;
  2202. }
  2203. int
  2204. is_pre_cleaned_rid(ReplicaId rid)
  2205. {
  2206. int i;
  2207. slapi_rwlock_rdlock(rid_lock);
  2208. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++){
  2209. if(rid == pre_cleaned_rids[i]){
  2210. slapi_rwlock_unlock(rid_lock);
  2211. return 1;
  2212. }
  2213. }
  2214. slapi_rwlock_unlock(rid_lock);
  2215. return 0;
  2216. }
  2217. int
  2218. is_task_aborted(ReplicaId rid)
  2219. {
  2220. int i;
  2221. if(rid == 0){
  2222. return 0;
  2223. }
  2224. slapi_rwlock_rdlock(abort_rid_lock);
  2225. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++){
  2226. if(rid == aborted_rids[i]){
  2227. slapi_rwlock_unlock(abort_rid_lock);
  2228. return 1;
  2229. }
  2230. }
  2231. slapi_rwlock_unlock(abort_rid_lock);
  2232. return 0;
  2233. }
  2234. static void
  2235. preset_cleaned_rid(ReplicaId rid)
  2236. {
  2237. int i;
  2238. slapi_rwlock_wrlock(rid_lock);
  2239. for(i = 0; i < CLEANRIDSIZ; i++){
  2240. if(pre_cleaned_rids[i] == 0){
  2241. pre_cleaned_rids[i] = rid;
  2242. pre_cleaned_rids[i + 1] = 0;
  2243. break;
  2244. }
  2245. }
  2246. slapi_rwlock_unlock(rid_lock);
  2247. }
  2248. /*
  2249. * Just add the rid to the in memory, as we don't want it to survive after a restart,
  2250. * This prevent the changelog from sending updates from this rid, and the local ruv
  2251. * will not be updated either.
  2252. */
  2253. void
  2254. set_cleaned_rid(ReplicaId rid)
  2255. {
  2256. int i;
  2257. slapi_rwlock_wrlock(rid_lock);
  2258. for(i = 0; i < CLEANRIDSIZ; i++){
  2259. if(cleaned_rids[i] == 0){
  2260. cleaned_rids[i] = rid;
  2261. cleaned_rids[i + 1] = 0;
  2262. }
  2263. }
  2264. slapi_rwlock_unlock(rid_lock);
  2265. }
  2266. /*
  2267. * Add the rid and maxcsn to the repl config (so we can resume after a server restart)
  2268. */
  2269. void
  2270. add_cleaned_rid(ReplicaId rid, Replica *r, char *maxcsn, char *forcing)
  2271. {
  2272. Slapi_PBlock *pb;
  2273. struct berval *vals[2];
  2274. struct berval val;
  2275. LDAPMod *mods[2];
  2276. LDAPMod mod;
  2277. char data[CSN_STRSIZE + 10];
  2278. char *dn;
  2279. int rc;
  2280. if(r == NULL || maxcsn == NULL){
  2281. return;
  2282. }
  2283. /*
  2284. * Write the rid & maxcsn to the config entry
  2285. */
  2286. val.bv_len = PR_snprintf(data, sizeof(data),"%d:%s:%s", rid, maxcsn, forcing);
  2287. dn = replica_get_dn(r);
  2288. pb = slapi_pblock_new();
  2289. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2290. mod.mod_type = (char *)type_replicaCleanRUV;
  2291. mod.mod_bvalues = vals;
  2292. vals [0] = &val;
  2293. vals [1] = NULL;
  2294. val.bv_val = data;
  2295. mods[0] = &mod;
  2296. mods[1] = NULL;
  2297. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2298. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2299. slapi_modify_internal_pb (pb);
  2300. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2301. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2302. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: failed to update replica "
  2303. "config (%d), rid (%d)\n", rc, rid);
  2304. }
  2305. slapi_ch_free_string(&dn);
  2306. slapi_pblock_destroy(pb);
  2307. }
  2308. /*
  2309. * Add aborted rid and repl root to config in case of a server restart
  2310. */
  2311. void
  2312. add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
  2313. {
  2314. Slapi_PBlock *pb;
  2315. struct berval *vals[2];
  2316. struct berval val;
  2317. LDAPMod *mods[2];
  2318. LDAPMod mod;
  2319. char *data;
  2320. char *dn;
  2321. int rc;
  2322. int i;
  2323. slapi_rwlock_wrlock(abort_rid_lock);
  2324. for(i = 0; i < CLEANRIDSIZ; i++){
  2325. if(aborted_rids[i] == 0){
  2326. aborted_rids[i] = rid;
  2327. aborted_rids[i + 1] = 0;
  2328. break;
  2329. }
  2330. }
  2331. slapi_rwlock_unlock(abort_rid_lock);
  2332. /*
  2333. * Write the rid to the config entry
  2334. */
  2335. dn = replica_get_dn(r);
  2336. pb = slapi_pblock_new();
  2337. data = PR_smprintf("%d:%s", rid, repl_root);
  2338. mod.mod_op = LDAP_MOD_ADD|LDAP_MOD_BVALUES;
  2339. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2340. mod.mod_bvalues = vals;
  2341. vals [0] = &val;
  2342. vals [1] = NULL;
  2343. val.bv_val = data;
  2344. val.bv_len = strlen (data);
  2345. mods[0] = &mod;
  2346. mods[1] = NULL;
  2347. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2348. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2349. slapi_modify_internal_pb (pb);
  2350. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2351. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS && rc != LDAP_NO_SUCH_OBJECT){
  2352. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to update "
  2353. "replica config (%d), rid (%d)\n", rc, rid);
  2354. }
  2355. slapi_ch_free_string(&dn);
  2356. slapi_ch_free_string(&data);
  2357. slapi_pblock_destroy(pb);
  2358. }
  2359. void
  2360. delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip){
  2361. Slapi_PBlock *pb;
  2362. LDAPMod *mods[2];
  2363. LDAPMod mod;
  2364. struct berval *vals[2];
  2365. struct berval val;
  2366. char *data;
  2367. char *dn;
  2368. int rc;
  2369. int i;
  2370. if(r == NULL)
  2371. return;
  2372. if(skip){
  2373. /* skip the deleting of the config, and just remove the in memory rid */
  2374. slapi_rwlock_wrlock(abort_rid_lock);
  2375. for(i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++); /* found rid, stop */
  2376. for(; i < CLEANRIDSIZ; i++){
  2377. /* rewrite entire array */
  2378. aborted_rids[i] = aborted_rids[i + 1];
  2379. }
  2380. slapi_rwlock_unlock(abort_rid_lock);
  2381. } else {
  2382. /* only remove the config, leave the in-memory rid */
  2383. dn = replica_get_dn(r);
  2384. data = PR_smprintf("%d:%s", (int)rid, repl_root);
  2385. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2386. mod.mod_type = (char *)type_replicaAbortCleanRUV;
  2387. mod.mod_bvalues = vals;
  2388. vals [0] = &val;
  2389. vals [1] = NULL;
  2390. val.bv_val = data;
  2391. val.bv_len = strlen (data);
  2392. mods[0] = &mod;
  2393. mods[1] = NULL;
  2394. pb = slapi_pblock_new();
  2395. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2396. slapi_modify_internal_pb (pb);
  2397. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2398. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2399. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to remove replica "
  2400. "config (%d), rid (%d)\n", rc, rid);
  2401. }
  2402. slapi_pblock_destroy (pb);
  2403. slapi_ch_free_string(&dn);
  2404. slapi_ch_free_string(&data);
  2405. }
  2406. }
  2407. /*
  2408. * Just remove the dse.ldif config, but we need to keep the cleaned rids in memory until we know we are done
  2409. */
  2410. static void
  2411. delete_cleaned_rid_config(cleanruv_data *clean_data)
  2412. {
  2413. Slapi_PBlock *pb, *modpb;
  2414. Slapi_Entry **entries = NULL;
  2415. LDAPMod *mods[2];
  2416. LDAPMod mod;
  2417. struct berval *vals[5]= {0, 0, 0, 0, 0}; /* maximum of 4 tasks */
  2418. struct berval val[5];
  2419. char *iter = NULL;
  2420. char *dn = NULL;
  2421. int i, ii;
  2422. int rc = -1, ret, rid;
  2423. if(clean_data == NULL){
  2424. cleanruv_log(NULL, -1, CLEANALLRUV_ID, "delete_cleaned_rid_config: cleanruv data is NULL, "
  2425. "failed to clean the config.");
  2426. return;
  2427. }
  2428. /*
  2429. * Search the config for the exact attribute value to delete
  2430. */
  2431. pb = slapi_pblock_new();
  2432. if(clean_data->replica){
  2433. dn = replica_get_dn(clean_data->replica);
  2434. } else {
  2435. goto bail;
  2436. }
  2437. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "nsds5ReplicaCleanRUV=*", NULL, 0, NULL, NULL,
  2438. (void *)plugin_get_default_component_id(), 0);
  2439. slapi_search_internal_pb(pb);
  2440. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2441. if (ret != LDAP_SUCCESS){
  2442. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2443. "delete_cleaned_rid_config: internal search failed(%d).",ret);
  2444. goto bail;
  2445. } else {
  2446. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2447. if (entries == NULL || entries[0] == NULL){
  2448. /*
  2449. * No matching entries!
  2450. */
  2451. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2452. "delete_cleaned_rid_config: failed to find any "
  2453. "entries with nsds5ReplicaCleanRUV under (%s)", dn);
  2454. goto bail;
  2455. } else {
  2456. /*
  2457. * Clean all the matching entries
  2458. */
  2459. for(i = 0; entries[i] != NULL;i++){
  2460. char **attr_val = slapi_entry_attr_get_charray(entries[i], type_replicaCleanRUV);
  2461. char *edn = slapi_entry_get_dn(entries[i]);
  2462. int count = 0;
  2463. for (ii = 0; attr_val && attr_val[ii] && i < 5; ii++){
  2464. /* make a copy to retain the full value after toking */
  2465. char *aval = slapi_ch_strdup(attr_val[ii]);
  2466. rid = atoi(ldap_utf8strtok_r(attr_val[ii], ":", &iter));
  2467. if(rid == clean_data->rid){
  2468. val[count].bv_len = strlen(aval);
  2469. val[count].bv_val = aval;
  2470. vals[count] = &val[count];
  2471. count++;
  2472. } else {
  2473. slapi_ch_free_string(&aval);
  2474. }
  2475. }
  2476. slapi_ch_array_free(attr_val);
  2477. /*
  2478. * Now delete the attribute
  2479. */
  2480. vals[4] = NULL;
  2481. mod.mod_op = LDAP_MOD_DELETE|LDAP_MOD_BVALUES;
  2482. mod.mod_type = (char *)type_replicaCleanRUV;
  2483. mod.mod_bvalues = vals;
  2484. mods[0] = &mod;
  2485. mods[1] = NULL;
  2486. modpb = slapi_pblock_new();
  2487. slapi_modify_internal_set_pb(modpb, edn, mods, NULL, NULL,
  2488. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2489. slapi_modify_internal_pb (modpb);
  2490. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2491. slapi_pblock_destroy(modpb);
  2492. /* free the attr vals */
  2493. for (ii = 0; ii < count; ii++){
  2494. slapi_ch_free_string(&val[ii].bv_val);
  2495. }
  2496. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2497. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2498. "delete_cleaned_rid_config: failed to remove task data "
  2499. "from (%s) error (%d), rid (%d)", edn, rc, clean_data->rid);
  2500. goto bail;
  2501. }
  2502. }
  2503. }
  2504. }
  2505. bail:
  2506. if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT){
  2507. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,
  2508. "delete_cleaned_rid_config: failed to remove replica config "
  2509. "(%d), rid (%d)", rc, clean_data->rid);
  2510. }
  2511. slapi_free_search_results_internal(pb);
  2512. slapi_pblock_destroy(pb);
  2513. slapi_ch_free_string(&dn);
  2514. }
  2515. /*
  2516. * Remove the rid from our list, and the config
  2517. */
  2518. void
  2519. remove_cleaned_rid(ReplicaId rid)
  2520. {
  2521. int i;
  2522. /*
  2523. * Remove this rid, and optimize the array
  2524. */
  2525. slapi_rwlock_wrlock(rid_lock);
  2526. for(i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++); /* found rid, stop */
  2527. for(; i < CLEANRIDSIZ; i++){
  2528. /* rewrite entire array */
  2529. cleaned_rids[i] = cleaned_rids[i + 1];
  2530. }
  2531. /* now do the preset cleaned rids */
  2532. for(i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++); /* found rid, stop */
  2533. for(; i < CLEANRIDSIZ; i++){
  2534. /* rewrite entire array */
  2535. pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
  2536. }
  2537. slapi_rwlock_unlock(rid_lock);
  2538. }
  2539. /*
  2540. * Abort the CLEANALLRUV task
  2541. */
  2542. int
  2543. replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2544. int *returncode, char *returntext, void *arg)
  2545. {
  2546. struct berval *payload = NULL;
  2547. cleanruv_data *data = NULL;
  2548. PRThread *thread = NULL;
  2549. Slapi_Task *task = NULL;
  2550. Slapi_DN *sdn = NULL;
  2551. Replica *replica;
  2552. ReplicaId rid = -1;
  2553. Object *r;
  2554. const char *certify_all;
  2555. const char *base_dn;
  2556. const char *rid_str;
  2557. char *ridstr = NULL;
  2558. int rc = SLAPI_DSE_CALLBACK_OK;
  2559. if(get_abort_cleanruv_task_count() >= CLEANRIDSIZ){
  2560. /* we are already running the maximum number of tasks */
  2561. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2562. "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
  2563. CLEANRIDSIZ);
  2564. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2565. *returncode = LDAP_OPERATIONS_ERROR;
  2566. return SLAPI_DSE_CALLBACK_ERROR;
  2567. }
  2568. /* allocate new task now */
  2569. task = slapi_new_task(slapi_entry_get_ndn(e));
  2570. /* register our destructor for waiting the task is done */
  2571. slapi_task_set_destructor_fn(task, replica_cleanall_ruv_abort_destructor);
  2572. /*
  2573. * Get our task settings
  2574. */
  2575. if ((rid_str = fetch_attr(e, "replica-id", 0)) == NULL){
  2576. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-id\"");
  2577. cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2578. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2579. rc = SLAPI_DSE_CALLBACK_ERROR;
  2580. goto out;
  2581. }
  2582. certify_all = fetch_attr(e, "replica-certify-all", 0);
  2583. /*
  2584. * Check the rid
  2585. */
  2586. rid = atoi(rid_str);
  2587. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2588. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid replica id (%d) for task - (%s)",
  2589. rid, slapi_sdn_get_dn(slapi_entry_get_sdn(e)));
  2590. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"%s", returntext);
  2591. *returncode = LDAP_OPERATIONS_ERROR;
  2592. rc = SLAPI_DSE_CALLBACK_ERROR;
  2593. goto out;
  2594. }
  2595. if ((base_dn = fetch_attr(e, "replica-base-dn", 0)) == NULL){
  2596. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Missing required attr \"replica-base-dn\"");
  2597. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2598. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2599. rc = SLAPI_DSE_CALLBACK_ERROR;
  2600. goto out;
  2601. }
  2602. if(!is_cleaned_rid(rid) && !is_pre_cleaned_rid(rid)){
  2603. /* we are not cleaning this rid */
  2604. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is not being cleaned, nothing to abort.", rid);
  2605. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2606. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2607. rc = SLAPI_DSE_CALLBACK_ERROR;
  2608. goto out;
  2609. }
  2610. if(is_task_aborted(rid)){
  2611. /* we are already aborting this rid */
  2612. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Replica id (%d) is already being aborted", rid);
  2613. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2614. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2615. rc = SLAPI_DSE_CALLBACK_ERROR;
  2616. goto out;
  2617. }
  2618. /*
  2619. * Get the replica object
  2620. */
  2621. sdn = slapi_sdn_new_dn_byval(base_dn);
  2622. if((r = replica_get_replica_from_dn(sdn)) == NULL){
  2623. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to find replica from dn(%s)", base_dn);
  2624. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2625. *returncode = LDAP_OPERATIONS_ERROR;
  2626. rc = SLAPI_DSE_CALLBACK_ERROR;
  2627. goto out;
  2628. }
  2629. /*
  2630. * Check certify value
  2631. */
  2632. if(certify_all){
  2633. if(strcasecmp(certify_all,"yes") && strcasecmp(certify_all,"no")){
  2634. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Invalid value for \"replica-certify-all\", the value "
  2635. "must be \"yes\" or \"no\".");
  2636. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "%s", returntext);
  2637. *returncode = LDAP_OPERATIONS_ERROR;
  2638. rc = SLAPI_DSE_CALLBACK_ERROR;
  2639. goto out;
  2640. }
  2641. } else {
  2642. /*
  2643. * The default should be not to certify all the replicas, because
  2644. * we might be trying to abort a clean task that is "hanging" due
  2645. * to unreachable replicas. If the default is "yes" then the abort
  2646. * task will run into the same issue.
  2647. */
  2648. certify_all = "no";
  2649. }
  2650. /*
  2651. * Create payload
  2652. */
  2653. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, base_dn, certify_all);
  2654. payload = create_cleanruv_payload(ridstr);
  2655. if(payload == NULL){
  2656. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Failed to create extended op payload, aborting task");
  2657. *returncode = LDAP_OPERATIONS_ERROR;
  2658. rc = SLAPI_DSE_CALLBACK_ERROR;
  2659. goto out;
  2660. }
  2661. /*
  2662. * Stop the cleaning, and delete the rid
  2663. */
  2664. replica = (Replica*)object_get_data (r);
  2665. add_aborted_rid(rid, replica, (char *)base_dn);
  2666. stop_ruv_cleaning();
  2667. /*
  2668. * Prepare the abort struct and fire off the thread
  2669. */
  2670. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2671. if (data == NULL) {
  2672. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Failed to allocate abort_cleanruv_data. Aborting task.");
  2673. *returncode = LDAP_OPERATIONS_ERROR;
  2674. rc = SLAPI_DSE_CALLBACK_ERROR;
  2675. goto out;
  2676. }
  2677. data->repl_obj = r; /* released in replica_abort_task_thread() */
  2678. data->replica = replica;
  2679. data->task = task;
  2680. data->payload = payload;
  2681. data->rid = rid;
  2682. data->repl_root = slapi_ch_strdup(base_dn);
  2683. data->sdn = NULL;
  2684. data->certify = slapi_ch_strdup(certify_all);
  2685. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2686. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2687. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2688. if (thread == NULL) {
  2689. object_release(r);
  2690. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID,"Unable to create abort thread. Aborting task.");
  2691. *returncode = LDAP_OPERATIONS_ERROR;
  2692. slapi_ch_free_string(&data->certify);
  2693. rc = SLAPI_DSE_CALLBACK_ERROR;
  2694. }
  2695. out:
  2696. slapi_ch_free_string(&ridstr);
  2697. slapi_sdn_free(&sdn);
  2698. if(rc != SLAPI_DSE_CALLBACK_OK){
  2699. cleanruv_log(task, rid, ABORT_CLEANALLRUV_ID, "Abort Task failed (%d)", rc);
  2700. slapi_task_finish(task, rc);
  2701. }
  2702. return rc;
  2703. }
  2704. /*
  2705. * Abort CLEANALLRUV task thread
  2706. */
  2707. void
  2708. replica_abort_task_thread(void *arg)
  2709. {
  2710. cleanruv_data *data = (cleanruv_data *)arg;
  2711. Repl_Agmt *agmt;
  2712. Object *agmt_obj;
  2713. int agmt_not_notified = 1;
  2714. int interval = 10;
  2715. int release_it = 0;
  2716. int count = 0, rc = 0;
  2717. if (!data) {
  2718. return; /* no data */
  2719. }
  2720. if (data->task) {
  2721. slapi_task_inc_refcount(data->task);
  2722. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread --> refcount incremented.\n");
  2723. }
  2724. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Aborting task for rid(%d)...",data->rid);
  2725. /*
  2726. * Need to build the replica from the dn
  2727. */
  2728. if(data->replica == NULL && data->repl_obj == NULL){
  2729. /*
  2730. * This thread was initiated at startup because the process did not finish. Due
  2731. * to timing issues, we need to wait to grab the replica obj until we get here.
  2732. */
  2733. if((data->repl_obj = replica_get_replica_from_dn(data->sdn)) == NULL){
  2734. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to get replica object from dn (%s).", slapi_sdn_get_dn(data->sdn));
  2735. goto done;
  2736. }
  2737. if(data->replica == NULL && data->repl_obj){
  2738. data->replica = (Replica*)object_get_data(data->repl_obj);
  2739. }
  2740. release_it = 1;
  2741. }
  2742. /*
  2743. * Now send the cleanruv extended op to all the agreements
  2744. */
  2745. while(agmt_not_notified && !slapi_is_shutting_down()){
  2746. agmt_obj = agmtlist_get_first_agreement_for_replica (data->replica);
  2747. if(agmt_obj == NULL){
  2748. agmt_not_notified = 0;
  2749. break;
  2750. }
  2751. while (agmt_obj){
  2752. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2753. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2754. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2755. agmt_not_notified = 0;
  2756. continue;
  2757. }
  2758. if(replica_cleanallruv_send_abort_extop(agmt, data->task, data->payload)){
  2759. if(strcasecmp(data->certify,"yes") == 0){
  2760. /* we are verifying all the replicas receive the abort task */
  2761. agmt_not_notified = 1;
  2762. break;
  2763. } else {
  2764. /* we do not care if we could not reach a replica, just continue as if we did */
  2765. agmt_not_notified = 0;
  2766. }
  2767. } else {
  2768. /* success */
  2769. agmt_not_notified = 0;
  2770. }
  2771. agmt_obj = agmtlist_get_next_agreement_for_replica (data->replica, agmt_obj);
  2772. } /* while loop for agmts */
  2773. if(agmt_not_notified == 0){
  2774. /* everybody has been contacted */
  2775. break;
  2776. }
  2777. /*
  2778. * need to sleep between passes
  2779. */
  2780. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Retrying in %d seconds",interval);
  2781. PR_Lock( notify_lock );
  2782. PR_WaitCondVar( notify_cvar, PR_SecondsToInterval(interval) );
  2783. PR_Unlock( notify_lock );
  2784. if(interval < 14400){ /* 4 hour max */
  2785. interval = interval * 2;
  2786. } else {
  2787. interval = 14400;
  2788. }
  2789. } /* while */
  2790. done:
  2791. if(agmt_not_notified){
  2792. /* failure */
  2793. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID,"Abort task failed, will resume the task at the next server startup.");
  2794. } else {
  2795. /*
  2796. * Wait for this server to stop its cleanallruv task(which removes the rid from the cleaned list)
  2797. */
  2798. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Waiting for CleanAllRUV task to abort...");
  2799. while(is_cleaned_rid(data->rid)){
  2800. DS_Sleep(PR_SecondsToInterval(1));
  2801. count++;
  2802. if(count == 60){ /* it should not take this long */
  2803. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "CleanAllRUV task failed to abort. You might need to "
  2804. "rerun the task.");
  2805. rc = -1;
  2806. break;
  2807. }
  2808. }
  2809. /*
  2810. * Clean up the config
  2811. */
  2812. delete_aborted_rid(data->replica, data->rid, data->repl_root, 1); /* delete just the config, leave rid in memory */
  2813. if(strcasecmp(data->certify, "yes") == 0){
  2814. check_replicas_are_done_aborting(data);
  2815. }
  2816. delete_aborted_rid(data->replica, data->rid, data->repl_root, 0); /* remove the in-memory aborted rid */
  2817. if(rc == 0){
  2818. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Successfully aborted task for rid(%d)", data->rid);
  2819. } else {
  2820. cleanruv_log(data->task, data->rid, ABORT_CLEANALLRUV_ID, "Failed to abort task for rid(%d)",data->rid);
  2821. }
  2822. }
  2823. if(data->task){
  2824. slapi_task_finish(data->task, agmt_not_notified);
  2825. slapi_task_dec_refcount(data->task);
  2826. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "replica_abort_task_thread <-- refcount incremented.\n");
  2827. }
  2828. if(data->repl_obj && release_it)
  2829. object_release(data->repl_obj);
  2830. if(data->payload){
  2831. ber_bvfree(data->payload);
  2832. }
  2833. slapi_ch_free_string(&data->repl_root);
  2834. slapi_ch_free_string(&data->certify);
  2835. slapi_sdn_free(&data->sdn);
  2836. slapi_ch_free((void **)&data);
  2837. }
  2838. static int
  2839. replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task, struct berval *payload)
  2840. {
  2841. Repl_Connection *conn = NULL;
  2842. ConnResult crc = 0;
  2843. int msgid = 0;
  2844. int rc = -1;
  2845. if((conn = conn_new(ra)) == NULL){
  2846. return rc;
  2847. }
  2848. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2849. crc = conn_send_extended_operation(conn, REPL_ABORT_CLEANRUV_OID, payload, NULL, &msgid);
  2850. /*
  2851. * success or failure, just return the error code
  2852. */
  2853. rc = crc;
  2854. if(rc){
  2855. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2856. "Failed to send extop to replica(%s).", agmt_get_long_name(ra));
  2857. }
  2858. } else {
  2859. cleanruv_log(task, agmt_get_consumer_rid(ra, conn), ABORT_CLEANALLRUV_ID,
  2860. "Failed to connect to replica(%s).", agmt_get_long_name(ra));
  2861. rc = -1;
  2862. }
  2863. conn_delete_internal_ext(conn);
  2864. return rc;
  2865. }
  2866. static int
  2867. replica_cleanallruv_send_extop(Repl_Agmt *ra, cleanruv_data *clean_data, int check_result)
  2868. {
  2869. Repl_Connection *conn = NULL;
  2870. ConnResult crc = 0;
  2871. int msgid = 0;
  2872. int rc = -1;
  2873. if((conn = conn_new(ra)) == NULL){
  2874. return rc;
  2875. }
  2876. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2877. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_OID, clean_data->payload, NULL, &msgid);
  2878. if(crc == CONN_OPERATION_SUCCESS && check_result){
  2879. struct berval *retsdata = NULL;
  2880. char *retoid = NULL;
  2881. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2882. if (CONN_OPERATION_SUCCESS == crc ){
  2883. char *response = NULL;
  2884. decode_cleanruv_payload(retsdata, &response);
  2885. if(response && strcmp(response,CLEANRUV_ACCEPTED) == 0){
  2886. /* extop was accepted */
  2887. rc = 0;
  2888. } else {
  2889. cleanruv_log(clean_data->task, clean_data->rid, CLEANALLRUV_ID,"Replica %s does not support the CLEANALLRUV task. "
  2890. "Sending replica CLEANRUV task...", slapi_sdn_get_dn(agmt_get_dn_byref(ra)));
  2891. /*
  2892. * Ok, this replica doesn't know about CLEANALLRUV, so just manually
  2893. * add the CLEANRUV task to the replica.
  2894. */
  2895. replica_send_cleanruv_task(ra, clean_data);
  2896. rc = 0;
  2897. }
  2898. if (NULL != retsdata)
  2899. ber_bvfree(retsdata);
  2900. slapi_ch_free_string(&retoid);
  2901. slapi_ch_free_string(&response);
  2902. }
  2903. } else {
  2904. /*
  2905. * success or failure, just return the error code
  2906. */
  2907. rc = crc;
  2908. }
  2909. }
  2910. conn_delete_internal_ext(conn);
  2911. return rc;
  2912. }
  2913. static CSN*
  2914. replica_cleanallruv_find_maxcsn(Replica *replica, ReplicaId rid, char *basedn)
  2915. {
  2916. Object *agmt_obj;
  2917. Repl_Agmt *agmt;
  2918. CSN *maxcsn = NULL, *topcsn = NULL;
  2919. char *rid_text = slapi_ch_smprintf("%d", rid);
  2920. char *csnstr = NULL;
  2921. /* start with the local maxcsn */
  2922. csnstr = replica_cleanallruv_get_local_maxcsn(rid, basedn);
  2923. if(csnstr){
  2924. topcsn = csn_new();
  2925. csn_init_by_string(topcsn, csnstr);
  2926. slapi_ch_free_string(&csnstr);
  2927. }
  2928. agmt_obj = agmtlist_get_first_agreement_for_replica (replica);
  2929. if(agmt_obj == NULL){ /* no agreements */
  2930. goto done;
  2931. }
  2932. while (agmt_obj && !slapi_is_shutting_down()){
  2933. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2934. if(!agmt_is_enabled(agmt) || get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS){
  2935. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2936. continue;
  2937. }
  2938. if(replica_cleanallruv_get_replica_maxcsn(agmt, rid_text, basedn, &maxcsn) == 0){
  2939. if(maxcsn == NULL){
  2940. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2941. continue;
  2942. }
  2943. if(topcsn == NULL){
  2944. topcsn = maxcsn;
  2945. } else {
  2946. if(csn_compare(topcsn, maxcsn) < 0){
  2947. csn_free(&topcsn);
  2948. topcsn = maxcsn;
  2949. } else {
  2950. csn_free(&maxcsn);
  2951. }
  2952. }
  2953. }
  2954. agmt_obj = agmtlist_get_next_agreement_for_replica (replica, agmt_obj);
  2955. }
  2956. done:
  2957. slapi_ch_free_string(&rid_text);
  2958. return topcsn;
  2959. }
  2960. static int
  2961. replica_cleanallruv_get_replica_maxcsn(Repl_Agmt *agmt, char *rid_text, char *basedn, CSN **csn)
  2962. {
  2963. Repl_Connection *conn = NULL;
  2964. ConnResult crc = -1;
  2965. struct berval *payload = NULL;
  2966. CSN *maxcsn = NULL;
  2967. char *data = NULL;
  2968. int msgid = 0;
  2969. if((conn = conn_new(agmt)) == NULL){
  2970. return -1;
  2971. }
  2972. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  2973. payload = create_cleanruv_payload(data);
  2974. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  2975. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  2976. if(crc == CONN_OPERATION_SUCCESS){
  2977. struct berval *retsdata = NULL;
  2978. char *retoid = NULL;
  2979. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  2980. if (CONN_OPERATION_SUCCESS == crc ){
  2981. char *remote_maxcsn = NULL;
  2982. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  2983. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  2984. maxcsn = csn_new();
  2985. csn_init_by_string(maxcsn, remote_maxcsn);
  2986. *csn = maxcsn;
  2987. } else {
  2988. /* no csn */
  2989. *csn = NULL;
  2990. }
  2991. slapi_ch_free_string(&retoid);
  2992. slapi_ch_free_string(&remote_maxcsn);
  2993. if (NULL != retsdata)
  2994. ber_bvfree(retsdata);
  2995. }
  2996. }
  2997. }
  2998. conn_delete_internal_ext(conn);
  2999. slapi_ch_free_string(&data);
  3000. if(payload)
  3001. ber_bvfree(payload);
  3002. return (int)crc;
  3003. }
  3004. static int
  3005. replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task)
  3006. {
  3007. Repl_Connection *conn = NULL;
  3008. ConnResult crc = 0;
  3009. struct berval *payload = NULL;
  3010. char *data = NULL;
  3011. int msgid = 0;
  3012. int rc = -1;
  3013. if((conn = conn_new(agmt)) == NULL){
  3014. return -1;
  3015. }
  3016. data = slapi_ch_smprintf("%s:%s",rid_text, basedn);
  3017. payload = create_cleanruv_payload(data);
  3018. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3019. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3020. if(crc == CONN_OPERATION_SUCCESS){
  3021. struct berval *retsdata = NULL;
  3022. char *retoid = NULL;
  3023. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3024. if (CONN_OPERATION_SUCCESS == crc ){
  3025. char *remote_maxcsn = NULL;
  3026. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3027. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3028. CSN *max, *repl_max;
  3029. max = csn_new();
  3030. repl_max = csn_new();
  3031. csn_init_by_string(max, maxcsn);
  3032. csn_init_by_string(repl_max, remote_maxcsn);
  3033. if(csn_compare (repl_max, max) < 0){
  3034. /* we are not caught up yet, free, and return */
  3035. cleanruv_log(task, atoi(rid_text), CLEANALLRUV_ID,"Replica maxcsn (%s) is not caught up with deleted replica's maxcsn(%s)",
  3036. remote_maxcsn, maxcsn);
  3037. rc = -1;
  3038. } else {
  3039. /* ok this replica is caught up */
  3040. rc = 0;
  3041. }
  3042. csn_free(&max);
  3043. csn_free(&repl_max);
  3044. } else {
  3045. /* no remote_maxcsn - return success */
  3046. rc = 0;
  3047. }
  3048. slapi_ch_free_string(&retoid);
  3049. slapi_ch_free_string(&remote_maxcsn);
  3050. if (NULL != retsdata)
  3051. ber_bvfree(retsdata);
  3052. }
  3053. }
  3054. }
  3055. conn_delete_internal_ext(conn);
  3056. slapi_ch_free_string(&data);
  3057. if(payload)
  3058. ber_bvfree(payload);
  3059. return rc;
  3060. }
  3061. static int
  3062. replica_cleanallruv_replica_alive(Repl_Agmt *agmt)
  3063. {
  3064. Repl_Connection *conn = NULL;
  3065. LDAP *ld = NULL;
  3066. LDAPMessage *result = NULL;
  3067. int rc = -1;
  3068. if((conn = conn_new(agmt)) == NULL){
  3069. return rc;
  3070. }
  3071. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3072. ld = conn_get_ldap(conn);
  3073. if(ld == NULL){
  3074. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "CleanAllRUV_task: failed to get LDAP "
  3075. "handle from the replication agmt (%s). Moving on to the next agmt.\n",agmt_get_long_name(agmt));
  3076. conn_delete_internal_ext(conn);
  3077. return -1;
  3078. }
  3079. if(ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "objectclass=top",
  3080. NULL, 0, NULL, NULL, NULL, 0, &result) == LDAP_SUCCESS)
  3081. {
  3082. rc = 0;
  3083. } else {
  3084. rc = -1;
  3085. }
  3086. if(result)
  3087. ldap_msgfree( result );
  3088. }
  3089. conn_delete_internal_ext(conn);
  3090. return rc;
  3091. }
  3092. static int
  3093. replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text, Slapi_Task *task)
  3094. {
  3095. Repl_Connection *conn = NULL;
  3096. ConnResult crc = 0;
  3097. struct berval *payload = NULL;
  3098. char *data = NULL;
  3099. int msgid = 0;
  3100. int rc = -1;
  3101. if((conn = conn_new(agmt)) == NULL){
  3102. return rc;
  3103. }
  3104. data = slapi_ch_smprintf("%s:%s",rid_text, repl_root);
  3105. payload = create_cleanruv_payload(data);
  3106. if(conn_connect(conn) == CONN_OPERATION_SUCCESS){
  3107. crc = conn_send_extended_operation(conn, REPL_CLEANRUV_GET_MAXCSN_OID, payload, NULL, &msgid);
  3108. if(crc == CONN_OPERATION_SUCCESS){
  3109. struct berval *retsdata = NULL;
  3110. char *retoid = NULL;
  3111. crc = conn_read_result_ex(conn, &retoid, &retsdata, NULL, msgid, NULL, 1);
  3112. if (CONN_OPERATION_SUCCESS == crc ){
  3113. char *remote_maxcsn = NULL;
  3114. decode_cleanruv_payload(retsdata, &remote_maxcsn);
  3115. if(remote_maxcsn && strcmp(remote_maxcsn, CLEANRUV_NO_MAXCSN)){
  3116. /* remote replica still has dirty RUV element */
  3117. rc = -1;
  3118. } else {
  3119. /* no maxcsn = we're clean */
  3120. rc = 0;
  3121. }
  3122. slapi_ch_free_string(&retoid);
  3123. slapi_ch_free_string(&remote_maxcsn);
  3124. if (NULL != retsdata)
  3125. ber_bvfree(retsdata);
  3126. }
  3127. }
  3128. }
  3129. conn_delete_internal_ext(conn);
  3130. slapi_ch_free_string(&data);
  3131. if(payload)
  3132. ber_bvfree(payload);
  3133. return rc;
  3134. }
  3135. static int
  3136. get_cleanruv_task_count()
  3137. {
  3138. int i, count = 0;
  3139. slapi_rwlock_wrlock(rid_lock);
  3140. for(i = 0; i < CLEANRIDSIZ; i++){
  3141. if(cleaned_rids[i] != 0){
  3142. count++;
  3143. }
  3144. }
  3145. slapi_rwlock_unlock(rid_lock);
  3146. return count;
  3147. }
  3148. static int
  3149. get_abort_cleanruv_task_count()
  3150. {
  3151. int i, count = 0;
  3152. slapi_rwlock_wrlock(rid_lock);
  3153. for(i = 0; i < CLEANRIDSIZ; i++){
  3154. if(aborted_rids[i] != 0){
  3155. count++;
  3156. }
  3157. }
  3158. slapi_rwlock_unlock(rid_lock);
  3159. return count;
  3160. }
  3161. /*
  3162. * Notify sleeping CLEANALLRUV threads to stop
  3163. */
  3164. void
  3165. stop_ruv_cleaning()
  3166. {
  3167. if(notify_lock){
  3168. PR_Lock( notify_lock );
  3169. PR_NotifyCondVar( notify_cvar );
  3170. PR_Unlock( notify_lock );
  3171. }
  3172. }
  3173. /*
  3174. * Write our logging to the task and error log
  3175. */
  3176. void
  3177. cleanruv_log(Slapi_Task *task, int rid, char *task_type, char *fmt, ...)
  3178. {
  3179. va_list ap1;
  3180. va_list ap2;
  3181. va_list ap3;
  3182. va_list ap4;
  3183. char *errlog_fmt;
  3184. va_start(ap1, fmt);
  3185. va_start(ap2, fmt);
  3186. va_start(ap3, fmt);
  3187. va_start(ap4, fmt);
  3188. if(task){
  3189. slapi_task_log_notice_ext(task, fmt, ap1);
  3190. slapi_task_log_status_ext(task, fmt, ap2);
  3191. slapi_task_inc_progress(task);
  3192. }
  3193. errlog_fmt = PR_smprintf("%s (rid %d): %s \n",task_type, rid, fmt);
  3194. slapi_log_error_ext(SLAPI_LOG_FATAL, repl_plugin_name, errlog_fmt, ap3, ap4);
  3195. slapi_ch_free_string(&errlog_fmt);
  3196. va_end(ap1);
  3197. va_end(ap2);
  3198. va_end(ap3);
  3199. va_end(ap4);
  3200. }
  3201. char *
  3202. replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn)
  3203. {
  3204. Slapi_PBlock *search_pb = NULL;
  3205. Slapi_Entry **entries = NULL;
  3206. char **ruv_elements = NULL;
  3207. char *maxcsn = NULL;
  3208. char *filter = NULL;
  3209. char *ridstr = NULL;
  3210. char *iter = NULL;
  3211. char *attrs[2];
  3212. char *ruv_part = NULL;
  3213. int part_count = 0;
  3214. int res, i;
  3215. /*
  3216. * Get the maxruv from the database tombstone entry
  3217. */
  3218. filter = "(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))";
  3219. attrs[0] = "nsds50ruv";
  3220. attrs[1] = NULL;
  3221. ridstr = slapi_ch_smprintf("{replica %d ldap", rid);
  3222. search_pb = slapi_pblock_new();
  3223. slapi_search_internal_set_pb(search_pb, base_dn, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  3224. slapi_search_internal_pb (search_pb);
  3225. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  3226. if ( LDAP_SUCCESS == res ) {
  3227. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3228. if (NULL == entries || entries[0] == NULL) {
  3229. /* Hmmm, no tombstone! Error out */
  3230. } else {
  3231. /* find the right ruv element, and find the maxcsn */
  3232. ruv_elements = slapi_entry_attr_get_charray(entries[0],attrs[0]);
  3233. for(i = 0; ruv_elements && ruv_elements[i] ; i++){
  3234. if(strstr(ruv_elements[i], ridstr)){
  3235. /* get the max csn */
  3236. ruv_part = ldap_utf8strtok_r(ruv_elements[i], " ", &iter);
  3237. for(part_count = 1; ruv_part && part_count < 5; part_count++){
  3238. ruv_part = ldap_utf8strtok_r(iter, " ", &iter);
  3239. }
  3240. if(part_count == 5 && ruv_part){/* we have the maxcsn */
  3241. maxcsn = slapi_ch_strdup(ruv_part);
  3242. break;
  3243. }
  3244. }
  3245. }
  3246. slapi_ch_array_free(ruv_elements);
  3247. }
  3248. } else {
  3249. /* internal search failed */
  3250. cleanruv_log(NULL, (int)rid, CLEANALLRUV_ID, "replica_cleanallruv_get_local_maxcsn: internal search failed (%d)\n", res);
  3251. }
  3252. slapi_free_search_results_internal(search_pb);
  3253. slapi_pblock_destroy(search_pb);
  3254. slapi_ch_free_string(&ridstr);
  3255. return maxcsn;
  3256. }