alloc.c 195 KB

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