cl5_api.c 192 KB

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