alloc.c 191 KB

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