alloc.c 195 KB

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