alloc.c 189 KB

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