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