alloc.c 192 KB

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