1
0

cl5_api.c 182 KB

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