alloc.c 168 KB

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