cl5_api.c 197 KB

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