alloc.c 196 KB

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