alloc.c 186 KB

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