cl5_api.c 200 KB

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