alloc.c 190 KB

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