alloc.c 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421
  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. ocfs2_error(sb,
  755. "Extent block #%llu has bad signature %.*s",
  756. (unsigned long long)bh->b_blocknr, 7,
  757. eb->h_signature);
  758. return -EINVAL;
  759. }
  760. if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
  761. ocfs2_error(sb,
  762. "Extent block #%llu has an invalid h_blkno "
  763. "of %llu",
  764. (unsigned long long)bh->b_blocknr,
  765. (unsigned long long)le64_to_cpu(eb->h_blkno));
  766. return -EINVAL;
  767. }
  768. if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  769. ocfs2_error(sb,
  770. "Extent block #%llu has an invalid "
  771. "h_fs_generation of #%u",
  772. (unsigned long long)bh->b_blocknr,
  773. le32_to_cpu(eb->h_fs_generation));
  774. return -EINVAL;
  775. }
  776. return 0;
  777. }
  778. int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
  779. struct buffer_head **bh)
  780. {
  781. int rc;
  782. struct buffer_head *tmp = *bh;
  783. rc = ocfs2_read_block(ci, eb_blkno, &tmp,
  784. ocfs2_validate_extent_block);
  785. /* If ocfs2_read_block() got us a new bh, pass it up. */
  786. if (!rc && !*bh)
  787. *bh = tmp;
  788. return rc;
  789. }
  790. /*
  791. * How many free extents have we got before we need more meta data?
  792. */
  793. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  794. struct ocfs2_extent_tree *et)
  795. {
  796. int retval;
  797. struct ocfs2_extent_list *el = NULL;
  798. struct ocfs2_extent_block *eb;
  799. struct buffer_head *eb_bh = NULL;
  800. u64 last_eb_blk = 0;
  801. el = et->et_root_el;
  802. last_eb_blk = ocfs2_et_get_last_eb_blk(et);
  803. if (last_eb_blk) {
  804. retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
  805. &eb_bh);
  806. if (retval < 0) {
  807. mlog_errno(retval);
  808. goto bail;
  809. }
  810. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  811. el = &eb->h_list;
  812. }
  813. BUG_ON(el->l_tree_depth != 0);
  814. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  815. bail:
  816. brelse(eb_bh);
  817. trace_ocfs2_num_free_extents(retval);
  818. return retval;
  819. }
  820. /* expects array to already be allocated
  821. *
  822. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  823. * l_count for you
  824. */
  825. static int ocfs2_create_new_meta_bhs(handle_t *handle,
  826. struct ocfs2_extent_tree *et,
  827. int wanted,
  828. struct ocfs2_alloc_context *meta_ac,
  829. struct buffer_head *bhs[])
  830. {
  831. int count, status, i;
  832. u16 suballoc_bit_start;
  833. u32 num_got;
  834. u64 suballoc_loc, first_blkno;
  835. struct ocfs2_super *osb =
  836. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  837. struct ocfs2_extent_block *eb;
  838. count = 0;
  839. while (count < wanted) {
  840. status = ocfs2_claim_metadata(handle,
  841. meta_ac,
  842. wanted - count,
  843. &suballoc_loc,
  844. &suballoc_bit_start,
  845. &num_got,
  846. &first_blkno);
  847. if (status < 0) {
  848. mlog_errno(status);
  849. goto bail;
  850. }
  851. for(i = count; i < (num_got + count); i++) {
  852. bhs[i] = sb_getblk(osb->sb, first_blkno);
  853. if (bhs[i] == NULL) {
  854. status = -ENOMEM;
  855. mlog_errno(status);
  856. goto bail;
  857. }
  858. ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
  859. status = ocfs2_journal_access_eb(handle, et->et_ci,
  860. bhs[i],
  861. OCFS2_JOURNAL_ACCESS_CREATE);
  862. if (status < 0) {
  863. mlog_errno(status);
  864. goto bail;
  865. }
  866. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  867. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  868. /* Ok, setup the minimal stuff here. */
  869. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  870. eb->h_blkno = cpu_to_le64(first_blkno);
  871. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  872. eb->h_suballoc_slot =
  873. cpu_to_le16(meta_ac->ac_alloc_slot);
  874. eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
  875. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  876. eb->h_list.l_count =
  877. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  878. suballoc_bit_start++;
  879. first_blkno++;
  880. /* We'll also be dirtied by the caller, so
  881. * this isn't absolutely necessary. */
  882. ocfs2_journal_dirty(handle, bhs[i]);
  883. }
  884. count += num_got;
  885. }
  886. status = 0;
  887. bail:
  888. if (status < 0) {
  889. for(i = 0; i < wanted; i++) {
  890. brelse(bhs[i]);
  891. bhs[i] = NULL;
  892. }
  893. mlog_errno(status);
  894. }
  895. return status;
  896. }
  897. /*
  898. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  899. *
  900. * Returns the sum of the rightmost extent rec logical offset and
  901. * cluster count.
  902. *
  903. * ocfs2_add_branch() uses this to determine what logical cluster
  904. * value should be populated into the leftmost new branch records.
  905. *
  906. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  907. * value for the new topmost tree record.
  908. */
  909. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  910. {
  911. int i;
  912. i = le16_to_cpu(el->l_next_free_rec) - 1;
  913. return le32_to_cpu(el->l_recs[i].e_cpos) +
  914. ocfs2_rec_clusters(el, &el->l_recs[i]);
  915. }
  916. /*
  917. * Change range of the branches in the right most path according to the leaf
  918. * extent block's rightmost record.
  919. */
  920. static int ocfs2_adjust_rightmost_branch(handle_t *handle,
  921. struct ocfs2_extent_tree *et)
  922. {
  923. int status;
  924. struct ocfs2_path *path = NULL;
  925. struct ocfs2_extent_list *el;
  926. struct ocfs2_extent_rec *rec;
  927. path = ocfs2_new_path_from_et(et);
  928. if (!path) {
  929. status = -ENOMEM;
  930. return status;
  931. }
  932. status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
  933. if (status < 0) {
  934. mlog_errno(status);
  935. goto out;
  936. }
  937. status = ocfs2_extend_trans(handle, path_num_items(path));
  938. if (status < 0) {
  939. mlog_errno(status);
  940. goto out;
  941. }
  942. status = ocfs2_journal_access_path(et->et_ci, handle, path);
  943. if (status < 0) {
  944. mlog_errno(status);
  945. goto out;
  946. }
  947. el = path_leaf_el(path);
  948. rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1];
  949. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  950. out:
  951. ocfs2_free_path(path);
  952. return status;
  953. }
  954. /*
  955. * Add an entire tree branch to our inode. eb_bh is the extent block
  956. * to start at, if we don't want to start the branch at the root
  957. * structure.
  958. *
  959. * last_eb_bh is required as we have to update it's next_leaf pointer
  960. * for the new last extent block.
  961. *
  962. * the new branch will be 'empty' in the sense that every block will
  963. * contain a single record with cluster count == 0.
  964. */
  965. static int ocfs2_add_branch(handle_t *handle,
  966. struct ocfs2_extent_tree *et,
  967. struct buffer_head *eb_bh,
  968. struct buffer_head **last_eb_bh,
  969. struct ocfs2_alloc_context *meta_ac)
  970. {
  971. int status, new_blocks, i;
  972. u64 next_blkno, new_last_eb_blk;
  973. struct buffer_head *bh;
  974. struct buffer_head **new_eb_bhs = NULL;
  975. struct ocfs2_extent_block *eb;
  976. struct ocfs2_extent_list *eb_el;
  977. struct ocfs2_extent_list *el;
  978. u32 new_cpos, root_end;
  979. BUG_ON(!last_eb_bh || !*last_eb_bh);
  980. if (eb_bh) {
  981. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  982. el = &eb->h_list;
  983. } else
  984. el = et->et_root_el;
  985. /* we never add a branch to a leaf. */
  986. BUG_ON(!el->l_tree_depth);
  987. new_blocks = le16_to_cpu(el->l_tree_depth);
  988. eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
  989. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  990. root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
  991. /*
  992. * If there is a gap before the root end and the real end
  993. * of the righmost leaf block, we need to remove the gap
  994. * between new_cpos and root_end first so that the tree
  995. * is consistent after we add a new branch(it will start
  996. * from new_cpos).
  997. */
  998. if (root_end > new_cpos) {
  999. trace_ocfs2_adjust_rightmost_branch(
  1000. (unsigned long long)
  1001. ocfs2_metadata_cache_owner(et->et_ci),
  1002. root_end, new_cpos);
  1003. status = ocfs2_adjust_rightmost_branch(handle, et);
  1004. if (status) {
  1005. mlog_errno(status);
  1006. goto bail;
  1007. }
  1008. }
  1009. /* allocate the number of new eb blocks we need */
  1010. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  1011. GFP_KERNEL);
  1012. if (!new_eb_bhs) {
  1013. status = -ENOMEM;
  1014. mlog_errno(status);
  1015. goto bail;
  1016. }
  1017. status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
  1018. meta_ac, new_eb_bhs);
  1019. if (status < 0) {
  1020. mlog_errno(status);
  1021. goto bail;
  1022. }
  1023. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  1024. * linked with the rest of the tree.
  1025. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  1026. *
  1027. * when we leave the loop, new_last_eb_blk will point to the
  1028. * newest leaf, and next_blkno will point to the topmost extent
  1029. * block. */
  1030. next_blkno = new_last_eb_blk = 0;
  1031. for(i = 0; i < new_blocks; i++) {
  1032. bh = new_eb_bhs[i];
  1033. eb = (struct ocfs2_extent_block *) bh->b_data;
  1034. /* ocfs2_create_new_meta_bhs() should create it right! */
  1035. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1036. eb_el = &eb->h_list;
  1037. status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
  1038. OCFS2_JOURNAL_ACCESS_CREATE);
  1039. if (status < 0) {
  1040. mlog_errno(status);
  1041. goto bail;
  1042. }
  1043. eb->h_next_leaf_blk = 0;
  1044. eb_el->l_tree_depth = cpu_to_le16(i);
  1045. eb_el->l_next_free_rec = cpu_to_le16(1);
  1046. /*
  1047. * This actually counts as an empty extent as
  1048. * c_clusters == 0
  1049. */
  1050. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  1051. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  1052. /*
  1053. * eb_el isn't always an interior node, but even leaf
  1054. * nodes want a zero'd flags and reserved field so
  1055. * this gets the whole 32 bits regardless of use.
  1056. */
  1057. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  1058. if (!eb_el->l_tree_depth)
  1059. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  1060. ocfs2_journal_dirty(handle, bh);
  1061. next_blkno = le64_to_cpu(eb->h_blkno);
  1062. }
  1063. /* This is a bit hairy. We want to update up to three blocks
  1064. * here without leaving any of them in an inconsistent state
  1065. * in case of error. We don't have to worry about
  1066. * journal_dirty erroring as it won't unless we've aborted the
  1067. * handle (in which case we would never be here) so reserving
  1068. * the write with journal_access is all we need to do. */
  1069. status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
  1070. OCFS2_JOURNAL_ACCESS_WRITE);
  1071. if (status < 0) {
  1072. mlog_errno(status);
  1073. goto bail;
  1074. }
  1075. status = ocfs2_et_root_journal_access(handle, et,
  1076. OCFS2_JOURNAL_ACCESS_WRITE);
  1077. if (status < 0) {
  1078. mlog_errno(status);
  1079. goto bail;
  1080. }
  1081. if (eb_bh) {
  1082. status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
  1083. OCFS2_JOURNAL_ACCESS_WRITE);
  1084. if (status < 0) {
  1085. mlog_errno(status);
  1086. goto bail;
  1087. }
  1088. }
  1089. /* Link the new branch into the rest of the tree (el will
  1090. * either be on the root_bh, or the extent block passed in. */
  1091. i = le16_to_cpu(el->l_next_free_rec);
  1092. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  1093. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  1094. el->l_recs[i].e_int_clusters = 0;
  1095. le16_add_cpu(&el->l_next_free_rec, 1);
  1096. /* fe needs a new last extent block pointer, as does the
  1097. * next_leaf on the previously last-extent-block. */
  1098. ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
  1099. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  1100. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  1101. ocfs2_journal_dirty(handle, *last_eb_bh);
  1102. ocfs2_journal_dirty(handle, et->et_root_bh);
  1103. if (eb_bh)
  1104. ocfs2_journal_dirty(handle, eb_bh);
  1105. /*
  1106. * Some callers want to track the rightmost leaf so pass it
  1107. * back here.
  1108. */
  1109. brelse(*last_eb_bh);
  1110. get_bh(new_eb_bhs[0]);
  1111. *last_eb_bh = new_eb_bhs[0];
  1112. status = 0;
  1113. bail:
  1114. if (new_eb_bhs) {
  1115. for (i = 0; i < new_blocks; i++)
  1116. brelse(new_eb_bhs[i]);
  1117. kfree(new_eb_bhs);
  1118. }
  1119. return status;
  1120. }
  1121. /*
  1122. * adds another level to the allocation tree.
  1123. * returns back the new extent block so you can add a branch to it
  1124. * after this call.
  1125. */
  1126. static int ocfs2_shift_tree_depth(handle_t *handle,
  1127. struct ocfs2_extent_tree *et,
  1128. struct ocfs2_alloc_context *meta_ac,
  1129. struct buffer_head **ret_new_eb_bh)
  1130. {
  1131. int status, i;
  1132. u32 new_clusters;
  1133. struct buffer_head *new_eb_bh = NULL;
  1134. struct ocfs2_extent_block *eb;
  1135. struct ocfs2_extent_list *root_el;
  1136. struct ocfs2_extent_list *eb_el;
  1137. status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
  1138. &new_eb_bh);
  1139. if (status < 0) {
  1140. mlog_errno(status);
  1141. goto bail;
  1142. }
  1143. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  1144. /* ocfs2_create_new_meta_bhs() should create it right! */
  1145. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1146. eb_el = &eb->h_list;
  1147. root_el = et->et_root_el;
  1148. status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
  1149. OCFS2_JOURNAL_ACCESS_CREATE);
  1150. if (status < 0) {
  1151. mlog_errno(status);
  1152. goto bail;
  1153. }
  1154. /* copy the root extent list data into the new extent block */
  1155. eb_el->l_tree_depth = root_el->l_tree_depth;
  1156. eb_el->l_next_free_rec = root_el->l_next_free_rec;
  1157. for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1158. eb_el->l_recs[i] = root_el->l_recs[i];
  1159. ocfs2_journal_dirty(handle, new_eb_bh);
  1160. status = ocfs2_et_root_journal_access(handle, et,
  1161. OCFS2_JOURNAL_ACCESS_WRITE);
  1162. if (status < 0) {
  1163. mlog_errno(status);
  1164. goto bail;
  1165. }
  1166. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  1167. /* update root_bh now */
  1168. le16_add_cpu(&root_el->l_tree_depth, 1);
  1169. root_el->l_recs[0].e_cpos = 0;
  1170. root_el->l_recs[0].e_blkno = eb->h_blkno;
  1171. root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  1172. for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1173. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  1174. root_el->l_next_free_rec = cpu_to_le16(1);
  1175. /* If this is our 1st tree depth shift, then last_eb_blk
  1176. * becomes the allocated extent block */
  1177. if (root_el->l_tree_depth == cpu_to_le16(1))
  1178. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  1179. ocfs2_journal_dirty(handle, et->et_root_bh);
  1180. *ret_new_eb_bh = new_eb_bh;
  1181. new_eb_bh = NULL;
  1182. status = 0;
  1183. bail:
  1184. brelse(new_eb_bh);
  1185. return status;
  1186. }
  1187. /*
  1188. * Should only be called when there is no space left in any of the
  1189. * leaf nodes. What we want to do is find the lowest tree depth
  1190. * non-leaf extent block with room for new records. There are three
  1191. * valid results of this search:
  1192. *
  1193. * 1) a lowest extent block is found, then we pass it back in
  1194. * *lowest_eb_bh and return '0'
  1195. *
  1196. * 2) the search fails to find anything, but the root_el has room. We
  1197. * pass NULL back in *lowest_eb_bh, but still return '0'
  1198. *
  1199. * 3) the search fails to find anything AND the root_el is full, in
  1200. * which case we return > 0
  1201. *
  1202. * return status < 0 indicates an error.
  1203. */
  1204. static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
  1205. struct buffer_head **target_bh)
  1206. {
  1207. int status = 0, i;
  1208. u64 blkno;
  1209. struct ocfs2_extent_block *eb;
  1210. struct ocfs2_extent_list *el;
  1211. struct buffer_head *bh = NULL;
  1212. struct buffer_head *lowest_bh = NULL;
  1213. *target_bh = NULL;
  1214. el = et->et_root_el;
  1215. while(le16_to_cpu(el->l_tree_depth) > 1) {
  1216. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1217. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1218. "Owner %llu has empty "
  1219. "extent list (next_free_rec == 0)",
  1220. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  1221. status = -EIO;
  1222. goto bail;
  1223. }
  1224. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1225. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1226. if (!blkno) {
  1227. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1228. "Owner %llu has extent "
  1229. "list where extent # %d has no physical "
  1230. "block start",
  1231. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
  1232. status = -EIO;
  1233. goto bail;
  1234. }
  1235. brelse(bh);
  1236. bh = NULL;
  1237. status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
  1238. if (status < 0) {
  1239. mlog_errno(status);
  1240. goto bail;
  1241. }
  1242. eb = (struct ocfs2_extent_block *) bh->b_data;
  1243. el = &eb->h_list;
  1244. if (le16_to_cpu(el->l_next_free_rec) <
  1245. le16_to_cpu(el->l_count)) {
  1246. brelse(lowest_bh);
  1247. lowest_bh = bh;
  1248. get_bh(lowest_bh);
  1249. }
  1250. }
  1251. /* If we didn't find one and the fe doesn't have any room,
  1252. * then return '1' */
  1253. el = et->et_root_el;
  1254. if (!lowest_bh && (el->l_next_free_rec == el->l_count))
  1255. status = 1;
  1256. *target_bh = lowest_bh;
  1257. bail:
  1258. brelse(bh);
  1259. return status;
  1260. }
  1261. /*
  1262. * Grow a b-tree so that it has more records.
  1263. *
  1264. * We might shift the tree depth in which case existing paths should
  1265. * be considered invalid.
  1266. *
  1267. * Tree depth after the grow is returned via *final_depth.
  1268. *
  1269. * *last_eb_bh will be updated by ocfs2_add_branch().
  1270. */
  1271. static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  1272. int *final_depth, struct buffer_head **last_eb_bh,
  1273. struct ocfs2_alloc_context *meta_ac)
  1274. {
  1275. int ret, shift;
  1276. struct ocfs2_extent_list *el = et->et_root_el;
  1277. int depth = le16_to_cpu(el->l_tree_depth);
  1278. struct buffer_head *bh = NULL;
  1279. BUG_ON(meta_ac == NULL);
  1280. shift = ocfs2_find_branch_target(et, &bh);
  1281. if (shift < 0) {
  1282. ret = shift;
  1283. mlog_errno(ret);
  1284. goto out;
  1285. }
  1286. /* We traveled all the way to the bottom of the allocation tree
  1287. * and didn't find room for any more extents - we need to add
  1288. * another tree level */
  1289. if (shift) {
  1290. BUG_ON(bh);
  1291. trace_ocfs2_grow_tree(
  1292. (unsigned long long)
  1293. ocfs2_metadata_cache_owner(et->et_ci),
  1294. depth);
  1295. /* ocfs2_shift_tree_depth will return us a buffer with
  1296. * the new extent block (so we can pass that to
  1297. * ocfs2_add_branch). */
  1298. ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
  1299. if (ret < 0) {
  1300. mlog_errno(ret);
  1301. goto out;
  1302. }
  1303. depth++;
  1304. if (depth == 1) {
  1305. /*
  1306. * Special case: we have room now if we shifted from
  1307. * tree_depth 0, so no more work needs to be done.
  1308. *
  1309. * We won't be calling add_branch, so pass
  1310. * back *last_eb_bh as the new leaf. At depth
  1311. * zero, it should always be null so there's
  1312. * no reason to brelse.
  1313. */
  1314. BUG_ON(*last_eb_bh);
  1315. get_bh(bh);
  1316. *last_eb_bh = bh;
  1317. goto out;
  1318. }
  1319. }
  1320. /* call ocfs2_add_branch to add the final part of the tree with
  1321. * the new data. */
  1322. ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
  1323. meta_ac);
  1324. if (ret < 0) {
  1325. mlog_errno(ret);
  1326. goto out;
  1327. }
  1328. out:
  1329. if (final_depth)
  1330. *final_depth = depth;
  1331. brelse(bh);
  1332. return ret;
  1333. }
  1334. /*
  1335. * This function will discard the rightmost extent record.
  1336. */
  1337. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  1338. {
  1339. int next_free = le16_to_cpu(el->l_next_free_rec);
  1340. int count = le16_to_cpu(el->l_count);
  1341. unsigned int num_bytes;
  1342. BUG_ON(!next_free);
  1343. /* This will cause us to go off the end of our extent list. */
  1344. BUG_ON(next_free >= count);
  1345. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  1346. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  1347. }
  1348. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  1349. struct ocfs2_extent_rec *insert_rec)
  1350. {
  1351. int i, insert_index, next_free, has_empty, num_bytes;
  1352. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  1353. struct ocfs2_extent_rec *rec;
  1354. next_free = le16_to_cpu(el->l_next_free_rec);
  1355. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  1356. BUG_ON(!next_free);
  1357. /* The tree code before us didn't allow enough room in the leaf. */
  1358. BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
  1359. /*
  1360. * The easiest way to approach this is to just remove the
  1361. * empty extent and temporarily decrement next_free.
  1362. */
  1363. if (has_empty) {
  1364. /*
  1365. * If next_free was 1 (only an empty extent), this
  1366. * loop won't execute, which is fine. We still want
  1367. * the decrement above to happen.
  1368. */
  1369. for(i = 0; i < (next_free - 1); i++)
  1370. el->l_recs[i] = el->l_recs[i+1];
  1371. next_free--;
  1372. }
  1373. /*
  1374. * Figure out what the new record index should be.
  1375. */
  1376. for(i = 0; i < next_free; i++) {
  1377. rec = &el->l_recs[i];
  1378. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  1379. break;
  1380. }
  1381. insert_index = i;
  1382. trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
  1383. has_empty, next_free,
  1384. le16_to_cpu(el->l_count));
  1385. BUG_ON(insert_index < 0);
  1386. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  1387. BUG_ON(insert_index > next_free);
  1388. /*
  1389. * No need to memmove if we're just adding to the tail.
  1390. */
  1391. if (insert_index != next_free) {
  1392. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  1393. num_bytes = next_free - insert_index;
  1394. num_bytes *= sizeof(struct ocfs2_extent_rec);
  1395. memmove(&el->l_recs[insert_index + 1],
  1396. &el->l_recs[insert_index],
  1397. num_bytes);
  1398. }
  1399. /*
  1400. * Either we had an empty extent, and need to re-increment or
  1401. * there was no empty extent on a non full rightmost leaf node,
  1402. * in which case we still need to increment.
  1403. */
  1404. next_free++;
  1405. el->l_next_free_rec = cpu_to_le16(next_free);
  1406. /*
  1407. * Make sure none of the math above just messed up our tree.
  1408. */
  1409. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  1410. el->l_recs[insert_index] = *insert_rec;
  1411. }
  1412. static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
  1413. {
  1414. int size, num_recs = le16_to_cpu(el->l_next_free_rec);
  1415. BUG_ON(num_recs == 0);
  1416. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  1417. num_recs--;
  1418. size = num_recs * sizeof(struct ocfs2_extent_rec);
  1419. memmove(&el->l_recs[0], &el->l_recs[1], size);
  1420. memset(&el->l_recs[num_recs], 0,
  1421. sizeof(struct ocfs2_extent_rec));
  1422. el->l_next_free_rec = cpu_to_le16(num_recs);
  1423. }
  1424. }
  1425. /*
  1426. * Create an empty extent record .
  1427. *
  1428. * l_next_free_rec may be updated.
  1429. *
  1430. * If an empty extent already exists do nothing.
  1431. */
  1432. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  1433. {
  1434. int next_free = le16_to_cpu(el->l_next_free_rec);
  1435. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1436. if (next_free == 0)
  1437. goto set_and_inc;
  1438. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  1439. return;
  1440. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  1441. "Asked to create an empty extent in a full list:\n"
  1442. "count = %u, tree depth = %u",
  1443. le16_to_cpu(el->l_count),
  1444. le16_to_cpu(el->l_tree_depth));
  1445. ocfs2_shift_records_right(el);
  1446. set_and_inc:
  1447. le16_add_cpu(&el->l_next_free_rec, 1);
  1448. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1449. }
  1450. /*
  1451. * For a rotation which involves two leaf nodes, the "root node" is
  1452. * the lowest level tree node which contains a path to both leafs. This
  1453. * resulting set of information can be used to form a complete "subtree"
  1454. *
  1455. * This function is passed two full paths from the dinode down to a
  1456. * pair of adjacent leaves. It's task is to figure out which path
  1457. * index contains the subtree root - this can be the root index itself
  1458. * in a worst-case rotation.
  1459. *
  1460. * The array index of the subtree root is passed back.
  1461. */
  1462. int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
  1463. struct ocfs2_path *left,
  1464. struct ocfs2_path *right)
  1465. {
  1466. int i = 0;
  1467. /*
  1468. * Check that the caller passed in two paths from the same tree.
  1469. */
  1470. BUG_ON(path_root_bh(left) != path_root_bh(right));
  1471. do {
  1472. i++;
  1473. /*
  1474. * The caller didn't pass two adjacent paths.
  1475. */
  1476. mlog_bug_on_msg(i > left->p_tree_depth,
  1477. "Owner %llu, left depth %u, right depth %u\n"
  1478. "left leaf blk %llu, right leaf blk %llu\n",
  1479. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1480. left->p_tree_depth, right->p_tree_depth,
  1481. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  1482. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  1483. } while (left->p_node[i].bh->b_blocknr ==
  1484. right->p_node[i].bh->b_blocknr);
  1485. return i - 1;
  1486. }
  1487. typedef void (path_insert_t)(void *, struct buffer_head *);
  1488. /*
  1489. * Traverse a btree path in search of cpos, starting at root_el.
  1490. *
  1491. * This code can be called with a cpos larger than the tree, in which
  1492. * case it will return the rightmost path.
  1493. */
  1494. static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
  1495. struct ocfs2_extent_list *root_el, u32 cpos,
  1496. path_insert_t *func, void *data)
  1497. {
  1498. int i, ret = 0;
  1499. u32 range;
  1500. u64 blkno;
  1501. struct buffer_head *bh = NULL;
  1502. struct ocfs2_extent_block *eb;
  1503. struct ocfs2_extent_list *el;
  1504. struct ocfs2_extent_rec *rec;
  1505. el = root_el;
  1506. while (el->l_tree_depth) {
  1507. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1508. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1509. "Owner %llu has empty extent list at "
  1510. "depth %u\n",
  1511. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1512. le16_to_cpu(el->l_tree_depth));
  1513. ret = -EROFS;
  1514. goto out;
  1515. }
  1516. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  1517. rec = &el->l_recs[i];
  1518. /*
  1519. * In the case that cpos is off the allocation
  1520. * tree, this should just wind up returning the
  1521. * rightmost record.
  1522. */
  1523. range = le32_to_cpu(rec->e_cpos) +
  1524. ocfs2_rec_clusters(el, rec);
  1525. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1526. break;
  1527. }
  1528. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1529. if (blkno == 0) {
  1530. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1531. "Owner %llu has bad blkno in extent list "
  1532. "at depth %u (index %d)\n",
  1533. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1534. le16_to_cpu(el->l_tree_depth), i);
  1535. ret = -EROFS;
  1536. goto out;
  1537. }
  1538. brelse(bh);
  1539. bh = NULL;
  1540. ret = ocfs2_read_extent_block(ci, blkno, &bh);
  1541. if (ret) {
  1542. mlog_errno(ret);
  1543. goto out;
  1544. }
  1545. eb = (struct ocfs2_extent_block *) bh->b_data;
  1546. el = &eb->h_list;
  1547. if (le16_to_cpu(el->l_next_free_rec) >
  1548. le16_to_cpu(el->l_count)) {
  1549. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1550. "Owner %llu has bad count in extent list "
  1551. "at block %llu (next free=%u, count=%u)\n",
  1552. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1553. (unsigned long long)bh->b_blocknr,
  1554. le16_to_cpu(el->l_next_free_rec),
  1555. le16_to_cpu(el->l_count));
  1556. ret = -EROFS;
  1557. goto out;
  1558. }
  1559. if (func)
  1560. func(data, bh);
  1561. }
  1562. out:
  1563. /*
  1564. * Catch any trailing bh that the loop didn't handle.
  1565. */
  1566. brelse(bh);
  1567. return ret;
  1568. }
  1569. /*
  1570. * Given an initialized path (that is, it has a valid root extent
  1571. * list), this function will traverse the btree in search of the path
  1572. * which would contain cpos.
  1573. *
  1574. * The path traveled is recorded in the path structure.
  1575. *
  1576. * Note that this will not do any comparisons on leaf node extent
  1577. * records, so it will work fine in the case that we just added a tree
  1578. * branch.
  1579. */
  1580. struct find_path_data {
  1581. int index;
  1582. struct ocfs2_path *path;
  1583. };
  1584. static void find_path_ins(void *data, struct buffer_head *bh)
  1585. {
  1586. struct find_path_data *fp = data;
  1587. get_bh(bh);
  1588. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  1589. fp->index++;
  1590. }
  1591. int ocfs2_find_path(struct ocfs2_caching_info *ci,
  1592. struct ocfs2_path *path, u32 cpos)
  1593. {
  1594. struct find_path_data data;
  1595. data.index = 1;
  1596. data.path = path;
  1597. return __ocfs2_find_path(ci, path_root_el(path), cpos,
  1598. find_path_ins, &data);
  1599. }
  1600. static void find_leaf_ins(void *data, struct buffer_head *bh)
  1601. {
  1602. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  1603. struct ocfs2_extent_list *el = &eb->h_list;
  1604. struct buffer_head **ret = data;
  1605. /* We want to retain only the leaf block. */
  1606. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1607. get_bh(bh);
  1608. *ret = bh;
  1609. }
  1610. }
  1611. /*
  1612. * Find the leaf block in the tree which would contain cpos. No
  1613. * checking of the actual leaf is done.
  1614. *
  1615. * Some paths want to call this instead of allocating a path structure
  1616. * and calling ocfs2_find_path().
  1617. *
  1618. * This function doesn't handle non btree extent lists.
  1619. */
  1620. int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
  1621. struct ocfs2_extent_list *root_el, u32 cpos,
  1622. struct buffer_head **leaf_bh)
  1623. {
  1624. int ret;
  1625. struct buffer_head *bh = NULL;
  1626. ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
  1627. if (ret) {
  1628. mlog_errno(ret);
  1629. goto out;
  1630. }
  1631. *leaf_bh = bh;
  1632. out:
  1633. return ret;
  1634. }
  1635. /*
  1636. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1637. *
  1638. * Basically, we've moved stuff around at the bottom of the tree and
  1639. * we need to fix up the extent records above the changes to reflect
  1640. * the new changes.
  1641. *
  1642. * left_rec: the record on the left.
  1643. * left_child_el: is the child list pointed to by left_rec
  1644. * right_rec: the record to the right of left_rec
  1645. * right_child_el: is the child list pointed to by right_rec
  1646. *
  1647. * By definition, this only works on interior nodes.
  1648. */
  1649. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1650. struct ocfs2_extent_list *left_child_el,
  1651. struct ocfs2_extent_rec *right_rec,
  1652. struct ocfs2_extent_list *right_child_el)
  1653. {
  1654. u32 left_clusters, right_end;
  1655. /*
  1656. * Interior nodes never have holes. Their cpos is the cpos of
  1657. * the leftmost record in their child list. Their cluster
  1658. * count covers the full theoretical range of their child list
  1659. * - the range between their cpos and the cpos of the record
  1660. * immediately to their right.
  1661. */
  1662. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1663. if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
  1664. BUG_ON(right_child_el->l_tree_depth);
  1665. BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
  1666. left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
  1667. }
  1668. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1669. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1670. /*
  1671. * Calculate the rightmost cluster count boundary before
  1672. * moving cpos - we will need to adjust clusters after
  1673. * updating e_cpos to keep the same highest cluster count.
  1674. */
  1675. right_end = le32_to_cpu(right_rec->e_cpos);
  1676. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1677. right_rec->e_cpos = left_rec->e_cpos;
  1678. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1679. right_end -= le32_to_cpu(right_rec->e_cpos);
  1680. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1681. }
  1682. /*
  1683. * Adjust the adjacent root node records involved in a
  1684. * rotation. left_el_blkno is passed in as a key so that we can easily
  1685. * find it's index in the root list.
  1686. */
  1687. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1688. struct ocfs2_extent_list *left_el,
  1689. struct ocfs2_extent_list *right_el,
  1690. u64 left_el_blkno)
  1691. {
  1692. int i;
  1693. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1694. le16_to_cpu(left_el->l_tree_depth));
  1695. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1696. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1697. break;
  1698. }
  1699. /*
  1700. * The path walking code should have never returned a root and
  1701. * two paths which are not adjacent.
  1702. */
  1703. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1704. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1705. &root_el->l_recs[i + 1], right_el);
  1706. }
  1707. /*
  1708. * We've changed a leaf block (in right_path) and need to reflect that
  1709. * change back up the subtree.
  1710. *
  1711. * This happens in multiple places:
  1712. * - When we've moved an extent record from the left path leaf to the right
  1713. * path leaf to make room for an empty extent in the left path leaf.
  1714. * - When our insert into the right path leaf is at the leftmost edge
  1715. * and requires an update of the path immediately to it's left. This
  1716. * can occur at the end of some types of rotation and appending inserts.
  1717. * - When we've adjusted the last extent record in the left path leaf and the
  1718. * 1st extent record in the right path leaf during cross extent block merge.
  1719. */
  1720. static void ocfs2_complete_edge_insert(handle_t *handle,
  1721. struct ocfs2_path *left_path,
  1722. struct ocfs2_path *right_path,
  1723. int subtree_index)
  1724. {
  1725. int i, idx;
  1726. struct ocfs2_extent_list *el, *left_el, *right_el;
  1727. struct ocfs2_extent_rec *left_rec, *right_rec;
  1728. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1729. /*
  1730. * Update the counts and position values within all the
  1731. * interior nodes to reflect the leaf rotation we just did.
  1732. *
  1733. * The root node is handled below the loop.
  1734. *
  1735. * We begin the loop with right_el and left_el pointing to the
  1736. * leaf lists and work our way up.
  1737. *
  1738. * NOTE: within this loop, left_el and right_el always refer
  1739. * to the *child* lists.
  1740. */
  1741. left_el = path_leaf_el(left_path);
  1742. right_el = path_leaf_el(right_path);
  1743. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1744. trace_ocfs2_complete_edge_insert(i);
  1745. /*
  1746. * One nice property of knowing that all of these
  1747. * nodes are below the root is that we only deal with
  1748. * the leftmost right node record and the rightmost
  1749. * left node record.
  1750. */
  1751. el = left_path->p_node[i].el;
  1752. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1753. left_rec = &el->l_recs[idx];
  1754. el = right_path->p_node[i].el;
  1755. right_rec = &el->l_recs[0];
  1756. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1757. right_el);
  1758. ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1759. ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1760. /*
  1761. * Setup our list pointers now so that the current
  1762. * parents become children in the next iteration.
  1763. */
  1764. left_el = left_path->p_node[i].el;
  1765. right_el = right_path->p_node[i].el;
  1766. }
  1767. /*
  1768. * At the root node, adjust the two adjacent records which
  1769. * begin our path to the leaves.
  1770. */
  1771. el = left_path->p_node[subtree_index].el;
  1772. left_el = left_path->p_node[subtree_index + 1].el;
  1773. right_el = right_path->p_node[subtree_index + 1].el;
  1774. ocfs2_adjust_root_records(el, left_el, right_el,
  1775. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1776. root_bh = left_path->p_node[subtree_index].bh;
  1777. ocfs2_journal_dirty(handle, root_bh);
  1778. }
  1779. static int ocfs2_rotate_subtree_right(handle_t *handle,
  1780. struct ocfs2_extent_tree *et,
  1781. struct ocfs2_path *left_path,
  1782. struct ocfs2_path *right_path,
  1783. int subtree_index)
  1784. {
  1785. int ret, i;
  1786. struct buffer_head *right_leaf_bh;
  1787. struct buffer_head *left_leaf_bh = NULL;
  1788. struct buffer_head *root_bh;
  1789. struct ocfs2_extent_list *right_el, *left_el;
  1790. struct ocfs2_extent_rec move_rec;
  1791. left_leaf_bh = path_leaf_bh(left_path);
  1792. left_el = path_leaf_el(left_path);
  1793. if (left_el->l_next_free_rec != left_el->l_count) {
  1794. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1795. "Inode %llu has non-full interior leaf node %llu"
  1796. "(next free = %u)",
  1797. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1798. (unsigned long long)left_leaf_bh->b_blocknr,
  1799. le16_to_cpu(left_el->l_next_free_rec));
  1800. return -EROFS;
  1801. }
  1802. /*
  1803. * This extent block may already have an empty record, so we
  1804. * return early if so.
  1805. */
  1806. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1807. return 0;
  1808. root_bh = left_path->p_node[subtree_index].bh;
  1809. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1810. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  1811. subtree_index);
  1812. if (ret) {
  1813. mlog_errno(ret);
  1814. goto out;
  1815. }
  1816. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1817. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1818. right_path, i);
  1819. if (ret) {
  1820. mlog_errno(ret);
  1821. goto out;
  1822. }
  1823. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1824. left_path, i);
  1825. if (ret) {
  1826. mlog_errno(ret);
  1827. goto out;
  1828. }
  1829. }
  1830. right_leaf_bh = path_leaf_bh(right_path);
  1831. right_el = path_leaf_el(right_path);
  1832. /* This is a code error, not a disk corruption. */
  1833. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1834. "because rightmost leaf block %llu is empty\n",
  1835. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1836. (unsigned long long)right_leaf_bh->b_blocknr);
  1837. ocfs2_create_empty_extent(right_el);
  1838. ocfs2_journal_dirty(handle, right_leaf_bh);
  1839. /* Do the copy now. */
  1840. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1841. move_rec = left_el->l_recs[i];
  1842. right_el->l_recs[0] = move_rec;
  1843. /*
  1844. * Clear out the record we just copied and shift everything
  1845. * over, leaving an empty extent in the left leaf.
  1846. *
  1847. * We temporarily subtract from next_free_rec so that the
  1848. * shift will lose the tail record (which is now defunct).
  1849. */
  1850. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1851. ocfs2_shift_records_right(left_el);
  1852. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1853. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1854. ocfs2_journal_dirty(handle, left_leaf_bh);
  1855. ocfs2_complete_edge_insert(handle, left_path, right_path,
  1856. subtree_index);
  1857. out:
  1858. return ret;
  1859. }
  1860. /*
  1861. * Given a full path, determine what cpos value would return us a path
  1862. * containing the leaf immediately to the left of the current one.
  1863. *
  1864. * Will return zero if the path passed in is already the leftmost path.
  1865. */
  1866. int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1867. struct ocfs2_path *path, u32 *cpos)
  1868. {
  1869. int i, j, ret = 0;
  1870. u64 blkno;
  1871. struct ocfs2_extent_list *el;
  1872. BUG_ON(path->p_tree_depth == 0);
  1873. *cpos = 0;
  1874. blkno = path_leaf_bh(path)->b_blocknr;
  1875. /* Start at the tree node just above the leaf and work our way up. */
  1876. i = path->p_tree_depth - 1;
  1877. while (i >= 0) {
  1878. el = path->p_node[i].el;
  1879. /*
  1880. * Find the extent record just before the one in our
  1881. * path.
  1882. */
  1883. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1884. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1885. if (j == 0) {
  1886. if (i == 0) {
  1887. /*
  1888. * We've determined that the
  1889. * path specified is already
  1890. * the leftmost one - return a
  1891. * cpos of zero.
  1892. */
  1893. goto out;
  1894. }
  1895. /*
  1896. * The leftmost record points to our
  1897. * leaf - we need to travel up the
  1898. * tree one level.
  1899. */
  1900. goto next_node;
  1901. }
  1902. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1903. *cpos = *cpos + ocfs2_rec_clusters(el,
  1904. &el->l_recs[j - 1]);
  1905. *cpos = *cpos - 1;
  1906. goto out;
  1907. }
  1908. }
  1909. /*
  1910. * If we got here, we never found a valid node where
  1911. * the tree indicated one should be.
  1912. */
  1913. ocfs2_error(sb,
  1914. "Invalid extent tree at extent block %llu\n",
  1915. (unsigned long long)blkno);
  1916. ret = -EROFS;
  1917. goto out;
  1918. next_node:
  1919. blkno = path->p_node[i].bh->b_blocknr;
  1920. i--;
  1921. }
  1922. out:
  1923. return ret;
  1924. }
  1925. /*
  1926. * Extend the transaction by enough credits to complete the rotation,
  1927. * and still leave at least the original number of credits allocated
  1928. * to this transaction.
  1929. */
  1930. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1931. int op_credits,
  1932. struct ocfs2_path *path)
  1933. {
  1934. int ret = 0;
  1935. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
  1936. if (handle->h_buffer_credits < credits)
  1937. ret = ocfs2_extend_trans(handle,
  1938. credits - handle->h_buffer_credits);
  1939. return ret;
  1940. }
  1941. /*
  1942. * Trap the case where we're inserting into the theoretical range past
  1943. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1944. * whose cpos is less than ours into the right leaf.
  1945. *
  1946. * It's only necessary to look at the rightmost record of the left
  1947. * leaf because the logic that calls us should ensure that the
  1948. * theoretical ranges in the path components above the leaves are
  1949. * correct.
  1950. */
  1951. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1952. u32 insert_cpos)
  1953. {
  1954. struct ocfs2_extent_list *left_el;
  1955. struct ocfs2_extent_rec *rec;
  1956. int next_free;
  1957. left_el = path_leaf_el(left_path);
  1958. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1959. rec = &left_el->l_recs[next_free - 1];
  1960. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1961. return 1;
  1962. return 0;
  1963. }
  1964. static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  1965. {
  1966. int next_free = le16_to_cpu(el->l_next_free_rec);
  1967. unsigned int range;
  1968. struct ocfs2_extent_rec *rec;
  1969. if (next_free == 0)
  1970. return 0;
  1971. rec = &el->l_recs[0];
  1972. if (ocfs2_is_empty_extent(rec)) {
  1973. /* Empty list. */
  1974. if (next_free == 1)
  1975. return 0;
  1976. rec = &el->l_recs[1];
  1977. }
  1978. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1979. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1980. return 1;
  1981. return 0;
  1982. }
  1983. /*
  1984. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1985. *
  1986. * The path to the rightmost leaf should be passed in.
  1987. *
  1988. * The array is assumed to be large enough to hold an entire path (tree depth).
  1989. *
  1990. * Upon successful return from this function:
  1991. *
  1992. * - The 'right_path' array will contain a path to the leaf block
  1993. * whose range contains e_cpos.
  1994. * - That leaf block will have a single empty extent in list index 0.
  1995. * - In the case that the rotation requires a post-insert update,
  1996. * *ret_left_path will contain a valid path which can be passed to
  1997. * ocfs2_insert_path().
  1998. */
  1999. static int ocfs2_rotate_tree_right(handle_t *handle,
  2000. struct ocfs2_extent_tree *et,
  2001. enum ocfs2_split_type split,
  2002. u32 insert_cpos,
  2003. struct ocfs2_path *right_path,
  2004. struct ocfs2_path **ret_left_path)
  2005. {
  2006. int ret, start, orig_credits = handle->h_buffer_credits;
  2007. u32 cpos;
  2008. struct ocfs2_path *left_path = NULL;
  2009. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2010. *ret_left_path = NULL;
  2011. left_path = ocfs2_new_path_from_path(right_path);
  2012. if (!left_path) {
  2013. ret = -ENOMEM;
  2014. mlog_errno(ret);
  2015. goto out;
  2016. }
  2017. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2018. if (ret) {
  2019. mlog_errno(ret);
  2020. goto out;
  2021. }
  2022. trace_ocfs2_rotate_tree_right(
  2023. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2024. insert_cpos, cpos);
  2025. /*
  2026. * What we want to do here is:
  2027. *
  2028. * 1) Start with the rightmost path.
  2029. *
  2030. * 2) Determine a path to the leaf block directly to the left
  2031. * of that leaf.
  2032. *
  2033. * 3) Determine the 'subtree root' - the lowest level tree node
  2034. * which contains a path to both leaves.
  2035. *
  2036. * 4) Rotate the subtree.
  2037. *
  2038. * 5) Find the next subtree by considering the left path to be
  2039. * the new right path.
  2040. *
  2041. * The check at the top of this while loop also accepts
  2042. * insert_cpos == cpos because cpos is only a _theoretical_
  2043. * value to get us the left path - insert_cpos might very well
  2044. * be filling that hole.
  2045. *
  2046. * Stop at a cpos of '0' because we either started at the
  2047. * leftmost branch (i.e., a tree with one branch and a
  2048. * rotation inside of it), or we've gone as far as we can in
  2049. * rotating subtrees.
  2050. */
  2051. while (cpos && insert_cpos <= cpos) {
  2052. trace_ocfs2_rotate_tree_right(
  2053. (unsigned long long)
  2054. ocfs2_metadata_cache_owner(et->et_ci),
  2055. insert_cpos, cpos);
  2056. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2057. if (ret) {
  2058. mlog_errno(ret);
  2059. goto out;
  2060. }
  2061. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  2062. path_leaf_bh(right_path),
  2063. "Owner %llu: error during insert of %u "
  2064. "(left path cpos %u) results in two identical "
  2065. "paths ending at %llu\n",
  2066. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2067. insert_cpos, cpos,
  2068. (unsigned long long)
  2069. path_leaf_bh(left_path)->b_blocknr);
  2070. if (split == SPLIT_NONE &&
  2071. ocfs2_rotate_requires_path_adjustment(left_path,
  2072. insert_cpos)) {
  2073. /*
  2074. * We've rotated the tree as much as we
  2075. * should. The rest is up to
  2076. * ocfs2_insert_path() to complete, after the
  2077. * record insertion. We indicate this
  2078. * situation by returning the left path.
  2079. *
  2080. * The reason we don't adjust the records here
  2081. * before the record insert is that an error
  2082. * later might break the rule where a parent
  2083. * record e_cpos will reflect the actual
  2084. * e_cpos of the 1st nonempty record of the
  2085. * child list.
  2086. */
  2087. *ret_left_path = left_path;
  2088. goto out_ret_path;
  2089. }
  2090. start = ocfs2_find_subtree_root(et, left_path, right_path);
  2091. trace_ocfs2_rotate_subtree(start,
  2092. (unsigned long long)
  2093. right_path->p_node[start].bh->b_blocknr,
  2094. right_path->p_tree_depth);
  2095. ret = ocfs2_extend_rotate_transaction(handle, start,
  2096. orig_credits, right_path);
  2097. if (ret) {
  2098. mlog_errno(ret);
  2099. goto out;
  2100. }
  2101. ret = ocfs2_rotate_subtree_right(handle, et, left_path,
  2102. right_path, start);
  2103. if (ret) {
  2104. mlog_errno(ret);
  2105. goto out;
  2106. }
  2107. if (split != SPLIT_NONE &&
  2108. ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
  2109. insert_cpos)) {
  2110. /*
  2111. * A rotate moves the rightmost left leaf
  2112. * record over to the leftmost right leaf
  2113. * slot. If we're doing an extent split
  2114. * instead of a real insert, then we have to
  2115. * check that the extent to be split wasn't
  2116. * just moved over. If it was, then we can
  2117. * exit here, passing left_path back -
  2118. * ocfs2_split_extent() is smart enough to
  2119. * search both leaves.
  2120. */
  2121. *ret_left_path = left_path;
  2122. goto out_ret_path;
  2123. }
  2124. /*
  2125. * There is no need to re-read the next right path
  2126. * as we know that it'll be our current left
  2127. * path. Optimize by copying values instead.
  2128. */
  2129. ocfs2_mv_path(right_path, left_path);
  2130. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2131. if (ret) {
  2132. mlog_errno(ret);
  2133. goto out;
  2134. }
  2135. }
  2136. out:
  2137. ocfs2_free_path(left_path);
  2138. out_ret_path:
  2139. return ret;
  2140. }
  2141. static int ocfs2_update_edge_lengths(handle_t *handle,
  2142. struct ocfs2_extent_tree *et,
  2143. int subtree_index, struct ocfs2_path *path)
  2144. {
  2145. int i, idx, ret;
  2146. struct ocfs2_extent_rec *rec;
  2147. struct ocfs2_extent_list *el;
  2148. struct ocfs2_extent_block *eb;
  2149. u32 range;
  2150. /*
  2151. * In normal tree rotation process, we will never touch the
  2152. * tree branch above subtree_index and ocfs2_extend_rotate_transaction
  2153. * doesn't reserve the credits for them either.
  2154. *
  2155. * But we do have a special case here which will update the rightmost
  2156. * records for all the bh in the path.
  2157. * So we have to allocate extra credits and access them.
  2158. */
  2159. ret = ocfs2_extend_trans(handle, subtree_index);
  2160. if (ret) {
  2161. mlog_errno(ret);
  2162. goto out;
  2163. }
  2164. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2165. if (ret) {
  2166. mlog_errno(ret);
  2167. goto out;
  2168. }
  2169. /* Path should always be rightmost. */
  2170. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2171. BUG_ON(eb->h_next_leaf_blk != 0ULL);
  2172. el = &eb->h_list;
  2173. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  2174. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2175. rec = &el->l_recs[idx];
  2176. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  2177. for (i = 0; i < path->p_tree_depth; i++) {
  2178. el = path->p_node[i].el;
  2179. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2180. rec = &el->l_recs[idx];
  2181. rec->e_int_clusters = cpu_to_le32(range);
  2182. le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
  2183. ocfs2_journal_dirty(handle, path->p_node[i].bh);
  2184. }
  2185. out:
  2186. return ret;
  2187. }
  2188. static void ocfs2_unlink_path(handle_t *handle,
  2189. struct ocfs2_extent_tree *et,
  2190. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2191. struct ocfs2_path *path, int unlink_start)
  2192. {
  2193. int ret, i;
  2194. struct ocfs2_extent_block *eb;
  2195. struct ocfs2_extent_list *el;
  2196. struct buffer_head *bh;
  2197. for(i = unlink_start; i < path_num_items(path); i++) {
  2198. bh = path->p_node[i].bh;
  2199. eb = (struct ocfs2_extent_block *)bh->b_data;
  2200. /*
  2201. * Not all nodes might have had their final count
  2202. * decremented by the caller - handle this here.
  2203. */
  2204. el = &eb->h_list;
  2205. if (le16_to_cpu(el->l_next_free_rec) > 1) {
  2206. mlog(ML_ERROR,
  2207. "Inode %llu, attempted to remove extent block "
  2208. "%llu with %u records\n",
  2209. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2210. (unsigned long long)le64_to_cpu(eb->h_blkno),
  2211. le16_to_cpu(el->l_next_free_rec));
  2212. ocfs2_journal_dirty(handle, bh);
  2213. ocfs2_remove_from_cache(et->et_ci, bh);
  2214. continue;
  2215. }
  2216. el->l_next_free_rec = 0;
  2217. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2218. ocfs2_journal_dirty(handle, bh);
  2219. ret = ocfs2_cache_extent_block_free(dealloc, eb);
  2220. if (ret)
  2221. mlog_errno(ret);
  2222. ocfs2_remove_from_cache(et->et_ci, bh);
  2223. }
  2224. }
  2225. static void ocfs2_unlink_subtree(handle_t *handle,
  2226. struct ocfs2_extent_tree *et,
  2227. struct ocfs2_path *left_path,
  2228. struct ocfs2_path *right_path,
  2229. int subtree_index,
  2230. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2231. {
  2232. int i;
  2233. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  2234. struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
  2235. struct ocfs2_extent_list *el;
  2236. struct ocfs2_extent_block *eb;
  2237. el = path_leaf_el(left_path);
  2238. eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
  2239. for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  2240. if (root_el->l_recs[i].e_blkno == eb->h_blkno)
  2241. break;
  2242. BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
  2243. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  2244. le16_add_cpu(&root_el->l_next_free_rec, -1);
  2245. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2246. eb->h_next_leaf_blk = 0;
  2247. ocfs2_journal_dirty(handle, root_bh);
  2248. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2249. ocfs2_unlink_path(handle, et, dealloc, right_path,
  2250. subtree_index + 1);
  2251. }
  2252. static int ocfs2_rotate_subtree_left(handle_t *handle,
  2253. struct ocfs2_extent_tree *et,
  2254. struct ocfs2_path *left_path,
  2255. struct ocfs2_path *right_path,
  2256. int subtree_index,
  2257. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2258. int *deleted)
  2259. {
  2260. int ret, i, del_right_subtree = 0, right_has_empty = 0;
  2261. struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
  2262. struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
  2263. struct ocfs2_extent_block *eb;
  2264. *deleted = 0;
  2265. right_leaf_el = path_leaf_el(right_path);
  2266. left_leaf_el = path_leaf_el(left_path);
  2267. root_bh = left_path->p_node[subtree_index].bh;
  2268. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2269. if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
  2270. return 0;
  2271. eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
  2272. if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
  2273. /*
  2274. * It's legal for us to proceed if the right leaf is
  2275. * the rightmost one and it has an empty extent. There
  2276. * are two cases to handle - whether the leaf will be
  2277. * empty after removal or not. If the leaf isn't empty
  2278. * then just remove the empty extent up front. The
  2279. * next block will handle empty leaves by flagging
  2280. * them for unlink.
  2281. *
  2282. * Non rightmost leaves will throw -EAGAIN and the
  2283. * caller can manually move the subtree and retry.
  2284. */
  2285. if (eb->h_next_leaf_blk != 0ULL)
  2286. return -EAGAIN;
  2287. if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
  2288. ret = ocfs2_journal_access_eb(handle, et->et_ci,
  2289. path_leaf_bh(right_path),
  2290. OCFS2_JOURNAL_ACCESS_WRITE);
  2291. if (ret) {
  2292. mlog_errno(ret);
  2293. goto out;
  2294. }
  2295. ocfs2_remove_empty_extent(right_leaf_el);
  2296. } else
  2297. right_has_empty = 1;
  2298. }
  2299. if (eb->h_next_leaf_blk == 0ULL &&
  2300. le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
  2301. /*
  2302. * We have to update i_last_eb_blk during the meta
  2303. * data delete.
  2304. */
  2305. ret = ocfs2_et_root_journal_access(handle, et,
  2306. OCFS2_JOURNAL_ACCESS_WRITE);
  2307. if (ret) {
  2308. mlog_errno(ret);
  2309. goto out;
  2310. }
  2311. del_right_subtree = 1;
  2312. }
  2313. /*
  2314. * Getting here with an empty extent in the right path implies
  2315. * that it's the rightmost path and will be deleted.
  2316. */
  2317. BUG_ON(right_has_empty && !del_right_subtree);
  2318. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2319. subtree_index);
  2320. if (ret) {
  2321. mlog_errno(ret);
  2322. goto out;
  2323. }
  2324. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  2325. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2326. right_path, i);
  2327. if (ret) {
  2328. mlog_errno(ret);
  2329. goto out;
  2330. }
  2331. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2332. left_path, i);
  2333. if (ret) {
  2334. mlog_errno(ret);
  2335. goto out;
  2336. }
  2337. }
  2338. if (!right_has_empty) {
  2339. /*
  2340. * Only do this if we're moving a real
  2341. * record. Otherwise, the action is delayed until
  2342. * after removal of the right path in which case we
  2343. * can do a simple shift to remove the empty extent.
  2344. */
  2345. ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
  2346. memset(&right_leaf_el->l_recs[0], 0,
  2347. sizeof(struct ocfs2_extent_rec));
  2348. }
  2349. if (eb->h_next_leaf_blk == 0ULL) {
  2350. /*
  2351. * Move recs over to get rid of empty extent, decrease
  2352. * next_free. This is allowed to remove the last
  2353. * extent in our leaf (setting l_next_free_rec to
  2354. * zero) - the delete code below won't care.
  2355. */
  2356. ocfs2_remove_empty_extent(right_leaf_el);
  2357. }
  2358. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2359. ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2360. if (del_right_subtree) {
  2361. ocfs2_unlink_subtree(handle, et, left_path, right_path,
  2362. subtree_index, dealloc);
  2363. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2364. left_path);
  2365. if (ret) {
  2366. mlog_errno(ret);
  2367. goto out;
  2368. }
  2369. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2370. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2371. /*
  2372. * Removal of the extent in the left leaf was skipped
  2373. * above so we could delete the right path
  2374. * 1st.
  2375. */
  2376. if (right_has_empty)
  2377. ocfs2_remove_empty_extent(left_leaf_el);
  2378. ocfs2_journal_dirty(handle, et_root_bh);
  2379. *deleted = 1;
  2380. } else
  2381. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2382. subtree_index);
  2383. out:
  2384. return ret;
  2385. }
  2386. /*
  2387. * Given a full path, determine what cpos value would return us a path
  2388. * containing the leaf immediately to the right of the current one.
  2389. *
  2390. * Will return zero if the path passed in is already the rightmost path.
  2391. *
  2392. * This looks similar, but is subtly different to
  2393. * ocfs2_find_cpos_for_left_leaf().
  2394. */
  2395. int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  2396. struct ocfs2_path *path, u32 *cpos)
  2397. {
  2398. int i, j, ret = 0;
  2399. u64 blkno;
  2400. struct ocfs2_extent_list *el;
  2401. *cpos = 0;
  2402. if (path->p_tree_depth == 0)
  2403. return 0;
  2404. blkno = path_leaf_bh(path)->b_blocknr;
  2405. /* Start at the tree node just above the leaf and work our way up. */
  2406. i = path->p_tree_depth - 1;
  2407. while (i >= 0) {
  2408. int next_free;
  2409. el = path->p_node[i].el;
  2410. /*
  2411. * Find the extent record just after the one in our
  2412. * path.
  2413. */
  2414. next_free = le16_to_cpu(el->l_next_free_rec);
  2415. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  2416. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  2417. if (j == (next_free - 1)) {
  2418. if (i == 0) {
  2419. /*
  2420. * We've determined that the
  2421. * path specified is already
  2422. * the rightmost one - return a
  2423. * cpos of zero.
  2424. */
  2425. goto out;
  2426. }
  2427. /*
  2428. * The rightmost record points to our
  2429. * leaf - we need to travel up the
  2430. * tree one level.
  2431. */
  2432. goto next_node;
  2433. }
  2434. *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
  2435. goto out;
  2436. }
  2437. }
  2438. /*
  2439. * If we got here, we never found a valid node where
  2440. * the tree indicated one should be.
  2441. */
  2442. ocfs2_error(sb,
  2443. "Invalid extent tree at extent block %llu\n",
  2444. (unsigned long long)blkno);
  2445. ret = -EROFS;
  2446. goto out;
  2447. next_node:
  2448. blkno = path->p_node[i].bh->b_blocknr;
  2449. i--;
  2450. }
  2451. out:
  2452. return ret;
  2453. }
  2454. static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
  2455. struct ocfs2_extent_tree *et,
  2456. struct ocfs2_path *path)
  2457. {
  2458. int ret;
  2459. struct buffer_head *bh = path_leaf_bh(path);
  2460. struct ocfs2_extent_list *el = path_leaf_el(path);
  2461. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2462. return 0;
  2463. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  2464. path_num_items(path) - 1);
  2465. if (ret) {
  2466. mlog_errno(ret);
  2467. goto out;
  2468. }
  2469. ocfs2_remove_empty_extent(el);
  2470. ocfs2_journal_dirty(handle, bh);
  2471. out:
  2472. return ret;
  2473. }
  2474. static int __ocfs2_rotate_tree_left(handle_t *handle,
  2475. struct ocfs2_extent_tree *et,
  2476. int orig_credits,
  2477. struct ocfs2_path *path,
  2478. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2479. struct ocfs2_path **empty_extent_path)
  2480. {
  2481. int ret, subtree_root, deleted;
  2482. u32 right_cpos;
  2483. struct ocfs2_path *left_path = NULL;
  2484. struct ocfs2_path *right_path = NULL;
  2485. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2486. if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])))
  2487. return 0;
  2488. *empty_extent_path = NULL;
  2489. ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  2490. if (ret) {
  2491. mlog_errno(ret);
  2492. goto out;
  2493. }
  2494. left_path = ocfs2_new_path_from_path(path);
  2495. if (!left_path) {
  2496. ret = -ENOMEM;
  2497. mlog_errno(ret);
  2498. goto out;
  2499. }
  2500. ocfs2_cp_path(left_path, path);
  2501. right_path = ocfs2_new_path_from_path(path);
  2502. if (!right_path) {
  2503. ret = -ENOMEM;
  2504. mlog_errno(ret);
  2505. goto out;
  2506. }
  2507. while (right_cpos) {
  2508. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2509. if (ret) {
  2510. mlog_errno(ret);
  2511. goto out;
  2512. }
  2513. subtree_root = ocfs2_find_subtree_root(et, left_path,
  2514. right_path);
  2515. trace_ocfs2_rotate_subtree(subtree_root,
  2516. (unsigned long long)
  2517. right_path->p_node[subtree_root].bh->b_blocknr,
  2518. right_path->p_tree_depth);
  2519. ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
  2520. orig_credits, left_path);
  2521. if (ret) {
  2522. mlog_errno(ret);
  2523. goto out;
  2524. }
  2525. /*
  2526. * Caller might still want to make changes to the
  2527. * tree root, so re-add it to the journal here.
  2528. */
  2529. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2530. left_path, 0);
  2531. if (ret) {
  2532. mlog_errno(ret);
  2533. goto out;
  2534. }
  2535. ret = ocfs2_rotate_subtree_left(handle, et, left_path,
  2536. right_path, subtree_root,
  2537. dealloc, &deleted);
  2538. if (ret == -EAGAIN) {
  2539. /*
  2540. * The rotation has to temporarily stop due to
  2541. * the right subtree having an empty
  2542. * extent. Pass it back to the caller for a
  2543. * fixup.
  2544. */
  2545. *empty_extent_path = right_path;
  2546. right_path = NULL;
  2547. goto out;
  2548. }
  2549. if (ret) {
  2550. mlog_errno(ret);
  2551. goto out;
  2552. }
  2553. /*
  2554. * The subtree rotate might have removed records on
  2555. * the rightmost edge. If so, then rotation is
  2556. * complete.
  2557. */
  2558. if (deleted)
  2559. break;
  2560. ocfs2_mv_path(left_path, right_path);
  2561. ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
  2562. &right_cpos);
  2563. if (ret) {
  2564. mlog_errno(ret);
  2565. goto out;
  2566. }
  2567. }
  2568. out:
  2569. ocfs2_free_path(right_path);
  2570. ocfs2_free_path(left_path);
  2571. return ret;
  2572. }
  2573. static int ocfs2_remove_rightmost_path(handle_t *handle,
  2574. struct ocfs2_extent_tree *et,
  2575. struct ocfs2_path *path,
  2576. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2577. {
  2578. int ret, subtree_index;
  2579. u32 cpos;
  2580. struct ocfs2_path *left_path = NULL;
  2581. struct ocfs2_extent_block *eb;
  2582. struct ocfs2_extent_list *el;
  2583. ret = ocfs2_et_sanity_check(et);
  2584. if (ret)
  2585. goto out;
  2586. /*
  2587. * There's two ways we handle this depending on
  2588. * whether path is the only existing one.
  2589. */
  2590. ret = ocfs2_extend_rotate_transaction(handle, 0,
  2591. handle->h_buffer_credits,
  2592. path);
  2593. if (ret) {
  2594. mlog_errno(ret);
  2595. goto out;
  2596. }
  2597. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2598. if (ret) {
  2599. mlog_errno(ret);
  2600. goto out;
  2601. }
  2602. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2603. path, &cpos);
  2604. if (ret) {
  2605. mlog_errno(ret);
  2606. goto out;
  2607. }
  2608. if (cpos) {
  2609. /*
  2610. * We have a path to the left of this one - it needs
  2611. * an update too.
  2612. */
  2613. left_path = ocfs2_new_path_from_path(path);
  2614. if (!left_path) {
  2615. ret = -ENOMEM;
  2616. mlog_errno(ret);
  2617. goto out;
  2618. }
  2619. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2620. if (ret) {
  2621. mlog_errno(ret);
  2622. goto out;
  2623. }
  2624. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  2625. if (ret) {
  2626. mlog_errno(ret);
  2627. goto out;
  2628. }
  2629. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  2630. ocfs2_unlink_subtree(handle, et, left_path, path,
  2631. subtree_index, dealloc);
  2632. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2633. left_path);
  2634. if (ret) {
  2635. mlog_errno(ret);
  2636. goto out;
  2637. }
  2638. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2639. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2640. } else {
  2641. /*
  2642. * 'path' is also the leftmost path which
  2643. * means it must be the only one. This gets
  2644. * handled differently because we want to
  2645. * revert the root back to having extents
  2646. * in-line.
  2647. */
  2648. ocfs2_unlink_path(handle, et, dealloc, path, 1);
  2649. el = et->et_root_el;
  2650. el->l_tree_depth = 0;
  2651. el->l_next_free_rec = 0;
  2652. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2653. ocfs2_et_set_last_eb_blk(et, 0);
  2654. }
  2655. ocfs2_journal_dirty(handle, path_root_bh(path));
  2656. out:
  2657. ocfs2_free_path(left_path);
  2658. return ret;
  2659. }
  2660. /*
  2661. * Left rotation of btree records.
  2662. *
  2663. * In many ways, this is (unsurprisingly) the opposite of right
  2664. * rotation. We start at some non-rightmost path containing an empty
  2665. * extent in the leaf block. The code works its way to the rightmost
  2666. * path by rotating records to the left in every subtree.
  2667. *
  2668. * This is used by any code which reduces the number of extent records
  2669. * in a leaf. After removal, an empty record should be placed in the
  2670. * leftmost list position.
  2671. *
  2672. * This won't handle a length update of the rightmost path records if
  2673. * the rightmost tree leaf record is removed so the caller is
  2674. * responsible for detecting and correcting that.
  2675. */
  2676. static int ocfs2_rotate_tree_left(handle_t *handle,
  2677. struct ocfs2_extent_tree *et,
  2678. struct ocfs2_path *path,
  2679. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2680. {
  2681. int ret, orig_credits = handle->h_buffer_credits;
  2682. struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
  2683. struct ocfs2_extent_block *eb;
  2684. struct ocfs2_extent_list *el;
  2685. el = path_leaf_el(path);
  2686. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2687. return 0;
  2688. if (path->p_tree_depth == 0) {
  2689. rightmost_no_delete:
  2690. /*
  2691. * Inline extents. This is trivially handled, so do
  2692. * it up front.
  2693. */
  2694. ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
  2695. if (ret)
  2696. mlog_errno(ret);
  2697. goto out;
  2698. }
  2699. /*
  2700. * Handle rightmost branch now. There's several cases:
  2701. * 1) simple rotation leaving records in there. That's trivial.
  2702. * 2) rotation requiring a branch delete - there's no more
  2703. * records left. Two cases of this:
  2704. * a) There are branches to the left.
  2705. * b) This is also the leftmost (the only) branch.
  2706. *
  2707. * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
  2708. * 2a) we need the left branch so that we can update it with the unlink
  2709. * 2b) we need to bring the root back to inline extents.
  2710. */
  2711. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2712. el = &eb->h_list;
  2713. if (eb->h_next_leaf_blk == 0) {
  2714. /*
  2715. * This gets a bit tricky if we're going to delete the
  2716. * rightmost path. Get the other cases out of the way
  2717. * 1st.
  2718. */
  2719. if (le16_to_cpu(el->l_next_free_rec) > 1)
  2720. goto rightmost_no_delete;
  2721. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  2722. ret = -EIO;
  2723. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  2724. "Owner %llu has empty extent block at %llu",
  2725. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2726. (unsigned long long)le64_to_cpu(eb->h_blkno));
  2727. goto out;
  2728. }
  2729. /*
  2730. * XXX: The caller can not trust "path" any more after
  2731. * this as it will have been deleted. What do we do?
  2732. *
  2733. * In theory the rotate-for-merge code will never get
  2734. * here because it'll always ask for a rotate in a
  2735. * nonempty list.
  2736. */
  2737. ret = ocfs2_remove_rightmost_path(handle, et, path,
  2738. dealloc);
  2739. if (ret)
  2740. mlog_errno(ret);
  2741. goto out;
  2742. }
  2743. /*
  2744. * Now we can loop, remembering the path we get from -EAGAIN
  2745. * and restarting from there.
  2746. */
  2747. try_rotate:
  2748. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
  2749. dealloc, &restart_path);
  2750. if (ret && ret != -EAGAIN) {
  2751. mlog_errno(ret);
  2752. goto out;
  2753. }
  2754. while (ret == -EAGAIN) {
  2755. tmp_path = restart_path;
  2756. restart_path = NULL;
  2757. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
  2758. tmp_path, dealloc,
  2759. &restart_path);
  2760. if (ret && ret != -EAGAIN) {
  2761. mlog_errno(ret);
  2762. goto out;
  2763. }
  2764. ocfs2_free_path(tmp_path);
  2765. tmp_path = NULL;
  2766. if (ret == 0)
  2767. goto try_rotate;
  2768. }
  2769. out:
  2770. ocfs2_free_path(tmp_path);
  2771. ocfs2_free_path(restart_path);
  2772. return ret;
  2773. }
  2774. static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
  2775. int index)
  2776. {
  2777. struct ocfs2_extent_rec *rec = &el->l_recs[index];
  2778. unsigned int size;
  2779. if (rec->e_leaf_clusters == 0) {
  2780. /*
  2781. * We consumed all of the merged-from record. An empty
  2782. * extent cannot exist anywhere but the 1st array
  2783. * position, so move things over if the merged-from
  2784. * record doesn't occupy that position.
  2785. *
  2786. * This creates a new empty extent so the caller
  2787. * should be smart enough to have removed any existing
  2788. * ones.
  2789. */
  2790. if (index > 0) {
  2791. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2792. size = index * sizeof(struct ocfs2_extent_rec);
  2793. memmove(&el->l_recs[1], &el->l_recs[0], size);
  2794. }
  2795. /*
  2796. * Always memset - the caller doesn't check whether it
  2797. * created an empty extent, so there could be junk in
  2798. * the other fields.
  2799. */
  2800. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2801. }
  2802. }
  2803. static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
  2804. struct ocfs2_path *left_path,
  2805. struct ocfs2_path **ret_right_path)
  2806. {
  2807. int ret;
  2808. u32 right_cpos;
  2809. struct ocfs2_path *right_path = NULL;
  2810. struct ocfs2_extent_list *left_el;
  2811. *ret_right_path = NULL;
  2812. /* This function shouldn't be called for non-trees. */
  2813. BUG_ON(left_path->p_tree_depth == 0);
  2814. left_el = path_leaf_el(left_path);
  2815. BUG_ON(left_el->l_next_free_rec != left_el->l_count);
  2816. ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2817. left_path, &right_cpos);
  2818. if (ret) {
  2819. mlog_errno(ret);
  2820. goto out;
  2821. }
  2822. /* This function shouldn't be called for the rightmost leaf. */
  2823. BUG_ON(right_cpos == 0);
  2824. right_path = ocfs2_new_path_from_path(left_path);
  2825. if (!right_path) {
  2826. ret = -ENOMEM;
  2827. mlog_errno(ret);
  2828. goto out;
  2829. }
  2830. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2831. if (ret) {
  2832. mlog_errno(ret);
  2833. goto out;
  2834. }
  2835. *ret_right_path = right_path;
  2836. out:
  2837. if (ret)
  2838. ocfs2_free_path(right_path);
  2839. return ret;
  2840. }
  2841. /*
  2842. * Remove split_rec clusters from the record at index and merge them
  2843. * onto the beginning of the record "next" to it.
  2844. * For index < l_count - 1, the next means the extent rec at index + 1.
  2845. * For index == l_count - 1, the "next" means the 1st extent rec of the
  2846. * next extent block.
  2847. */
  2848. static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
  2849. handle_t *handle,
  2850. struct ocfs2_extent_tree *et,
  2851. struct ocfs2_extent_rec *split_rec,
  2852. int index)
  2853. {
  2854. int ret, next_free, i;
  2855. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2856. struct ocfs2_extent_rec *left_rec;
  2857. struct ocfs2_extent_rec *right_rec;
  2858. struct ocfs2_extent_list *right_el;
  2859. struct ocfs2_path *right_path = NULL;
  2860. int subtree_index = 0;
  2861. struct ocfs2_extent_list *el = path_leaf_el(left_path);
  2862. struct buffer_head *bh = path_leaf_bh(left_path);
  2863. struct buffer_head *root_bh = NULL;
  2864. BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
  2865. left_rec = &el->l_recs[index];
  2866. if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
  2867. le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
  2868. /* we meet with a cross extent block merge. */
  2869. ret = ocfs2_get_right_path(et, left_path, &right_path);
  2870. if (ret) {
  2871. mlog_errno(ret);
  2872. return ret;
  2873. }
  2874. right_el = path_leaf_el(right_path);
  2875. next_free = le16_to_cpu(right_el->l_next_free_rec);
  2876. BUG_ON(next_free <= 0);
  2877. right_rec = &right_el->l_recs[0];
  2878. if (ocfs2_is_empty_extent(right_rec)) {
  2879. BUG_ON(next_free <= 1);
  2880. right_rec = &right_el->l_recs[1];
  2881. }
  2882. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  2883. le16_to_cpu(left_rec->e_leaf_clusters) !=
  2884. le32_to_cpu(right_rec->e_cpos));
  2885. subtree_index = ocfs2_find_subtree_root(et, left_path,
  2886. right_path);
  2887. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  2888. handle->h_buffer_credits,
  2889. right_path);
  2890. if (ret) {
  2891. mlog_errno(ret);
  2892. goto out;
  2893. }
  2894. root_bh = left_path->p_node[subtree_index].bh;
  2895. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2896. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2897. subtree_index);
  2898. if (ret) {
  2899. mlog_errno(ret);
  2900. goto out;
  2901. }
  2902. for (i = subtree_index + 1;
  2903. i < path_num_items(right_path); i++) {
  2904. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2905. right_path, i);
  2906. if (ret) {
  2907. mlog_errno(ret);
  2908. goto out;
  2909. }
  2910. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2911. left_path, i);
  2912. if (ret) {
  2913. mlog_errno(ret);
  2914. goto out;
  2915. }
  2916. }
  2917. } else {
  2918. BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
  2919. right_rec = &el->l_recs[index + 1];
  2920. }
  2921. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
  2922. path_num_items(left_path) - 1);
  2923. if (ret) {
  2924. mlog_errno(ret);
  2925. goto out;
  2926. }
  2927. le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
  2928. le32_add_cpu(&right_rec->e_cpos, -split_clusters);
  2929. le64_add_cpu(&right_rec->e_blkno,
  2930. -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  2931. split_clusters));
  2932. le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
  2933. ocfs2_cleanup_merge(el, index);
  2934. ocfs2_journal_dirty(handle, bh);
  2935. if (right_path) {
  2936. ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2937. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2938. subtree_index);
  2939. }
  2940. out:
  2941. ocfs2_free_path(right_path);
  2942. return ret;
  2943. }
  2944. static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
  2945. struct ocfs2_path *right_path,
  2946. struct ocfs2_path **ret_left_path)
  2947. {
  2948. int ret;
  2949. u32 left_cpos;
  2950. struct ocfs2_path *left_path = NULL;
  2951. *ret_left_path = NULL;
  2952. /* This function shouldn't be called for non-trees. */
  2953. BUG_ON(right_path->p_tree_depth == 0);
  2954. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2955. right_path, &left_cpos);
  2956. if (ret) {
  2957. mlog_errno(ret);
  2958. goto out;
  2959. }
  2960. /* This function shouldn't be called for the leftmost leaf. */
  2961. BUG_ON(left_cpos == 0);
  2962. left_path = ocfs2_new_path_from_path(right_path);
  2963. if (!left_path) {
  2964. ret = -ENOMEM;
  2965. mlog_errno(ret);
  2966. goto out;
  2967. }
  2968. ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
  2969. if (ret) {
  2970. mlog_errno(ret);
  2971. goto out;
  2972. }
  2973. *ret_left_path = left_path;
  2974. out:
  2975. if (ret)
  2976. ocfs2_free_path(left_path);
  2977. return ret;
  2978. }
  2979. /*
  2980. * Remove split_rec clusters from the record at index and merge them
  2981. * onto the tail of the record "before" it.
  2982. * For index > 0, the "before" means the extent rec at index - 1.
  2983. *
  2984. * For index == 0, the "before" means the last record of the previous
  2985. * extent block. And there is also a situation that we may need to
  2986. * remove the rightmost leaf extent block in the right_path and change
  2987. * the right path to indicate the new rightmost path.
  2988. */
  2989. static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
  2990. handle_t *handle,
  2991. struct ocfs2_extent_tree *et,
  2992. struct ocfs2_extent_rec *split_rec,
  2993. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2994. int index)
  2995. {
  2996. int ret, i, subtree_index = 0, has_empty_extent = 0;
  2997. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2998. struct ocfs2_extent_rec *left_rec;
  2999. struct ocfs2_extent_rec *right_rec;
  3000. struct ocfs2_extent_list *el = path_leaf_el(right_path);
  3001. struct buffer_head *bh = path_leaf_bh(right_path);
  3002. struct buffer_head *root_bh = NULL;
  3003. struct ocfs2_path *left_path = NULL;
  3004. struct ocfs2_extent_list *left_el;
  3005. BUG_ON(index < 0);
  3006. right_rec = &el->l_recs[index];
  3007. if (index == 0) {
  3008. /* we meet with a cross extent block merge. */
  3009. ret = ocfs2_get_left_path(et, right_path, &left_path);
  3010. if (ret) {
  3011. mlog_errno(ret);
  3012. return ret;
  3013. }
  3014. left_el = path_leaf_el(left_path);
  3015. BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
  3016. le16_to_cpu(left_el->l_count));
  3017. left_rec = &left_el->l_recs[
  3018. le16_to_cpu(left_el->l_next_free_rec) - 1];
  3019. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  3020. le16_to_cpu(left_rec->e_leaf_clusters) !=
  3021. le32_to_cpu(split_rec->e_cpos));
  3022. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3023. right_path);
  3024. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  3025. handle->h_buffer_credits,
  3026. left_path);
  3027. if (ret) {
  3028. mlog_errno(ret);
  3029. goto out;
  3030. }
  3031. root_bh = left_path->p_node[subtree_index].bh;
  3032. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  3033. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3034. subtree_index);
  3035. if (ret) {
  3036. mlog_errno(ret);
  3037. goto out;
  3038. }
  3039. for (i = subtree_index + 1;
  3040. i < path_num_items(right_path); i++) {
  3041. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3042. right_path, i);
  3043. if (ret) {
  3044. mlog_errno(ret);
  3045. goto out;
  3046. }
  3047. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3048. left_path, i);
  3049. if (ret) {
  3050. mlog_errno(ret);
  3051. goto out;
  3052. }
  3053. }
  3054. } else {
  3055. left_rec = &el->l_recs[index - 1];
  3056. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  3057. has_empty_extent = 1;
  3058. }
  3059. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3060. path_num_items(right_path) - 1);
  3061. if (ret) {
  3062. mlog_errno(ret);
  3063. goto out;
  3064. }
  3065. if (has_empty_extent && index == 1) {
  3066. /*
  3067. * The easy case - we can just plop the record right in.
  3068. */
  3069. *left_rec = *split_rec;
  3070. has_empty_extent = 0;
  3071. } else
  3072. le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
  3073. le32_add_cpu(&right_rec->e_cpos, split_clusters);
  3074. le64_add_cpu(&right_rec->e_blkno,
  3075. ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  3076. split_clusters));
  3077. le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
  3078. ocfs2_cleanup_merge(el, index);
  3079. ocfs2_journal_dirty(handle, bh);
  3080. if (left_path) {
  3081. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  3082. /*
  3083. * In the situation that the right_rec is empty and the extent
  3084. * block is empty also, ocfs2_complete_edge_insert can't handle
  3085. * it and we need to delete the right extent block.
  3086. */
  3087. if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
  3088. le16_to_cpu(el->l_next_free_rec) == 1) {
  3089. ret = ocfs2_remove_rightmost_path(handle, et,
  3090. right_path,
  3091. dealloc);
  3092. if (ret) {
  3093. mlog_errno(ret);
  3094. goto out;
  3095. }
  3096. /* Now the rightmost extent block has been deleted.
  3097. * So we use the new rightmost path.
  3098. */
  3099. ocfs2_mv_path(right_path, left_path);
  3100. left_path = NULL;
  3101. } else
  3102. ocfs2_complete_edge_insert(handle, left_path,
  3103. right_path, subtree_index);
  3104. }
  3105. out:
  3106. ocfs2_free_path(left_path);
  3107. return ret;
  3108. }
  3109. static int ocfs2_try_to_merge_extent(handle_t *handle,
  3110. struct ocfs2_extent_tree *et,
  3111. struct ocfs2_path *path,
  3112. int split_index,
  3113. struct ocfs2_extent_rec *split_rec,
  3114. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3115. struct ocfs2_merge_ctxt *ctxt)
  3116. {
  3117. int ret = 0;
  3118. struct ocfs2_extent_list *el = path_leaf_el(path);
  3119. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  3120. BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
  3121. if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
  3122. /*
  3123. * The merge code will need to create an empty
  3124. * extent to take the place of the newly
  3125. * emptied slot. Remove any pre-existing empty
  3126. * extents - having more than one in a leaf is
  3127. * illegal.
  3128. */
  3129. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3130. if (ret) {
  3131. mlog_errno(ret);
  3132. goto out;
  3133. }
  3134. split_index--;
  3135. rec = &el->l_recs[split_index];
  3136. }
  3137. if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
  3138. /*
  3139. * Left-right contig implies this.
  3140. */
  3141. BUG_ON(!ctxt->c_split_covers_rec);
  3142. /*
  3143. * Since the leftright insert always covers the entire
  3144. * extent, this call will delete the insert record
  3145. * entirely, resulting in an empty extent record added to
  3146. * the extent block.
  3147. *
  3148. * Since the adding of an empty extent shifts
  3149. * everything back to the right, there's no need to
  3150. * update split_index here.
  3151. *
  3152. * When the split_index is zero, we need to merge it to the
  3153. * prevoius extent block. It is more efficient and easier
  3154. * if we do merge_right first and merge_left later.
  3155. */
  3156. ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
  3157. split_index);
  3158. if (ret) {
  3159. mlog_errno(ret);
  3160. goto out;
  3161. }
  3162. /*
  3163. * We can only get this from logic error above.
  3164. */
  3165. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  3166. /* The merge left us with an empty extent, remove it. */
  3167. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3168. if (ret) {
  3169. mlog_errno(ret);
  3170. goto out;
  3171. }
  3172. rec = &el->l_recs[split_index];
  3173. /*
  3174. * Note that we don't pass split_rec here on purpose -
  3175. * we've merged it into the rec already.
  3176. */
  3177. ret = ocfs2_merge_rec_left(path, handle, et, rec,
  3178. dealloc, split_index);
  3179. if (ret) {
  3180. mlog_errno(ret);
  3181. goto out;
  3182. }
  3183. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3184. /*
  3185. * Error from this last rotate is not critical, so
  3186. * print but don't bubble it up.
  3187. */
  3188. if (ret)
  3189. mlog_errno(ret);
  3190. ret = 0;
  3191. } else {
  3192. /*
  3193. * Merge a record to the left or right.
  3194. *
  3195. * 'contig_type' is relative to the existing record,
  3196. * so for example, if we're "right contig", it's to
  3197. * the record on the left (hence the left merge).
  3198. */
  3199. if (ctxt->c_contig_type == CONTIG_RIGHT) {
  3200. ret = ocfs2_merge_rec_left(path, handle, et,
  3201. split_rec, dealloc,
  3202. split_index);
  3203. if (ret) {
  3204. mlog_errno(ret);
  3205. goto out;
  3206. }
  3207. } else {
  3208. ret = ocfs2_merge_rec_right(path, handle,
  3209. et, split_rec,
  3210. split_index);
  3211. if (ret) {
  3212. mlog_errno(ret);
  3213. goto out;
  3214. }
  3215. }
  3216. if (ctxt->c_split_covers_rec) {
  3217. /*
  3218. * The merge may have left an empty extent in
  3219. * our leaf. Try to rotate it away.
  3220. */
  3221. ret = ocfs2_rotate_tree_left(handle, et, path,
  3222. dealloc);
  3223. if (ret)
  3224. mlog_errno(ret);
  3225. ret = 0;
  3226. }
  3227. }
  3228. out:
  3229. return ret;
  3230. }
  3231. static void ocfs2_subtract_from_rec(struct super_block *sb,
  3232. enum ocfs2_split_type split,
  3233. struct ocfs2_extent_rec *rec,
  3234. struct ocfs2_extent_rec *split_rec)
  3235. {
  3236. u64 len_blocks;
  3237. len_blocks = ocfs2_clusters_to_blocks(sb,
  3238. le16_to_cpu(split_rec->e_leaf_clusters));
  3239. if (split == SPLIT_LEFT) {
  3240. /*
  3241. * Region is on the left edge of the existing
  3242. * record.
  3243. */
  3244. le32_add_cpu(&rec->e_cpos,
  3245. le16_to_cpu(split_rec->e_leaf_clusters));
  3246. le64_add_cpu(&rec->e_blkno, len_blocks);
  3247. le16_add_cpu(&rec->e_leaf_clusters,
  3248. -le16_to_cpu(split_rec->e_leaf_clusters));
  3249. } else {
  3250. /*
  3251. * Region is on the right edge of the existing
  3252. * record.
  3253. */
  3254. le16_add_cpu(&rec->e_leaf_clusters,
  3255. -le16_to_cpu(split_rec->e_leaf_clusters));
  3256. }
  3257. }
  3258. /*
  3259. * Do the final bits of extent record insertion at the target leaf
  3260. * list. If this leaf is part of an allocation tree, it is assumed
  3261. * that the tree above has been prepared.
  3262. */
  3263. static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
  3264. struct ocfs2_extent_rec *insert_rec,
  3265. struct ocfs2_extent_list *el,
  3266. struct ocfs2_insert_type *insert)
  3267. {
  3268. int i = insert->ins_contig_index;
  3269. unsigned int range;
  3270. struct ocfs2_extent_rec *rec;
  3271. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3272. if (insert->ins_split != SPLIT_NONE) {
  3273. i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
  3274. BUG_ON(i == -1);
  3275. rec = &el->l_recs[i];
  3276. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3277. insert->ins_split, rec,
  3278. insert_rec);
  3279. goto rotate;
  3280. }
  3281. /*
  3282. * Contiguous insert - either left or right.
  3283. */
  3284. if (insert->ins_contig != CONTIG_NONE) {
  3285. rec = &el->l_recs[i];
  3286. if (insert->ins_contig == CONTIG_LEFT) {
  3287. rec->e_blkno = insert_rec->e_blkno;
  3288. rec->e_cpos = insert_rec->e_cpos;
  3289. }
  3290. le16_add_cpu(&rec->e_leaf_clusters,
  3291. le16_to_cpu(insert_rec->e_leaf_clusters));
  3292. return;
  3293. }
  3294. /*
  3295. * Handle insert into an empty leaf.
  3296. */
  3297. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  3298. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  3299. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3300. el->l_recs[0] = *insert_rec;
  3301. el->l_next_free_rec = cpu_to_le16(1);
  3302. return;
  3303. }
  3304. /*
  3305. * Appending insert.
  3306. */
  3307. if (insert->ins_appending == APPEND_TAIL) {
  3308. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3309. rec = &el->l_recs[i];
  3310. range = le32_to_cpu(rec->e_cpos)
  3311. + le16_to_cpu(rec->e_leaf_clusters);
  3312. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  3313. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  3314. le16_to_cpu(el->l_count),
  3315. "owner %llu, depth %u, count %u, next free %u, "
  3316. "rec.cpos %u, rec.clusters %u, "
  3317. "insert.cpos %u, insert.clusters %u\n",
  3318. ocfs2_metadata_cache_owner(et->et_ci),
  3319. le16_to_cpu(el->l_tree_depth),
  3320. le16_to_cpu(el->l_count),
  3321. le16_to_cpu(el->l_next_free_rec),
  3322. le32_to_cpu(el->l_recs[i].e_cpos),
  3323. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  3324. le32_to_cpu(insert_rec->e_cpos),
  3325. le16_to_cpu(insert_rec->e_leaf_clusters));
  3326. i++;
  3327. el->l_recs[i] = *insert_rec;
  3328. le16_add_cpu(&el->l_next_free_rec, 1);
  3329. return;
  3330. }
  3331. rotate:
  3332. /*
  3333. * Ok, we have to rotate.
  3334. *
  3335. * At this point, it is safe to assume that inserting into an
  3336. * empty leaf and appending to a leaf have both been handled
  3337. * above.
  3338. *
  3339. * This leaf needs to have space, either by the empty 1st
  3340. * extent record, or by virtue of an l_next_rec < l_count.
  3341. */
  3342. ocfs2_rotate_leaf(el, insert_rec);
  3343. }
  3344. static void ocfs2_adjust_rightmost_records(handle_t *handle,
  3345. struct ocfs2_extent_tree *et,
  3346. struct ocfs2_path *path,
  3347. struct ocfs2_extent_rec *insert_rec)
  3348. {
  3349. int ret, i, next_free;
  3350. struct buffer_head *bh;
  3351. struct ocfs2_extent_list *el;
  3352. struct ocfs2_extent_rec *rec;
  3353. /*
  3354. * Update everything except the leaf block.
  3355. */
  3356. for (i = 0; i < path->p_tree_depth; i++) {
  3357. bh = path->p_node[i].bh;
  3358. el = path->p_node[i].el;
  3359. next_free = le16_to_cpu(el->l_next_free_rec);
  3360. if (next_free == 0) {
  3361. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  3362. "Owner %llu has a bad extent list",
  3363. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  3364. ret = -EIO;
  3365. return;
  3366. }
  3367. rec = &el->l_recs[next_free - 1];
  3368. rec->e_int_clusters = insert_rec->e_cpos;
  3369. le32_add_cpu(&rec->e_int_clusters,
  3370. le16_to_cpu(insert_rec->e_leaf_clusters));
  3371. le32_add_cpu(&rec->e_int_clusters,
  3372. -le32_to_cpu(rec->e_cpos));
  3373. ocfs2_journal_dirty(handle, bh);
  3374. }
  3375. }
  3376. static int ocfs2_append_rec_to_path(handle_t *handle,
  3377. struct ocfs2_extent_tree *et,
  3378. struct ocfs2_extent_rec *insert_rec,
  3379. struct ocfs2_path *right_path,
  3380. struct ocfs2_path **ret_left_path)
  3381. {
  3382. int ret, next_free;
  3383. struct ocfs2_extent_list *el;
  3384. struct ocfs2_path *left_path = NULL;
  3385. *ret_left_path = NULL;
  3386. /*
  3387. * This shouldn't happen for non-trees. The extent rec cluster
  3388. * count manipulation below only works for interior nodes.
  3389. */
  3390. BUG_ON(right_path->p_tree_depth == 0);
  3391. /*
  3392. * If our appending insert is at the leftmost edge of a leaf,
  3393. * then we might need to update the rightmost records of the
  3394. * neighboring path.
  3395. */
  3396. el = path_leaf_el(right_path);
  3397. next_free = le16_to_cpu(el->l_next_free_rec);
  3398. if (next_free == 0 ||
  3399. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3400. u32 left_cpos;
  3401. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  3402. right_path, &left_cpos);
  3403. if (ret) {
  3404. mlog_errno(ret);
  3405. goto out;
  3406. }
  3407. trace_ocfs2_append_rec_to_path(
  3408. (unsigned long long)
  3409. ocfs2_metadata_cache_owner(et->et_ci),
  3410. le32_to_cpu(insert_rec->e_cpos),
  3411. left_cpos);
  3412. /*
  3413. * No need to worry if the append is already in the
  3414. * leftmost leaf.
  3415. */
  3416. if (left_cpos) {
  3417. left_path = ocfs2_new_path_from_path(right_path);
  3418. if (!left_path) {
  3419. ret = -ENOMEM;
  3420. mlog_errno(ret);
  3421. goto out;
  3422. }
  3423. ret = ocfs2_find_path(et->et_ci, left_path,
  3424. left_cpos);
  3425. if (ret) {
  3426. mlog_errno(ret);
  3427. goto out;
  3428. }
  3429. /*
  3430. * ocfs2_insert_path() will pass the left_path to the
  3431. * journal for us.
  3432. */
  3433. }
  3434. }
  3435. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3436. if (ret) {
  3437. mlog_errno(ret);
  3438. goto out;
  3439. }
  3440. ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
  3441. *ret_left_path = left_path;
  3442. ret = 0;
  3443. out:
  3444. if (ret != 0)
  3445. ocfs2_free_path(left_path);
  3446. return ret;
  3447. }
  3448. static void ocfs2_split_record(struct ocfs2_extent_tree *et,
  3449. struct ocfs2_path *left_path,
  3450. struct ocfs2_path *right_path,
  3451. struct ocfs2_extent_rec *split_rec,
  3452. enum ocfs2_split_type split)
  3453. {
  3454. int index;
  3455. u32 cpos = le32_to_cpu(split_rec->e_cpos);
  3456. struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
  3457. struct ocfs2_extent_rec *rec, *tmprec;
  3458. right_el = path_leaf_el(right_path);
  3459. if (left_path)
  3460. left_el = path_leaf_el(left_path);
  3461. el = right_el;
  3462. insert_el = right_el;
  3463. index = ocfs2_search_extent_list(el, cpos);
  3464. if (index != -1) {
  3465. if (index == 0 && left_path) {
  3466. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  3467. /*
  3468. * This typically means that the record
  3469. * started in the left path but moved to the
  3470. * right as a result of rotation. We either
  3471. * move the existing record to the left, or we
  3472. * do the later insert there.
  3473. *
  3474. * In this case, the left path should always
  3475. * exist as the rotate code will have passed
  3476. * it back for a post-insert update.
  3477. */
  3478. if (split == SPLIT_LEFT) {
  3479. /*
  3480. * It's a left split. Since we know
  3481. * that the rotate code gave us an
  3482. * empty extent in the left path, we
  3483. * can just do the insert there.
  3484. */
  3485. insert_el = left_el;
  3486. } else {
  3487. /*
  3488. * Right split - we have to move the
  3489. * existing record over to the left
  3490. * leaf. The insert will be into the
  3491. * newly created empty extent in the
  3492. * right leaf.
  3493. */
  3494. tmprec = &right_el->l_recs[index];
  3495. ocfs2_rotate_leaf(left_el, tmprec);
  3496. el = left_el;
  3497. memset(tmprec, 0, sizeof(*tmprec));
  3498. index = ocfs2_search_extent_list(left_el, cpos);
  3499. BUG_ON(index == -1);
  3500. }
  3501. }
  3502. } else {
  3503. BUG_ON(!left_path);
  3504. BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
  3505. /*
  3506. * Left path is easy - we can just allow the insert to
  3507. * happen.
  3508. */
  3509. el = left_el;
  3510. insert_el = left_el;
  3511. index = ocfs2_search_extent_list(el, cpos);
  3512. BUG_ON(index == -1);
  3513. }
  3514. rec = &el->l_recs[index];
  3515. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3516. split, rec, split_rec);
  3517. ocfs2_rotate_leaf(insert_el, split_rec);
  3518. }
  3519. /*
  3520. * This function only does inserts on an allocation b-tree. For tree
  3521. * depth = 0, ocfs2_insert_at_leaf() is called directly.
  3522. *
  3523. * right_path is the path we want to do the actual insert
  3524. * in. left_path should only be passed in if we need to update that
  3525. * portion of the tree after an edge insert.
  3526. */
  3527. static int ocfs2_insert_path(handle_t *handle,
  3528. struct ocfs2_extent_tree *et,
  3529. struct ocfs2_path *left_path,
  3530. struct ocfs2_path *right_path,
  3531. struct ocfs2_extent_rec *insert_rec,
  3532. struct ocfs2_insert_type *insert)
  3533. {
  3534. int ret, subtree_index;
  3535. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  3536. if (left_path) {
  3537. /*
  3538. * There's a chance that left_path got passed back to
  3539. * us without being accounted for in the
  3540. * journal. Extend our transaction here to be sure we
  3541. * can change those blocks.
  3542. */
  3543. ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
  3544. if (ret < 0) {
  3545. mlog_errno(ret);
  3546. goto out;
  3547. }
  3548. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  3549. if (ret < 0) {
  3550. mlog_errno(ret);
  3551. goto out;
  3552. }
  3553. }
  3554. /*
  3555. * Pass both paths to the journal. The majority of inserts
  3556. * will be touching all components anyway.
  3557. */
  3558. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3559. if (ret < 0) {
  3560. mlog_errno(ret);
  3561. goto out;
  3562. }
  3563. if (insert->ins_split != SPLIT_NONE) {
  3564. /*
  3565. * We could call ocfs2_insert_at_leaf() for some types
  3566. * of splits, but it's easier to just let one separate
  3567. * function sort it all out.
  3568. */
  3569. ocfs2_split_record(et, left_path, right_path,
  3570. insert_rec, insert->ins_split);
  3571. /*
  3572. * Split might have modified either leaf and we don't
  3573. * have a guarantee that the later edge insert will
  3574. * dirty this for us.
  3575. */
  3576. if (left_path)
  3577. ocfs2_journal_dirty(handle,
  3578. path_leaf_bh(left_path));
  3579. } else
  3580. ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
  3581. insert);
  3582. ocfs2_journal_dirty(handle, leaf_bh);
  3583. if (left_path) {
  3584. /*
  3585. * The rotate code has indicated that we need to fix
  3586. * up portions of the tree after the insert.
  3587. *
  3588. * XXX: Should we extend the transaction here?
  3589. */
  3590. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3591. right_path);
  3592. ocfs2_complete_edge_insert(handle, left_path, right_path,
  3593. subtree_index);
  3594. }
  3595. ret = 0;
  3596. out:
  3597. return ret;
  3598. }
  3599. static int ocfs2_do_insert_extent(handle_t *handle,
  3600. struct ocfs2_extent_tree *et,
  3601. struct ocfs2_extent_rec *insert_rec,
  3602. struct ocfs2_insert_type *type)
  3603. {
  3604. int ret, rotate = 0;
  3605. u32 cpos;
  3606. struct ocfs2_path *right_path = NULL;
  3607. struct ocfs2_path *left_path = NULL;
  3608. struct ocfs2_extent_list *el;
  3609. el = et->et_root_el;
  3610. ret = ocfs2_et_root_journal_access(handle, et,
  3611. OCFS2_JOURNAL_ACCESS_WRITE);
  3612. if (ret) {
  3613. mlog_errno(ret);
  3614. goto out;
  3615. }
  3616. if (le16_to_cpu(el->l_tree_depth) == 0) {
  3617. ocfs2_insert_at_leaf(et, insert_rec, el, type);
  3618. goto out_update_clusters;
  3619. }
  3620. right_path = ocfs2_new_path_from_et(et);
  3621. if (!right_path) {
  3622. ret = -ENOMEM;
  3623. mlog_errno(ret);
  3624. goto out;
  3625. }
  3626. /*
  3627. * Determine the path to start with. Rotations need the
  3628. * rightmost path, everything else can go directly to the
  3629. * target leaf.
  3630. */
  3631. cpos = le32_to_cpu(insert_rec->e_cpos);
  3632. if (type->ins_appending == APPEND_NONE &&
  3633. type->ins_contig == CONTIG_NONE) {
  3634. rotate = 1;
  3635. cpos = UINT_MAX;
  3636. }
  3637. ret = ocfs2_find_path(et->et_ci, right_path, cpos);
  3638. if (ret) {
  3639. mlog_errno(ret);
  3640. goto out;
  3641. }
  3642. /*
  3643. * Rotations and appends need special treatment - they modify
  3644. * parts of the tree's above them.
  3645. *
  3646. * Both might pass back a path immediate to the left of the
  3647. * one being inserted to. This will be cause
  3648. * ocfs2_insert_path() to modify the rightmost records of
  3649. * left_path to account for an edge insert.
  3650. *
  3651. * XXX: When modifying this code, keep in mind that an insert
  3652. * can wind up skipping both of these two special cases...
  3653. */
  3654. if (rotate) {
  3655. ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
  3656. le32_to_cpu(insert_rec->e_cpos),
  3657. right_path, &left_path);
  3658. if (ret) {
  3659. mlog_errno(ret);
  3660. goto out;
  3661. }
  3662. /*
  3663. * ocfs2_rotate_tree_right() might have extended the
  3664. * transaction without re-journaling our tree root.
  3665. */
  3666. ret = ocfs2_et_root_journal_access(handle, et,
  3667. OCFS2_JOURNAL_ACCESS_WRITE);
  3668. if (ret) {
  3669. mlog_errno(ret);
  3670. goto out;
  3671. }
  3672. } else if (type->ins_appending == APPEND_TAIL
  3673. && type->ins_contig != CONTIG_LEFT) {
  3674. ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
  3675. right_path, &left_path);
  3676. if (ret) {
  3677. mlog_errno(ret);
  3678. goto out;
  3679. }
  3680. }
  3681. ret = ocfs2_insert_path(handle, et, left_path, right_path,
  3682. insert_rec, type);
  3683. if (ret) {
  3684. mlog_errno(ret);
  3685. goto out;
  3686. }
  3687. out_update_clusters:
  3688. if (type->ins_split == SPLIT_NONE)
  3689. ocfs2_et_update_clusters(et,
  3690. le16_to_cpu(insert_rec->e_leaf_clusters));
  3691. ocfs2_journal_dirty(handle, et->et_root_bh);
  3692. out:
  3693. ocfs2_free_path(left_path);
  3694. ocfs2_free_path(right_path);
  3695. return ret;
  3696. }
  3697. static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
  3698. struct ocfs2_path *path,
  3699. struct ocfs2_extent_list *el, int index,
  3700. struct ocfs2_extent_rec *split_rec,
  3701. struct ocfs2_merge_ctxt *ctxt)
  3702. {
  3703. int status = 0;
  3704. enum ocfs2_contig_type ret = CONTIG_NONE;
  3705. u32 left_cpos, right_cpos;
  3706. struct ocfs2_extent_rec *rec = NULL;
  3707. struct ocfs2_extent_list *new_el;
  3708. struct ocfs2_path *left_path = NULL, *right_path = NULL;
  3709. struct buffer_head *bh;
  3710. struct ocfs2_extent_block *eb;
  3711. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  3712. if (index > 0) {
  3713. rec = &el->l_recs[index - 1];
  3714. } else if (path->p_tree_depth > 0) {
  3715. status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  3716. if (status)
  3717. goto exit;
  3718. if (left_cpos != 0) {
  3719. left_path = ocfs2_new_path_from_path(path);
  3720. if (!left_path) {
  3721. status = -ENOMEM;
  3722. mlog_errno(status);
  3723. goto exit;
  3724. }
  3725. status = ocfs2_find_path(et->et_ci, left_path,
  3726. left_cpos);
  3727. if (status)
  3728. goto free_left_path;
  3729. new_el = path_leaf_el(left_path);
  3730. if (le16_to_cpu(new_el->l_next_free_rec) !=
  3731. le16_to_cpu(new_el->l_count)) {
  3732. bh = path_leaf_bh(left_path);
  3733. eb = (struct ocfs2_extent_block *)bh->b_data;
  3734. ocfs2_error(sb,
  3735. "Extent block #%llu has an "
  3736. "invalid l_next_free_rec of "
  3737. "%d. It should have "
  3738. "matched the l_count of %d",
  3739. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3740. le16_to_cpu(new_el->l_next_free_rec),
  3741. le16_to_cpu(new_el->l_count));
  3742. status = -EINVAL;
  3743. goto free_left_path;
  3744. }
  3745. rec = &new_el->l_recs[
  3746. le16_to_cpu(new_el->l_next_free_rec) - 1];
  3747. }
  3748. }
  3749. /*
  3750. * We're careful to check for an empty extent record here -
  3751. * the merge code will know what to do if it sees one.
  3752. */
  3753. if (rec) {
  3754. if (index == 1 && ocfs2_is_empty_extent(rec)) {
  3755. if (split_rec->e_cpos == el->l_recs[index].e_cpos)
  3756. ret = CONTIG_RIGHT;
  3757. } else {
  3758. ret = ocfs2_et_extent_contig(et, rec, split_rec);
  3759. }
  3760. }
  3761. rec = NULL;
  3762. if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
  3763. rec = &el->l_recs[index + 1];
  3764. else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
  3765. path->p_tree_depth > 0) {
  3766. status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  3767. if (status)
  3768. goto free_left_path;
  3769. if (right_cpos == 0)
  3770. goto free_left_path;
  3771. right_path = ocfs2_new_path_from_path(path);
  3772. if (!right_path) {
  3773. status = -ENOMEM;
  3774. mlog_errno(status);
  3775. goto free_left_path;
  3776. }
  3777. status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  3778. if (status)
  3779. goto free_right_path;
  3780. new_el = path_leaf_el(right_path);
  3781. rec = &new_el->l_recs[0];
  3782. if (ocfs2_is_empty_extent(rec)) {
  3783. if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
  3784. bh = path_leaf_bh(right_path);
  3785. eb = (struct ocfs2_extent_block *)bh->b_data;
  3786. ocfs2_error(sb,
  3787. "Extent block #%llu has an "
  3788. "invalid l_next_free_rec of %d",
  3789. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3790. le16_to_cpu(new_el->l_next_free_rec));
  3791. status = -EINVAL;
  3792. goto free_right_path;
  3793. }
  3794. rec = &new_el->l_recs[1];
  3795. }
  3796. }
  3797. if (rec) {
  3798. enum ocfs2_contig_type contig_type;
  3799. contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
  3800. if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
  3801. ret = CONTIG_LEFTRIGHT;
  3802. else if (ret == CONTIG_NONE)
  3803. ret = contig_type;
  3804. }
  3805. free_right_path:
  3806. ocfs2_free_path(right_path);
  3807. free_left_path:
  3808. ocfs2_free_path(left_path);
  3809. exit:
  3810. if (status == 0)
  3811. ctxt->c_contig_type = ret;
  3812. return status;
  3813. }
  3814. static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
  3815. struct ocfs2_insert_type *insert,
  3816. struct ocfs2_extent_list *el,
  3817. struct ocfs2_extent_rec *insert_rec)
  3818. {
  3819. int i;
  3820. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  3821. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3822. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  3823. contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
  3824. insert_rec);
  3825. if (contig_type != CONTIG_NONE) {
  3826. insert->ins_contig_index = i;
  3827. break;
  3828. }
  3829. }
  3830. insert->ins_contig = contig_type;
  3831. if (insert->ins_contig != CONTIG_NONE) {
  3832. struct ocfs2_extent_rec *rec =
  3833. &el->l_recs[insert->ins_contig_index];
  3834. unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
  3835. le16_to_cpu(insert_rec->e_leaf_clusters);
  3836. /*
  3837. * Caller might want us to limit the size of extents, don't
  3838. * calculate contiguousness if we might exceed that limit.
  3839. */
  3840. if (et->et_max_leaf_clusters &&
  3841. (len > et->et_max_leaf_clusters))
  3842. insert->ins_contig = CONTIG_NONE;
  3843. }
  3844. }
  3845. /*
  3846. * This should only be called against the righmost leaf extent list.
  3847. *
  3848. * ocfs2_figure_appending_type() will figure out whether we'll have to
  3849. * insert at the tail of the rightmost leaf.
  3850. *
  3851. * This should also work against the root extent list for tree's with 0
  3852. * depth. If we consider the root extent list to be the rightmost leaf node
  3853. * then the logic here makes sense.
  3854. */
  3855. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  3856. struct ocfs2_extent_list *el,
  3857. struct ocfs2_extent_rec *insert_rec)
  3858. {
  3859. int i;
  3860. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  3861. struct ocfs2_extent_rec *rec;
  3862. insert->ins_appending = APPEND_NONE;
  3863. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3864. if (!el->l_next_free_rec)
  3865. goto set_tail_append;
  3866. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  3867. /* Were all records empty? */
  3868. if (le16_to_cpu(el->l_next_free_rec) == 1)
  3869. goto set_tail_append;
  3870. }
  3871. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3872. rec = &el->l_recs[i];
  3873. if (cpos >=
  3874. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  3875. goto set_tail_append;
  3876. return;
  3877. set_tail_append:
  3878. insert->ins_appending = APPEND_TAIL;
  3879. }
  3880. /*
  3881. * Helper function called at the beginning of an insert.
  3882. *
  3883. * This computes a few things that are commonly used in the process of
  3884. * inserting into the btree:
  3885. * - Whether the new extent is contiguous with an existing one.
  3886. * - The current tree depth.
  3887. * - Whether the insert is an appending one.
  3888. * - The total # of free records in the tree.
  3889. *
  3890. * All of the information is stored on the ocfs2_insert_type
  3891. * structure.
  3892. */
  3893. static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
  3894. struct buffer_head **last_eb_bh,
  3895. struct ocfs2_extent_rec *insert_rec,
  3896. int *free_records,
  3897. struct ocfs2_insert_type *insert)
  3898. {
  3899. int ret;
  3900. struct ocfs2_extent_block *eb;
  3901. struct ocfs2_extent_list *el;
  3902. struct ocfs2_path *path = NULL;
  3903. struct buffer_head *bh = NULL;
  3904. insert->ins_split = SPLIT_NONE;
  3905. el = et->et_root_el;
  3906. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  3907. if (el->l_tree_depth) {
  3908. /*
  3909. * If we have tree depth, we read in the
  3910. * rightmost extent block ahead of time as
  3911. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  3912. * may want it later.
  3913. */
  3914. ret = ocfs2_read_extent_block(et->et_ci,
  3915. ocfs2_et_get_last_eb_blk(et),
  3916. &bh);
  3917. if (ret) {
  3918. mlog_errno(ret);
  3919. goto out;
  3920. }
  3921. eb = (struct ocfs2_extent_block *) bh->b_data;
  3922. el = &eb->h_list;
  3923. }
  3924. /*
  3925. * Unless we have a contiguous insert, we'll need to know if
  3926. * there is room left in our allocation tree for another
  3927. * extent record.
  3928. *
  3929. * XXX: This test is simplistic, we can search for empty
  3930. * extent records too.
  3931. */
  3932. *free_records = le16_to_cpu(el->l_count) -
  3933. le16_to_cpu(el->l_next_free_rec);
  3934. if (!insert->ins_tree_depth) {
  3935. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  3936. ocfs2_figure_appending_type(insert, el, insert_rec);
  3937. return 0;
  3938. }
  3939. path = ocfs2_new_path_from_et(et);
  3940. if (!path) {
  3941. ret = -ENOMEM;
  3942. mlog_errno(ret);
  3943. goto out;
  3944. }
  3945. /*
  3946. * In the case that we're inserting past what the tree
  3947. * currently accounts for, ocfs2_find_path() will return for
  3948. * us the rightmost tree path. This is accounted for below in
  3949. * the appending code.
  3950. */
  3951. ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
  3952. if (ret) {
  3953. mlog_errno(ret);
  3954. goto out;
  3955. }
  3956. el = path_leaf_el(path);
  3957. /*
  3958. * Now that we have the path, there's two things we want to determine:
  3959. * 1) Contiguousness (also set contig_index if this is so)
  3960. *
  3961. * 2) Are we doing an append? We can trivially break this up
  3962. * into two types of appends: simple record append, or a
  3963. * rotate inside the tail leaf.
  3964. */
  3965. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  3966. /*
  3967. * The insert code isn't quite ready to deal with all cases of
  3968. * left contiguousness. Specifically, if it's an insert into
  3969. * the 1st record in a leaf, it will require the adjustment of
  3970. * cluster count on the last record of the path directly to it's
  3971. * left. For now, just catch that case and fool the layers
  3972. * above us. This works just fine for tree_depth == 0, which
  3973. * is why we allow that above.
  3974. */
  3975. if (insert->ins_contig == CONTIG_LEFT &&
  3976. insert->ins_contig_index == 0)
  3977. insert->ins_contig = CONTIG_NONE;
  3978. /*
  3979. * Ok, so we can simply compare against last_eb to figure out
  3980. * whether the path doesn't exist. This will only happen in
  3981. * the case that we're doing a tail append, so maybe we can
  3982. * take advantage of that information somehow.
  3983. */
  3984. if (ocfs2_et_get_last_eb_blk(et) ==
  3985. path_leaf_bh(path)->b_blocknr) {
  3986. /*
  3987. * Ok, ocfs2_find_path() returned us the rightmost
  3988. * tree path. This might be an appending insert. There are
  3989. * two cases:
  3990. * 1) We're doing a true append at the tail:
  3991. * -This might even be off the end of the leaf
  3992. * 2) We're "appending" by rotating in the tail
  3993. */
  3994. ocfs2_figure_appending_type(insert, el, insert_rec);
  3995. }
  3996. out:
  3997. ocfs2_free_path(path);
  3998. if (ret == 0)
  3999. *last_eb_bh = bh;
  4000. else
  4001. brelse(bh);
  4002. return ret;
  4003. }
  4004. /*
  4005. * Insert an extent into a btree.
  4006. *
  4007. * The caller needs to update the owning btree's cluster count.
  4008. */
  4009. int ocfs2_insert_extent(handle_t *handle,
  4010. struct ocfs2_extent_tree *et,
  4011. u32 cpos,
  4012. u64 start_blk,
  4013. u32 new_clusters,
  4014. u8 flags,
  4015. struct ocfs2_alloc_context *meta_ac)
  4016. {
  4017. int status;
  4018. int uninitialized_var(free_records);
  4019. struct buffer_head *last_eb_bh = NULL;
  4020. struct ocfs2_insert_type insert = {0, };
  4021. struct ocfs2_extent_rec rec;
  4022. trace_ocfs2_insert_extent_start(
  4023. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4024. cpos, new_clusters);
  4025. memset(&rec, 0, sizeof(rec));
  4026. rec.e_cpos = cpu_to_le32(cpos);
  4027. rec.e_blkno = cpu_to_le64(start_blk);
  4028. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  4029. rec.e_flags = flags;
  4030. status = ocfs2_et_insert_check(et, &rec);
  4031. if (status) {
  4032. mlog_errno(status);
  4033. goto bail;
  4034. }
  4035. status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
  4036. &free_records, &insert);
  4037. if (status < 0) {
  4038. mlog_errno(status);
  4039. goto bail;
  4040. }
  4041. trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
  4042. insert.ins_contig_index, free_records,
  4043. insert.ins_tree_depth);
  4044. if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
  4045. status = ocfs2_grow_tree(handle, et,
  4046. &insert.ins_tree_depth, &last_eb_bh,
  4047. meta_ac);
  4048. if (status) {
  4049. mlog_errno(status);
  4050. goto bail;
  4051. }
  4052. }
  4053. /* Finally, we can add clusters. This might rotate the tree for us. */
  4054. status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
  4055. if (status < 0)
  4056. mlog_errno(status);
  4057. else
  4058. ocfs2_et_extent_map_insert(et, &rec);
  4059. bail:
  4060. brelse(last_eb_bh);
  4061. return status;
  4062. }
  4063. /*
  4064. * Allcate and add clusters into the extent b-tree.
  4065. * The new clusters(clusters_to_add) will be inserted at logical_offset.
  4066. * The extent b-tree's root is specified by et, and
  4067. * it is not limited to the file storage. Any extent tree can use this
  4068. * function if it implements the proper ocfs2_extent_tree.
  4069. */
  4070. int ocfs2_add_clusters_in_btree(handle_t *handle,
  4071. struct ocfs2_extent_tree *et,
  4072. u32 *logical_offset,
  4073. u32 clusters_to_add,
  4074. int mark_unwritten,
  4075. struct ocfs2_alloc_context *data_ac,
  4076. struct ocfs2_alloc_context *meta_ac,
  4077. enum ocfs2_alloc_restarted *reason_ret)
  4078. {
  4079. int status = 0, err = 0;
  4080. int need_free = 0;
  4081. int free_extents;
  4082. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  4083. u32 bit_off, num_bits;
  4084. u64 block;
  4085. u8 flags = 0;
  4086. struct ocfs2_super *osb =
  4087. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  4088. BUG_ON(!clusters_to_add);
  4089. if (mark_unwritten)
  4090. flags = OCFS2_EXT_UNWRITTEN;
  4091. free_extents = ocfs2_num_free_extents(osb, et);
  4092. if (free_extents < 0) {
  4093. status = free_extents;
  4094. mlog_errno(status);
  4095. goto leave;
  4096. }
  4097. /* there are two cases which could cause us to EAGAIN in the
  4098. * we-need-more-metadata case:
  4099. * 1) we haven't reserved *any*
  4100. * 2) we are so fragmented, we've needed to add metadata too
  4101. * many times. */
  4102. if (!free_extents && !meta_ac) {
  4103. err = -1;
  4104. status = -EAGAIN;
  4105. reason = RESTART_META;
  4106. goto leave;
  4107. } else if ((!free_extents)
  4108. && (ocfs2_alloc_context_bits_left(meta_ac)
  4109. < ocfs2_extend_meta_needed(et->et_root_el))) {
  4110. err = -2;
  4111. status = -EAGAIN;
  4112. reason = RESTART_META;
  4113. goto leave;
  4114. }
  4115. status = __ocfs2_claim_clusters(handle, data_ac, 1,
  4116. clusters_to_add, &bit_off, &num_bits);
  4117. if (status < 0) {
  4118. if (status != -ENOSPC)
  4119. mlog_errno(status);
  4120. goto leave;
  4121. }
  4122. BUG_ON(num_bits > clusters_to_add);
  4123. /* reserve our write early -- insert_extent may update the tree root */
  4124. status = ocfs2_et_root_journal_access(handle, et,
  4125. OCFS2_JOURNAL_ACCESS_WRITE);
  4126. if (status < 0) {
  4127. mlog_errno(status);
  4128. need_free = 1;
  4129. goto bail;
  4130. }
  4131. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  4132. trace_ocfs2_add_clusters_in_btree(
  4133. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4134. bit_off, num_bits);
  4135. status = ocfs2_insert_extent(handle, et, *logical_offset, block,
  4136. num_bits, flags, meta_ac);
  4137. if (status < 0) {
  4138. mlog_errno(status);
  4139. need_free = 1;
  4140. goto bail;
  4141. }
  4142. ocfs2_journal_dirty(handle, et->et_root_bh);
  4143. clusters_to_add -= num_bits;
  4144. *logical_offset += num_bits;
  4145. if (clusters_to_add) {
  4146. err = clusters_to_add;
  4147. status = -EAGAIN;
  4148. reason = RESTART_TRANS;
  4149. }
  4150. bail:
  4151. if (need_free) {
  4152. if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
  4153. ocfs2_free_local_alloc_bits(osb, handle, data_ac,
  4154. bit_off, num_bits);
  4155. else
  4156. ocfs2_free_clusters(handle,
  4157. data_ac->ac_inode,
  4158. data_ac->ac_bh,
  4159. ocfs2_clusters_to_blocks(osb->sb, bit_off),
  4160. num_bits);
  4161. }
  4162. leave:
  4163. if (reason_ret)
  4164. *reason_ret = reason;
  4165. trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
  4166. return status;
  4167. }
  4168. static void ocfs2_make_right_split_rec(struct super_block *sb,
  4169. struct ocfs2_extent_rec *split_rec,
  4170. u32 cpos,
  4171. struct ocfs2_extent_rec *rec)
  4172. {
  4173. u32 rec_cpos = le32_to_cpu(rec->e_cpos);
  4174. u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
  4175. memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4176. split_rec->e_cpos = cpu_to_le32(cpos);
  4177. split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
  4178. split_rec->e_blkno = rec->e_blkno;
  4179. le64_add_cpu(&split_rec->e_blkno,
  4180. ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
  4181. split_rec->e_flags = rec->e_flags;
  4182. }
  4183. static int ocfs2_split_and_insert(handle_t *handle,
  4184. struct ocfs2_extent_tree *et,
  4185. struct ocfs2_path *path,
  4186. struct buffer_head **last_eb_bh,
  4187. int split_index,
  4188. struct ocfs2_extent_rec *orig_split_rec,
  4189. struct ocfs2_alloc_context *meta_ac)
  4190. {
  4191. int ret = 0, depth;
  4192. unsigned int insert_range, rec_range, do_leftright = 0;
  4193. struct ocfs2_extent_rec tmprec;
  4194. struct ocfs2_extent_list *rightmost_el;
  4195. struct ocfs2_extent_rec rec;
  4196. struct ocfs2_extent_rec split_rec = *orig_split_rec;
  4197. struct ocfs2_insert_type insert;
  4198. struct ocfs2_extent_block *eb;
  4199. leftright:
  4200. /*
  4201. * Store a copy of the record on the stack - it might move
  4202. * around as the tree is manipulated below.
  4203. */
  4204. rec = path_leaf_el(path)->l_recs[split_index];
  4205. rightmost_el = et->et_root_el;
  4206. depth = le16_to_cpu(rightmost_el->l_tree_depth);
  4207. if (depth) {
  4208. BUG_ON(!(*last_eb_bh));
  4209. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  4210. rightmost_el = &eb->h_list;
  4211. }
  4212. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4213. le16_to_cpu(rightmost_el->l_count)) {
  4214. ret = ocfs2_grow_tree(handle, et,
  4215. &depth, last_eb_bh, meta_ac);
  4216. if (ret) {
  4217. mlog_errno(ret);
  4218. goto out;
  4219. }
  4220. }
  4221. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4222. insert.ins_appending = APPEND_NONE;
  4223. insert.ins_contig = CONTIG_NONE;
  4224. insert.ins_tree_depth = depth;
  4225. insert_range = le32_to_cpu(split_rec.e_cpos) +
  4226. le16_to_cpu(split_rec.e_leaf_clusters);
  4227. rec_range = le32_to_cpu(rec.e_cpos) +
  4228. le16_to_cpu(rec.e_leaf_clusters);
  4229. if (split_rec.e_cpos == rec.e_cpos) {
  4230. insert.ins_split = SPLIT_LEFT;
  4231. } else if (insert_range == rec_range) {
  4232. insert.ins_split = SPLIT_RIGHT;
  4233. } else {
  4234. /*
  4235. * Left/right split. We fake this as a right split
  4236. * first and then make a second pass as a left split.
  4237. */
  4238. insert.ins_split = SPLIT_RIGHT;
  4239. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4240. &tmprec, insert_range, &rec);
  4241. split_rec = tmprec;
  4242. BUG_ON(do_leftright);
  4243. do_leftright = 1;
  4244. }
  4245. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4246. if (ret) {
  4247. mlog_errno(ret);
  4248. goto out;
  4249. }
  4250. if (do_leftright == 1) {
  4251. u32 cpos;
  4252. struct ocfs2_extent_list *el;
  4253. do_leftright++;
  4254. split_rec = *orig_split_rec;
  4255. ocfs2_reinit_path(path, 1);
  4256. cpos = le32_to_cpu(split_rec.e_cpos);
  4257. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4258. if (ret) {
  4259. mlog_errno(ret);
  4260. goto out;
  4261. }
  4262. el = path_leaf_el(path);
  4263. split_index = ocfs2_search_extent_list(el, cpos);
  4264. if (split_index == -1) {
  4265. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4266. "Owner %llu has an extent at cpos %u "
  4267. "which can no longer be found.\n",
  4268. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4269. cpos);
  4270. ret = -EROFS;
  4271. goto out;
  4272. }
  4273. goto leftright;
  4274. }
  4275. out:
  4276. return ret;
  4277. }
  4278. static int ocfs2_replace_extent_rec(handle_t *handle,
  4279. struct ocfs2_extent_tree *et,
  4280. struct ocfs2_path *path,
  4281. struct ocfs2_extent_list *el,
  4282. int split_index,
  4283. struct ocfs2_extent_rec *split_rec)
  4284. {
  4285. int ret;
  4286. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  4287. path_num_items(path) - 1);
  4288. if (ret) {
  4289. mlog_errno(ret);
  4290. goto out;
  4291. }
  4292. el->l_recs[split_index] = *split_rec;
  4293. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4294. out:
  4295. return ret;
  4296. }
  4297. /*
  4298. * Split part or all of the extent record at split_index in the leaf
  4299. * pointed to by path. Merge with the contiguous extent record if needed.
  4300. *
  4301. * Care is taken to handle contiguousness so as to not grow the tree.
  4302. *
  4303. * meta_ac is not strictly necessary - we only truly need it if growth
  4304. * of the tree is required. All other cases will degrade into a less
  4305. * optimal tree layout.
  4306. *
  4307. * last_eb_bh should be the rightmost leaf block for any extent
  4308. * btree. Since a split may grow the tree or a merge might shrink it,
  4309. * the caller cannot trust the contents of that buffer after this call.
  4310. *
  4311. * This code is optimized for readability - several passes might be
  4312. * made over certain portions of the tree. All of those blocks will
  4313. * have been brought into cache (and pinned via the journal), so the
  4314. * extra overhead is not expressed in terms of disk reads.
  4315. */
  4316. int ocfs2_split_extent(handle_t *handle,
  4317. struct ocfs2_extent_tree *et,
  4318. struct ocfs2_path *path,
  4319. int split_index,
  4320. struct ocfs2_extent_rec *split_rec,
  4321. struct ocfs2_alloc_context *meta_ac,
  4322. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4323. {
  4324. int ret = 0;
  4325. struct ocfs2_extent_list *el = path_leaf_el(path);
  4326. struct buffer_head *last_eb_bh = NULL;
  4327. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  4328. struct ocfs2_merge_ctxt ctxt;
  4329. struct ocfs2_extent_list *rightmost_el;
  4330. if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
  4331. ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
  4332. (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
  4333. ret = -EIO;
  4334. mlog_errno(ret);
  4335. goto out;
  4336. }
  4337. ret = ocfs2_figure_merge_contig_type(et, path, el,
  4338. split_index,
  4339. split_rec,
  4340. &ctxt);
  4341. if (ret) {
  4342. mlog_errno(ret);
  4343. goto out;
  4344. }
  4345. /*
  4346. * The core merge / split code wants to know how much room is
  4347. * left in this allocation tree, so we pass the
  4348. * rightmost extent list.
  4349. */
  4350. if (path->p_tree_depth) {
  4351. struct ocfs2_extent_block *eb;
  4352. ret = ocfs2_read_extent_block(et->et_ci,
  4353. ocfs2_et_get_last_eb_blk(et),
  4354. &last_eb_bh);
  4355. if (ret) {
  4356. mlog_errno(ret);
  4357. goto out;
  4358. }
  4359. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4360. rightmost_el = &eb->h_list;
  4361. } else
  4362. rightmost_el = path_root_el(path);
  4363. if (rec->e_cpos == split_rec->e_cpos &&
  4364. rec->e_leaf_clusters == split_rec->e_leaf_clusters)
  4365. ctxt.c_split_covers_rec = 1;
  4366. else
  4367. ctxt.c_split_covers_rec = 0;
  4368. ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
  4369. trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
  4370. ctxt.c_has_empty_extent,
  4371. ctxt.c_split_covers_rec);
  4372. if (ctxt.c_contig_type == CONTIG_NONE) {
  4373. if (ctxt.c_split_covers_rec)
  4374. ret = ocfs2_replace_extent_rec(handle, et, path, el,
  4375. split_index, split_rec);
  4376. else
  4377. ret = ocfs2_split_and_insert(handle, et, path,
  4378. &last_eb_bh, split_index,
  4379. split_rec, meta_ac);
  4380. if (ret)
  4381. mlog_errno(ret);
  4382. } else {
  4383. ret = ocfs2_try_to_merge_extent(handle, et, path,
  4384. split_index, split_rec,
  4385. dealloc, &ctxt);
  4386. if (ret)
  4387. mlog_errno(ret);
  4388. }
  4389. out:
  4390. brelse(last_eb_bh);
  4391. return ret;
  4392. }
  4393. /*
  4394. * Change the flags of the already-existing extent at cpos for len clusters.
  4395. *
  4396. * new_flags: the flags we want to set.
  4397. * clear_flags: the flags we want to clear.
  4398. * phys: the new physical offset we want this new extent starts from.
  4399. *
  4400. * If the existing extent is larger than the request, initiate a
  4401. * split. An attempt will be made at merging with adjacent extents.
  4402. *
  4403. * The caller is responsible for passing down meta_ac if we'll need it.
  4404. */
  4405. int ocfs2_change_extent_flag(handle_t *handle,
  4406. struct ocfs2_extent_tree *et,
  4407. u32 cpos, u32 len, u32 phys,
  4408. struct ocfs2_alloc_context *meta_ac,
  4409. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4410. int new_flags, int clear_flags)
  4411. {
  4412. int ret, index;
  4413. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4414. u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
  4415. struct ocfs2_extent_rec split_rec;
  4416. struct ocfs2_path *left_path = NULL;
  4417. struct ocfs2_extent_list *el;
  4418. struct ocfs2_extent_rec *rec;
  4419. left_path = ocfs2_new_path_from_et(et);
  4420. if (!left_path) {
  4421. ret = -ENOMEM;
  4422. mlog_errno(ret);
  4423. goto out;
  4424. }
  4425. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  4426. if (ret) {
  4427. mlog_errno(ret);
  4428. goto out;
  4429. }
  4430. el = path_leaf_el(left_path);
  4431. index = ocfs2_search_extent_list(el, cpos);
  4432. if (index == -1) {
  4433. ocfs2_error(sb,
  4434. "Owner %llu has an extent at cpos %u which can no "
  4435. "longer be found.\n",
  4436. (unsigned long long)
  4437. ocfs2_metadata_cache_owner(et->et_ci), cpos);
  4438. ret = -EROFS;
  4439. goto out;
  4440. }
  4441. ret = -EIO;
  4442. rec = &el->l_recs[index];
  4443. if (new_flags && (rec->e_flags & new_flags)) {
  4444. mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
  4445. "extent that already had them",
  4446. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4447. new_flags);
  4448. goto out;
  4449. }
  4450. if (clear_flags && !(rec->e_flags & clear_flags)) {
  4451. mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
  4452. "extent that didn't have them",
  4453. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4454. clear_flags);
  4455. goto out;
  4456. }
  4457. memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4458. split_rec.e_cpos = cpu_to_le32(cpos);
  4459. split_rec.e_leaf_clusters = cpu_to_le16(len);
  4460. split_rec.e_blkno = cpu_to_le64(start_blkno);
  4461. split_rec.e_flags = rec->e_flags;
  4462. if (new_flags)
  4463. split_rec.e_flags |= new_flags;
  4464. if (clear_flags)
  4465. split_rec.e_flags &= ~clear_flags;
  4466. ret = ocfs2_split_extent(handle, et, left_path,
  4467. index, &split_rec, meta_ac,
  4468. dealloc);
  4469. if (ret)
  4470. mlog_errno(ret);
  4471. out:
  4472. ocfs2_free_path(left_path);
  4473. return ret;
  4474. }
  4475. /*
  4476. * Mark the already-existing extent at cpos as written for len clusters.
  4477. * This removes the unwritten extent flag.
  4478. *
  4479. * If the existing extent is larger than the request, initiate a
  4480. * split. An attempt will be made at merging with adjacent extents.
  4481. *
  4482. * The caller is responsible for passing down meta_ac if we'll need it.
  4483. */
  4484. int ocfs2_mark_extent_written(struct inode *inode,
  4485. struct ocfs2_extent_tree *et,
  4486. handle_t *handle, u32 cpos, u32 len, u32 phys,
  4487. struct ocfs2_alloc_context *meta_ac,
  4488. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4489. {
  4490. int ret;
  4491. trace_ocfs2_mark_extent_written(
  4492. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  4493. cpos, len, phys);
  4494. if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
  4495. ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
  4496. "that are being written to, but the feature bit "
  4497. "is not set in the super block.",
  4498. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  4499. ret = -EROFS;
  4500. goto out;
  4501. }
  4502. /*
  4503. * XXX: This should be fixed up so that we just re-insert the
  4504. * next extent records.
  4505. */
  4506. ocfs2_et_extent_map_truncate(et, 0);
  4507. ret = ocfs2_change_extent_flag(handle, et, cpos,
  4508. len, phys, meta_ac, dealloc,
  4509. 0, OCFS2_EXT_UNWRITTEN);
  4510. if (ret)
  4511. mlog_errno(ret);
  4512. out:
  4513. return ret;
  4514. }
  4515. static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  4516. struct ocfs2_path *path,
  4517. int index, u32 new_range,
  4518. struct ocfs2_alloc_context *meta_ac)
  4519. {
  4520. int ret, depth, credits;
  4521. struct buffer_head *last_eb_bh = NULL;
  4522. struct ocfs2_extent_block *eb;
  4523. struct ocfs2_extent_list *rightmost_el, *el;
  4524. struct ocfs2_extent_rec split_rec;
  4525. struct ocfs2_extent_rec *rec;
  4526. struct ocfs2_insert_type insert;
  4527. /*
  4528. * Setup the record to split before we grow the tree.
  4529. */
  4530. el = path_leaf_el(path);
  4531. rec = &el->l_recs[index];
  4532. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4533. &split_rec, new_range, rec);
  4534. depth = path->p_tree_depth;
  4535. if (depth > 0) {
  4536. ret = ocfs2_read_extent_block(et->et_ci,
  4537. ocfs2_et_get_last_eb_blk(et),
  4538. &last_eb_bh);
  4539. if (ret < 0) {
  4540. mlog_errno(ret);
  4541. goto out;
  4542. }
  4543. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4544. rightmost_el = &eb->h_list;
  4545. } else
  4546. rightmost_el = path_leaf_el(path);
  4547. credits = path->p_tree_depth +
  4548. ocfs2_extend_meta_needed(et->et_root_el);
  4549. ret = ocfs2_extend_trans(handle, credits);
  4550. if (ret) {
  4551. mlog_errno(ret);
  4552. goto out;
  4553. }
  4554. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4555. le16_to_cpu(rightmost_el->l_count)) {
  4556. ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
  4557. meta_ac);
  4558. if (ret) {
  4559. mlog_errno(ret);
  4560. goto out;
  4561. }
  4562. }
  4563. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4564. insert.ins_appending = APPEND_NONE;
  4565. insert.ins_contig = CONTIG_NONE;
  4566. insert.ins_split = SPLIT_RIGHT;
  4567. insert.ins_tree_depth = depth;
  4568. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4569. if (ret)
  4570. mlog_errno(ret);
  4571. out:
  4572. brelse(last_eb_bh);
  4573. return ret;
  4574. }
  4575. static int ocfs2_truncate_rec(handle_t *handle,
  4576. struct ocfs2_extent_tree *et,
  4577. struct ocfs2_path *path, int index,
  4578. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4579. u32 cpos, u32 len)
  4580. {
  4581. int ret;
  4582. u32 left_cpos, rec_range, trunc_range;
  4583. int wants_rotate = 0, is_rightmost_tree_rec = 0;
  4584. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4585. struct ocfs2_path *left_path = NULL;
  4586. struct ocfs2_extent_list *el = path_leaf_el(path);
  4587. struct ocfs2_extent_rec *rec;
  4588. struct ocfs2_extent_block *eb;
  4589. if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
  4590. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4591. if (ret) {
  4592. mlog_errno(ret);
  4593. goto out;
  4594. }
  4595. index--;
  4596. }
  4597. if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
  4598. path->p_tree_depth) {
  4599. /*
  4600. * Check whether this is the rightmost tree record. If
  4601. * we remove all of this record or part of its right
  4602. * edge then an update of the record lengths above it
  4603. * will be required.
  4604. */
  4605. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  4606. if (eb->h_next_leaf_blk == 0)
  4607. is_rightmost_tree_rec = 1;
  4608. }
  4609. rec = &el->l_recs[index];
  4610. if (index == 0 && path->p_tree_depth &&
  4611. le32_to_cpu(rec->e_cpos) == cpos) {
  4612. /*
  4613. * Changing the leftmost offset (via partial or whole
  4614. * record truncate) of an interior (or rightmost) path
  4615. * means we have to update the subtree that is formed
  4616. * by this leaf and the one to it's left.
  4617. *
  4618. * There are two cases we can skip:
  4619. * 1) Path is the leftmost one in our btree.
  4620. * 2) The leaf is rightmost and will be empty after
  4621. * we remove the extent record - the rotate code
  4622. * knows how to update the newly formed edge.
  4623. */
  4624. ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  4625. if (ret) {
  4626. mlog_errno(ret);
  4627. goto out;
  4628. }
  4629. if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
  4630. left_path = ocfs2_new_path_from_path(path);
  4631. if (!left_path) {
  4632. ret = -ENOMEM;
  4633. mlog_errno(ret);
  4634. goto out;
  4635. }
  4636. ret = ocfs2_find_path(et->et_ci, left_path,
  4637. left_cpos);
  4638. if (ret) {
  4639. mlog_errno(ret);
  4640. goto out;
  4641. }
  4642. }
  4643. }
  4644. ret = ocfs2_extend_rotate_transaction(handle, 0,
  4645. handle->h_buffer_credits,
  4646. path);
  4647. if (ret) {
  4648. mlog_errno(ret);
  4649. goto out;
  4650. }
  4651. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  4652. if (ret) {
  4653. mlog_errno(ret);
  4654. goto out;
  4655. }
  4656. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  4657. if (ret) {
  4658. mlog_errno(ret);
  4659. goto out;
  4660. }
  4661. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4662. trunc_range = cpos + len;
  4663. if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
  4664. int next_free;
  4665. memset(rec, 0, sizeof(*rec));
  4666. ocfs2_cleanup_merge(el, index);
  4667. wants_rotate = 1;
  4668. next_free = le16_to_cpu(el->l_next_free_rec);
  4669. if (is_rightmost_tree_rec && next_free > 1) {
  4670. /*
  4671. * We skip the edge update if this path will
  4672. * be deleted by the rotate code.
  4673. */
  4674. rec = &el->l_recs[next_free - 1];
  4675. ocfs2_adjust_rightmost_records(handle, et, path,
  4676. rec);
  4677. }
  4678. } else if (le32_to_cpu(rec->e_cpos) == cpos) {
  4679. /* Remove leftmost portion of the record. */
  4680. le32_add_cpu(&rec->e_cpos, len);
  4681. le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
  4682. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4683. } else if (rec_range == trunc_range) {
  4684. /* Remove rightmost portion of the record */
  4685. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4686. if (is_rightmost_tree_rec)
  4687. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  4688. } else {
  4689. /* Caller should have trapped this. */
  4690. mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
  4691. "(%u, %u)\n",
  4692. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4693. le32_to_cpu(rec->e_cpos),
  4694. le16_to_cpu(rec->e_leaf_clusters), cpos, len);
  4695. BUG();
  4696. }
  4697. if (left_path) {
  4698. int subtree_index;
  4699. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  4700. ocfs2_complete_edge_insert(handle, left_path, path,
  4701. subtree_index);
  4702. }
  4703. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4704. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4705. if (ret) {
  4706. mlog_errno(ret);
  4707. goto out;
  4708. }
  4709. out:
  4710. ocfs2_free_path(left_path);
  4711. return ret;
  4712. }
  4713. int ocfs2_remove_extent(handle_t *handle,
  4714. struct ocfs2_extent_tree *et,
  4715. u32 cpos, u32 len,
  4716. struct ocfs2_alloc_context *meta_ac,
  4717. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4718. {
  4719. int ret, index;
  4720. u32 rec_range, trunc_range;
  4721. struct ocfs2_extent_rec *rec;
  4722. struct ocfs2_extent_list *el;
  4723. struct ocfs2_path *path = NULL;
  4724. /*
  4725. * XXX: Why are we truncating to 0 instead of wherever this
  4726. * affects us?
  4727. */
  4728. ocfs2_et_extent_map_truncate(et, 0);
  4729. path = ocfs2_new_path_from_et(et);
  4730. if (!path) {
  4731. ret = -ENOMEM;
  4732. mlog_errno(ret);
  4733. goto out;
  4734. }
  4735. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4736. if (ret) {
  4737. mlog_errno(ret);
  4738. goto out;
  4739. }
  4740. el = path_leaf_el(path);
  4741. index = ocfs2_search_extent_list(el, cpos);
  4742. if (index == -1) {
  4743. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4744. "Owner %llu has an extent at cpos %u which can no "
  4745. "longer be found.\n",
  4746. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4747. cpos);
  4748. ret = -EROFS;
  4749. goto out;
  4750. }
  4751. /*
  4752. * We have 3 cases of extent removal:
  4753. * 1) Range covers the entire extent rec
  4754. * 2) Range begins or ends on one edge of the extent rec
  4755. * 3) Range is in the middle of the extent rec (no shared edges)
  4756. *
  4757. * For case 1 we remove the extent rec and left rotate to
  4758. * fill the hole.
  4759. *
  4760. * For case 2 we just shrink the existing extent rec, with a
  4761. * tree update if the shrinking edge is also the edge of an
  4762. * extent block.
  4763. *
  4764. * For case 3 we do a right split to turn the extent rec into
  4765. * something case 2 can handle.
  4766. */
  4767. rec = &el->l_recs[index];
  4768. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4769. trunc_range = cpos + len;
  4770. BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
  4771. trace_ocfs2_remove_extent(
  4772. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4773. cpos, len, index, le32_to_cpu(rec->e_cpos),
  4774. ocfs2_rec_clusters(el, rec));
  4775. if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
  4776. ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
  4777. cpos, len);
  4778. if (ret) {
  4779. mlog_errno(ret);
  4780. goto out;
  4781. }
  4782. } else {
  4783. ret = ocfs2_split_tree(handle, et, path, index,
  4784. trunc_range, meta_ac);
  4785. if (ret) {
  4786. mlog_errno(ret);
  4787. goto out;
  4788. }
  4789. /*
  4790. * The split could have manipulated the tree enough to
  4791. * move the record location, so we have to look for it again.
  4792. */
  4793. ocfs2_reinit_path(path, 1);
  4794. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4795. if (ret) {
  4796. mlog_errno(ret);
  4797. goto out;
  4798. }
  4799. el = path_leaf_el(path);
  4800. index = ocfs2_search_extent_list(el, cpos);
  4801. if (index == -1) {
  4802. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4803. "Owner %llu: split at cpos %u lost record.",
  4804. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4805. cpos);
  4806. ret = -EROFS;
  4807. goto out;
  4808. }
  4809. /*
  4810. * Double check our values here. If anything is fishy,
  4811. * it's easier to catch it at the top level.
  4812. */
  4813. rec = &el->l_recs[index];
  4814. rec_range = le32_to_cpu(rec->e_cpos) +
  4815. ocfs2_rec_clusters(el, rec);
  4816. if (rec_range != trunc_range) {
  4817. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4818. "Owner %llu: error after split at cpos %u"
  4819. "trunc len %u, existing record is (%u,%u)",
  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 && (*tl_copy)) {
  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. ocfs2_error(inode->i_sb, "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 = -EROFS;
  6090. goto bail;
  6091. }
  6092. trunc_cpos = le32_to_cpu(rec->e_cpos);
  6093. trunc_len = 0;
  6094. blkno = 0;
  6095. } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
  6096. /*
  6097. * Truncate entire record.
  6098. */
  6099. trunc_cpos = le32_to_cpu(rec->e_cpos);
  6100. trunc_len = ocfs2_rec_clusters(el, rec);
  6101. blkno = le64_to_cpu(rec->e_blkno);
  6102. } else if (range > new_highest_cpos) {
  6103. /*
  6104. * Partial truncate. it also should be
  6105. * the last truncate we're doing.
  6106. */
  6107. trunc_cpos = new_highest_cpos;
  6108. trunc_len = range - new_highest_cpos;
  6109. coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
  6110. blkno = le64_to_cpu(rec->e_blkno) +
  6111. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  6112. } else {
  6113. /*
  6114. * Truncate completed, leave happily.
  6115. */
  6116. status = 0;
  6117. goto bail;
  6118. }
  6119. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  6120. if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) {
  6121. status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
  6122. &ref_tree, NULL);
  6123. if (status) {
  6124. mlog_errno(status);
  6125. goto bail;
  6126. }
  6127. }
  6128. status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  6129. phys_cpos, trunc_len, flags, &dealloc,
  6130. refcount_loc, true);
  6131. if (status < 0) {
  6132. mlog_errno(status);
  6133. goto bail;
  6134. }
  6135. ocfs2_reinit_path(path, 1);
  6136. /*
  6137. * The check above will catch the case where we've truncated
  6138. * away all allocation.
  6139. */
  6140. goto start;
  6141. bail:
  6142. if (ref_tree)
  6143. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  6144. ocfs2_schedule_truncate_log_flush(osb, 1);
  6145. ocfs2_run_deallocs(osb, &dealloc);
  6146. ocfs2_free_path(path);
  6147. return status;
  6148. }
  6149. /*
  6150. * 'start' is inclusive, 'end' is not.
  6151. */
  6152. int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
  6153. unsigned int start, unsigned int end, int trunc)
  6154. {
  6155. int ret;
  6156. unsigned int numbytes;
  6157. handle_t *handle;
  6158. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  6159. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  6160. struct ocfs2_inline_data *idata = &di->id2.i_data;
  6161. if (end > i_size_read(inode))
  6162. end = i_size_read(inode);
  6163. BUG_ON(start > end);
  6164. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  6165. !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
  6166. !ocfs2_supports_inline_data(osb)) {
  6167. ocfs2_error(inode->i_sb,
  6168. "Inline data flags for inode %llu don't agree! "
  6169. "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
  6170. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  6171. le16_to_cpu(di->i_dyn_features),
  6172. OCFS2_I(inode)->ip_dyn_features,
  6173. osb->s_feature_incompat);
  6174. ret = -EROFS;
  6175. goto out;
  6176. }
  6177. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  6178. if (IS_ERR(handle)) {
  6179. ret = PTR_ERR(handle);
  6180. mlog_errno(ret);
  6181. goto out;
  6182. }
  6183. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  6184. OCFS2_JOURNAL_ACCESS_WRITE);
  6185. if (ret) {
  6186. mlog_errno(ret);
  6187. goto out_commit;
  6188. }
  6189. numbytes = end - start;
  6190. memset(idata->id_data + start, 0, numbytes);
  6191. /*
  6192. * No need to worry about the data page here - it's been
  6193. * truncated already and inline data doesn't need it for
  6194. * pushing zero's to disk, so we'll let readpage pick it up
  6195. * later.
  6196. */
  6197. if (trunc) {
  6198. i_size_write(inode, start);
  6199. di->i_size = cpu_to_le64(start);
  6200. }
  6201. inode->i_blocks = ocfs2_inode_sector_count(inode);
  6202. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  6203. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  6204. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  6205. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  6206. ocfs2_journal_dirty(handle, di_bh);
  6207. out_commit:
  6208. ocfs2_commit_trans(osb, handle);
  6209. out:
  6210. return ret;
  6211. }
  6212. static int ocfs2_trim_extent(struct super_block *sb,
  6213. struct ocfs2_group_desc *gd,
  6214. u32 start, u32 count)
  6215. {
  6216. u64 discard, bcount;
  6217. bcount = ocfs2_clusters_to_blocks(sb, count);
  6218. discard = le64_to_cpu(gd->bg_blkno) +
  6219. ocfs2_clusters_to_blocks(sb, start);
  6220. trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
  6221. return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
  6222. }
  6223. static int ocfs2_trim_group(struct super_block *sb,
  6224. struct ocfs2_group_desc *gd,
  6225. u32 start, u32 max, u32 minbits)
  6226. {
  6227. int ret = 0, count = 0, next;
  6228. void *bitmap = gd->bg_bitmap;
  6229. if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
  6230. return 0;
  6231. trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
  6232. start, max, minbits);
  6233. while (start < max) {
  6234. start = ocfs2_find_next_zero_bit(bitmap, max, start);
  6235. if (start >= max)
  6236. break;
  6237. next = ocfs2_find_next_bit(bitmap, max, start);
  6238. if ((next - start) >= minbits) {
  6239. ret = ocfs2_trim_extent(sb, gd,
  6240. start, next - start);
  6241. if (ret < 0) {
  6242. mlog_errno(ret);
  6243. break;
  6244. }
  6245. count += next - start;
  6246. }
  6247. start = next + 1;
  6248. if (fatal_signal_pending(current)) {
  6249. count = -ERESTARTSYS;
  6250. break;
  6251. }
  6252. if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
  6253. break;
  6254. }
  6255. if (ret < 0)
  6256. count = ret;
  6257. return count;
  6258. }
  6259. int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
  6260. {
  6261. struct ocfs2_super *osb = OCFS2_SB(sb);
  6262. u64 start, len, trimmed, first_group, last_group, group;
  6263. int ret, cnt;
  6264. u32 first_bit, last_bit, minlen;
  6265. struct buffer_head *main_bm_bh = NULL;
  6266. struct inode *main_bm_inode = NULL;
  6267. struct buffer_head *gd_bh = NULL;
  6268. struct ocfs2_dinode *main_bm;
  6269. struct ocfs2_group_desc *gd = NULL;
  6270. start = range->start >> osb->s_clustersize_bits;
  6271. len = range->len >> osb->s_clustersize_bits;
  6272. minlen = range->minlen >> osb->s_clustersize_bits;
  6273. if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize)
  6274. return -EINVAL;
  6275. main_bm_inode = ocfs2_get_system_file_inode(osb,
  6276. GLOBAL_BITMAP_SYSTEM_INODE,
  6277. OCFS2_INVALID_SLOT);
  6278. if (!main_bm_inode) {
  6279. ret = -EIO;
  6280. mlog_errno(ret);
  6281. goto out;
  6282. }
  6283. mutex_lock(&main_bm_inode->i_mutex);
  6284. ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
  6285. if (ret < 0) {
  6286. mlog_errno(ret);
  6287. goto out_mutex;
  6288. }
  6289. main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
  6290. if (start >= le32_to_cpu(main_bm->i_clusters)) {
  6291. ret = -EINVAL;
  6292. goto out_unlock;
  6293. }
  6294. len = range->len >> osb->s_clustersize_bits;
  6295. if (start + len > le32_to_cpu(main_bm->i_clusters))
  6296. len = le32_to_cpu(main_bm->i_clusters) - start;
  6297. trace_ocfs2_trim_fs(start, len, minlen);
  6298. /* Determine first and last group to examine based on start and len */
  6299. first_group = ocfs2_which_cluster_group(main_bm_inode, start);
  6300. if (first_group == osb->first_cluster_group_blkno)
  6301. first_bit = start;
  6302. else
  6303. first_bit = start - ocfs2_blocks_to_clusters(sb, first_group);
  6304. last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1);
  6305. last_bit = osb->bitmap_cpg;
  6306. trimmed = 0;
  6307. for (group = first_group; group <= last_group;) {
  6308. if (first_bit + len >= osb->bitmap_cpg)
  6309. last_bit = osb->bitmap_cpg;
  6310. else
  6311. last_bit = first_bit + len;
  6312. ret = ocfs2_read_group_descriptor(main_bm_inode,
  6313. main_bm, group,
  6314. &gd_bh);
  6315. if (ret < 0) {
  6316. mlog_errno(ret);
  6317. break;
  6318. }
  6319. gd = (struct ocfs2_group_desc *)gd_bh->b_data;
  6320. cnt = ocfs2_trim_group(sb, gd, first_bit, last_bit, minlen);
  6321. brelse(gd_bh);
  6322. gd_bh = NULL;
  6323. if (cnt < 0) {
  6324. ret = cnt;
  6325. mlog_errno(ret);
  6326. break;
  6327. }
  6328. trimmed += cnt;
  6329. len -= osb->bitmap_cpg - first_bit;
  6330. first_bit = 0;
  6331. if (group == osb->first_cluster_group_blkno)
  6332. group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
  6333. else
  6334. group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
  6335. }
  6336. range->len = trimmed * sb->s_blocksize;
  6337. out_unlock:
  6338. ocfs2_inode_unlock(main_bm_inode, 0);
  6339. brelse(main_bm_bh);
  6340. out_mutex:
  6341. mutex_unlock(&main_bm_inode->i_mutex);
  6342. iput(main_bm_inode);
  6343. out:
  6344. return ret;
  6345. }