alloc.c 197 KB

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