alloc.c 179 KB

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