cl5_api.c 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * Copyright (C) 2010 Hewlett-Packard Development Company, L.P.
  37. * All rights reserved.
  38. * END COPYRIGHT BLOCK **/
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. /* cl5_api.c - implementation of 5.0 style changelog API */
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #include <sys/stat.h>
  46. #if defined( OS_solaris ) || defined( hpux )
  47. #include <sys/types.h>
  48. #include <sys/statvfs.h>
  49. #endif
  50. #if defined( linux )
  51. #include <sys/vfs.h>
  52. #endif
  53. #include "cl5.h"
  54. #include "cl_crypt.h"
  55. #include "plhash.h"
  56. #include "plstr.h"
  57. #include "db.h"
  58. #include "cl5_clcache.h" /* To use the Changelog Cache */
  59. #include "repl5.h" /* for agmt_get_consumer_rid() */
  60. #define GUARDIAN_FILE "guardian" /* name of the guardian file */
  61. #define VERSION_FILE "DBVERSION" /* name of the version file */
  62. #define V_5 5 /* changelog entry version */
  63. #define CHUNK_SIZE 64*1024
  64. #define DBID_SIZE 64
  65. #define FILE_SEP "_" /* separates parts of the db file name */
  66. #define T_CSNSTR "csn"
  67. #define T_UNIQUEIDSTR "nsuniqueid"
  68. #define T_PARENTIDSTR "parentuniqueid"
  69. #define T_NEWSUPERIORDNSTR "newsuperiordn"
  70. #define T_NEWSUPERIORIDSTR "newsuperioruniqueid"
  71. #define T_REPLGEN "replgen"
  72. #define ENTRY_COUNT_TIME 111 /* this time is used to construct csn
  73. used to store/retrieve entry count */
  74. #define PURGE_RUV_TIME 222 /* this time is used to construct csn
  75. used to store purge RUV vector */
  76. #define MAX_RUV_TIME 333 /* this time is used to construct csn
  77. used to store upper boundary RUV vector */
  78. #define DB_EXTENSION_DB3 "db3"
  79. #define DB_EXTENSION "db4"
  80. #define HASH_BACKETS_COUNT 16 /* number of buckets in a hash table */
  81. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4100
  82. #define USE_DB_TXN 1 /* use transactions */
  83. #define DEFAULT_DB_ENV_OP_FLAGS DB_AUTO_COMMIT
  84. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR <= 4300
  85. /* we are enabling transactions everywhere - since we are opening databases
  86. with DB_AUTO_COMMIT we must either open and pass a valid txn handle to
  87. all operations that modify the database (put, del) or we must pass the
  88. DB_AUTO_COMMIT flag to those operations */
  89. #define DEFAULT_DB_OP_FLAGS(txn) (txn ? 0 : DB_AUTO_COMMIT)
  90. #else
  91. #define DEFAULT_DB_OP_FLAGS(txn) 0
  92. #endif
  93. #define DB_OPEN(oflags, db, txnid, file, database, type, flags, mode, rval) \
  94. { \
  95. if (((oflags) & DB_INIT_TXN) && ((oflags) & DB_INIT_LOG)) \
  96. { \
  97. (rval) = (db)->open((db), (txnid), (file), (database), (type), (flags)|DB_AUTO_COMMIT, (mode)); \
  98. } \
  99. else \
  100. { \
  101. (rval) = (db)->open((db), (txnid), (file), (database), (type), (flags), (mode)); \
  102. } \
  103. }
  104. #else /* older then db 41 */
  105. #define DEFAULT_DB_OP_FLAGS(txn) 0
  106. #define DB_OPEN(oflags, db, txnid, file, database, type, flags, mode, rval) \
  107. (rval) = (db)->open((db), (file), (database), (type), (flags), (mode))
  108. #endif
  109. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4000
  110. #define DB_ENV_SET_REGION_INIT(env) (env)->set_flags((env), DB_REGION_INIT, 1)
  111. #define TXN_BEGIN(env, parent_txn, tid, flags) \
  112. (env)->txn_begin((env), (parent_txn), (tid), (flags))
  113. #define TXN_COMMIT(txn, flags) (txn)->commit((txn), (flags))
  114. #define TXN_ABORT(txn) (txn)->abort(txn)
  115. #define MEMP_STAT(env, gsp, fsp, flags, malloc) \
  116. (env)->memp_stat((env), (gsp), (fsp), (flags))
  117. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4400 /* db4.4 or later */
  118. #define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
  119. (env)->mutex_set_tas_spins((env), (tas_spins))
  120. #else
  121. #define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
  122. (env)->set_tas_spins((env), (tas_spins))
  123. #endif
  124. #else /* older than db 4.0 */
  125. #define DB_ENV_SET_REGION_INIT(env) db_env_set_region_init(1)
  126. #define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
  127. db_env_set_tas_spins((tas_spins))
  128. #define TXN_BEGIN(env, parent_txn, tid, flags) \
  129. txn_begin((env), (parent_txn), (tid), (flags))
  130. #define TXN_COMMIT(txn, flags) txn_commit((txn), (flags))
  131. #define TXN_ABORT(txn) txn_abort((txn))
  132. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3300
  133. #define MEMP_STAT(env, gsp, fsp, flags, malloc) memp_stat((env), (gsp), (fsp))
  134. #else /* older than db 3.3 */
  135. #define MEMP_STAT(env, gsp, fsp, flags, malloc) \
  136. memp_stat((env), (gsp), (fsp), (malloc))
  137. #endif
  138. #endif
  139. /*
  140. * The defult thread stacksize for nspr21 is 64k. For OSF, we require
  141. * a larger stacksize as actual storage allocation is higher i.e
  142. * pointers are allocated 8 bytes but lower 4 bytes are used.
  143. * The value 0 means use the default stacksize.
  144. */
  145. #if defined (OSF1) || defined (__LP64__) || defined (_LP64) /* 64-bit architectures need bigger stacks */
  146. #if defined(__hpux) && defined(__ia64)
  147. #define DEFAULT_THREAD_STACKSIZE 524288L
  148. #else
  149. #define DEFAULT_THREAD_STACKSIZE 131072L
  150. #endif
  151. #else
  152. #define DEFAULT_THREAD_STACKSIZE 0
  153. #endif
  154. #ifdef _WIN32
  155. #define FILE_CREATE_MODE S_IREAD | S_IWRITE
  156. #define DIR_CREATE_MODE 0755
  157. #else /* _WIN32 */
  158. #define FILE_CREATE_MODE S_IRUSR | S_IWUSR
  159. #define DIR_CREATE_MODE 0755
  160. #endif
  161. #define NO_DISK_SPACE 1024
  162. #define MIN_DISK_SPACE 10485760 /* 10 MB */
  163. /***** Data Definitions *****/
  164. /* possible changelog open modes */
  165. typedef enum
  166. {
  167. CL5_OPEN_NONE, /* nothing specified */
  168. CL5_OPEN_NORMAL, /* open for normal read/write use */
  169. CL5_OPEN_RESTORE_RECOVER, /* restore from archive and recover */
  170. CL5_OPEN_RESTORE, /* restore, but no recovery */
  171. CL5_OPEN_LDIF2CL, /* open as part of ldif2cl: no locking,
  172. recovery, checkpointing */
  173. CL5_OPEN_CLEAN_RECOVER /* remove env after recover open (upgrade) */
  174. } CL5OpenMode;
  175. #define DB_FILE_DELETED 0x1
  176. #define DB_FILE_INIT 0x2
  177. /* this structure represents one changelog file, Each changelog file contains
  178. changes applied to a single backend. Files are named by the database id */
  179. typedef struct cl5dbfile
  180. {
  181. char *name; /* file name (with the extension) */
  182. char *replGen; /* replica generation of the data */
  183. char *replName; /* replica name */
  184. DB *db; /* db handle to the changelog file*/
  185. int entryCount; /* number of entries in the file */
  186. int flags; /* currently used to mark the file as deleted
  187. * or as initialized */
  188. RUV *purgeRUV; /* ruv to which the file has been purged */
  189. RUV *maxRUV; /* ruv that marks the upper boundary of the data */
  190. char *semaName; /* semaphore name */
  191. PRSem *sema; /* semaphore for max concurrent cl writes */
  192. }CL5DBFile;
  193. /* structure that allows to iterate through entries to be sent to a consumer
  194. that originated on a particular supplier. */
  195. struct cl5replayiterator
  196. {
  197. Object *fileObj;
  198. CLC_Buffer *clcache; /* changelog cache */
  199. ReplicaId consumerRID; /* consumer's RID */
  200. const RUV *consumerRuv; /* consumer's update vector */
  201. Object *supplierRuvObj;/* supplier's update vector object */
  202. };
  203. typedef struct cl5iterator
  204. {
  205. DBC *cursor; /* current position in the db file */
  206. Object *file; /* handle to release db file object */
  207. }CL5Iterator;
  208. /* changelog trimming configuration */
  209. typedef struct cl5trim
  210. {
  211. time_t maxAge; /* maximum entry age in seconds */
  212. int maxEntries; /* maximum number of entries across all changelog files */
  213. PRLock* lock; /* controls access to trimming configuration */
  214. } CL5Trim;
  215. /* this structure defines 5.0 changelog internals */
  216. typedef struct cl5desc
  217. {
  218. char *dbDir; /* absolute path to changelog directory */
  219. DB_ENV *dbEnv; /* db environment shared by all db files */
  220. int dbEnvOpenFlags;/* openflag used for env->open */
  221. Objset *dbFiles; /* ref counted set of changelog files (CL5DBFile) */
  222. PRLock *fileLock; /* ensures that changelog file is not added twice */
  223. CL5OpenMode dbOpenMode; /* how we open db */
  224. CL5DBConfig dbConfig; /* database configuration params */
  225. CL5Trim dbTrim; /* trimming parameters */
  226. CL5State dbState; /* changelog current state */
  227. Slapi_RWLock *stLock; /* lock that controls access to the changelog state */
  228. PRBool dbRmOnClose;/* indicates whether changelog should be removed when
  229. it is closed */
  230. PRBool fatalError; /* bad stuff happened like out of disk space; don't
  231. write guardian file on close - UnUsed so far */
  232. int threadCount;/* threads that globally access changelog like
  233. deadlock detection, etc. */
  234. PRLock *clLock; /* Lock associated to clVar, used to notify threads on close */
  235. PRCondVar *clCvar; /* Condition Variable used to notify threads on close */
  236. void *clcrypt_handle; /* for cl encryption */
  237. } CL5Desc;
  238. typedef void (*VFP)(void *);
  239. /***** Global Variables *****/
  240. static CL5Desc s_cl5Desc;
  241. /***** Forward Declarations *****/
  242. /* changelog initialization and cleanup */
  243. static int _cl5Open (const char *dir, const CL5DBConfig *config, CL5OpenMode openMode);
  244. static int _cl5AppInit (void);
  245. static int _cl5DBOpen (void);
  246. static void _cl5SetDefaultDBConfig ();
  247. static void _cl5SetDBConfig (const CL5DBConfig *config);
  248. static int _cl5CheckDBVersion ();
  249. static int _cl5ReadDBVersion (const char *dir, char *clVersion, int buflen);
  250. static int _cl5WriteDBVersion ();
  251. static void _cl5Close ();
  252. static int _cl5Delete (const char *dir, PRBool rmDir);
  253. static void _cl5DBClose ();
  254. /* thread management */
  255. static int _cl5DispatchDBThreads ();
  256. static int _cl5AddThread ();
  257. static void _cl5RemoveThread ();
  258. /* functions that work with individual changelog files */
  259. static int _cl5NewDBFile (const char *replName, const char *replGen, CL5DBFile** dbFile);
  260. static int _cl5DBOpenFile (Object *replica, Object **obj, PRBool checkDups);
  261. static int _cl5DBOpenFileByReplicaName (const char *replName, const char *replGen,
  262. Object **obj, PRBool checkDups);
  263. static void _cl5DBCloseFile (void **data);
  264. static void _cl5DBDeleteFile (Object *obj);
  265. static void _cl5DBFileInitialized (Object *obj);
  266. static int _cl5GetDBFile (Object *replica, Object **obj);
  267. static int _cl5GetDBFileByReplicaName (const char *replName, const char *replGen,
  268. Object **obj);
  269. static int _cl5AddDBFile (CL5DBFile *file, Object **obj);
  270. static int _cl5CompareDBFile (Object *el1, const void *el2);
  271. static char* _cl5Replica2FileName (Object *replica);
  272. static char* _cl5MakeFileName (const char *replName, const char *replGen);
  273. static PRBool _cl5FileName2Replica (const char *fileName, Object **replica);
  274. static int _cl5ExportFile (PRFileDesc *prFile, Object *obj);
  275. static PRBool _cl5ReplicaInList (Object *replica, Object **replicas);
  276. /* data storage and retrieval */
  277. static int _cl5Entry2DBData (const CL5Entry *entry, char **data, PRUint32 *len);
  278. static int _cl5WriteOperation(const char *replName, const char *replGen,
  279. const slapi_operation_parameters *op, PRBool local);
  280. static int _cl5WriteOperationTxn(const char *replName, const char *replGen,
  281. const slapi_operation_parameters *op, PRBool local, void *txn);
  282. static int _cl5GetFirstEntry (Object *obj, CL5Entry *entry, void **iterator, DB_TXN *txnid);
  283. static int _cl5GetNextEntry (CL5Entry *entry, void *iterator);
  284. static int _cl5CurrentDeleteEntry (void *iterator);
  285. static PRBool _cl5IsValidIterator (const CL5Iterator *iterator);
  286. static int _cl5GetOperation (Object *replica, slapi_operation_parameters *op);
  287. static const char* _cl5OperationType2Str (int type);
  288. static int _cl5Str2OperationType (const char *str);
  289. static void _cl5WriteString (const char *str, char **buff);
  290. static void _cl5ReadString (char **str, char **buff);
  291. static void _cl5WriteMods (LDAPMod **mods, char **buff);
  292. static int _cl5WriteMod (LDAPMod *mod, char **buff);
  293. static int _cl5ReadMods (LDAPMod ***mods, char **buff);
  294. static int _cl5ReadMod (Slapi_Mod *mod, char **buff);
  295. static int _cl5GetModsSize (LDAPMod **mods);
  296. static int _cl5GetModSize (LDAPMod *mod);
  297. static void _cl5ReadBerval (struct berval *bv, char** buff);
  298. static void _cl5WriteBerval (struct berval *bv, char** buff);
  299. static int _cl5ReadBervals (struct berval ***bv, char** buff, unsigned int size);
  300. static int _cl5WriteBervals (struct berval **bv, char** buff, unsigned int *size);
  301. /* replay iteration */
  302. #ifdef FOR_DEBUGGING
  303. static PRBool _cl5ValidReplayIterator (const CL5ReplayIterator *iterator);
  304. #endif
  305. static int _cl5PositionCursorForReplay (ReplicaId consumerRID, const RUV *consumerRuv,
  306. Object *replica, Object *fileObject, CL5ReplayIterator **iterator, int *continue_on_missing);
  307. static int _cl5CheckMissingCSN (const CSN *minCsn, const RUV *supplierRUV, CL5DBFile *file);
  308. /* changelog trimming */
  309. static int _cl5TrimInit ();
  310. static void _cl5TrimCleanup ();
  311. static int _cl5TrimMain (void *param);
  312. static void _cl5DoTrimming ();
  313. static PRBool _cl5CanTrim (time_t time, long *numToTrim);
  314. static void _cl5TrimFile (Object *obj, long *numToTrim);
  315. static void _cl5PurgeRID(Object *obj, ReplicaId cleaned_rid);
  316. static int _cl5PurgeGetFirstEntry (Object *obj, CL5Entry *entry, void **iterator, DB_TXN *txnid, int rid, DBT *key);
  317. static int _cl5PurgeGetNextEntry (CL5Entry *entry, void *iterator, DBT *key);
  318. static int _cl5ReadRUV (const char *replGen, Object *obj, PRBool purge);
  319. static int _cl5WriteRUV (CL5DBFile *file, PRBool purge);
  320. static int _cl5ConstructRUV (const char *replGen, Object *obj, PRBool purge);
  321. static int _cl5UpdateRUV (Object *obj, CSN *csn, PRBool newReplica, PRBool purge);
  322. static int _cl5GetRUV2Purge2 (Object *fileObj, RUV **ruv);
  323. void trigger_cl_purging_thread(void *rid);
  324. /* bakup/recovery, import/export */
  325. static int _cl5LDIF2Operation (char *ldifEntry, slapi_operation_parameters *op,
  326. char **replGen);
  327. static int _cl5Operation2LDIF (const slapi_operation_parameters *op, const char *replGen,
  328. char **ldifEntry, PRInt32 *lenLDIF);
  329. /* entry count */
  330. static int _cl5GetEntryCount (CL5DBFile *file);
  331. static int _cl5WriteEntryCount (CL5DBFile *file);
  332. /* misc */
  333. static char* _cl5GetHelperEntryKey (int type, char *csnStr);
  334. static Object* _cl5GetReplica (const slapi_operation_parameters *op, const char* replGen);
  335. static int _cl5FileEndsWith(const char *filename, const char *ext);
  336. static PRLock *cl5_diskfull_lock = NULL;
  337. static int cl5_diskfull_flag = 0;
  338. static void cl5_set_diskfull();
  339. static void cl5_set_no_diskfull();
  340. /***** Module APIs *****/
  341. /* Name: cl5Init
  342. Description: initializes changelog module; must be called by a single thread
  343. before any other changelog function.
  344. Parameters: none
  345. Return: CL5_SUCCESS if function is successful;
  346. CL5_SYSTEM_ERROR error if NSPR call fails.
  347. */
  348. int cl5Init ()
  349. {
  350. s_cl5Desc.stLock = slapi_new_rwlock();
  351. if (s_cl5Desc.stLock == NULL)
  352. {
  353. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  354. "cl5Init: failed to create state lock; NSPR error - %d\n",
  355. PR_GetError ());
  356. return CL5_SYSTEM_ERROR;
  357. }
  358. if ((s_cl5Desc.clLock = PR_NewLock()) == NULL)
  359. {
  360. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  361. "cl5Init: failed to create on close lock; NSPR error - %d\n",
  362. PR_GetError ());
  363. return CL5_SYSTEM_ERROR;
  364. }
  365. if ((s_cl5Desc.clCvar = PR_NewCondVar(s_cl5Desc.clLock)) == NULL)
  366. {
  367. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  368. "cl5Init: failed to create on close cvar; NSPR error - %d\n",
  369. PR_GetError ());
  370. return CL5_SYSTEM_ERROR;
  371. }
  372. if (( clcache_init (&s_cl5Desc.dbEnv) != 0 )) {
  373. return CL5_SYSTEM_ERROR;
  374. }
  375. s_cl5Desc.dbState = CL5_STATE_CLOSED;
  376. s_cl5Desc.fatalError = PR_FALSE;
  377. s_cl5Desc.dbRmOnClose = PR_FALSE;
  378. s_cl5Desc.threadCount = 0;
  379. if (NULL == cl5_diskfull_lock)
  380. {
  381. cl5_diskfull_lock = PR_NewLock ();
  382. }
  383. return CL5_SUCCESS;
  384. }
  385. /* Name: cl5Cleanup
  386. Description: performs cleanup of the changelog module; must be called by a single
  387. thread; it closes changelog if it is still open.
  388. Parameters: none
  389. Return: none
  390. */
  391. void cl5Cleanup ()
  392. {
  393. /* close db if it is still open */
  394. if (s_cl5Desc.dbState == CL5_STATE_OPEN)
  395. {
  396. cl5Close ();
  397. }
  398. if (s_cl5Desc.stLock)
  399. slapi_destroy_rwlock (s_cl5Desc.stLock);
  400. s_cl5Desc.stLock = NULL;
  401. if (cl5_diskfull_lock)
  402. {
  403. PR_DestroyLock (cl5_diskfull_lock);
  404. cl5_diskfull_lock = NULL;
  405. }
  406. memset (&s_cl5Desc, 0, sizeof (s_cl5Desc));
  407. }
  408. /* Name: cl5Open
  409. Description: opens changelog; must be called after changelog is
  410. initialized using cl5Init. It is thread safe and the second
  411. call is ignored.
  412. Parameters: dir - changelog dir
  413. config - db configuration parameters; currently not used
  414. Return: CL5_SUCCESS if successfull;
  415. CL5_BAD_DATA if invalid directory is passed;
  416. CL5_BAD_STATE if changelog is not initialized;
  417. CL5_BAD_DBVERSION if dbversion file is missing or has unexpected data
  418. CL5_SYSTEM_ERROR if NSPR error occured (during db directory creation);
  419. CL5_MEMORY_ERROR if memory allocation fails;
  420. CL5_DB_ERROR if db initialization fails.
  421. */
  422. int cl5Open (const char *dir, const CL5DBConfig *config)
  423. {
  424. int rc;
  425. if (dir == NULL)
  426. {
  427. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "cl5Open: null directory\n");
  428. return CL5_BAD_DATA;
  429. }
  430. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  431. {
  432. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  433. "cl5Open: changelog is not initialized\n");
  434. return CL5_BAD_STATE;
  435. }
  436. /* prevent state from changing */
  437. slapi_rwlock_wrlock (s_cl5Desc.stLock);
  438. /* already open - ignore */
  439. if (s_cl5Desc.dbState == CL5_STATE_OPEN)
  440. {
  441. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  442. "cl5Open: changelog already opened; request ignored\n");
  443. rc = CL5_SUCCESS;
  444. goto done;
  445. }
  446. else if (s_cl5Desc.dbState != CL5_STATE_CLOSED)
  447. {
  448. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  449. "cl5Open: invalid state - %d\n", s_cl5Desc.dbState);
  450. rc = CL5_BAD_STATE;
  451. goto done;
  452. }
  453. rc = _cl5Open (dir, config, CL5_OPEN_NORMAL);
  454. if (rc != CL5_SUCCESS)
  455. {
  456. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  457. "cl5Open: failed to open changelog\n");
  458. goto done;
  459. }
  460. /* dispatch global threads like deadlock detection, trimming, etc */
  461. rc = _cl5DispatchDBThreads ();
  462. if (rc != CL5_SUCCESS)
  463. {
  464. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  465. "cl5Open: failed to start database monitoring threads\n");
  466. _cl5Close ();
  467. }
  468. else
  469. {
  470. s_cl5Desc.dbState = CL5_STATE_OPEN;
  471. clcache_set_config();
  472. /* Set the cl encryption algorithm (if configured) */
  473. rc = clcrypt_init(config, &s_cl5Desc.clcrypt_handle);
  474. }
  475. done:
  476. slapi_rwlock_unlock (s_cl5Desc.stLock);
  477. return rc;
  478. }
  479. /* Name: cl5Close
  480. Description: closes changelog; waits until all threads are done using changelog;
  481. call is ignored if changelog is already closed.
  482. Parameters: none
  483. Return: CL5_SUCCESS if successful;
  484. CL5_BAD_STATE if db is not in the open or closed state;
  485. CL5_SYSTEM_ERROR if NSPR call fails;
  486. CL5_DB_ERROR if db shutdown fails
  487. */
  488. int cl5Close ()
  489. {
  490. int rc = CL5_SUCCESS;
  491. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  492. {
  493. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  494. "cl5Close: changelog is not initialized\n");
  495. return CL5_BAD_STATE;
  496. }
  497. slapi_rwlock_wrlock (s_cl5Desc.stLock);
  498. /* already closed - ignore */
  499. if (s_cl5Desc.dbState == CL5_STATE_CLOSED)
  500. {
  501. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  502. "cl5Close: changelog closed; request ignored\n");
  503. slapi_rwlock_unlock (s_cl5Desc.stLock);
  504. return CL5_SUCCESS;
  505. }
  506. else if (s_cl5Desc.dbState != CL5_STATE_OPEN)
  507. {
  508. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  509. "cl5Close: invalid state - %d\n", s_cl5Desc.dbState);
  510. slapi_rwlock_unlock (s_cl5Desc.stLock);
  511. return CL5_BAD_STATE;
  512. }
  513. /* signal changelog closing to all threads */
  514. s_cl5Desc.dbState = CL5_STATE_CLOSING;
  515. PR_Lock(s_cl5Desc.clLock);
  516. PR_NotifyCondVar(s_cl5Desc.clCvar);
  517. PR_Unlock(s_cl5Desc.clLock);
  518. _cl5Close ();
  519. s_cl5Desc.dbState = CL5_STATE_CLOSED;
  520. slapi_rwlock_unlock (s_cl5Desc.stLock);
  521. return rc;
  522. }
  523. /* Name: cl5Delete
  524. Description: removes changelog; changelog must be in the closed state.
  525. Parameters: dir - changelog directory
  526. Return: CL5_SUCCESS if successful;
  527. CL5_BAD_STATE if the changelog is not in closed state;
  528. CL5_BAD_DATA if invalid directory supplied
  529. CL5_SYSTEM_ERROR if NSPR call fails
  530. */
  531. int cl5Delete (const char *dir)
  532. {
  533. int rc;
  534. if (dir == NULL)
  535. {
  536. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl, "cl5Delete: null directory\n");
  537. return CL5_BAD_DATA;
  538. }
  539. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  540. {
  541. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  542. "cl5Delete: changelog is not initialized\n");
  543. return CL5_BAD_STATE;
  544. }
  545. slapi_rwlock_wrlock (s_cl5Desc.stLock);
  546. if (s_cl5Desc.dbState != CL5_STATE_CLOSED)
  547. {
  548. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  549. "cl5Delete: invalid state - %d\n", s_cl5Desc.dbState);
  550. slapi_rwlock_unlock (s_cl5Desc.stLock);
  551. return CL5_BAD_STATE;
  552. }
  553. rc = _cl5Delete (dir, PR_TRUE /* remove changelog dir */);
  554. if (rc != CL5_SUCCESS)
  555. {
  556. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  557. "cl5Delete: failed to remove changelog\n");
  558. }
  559. slapi_rwlock_unlock (s_cl5Desc.stLock);
  560. return rc;
  561. }
  562. /* Name: cl5DeleteDBSync
  563. Description: The same as cl5DeleteDB except the function does not return
  564. until the file is removed.
  565. */
  566. int cl5DeleteDBSync (Object *replica)
  567. {
  568. Object *obj;
  569. int rc;
  570. CL5DBFile *file;
  571. if (replica == NULL)
  572. {
  573. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  574. "cl5DeleteDBSync: invalid database id\n");
  575. return CL5_BAD_DATA;
  576. }
  577. /* changelog is not initialized */
  578. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  579. {
  580. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5DeleteDBSync: "
  581. "changelog is not initialized\n");
  582. return CL5_BAD_STATE;
  583. }
  584. /* make sure that changelog stays open while operation is in progress */
  585. rc = _cl5AddThread ();
  586. if (rc != CL5_SUCCESS)
  587. return rc;
  588. rc = _cl5GetDBFile (replica, &obj);
  589. if (rc == CL5_SUCCESS)
  590. {
  591. char *filename = NULL;
  592. file = (CL5DBFile*)object_get_data (obj);
  593. PR_ASSERT (file);
  594. /* file->name is freed in _cl5DBDeleteFile */
  595. filename = slapi_ch_strdup(file->name);
  596. _cl5DBDeleteFile (obj);
  597. /* wait until the file is gone */
  598. while (PR_Access (filename, PR_ACCESS_EXISTS) == PR_SUCCESS)
  599. {
  600. DS_Sleep (PR_MillisecondsToInterval(100));
  601. }
  602. slapi_ch_free_string(&filename);
  603. }
  604. else
  605. {
  606. Replica *r = (Replica*)object_get_data (replica);
  607. PR_ASSERT (r);
  608. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5DeleteDBSync: "
  609. "file for replica at (%s) not found\n",
  610. slapi_sdn_get_dn (replica_get_root (r)));
  611. }
  612. _cl5RemoveThread ();
  613. return rc;
  614. }
  615. /* Name: cl5GetUpperBoundRUV
  616. Description: retrieves vector for that represents the upper bound of the changes for a replica.
  617. Parameters: r - replica for which the purge vector is requested
  618. ruv - contains a copy of the purge ruv if function is successful;
  619. unchanged otherwise. It is responsibility of the caller to free
  620. the ruv when it is no longer is in use
  621. Return: CL5_SUCCESS if function is successful
  622. CL5_BAD_STATE if the changelog is not initialized;
  623. CL5_BAD_DATA - if NULL id is supplied
  624. CL5_NOTFOUND, if changelog file for replica is not found
  625. */
  626. int cl5GetUpperBoundRUV (Replica *r, RUV **ruv)
  627. {
  628. int rc;
  629. Object *r_obj, *file_obj;
  630. CL5DBFile *file;
  631. if (r == NULL || ruv == NULL)
  632. {
  633. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  634. "cl5GetUpperBoundRUV: invalid parameters\n");
  635. return CL5_BAD_DATA;
  636. }
  637. /* changelog is not initialized */
  638. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  639. {
  640. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5GetUpperBoundRUV: "
  641. "changelog is not initialized\n");
  642. return CL5_BAD_STATE;
  643. }
  644. /* make sure that changelog stays open while operation is in progress */
  645. rc = _cl5AddThread ();
  646. if (rc != CL5_SUCCESS)
  647. return rc;
  648. /* create a temporary replica object because of the interface we have */
  649. r_obj = object_new (r, NULL);
  650. rc = _cl5GetDBFile (r_obj, &file_obj);
  651. if (rc == CL5_SUCCESS)
  652. {
  653. file = (CL5DBFile*)object_get_data (file_obj);
  654. PR_ASSERT (file && file->maxRUV);
  655. *ruv = ruv_dup (file->maxRUV);
  656. object_release (file_obj);
  657. }
  658. else
  659. {
  660. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5GetUpperBoundRUV: "
  661. "could not find DB object for replica\n");
  662. }
  663. object_release (r_obj);
  664. _cl5RemoveThread ();
  665. return rc;
  666. }
  667. /* Name: cl5ExportLDIF
  668. Description: dumps changelog to an LDIF file; changelog can be open or closed.
  669. Parameters: clDir - changelog dir
  670. ldifFile - full path to ldif file to write
  671. replicas - optional list of replicas whose changes should be exported;
  672. if the list is NULL, entire changelog is exported.
  673. Return: CL5_SUCCESS if function is successfull;
  674. CL5_BAD_DATA if invalid parameter is passed;
  675. CL5_BAD_STATE if changelog is not initialized;
  676. CL5_DB_ERROR if db api fails;
  677. CL5_SYSTEM_ERROR if NSPR call fails;
  678. CL5_MEMORY_ERROR if memory allocation fials.
  679. */
  680. int cl5ExportLDIF (const char *ldifFile, Object **replicas)
  681. {
  682. int i;
  683. int rc;
  684. PRFileDesc *prFile = NULL;
  685. Object *obj;
  686. if (ldifFile == NULL)
  687. {
  688. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  689. "cl5ExportLDIF: null ldif file name\n");
  690. return CL5_BAD_DATA;
  691. }
  692. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  693. {
  694. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  695. "cl5ExportLDIF: changelog is not initialized\n");
  696. return CL5_BAD_STATE;
  697. }
  698. /* make sure that changelog is open while operation is in progress */
  699. rc = _cl5AddThread ();
  700. if (rc != CL5_SUCCESS)
  701. return rc;
  702. prFile = PR_Open (ldifFile, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600);
  703. if (prFile == NULL)
  704. {
  705. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  706. "cl5ExportLDIF: failed to open (%s) file; NSPR error - %d\n",
  707. ldifFile, PR_GetError ());
  708. rc = CL5_SYSTEM_ERROR;
  709. goto done;
  710. }
  711. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  712. "cl5ExportLDIF: starting changelog export to (%s) ...\n", ldifFile);
  713. if (replicas) /* export only selected files */
  714. {
  715. for (i = 0; replicas[i]; i++)
  716. {
  717. rc = _cl5GetDBFile (replicas[i], &obj);
  718. if (rc == CL5_SUCCESS)
  719. {
  720. rc = _cl5ExportFile (prFile, obj);
  721. object_release (obj);
  722. }
  723. else
  724. {
  725. Replica *r = (Replica*)object_get_data (replicas[i]);
  726. PR_ASSERT (r);
  727. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "cl5ExportLDIF: "
  728. "failed to locate changelog file for replica at (%s)\n",
  729. slapi_sdn_get_dn (replica_get_root (r)));
  730. }
  731. }
  732. }
  733. else /* export all files */
  734. {
  735. for (obj = objset_first_obj(s_cl5Desc.dbFiles); obj;
  736. obj = objset_next_obj(s_cl5Desc.dbFiles, obj))
  737. {
  738. rc = _cl5ExportFile (prFile, obj);
  739. }
  740. }
  741. rc = CL5_SUCCESS;
  742. done:;
  743. _cl5RemoveThread ();
  744. if (rc == CL5_SUCCESS)
  745. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  746. "cl5ExportLDIF: changelog export is finished.\n");
  747. if (prFile)
  748. PR_Close (prFile);
  749. return rc;
  750. }
  751. /* Name: cl5ImportLDIF
  752. Description: imports ldif file into changelog; changelog must be in the closed state
  753. Parameters: clDir - changelog dir
  754. ldifFile - absolute path to the ldif file to import
  755. replicas - list of replicas whose data should be imported.
  756. Return: CL5_SUCCESS if function is successfull;
  757. CL5_BAD_DATA if invalid parameter is passed;
  758. CL5_BAD_STATE if changelog is open or not inititalized;
  759. CL5_DB_ERROR if db api fails;
  760. CL5_SYSTEM_ERROR if NSPR call fails;
  761. CL5_MEMORY_ERROR if memory allocation fials.
  762. */
  763. int
  764. cl5ImportLDIF (const char *clDir, const char *ldifFile, Object **replicas)
  765. {
  766. #if defined(USE_OPENLDAP)
  767. LDIFFP *file = NULL;
  768. int buflen;
  769. ldif_record_lineno_t lineno = 0;
  770. #else
  771. FILE *file = NULL;
  772. int lineno = 0;
  773. #endif
  774. int rc;
  775. char *buff = NULL;
  776. slapi_operation_parameters op;
  777. Object *prim_replica_obj = NULL;
  778. Object *replica_obj = NULL;
  779. Object *file_obj = NULL;
  780. Replica *prim_replica = NULL;
  781. char *replGen = NULL;
  782. CL5DBFile *dbfile = NULL;
  783. struct berval **purgevals = NULL;
  784. struct berval **maxvals = NULL;
  785. int purgeidx = 0;
  786. int maxidx = 0;
  787. int maxpurgesz = 0;
  788. int maxmaxsz = 0;
  789. int entryCount = 0;
  790. /* validate params */
  791. if (ldifFile == NULL)
  792. {
  793. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  794. "cl5ImportLDIF: null ldif file name\n");
  795. return CL5_BAD_DATA;
  796. }
  797. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  798. {
  799. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  800. "cl5ImportLDIF: changelog is not initialized\n");
  801. return CL5_BAD_STATE;
  802. }
  803. if (replicas == NULL)
  804. {
  805. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  806. "cl5ImportLDIF: null list of replicas\n");
  807. return CL5_BAD_DATA;
  808. }
  809. prim_replica_obj = replicas[0];
  810. if (NULL == prim_replica_obj)
  811. {
  812. /* Never happens for now. (see replica_execute_ldif2cl_task) */
  813. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  814. "cl5ImportLDIF: empty replica list\n");
  815. return CL5_BAD_DATA;
  816. }
  817. prim_replica = (Replica*)object_get_data(prim_replica_obj);
  818. /* make sure that nobody change changelog state while import is in progress */
  819. slapi_rwlock_wrlock (s_cl5Desc.stLock);
  820. /* make sure changelog is closed */
  821. if (s_cl5Desc.dbState != CL5_STATE_CLOSED)
  822. {
  823. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  824. "cl5ImportLDIF: invalid state - %d \n", s_cl5Desc.dbState);
  825. slapi_rwlock_unlock (s_cl5Desc.stLock);
  826. return CL5_BAD_STATE;
  827. }
  828. /* open LDIF file */
  829. #if defined(USE_OPENLDAP)
  830. file = ldif_open (ldifFile, "r");
  831. #else
  832. file = fopen (ldifFile, "r"); /* XXXggood Does fopen reliably work if > 255 files open? */
  833. #endif
  834. if (file == NULL)
  835. {
  836. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  837. "cl5ImportLDIF: failed to open (%s) ldif file; system error - %d\n",
  838. ldifFile, errno);
  839. rc = CL5_SYSTEM_ERROR;
  840. goto done;
  841. }
  842. /* remove changelog */
  843. rc = _cl5Delete (clDir, PR_FALSE);
  844. if (rc != CL5_SUCCESS)
  845. {
  846. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  847. "cl5ImportLDIF: failed to remove changelog\n");
  848. goto done;
  849. }
  850. /* open changelog */
  851. rc = _cl5Open (clDir, NULL, CL5_OPEN_LDIF2CL);
  852. if (rc != CL5_SUCCESS)
  853. {
  854. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  855. "cl5ImportLDIF: failed to open changelog\n");
  856. goto done;
  857. }
  858. s_cl5Desc.dbState = CL5_STATE_OPEN; /* force to change the state */
  859. /* read entries and write them to changelog */
  860. #if defined(USE_OPENLDAP)
  861. while (ldif_read_record( file, &lineno, &buff, &buflen ))
  862. #else
  863. while ((buff = ldif_get_entry( file, &lineno )) != NULL)
  864. #endif
  865. {
  866. rc = _cl5LDIF2Operation (buff, &op, &replGen);
  867. if (rc != CL5_SUCCESS)
  868. {
  869. /*
  870. * clpurgeruv: {replicageneration} 4d13a124000000010000
  871. * clpurgeruv: {replica 2 ldap://host:port}
  872. * clpurgeruv: {replica 1 ldap://host:port}
  873. * clmaxruv: {replicageneration} 4d13a124000000010000
  874. * clmaxruv: {replica 2} <mincsn> <maxcsn> <timestamp>
  875. * clmaxruv: {replica 1} <mincsn> <maxcsn> <timestamp>
  876. */
  877. char *line;
  878. char *next = buff;
  879. struct berval type, value;
  880. int freeval = 0;
  881. while ((line = ldif_getline(&next)) != NULL) {
  882. rc = slapi_ldif_parse_line(line, &type, &value, &freeval);
  883. /* ruv_dump (dbfile->purgeRUV, "clpurgeruv", prFile); */
  884. if (0 == strcasecmp (type.bv_val, "clpurgeruv")) {
  885. if (maxpurgesz < purgeidx + 2) {
  886. if (!maxpurgesz) {
  887. maxpurgesz = 4 * (purgeidx + 2);
  888. } else {
  889. maxpurgesz *= 2;
  890. }
  891. purgevals = (struct berval **)slapi_ch_realloc(
  892. (char *)purgevals,
  893. sizeof(struct berval *) * maxpurgesz);
  894. }
  895. purgevals[purgeidx++] = slapi_ch_bvdup(&value);
  896. purgevals[purgeidx] = NULL; /* make sure NULL terminated */
  897. }
  898. /* ruv_dump (dbfile->maxRUV, "clmaxruv", prFile); */
  899. else if (0 == strcasecmp (type.bv_val, "clmaxruv")) {
  900. if (maxmaxsz < maxidx + 2) {
  901. if (!maxmaxsz) {
  902. maxmaxsz = 4 * (maxidx + 2);
  903. } else {
  904. maxmaxsz *= 2;
  905. }
  906. maxvals = (struct berval **)slapi_ch_realloc(
  907. (char *)maxvals,
  908. sizeof(struct berval *) * maxmaxsz);
  909. }
  910. /* {replica #} min_csn csn [last_modified] */
  911. /* get rid of last_modified, if any */
  912. maxvals[maxidx++] = slapi_ch_bvdup(&value);
  913. maxvals[maxidx] = NULL; /* make sure NULL terminated */
  914. }
  915. if (freeval) {
  916. slapi_ch_free_string(&value.bv_val);
  917. }
  918. }
  919. slapi_ch_free_string(&buff);
  920. #if defined(USE_OPENLDAP)
  921. buflen = 0;
  922. #endif
  923. goto next;
  924. }
  925. slapi_ch_free_string(&buff);
  926. #if defined(USE_OPENLDAP)
  927. buflen = 0;
  928. #endif
  929. /* if we perform selective import, check if the operation should be wriiten to changelog */
  930. replica_obj = _cl5GetReplica (&op, replGen);
  931. if (replica_obj == NULL)
  932. {
  933. /*
  934. * changetype: delete
  935. * replgen: 4d13a124000000010000
  936. * csn: 4d23b909000000020000
  937. * nsuniqueid: 00000000-00000000-00000000-00000000
  938. * dn: cn=start iteration
  939. */
  940. rc = _cl5WriteOperation (replica_get_name(prim_replica),
  941. replGen, &op, 1);
  942. if (rc != CL5_SUCCESS)
  943. {
  944. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  945. "cl5ImportLDIF: "
  946. "failed to write operation to the changelog: "
  947. "type: %lu, dn: %s\n",
  948. op.operation_type, REPL_GET_DN(&op.target_address));
  949. slapi_ch_free_string(&replGen);
  950. operation_parameters_done (&op);
  951. goto done;
  952. }
  953. entryCount++;
  954. goto next;
  955. }
  956. if (!replicas || _cl5ReplicaInList (replica_obj, replicas))
  957. {
  958. /* write operation creates the file if it does not exist */
  959. rc = _cl5WriteOperation (replica_get_name ((Replica*)object_get_data(replica_obj)),
  960. replGen, &op, 1);
  961. if (rc != CL5_SUCCESS)
  962. {
  963. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  964. "cl5ImportLDIF: "
  965. "failed to write operation to the changelog: "
  966. "type: %lu, dn: %s\n",
  967. op.operation_type, REPL_GET_DN(&op.target_address));
  968. object_release (replica_obj);
  969. slapi_ch_free_string(&replGen);
  970. operation_parameters_done (&op);
  971. goto done;
  972. }
  973. entryCount++;
  974. }
  975. next:
  976. if (replica_obj) {
  977. object_release (replica_obj);
  978. }
  979. slapi_ch_free_string(&replGen);
  980. operation_parameters_done (&op);
  981. }
  982. /* Set RUVs and entry count */
  983. file_obj = objset_first_obj(s_cl5Desc.dbFiles);
  984. while (file_obj)
  985. {
  986. dbfile = (CL5DBFile *)object_get_data(file_obj);
  987. if (0 == strcasecmp(dbfile->replName, replica_get_name(prim_replica))) {
  988. break;
  989. }
  990. dbfile = NULL;
  991. file_obj = objset_next_obj(s_cl5Desc.dbFiles, file_obj);
  992. }
  993. if (dbfile) {
  994. if (purgeidx > 0) {
  995. ruv_destroy (&dbfile->purgeRUV);
  996. rc = ruv_init_from_bervals(purgevals, &dbfile->purgeRUV);
  997. }
  998. if (maxidx > 0) {
  999. ruv_destroy (&dbfile->maxRUV);
  1000. rc = ruv_init_from_bervals(maxvals, &dbfile->maxRUV);
  1001. }
  1002. dbfile->entryCount = entryCount;
  1003. }
  1004. if (file_obj) {
  1005. object_release (file_obj);
  1006. }
  1007. done:
  1008. for (purgeidx = 0; purgevals && purgevals[purgeidx]; purgeidx++) {
  1009. slapi_ch_bvfree(&purgevals[purgeidx]);
  1010. }
  1011. slapi_ch_free((void **)&purgevals);
  1012. for (maxidx = 0; maxvals && maxvals[maxidx]; maxidx++) {
  1013. slapi_ch_bvfree(&maxvals[maxidx]);
  1014. }
  1015. slapi_ch_free((void **)&maxvals);
  1016. if (file) {
  1017. #if defined(USE_OPENLDAP)
  1018. ldif_close(file);
  1019. #else
  1020. fclose(file);
  1021. #endif
  1022. }
  1023. if (CL5_STATE_OPEN == s_cl5Desc.dbState) {
  1024. _cl5Close ();
  1025. s_cl5Desc.dbState = CL5_STATE_CLOSED; /* force to change the state */
  1026. }
  1027. slapi_rwlock_unlock (s_cl5Desc.stLock);
  1028. return rc;
  1029. }
  1030. /* Name: cl5GetState
  1031. Description: returns database state
  1032. Parameters: none
  1033. Return: changelog state
  1034. */
  1035. int cl5GetState ()
  1036. {
  1037. return s_cl5Desc.dbState;
  1038. }
  1039. /* Name: cl5ConfigTrimming
  1040. Description: sets changelog trimming parameters; changelog must be open.
  1041. Parameters: maxEntries - maximum number of entries in the chnagelog (in all files);
  1042. maxAge - maximum entry age;
  1043. Return: CL5_SUCCESS if successful;
  1044. CL5_BAD_STATE if changelog is not open
  1045. */
  1046. int cl5ConfigTrimming (int maxEntries, const char *maxAge)
  1047. {
  1048. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1049. {
  1050. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1051. "cl5ConfigTrimming: changelog is not initialized\n");
  1052. return CL5_BAD_STATE;
  1053. }
  1054. /* make sure changelog is not closed while trimming configuration
  1055. is updated.*/
  1056. if (CL5_SUCCESS != _cl5AddThread ()) {
  1057. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1058. "cl5ConfigTrimming: could not start changelog trimming thread\n");
  1059. return CL5_BAD_STATE;
  1060. }
  1061. PR_Lock (s_cl5Desc.dbTrim.lock);
  1062. if (maxAge)
  1063. {
  1064. /* don't ignore this argument */
  1065. if (strcmp (maxAge, CL5_STR_IGNORE) != 0)
  1066. {
  1067. s_cl5Desc.dbTrim.maxAge = age_str2time (maxAge);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. /* unlimited */
  1073. s_cl5Desc.dbTrim.maxAge = 0;
  1074. }
  1075. if (maxEntries != CL5_NUM_IGNORE)
  1076. {
  1077. s_cl5Desc.dbTrim.maxEntries = maxEntries;
  1078. }
  1079. PR_Unlock (s_cl5Desc.dbTrim.lock);
  1080. _cl5RemoveThread ();
  1081. return CL5_SUCCESS;
  1082. }
  1083. /* Name: cl5GetOperation
  1084. Description: retireves operation specified by its csn and databaseid
  1085. Parameters: op - must contain csn and databaseid; the rest of data is
  1086. filled if function is successfull
  1087. Return: CL5_SUCCESS if function is successfull;
  1088. CL5_BAD_DATA if invalid op is passed;
  1089. CL5_BAD_STATE if db has not been initialized;
  1090. CL5_NOTFOUND if entry was not found;
  1091. CL5_DB_ERROR if any other db error occured;
  1092. CL5_BADFORMAT if db data format does not match entry format.
  1093. */
  1094. int cl5GetOperation (Object *replica, slapi_operation_parameters *op)
  1095. {
  1096. int rc;
  1097. char *agmt_name;
  1098. agmt_name = get_thread_private_agmtname();
  1099. if (replica == NULL)
  1100. {
  1101. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5GetOperation: NULL replica\n");
  1102. return CL5_BAD_DATA;
  1103. }
  1104. if (op == NULL)
  1105. {
  1106. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "cl5GetOperation: NULL operation\n");
  1107. return CL5_BAD_DATA;
  1108. }
  1109. if (op->csn == NULL)
  1110. {
  1111. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "%s: cl5GetOperation: operation contains no CSN\n", agmt_name);
  1112. return CL5_BAD_DATA;
  1113. }
  1114. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1115. {
  1116. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1117. "%s: cl5GetOperation: changelog is not initialized\n", agmt_name);
  1118. return CL5_BAD_STATE;
  1119. }
  1120. /* make sure that changelog is open while operation is in progress */
  1121. rc = _cl5AddThread ();
  1122. if (rc != CL5_SUCCESS)
  1123. return rc;
  1124. rc = _cl5GetOperation (replica, op);
  1125. _cl5RemoveThread ();
  1126. return rc;
  1127. }
  1128. /* Name: cl5GetFirstOperation
  1129. Description: retrieves first operation for a particular database
  1130. replica - replica for which the operation should be retrieved.
  1131. Parameters: op - buffer to store the operation;
  1132. iterator - to be passed to the call to cl5GetNextOperation
  1133. Return: CL5_SUCCESS, if successful
  1134. CL5_BADDATA, if operation is NULL
  1135. CL5_BAD_STATE, if changelog is not open
  1136. CL5_DB_ERROR, if db call fails
  1137. */
  1138. int cl5GetFirstOperation (Object *replica, slapi_operation_parameters *op, void **iterator)
  1139. {
  1140. int rc;
  1141. CL5Entry entry;
  1142. Object *obj;
  1143. char *agmt_name;
  1144. if (replica == NULL || op == NULL || iterator == NULL)
  1145. {
  1146. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1147. "cl5GetFirstOperation: invalid argument\n");
  1148. return CL5_BAD_DATA;
  1149. }
  1150. *iterator = NULL;
  1151. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1152. {
  1153. agmt_name = get_thread_private_agmtname();
  1154. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1155. "%s: cl5GetFirstOperation: changelog is not initialized\n", agmt_name);
  1156. return CL5_BAD_STATE;
  1157. }
  1158. /* make sure that changelog stays open while operation is in progress */
  1159. rc = _cl5AddThread ();
  1160. if (rc != CL5_SUCCESS)
  1161. return rc;
  1162. rc = _cl5GetDBFile (replica, &obj);
  1163. if (rc != CL5_SUCCESS)
  1164. {
  1165. _cl5RemoveThread ();
  1166. return rc;
  1167. }
  1168. entry.op = op;
  1169. /* Callers of this function should cl5_operation_parameters_done(op) */
  1170. rc = _cl5GetFirstEntry (obj, &entry, iterator, NULL);
  1171. object_release (obj);
  1172. _cl5RemoveThread ();
  1173. return rc;
  1174. }
  1175. /* Name: cl5GetNextOperation
  1176. Description: retrieves the next op from the changelog as defined by the iterator;
  1177. changelog must be open.
  1178. Parameters: op - returned operation, if function is successful
  1179. iterator - in: identifies op to retrieve; out: identifies next op
  1180. Return: CL5_SUCCESS, if successful
  1181. CL5_BADDATA, if op is NULL
  1182. CL5_BAD_STATE, if changelog is not open
  1183. CL5_NOTFOUND, empty changelog
  1184. CL5_DB_ERROR, if db call fails
  1185. */
  1186. int cl5GetNextOperation (slapi_operation_parameters *op, void *iterator)
  1187. {
  1188. CL5Entry entry;
  1189. if (op == NULL || iterator == NULL || !_cl5IsValidIterator (iterator))
  1190. {
  1191. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1192. "cl5GetNextOperation: invalid argument\n");
  1193. return CL5_BAD_DATA;
  1194. }
  1195. if (s_cl5Desc.dbState != CL5_STATE_OPEN)
  1196. {
  1197. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1198. "cl5GetNextOperation: changelog is not open\n");
  1199. return CL5_BAD_STATE;
  1200. }
  1201. /* we don't need to increment thread count since cl5GetFirstOperation
  1202. locked the file through which we are iterating */
  1203. entry.op = op;
  1204. /* Callers of this function should cl5_operation_parameters_done(op) */
  1205. return _cl5GetNextEntry (&entry, iterator);
  1206. }
  1207. /* Name: cl5DestroyIterator
  1208. Description: destroys iterator once iteration through changelog is done
  1209. Parameters: iterator - iterator to destroy
  1210. Return: none
  1211. */
  1212. void cl5DestroyIterator (void *iterator)
  1213. {
  1214. CL5Iterator *it = (CL5Iterator*)iterator;
  1215. if (it == NULL)
  1216. return;
  1217. /* close cursor */
  1218. if (it->cursor)
  1219. it->cursor->c_close (it->cursor);
  1220. if (it->file)
  1221. object_release (it->file);
  1222. slapi_ch_free ((void**)&it);
  1223. }
  1224. /* Name: cl5WriteOperationTxn
  1225. Description: writes operation to changelog
  1226. Parameters: replName - name of the replica to which operation applies
  1227. replGen - replica generation for the operation
  1228. !!!Note that we pass name and generation rather than
  1229. replica object since generation can change while operation
  1230. is in progress (if the data is reloaded). !!!
  1231. op - operation to write
  1232. local - this is a non-replicated operation
  1233. txn - the transaction containing this operation
  1234. Return: CL5_SUCCESS if function is successfull;
  1235. CL5_BAD_DATA if invalid op is passed;
  1236. CL5_BAD_STATE if db has not been initialized;
  1237. CL5_MEMORY_ERROR if memory allocation failed;
  1238. CL5_DB_ERROR if any other db error occured;
  1239. */
  1240. int cl5WriteOperationTxn(const char *replName, const char *replGen,
  1241. const slapi_operation_parameters *op, PRBool local, void *txn)
  1242. {
  1243. int rc;
  1244. if (op == NULL)
  1245. {
  1246. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1247. "cl5WriteOperation: NULL operation passed\n");
  1248. return CL5_BAD_DATA;
  1249. }
  1250. if (!IsValidOperation (op))
  1251. {
  1252. return CL5_BAD_DATA;
  1253. }
  1254. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1255. {
  1256. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1257. "cl5WriteOperation: changelog is not initialized\n");
  1258. return CL5_BAD_STATE;
  1259. }
  1260. /* make sure that changelog is open while operation is in progress */
  1261. rc = _cl5AddThread ();
  1262. if (rc != CL5_SUCCESS)
  1263. return rc;
  1264. rc = _cl5WriteOperationTxn(replName, replGen, op, local, txn);
  1265. /* update the upper bound ruv vector */
  1266. if (rc == CL5_SUCCESS)
  1267. {
  1268. Object *file_obj = NULL;
  1269. if ( _cl5GetDBFileByReplicaName (replName, replGen, &file_obj) == CL5_SUCCESS) {
  1270. rc = _cl5UpdateRUV (file_obj, op->csn, PR_FALSE, PR_FALSE);
  1271. object_release (file_obj);
  1272. }
  1273. }
  1274. _cl5RemoveThread ();
  1275. return rc;
  1276. }
  1277. /* Name: cl5WriteOperation
  1278. Description: writes operation to changelog
  1279. Parameters: replName - name of the replica to which operation applies
  1280. replGen - replica generation for the operation
  1281. !!!Note that we pass name and generation rather than
  1282. replica object since generation can change while operation
  1283. is in progress (if the data is reloaded). !!!
  1284. op - operation to write
  1285. local - this is a non-replicated operation
  1286. Return: CL5_SUCCESS if function is successfull;
  1287. CL5_BAD_DATA if invalid op is passed;
  1288. CL5_BAD_STATE if db has not been initialized;
  1289. CL5_MEMORY_ERROR if memory allocation failed;
  1290. CL5_DB_ERROR if any other db error occured;
  1291. */
  1292. int cl5WriteOperation(const char *replName, const char *replGen,
  1293. const slapi_operation_parameters *op, PRBool local)
  1294. {
  1295. return cl5WriteOperationTxn(replName, replGen, op, local, NULL);
  1296. }
  1297. /* Name: cl5CreateReplayIterator
  1298. Description: creates an iterator that allows to retrieve changes that should
  1299. to be sent to the consumer identified by ruv. The iteration is performed by
  1300. repeated calls to cl5GetNextOperationToReplay.
  1301. Parameters: replica - replica whose data we wish to iterate;
  1302. ruv - consumer ruv;
  1303. iterator - iterator to be passed to cl5GetNextOperationToReplay call
  1304. Return: CL5_SUCCESS, if function is successful;
  1305. CL5_MISSING_DATA, if data that should be in the changelog is missing
  1306. CL5_PURGED_DATA, if some data that consumer needs has been purged.
  1307. Note that the iterator can be non null if the supplier contains
  1308. some data that needs to be sent to the consumer
  1309. CL5_NOTFOUND if the consumer is up to data with respect to the supplier
  1310. CL5_BAD_DATA if invalid parameter is passed;
  1311. CL5_BAD_STATE if db has not been open;
  1312. CL5_DB_ERROR if any other db error occurred;
  1313. CL5_MEMORY_ERROR if memory allocation fails.
  1314. Algorithm: Build a list of csns from consumer's and supplier's ruv. For each element
  1315. of the consumer's ruv put max csn into the csn list. For each element
  1316. of the supplier's ruv not in the consumer's ruv put min csn from the
  1317. supplier's ruv into the list. The list contains, for each known replica,
  1318. the starting point for changes to be sent to the consumer.
  1319. Sort the list in ascending order.
  1320. Build a hash which contains, for each known replica, whether the
  1321. supplier can bring the consumer up to data with respect to that replica.
  1322. The hash is used to decide whether a change can be sent to the consumer
  1323. Find the replica with the smallest csn in the list for which
  1324. we can bring the consumer up to date.
  1325. Position the db cursor on the change entry that corresponds to this csn.
  1326. Hash entries are created for each replica traversed so far. sendChanges
  1327. flag is set to FALSE for all replicas except the last traversed.
  1328. */
  1329. int cl5CreateReplayIteratorEx (Private_Repl_Protocol *prp, const RUV *consumerRuv,
  1330. CL5ReplayIterator **iterator, ReplicaId consumerRID )
  1331. {
  1332. int rc;
  1333. Object *replica;
  1334. Object *obj = NULL;
  1335. replica = prp->replica_object;
  1336. if (replica == NULL || consumerRuv == NULL || iterator == NULL)
  1337. {
  1338. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1339. "cl5CreateReplayIteratorEx: invalid parameter\n");
  1340. return CL5_BAD_DATA;
  1341. }
  1342. *iterator = NULL;
  1343. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1344. {
  1345. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1346. "cl5CreateReplayIteratorEx: changelog is not initialized\n");
  1347. return CL5_BAD_STATE;
  1348. }
  1349. /* make sure that changelog is open while operation is in progress */
  1350. rc = _cl5AddThread ();
  1351. if (rc != CL5_SUCCESS ) return rc;
  1352. rc = _cl5GetDBFile (replica, &obj);
  1353. if (rc == CL5_SUCCESS && obj)
  1354. {
  1355. /* iterate through the ruv in csn order to find first master for which
  1356. we can replay changes */
  1357. rc = _cl5PositionCursorForReplay (consumerRID, consumerRuv, replica, obj, iterator, NULL);
  1358. }
  1359. else
  1360. {
  1361. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1362. "cl5CreateReplayIteratorEx: could not find DB object for replica\n");
  1363. }
  1364. if (rc != CL5_SUCCESS)
  1365. {
  1366. if (obj) object_release (obj);
  1367. /* release the thread */
  1368. _cl5RemoveThread ();
  1369. }
  1370. return rc;
  1371. }
  1372. /* cl5CreateReplayIterator is now a wrapper for cl5CreateReplayIteratorEx */
  1373. int cl5CreateReplayIterator (Private_Repl_Protocol *prp, const RUV *consumerRuv,
  1374. CL5ReplayIterator **iterator)
  1375. {
  1376. /* DBDB : I thought it should be possible to refactor this like so, but it seems to not work.
  1377. Possibly the ordering of the calls is significant.
  1378. ReplicaId consumerRID = agmt_get_consumer_rid ( prp->agmt, prp->conn );
  1379. return cl5CreateReplayIteratorEx(prp,consumerRuv,iterator,consumerRID); */
  1380. int rc;
  1381. Object *replica;
  1382. Object *obj = NULL;
  1383. replica = prp->replica_object;
  1384. if (replica == NULL || consumerRuv == NULL || iterator == NULL)
  1385. {
  1386. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1387. "cl5CreateReplayIterator: invalid parameter\n");
  1388. return CL5_BAD_DATA;
  1389. }
  1390. *iterator = NULL;
  1391. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1392. {
  1393. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1394. "cl5CreateReplayIterator: changelog is not initialized\n");
  1395. return CL5_BAD_STATE;
  1396. }
  1397. /* make sure that changelog is open while operation is in progress */
  1398. rc = _cl5AddThread ();
  1399. if (rc != CL5_SUCCESS ) return rc;
  1400. rc = _cl5GetDBFile (replica, &obj);
  1401. if (rc == CL5_SUCCESS && obj)
  1402. {
  1403. /* iterate through the ruv in csn order to find first master for which
  1404. we can replay changes */
  1405. ReplicaId consumerRID = agmt_get_consumer_rid ( prp->agmt, prp->conn );
  1406. int continue_on_missing = agmt_get_ignoremissing ( prp->agmt);
  1407. int save_cont_miss = continue_on_missing;
  1408. rc = _cl5PositionCursorForReplay (consumerRID, consumerRuv, replica, obj, iterator, &continue_on_missing);
  1409. if (save_cont_miss == 1 && continue_on_missing ==0) {
  1410. /* the option to continue once on a missing csn was used, rest */
  1411. agmt_set_ignoremissing ( prp->agmt, 0);
  1412. }
  1413. }
  1414. else
  1415. {
  1416. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1417. "cl5CreateReplayIterator: could not find DB object for replica\n");
  1418. }
  1419. if (rc != CL5_SUCCESS)
  1420. {
  1421. if (obj) object_release (obj);
  1422. /* release the thread */
  1423. _cl5RemoveThread ();
  1424. }
  1425. return rc;
  1426. }
  1427. /* Name: cl5GetNextOperationToReplay
  1428. Description: retrieves next operation to be sent to a particular consumer and
  1429. that was created on a particular master. Consumer and master info
  1430. is encoded in the iterator parameter that must be created by call
  1431. to cl5CreateReplayIterator.
  1432. Parameters: iterator - iterator that identifies next entry to retrieve;
  1433. op - operation retrieved if function is successful
  1434. Return: CL5_SUCCESS if function is successfull;
  1435. CL5_BAD_DATA if invalid parameter is passed;
  1436. CL5_NOTFOUND if end of iteration list is reached
  1437. CL5_DB_ERROR if any other db error occured;
  1438. CL5_BADFORMAT if data in db is of unrecognized format;
  1439. CL5_MEMORY_ERROR if memory allocation fails.
  1440. Algorithm: Iterate through changelog entries until a change is found that
  1441. originated at the replica for which we are sending changes
  1442. (based on the information in the iteration hash) and
  1443. whose csn is larger than the csn already seen by the consumer
  1444. If change originated at the replica not in the hash,
  1445. determine whether we should send changes originated at the replica
  1446. and add replica entry into the hash. We can send the changes for
  1447. the replica if the current csn is smaller or equal to the csn
  1448. in the consumer's ruv (if present) or if it is equal to the min
  1449. csn in the supplier's ruv.
  1450. */
  1451. int
  1452. cl5GetNextOperationToReplay (CL5ReplayIterator *iterator, CL5Entry *entry)
  1453. {
  1454. CSN *csn;
  1455. char *key, *data;
  1456. size_t keylen, datalen;
  1457. char *agmt_name;
  1458. int rc = 0;
  1459. agmt_name = get_thread_private_agmtname();
  1460. if (entry == NULL)
  1461. {
  1462. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1463. "%s: cl5GetNextOperationToReplay: invalid parameter passed\n", agmt_name);
  1464. return CL5_BAD_DATA;
  1465. }
  1466. rc = clcache_get_next_change (iterator->clcache, (void **)&key, &keylen, (void **)&data, &datalen, &csn);
  1467. if (rc == DB_NOTFOUND)
  1468. {
  1469. /*
  1470. * Abort means we've figured out that we've passed the replica Min CSN,
  1471. * so we should stop looping through the changelog
  1472. */
  1473. return CL5_NOTFOUND;
  1474. }
  1475. if (rc != 0)
  1476. {
  1477. slapi_log_error(SLAPI_LOG_FATAL, NULL, "%s: cl5GetNextOperationToReplay: "
  1478. "failed to read next entry; DB error %d\n", agmt_name, rc);
  1479. return CL5_DB_ERROR;
  1480. }
  1481. if(is_cleaned_rid(csn_get_replicaid(csn))){
  1482. /*
  1483. * This operation is from a deleted replica. During the cleanallruv task the
  1484. * replicas are cleaned first before this instance is. This can cause the
  1485. * server to basically do a full update over and over. So we have to watch for
  1486. * this, and not send these operations out.
  1487. */
  1488. return CL5_IGNORE_OP;
  1489. }
  1490. /* there is an entry we should return */
  1491. /* Callers of this function should cl5_operation_parameters_done(op) */
  1492. if ( 0 != cl5DBData2Entry ( data, datalen, entry ) )
  1493. {
  1494. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1495. "%s: cl5GetNextOperationToReplay: failed to format entry rc=%d\n", agmt_name, rc);
  1496. return rc;
  1497. }
  1498. return CL5_SUCCESS;
  1499. }
  1500. /* Name: cl5DestroyReplayIterator
  1501. Description: destorys iterator
  1502. Parameters: iterator - iterator to destory
  1503. Return: none
  1504. */
  1505. void cl5DestroyReplayIterator (CL5ReplayIterator **iterator)
  1506. {
  1507. if (iterator == NULL)
  1508. {
  1509. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1510. "cl5DestroyReplayIterator: invalid iterartor passed\n");
  1511. return;
  1512. }
  1513. clcache_return_buffer ( &(*iterator)->clcache );
  1514. if ((*iterator)->fileObj) {
  1515. object_release ((*iterator)->fileObj);
  1516. (*iterator)->fileObj = NULL;
  1517. }
  1518. /* release supplier's ruv */
  1519. if ((*iterator)->supplierRuvObj) {
  1520. object_release ((*iterator)->supplierRuvObj);
  1521. (*iterator)->supplierRuvObj = NULL;
  1522. }
  1523. slapi_ch_free ((void **)iterator);
  1524. /* this thread no longer holds a db reference, release it */
  1525. _cl5RemoveThread();
  1526. }
  1527. /* Name: cl5DeleteOnClose
  1528. Description: marks changelog for deletion when it is closed
  1529. Parameters: flag; if flag = 1 then delete else don't
  1530. Return: none
  1531. */
  1532. void cl5DeleteOnClose (PRBool rm)
  1533. {
  1534. s_cl5Desc.dbRmOnClose = rm;
  1535. }
  1536. /* Name: cl5GetDir
  1537. Description: returns changelog directory
  1538. Parameters: none
  1539. Return: copy of the directory; caller needs to free the string
  1540. */
  1541. char *cl5GetDir ()
  1542. {
  1543. if (s_cl5Desc.dbDir == NULL)
  1544. {
  1545. return NULL;
  1546. }
  1547. else
  1548. {
  1549. return slapi_ch_strdup (s_cl5Desc.dbDir);
  1550. }
  1551. }
  1552. /* Name: cl5Exist
  1553. Description: checks if a changelog exists in the specified directory;
  1554. We consider changelog to exist if it contains the dbversion file.
  1555. Parameters: clDir - directory to check
  1556. Return: 1 - if changelog exists; 0 - otherwise
  1557. */
  1558. PRBool cl5Exist (const char *clDir)
  1559. {
  1560. char fName [MAXPATHLEN + 1];
  1561. int rc;
  1562. PR_snprintf (fName, MAXPATHLEN, "%s/%s", clDir, VERSION_FILE);
  1563. rc = PR_Access (fName, PR_ACCESS_EXISTS);
  1564. return (rc == PR_SUCCESS);
  1565. }
  1566. /* Name: cl5GetOperationCount
  1567. Description: returns number of entries in the changelog. The changelog must be
  1568. open for the value to be meaningful.
  1569. Parameters: replica - optional parameter that specifies the replica whose operations
  1570. we wish to count; if NULL all changelog entries are counted
  1571. Return: number of entries in the changelog
  1572. */
  1573. int cl5GetOperationCount (Object *replica)
  1574. {
  1575. Object *obj;
  1576. CL5DBFile *file;
  1577. int count = 0;
  1578. int rc;
  1579. if (s_cl5Desc.dbState == CL5_STATE_NONE)
  1580. {
  1581. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1582. "cl5GetOperationCount: changelog is not initialized\n");
  1583. return -1;
  1584. }
  1585. /* make sure that changelog is open while operation is in progress */
  1586. rc = _cl5AddThread ();
  1587. if (rc != CL5_SUCCESS)
  1588. return -1;
  1589. if (replica == NULL) /* compute total entry count */
  1590. {
  1591. obj = objset_first_obj (s_cl5Desc.dbFiles);
  1592. while (obj)
  1593. {
  1594. file = (CL5DBFile*)object_get_data (obj);
  1595. PR_ASSERT (file);
  1596. count += file->entryCount;
  1597. obj = objset_next_obj (s_cl5Desc.dbFiles, obj);
  1598. }
  1599. }
  1600. else /* return count for particular db */
  1601. {
  1602. /* select correct db file */
  1603. rc = _cl5GetDBFile (replica, &obj);
  1604. if (rc == CL5_SUCCESS)
  1605. {
  1606. file = (CL5DBFile*)object_get_data (obj);
  1607. PR_ASSERT (file);
  1608. count = file->entryCount;
  1609. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1610. "cl5GetOperationCount: found DB object %p\n", obj);
  1611. object_release (obj);
  1612. }
  1613. else
  1614. {
  1615. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1616. "cl5GetOperationCount: could not get DB object for replica\n");
  1617. count = 0;
  1618. }
  1619. }
  1620. _cl5RemoveThread ();
  1621. return count;
  1622. }
  1623. /***** Helper Functions *****/
  1624. /* this call happens under state lock */
  1625. static int _cl5Open (const char *dir, const CL5DBConfig *config, CL5OpenMode openMode)
  1626. {
  1627. int rc;
  1628. PR_ASSERT (dir);
  1629. /* setup db configuration parameters */
  1630. if (config)
  1631. {
  1632. _cl5SetDBConfig (config);
  1633. }
  1634. else
  1635. {
  1636. _cl5SetDefaultDBConfig ();
  1637. }
  1638. /* initialize trimming */
  1639. rc = _cl5TrimInit ();
  1640. if (rc != CL5_SUCCESS)
  1641. {
  1642. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1643. "_cl5Open: failed to initialize trimming\n");
  1644. goto done;
  1645. }
  1646. /* create the changelog directory if it does not exist */
  1647. rc = cl5CreateDirIfNeeded (dir);
  1648. if (rc != CL5_SUCCESS)
  1649. {
  1650. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1651. "_cl5Open: failed to create changelog directory (%s)\n", dir);
  1652. goto done;
  1653. }
  1654. if (s_cl5Desc.dbDir) {
  1655. slapi_ch_free_string (&s_cl5Desc.dbDir);
  1656. }
  1657. s_cl5Desc.dbDir = slapi_ch_strdup (dir);
  1658. /* check database version */
  1659. rc = _cl5CheckDBVersion ();
  1660. if (rc != CL5_SUCCESS)
  1661. {
  1662. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5Open: invalid db version\n");
  1663. goto done;
  1664. }
  1665. s_cl5Desc.dbOpenMode = openMode;
  1666. /* initialize db environment */
  1667. rc = _cl5AppInit ();
  1668. if (rc != CL5_SUCCESS)
  1669. {
  1670. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1671. "_cl5Open: failed to initialize db environment\n");
  1672. goto done;
  1673. }
  1674. /* init the clcache */
  1675. if (( clcache_init (&s_cl5Desc.dbEnv) != 0 )) {
  1676. rc = CL5_SYSTEM_ERROR;
  1677. goto done;
  1678. }
  1679. /* open database files */
  1680. rc = _cl5DBOpen ();
  1681. if (rc != CL5_SUCCESS)
  1682. {
  1683. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1684. "_cl5Open: failed to open changelog database\n");
  1685. goto done;
  1686. }
  1687. done:;
  1688. if (rc != CL5_SUCCESS)
  1689. {
  1690. _cl5Close ();
  1691. }
  1692. return rc;
  1693. }
  1694. int cl5CreateDirIfNeeded (const char *dirName)
  1695. {
  1696. int rc;
  1697. char buff [MAXPATHLEN + 1];
  1698. char *t;
  1699. PR_ASSERT (dirName);
  1700. rc = PR_Access(dirName, PR_ACCESS_EXISTS);
  1701. if (rc == PR_SUCCESS)
  1702. {
  1703. return CL5_SUCCESS;
  1704. }
  1705. /* directory does not exist - try to create */
  1706. PL_strncpyz (buff, dirName, sizeof(buff)-1);
  1707. t = strchr (buff, '/');
  1708. /* skip first slash */
  1709. if (t)
  1710. {
  1711. t = strchr (t+1, '/');
  1712. }
  1713. while (t)
  1714. {
  1715. *t = '\0';
  1716. if (PR_Access (buff, PR_ACCESS_EXISTS) != PR_SUCCESS)
  1717. {
  1718. rc = PR_MkDir (buff, DIR_CREATE_MODE);
  1719. if (rc != PR_SUCCESS)
  1720. {
  1721. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1722. "cl5CreateDirIfNeeded: failed to create dir (%s); NSPR error - %d\n",
  1723. dirName, PR_GetError ());
  1724. return CL5_SYSTEM_ERROR;
  1725. }
  1726. }
  1727. *t++ = FILE_PATHSEP;
  1728. t = strchr (t, '/');
  1729. }
  1730. /* last piece */
  1731. rc = PR_MkDir (buff, DIR_CREATE_MODE);
  1732. if (rc != PR_SUCCESS)
  1733. {
  1734. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1735. "cl5CreateDirIfNeeded: failed to create dir; NSPR error - %d\n",
  1736. PR_GetError ());
  1737. return CL5_SYSTEM_ERROR;
  1738. }
  1739. return CL5_SUCCESS;
  1740. }
  1741. static int _cl5AppInit (void)
  1742. {
  1743. int rc = -1; /* initialize to failure */
  1744. DB_ENV *dbEnv = NULL;
  1745. size_t pagesize = 0;
  1746. int openflags = 0;
  1747. char *cookie = NULL;
  1748. Slapi_Backend *be = slapi_get_first_backend(&cookie);
  1749. while (be) {
  1750. rc = slapi_back_get_info(be, BACK_INFO_DBENV, (void **)&dbEnv);
  1751. if ((LDAP_SUCCESS == rc) && dbEnv) {
  1752. rc = slapi_back_get_info(be,
  1753. BACK_INFO_INDEXPAGESIZE, (void **)&pagesize);
  1754. if ((LDAP_SUCCESS == rc) && pagesize) {
  1755. rc = slapi_back_get_info(be,
  1756. BACK_INFO_DBENV_OPENFLAGS, (void **)&openflags);
  1757. if (LDAP_SUCCESS == rc) {
  1758. break; /* Successfully fetched */
  1759. }
  1760. }
  1761. }
  1762. be = slapi_get_next_backend(cookie);
  1763. }
  1764. slapi_ch_free((void **)&cookie);
  1765. if (rc == 0 && dbEnv && pagesize)
  1766. {
  1767. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1768. "_cl5AppInit: fetched backend dbEnv (%p)\n", dbEnv);
  1769. s_cl5Desc.dbEnv = dbEnv;
  1770. s_cl5Desc.dbEnvOpenFlags = openflags;
  1771. s_cl5Desc.dbConfig.pageSize = pagesize;
  1772. return CL5_SUCCESS;
  1773. }
  1774. else
  1775. {
  1776. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1777. "_cl5AppInit: failed to fetch backend dbenv (%p) and/or "
  1778. "index page size (%lu)\n", dbEnv, pagesize);
  1779. return CL5_DB_ERROR;
  1780. }
  1781. }
  1782. static int _cl5DBOpen ()
  1783. {
  1784. PRBool dbFile;
  1785. PRDir *dir;
  1786. PRDirEntry *entry = NULL;
  1787. int rc = -1; /* initialize to failure */
  1788. Object *replica;
  1789. int count = 0;
  1790. /* create lock that guarantees that each file is only added once to the list */
  1791. s_cl5Desc.fileLock = PR_NewLock ();
  1792. /* loop over all db files and open them; file name format is cl5_<dbid>.<dbext> */
  1793. dir = PR_OpenDir(s_cl5Desc.dbDir);
  1794. if (dir == NULL)
  1795. {
  1796. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1797. "_cl5DBOpen: failed to open changelog dir; NSPR error - %d\n",
  1798. PR_GetError ());
  1799. return CL5_SYSTEM_ERROR;
  1800. }
  1801. /* initialize set of db file objects */
  1802. s_cl5Desc.dbFiles = objset_new(NULL);
  1803. while (NULL != (entry = PR_ReadDir(dir, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
  1804. {
  1805. if (NULL == entry->name)
  1806. {
  1807. break;
  1808. }
  1809. dbFile = _cl5FileName2Replica (entry->name, &replica);
  1810. if (dbFile) /* this is db file, not a log or dbversion; those are just skipped */
  1811. {
  1812. /* we only open files for existing replicas */
  1813. if (replica)
  1814. {
  1815. rc = _cl5DBOpenFile (replica, NULL /* file object */,
  1816. PR_FALSE /* check for duplicates */);
  1817. if (rc != CL5_SUCCESS)
  1818. {
  1819. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBOpen: "
  1820. "Error opening file %s\n",
  1821. entry->name);
  1822. return rc;
  1823. }
  1824. object_release (replica);
  1825. count++;
  1826. }
  1827. else /* there is no matching replica for the file - remove */
  1828. {
  1829. char fullpathname[MAXPATHLEN];
  1830. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBOpen: "
  1831. "file %s has no matching replica; removing\n", entry->name);
  1832. PR_snprintf(fullpathname, MAXPATHLEN, "%s/%s", s_cl5Desc.dbDir, entry->name);
  1833. rc = s_cl5Desc.dbEnv->dbremove(s_cl5Desc.dbEnv,
  1834. 0, fullpathname, 0,
  1835. DEFAULT_DB_ENV_OP_FLAGS);
  1836. if (rc != 0)
  1837. {
  1838. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  1839. "_cl5DBOpen: failed to remove (%s) file; "
  1840. "libdb error - %d (%s)\n",
  1841. fullpathname, rc, db_strerror(rc));
  1842. if (PR_Delete(fullpathname) != PR_SUCCESS) {
  1843. PRErrorCode prerr = PR_GetError();
  1844. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1845. "_cl5DBOpen: failed to remove (%s) file; "
  1846. "nspr error - %d (%s)\n",
  1847. fullpathname, prerr, slapd_pr_strerror(prerr));
  1848. }
  1849. }
  1850. }
  1851. }
  1852. }
  1853. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBOpen: "
  1854. "opened %d existing databases in %s\n", count, s_cl5Desc.dbDir);
  1855. PR_CloseDir(dir);
  1856. return CL5_SUCCESS;
  1857. }
  1858. /* this function assumes that the entry was validated
  1859. using IsValidOperation
  1860. Data in db format:
  1861. ------------------
  1862. <1 byte version><1 byte change_type><sizeof time_t time><null terminated csn>
  1863. <null terminated uniqueid><null terminated targetdn>
  1864. [<null terminated newrdn><1 byte deleteoldrdn>][<4 byte mod count><mod1><mod2>....]
  1865. mod format:
  1866. -----------
  1867. <1 byte modop><null terminated attr name><4 byte value count>
  1868. <4 byte value size><value1><4 byte value size><value2>
  1869. */
  1870. static int _cl5Entry2DBData (const CL5Entry *entry, char **data, PRUint32 *len)
  1871. {
  1872. int size = 1 /* version */ + 1 /* operation type */ + sizeof (time_t);
  1873. char *pos;
  1874. PRUint32 t;
  1875. slapi_operation_parameters *op;
  1876. LDAPMod **add_mods = NULL;
  1877. char *rawDN = NULL;
  1878. char s[CSN_STRSIZE];
  1879. PR_ASSERT (entry && entry->op && data && len);
  1880. op = entry->op;
  1881. PR_ASSERT (op->target_address.uniqueid);
  1882. /* compute size of the buffer needed to hold the data */
  1883. size += CSN_STRSIZE;
  1884. size += strlen (op->target_address.uniqueid) + 1;
  1885. switch (op->operation_type)
  1886. {
  1887. case SLAPI_OPERATION_ADD: if (op->p.p_add.parentuniqueid)
  1888. size += strlen (op->p.p_add.parentuniqueid) + 1;
  1889. else
  1890. size ++; /* we just store NULL char */
  1891. slapi_entry2mods (op->p.p_add.target_entry, &rawDN/* dn */, &add_mods);
  1892. size += strlen (rawDN) + 1;
  1893. /* Need larger buffer for the encrypted changelog */
  1894. if (s_cl5Desc.clcrypt_handle) {
  1895. size += (_cl5GetModsSize (add_mods) * (1 + BACK_CRYPT_OUTBUFF_EXTLEN));
  1896. } else {
  1897. size += _cl5GetModsSize (add_mods);
  1898. }
  1899. break;
  1900. case SLAPI_OPERATION_MODIFY: size += REPL_GET_DN_LEN(&op->target_address) + 1;
  1901. /* Need larger buffer for the encrypted changelog */
  1902. if (s_cl5Desc.clcrypt_handle) {
  1903. size += (_cl5GetModsSize (op->p.p_modify.modify_mods) * (1 + BACK_CRYPT_OUTBUFF_EXTLEN));
  1904. } else {
  1905. size += _cl5GetModsSize (op->p.p_modify.modify_mods);
  1906. }
  1907. break;
  1908. case SLAPI_OPERATION_MODRDN: size += REPL_GET_DN_LEN(&op->target_address) + 1;
  1909. /* 1 for deleteoldrdn */
  1910. size += strlen (op->p.p_modrdn.modrdn_newrdn) + 2;
  1911. if (REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address))
  1912. size += REPL_GET_DN_LEN(&op->p.p_modrdn.modrdn_newsuperior_address) + 1;
  1913. else
  1914. size ++; /* for NULL char */
  1915. if (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid)
  1916. size += strlen (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid) + 1;
  1917. else
  1918. size ++; /* for NULL char */
  1919. /* Need larger buffer for the encrypted changelog */
  1920. if (s_cl5Desc.clcrypt_handle) {
  1921. size += (_cl5GetModsSize (op->p.p_modrdn.modrdn_mods) * (1 + BACK_CRYPT_OUTBUFF_EXTLEN));
  1922. } else {
  1923. size += _cl5GetModsSize (op->p.p_modrdn.modrdn_mods);
  1924. }
  1925. break;
  1926. case SLAPI_OPERATION_DELETE: size += REPL_GET_DN_LEN(&op->target_address) + 1;
  1927. break;
  1928. }
  1929. /* allocate data buffer */
  1930. (*data) = slapi_ch_malloc (size);
  1931. if ((*data) == NULL)
  1932. {
  1933. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1934. "_cl5Entry2DBData: failed to allocate data buffer\n");
  1935. return CL5_MEMORY_ERROR;
  1936. }
  1937. /* fill in the data buffer */
  1938. pos = *data;
  1939. /* write a byte of version */
  1940. (*pos) = V_5;
  1941. pos ++;
  1942. /* write change type */
  1943. (*pos) = (unsigned char)op->operation_type;
  1944. pos ++;
  1945. /* write time */
  1946. t = PR_htonl((PRUint32)entry->time);
  1947. memcpy (pos, &t, sizeof (t));
  1948. pos += sizeof (t);
  1949. /* write csn */
  1950. _cl5WriteString (csn_as_string(op->csn,PR_FALSE,s), &pos);
  1951. /* write UniqueID */
  1952. _cl5WriteString (op->target_address.uniqueid, &pos);
  1953. /* figure out what else we need to write depending on the operation type */
  1954. switch (op->operation_type)
  1955. {
  1956. case SLAPI_OPERATION_ADD: _cl5WriteString (op->p.p_add.parentuniqueid, &pos);
  1957. _cl5WriteString (rawDN, &pos);
  1958. _cl5WriteMods (add_mods, &pos);
  1959. slapi_ch_free ((void**)&rawDN);
  1960. ldap_mods_free (add_mods, 1);
  1961. break;
  1962. case SLAPI_OPERATION_MODIFY: _cl5WriteString (REPL_GET_DN(&op->target_address), &pos);
  1963. _cl5WriteMods (op->p.p_modify.modify_mods, &pos);
  1964. break;
  1965. case SLAPI_OPERATION_MODRDN: _cl5WriteString (REPL_GET_DN(&op->target_address), &pos);
  1966. _cl5WriteString (op->p.p_modrdn.modrdn_newrdn, &pos);
  1967. *pos = (PRUint8)op->p.p_modrdn.modrdn_deloldrdn;
  1968. pos ++;
  1969. _cl5WriteString (REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address), &pos);
  1970. _cl5WriteString (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid, &pos);
  1971. _cl5WriteMods (op->p.p_modrdn.modrdn_mods, &pos);
  1972. break;
  1973. case SLAPI_OPERATION_DELETE: _cl5WriteString (REPL_GET_DN(&op->target_address), &pos);
  1974. break;
  1975. }
  1976. /* (*len) != size in case encrypted */
  1977. (*len) = pos - *data;
  1978. if (*len > size) {
  1979. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  1980. "_cl5Entry2DBData: real len %d > estimated size %d\n",
  1981. *len, size);
  1982. return CL5_MEMORY_ERROR;
  1983. }
  1984. return CL5_SUCCESS;
  1985. }
  1986. /*
  1987. Data in db format:
  1988. ------------------
  1989. <1 byte version><1 byte change_type><sizeof time_t time><null terminated dbid>
  1990. <null terminated csn><null terminated uniqueid><null terminated targetdn>
  1991. [<null terminated newrdn><1 byte deleteoldrdn>][<4 byte mod count><mod1><mod2>....]
  1992. mod format:
  1993. -----------
  1994. <1 byte modop><null terminated attr name><4 byte value count>
  1995. <4 byte value size><value1><4 byte value size><value2>
  1996. */
  1997. int
  1998. cl5DBData2Entry (const char *data, PRUint32 len, CL5Entry *entry)
  1999. {
  2000. int rc;
  2001. PRUint8 version;
  2002. char *pos = (char *)data;
  2003. char *strCSN;
  2004. PRUint32 thetime;
  2005. slapi_operation_parameters *op;
  2006. LDAPMod **add_mods;
  2007. char *rawDN = NULL;
  2008. char s[CSN_STRSIZE];
  2009. PR_ASSERT (data && entry && entry->op);
  2010. /* ONREPL - check that we do not go beyond the end of the buffer */
  2011. /* read byte of version */
  2012. version = (PRUint8)(*pos);
  2013. if (version != V_5)
  2014. {
  2015. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2016. "cl5DBData2Entry: invalid data version\n");
  2017. return CL5_BAD_FORMAT;
  2018. }
  2019. op = entry->op;
  2020. pos += sizeof(version);
  2021. /* read change type */
  2022. op->operation_type = (PRUint8)(*pos);
  2023. pos ++;
  2024. /* need to do the copy first, to skirt around alignment problems on
  2025. certain architectures */
  2026. memcpy((char *)&thetime,pos,sizeof(thetime));
  2027. entry->time = (time_t)PR_ntohl(thetime);
  2028. pos += sizeof (thetime);
  2029. /* read csn */
  2030. _cl5ReadString (&strCSN, &pos);
  2031. if (op->csn == NULL || strcmp (strCSN, csn_as_string(op->csn,PR_FALSE,s)) != 0)
  2032. {
  2033. op->csn = csn_new_by_string (strCSN);
  2034. }
  2035. slapi_ch_free ((void**)&strCSN);
  2036. /* read UniqueID */
  2037. _cl5ReadString (&op->target_address.uniqueid, &pos);
  2038. /* figure out what else we need to read depending on the operation type */
  2039. switch (op->operation_type)
  2040. {
  2041. case SLAPI_OPERATION_ADD: _cl5ReadString (&op->p.p_add.parentuniqueid, &pos);
  2042. /* richm: need to free parentuniqueid */
  2043. _cl5ReadString (&rawDN, &pos);
  2044. op->target_address.sdn = slapi_sdn_new_dn_passin(rawDN);
  2045. /* convert mods to entry */
  2046. rc = _cl5ReadMods (&add_mods, &pos);
  2047. slapi_mods2entry (&(op->p.p_add.target_entry), rawDN, add_mods);
  2048. ldap_mods_free (add_mods, 1);
  2049. break;
  2050. case SLAPI_OPERATION_MODIFY: _cl5ReadString (&rawDN, &pos);
  2051. op->target_address.sdn = slapi_sdn_new_dn_passin(rawDN);
  2052. rc = _cl5ReadMods (&op->p.p_modify.modify_mods, &pos);
  2053. break;
  2054. case SLAPI_OPERATION_MODRDN: _cl5ReadString (&rawDN, &pos);
  2055. op->target_address.sdn = slapi_sdn_new_dn_passin(rawDN);
  2056. _cl5ReadString (&op->p.p_modrdn.modrdn_newrdn, &pos);
  2057. op->p.p_modrdn.modrdn_deloldrdn = *pos;
  2058. pos ++;
  2059. _cl5ReadString (&rawDN, &pos);
  2060. op->p.p_modrdn.modrdn_newsuperior_address.sdn = slapi_sdn_new_dn_passin(rawDN);
  2061. _cl5ReadString (&op->p.p_modrdn.modrdn_newsuperior_address.uniqueid, &pos);
  2062. rc = _cl5ReadMods (&op->p.p_modrdn.modrdn_mods, &pos);
  2063. break;
  2064. case SLAPI_OPERATION_DELETE: _cl5ReadString (&rawDN, &pos);
  2065. op->target_address.sdn = slapi_sdn_new_dn_passin(rawDN);
  2066. rc = CL5_SUCCESS;
  2067. break;
  2068. default: rc = CL5_BAD_FORMAT;
  2069. slapi_log_error(SLAPI_LOG_FATAL,
  2070. repl_plugin_name_cl,
  2071. "cl5DBData2Entry: failed to format entry\n");
  2072. break;
  2073. }
  2074. return rc;
  2075. }
  2076. /* thread management functions */
  2077. static int _cl5DispatchDBThreads ()
  2078. {
  2079. PRThread *pth = NULL;
  2080. pth = PR_CreateThread(PR_USER_THREAD, (VFP)(void*)_cl5TrimMain,
  2081. NULL, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2082. PR_UNJOINABLE_THREAD, DEFAULT_THREAD_STACKSIZE);
  2083. if (NULL == pth)
  2084. {
  2085. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2086. "_cl5DispatchDBThreads: failed to create trimming "
  2087. "thread; NSPR error - %d\n", PR_GetError ());
  2088. return CL5_SYSTEM_ERROR;
  2089. }
  2090. return CL5_SUCCESS;
  2091. }
  2092. static int _cl5AddThread ()
  2093. {
  2094. /* lock the state lock so that nobody can change the state
  2095. while backup is in progress
  2096. */
  2097. slapi_rwlock_rdlock (s_cl5Desc.stLock);
  2098. /* open changelog if it is not already open */
  2099. if (s_cl5Desc.dbState != CL5_STATE_OPEN)
  2100. {
  2101. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2102. "_cl5AddThread: invalid changelog state - %d\n", s_cl5Desc.dbState);
  2103. slapi_rwlock_unlock (s_cl5Desc.stLock);
  2104. return CL5_BAD_STATE;
  2105. }
  2106. slapi_rwlock_unlock (s_cl5Desc.stLock);
  2107. /* increment global thread count to make sure that changelog does not close while
  2108. backup is in progress */
  2109. PR_AtomicIncrement (&s_cl5Desc.threadCount);
  2110. return CL5_SUCCESS;
  2111. }
  2112. static void _cl5RemoveThread ()
  2113. {
  2114. PR_ASSERT (s_cl5Desc.threadCount > 0);
  2115. PR_AtomicDecrement (&s_cl5Desc.threadCount);
  2116. }
  2117. /* data conversion functions */
  2118. static void _cl5WriteString (const char *str, char **buff)
  2119. {
  2120. if (str)
  2121. {
  2122. strcpy (*buff, str);
  2123. (*buff) += strlen (str) + 1;
  2124. }
  2125. else /* just write NULL char */
  2126. {
  2127. (**buff) = '\0';
  2128. (*buff) ++;
  2129. }
  2130. }
  2131. static void _cl5ReadString (char **str, char **buff)
  2132. {
  2133. if (str)
  2134. {
  2135. int len = strlen (*buff);
  2136. if (len)
  2137. {
  2138. *str = slapi_ch_strdup (*buff);
  2139. (*buff) += len + 1;
  2140. }
  2141. else /* just null char - skip it */
  2142. {
  2143. *str = NULL;
  2144. (*buff) ++;
  2145. }
  2146. }
  2147. else /* just skip this string */
  2148. {
  2149. (*buff) += strlen (*buff) + 1;
  2150. }
  2151. }
  2152. /* mods format:
  2153. -----------
  2154. <4 byte mods count><mod1><mod2>...
  2155. mod format:
  2156. -----------
  2157. <1 byte modop><null terminated attr name><4 byte count>
  2158. <4 byte size><value1><4 byte size><value2>...
  2159. */
  2160. static void _cl5WriteMods (LDAPMod **mods, char **buff)
  2161. {
  2162. PRInt32 i;
  2163. char *mod_start;
  2164. PRInt32 count = 0;
  2165. if (mods == NULL)
  2166. return;
  2167. /* skip mods count */
  2168. mod_start = (*buff) + sizeof (count);
  2169. /* write mods*/
  2170. for (i = 0; mods[i]; i++) {
  2171. if (0 <= _cl5WriteMod (mods[i], &mod_start)) {
  2172. count++;
  2173. }
  2174. }
  2175. count = PR_htonl(count);
  2176. memcpy (*buff, &count, sizeof (count));
  2177. (*buff) = mod_start;
  2178. }
  2179. /*
  2180. * return values:
  2181. * positive: no need to encrypt && succeeded to write a mod
  2182. * 0: succeeded to encrypt && write a mod
  2183. * netative: failed to encrypt && no write to the changelog
  2184. */
  2185. static int
  2186. _cl5WriteMod (LDAPMod *mod, char **buff)
  2187. {
  2188. char *orig_pos;
  2189. char *pos;
  2190. PRInt32 count;
  2191. struct berval *bv;
  2192. struct berval *encbv;
  2193. struct berval *bv_to_use;
  2194. Slapi_Mod smod;
  2195. int rc = -1;
  2196. if (NULL == mod) {
  2197. return rc;
  2198. }
  2199. if (SLAPD_UNHASHED_PW_NOLOG == slapi_config_get_unhashed_pw_switch()) {
  2200. if (0 == strcasecmp(mod->mod_type, PSEUDO_ATTR_UNHASHEDUSERPASSWORD)) {
  2201. /* If nsslapd-unhashed-pw-switch == nolog, skip writing it to cl. */
  2202. return rc;
  2203. }
  2204. }
  2205. slapi_mod_init_byref(&smod, mod);
  2206. orig_pos = pos = *buff;
  2207. /* write mod op */
  2208. *pos = (PRUint8)slapi_mod_get_operation (&smod);
  2209. pos ++;
  2210. /* write attribute name */
  2211. _cl5WriteString (slapi_mod_get_type (&smod), &pos);
  2212. /* write value count */
  2213. count = PR_htonl(slapi_mod_get_num_values(&smod));
  2214. memcpy (pos, &count, sizeof (count));
  2215. pos += sizeof (PRInt32);
  2216. /* if the mod has no values, eg delete attr or replace attr without values
  2217. * do not reset buffer
  2218. */
  2219. rc = 0;
  2220. bv = slapi_mod_get_first_value (&smod);
  2221. while (bv)
  2222. {
  2223. encbv = NULL;
  2224. rc = clcrypt_encrypt_value(s_cl5Desc.clcrypt_handle,
  2225. bv, &encbv);
  2226. if (rc > 0) {
  2227. /* no encryption needed. use the original bv */
  2228. bv_to_use = bv;
  2229. } else if ((0 == rc) && encbv) {
  2230. /* successfully encrypted. use the encrypted bv */
  2231. bv_to_use = encbv;
  2232. } else { /* failed */
  2233. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2234. "_cl5WriteMod: encrypting \"%s: %s\" failed\n",
  2235. slapi_mod_get_type(&smod), bv->bv_val);
  2236. bv_to_use = NULL;
  2237. rc = -1;
  2238. break;
  2239. }
  2240. if (bv_to_use) {
  2241. _cl5WriteBerval (bv_to_use, &pos);
  2242. }
  2243. slapi_ch_bvfree(&encbv);
  2244. bv = slapi_mod_get_next_value (&smod);
  2245. }
  2246. if (rc < 0) {
  2247. (*buff) = orig_pos;
  2248. } else {
  2249. (*buff) = pos;
  2250. }
  2251. slapi_mod_done (&smod);
  2252. return rc;
  2253. }
  2254. /* mods format:
  2255. -----------
  2256. <4 byte mods count><mod1><mod2>...
  2257. mod format:
  2258. -----------
  2259. <1 byte modop><null terminated attr name><4 byte count>
  2260. {<4 byte size><value1><4 byte size><value2>... ||
  2261. <null terminated str1> <null terminated str2>...}
  2262. */
  2263. static int _cl5ReadMods (LDAPMod ***mods, char **buff)
  2264. {
  2265. char *pos = *buff;
  2266. int i;
  2267. int rc;
  2268. PRInt32 mod_count;
  2269. Slapi_Mods smods;
  2270. Slapi_Mod smod;
  2271. /* need to copy first, to skirt around alignment problems on certain
  2272. architectures */
  2273. memcpy((char *)&mod_count,*buff,sizeof(mod_count));
  2274. mod_count = PR_ntohl(mod_count);
  2275. pos += sizeof (mod_count);
  2276. slapi_mods_init (&smods , mod_count);
  2277. for (i = 0; i < mod_count; i++)
  2278. {
  2279. rc = _cl5ReadMod (&smod, &pos);
  2280. if (rc != CL5_SUCCESS)
  2281. {
  2282. slapi_mods_done(&smods);
  2283. return rc;
  2284. }
  2285. slapi_mods_add_smod(&smods, &smod);
  2286. }
  2287. *buff = pos;
  2288. *mods = slapi_mods_get_ldapmods_passout (&smods);
  2289. slapi_mods_done(&smods);
  2290. return CL5_SUCCESS;
  2291. }
  2292. static int _cl5ReadMod (Slapi_Mod *smod, char **buff)
  2293. {
  2294. char *pos = *buff;
  2295. int i;
  2296. PRInt32 val_count;
  2297. char *type;
  2298. int op;
  2299. struct berval bv;
  2300. struct berval *decbv;
  2301. struct berval *bv_to_use;
  2302. int rc = 0;
  2303. op = (*pos) & 0x000000FF;
  2304. pos ++;
  2305. _cl5ReadString (&type, &pos);
  2306. /* need to do the copy first, to skirt around alignment problems on
  2307. certain architectures */
  2308. memcpy((char *)&val_count,pos,sizeof(val_count));
  2309. val_count = PR_ntohl(val_count);
  2310. pos += sizeof (PRInt32);
  2311. slapi_mod_init(smod, val_count);
  2312. slapi_mod_set_operation (smod, op|LDAP_MOD_BVALUES);
  2313. slapi_mod_set_type (smod, type);
  2314. slapi_ch_free ((void**)&type);
  2315. for (i = 0; i < val_count; i++)
  2316. {
  2317. _cl5ReadBerval (&bv, &pos);
  2318. decbv = NULL;
  2319. rc = 0;
  2320. rc = clcrypt_decrypt_value(s_cl5Desc.clcrypt_handle,
  2321. &bv, &decbv);
  2322. if (rc > 0) {
  2323. /* not encrypted. use the original bv */
  2324. bv_to_use = &bv;
  2325. } else if ((0 == rc) && decbv) {
  2326. /* successfully decrypted. use the decrypted bv */
  2327. bv_to_use = decbv;
  2328. } else { /* failed */
  2329. char encstr[128];
  2330. char *encend = encstr + 128;
  2331. char *ptr;
  2332. int i;
  2333. for (i = 0, ptr = encstr; (i < bv.bv_len) && (ptr < encend - 4);
  2334. i++, ptr += 3) {
  2335. sprintf(ptr, "%x", 0xff & bv.bv_val[i]);
  2336. }
  2337. if (ptr >= encend - 4) {
  2338. sprintf(ptr, "...");
  2339. ptr += 3;
  2340. }
  2341. *ptr = '\0';
  2342. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2343. "_cl5ReadMod: decrypting \"%s: %s\" failed\n",
  2344. slapi_mod_get_type(smod), encstr);
  2345. bv_to_use = NULL;
  2346. }
  2347. if (bv_to_use) {
  2348. slapi_mod_add_value (smod, bv_to_use);
  2349. }
  2350. slapi_ch_bvfree(&decbv);
  2351. slapi_ch_free((void **) &bv.bv_val);
  2352. }
  2353. (*buff) = pos;
  2354. return CL5_SUCCESS;
  2355. }
  2356. static int _cl5GetModsSize (LDAPMod **mods)
  2357. {
  2358. int size;
  2359. int i;
  2360. if (mods == NULL)
  2361. return 0;
  2362. size = sizeof (PRInt32);
  2363. for (i=0; mods[i]; i++)
  2364. {
  2365. size += _cl5GetModSize (mods[i]);
  2366. }
  2367. return size;
  2368. }
  2369. static int _cl5GetModSize (LDAPMod *mod)
  2370. {
  2371. int size;
  2372. int i;
  2373. size = 1 + strlen (mod->mod_type) + 1 + sizeof (mod->mod_op);
  2374. i = 0;
  2375. if (mod->mod_op & LDAP_MOD_BVALUES) /* values are in binary form */
  2376. {
  2377. while (mod->mod_bvalues != NULL && mod->mod_bvalues[i] != NULL)
  2378. {
  2379. size += mod->mod_bvalues[i]->bv_len + sizeof (mod->mod_bvalues[i]->bv_len);
  2380. i++;
  2381. }
  2382. }
  2383. else /* string data */
  2384. {
  2385. PR_ASSERT(0); /* ggood string values should never be used in the server */
  2386. }
  2387. return size;
  2388. }
  2389. static void _cl5ReadBerval (struct berval *bv, char** buff)
  2390. {
  2391. PRUint32 length = 0;
  2392. PRUint32 net_length = 0;
  2393. PR_ASSERT (bv && buff);
  2394. /***PINAKI need to do the copy first, to skirt around alignment problems on
  2395. certain architectures */
  2396. /* DBDB : struct berval.bv_len is defined as unsigned long
  2397. * But code here expects it to be 32-bits in size.
  2398. * On 64-bit machines, this is not the case.
  2399. * I changed the code to consistently use 32-bit (4-byte)
  2400. * values on the encoded side. This means that it's
  2401. * possible to generate a huge berval that will not
  2402. * be encoded properly. However, this seems unlikely
  2403. * to happen in reality, and I felt that retaining the
  2404. * old on-disk format for the changely in the 64-bit
  2405. * version of the server was important.
  2406. */
  2407. memcpy((char *)&net_length, *buff, sizeof(net_length));
  2408. length = PR_ntohl(net_length);
  2409. *buff += sizeof(net_length);
  2410. bv->bv_len = length;
  2411. if (bv->bv_len > 0) {
  2412. bv->bv_val = slapi_ch_malloc (bv->bv_len);
  2413. memcpy (bv->bv_val, *buff, bv->bv_len);
  2414. *buff += bv->bv_len;
  2415. }
  2416. else {
  2417. bv->bv_val = NULL;
  2418. }
  2419. }
  2420. static void _cl5WriteBerval (struct berval *bv, char** buff)
  2421. {
  2422. PRUint32 length = 0;
  2423. PRUint32 net_length = 0;
  2424. length = (PRUint32) bv->bv_len;
  2425. net_length = PR_htonl(length);
  2426. memcpy(*buff, &net_length, sizeof (net_length));
  2427. *buff += sizeof (net_length);
  2428. memcpy (*buff, bv->bv_val, length);
  2429. *buff += length;
  2430. }
  2431. /* data format: <value count> <value size> <value> <value size> <value> ..... */
  2432. static int _cl5ReadBervals (struct berval ***bv, char** buff, unsigned int size)
  2433. {
  2434. PRInt32 count;
  2435. int i;
  2436. char *pos;
  2437. PR_ASSERT (bv && buff);
  2438. /* ONREPL - need to check that we don't go beyond the end of the buffer */
  2439. pos = *buff;
  2440. memcpy((char *)&count, pos, sizeof(count));
  2441. count = PR_htonl (count);
  2442. pos += sizeof(count);
  2443. /* allocate bervals */
  2444. *bv = (struct berval **)slapi_ch_malloc ((count + 1) * sizeof (struct berval*));
  2445. if (*bv == NULL)
  2446. {
  2447. return CL5_MEMORY_ERROR;
  2448. }
  2449. for (i = 0; i < count; i++)
  2450. {
  2451. (*bv)[i] = (struct berval *)slapi_ch_malloc (sizeof (struct berval));
  2452. if ((*bv)[i] == NULL)
  2453. {
  2454. ber_bvecfree(*bv);
  2455. return CL5_MEMORY_ERROR;
  2456. }
  2457. _cl5ReadBerval ((*bv)[i], &pos);
  2458. }
  2459. (*bv)[count] = NULL;
  2460. *buff = pos;
  2461. return CL5_SUCCESS;
  2462. }
  2463. /* data format: <value count> <value size> <value> <value size> <value> ..... */
  2464. static int _cl5WriteBervals (struct berval **bv, char** buff, unsigned int *size)
  2465. {
  2466. PRInt32 count, net_count;
  2467. char *pos;
  2468. int i;
  2469. PR_ASSERT (bv && buff && size);
  2470. /* compute number of values and size of the buffer to hold them */
  2471. *size = sizeof (count);
  2472. for (count = 0; bv[count]; count ++)
  2473. {
  2474. *size += sizeof (bv[count]->bv_len) + bv[count]->bv_len;
  2475. }
  2476. /* allocate buffer */
  2477. *buff = (char*) slapi_ch_malloc (*size);
  2478. if (*buff == NULL)
  2479. {
  2480. *size = 0;
  2481. return CL5_MEMORY_ERROR;
  2482. }
  2483. /* fill the buffer */
  2484. pos = *buff;
  2485. net_count = PR_htonl(count);
  2486. memcpy (pos, &net_count, sizeof (net_count));
  2487. pos += sizeof (net_count);
  2488. for (i = 0; i < count; i ++)
  2489. {
  2490. _cl5WriteBerval (bv[i], &pos);
  2491. }
  2492. return CL5_SUCCESS;
  2493. }
  2494. /* upgrade from db33 to db41
  2495. * 1. Run recovery on the database environment using the DB_ENV->open method
  2496. * 2. Remove any Berkeley DB environment using the DB_ENV->remove method
  2497. * 3. Remove any Berkeley DB transaction log files
  2498. * 4. extention .db3 -> .db4
  2499. */
  2500. static int _cl5Upgrade3_4(char *fromVersion, char *toVersion)
  2501. {
  2502. PRDir *dir = NULL;
  2503. PRDirEntry *entry = NULL;
  2504. DB *thisdb = NULL;
  2505. CL5OpenMode backup;
  2506. int rc = 0;
  2507. backup = s_cl5Desc.dbOpenMode;
  2508. s_cl5Desc.dbOpenMode = CL5_OPEN_CLEAN_RECOVER;
  2509. /* CL5_OPEN_CLEAN_RECOVER does 1 and 2 */
  2510. rc = _cl5AppInit ();
  2511. if (rc != CL5_SUCCESS)
  2512. {
  2513. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2514. "_cl5Upgrade3_4: failed to open the db env\n");
  2515. return rc;
  2516. }
  2517. s_cl5Desc.dbOpenMode = backup;
  2518. dir = PR_OpenDir(s_cl5Desc.dbDir);
  2519. if (dir == NULL)
  2520. {
  2521. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2522. "_cl5Upgrade3_4: failed to open changelog dir %s; NSPR error - %d\n",
  2523. s_cl5Desc.dbDir, PR_GetError ());
  2524. goto out;
  2525. }
  2526. while (NULL != (entry = PR_ReadDir(dir, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
  2527. {
  2528. if (NULL == entry->name)
  2529. {
  2530. break;
  2531. }
  2532. if (_cl5FileEndsWith(entry->name, DB_EXTENSION_DB3))
  2533. {
  2534. char oName [MAXPATHLEN + 1];
  2535. char nName [MAXPATHLEN + 1];
  2536. char *p = NULL;
  2537. char c;
  2538. int baselen = 0;
  2539. PR_snprintf(oName, MAXPATHLEN, "%s/%s", s_cl5Desc.dbDir, entry->name);
  2540. p = strstr(oName, DB_EXTENSION_DB3);
  2541. if (NULL == p)
  2542. {
  2543. continue;
  2544. }
  2545. /* db->rename closes DB; need to create every time */
  2546. rc = db_create(&thisdb, s_cl5Desc.dbEnv, 0);
  2547. if (0 != rc) {
  2548. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2549. "_cl5Upgrade3_4: failed to get db handle\n");
  2550. goto out;
  2551. }
  2552. baselen = p - oName;
  2553. c = *p;
  2554. *p = '\0';
  2555. PR_snprintf(nName, MAXPATHLEN+1, "%s", oName);
  2556. PR_snprintf(nName + baselen, MAXPATHLEN+1-baselen, "%s", DB_EXTENSION);
  2557. *p = c;
  2558. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2559. "_cl5Upgrade3_4: renaming %s to %s\n", oName, nName);
  2560. rc = thisdb->rename(thisdb, (const char *)oName, NULL /* subdb */,
  2561. (const char *)nName, 0);
  2562. if (rc != PR_SUCCESS)
  2563. {
  2564. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2565. "_cl5Upgrade3_4: failed to rename file (%s -> %s); "
  2566. "db error - %d %s\n", oName, nName, rc, db_strerror(rc));
  2567. break;
  2568. }
  2569. }
  2570. }
  2571. /* update the version file */
  2572. _cl5WriteDBVersion ();
  2573. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2574. "Upgrading from %s to %s is successfully done (%s)\n",
  2575. fromVersion, toVersion, s_cl5Desc.dbDir);
  2576. out:
  2577. if (NULL != dir)
  2578. {
  2579. PR_CloseDir(dir);
  2580. }
  2581. return rc;
  2582. }
  2583. /* upgrade from db41 -> db42 -> db43 -> db44 -> db45
  2584. * 1. Run recovery on the database environment using the DB_ENV->open method
  2585. * 2. Remove any Berkeley DB environment using the DB_ENV->remove method
  2586. * 3. Remove any Berkeley DB transaction log files
  2587. */
  2588. static int _cl5Upgrade4_4(char *fromVersion, char *toVersion)
  2589. {
  2590. CL5OpenMode backup;
  2591. int rc = 0;
  2592. backup = s_cl5Desc.dbOpenMode;
  2593. s_cl5Desc.dbOpenMode = CL5_OPEN_CLEAN_RECOVER;
  2594. /* CL5_OPEN_CLEAN_RECOVER does 1 and 2 */
  2595. rc = _cl5AppInit ();
  2596. if (rc != CL5_SUCCESS)
  2597. {
  2598. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2599. "_cl5Upgrade4_4: failed to open the db env\n");
  2600. return rc;
  2601. }
  2602. s_cl5Desc.dbOpenMode = backup;
  2603. /* update the version file */
  2604. _cl5WriteDBVersion ();
  2605. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2606. "Upgrading from %s to %s is successfully done (%s)\n",
  2607. fromVersion, toVersion, s_cl5Desc.dbDir);
  2608. return rc;
  2609. }
  2610. static int _cl5CheckDBVersion ()
  2611. {
  2612. char clVersion [VERSION_SIZE + 1];
  2613. char dbVersion [VERSION_SIZE + 1];
  2614. int rc;
  2615. if (!cl5Exist (s_cl5Desc.dbDir))
  2616. {
  2617. /* this is new changelog - write DB version and guardian file */
  2618. rc = _cl5WriteDBVersion ();
  2619. }
  2620. else
  2621. {
  2622. char *versionp = NULL;
  2623. char *versionendp = NULL;
  2624. char *dotp = NULL;
  2625. int dbmajor = 0;
  2626. int dbminor = 0;
  2627. PR_snprintf (clVersion, VERSION_SIZE, "%s/%d.%d/%s",
  2628. BDB_IMPL, DB_VERSION_MAJOR, DB_VERSION_MINOR, BDB_REPLPLUGIN);
  2629. rc = _cl5ReadDBVersion (s_cl5Desc.dbDir, dbVersion, sizeof(dbVersion));
  2630. if (rc != CL5_SUCCESS)
  2631. {
  2632. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2633. "_cl5CheckDBVersion: invalid dbversion\n");
  2634. rc = CL5_BAD_DBVERSION;
  2635. goto bailout;
  2636. }
  2637. versionendp = dbVersion + strlen(dbVersion);
  2638. /* get the version number */
  2639. /* old DBVERSION string: CL5_TYPE/REPL_PLUGIN_NAME/#.# */
  2640. if (PL_strncmp(dbVersion, CL5_TYPE, strlen(CL5_TYPE)) == 0)
  2641. {
  2642. versionp = strrchr(dbVersion, '/');
  2643. }
  2644. /* new DBVERSION string: bdb/#.#/libreplication-plugin */
  2645. else if (PL_strncmp(dbVersion, BDB_IMPL, strlen(BDB_IMPL)) == 0)
  2646. {
  2647. versionp = strchr(dbVersion, '/');
  2648. }
  2649. if (NULL == versionp || versionp == versionendp)
  2650. {
  2651. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2652. "_cl5CheckDBVersion: invalid dbversion: %s\n", dbVersion);
  2653. rc = CL5_BAD_DBVERSION;
  2654. goto bailout;
  2655. }
  2656. dotp = strchr(++versionp, '.');
  2657. if (NULL != dotp)
  2658. {
  2659. *dotp = '\0';
  2660. dbmajor = strtol(versionp, (char **)NULL, 10);
  2661. dbminor = strtol(dotp+1, (char **)NULL, 10);
  2662. *dotp = '.';
  2663. }
  2664. else
  2665. {
  2666. dbmajor = strtol(versionp, (char **)NULL, 10);
  2667. }
  2668. if (dbmajor < DB_VERSION_MAJOR)
  2669. {
  2670. /* upgrade */
  2671. rc = _cl5Upgrade3_4(dbVersion, clVersion);
  2672. if (rc != CL5_SUCCESS)
  2673. {
  2674. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2675. "_cl5CheckDBVersion: upgrade %s -> %s failed\n",
  2676. dbVersion, clVersion);
  2677. rc = CL5_BAD_DBVERSION;
  2678. }
  2679. }
  2680. else if (dbminor < DB_VERSION_MINOR)
  2681. {
  2682. /* minor upgrade */
  2683. rc = _cl5Upgrade4_4(dbVersion, clVersion);
  2684. if (rc != CL5_SUCCESS)
  2685. {
  2686. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2687. "_cl5CheckDBVersion: upgrade %s -> %s failed\n",
  2688. dbVersion, clVersion);
  2689. rc = CL5_BAD_DBVERSION;
  2690. }
  2691. }
  2692. }
  2693. bailout:
  2694. return rc;
  2695. }
  2696. static int _cl5ReadDBVersion (const char *dir, char *clVersion, int buflen)
  2697. {
  2698. int rc;
  2699. PRFileDesc *file;
  2700. char fName [MAXPATHLEN + 1];
  2701. char buff [BUFSIZ];
  2702. PRInt32 size;
  2703. char *tok;
  2704. char * iter = NULL;
  2705. if (clVersion)
  2706. {
  2707. clVersion [0] = '\0';
  2708. }
  2709. PR_snprintf (fName, MAXPATHLEN, "%s/%s", dir, VERSION_FILE);
  2710. file = PR_Open (fName, PR_RDONLY, 777);
  2711. if (file == NULL)
  2712. {
  2713. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2714. "_cl5ReadDBVersion: failed to open DBVERSION; NSPR error - %d\n",
  2715. PR_GetError ());
  2716. return CL5_SYSTEM_ERROR;
  2717. }
  2718. size = slapi_read_buffer (file, buff, BUFSIZ);
  2719. if (size < 0)
  2720. {
  2721. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2722. "_cl5ReadDBVersion: failed to read DBVERSION; NSPR error - %d\n",
  2723. PR_GetError ());
  2724. PR_Close (file);
  2725. return CL5_SYSTEM_ERROR;
  2726. }
  2727. /* parse the data */
  2728. buff[size]= '\0';
  2729. tok = ldap_utf8strtok_r (buff, "\n", &iter);
  2730. if (tok)
  2731. {
  2732. if (clVersion)
  2733. {
  2734. PL_strncpyz(clVersion, tok, buflen);
  2735. }
  2736. }
  2737. rc = PR_Close (file);
  2738. if (rc != PR_SUCCESS)
  2739. {
  2740. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2741. "_cl5ReadDBVersion: failed to close DBVERSION; NSPR error - %d\n",
  2742. PR_GetError ());
  2743. return CL5_SYSTEM_ERROR;
  2744. }
  2745. return CL5_SUCCESS;
  2746. }
  2747. static int _cl5WriteDBVersion ()
  2748. {
  2749. int rc;
  2750. PRFileDesc *file;
  2751. char fName [MAXPATHLEN + 1];
  2752. char clVersion [VERSION_SIZE + 1];
  2753. PRInt32 len, size;
  2754. PR_snprintf (fName, MAXPATHLEN, "%s/%s", s_cl5Desc.dbDir, VERSION_FILE);
  2755. file = PR_Open (fName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
  2756. s_cl5Desc.dbConfig.fileMode);
  2757. if (file == NULL)
  2758. {
  2759. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2760. "_cl5WriteDBVersion: failed to open DBVERSION; NSPR error - %d\n",
  2761. PR_GetError ());
  2762. return CL5_SYSTEM_ERROR;
  2763. }
  2764. /* write changelog version */
  2765. PR_snprintf (clVersion, VERSION_SIZE, "%s/%d.%d/%s\n",
  2766. BDB_IMPL, DB_VERSION_MAJOR, DB_VERSION_MINOR, BDB_REPLPLUGIN);
  2767. len = strlen(clVersion);
  2768. size = slapi_write_buffer (file, clVersion, len);
  2769. if (size != len)
  2770. {
  2771. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2772. "_cl5WriteDBVersion: failed to write DBVERSION; NSPR error - %d\n",
  2773. PR_GetError ());
  2774. PR_Close (file);
  2775. return CL5_SYSTEM_ERROR;
  2776. }
  2777. rc = PR_Close (file);
  2778. if (rc != PR_SUCCESS)
  2779. {
  2780. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2781. "_cl5WriteDBVersion: failed to close DBVERSION; NSPR error - %d\n",
  2782. PR_GetError ());
  2783. return CL5_SYSTEM_ERROR;
  2784. }
  2785. return CL5_SUCCESS;
  2786. }
  2787. /* must be called under the state lock */
  2788. static void _cl5Close ()
  2789. {
  2790. PRIntervalTime interval;
  2791. if (s_cl5Desc.dbState != CL5_STATE_CLOSED) /* Don't try to close twice */
  2792. {
  2793. /* cl5Close() set the state flag to CL5_STATE_CLOSING, which should
  2794. trigger all of the db housekeeping threads to exit, and which will
  2795. eventually cause no new update threads to start - so we wait here
  2796. for those other threads to finish before we proceed */
  2797. interval = PR_MillisecondsToInterval(100);
  2798. while (s_cl5Desc.threadCount > 0)
  2799. {
  2800. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name_cl,
  2801. "_cl5Close: waiting for threads to exit: %d thread(s) still active\n",
  2802. s_cl5Desc.threadCount);
  2803. DS_Sleep(interval);
  2804. }
  2805. /* There should now be no threads accessing any of the changelog databases -
  2806. it is safe to remove those databases */
  2807. _cl5DBClose ();
  2808. /* cleanup trimming */
  2809. _cl5TrimCleanup ();
  2810. /* remove changelog if requested */
  2811. if (s_cl5Desc.dbRmOnClose)
  2812. {
  2813. if (_cl5Delete (s_cl5Desc.dbDir, 1) != CL5_SUCCESS)
  2814. {
  2815. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2816. "cl5Close: failed to remove changelog\n");
  2817. }
  2818. s_cl5Desc.dbRmOnClose = PR_FALSE;
  2819. }
  2820. slapi_ch_free ((void **)&s_cl5Desc.dbDir);
  2821. memset (&s_cl5Desc.dbConfig, 0, sizeof (s_cl5Desc.dbConfig));
  2822. s_cl5Desc.fatalError = PR_FALSE;
  2823. s_cl5Desc.threadCount = 0;
  2824. s_cl5Desc.dbOpenMode = CL5_OPEN_NONE;
  2825. }
  2826. }
  2827. static void _cl5DBClose ()
  2828. {
  2829. if (NULL != s_cl5Desc.dbFiles)
  2830. {
  2831. Object *obj;
  2832. for (obj = objset_first_obj(s_cl5Desc.dbFiles); obj;
  2833. obj = objset_next_obj(s_cl5Desc.dbFiles, obj)) {
  2834. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2835. "_cl5DBClose: deleting DB object %p\n", obj);
  2836. }
  2837. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2838. "_cl5DBClose: closing databases in %s\n", s_cl5Desc.dbDir);
  2839. objset_delete (&s_cl5Desc.dbFiles);
  2840. }
  2841. if (NULL != s_cl5Desc.fileLock)
  2842. {
  2843. PR_DestroyLock (s_cl5Desc.fileLock);
  2844. s_cl5Desc.fileLock = NULL;
  2845. }
  2846. }
  2847. /* see if the given file is a changelog db file */
  2848. static int
  2849. _cl5IsDbFile(const char *fname)
  2850. {
  2851. if (!fname || !*fname) {
  2852. return 0;
  2853. }
  2854. if (!strcmp(fname, VERSION_FILE)) {
  2855. return 1;
  2856. }
  2857. if (_cl5FileEndsWith(fname, DB_EXTENSION)) {
  2858. return 1;
  2859. }
  2860. return 0; /* not a filename we recognize as being associated with the db */
  2861. }
  2862. /* state lock must be locked */
  2863. static int _cl5Delete (const char *clDir, int rmDir)
  2864. {
  2865. PRDir *dir;
  2866. char filename[MAXPATHLEN + 1];
  2867. PRDirEntry *entry = NULL;
  2868. int rc;
  2869. int dirisempty = 1;
  2870. /* remove all files in the directory and the directory */
  2871. dir = PR_OpenDir(clDir);
  2872. if (dir == NULL)
  2873. {
  2874. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2875. "_cl5Delete: failed to open changelog dir; NSPR error - %d\n",
  2876. PR_GetError ());
  2877. return CL5_SYSTEM_ERROR;
  2878. }
  2879. while (NULL != (entry = PR_ReadDir(dir, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
  2880. {
  2881. if (NULL == entry->name)
  2882. {
  2883. break;
  2884. }
  2885. if (!_cl5IsDbFile(entry->name)) {
  2886. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2887. "_cl5Delete: Skipping file [%s/%s] because it is not a changelogdb file.\n",
  2888. clDir, entry->name);
  2889. dirisempty = 0; /* skipped at least one file - dir not empty */
  2890. continue;
  2891. }
  2892. PR_snprintf(filename, MAXPATHLEN, "%s/%s", clDir, entry->name);
  2893. /* _cl5Delete deletes the whole changelog directory with all the files
  2894. * underneath. Thus, we can just remove them physically. */
  2895. if (0 == strcmp(entry->name, VERSION_FILE)) {
  2896. /* DBVERSION */
  2897. rc = PR_Delete(filename) != PR_SUCCESS;
  2898. if (PR_SUCCESS != rc) {
  2899. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2900. "_cl5Delete: failed to remove \"%s\"; NSPR error - %d\n",
  2901. filename, PR_GetError ());
  2902. }
  2903. } else {
  2904. /* DB files */
  2905. rc = s_cl5Desc.dbEnv->dbremove(s_cl5Desc.dbEnv, 0, filename, 0,
  2906. DEFAULT_DB_ENV_OP_FLAGS);
  2907. if (rc) {
  2908. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  2909. "_cl5Delete: failed to remove \"%s\"; "
  2910. "libdb error - %d (%s)\n",
  2911. filename, rc, db_strerror(rc));
  2912. }
  2913. }
  2914. }
  2915. rc = PR_CloseDir(dir);
  2916. if (rc != PR_SUCCESS)
  2917. {
  2918. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2919. "_cl5Delete: failed to close changelog dir (%s); NSPR error - %d\n",
  2920. clDir, PR_GetError ());
  2921. return CL5_SYSTEM_ERROR;
  2922. }
  2923. if (rmDir && dirisempty)
  2924. {
  2925. rc = PR_RmDir (clDir);
  2926. if (rc != 0)
  2927. {
  2928. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2929. "_cl5Delete: failed to remove changelog dir (%s); errno = %d\n",
  2930. clDir, errno);
  2931. return CL5_SYSTEM_ERROR;
  2932. }
  2933. } else if (rmDir && !dirisempty) {
  2934. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2935. "_cl5Delete: changelog dir (%s) is not empty - cannot remove\n",
  2936. clDir);
  2937. }
  2938. /* invalidate the clcache */
  2939. clcache_destroy();
  2940. return CL5_SUCCESS;
  2941. }
  2942. static void _cl5SetDefaultDBConfig ()
  2943. {
  2944. s_cl5Desc.dbConfig.maxConcurrentWrites= CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  2945. s_cl5Desc.dbConfig.fileMode = FILE_CREATE_MODE;
  2946. }
  2947. static void _cl5SetDBConfig (const CL5DBConfig *config)
  2948. {
  2949. /* s_cl5Desc.dbConfig.pageSize is retrieved from backend */
  2950. /* Some other configuration parameters are hardcoded... */
  2951. s_cl5Desc.dbConfig.maxConcurrentWrites = config->maxConcurrentWrites;
  2952. s_cl5Desc.dbConfig.fileMode = FILE_CREATE_MODE;
  2953. }
  2954. /* Trimming helper functions */
  2955. static int _cl5TrimInit ()
  2956. {
  2957. /* just create the lock while we are singlethreaded */
  2958. s_cl5Desc.dbTrim.lock = PR_NewLock();
  2959. if (s_cl5Desc.dbTrim.lock == NULL)
  2960. {
  2961. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  2962. "_cl5InitTrimming: failed to create lock; NSPR error - %d\n",
  2963. PR_GetError ());
  2964. return CL5_SYSTEM_ERROR;
  2965. }
  2966. else
  2967. {
  2968. return CL5_SUCCESS;
  2969. }
  2970. }
  2971. static void _cl5TrimCleanup ()
  2972. {
  2973. if (s_cl5Desc.dbTrim.lock)
  2974. PR_DestroyLock (s_cl5Desc.dbTrim.lock);
  2975. memset (&s_cl5Desc.dbTrim, 0, sizeof (s_cl5Desc.dbTrim));
  2976. }
  2977. static int _cl5TrimMain (void *param)
  2978. {
  2979. PRIntervalTime interval;
  2980. time_t timePrev = current_time ();
  2981. time_t timeNow;
  2982. PR_AtomicIncrement (&s_cl5Desc.threadCount);
  2983. interval = PR_SecondsToInterval(CHANGELOGDB_TRIM_INTERVAL);
  2984. while (s_cl5Desc.dbState != CL5_STATE_CLOSING)
  2985. {
  2986. timeNow = current_time ();
  2987. if (timeNow - timePrev >= CHANGELOGDB_TRIM_INTERVAL)
  2988. {
  2989. /* time to trim */
  2990. timePrev = timeNow;
  2991. _cl5DoTrimming (0 /* there's no cleaned rid */);
  2992. }
  2993. if (NULL == s_cl5Desc.clLock)
  2994. {
  2995. /* most likely, emergency */
  2996. break;
  2997. }
  2998. PR_Lock(s_cl5Desc.clLock);
  2999. PR_WaitCondVar(s_cl5Desc.clCvar, interval);
  3000. PR_Unlock(s_cl5Desc.clLock);
  3001. }
  3002. PR_AtomicDecrement (&s_cl5Desc.threadCount);
  3003. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5TrimMain: exiting\n");
  3004. return 0;
  3005. }
  3006. /*
  3007. * We remove an entry if it has been replayed to all consumers and the number
  3008. * of entries in the changelog is larger than maxEntries or age of the entry
  3009. * is larger than maxAge. Also we can't purge entries which correspond to max
  3010. * csns in the supplier's ruv. Here is a example where we can get into trouble:
  3011. *
  3012. * The server is setup with time based trimming and no consumer's
  3013. * At some point all the entries are trimmed from the changelog.
  3014. * At a later point a consumer is added and initialized online.
  3015. * Then a change is made on the supplier.
  3016. * To update the consumer, the supplier would attempt to locate the last
  3017. * change sent to the consumer in the changelog and will fail because the
  3018. * change was removed.
  3019. */
  3020. static void _cl5DoTrimming ()
  3021. {
  3022. Object *obj;
  3023. long numToTrim;
  3024. PR_Lock (s_cl5Desc.dbTrim.lock);
  3025. /*
  3026. * We are trimming all the changelogs. We trim file by file which
  3027. * means that some files will be trimmed more often than other. We
  3028. * might have to fix that by, for example, randomizing the starting
  3029. * point.
  3030. */
  3031. obj = objset_first_obj (s_cl5Desc.dbFiles);
  3032. while (obj && _cl5CanTrim ((time_t)0, &numToTrim))
  3033. {
  3034. _cl5TrimFile (obj, &numToTrim);
  3035. obj = objset_next_obj (s_cl5Desc.dbFiles, obj);
  3036. }
  3037. if (obj)
  3038. object_release (obj);
  3039. PR_Unlock (s_cl5Desc.dbTrim.lock);
  3040. return;
  3041. }
  3042. /*
  3043. * We are purging a changelog after a cleanAllRUV task. Find the specific
  3044. * changelog for the backend that is being cleaned, and purge all the records
  3045. * with the cleaned rid.
  3046. */
  3047. static void _cl5DoPurging (cleanruv_purge_data *purge_data)
  3048. {
  3049. ReplicaId rid = purge_data->cleaned_rid;
  3050. const Slapi_DN *suffix_sdn = purge_data->suffix_sdn;
  3051. const char *replName = purge_data->replName;
  3052. char *replGen = purge_data->replGen;
  3053. char *fileName;
  3054. Object *obj;
  3055. PR_Lock (s_cl5Desc.dbTrim.lock);
  3056. fileName = _cl5MakeFileName (replName, replGen);
  3057. obj = objset_find(s_cl5Desc.dbFiles, _cl5CompareDBFile, fileName);
  3058. if (obj) {
  3059. /* We found our changelog, now purge it */
  3060. _cl5PurgeRID (obj, rid);
  3061. object_release (obj);
  3062. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3063. "_cl5DoPurging - Purged rid (%d) from suffix (%s)\n",
  3064. rid, slapi_sdn_get_dn(suffix_sdn));
  3065. } else {
  3066. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3067. "_cl5DoPurging - Purge rid (%d) failed to find changelog file (%s) for suffix (%s)\n",
  3068. rid, fileName, slapi_sdn_get_dn(suffix_sdn));
  3069. }
  3070. PR_Unlock (s_cl5Desc.dbTrim.lock);
  3071. return;
  3072. }
  3073. /*
  3074. * If the rid is not set it is the very first iteration of the changelog.
  3075. * If the rid is set, we are doing another pass, and we have a key as our
  3076. * starting point.
  3077. */
  3078. static int
  3079. _cl5PurgeGetFirstEntry(Object *obj, CL5Entry *entry, void **iterator, DB_TXN *txnid, int rid, DBT *key)
  3080. {
  3081. DBC *cursor = NULL;
  3082. DBT data = {0};
  3083. CL5Iterator *it;
  3084. CL5DBFile *file;
  3085. int rc;
  3086. file = (CL5DBFile*)object_get_data (obj);
  3087. /* create cursor */
  3088. rc = file->db->cursor(file->db, txnid, &cursor, 0);
  3089. if (rc != 0)
  3090. {
  3091. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3092. "_cl5PurgeGetFirstEntry: failed to create cursor; db error - %d %s\n", rc, db_strerror(rc));
  3093. rc = CL5_DB_ERROR;
  3094. goto done;
  3095. }
  3096. key->flags = DB_DBT_MALLOC;
  3097. data.flags = DB_DBT_MALLOC;
  3098. while ((rc = cursor->c_get(cursor, key, &data, rid?DB_SET:DB_NEXT)) == 0)
  3099. {
  3100. /* skip service entries on the first pass (rid == 0)*/
  3101. if (!rid && cl5HelperEntry ((char*)key->data, NULL))
  3102. {
  3103. slapi_ch_free(&key->data);
  3104. slapi_ch_free(&(data.data));
  3105. continue;
  3106. }
  3107. /* format entry */
  3108. rc = cl5DBData2Entry(data.data, data.size, entry);
  3109. slapi_ch_free(&(data.data));
  3110. if (rc != 0)
  3111. {
  3112. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3113. "_cl5PurgeGetFirstEntry: failed to format entry: %d\n", rc);
  3114. goto done;
  3115. }
  3116. it = (CL5Iterator*)slapi_ch_malloc(sizeof (CL5Iterator));
  3117. it->cursor = cursor;
  3118. object_acquire (obj);
  3119. it->file = obj;
  3120. *(CL5Iterator**)iterator = it;
  3121. return CL5_SUCCESS;
  3122. }
  3123. slapi_ch_free(&key->data);
  3124. slapi_ch_free(&(data.data));
  3125. /* walked of the end of the file */
  3126. if (rc == DB_NOTFOUND)
  3127. {
  3128. rc = CL5_NOTFOUND;
  3129. goto done;
  3130. }
  3131. /* db error occured while iterating */
  3132. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3133. "_cl5PurgeGetFirstEntry: failed to get entry; db error - %d %s\n",
  3134. rc, db_strerror(rc));
  3135. rc = CL5_DB_ERROR;
  3136. done:
  3137. /*
  3138. * We didn't success in assigning this cursor to the iterator,
  3139. * so we need to free the cursor here.
  3140. */
  3141. if (cursor)
  3142. cursor->c_close(cursor);
  3143. return rc;
  3144. }
  3145. /*
  3146. * Get the next entry. If we get a lock error we will restart the process
  3147. * starting at the current key.
  3148. */
  3149. static int
  3150. _cl5PurgeGetNextEntry (CL5Entry *entry, void *iterator, DBT *key)
  3151. {
  3152. CL5Iterator *it;
  3153. DBT data={0};
  3154. int rc;
  3155. it = (CL5Iterator*) iterator;
  3156. key->flags = DB_DBT_MALLOC;
  3157. data.flags = DB_DBT_MALLOC;
  3158. while ((rc = it->cursor->c_get(it->cursor, key, &data, DB_NEXT)) == 0)
  3159. {
  3160. if (cl5HelperEntry ((char*)key->data, NULL))
  3161. {
  3162. slapi_ch_free(&key->data);
  3163. slapi_ch_free(&(data.data));
  3164. continue;
  3165. }
  3166. /* format entry */
  3167. rc = cl5DBData2Entry (data.data, data.size, entry);
  3168. slapi_ch_free (&(data.data));
  3169. if (rc != 0)
  3170. {
  3171. if (rc != CL5_DB_LOCK_ERROR){
  3172. /* Not a lock error, free the key */
  3173. slapi_ch_free(&key->data);
  3174. }
  3175. slapi_log_error(rc == CL5_DB_LOCK_ERROR?SLAPI_LOG_REPL:SLAPI_LOG_FATAL,
  3176. repl_plugin_name_cl,
  3177. "_cl5PurgeGetNextEntry: failed to format entry: %d\n",
  3178. rc);
  3179. }
  3180. return rc;
  3181. }
  3182. slapi_ch_free(&(data.data));
  3183. /* walked of the end of the file or entry is out of range */
  3184. if (rc == 0 || rc == DB_NOTFOUND){
  3185. slapi_ch_free(&key->data);
  3186. return CL5_NOTFOUND;
  3187. }
  3188. if (rc != CL5_DB_LOCK_ERROR){
  3189. /* Not a lock error, free the key */
  3190. slapi_ch_free(&key->data);
  3191. }
  3192. /* cursor operation failed */
  3193. slapi_log_error(rc == CL5_DB_LOCK_ERROR?SLAPI_LOG_REPL:SLAPI_LOG_FATAL,
  3194. repl_plugin_name_cl,
  3195. "_cl5PurgeGetNextEntry: failed to get entry; db error - %d %s\n",
  3196. rc, db_strerror(rc));
  3197. return rc;
  3198. }
  3199. #define MAX_RETRIES 10
  3200. /*
  3201. * _cl5PurgeRID(Object *obj, ReplicaId cleaned_rid)
  3202. *
  3203. * Clean the entire changelog of updates from the "cleaned rid" via CLEANALLRUV
  3204. * Delete entries in batches so we don't consume too many db locks, and we don't
  3205. * lockup the changelog during the entire purging process using one transaction.
  3206. * We save the key from the last iteration so we don't have to start from the
  3207. * beginning for each new iteration.
  3208. */
  3209. static void
  3210. _cl5PurgeRID(Object *obj, ReplicaId cleaned_rid)
  3211. {
  3212. slapi_operation_parameters op = {0};
  3213. ReplicaId csn_rid;
  3214. CL5Entry entry;
  3215. DB_TXN *txnid = NULL;
  3216. DBT key = {0};
  3217. void *iterator = NULL;
  3218. long totalTrimmed = 0;
  3219. long trimmed = 0;
  3220. char *starting_key = NULL;
  3221. int batch_count = 0;
  3222. int db_lock_retry_count = 0;
  3223. int first_pass = 1;
  3224. int finished = 0;
  3225. int rc = 0;
  3226. PR_ASSERT (obj);
  3227. entry.op = &op;
  3228. /*
  3229. * Keep processing the changelog until we are done, shutting down, or we
  3230. * maxed out on the db lock retries.
  3231. */
  3232. while (!finished && db_lock_retry_count < MAX_RETRIES && !slapi_is_shutting_down()){
  3233. trimmed = 0;
  3234. /*
  3235. * Sleep a bit to allow others to use the changelog - we can't hog the
  3236. * changelog for the entire purge.
  3237. */
  3238. DS_Sleep(PR_MillisecondsToInterval(100));
  3239. rc = TXN_BEGIN(s_cl5Desc.dbEnv, NULL, &txnid, 0);
  3240. if (rc != 0){
  3241. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3242. "_cl5PurgeRID: failed to begin transaction; db error - %d %s. "
  3243. "Changelog was not purged of rid(%d)\n",
  3244. rc, db_strerror(rc), cleaned_rid);
  3245. return;
  3246. }
  3247. /*
  3248. * Check every changelog entry for the cleaned rid
  3249. */
  3250. rc = _cl5PurgeGetFirstEntry(obj, &entry, &iterator, txnid, first_pass?0:cleaned_rid, &key);
  3251. first_pass = 0;
  3252. while (rc == CL5_SUCCESS && !slapi_is_shutting_down()) {
  3253. /*
  3254. * Store the new starting key - we need this starting key in case
  3255. * we run out of locks and have to start the transaction over.
  3256. */
  3257. slapi_ch_free_string(&starting_key);
  3258. starting_key = slapi_ch_strdup((char*)key.data);
  3259. if(trimmed == 10000 || (batch_count && trimmed == batch_count)){
  3260. /*
  3261. * Break out, and commit these deletes. Do not free the key,
  3262. * we need it for the next pass.
  3263. */
  3264. cl5_operation_parameters_done (&op);
  3265. db_lock_retry_count = 0; /* reset the retry count */
  3266. break;
  3267. }
  3268. if(op.csn){
  3269. csn_rid = csn_get_replicaid (op.csn);
  3270. if (csn_rid == cleaned_rid){
  3271. rc = _cl5CurrentDeleteEntry (iterator);
  3272. if (rc != CL5_SUCCESS){
  3273. /* log error */
  3274. cl5_operation_parameters_done (&op);
  3275. if (rc == CL5_DB_LOCK_ERROR){
  3276. /*
  3277. * Ran out of locks, need to restart the transaction.
  3278. * Reduce the the batch count and reset the key to
  3279. * the starting point
  3280. */
  3281. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3282. "_cl5PurgeRID: Ran out of db locks deleting entry. "
  3283. "Reduce the batch value and restart.\n");
  3284. batch_count = trimmed - 10;
  3285. if (batch_count < 10){
  3286. batch_count = 10;
  3287. }
  3288. trimmed = 0;
  3289. slapi_ch_free(&(key.data));
  3290. key.data = starting_key;
  3291. starting_key = NULL;
  3292. db_lock_retry_count++;
  3293. break;
  3294. } else {
  3295. /* fatal error */
  3296. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3297. "_cl5PurgeRID: fatal error (%d)\n", rc);
  3298. slapi_ch_free(&(key.data));
  3299. finished = 1;
  3300. break;
  3301. }
  3302. }
  3303. trimmed++;
  3304. }
  3305. }
  3306. slapi_ch_free(&(key.data));
  3307. cl5_operation_parameters_done (&op);
  3308. rc = _cl5PurgeGetNextEntry (&entry, iterator, &key);
  3309. if (rc == CL5_DB_LOCK_ERROR){
  3310. /*
  3311. * Ran out of locks, need to restart the transaction.
  3312. * Reduce the the batch count and reset the key to the starting
  3313. * point.
  3314. */
  3315. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3316. "_cl5PurgeRID: Ran out of db locks getting the next entry. "
  3317. "Reduce the batch value and restart.\n");
  3318. batch_count = trimmed - 10;
  3319. if (batch_count < 10){
  3320. batch_count = 10;
  3321. }
  3322. trimmed = 0;
  3323. cl5_operation_parameters_done (&op);
  3324. slapi_ch_free(&(key.data));
  3325. key.data = starting_key;
  3326. starting_key = NULL;
  3327. db_lock_retry_count++;
  3328. break;
  3329. }
  3330. }
  3331. if (rc == CL5_NOTFOUND){
  3332. /* Scanned the entire changelog, we're done */
  3333. finished = 1;
  3334. }
  3335. /* Destroy the iterator before we finish with the txn */
  3336. cl5DestroyIterator (iterator);
  3337. /*
  3338. * Commit or abort the txn
  3339. */
  3340. if (rc == CL5_SUCCESS || rc == CL5_NOTFOUND){
  3341. rc = TXN_COMMIT (txnid, 0);
  3342. if (rc != 0){
  3343. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3344. "_cl5PurgeRID: failed to commit transaction; db error - %d %s. "
  3345. "Changelog was not completely purged of rid (%d)\n",
  3346. rc, db_strerror(rc), cleaned_rid);
  3347. break;
  3348. } else if (finished){
  3349. /* We're done */
  3350. totalTrimmed += trimmed;
  3351. break;
  3352. } else {
  3353. /* Not done yet */
  3354. totalTrimmed += trimmed;
  3355. trimmed = 0;
  3356. }
  3357. } else {
  3358. rc = TXN_ABORT (txnid);
  3359. if (rc != 0){
  3360. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3361. "_cl5PurgeRID: failed to abort transaction; db error - %d %s. "
  3362. "Changelog was not completely purged of rid (%d)\n",
  3363. rc, db_strerror(rc), cleaned_rid);
  3364. }
  3365. if (batch_count == 0){
  3366. /* This was not a retry. Fatal error, break out */
  3367. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3368. "_cl5PurgeRID: Changelog was not purged of rid (%d)\n",
  3369. cleaned_rid);
  3370. break;
  3371. }
  3372. }
  3373. }
  3374. slapi_ch_free_string(&starting_key);
  3375. slapi_log_error (SLAPI_LOG_REPL, repl_plugin_name_cl,
  3376. "_cl5PurgeRID: Removed (%ld entries) that originated from rid (%d)\n",
  3377. totalTrimmed, cleaned_rid);
  3378. }
  3379. /* Note that each file contains changes for a single replicated area.
  3380. trimming algorithm:
  3381. */
  3382. #define CL5_TRIM_MAX_PER_TRANSACTION 10
  3383. static void _cl5TrimFile (Object *obj, long *numToTrim)
  3384. {
  3385. DB_TXN *txnid;
  3386. RUV *ruv = NULL;
  3387. CL5Entry entry;
  3388. slapi_operation_parameters op = {0};
  3389. ReplicaId csn_rid;
  3390. void *it;
  3391. int finished = 0, totalTrimmed = 0, count;
  3392. PRBool abort;
  3393. char strCSN[CSN_STRSIZE];
  3394. int rc;
  3395. PR_ASSERT (obj);
  3396. /* construct the ruv up to which we can purge */
  3397. rc = _cl5GetRUV2Purge2 (obj, &ruv);
  3398. if (rc != CL5_SUCCESS || ruv == NULL)
  3399. {
  3400. return;
  3401. }
  3402. entry.op = &op;
  3403. while ( !finished && !slapi_is_shutting_down() )
  3404. {
  3405. it = NULL;
  3406. count = 0;
  3407. txnid = NULL;
  3408. abort = PR_FALSE;
  3409. /* DB txn lock accessed pages until the end of the transaction. */
  3410. rc = TXN_BEGIN(s_cl5Desc.dbEnv, NULL, &txnid, 0);
  3411. if (rc != 0)
  3412. {
  3413. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3414. "_cl5TrimFile: failed to begin transaction; db error - %d %s\n",
  3415. rc, db_strerror(rc));
  3416. finished = PR_TRUE;
  3417. break;
  3418. }
  3419. finished = _cl5GetFirstEntry (obj, &entry, &it, txnid);
  3420. while ( !finished && !slapi_is_shutting_down())
  3421. {
  3422. /*
  3423. * This change can be trimmed if it exceeds purge
  3424. * parameters and has been seen by all consumers.
  3425. */
  3426. if(op.csn == NULL){
  3427. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "_cl5TrimFile: "
  3428. "Operation missing csn, moving on to next entry.\n");
  3429. cl5_operation_parameters_done (&op);
  3430. finished =_cl5GetNextEntry (&entry, it);
  3431. continue;
  3432. }
  3433. csn_rid = csn_get_replicaid (op.csn);
  3434. if ( (*numToTrim > 0 || _cl5CanTrim (entry.time, numToTrim)) &&
  3435. ruv_covers_csn_strict (ruv, op.csn) )
  3436. {
  3437. rc = _cl5CurrentDeleteEntry (it);
  3438. if ( rc == CL5_SUCCESS)
  3439. {
  3440. rc = _cl5UpdateRUV (obj, op.csn, PR_FALSE, PR_TRUE);
  3441. }
  3442. if ( rc == CL5_SUCCESS)
  3443. {
  3444. if (*numToTrim > 0) (*numToTrim)--;
  3445. count++;
  3446. }
  3447. else
  3448. {
  3449. /* The above two functions have logged the error */
  3450. abort = PR_TRUE;
  3451. }
  3452. }
  3453. else
  3454. {
  3455. /* The changelog DB is time ordered. If we can not trim
  3456. * a CSN, we will not be allowed to trim the rest of the
  3457. * CSNs generally. However, the maxcsn of each replica ID
  3458. * is always kept in the changelog as an anchor for
  3459. * replaying future changes. We have to skip those anchor
  3460. * CSNs, otherwise a non-active replica ID could block
  3461. * the trim forever.
  3462. */
  3463. CSN *maxcsn = NULL;
  3464. ruv_get_largest_csn_for_replica (ruv, csn_rid, &maxcsn);
  3465. if ( csn_compare (op.csn, maxcsn) != 0 )
  3466. {
  3467. /* op.csn is not anchor CSN */
  3468. finished = 1;
  3469. }
  3470. else
  3471. {
  3472. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  3473. slapi_log_error (SLAPI_LOG_REPL, NULL,
  3474. "Changelog purge skipped anchor csn %s\n",
  3475. csn_as_string (maxcsn, PR_FALSE, strCSN));
  3476. }
  3477. /* extra read to skip the current record */
  3478. cl5_operation_parameters_done (&op);
  3479. finished =_cl5GetNextEntry (&entry, it);
  3480. }
  3481. if (maxcsn) csn_free (&maxcsn);
  3482. }
  3483. cl5_operation_parameters_done (&op);
  3484. if (finished || abort || count >= CL5_TRIM_MAX_PER_TRANSACTION)
  3485. {
  3486. /* If we reach CL5_TRIM_MAX_PER_TRANSACTION,
  3487. * we close the cursor,
  3488. * commit the transaction and restart a new transaction
  3489. */
  3490. break;
  3491. }
  3492. finished = _cl5GetNextEntry (&entry, it);
  3493. }
  3494. /* MAB: We need to close the cursor BEFORE the txn commits/aborts.
  3495. * If we don't respect this order, we'll screw up the database,
  3496. * placing it in DB_RUNRECOVERY mode
  3497. */
  3498. cl5DestroyIterator (it);
  3499. if (abort)
  3500. {
  3501. finished = 1;
  3502. rc = TXN_ABORT (txnid);
  3503. if (rc != 0)
  3504. {
  3505. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3506. "_cl5TrimFile: failed to abort transaction; db error - %d %s\n",
  3507. rc, db_strerror(rc));
  3508. }
  3509. }
  3510. else
  3511. {
  3512. rc = TXN_COMMIT (txnid, 0);
  3513. if (rc != 0)
  3514. {
  3515. finished = 1;
  3516. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3517. "_cl5TrimFile: failed to commit transaction; db error - %d %s\n",
  3518. rc, db_strerror(rc));
  3519. }
  3520. else
  3521. {
  3522. totalTrimmed += count;
  3523. }
  3524. }
  3525. } /* While (!finished) */
  3526. if (ruv)
  3527. ruv_destroy (&ruv);
  3528. if (totalTrimmed)
  3529. {
  3530. slapi_log_error (SLAPI_LOG_REPL, NULL, "Trimmed %d changes from the changelog\n", totalTrimmed);
  3531. }
  3532. }
  3533. static PRBool _cl5CanTrim (time_t time, long *numToTrim)
  3534. {
  3535. *numToTrim = 0;
  3536. if (s_cl5Desc.dbTrim.maxAge == 0 && s_cl5Desc.dbTrim.maxEntries == 0) {
  3537. return PR_FALSE;
  3538. }
  3539. if (s_cl5Desc.dbTrim.maxAge == 0)
  3540. {
  3541. *numToTrim = cl5GetOperationCount (NULL) - s_cl5Desc.dbTrim.maxEntries;
  3542. return ( *numToTrim > 0 );
  3543. }
  3544. if (s_cl5Desc.dbTrim.maxEntries > 0 &&
  3545. (*numToTrim = cl5GetOperationCount (NULL) - s_cl5Desc.dbTrim.maxEntries) > 0) {
  3546. return PR_TRUE;
  3547. }
  3548. if (time) {
  3549. return (current_time () - time > s_cl5Desc.dbTrim.maxAge);
  3550. } else {
  3551. return PR_TRUE;
  3552. }
  3553. }
  3554. static int _cl5ReadRUV (const char *replGen, Object *obj, PRBool purge)
  3555. {
  3556. int rc;
  3557. char csnStr [CSN_STRSIZE];
  3558. DBT key={0}, data={0};
  3559. struct berval **vals = NULL;
  3560. CL5DBFile *file;
  3561. char *pos;
  3562. char *agmt_name;
  3563. PR_ASSERT (replGen && obj);
  3564. file = (CL5DBFile*)object_get_data (obj);
  3565. PR_ASSERT (file);
  3566. agmt_name = get_thread_private_agmtname();
  3567. if (purge) { /* read purge vector entry */
  3568. key.data = _cl5GetHelperEntryKey (PURGE_RUV_TIME, csnStr);
  3569. } else { /* read upper bound vector */
  3570. key.data = _cl5GetHelperEntryKey (MAX_RUV_TIME, csnStr);
  3571. }
  3572. key.size = CSN_STRSIZE;
  3573. data.flags = DB_DBT_MALLOC;
  3574. rc = file->db->get(file->db, NULL/*txn*/, &key, &data, 0);
  3575. switch (rc)
  3576. {
  3577. case 0: pos = data.data;
  3578. rc = _cl5ReadBervals (&vals, &pos, data.size);
  3579. slapi_ch_free (&(data.data));
  3580. if (rc != CL5_SUCCESS)
  3581. goto done;
  3582. if (purge) {
  3583. rc = ruv_init_from_bervals(vals, &file->purgeRUV);
  3584. } else {
  3585. rc = ruv_init_from_bervals(vals, &file->maxRUV);
  3586. }
  3587. if (rc != RUV_SUCCESS)
  3588. {
  3589. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3590. "%s: _cl5ReadRUV: failed to initialize %s ruv; "
  3591. "RUV error %d\n", agmt_name, purge? "purge" : "upper bound", rc);
  3592. rc = CL5_RUV_ERROR;
  3593. goto done;
  3594. }
  3595. /* delete the entry; it is re-added when file
  3596. is successfully closed */
  3597. file->db->del (file->db, NULL, &key, DEFAULT_DB_OP_FLAGS(NULL));
  3598. rc = CL5_SUCCESS;
  3599. goto done;
  3600. case DB_NOTFOUND: /* RUV is lost - need to construct */
  3601. rc = _cl5ConstructRUV (replGen, obj, purge);
  3602. goto done;
  3603. default: slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3604. "%s: _cl5ReadRUV: failed to get purge RUV; "
  3605. "db error - %d %s\n", agmt_name, rc, db_strerror(rc));
  3606. rc = CL5_DB_ERROR;
  3607. goto done;
  3608. }
  3609. done:
  3610. ber_bvecfree(vals);
  3611. return rc;
  3612. }
  3613. static int _cl5WriteRUV (CL5DBFile *file, PRBool purge)
  3614. {
  3615. int rc;
  3616. DBT key={0}, data={0};
  3617. char csnStr [CSN_STRSIZE];
  3618. struct berval **vals;
  3619. DB_TXN *txnid = NULL;
  3620. char *buff;
  3621. if ((purge && file->purgeRUV == NULL) || (!purge && file->maxRUV == NULL))
  3622. return CL5_SUCCESS;
  3623. if (purge)
  3624. {
  3625. /* Set the minimum CSN of each vector to a dummy CSN that contains
  3626. * just a replica ID, e.g. 00000000000000010000.
  3627. * The minimum CSN in a purge RUV is not used so the value doesn't
  3628. * matter, but it needs to be set to something so that it can be
  3629. * flushed to changelog at shutdown and parsed at startup with the
  3630. * regular string-to-RUV parsing routines. */
  3631. ruv_insert_dummy_min_csn(file->purgeRUV);
  3632. key.data = _cl5GetHelperEntryKey (PURGE_RUV_TIME, csnStr);
  3633. rc = ruv_to_bervals(file->purgeRUV, &vals);
  3634. }
  3635. else
  3636. {
  3637. key.data = _cl5GetHelperEntryKey (MAX_RUV_TIME, csnStr);
  3638. rc = ruv_to_bervals(file->maxRUV, &vals);
  3639. }
  3640. key.size = CSN_STRSIZE;
  3641. rc = _cl5WriteBervals (vals, &buff, &data.size);
  3642. data.data = buff;
  3643. ber_bvecfree(vals);
  3644. if (rc != CL5_SUCCESS)
  3645. {
  3646. return rc;
  3647. }
  3648. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  3649. rc = txn_begin(s_cl5Desc.dbEnv, NULL, &txnid, 0);
  3650. if (rc != 0)
  3651. {
  3652. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3653. "_cl5WriteRUV: failed to begin transaction; db error - %d %s\n",
  3654. rc, db_strerror(rc));
  3655. return CL5_DB_ERROR;
  3656. }
  3657. #endif
  3658. rc = file->db->put(file->db, txnid, &key, &data, DEFAULT_DB_OP_FLAGS(txnid));
  3659. slapi_ch_free (&(data.data));
  3660. if ( rc == 0 )
  3661. {
  3662. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  3663. rc = txn_commit (txnid, 0);
  3664. if (rc != 0)
  3665. {
  3666. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3667. "_cl5WriteRUV: failed to commit transaction; db error - %d %s\n",
  3668. rc, db_strerror(rc));
  3669. return CL5_DB_ERROR;
  3670. }
  3671. #endif
  3672. return CL5_SUCCESS;
  3673. }
  3674. else
  3675. {
  3676. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3677. "_cl5WriteRUV: failed to write %s RUV for file %s; db error - %d (%s)\n",
  3678. purge? "purge" : "upper bound", file->name, rc, db_strerror(rc));
  3679. if (CL5_OS_ERR_IS_DISKFULL(rc))
  3680. {
  3681. cl5_set_diskfull();
  3682. return CL5_DB_ERROR;
  3683. }
  3684. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  3685. rc = txn_abort (txnid);
  3686. if (rc != 0)
  3687. {
  3688. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3689. "_cl5WriteRUV: failed to abort transaction; db error - %d %s\n",
  3690. rc, db_strerror(rc));
  3691. }
  3692. #endif
  3693. return CL5_DB_ERROR;
  3694. }
  3695. }
  3696. /* This is a very slow process since we have to read every changelog entry.
  3697. Hopefully, this function is not called too often */
  3698. static int _cl5ConstructRUV (const char *replGen, Object *obj, PRBool purge)
  3699. {
  3700. int rc;
  3701. CL5Entry entry;
  3702. void *iterator = NULL;
  3703. slapi_operation_parameters op = {0};
  3704. CL5DBFile *file;
  3705. ReplicaId rid;
  3706. PR_ASSERT (replGen && obj);
  3707. file = (CL5DBFile*)object_get_data (obj);
  3708. PR_ASSERT (file);
  3709. /* construct the RUV */
  3710. if (purge)
  3711. rc = ruv_init_new (replGen, 0, NULL, &file->purgeRUV);
  3712. else
  3713. rc = ruv_init_new (replGen, 0, NULL, &file->maxRUV);
  3714. if (rc != RUV_SUCCESS)
  3715. {
  3716. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5ConstructRUV: "
  3717. "failed to initialize %s RUV for file %s; ruv error - %d\n",
  3718. purge? "purge" : "upper bound", file->name, rc);
  3719. return CL5_RUV_ERROR;
  3720. }
  3721. entry.op = &op;
  3722. rc = _cl5GetFirstEntry (obj, &entry, &iterator, NULL);
  3723. while (rc == CL5_SUCCESS)
  3724. {
  3725. if(op.csn){
  3726. rid = csn_get_replicaid (op.csn);
  3727. } else {
  3728. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "_cl5ConstructRUV: "
  3729. "Operation missing csn, moving on to next entry.\n");
  3730. cl5_operation_parameters_done (&op);
  3731. rc = _cl5GetNextEntry (&entry, iterator);
  3732. continue;
  3733. }
  3734. if(is_cleaned_rid(rid)){
  3735. /* skip this entry as the rid is invalid */
  3736. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5ConstructRUV: "
  3737. "skipping entry because its csn contains a cleaned rid(%d)\n", rid);
  3738. cl5_operation_parameters_done (&op);
  3739. rc = _cl5GetNextEntry (&entry, iterator);
  3740. continue;
  3741. }
  3742. if (purge)
  3743. rc = ruv_set_csns_keep_smallest(file->purgeRUV, op.csn);
  3744. else
  3745. rc = ruv_set_csns (file->maxRUV, op.csn, NULL);
  3746. cl5_operation_parameters_done (&op);
  3747. if (rc != RUV_SUCCESS)
  3748. {
  3749. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5ConstructRUV: "
  3750. "failed to updated %s RUV for file %s; ruv error - %d\n",
  3751. purge ? "purge" : "upper bound", file->name, rc);
  3752. rc = CL5_RUV_ERROR;
  3753. continue;
  3754. }
  3755. rc = _cl5GetNextEntry (&entry, iterator);
  3756. }
  3757. cl5_operation_parameters_done (&op);
  3758. if (iterator)
  3759. cl5DestroyIterator (iterator);
  3760. if (rc == CL5_NOTFOUND)
  3761. {
  3762. rc = CL5_SUCCESS;
  3763. }
  3764. else
  3765. {
  3766. if (purge)
  3767. ruv_destroy (&file->purgeRUV);
  3768. else
  3769. ruv_destroy (&file->maxRUV);
  3770. }
  3771. return rc;
  3772. }
  3773. static int _cl5UpdateRUV (Object *obj, CSN *csn, PRBool newReplica, PRBool purge)
  3774. {
  3775. ReplicaId rid;
  3776. int rc = RUV_SUCCESS; /* initialize rc to avoid erroneous logs */
  3777. CL5DBFile *file;
  3778. PR_ASSERT (obj && csn);
  3779. file = (CL5DBFile*)object_get_data (obj);
  3780. /*
  3781. * if purge is TRUE, file->purgeRUV must be set;
  3782. * if purge is FALSE, maxRUV must be set
  3783. */
  3784. PR_ASSERT (file && ((purge && file->purgeRUV) || (!purge && file->maxRUV)));
  3785. rid = csn_get_replicaid(csn);
  3786. /* update vector only if this replica is not yet part of RUV */
  3787. if (purge && newReplica)
  3788. {
  3789. if (ruv_contains_replica (file->purgeRUV, rid)){
  3790. return CL5_SUCCESS;
  3791. } else {
  3792. /* if the replica is not part of the purgeRUV yet, add it unless it's from a cleaned rid */
  3793. ruv_add_replica (file->purgeRUV, rid, multimaster_get_local_purl());
  3794. }
  3795. }
  3796. else
  3797. {
  3798. if (purge){
  3799. rc = ruv_set_csns(file->purgeRUV, csn, NULL);
  3800. } else {
  3801. rc = ruv_set_csns(file->maxRUV, csn, NULL);
  3802. }
  3803. }
  3804. if (rc != RUV_SUCCESS)
  3805. {
  3806. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5UpdatePurgeRUV: "
  3807. "failed to update %s RUV for file %s; ruv error - %d\n",
  3808. purge ? "purge" : "upper bound", file->name, rc);
  3809. return CL5_RUV_ERROR;
  3810. }
  3811. return CL5_SUCCESS;
  3812. }
  3813. static int _cl5EnumConsumerRUV (const ruv_enum_data *element, void *arg)
  3814. {
  3815. int rc;
  3816. RUV *ruv;
  3817. CSN *csn = NULL;
  3818. PR_ASSERT (element && element->csn && arg);
  3819. ruv = (RUV*)arg;
  3820. rc = ruv_get_largest_csn_for_replica(ruv, csn_get_replicaid (element->csn), &csn);
  3821. if (rc != RUV_SUCCESS || csn == NULL || csn_compare (element->csn, csn) < 0)
  3822. {
  3823. ruv_set_max_csn(ruv, element->csn, NULL);
  3824. }
  3825. if (csn)
  3826. csn_free (&csn);
  3827. return 0;
  3828. }
  3829. static int _cl5GetRUV2Purge2 (Object *fileObj, RUV **ruv)
  3830. {
  3831. int rc = CL5_SUCCESS;
  3832. CL5DBFile *dbFile;
  3833. Object *rObj = NULL;
  3834. Replica *r = NULL;
  3835. Object *agmtObj = NULL;
  3836. Repl_Agmt *agmt;
  3837. Object *consRUVObj, *supRUVObj;
  3838. RUV *consRUV, *supRUV;
  3839. CSN *csn;
  3840. PR_ASSERT (fileObj && ruv);
  3841. if (!ruv) {
  3842. rc = CL5_UNKNOWN_ERROR;
  3843. goto done;
  3844. }
  3845. dbFile = (CL5DBFile*)object_get_data (fileObj);
  3846. PR_ASSERT (dbFile);
  3847. rObj = replica_get_by_name (dbFile->replName);
  3848. PR_ASSERT (rObj);
  3849. if (!rObj) {
  3850. rc = CL5_NOTFOUND;
  3851. goto done;
  3852. }
  3853. r = (Replica*)object_get_data (rObj);
  3854. PR_ASSERT (r);
  3855. /* We start with this replica's RUV. See note in _cl5DoTrimming */
  3856. supRUVObj = replica_get_ruv (r);
  3857. PR_ASSERT (supRUVObj);
  3858. supRUV = (RUV*)object_get_data (supRUVObj);
  3859. PR_ASSERT (supRUV);
  3860. *ruv = ruv_dup (supRUV);
  3861. object_release (supRUVObj);
  3862. agmtObj = agmtlist_get_first_agreement_for_replica (r);
  3863. while (agmtObj)
  3864. {
  3865. agmt = (Repl_Agmt*)object_get_data (agmtObj);
  3866. PR_ASSERT (agmt);
  3867. if(!agmt_is_enabled(agmt)){
  3868. agmtObj = agmtlist_get_next_agreement_for_replica(r, agmtObj);
  3869. continue;
  3870. }
  3871. consRUVObj = agmt_get_consumer_ruv (agmt);
  3872. if (consRUVObj)
  3873. {
  3874. consRUV = (RUV*)object_get_data (consRUVObj);
  3875. rc = ruv_enumerate_elements (consRUV, _cl5EnumConsumerRUV, *ruv);
  3876. if (rc != RUV_SUCCESS)
  3877. {
  3878. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5GetRUV2Purge2: "
  3879. "failed to construct ruv; ruv error - %d\n", rc);
  3880. rc = CL5_RUV_ERROR;
  3881. object_release (consRUVObj);
  3882. object_release (agmtObj);
  3883. break;
  3884. }
  3885. object_release (consRUVObj);
  3886. }
  3887. agmtObj = agmtlist_get_next_agreement_for_replica (r, agmtObj);
  3888. }
  3889. /* check if there is any data in the constructed ruv - otherwise get rid of it */
  3890. if (ruv_get_max_csn(*ruv, &csn) != RUV_SUCCESS || csn == NULL)
  3891. {
  3892. ruv_destroy (ruv);
  3893. }
  3894. else
  3895. {
  3896. csn_free (&csn);
  3897. }
  3898. done:
  3899. if (rObj)
  3900. object_release (rObj);
  3901. if (rc != CL5_SUCCESS && ruv)
  3902. ruv_destroy (ruv);
  3903. return rc;
  3904. }
  3905. static int _cl5GetEntryCount (CL5DBFile *file)
  3906. {
  3907. int rc;
  3908. char csnStr [CSN_STRSIZE];
  3909. DBT key={0}, data={0};
  3910. DB_BTREE_STAT *stats = NULL;
  3911. PR_ASSERT (file);
  3912. /* read entry count. if the entry is there - the file was successfully closed
  3913. last time it was used */
  3914. key.data = _cl5GetHelperEntryKey (ENTRY_COUNT_TIME, csnStr);
  3915. key.size = CSN_STRSIZE;
  3916. data.flags = DB_DBT_MALLOC;
  3917. rc = file->db->get(file->db, NULL/*txn*/, &key, &data, 0);
  3918. switch (rc)
  3919. {
  3920. case 0: file->entryCount = *(int*)data.data;
  3921. slapi_ch_free (&(data.data));
  3922. /* delete the entry. the entry is re-added when file
  3923. is successfully closed */
  3924. file->db->del (file->db, NULL, &key, DEFAULT_DB_OP_FLAGS(NULL));
  3925. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3926. "_cl5GetEntryCount: %d changes for replica %s\n",
  3927. file->entryCount, file->replName);
  3928. return CL5_SUCCESS;
  3929. case DB_NOTFOUND: file->entryCount = 0;
  3930. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
  3931. rc = file->db->stat(file->db, NULL, (void*)&stats, 0);
  3932. #elif 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3300
  3933. rc = file->db->stat(file->db, (void*)&stats, 0);
  3934. #else
  3935. rc = file->db->stat(file->db, (void*)&stats, (void *)slapi_ch_malloc, 0);
  3936. #endif
  3937. if (rc != 0)
  3938. {
  3939. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3940. "_cl5GetEntryCount: failed to get changelog statistics; "
  3941. "db error - %d %s\n", rc, db_strerror(rc));
  3942. return CL5_DB_ERROR;
  3943. }
  3944. #ifdef DB30
  3945. file->entryCount = stats->bt_nrecs;
  3946. #else /* DB31 */
  3947. file->entryCount = stats->bt_ndata;
  3948. #endif
  3949. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  3950. "_cl5GetEntryCount: %d changes for replica %s\n",
  3951. file->entryCount, file->replName);
  3952. slapi_ch_free ((void **)&stats);
  3953. return CL5_SUCCESS;
  3954. default: slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3955. "_cl5GetEntryCount: failed to get count entry; "
  3956. "db error - %d %s\n", rc, db_strerror(rc));
  3957. return CL5_DB_ERROR;
  3958. }
  3959. }
  3960. static int _cl5WriteEntryCount (CL5DBFile *file)
  3961. {
  3962. int rc;
  3963. DBT key={0}, data={0};
  3964. char csnStr [CSN_STRSIZE];
  3965. DB_TXN *txnid = NULL;
  3966. key.data = _cl5GetHelperEntryKey (ENTRY_COUNT_TIME, csnStr);
  3967. key.size = CSN_STRSIZE;
  3968. data.data = (void*)&file->entryCount;
  3969. data.size = sizeof (file->entryCount);
  3970. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  3971. rc = txn_begin(s_cl5Desc.dbEnv, NULL, &txnid, 0);
  3972. if (rc != 0)
  3973. {
  3974. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3975. "_cl5WriteEntryCount: failed to begin transaction; db error - %d %s\n",
  3976. rc, db_strerror(rc));
  3977. return CL5_DB_ERROR;
  3978. }
  3979. #endif
  3980. rc = file->db->put(file->db, txnid, &key, &data, DEFAULT_DB_OP_FLAGS(txnid));
  3981. if (rc == 0)
  3982. {
  3983. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  3984. rc = txn_commit (txnid, 0);
  3985. if (rc != 0)
  3986. {
  3987. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3988. "_cl5WriteEntryCount: failed to commit transaction; db error - %d %s\n",
  3989. rc, db_strerror(rc));
  3990. return CL5_DB_ERROR;
  3991. }
  3992. #endif
  3993. return CL5_SUCCESS;
  3994. }
  3995. else
  3996. {
  3997. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  3998. "_cl5WriteEntryCount: "
  3999. "failed to write count entry for file %s; db error - %d %s\n",
  4000. file->name, rc, db_strerror(rc));
  4001. if (CL5_OS_ERR_IS_DISKFULL(rc))
  4002. {
  4003. cl5_set_diskfull();
  4004. return CL5_DB_ERROR;
  4005. }
  4006. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
  4007. rc = txn_abort (txnid);
  4008. if (rc != 0)
  4009. {
  4010. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4011. "_cl5WriteEntryCount: failed to abort transaction; db error - %d %s\n",
  4012. rc, db_strerror(rc));
  4013. }
  4014. #endif
  4015. return CL5_DB_ERROR;
  4016. }
  4017. }
  4018. static const char* _cl5OperationType2Str (int type)
  4019. {
  4020. switch (type)
  4021. {
  4022. case SLAPI_OPERATION_ADD: return T_ADDCTSTR;
  4023. case SLAPI_OPERATION_MODIFY: return T_MODIFYCTSTR;
  4024. case SLAPI_OPERATION_MODRDN: return T_MODRDNCTSTR;
  4025. case SLAPI_OPERATION_DELETE: return T_DELETECTSTR;
  4026. default: return NULL;
  4027. }
  4028. }
  4029. static int _cl5Str2OperationType (const char *str)
  4030. {
  4031. if (strcasecmp (str, T_ADDCTSTR) == 0)
  4032. return SLAPI_OPERATION_ADD;
  4033. if (strcasecmp (str, T_MODIFYCTSTR) == 0)
  4034. return SLAPI_OPERATION_MODIFY;
  4035. if (strcasecmp (str, T_MODRDNCTSTR) == 0)
  4036. return SLAPI_OPERATION_MODRDN;
  4037. if (strcasecmp (str, T_DELETECTSTR) == 0)
  4038. return SLAPI_OPERATION_DELETE;
  4039. return -1;
  4040. }
  4041. static int _cl5Operation2LDIF (const slapi_operation_parameters *op, const char *replGen,
  4042. char **ldifEntry, PRInt32 *lenLDIF)
  4043. {
  4044. int len = 2;
  4045. lenstr *l = NULL;
  4046. const char *strType;
  4047. const char *strDeleteOldRDN = "false";
  4048. char *buff, *start;
  4049. LDAPMod **add_mods;
  4050. char *rawDN = NULL;
  4051. char strCSN[CSN_STRSIZE];
  4052. PR_ASSERT (op && replGen && ldifEntry && IsValidOperation (op));
  4053. strType = _cl5OperationType2Str (op->operation_type);
  4054. csn_as_string(op->csn,PR_FALSE,strCSN);
  4055. /* find length of the buffer */
  4056. len += LDIF_SIZE_NEEDED(strlen (T_CHANGETYPESTR), strlen (strType));
  4057. len += LDIF_SIZE_NEEDED(strlen (T_REPLGEN), strlen (replGen));
  4058. len += LDIF_SIZE_NEEDED(strlen (T_CSNSTR), strlen (strCSN));
  4059. len += LDIF_SIZE_NEEDED(strlen (T_UNIQUEIDSTR), strlen (op->target_address.uniqueid));
  4060. switch (op->operation_type)
  4061. {
  4062. case SLAPI_OPERATION_ADD: if (NULL == op->p.p_add.target_entry) {
  4063. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4064. "_cl5Operation2LDIF(ADD): entry is NULL\n");
  4065. return CL5_BAD_FORMAT;
  4066. }
  4067. if (op->p.p_add.parentuniqueid)
  4068. len += LDIF_SIZE_NEEDED(strlen (T_PARENTIDSTR),
  4069. strlen (op->p.p_add.parentuniqueid));
  4070. slapi_entry2mods (op->p.p_add.target_entry, &rawDN, &add_mods);
  4071. len += LDIF_SIZE_NEEDED(strlen (T_DNSTR), strlen (rawDN));
  4072. l = make_changes_string(add_mods, NULL);
  4073. len += LDIF_SIZE_NEEDED(strlen (T_CHANGESTR), l->ls_len);
  4074. ldap_mods_free (add_mods, 1);
  4075. break;
  4076. case SLAPI_OPERATION_MODIFY: if (NULL == op->p.p_modify.modify_mods) {
  4077. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4078. "_cl5Operation2LDIF(MODIFY): mods are NULL\n");
  4079. return CL5_BAD_FORMAT;
  4080. }
  4081. len += LDIF_SIZE_NEEDED(strlen (T_DNSTR), REPL_GET_DN_LEN(&op->target_address));
  4082. l = make_changes_string(op->p.p_modify.modify_mods, NULL);
  4083. len += LDIF_SIZE_NEEDED(strlen (T_CHANGESTR), l->ls_len);
  4084. break;
  4085. case SLAPI_OPERATION_MODRDN: if (NULL == op->p.p_modrdn.modrdn_mods) {
  4086. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4087. "_cl5Operation2LDIF(MODRDN): mods are NULL\n");
  4088. return CL5_BAD_FORMAT;
  4089. }
  4090. len += LDIF_SIZE_NEEDED(strlen (T_DNSTR), REPL_GET_DN_LEN(&op->target_address));
  4091. len += LDIF_SIZE_NEEDED(strlen (T_NEWRDNSTR),
  4092. strlen (op->p.p_modrdn.modrdn_newrdn));
  4093. strDeleteOldRDN = (op->p.p_modrdn.modrdn_deloldrdn ? "true" : "false");
  4094. len += LDIF_SIZE_NEEDED(strlen (T_DRDNFLAGSTR),
  4095. strlen (strDeleteOldRDN));
  4096. if (REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address))
  4097. len += LDIF_SIZE_NEEDED(strlen (T_NEWSUPERIORDNSTR),
  4098. REPL_GET_DN_LEN(&op->p.p_modrdn.modrdn_newsuperior_address));
  4099. if (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid)
  4100. len += LDIF_SIZE_NEEDED(strlen (T_NEWSUPERIORIDSTR),
  4101. strlen (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid));
  4102. l = make_changes_string(op->p.p_modrdn.modrdn_mods, NULL);
  4103. len += LDIF_SIZE_NEEDED(strlen (T_CHANGESTR), l->ls_len);
  4104. break;
  4105. case SLAPI_OPERATION_DELETE: if (NULL == REPL_GET_DN(&op->target_address)) {
  4106. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4107. "_cl5Operation2LDIF(DELETE): target dn is NULL\n");
  4108. return CL5_BAD_FORMAT;
  4109. }
  4110. len += LDIF_SIZE_NEEDED(strlen (T_DNSTR), REPL_GET_DN_LEN(&op->target_address));
  4111. break;
  4112. default: slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4113. "_cl5Operation2LDIF: invalid operation type - %lu\n", op->operation_type);
  4114. return CL5_BAD_FORMAT;
  4115. }
  4116. /* allocate buffer */
  4117. buff = slapi_ch_malloc (len);
  4118. start = buff;
  4119. if (buff == NULL)
  4120. {
  4121. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4122. "_cl5Operation2LDIF: memory allocation failed\n");
  4123. return CL5_MEMORY_ERROR;
  4124. }
  4125. /* fill buffer */
  4126. slapi_ldif_put_type_and_value_with_options(&buff, T_CHANGETYPESTR, (char*)strType, strlen (strType), 0);
  4127. slapi_ldif_put_type_and_value_with_options(&buff, T_REPLGEN, (char*)replGen, strlen (replGen), 0);
  4128. slapi_ldif_put_type_and_value_with_options(&buff, T_CSNSTR, (char*)strCSN, strlen (strCSN), 0);
  4129. slapi_ldif_put_type_and_value_with_options(&buff, T_UNIQUEIDSTR, op->target_address.uniqueid,
  4130. strlen (op->target_address.uniqueid), 0);
  4131. switch (op->operation_type)
  4132. {
  4133. case SLAPI_OPERATION_ADD: if (op->p.p_add.parentuniqueid)
  4134. slapi_ldif_put_type_and_value_with_options(&buff, T_PARENTIDSTR,
  4135. op->p.p_add.parentuniqueid, strlen (op->p.p_add.parentuniqueid), 0);
  4136. slapi_ldif_put_type_and_value_with_options(&buff, T_DNSTR, rawDN, strlen (rawDN), 0);
  4137. slapi_ldif_put_type_and_value_with_options(&buff, T_CHANGESTR, l->ls_buf, l->ls_len, 0);
  4138. slapi_ch_free ((void**)&rawDN);
  4139. break;
  4140. case SLAPI_OPERATION_MODIFY: slapi_ldif_put_type_and_value_with_options(&buff, T_DNSTR, REPL_GET_DN(&op->target_address),
  4141. REPL_GET_DN_LEN(&op->target_address), 0);
  4142. slapi_ldif_put_type_and_value_with_options(&buff, T_CHANGESTR, l->ls_buf, l->ls_len, 0);
  4143. break;
  4144. case SLAPI_OPERATION_MODRDN: slapi_ldif_put_type_and_value_with_options(&buff, T_DNSTR, REPL_GET_DN(&op->target_address),
  4145. REPL_GET_DN_LEN(&op->target_address), 0);
  4146. slapi_ldif_put_type_and_value_with_options(&buff, T_NEWRDNSTR, op->p.p_modrdn.modrdn_newrdn,
  4147. strlen (op->p.p_modrdn.modrdn_newrdn), 0);
  4148. slapi_ldif_put_type_and_value_with_options(&buff, T_DRDNFLAGSTR, strDeleteOldRDN,
  4149. strlen (strDeleteOldRDN), 0);
  4150. if (REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address))
  4151. slapi_ldif_put_type_and_value_with_options(&buff, T_NEWSUPERIORDNSTR,
  4152. REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address),
  4153. REPL_GET_DN_LEN(&op->p.p_modrdn.modrdn_newsuperior_address), 0);
  4154. if (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid)
  4155. slapi_ldif_put_type_and_value_with_options(&buff, T_NEWSUPERIORIDSTR,
  4156. op->p.p_modrdn.modrdn_newsuperior_address.uniqueid,
  4157. strlen (op->p.p_modrdn.modrdn_newsuperior_address.uniqueid), 0);
  4158. slapi_ldif_put_type_and_value_with_options(&buff, T_CHANGESTR, l->ls_buf, l->ls_len, 0);
  4159. break;
  4160. case SLAPI_OPERATION_DELETE: slapi_ldif_put_type_and_value_with_options(&buff, T_DNSTR, REPL_GET_DN(&op->target_address),
  4161. REPL_GET_DN_LEN(&op->target_address), 0);
  4162. break;
  4163. }
  4164. *buff = '\n';
  4165. buff ++;
  4166. *buff = '\0';
  4167. *ldifEntry = start;
  4168. *lenLDIF = buff - start;
  4169. if (l)
  4170. lenstr_free(&l);
  4171. return CL5_SUCCESS;
  4172. }
  4173. static int
  4174. _cl5LDIF2Operation (char *ldifEntry, slapi_operation_parameters *op, char **replGen)
  4175. {
  4176. int rc;
  4177. int rval = CL5_BAD_FORMAT;
  4178. char *next, *line;
  4179. struct berval type, value;
  4180. struct berval bv_null = {0, NULL};
  4181. int freeval = 0;
  4182. Slapi_Mods *mods;
  4183. char *rawDN = NULL;
  4184. char *ldifEntryWork = slapi_ch_strdup(ldifEntry);
  4185. PR_ASSERT (op && ldifEntry && replGen);
  4186. memset (op, 0, sizeof (*op));
  4187. next = ldifEntryWork;
  4188. while ((line = ldif_getline(&next)) != NULL)
  4189. {
  4190. if ( *line == '\n' || *line == '\0' )
  4191. {
  4192. break;
  4193. }
  4194. /* this call modifies ldifEntry */
  4195. type = bv_null;
  4196. value = bv_null;
  4197. rc = slapi_ldif_parse_line(line, &type, &value, &freeval);
  4198. if (rc != 0)
  4199. {
  4200. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4201. "_cl5LDIF2Operation: warning - failed to parse ldif line\n");
  4202. continue;
  4203. }
  4204. if (strncasecmp (type.bv_val, T_CHANGETYPESTR,
  4205. strlen(T_CHANGETYPESTR)>type.bv_len?
  4206. strlen(T_CHANGETYPESTR):type.bv_len) == 0)
  4207. {
  4208. op->operation_type = _cl5Str2OperationType (value.bv_val);
  4209. }
  4210. else if (strncasecmp (type.bv_val, T_REPLGEN, type.bv_len) == 0)
  4211. {
  4212. *replGen = slapi_ch_strdup (value.bv_val);
  4213. }
  4214. else if (strncasecmp (type.bv_val, T_CSNSTR, type.bv_len) == 0)
  4215. {
  4216. op->csn = csn_new_by_string(value.bv_val);
  4217. }
  4218. else if (strncasecmp (type.bv_val, T_UNIQUEIDSTR, type.bv_len) == 0)
  4219. {
  4220. op->target_address.uniqueid = slapi_ch_strdup (value.bv_val);
  4221. }
  4222. else if (strncasecmp (type.bv_val, T_DNSTR, type.bv_len) == 0)
  4223. {
  4224. PR_ASSERT (op->operation_type);
  4225. if (op->operation_type == SLAPI_OPERATION_ADD)
  4226. {
  4227. rawDN = slapi_ch_strdup (value.bv_val);
  4228. op->target_address.sdn = slapi_sdn_new_dn_byval(rawDN);
  4229. }
  4230. else
  4231. op->target_address.sdn = slapi_sdn_new_dn_byval(value.bv_val);
  4232. }
  4233. else if (strncasecmp (type.bv_val, T_PARENTIDSTR, type.bv_len) == 0)
  4234. {
  4235. op->p.p_add.parentuniqueid = slapi_ch_strdup (value.bv_val);
  4236. }
  4237. else if (strncasecmp (type.bv_val, T_NEWRDNSTR, type.bv_len) == 0)
  4238. {
  4239. op->p.p_modrdn.modrdn_newrdn = slapi_ch_strdup (value.bv_val);
  4240. }
  4241. else if (strncasecmp (type.bv_val, T_DRDNFLAGSTR, type.bv_len) == 0)
  4242. {
  4243. op->p.p_modrdn.modrdn_deloldrdn = (strncasecmp (value.bv_val, "true", value.bv_len) ? PR_FALSE : PR_TRUE);
  4244. }
  4245. else if (strncasecmp (type.bv_val, T_NEWSUPERIORDNSTR, type.bv_len) == 0)
  4246. {
  4247. op->p.p_modrdn.modrdn_newsuperior_address.sdn = slapi_sdn_new_dn_byval(value.bv_val);
  4248. }
  4249. else if (strncasecmp (type.bv_val, T_NEWSUPERIORIDSTR, type.bv_len) == 0)
  4250. {
  4251. op->p.p_modrdn.modrdn_newsuperior_address.uniqueid = slapi_ch_strdup (value.bv_val);
  4252. }
  4253. else if (strncasecmp (type.bv_val, T_CHANGESTR,
  4254. strlen(T_CHANGESTR)>type.bv_len?
  4255. strlen(T_CHANGESTR):type.bv_len) == 0)
  4256. {
  4257. PR_ASSERT (op->operation_type);
  4258. switch (op->operation_type)
  4259. {
  4260. case SLAPI_OPERATION_ADD: /*
  4261. * When it comes here, case T_DNSTR is already
  4262. * passed and rawDN is supposed to set.
  4263. * But it's a good idea to make sure it is
  4264. * not NULL.
  4265. */
  4266. if (NULL == rawDN) {
  4267. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4268. "_cl5LDIF2Operation: corrupted format "
  4269. "for operation type - %lu\n",
  4270. op->operation_type);
  4271. slapi_ch_free_string(&ldifEntryWork);
  4272. return CL5_BAD_FORMAT;
  4273. }
  4274. mods = parse_changes_string(value.bv_val);
  4275. PR_ASSERT (mods);
  4276. slapi_mods2entry (&(op->p.p_add.target_entry), rawDN,
  4277. slapi_mods_get_ldapmods_byref(mods));
  4278. slapi_ch_free ((void**)&rawDN);
  4279. slapi_mods_free (&mods);
  4280. break;
  4281. case SLAPI_OPERATION_MODIFY: mods = parse_changes_string(value.bv_val);
  4282. PR_ASSERT (mods);
  4283. op->p.p_modify.modify_mods = slapi_mods_get_ldapmods_passout (mods);
  4284. slapi_mods_free (&mods);
  4285. break;
  4286. case SLAPI_OPERATION_MODRDN: mods = parse_changes_string(value.bv_val);
  4287. PR_ASSERT (mods);
  4288. op->p.p_modrdn.modrdn_mods = slapi_mods_get_ldapmods_passout (mods);
  4289. slapi_mods_free (&mods);
  4290. break;
  4291. default: slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4292. "_cl5LDIF2Operation: invalid operation type - %lu\n",
  4293. op->operation_type);
  4294. if (freeval) {
  4295. slapi_ch_free_string(&value.bv_val);
  4296. }
  4297. slapi_ch_free_string(&ldifEntryWork);
  4298. return CL5_BAD_FORMAT;
  4299. }
  4300. }
  4301. if (freeval) {
  4302. slapi_ch_free_string(&value.bv_val);
  4303. }
  4304. }
  4305. if ((0 != strncmp(ldifEntryWork, "clpurgeruv", 10)) && /* skip RUV; */
  4306. (0 != strncmp(ldifEntryWork, "clmaxruv", 8))) /* RUV has NULL op */
  4307. {
  4308. if (IsValidOperation (op))
  4309. {
  4310. rval = CL5_SUCCESS;
  4311. }
  4312. else
  4313. {
  4314. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4315. "_cl5LDIF2Operation: invalid data format\n");
  4316. }
  4317. }
  4318. slapi_ch_free_string(&ldifEntryWork);
  4319. return rval;
  4320. }
  4321. static int _cl5WriteOperationTxn(const char *replName, const char *replGen,
  4322. const slapi_operation_parameters *op, PRBool local, void *txn)
  4323. {
  4324. int rc;
  4325. int cnt;
  4326. DBT key={0};
  4327. DBT * data=NULL;
  4328. char csnStr [CSN_STRSIZE];
  4329. PRIntervalTime interval;
  4330. CL5Entry entry;
  4331. CL5DBFile *file = NULL;
  4332. Object *file_obj = NULL;
  4333. DB_TXN *txnid = NULL;
  4334. DB_TXN *parent_txnid = (DB_TXN *)txn;
  4335. rc = _cl5GetDBFileByReplicaName (replName, replGen, &file_obj);
  4336. if (rc == CL5_NOTFOUND)
  4337. {
  4338. rc = _cl5DBOpenFileByReplicaName (replName, replGen, &file_obj,
  4339. PR_TRUE /* check for duplicates */);
  4340. if (rc != CL5_SUCCESS)
  4341. {
  4342. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4343. "_cl5WriteOperationTxn: failed to find or open DB object for replica %s\n", replName);
  4344. return rc;
  4345. }
  4346. }
  4347. else if (rc != CL5_SUCCESS)
  4348. {
  4349. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4350. "_cl5WriteOperationTxn: failed to get db file for target dn (%s)",
  4351. REPL_GET_DN(&op->target_address));
  4352. return CL5_OBJSET_ERROR;
  4353. }
  4354. /* assign entry time - used for trimming */
  4355. entry.time = current_time ();
  4356. entry.op = (slapi_operation_parameters *)op;
  4357. /* construct the key */
  4358. key.data = csn_as_string(op->csn, PR_FALSE, csnStr);
  4359. key.size = CSN_STRSIZE;
  4360. /* construct the data */
  4361. data = (DBT *) slapi_ch_calloc(1, sizeof(DBT));
  4362. rc = _cl5Entry2DBData (&entry, (char**)&data->data, &data->size);
  4363. if (rc != CL5_SUCCESS)
  4364. {
  4365. char s[CSN_STRSIZE];
  4366. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4367. "_cl5WriteOperationTxn: failed to convert entry with csn (%s) "
  4368. "to db format\n", csn_as_string(op->csn,PR_FALSE,s));
  4369. goto done;
  4370. }
  4371. file = (CL5DBFile*)object_get_data (file_obj);
  4372. PR_ASSERT (file);
  4373. /* if this is part of ldif2cl - just write the entry without transaction */
  4374. if (s_cl5Desc.dbOpenMode == CL5_OPEN_LDIF2CL)
  4375. {
  4376. rc = file->db->put(file->db, NULL, &key, data, 0);
  4377. if (rc != 0)
  4378. {
  4379. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4380. "_cl5WriteOperationTxn: failed to write entry; db error - %d %s\n",
  4381. rc, db_strerror(rc));
  4382. if (CL5_OS_ERR_IS_DISKFULL(rc))
  4383. {
  4384. cl5_set_diskfull();
  4385. }
  4386. rc = CL5_DB_ERROR;
  4387. }
  4388. goto done;
  4389. }
  4390. /* write the entry */
  4391. rc = EAGAIN;
  4392. cnt = 0;
  4393. while ((rc == EAGAIN || rc == DB_LOCK_DEADLOCK) && cnt < MAX_TRIALS)
  4394. {
  4395. if (cnt != 0)
  4396. {
  4397. #if USE_DB_TXN
  4398. /* abort previous transaction */
  4399. rc = TXN_ABORT (txnid);
  4400. if (rc != 0)
  4401. {
  4402. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4403. "_cl5WriteOperationTxn: failed to abort transaction; db error - %d %s\n",
  4404. rc, db_strerror(rc));
  4405. rc = CL5_DB_ERROR;
  4406. goto done;
  4407. }
  4408. #endif
  4409. /* back off */
  4410. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  4411. DS_Sleep(interval);
  4412. }
  4413. #if USE_DB_TXN
  4414. /* begin transaction */
  4415. rc = TXN_BEGIN(s_cl5Desc.dbEnv, parent_txnid, &txnid, 0);
  4416. if (rc != 0)
  4417. {
  4418. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4419. "_cl5WriteOperationTxn: failed to start transaction; db error - %d %s\n",
  4420. rc, db_strerror(rc));
  4421. rc = CL5_DB_ERROR;
  4422. goto done;
  4423. }
  4424. #endif
  4425. if ( file->sema )
  4426. {
  4427. PR_WaitSemaphore(file->sema);
  4428. }
  4429. rc = file->db->put(file->db, txnid, &key, data, DEFAULT_DB_OP_FLAGS(txnid));
  4430. if ( file->sema )
  4431. {
  4432. PR_PostSemaphore(file->sema);
  4433. }
  4434. if (CL5_OS_ERR_IS_DISKFULL(rc))
  4435. {
  4436. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4437. "_cl5WriteOperationTxn: changelog (%s) DISK FULL; db error - %d %s\n",
  4438. s_cl5Desc.dbDir, rc, db_strerror(rc));
  4439. cl5_set_diskfull();
  4440. rc = CL5_DB_ERROR;
  4441. goto done;
  4442. }
  4443. if (cnt != 0)
  4444. {
  4445. if (rc == 0)
  4446. {
  4447. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "_cl5WriteOperationTxn: retry (%d) the transaction (csn=%s) succeeded\n", cnt, (char*)key.data);
  4448. }
  4449. else if ((cnt + 1) >= MAX_TRIALS)
  4450. {
  4451. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "_cl5WriteOperationTxn: retry (%d) the transaction (csn=%s) failed (rc=%d (%s))\n", cnt, (char*)key.data, rc, db_strerror(rc));
  4452. }
  4453. }
  4454. cnt ++;
  4455. }
  4456. if (rc == 0) /* we successfully added entry */
  4457. {
  4458. #if USE_DB_TXN
  4459. rc = TXN_COMMIT (txnid, 0);
  4460. #endif
  4461. }
  4462. else
  4463. {
  4464. char s[CSN_STRSIZE];
  4465. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4466. "_cl5WriteOperationTxn: failed to write entry with csn (%s); "
  4467. "db error - %d %s\n", csn_as_string(op->csn,PR_FALSE,s),
  4468. rc, db_strerror(rc));
  4469. #if USE_DB_TXN
  4470. rc = TXN_ABORT (txnid);
  4471. if (rc != 0)
  4472. {
  4473. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4474. "_cl5WriteOperationTxn: failed to abort transaction; db error - %d %s\n",
  4475. rc, db_strerror(rc));
  4476. }
  4477. #endif
  4478. rc = CL5_DB_ERROR;
  4479. goto done;
  4480. }
  4481. /* update entry count - we assume that all entries are new */
  4482. PR_AtomicIncrement (&file->entryCount);
  4483. /* update purge vector if we have not seen any changes from this replica before */
  4484. _cl5UpdateRUV (file_obj, op->csn, PR_TRUE, PR_TRUE);
  4485. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  4486. "cl5WriteOperationTxn: successfully written entry with csn (%s)\n", csnStr);
  4487. rc = CL5_SUCCESS;
  4488. done:
  4489. if (data->data)
  4490. slapi_ch_free (&(data->data));
  4491. slapi_ch_free((void**) &data);
  4492. if (file_obj)
  4493. object_release (file_obj);
  4494. return rc;
  4495. }
  4496. static int _cl5WriteOperation(const char *replName, const char *replGen,
  4497. const slapi_operation_parameters *op, PRBool local)
  4498. {
  4499. return _cl5WriteOperationTxn(replName, replGen, op, local, NULL);
  4500. }
  4501. static int _cl5GetFirstEntry (Object *obj, CL5Entry *entry, void **iterator, DB_TXN *txnid)
  4502. {
  4503. int rc;
  4504. DBC *cursor = NULL;
  4505. DBT key={0}, data={0};
  4506. CL5Iterator *it;
  4507. CL5DBFile *file;
  4508. PR_ASSERT (obj && entry && iterator);
  4509. file = (CL5DBFile*)object_get_data (obj);
  4510. PR_ASSERT (file);
  4511. /* create cursor */
  4512. rc = file->db->cursor(file->db, txnid, &cursor, 0);
  4513. if (rc != 0)
  4514. {
  4515. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4516. "_cl5GetFirstEntry: failed to create cursor; db error - %d %s\n", rc, db_strerror(rc));
  4517. rc = CL5_DB_ERROR;
  4518. goto done;
  4519. }
  4520. key.flags = DB_DBT_MALLOC;
  4521. data.flags = DB_DBT_MALLOC;
  4522. while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0)
  4523. {
  4524. /* skip service entries */
  4525. if (cl5HelperEntry ((char*)key.data, NULL))
  4526. {
  4527. slapi_ch_free (&(key.data));
  4528. slapi_ch_free (&(data.data));
  4529. continue;
  4530. }
  4531. /* format entry */
  4532. slapi_ch_free (&(key.data));
  4533. rc = cl5DBData2Entry (data.data, data.size, entry);
  4534. slapi_ch_free (&(data.data));
  4535. if (rc != 0)
  4536. {
  4537. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4538. "_cl5GetFirstOperation: failed to format entry: %d\n", rc);
  4539. goto done;
  4540. }
  4541. it = (CL5Iterator*)slapi_ch_malloc(sizeof (CL5Iterator));
  4542. it->cursor = cursor;
  4543. object_acquire (obj);
  4544. it->file = obj;
  4545. *(CL5Iterator**)iterator = it;
  4546. return CL5_SUCCESS;
  4547. }
  4548. /*
  4549. * Bug 430172 - memory leaks after db "get" deadlocks, e.g. in CL5 trim
  4550. * Even when db->c_get() does not return success, memory may have been
  4551. * allocated in the DBT. This seems to happen when DB_DBT_MALLOC was set,
  4552. * the data being retrieved is larger than the page size, and we got
  4553. * DB_LOCK_DEADLOCK. libdb allocates the memory and then finds itself
  4554. * deadlocked trying to go through the overflow page list. It returns
  4555. * DB_LOCK_DEADLOCK which we've assumed meant that no memory was allocated
  4556. * for the DBT.
  4557. *
  4558. * The following slapi_ch_free frees the memory only when the value is
  4559. * non NULL, which is true if the situation described above occurs.
  4560. */
  4561. slapi_ch_free ((void **)&key.data);
  4562. slapi_ch_free ((void **)&data.data);
  4563. /* walked of the end of the file */
  4564. if (rc == DB_NOTFOUND)
  4565. {
  4566. rc = CL5_NOTFOUND;
  4567. goto done;
  4568. }
  4569. /* db error occured while iterating */
  4570. /* On this path, the condition "rc != 0" cannot be false */
  4571. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4572. "_cl5GetFirstEntry: failed to get entry; db error - %d %s\n",
  4573. rc, db_strerror(rc));
  4574. rc = CL5_DB_ERROR;
  4575. done:
  4576. /* error occured */
  4577. /* We didn't success in assigning this cursor to the iterator,
  4578. * so we need to free the cursor here */
  4579. if (cursor)
  4580. cursor->c_close(cursor);
  4581. return rc;
  4582. }
  4583. static int _cl5GetNextEntry (CL5Entry *entry, void *iterator)
  4584. {
  4585. int rc;
  4586. CL5Iterator *it;
  4587. DBT key={0}, data={0};
  4588. PR_ASSERT (entry && iterator);
  4589. it = (CL5Iterator*) iterator;
  4590. key.flags = DB_DBT_MALLOC;
  4591. data.flags = DB_DBT_MALLOC;
  4592. while ((rc = it->cursor->c_get(it->cursor, &key, &data, DB_NEXT)) == 0)
  4593. {
  4594. if (cl5HelperEntry ((char*)key.data, NULL))
  4595. {
  4596. slapi_ch_free (&(key.data));
  4597. slapi_ch_free (&(data.data));
  4598. continue;
  4599. }
  4600. slapi_ch_free (&(key.data));
  4601. /* format entry */
  4602. rc = cl5DBData2Entry (data.data, data.size, entry);
  4603. slapi_ch_free (&(data.data));
  4604. if (rc != 0)
  4605. {
  4606. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4607. "_cl5GetNextEntry: failed to format entry: %d\n", rc);
  4608. }
  4609. return rc;
  4610. }
  4611. /*
  4612. * Bug 430172 - memory leaks after db "get" deadlocks, e.g. in CL5 trim
  4613. * Even when db->c_get() does not return success, memory may have been
  4614. * allocated in the DBT. This seems to happen when DB_DBT_MALLOC was set,
  4615. * the data being retrieved is larger than the page size, and we got
  4616. * DB_LOCK_DEADLOCK. libdb allocates the memory and then finds itself
  4617. * deadlocked trying to go through the overflow page list. It returns
  4618. * DB_LOCK_DEADLOCK which we've assumed meant that no memory was allocated
  4619. * for the DBT.
  4620. *
  4621. * The following slapi_ch_free frees the memory only when the value is
  4622. * non NULL, which is true if the situation described above occurs.
  4623. */
  4624. slapi_ch_free ((void **)&key.data);
  4625. slapi_ch_free ((void **)&data.data);
  4626. /* walked of the end of the file or entry is out of range */
  4627. if (rc == 0 || rc == DB_NOTFOUND)
  4628. {
  4629. return CL5_NOTFOUND;
  4630. }
  4631. /* cursor operation failed */
  4632. slapi_log_error(rc == CL5_DB_LOCK_ERROR?SLAPI_LOG_REPL:SLAPI_LOG_FATAL,
  4633. repl_plugin_name_cl,
  4634. "_cl5GetNextEntry: failed to get entry; db error - %d %s\n",
  4635. rc, db_strerror(rc));
  4636. return rc;
  4637. }
  4638. static int _cl5CurrentDeleteEntry (void *iterator)
  4639. {
  4640. int rc;
  4641. CL5Iterator *it;
  4642. CL5DBFile *file;
  4643. PR_ASSERT (iterator);
  4644. it = (CL5Iterator*)iterator;
  4645. rc = it->cursor->c_del (it->cursor, 0);
  4646. if (rc == 0) {
  4647. /* decrement entry count */
  4648. file = (CL5DBFile*)object_get_data (it->file);
  4649. PR_AtomicDecrement (&file->entryCount);
  4650. return CL5_SUCCESS;
  4651. } else {
  4652. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4653. "_cl5CurrentDeleteEntry failed, err=%d %s\n",
  4654. rc, db_strerror(rc));
  4655. /*
  4656. * We don't free(close) the cursor here, as the caller will free it by
  4657. * a call to cl5DestroyIterator. Freeing it here is a potential bug,
  4658. * as the cursor can't be referenced later once freed.
  4659. */
  4660. return rc;
  4661. }
  4662. }
  4663. static PRBool _cl5IsValidIterator (const CL5Iterator *iterator)
  4664. {
  4665. return (iterator && iterator->cursor && iterator->file);
  4666. }
  4667. static int _cl5GetOperation (Object *replica, slapi_operation_parameters *op)
  4668. {
  4669. int rc;
  4670. DBT key={0}, data={0};
  4671. CL5DBFile *file;
  4672. CL5Entry entry;
  4673. Object *obj = NULL;
  4674. char csnStr[CSN_STRSIZE];
  4675. rc = _cl5GetDBFile (replica, &obj);
  4676. if (rc != CL5_SUCCESS || !obj)
  4677. {
  4678. goto done;
  4679. }
  4680. file = (CL5DBFile*)object_get_data (obj);
  4681. PR_ASSERT (file);
  4682. /* construct the key */
  4683. key.data = csn_as_string(op->csn, PR_FALSE, csnStr);
  4684. key.size = CSN_STRSIZE;
  4685. data.flags = DB_DBT_MALLOC;
  4686. rc = file->db->get(file->db, NULL/*txn*/, &key, &data, 0);
  4687. switch (rc)
  4688. {
  4689. case 0: entry.op = op;
  4690. /* Callers of this function should cl5_operation_parameters_done(op) */
  4691. rc = cl5DBData2Entry (data.data, data.size, &entry);
  4692. if (rc == CL5_SUCCESS)
  4693. {
  4694. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  4695. "_cl5GetOperation: successfully retrieved operation with csn (%s)\n",
  4696. csnStr);
  4697. }
  4698. else
  4699. {
  4700. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4701. "_cl5GetOperation: failed to convert db data to operation;"
  4702. " csn - %s\n", csnStr);
  4703. }
  4704. goto done;
  4705. case DB_NOTFOUND: slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4706. "_cl5GetOperation: operation for csn (%s) is not found in db that should contain dn (%s)\n",
  4707. csnStr, REPL_GET_DN(&op->target_address));
  4708. rc = CL5_NOTFOUND;
  4709. goto done;
  4710. default: slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4711. "_cl5GetOperation: failed to get entry for csn (%s); "
  4712. "db error - %d %s\n", csnStr, rc, db_strerror(rc));
  4713. rc = CL5_DB_ERROR;
  4714. goto done;
  4715. }
  4716. done:
  4717. if (obj)
  4718. object_release (obj);
  4719. slapi_ch_free (&(data.data));
  4720. return rc;
  4721. }
  4722. PRBool cl5HelperEntry (const char *csnstr, CSN *csnp)
  4723. {
  4724. CSN *csn;
  4725. time_t csnTime;
  4726. PRBool retval = PR_FALSE;
  4727. if (csnp)
  4728. {
  4729. csn = csnp;
  4730. }
  4731. else
  4732. {
  4733. csn= csn_new_by_string(csnstr);
  4734. }
  4735. if (csn == NULL)
  4736. {
  4737. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4738. "cl5HelperEntry: failed to get csn time; csn error\n");
  4739. return PR_FALSE;
  4740. }
  4741. csnTime= csn_get_time(csn);
  4742. if (csnTime == ENTRY_COUNT_TIME || csnTime == PURGE_RUV_TIME)
  4743. {
  4744. retval = PR_TRUE;
  4745. }
  4746. if (NULL == csnp)
  4747. csn_free(&csn);
  4748. return retval;
  4749. }
  4750. #ifdef FOR_DEBUGGING
  4751. /* Replay iteration helper functions */
  4752. static PRBool _cl5ValidReplayIterator (const CL5ReplayIterator *iterator)
  4753. {
  4754. if (iterator == NULL ||
  4755. iterator->consumerRuv == NULL || iterator->supplierRuvObj == NULL ||
  4756. iterator->fileObj == NULL)
  4757. return PR_FALSE;
  4758. return PR_TRUE;
  4759. }
  4760. #endif
  4761. /* Algorithm: ONREPL!!!
  4762. */
  4763. struct replica_hash_entry
  4764. {
  4765. ReplicaId rid; /* replica id */
  4766. PRBool sendChanges; /* indicates whether changes should be sent for this replica */
  4767. };
  4768. static int _cl5PositionCursorForReplay (ReplicaId consumerRID, const RUV *consumerRuv,
  4769. Object *replica, Object *fileObj, CL5ReplayIterator **iterator, int *continue_on_missing)
  4770. {
  4771. CLC_Buffer *clcache = NULL;
  4772. CL5DBFile *file;
  4773. CSN *startCSN = NULL;
  4774. char csnStr [CSN_STRSIZE];
  4775. int rc = CL5_SUCCESS;
  4776. Object *supplierRuvObj = NULL;
  4777. RUV *supplierRuv = NULL;
  4778. PRBool haveChanges = PR_FALSE;
  4779. char *agmt_name;
  4780. PR_ASSERT (consumerRuv && replica && fileObj && iterator);
  4781. csnStr[0] = '\0';
  4782. file = (CL5DBFile*)object_get_data (fileObj);
  4783. /* get supplier's RUV */
  4784. supplierRuvObj = replica_get_ruv((Replica*)object_get_data(replica));
  4785. PR_ASSERT (supplierRuvObj);
  4786. if (!supplierRuvObj) {
  4787. rc = CL5_UNKNOWN_ERROR;
  4788. goto done;
  4789. }
  4790. supplierRuv = (RUV*)object_get_data (supplierRuvObj);
  4791. PR_ASSERT (supplierRuv);
  4792. agmt_name = get_thread_private_agmtname();
  4793. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  4794. slapi_log_error(SLAPI_LOG_REPL, NULL, "_cl5PositionCursorForReplay (%s): Consumer RUV:\n", agmt_name);
  4795. ruv_dump (consumerRuv, agmt_name, NULL);
  4796. slapi_log_error(SLAPI_LOG_REPL, NULL, "_cl5PositionCursorForReplay (%s): Supplier RUV:\n", agmt_name);
  4797. ruv_dump (supplierRuv, agmt_name, NULL);
  4798. }
  4799. /* initialize the changelog buffer and do the initial load */
  4800. rc = clcache_get_buffer ( &clcache, file->db, consumerRID, consumerRuv, supplierRuv );
  4801. if ( rc != 0 ) goto done;
  4802. rc = clcache_load_buffer (clcache, &startCSN, continue_on_missing);
  4803. if (rc == 0) {
  4804. haveChanges = PR_TRUE;
  4805. rc = CL5_SUCCESS;
  4806. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  4807. csn_as_string(startCSN, PR_FALSE, csnStr);
  4808. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4809. "%s: CSN %s found, position set for replay\n", agmt_name, csnStr);
  4810. }
  4811. }
  4812. else if (rc == DB_NOTFOUND) {
  4813. /* buffer not loaded.
  4814. * either because no changes have to be sent ==> startCSN is NULL
  4815. * or the calculated startCSN cannot be found in the changelog
  4816. */
  4817. if (startCSN == NULL) {
  4818. rc = CL5_NOTFOUND;
  4819. goto done;
  4820. }
  4821. /* check whether this csn should be present */
  4822. rc = _cl5CheckMissingCSN (startCSN, supplierRuv, file);
  4823. if (rc == CL5_MISSING_DATA) /* we should have had the change but we don't */
  4824. {
  4825. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  4826. csn_as_string(startCSN, PR_FALSE, csnStr);
  4827. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  4828. "%s: CSN %s not found, seems to be missing\n", agmt_name, csnStr);
  4829. }
  4830. }
  4831. else /* we are not as up to date or we purged */
  4832. {
  4833. csn_as_string(startCSN, PR_FALSE, csnStr);
  4834. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4835. "%s: CSN %s not found, we aren't as up to date, or we purged\n",
  4836. agmt_name, csnStr);
  4837. }
  4838. }
  4839. else
  4840. {
  4841. csn_as_string(startCSN, PR_FALSE, csnStr);
  4842. /* db error */
  4843. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4844. "%s: Failed to retrieve change with CSN %s; db error - %d %s\n",
  4845. agmt_name, csnStr, rc, db_strerror(rc));
  4846. rc = CL5_DB_ERROR;
  4847. }
  4848. /* setup the iterator */
  4849. if (haveChanges)
  4850. {
  4851. *iterator = (CL5ReplayIterator*) slapi_ch_calloc (1, sizeof (CL5ReplayIterator));
  4852. if (*iterator == NULL)
  4853. {
  4854. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  4855. "%s: _cl5PositionCursorForReplay: failed to allocate iterator\n", agmt_name);
  4856. rc = CL5_MEMORY_ERROR;
  4857. goto done;
  4858. }
  4859. /* ONREPL - should we make a copy of both RUVs here ?*/
  4860. (*iterator)->fileObj = fileObj;
  4861. (*iterator)->clcache = clcache; clcache = NULL;
  4862. (*iterator)->consumerRID = consumerRID;
  4863. (*iterator)->consumerRuv = consumerRuv;
  4864. (*iterator)->supplierRuvObj = supplierRuvObj;
  4865. }
  4866. else if (rc == CL5_SUCCESS)
  4867. {
  4868. /* we have no changes to send */
  4869. rc = CL5_NOTFOUND;
  4870. }
  4871. done:
  4872. if ( clcache )
  4873. clcache_return_buffer ( &clcache );
  4874. if (rc != CL5_SUCCESS)
  4875. {
  4876. if (supplierRuvObj)
  4877. object_release (supplierRuvObj);
  4878. }
  4879. return rc;
  4880. }
  4881. struct ruv_it
  4882. {
  4883. CSN **csns; /* csn list */
  4884. int alloc; /* allocated size */
  4885. int pos; /* position in the list */
  4886. };
  4887. static int ruv_consumer_iterator (const ruv_enum_data *enum_data, void *arg)
  4888. {
  4889. struct ruv_it *data = (struct ruv_it*)arg;
  4890. PR_ASSERT (data);
  4891. /* check if we have space for one more element */
  4892. if (data->pos >= data->alloc - 2)
  4893. {
  4894. data->alloc += 4;
  4895. data->csns = (CSN**) slapi_ch_realloc ((void*)data->csns, data->alloc * sizeof (CSN*));
  4896. }
  4897. data->csns [data->pos] = csn_dup (enum_data->csn);
  4898. data->pos ++;
  4899. return 0;
  4900. }
  4901. static int ruv_supplier_iterator (const ruv_enum_data *enum_data, void *arg)
  4902. {
  4903. int i;
  4904. PRBool found = PR_FALSE;
  4905. ReplicaId rid;
  4906. struct ruv_it *data = (struct ruv_it*)arg;
  4907. PR_ASSERT (data);
  4908. rid = csn_get_replicaid (enum_data->min_csn);
  4909. /* check if the replica that generated the csn is already in the list */
  4910. for (i = 0; i < data->pos; i++)
  4911. {
  4912. if (rid == csn_get_replicaid (data->csns[i]))
  4913. {
  4914. found = PR_TRUE;
  4915. /* remove datacsn[i] if it is greater or equal to the supplier's maxcsn */
  4916. if ( csn_compare ( data->csns[i], enum_data->csn ) >= 0 )
  4917. {
  4918. int j;
  4919. csn_free ( & data->csns[i] );
  4920. for (j = i+1; j < data->pos; j++)
  4921. {
  4922. data->csns [j-1] = data->csns [j];
  4923. }
  4924. data->pos --;
  4925. }
  4926. break;
  4927. }
  4928. }
  4929. if (!found)
  4930. {
  4931. /* check if we have space for one more element */
  4932. if (data->pos >= data->alloc - 2)
  4933. {
  4934. data->alloc += 4;
  4935. data->csns = (CSN**)slapi_ch_realloc ((void*)data->csns,
  4936. data->alloc * sizeof (CSN*));
  4937. }
  4938. data->csns [data->pos] = csn_dup (enum_data->min_csn);
  4939. data->pos ++;
  4940. }
  4941. return 0;
  4942. }
  4943. static int
  4944. my_csn_compare(const void *arg1, const void *arg2)
  4945. {
  4946. return(csn_compare(*((CSN **)arg1), *((CSN **)arg2)));
  4947. }
  4948. /* builds CSN ordered list of all csns in the RUV */
  4949. CSN** cl5BuildCSNList (const RUV *consRuv, const RUV *supRuv)
  4950. {
  4951. struct ruv_it data;
  4952. int count, rc;
  4953. CSN **csns;
  4954. PR_ASSERT (consRuv);
  4955. count = ruv_replica_count (consRuv);
  4956. csns = (CSN**)slapi_ch_calloc (count + 1, sizeof (CSN*));
  4957. data.csns = csns;
  4958. data.alloc = count + 1;
  4959. data.pos = 0;
  4960. /* add consumer elements to the list */
  4961. rc = ruv_enumerate_elements (consRuv, ruv_consumer_iterator, &data);
  4962. if (rc == 0 && supRuv)
  4963. {
  4964. /* add supplier elements to the list */
  4965. rc = ruv_enumerate_elements (supRuv, ruv_supplier_iterator, &data);
  4966. }
  4967. /* we have no csns */
  4968. if (data.csns[0] == NULL)
  4969. {
  4970. /* csns might have been realloced in ruv_supplier_iterator() */
  4971. slapi_ch_free ((void**)&data.csns);
  4972. csns = NULL;
  4973. }
  4974. else
  4975. {
  4976. csns = data.csns;
  4977. data.csns [data.pos] = NULL;
  4978. if (rc == 0)
  4979. {
  4980. qsort (csns, data.pos, sizeof (CSN*), my_csn_compare);
  4981. }
  4982. else
  4983. {
  4984. cl5DestroyCSNList (&csns);
  4985. }
  4986. }
  4987. return csns;
  4988. }
  4989. void cl5DestroyCSNList (CSN*** csns)
  4990. {
  4991. if (csns && *csns)
  4992. {
  4993. int i;
  4994. for (i = 0; (*csns)[i]; i++)
  4995. {
  4996. csn_free (&(*csns)[i]);
  4997. }
  4998. slapi_ch_free ((void**)csns);
  4999. }
  5000. }
  5001. /* A csn should be in the changelog if it is larger than purge vector csn for the same
  5002. replica and is smaller than the csn in supplier's ruv for the same replica.
  5003. The functions returns
  5004. CL5_PURGED if data was purged from the changelog or was never logged
  5005. because it was loaded as part of replica initialization
  5006. CL5_MISSING if the data erouneously missing
  5007. CL5_SUCCESS if that has not and should not been seen by the server
  5008. */
  5009. static int _cl5CheckMissingCSN (const CSN *csn, const RUV *supplierRuv, CL5DBFile *file)
  5010. {
  5011. ReplicaId rid;
  5012. CSN *supplierCsn = NULL;
  5013. CSN *purgeCsn = NULL;
  5014. int rc = CL5_SUCCESS;
  5015. char csnStr [CSN_STRSIZE];
  5016. PR_ASSERT (csn && supplierRuv && file);
  5017. rid = csn_get_replicaid (csn);
  5018. ruv_get_largest_csn_for_replica (supplierRuv, rid, &supplierCsn);
  5019. if (supplierCsn == NULL)
  5020. {
  5021. /* we have not seen any changes from this replica so it is
  5022. ok not to have this csn */
  5023. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5024. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5CheckMissingCSN: "
  5025. "can't locate %s csn: we have not seen any changes for replica %d\n",
  5026. csn_as_string (csn, PR_FALSE, csnStr), rid);
  5027. }
  5028. return CL5_SUCCESS;
  5029. }
  5030. ruv_get_largest_csn_for_replica (file->purgeRUV, rid, &purgeCsn);
  5031. if (purgeCsn == NULL)
  5032. {
  5033. /* changelog never contained any changes for this replica */
  5034. if (csn_compare (csn, supplierCsn) <= 0)
  5035. {
  5036. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5037. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5CheckMissingCSN: "
  5038. "the change with %s csn was never logged because it was imported "
  5039. "during replica initialization\n", csn_as_string (csn, PR_FALSE, csnStr));
  5040. }
  5041. rc = CL5_PURGED_DATA; /* XXXggood is that the correct return value? */
  5042. }
  5043. else
  5044. {
  5045. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5046. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5CheckMissingCSN: "
  5047. "change with %s csn has not yet been seen by this server; "
  5048. " last csn seen from that replica is %s\n",
  5049. csn_as_string (csn, PR_FALSE, csnStr),
  5050. csn_as_string (supplierCsn, PR_FALSE, csnStr));
  5051. }
  5052. rc = CL5_SUCCESS;
  5053. }
  5054. }
  5055. else /* we have both purge and supplier csn */
  5056. {
  5057. if (csn_compare (csn, purgeCsn) < 0) /* the csn is below the purge point */
  5058. {
  5059. rc = CL5_PURGED_DATA;
  5060. }
  5061. else
  5062. {
  5063. if (csn_compare (csn, supplierCsn) <= 0) /* we should have the data but we don't */
  5064. {
  5065. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5066. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5CheckMissingCSN: "
  5067. "change with %s csn has been purged by this server; "
  5068. "the current purge point for that replica is %s\n",
  5069. csn_as_string (csn, PR_FALSE, csnStr),
  5070. csn_as_string (purgeCsn, PR_FALSE, csnStr));
  5071. }
  5072. rc = CL5_MISSING_DATA;
  5073. }
  5074. else
  5075. {
  5076. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5077. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5CheckMissingCSN: "
  5078. "change with %s csn has not yet been seen by this server; "
  5079. " last csn seen from that replica is %s\n",
  5080. csn_as_string (csn, PR_FALSE, csnStr),
  5081. csn_as_string (supplierCsn, PR_FALSE, csnStr));
  5082. }
  5083. rc = CL5_SUCCESS;
  5084. }
  5085. }
  5086. }
  5087. if (supplierCsn)
  5088. csn_free (&supplierCsn);
  5089. if (purgeCsn)
  5090. csn_free (&purgeCsn);
  5091. return rc;
  5092. }
  5093. /* Helper functions that work with individual changelog files */
  5094. /* file name format : <replica name>_<replica generation>db{2,3,...} */
  5095. static PRBool _cl5FileName2Replica (const char *file_name, Object **replica)
  5096. {
  5097. Replica *r;
  5098. char *repl_name, *file_gen, *repl_gen;
  5099. int len;
  5100. PR_ASSERT (file_name && replica);
  5101. *replica = NULL;
  5102. /* this is database file */
  5103. if (_cl5FileEndsWith (file_name, DB_EXTENSION) ||
  5104. _cl5FileEndsWith (file_name, DB_EXTENSION_DB3) )
  5105. {
  5106. repl_name = slapi_ch_strdup (file_name);
  5107. file_gen = strstr(repl_name, FILE_SEP);
  5108. if (file_gen)
  5109. {
  5110. int extlen = strlen(DB_EXTENSION);
  5111. *file_gen = '\0';
  5112. file_gen += strlen (FILE_SEP);
  5113. len = strlen (file_gen);
  5114. if (len <= extlen + 1)
  5115. {
  5116. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5FileName2Replica "
  5117. "invalid file name (%s)\n", file_name);
  5118. }
  5119. else
  5120. {
  5121. /* get rid of the file extension */
  5122. file_gen [len - extlen - 1] = '\0';
  5123. *replica = replica_get_by_name (repl_name);
  5124. if (*replica)
  5125. {
  5126. /* check that generation matches the one in replica object */
  5127. r = (Replica*)object_get_data (*replica);
  5128. repl_gen = replica_get_generation (r);
  5129. PR_ASSERT (repl_gen);
  5130. if (strcmp (file_gen, repl_gen) != 0)
  5131. {
  5132. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5FileName2Replica "
  5133. "replica generation mismatch for replica at (%s), "
  5134. "file generation %s, new replica generation %s\n",
  5135. slapi_sdn_get_dn (replica_get_root (r)), file_gen, repl_gen);
  5136. object_release (*replica);
  5137. *replica = NULL;
  5138. }
  5139. slapi_ch_free ((void**)&repl_gen);
  5140. }
  5141. }
  5142. slapi_ch_free ((void**)&repl_name);
  5143. }
  5144. else
  5145. {
  5146. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5FileName2Replica "
  5147. "malformed file name - %s\n", file_name);
  5148. }
  5149. return PR_TRUE;
  5150. }
  5151. else
  5152. return PR_FALSE;
  5153. }
  5154. /* file name format : <replica name>_<replica generation>db{2,3} */
  5155. static char* _cl5Replica2FileName (Object *replica)
  5156. {
  5157. const char *replName;
  5158. char *replGen, *fileName;
  5159. Replica *r;
  5160. PR_ASSERT (replica);
  5161. r = (Replica*)object_get_data (replica);
  5162. PR_ASSERT (r);
  5163. replName = replica_get_name (r);
  5164. replGen = replica_get_generation (r);
  5165. fileName = _cl5MakeFileName (replName, replGen) ;
  5166. slapi_ch_free ((void**)&replGen);
  5167. return fileName;
  5168. }
  5169. static char* _cl5MakeFileName (const char *replName, const char *replGen)
  5170. {
  5171. char *fileName = slapi_ch_smprintf("%s/%s%s%s.%s",
  5172. s_cl5Desc.dbDir, replName,
  5173. FILE_SEP, replGen, DB_EXTENSION);
  5174. return fileName;
  5175. }
  5176. /* open file that corresponds to a particular database */
  5177. static int _cl5DBOpenFile (Object *replica, Object **obj, PRBool checkDups)
  5178. {
  5179. int rc;
  5180. const char *replName;
  5181. char *replGen;
  5182. Replica *r;
  5183. PR_ASSERT (replica);
  5184. r = (Replica*)object_get_data (replica);
  5185. replName = replica_get_name (r);
  5186. PR_ASSERT (replName);
  5187. replGen = replica_get_generation (r);
  5188. PR_ASSERT (replGen);
  5189. rc = _cl5DBOpenFileByReplicaName (replName, replGen, obj, checkDups);
  5190. slapi_ch_free ((void**)&replGen);
  5191. return rc;
  5192. }
  5193. static int _cl5DBOpenFileByReplicaName (const char *replName, const char *replGen,
  5194. Object **obj, PRBool checkDups)
  5195. {
  5196. int rc = CL5_SUCCESS;
  5197. Object *tmpObj;
  5198. CL5DBFile *file;
  5199. char *file_name;
  5200. PR_ASSERT (replName && replGen);
  5201. if (checkDups)
  5202. {
  5203. PR_Lock (s_cl5Desc.fileLock);
  5204. file_name = _cl5MakeFileName (replName, replGen);
  5205. tmpObj = objset_find (s_cl5Desc.dbFiles, _cl5CompareDBFile, file_name);
  5206. slapi_ch_free((void **)&file_name);
  5207. if (tmpObj) /* this file already exist */
  5208. {
  5209. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5210. "_cl5DBOpenFileByReplicaName: Found DB object %p for replica %s\n", tmpObj, replName);
  5211. /* if we were asked for file handle - keep the handle */
  5212. if (obj)
  5213. {
  5214. *obj = tmpObj;
  5215. }
  5216. else
  5217. {
  5218. object_release (tmpObj);
  5219. }
  5220. rc = CL5_SUCCESS;
  5221. goto done;
  5222. }
  5223. }
  5224. rc = _cl5NewDBFile (replName, replGen, &file);
  5225. if (rc == CL5_SUCCESS)
  5226. {
  5227. /* This creates the file but doesn't set the init flag
  5228. * The flag is set later when the purge and max ruvs are set.
  5229. * This is to prevent some thread to get file access before the
  5230. * structure is fully initialized */
  5231. rc = _cl5AddDBFile (file, &tmpObj);
  5232. if (rc == CL5_SUCCESS)
  5233. {
  5234. /* read purge RUV - done here because it needs file object rather than file pointer */
  5235. rc = _cl5ReadRUV (replGen, tmpObj, PR_TRUE);
  5236. if (rc != CL5_SUCCESS)
  5237. {
  5238. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5239. "_cl5DBOpenFileByReplicaName: failed to get purge RUV\n");
  5240. goto done;
  5241. }
  5242. /* read ruv that represents the upper bound of the changes stored in the file */
  5243. rc = _cl5ReadRUV (replGen, tmpObj, PR_FALSE);
  5244. if (rc != CL5_SUCCESS)
  5245. {
  5246. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5247. "_cl5DBOpenFileByReplicaName: failed to get upper bound RUV\n");
  5248. goto done;
  5249. }
  5250. /* Mark the DB File initialize */
  5251. _cl5DBFileInitialized(tmpObj);
  5252. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5253. "_cl5DBOpenFileByReplicaName: created new DB object %p\n", tmpObj);
  5254. if (obj)
  5255. {
  5256. *obj = tmpObj;
  5257. }
  5258. else
  5259. {
  5260. object_release (tmpObj);
  5261. }
  5262. }
  5263. }
  5264. done:;
  5265. if (rc != CL5_SUCCESS)
  5266. {
  5267. if (file)
  5268. _cl5DBCloseFile ((void**)&file);
  5269. }
  5270. if (checkDups)
  5271. {
  5272. PR_Unlock (s_cl5Desc.fileLock);
  5273. }
  5274. return rc;
  5275. }
  5276. /* adds file to the db file list */
  5277. static int _cl5AddDBFile (CL5DBFile *file, Object **obj)
  5278. {
  5279. int rc;
  5280. Object *tmpObj;
  5281. PR_ASSERT (file);
  5282. tmpObj = object_new (file, _cl5DBCloseFile);
  5283. rc = objset_add_obj(s_cl5Desc.dbFiles, tmpObj);
  5284. if (rc != OBJSET_SUCCESS)
  5285. {
  5286. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5287. "_cl5AddDBFile: failed to add db file to the list; "
  5288. "repl_objset error - %d\n", rc);
  5289. object_release (tmpObj);
  5290. return CL5_OBJSET_ERROR;
  5291. }
  5292. else
  5293. {
  5294. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5295. "_cl5AddDBFile: Added new DB object %p\n", tmpObj);
  5296. }
  5297. if (obj)
  5298. {
  5299. *obj = tmpObj;
  5300. }
  5301. else
  5302. object_release (tmpObj);
  5303. return CL5_SUCCESS;
  5304. }
  5305. static int _cl5NewDBFile (const char *replName, const char *replGen, CL5DBFile** dbFile)
  5306. {
  5307. int rc;
  5308. DB *db = NULL;
  5309. char *name;
  5310. char *semadir;
  5311. #ifdef HPUX
  5312. char cwd [PATH_MAX+1];
  5313. #endif
  5314. PR_ASSERT (replName && replGen && dbFile);
  5315. if (!dbFile) {
  5316. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5317. "_cl5NewDBFile: NULL dbFile\n");
  5318. return CL5_UNKNOWN_ERROR;
  5319. }
  5320. (*dbFile) = (CL5DBFile *)slapi_ch_calloc (1, sizeof (CL5DBFile));
  5321. if (*dbFile == NULL)
  5322. {
  5323. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5324. "_cl5NewDBFile: memory allocation failed\n");
  5325. return CL5_MEMORY_ERROR;
  5326. }
  5327. name = _cl5MakeFileName (replName, replGen);
  5328. {
  5329. /* The subname argument allows applications to have
  5330. * subdatabases, i.e., multiple databases inside of a single
  5331. * physical file. This is useful when the logical databases
  5332. * are both numerous and reasonably small, in order to
  5333. * avoid creating a large number of underlying files.
  5334. */
  5335. char *subname = NULL;
  5336. DB_ENV *dbEnv = s_cl5Desc.dbEnv;
  5337. rc = db_create(&db, dbEnv, 0);
  5338. if (0 != rc) {
  5339. goto out;
  5340. }
  5341. rc = db->set_pagesize(
  5342. db,
  5343. s_cl5Desc.dbConfig.pageSize);
  5344. if (0 != rc) {
  5345. goto out;
  5346. }
  5347. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 3300
  5348. rc = db->set_malloc(db, (void *)slapi_ch_malloc);
  5349. if (0 != rc) {
  5350. goto out;
  5351. }
  5352. #endif
  5353. DB_OPEN(s_cl5Desc.dbEnvOpenFlags,
  5354. db, NULL /* txnid */, name, subname, DB_BTREE,
  5355. DB_CREATE | DB_THREAD, s_cl5Desc.dbConfig.fileMode, rc);
  5356. }
  5357. out:
  5358. if (rc != 0)
  5359. {
  5360. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5361. "_cl5NewDBFile: db_open failed; db error - %d %s\n",
  5362. rc, db_strerror(rc));
  5363. rc = CL5_DB_ERROR;
  5364. goto done;
  5365. }
  5366. (*dbFile)->db = db;
  5367. (*dbFile)->name = name;
  5368. (*dbFile)->replName = slapi_ch_strdup (replName);
  5369. (*dbFile)->replGen = slapi_ch_strdup (replGen);
  5370. /*
  5371. * Considerations for setting up cl semaphore:
  5372. * (1) The NT version of SleepyCat uses test-and-set mutexes
  5373. * at the DB page level instead of blocking mutexes. That has
  5374. * proven to be a killer for the changelog DB, as this DB is
  5375. * accessed by multiple a reader threads (the repl thread) and
  5376. * writer threads (the server ops threads) usually at the last
  5377. * pages of the DB, due to the sequential nature of the changelog
  5378. * keys. To avoid the test-and-set mutexes, we could use semaphore
  5379. * to serialize the writers and avoid the high mutex contention
  5380. * that SleepyCat is unable to avoid.
  5381. * (2) DS 6.2 introduced the semaphore on all platforms (replaced
  5382. * the serial lock used on Windows and Linux described above).
  5383. * The number of the concurrent writes now is configurable by
  5384. * nsslapd-changelogmaxconcurrentwrites (the server needs to
  5385. * be restarted).
  5386. */
  5387. semadir = s_cl5Desc.dbDir;
  5388. #ifdef HPUX
  5389. /*
  5390. * HP sem_open() does not allow pathname component "./" or "../"
  5391. * in the semaphore name. For simplicity and to avoid doing
  5392. * chdir() in multi-thread environment, current working dir
  5393. * (log dir) is used to replace the original semaphore dir
  5394. * if it contains "./".
  5395. */
  5396. if ( strstr ( semadir, "./" ) != NULL && getcwd ( cwd, PATH_MAX+1 ) != NULL )
  5397. {
  5398. semadir = cwd;
  5399. }
  5400. #endif
  5401. if ( semadir != NULL )
  5402. {
  5403. (*dbFile)->semaName = slapi_ch_smprintf("%s/%s.sema", semadir, replName);
  5404. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5405. "_cl5NewDBFile: semaphore %s\n", (*dbFile)->semaName);
  5406. (*dbFile)->sema = PR_OpenSemaphore((*dbFile)->semaName,
  5407. PR_SEM_CREATE | PR_SEM_EXCL, 0666,
  5408. s_cl5Desc.dbConfig.maxConcurrentWrites );
  5409. slapi_log_error (SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5NewDBFile: maxConcurrentWrites=%d\n", s_cl5Desc.dbConfig.maxConcurrentWrites );
  5410. }
  5411. if ((*dbFile)->sema == NULL )
  5412. {
  5413. /* If the semaphore was left around due
  5414. * to an unclean exit last time, remove
  5415. * and re-create it.
  5416. */
  5417. if (PR_GetError() == PR_FILE_EXISTS_ERROR) {
  5418. PR_DeleteSemaphore((*dbFile)->semaName);
  5419. (*dbFile)->sema = PR_OpenSemaphore((*dbFile)->semaName,
  5420. PR_SEM_CREATE | PR_SEM_EXCL, 0666,
  5421. s_cl5Desc.dbConfig.maxConcurrentWrites );
  5422. }
  5423. /* If we still failed to create the semaphore,
  5424. * we should just error out. */
  5425. if ((*dbFile)->sema == NULL )
  5426. {
  5427. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5428. "_cl5NewDBFile: failed to create semaphore %s; NSPR error - %d\n",
  5429. (*dbFile)->semaName ? (*dbFile)->semaName : "(nil)", PR_GetError());
  5430. rc = CL5_SYSTEM_ERROR;
  5431. goto done;
  5432. }
  5433. }
  5434. /* compute number of entries in the file */
  5435. /* ONREPL - to improve performance, we keep entry count in memory
  5436. and write it down during shutdown. Problem: this will not
  5437. work with multiple processes. Do we have to worry about that?
  5438. */
  5439. if (s_cl5Desc.dbOpenMode == CL5_OPEN_NORMAL)
  5440. {
  5441. rc = _cl5GetEntryCount (*dbFile);
  5442. if (rc != CL5_SUCCESS)
  5443. {
  5444. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5445. "_cl5NewDBFile: failed to get entry count\n");
  5446. goto done;
  5447. }
  5448. }
  5449. done:
  5450. if (rc != CL5_SUCCESS)
  5451. {
  5452. _cl5DBCloseFile ((void**)dbFile);
  5453. /* slapi_ch_free accepts NULL pointer */
  5454. slapi_ch_free ((void**)&name);
  5455. slapi_ch_free ((void**)dbFile);
  5456. }
  5457. return rc;
  5458. }
  5459. static void _cl5DBCloseFile (void **data)
  5460. {
  5461. CL5DBFile *file;
  5462. int rc = 0;
  5463. PR_ASSERT (data);
  5464. file = *(CL5DBFile**)data;
  5465. PR_ASSERT (file);
  5466. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBCloseFile: "
  5467. "Closing database %s\n", file->name);
  5468. /* close the file */
  5469. /* if this is normal close or close after import, update entry count */
  5470. if ((s_cl5Desc.dbOpenMode == CL5_OPEN_NORMAL && s_cl5Desc.dbState == CL5_STATE_CLOSING) ||
  5471. s_cl5Desc.dbOpenMode == CL5_OPEN_LDIF2CL)
  5472. {
  5473. _cl5WriteEntryCount (file);
  5474. _cl5WriteRUV (file, PR_TRUE);
  5475. _cl5WriteRUV (file, PR_FALSE);
  5476. }
  5477. /* close the db */
  5478. if (file->db) {
  5479. rc = file->db->close(file->db, 0);
  5480. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5481. "_cl5DBCloseFile: "
  5482. "Closed the changelog database handle for %s "
  5483. "(rc: %d)\n", file->name, rc);
  5484. file->db = NULL;
  5485. }
  5486. if (file->flags & DB_FILE_DELETED)
  5487. {
  5488. /* We need to use the libdb API to delete the files, otherwise we'll
  5489. * run into problems when we try to checkpoint transactions later. */
  5490. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBCloseFile: "
  5491. "removing the changelog %s (flag %d)\n",
  5492. file->name, DEFAULT_DB_ENV_OP_FLAGS);
  5493. rc = s_cl5Desc.dbEnv->dbremove(s_cl5Desc.dbEnv, 0, file->name, 0,
  5494. DEFAULT_DB_ENV_OP_FLAGS);
  5495. if (rc != 0)
  5496. {
  5497. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBCloseFile: "
  5498. "failed to remove (%s) file; libdb error - %d (%s)\n",
  5499. file->name, rc, db_strerror(rc));
  5500. } else {
  5501. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBCloseFile: "
  5502. "Deleted the changelog database file %s\n", file->name);
  5503. }
  5504. }
  5505. /* slapi_ch_free accepts NULL pointer */
  5506. slapi_ch_free ((void**)&file->name);
  5507. slapi_ch_free ((void**)&file->replName);
  5508. slapi_ch_free ((void**)&file->replGen);
  5509. ruv_destroy(&file->maxRUV);
  5510. ruv_destroy(&file->purgeRUV);
  5511. file->db = NULL;
  5512. if (file->sema) {
  5513. PR_CloseSemaphore (file->sema);
  5514. PR_DeleteSemaphore (file->semaName);
  5515. file->sema = NULL;
  5516. }
  5517. slapi_ch_free ((void**)&file->semaName);
  5518. slapi_ch_free (data);
  5519. }
  5520. static int _cl5GetDBFile (Object *replica, Object **obj)
  5521. {
  5522. char *fileName;
  5523. PR_ASSERT (replica && obj);
  5524. fileName = _cl5Replica2FileName (replica);
  5525. *obj = objset_find(s_cl5Desc.dbFiles, _cl5CompareDBFile, fileName);
  5526. if (*obj)
  5527. {
  5528. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5GetDBFile: "
  5529. "found DB object %p for database %s\n", *obj, fileName);
  5530. slapi_ch_free_string(&fileName);
  5531. return CL5_SUCCESS;
  5532. }
  5533. else
  5534. {
  5535. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5GetDBFile: "
  5536. "no DB object found for database %s\n", fileName);
  5537. slapi_ch_free_string(&fileName);
  5538. return CL5_NOTFOUND;
  5539. }
  5540. }
  5541. static int _cl5GetDBFileByReplicaName (const char *replName, const char *replGen,
  5542. Object **obj)
  5543. {
  5544. char *fileName;
  5545. PR_ASSERT (replName && replGen && obj);
  5546. fileName = _cl5MakeFileName (replName, replGen);
  5547. *obj = objset_find(s_cl5Desc.dbFiles, _cl5CompareDBFile, fileName);
  5548. if (*obj)
  5549. {
  5550. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5GetDBFileByReplicaName: "
  5551. "found DB object %p for database %s\n", *obj, fileName);
  5552. slapi_ch_free_string(&fileName);
  5553. return CL5_SUCCESS;
  5554. }
  5555. else
  5556. {
  5557. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5GetDBFileByReplicaName: "
  5558. "no DB object found for database %s\n", fileName);
  5559. slapi_ch_free_string(&fileName);
  5560. return CL5_NOTFOUND;
  5561. }
  5562. }
  5563. static void _cl5DBDeleteFile (Object *obj)
  5564. {
  5565. CL5DBFile *file;
  5566. int rc = 0;
  5567. PR_ASSERT (obj);
  5568. file = (CL5DBFile*)object_get_data (obj);
  5569. PR_ASSERT (file);
  5570. file->flags |= DB_FILE_DELETED;
  5571. rc = objset_remove_obj(s_cl5Desc.dbFiles, obj);
  5572. if (rc != OBJSET_SUCCESS) {
  5573. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBDeleteFile: "
  5574. "could not find DB object %p\n", obj);
  5575. } else {
  5576. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl, "_cl5DBDeleteFile: "
  5577. "removed DB object %p\n", obj);
  5578. }
  5579. object_release (obj);
  5580. }
  5581. static void _cl5DBFileInitialized (Object *obj)
  5582. {
  5583. CL5DBFile *file;
  5584. PR_ASSERT (obj);
  5585. file = (CL5DBFile*)object_get_data (obj);
  5586. PR_ASSERT (file);
  5587. file->flags |= DB_FILE_INIT;
  5588. }
  5589. static int _cl5CompareDBFile (Object *el1, const void *el2)
  5590. {
  5591. CL5DBFile *file;
  5592. const char *name;
  5593. PR_ASSERT (el1 && el2);
  5594. file = (CL5DBFile*) object_get_data (el1);
  5595. name = (const char*) el2;
  5596. return ((file->flags & DB_FILE_INIT) ? strcmp (file->name, name) : 1);
  5597. }
  5598. /*
  5599. * return 1: true (the "filename" ends with "ext")
  5600. * return 0: false
  5601. */
  5602. static int _cl5FileEndsWith(const char *filename, const char *ext)
  5603. {
  5604. char *p = NULL;
  5605. int flen = strlen(filename);
  5606. int elen = strlen(ext);
  5607. if (0 == flen || 0 == elen)
  5608. {
  5609. return 0;
  5610. }
  5611. p = PL_strrstr(filename, ext);
  5612. if (NULL == p)
  5613. {
  5614. return 0;
  5615. }
  5616. if (p - filename + elen == flen)
  5617. {
  5618. return 1;
  5619. }
  5620. return 0;
  5621. }
  5622. static int _cl5ExportFile (PRFileDesc *prFile, Object *obj)
  5623. {
  5624. int rc;
  5625. void *iterator = NULL;
  5626. slapi_operation_parameters op = {0};
  5627. char *buff;
  5628. PRInt32 len, wlen;
  5629. CL5Entry entry;
  5630. CL5DBFile *file;
  5631. PR_ASSERT (prFile && obj);
  5632. file = (CL5DBFile*)object_get_data (obj);
  5633. PR_ASSERT (file);
  5634. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  5635. ruv_dump (file->purgeRUV, "clpurgeruv", prFile);
  5636. ruv_dump (file->maxRUV, "clmaxruv", prFile);
  5637. }
  5638. slapi_write_buffer (prFile, "\n", strlen("\n"));
  5639. entry.op = &op;
  5640. rc = _cl5GetFirstEntry (obj, &entry, &iterator, NULL);
  5641. while (rc == CL5_SUCCESS)
  5642. {
  5643. rc = _cl5Operation2LDIF (&op, file->replGen, &buff, &len);
  5644. if (rc != CL5_SUCCESS)
  5645. {
  5646. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5647. "_cl5ExportFile: failed to convert operation to ldif\n");
  5648. operation_parameters_done (&op);
  5649. break;
  5650. }
  5651. wlen = slapi_write_buffer (prFile, buff, len);
  5652. slapi_ch_free((void **)&buff);
  5653. if (wlen < len)
  5654. {
  5655. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5656. "_cl5ExportFile: failed to write to ldif file\n");
  5657. rc = CL5_SYSTEM_ERROR;
  5658. operation_parameters_done (&op);
  5659. break;
  5660. }
  5661. cl5_operation_parameters_done (&op);
  5662. rc = _cl5GetNextEntry (&entry, iterator);
  5663. }
  5664. cl5_operation_parameters_done (&op);
  5665. if (iterator)
  5666. cl5DestroyIterator (iterator);
  5667. if (rc != CL5_NOTFOUND)
  5668. {
  5669. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5670. "_cl5ExportFile: failed to retrieve changelog entry\n");
  5671. }
  5672. else
  5673. {
  5674. rc = CL5_SUCCESS;
  5675. }
  5676. return rc;
  5677. }
  5678. static PRBool _cl5ReplicaInList (Object *replica, Object **replicas)
  5679. {
  5680. int i;
  5681. PR_ASSERT (replica && replicas);
  5682. /* ONREPL I think it should be sufficient to just compare replica pointers */
  5683. for (i=0; replicas[i]; i++)
  5684. {
  5685. if (replica == replicas[i])
  5686. return PR_TRUE;
  5687. }
  5688. return PR_FALSE;
  5689. }
  5690. static char* _cl5GetHelperEntryKey (int type, char *csnStr)
  5691. {
  5692. CSN *csn= csn_new();
  5693. char *rt;
  5694. csn_set_time(csn, (time_t)type);
  5695. csn_set_replicaid(csn, 0);
  5696. rt = csn_as_string(csn, PR_FALSE, csnStr);
  5697. csn_free(&csn);
  5698. return rt;
  5699. }
  5700. static Object* _cl5GetReplica (const slapi_operation_parameters *op, const char* replGen)
  5701. {
  5702. Slapi_DN *sdn;
  5703. Object *replObj;
  5704. Replica *replica;
  5705. char *newGen;
  5706. PR_ASSERT (op && replGen);
  5707. sdn = op->target_address.sdn;
  5708. replObj = replica_get_replica_from_dn (sdn);
  5709. if (replObj)
  5710. {
  5711. /* check to see if replica generation has not change */
  5712. replica = (Replica*)object_get_data (replObj);
  5713. PR_ASSERT (replica);
  5714. newGen = replica_get_generation (replica);
  5715. PR_ASSERT (newGen);
  5716. if (strcmp (replGen, newGen) != 0)
  5717. {
  5718. object_release (replObj);
  5719. replObj = NULL;
  5720. }
  5721. slapi_ch_free ((void**)&newGen);
  5722. }
  5723. return replObj;
  5724. }
  5725. int
  5726. cl5_is_diskfull()
  5727. {
  5728. int rc;
  5729. PR_Lock(cl5_diskfull_lock);
  5730. rc = cl5_diskfull_flag;
  5731. PR_Unlock(cl5_diskfull_lock);
  5732. return rc;
  5733. }
  5734. static void
  5735. cl5_set_diskfull()
  5736. {
  5737. PR_Lock(cl5_diskfull_lock);
  5738. cl5_diskfull_flag = 1;
  5739. PR_Unlock(cl5_diskfull_lock);
  5740. }
  5741. static void
  5742. cl5_set_no_diskfull()
  5743. {
  5744. PR_Lock(cl5_diskfull_lock);
  5745. cl5_diskfull_flag = 0;
  5746. PR_Unlock(cl5_diskfull_lock);
  5747. }
  5748. int
  5749. cl5_diskspace_is_available()
  5750. {
  5751. int rval = 1;
  5752. #if defined( OS_solaris ) || defined( hpux )
  5753. struct statvfs fsbuf;
  5754. if (statvfs(s_cl5Desc.dbDir, &fsbuf) < 0)
  5755. {
  5756. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5757. "cl5_diskspace_is_available: Cannot get file system info\n");
  5758. rval = 0;
  5759. }
  5760. else
  5761. {
  5762. unsigned long fsiz = fsbuf.f_bavail * fsbuf.f_frsize;
  5763. if (fsiz < NO_DISK_SPACE)
  5764. {
  5765. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5766. "cl5_diskspace_is_available: No enough diskspace for changelog: (%u bytes free)\n", fsiz);
  5767. rval = 0;
  5768. }
  5769. else if (fsiz > MIN_DISK_SPACE)
  5770. {
  5771. /* assume recovered */
  5772. cl5_set_no_diskfull();
  5773. }
  5774. }
  5775. #endif
  5776. #if defined( linux )
  5777. struct statfs fsbuf;
  5778. if (statfs(s_cl5Desc.dbDir, &fsbuf) < 0)
  5779. {
  5780. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5781. "cl5_diskspace_is_available: Cannot get file system info\n");
  5782. rval = 0;
  5783. }
  5784. else
  5785. {
  5786. unsigned long fsiz = fsbuf.f_bavail * fsbuf.f_bsize;
  5787. if (fsiz < NO_DISK_SPACE)
  5788. {
  5789. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5790. "cl5_diskspace_is_available: No enough diskspace for changelog: (%lu bytes free)\n", fsiz);
  5791. rval = 0;
  5792. }
  5793. else if (fsiz > MIN_DISK_SPACE)
  5794. {
  5795. /* assume recovered */
  5796. cl5_set_no_diskfull();
  5797. }
  5798. }
  5799. #endif
  5800. return rval;
  5801. }
  5802. int
  5803. cl5DbDirIsEmpty(const char *dir)
  5804. {
  5805. PRDir *prDir;
  5806. PRDirEntry *prDirEntry;
  5807. int isempty = 1;
  5808. if (!dir || !*dir) {
  5809. return isempty;
  5810. }
  5811. /* assume failure means it does not exist - other failure
  5812. cases will be handled by code which attempts to create the
  5813. db in this directory */
  5814. if (PR_Access(dir, PR_ACCESS_EXISTS)) {
  5815. return isempty;
  5816. }
  5817. prDir = PR_OpenDir(dir);
  5818. if (prDir == NULL) {
  5819. return isempty; /* assume failure means does not exist */
  5820. }
  5821. while (NULL != (prDirEntry = PR_ReadDir(prDir, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
  5822. if (NULL == prDirEntry->name) { /* NSPR doesn't behave like the docs say it should */
  5823. break;
  5824. }
  5825. isempty = 0; /* found at least one "real" file */
  5826. break;
  5827. }
  5828. PR_CloseDir(prDir);
  5829. return isempty;
  5830. }
  5831. /*
  5832. * Write RUVs into the changelog;
  5833. * implemented for backup to make sure the backed up changelog contains RUVs
  5834. * Return values: 0 -- success
  5835. * 1 -- failure
  5836. */
  5837. int
  5838. cl5WriteRUV()
  5839. {
  5840. int rc = 0;
  5841. Object *file_obj = NULL;
  5842. CL5DBFile *dbfile = NULL;
  5843. int closeit = 0;
  5844. int slapd_pid = 0;
  5845. changelog5Config config;
  5846. /* read changelog configuration */
  5847. changelog5_read_config (&config);
  5848. if (config.dir == NULL) {
  5849. /* Changelog is not configured; Replication is not enabled.
  5850. * we don't have to update RUVs.
  5851. * bail out - return success */
  5852. goto bail;
  5853. }
  5854. slapd_pid = is_slapd_running();
  5855. if (slapd_pid <= 0) {
  5856. /* I'm not a server, rather a utility.
  5857. * And the server is NOT running.
  5858. * RUVs should be in the changelog.
  5859. * we don't have to update RUVs.
  5860. * bail out - return success */
  5861. goto bail;
  5862. }
  5863. if (getpid() != slapd_pid) {
  5864. /* I'm not a server, rather a utility.
  5865. * And the server IS running.
  5866. * RUVs are not in the changelog and no easy way to retrieve them.
  5867. * bail out - return failure */
  5868. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5869. "cl5WriteRUV: server (pid %d) is already running; bail.\n",
  5870. slapd_pid);
  5871. rc = 1;
  5872. goto bail;
  5873. }
  5874. /* file is stored in the changelog directory and is named
  5875. * <replica name>.ldif */
  5876. if (CL5_STATE_OPEN != s_cl5Desc.dbState) {
  5877. rc = _cl5Open(config.dir, &config.dbconfig, CL5_OPEN_NORMAL);
  5878. if (rc != CL5_SUCCESS) {
  5879. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5880. "cl5WriteRUV: failed to open changelog\n");
  5881. goto bail;
  5882. }
  5883. s_cl5Desc.dbState = CL5_STATE_OPEN; /* force to change the state */
  5884. closeit = 1; /* It had not been opened; close it */
  5885. }
  5886. file_obj = objset_first_obj(s_cl5Desc.dbFiles);
  5887. while (file_obj) {
  5888. dbfile = (CL5DBFile *)object_get_data(file_obj);
  5889. if (dbfile) {
  5890. _cl5WriteEntryCount(dbfile);
  5891. _cl5WriteRUV(dbfile, PR_TRUE);
  5892. _cl5WriteRUV(dbfile, PR_FALSE);
  5893. }
  5894. file_obj = objset_next_obj(s_cl5Desc.dbFiles, file_obj);
  5895. }
  5896. bail:
  5897. if (closeit && (CL5_STATE_OPEN == s_cl5Desc.dbState)) {
  5898. _cl5Close ();
  5899. s_cl5Desc.dbState = CL5_STATE_CLOSED; /* force to change the state */
  5900. }
  5901. changelog5_config_done(&config);
  5902. return rc;
  5903. }
  5904. /*
  5905. * Delete RUVs from the changelog;
  5906. * implemented for backup to clean up RUVs
  5907. * Return values: 0 -- success
  5908. * 1 -- failure
  5909. */
  5910. int
  5911. cl5DeleteRUV()
  5912. {
  5913. int rc = 0;
  5914. Object *file_obj = NULL;
  5915. CL5DBFile *dbfile = NULL;
  5916. int slapd_pid = 0;
  5917. int closeit = 0;
  5918. changelog5Config config;
  5919. /* read changelog configuration */
  5920. changelog5_read_config (&config);
  5921. if (config.dir == NULL) {
  5922. /* Changelog is not configured; Replication is not enabled.
  5923. * we don't have to update RUVs.
  5924. * bail out - return success */
  5925. goto bail;
  5926. }
  5927. slapd_pid = is_slapd_running();
  5928. if (slapd_pid <= 0) {
  5929. /* I'm not a server, rather a utility.
  5930. * And the server is NOT running.
  5931. * RUVs should be in the changelog.
  5932. * we don't have to update RUVs.
  5933. * bail out - return success */
  5934. goto bail;
  5935. }
  5936. if (getpid() != slapd_pid) {
  5937. /* I'm not a server, rather a utility.
  5938. * And the server IS running.
  5939. * RUVs are not in the changelog.
  5940. * bail out - return success */
  5941. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5942. "cl5DeleteRUV: server (pid %d) is already running; bail.\n",
  5943. slapd_pid);
  5944. goto bail;
  5945. }
  5946. /* file is stored in the changelog directory and is named
  5947. * <replica name>.ldif */
  5948. if (CL5_STATE_OPEN != s_cl5Desc.dbState) {
  5949. rc = _cl5Open(config.dir, &config.dbconfig, CL5_OPEN_NORMAL);
  5950. if (rc != CL5_SUCCESS) {
  5951. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5952. "cl5DeleteRUV: failed to open changelog\n");
  5953. goto bail;
  5954. }
  5955. s_cl5Desc.dbState = CL5_STATE_OPEN; /* force to change the state */
  5956. closeit = 1; /* It had been opened; no need to close */
  5957. }
  5958. file_obj = objset_first_obj(s_cl5Desc.dbFiles);
  5959. while (file_obj) {
  5960. dbfile = (CL5DBFile *)object_get_data(file_obj);
  5961. /* _cl5GetEntryCount deletes entry count after reading it */
  5962. rc = _cl5GetEntryCount(dbfile);
  5963. if (rc != CL5_SUCCESS)
  5964. {
  5965. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  5966. "cl5DeleteRUV: failed to get/delete entry count\n");
  5967. goto bail;
  5968. }
  5969. /* _cl5ReadRUV deletes RUV after reading it */
  5970. rc = _cl5ReadRUV (dbfile->replGen, file_obj, PR_TRUE);
  5971. if (rc != CL5_SUCCESS) {
  5972. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5973. "cl5DeleteRUV: failed to read/delete purge RUV\n");
  5974. goto bail;
  5975. }
  5976. rc = _cl5ReadRUV (dbfile->replGen, file_obj, PR_FALSE);
  5977. if (rc != CL5_SUCCESS) {
  5978. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  5979. "cl5DeleteRUV: failed to read/delete upper bound RUV\n");
  5980. goto bail;
  5981. }
  5982. file_obj = objset_next_obj(s_cl5Desc.dbFiles, file_obj);
  5983. }
  5984. bail:
  5985. if (file_obj) {
  5986. object_release (file_obj);
  5987. }
  5988. if (closeit && (CL5_STATE_OPEN == s_cl5Desc.dbState)) {
  5989. _cl5Close ();
  5990. s_cl5Desc.dbState = CL5_STATE_CLOSED; /* force to change the state */
  5991. }
  5992. changelog5_config_done(&config);
  5993. return rc;
  5994. }
  5995. /*
  5996. * Clean the in memory RUV, at shutdown we will write the update to the db
  5997. */
  5998. void
  5999. cl5CleanRUV(ReplicaId rid){
  6000. CL5DBFile *file;
  6001. Object *obj = NULL;
  6002. slapi_rwlock_wrlock (s_cl5Desc.stLock);
  6003. obj = objset_first_obj(s_cl5Desc.dbFiles);
  6004. while (obj){
  6005. file = (CL5DBFile *)object_get_data(obj);
  6006. ruv_delete_replica(file->purgeRUV, rid);
  6007. ruv_delete_replica(file->maxRUV, rid);
  6008. obj = objset_next_obj(s_cl5Desc.dbFiles, obj);
  6009. }
  6010. slapi_rwlock_unlock (s_cl5Desc.stLock);
  6011. }
  6012. static void free_purge_data(cleanruv_purge_data *purge_data)
  6013. {
  6014. slapi_ch_free_string(&purge_data->replGen);
  6015. slapi_ch_free((void **)&purge_data);
  6016. }
  6017. /*
  6018. * Create a thread to purge a changelog of cleaned RIDs
  6019. */
  6020. void trigger_cl_purging(cleanruv_purge_data *purge_data){
  6021. PRThread *trim_tid = NULL;
  6022. trim_tid = PR_CreateThread(PR_USER_THREAD, (VFP)(void*)trigger_cl_purging_thread,
  6023. (void *)purge_data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  6024. PR_UNJOINABLE_THREAD, DEFAULT_THREAD_STACKSIZE);
  6025. if (NULL == trim_tid){
  6026. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  6027. "trigger_cl_purging: failed to create cl trimming "
  6028. "thread; NSPR error - %d\n", PR_GetError ());
  6029. free_purge_data(purge_data);
  6030. } else {
  6031. /* need a little time for the thread to get started */
  6032. DS_Sleep(PR_SecondsToInterval(1));
  6033. }
  6034. }
  6035. /*
  6036. * Purge a changelog of entries that originated from a particular replica(rid)
  6037. */
  6038. void
  6039. trigger_cl_purging_thread(void *arg){
  6040. cleanruv_purge_data *purge_data = (cleanruv_purge_data *)arg;
  6041. /* Make sure we have a change log, and we aren't closing it */
  6042. if (s_cl5Desc.dbState == CL5_STATE_CLOSED ||
  6043. s_cl5Desc.dbState == CL5_STATE_CLOSING) {
  6044. goto free_and_return;
  6045. }
  6046. /* Bump the changelog thread count */
  6047. if (CL5_SUCCESS != _cl5AddThread()) {
  6048. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  6049. "trigger_cl_purging: Abort - failed to increment thread count "
  6050. "NSPR error - %d\n", PR_GetError ());
  6051. goto free_and_return;
  6052. }
  6053. /* Purge the changelog */
  6054. _cl5DoPurging(purge_data);
  6055. _cl5RemoveThread();
  6056. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name_cl,
  6057. "trigger_cl_purging_thread - purged changelog for (%s) rid (%d)\n",
  6058. slapi_sdn_get_dn(purge_data->suffix_sdn), purge_data->cleaned_rid);
  6059. free_and_return:
  6060. free_purge_data(purge_data);
  6061. }