alloc.c 179 KB

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