alloc.c 166 KB

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