alloc.c 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * alloc.c
  5. *
  6. * Extent allocs and frees
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/swap.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "aops.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "localalloc.h"
  40. #include "suballoc.h"
  41. #include "sysfile.h"
  42. #include "file.h"
  43. #include "super.h"
  44. #include "uptodate.h"
  45. #include "buffer_head_io.h"
  46. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  47. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  48. struct ocfs2_extent_block *eb);
  49. /*
  50. * Structures which describe a path through a btree, and functions to
  51. * manipulate them.
  52. *
  53. * The idea here is to be as generic as possible with the tree
  54. * manipulation code.
  55. */
  56. struct ocfs2_path_item {
  57. struct buffer_head *bh;
  58. struct ocfs2_extent_list *el;
  59. };
  60. #define OCFS2_MAX_PATH_DEPTH 5
  61. struct ocfs2_path {
  62. int p_tree_depth;
  63. struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
  64. };
  65. #define path_root_bh(_path) ((_path)->p_node[0].bh)
  66. #define path_root_el(_path) ((_path)->p_node[0].el)
  67. #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
  68. #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
  69. #define path_num_items(_path) ((_path)->p_tree_depth + 1)
  70. /*
  71. * Reset the actual path elements so that we can re-use the structure
  72. * to build another path. Generally, this involves freeing the buffer
  73. * heads.
  74. */
  75. static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  76. {
  77. int i, start = 0, depth = 0;
  78. struct ocfs2_path_item *node;
  79. if (keep_root)
  80. start = 1;
  81. for(i = start; i < path_num_items(path); i++) {
  82. node = &path->p_node[i];
  83. brelse(node->bh);
  84. node->bh = NULL;
  85. node->el = NULL;
  86. }
  87. /*
  88. * Tree depth may change during truncate, or insert. If we're
  89. * keeping the root extent list, then make sure that our path
  90. * structure reflects the proper depth.
  91. */
  92. if (keep_root)
  93. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  94. path->p_tree_depth = depth;
  95. }
  96. static void ocfs2_free_path(struct ocfs2_path *path)
  97. {
  98. if (path) {
  99. ocfs2_reinit_path(path, 0);
  100. kfree(path);
  101. }
  102. }
  103. /*
  104. * All the elements of src into dest. After this call, src could be freed
  105. * without affecting dest.
  106. *
  107. * Both paths should have the same root. Any non-root elements of dest
  108. * will be freed.
  109. */
  110. static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  111. {
  112. int i;
  113. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  114. BUG_ON(path_root_el(dest) != path_root_el(src));
  115. ocfs2_reinit_path(dest, 1);
  116. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  117. dest->p_node[i].bh = src->p_node[i].bh;
  118. dest->p_node[i].el = src->p_node[i].el;
  119. if (dest->p_node[i].bh)
  120. get_bh(dest->p_node[i].bh);
  121. }
  122. }
  123. /*
  124. * Make the *dest path the same as src and re-initialize src path to
  125. * have a root only.
  126. */
  127. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  128. {
  129. int i;
  130. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  131. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  132. brelse(dest->p_node[i].bh);
  133. dest->p_node[i].bh = src->p_node[i].bh;
  134. dest->p_node[i].el = src->p_node[i].el;
  135. src->p_node[i].bh = NULL;
  136. src->p_node[i].el = NULL;
  137. }
  138. }
  139. /*
  140. * Insert an extent block at given index.
  141. *
  142. * This will not take an additional reference on eb_bh.
  143. */
  144. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  145. struct buffer_head *eb_bh)
  146. {
  147. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  148. /*
  149. * Right now, no root bh is an extent block, so this helps
  150. * catch code errors with dinode trees. The assertion can be
  151. * safely removed if we ever need to insert extent block
  152. * structures at the root.
  153. */
  154. BUG_ON(index == 0);
  155. path->p_node[index].bh = eb_bh;
  156. path->p_node[index].el = &eb->h_list;
  157. }
  158. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  159. struct ocfs2_extent_list *root_el)
  160. {
  161. struct ocfs2_path *path;
  162. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  163. path = kzalloc(sizeof(*path), GFP_NOFS);
  164. if (path) {
  165. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  166. get_bh(root_bh);
  167. path_root_bh(path) = root_bh;
  168. path_root_el(path) = root_el;
  169. }
  170. return path;
  171. }
  172. /*
  173. * Allocate and initialize a new path based on a disk inode tree.
  174. */
  175. static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
  176. {
  177. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  178. struct ocfs2_extent_list *el = &di->id2.i_list;
  179. return ocfs2_new_path(di_bh, el);
  180. }
  181. /*
  182. * Convenience function to journal all components in a path.
  183. */
  184. static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
  185. struct ocfs2_path *path)
  186. {
  187. int i, ret = 0;
  188. if (!path)
  189. goto out;
  190. for(i = 0; i < path_num_items(path); i++) {
  191. ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
  192. OCFS2_JOURNAL_ACCESS_WRITE);
  193. if (ret < 0) {
  194. mlog_errno(ret);
  195. goto out;
  196. }
  197. }
  198. out:
  199. return ret;
  200. }
  201. /*
  202. * Return the index of the extent record which contains cluster #v_cluster.
  203. * -1 is returned if it was not found.
  204. *
  205. * Should work fine on interior and exterior nodes.
  206. */
  207. int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
  208. {
  209. int ret = -1;
  210. int i;
  211. struct ocfs2_extent_rec *rec;
  212. u32 rec_end, rec_start, clusters;
  213. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  214. rec = &el->l_recs[i];
  215. rec_start = le32_to_cpu(rec->e_cpos);
  216. clusters = ocfs2_rec_clusters(el, rec);
  217. rec_end = rec_start + clusters;
  218. if (v_cluster >= rec_start && v_cluster < rec_end) {
  219. ret = i;
  220. break;
  221. }
  222. }
  223. return ret;
  224. }
  225. enum ocfs2_contig_type {
  226. CONTIG_NONE = 0,
  227. CONTIG_LEFT,
  228. CONTIG_RIGHT,
  229. CONTIG_LEFTRIGHT,
  230. };
  231. /*
  232. * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
  233. * ocfs2_extent_contig only work properly against leaf nodes!
  234. */
  235. static int ocfs2_block_extent_contig(struct super_block *sb,
  236. struct ocfs2_extent_rec *ext,
  237. u64 blkno)
  238. {
  239. u64 blk_end = le64_to_cpu(ext->e_blkno);
  240. blk_end += ocfs2_clusters_to_blocks(sb,
  241. le16_to_cpu(ext->e_leaf_clusters));
  242. return blkno == blk_end;
  243. }
  244. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  245. struct ocfs2_extent_rec *right)
  246. {
  247. u32 left_range;
  248. left_range = le32_to_cpu(left->e_cpos) +
  249. le16_to_cpu(left->e_leaf_clusters);
  250. return (left_range == le32_to_cpu(right->e_cpos));
  251. }
  252. static enum ocfs2_contig_type
  253. ocfs2_extent_contig(struct inode *inode,
  254. struct ocfs2_extent_rec *ext,
  255. struct ocfs2_extent_rec *insert_rec)
  256. {
  257. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  258. /*
  259. * Refuse to coalesce extent records with different flag
  260. * fields - we don't want to mix unwritten extents with user
  261. * data.
  262. */
  263. if (ext->e_flags != insert_rec->e_flags)
  264. return CONTIG_NONE;
  265. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  266. ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
  267. return CONTIG_RIGHT;
  268. blkno = le64_to_cpu(ext->e_blkno);
  269. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  270. ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
  271. return CONTIG_LEFT;
  272. return CONTIG_NONE;
  273. }
  274. /*
  275. * NOTE: We can have pretty much any combination of contiguousness and
  276. * appending.
  277. *
  278. * The usefulness of APPEND_TAIL is more in that it lets us know that
  279. * we'll have to update the path to that leaf.
  280. */
  281. enum ocfs2_append_type {
  282. APPEND_NONE = 0,
  283. APPEND_TAIL,
  284. };
  285. enum ocfs2_split_type {
  286. SPLIT_NONE = 0,
  287. SPLIT_LEFT,
  288. SPLIT_RIGHT,
  289. };
  290. struct ocfs2_insert_type {
  291. enum ocfs2_split_type ins_split;
  292. enum ocfs2_append_type ins_appending;
  293. enum ocfs2_contig_type ins_contig;
  294. int ins_contig_index;
  295. int ins_tree_depth;
  296. };
  297. struct ocfs2_merge_ctxt {
  298. enum ocfs2_contig_type c_contig_type;
  299. int c_has_empty_extent;
  300. int c_split_covers_rec;
  301. };
  302. /*
  303. * How many free extents have we got before we need more meta data?
  304. */
  305. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  306. struct inode *inode,
  307. struct ocfs2_dinode *fe)
  308. {
  309. int retval;
  310. struct ocfs2_extent_list *el;
  311. struct ocfs2_extent_block *eb;
  312. struct buffer_head *eb_bh = NULL;
  313. mlog_entry_void();
  314. if (!OCFS2_IS_VALID_DINODE(fe)) {
  315. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  316. retval = -EIO;
  317. goto bail;
  318. }
  319. if (fe->i_last_eb_blk) {
  320. retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  321. &eb_bh, OCFS2_BH_CACHED, inode);
  322. if (retval < 0) {
  323. mlog_errno(retval);
  324. goto bail;
  325. }
  326. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  327. el = &eb->h_list;
  328. } else
  329. el = &fe->id2.i_list;
  330. BUG_ON(el->l_tree_depth != 0);
  331. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  332. bail:
  333. if (eb_bh)
  334. brelse(eb_bh);
  335. mlog_exit(retval);
  336. return retval;
  337. }
  338. /* expects array to already be allocated
  339. *
  340. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  341. * l_count for you
  342. */
  343. static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
  344. handle_t *handle,
  345. struct inode *inode,
  346. int wanted,
  347. struct ocfs2_alloc_context *meta_ac,
  348. struct buffer_head *bhs[])
  349. {
  350. int count, status, i;
  351. u16 suballoc_bit_start;
  352. u32 num_got;
  353. u64 first_blkno;
  354. struct ocfs2_extent_block *eb;
  355. mlog_entry_void();
  356. count = 0;
  357. while (count < wanted) {
  358. status = ocfs2_claim_metadata(osb,
  359. handle,
  360. meta_ac,
  361. wanted - count,
  362. &suballoc_bit_start,
  363. &num_got,
  364. &first_blkno);
  365. if (status < 0) {
  366. mlog_errno(status);
  367. goto bail;
  368. }
  369. for(i = count; i < (num_got + count); i++) {
  370. bhs[i] = sb_getblk(osb->sb, first_blkno);
  371. if (bhs[i] == NULL) {
  372. status = -EIO;
  373. mlog_errno(status);
  374. goto bail;
  375. }
  376. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  377. status = ocfs2_journal_access(handle, inode, bhs[i],
  378. OCFS2_JOURNAL_ACCESS_CREATE);
  379. if (status < 0) {
  380. mlog_errno(status);
  381. goto bail;
  382. }
  383. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  384. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  385. /* Ok, setup the minimal stuff here. */
  386. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  387. eb->h_blkno = cpu_to_le64(first_blkno);
  388. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  389. eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
  390. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  391. eb->h_list.l_count =
  392. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  393. suballoc_bit_start++;
  394. first_blkno++;
  395. /* We'll also be dirtied by the caller, so
  396. * this isn't absolutely necessary. */
  397. status = ocfs2_journal_dirty(handle, bhs[i]);
  398. if (status < 0) {
  399. mlog_errno(status);
  400. goto bail;
  401. }
  402. }
  403. count += num_got;
  404. }
  405. status = 0;
  406. bail:
  407. if (status < 0) {
  408. for(i = 0; i < wanted; i++) {
  409. if (bhs[i])
  410. brelse(bhs[i]);
  411. bhs[i] = NULL;
  412. }
  413. }
  414. mlog_exit(status);
  415. return status;
  416. }
  417. /*
  418. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  419. *
  420. * Returns the sum of the rightmost extent rec logical offset and
  421. * cluster count.
  422. *
  423. * ocfs2_add_branch() uses this to determine what logical cluster
  424. * value should be populated into the leftmost new branch records.
  425. *
  426. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  427. * value for the new topmost tree record.
  428. */
  429. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  430. {
  431. int i;
  432. i = le16_to_cpu(el->l_next_free_rec) - 1;
  433. return le32_to_cpu(el->l_recs[i].e_cpos) +
  434. ocfs2_rec_clusters(el, &el->l_recs[i]);
  435. }
  436. /*
  437. * Add an entire tree branch to our inode. eb_bh is the extent block
  438. * to start at, if we don't want to start the branch at the dinode
  439. * structure.
  440. *
  441. * last_eb_bh is required as we have to update it's next_leaf pointer
  442. * for the new last extent block.
  443. *
  444. * the new branch will be 'empty' in the sense that every block will
  445. * contain a single record with cluster count == 0.
  446. */
  447. static int ocfs2_add_branch(struct ocfs2_super *osb,
  448. handle_t *handle,
  449. struct inode *inode,
  450. struct buffer_head *fe_bh,
  451. struct buffer_head *eb_bh,
  452. struct buffer_head **last_eb_bh,
  453. struct ocfs2_alloc_context *meta_ac)
  454. {
  455. int status, new_blocks, i;
  456. u64 next_blkno, new_last_eb_blk;
  457. struct buffer_head *bh;
  458. struct buffer_head **new_eb_bhs = NULL;
  459. struct ocfs2_dinode *fe;
  460. struct ocfs2_extent_block *eb;
  461. struct ocfs2_extent_list *eb_el;
  462. struct ocfs2_extent_list *el;
  463. u32 new_cpos;
  464. mlog_entry_void();
  465. BUG_ON(!last_eb_bh || !*last_eb_bh);
  466. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  467. if (eb_bh) {
  468. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  469. el = &eb->h_list;
  470. } else
  471. el = &fe->id2.i_list;
  472. /* we never add a branch to a leaf. */
  473. BUG_ON(!el->l_tree_depth);
  474. new_blocks = le16_to_cpu(el->l_tree_depth);
  475. /* allocate the number of new eb blocks we need */
  476. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  477. GFP_KERNEL);
  478. if (!new_eb_bhs) {
  479. status = -ENOMEM;
  480. mlog_errno(status);
  481. goto bail;
  482. }
  483. status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
  484. meta_ac, new_eb_bhs);
  485. if (status < 0) {
  486. mlog_errno(status);
  487. goto bail;
  488. }
  489. eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
  490. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  491. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  492. * linked with the rest of the tree.
  493. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  494. *
  495. * when we leave the loop, new_last_eb_blk will point to the
  496. * newest leaf, and next_blkno will point to the topmost extent
  497. * block. */
  498. next_blkno = new_last_eb_blk = 0;
  499. for(i = 0; i < new_blocks; i++) {
  500. bh = new_eb_bhs[i];
  501. eb = (struct ocfs2_extent_block *) bh->b_data;
  502. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  503. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  504. status = -EIO;
  505. goto bail;
  506. }
  507. eb_el = &eb->h_list;
  508. status = ocfs2_journal_access(handle, inode, bh,
  509. OCFS2_JOURNAL_ACCESS_CREATE);
  510. if (status < 0) {
  511. mlog_errno(status);
  512. goto bail;
  513. }
  514. eb->h_next_leaf_blk = 0;
  515. eb_el->l_tree_depth = cpu_to_le16(i);
  516. eb_el->l_next_free_rec = cpu_to_le16(1);
  517. /*
  518. * This actually counts as an empty extent as
  519. * c_clusters == 0
  520. */
  521. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  522. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  523. /*
  524. * eb_el isn't always an interior node, but even leaf
  525. * nodes want a zero'd flags and reserved field so
  526. * this gets the whole 32 bits regardless of use.
  527. */
  528. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  529. if (!eb_el->l_tree_depth)
  530. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  531. status = ocfs2_journal_dirty(handle, bh);
  532. if (status < 0) {
  533. mlog_errno(status);
  534. goto bail;
  535. }
  536. next_blkno = le64_to_cpu(eb->h_blkno);
  537. }
  538. /* This is a bit hairy. We want to update up to three blocks
  539. * here without leaving any of them in an inconsistent state
  540. * in case of error. We don't have to worry about
  541. * journal_dirty erroring as it won't unless we've aborted the
  542. * handle (in which case we would never be here) so reserving
  543. * the write with journal_access is all we need to do. */
  544. status = ocfs2_journal_access(handle, inode, *last_eb_bh,
  545. OCFS2_JOURNAL_ACCESS_WRITE);
  546. if (status < 0) {
  547. mlog_errno(status);
  548. goto bail;
  549. }
  550. status = ocfs2_journal_access(handle, inode, fe_bh,
  551. OCFS2_JOURNAL_ACCESS_WRITE);
  552. if (status < 0) {
  553. mlog_errno(status);
  554. goto bail;
  555. }
  556. if (eb_bh) {
  557. status = ocfs2_journal_access(handle, inode, eb_bh,
  558. OCFS2_JOURNAL_ACCESS_WRITE);
  559. if (status < 0) {
  560. mlog_errno(status);
  561. goto bail;
  562. }
  563. }
  564. /* Link the new branch into the rest of the tree (el will
  565. * either be on the fe, or the extent block passed in. */
  566. i = le16_to_cpu(el->l_next_free_rec);
  567. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  568. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  569. el->l_recs[i].e_int_clusters = 0;
  570. le16_add_cpu(&el->l_next_free_rec, 1);
  571. /* fe needs a new last extent block pointer, as does the
  572. * next_leaf on the previously last-extent-block. */
  573. fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
  574. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  575. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  576. status = ocfs2_journal_dirty(handle, *last_eb_bh);
  577. if (status < 0)
  578. mlog_errno(status);
  579. status = ocfs2_journal_dirty(handle, fe_bh);
  580. if (status < 0)
  581. mlog_errno(status);
  582. if (eb_bh) {
  583. status = ocfs2_journal_dirty(handle, eb_bh);
  584. if (status < 0)
  585. mlog_errno(status);
  586. }
  587. /*
  588. * Some callers want to track the rightmost leaf so pass it
  589. * back here.
  590. */
  591. brelse(*last_eb_bh);
  592. get_bh(new_eb_bhs[0]);
  593. *last_eb_bh = new_eb_bhs[0];
  594. status = 0;
  595. bail:
  596. if (new_eb_bhs) {
  597. for (i = 0; i < new_blocks; i++)
  598. if (new_eb_bhs[i])
  599. brelse(new_eb_bhs[i]);
  600. kfree(new_eb_bhs);
  601. }
  602. mlog_exit(status);
  603. return status;
  604. }
  605. /*
  606. * adds another level to the allocation tree.
  607. * returns back the new extent block so you can add a branch to it
  608. * after this call.
  609. */
  610. static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
  611. handle_t *handle,
  612. struct inode *inode,
  613. struct buffer_head *fe_bh,
  614. struct ocfs2_alloc_context *meta_ac,
  615. struct buffer_head **ret_new_eb_bh)
  616. {
  617. int status, i;
  618. u32 new_clusters;
  619. struct buffer_head *new_eb_bh = NULL;
  620. struct ocfs2_dinode *fe;
  621. struct ocfs2_extent_block *eb;
  622. struct ocfs2_extent_list *fe_el;
  623. struct ocfs2_extent_list *eb_el;
  624. mlog_entry_void();
  625. status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
  626. &new_eb_bh);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto bail;
  630. }
  631. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  632. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  633. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  634. status = -EIO;
  635. goto bail;
  636. }
  637. eb_el = &eb->h_list;
  638. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  639. fe_el = &fe->id2.i_list;
  640. status = ocfs2_journal_access(handle, inode, new_eb_bh,
  641. OCFS2_JOURNAL_ACCESS_CREATE);
  642. if (status < 0) {
  643. mlog_errno(status);
  644. goto bail;
  645. }
  646. /* copy the fe data into the new extent block */
  647. eb_el->l_tree_depth = fe_el->l_tree_depth;
  648. eb_el->l_next_free_rec = fe_el->l_next_free_rec;
  649. for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  650. eb_el->l_recs[i] = fe_el->l_recs[i];
  651. status = ocfs2_journal_dirty(handle, new_eb_bh);
  652. if (status < 0) {
  653. mlog_errno(status);
  654. goto bail;
  655. }
  656. status = ocfs2_journal_access(handle, inode, fe_bh,
  657. OCFS2_JOURNAL_ACCESS_WRITE);
  658. if (status < 0) {
  659. mlog_errno(status);
  660. goto bail;
  661. }
  662. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  663. /* update fe now */
  664. le16_add_cpu(&fe_el->l_tree_depth, 1);
  665. fe_el->l_recs[0].e_cpos = 0;
  666. fe_el->l_recs[0].e_blkno = eb->h_blkno;
  667. fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  668. for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  669. memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  670. fe_el->l_next_free_rec = cpu_to_le16(1);
  671. /* If this is our 1st tree depth shift, then last_eb_blk
  672. * becomes the allocated extent block */
  673. if (fe_el->l_tree_depth == cpu_to_le16(1))
  674. fe->i_last_eb_blk = eb->h_blkno;
  675. status = ocfs2_journal_dirty(handle, fe_bh);
  676. if (status < 0) {
  677. mlog_errno(status);
  678. goto bail;
  679. }
  680. *ret_new_eb_bh = new_eb_bh;
  681. new_eb_bh = NULL;
  682. status = 0;
  683. bail:
  684. if (new_eb_bh)
  685. brelse(new_eb_bh);
  686. mlog_exit(status);
  687. return status;
  688. }
  689. /*
  690. * Should only be called when there is no space left in any of the
  691. * leaf nodes. What we want to do is find the lowest tree depth
  692. * non-leaf extent block with room for new records. There are three
  693. * valid results of this search:
  694. *
  695. * 1) a lowest extent block is found, then we pass it back in
  696. * *lowest_eb_bh and return '0'
  697. *
  698. * 2) the search fails to find anything, but the dinode has room. We
  699. * pass NULL back in *lowest_eb_bh, but still return '0'
  700. *
  701. * 3) the search fails to find anything AND the dinode is full, in
  702. * which case we return > 0
  703. *
  704. * return status < 0 indicates an error.
  705. */
  706. static int ocfs2_find_branch_target(struct ocfs2_super *osb,
  707. struct inode *inode,
  708. struct buffer_head *fe_bh,
  709. struct buffer_head **target_bh)
  710. {
  711. int status = 0, i;
  712. u64 blkno;
  713. struct ocfs2_dinode *fe;
  714. struct ocfs2_extent_block *eb;
  715. struct ocfs2_extent_list *el;
  716. struct buffer_head *bh = NULL;
  717. struct buffer_head *lowest_bh = NULL;
  718. mlog_entry_void();
  719. *target_bh = NULL;
  720. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  721. el = &fe->id2.i_list;
  722. while(le16_to_cpu(el->l_tree_depth) > 1) {
  723. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  724. ocfs2_error(inode->i_sb, "Dinode %llu has empty "
  725. "extent list (next_free_rec == 0)",
  726. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  727. status = -EIO;
  728. goto bail;
  729. }
  730. i = le16_to_cpu(el->l_next_free_rec) - 1;
  731. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  732. if (!blkno) {
  733. ocfs2_error(inode->i_sb, "Dinode %llu has extent "
  734. "list where extent # %d has no physical "
  735. "block start",
  736. (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
  737. status = -EIO;
  738. goto bail;
  739. }
  740. if (bh) {
  741. brelse(bh);
  742. bh = NULL;
  743. }
  744. status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
  745. inode);
  746. if (status < 0) {
  747. mlog_errno(status);
  748. goto bail;
  749. }
  750. eb = (struct ocfs2_extent_block *) bh->b_data;
  751. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  752. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  753. status = -EIO;
  754. goto bail;
  755. }
  756. el = &eb->h_list;
  757. if (le16_to_cpu(el->l_next_free_rec) <
  758. le16_to_cpu(el->l_count)) {
  759. if (lowest_bh)
  760. brelse(lowest_bh);
  761. lowest_bh = bh;
  762. get_bh(lowest_bh);
  763. }
  764. }
  765. /* If we didn't find one and the fe doesn't have any room,
  766. * then return '1' */
  767. if (!lowest_bh
  768. && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
  769. status = 1;
  770. *target_bh = lowest_bh;
  771. bail:
  772. if (bh)
  773. brelse(bh);
  774. mlog_exit(status);
  775. return status;
  776. }
  777. /*
  778. * Grow a b-tree so that it has more records.
  779. *
  780. * We might shift the tree depth in which case existing paths should
  781. * be considered invalid.
  782. *
  783. * Tree depth after the grow is returned via *final_depth.
  784. *
  785. * *last_eb_bh will be updated by ocfs2_add_branch().
  786. */
  787. static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
  788. struct buffer_head *di_bh, int *final_depth,
  789. struct buffer_head **last_eb_bh,
  790. struct ocfs2_alloc_context *meta_ac)
  791. {
  792. int ret, shift;
  793. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  794. int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
  795. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  796. struct buffer_head *bh = NULL;
  797. BUG_ON(meta_ac == NULL);
  798. shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
  799. if (shift < 0) {
  800. ret = shift;
  801. mlog_errno(ret);
  802. goto out;
  803. }
  804. /* We traveled all the way to the bottom of the allocation tree
  805. * and didn't find room for any more extents - we need to add
  806. * another tree level */
  807. if (shift) {
  808. BUG_ON(bh);
  809. mlog(0, "need to shift tree depth (current = %d)\n", depth);
  810. /* ocfs2_shift_tree_depth will return us a buffer with
  811. * the new extent block (so we can pass that to
  812. * ocfs2_add_branch). */
  813. ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
  814. meta_ac, &bh);
  815. if (ret < 0) {
  816. mlog_errno(ret);
  817. goto out;
  818. }
  819. depth++;
  820. if (depth == 1) {
  821. /*
  822. * Special case: we have room now if we shifted from
  823. * tree_depth 0, so no more work needs to be done.
  824. *
  825. * We won't be calling add_branch, so pass
  826. * back *last_eb_bh as the new leaf. At depth
  827. * zero, it should always be null so there's
  828. * no reason to brelse.
  829. */
  830. BUG_ON(*last_eb_bh);
  831. get_bh(bh);
  832. *last_eb_bh = bh;
  833. goto out;
  834. }
  835. }
  836. /* call ocfs2_add_branch to add the final part of the tree with
  837. * the new data. */
  838. mlog(0, "add branch. bh = %p\n", bh);
  839. ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
  840. meta_ac);
  841. if (ret < 0) {
  842. mlog_errno(ret);
  843. goto out;
  844. }
  845. out:
  846. if (final_depth)
  847. *final_depth = depth;
  848. brelse(bh);
  849. return ret;
  850. }
  851. /*
  852. * This is only valid for leaf nodes, which are the only ones that can
  853. * have empty extents anyway.
  854. */
  855. static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
  856. {
  857. return !rec->e_leaf_clusters;
  858. }
  859. /*
  860. * This function will discard the rightmost extent record.
  861. */
  862. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  863. {
  864. int next_free = le16_to_cpu(el->l_next_free_rec);
  865. int count = le16_to_cpu(el->l_count);
  866. unsigned int num_bytes;
  867. BUG_ON(!next_free);
  868. /* This will cause us to go off the end of our extent list. */
  869. BUG_ON(next_free >= count);
  870. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  871. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  872. }
  873. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  874. struct ocfs2_extent_rec *insert_rec)
  875. {
  876. int i, insert_index, next_free, has_empty, num_bytes;
  877. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  878. struct ocfs2_extent_rec *rec;
  879. next_free = le16_to_cpu(el->l_next_free_rec);
  880. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  881. BUG_ON(!next_free);
  882. /* The tree code before us didn't allow enough room in the leaf. */
  883. if (el->l_next_free_rec == el->l_count && !has_empty)
  884. BUG();
  885. /*
  886. * The easiest way to approach this is to just remove the
  887. * empty extent and temporarily decrement next_free.
  888. */
  889. if (has_empty) {
  890. /*
  891. * If next_free was 1 (only an empty extent), this
  892. * loop won't execute, which is fine. We still want
  893. * the decrement above to happen.
  894. */
  895. for(i = 0; i < (next_free - 1); i++)
  896. el->l_recs[i] = el->l_recs[i+1];
  897. next_free--;
  898. }
  899. /*
  900. * Figure out what the new record index should be.
  901. */
  902. for(i = 0; i < next_free; i++) {
  903. rec = &el->l_recs[i];
  904. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  905. break;
  906. }
  907. insert_index = i;
  908. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  909. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  910. BUG_ON(insert_index < 0);
  911. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  912. BUG_ON(insert_index > next_free);
  913. /*
  914. * No need to memmove if we're just adding to the tail.
  915. */
  916. if (insert_index != next_free) {
  917. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  918. num_bytes = next_free - insert_index;
  919. num_bytes *= sizeof(struct ocfs2_extent_rec);
  920. memmove(&el->l_recs[insert_index + 1],
  921. &el->l_recs[insert_index],
  922. num_bytes);
  923. }
  924. /*
  925. * Either we had an empty extent, and need to re-increment or
  926. * there was no empty extent on a non full rightmost leaf node,
  927. * in which case we still need to increment.
  928. */
  929. next_free++;
  930. el->l_next_free_rec = cpu_to_le16(next_free);
  931. /*
  932. * Make sure none of the math above just messed up our tree.
  933. */
  934. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  935. el->l_recs[insert_index] = *insert_rec;
  936. }
  937. static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
  938. {
  939. int size, num_recs = le16_to_cpu(el->l_next_free_rec);
  940. BUG_ON(num_recs == 0);
  941. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  942. num_recs--;
  943. size = num_recs * sizeof(struct ocfs2_extent_rec);
  944. memmove(&el->l_recs[0], &el->l_recs[1], size);
  945. memset(&el->l_recs[num_recs], 0,
  946. sizeof(struct ocfs2_extent_rec));
  947. el->l_next_free_rec = cpu_to_le16(num_recs);
  948. }
  949. }
  950. /*
  951. * Create an empty extent record .
  952. *
  953. * l_next_free_rec may be updated.
  954. *
  955. * If an empty extent already exists do nothing.
  956. */
  957. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  958. {
  959. int next_free = le16_to_cpu(el->l_next_free_rec);
  960. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  961. if (next_free == 0)
  962. goto set_and_inc;
  963. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  964. return;
  965. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  966. "Asked to create an empty extent in a full list:\n"
  967. "count = %u, tree depth = %u",
  968. le16_to_cpu(el->l_count),
  969. le16_to_cpu(el->l_tree_depth));
  970. ocfs2_shift_records_right(el);
  971. set_and_inc:
  972. le16_add_cpu(&el->l_next_free_rec, 1);
  973. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  974. }
  975. /*
  976. * For a rotation which involves two leaf nodes, the "root node" is
  977. * the lowest level tree node which contains a path to both leafs. This
  978. * resulting set of information can be used to form a complete "subtree"
  979. *
  980. * This function is passed two full paths from the dinode down to a
  981. * pair of adjacent leaves. It's task is to figure out which path
  982. * index contains the subtree root - this can be the root index itself
  983. * in a worst-case rotation.
  984. *
  985. * The array index of the subtree root is passed back.
  986. */
  987. static int ocfs2_find_subtree_root(struct inode *inode,
  988. struct ocfs2_path *left,
  989. struct ocfs2_path *right)
  990. {
  991. int i = 0;
  992. /*
  993. * Check that the caller passed in two paths from the same tree.
  994. */
  995. BUG_ON(path_root_bh(left) != path_root_bh(right));
  996. do {
  997. i++;
  998. /*
  999. * The caller didn't pass two adjacent paths.
  1000. */
  1001. mlog_bug_on_msg(i > left->p_tree_depth,
  1002. "Inode %lu, left depth %u, right depth %u\n"
  1003. "left leaf blk %llu, right leaf blk %llu\n",
  1004. inode->i_ino, left->p_tree_depth,
  1005. right->p_tree_depth,
  1006. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  1007. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  1008. } while (left->p_node[i].bh->b_blocknr ==
  1009. right->p_node[i].bh->b_blocknr);
  1010. return i - 1;
  1011. }
  1012. typedef void (path_insert_t)(void *, struct buffer_head *);
  1013. /*
  1014. * Traverse a btree path in search of cpos, starting at root_el.
  1015. *
  1016. * This code can be called with a cpos larger than the tree, in which
  1017. * case it will return the rightmost path.
  1018. */
  1019. static int __ocfs2_find_path(struct inode *inode,
  1020. struct ocfs2_extent_list *root_el, u32 cpos,
  1021. path_insert_t *func, void *data)
  1022. {
  1023. int i, ret = 0;
  1024. u32 range;
  1025. u64 blkno;
  1026. struct buffer_head *bh = NULL;
  1027. struct ocfs2_extent_block *eb;
  1028. struct ocfs2_extent_list *el;
  1029. struct ocfs2_extent_rec *rec;
  1030. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1031. el = root_el;
  1032. while (el->l_tree_depth) {
  1033. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1034. ocfs2_error(inode->i_sb,
  1035. "Inode %llu has empty extent list at "
  1036. "depth %u\n",
  1037. (unsigned long long)oi->ip_blkno,
  1038. le16_to_cpu(el->l_tree_depth));
  1039. ret = -EROFS;
  1040. goto out;
  1041. }
  1042. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  1043. rec = &el->l_recs[i];
  1044. /*
  1045. * In the case that cpos is off the allocation
  1046. * tree, this should just wind up returning the
  1047. * rightmost record.
  1048. */
  1049. range = le32_to_cpu(rec->e_cpos) +
  1050. ocfs2_rec_clusters(el, rec);
  1051. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1052. break;
  1053. }
  1054. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1055. if (blkno == 0) {
  1056. ocfs2_error(inode->i_sb,
  1057. "Inode %llu has bad blkno in extent list "
  1058. "at depth %u (index %d)\n",
  1059. (unsigned long long)oi->ip_blkno,
  1060. le16_to_cpu(el->l_tree_depth), i);
  1061. ret = -EROFS;
  1062. goto out;
  1063. }
  1064. brelse(bh);
  1065. bh = NULL;
  1066. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  1067. &bh, OCFS2_BH_CACHED, inode);
  1068. if (ret) {
  1069. mlog_errno(ret);
  1070. goto out;
  1071. }
  1072. eb = (struct ocfs2_extent_block *) bh->b_data;
  1073. el = &eb->h_list;
  1074. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  1075. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  1076. ret = -EIO;
  1077. goto out;
  1078. }
  1079. if (le16_to_cpu(el->l_next_free_rec) >
  1080. le16_to_cpu(el->l_count)) {
  1081. ocfs2_error(inode->i_sb,
  1082. "Inode %llu has bad count in extent list "
  1083. "at block %llu (next free=%u, count=%u)\n",
  1084. (unsigned long long)oi->ip_blkno,
  1085. (unsigned long long)bh->b_blocknr,
  1086. le16_to_cpu(el->l_next_free_rec),
  1087. le16_to_cpu(el->l_count));
  1088. ret = -EROFS;
  1089. goto out;
  1090. }
  1091. if (func)
  1092. func(data, bh);
  1093. }
  1094. out:
  1095. /*
  1096. * Catch any trailing bh that the loop didn't handle.
  1097. */
  1098. brelse(bh);
  1099. return ret;
  1100. }
  1101. /*
  1102. * Given an initialized path (that is, it has a valid root extent
  1103. * list), this function will traverse the btree in search of the path
  1104. * which would contain cpos.
  1105. *
  1106. * The path traveled is recorded in the path structure.
  1107. *
  1108. * Note that this will not do any comparisons on leaf node extent
  1109. * records, so it will work fine in the case that we just added a tree
  1110. * branch.
  1111. */
  1112. struct find_path_data {
  1113. int index;
  1114. struct ocfs2_path *path;
  1115. };
  1116. static void find_path_ins(void *data, struct buffer_head *bh)
  1117. {
  1118. struct find_path_data *fp = data;
  1119. get_bh(bh);
  1120. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  1121. fp->index++;
  1122. }
  1123. static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
  1124. u32 cpos)
  1125. {
  1126. struct find_path_data data;
  1127. data.index = 1;
  1128. data.path = path;
  1129. return __ocfs2_find_path(inode, path_root_el(path), cpos,
  1130. find_path_ins, &data);
  1131. }
  1132. static void find_leaf_ins(void *data, struct buffer_head *bh)
  1133. {
  1134. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  1135. struct ocfs2_extent_list *el = &eb->h_list;
  1136. struct buffer_head **ret = data;
  1137. /* We want to retain only the leaf block. */
  1138. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1139. get_bh(bh);
  1140. *ret = bh;
  1141. }
  1142. }
  1143. /*
  1144. * Find the leaf block in the tree which would contain cpos. No
  1145. * checking of the actual leaf is done.
  1146. *
  1147. * Some paths want to call this instead of allocating a path structure
  1148. * and calling ocfs2_find_path().
  1149. *
  1150. * This function doesn't handle non btree extent lists.
  1151. */
  1152. int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
  1153. u32 cpos, struct buffer_head **leaf_bh)
  1154. {
  1155. int ret;
  1156. struct buffer_head *bh = NULL;
  1157. ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
  1158. if (ret) {
  1159. mlog_errno(ret);
  1160. goto out;
  1161. }
  1162. *leaf_bh = bh;
  1163. out:
  1164. return ret;
  1165. }
  1166. /*
  1167. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1168. *
  1169. * Basically, we've moved stuff around at the bottom of the tree and
  1170. * we need to fix up the extent records above the changes to reflect
  1171. * the new changes.
  1172. *
  1173. * left_rec: the record on the left.
  1174. * left_child_el: is the child list pointed to by left_rec
  1175. * right_rec: the record to the right of left_rec
  1176. * right_child_el: is the child list pointed to by right_rec
  1177. *
  1178. * By definition, this only works on interior nodes.
  1179. */
  1180. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1181. struct ocfs2_extent_list *left_child_el,
  1182. struct ocfs2_extent_rec *right_rec,
  1183. struct ocfs2_extent_list *right_child_el)
  1184. {
  1185. u32 left_clusters, right_end;
  1186. /*
  1187. * Interior nodes never have holes. Their cpos is the cpos of
  1188. * the leftmost record in their child list. Their cluster
  1189. * count covers the full theoretical range of their child list
  1190. * - the range between their cpos and the cpos of the record
  1191. * immediately to their right.
  1192. */
  1193. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1194. if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
  1195. BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
  1196. left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
  1197. }
  1198. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1199. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1200. /*
  1201. * Calculate the rightmost cluster count boundary before
  1202. * moving cpos - we will need to adjust clusters after
  1203. * updating e_cpos to keep the same highest cluster count.
  1204. */
  1205. right_end = le32_to_cpu(right_rec->e_cpos);
  1206. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1207. right_rec->e_cpos = left_rec->e_cpos;
  1208. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1209. right_end -= le32_to_cpu(right_rec->e_cpos);
  1210. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1211. }
  1212. /*
  1213. * Adjust the adjacent root node records involved in a
  1214. * rotation. left_el_blkno is passed in as a key so that we can easily
  1215. * find it's index in the root list.
  1216. */
  1217. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1218. struct ocfs2_extent_list *left_el,
  1219. struct ocfs2_extent_list *right_el,
  1220. u64 left_el_blkno)
  1221. {
  1222. int i;
  1223. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1224. le16_to_cpu(left_el->l_tree_depth));
  1225. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1226. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1227. break;
  1228. }
  1229. /*
  1230. * The path walking code should have never returned a root and
  1231. * two paths which are not adjacent.
  1232. */
  1233. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1234. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1235. &root_el->l_recs[i + 1], right_el);
  1236. }
  1237. /*
  1238. * We've changed a leaf block (in right_path) and need to reflect that
  1239. * change back up the subtree.
  1240. *
  1241. * This happens in multiple places:
  1242. * - When we've moved an extent record from the left path leaf to the right
  1243. * path leaf to make room for an empty extent in the left path leaf.
  1244. * - When our insert into the right path leaf is at the leftmost edge
  1245. * and requires an update of the path immediately to it's left. This
  1246. * can occur at the end of some types of rotation and appending inserts.
  1247. */
  1248. static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
  1249. struct ocfs2_path *left_path,
  1250. struct ocfs2_path *right_path,
  1251. int subtree_index)
  1252. {
  1253. int ret, i, idx;
  1254. struct ocfs2_extent_list *el, *left_el, *right_el;
  1255. struct ocfs2_extent_rec *left_rec, *right_rec;
  1256. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1257. /*
  1258. * Update the counts and position values within all the
  1259. * interior nodes to reflect the leaf rotation we just did.
  1260. *
  1261. * The root node is handled below the loop.
  1262. *
  1263. * We begin the loop with right_el and left_el pointing to the
  1264. * leaf lists and work our way up.
  1265. *
  1266. * NOTE: within this loop, left_el and right_el always refer
  1267. * to the *child* lists.
  1268. */
  1269. left_el = path_leaf_el(left_path);
  1270. right_el = path_leaf_el(right_path);
  1271. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1272. mlog(0, "Adjust records at index %u\n", i);
  1273. /*
  1274. * One nice property of knowing that all of these
  1275. * nodes are below the root is that we only deal with
  1276. * the leftmost right node record and the rightmost
  1277. * left node record.
  1278. */
  1279. el = left_path->p_node[i].el;
  1280. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1281. left_rec = &el->l_recs[idx];
  1282. el = right_path->p_node[i].el;
  1283. right_rec = &el->l_recs[0];
  1284. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1285. right_el);
  1286. ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1287. if (ret)
  1288. mlog_errno(ret);
  1289. ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1290. if (ret)
  1291. mlog_errno(ret);
  1292. /*
  1293. * Setup our list pointers now so that the current
  1294. * parents become children in the next iteration.
  1295. */
  1296. left_el = left_path->p_node[i].el;
  1297. right_el = right_path->p_node[i].el;
  1298. }
  1299. /*
  1300. * At the root node, adjust the two adjacent records which
  1301. * begin our path to the leaves.
  1302. */
  1303. el = left_path->p_node[subtree_index].el;
  1304. left_el = left_path->p_node[subtree_index + 1].el;
  1305. right_el = right_path->p_node[subtree_index + 1].el;
  1306. ocfs2_adjust_root_records(el, left_el, right_el,
  1307. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1308. root_bh = left_path->p_node[subtree_index].bh;
  1309. ret = ocfs2_journal_dirty(handle, root_bh);
  1310. if (ret)
  1311. mlog_errno(ret);
  1312. }
  1313. static int ocfs2_rotate_subtree_right(struct inode *inode,
  1314. handle_t *handle,
  1315. struct ocfs2_path *left_path,
  1316. struct ocfs2_path *right_path,
  1317. int subtree_index)
  1318. {
  1319. int ret, i;
  1320. struct buffer_head *right_leaf_bh;
  1321. struct buffer_head *left_leaf_bh = NULL;
  1322. struct buffer_head *root_bh;
  1323. struct ocfs2_extent_list *right_el, *left_el;
  1324. struct ocfs2_extent_rec move_rec;
  1325. left_leaf_bh = path_leaf_bh(left_path);
  1326. left_el = path_leaf_el(left_path);
  1327. if (left_el->l_next_free_rec != left_el->l_count) {
  1328. ocfs2_error(inode->i_sb,
  1329. "Inode %llu has non-full interior leaf node %llu"
  1330. "(next free = %u)",
  1331. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1332. (unsigned long long)left_leaf_bh->b_blocknr,
  1333. le16_to_cpu(left_el->l_next_free_rec));
  1334. return -EROFS;
  1335. }
  1336. /*
  1337. * This extent block may already have an empty record, so we
  1338. * return early if so.
  1339. */
  1340. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1341. return 0;
  1342. root_bh = left_path->p_node[subtree_index].bh;
  1343. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1344. ret = ocfs2_journal_access(handle, inode, root_bh,
  1345. OCFS2_JOURNAL_ACCESS_WRITE);
  1346. if (ret) {
  1347. mlog_errno(ret);
  1348. goto out;
  1349. }
  1350. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1351. ret = ocfs2_journal_access(handle, inode,
  1352. right_path->p_node[i].bh,
  1353. OCFS2_JOURNAL_ACCESS_WRITE);
  1354. if (ret) {
  1355. mlog_errno(ret);
  1356. goto out;
  1357. }
  1358. ret = ocfs2_journal_access(handle, inode,
  1359. left_path->p_node[i].bh,
  1360. OCFS2_JOURNAL_ACCESS_WRITE);
  1361. if (ret) {
  1362. mlog_errno(ret);
  1363. goto out;
  1364. }
  1365. }
  1366. right_leaf_bh = path_leaf_bh(right_path);
  1367. right_el = path_leaf_el(right_path);
  1368. /* This is a code error, not a disk corruption. */
  1369. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1370. "because rightmost leaf block %llu is empty\n",
  1371. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1372. (unsigned long long)right_leaf_bh->b_blocknr);
  1373. ocfs2_create_empty_extent(right_el);
  1374. ret = ocfs2_journal_dirty(handle, right_leaf_bh);
  1375. if (ret) {
  1376. mlog_errno(ret);
  1377. goto out;
  1378. }
  1379. /* Do the copy now. */
  1380. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1381. move_rec = left_el->l_recs[i];
  1382. right_el->l_recs[0] = move_rec;
  1383. /*
  1384. * Clear out the record we just copied and shift everything
  1385. * over, leaving an empty extent in the left leaf.
  1386. *
  1387. * We temporarily subtract from next_free_rec so that the
  1388. * shift will lose the tail record (which is now defunct).
  1389. */
  1390. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1391. ocfs2_shift_records_right(left_el);
  1392. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1393. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1394. ret = ocfs2_journal_dirty(handle, left_leaf_bh);
  1395. if (ret) {
  1396. mlog_errno(ret);
  1397. goto out;
  1398. }
  1399. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1400. subtree_index);
  1401. out:
  1402. return ret;
  1403. }
  1404. /*
  1405. * Given a full path, determine what cpos value would return us a path
  1406. * containing the leaf immediately to the left of the current one.
  1407. *
  1408. * Will return zero if the path passed in is already the leftmost path.
  1409. */
  1410. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1411. struct ocfs2_path *path, u32 *cpos)
  1412. {
  1413. int i, j, ret = 0;
  1414. u64 blkno;
  1415. struct ocfs2_extent_list *el;
  1416. BUG_ON(path->p_tree_depth == 0);
  1417. *cpos = 0;
  1418. blkno = path_leaf_bh(path)->b_blocknr;
  1419. /* Start at the tree node just above the leaf and work our way up. */
  1420. i = path->p_tree_depth - 1;
  1421. while (i >= 0) {
  1422. el = path->p_node[i].el;
  1423. /*
  1424. * Find the extent record just before the one in our
  1425. * path.
  1426. */
  1427. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1428. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1429. if (j == 0) {
  1430. if (i == 0) {
  1431. /*
  1432. * We've determined that the
  1433. * path specified is already
  1434. * the leftmost one - return a
  1435. * cpos of zero.
  1436. */
  1437. goto out;
  1438. }
  1439. /*
  1440. * The leftmost record points to our
  1441. * leaf - we need to travel up the
  1442. * tree one level.
  1443. */
  1444. goto next_node;
  1445. }
  1446. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1447. *cpos = *cpos + ocfs2_rec_clusters(el,
  1448. &el->l_recs[j - 1]);
  1449. *cpos = *cpos - 1;
  1450. goto out;
  1451. }
  1452. }
  1453. /*
  1454. * If we got here, we never found a valid node where
  1455. * the tree indicated one should be.
  1456. */
  1457. ocfs2_error(sb,
  1458. "Invalid extent tree at extent block %llu\n",
  1459. (unsigned long long)blkno);
  1460. ret = -EROFS;
  1461. goto out;
  1462. next_node:
  1463. blkno = path->p_node[i].bh->b_blocknr;
  1464. i--;
  1465. }
  1466. out:
  1467. return ret;
  1468. }
  1469. /*
  1470. * Extend the transaction by enough credits to complete the rotation,
  1471. * and still leave at least the original number of credits allocated
  1472. * to this transaction.
  1473. */
  1474. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1475. int op_credits,
  1476. struct ocfs2_path *path)
  1477. {
  1478. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
  1479. if (handle->h_buffer_credits < credits)
  1480. return ocfs2_extend_trans(handle, credits);
  1481. return 0;
  1482. }
  1483. /*
  1484. * Trap the case where we're inserting into the theoretical range past
  1485. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1486. * whose cpos is less than ours into the right leaf.
  1487. *
  1488. * It's only necessary to look at the rightmost record of the left
  1489. * leaf because the logic that calls us should ensure that the
  1490. * theoretical ranges in the path components above the leaves are
  1491. * correct.
  1492. */
  1493. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1494. u32 insert_cpos)
  1495. {
  1496. struct ocfs2_extent_list *left_el;
  1497. struct ocfs2_extent_rec *rec;
  1498. int next_free;
  1499. left_el = path_leaf_el(left_path);
  1500. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1501. rec = &left_el->l_recs[next_free - 1];
  1502. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1503. return 1;
  1504. return 0;
  1505. }
  1506. static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  1507. {
  1508. int next_free = le16_to_cpu(el->l_next_free_rec);
  1509. unsigned int range;
  1510. struct ocfs2_extent_rec *rec;
  1511. if (next_free == 0)
  1512. return 0;
  1513. rec = &el->l_recs[0];
  1514. if (ocfs2_is_empty_extent(rec)) {
  1515. /* Empty list. */
  1516. if (next_free == 1)
  1517. return 0;
  1518. rec = &el->l_recs[1];
  1519. }
  1520. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1521. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1522. return 1;
  1523. return 0;
  1524. }
  1525. /*
  1526. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1527. *
  1528. * The path to the rightmost leaf should be passed in.
  1529. *
  1530. * The array is assumed to be large enough to hold an entire path (tree depth).
  1531. *
  1532. * Upon succesful return from this function:
  1533. *
  1534. * - The 'right_path' array will contain a path to the leaf block
  1535. * whose range contains e_cpos.
  1536. * - That leaf block will have a single empty extent in list index 0.
  1537. * - In the case that the rotation requires a post-insert update,
  1538. * *ret_left_path will contain a valid path which can be passed to
  1539. * ocfs2_insert_path().
  1540. */
  1541. static int ocfs2_rotate_tree_right(struct inode *inode,
  1542. handle_t *handle,
  1543. enum ocfs2_split_type split,
  1544. u32 insert_cpos,
  1545. struct ocfs2_path *right_path,
  1546. struct ocfs2_path **ret_left_path)
  1547. {
  1548. int ret, start, orig_credits = handle->h_buffer_credits;
  1549. u32 cpos;
  1550. struct ocfs2_path *left_path = NULL;
  1551. *ret_left_path = NULL;
  1552. left_path = ocfs2_new_path(path_root_bh(right_path),
  1553. path_root_el(right_path));
  1554. if (!left_path) {
  1555. ret = -ENOMEM;
  1556. mlog_errno(ret);
  1557. goto out;
  1558. }
  1559. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
  1560. if (ret) {
  1561. mlog_errno(ret);
  1562. goto out;
  1563. }
  1564. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  1565. /*
  1566. * What we want to do here is:
  1567. *
  1568. * 1) Start with the rightmost path.
  1569. *
  1570. * 2) Determine a path to the leaf block directly to the left
  1571. * of that leaf.
  1572. *
  1573. * 3) Determine the 'subtree root' - the lowest level tree node
  1574. * which contains a path to both leaves.
  1575. *
  1576. * 4) Rotate the subtree.
  1577. *
  1578. * 5) Find the next subtree by considering the left path to be
  1579. * the new right path.
  1580. *
  1581. * The check at the top of this while loop also accepts
  1582. * insert_cpos == cpos because cpos is only a _theoretical_
  1583. * value to get us the left path - insert_cpos might very well
  1584. * be filling that hole.
  1585. *
  1586. * Stop at a cpos of '0' because we either started at the
  1587. * leftmost branch (i.e., a tree with one branch and a
  1588. * rotation inside of it), or we've gone as far as we can in
  1589. * rotating subtrees.
  1590. */
  1591. while (cpos && insert_cpos <= cpos) {
  1592. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  1593. insert_cpos, cpos);
  1594. ret = ocfs2_find_path(inode, left_path, cpos);
  1595. if (ret) {
  1596. mlog_errno(ret);
  1597. goto out;
  1598. }
  1599. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  1600. path_leaf_bh(right_path),
  1601. "Inode %lu: error during insert of %u "
  1602. "(left path cpos %u) results in two identical "
  1603. "paths ending at %llu\n",
  1604. inode->i_ino, insert_cpos, cpos,
  1605. (unsigned long long)
  1606. path_leaf_bh(left_path)->b_blocknr);
  1607. if (split == SPLIT_NONE &&
  1608. ocfs2_rotate_requires_path_adjustment(left_path,
  1609. insert_cpos)) {
  1610. /*
  1611. * We've rotated the tree as much as we
  1612. * should. The rest is up to
  1613. * ocfs2_insert_path() to complete, after the
  1614. * record insertion. We indicate this
  1615. * situation by returning the left path.
  1616. *
  1617. * The reason we don't adjust the records here
  1618. * before the record insert is that an error
  1619. * later might break the rule where a parent
  1620. * record e_cpos will reflect the actual
  1621. * e_cpos of the 1st nonempty record of the
  1622. * child list.
  1623. */
  1624. *ret_left_path = left_path;
  1625. goto out_ret_path;
  1626. }
  1627. start = ocfs2_find_subtree_root(inode, left_path, right_path);
  1628. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  1629. start,
  1630. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  1631. right_path->p_tree_depth);
  1632. ret = ocfs2_extend_rotate_transaction(handle, start,
  1633. orig_credits, right_path);
  1634. if (ret) {
  1635. mlog_errno(ret);
  1636. goto out;
  1637. }
  1638. ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
  1639. right_path, start);
  1640. if (ret) {
  1641. mlog_errno(ret);
  1642. goto out;
  1643. }
  1644. if (split != SPLIT_NONE &&
  1645. ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
  1646. insert_cpos)) {
  1647. /*
  1648. * A rotate moves the rightmost left leaf
  1649. * record over to the leftmost right leaf
  1650. * slot. If we're doing an extent split
  1651. * instead of a real insert, then we have to
  1652. * check that the extent to be split wasn't
  1653. * just moved over. If it was, then we can
  1654. * exit here, passing left_path back -
  1655. * ocfs2_split_extent() is smart enough to
  1656. * search both leaves.
  1657. */
  1658. *ret_left_path = left_path;
  1659. goto out_ret_path;
  1660. }
  1661. /*
  1662. * There is no need to re-read the next right path
  1663. * as we know that it'll be our current left
  1664. * path. Optimize by copying values instead.
  1665. */
  1666. ocfs2_mv_path(right_path, left_path);
  1667. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1668. &cpos);
  1669. if (ret) {
  1670. mlog_errno(ret);
  1671. goto out;
  1672. }
  1673. }
  1674. out:
  1675. ocfs2_free_path(left_path);
  1676. out_ret_path:
  1677. return ret;
  1678. }
  1679. static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
  1680. struct ocfs2_path *path)
  1681. {
  1682. int i, idx;
  1683. struct ocfs2_extent_rec *rec;
  1684. struct ocfs2_extent_list *el;
  1685. struct ocfs2_extent_block *eb;
  1686. u32 range;
  1687. /* Path should always be rightmost. */
  1688. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  1689. BUG_ON(eb->h_next_leaf_blk != 0ULL);
  1690. el = &eb->h_list;
  1691. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  1692. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  1693. rec = &el->l_recs[idx];
  1694. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1695. for (i = 0; i < path->p_tree_depth; i++) {
  1696. el = path->p_node[i].el;
  1697. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  1698. rec = &el->l_recs[idx];
  1699. rec->e_int_clusters = cpu_to_le32(range);
  1700. le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
  1701. ocfs2_journal_dirty(handle, path->p_node[i].bh);
  1702. }
  1703. }
  1704. static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
  1705. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1706. struct ocfs2_path *path, int unlink_start)
  1707. {
  1708. int ret, i;
  1709. struct ocfs2_extent_block *eb;
  1710. struct ocfs2_extent_list *el;
  1711. struct buffer_head *bh;
  1712. for(i = unlink_start; i < path_num_items(path); i++) {
  1713. bh = path->p_node[i].bh;
  1714. eb = (struct ocfs2_extent_block *)bh->b_data;
  1715. /*
  1716. * Not all nodes might have had their final count
  1717. * decremented by the caller - handle this here.
  1718. */
  1719. el = &eb->h_list;
  1720. if (le16_to_cpu(el->l_next_free_rec) > 1) {
  1721. mlog(ML_ERROR,
  1722. "Inode %llu, attempted to remove extent block "
  1723. "%llu with %u records\n",
  1724. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1725. (unsigned long long)le64_to_cpu(eb->h_blkno),
  1726. le16_to_cpu(el->l_next_free_rec));
  1727. ocfs2_journal_dirty(handle, bh);
  1728. ocfs2_remove_from_cache(inode, bh);
  1729. continue;
  1730. }
  1731. el->l_next_free_rec = 0;
  1732. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1733. ocfs2_journal_dirty(handle, bh);
  1734. ret = ocfs2_cache_extent_block_free(dealloc, eb);
  1735. if (ret)
  1736. mlog_errno(ret);
  1737. ocfs2_remove_from_cache(inode, bh);
  1738. }
  1739. }
  1740. static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
  1741. struct ocfs2_path *left_path,
  1742. struct ocfs2_path *right_path,
  1743. int subtree_index,
  1744. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1745. {
  1746. int i;
  1747. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1748. struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
  1749. struct ocfs2_extent_list *el;
  1750. struct ocfs2_extent_block *eb;
  1751. el = path_leaf_el(left_path);
  1752. eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
  1753. for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1754. if (root_el->l_recs[i].e_blkno == eb->h_blkno)
  1755. break;
  1756. BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
  1757. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  1758. le16_add_cpu(&root_el->l_next_free_rec, -1);
  1759. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  1760. eb->h_next_leaf_blk = 0;
  1761. ocfs2_journal_dirty(handle, root_bh);
  1762. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  1763. ocfs2_unlink_path(inode, handle, dealloc, right_path,
  1764. subtree_index + 1);
  1765. }
  1766. static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
  1767. struct ocfs2_path *left_path,
  1768. struct ocfs2_path *right_path,
  1769. int subtree_index,
  1770. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1771. int *deleted)
  1772. {
  1773. int ret, i, del_right_subtree = 0, right_has_empty = 0;
  1774. struct buffer_head *root_bh, *di_bh = path_root_bh(right_path);
  1775. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1776. struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
  1777. struct ocfs2_extent_block *eb;
  1778. *deleted = 0;
  1779. right_leaf_el = path_leaf_el(right_path);
  1780. left_leaf_el = path_leaf_el(left_path);
  1781. root_bh = left_path->p_node[subtree_index].bh;
  1782. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1783. if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
  1784. return 0;
  1785. eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
  1786. if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
  1787. /*
  1788. * It's legal for us to proceed if the right leaf is
  1789. * the rightmost one and it has an empty extent. There
  1790. * are two cases to handle - whether the leaf will be
  1791. * empty after removal or not. If the leaf isn't empty
  1792. * then just remove the empty extent up front. The
  1793. * next block will handle empty leaves by flagging
  1794. * them for unlink.
  1795. *
  1796. * Non rightmost leaves will throw -EAGAIN and the
  1797. * caller can manually move the subtree and retry.
  1798. */
  1799. if (eb->h_next_leaf_blk != 0ULL)
  1800. return -EAGAIN;
  1801. if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
  1802. ret = ocfs2_journal_access(handle, inode,
  1803. path_leaf_bh(right_path),
  1804. OCFS2_JOURNAL_ACCESS_WRITE);
  1805. if (ret) {
  1806. mlog_errno(ret);
  1807. goto out;
  1808. }
  1809. ocfs2_remove_empty_extent(right_leaf_el);
  1810. } else
  1811. right_has_empty = 1;
  1812. }
  1813. if (eb->h_next_leaf_blk == 0ULL &&
  1814. le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
  1815. /*
  1816. * We have to update i_last_eb_blk during the meta
  1817. * data delete.
  1818. */
  1819. ret = ocfs2_journal_access(handle, inode, di_bh,
  1820. OCFS2_JOURNAL_ACCESS_WRITE);
  1821. if (ret) {
  1822. mlog_errno(ret);
  1823. goto out;
  1824. }
  1825. del_right_subtree = 1;
  1826. }
  1827. /*
  1828. * Getting here with an empty extent in the right path implies
  1829. * that it's the rightmost path and will be deleted.
  1830. */
  1831. BUG_ON(right_has_empty && !del_right_subtree);
  1832. ret = ocfs2_journal_access(handle, inode, root_bh,
  1833. OCFS2_JOURNAL_ACCESS_WRITE);
  1834. if (ret) {
  1835. mlog_errno(ret);
  1836. goto out;
  1837. }
  1838. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1839. ret = ocfs2_journal_access(handle, inode,
  1840. right_path->p_node[i].bh,
  1841. OCFS2_JOURNAL_ACCESS_WRITE);
  1842. if (ret) {
  1843. mlog_errno(ret);
  1844. goto out;
  1845. }
  1846. ret = ocfs2_journal_access(handle, inode,
  1847. left_path->p_node[i].bh,
  1848. OCFS2_JOURNAL_ACCESS_WRITE);
  1849. if (ret) {
  1850. mlog_errno(ret);
  1851. goto out;
  1852. }
  1853. }
  1854. if (!right_has_empty) {
  1855. /*
  1856. * Only do this if we're moving a real
  1857. * record. Otherwise, the action is delayed until
  1858. * after removal of the right path in which case we
  1859. * can do a simple shift to remove the empty extent.
  1860. */
  1861. ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
  1862. memset(&right_leaf_el->l_recs[0], 0,
  1863. sizeof(struct ocfs2_extent_rec));
  1864. }
  1865. if (eb->h_next_leaf_blk == 0ULL) {
  1866. /*
  1867. * Move recs over to get rid of empty extent, decrease
  1868. * next_free. This is allowed to remove the last
  1869. * extent in our leaf (setting l_next_free_rec to
  1870. * zero) - the delete code below won't care.
  1871. */
  1872. ocfs2_remove_empty_extent(right_leaf_el);
  1873. }
  1874. ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  1875. if (ret)
  1876. mlog_errno(ret);
  1877. ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  1878. if (ret)
  1879. mlog_errno(ret);
  1880. if (del_right_subtree) {
  1881. ocfs2_unlink_subtree(inode, handle, left_path, right_path,
  1882. subtree_index, dealloc);
  1883. ocfs2_update_edge_lengths(inode, handle, left_path);
  1884. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  1885. di->i_last_eb_blk = eb->h_blkno;
  1886. /*
  1887. * Removal of the extent in the left leaf was skipped
  1888. * above so we could delete the right path
  1889. * 1st.
  1890. */
  1891. if (right_has_empty)
  1892. ocfs2_remove_empty_extent(left_leaf_el);
  1893. ret = ocfs2_journal_dirty(handle, di_bh);
  1894. if (ret)
  1895. mlog_errno(ret);
  1896. *deleted = 1;
  1897. } else
  1898. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1899. subtree_index);
  1900. out:
  1901. return ret;
  1902. }
  1903. /*
  1904. * Given a full path, determine what cpos value would return us a path
  1905. * containing the leaf immediately to the right of the current one.
  1906. *
  1907. * Will return zero if the path passed in is already the rightmost path.
  1908. *
  1909. * This looks similar, but is subtly different to
  1910. * ocfs2_find_cpos_for_left_leaf().
  1911. */
  1912. static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  1913. struct ocfs2_path *path, u32 *cpos)
  1914. {
  1915. int i, j, ret = 0;
  1916. u64 blkno;
  1917. struct ocfs2_extent_list *el;
  1918. *cpos = 0;
  1919. if (path->p_tree_depth == 0)
  1920. return 0;
  1921. blkno = path_leaf_bh(path)->b_blocknr;
  1922. /* Start at the tree node just above the leaf and work our way up. */
  1923. i = path->p_tree_depth - 1;
  1924. while (i >= 0) {
  1925. int next_free;
  1926. el = path->p_node[i].el;
  1927. /*
  1928. * Find the extent record just after the one in our
  1929. * path.
  1930. */
  1931. next_free = le16_to_cpu(el->l_next_free_rec);
  1932. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1933. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1934. if (j == (next_free - 1)) {
  1935. if (i == 0) {
  1936. /*
  1937. * We've determined that the
  1938. * path specified is already
  1939. * the rightmost one - return a
  1940. * cpos of zero.
  1941. */
  1942. goto out;
  1943. }
  1944. /*
  1945. * The rightmost record points to our
  1946. * leaf - we need to travel up the
  1947. * tree one level.
  1948. */
  1949. goto next_node;
  1950. }
  1951. *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
  1952. goto out;
  1953. }
  1954. }
  1955. /*
  1956. * If we got here, we never found a valid node where
  1957. * the tree indicated one should be.
  1958. */
  1959. ocfs2_error(sb,
  1960. "Invalid extent tree at extent block %llu\n",
  1961. (unsigned long long)blkno);
  1962. ret = -EROFS;
  1963. goto out;
  1964. next_node:
  1965. blkno = path->p_node[i].bh->b_blocknr;
  1966. i--;
  1967. }
  1968. out:
  1969. return ret;
  1970. }
  1971. static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
  1972. handle_t *handle,
  1973. struct buffer_head *bh,
  1974. struct ocfs2_extent_list *el)
  1975. {
  1976. int ret;
  1977. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  1978. return 0;
  1979. ret = ocfs2_journal_access(handle, inode, bh,
  1980. OCFS2_JOURNAL_ACCESS_WRITE);
  1981. if (ret) {
  1982. mlog_errno(ret);
  1983. goto out;
  1984. }
  1985. ocfs2_remove_empty_extent(el);
  1986. ret = ocfs2_journal_dirty(handle, bh);
  1987. if (ret)
  1988. mlog_errno(ret);
  1989. out:
  1990. return ret;
  1991. }
  1992. static int __ocfs2_rotate_tree_left(struct inode *inode,
  1993. handle_t *handle, int orig_credits,
  1994. struct ocfs2_path *path,
  1995. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1996. struct ocfs2_path **empty_extent_path)
  1997. {
  1998. int ret, subtree_root, deleted;
  1999. u32 right_cpos;
  2000. struct ocfs2_path *left_path = NULL;
  2001. struct ocfs2_path *right_path = NULL;
  2002. BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
  2003. *empty_extent_path = NULL;
  2004. ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
  2005. &right_cpos);
  2006. if (ret) {
  2007. mlog_errno(ret);
  2008. goto out;
  2009. }
  2010. left_path = ocfs2_new_path(path_root_bh(path),
  2011. path_root_el(path));
  2012. if (!left_path) {
  2013. ret = -ENOMEM;
  2014. mlog_errno(ret);
  2015. goto out;
  2016. }
  2017. ocfs2_cp_path(left_path, path);
  2018. right_path = ocfs2_new_path(path_root_bh(path),
  2019. path_root_el(path));
  2020. if (!right_path) {
  2021. ret = -ENOMEM;
  2022. mlog_errno(ret);
  2023. goto out;
  2024. }
  2025. while (right_cpos) {
  2026. ret = ocfs2_find_path(inode, right_path, right_cpos);
  2027. if (ret) {
  2028. mlog_errno(ret);
  2029. goto out;
  2030. }
  2031. subtree_root = ocfs2_find_subtree_root(inode, left_path,
  2032. right_path);
  2033. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2034. subtree_root,
  2035. (unsigned long long)
  2036. right_path->p_node[subtree_root].bh->b_blocknr,
  2037. right_path->p_tree_depth);
  2038. ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
  2039. orig_credits, left_path);
  2040. if (ret) {
  2041. mlog_errno(ret);
  2042. goto out;
  2043. }
  2044. ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
  2045. right_path, subtree_root,
  2046. dealloc, &deleted);
  2047. if (ret == -EAGAIN) {
  2048. /*
  2049. * The rotation has to temporarily stop due to
  2050. * the right subtree having an empty
  2051. * extent. Pass it back to the caller for a
  2052. * fixup.
  2053. */
  2054. *empty_extent_path = right_path;
  2055. right_path = NULL;
  2056. goto out;
  2057. }
  2058. if (ret) {
  2059. mlog_errno(ret);
  2060. goto out;
  2061. }
  2062. /*
  2063. * The subtree rotate might have removed records on
  2064. * the rightmost edge. If so, then rotation is
  2065. * complete.
  2066. */
  2067. if (deleted)
  2068. break;
  2069. ocfs2_mv_path(left_path, right_path);
  2070. ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
  2071. &right_cpos);
  2072. if (ret) {
  2073. mlog_errno(ret);
  2074. goto out;
  2075. }
  2076. }
  2077. out:
  2078. ocfs2_free_path(right_path);
  2079. ocfs2_free_path(left_path);
  2080. return ret;
  2081. }
  2082. static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
  2083. struct ocfs2_path *path,
  2084. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2085. {
  2086. int ret, subtree_index;
  2087. u32 cpos;
  2088. struct ocfs2_path *left_path = NULL;
  2089. struct ocfs2_dinode *di;
  2090. struct ocfs2_extent_block *eb;
  2091. struct ocfs2_extent_list *el;
  2092. /*
  2093. * XXX: This code assumes that the root is an inode, which is
  2094. * true for now but may change as tree code gets generic.
  2095. */
  2096. di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
  2097. if (!OCFS2_IS_VALID_DINODE(di)) {
  2098. ret = -EIO;
  2099. ocfs2_error(inode->i_sb,
  2100. "Inode %llu has invalid path root",
  2101. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2102. goto out;
  2103. }
  2104. /*
  2105. * There's two ways we handle this depending on
  2106. * whether path is the only existing one.
  2107. */
  2108. ret = ocfs2_extend_rotate_transaction(handle, 0,
  2109. handle->h_buffer_credits,
  2110. path);
  2111. if (ret) {
  2112. mlog_errno(ret);
  2113. goto out;
  2114. }
  2115. ret = ocfs2_journal_access_path(inode, handle, path);
  2116. if (ret) {
  2117. mlog_errno(ret);
  2118. goto out;
  2119. }
  2120. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  2121. if (ret) {
  2122. mlog_errno(ret);
  2123. goto out;
  2124. }
  2125. if (cpos) {
  2126. /*
  2127. * We have a path to the left of this one - it needs
  2128. * an update too.
  2129. */
  2130. left_path = ocfs2_new_path(path_root_bh(path),
  2131. path_root_el(path));
  2132. if (!left_path) {
  2133. ret = -ENOMEM;
  2134. mlog_errno(ret);
  2135. goto out;
  2136. }
  2137. ret = ocfs2_find_path(inode, left_path, cpos);
  2138. if (ret) {
  2139. mlog_errno(ret);
  2140. goto out;
  2141. }
  2142. ret = ocfs2_journal_access_path(inode, handle, left_path);
  2143. if (ret) {
  2144. mlog_errno(ret);
  2145. goto out;
  2146. }
  2147. subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
  2148. ocfs2_unlink_subtree(inode, handle, left_path, path,
  2149. subtree_index, dealloc);
  2150. ocfs2_update_edge_lengths(inode, handle, left_path);
  2151. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2152. di->i_last_eb_blk = eb->h_blkno;
  2153. } else {
  2154. /*
  2155. * 'path' is also the leftmost path which
  2156. * means it must be the only one. This gets
  2157. * handled differently because we want to
  2158. * revert the inode back to having extents
  2159. * in-line.
  2160. */
  2161. ocfs2_unlink_path(inode, handle, dealloc, path, 1);
  2162. el = &di->id2.i_list;
  2163. el->l_tree_depth = 0;
  2164. el->l_next_free_rec = 0;
  2165. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2166. di->i_last_eb_blk = 0;
  2167. }
  2168. ocfs2_journal_dirty(handle, path_root_bh(path));
  2169. out:
  2170. ocfs2_free_path(left_path);
  2171. return ret;
  2172. }
  2173. /*
  2174. * Left rotation of btree records.
  2175. *
  2176. * In many ways, this is (unsurprisingly) the opposite of right
  2177. * rotation. We start at some non-rightmost path containing an empty
  2178. * extent in the leaf block. The code works its way to the rightmost
  2179. * path by rotating records to the left in every subtree.
  2180. *
  2181. * This is used by any code which reduces the number of extent records
  2182. * in a leaf. After removal, an empty record should be placed in the
  2183. * leftmost list position.
  2184. *
  2185. * This won't handle a length update of the rightmost path records if
  2186. * the rightmost tree leaf record is removed so the caller is
  2187. * responsible for detecting and correcting that.
  2188. */
  2189. static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
  2190. struct ocfs2_path *path,
  2191. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2192. {
  2193. int ret, orig_credits = handle->h_buffer_credits;
  2194. struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
  2195. struct ocfs2_extent_block *eb;
  2196. struct ocfs2_extent_list *el;
  2197. el = path_leaf_el(path);
  2198. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2199. return 0;
  2200. if (path->p_tree_depth == 0) {
  2201. rightmost_no_delete:
  2202. /*
  2203. * In-inode extents. This is trivially handled, so do
  2204. * it up front.
  2205. */
  2206. ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
  2207. path_leaf_bh(path),
  2208. path_leaf_el(path));
  2209. if (ret)
  2210. mlog_errno(ret);
  2211. goto out;
  2212. }
  2213. /*
  2214. * Handle rightmost branch now. There's several cases:
  2215. * 1) simple rotation leaving records in there. That's trivial.
  2216. * 2) rotation requiring a branch delete - there's no more
  2217. * records left. Two cases of this:
  2218. * a) There are branches to the left.
  2219. * b) This is also the leftmost (the only) branch.
  2220. *
  2221. * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
  2222. * 2a) we need the left branch so that we can update it with the unlink
  2223. * 2b) we need to bring the inode back to inline extents.
  2224. */
  2225. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2226. el = &eb->h_list;
  2227. if (eb->h_next_leaf_blk == 0) {
  2228. /*
  2229. * This gets a bit tricky if we're going to delete the
  2230. * rightmost path. Get the other cases out of the way
  2231. * 1st.
  2232. */
  2233. if (le16_to_cpu(el->l_next_free_rec) > 1)
  2234. goto rightmost_no_delete;
  2235. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  2236. ret = -EIO;
  2237. ocfs2_error(inode->i_sb,
  2238. "Inode %llu has empty extent block at %llu",
  2239. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2240. (unsigned long long)le64_to_cpu(eb->h_blkno));
  2241. goto out;
  2242. }
  2243. /*
  2244. * XXX: The caller can not trust "path" any more after
  2245. * this as it will have been deleted. What do we do?
  2246. *
  2247. * In theory the rotate-for-merge code will never get
  2248. * here because it'll always ask for a rotate in a
  2249. * nonempty list.
  2250. */
  2251. ret = ocfs2_remove_rightmost_path(inode, handle, path,
  2252. dealloc);
  2253. if (ret)
  2254. mlog_errno(ret);
  2255. goto out;
  2256. }
  2257. /*
  2258. * Now we can loop, remembering the path we get from -EAGAIN
  2259. * and restarting from there.
  2260. */
  2261. try_rotate:
  2262. ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
  2263. dealloc, &restart_path);
  2264. if (ret && ret != -EAGAIN) {
  2265. mlog_errno(ret);
  2266. goto out;
  2267. }
  2268. while (ret == -EAGAIN) {
  2269. tmp_path = restart_path;
  2270. restart_path = NULL;
  2271. ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
  2272. tmp_path, dealloc,
  2273. &restart_path);
  2274. if (ret && ret != -EAGAIN) {
  2275. mlog_errno(ret);
  2276. goto out;
  2277. }
  2278. ocfs2_free_path(tmp_path);
  2279. tmp_path = NULL;
  2280. if (ret == 0)
  2281. goto try_rotate;
  2282. }
  2283. out:
  2284. ocfs2_free_path(tmp_path);
  2285. ocfs2_free_path(restart_path);
  2286. return ret;
  2287. }
  2288. static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
  2289. int index)
  2290. {
  2291. struct ocfs2_extent_rec *rec = &el->l_recs[index];
  2292. unsigned int size;
  2293. if (rec->e_leaf_clusters == 0) {
  2294. /*
  2295. * We consumed all of the merged-from record. An empty
  2296. * extent cannot exist anywhere but the 1st array
  2297. * position, so move things over if the merged-from
  2298. * record doesn't occupy that position.
  2299. *
  2300. * This creates a new empty extent so the caller
  2301. * should be smart enough to have removed any existing
  2302. * ones.
  2303. */
  2304. if (index > 0) {
  2305. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2306. size = index * sizeof(struct ocfs2_extent_rec);
  2307. memmove(&el->l_recs[1], &el->l_recs[0], size);
  2308. }
  2309. /*
  2310. * Always memset - the caller doesn't check whether it
  2311. * created an empty extent, so there could be junk in
  2312. * the other fields.
  2313. */
  2314. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2315. }
  2316. }
  2317. /*
  2318. * Remove split_rec clusters from the record at index and merge them
  2319. * onto the beginning of the record at index + 1.
  2320. */
  2321. static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh,
  2322. handle_t *handle,
  2323. struct ocfs2_extent_rec *split_rec,
  2324. struct ocfs2_extent_list *el, int index)
  2325. {
  2326. int ret;
  2327. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2328. struct ocfs2_extent_rec *left_rec;
  2329. struct ocfs2_extent_rec *right_rec;
  2330. BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
  2331. left_rec = &el->l_recs[index];
  2332. right_rec = &el->l_recs[index + 1];
  2333. ret = ocfs2_journal_access(handle, inode, bh,
  2334. OCFS2_JOURNAL_ACCESS_WRITE);
  2335. if (ret) {
  2336. mlog_errno(ret);
  2337. goto out;
  2338. }
  2339. le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
  2340. le32_add_cpu(&right_rec->e_cpos, -split_clusters);
  2341. le64_add_cpu(&right_rec->e_blkno,
  2342. -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
  2343. le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
  2344. ocfs2_cleanup_merge(el, index);
  2345. ret = ocfs2_journal_dirty(handle, bh);
  2346. if (ret)
  2347. mlog_errno(ret);
  2348. out:
  2349. return ret;
  2350. }
  2351. /*
  2352. * Remove split_rec clusters from the record at index and merge them
  2353. * onto the tail of the record at index - 1.
  2354. */
  2355. static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh,
  2356. handle_t *handle,
  2357. struct ocfs2_extent_rec *split_rec,
  2358. struct ocfs2_extent_list *el, int index)
  2359. {
  2360. int ret, has_empty_extent = 0;
  2361. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2362. struct ocfs2_extent_rec *left_rec;
  2363. struct ocfs2_extent_rec *right_rec;
  2364. BUG_ON(index <= 0);
  2365. left_rec = &el->l_recs[index - 1];
  2366. right_rec = &el->l_recs[index];
  2367. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  2368. has_empty_extent = 1;
  2369. ret = ocfs2_journal_access(handle, inode, bh,
  2370. OCFS2_JOURNAL_ACCESS_WRITE);
  2371. if (ret) {
  2372. mlog_errno(ret);
  2373. goto out;
  2374. }
  2375. if (has_empty_extent && index == 1) {
  2376. /*
  2377. * The easy case - we can just plop the record right in.
  2378. */
  2379. *left_rec = *split_rec;
  2380. has_empty_extent = 0;
  2381. } else {
  2382. le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
  2383. }
  2384. le32_add_cpu(&right_rec->e_cpos, split_clusters);
  2385. le64_add_cpu(&right_rec->e_blkno,
  2386. ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
  2387. le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
  2388. ocfs2_cleanup_merge(el, index);
  2389. ret = ocfs2_journal_dirty(handle, bh);
  2390. if (ret)
  2391. mlog_errno(ret);
  2392. out:
  2393. return ret;
  2394. }
  2395. static int ocfs2_try_to_merge_extent(struct inode *inode,
  2396. handle_t *handle,
  2397. struct ocfs2_path *left_path,
  2398. int split_index,
  2399. struct ocfs2_extent_rec *split_rec,
  2400. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2401. struct ocfs2_merge_ctxt *ctxt)
  2402. {
  2403. int ret = 0;
  2404. struct ocfs2_extent_list *el = path_leaf_el(left_path);
  2405. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  2406. BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
  2407. if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
  2408. /*
  2409. * The merge code will need to create an empty
  2410. * extent to take the place of the newly
  2411. * emptied slot. Remove any pre-existing empty
  2412. * extents - having more than one in a leaf is
  2413. * illegal.
  2414. */
  2415. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2416. dealloc);
  2417. if (ret) {
  2418. mlog_errno(ret);
  2419. goto out;
  2420. }
  2421. split_index--;
  2422. rec = &el->l_recs[split_index];
  2423. }
  2424. if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
  2425. /*
  2426. * Left-right contig implies this.
  2427. */
  2428. BUG_ON(!ctxt->c_split_covers_rec);
  2429. BUG_ON(split_index == 0);
  2430. /*
  2431. * Since the leftright insert always covers the entire
  2432. * extent, this call will delete the insert record
  2433. * entirely, resulting in an empty extent record added to
  2434. * the extent block.
  2435. *
  2436. * Since the adding of an empty extent shifts
  2437. * everything back to the right, there's no need to
  2438. * update split_index here.
  2439. */
  2440. ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path),
  2441. handle, split_rec, el, split_index);
  2442. if (ret) {
  2443. mlog_errno(ret);
  2444. goto out;
  2445. }
  2446. /*
  2447. * We can only get this from logic error above.
  2448. */
  2449. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  2450. /*
  2451. * The left merge left us with an empty extent, remove
  2452. * it.
  2453. */
  2454. ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc);
  2455. if (ret) {
  2456. mlog_errno(ret);
  2457. goto out;
  2458. }
  2459. split_index--;
  2460. rec = &el->l_recs[split_index];
  2461. /*
  2462. * Note that we don't pass split_rec here on purpose -
  2463. * we've merged it into the left side.
  2464. */
  2465. ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path),
  2466. handle, rec, el, split_index);
  2467. if (ret) {
  2468. mlog_errno(ret);
  2469. goto out;
  2470. }
  2471. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  2472. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2473. dealloc);
  2474. /*
  2475. * Error from this last rotate is not critical, so
  2476. * print but don't bubble it up.
  2477. */
  2478. if (ret)
  2479. mlog_errno(ret);
  2480. ret = 0;
  2481. } else {
  2482. /*
  2483. * Merge a record to the left or right.
  2484. *
  2485. * 'contig_type' is relative to the existing record,
  2486. * so for example, if we're "right contig", it's to
  2487. * the record on the left (hence the left merge).
  2488. */
  2489. if (ctxt->c_contig_type == CONTIG_RIGHT) {
  2490. ret = ocfs2_merge_rec_left(inode,
  2491. path_leaf_bh(left_path),
  2492. handle, split_rec, el,
  2493. split_index);
  2494. if (ret) {
  2495. mlog_errno(ret);
  2496. goto out;
  2497. }
  2498. } else {
  2499. ret = ocfs2_merge_rec_right(inode,
  2500. path_leaf_bh(left_path),
  2501. handle, split_rec, el,
  2502. split_index);
  2503. if (ret) {
  2504. mlog_errno(ret);
  2505. goto out;
  2506. }
  2507. }
  2508. if (ctxt->c_split_covers_rec) {
  2509. /*
  2510. * The merge may have left an empty extent in
  2511. * our leaf. Try to rotate it away.
  2512. */
  2513. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2514. dealloc);
  2515. if (ret)
  2516. mlog_errno(ret);
  2517. ret = 0;
  2518. }
  2519. }
  2520. out:
  2521. return ret;
  2522. }
  2523. static void ocfs2_subtract_from_rec(struct super_block *sb,
  2524. enum ocfs2_split_type split,
  2525. struct ocfs2_extent_rec *rec,
  2526. struct ocfs2_extent_rec *split_rec)
  2527. {
  2528. u64 len_blocks;
  2529. len_blocks = ocfs2_clusters_to_blocks(sb,
  2530. le16_to_cpu(split_rec->e_leaf_clusters));
  2531. if (split == SPLIT_LEFT) {
  2532. /*
  2533. * Region is on the left edge of the existing
  2534. * record.
  2535. */
  2536. le32_add_cpu(&rec->e_cpos,
  2537. le16_to_cpu(split_rec->e_leaf_clusters));
  2538. le64_add_cpu(&rec->e_blkno, len_blocks);
  2539. le16_add_cpu(&rec->e_leaf_clusters,
  2540. -le16_to_cpu(split_rec->e_leaf_clusters));
  2541. } else {
  2542. /*
  2543. * Region is on the right edge of the existing
  2544. * record.
  2545. */
  2546. le16_add_cpu(&rec->e_leaf_clusters,
  2547. -le16_to_cpu(split_rec->e_leaf_clusters));
  2548. }
  2549. }
  2550. /*
  2551. * Do the final bits of extent record insertion at the target leaf
  2552. * list. If this leaf is part of an allocation tree, it is assumed
  2553. * that the tree above has been prepared.
  2554. */
  2555. static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
  2556. struct ocfs2_extent_list *el,
  2557. struct ocfs2_insert_type *insert,
  2558. struct inode *inode)
  2559. {
  2560. int i = insert->ins_contig_index;
  2561. unsigned int range;
  2562. struct ocfs2_extent_rec *rec;
  2563. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  2564. if (insert->ins_split != SPLIT_NONE) {
  2565. i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
  2566. BUG_ON(i == -1);
  2567. rec = &el->l_recs[i];
  2568. ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
  2569. insert_rec);
  2570. goto rotate;
  2571. }
  2572. /*
  2573. * Contiguous insert - either left or right.
  2574. */
  2575. if (insert->ins_contig != CONTIG_NONE) {
  2576. rec = &el->l_recs[i];
  2577. if (insert->ins_contig == CONTIG_LEFT) {
  2578. rec->e_blkno = insert_rec->e_blkno;
  2579. rec->e_cpos = insert_rec->e_cpos;
  2580. }
  2581. le16_add_cpu(&rec->e_leaf_clusters,
  2582. le16_to_cpu(insert_rec->e_leaf_clusters));
  2583. return;
  2584. }
  2585. /*
  2586. * Handle insert into an empty leaf.
  2587. */
  2588. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  2589. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  2590. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  2591. el->l_recs[0] = *insert_rec;
  2592. el->l_next_free_rec = cpu_to_le16(1);
  2593. return;
  2594. }
  2595. /*
  2596. * Appending insert.
  2597. */
  2598. if (insert->ins_appending == APPEND_TAIL) {
  2599. i = le16_to_cpu(el->l_next_free_rec) - 1;
  2600. rec = &el->l_recs[i];
  2601. range = le32_to_cpu(rec->e_cpos)
  2602. + le16_to_cpu(rec->e_leaf_clusters);
  2603. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  2604. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  2605. le16_to_cpu(el->l_count),
  2606. "inode %lu, depth %u, count %u, next free %u, "
  2607. "rec.cpos %u, rec.clusters %u, "
  2608. "insert.cpos %u, insert.clusters %u\n",
  2609. inode->i_ino,
  2610. le16_to_cpu(el->l_tree_depth),
  2611. le16_to_cpu(el->l_count),
  2612. le16_to_cpu(el->l_next_free_rec),
  2613. le32_to_cpu(el->l_recs[i].e_cpos),
  2614. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  2615. le32_to_cpu(insert_rec->e_cpos),
  2616. le16_to_cpu(insert_rec->e_leaf_clusters));
  2617. i++;
  2618. el->l_recs[i] = *insert_rec;
  2619. le16_add_cpu(&el->l_next_free_rec, 1);
  2620. return;
  2621. }
  2622. rotate:
  2623. /*
  2624. * Ok, we have to rotate.
  2625. *
  2626. * At this point, it is safe to assume that inserting into an
  2627. * empty leaf and appending to a leaf have both been handled
  2628. * above.
  2629. *
  2630. * This leaf needs to have space, either by the empty 1st
  2631. * extent record, or by virtue of an l_next_rec < l_count.
  2632. */
  2633. ocfs2_rotate_leaf(el, insert_rec);
  2634. }
  2635. static inline void ocfs2_update_dinode_clusters(struct inode *inode,
  2636. struct ocfs2_dinode *di,
  2637. u32 clusters)
  2638. {
  2639. le32_add_cpu(&di->i_clusters, clusters);
  2640. spin_lock(&OCFS2_I(inode)->ip_lock);
  2641. OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
  2642. spin_unlock(&OCFS2_I(inode)->ip_lock);
  2643. }
  2644. static void ocfs2_adjust_rightmost_records(struct inode *inode,
  2645. handle_t *handle,
  2646. struct ocfs2_path *path,
  2647. struct ocfs2_extent_rec *insert_rec)
  2648. {
  2649. int ret, i, next_free;
  2650. struct buffer_head *bh;
  2651. struct ocfs2_extent_list *el;
  2652. struct ocfs2_extent_rec *rec;
  2653. /*
  2654. * Update everything except the leaf block.
  2655. */
  2656. for (i = 0; i < path->p_tree_depth; i++) {
  2657. bh = path->p_node[i].bh;
  2658. el = path->p_node[i].el;
  2659. next_free = le16_to_cpu(el->l_next_free_rec);
  2660. if (next_free == 0) {
  2661. ocfs2_error(inode->i_sb,
  2662. "Dinode %llu has a bad extent list",
  2663. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2664. ret = -EIO;
  2665. return;
  2666. }
  2667. rec = &el->l_recs[next_free - 1];
  2668. rec->e_int_clusters = insert_rec->e_cpos;
  2669. le32_add_cpu(&rec->e_int_clusters,
  2670. le16_to_cpu(insert_rec->e_leaf_clusters));
  2671. le32_add_cpu(&rec->e_int_clusters,
  2672. -le32_to_cpu(rec->e_cpos));
  2673. ret = ocfs2_journal_dirty(handle, bh);
  2674. if (ret)
  2675. mlog_errno(ret);
  2676. }
  2677. }
  2678. static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
  2679. struct ocfs2_extent_rec *insert_rec,
  2680. struct ocfs2_path *right_path,
  2681. struct ocfs2_path **ret_left_path)
  2682. {
  2683. int ret, next_free;
  2684. struct ocfs2_extent_list *el;
  2685. struct ocfs2_path *left_path = NULL;
  2686. *ret_left_path = NULL;
  2687. /*
  2688. * This shouldn't happen for non-trees. The extent rec cluster
  2689. * count manipulation below only works for interior nodes.
  2690. */
  2691. BUG_ON(right_path->p_tree_depth == 0);
  2692. /*
  2693. * If our appending insert is at the leftmost edge of a leaf,
  2694. * then we might need to update the rightmost records of the
  2695. * neighboring path.
  2696. */
  2697. el = path_leaf_el(right_path);
  2698. next_free = le16_to_cpu(el->l_next_free_rec);
  2699. if (next_free == 0 ||
  2700. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  2701. u32 left_cpos;
  2702. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  2703. &left_cpos);
  2704. if (ret) {
  2705. mlog_errno(ret);
  2706. goto out;
  2707. }
  2708. mlog(0, "Append may need a left path update. cpos: %u, "
  2709. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  2710. left_cpos);
  2711. /*
  2712. * No need to worry if the append is already in the
  2713. * leftmost leaf.
  2714. */
  2715. if (left_cpos) {
  2716. left_path = ocfs2_new_path(path_root_bh(right_path),
  2717. path_root_el(right_path));
  2718. if (!left_path) {
  2719. ret = -ENOMEM;
  2720. mlog_errno(ret);
  2721. goto out;
  2722. }
  2723. ret = ocfs2_find_path(inode, left_path, left_cpos);
  2724. if (ret) {
  2725. mlog_errno(ret);
  2726. goto out;
  2727. }
  2728. /*
  2729. * ocfs2_insert_path() will pass the left_path to the
  2730. * journal for us.
  2731. */
  2732. }
  2733. }
  2734. ret = ocfs2_journal_access_path(inode, handle, right_path);
  2735. if (ret) {
  2736. mlog_errno(ret);
  2737. goto out;
  2738. }
  2739. ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
  2740. *ret_left_path = left_path;
  2741. ret = 0;
  2742. out:
  2743. if (ret != 0)
  2744. ocfs2_free_path(left_path);
  2745. return ret;
  2746. }
  2747. static void ocfs2_split_record(struct inode *inode,
  2748. struct ocfs2_path *left_path,
  2749. struct ocfs2_path *right_path,
  2750. struct ocfs2_extent_rec *split_rec,
  2751. enum ocfs2_split_type split)
  2752. {
  2753. int index;
  2754. u32 cpos = le32_to_cpu(split_rec->e_cpos);
  2755. struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
  2756. struct ocfs2_extent_rec *rec, *tmprec;
  2757. right_el = path_leaf_el(right_path);;
  2758. if (left_path)
  2759. left_el = path_leaf_el(left_path);
  2760. el = right_el;
  2761. insert_el = right_el;
  2762. index = ocfs2_search_extent_list(el, cpos);
  2763. if (index != -1) {
  2764. if (index == 0 && left_path) {
  2765. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2766. /*
  2767. * This typically means that the record
  2768. * started in the left path but moved to the
  2769. * right as a result of rotation. We either
  2770. * move the existing record to the left, or we
  2771. * do the later insert there.
  2772. *
  2773. * In this case, the left path should always
  2774. * exist as the rotate code will have passed
  2775. * it back for a post-insert update.
  2776. */
  2777. if (split == SPLIT_LEFT) {
  2778. /*
  2779. * It's a left split. Since we know
  2780. * that the rotate code gave us an
  2781. * empty extent in the left path, we
  2782. * can just do the insert there.
  2783. */
  2784. insert_el = left_el;
  2785. } else {
  2786. /*
  2787. * Right split - we have to move the
  2788. * existing record over to the left
  2789. * leaf. The insert will be into the
  2790. * newly created empty extent in the
  2791. * right leaf.
  2792. */
  2793. tmprec = &right_el->l_recs[index];
  2794. ocfs2_rotate_leaf(left_el, tmprec);
  2795. el = left_el;
  2796. memset(tmprec, 0, sizeof(*tmprec));
  2797. index = ocfs2_search_extent_list(left_el, cpos);
  2798. BUG_ON(index == -1);
  2799. }
  2800. }
  2801. } else {
  2802. BUG_ON(!left_path);
  2803. BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
  2804. /*
  2805. * Left path is easy - we can just allow the insert to
  2806. * happen.
  2807. */
  2808. el = left_el;
  2809. insert_el = left_el;
  2810. index = ocfs2_search_extent_list(el, cpos);
  2811. BUG_ON(index == -1);
  2812. }
  2813. rec = &el->l_recs[index];
  2814. ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
  2815. ocfs2_rotate_leaf(insert_el, split_rec);
  2816. }
  2817. /*
  2818. * This function only does inserts on an allocation b-tree. For dinode
  2819. * lists, ocfs2_insert_at_leaf() is called directly.
  2820. *
  2821. * right_path is the path we want to do the actual insert
  2822. * in. left_path should only be passed in if we need to update that
  2823. * portion of the tree after an edge insert.
  2824. */
  2825. static int ocfs2_insert_path(struct inode *inode,
  2826. handle_t *handle,
  2827. struct ocfs2_path *left_path,
  2828. struct ocfs2_path *right_path,
  2829. struct ocfs2_extent_rec *insert_rec,
  2830. struct ocfs2_insert_type *insert)
  2831. {
  2832. int ret, subtree_index;
  2833. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  2834. /*
  2835. * Pass both paths to the journal. The majority of inserts
  2836. * will be touching all components anyway.
  2837. */
  2838. ret = ocfs2_journal_access_path(inode, handle, right_path);
  2839. if (ret < 0) {
  2840. mlog_errno(ret);
  2841. goto out;
  2842. }
  2843. if (left_path) {
  2844. int credits = handle->h_buffer_credits;
  2845. /*
  2846. * There's a chance that left_path got passed back to
  2847. * us without being accounted for in the
  2848. * journal. Extend our transaction here to be sure we
  2849. * can change those blocks.
  2850. */
  2851. credits += left_path->p_tree_depth;
  2852. ret = ocfs2_extend_trans(handle, credits);
  2853. if (ret < 0) {
  2854. mlog_errno(ret);
  2855. goto out;
  2856. }
  2857. ret = ocfs2_journal_access_path(inode, handle, left_path);
  2858. if (ret < 0) {
  2859. mlog_errno(ret);
  2860. goto out;
  2861. }
  2862. }
  2863. if (insert->ins_split != SPLIT_NONE) {
  2864. /*
  2865. * We could call ocfs2_insert_at_leaf() for some types
  2866. * of splits, but it's easier to just let one seperate
  2867. * function sort it all out.
  2868. */
  2869. ocfs2_split_record(inode, left_path, right_path,
  2870. insert_rec, insert->ins_split);
  2871. } else
  2872. ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
  2873. insert, inode);
  2874. ret = ocfs2_journal_dirty(handle, leaf_bh);
  2875. if (ret)
  2876. mlog_errno(ret);
  2877. if (left_path) {
  2878. /*
  2879. * The rotate code has indicated that we need to fix
  2880. * up portions of the tree after the insert.
  2881. *
  2882. * XXX: Should we extend the transaction here?
  2883. */
  2884. subtree_index = ocfs2_find_subtree_root(inode, left_path,
  2885. right_path);
  2886. ocfs2_complete_edge_insert(inode, handle, left_path,
  2887. right_path, subtree_index);
  2888. }
  2889. ret = 0;
  2890. out:
  2891. return ret;
  2892. }
  2893. static int ocfs2_do_insert_extent(struct inode *inode,
  2894. handle_t *handle,
  2895. struct buffer_head *di_bh,
  2896. struct ocfs2_extent_rec *insert_rec,
  2897. struct ocfs2_insert_type *type)
  2898. {
  2899. int ret, rotate = 0;
  2900. u32 cpos;
  2901. struct ocfs2_path *right_path = NULL;
  2902. struct ocfs2_path *left_path = NULL;
  2903. struct ocfs2_dinode *di;
  2904. struct ocfs2_extent_list *el;
  2905. di = (struct ocfs2_dinode *) di_bh->b_data;
  2906. el = &di->id2.i_list;
  2907. ret = ocfs2_journal_access(handle, inode, di_bh,
  2908. OCFS2_JOURNAL_ACCESS_WRITE);
  2909. if (ret) {
  2910. mlog_errno(ret);
  2911. goto out;
  2912. }
  2913. if (le16_to_cpu(el->l_tree_depth) == 0) {
  2914. ocfs2_insert_at_leaf(insert_rec, el, type, inode);
  2915. goto out_update_clusters;
  2916. }
  2917. right_path = ocfs2_new_inode_path(di_bh);
  2918. if (!right_path) {
  2919. ret = -ENOMEM;
  2920. mlog_errno(ret);
  2921. goto out;
  2922. }
  2923. /*
  2924. * Determine the path to start with. Rotations need the
  2925. * rightmost path, everything else can go directly to the
  2926. * target leaf.
  2927. */
  2928. cpos = le32_to_cpu(insert_rec->e_cpos);
  2929. if (type->ins_appending == APPEND_NONE &&
  2930. type->ins_contig == CONTIG_NONE) {
  2931. rotate = 1;
  2932. cpos = UINT_MAX;
  2933. }
  2934. ret = ocfs2_find_path(inode, right_path, cpos);
  2935. if (ret) {
  2936. mlog_errno(ret);
  2937. goto out;
  2938. }
  2939. /*
  2940. * Rotations and appends need special treatment - they modify
  2941. * parts of the tree's above them.
  2942. *
  2943. * Both might pass back a path immediate to the left of the
  2944. * one being inserted to. This will be cause
  2945. * ocfs2_insert_path() to modify the rightmost records of
  2946. * left_path to account for an edge insert.
  2947. *
  2948. * XXX: When modifying this code, keep in mind that an insert
  2949. * can wind up skipping both of these two special cases...
  2950. */
  2951. if (rotate) {
  2952. ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
  2953. le32_to_cpu(insert_rec->e_cpos),
  2954. right_path, &left_path);
  2955. if (ret) {
  2956. mlog_errno(ret);
  2957. goto out;
  2958. }
  2959. } else if (type->ins_appending == APPEND_TAIL
  2960. && type->ins_contig != CONTIG_LEFT) {
  2961. ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
  2962. right_path, &left_path);
  2963. if (ret) {
  2964. mlog_errno(ret);
  2965. goto out;
  2966. }
  2967. }
  2968. ret = ocfs2_insert_path(inode, handle, left_path, right_path,
  2969. insert_rec, type);
  2970. if (ret) {
  2971. mlog_errno(ret);
  2972. goto out;
  2973. }
  2974. out_update_clusters:
  2975. if (type->ins_split == SPLIT_NONE)
  2976. ocfs2_update_dinode_clusters(inode, di,
  2977. le16_to_cpu(insert_rec->e_leaf_clusters));
  2978. ret = ocfs2_journal_dirty(handle, di_bh);
  2979. if (ret)
  2980. mlog_errno(ret);
  2981. out:
  2982. ocfs2_free_path(left_path);
  2983. ocfs2_free_path(right_path);
  2984. return ret;
  2985. }
  2986. static enum ocfs2_contig_type
  2987. ocfs2_figure_merge_contig_type(struct inode *inode,
  2988. struct ocfs2_extent_list *el, int index,
  2989. struct ocfs2_extent_rec *split_rec)
  2990. {
  2991. struct ocfs2_extent_rec *rec;
  2992. enum ocfs2_contig_type ret = CONTIG_NONE;
  2993. /*
  2994. * We're careful to check for an empty extent record here -
  2995. * the merge code will know what to do if it sees one.
  2996. */
  2997. if (index > 0) {
  2998. rec = &el->l_recs[index - 1];
  2999. if (index == 1 && ocfs2_is_empty_extent(rec)) {
  3000. if (split_rec->e_cpos == el->l_recs[index].e_cpos)
  3001. ret = CONTIG_RIGHT;
  3002. } else {
  3003. ret = ocfs2_extent_contig(inode, rec, split_rec);
  3004. }
  3005. }
  3006. if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) {
  3007. enum ocfs2_contig_type contig_type;
  3008. rec = &el->l_recs[index + 1];
  3009. contig_type = ocfs2_extent_contig(inode, rec, split_rec);
  3010. if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
  3011. ret = CONTIG_LEFTRIGHT;
  3012. else if (ret == CONTIG_NONE)
  3013. ret = contig_type;
  3014. }
  3015. return ret;
  3016. }
  3017. static void ocfs2_figure_contig_type(struct inode *inode,
  3018. struct ocfs2_insert_type *insert,
  3019. struct ocfs2_extent_list *el,
  3020. struct ocfs2_extent_rec *insert_rec)
  3021. {
  3022. int i;
  3023. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  3024. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3025. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  3026. contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
  3027. insert_rec);
  3028. if (contig_type != CONTIG_NONE) {
  3029. insert->ins_contig_index = i;
  3030. break;
  3031. }
  3032. }
  3033. insert->ins_contig = contig_type;
  3034. }
  3035. /*
  3036. * This should only be called against the righmost leaf extent list.
  3037. *
  3038. * ocfs2_figure_appending_type() will figure out whether we'll have to
  3039. * insert at the tail of the rightmost leaf.
  3040. *
  3041. * This should also work against the dinode list for tree's with 0
  3042. * depth. If we consider the dinode list to be the rightmost leaf node
  3043. * then the logic here makes sense.
  3044. */
  3045. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  3046. struct ocfs2_extent_list *el,
  3047. struct ocfs2_extent_rec *insert_rec)
  3048. {
  3049. int i;
  3050. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  3051. struct ocfs2_extent_rec *rec;
  3052. insert->ins_appending = APPEND_NONE;
  3053. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3054. if (!el->l_next_free_rec)
  3055. goto set_tail_append;
  3056. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  3057. /* Were all records empty? */
  3058. if (le16_to_cpu(el->l_next_free_rec) == 1)
  3059. goto set_tail_append;
  3060. }
  3061. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3062. rec = &el->l_recs[i];
  3063. if (cpos >=
  3064. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  3065. goto set_tail_append;
  3066. return;
  3067. set_tail_append:
  3068. insert->ins_appending = APPEND_TAIL;
  3069. }
  3070. /*
  3071. * Helper function called at the begining of an insert.
  3072. *
  3073. * This computes a few things that are commonly used in the process of
  3074. * inserting into the btree:
  3075. * - Whether the new extent is contiguous with an existing one.
  3076. * - The current tree depth.
  3077. * - Whether the insert is an appending one.
  3078. * - The total # of free records in the tree.
  3079. *
  3080. * All of the information is stored on the ocfs2_insert_type
  3081. * structure.
  3082. */
  3083. static int ocfs2_figure_insert_type(struct inode *inode,
  3084. struct buffer_head *di_bh,
  3085. struct buffer_head **last_eb_bh,
  3086. struct ocfs2_extent_rec *insert_rec,
  3087. int *free_records,
  3088. struct ocfs2_insert_type *insert)
  3089. {
  3090. int ret;
  3091. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3092. struct ocfs2_extent_block *eb;
  3093. struct ocfs2_extent_list *el;
  3094. struct ocfs2_path *path = NULL;
  3095. struct buffer_head *bh = NULL;
  3096. insert->ins_split = SPLIT_NONE;
  3097. el = &di->id2.i_list;
  3098. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  3099. if (el->l_tree_depth) {
  3100. /*
  3101. * If we have tree depth, we read in the
  3102. * rightmost extent block ahead of time as
  3103. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  3104. * may want it later.
  3105. */
  3106. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3107. le64_to_cpu(di->i_last_eb_blk), &bh,
  3108. OCFS2_BH_CACHED, inode);
  3109. if (ret) {
  3110. mlog_exit(ret);
  3111. goto out;
  3112. }
  3113. eb = (struct ocfs2_extent_block *) bh->b_data;
  3114. el = &eb->h_list;
  3115. }
  3116. /*
  3117. * Unless we have a contiguous insert, we'll need to know if
  3118. * there is room left in our allocation tree for another
  3119. * extent record.
  3120. *
  3121. * XXX: This test is simplistic, we can search for empty
  3122. * extent records too.
  3123. */
  3124. *free_records = le16_to_cpu(el->l_count) -
  3125. le16_to_cpu(el->l_next_free_rec);
  3126. if (!insert->ins_tree_depth) {
  3127. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  3128. ocfs2_figure_appending_type(insert, el, insert_rec);
  3129. return 0;
  3130. }
  3131. path = ocfs2_new_inode_path(di_bh);
  3132. if (!path) {
  3133. ret = -ENOMEM;
  3134. mlog_errno(ret);
  3135. goto out;
  3136. }
  3137. /*
  3138. * In the case that we're inserting past what the tree
  3139. * currently accounts for, ocfs2_find_path() will return for
  3140. * us the rightmost tree path. This is accounted for below in
  3141. * the appending code.
  3142. */
  3143. ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
  3144. if (ret) {
  3145. mlog_errno(ret);
  3146. goto out;
  3147. }
  3148. el = path_leaf_el(path);
  3149. /*
  3150. * Now that we have the path, there's two things we want to determine:
  3151. * 1) Contiguousness (also set contig_index if this is so)
  3152. *
  3153. * 2) Are we doing an append? We can trivially break this up
  3154. * into two types of appends: simple record append, or a
  3155. * rotate inside the tail leaf.
  3156. */
  3157. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  3158. /*
  3159. * The insert code isn't quite ready to deal with all cases of
  3160. * left contiguousness. Specifically, if it's an insert into
  3161. * the 1st record in a leaf, it will require the adjustment of
  3162. * cluster count on the last record of the path directly to it's
  3163. * left. For now, just catch that case and fool the layers
  3164. * above us. This works just fine for tree_depth == 0, which
  3165. * is why we allow that above.
  3166. */
  3167. if (insert->ins_contig == CONTIG_LEFT &&
  3168. insert->ins_contig_index == 0)
  3169. insert->ins_contig = CONTIG_NONE;
  3170. /*
  3171. * Ok, so we can simply compare against last_eb to figure out
  3172. * whether the path doesn't exist. This will only happen in
  3173. * the case that we're doing a tail append, so maybe we can
  3174. * take advantage of that information somehow.
  3175. */
  3176. if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
  3177. /*
  3178. * Ok, ocfs2_find_path() returned us the rightmost
  3179. * tree path. This might be an appending insert. There are
  3180. * two cases:
  3181. * 1) We're doing a true append at the tail:
  3182. * -This might even be off the end of the leaf
  3183. * 2) We're "appending" by rotating in the tail
  3184. */
  3185. ocfs2_figure_appending_type(insert, el, insert_rec);
  3186. }
  3187. out:
  3188. ocfs2_free_path(path);
  3189. if (ret == 0)
  3190. *last_eb_bh = bh;
  3191. else
  3192. brelse(bh);
  3193. return ret;
  3194. }
  3195. /*
  3196. * Insert an extent into an inode btree.
  3197. *
  3198. * The caller needs to update fe->i_clusters
  3199. */
  3200. int ocfs2_insert_extent(struct ocfs2_super *osb,
  3201. handle_t *handle,
  3202. struct inode *inode,
  3203. struct buffer_head *fe_bh,
  3204. u32 cpos,
  3205. u64 start_blk,
  3206. u32 new_clusters,
  3207. u8 flags,
  3208. struct ocfs2_alloc_context *meta_ac)
  3209. {
  3210. int status;
  3211. int uninitialized_var(free_records);
  3212. struct buffer_head *last_eb_bh = NULL;
  3213. struct ocfs2_insert_type insert = {0, };
  3214. struct ocfs2_extent_rec rec;
  3215. BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
  3216. mlog(0, "add %u clusters at position %u to inode %llu\n",
  3217. new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3218. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  3219. (OCFS2_I(inode)->ip_clusters != cpos),
  3220. "Device %s, asking for sparse allocation: inode %llu, "
  3221. "cpos %u, clusters %u\n",
  3222. osb->dev_str,
  3223. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
  3224. OCFS2_I(inode)->ip_clusters);
  3225. memset(&rec, 0, sizeof(rec));
  3226. rec.e_cpos = cpu_to_le32(cpos);
  3227. rec.e_blkno = cpu_to_le64(start_blk);
  3228. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  3229. rec.e_flags = flags;
  3230. status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
  3231. &free_records, &insert);
  3232. if (status < 0) {
  3233. mlog_errno(status);
  3234. goto bail;
  3235. }
  3236. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  3237. "Insert.contig_index: %d, Insert.free_records: %d, "
  3238. "Insert.tree_depth: %d\n",
  3239. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  3240. free_records, insert.ins_tree_depth);
  3241. if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
  3242. status = ocfs2_grow_tree(inode, handle, fe_bh,
  3243. &insert.ins_tree_depth, &last_eb_bh,
  3244. meta_ac);
  3245. if (status) {
  3246. mlog_errno(status);
  3247. goto bail;
  3248. }
  3249. }
  3250. /* Finally, we can add clusters. This might rotate the tree for us. */
  3251. status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
  3252. if (status < 0)
  3253. mlog_errno(status);
  3254. else
  3255. ocfs2_extent_map_insert_rec(inode, &rec);
  3256. bail:
  3257. if (last_eb_bh)
  3258. brelse(last_eb_bh);
  3259. mlog_exit(status);
  3260. return status;
  3261. }
  3262. static void ocfs2_make_right_split_rec(struct super_block *sb,
  3263. struct ocfs2_extent_rec *split_rec,
  3264. u32 cpos,
  3265. struct ocfs2_extent_rec *rec)
  3266. {
  3267. u32 rec_cpos = le32_to_cpu(rec->e_cpos);
  3268. u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
  3269. memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
  3270. split_rec->e_cpos = cpu_to_le32(cpos);
  3271. split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
  3272. split_rec->e_blkno = rec->e_blkno;
  3273. le64_add_cpu(&split_rec->e_blkno,
  3274. ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
  3275. split_rec->e_flags = rec->e_flags;
  3276. }
  3277. static int ocfs2_split_and_insert(struct inode *inode,
  3278. handle_t *handle,
  3279. struct ocfs2_path *path,
  3280. struct buffer_head *di_bh,
  3281. struct buffer_head **last_eb_bh,
  3282. int split_index,
  3283. struct ocfs2_extent_rec *orig_split_rec,
  3284. struct ocfs2_alloc_context *meta_ac)
  3285. {
  3286. int ret = 0, depth;
  3287. unsigned int insert_range, rec_range, do_leftright = 0;
  3288. struct ocfs2_extent_rec tmprec;
  3289. struct ocfs2_extent_list *rightmost_el;
  3290. struct ocfs2_extent_rec rec;
  3291. struct ocfs2_extent_rec split_rec = *orig_split_rec;
  3292. struct ocfs2_insert_type insert;
  3293. struct ocfs2_extent_block *eb;
  3294. struct ocfs2_dinode *di;
  3295. leftright:
  3296. /*
  3297. * Store a copy of the record on the stack - it might move
  3298. * around as the tree is manipulated below.
  3299. */
  3300. rec = path_leaf_el(path)->l_recs[split_index];
  3301. di = (struct ocfs2_dinode *)di_bh->b_data;
  3302. rightmost_el = &di->id2.i_list;
  3303. depth = le16_to_cpu(rightmost_el->l_tree_depth);
  3304. if (depth) {
  3305. BUG_ON(!(*last_eb_bh));
  3306. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  3307. rightmost_el = &eb->h_list;
  3308. }
  3309. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  3310. le16_to_cpu(rightmost_el->l_count)) {
  3311. ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
  3312. meta_ac);
  3313. if (ret) {
  3314. mlog_errno(ret);
  3315. goto out;
  3316. }
  3317. }
  3318. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  3319. insert.ins_appending = APPEND_NONE;
  3320. insert.ins_contig = CONTIG_NONE;
  3321. insert.ins_tree_depth = depth;
  3322. insert_range = le32_to_cpu(split_rec.e_cpos) +
  3323. le16_to_cpu(split_rec.e_leaf_clusters);
  3324. rec_range = le32_to_cpu(rec.e_cpos) +
  3325. le16_to_cpu(rec.e_leaf_clusters);
  3326. if (split_rec.e_cpos == rec.e_cpos) {
  3327. insert.ins_split = SPLIT_LEFT;
  3328. } else if (insert_range == rec_range) {
  3329. insert.ins_split = SPLIT_RIGHT;
  3330. } else {
  3331. /*
  3332. * Left/right split. We fake this as a right split
  3333. * first and then make a second pass as a left split.
  3334. */
  3335. insert.ins_split = SPLIT_RIGHT;
  3336. ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
  3337. &rec);
  3338. split_rec = tmprec;
  3339. BUG_ON(do_leftright);
  3340. do_leftright = 1;
  3341. }
  3342. ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
  3343. &insert);
  3344. if (ret) {
  3345. mlog_errno(ret);
  3346. goto out;
  3347. }
  3348. if (do_leftright == 1) {
  3349. u32 cpos;
  3350. struct ocfs2_extent_list *el;
  3351. do_leftright++;
  3352. split_rec = *orig_split_rec;
  3353. ocfs2_reinit_path(path, 1);
  3354. cpos = le32_to_cpu(split_rec.e_cpos);
  3355. ret = ocfs2_find_path(inode, path, cpos);
  3356. if (ret) {
  3357. mlog_errno(ret);
  3358. goto out;
  3359. }
  3360. el = path_leaf_el(path);
  3361. split_index = ocfs2_search_extent_list(el, cpos);
  3362. goto leftright;
  3363. }
  3364. out:
  3365. return ret;
  3366. }
  3367. /*
  3368. * Mark part or all of the extent record at split_index in the leaf
  3369. * pointed to by path as written. This removes the unwritten
  3370. * extent flag.
  3371. *
  3372. * Care is taken to handle contiguousness so as to not grow the tree.
  3373. *
  3374. * meta_ac is not strictly necessary - we only truly need it if growth
  3375. * of the tree is required. All other cases will degrade into a less
  3376. * optimal tree layout.
  3377. *
  3378. * last_eb_bh should be the rightmost leaf block for any inode with a
  3379. * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
  3380. *
  3381. * This code is optimized for readability - several passes might be
  3382. * made over certain portions of the tree. All of those blocks will
  3383. * have been brought into cache (and pinned via the journal), so the
  3384. * extra overhead is not expressed in terms of disk reads.
  3385. */
  3386. static int __ocfs2_mark_extent_written(struct inode *inode,
  3387. struct buffer_head *di_bh,
  3388. handle_t *handle,
  3389. struct ocfs2_path *path,
  3390. int split_index,
  3391. struct ocfs2_extent_rec *split_rec,
  3392. struct ocfs2_alloc_context *meta_ac,
  3393. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3394. {
  3395. int ret = 0;
  3396. struct ocfs2_extent_list *el = path_leaf_el(path);
  3397. struct buffer_head *eb_bh, *last_eb_bh = NULL;
  3398. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  3399. struct ocfs2_merge_ctxt ctxt;
  3400. struct ocfs2_extent_list *rightmost_el;
  3401. if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) {
  3402. ret = -EIO;
  3403. mlog_errno(ret);
  3404. goto out;
  3405. }
  3406. if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
  3407. ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
  3408. (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
  3409. ret = -EIO;
  3410. mlog_errno(ret);
  3411. goto out;
  3412. }
  3413. eb_bh = path_leaf_bh(path);
  3414. ret = ocfs2_journal_access(handle, inode, eb_bh,
  3415. OCFS2_JOURNAL_ACCESS_WRITE);
  3416. if (ret) {
  3417. mlog_errno(ret);
  3418. goto out;
  3419. }
  3420. ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el,
  3421. split_index,
  3422. split_rec);
  3423. /*
  3424. * The core merge / split code wants to know how much room is
  3425. * left in this inodes allocation tree, so we pass the
  3426. * rightmost extent list.
  3427. */
  3428. if (path->p_tree_depth) {
  3429. struct ocfs2_extent_block *eb;
  3430. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3431. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3432. le64_to_cpu(di->i_last_eb_blk),
  3433. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3434. if (ret) {
  3435. mlog_exit(ret);
  3436. goto out;
  3437. }
  3438. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3439. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  3440. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  3441. ret = -EROFS;
  3442. goto out;
  3443. }
  3444. rightmost_el = &eb->h_list;
  3445. } else
  3446. rightmost_el = path_root_el(path);
  3447. if (rec->e_cpos == split_rec->e_cpos &&
  3448. rec->e_leaf_clusters == split_rec->e_leaf_clusters)
  3449. ctxt.c_split_covers_rec = 1;
  3450. else
  3451. ctxt.c_split_covers_rec = 0;
  3452. ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
  3453. mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
  3454. split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
  3455. ctxt.c_split_covers_rec);
  3456. if (ctxt.c_contig_type == CONTIG_NONE) {
  3457. if (ctxt.c_split_covers_rec)
  3458. el->l_recs[split_index] = *split_rec;
  3459. else
  3460. ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
  3461. &last_eb_bh, split_index,
  3462. split_rec, meta_ac);
  3463. if (ret)
  3464. mlog_errno(ret);
  3465. } else {
  3466. ret = ocfs2_try_to_merge_extent(inode, handle, path,
  3467. split_index, split_rec,
  3468. dealloc, &ctxt);
  3469. if (ret)
  3470. mlog_errno(ret);
  3471. }
  3472. ocfs2_journal_dirty(handle, eb_bh);
  3473. out:
  3474. brelse(last_eb_bh);
  3475. return ret;
  3476. }
  3477. /*
  3478. * Mark the already-existing extent at cpos as written for len clusters.
  3479. *
  3480. * If the existing extent is larger than the request, initiate a
  3481. * split. An attempt will be made at merging with adjacent extents.
  3482. *
  3483. * The caller is responsible for passing down meta_ac if we'll need it.
  3484. */
  3485. int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
  3486. handle_t *handle, u32 cpos, u32 len, u32 phys,
  3487. struct ocfs2_alloc_context *meta_ac,
  3488. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3489. {
  3490. int ret, index;
  3491. u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
  3492. struct ocfs2_extent_rec split_rec;
  3493. struct ocfs2_path *left_path = NULL;
  3494. struct ocfs2_extent_list *el;
  3495. mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
  3496. inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
  3497. if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
  3498. ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
  3499. "that are being written to, but the feature bit "
  3500. "is not set in the super block.",
  3501. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3502. ret = -EROFS;
  3503. goto out;
  3504. }
  3505. /*
  3506. * XXX: This should be fixed up so that we just re-insert the
  3507. * next extent records.
  3508. */
  3509. ocfs2_extent_map_trunc(inode, 0);
  3510. left_path = ocfs2_new_inode_path(di_bh);
  3511. if (!left_path) {
  3512. ret = -ENOMEM;
  3513. mlog_errno(ret);
  3514. goto out;
  3515. }
  3516. ret = ocfs2_find_path(inode, left_path, cpos);
  3517. if (ret) {
  3518. mlog_errno(ret);
  3519. goto out;
  3520. }
  3521. el = path_leaf_el(left_path);
  3522. index = ocfs2_search_extent_list(el, cpos);
  3523. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3524. ocfs2_error(inode->i_sb,
  3525. "Inode %llu has an extent at cpos %u which can no "
  3526. "longer be found.\n",
  3527. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
  3528. ret = -EROFS;
  3529. goto out;
  3530. }
  3531. memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
  3532. split_rec.e_cpos = cpu_to_le32(cpos);
  3533. split_rec.e_leaf_clusters = cpu_to_le16(len);
  3534. split_rec.e_blkno = cpu_to_le64(start_blkno);
  3535. split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
  3536. split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
  3537. ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
  3538. index, &split_rec, meta_ac, dealloc);
  3539. if (ret)
  3540. mlog_errno(ret);
  3541. out:
  3542. ocfs2_free_path(left_path);
  3543. return ret;
  3544. }
  3545. static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
  3546. handle_t *handle, struct ocfs2_path *path,
  3547. int index, u32 new_range,
  3548. struct ocfs2_alloc_context *meta_ac)
  3549. {
  3550. int ret, depth, credits = handle->h_buffer_credits;
  3551. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3552. struct buffer_head *last_eb_bh = NULL;
  3553. struct ocfs2_extent_block *eb;
  3554. struct ocfs2_extent_list *rightmost_el, *el;
  3555. struct ocfs2_extent_rec split_rec;
  3556. struct ocfs2_extent_rec *rec;
  3557. struct ocfs2_insert_type insert;
  3558. /*
  3559. * Setup the record to split before we grow the tree.
  3560. */
  3561. el = path_leaf_el(path);
  3562. rec = &el->l_recs[index];
  3563. ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
  3564. depth = path->p_tree_depth;
  3565. if (depth > 0) {
  3566. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3567. le64_to_cpu(di->i_last_eb_blk),
  3568. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3569. if (ret < 0) {
  3570. mlog_errno(ret);
  3571. goto out;
  3572. }
  3573. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3574. rightmost_el = &eb->h_list;
  3575. } else
  3576. rightmost_el = path_leaf_el(path);
  3577. credits += path->p_tree_depth + ocfs2_extend_meta_needed(di);
  3578. ret = ocfs2_extend_trans(handle, credits);
  3579. if (ret) {
  3580. mlog_errno(ret);
  3581. goto out;
  3582. }
  3583. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  3584. le16_to_cpu(rightmost_el->l_count)) {
  3585. ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
  3586. meta_ac);
  3587. if (ret) {
  3588. mlog_errno(ret);
  3589. goto out;
  3590. }
  3591. }
  3592. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  3593. insert.ins_appending = APPEND_NONE;
  3594. insert.ins_contig = CONTIG_NONE;
  3595. insert.ins_split = SPLIT_RIGHT;
  3596. insert.ins_tree_depth = depth;
  3597. ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
  3598. if (ret)
  3599. mlog_errno(ret);
  3600. out:
  3601. brelse(last_eb_bh);
  3602. return ret;
  3603. }
  3604. static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
  3605. struct ocfs2_path *path, int index,
  3606. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3607. u32 cpos, u32 len)
  3608. {
  3609. int ret;
  3610. u32 left_cpos, rec_range, trunc_range;
  3611. int wants_rotate = 0, is_rightmost_tree_rec = 0;
  3612. struct super_block *sb = inode->i_sb;
  3613. struct ocfs2_path *left_path = NULL;
  3614. struct ocfs2_extent_list *el = path_leaf_el(path);
  3615. struct ocfs2_extent_rec *rec;
  3616. struct ocfs2_extent_block *eb;
  3617. if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
  3618. ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
  3619. if (ret) {
  3620. mlog_errno(ret);
  3621. goto out;
  3622. }
  3623. index--;
  3624. }
  3625. if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
  3626. path->p_tree_depth) {
  3627. /*
  3628. * Check whether this is the rightmost tree record. If
  3629. * we remove all of this record or part of its right
  3630. * edge then an update of the record lengths above it
  3631. * will be required.
  3632. */
  3633. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  3634. if (eb->h_next_leaf_blk == 0)
  3635. is_rightmost_tree_rec = 1;
  3636. }
  3637. rec = &el->l_recs[index];
  3638. if (index == 0 && path->p_tree_depth &&
  3639. le32_to_cpu(rec->e_cpos) == cpos) {
  3640. /*
  3641. * Changing the leftmost offset (via partial or whole
  3642. * record truncate) of an interior (or rightmost) path
  3643. * means we have to update the subtree that is formed
  3644. * by this leaf and the one to it's left.
  3645. *
  3646. * There are two cases we can skip:
  3647. * 1) Path is the leftmost one in our inode tree.
  3648. * 2) The leaf is rightmost and will be empty after
  3649. * we remove the extent record - the rotate code
  3650. * knows how to update the newly formed edge.
  3651. */
  3652. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
  3653. &left_cpos);
  3654. if (ret) {
  3655. mlog_errno(ret);
  3656. goto out;
  3657. }
  3658. if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
  3659. left_path = ocfs2_new_path(path_root_bh(path),
  3660. path_root_el(path));
  3661. if (!left_path) {
  3662. ret = -ENOMEM;
  3663. mlog_errno(ret);
  3664. goto out;
  3665. }
  3666. ret = ocfs2_find_path(inode, left_path, left_cpos);
  3667. if (ret) {
  3668. mlog_errno(ret);
  3669. goto out;
  3670. }
  3671. }
  3672. }
  3673. ret = ocfs2_extend_rotate_transaction(handle, 0,
  3674. handle->h_buffer_credits,
  3675. path);
  3676. if (ret) {
  3677. mlog_errno(ret);
  3678. goto out;
  3679. }
  3680. ret = ocfs2_journal_access_path(inode, handle, path);
  3681. if (ret) {
  3682. mlog_errno(ret);
  3683. goto out;
  3684. }
  3685. ret = ocfs2_journal_access_path(inode, handle, left_path);
  3686. if (ret) {
  3687. mlog_errno(ret);
  3688. goto out;
  3689. }
  3690. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  3691. trunc_range = cpos + len;
  3692. if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
  3693. int next_free;
  3694. memset(rec, 0, sizeof(*rec));
  3695. ocfs2_cleanup_merge(el, index);
  3696. wants_rotate = 1;
  3697. next_free = le16_to_cpu(el->l_next_free_rec);
  3698. if (is_rightmost_tree_rec && next_free > 1) {
  3699. /*
  3700. * We skip the edge update if this path will
  3701. * be deleted by the rotate code.
  3702. */
  3703. rec = &el->l_recs[next_free - 1];
  3704. ocfs2_adjust_rightmost_records(inode, handle, path,
  3705. rec);
  3706. }
  3707. } else if (le32_to_cpu(rec->e_cpos) == cpos) {
  3708. /* Remove leftmost portion of the record. */
  3709. le32_add_cpu(&rec->e_cpos, len);
  3710. le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
  3711. le16_add_cpu(&rec->e_leaf_clusters, -len);
  3712. } else if (rec_range == trunc_range) {
  3713. /* Remove rightmost portion of the record */
  3714. le16_add_cpu(&rec->e_leaf_clusters, -len);
  3715. if (is_rightmost_tree_rec)
  3716. ocfs2_adjust_rightmost_records(inode, handle, path, rec);
  3717. } else {
  3718. /* Caller should have trapped this. */
  3719. mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
  3720. "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3721. le32_to_cpu(rec->e_cpos),
  3722. le16_to_cpu(rec->e_leaf_clusters), cpos, len);
  3723. BUG();
  3724. }
  3725. if (left_path) {
  3726. int subtree_index;
  3727. subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
  3728. ocfs2_complete_edge_insert(inode, handle, left_path, path,
  3729. subtree_index);
  3730. }
  3731. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  3732. ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
  3733. if (ret) {
  3734. mlog_errno(ret);
  3735. goto out;
  3736. }
  3737. out:
  3738. ocfs2_free_path(left_path);
  3739. return ret;
  3740. }
  3741. int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
  3742. u32 cpos, u32 len, handle_t *handle,
  3743. struct ocfs2_alloc_context *meta_ac,
  3744. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3745. {
  3746. int ret, index;
  3747. u32 rec_range, trunc_range;
  3748. struct ocfs2_extent_rec *rec;
  3749. struct ocfs2_extent_list *el;
  3750. struct ocfs2_path *path;
  3751. ocfs2_extent_map_trunc(inode, 0);
  3752. path = ocfs2_new_inode_path(di_bh);
  3753. if (!path) {
  3754. ret = -ENOMEM;
  3755. mlog_errno(ret);
  3756. goto out;
  3757. }
  3758. ret = ocfs2_find_path(inode, path, cpos);
  3759. if (ret) {
  3760. mlog_errno(ret);
  3761. goto out;
  3762. }
  3763. el = path_leaf_el(path);
  3764. index = ocfs2_search_extent_list(el, cpos);
  3765. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3766. ocfs2_error(inode->i_sb,
  3767. "Inode %llu has an extent at cpos %u which can no "
  3768. "longer be found.\n",
  3769. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
  3770. ret = -EROFS;
  3771. goto out;
  3772. }
  3773. /*
  3774. * We have 3 cases of extent removal:
  3775. * 1) Range covers the entire extent rec
  3776. * 2) Range begins or ends on one edge of the extent rec
  3777. * 3) Range is in the middle of the extent rec (no shared edges)
  3778. *
  3779. * For case 1 we remove the extent rec and left rotate to
  3780. * fill the hole.
  3781. *
  3782. * For case 2 we just shrink the existing extent rec, with a
  3783. * tree update if the shrinking edge is also the edge of an
  3784. * extent block.
  3785. *
  3786. * For case 3 we do a right split to turn the extent rec into
  3787. * something case 2 can handle.
  3788. */
  3789. rec = &el->l_recs[index];
  3790. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  3791. trunc_range = cpos + len;
  3792. BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
  3793. mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
  3794. "(cpos %u, len %u)\n",
  3795. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
  3796. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
  3797. if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
  3798. ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
  3799. cpos, len);
  3800. if (ret) {
  3801. mlog_errno(ret);
  3802. goto out;
  3803. }
  3804. } else {
  3805. ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
  3806. trunc_range, meta_ac);
  3807. if (ret) {
  3808. mlog_errno(ret);
  3809. goto out;
  3810. }
  3811. /*
  3812. * The split could have manipulated the tree enough to
  3813. * move the record location, so we have to look for it again.
  3814. */
  3815. ocfs2_reinit_path(path, 1);
  3816. ret = ocfs2_find_path(inode, path, cpos);
  3817. if (ret) {
  3818. mlog_errno(ret);
  3819. goto out;
  3820. }
  3821. el = path_leaf_el(path);
  3822. index = ocfs2_search_extent_list(el, cpos);
  3823. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3824. ocfs2_error(inode->i_sb,
  3825. "Inode %llu: split at cpos %u lost record.",
  3826. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3827. cpos);
  3828. ret = -EROFS;
  3829. goto out;
  3830. }
  3831. /*
  3832. * Double check our values here. If anything is fishy,
  3833. * it's easier to catch it at the top level.
  3834. */
  3835. rec = &el->l_recs[index];
  3836. rec_range = le32_to_cpu(rec->e_cpos) +
  3837. ocfs2_rec_clusters(el, rec);
  3838. if (rec_range != trunc_range) {
  3839. ocfs2_error(inode->i_sb,
  3840. "Inode %llu: error after split at cpos %u"
  3841. "trunc len %u, existing record is (%u,%u)",
  3842. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3843. cpos, len, le32_to_cpu(rec->e_cpos),
  3844. ocfs2_rec_clusters(el, rec));
  3845. ret = -EROFS;
  3846. goto out;
  3847. }
  3848. ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
  3849. cpos, len);
  3850. if (ret) {
  3851. mlog_errno(ret);
  3852. goto out;
  3853. }
  3854. }
  3855. out:
  3856. ocfs2_free_path(path);
  3857. return ret;
  3858. }
  3859. int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  3860. {
  3861. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3862. struct ocfs2_dinode *di;
  3863. struct ocfs2_truncate_log *tl;
  3864. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3865. tl = &di->id2.i_dealloc;
  3866. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  3867. "slot %d, invalid truncate log parameters: used = "
  3868. "%u, count = %u\n", osb->slot_num,
  3869. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  3870. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  3871. }
  3872. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  3873. unsigned int new_start)
  3874. {
  3875. unsigned int tail_index;
  3876. unsigned int current_tail;
  3877. /* No records, nothing to coalesce */
  3878. if (!le16_to_cpu(tl->tl_used))
  3879. return 0;
  3880. tail_index = le16_to_cpu(tl->tl_used) - 1;
  3881. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  3882. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  3883. return current_tail == new_start;
  3884. }
  3885. int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  3886. handle_t *handle,
  3887. u64 start_blk,
  3888. unsigned int num_clusters)
  3889. {
  3890. int status, index;
  3891. unsigned int start_cluster, tl_count;
  3892. struct inode *tl_inode = osb->osb_tl_inode;
  3893. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3894. struct ocfs2_dinode *di;
  3895. struct ocfs2_truncate_log *tl;
  3896. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  3897. (unsigned long long)start_blk, num_clusters);
  3898. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  3899. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  3900. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3901. tl = &di->id2.i_dealloc;
  3902. if (!OCFS2_IS_VALID_DINODE(di)) {
  3903. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  3904. status = -EIO;
  3905. goto bail;
  3906. }
  3907. tl_count = le16_to_cpu(tl->tl_count);
  3908. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  3909. tl_count == 0,
  3910. "Truncate record count on #%llu invalid "
  3911. "wanted %u, actual %u\n",
  3912. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  3913. ocfs2_truncate_recs_per_inode(osb->sb),
  3914. le16_to_cpu(tl->tl_count));
  3915. /* Caller should have known to flush before calling us. */
  3916. index = le16_to_cpu(tl->tl_used);
  3917. if (index >= tl_count) {
  3918. status = -ENOSPC;
  3919. mlog_errno(status);
  3920. goto bail;
  3921. }
  3922. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  3923. OCFS2_JOURNAL_ACCESS_WRITE);
  3924. if (status < 0) {
  3925. mlog_errno(status);
  3926. goto bail;
  3927. }
  3928. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  3929. "%llu (index = %d)\n", num_clusters, start_cluster,
  3930. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  3931. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  3932. /*
  3933. * Move index back to the record we are coalescing with.
  3934. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  3935. */
  3936. index--;
  3937. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  3938. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  3939. index, le32_to_cpu(tl->tl_recs[index].t_start),
  3940. num_clusters);
  3941. } else {
  3942. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  3943. tl->tl_used = cpu_to_le16(index + 1);
  3944. }
  3945. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  3946. status = ocfs2_journal_dirty(handle, tl_bh);
  3947. if (status < 0) {
  3948. mlog_errno(status);
  3949. goto bail;
  3950. }
  3951. bail:
  3952. mlog_exit(status);
  3953. return status;
  3954. }
  3955. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  3956. handle_t *handle,
  3957. struct inode *data_alloc_inode,
  3958. struct buffer_head *data_alloc_bh)
  3959. {
  3960. int status = 0;
  3961. int i;
  3962. unsigned int num_clusters;
  3963. u64 start_blk;
  3964. struct ocfs2_truncate_rec rec;
  3965. struct ocfs2_dinode *di;
  3966. struct ocfs2_truncate_log *tl;
  3967. struct inode *tl_inode = osb->osb_tl_inode;
  3968. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3969. mlog_entry_void();
  3970. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3971. tl = &di->id2.i_dealloc;
  3972. i = le16_to_cpu(tl->tl_used) - 1;
  3973. while (i >= 0) {
  3974. /* Caller has given us at least enough credits to
  3975. * update the truncate log dinode */
  3976. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  3977. OCFS2_JOURNAL_ACCESS_WRITE);
  3978. if (status < 0) {
  3979. mlog_errno(status);
  3980. goto bail;
  3981. }
  3982. tl->tl_used = cpu_to_le16(i);
  3983. status = ocfs2_journal_dirty(handle, tl_bh);
  3984. if (status < 0) {
  3985. mlog_errno(status);
  3986. goto bail;
  3987. }
  3988. /* TODO: Perhaps we can calculate the bulk of the
  3989. * credits up front rather than extending like
  3990. * this. */
  3991. status = ocfs2_extend_trans(handle,
  3992. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  3993. if (status < 0) {
  3994. mlog_errno(status);
  3995. goto bail;
  3996. }
  3997. rec = tl->tl_recs[i];
  3998. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  3999. le32_to_cpu(rec.t_start));
  4000. num_clusters = le32_to_cpu(rec.t_clusters);
  4001. /* if start_blk is not set, we ignore the record as
  4002. * invalid. */
  4003. if (start_blk) {
  4004. mlog(0, "free record %d, start = %u, clusters = %u\n",
  4005. i, le32_to_cpu(rec.t_start), num_clusters);
  4006. status = ocfs2_free_clusters(handle, data_alloc_inode,
  4007. data_alloc_bh, start_blk,
  4008. num_clusters);
  4009. if (status < 0) {
  4010. mlog_errno(status);
  4011. goto bail;
  4012. }
  4013. }
  4014. i--;
  4015. }
  4016. bail:
  4017. mlog_exit(status);
  4018. return status;
  4019. }
  4020. /* Expects you to already be holding tl_inode->i_mutex */
  4021. int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  4022. {
  4023. int status;
  4024. unsigned int num_to_flush;
  4025. handle_t *handle;
  4026. struct inode *tl_inode = osb->osb_tl_inode;
  4027. struct inode *data_alloc_inode = NULL;
  4028. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4029. struct buffer_head *data_alloc_bh = NULL;
  4030. struct ocfs2_dinode *di;
  4031. struct ocfs2_truncate_log *tl;
  4032. mlog_entry_void();
  4033. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  4034. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4035. tl = &di->id2.i_dealloc;
  4036. if (!OCFS2_IS_VALID_DINODE(di)) {
  4037. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  4038. status = -EIO;
  4039. goto out;
  4040. }
  4041. num_to_flush = le16_to_cpu(tl->tl_used);
  4042. mlog(0, "Flush %u records from truncate log #%llu\n",
  4043. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  4044. if (!num_to_flush) {
  4045. status = 0;
  4046. goto out;
  4047. }
  4048. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  4049. GLOBAL_BITMAP_SYSTEM_INODE,
  4050. OCFS2_INVALID_SLOT);
  4051. if (!data_alloc_inode) {
  4052. status = -EINVAL;
  4053. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  4054. goto out;
  4055. }
  4056. mutex_lock(&data_alloc_inode->i_mutex);
  4057. status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
  4058. if (status < 0) {
  4059. mlog_errno(status);
  4060. goto out_mutex;
  4061. }
  4062. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  4063. if (IS_ERR(handle)) {
  4064. status = PTR_ERR(handle);
  4065. mlog_errno(status);
  4066. goto out_unlock;
  4067. }
  4068. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  4069. data_alloc_bh);
  4070. if (status < 0)
  4071. mlog_errno(status);
  4072. ocfs2_commit_trans(osb, handle);
  4073. out_unlock:
  4074. brelse(data_alloc_bh);
  4075. ocfs2_meta_unlock(data_alloc_inode, 1);
  4076. out_mutex:
  4077. mutex_unlock(&data_alloc_inode->i_mutex);
  4078. iput(data_alloc_inode);
  4079. out:
  4080. mlog_exit(status);
  4081. return status;
  4082. }
  4083. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  4084. {
  4085. int status;
  4086. struct inode *tl_inode = osb->osb_tl_inode;
  4087. mutex_lock(&tl_inode->i_mutex);
  4088. status = __ocfs2_flush_truncate_log(osb);
  4089. mutex_unlock(&tl_inode->i_mutex);
  4090. return status;
  4091. }
  4092. static void ocfs2_truncate_log_worker(struct work_struct *work)
  4093. {
  4094. int status;
  4095. struct ocfs2_super *osb =
  4096. container_of(work, struct ocfs2_super,
  4097. osb_truncate_log_wq.work);
  4098. mlog_entry_void();
  4099. status = ocfs2_flush_truncate_log(osb);
  4100. if (status < 0)
  4101. mlog_errno(status);
  4102. mlog_exit(status);
  4103. }
  4104. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  4105. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  4106. int cancel)
  4107. {
  4108. if (osb->osb_tl_inode) {
  4109. /* We want to push off log flushes while truncates are
  4110. * still running. */
  4111. if (cancel)
  4112. cancel_delayed_work(&osb->osb_truncate_log_wq);
  4113. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  4114. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  4115. }
  4116. }
  4117. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  4118. int slot_num,
  4119. struct inode **tl_inode,
  4120. struct buffer_head **tl_bh)
  4121. {
  4122. int status;
  4123. struct inode *inode = NULL;
  4124. struct buffer_head *bh = NULL;
  4125. inode = ocfs2_get_system_file_inode(osb,
  4126. TRUNCATE_LOG_SYSTEM_INODE,
  4127. slot_num);
  4128. if (!inode) {
  4129. status = -EINVAL;
  4130. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  4131. goto bail;
  4132. }
  4133. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  4134. OCFS2_BH_CACHED, inode);
  4135. if (status < 0) {
  4136. iput(inode);
  4137. mlog_errno(status);
  4138. goto bail;
  4139. }
  4140. *tl_inode = inode;
  4141. *tl_bh = bh;
  4142. bail:
  4143. mlog_exit(status);
  4144. return status;
  4145. }
  4146. /* called during the 1st stage of node recovery. we stamp a clean
  4147. * truncate log and pass back a copy for processing later. if the
  4148. * truncate log does not require processing, a *tl_copy is set to
  4149. * NULL. */
  4150. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  4151. int slot_num,
  4152. struct ocfs2_dinode **tl_copy)
  4153. {
  4154. int status;
  4155. struct inode *tl_inode = NULL;
  4156. struct buffer_head *tl_bh = NULL;
  4157. struct ocfs2_dinode *di;
  4158. struct ocfs2_truncate_log *tl;
  4159. *tl_copy = NULL;
  4160. mlog(0, "recover truncate log from slot %d\n", slot_num);
  4161. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  4162. if (status < 0) {
  4163. mlog_errno(status);
  4164. goto bail;
  4165. }
  4166. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4167. tl = &di->id2.i_dealloc;
  4168. if (!OCFS2_IS_VALID_DINODE(di)) {
  4169. OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
  4170. status = -EIO;
  4171. goto bail;
  4172. }
  4173. if (le16_to_cpu(tl->tl_used)) {
  4174. mlog(0, "We'll have %u logs to recover\n",
  4175. le16_to_cpu(tl->tl_used));
  4176. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  4177. if (!(*tl_copy)) {
  4178. status = -ENOMEM;
  4179. mlog_errno(status);
  4180. goto bail;
  4181. }
  4182. /* Assuming the write-out below goes well, this copy
  4183. * will be passed back to recovery for processing. */
  4184. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  4185. /* All we need to do to clear the truncate log is set
  4186. * tl_used. */
  4187. tl->tl_used = 0;
  4188. status = ocfs2_write_block(osb, tl_bh, tl_inode);
  4189. if (status < 0) {
  4190. mlog_errno(status);
  4191. goto bail;
  4192. }
  4193. }
  4194. bail:
  4195. if (tl_inode)
  4196. iput(tl_inode);
  4197. if (tl_bh)
  4198. brelse(tl_bh);
  4199. if (status < 0 && (*tl_copy)) {
  4200. kfree(*tl_copy);
  4201. *tl_copy = NULL;
  4202. }
  4203. mlog_exit(status);
  4204. return status;
  4205. }
  4206. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  4207. struct ocfs2_dinode *tl_copy)
  4208. {
  4209. int status = 0;
  4210. int i;
  4211. unsigned int clusters, num_recs, start_cluster;
  4212. u64 start_blk;
  4213. handle_t *handle;
  4214. struct inode *tl_inode = osb->osb_tl_inode;
  4215. struct ocfs2_truncate_log *tl;
  4216. mlog_entry_void();
  4217. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  4218. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  4219. return -EINVAL;
  4220. }
  4221. tl = &tl_copy->id2.i_dealloc;
  4222. num_recs = le16_to_cpu(tl->tl_used);
  4223. mlog(0, "cleanup %u records from %llu\n", num_recs,
  4224. (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
  4225. mutex_lock(&tl_inode->i_mutex);
  4226. for(i = 0; i < num_recs; i++) {
  4227. if (ocfs2_truncate_log_needs_flush(osb)) {
  4228. status = __ocfs2_flush_truncate_log(osb);
  4229. if (status < 0) {
  4230. mlog_errno(status);
  4231. goto bail_up;
  4232. }
  4233. }
  4234. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  4235. if (IS_ERR(handle)) {
  4236. status = PTR_ERR(handle);
  4237. mlog_errno(status);
  4238. goto bail_up;
  4239. }
  4240. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  4241. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  4242. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  4243. status = ocfs2_truncate_log_append(osb, handle,
  4244. start_blk, clusters);
  4245. ocfs2_commit_trans(osb, handle);
  4246. if (status < 0) {
  4247. mlog_errno(status);
  4248. goto bail_up;
  4249. }
  4250. }
  4251. bail_up:
  4252. mutex_unlock(&tl_inode->i_mutex);
  4253. mlog_exit(status);
  4254. return status;
  4255. }
  4256. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  4257. {
  4258. int status;
  4259. struct inode *tl_inode = osb->osb_tl_inode;
  4260. mlog_entry_void();
  4261. if (tl_inode) {
  4262. cancel_delayed_work(&osb->osb_truncate_log_wq);
  4263. flush_workqueue(ocfs2_wq);
  4264. status = ocfs2_flush_truncate_log(osb);
  4265. if (status < 0)
  4266. mlog_errno(status);
  4267. brelse(osb->osb_tl_bh);
  4268. iput(osb->osb_tl_inode);
  4269. }
  4270. mlog_exit_void();
  4271. }
  4272. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  4273. {
  4274. int status;
  4275. struct inode *tl_inode = NULL;
  4276. struct buffer_head *tl_bh = NULL;
  4277. mlog_entry_void();
  4278. status = ocfs2_get_truncate_log_info(osb,
  4279. osb->slot_num,
  4280. &tl_inode,
  4281. &tl_bh);
  4282. if (status < 0)
  4283. mlog_errno(status);
  4284. /* ocfs2_truncate_log_shutdown keys on the existence of
  4285. * osb->osb_tl_inode so we don't set any of the osb variables
  4286. * until we're sure all is well. */
  4287. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  4288. ocfs2_truncate_log_worker);
  4289. osb->osb_tl_bh = tl_bh;
  4290. osb->osb_tl_inode = tl_inode;
  4291. mlog_exit(status);
  4292. return status;
  4293. }
  4294. /*
  4295. * Delayed de-allocation of suballocator blocks.
  4296. *
  4297. * Some sets of block de-allocations might involve multiple suballocator inodes.
  4298. *
  4299. * The locking for this can get extremely complicated, especially when
  4300. * the suballocator inodes to delete from aren't known until deep
  4301. * within an unrelated codepath.
  4302. *
  4303. * ocfs2_extent_block structures are a good example of this - an inode
  4304. * btree could have been grown by any number of nodes each allocating
  4305. * out of their own suballoc inode.
  4306. *
  4307. * These structures allow the delay of block de-allocation until a
  4308. * later time, when locking of multiple cluster inodes won't cause
  4309. * deadlock.
  4310. */
  4311. /*
  4312. * Describes a single block free from a suballocator
  4313. */
  4314. struct ocfs2_cached_block_free {
  4315. struct ocfs2_cached_block_free *free_next;
  4316. u64 free_blk;
  4317. unsigned int free_bit;
  4318. };
  4319. struct ocfs2_per_slot_free_list {
  4320. struct ocfs2_per_slot_free_list *f_next_suballocator;
  4321. int f_inode_type;
  4322. int f_slot;
  4323. struct ocfs2_cached_block_free *f_first;
  4324. };
  4325. static int ocfs2_free_cached_items(struct ocfs2_super *osb,
  4326. int sysfile_type,
  4327. int slot,
  4328. struct ocfs2_cached_block_free *head)
  4329. {
  4330. int ret;
  4331. u64 bg_blkno;
  4332. handle_t *handle;
  4333. struct inode *inode;
  4334. struct buffer_head *di_bh = NULL;
  4335. struct ocfs2_cached_block_free *tmp;
  4336. inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
  4337. if (!inode) {
  4338. ret = -EINVAL;
  4339. mlog_errno(ret);
  4340. goto out;
  4341. }
  4342. mutex_lock(&inode->i_mutex);
  4343. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  4344. if (ret) {
  4345. mlog_errno(ret);
  4346. goto out_mutex;
  4347. }
  4348. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  4349. if (IS_ERR(handle)) {
  4350. ret = PTR_ERR(handle);
  4351. mlog_errno(ret);
  4352. goto out_unlock;
  4353. }
  4354. while (head) {
  4355. bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
  4356. head->free_bit);
  4357. mlog(0, "Free bit: (bit %u, blkno %llu)\n",
  4358. head->free_bit, (unsigned long long)head->free_blk);
  4359. ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
  4360. head->free_bit, bg_blkno, 1);
  4361. if (ret) {
  4362. mlog_errno(ret);
  4363. goto out_journal;
  4364. }
  4365. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  4366. if (ret) {
  4367. mlog_errno(ret);
  4368. goto out_journal;
  4369. }
  4370. tmp = head;
  4371. head = head->free_next;
  4372. kfree(tmp);
  4373. }
  4374. out_journal:
  4375. ocfs2_commit_trans(osb, handle);
  4376. out_unlock:
  4377. ocfs2_meta_unlock(inode, 1);
  4378. brelse(di_bh);
  4379. out_mutex:
  4380. mutex_unlock(&inode->i_mutex);
  4381. iput(inode);
  4382. out:
  4383. while(head) {
  4384. /* Premature exit may have left some dangling items. */
  4385. tmp = head;
  4386. head = head->free_next;
  4387. kfree(tmp);
  4388. }
  4389. return ret;
  4390. }
  4391. int ocfs2_run_deallocs(struct ocfs2_super *osb,
  4392. struct ocfs2_cached_dealloc_ctxt *ctxt)
  4393. {
  4394. int ret = 0, ret2;
  4395. struct ocfs2_per_slot_free_list *fl;
  4396. if (!ctxt)
  4397. return 0;
  4398. while (ctxt->c_first_suballocator) {
  4399. fl = ctxt->c_first_suballocator;
  4400. if (fl->f_first) {
  4401. mlog(0, "Free items: (type %u, slot %d)\n",
  4402. fl->f_inode_type, fl->f_slot);
  4403. ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
  4404. fl->f_slot, fl->f_first);
  4405. if (ret2)
  4406. mlog_errno(ret2);
  4407. if (!ret)
  4408. ret = ret2;
  4409. }
  4410. ctxt->c_first_suballocator = fl->f_next_suballocator;
  4411. kfree(fl);
  4412. }
  4413. return ret;
  4414. }
  4415. static struct ocfs2_per_slot_free_list *
  4416. ocfs2_find_per_slot_free_list(int type,
  4417. int slot,
  4418. struct ocfs2_cached_dealloc_ctxt *ctxt)
  4419. {
  4420. struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
  4421. while (fl) {
  4422. if (fl->f_inode_type == type && fl->f_slot == slot)
  4423. return fl;
  4424. fl = fl->f_next_suballocator;
  4425. }
  4426. fl = kmalloc(sizeof(*fl), GFP_NOFS);
  4427. if (fl) {
  4428. fl->f_inode_type = type;
  4429. fl->f_slot = slot;
  4430. fl->f_first = NULL;
  4431. fl->f_next_suballocator = ctxt->c_first_suballocator;
  4432. ctxt->c_first_suballocator = fl;
  4433. }
  4434. return fl;
  4435. }
  4436. static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  4437. int type, int slot, u64 blkno,
  4438. unsigned int bit)
  4439. {
  4440. int ret;
  4441. struct ocfs2_per_slot_free_list *fl;
  4442. struct ocfs2_cached_block_free *item;
  4443. fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
  4444. if (fl == NULL) {
  4445. ret = -ENOMEM;
  4446. mlog_errno(ret);
  4447. goto out;
  4448. }
  4449. item = kmalloc(sizeof(*item), GFP_NOFS);
  4450. if (item == NULL) {
  4451. ret = -ENOMEM;
  4452. mlog_errno(ret);
  4453. goto out;
  4454. }
  4455. mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
  4456. type, slot, bit, (unsigned long long)blkno);
  4457. item->free_blk = blkno;
  4458. item->free_bit = bit;
  4459. item->free_next = fl->f_first;
  4460. fl->f_first = item;
  4461. ret = 0;
  4462. out:
  4463. return ret;
  4464. }
  4465. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  4466. struct ocfs2_extent_block *eb)
  4467. {
  4468. return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
  4469. le16_to_cpu(eb->h_suballoc_slot),
  4470. le64_to_cpu(eb->h_blkno),
  4471. le16_to_cpu(eb->h_suballoc_bit));
  4472. }
  4473. /* This function will figure out whether the currently last extent
  4474. * block will be deleted, and if it will, what the new last extent
  4475. * block will be so we can update his h_next_leaf_blk field, as well
  4476. * as the dinodes i_last_eb_blk */
  4477. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  4478. unsigned int clusters_to_del,
  4479. struct ocfs2_path *path,
  4480. struct buffer_head **new_last_eb)
  4481. {
  4482. int next_free, ret = 0;
  4483. u32 cpos;
  4484. struct ocfs2_extent_rec *rec;
  4485. struct ocfs2_extent_block *eb;
  4486. struct ocfs2_extent_list *el;
  4487. struct buffer_head *bh = NULL;
  4488. *new_last_eb = NULL;
  4489. /* we have no tree, so of course, no last_eb. */
  4490. if (!path->p_tree_depth)
  4491. goto out;
  4492. /* trunc to zero special case - this makes tree_depth = 0
  4493. * regardless of what it is. */
  4494. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  4495. goto out;
  4496. el = path_leaf_el(path);
  4497. BUG_ON(!el->l_next_free_rec);
  4498. /*
  4499. * Make sure that this extent list will actually be empty
  4500. * after we clear away the data. We can shortcut out if
  4501. * there's more than one non-empty extent in the
  4502. * list. Otherwise, a check of the remaining extent is
  4503. * necessary.
  4504. */
  4505. next_free = le16_to_cpu(el->l_next_free_rec);
  4506. rec = NULL;
  4507. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  4508. if (next_free > 2)
  4509. goto out;
  4510. /* We may have a valid extent in index 1, check it. */
  4511. if (next_free == 2)
  4512. rec = &el->l_recs[1];
  4513. /*
  4514. * Fall through - no more nonempty extents, so we want
  4515. * to delete this leaf.
  4516. */
  4517. } else {
  4518. if (next_free > 1)
  4519. goto out;
  4520. rec = &el->l_recs[0];
  4521. }
  4522. if (rec) {
  4523. /*
  4524. * Check it we'll only be trimming off the end of this
  4525. * cluster.
  4526. */
  4527. if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
  4528. goto out;
  4529. }
  4530. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  4531. if (ret) {
  4532. mlog_errno(ret);
  4533. goto out;
  4534. }
  4535. ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
  4536. if (ret) {
  4537. mlog_errno(ret);
  4538. goto out;
  4539. }
  4540. eb = (struct ocfs2_extent_block *) bh->b_data;
  4541. el = &eb->h_list;
  4542. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  4543. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  4544. ret = -EROFS;
  4545. goto out;
  4546. }
  4547. *new_last_eb = bh;
  4548. get_bh(*new_last_eb);
  4549. mlog(0, "returning block %llu, (cpos: %u)\n",
  4550. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  4551. out:
  4552. brelse(bh);
  4553. return ret;
  4554. }
  4555. /*
  4556. * Trim some clusters off the rightmost edge of a tree. Only called
  4557. * during truncate.
  4558. *
  4559. * The caller needs to:
  4560. * - start journaling of each path component.
  4561. * - compute and fully set up any new last ext block
  4562. */
  4563. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  4564. handle_t *handle, struct ocfs2_truncate_context *tc,
  4565. u32 clusters_to_del, u64 *delete_start)
  4566. {
  4567. int ret, i, index = path->p_tree_depth;
  4568. u32 new_edge = 0;
  4569. u64 deleted_eb = 0;
  4570. struct buffer_head *bh;
  4571. struct ocfs2_extent_list *el;
  4572. struct ocfs2_extent_rec *rec;
  4573. *delete_start = 0;
  4574. while (index >= 0) {
  4575. bh = path->p_node[index].bh;
  4576. el = path->p_node[index].el;
  4577. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  4578. index, (unsigned long long)bh->b_blocknr);
  4579. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  4580. if (index !=
  4581. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  4582. ocfs2_error(inode->i_sb,
  4583. "Inode %lu has invalid ext. block %llu",
  4584. inode->i_ino,
  4585. (unsigned long long)bh->b_blocknr);
  4586. ret = -EROFS;
  4587. goto out;
  4588. }
  4589. find_tail_record:
  4590. i = le16_to_cpu(el->l_next_free_rec) - 1;
  4591. rec = &el->l_recs[i];
  4592. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  4593. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  4594. ocfs2_rec_clusters(el, rec),
  4595. (unsigned long long)le64_to_cpu(rec->e_blkno),
  4596. le16_to_cpu(el->l_next_free_rec));
  4597. BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
  4598. if (le16_to_cpu(el->l_tree_depth) == 0) {
  4599. /*
  4600. * If the leaf block contains a single empty
  4601. * extent and no records, we can just remove
  4602. * the block.
  4603. */
  4604. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  4605. memset(rec, 0,
  4606. sizeof(struct ocfs2_extent_rec));
  4607. el->l_next_free_rec = cpu_to_le16(0);
  4608. goto delete;
  4609. }
  4610. /*
  4611. * Remove any empty extents by shifting things
  4612. * left. That should make life much easier on
  4613. * the code below. This condition is rare
  4614. * enough that we shouldn't see a performance
  4615. * hit.
  4616. */
  4617. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  4618. le16_add_cpu(&el->l_next_free_rec, -1);
  4619. for(i = 0;
  4620. i < le16_to_cpu(el->l_next_free_rec); i++)
  4621. el->l_recs[i] = el->l_recs[i + 1];
  4622. memset(&el->l_recs[i], 0,
  4623. sizeof(struct ocfs2_extent_rec));
  4624. /*
  4625. * We've modified our extent list. The
  4626. * simplest way to handle this change
  4627. * is to being the search from the
  4628. * start again.
  4629. */
  4630. goto find_tail_record;
  4631. }
  4632. le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
  4633. /*
  4634. * We'll use "new_edge" on our way back up the
  4635. * tree to know what our rightmost cpos is.
  4636. */
  4637. new_edge = le16_to_cpu(rec->e_leaf_clusters);
  4638. new_edge += le32_to_cpu(rec->e_cpos);
  4639. /*
  4640. * The caller will use this to delete data blocks.
  4641. */
  4642. *delete_start = le64_to_cpu(rec->e_blkno)
  4643. + ocfs2_clusters_to_blocks(inode->i_sb,
  4644. le16_to_cpu(rec->e_leaf_clusters));
  4645. /*
  4646. * If it's now empty, remove this record.
  4647. */
  4648. if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
  4649. memset(rec, 0,
  4650. sizeof(struct ocfs2_extent_rec));
  4651. le16_add_cpu(&el->l_next_free_rec, -1);
  4652. }
  4653. } else {
  4654. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  4655. memset(rec, 0,
  4656. sizeof(struct ocfs2_extent_rec));
  4657. le16_add_cpu(&el->l_next_free_rec, -1);
  4658. goto delete;
  4659. }
  4660. /* Can this actually happen? */
  4661. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4662. goto delete;
  4663. /*
  4664. * We never actually deleted any clusters
  4665. * because our leaf was empty. There's no
  4666. * reason to adjust the rightmost edge then.
  4667. */
  4668. if (new_edge == 0)
  4669. goto delete;
  4670. rec->e_int_clusters = cpu_to_le32(new_edge);
  4671. le32_add_cpu(&rec->e_int_clusters,
  4672. -le32_to_cpu(rec->e_cpos));
  4673. /*
  4674. * A deleted child record should have been
  4675. * caught above.
  4676. */
  4677. BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
  4678. }
  4679. delete:
  4680. ret = ocfs2_journal_dirty(handle, bh);
  4681. if (ret) {
  4682. mlog_errno(ret);
  4683. goto out;
  4684. }
  4685. mlog(0, "extent list container %llu, after: record %d: "
  4686. "(%u, %u, %llu), next = %u.\n",
  4687. (unsigned long long)bh->b_blocknr, i,
  4688. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
  4689. (unsigned long long)le64_to_cpu(rec->e_blkno),
  4690. le16_to_cpu(el->l_next_free_rec));
  4691. /*
  4692. * We must be careful to only attempt delete of an
  4693. * extent block (and not the root inode block).
  4694. */
  4695. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  4696. struct ocfs2_extent_block *eb =
  4697. (struct ocfs2_extent_block *)bh->b_data;
  4698. /*
  4699. * Save this for use when processing the
  4700. * parent block.
  4701. */
  4702. deleted_eb = le64_to_cpu(eb->h_blkno);
  4703. mlog(0, "deleting this extent block.\n");
  4704. ocfs2_remove_from_cache(inode, bh);
  4705. BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
  4706. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  4707. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  4708. ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
  4709. /* An error here is not fatal. */
  4710. if (ret < 0)
  4711. mlog_errno(ret);
  4712. } else {
  4713. deleted_eb = 0;
  4714. }
  4715. index--;
  4716. }
  4717. ret = 0;
  4718. out:
  4719. return ret;
  4720. }
  4721. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  4722. unsigned int clusters_to_del,
  4723. struct inode *inode,
  4724. struct buffer_head *fe_bh,
  4725. handle_t *handle,
  4726. struct ocfs2_truncate_context *tc,
  4727. struct ocfs2_path *path)
  4728. {
  4729. int status;
  4730. struct ocfs2_dinode *fe;
  4731. struct ocfs2_extent_block *last_eb = NULL;
  4732. struct ocfs2_extent_list *el;
  4733. struct buffer_head *last_eb_bh = NULL;
  4734. u64 delete_blk = 0;
  4735. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  4736. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  4737. path, &last_eb_bh);
  4738. if (status < 0) {
  4739. mlog_errno(status);
  4740. goto bail;
  4741. }
  4742. /*
  4743. * Each component will be touched, so we might as well journal
  4744. * here to avoid having to handle errors later.
  4745. */
  4746. status = ocfs2_journal_access_path(inode, handle, path);
  4747. if (status < 0) {
  4748. mlog_errno(status);
  4749. goto bail;
  4750. }
  4751. if (last_eb_bh) {
  4752. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  4753. OCFS2_JOURNAL_ACCESS_WRITE);
  4754. if (status < 0) {
  4755. mlog_errno(status);
  4756. goto bail;
  4757. }
  4758. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4759. }
  4760. el = &(fe->id2.i_list);
  4761. /*
  4762. * Lower levels depend on this never happening, but it's best
  4763. * to check it up here before changing the tree.
  4764. */
  4765. if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
  4766. ocfs2_error(inode->i_sb,
  4767. "Inode %lu has an empty extent record, depth %u\n",
  4768. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  4769. status = -EROFS;
  4770. goto bail;
  4771. }
  4772. spin_lock(&OCFS2_I(inode)->ip_lock);
  4773. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  4774. clusters_to_del;
  4775. spin_unlock(&OCFS2_I(inode)->ip_lock);
  4776. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  4777. inode->i_blocks = ocfs2_inode_sector_count(inode);
  4778. status = ocfs2_trim_tree(inode, path, handle, tc,
  4779. clusters_to_del, &delete_blk);
  4780. if (status) {
  4781. mlog_errno(status);
  4782. goto bail;
  4783. }
  4784. if (le32_to_cpu(fe->i_clusters) == 0) {
  4785. /* trunc to zero is a special case. */
  4786. el->l_tree_depth = 0;
  4787. fe->i_last_eb_blk = 0;
  4788. } else if (last_eb)
  4789. fe->i_last_eb_blk = last_eb->h_blkno;
  4790. status = ocfs2_journal_dirty(handle, fe_bh);
  4791. if (status < 0) {
  4792. mlog_errno(status);
  4793. goto bail;
  4794. }
  4795. if (last_eb) {
  4796. /* If there will be a new last extent block, then by
  4797. * definition, there cannot be any leaves to the right of
  4798. * him. */
  4799. last_eb->h_next_leaf_blk = 0;
  4800. status = ocfs2_journal_dirty(handle, last_eb_bh);
  4801. if (status < 0) {
  4802. mlog_errno(status);
  4803. goto bail;
  4804. }
  4805. }
  4806. if (delete_blk) {
  4807. status = ocfs2_truncate_log_append(osb, handle, delete_blk,
  4808. clusters_to_del);
  4809. if (status < 0) {
  4810. mlog_errno(status);
  4811. goto bail;
  4812. }
  4813. }
  4814. status = 0;
  4815. bail:
  4816. mlog_exit(status);
  4817. return status;
  4818. }
  4819. static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
  4820. {
  4821. set_buffer_uptodate(bh);
  4822. mark_buffer_dirty(bh);
  4823. return 0;
  4824. }
  4825. static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
  4826. {
  4827. set_buffer_uptodate(bh);
  4828. mark_buffer_dirty(bh);
  4829. return ocfs2_journal_dirty_data(handle, bh);
  4830. }
  4831. static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
  4832. unsigned int from, unsigned int to,
  4833. struct page *page, int zero, u64 *phys)
  4834. {
  4835. int ret, partial = 0;
  4836. ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
  4837. if (ret)
  4838. mlog_errno(ret);
  4839. if (zero)
  4840. zero_user_page(page, from, to - from, KM_USER0);
  4841. /*
  4842. * Need to set the buffers we zero'd into uptodate
  4843. * here if they aren't - ocfs2_map_page_blocks()
  4844. * might've skipped some
  4845. */
  4846. if (ocfs2_should_order_data(inode)) {
  4847. ret = walk_page_buffers(handle,
  4848. page_buffers(page),
  4849. from, to, &partial,
  4850. ocfs2_ordered_zero_func);
  4851. if (ret < 0)
  4852. mlog_errno(ret);
  4853. } else {
  4854. ret = walk_page_buffers(handle, page_buffers(page),
  4855. from, to, &partial,
  4856. ocfs2_writeback_zero_func);
  4857. if (ret < 0)
  4858. mlog_errno(ret);
  4859. }
  4860. if (!partial)
  4861. SetPageUptodate(page);
  4862. flush_dcache_page(page);
  4863. }
  4864. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
  4865. loff_t end, struct page **pages,
  4866. int numpages, u64 phys, handle_t *handle)
  4867. {
  4868. int i;
  4869. struct page *page;
  4870. unsigned int from, to = PAGE_CACHE_SIZE;
  4871. struct super_block *sb = inode->i_sb;
  4872. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  4873. if (numpages == 0)
  4874. goto out;
  4875. to = PAGE_CACHE_SIZE;
  4876. for(i = 0; i < numpages; i++) {
  4877. page = pages[i];
  4878. from = start & (PAGE_CACHE_SIZE - 1);
  4879. if ((end >> PAGE_CACHE_SHIFT) == page->index)
  4880. to = end & (PAGE_CACHE_SIZE - 1);
  4881. BUG_ON(from > PAGE_CACHE_SIZE);
  4882. BUG_ON(to > PAGE_CACHE_SIZE);
  4883. ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
  4884. &phys);
  4885. start = (page->index + 1) << PAGE_CACHE_SHIFT;
  4886. }
  4887. out:
  4888. if (pages)
  4889. ocfs2_unlock_and_free_pages(pages, numpages);
  4890. }
  4891. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
  4892. struct page **pages, int *num)
  4893. {
  4894. int numpages, ret = 0;
  4895. struct super_block *sb = inode->i_sb;
  4896. struct address_space *mapping = inode->i_mapping;
  4897. unsigned long index;
  4898. loff_t last_page_bytes;
  4899. BUG_ON(start > end);
  4900. BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
  4901. (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
  4902. numpages = 0;
  4903. last_page_bytes = PAGE_ALIGN(end);
  4904. index = start >> PAGE_CACHE_SHIFT;
  4905. do {
  4906. pages[numpages] = grab_cache_page(mapping, index);
  4907. if (!pages[numpages]) {
  4908. ret = -ENOMEM;
  4909. mlog_errno(ret);
  4910. goto out;
  4911. }
  4912. numpages++;
  4913. index++;
  4914. } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
  4915. out:
  4916. if (ret != 0) {
  4917. if (pages)
  4918. ocfs2_unlock_and_free_pages(pages, numpages);
  4919. numpages = 0;
  4920. }
  4921. *num = numpages;
  4922. return ret;
  4923. }
  4924. /*
  4925. * Zero the area past i_size but still within an allocated
  4926. * cluster. This avoids exposing nonzero data on subsequent file
  4927. * extends.
  4928. *
  4929. * We need to call this before i_size is updated on the inode because
  4930. * otherwise block_write_full_page() will skip writeout of pages past
  4931. * i_size. The new_i_size parameter is passed for this reason.
  4932. */
  4933. int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
  4934. u64 range_start, u64 range_end)
  4935. {
  4936. int ret = 0, numpages;
  4937. struct page **pages = NULL;
  4938. u64 phys;
  4939. unsigned int ext_flags;
  4940. struct super_block *sb = inode->i_sb;
  4941. /*
  4942. * File systems which don't support sparse files zero on every
  4943. * extend.
  4944. */
  4945. if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
  4946. return 0;
  4947. pages = kcalloc(ocfs2_pages_per_cluster(sb),
  4948. sizeof(struct page *), GFP_NOFS);
  4949. if (pages == NULL) {
  4950. ret = -ENOMEM;
  4951. mlog_errno(ret);
  4952. goto out;
  4953. }
  4954. if (range_start == range_end)
  4955. goto out;
  4956. ret = ocfs2_extent_map_get_blocks(inode,
  4957. range_start >> sb->s_blocksize_bits,
  4958. &phys, NULL, &ext_flags);
  4959. if (ret) {
  4960. mlog_errno(ret);
  4961. goto out;
  4962. }
  4963. /*
  4964. * Tail is a hole, or is marked unwritten. In either case, we
  4965. * can count on read and write to return/push zero's.
  4966. */
  4967. if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
  4968. goto out;
  4969. ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
  4970. &numpages);
  4971. if (ret) {
  4972. mlog_errno(ret);
  4973. goto out;
  4974. }
  4975. ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
  4976. numpages, phys, handle);
  4977. /*
  4978. * Initiate writeout of the pages we zero'd here. We don't
  4979. * wait on them - the truncate_inode_pages() call later will
  4980. * do that for us.
  4981. */
  4982. ret = do_sync_mapping_range(inode->i_mapping, range_start,
  4983. range_end - 1, SYNC_FILE_RANGE_WRITE);
  4984. if (ret)
  4985. mlog_errno(ret);
  4986. out:
  4987. if (pages)
  4988. kfree(pages);
  4989. return ret;
  4990. }
  4991. static void ocfs2_zero_dinode_id2(struct inode *inode, struct ocfs2_dinode *di)
  4992. {
  4993. unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
  4994. memset(&di->id2, 0, blocksize - offsetof(struct ocfs2_dinode, id2));
  4995. }
  4996. void ocfs2_dinode_new_extent_list(struct inode *inode,
  4997. struct ocfs2_dinode *di)
  4998. {
  4999. ocfs2_zero_dinode_id2(inode, di);
  5000. di->id2.i_list.l_tree_depth = 0;
  5001. di->id2.i_list.l_next_free_rec = 0;
  5002. di->id2.i_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(inode->i_sb));
  5003. }
  5004. void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
  5005. {
  5006. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  5007. struct ocfs2_inline_data *idata = &di->id2.i_data;
  5008. spin_lock(&oi->ip_lock);
  5009. oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
  5010. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  5011. spin_unlock(&oi->ip_lock);
  5012. /*
  5013. * We clear the entire i_data structure here so that all
  5014. * fields can be properly initialized.
  5015. */
  5016. ocfs2_zero_dinode_id2(inode, di);
  5017. idata->id_count = cpu_to_le16(ocfs2_max_inline_data(inode->i_sb));
  5018. }
  5019. int ocfs2_convert_inline_data_to_extents(struct inode *inode,
  5020. struct buffer_head *di_bh)
  5021. {
  5022. int ret, i, has_data, num_pages = 0;
  5023. handle_t *handle;
  5024. u64 uninitialized_var(block);
  5025. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  5026. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5027. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  5028. struct ocfs2_alloc_context *data_ac = NULL;
  5029. struct page **pages = NULL;
  5030. loff_t end = osb->s_clustersize;
  5031. has_data = i_size_read(inode) ? 1 : 0;
  5032. if (has_data) {
  5033. pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
  5034. sizeof(struct page *), GFP_NOFS);
  5035. if (pages == NULL) {
  5036. ret = -ENOMEM;
  5037. mlog_errno(ret);
  5038. goto out;
  5039. }
  5040. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  5041. if (ret) {
  5042. mlog_errno(ret);
  5043. goto out;
  5044. }
  5045. }
  5046. handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
  5047. if (IS_ERR(handle)) {
  5048. ret = PTR_ERR(handle);
  5049. mlog_errno(ret);
  5050. goto out_unlock;
  5051. }
  5052. ret = ocfs2_journal_access(handle, inode, di_bh,
  5053. OCFS2_JOURNAL_ACCESS_WRITE);
  5054. if (ret) {
  5055. mlog_errno(ret);
  5056. goto out_commit;
  5057. }
  5058. if (has_data) {
  5059. u32 bit_off, num;
  5060. unsigned int page_end;
  5061. u64 phys;
  5062. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
  5063. &num);
  5064. if (ret) {
  5065. mlog_errno(ret);
  5066. goto out_commit;
  5067. }
  5068. /*
  5069. * Save two copies, one for insert, and one that can
  5070. * be changed by ocfs2_map_and_dirty_page() below.
  5071. */
  5072. block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  5073. /*
  5074. * Non sparse file systems zero on extend, so no need
  5075. * to do that now.
  5076. */
  5077. if (!ocfs2_sparse_alloc(osb) &&
  5078. PAGE_CACHE_SIZE < osb->s_clustersize)
  5079. end = PAGE_CACHE_SIZE;
  5080. ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
  5081. if (ret) {
  5082. mlog_errno(ret);
  5083. goto out_commit;
  5084. }
  5085. /*
  5086. * This should populate the 1st page for us and mark
  5087. * it up to date.
  5088. */
  5089. ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
  5090. if (ret) {
  5091. mlog_errno(ret);
  5092. goto out_commit;
  5093. }
  5094. page_end = PAGE_CACHE_SIZE;
  5095. if (PAGE_CACHE_SIZE > osb->s_clustersize)
  5096. page_end = osb->s_clustersize;
  5097. for (i = 0; i < num_pages; i++)
  5098. ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
  5099. pages[i], i > 0, &phys);
  5100. }
  5101. spin_lock(&oi->ip_lock);
  5102. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  5103. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  5104. spin_unlock(&oi->ip_lock);
  5105. ocfs2_dinode_new_extent_list(inode, di);
  5106. ocfs2_journal_dirty(handle, di_bh);
  5107. if (has_data) {
  5108. /*
  5109. * An error at this point should be extremely rare. If
  5110. * this proves to be false, we could always re-build
  5111. * the in-inode data from our pages.
  5112. */
  5113. ret = ocfs2_insert_extent(osb, handle, inode, di_bh,
  5114. 0, block, 1, 0, NULL);
  5115. if (ret) {
  5116. mlog_errno(ret);
  5117. goto out_commit;
  5118. }
  5119. inode->i_blocks = ocfs2_inode_sector_count(inode);
  5120. }
  5121. out_commit:
  5122. ocfs2_commit_trans(osb, handle);
  5123. out_unlock:
  5124. if (data_ac)
  5125. ocfs2_free_alloc_context(data_ac);
  5126. out:
  5127. if (pages) {
  5128. ocfs2_unlock_and_free_pages(pages, num_pages);
  5129. kfree(pages);
  5130. }
  5131. return ret;
  5132. }
  5133. /*
  5134. * It is expected, that by the time you call this function,
  5135. * inode->i_size and fe->i_size have been adjusted.
  5136. *
  5137. * WARNING: This will kfree the truncate context
  5138. */
  5139. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  5140. struct inode *inode,
  5141. struct buffer_head *fe_bh,
  5142. struct ocfs2_truncate_context *tc)
  5143. {
  5144. int status, i, credits, tl_sem = 0;
  5145. u32 clusters_to_del, new_highest_cpos, range;
  5146. struct ocfs2_extent_list *el;
  5147. handle_t *handle = NULL;
  5148. struct inode *tl_inode = osb->osb_tl_inode;
  5149. struct ocfs2_path *path = NULL;
  5150. mlog_entry_void();
  5151. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  5152. i_size_read(inode));
  5153. path = ocfs2_new_inode_path(fe_bh);
  5154. if (!path) {
  5155. status = -ENOMEM;
  5156. mlog_errno(status);
  5157. goto bail;
  5158. }
  5159. ocfs2_extent_map_trunc(inode, new_highest_cpos);
  5160. start:
  5161. /*
  5162. * Check that we still have allocation to delete.
  5163. */
  5164. if (OCFS2_I(inode)->ip_clusters == 0) {
  5165. status = 0;
  5166. goto bail;
  5167. }
  5168. /*
  5169. * Truncate always works against the rightmost tree branch.
  5170. */
  5171. status = ocfs2_find_path(inode, path, UINT_MAX);
  5172. if (status) {
  5173. mlog_errno(status);
  5174. goto bail;
  5175. }
  5176. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  5177. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  5178. /*
  5179. * By now, el will point to the extent list on the bottom most
  5180. * portion of this tree. Only the tail record is considered in
  5181. * each pass.
  5182. *
  5183. * We handle the following cases, in order:
  5184. * - empty extent: delete the remaining branch
  5185. * - remove the entire record
  5186. * - remove a partial record
  5187. * - no record needs to be removed (truncate has completed)
  5188. */
  5189. el = path_leaf_el(path);
  5190. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  5191. ocfs2_error(inode->i_sb,
  5192. "Inode %llu has empty extent block at %llu\n",
  5193. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  5194. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  5195. status = -EROFS;
  5196. goto bail;
  5197. }
  5198. i = le16_to_cpu(el->l_next_free_rec) - 1;
  5199. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  5200. ocfs2_rec_clusters(el, &el->l_recs[i]);
  5201. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  5202. clusters_to_del = 0;
  5203. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  5204. clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
  5205. } else if (range > new_highest_cpos) {
  5206. clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
  5207. le32_to_cpu(el->l_recs[i].e_cpos)) -
  5208. new_highest_cpos;
  5209. } else {
  5210. status = 0;
  5211. goto bail;
  5212. }
  5213. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  5214. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  5215. BUG_ON(clusters_to_del == 0);
  5216. mutex_lock(&tl_inode->i_mutex);
  5217. tl_sem = 1;
  5218. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  5219. * record is free for use. If there isn't any, we flush to get
  5220. * an empty truncate log. */
  5221. if (ocfs2_truncate_log_needs_flush(osb)) {
  5222. status = __ocfs2_flush_truncate_log(osb);
  5223. if (status < 0) {
  5224. mlog_errno(status);
  5225. goto bail;
  5226. }
  5227. }
  5228. credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  5229. (struct ocfs2_dinode *)fe_bh->b_data,
  5230. el);
  5231. handle = ocfs2_start_trans(osb, credits);
  5232. if (IS_ERR(handle)) {
  5233. status = PTR_ERR(handle);
  5234. handle = NULL;
  5235. mlog_errno(status);
  5236. goto bail;
  5237. }
  5238. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  5239. tc, path);
  5240. if (status < 0) {
  5241. mlog_errno(status);
  5242. goto bail;
  5243. }
  5244. mutex_unlock(&tl_inode->i_mutex);
  5245. tl_sem = 0;
  5246. ocfs2_commit_trans(osb, handle);
  5247. handle = NULL;
  5248. ocfs2_reinit_path(path, 1);
  5249. /*
  5250. * The check above will catch the case where we've truncated
  5251. * away all allocation.
  5252. */
  5253. goto start;
  5254. bail:
  5255. ocfs2_schedule_truncate_log_flush(osb, 1);
  5256. if (tl_sem)
  5257. mutex_unlock(&tl_inode->i_mutex);
  5258. if (handle)
  5259. ocfs2_commit_trans(osb, handle);
  5260. ocfs2_run_deallocs(osb, &tc->tc_dealloc);
  5261. ocfs2_free_path(path);
  5262. /* This will drop the ext_alloc cluster lock for us */
  5263. ocfs2_free_truncate_context(tc);
  5264. mlog_exit(status);
  5265. return status;
  5266. }
  5267. /*
  5268. * Expects the inode to already be locked.
  5269. */
  5270. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  5271. struct inode *inode,
  5272. struct buffer_head *fe_bh,
  5273. struct ocfs2_truncate_context **tc)
  5274. {
  5275. int status;
  5276. unsigned int new_i_clusters;
  5277. struct ocfs2_dinode *fe;
  5278. struct ocfs2_extent_block *eb;
  5279. struct buffer_head *last_eb_bh = NULL;
  5280. mlog_entry_void();
  5281. *tc = NULL;
  5282. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  5283. i_size_read(inode));
  5284. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  5285. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  5286. "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
  5287. (unsigned long long)le64_to_cpu(fe->i_size));
  5288. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  5289. if (!(*tc)) {
  5290. status = -ENOMEM;
  5291. mlog_errno(status);
  5292. goto bail;
  5293. }
  5294. ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
  5295. if (fe->id2.i_list.l_tree_depth) {
  5296. status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  5297. &last_eb_bh, OCFS2_BH_CACHED, inode);
  5298. if (status < 0) {
  5299. mlog_errno(status);
  5300. goto bail;
  5301. }
  5302. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  5303. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  5304. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  5305. brelse(last_eb_bh);
  5306. status = -EIO;
  5307. goto bail;
  5308. }
  5309. }
  5310. (*tc)->tc_last_eb_bh = last_eb_bh;
  5311. status = 0;
  5312. bail:
  5313. if (status < 0) {
  5314. if (*tc)
  5315. ocfs2_free_truncate_context(*tc);
  5316. *tc = NULL;
  5317. }
  5318. mlog_exit_void();
  5319. return status;
  5320. }
  5321. /*
  5322. * 'start' is inclusive, 'end' is not.
  5323. */
  5324. int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
  5325. unsigned int start, unsigned int end, int trunc)
  5326. {
  5327. int ret;
  5328. unsigned int numbytes;
  5329. handle_t *handle;
  5330. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5331. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  5332. struct ocfs2_inline_data *idata = &di->id2.i_data;
  5333. if (end > i_size_read(inode))
  5334. end = i_size_read(inode);
  5335. BUG_ON(start >= end);
  5336. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  5337. !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
  5338. !ocfs2_supports_inline_data(osb)) {
  5339. ocfs2_error(inode->i_sb,
  5340. "Inline data flags for inode %llu don't agree! "
  5341. "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
  5342. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  5343. le16_to_cpu(di->i_dyn_features),
  5344. OCFS2_I(inode)->ip_dyn_features,
  5345. osb->s_feature_incompat);
  5346. ret = -EROFS;
  5347. goto out;
  5348. }
  5349. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  5350. if (IS_ERR(handle)) {
  5351. ret = PTR_ERR(handle);
  5352. mlog_errno(ret);
  5353. goto out;
  5354. }
  5355. ret = ocfs2_journal_access(handle, inode, di_bh,
  5356. OCFS2_JOURNAL_ACCESS_WRITE);
  5357. if (ret) {
  5358. mlog_errno(ret);
  5359. goto out_commit;
  5360. }
  5361. numbytes = end - start;
  5362. memset(idata->id_data + start, 0, numbytes);
  5363. /*
  5364. * No need to worry about the data page here - it's been
  5365. * truncated already and inline data doesn't need it for
  5366. * pushing zero's to disk, so we'll let readpage pick it up
  5367. * later.
  5368. */
  5369. if (trunc) {
  5370. i_size_write(inode, start);
  5371. di->i_size = cpu_to_le64(start);
  5372. }
  5373. inode->i_blocks = ocfs2_inode_sector_count(inode);
  5374. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  5375. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  5376. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  5377. ocfs2_journal_dirty(handle, di_bh);
  5378. out_commit:
  5379. ocfs2_commit_trans(osb, handle);
  5380. out:
  5381. return ret;
  5382. }
  5383. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  5384. {
  5385. /*
  5386. * The caller is responsible for completing deallocation
  5387. * before freeing the context.
  5388. */
  5389. if (tc->tc_dealloc.c_first_suballocator != NULL)
  5390. mlog(ML_NOTICE,
  5391. "Truncate completion has non-empty dealloc context\n");
  5392. if (tc->tc_last_eb_bh)
  5393. brelse(tc->tc_last_eb_bh);
  5394. kfree(tc);
  5395. }