alloc.c 181 KB

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