alloc.c 181 KB

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