alloc.c 186 KB

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