xfs_bmap.c 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_dmapi.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_itable.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_extfree_item.h"
  45. #include "xfs_alloc.h"
  46. #include "xfs_bmap.h"
  47. #include "xfs_rtalloc.h"
  48. #include "xfs_error.h"
  49. #include "xfs_dir_leaf.h"
  50. #include "xfs_attr_leaf.h"
  51. #include "xfs_rw.h"
  52. #include "xfs_quota.h"
  53. #include "xfs_trans_space.h"
  54. #include "xfs_buf_item.h"
  55. #ifdef DEBUG
  56. STATIC void
  57. xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
  58. #endif
  59. kmem_zone_t *xfs_bmap_free_item_zone;
  60. /*
  61. * Prototypes for internal bmap routines.
  62. */
  63. /*
  64. * Called from xfs_bmap_add_attrfork to handle extents format files.
  65. */
  66. STATIC int /* error */
  67. xfs_bmap_add_attrfork_extents(
  68. xfs_trans_t *tp, /* transaction pointer */
  69. xfs_inode_t *ip, /* incore inode pointer */
  70. xfs_fsblock_t *firstblock, /* first block allocated */
  71. xfs_bmap_free_t *flist, /* blocks to free at commit */
  72. int *flags); /* inode logging flags */
  73. /*
  74. * Called from xfs_bmap_add_attrfork to handle local format files.
  75. */
  76. STATIC int /* error */
  77. xfs_bmap_add_attrfork_local(
  78. xfs_trans_t *tp, /* transaction pointer */
  79. xfs_inode_t *ip, /* incore inode pointer */
  80. xfs_fsblock_t *firstblock, /* first block allocated */
  81. xfs_bmap_free_t *flist, /* blocks to free at commit */
  82. int *flags); /* inode logging flags */
  83. /*
  84. * Called by xfs_bmapi to update extent list structure and the btree
  85. * after allocating space (or doing a delayed allocation).
  86. */
  87. STATIC int /* error */
  88. xfs_bmap_add_extent(
  89. xfs_inode_t *ip, /* incore inode pointer */
  90. xfs_extnum_t idx, /* extent number to update/insert */
  91. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  92. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  93. xfs_fsblock_t *first, /* pointer to firstblock variable */
  94. xfs_bmap_free_t *flist, /* list of extents to be freed */
  95. int *logflagsp, /* inode logging flags */
  96. int whichfork, /* data or attr fork */
  97. int rsvd); /* OK to allocate reserved blocks */
  98. /*
  99. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  100. * allocation to a real allocation.
  101. */
  102. STATIC int /* error */
  103. xfs_bmap_add_extent_delay_real(
  104. xfs_inode_t *ip, /* incore inode pointer */
  105. xfs_extnum_t idx, /* extent number to update/insert */
  106. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  107. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  108. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  109. xfs_fsblock_t *first, /* pointer to firstblock variable */
  110. xfs_bmap_free_t *flist, /* list of extents to be freed */
  111. int *logflagsp, /* inode logging flags */
  112. int rsvd); /* OK to allocate reserved blocks */
  113. /*
  114. * Called by xfs_bmap_add_extent to handle cases converting a hole
  115. * to a delayed allocation.
  116. */
  117. STATIC int /* error */
  118. xfs_bmap_add_extent_hole_delay(
  119. xfs_inode_t *ip, /* incore inode pointer */
  120. xfs_extnum_t idx, /* extent number to update/insert */
  121. xfs_btree_cur_t *cur, /* if null, not a btree */
  122. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  123. int *logflagsp,/* inode logging flags */
  124. int rsvd); /* OK to allocate reserved blocks */
  125. /*
  126. * Called by xfs_bmap_add_extent to handle cases converting a hole
  127. * to a real allocation.
  128. */
  129. STATIC int /* error */
  130. xfs_bmap_add_extent_hole_real(
  131. xfs_inode_t *ip, /* incore inode pointer */
  132. xfs_extnum_t idx, /* extent number to update/insert */
  133. xfs_btree_cur_t *cur, /* if null, not a btree */
  134. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  135. int *logflagsp, /* inode logging flags */
  136. int whichfork); /* data or attr fork */
  137. /*
  138. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  139. * allocation to a real allocation or vice versa.
  140. */
  141. STATIC int /* error */
  142. xfs_bmap_add_extent_unwritten_real(
  143. xfs_inode_t *ip, /* incore inode pointer */
  144. xfs_extnum_t idx, /* extent number to update/insert */
  145. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  146. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  147. int *logflagsp); /* inode logging flags */
  148. /*
  149. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  150. * It figures out where to ask the underlying allocator to put the new extent.
  151. */
  152. STATIC int /* error */
  153. xfs_bmap_alloc(
  154. xfs_bmalloca_t *ap); /* bmap alloc argument struct */
  155. /*
  156. * Transform a btree format file with only one leaf node, where the
  157. * extents list will fit in the inode, into an extents format file.
  158. * Since the extent list is already in-core, all we have to do is
  159. * give up the space for the btree root and pitch the leaf block.
  160. */
  161. STATIC int /* error */
  162. xfs_bmap_btree_to_extents(
  163. xfs_trans_t *tp, /* transaction pointer */
  164. xfs_inode_t *ip, /* incore inode pointer */
  165. xfs_btree_cur_t *cur, /* btree cursor */
  166. int *logflagsp, /* inode logging flags */
  167. int whichfork); /* data or attr fork */
  168. #ifdef DEBUG
  169. /*
  170. * Check that the extents list for the inode ip is in the right order.
  171. */
  172. STATIC void
  173. xfs_bmap_check_extents(
  174. xfs_inode_t *ip, /* incore inode pointer */
  175. int whichfork); /* data or attr fork */
  176. #endif
  177. /*
  178. * Called by xfs_bmapi to update extent list structure and the btree
  179. * after removing space (or undoing a delayed allocation).
  180. */
  181. STATIC int /* error */
  182. xfs_bmap_del_extent(
  183. xfs_inode_t *ip, /* incore inode pointer */
  184. xfs_trans_t *tp, /* current trans pointer */
  185. xfs_extnum_t idx, /* extent number to update/insert */
  186. xfs_bmap_free_t *flist, /* list of extents to be freed */
  187. xfs_btree_cur_t *cur, /* if null, not a btree */
  188. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  189. int *logflagsp,/* inode logging flags */
  190. int whichfork, /* data or attr fork */
  191. int rsvd); /* OK to allocate reserved blocks */
  192. /*
  193. * Remove the entry "free" from the free item list. Prev points to the
  194. * previous entry, unless "free" is the head of the list.
  195. */
  196. STATIC void
  197. xfs_bmap_del_free(
  198. xfs_bmap_free_t *flist, /* free item list header */
  199. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  200. xfs_bmap_free_item_t *free); /* list item to be freed */
  201. /*
  202. * Remove count entries from the extents array for inode "ip", starting
  203. * at index "idx". Copies the remaining items down over the deleted ones,
  204. * and gives back the excess memory.
  205. */
  206. STATIC void
  207. xfs_bmap_delete_exlist(
  208. xfs_inode_t *ip, /* incode inode pointer */
  209. xfs_extnum_t idx, /* starting delete index */
  210. xfs_extnum_t count, /* count of items to delete */
  211. int whichfork); /* data or attr fork */
  212. /*
  213. * Convert an extents-format file into a btree-format file.
  214. * The new file will have a root block (in the inode) and a single child block.
  215. */
  216. STATIC int /* error */
  217. xfs_bmap_extents_to_btree(
  218. xfs_trans_t *tp, /* transaction pointer */
  219. xfs_inode_t *ip, /* incore inode pointer */
  220. xfs_fsblock_t *firstblock, /* first-block-allocated */
  221. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  222. xfs_btree_cur_t **curp, /* cursor returned to caller */
  223. int wasdel, /* converting a delayed alloc */
  224. int *logflagsp, /* inode logging flags */
  225. int whichfork); /* data or attr fork */
  226. /*
  227. * Insert new item(s) in the extent list for inode "ip".
  228. * Count new items are inserted at offset idx.
  229. */
  230. STATIC void
  231. xfs_bmap_insert_exlist(
  232. xfs_inode_t *ip, /* incore inode pointer */
  233. xfs_extnum_t idx, /* starting index of new items */
  234. xfs_extnum_t count, /* number of inserted items */
  235. xfs_bmbt_irec_t *new, /* items to insert */
  236. int whichfork); /* data or attr fork */
  237. /*
  238. * Convert a local file to an extents file.
  239. * This code is sort of bogus, since the file data needs to get
  240. * logged so it won't be lost. The bmap-level manipulations are ok, though.
  241. */
  242. STATIC int /* error */
  243. xfs_bmap_local_to_extents(
  244. xfs_trans_t *tp, /* transaction pointer */
  245. xfs_inode_t *ip, /* incore inode pointer */
  246. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  247. xfs_extlen_t total, /* total blocks needed by transaction */
  248. int *logflagsp, /* inode logging flags */
  249. int whichfork); /* data or attr fork */
  250. /*
  251. * Search the extents list for the inode, for the extent containing bno.
  252. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  253. * *eofp will be set, and *prevp will contain the last entry (null if none).
  254. * Else, *lastxp will be set to the index of the found
  255. * entry; *gotp will contain the entry.
  256. */
  257. STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
  258. xfs_bmap_search_extents(
  259. xfs_inode_t *ip, /* incore inode pointer */
  260. xfs_fileoff_t bno, /* block number searched for */
  261. int whichfork, /* data or attr fork */
  262. int *eofp, /* out: end of file found */
  263. xfs_extnum_t *lastxp, /* out: last extent index */
  264. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  265. xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
  266. /*
  267. * Check the last inode extent to determine whether this allocation will result
  268. * in blocks being allocated at the end of the file. When we allocate new data
  269. * blocks at the end of the file which do not start at the previous data block,
  270. * we will try to align the new blocks at stripe unit boundaries.
  271. */
  272. STATIC int /* error */
  273. xfs_bmap_isaeof(
  274. xfs_inode_t *ip, /* incore inode pointer */
  275. xfs_fileoff_t off, /* file offset in fsblocks */
  276. int whichfork, /* data or attribute fork */
  277. char *aeof); /* return value */
  278. #ifdef XFS_BMAP_TRACE
  279. /*
  280. * Add a bmap trace buffer entry. Base routine for the others.
  281. */
  282. STATIC void
  283. xfs_bmap_trace_addentry(
  284. int opcode, /* operation */
  285. char *fname, /* function name */
  286. char *desc, /* operation description */
  287. xfs_inode_t *ip, /* incore inode pointer */
  288. xfs_extnum_t idx, /* index of entry(ies) */
  289. xfs_extnum_t cnt, /* count of entries, 1 or 2 */
  290. xfs_bmbt_rec_t *r1, /* first record */
  291. xfs_bmbt_rec_t *r2, /* second record or null */
  292. int whichfork); /* data or attr fork */
  293. /*
  294. * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
  295. */
  296. STATIC void
  297. xfs_bmap_trace_delete(
  298. char *fname, /* function name */
  299. char *desc, /* operation description */
  300. xfs_inode_t *ip, /* incore inode pointer */
  301. xfs_extnum_t idx, /* index of entry(entries) deleted */
  302. xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
  303. int whichfork); /* data or attr fork */
  304. /*
  305. * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
  306. * reading in the extents list from the disk (in the btree).
  307. */
  308. STATIC void
  309. xfs_bmap_trace_insert(
  310. char *fname, /* function name */
  311. char *desc, /* operation description */
  312. xfs_inode_t *ip, /* incore inode pointer */
  313. xfs_extnum_t idx, /* index of entry(entries) inserted */
  314. xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
  315. xfs_bmbt_irec_t *r1, /* inserted record 1 */
  316. xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
  317. int whichfork); /* data or attr fork */
  318. /*
  319. * Add bmap trace entry after updating an extent list entry in place.
  320. */
  321. STATIC void
  322. xfs_bmap_trace_post_update(
  323. char *fname, /* function name */
  324. char *desc, /* operation description */
  325. xfs_inode_t *ip, /* incore inode pointer */
  326. xfs_extnum_t idx, /* index of entry updated */
  327. int whichfork); /* data or attr fork */
  328. /*
  329. * Add bmap trace entry prior to updating an extent list entry in place.
  330. */
  331. STATIC void
  332. xfs_bmap_trace_pre_update(
  333. char *fname, /* function name */
  334. char *desc, /* operation description */
  335. xfs_inode_t *ip, /* incore inode pointer */
  336. xfs_extnum_t idx, /* index of entry to be updated */
  337. int whichfork); /* data or attr fork */
  338. #else
  339. #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
  340. #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
  341. #define xfs_bmap_trace_post_update(f,d,ip,i,w)
  342. #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
  343. #endif /* XFS_BMAP_TRACE */
  344. /*
  345. * Compute the worst-case number of indirect blocks that will be used
  346. * for ip's delayed extent of length "len".
  347. */
  348. STATIC xfs_filblks_t
  349. xfs_bmap_worst_indlen(
  350. xfs_inode_t *ip, /* incore inode pointer */
  351. xfs_filblks_t len); /* delayed extent length */
  352. #ifdef DEBUG
  353. /*
  354. * Perform various validation checks on the values being returned
  355. * from xfs_bmapi().
  356. */
  357. STATIC void
  358. xfs_bmap_validate_ret(
  359. xfs_fileoff_t bno,
  360. xfs_filblks_t len,
  361. int flags,
  362. xfs_bmbt_irec_t *mval,
  363. int nmap,
  364. int ret_nmap);
  365. #else
  366. #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
  367. #endif /* DEBUG */
  368. #if defined(XFS_RW_TRACE)
  369. STATIC void
  370. xfs_bunmap_trace(
  371. xfs_inode_t *ip,
  372. xfs_fileoff_t bno,
  373. xfs_filblks_t len,
  374. int flags,
  375. inst_t *ra);
  376. #else
  377. #define xfs_bunmap_trace(ip, bno, len, flags, ra)
  378. #endif /* XFS_RW_TRACE */
  379. STATIC int
  380. xfs_bmap_count_tree(
  381. xfs_mount_t *mp,
  382. xfs_trans_t *tp,
  383. xfs_fsblock_t blockno,
  384. int levelin,
  385. int *count);
  386. STATIC int
  387. xfs_bmap_count_leaves(
  388. xfs_bmbt_rec_t *frp,
  389. int numrecs,
  390. int *count);
  391. STATIC int
  392. xfs_bmap_disk_count_leaves(
  393. xfs_bmbt_rec_t *frp,
  394. int numrecs,
  395. int *count);
  396. /*
  397. * Bmap internal routines.
  398. */
  399. /*
  400. * Called from xfs_bmap_add_attrfork to handle btree format files.
  401. */
  402. STATIC int /* error */
  403. xfs_bmap_add_attrfork_btree(
  404. xfs_trans_t *tp, /* transaction pointer */
  405. xfs_inode_t *ip, /* incore inode pointer */
  406. xfs_fsblock_t *firstblock, /* first block allocated */
  407. xfs_bmap_free_t *flist, /* blocks to free at commit */
  408. int *flags) /* inode logging flags */
  409. {
  410. xfs_btree_cur_t *cur; /* btree cursor */
  411. int error; /* error return value */
  412. xfs_mount_t *mp; /* file system mount struct */
  413. int stat; /* newroot status */
  414. mp = ip->i_mount;
  415. if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
  416. *flags |= XFS_ILOG_DBROOT;
  417. else {
  418. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  419. XFS_DATA_FORK);
  420. cur->bc_private.b.flist = flist;
  421. cur->bc_private.b.firstblock = *firstblock;
  422. if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
  423. goto error0;
  424. ASSERT(stat == 1); /* must be at least one entry */
  425. if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
  426. goto error0;
  427. if (stat == 0) {
  428. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  429. return XFS_ERROR(ENOSPC);
  430. }
  431. *firstblock = cur->bc_private.b.firstblock;
  432. cur->bc_private.b.allocated = 0;
  433. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  434. }
  435. return 0;
  436. error0:
  437. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  438. return error;
  439. }
  440. /*
  441. * Called from xfs_bmap_add_attrfork to handle extents format files.
  442. */
  443. STATIC int /* error */
  444. xfs_bmap_add_attrfork_extents(
  445. xfs_trans_t *tp, /* transaction pointer */
  446. xfs_inode_t *ip, /* incore inode pointer */
  447. xfs_fsblock_t *firstblock, /* first block allocated */
  448. xfs_bmap_free_t *flist, /* blocks to free at commit */
  449. int *flags) /* inode logging flags */
  450. {
  451. xfs_btree_cur_t *cur; /* bmap btree cursor */
  452. int error; /* error return value */
  453. if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
  454. return 0;
  455. cur = NULL;
  456. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
  457. flags, XFS_DATA_FORK);
  458. if (cur) {
  459. cur->bc_private.b.allocated = 0;
  460. xfs_btree_del_cursor(cur,
  461. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  462. }
  463. return error;
  464. }
  465. /*
  466. * Called from xfs_bmap_add_attrfork to handle local format files.
  467. */
  468. STATIC int /* error */
  469. xfs_bmap_add_attrfork_local(
  470. xfs_trans_t *tp, /* transaction pointer */
  471. xfs_inode_t *ip, /* incore inode pointer */
  472. xfs_fsblock_t *firstblock, /* first block allocated */
  473. xfs_bmap_free_t *flist, /* blocks to free at commit */
  474. int *flags) /* inode logging flags */
  475. {
  476. xfs_da_args_t dargs; /* args for dir/attr code */
  477. int error; /* error return value */
  478. xfs_mount_t *mp; /* mount structure pointer */
  479. if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
  480. return 0;
  481. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  482. mp = ip->i_mount;
  483. memset(&dargs, 0, sizeof(dargs));
  484. dargs.dp = ip;
  485. dargs.firstblock = firstblock;
  486. dargs.flist = flist;
  487. dargs.total = mp->m_dirblkfsbs;
  488. dargs.whichfork = XFS_DATA_FORK;
  489. dargs.trans = tp;
  490. error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
  491. } else
  492. error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
  493. XFS_DATA_FORK);
  494. return error;
  495. }
  496. /*
  497. * Called by xfs_bmapi to update extent list structure and the btree
  498. * after allocating space (or doing a delayed allocation).
  499. */
  500. STATIC int /* error */
  501. xfs_bmap_add_extent(
  502. xfs_inode_t *ip, /* incore inode pointer */
  503. xfs_extnum_t idx, /* extent number to update/insert */
  504. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  505. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  506. xfs_fsblock_t *first, /* pointer to firstblock variable */
  507. xfs_bmap_free_t *flist, /* list of extents to be freed */
  508. int *logflagsp, /* inode logging flags */
  509. int whichfork, /* data or attr fork */
  510. int rsvd) /* OK to use reserved data blocks */
  511. {
  512. xfs_btree_cur_t *cur; /* btree cursor or null */
  513. xfs_filblks_t da_new; /* new count del alloc blocks used */
  514. xfs_filblks_t da_old; /* old count del alloc blocks used */
  515. int error; /* error return value */
  516. #ifdef XFS_BMAP_TRACE
  517. static char fname[] = "xfs_bmap_add_extent";
  518. #endif
  519. xfs_ifork_t *ifp; /* inode fork ptr */
  520. int logflags; /* returned value */
  521. xfs_extnum_t nextents; /* number of extents in file now */
  522. XFS_STATS_INC(xs_add_exlist);
  523. cur = *curp;
  524. ifp = XFS_IFORK_PTR(ip, whichfork);
  525. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  526. ASSERT(idx <= nextents);
  527. da_old = da_new = 0;
  528. error = 0;
  529. /*
  530. * This is the first extent added to a new/empty file.
  531. * Special case this one, so other routines get to assume there are
  532. * already extents in the list.
  533. */
  534. if (nextents == 0) {
  535. xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
  536. NULL, whichfork);
  537. xfs_bmap_insert_exlist(ip, 0, 1, new, whichfork);
  538. ASSERT(cur == NULL);
  539. ifp->if_lastex = 0;
  540. if (!ISNULLSTARTBLOCK(new->br_startblock)) {
  541. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  542. logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  543. } else
  544. logflags = 0;
  545. }
  546. /*
  547. * Any kind of new delayed allocation goes here.
  548. */
  549. else if (ISNULLSTARTBLOCK(new->br_startblock)) {
  550. if (cur)
  551. ASSERT((cur->bc_private.b.flags &
  552. XFS_BTCUR_BPRV_WASDEL) == 0);
  553. if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
  554. &logflags, rsvd)))
  555. goto done;
  556. }
  557. /*
  558. * Real allocation off the end of the file.
  559. */
  560. else if (idx == nextents) {
  561. if (cur)
  562. ASSERT((cur->bc_private.b.flags &
  563. XFS_BTCUR_BPRV_WASDEL) == 0);
  564. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
  565. &logflags, whichfork)))
  566. goto done;
  567. } else {
  568. xfs_bmbt_irec_t prev; /* old extent at offset idx */
  569. /*
  570. * Get the record referred to by idx.
  571. */
  572. xfs_bmbt_get_all(&ifp->if_u1.if_extents[idx], &prev);
  573. /*
  574. * If it's a real allocation record, and the new allocation ends
  575. * after the start of the referred to record, then we're filling
  576. * in a delayed or unwritten allocation with a real one, or
  577. * converting real back to unwritten.
  578. */
  579. if (!ISNULLSTARTBLOCK(new->br_startblock) &&
  580. new->br_startoff + new->br_blockcount > prev.br_startoff) {
  581. if (prev.br_state != XFS_EXT_UNWRITTEN &&
  582. ISNULLSTARTBLOCK(prev.br_startblock)) {
  583. da_old = STARTBLOCKVAL(prev.br_startblock);
  584. if (cur)
  585. ASSERT(cur->bc_private.b.flags &
  586. XFS_BTCUR_BPRV_WASDEL);
  587. if ((error = xfs_bmap_add_extent_delay_real(ip,
  588. idx, &cur, new, &da_new, first, flist,
  589. &logflags, rsvd)))
  590. goto done;
  591. } else if (new->br_state == XFS_EXT_NORM) {
  592. ASSERT(new->br_state == XFS_EXT_NORM);
  593. if ((error = xfs_bmap_add_extent_unwritten_real(
  594. ip, idx, &cur, new, &logflags)))
  595. goto done;
  596. } else {
  597. ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
  598. if ((error = xfs_bmap_add_extent_unwritten_real(
  599. ip, idx, &cur, new, &logflags)))
  600. goto done;
  601. }
  602. ASSERT(*curp == cur || *curp == NULL);
  603. }
  604. /*
  605. * Otherwise we're filling in a hole with an allocation.
  606. */
  607. else {
  608. if (cur)
  609. ASSERT((cur->bc_private.b.flags &
  610. XFS_BTCUR_BPRV_WASDEL) == 0);
  611. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
  612. new, &logflags, whichfork)))
  613. goto done;
  614. }
  615. }
  616. ASSERT(*curp == cur || *curp == NULL);
  617. /*
  618. * Convert to a btree if necessary.
  619. */
  620. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  621. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  622. int tmp_logflags; /* partial log flag return val */
  623. ASSERT(cur == NULL);
  624. error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
  625. flist, &cur, da_old > 0, &tmp_logflags, whichfork);
  626. logflags |= tmp_logflags;
  627. if (error)
  628. goto done;
  629. }
  630. /*
  631. * Adjust for changes in reserved delayed indirect blocks.
  632. * Nothing to do for disk quotas here.
  633. */
  634. if (da_old || da_new) {
  635. xfs_filblks_t nblks;
  636. nblks = da_new;
  637. if (cur)
  638. nblks += cur->bc_private.b.allocated;
  639. ASSERT(nblks <= da_old);
  640. if (nblks < da_old)
  641. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  642. (int)(da_old - nblks), rsvd);
  643. }
  644. /*
  645. * Clear out the allocated field, done with it now in any case.
  646. */
  647. if (cur) {
  648. cur->bc_private.b.allocated = 0;
  649. *curp = cur;
  650. }
  651. done:
  652. #ifdef DEBUG
  653. if (!error)
  654. xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
  655. #endif
  656. *logflagsp = logflags;
  657. return error;
  658. }
  659. /*
  660. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  661. * allocation to a real allocation.
  662. */
  663. STATIC int /* error */
  664. xfs_bmap_add_extent_delay_real(
  665. xfs_inode_t *ip, /* incore inode pointer */
  666. xfs_extnum_t idx, /* extent number to update/insert */
  667. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  668. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  669. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  670. xfs_fsblock_t *first, /* pointer to firstblock variable */
  671. xfs_bmap_free_t *flist, /* list of extents to be freed */
  672. int *logflagsp, /* inode logging flags */
  673. int rsvd) /* OK to use reserved data block allocation */
  674. {
  675. xfs_bmbt_rec_t *base; /* base of extent entry list */
  676. xfs_btree_cur_t *cur; /* btree cursor */
  677. int diff; /* temp value */
  678. xfs_bmbt_rec_t *ep; /* extent entry for idx */
  679. int error; /* error return value */
  680. #ifdef XFS_BMAP_TRACE
  681. static char fname[] = "xfs_bmap_add_extent_delay_real";
  682. #endif
  683. int i; /* temp state */
  684. xfs_fileoff_t new_endoff; /* end offset of new entry */
  685. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  686. /* left is 0, right is 1, prev is 2 */
  687. int rval=0; /* return value (logging flags) */
  688. int state = 0;/* state bits, accessed thru macros */
  689. xfs_filblks_t temp; /* value for dnew calculations */
  690. xfs_filblks_t temp2; /* value for dnew calculations */
  691. int tmp_rval; /* partial logging flags */
  692. enum { /* bit number definitions for state */
  693. LEFT_CONTIG, RIGHT_CONTIG,
  694. LEFT_FILLING, RIGHT_FILLING,
  695. LEFT_DELAY, RIGHT_DELAY,
  696. LEFT_VALID, RIGHT_VALID
  697. };
  698. #define LEFT r[0]
  699. #define RIGHT r[1]
  700. #define PREV r[2]
  701. #define MASK(b) (1 << (b))
  702. #define MASK2(a,b) (MASK(a) | MASK(b))
  703. #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
  704. #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
  705. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  706. #define STATE_TEST(b) (state & MASK(b))
  707. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  708. ((state &= ~MASK(b)), 0))
  709. #define SWITCH_STATE \
  710. (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
  711. /*
  712. * Set up a bunch of variables to make the tests simpler.
  713. */
  714. cur = *curp;
  715. base = ip->i_df.if_u1.if_extents;
  716. ep = &base[idx];
  717. xfs_bmbt_get_all(ep, &PREV);
  718. new_endoff = new->br_startoff + new->br_blockcount;
  719. ASSERT(PREV.br_startoff <= new->br_startoff);
  720. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  721. /*
  722. * Set flags determining what part of the previous delayed allocation
  723. * extent is being replaced by a real allocation.
  724. */
  725. STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
  726. STATE_SET(RIGHT_FILLING,
  727. PREV.br_startoff + PREV.br_blockcount == new_endoff);
  728. /*
  729. * Check and set flags if this segment has a left neighbor.
  730. * Don't set contiguous if the combined extent would be too large.
  731. */
  732. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  733. xfs_bmbt_get_all(ep - 1, &LEFT);
  734. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
  735. }
  736. STATE_SET(LEFT_CONTIG,
  737. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  738. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  739. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  740. LEFT.br_state == new->br_state &&
  741. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  742. /*
  743. * Check and set flags if this segment has a right neighbor.
  744. * Don't set contiguous if the combined extent would be too large.
  745. * Also check for all-three-contiguous being too large.
  746. */
  747. if (STATE_SET_TEST(RIGHT_VALID,
  748. idx <
  749. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
  750. xfs_bmbt_get_all(ep + 1, &RIGHT);
  751. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
  752. }
  753. STATE_SET(RIGHT_CONTIG,
  754. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  755. new_endoff == RIGHT.br_startoff &&
  756. new->br_startblock + new->br_blockcount ==
  757. RIGHT.br_startblock &&
  758. new->br_state == RIGHT.br_state &&
  759. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  760. ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
  761. MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
  762. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  763. <= MAXEXTLEN));
  764. error = 0;
  765. /*
  766. * Switch out based on the FILLING and CONTIG state bits.
  767. */
  768. switch (SWITCH_STATE) {
  769. case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  770. /*
  771. * Filling in all of a previously delayed allocation extent.
  772. * The left and right neighbors are both contiguous with new.
  773. */
  774. xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  775. XFS_DATA_FORK);
  776. xfs_bmbt_set_blockcount(ep - 1,
  777. LEFT.br_blockcount + PREV.br_blockcount +
  778. RIGHT.br_blockcount);
  779. xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  780. XFS_DATA_FORK);
  781. xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
  782. XFS_DATA_FORK);
  783. xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
  784. ip->i_df.if_lastex = idx - 1;
  785. ip->i_d.di_nextents--;
  786. if (cur == NULL)
  787. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  788. else {
  789. rval = XFS_ILOG_CORE;
  790. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  791. RIGHT.br_startblock,
  792. RIGHT.br_blockcount, &i)))
  793. goto done;
  794. ASSERT(i == 1);
  795. if ((error = xfs_bmbt_delete(cur, &i)))
  796. goto done;
  797. ASSERT(i == 1);
  798. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  799. goto done;
  800. ASSERT(i == 1);
  801. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  802. LEFT.br_startblock,
  803. LEFT.br_blockcount +
  804. PREV.br_blockcount +
  805. RIGHT.br_blockcount, LEFT.br_state)))
  806. goto done;
  807. }
  808. *dnew = 0;
  809. break;
  810. case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
  811. /*
  812. * Filling in all of a previously delayed allocation extent.
  813. * The left neighbor is contiguous, the right is not.
  814. */
  815. xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
  816. XFS_DATA_FORK);
  817. xfs_bmbt_set_blockcount(ep - 1,
  818. LEFT.br_blockcount + PREV.br_blockcount);
  819. xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
  820. XFS_DATA_FORK);
  821. ip->i_df.if_lastex = idx - 1;
  822. xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
  823. XFS_DATA_FORK);
  824. xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
  825. if (cur == NULL)
  826. rval = XFS_ILOG_DEXT;
  827. else {
  828. rval = 0;
  829. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  830. LEFT.br_startblock, LEFT.br_blockcount,
  831. &i)))
  832. goto done;
  833. ASSERT(i == 1);
  834. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  835. LEFT.br_startblock,
  836. LEFT.br_blockcount +
  837. PREV.br_blockcount, LEFT.br_state)))
  838. goto done;
  839. }
  840. *dnew = 0;
  841. break;
  842. case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
  843. /*
  844. * Filling in all of a previously delayed allocation extent.
  845. * The right neighbor is contiguous, the left is not.
  846. */
  847. xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
  848. XFS_DATA_FORK);
  849. xfs_bmbt_set_startblock(ep, new->br_startblock);
  850. xfs_bmbt_set_blockcount(ep,
  851. PREV.br_blockcount + RIGHT.br_blockcount);
  852. xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
  853. XFS_DATA_FORK);
  854. ip->i_df.if_lastex = idx;
  855. xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
  856. XFS_DATA_FORK);
  857. xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
  858. if (cur == NULL)
  859. rval = XFS_ILOG_DEXT;
  860. else {
  861. rval = 0;
  862. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  863. RIGHT.br_startblock,
  864. RIGHT.br_blockcount, &i)))
  865. goto done;
  866. ASSERT(i == 1);
  867. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  868. new->br_startblock,
  869. PREV.br_blockcount +
  870. RIGHT.br_blockcount, PREV.br_state)))
  871. goto done;
  872. }
  873. *dnew = 0;
  874. break;
  875. case MASK2(LEFT_FILLING, RIGHT_FILLING):
  876. /*
  877. * Filling in all of a previously delayed allocation extent.
  878. * Neither the left nor right neighbors are contiguous with
  879. * the new one.
  880. */
  881. xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
  882. XFS_DATA_FORK);
  883. xfs_bmbt_set_startblock(ep, new->br_startblock);
  884. xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
  885. XFS_DATA_FORK);
  886. ip->i_df.if_lastex = idx;
  887. ip->i_d.di_nextents++;
  888. if (cur == NULL)
  889. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  890. else {
  891. rval = XFS_ILOG_CORE;
  892. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  893. new->br_startblock, new->br_blockcount,
  894. &i)))
  895. goto done;
  896. ASSERT(i == 0);
  897. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  898. if ((error = xfs_bmbt_insert(cur, &i)))
  899. goto done;
  900. ASSERT(i == 1);
  901. }
  902. *dnew = 0;
  903. break;
  904. case MASK2(LEFT_FILLING, LEFT_CONTIG):
  905. /*
  906. * Filling in the first part of a previous delayed allocation.
  907. * The left neighbor is contiguous.
  908. */
  909. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
  910. XFS_DATA_FORK);
  911. xfs_bmbt_set_blockcount(ep - 1,
  912. LEFT.br_blockcount + new->br_blockcount);
  913. xfs_bmbt_set_startoff(ep,
  914. PREV.br_startoff + new->br_blockcount);
  915. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
  916. XFS_DATA_FORK);
  917. temp = PREV.br_blockcount - new->br_blockcount;
  918. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
  919. XFS_DATA_FORK);
  920. xfs_bmbt_set_blockcount(ep, temp);
  921. ip->i_df.if_lastex = idx - 1;
  922. if (cur == NULL)
  923. rval = XFS_ILOG_DEXT;
  924. else {
  925. rval = 0;
  926. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  927. LEFT.br_startblock, LEFT.br_blockcount,
  928. &i)))
  929. goto done;
  930. ASSERT(i == 1);
  931. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  932. LEFT.br_startblock,
  933. LEFT.br_blockcount +
  934. new->br_blockcount,
  935. LEFT.br_state)))
  936. goto done;
  937. }
  938. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  939. STARTBLOCKVAL(PREV.br_startblock));
  940. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  941. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
  942. XFS_DATA_FORK);
  943. *dnew = temp;
  944. break;
  945. case MASK(LEFT_FILLING):
  946. /*
  947. * Filling in the first part of a previous delayed allocation.
  948. * The left neighbor is not contiguous.
  949. */
  950. xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  951. xfs_bmbt_set_startoff(ep, new_endoff);
  952. temp = PREV.br_blockcount - new->br_blockcount;
  953. xfs_bmbt_set_blockcount(ep, temp);
  954. xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
  955. XFS_DATA_FORK);
  956. xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
  957. ip->i_df.if_lastex = idx;
  958. ip->i_d.di_nextents++;
  959. if (cur == NULL)
  960. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  961. else {
  962. rval = XFS_ILOG_CORE;
  963. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  964. new->br_startblock, new->br_blockcount,
  965. &i)))
  966. goto done;
  967. ASSERT(i == 0);
  968. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  969. if ((error = xfs_bmbt_insert(cur, &i)))
  970. goto done;
  971. ASSERT(i == 1);
  972. }
  973. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  974. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  975. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  976. first, flist, &cur, 1, &tmp_rval,
  977. XFS_DATA_FORK);
  978. rval |= tmp_rval;
  979. if (error)
  980. goto done;
  981. }
  982. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  983. STARTBLOCKVAL(PREV.br_startblock) -
  984. (cur ? cur->bc_private.b.allocated : 0));
  985. base = ip->i_df.if_u1.if_extents;
  986. ep = &base[idx + 1];
  987. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  988. xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
  989. XFS_DATA_FORK);
  990. *dnew = temp;
  991. break;
  992. case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
  993. /*
  994. * Filling in the last part of a previous delayed allocation.
  995. * The right neighbor is contiguous with the new allocation.
  996. */
  997. temp = PREV.br_blockcount - new->br_blockcount;
  998. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
  999. XFS_DATA_FORK);
  1000. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
  1001. XFS_DATA_FORK);
  1002. xfs_bmbt_set_blockcount(ep, temp);
  1003. xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
  1004. new->br_blockcount + RIGHT.br_blockcount,
  1005. RIGHT.br_state);
  1006. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
  1007. XFS_DATA_FORK);
  1008. ip->i_df.if_lastex = idx + 1;
  1009. if (cur == NULL)
  1010. rval = XFS_ILOG_DEXT;
  1011. else {
  1012. rval = 0;
  1013. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1014. RIGHT.br_startblock,
  1015. RIGHT.br_blockcount, &i)))
  1016. goto done;
  1017. ASSERT(i == 1);
  1018. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1019. new->br_startblock,
  1020. new->br_blockcount +
  1021. RIGHT.br_blockcount,
  1022. RIGHT.br_state)))
  1023. goto done;
  1024. }
  1025. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  1026. STARTBLOCKVAL(PREV.br_startblock));
  1027. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1028. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
  1029. XFS_DATA_FORK);
  1030. *dnew = temp;
  1031. break;
  1032. case MASK(RIGHT_FILLING):
  1033. /*
  1034. * Filling in the last part of a previous delayed allocation.
  1035. * The right neighbor is not contiguous.
  1036. */
  1037. temp = PREV.br_blockcount - new->br_blockcount;
  1038. xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1039. xfs_bmbt_set_blockcount(ep, temp);
  1040. xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
  1041. new, NULL, XFS_DATA_FORK);
  1042. xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
  1043. ip->i_df.if_lastex = idx + 1;
  1044. ip->i_d.di_nextents++;
  1045. if (cur == NULL)
  1046. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1047. else {
  1048. rval = XFS_ILOG_CORE;
  1049. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1050. new->br_startblock, new->br_blockcount,
  1051. &i)))
  1052. goto done;
  1053. ASSERT(i == 0);
  1054. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1055. if ((error = xfs_bmbt_insert(cur, &i)))
  1056. goto done;
  1057. ASSERT(i == 1);
  1058. }
  1059. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1060. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1061. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1062. first, flist, &cur, 1, &tmp_rval,
  1063. XFS_DATA_FORK);
  1064. rval |= tmp_rval;
  1065. if (error)
  1066. goto done;
  1067. }
  1068. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  1069. STARTBLOCKVAL(PREV.br_startblock) -
  1070. (cur ? cur->bc_private.b.allocated : 0));
  1071. base = ip->i_df.if_u1.if_extents;
  1072. ep = &base[idx];
  1073. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1074. xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1075. *dnew = temp;
  1076. break;
  1077. case 0:
  1078. /*
  1079. * Filling in the middle part of a previous delayed allocation.
  1080. * Contiguity is impossible here.
  1081. * This case is avoided almost all the time.
  1082. */
  1083. temp = new->br_startoff - PREV.br_startoff;
  1084. xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1085. xfs_bmbt_set_blockcount(ep, temp);
  1086. r[0] = *new;
  1087. r[1].br_startoff = new_endoff;
  1088. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1089. r[1].br_blockcount = temp2;
  1090. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
  1091. XFS_DATA_FORK);
  1092. xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
  1093. ip->i_df.if_lastex = idx + 1;
  1094. ip->i_d.di_nextents++;
  1095. if (cur == NULL)
  1096. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1097. else {
  1098. rval = XFS_ILOG_CORE;
  1099. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1100. new->br_startblock, new->br_blockcount,
  1101. &i)))
  1102. goto done;
  1103. ASSERT(i == 0);
  1104. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1105. if ((error = xfs_bmbt_insert(cur, &i)))
  1106. goto done;
  1107. ASSERT(i == 1);
  1108. }
  1109. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1110. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1111. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1112. first, flist, &cur, 1, &tmp_rval,
  1113. XFS_DATA_FORK);
  1114. rval |= tmp_rval;
  1115. if (error)
  1116. goto done;
  1117. }
  1118. temp = xfs_bmap_worst_indlen(ip, temp);
  1119. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  1120. diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
  1121. (cur ? cur->bc_private.b.allocated : 0));
  1122. if (diff > 0 &&
  1123. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
  1124. /*
  1125. * Ick gross gag me with a spoon.
  1126. */
  1127. ASSERT(0); /* want to see if this ever happens! */
  1128. while (diff > 0) {
  1129. if (temp) {
  1130. temp--;
  1131. diff--;
  1132. if (!diff ||
  1133. !xfs_mod_incore_sb(ip->i_mount,
  1134. XFS_SBS_FDBLOCKS, -diff, rsvd))
  1135. break;
  1136. }
  1137. if (temp2) {
  1138. temp2--;
  1139. diff--;
  1140. if (!diff ||
  1141. !xfs_mod_incore_sb(ip->i_mount,
  1142. XFS_SBS_FDBLOCKS, -diff, rsvd))
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. base = ip->i_df.if_u1.if_extents;
  1148. ep = &base[idx];
  1149. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1150. xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1151. xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
  1152. XFS_DATA_FORK);
  1153. xfs_bmbt_set_startblock(ep + 2, NULLSTARTBLOCK((int)temp2));
  1154. xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
  1155. XFS_DATA_FORK);
  1156. *dnew = temp + temp2;
  1157. break;
  1158. case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1159. case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1160. case MASK2(LEFT_FILLING, RIGHT_CONTIG):
  1161. case MASK2(RIGHT_FILLING, LEFT_CONTIG):
  1162. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1163. case MASK(LEFT_CONTIG):
  1164. case MASK(RIGHT_CONTIG):
  1165. /*
  1166. * These cases are all impossible.
  1167. */
  1168. ASSERT(0);
  1169. }
  1170. *curp = cur;
  1171. done:
  1172. *logflagsp = rval;
  1173. return error;
  1174. #undef LEFT
  1175. #undef RIGHT
  1176. #undef PREV
  1177. #undef MASK
  1178. #undef MASK2
  1179. #undef MASK3
  1180. #undef MASK4
  1181. #undef STATE_SET
  1182. #undef STATE_TEST
  1183. #undef STATE_SET_TEST
  1184. #undef SWITCH_STATE
  1185. }
  1186. /*
  1187. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  1188. * allocation to a real allocation or vice versa.
  1189. */
  1190. STATIC int /* error */
  1191. xfs_bmap_add_extent_unwritten_real(
  1192. xfs_inode_t *ip, /* incore inode pointer */
  1193. xfs_extnum_t idx, /* extent number to update/insert */
  1194. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  1195. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  1196. int *logflagsp) /* inode logging flags */
  1197. {
  1198. xfs_bmbt_rec_t *base; /* base of extent entry list */
  1199. xfs_btree_cur_t *cur; /* btree cursor */
  1200. xfs_bmbt_rec_t *ep; /* extent entry for idx */
  1201. int error; /* error return value */
  1202. #ifdef XFS_BMAP_TRACE
  1203. static char fname[] = "xfs_bmap_add_extent_unwritten_real";
  1204. #endif
  1205. int i; /* temp state */
  1206. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1207. xfs_exntst_t newext; /* new extent state */
  1208. xfs_exntst_t oldext; /* old extent state */
  1209. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1210. /* left is 0, right is 1, prev is 2 */
  1211. int rval=0; /* return value (logging flags) */
  1212. int state = 0;/* state bits, accessed thru macros */
  1213. enum { /* bit number definitions for state */
  1214. LEFT_CONTIG, RIGHT_CONTIG,
  1215. LEFT_FILLING, RIGHT_FILLING,
  1216. LEFT_DELAY, RIGHT_DELAY,
  1217. LEFT_VALID, RIGHT_VALID
  1218. };
  1219. #define LEFT r[0]
  1220. #define RIGHT r[1]
  1221. #define PREV r[2]
  1222. #define MASK(b) (1 << (b))
  1223. #define MASK2(a,b) (MASK(a) | MASK(b))
  1224. #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
  1225. #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
  1226. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1227. #define STATE_TEST(b) (state & MASK(b))
  1228. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1229. ((state &= ~MASK(b)), 0))
  1230. #define SWITCH_STATE \
  1231. (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
  1232. /*
  1233. * Set up a bunch of variables to make the tests simpler.
  1234. */
  1235. error = 0;
  1236. cur = *curp;
  1237. base = ip->i_df.if_u1.if_extents;
  1238. ep = &base[idx];
  1239. xfs_bmbt_get_all(ep, &PREV);
  1240. newext = new->br_state;
  1241. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  1242. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  1243. ASSERT(PREV.br_state == oldext);
  1244. new_endoff = new->br_startoff + new->br_blockcount;
  1245. ASSERT(PREV.br_startoff <= new->br_startoff);
  1246. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  1247. /*
  1248. * Set flags determining what part of the previous oldext allocation
  1249. * extent is being replaced by a newext allocation.
  1250. */
  1251. STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
  1252. STATE_SET(RIGHT_FILLING,
  1253. PREV.br_startoff + PREV.br_blockcount == new_endoff);
  1254. /*
  1255. * Check and set flags if this segment has a left neighbor.
  1256. * Don't set contiguous if the combined extent would be too large.
  1257. */
  1258. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1259. xfs_bmbt_get_all(ep - 1, &LEFT);
  1260. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
  1261. }
  1262. STATE_SET(LEFT_CONTIG,
  1263. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  1264. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  1265. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  1266. LEFT.br_state == newext &&
  1267. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  1268. /*
  1269. * Check and set flags if this segment has a right neighbor.
  1270. * Don't set contiguous if the combined extent would be too large.
  1271. * Also check for all-three-contiguous being too large.
  1272. */
  1273. if (STATE_SET_TEST(RIGHT_VALID,
  1274. idx <
  1275. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
  1276. xfs_bmbt_get_all(ep + 1, &RIGHT);
  1277. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
  1278. }
  1279. STATE_SET(RIGHT_CONTIG,
  1280. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  1281. new_endoff == RIGHT.br_startoff &&
  1282. new->br_startblock + new->br_blockcount ==
  1283. RIGHT.br_startblock &&
  1284. newext == RIGHT.br_state &&
  1285. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  1286. ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
  1287. MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
  1288. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  1289. <= MAXEXTLEN));
  1290. /*
  1291. * Switch out based on the FILLING and CONTIG state bits.
  1292. */
  1293. switch (SWITCH_STATE) {
  1294. case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1295. /*
  1296. * Setting all of a previous oldext extent to newext.
  1297. * The left and right neighbors are both contiguous with new.
  1298. */
  1299. xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  1300. XFS_DATA_FORK);
  1301. xfs_bmbt_set_blockcount(ep - 1,
  1302. LEFT.br_blockcount + PREV.br_blockcount +
  1303. RIGHT.br_blockcount);
  1304. xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  1305. XFS_DATA_FORK);
  1306. xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
  1307. XFS_DATA_FORK);
  1308. xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
  1309. ip->i_df.if_lastex = idx - 1;
  1310. ip->i_d.di_nextents -= 2;
  1311. if (cur == NULL)
  1312. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1313. else {
  1314. rval = XFS_ILOG_CORE;
  1315. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1316. RIGHT.br_startblock,
  1317. RIGHT.br_blockcount, &i)))
  1318. goto done;
  1319. ASSERT(i == 1);
  1320. if ((error = xfs_bmbt_delete(cur, &i)))
  1321. goto done;
  1322. ASSERT(i == 1);
  1323. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1324. goto done;
  1325. ASSERT(i == 1);
  1326. if ((error = xfs_bmbt_delete(cur, &i)))
  1327. goto done;
  1328. ASSERT(i == 1);
  1329. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1330. goto done;
  1331. ASSERT(i == 1);
  1332. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1333. LEFT.br_startblock,
  1334. LEFT.br_blockcount + PREV.br_blockcount +
  1335. RIGHT.br_blockcount, LEFT.br_state)))
  1336. goto done;
  1337. }
  1338. break;
  1339. case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
  1340. /*
  1341. * Setting all of a previous oldext extent to newext.
  1342. * The left neighbor is contiguous, the right is not.
  1343. */
  1344. xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
  1345. XFS_DATA_FORK);
  1346. xfs_bmbt_set_blockcount(ep - 1,
  1347. LEFT.br_blockcount + PREV.br_blockcount);
  1348. xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
  1349. XFS_DATA_FORK);
  1350. ip->i_df.if_lastex = idx - 1;
  1351. xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
  1352. XFS_DATA_FORK);
  1353. xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
  1354. ip->i_d.di_nextents--;
  1355. if (cur == NULL)
  1356. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1357. else {
  1358. rval = XFS_ILOG_CORE;
  1359. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1360. PREV.br_startblock, PREV.br_blockcount,
  1361. &i)))
  1362. goto done;
  1363. ASSERT(i == 1);
  1364. if ((error = xfs_bmbt_delete(cur, &i)))
  1365. goto done;
  1366. ASSERT(i == 1);
  1367. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1368. goto done;
  1369. ASSERT(i == 1);
  1370. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1371. LEFT.br_startblock,
  1372. LEFT.br_blockcount + PREV.br_blockcount,
  1373. LEFT.br_state)))
  1374. goto done;
  1375. }
  1376. break;
  1377. case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
  1378. /*
  1379. * Setting all of a previous oldext extent to newext.
  1380. * The right neighbor is contiguous, the left is not.
  1381. */
  1382. xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
  1383. XFS_DATA_FORK);
  1384. xfs_bmbt_set_blockcount(ep,
  1385. PREV.br_blockcount + RIGHT.br_blockcount);
  1386. xfs_bmbt_set_state(ep, newext);
  1387. xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
  1388. XFS_DATA_FORK);
  1389. ip->i_df.if_lastex = idx;
  1390. xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
  1391. XFS_DATA_FORK);
  1392. xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
  1393. ip->i_d.di_nextents--;
  1394. if (cur == NULL)
  1395. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1396. else {
  1397. rval = XFS_ILOG_CORE;
  1398. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1399. RIGHT.br_startblock,
  1400. RIGHT.br_blockcount, &i)))
  1401. goto done;
  1402. ASSERT(i == 1);
  1403. if ((error = xfs_bmbt_delete(cur, &i)))
  1404. goto done;
  1405. ASSERT(i == 1);
  1406. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1407. goto done;
  1408. ASSERT(i == 1);
  1409. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1410. new->br_startblock,
  1411. new->br_blockcount + RIGHT.br_blockcount,
  1412. newext)))
  1413. goto done;
  1414. }
  1415. break;
  1416. case MASK2(LEFT_FILLING, RIGHT_FILLING):
  1417. /*
  1418. * Setting all of a previous oldext extent to newext.
  1419. * Neither the left nor right neighbors are contiguous with
  1420. * the new one.
  1421. */
  1422. xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
  1423. XFS_DATA_FORK);
  1424. xfs_bmbt_set_state(ep, newext);
  1425. xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
  1426. XFS_DATA_FORK);
  1427. ip->i_df.if_lastex = idx;
  1428. if (cur == NULL)
  1429. rval = XFS_ILOG_DEXT;
  1430. else {
  1431. rval = 0;
  1432. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1433. new->br_startblock, new->br_blockcount,
  1434. &i)))
  1435. goto done;
  1436. ASSERT(i == 1);
  1437. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1438. new->br_startblock, new->br_blockcount,
  1439. newext)))
  1440. goto done;
  1441. }
  1442. break;
  1443. case MASK2(LEFT_FILLING, LEFT_CONTIG):
  1444. /*
  1445. * Setting the first part of a previous oldext extent to newext.
  1446. * The left neighbor is contiguous.
  1447. */
  1448. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
  1449. XFS_DATA_FORK);
  1450. xfs_bmbt_set_blockcount(ep - 1,
  1451. LEFT.br_blockcount + new->br_blockcount);
  1452. xfs_bmbt_set_startoff(ep,
  1453. PREV.br_startoff + new->br_blockcount);
  1454. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
  1455. XFS_DATA_FORK);
  1456. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
  1457. XFS_DATA_FORK);
  1458. xfs_bmbt_set_startblock(ep,
  1459. new->br_startblock + new->br_blockcount);
  1460. xfs_bmbt_set_blockcount(ep,
  1461. PREV.br_blockcount - new->br_blockcount);
  1462. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
  1463. XFS_DATA_FORK);
  1464. ip->i_df.if_lastex = idx - 1;
  1465. if (cur == NULL)
  1466. rval = XFS_ILOG_DEXT;
  1467. else {
  1468. rval = 0;
  1469. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1470. PREV.br_startblock, PREV.br_blockcount,
  1471. &i)))
  1472. goto done;
  1473. ASSERT(i == 1);
  1474. if ((error = xfs_bmbt_update(cur,
  1475. PREV.br_startoff + new->br_blockcount,
  1476. PREV.br_startblock + new->br_blockcount,
  1477. PREV.br_blockcount - new->br_blockcount,
  1478. oldext)))
  1479. goto done;
  1480. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1481. goto done;
  1482. if (xfs_bmbt_update(cur, LEFT.br_startoff,
  1483. LEFT.br_startblock,
  1484. LEFT.br_blockcount + new->br_blockcount,
  1485. LEFT.br_state))
  1486. goto done;
  1487. }
  1488. break;
  1489. case MASK(LEFT_FILLING):
  1490. /*
  1491. * Setting the first part of a previous oldext extent to newext.
  1492. * The left neighbor is not contiguous.
  1493. */
  1494. xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  1495. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  1496. xfs_bmbt_set_startoff(ep, new_endoff);
  1497. xfs_bmbt_set_blockcount(ep,
  1498. PREV.br_blockcount - new->br_blockcount);
  1499. xfs_bmbt_set_startblock(ep,
  1500. new->br_startblock + new->br_blockcount);
  1501. xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  1502. xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
  1503. XFS_DATA_FORK);
  1504. xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
  1505. ip->i_df.if_lastex = idx;
  1506. ip->i_d.di_nextents++;
  1507. if (cur == NULL)
  1508. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1509. else {
  1510. rval = XFS_ILOG_CORE;
  1511. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1512. PREV.br_startblock, PREV.br_blockcount,
  1513. &i)))
  1514. goto done;
  1515. ASSERT(i == 1);
  1516. if ((error = xfs_bmbt_update(cur,
  1517. PREV.br_startoff + new->br_blockcount,
  1518. PREV.br_startblock + new->br_blockcount,
  1519. PREV.br_blockcount - new->br_blockcount,
  1520. oldext)))
  1521. goto done;
  1522. cur->bc_rec.b = *new;
  1523. if ((error = xfs_bmbt_insert(cur, &i)))
  1524. goto done;
  1525. ASSERT(i == 1);
  1526. }
  1527. break;
  1528. case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
  1529. /*
  1530. * Setting the last part of a previous oldext extent to newext.
  1531. * The right neighbor is contiguous with the new allocation.
  1532. */
  1533. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
  1534. XFS_DATA_FORK);
  1535. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
  1536. XFS_DATA_FORK);
  1537. xfs_bmbt_set_blockcount(ep,
  1538. PREV.br_blockcount - new->br_blockcount);
  1539. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
  1540. XFS_DATA_FORK);
  1541. xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
  1542. new->br_blockcount + RIGHT.br_blockcount, newext);
  1543. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
  1544. XFS_DATA_FORK);
  1545. ip->i_df.if_lastex = idx + 1;
  1546. if (cur == NULL)
  1547. rval = XFS_ILOG_DEXT;
  1548. else {
  1549. rval = 0;
  1550. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1551. PREV.br_startblock,
  1552. PREV.br_blockcount, &i)))
  1553. goto done;
  1554. ASSERT(i == 1);
  1555. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1556. PREV.br_startblock,
  1557. PREV.br_blockcount - new->br_blockcount,
  1558. oldext)))
  1559. goto done;
  1560. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  1561. goto done;
  1562. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1563. new->br_startblock,
  1564. new->br_blockcount + RIGHT.br_blockcount,
  1565. newext)))
  1566. goto done;
  1567. }
  1568. break;
  1569. case MASK(RIGHT_FILLING):
  1570. /*
  1571. * Setting the last part of a previous oldext extent to newext.
  1572. * The right neighbor is not contiguous.
  1573. */
  1574. xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1575. xfs_bmbt_set_blockcount(ep,
  1576. PREV.br_blockcount - new->br_blockcount);
  1577. xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1578. xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
  1579. new, NULL, XFS_DATA_FORK);
  1580. xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
  1581. ip->i_df.if_lastex = idx + 1;
  1582. ip->i_d.di_nextents++;
  1583. if (cur == NULL)
  1584. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1585. else {
  1586. rval = XFS_ILOG_CORE;
  1587. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1588. PREV.br_startblock, PREV.br_blockcount,
  1589. &i)))
  1590. goto done;
  1591. ASSERT(i == 1);
  1592. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1593. PREV.br_startblock,
  1594. PREV.br_blockcount - new->br_blockcount,
  1595. oldext)))
  1596. goto done;
  1597. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1598. new->br_startblock, new->br_blockcount,
  1599. &i)))
  1600. goto done;
  1601. ASSERT(i == 0);
  1602. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1603. if ((error = xfs_bmbt_insert(cur, &i)))
  1604. goto done;
  1605. ASSERT(i == 1);
  1606. }
  1607. break;
  1608. case 0:
  1609. /*
  1610. * Setting the middle part of a previous oldext extent to
  1611. * newext. Contiguity is impossible here.
  1612. * One extent becomes three extents.
  1613. */
  1614. xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1615. xfs_bmbt_set_blockcount(ep,
  1616. new->br_startoff - PREV.br_startoff);
  1617. xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1618. r[0] = *new;
  1619. r[1].br_startoff = new_endoff;
  1620. r[1].br_blockcount =
  1621. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1622. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  1623. r[1].br_state = oldext;
  1624. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
  1625. XFS_DATA_FORK);
  1626. xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
  1627. ip->i_df.if_lastex = idx + 1;
  1628. ip->i_d.di_nextents += 2;
  1629. if (cur == NULL)
  1630. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1631. else {
  1632. rval = XFS_ILOG_CORE;
  1633. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1634. PREV.br_startblock, PREV.br_blockcount,
  1635. &i)))
  1636. goto done;
  1637. ASSERT(i == 1);
  1638. /* new right extent - oldext */
  1639. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  1640. r[1].br_startblock, r[1].br_blockcount,
  1641. r[1].br_state)))
  1642. goto done;
  1643. /* new left extent - oldext */
  1644. PREV.br_blockcount =
  1645. new->br_startoff - PREV.br_startoff;
  1646. cur->bc_rec.b = PREV;
  1647. if ((error = xfs_bmbt_insert(cur, &i)))
  1648. goto done;
  1649. ASSERT(i == 1);
  1650. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  1651. goto done;
  1652. ASSERT(i == 1);
  1653. /* new middle extent - newext */
  1654. cur->bc_rec.b = *new;
  1655. if ((error = xfs_bmbt_insert(cur, &i)))
  1656. goto done;
  1657. ASSERT(i == 1);
  1658. }
  1659. break;
  1660. case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1661. case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1662. case MASK2(LEFT_FILLING, RIGHT_CONTIG):
  1663. case MASK2(RIGHT_FILLING, LEFT_CONTIG):
  1664. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1665. case MASK(LEFT_CONTIG):
  1666. case MASK(RIGHT_CONTIG):
  1667. /*
  1668. * These cases are all impossible.
  1669. */
  1670. ASSERT(0);
  1671. }
  1672. *curp = cur;
  1673. done:
  1674. *logflagsp = rval;
  1675. return error;
  1676. #undef LEFT
  1677. #undef RIGHT
  1678. #undef PREV
  1679. #undef MASK
  1680. #undef MASK2
  1681. #undef MASK3
  1682. #undef MASK4
  1683. #undef STATE_SET
  1684. #undef STATE_TEST
  1685. #undef STATE_SET_TEST
  1686. #undef SWITCH_STATE
  1687. }
  1688. /*
  1689. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1690. * to a delayed allocation.
  1691. */
  1692. /*ARGSUSED*/
  1693. STATIC int /* error */
  1694. xfs_bmap_add_extent_hole_delay(
  1695. xfs_inode_t *ip, /* incore inode pointer */
  1696. xfs_extnum_t idx, /* extent number to update/insert */
  1697. xfs_btree_cur_t *cur, /* if null, not a btree */
  1698. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  1699. int *logflagsp, /* inode logging flags */
  1700. int rsvd) /* OK to allocate reserved blocks */
  1701. {
  1702. xfs_bmbt_rec_t *base; /* base of extent entry list */
  1703. xfs_bmbt_rec_t *ep; /* extent list entry for idx */
  1704. #ifdef XFS_BMAP_TRACE
  1705. static char fname[] = "xfs_bmap_add_extent_hole_delay";
  1706. #endif
  1707. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1708. xfs_filblks_t newlen=0; /* new indirect size */
  1709. xfs_filblks_t oldlen=0; /* old indirect size */
  1710. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1711. int state; /* state bits, accessed thru macros */
  1712. xfs_filblks_t temp; /* temp for indirect calculations */
  1713. enum { /* bit number definitions for state */
  1714. LEFT_CONTIG, RIGHT_CONTIG,
  1715. LEFT_DELAY, RIGHT_DELAY,
  1716. LEFT_VALID, RIGHT_VALID
  1717. };
  1718. #define MASK(b) (1 << (b))
  1719. #define MASK2(a,b) (MASK(a) | MASK(b))
  1720. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1721. #define STATE_TEST(b) (state & MASK(b))
  1722. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1723. ((state &= ~MASK(b)), 0))
  1724. #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
  1725. base = ip->i_df.if_u1.if_extents;
  1726. ep = &base[idx];
  1727. state = 0;
  1728. ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
  1729. /*
  1730. * Check and set flags if this segment has a left neighbor
  1731. */
  1732. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1733. xfs_bmbt_get_all(ep - 1, &left);
  1734. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
  1735. }
  1736. /*
  1737. * Check and set flags if the current (right) segment exists.
  1738. * If it doesn't exist, we're converting the hole at end-of-file.
  1739. */
  1740. if (STATE_SET_TEST(RIGHT_VALID,
  1741. idx <
  1742. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  1743. xfs_bmbt_get_all(ep, &right);
  1744. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
  1745. }
  1746. /*
  1747. * Set contiguity flags on the left and right neighbors.
  1748. * Don't let extents get too large, even if the pieces are contiguous.
  1749. */
  1750. STATE_SET(LEFT_CONTIG,
  1751. STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
  1752. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1753. left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  1754. STATE_SET(RIGHT_CONTIG,
  1755. STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
  1756. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1757. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1758. (!STATE_TEST(LEFT_CONTIG) ||
  1759. (left.br_blockcount + new->br_blockcount +
  1760. right.br_blockcount <= MAXEXTLEN)));
  1761. /*
  1762. * Switch out based on the contiguity flags.
  1763. */
  1764. switch (SWITCH_STATE) {
  1765. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1766. /*
  1767. * New allocation is contiguous with delayed allocations
  1768. * on the left and on the right.
  1769. * Merge all three into a single extent list entry.
  1770. */
  1771. temp = left.br_blockcount + new->br_blockcount +
  1772. right.br_blockcount;
  1773. xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
  1774. XFS_DATA_FORK);
  1775. xfs_bmbt_set_blockcount(ep - 1, temp);
  1776. oldlen = STARTBLOCKVAL(left.br_startblock) +
  1777. STARTBLOCKVAL(new->br_startblock) +
  1778. STARTBLOCKVAL(right.br_startblock);
  1779. newlen = xfs_bmap_worst_indlen(ip, temp);
  1780. xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
  1781. xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
  1782. XFS_DATA_FORK);
  1783. xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
  1784. XFS_DATA_FORK);
  1785. xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
  1786. ip->i_df.if_lastex = idx - 1;
  1787. break;
  1788. case MASK(LEFT_CONTIG):
  1789. /*
  1790. * New allocation is contiguous with a delayed allocation
  1791. * on the left.
  1792. * Merge the new allocation with the left neighbor.
  1793. */
  1794. temp = left.br_blockcount + new->br_blockcount;
  1795. xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
  1796. XFS_DATA_FORK);
  1797. xfs_bmbt_set_blockcount(ep - 1, temp);
  1798. oldlen = STARTBLOCKVAL(left.br_startblock) +
  1799. STARTBLOCKVAL(new->br_startblock);
  1800. newlen = xfs_bmap_worst_indlen(ip, temp);
  1801. xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
  1802. xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
  1803. XFS_DATA_FORK);
  1804. ip->i_df.if_lastex = idx - 1;
  1805. break;
  1806. case MASK(RIGHT_CONTIG):
  1807. /*
  1808. * New allocation is contiguous with a delayed allocation
  1809. * on the right.
  1810. * Merge the new allocation with the right neighbor.
  1811. */
  1812. xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
  1813. temp = new->br_blockcount + right.br_blockcount;
  1814. oldlen = STARTBLOCKVAL(new->br_startblock) +
  1815. STARTBLOCKVAL(right.br_startblock);
  1816. newlen = xfs_bmap_worst_indlen(ip, temp);
  1817. xfs_bmbt_set_allf(ep, new->br_startoff,
  1818. NULLSTARTBLOCK((int)newlen), temp, right.br_state);
  1819. xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
  1820. ip->i_df.if_lastex = idx;
  1821. break;
  1822. case 0:
  1823. /*
  1824. * New allocation is not contiguous with another
  1825. * delayed allocation.
  1826. * Insert a new entry.
  1827. */
  1828. oldlen = newlen = 0;
  1829. xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
  1830. XFS_DATA_FORK);
  1831. xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
  1832. ip->i_df.if_lastex = idx;
  1833. break;
  1834. }
  1835. if (oldlen != newlen) {
  1836. ASSERT(oldlen > newlen);
  1837. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  1838. (int)(oldlen - newlen), rsvd);
  1839. /*
  1840. * Nothing to do for disk quota accounting here.
  1841. */
  1842. }
  1843. *logflagsp = 0;
  1844. return 0;
  1845. #undef MASK
  1846. #undef MASK2
  1847. #undef STATE_SET
  1848. #undef STATE_TEST
  1849. #undef STATE_SET_TEST
  1850. #undef SWITCH_STATE
  1851. }
  1852. /*
  1853. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1854. * to a real allocation.
  1855. */
  1856. STATIC int /* error */
  1857. xfs_bmap_add_extent_hole_real(
  1858. xfs_inode_t *ip, /* incore inode pointer */
  1859. xfs_extnum_t idx, /* extent number to update/insert */
  1860. xfs_btree_cur_t *cur, /* if null, not a btree */
  1861. xfs_bmbt_irec_t *new, /* new data to put in extent list */
  1862. int *logflagsp, /* inode logging flags */
  1863. int whichfork) /* data or attr fork */
  1864. {
  1865. xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
  1866. int error; /* error return value */
  1867. #ifdef XFS_BMAP_TRACE
  1868. static char fname[] = "xfs_bmap_add_extent_hole_real";
  1869. #endif
  1870. int i; /* temp state */
  1871. xfs_ifork_t *ifp; /* inode fork pointer */
  1872. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1873. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1874. int state; /* state bits, accessed thru macros */
  1875. enum { /* bit number definitions for state */
  1876. LEFT_CONTIG, RIGHT_CONTIG,
  1877. LEFT_DELAY, RIGHT_DELAY,
  1878. LEFT_VALID, RIGHT_VALID
  1879. };
  1880. #define MASK(b) (1 << (b))
  1881. #define MASK2(a,b) (MASK(a) | MASK(b))
  1882. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1883. #define STATE_TEST(b) (state & MASK(b))
  1884. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1885. ((state &= ~MASK(b)), 0))
  1886. #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
  1887. ifp = XFS_IFORK_PTR(ip, whichfork);
  1888. ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
  1889. ep = &ifp->if_u1.if_extents[idx];
  1890. state = 0;
  1891. /*
  1892. * Check and set flags if this segment has a left neighbor.
  1893. */
  1894. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1895. xfs_bmbt_get_all(ep - 1, &left);
  1896. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
  1897. }
  1898. /*
  1899. * Check and set flags if this segment has a current value.
  1900. * Not true if we're inserting into the "hole" at eof.
  1901. */
  1902. if (STATE_SET_TEST(RIGHT_VALID,
  1903. idx <
  1904. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  1905. xfs_bmbt_get_all(ep, &right);
  1906. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
  1907. }
  1908. /*
  1909. * We're inserting a real allocation between "left" and "right".
  1910. * Set the contiguity flags. Don't let extents get too large.
  1911. */
  1912. STATE_SET(LEFT_CONTIG,
  1913. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  1914. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1915. left.br_startblock + left.br_blockcount == new->br_startblock &&
  1916. left.br_state == new->br_state &&
  1917. left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  1918. STATE_SET(RIGHT_CONTIG,
  1919. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  1920. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1921. new->br_startblock + new->br_blockcount ==
  1922. right.br_startblock &&
  1923. new->br_state == right.br_state &&
  1924. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1925. (!STATE_TEST(LEFT_CONTIG) ||
  1926. left.br_blockcount + new->br_blockcount +
  1927. right.br_blockcount <= MAXEXTLEN));
  1928. /*
  1929. * Select which case we're in here, and implement it.
  1930. */
  1931. switch (SWITCH_STATE) {
  1932. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1933. /*
  1934. * New allocation is contiguous with real allocations on the
  1935. * left and on the right.
  1936. * Merge all three into a single extent list entry.
  1937. */
  1938. xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
  1939. whichfork);
  1940. xfs_bmbt_set_blockcount(ep - 1,
  1941. left.br_blockcount + new->br_blockcount +
  1942. right.br_blockcount);
  1943. xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
  1944. whichfork);
  1945. xfs_bmap_trace_delete(fname, "LC|RC", ip,
  1946. idx, 1, whichfork);
  1947. xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
  1948. ifp->if_lastex = idx - 1;
  1949. XFS_IFORK_NEXT_SET(ip, whichfork,
  1950. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  1951. if (cur == NULL) {
  1952. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  1953. return 0;
  1954. }
  1955. *logflagsp = XFS_ILOG_CORE;
  1956. if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
  1957. right.br_startblock, right.br_blockcount, &i)))
  1958. return error;
  1959. ASSERT(i == 1);
  1960. if ((error = xfs_bmbt_delete(cur, &i)))
  1961. return error;
  1962. ASSERT(i == 1);
  1963. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1964. return error;
  1965. ASSERT(i == 1);
  1966. error = xfs_bmbt_update(cur, left.br_startoff,
  1967. left.br_startblock,
  1968. left.br_blockcount + new->br_blockcount +
  1969. right.br_blockcount, left.br_state);
  1970. return error;
  1971. case MASK(LEFT_CONTIG):
  1972. /*
  1973. * New allocation is contiguous with a real allocation
  1974. * on the left.
  1975. * Merge the new allocation with the left neighbor.
  1976. */
  1977. xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
  1978. xfs_bmbt_set_blockcount(ep - 1,
  1979. left.br_blockcount + new->br_blockcount);
  1980. xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
  1981. ifp->if_lastex = idx - 1;
  1982. if (cur == NULL) {
  1983. *logflagsp = XFS_ILOG_FEXT(whichfork);
  1984. return 0;
  1985. }
  1986. *logflagsp = 0;
  1987. if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
  1988. left.br_startblock, left.br_blockcount, &i)))
  1989. return error;
  1990. ASSERT(i == 1);
  1991. error = xfs_bmbt_update(cur, left.br_startoff,
  1992. left.br_startblock,
  1993. left.br_blockcount + new->br_blockcount,
  1994. left.br_state);
  1995. return error;
  1996. case MASK(RIGHT_CONTIG):
  1997. /*
  1998. * New allocation is contiguous with a real allocation
  1999. * on the right.
  2000. * Merge the new allocation with the right neighbor.
  2001. */
  2002. xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
  2003. xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
  2004. new->br_blockcount + right.br_blockcount,
  2005. right.br_state);
  2006. xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
  2007. ifp->if_lastex = idx;
  2008. if (cur == NULL) {
  2009. *logflagsp = XFS_ILOG_FEXT(whichfork);
  2010. return 0;
  2011. }
  2012. *logflagsp = 0;
  2013. if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
  2014. right.br_startblock, right.br_blockcount, &i)))
  2015. return error;
  2016. ASSERT(i == 1);
  2017. error = xfs_bmbt_update(cur, new->br_startoff,
  2018. new->br_startblock,
  2019. new->br_blockcount + right.br_blockcount,
  2020. right.br_state);
  2021. return error;
  2022. case 0:
  2023. /*
  2024. * New allocation is not contiguous with another
  2025. * real allocation.
  2026. * Insert a new entry.
  2027. */
  2028. xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
  2029. whichfork);
  2030. xfs_bmap_insert_exlist(ip, idx, 1, new, whichfork);
  2031. ifp->if_lastex = idx;
  2032. XFS_IFORK_NEXT_SET(ip, whichfork,
  2033. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2034. if (cur == NULL) {
  2035. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2036. return 0;
  2037. }
  2038. *logflagsp = XFS_ILOG_CORE;
  2039. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2040. new->br_startblock, new->br_blockcount, &i)))
  2041. return error;
  2042. ASSERT(i == 0);
  2043. cur->bc_rec.b.br_state = new->br_state;
  2044. if ((error = xfs_bmbt_insert(cur, &i)))
  2045. return error;
  2046. ASSERT(i == 1);
  2047. return 0;
  2048. }
  2049. #undef MASK
  2050. #undef MASK2
  2051. #undef STATE_SET
  2052. #undef STATE_TEST
  2053. #undef STATE_SET_TEST
  2054. #undef SWITCH_STATE
  2055. /* NOTREACHED */
  2056. ASSERT(0);
  2057. return 0; /* keep gcc quite */
  2058. }
  2059. #define XFS_ALLOC_GAP_UNITS 4
  2060. /*
  2061. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2062. * It figures out where to ask the underlying allocator to put the new extent.
  2063. */
  2064. STATIC int /* error */
  2065. xfs_bmap_alloc(
  2066. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2067. {
  2068. xfs_fsblock_t adjust; /* adjustment to block numbers */
  2069. xfs_alloctype_t atype=0; /* type for allocation routines */
  2070. int error; /* error return value */
  2071. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2072. xfs_mount_t *mp; /* mount point structure */
  2073. int nullfb; /* true if ap->firstblock isn't set */
  2074. int rt; /* true if inode is realtime */
  2075. #ifdef __KERNEL__
  2076. xfs_extlen_t prod=0; /* product factor for allocators */
  2077. xfs_extlen_t ralen=0; /* realtime allocation length */
  2078. #endif
  2079. #define ISVALID(x,y) \
  2080. (rt ? \
  2081. (x) < mp->m_sb.sb_rblocks : \
  2082. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  2083. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  2084. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  2085. /*
  2086. * Set up variables.
  2087. */
  2088. mp = ap->ip->i_mount;
  2089. nullfb = ap->firstblock == NULLFSBLOCK;
  2090. rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
  2091. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2092. #ifdef __KERNEL__
  2093. if (rt) {
  2094. xfs_extlen_t extsz; /* file extent size for rt */
  2095. xfs_fileoff_t nexto; /* next file offset */
  2096. xfs_extlen_t orig_alen; /* original ap->alen */
  2097. xfs_fileoff_t orig_end; /* original off+len */
  2098. xfs_fileoff_t orig_off; /* original ap->off */
  2099. xfs_extlen_t mod_off; /* modulus calculations */
  2100. xfs_fileoff_t prevo; /* previous file offset */
  2101. xfs_rtblock_t rtx; /* realtime extent number */
  2102. xfs_extlen_t temp; /* temp for rt calculations */
  2103. /*
  2104. * Set prod to match the realtime extent size.
  2105. */
  2106. if (!(extsz = ap->ip->i_d.di_extsize))
  2107. extsz = mp->m_sb.sb_rextsize;
  2108. prod = extsz / mp->m_sb.sb_rextsize;
  2109. orig_off = ap->off;
  2110. orig_alen = ap->alen;
  2111. orig_end = orig_off + orig_alen;
  2112. /*
  2113. * If the file offset is unaligned vs. the extent size
  2114. * we need to align it. This will be possible unless
  2115. * the file was previously written with a kernel that didn't
  2116. * perform this alignment.
  2117. */
  2118. mod_off = do_mod(orig_off, extsz);
  2119. if (mod_off) {
  2120. ap->alen += mod_off;
  2121. ap->off -= mod_off;
  2122. }
  2123. /*
  2124. * Same adjustment for the end of the requested area.
  2125. */
  2126. if ((temp = (ap->alen % extsz)))
  2127. ap->alen += extsz - temp;
  2128. /*
  2129. * If the previous block overlaps with this proposed allocation
  2130. * then move the start forward without adjusting the length.
  2131. */
  2132. prevo =
  2133. ap->prevp->br_startoff == NULLFILEOFF ?
  2134. 0 :
  2135. (ap->prevp->br_startoff +
  2136. ap->prevp->br_blockcount);
  2137. if (ap->off != orig_off && ap->off < prevo)
  2138. ap->off = prevo;
  2139. /*
  2140. * If the next block overlaps with this proposed allocation
  2141. * then move the start back without adjusting the length,
  2142. * but not before offset 0.
  2143. * This may of course make the start overlap previous block,
  2144. * and if we hit the offset 0 limit then the next block
  2145. * can still overlap too.
  2146. */
  2147. nexto = (ap->eof || ap->gotp->br_startoff == NULLFILEOFF) ?
  2148. NULLFILEOFF : ap->gotp->br_startoff;
  2149. if (!ap->eof &&
  2150. ap->off + ap->alen != orig_end &&
  2151. ap->off + ap->alen > nexto)
  2152. ap->off = nexto > ap->alen ? nexto - ap->alen : 0;
  2153. /*
  2154. * If we're now overlapping the next or previous extent that
  2155. * means we can't fit an extsz piece in this hole. Just move
  2156. * the start forward to the first valid spot and set
  2157. * the length so we hit the end.
  2158. */
  2159. if ((ap->off != orig_off && ap->off < prevo) ||
  2160. (ap->off + ap->alen != orig_end &&
  2161. ap->off + ap->alen > nexto)) {
  2162. ap->off = prevo;
  2163. ap->alen = nexto - prevo;
  2164. }
  2165. /*
  2166. * If the result isn't a multiple of rtextents we need to
  2167. * remove blocks until it is.
  2168. */
  2169. if ((temp = (ap->alen % mp->m_sb.sb_rextsize))) {
  2170. /*
  2171. * We're not covering the original request, or
  2172. * we won't be able to once we fix the length.
  2173. */
  2174. if (orig_off < ap->off ||
  2175. orig_end > ap->off + ap->alen ||
  2176. ap->alen - temp < orig_alen)
  2177. return XFS_ERROR(EINVAL);
  2178. /*
  2179. * Try to fix it by moving the start up.
  2180. */
  2181. if (ap->off + temp <= orig_off) {
  2182. ap->alen -= temp;
  2183. ap->off += temp;
  2184. }
  2185. /*
  2186. * Try to fix it by moving the end in.
  2187. */
  2188. else if (ap->off + ap->alen - temp >= orig_end)
  2189. ap->alen -= temp;
  2190. /*
  2191. * Set the start to the minimum then trim the length.
  2192. */
  2193. else {
  2194. ap->alen -= orig_off - ap->off;
  2195. ap->off = orig_off;
  2196. ap->alen -= ap->alen % mp->m_sb.sb_rextsize;
  2197. }
  2198. /*
  2199. * Result doesn't cover the request, fail it.
  2200. */
  2201. if (orig_off < ap->off || orig_end > ap->off + ap->alen)
  2202. return XFS_ERROR(EINVAL);
  2203. }
  2204. ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
  2205. /*
  2206. * If the offset & length are not perfectly aligned
  2207. * then kill prod, it will just get us in trouble.
  2208. */
  2209. if (do_mod(ap->off, extsz) || ap->alen % extsz)
  2210. prod = 1;
  2211. /*
  2212. * Set ralen to be the actual requested length in rtextents.
  2213. */
  2214. ralen = ap->alen / mp->m_sb.sb_rextsize;
  2215. /*
  2216. * If the old value was close enough to MAXEXTLEN that
  2217. * we rounded up to it, cut it back so it's valid again.
  2218. * Note that if it's a really large request (bigger than
  2219. * MAXEXTLEN), we don't hear about that number, and can't
  2220. * adjust the starting point to match it.
  2221. */
  2222. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  2223. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  2224. /*
  2225. * If it's an allocation to an empty file at offset 0,
  2226. * pick an extent that will space things out in the rt area.
  2227. */
  2228. if (ap->eof && ap->off == 0) {
  2229. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  2230. if (error)
  2231. return error;
  2232. ap->rval = rtx * mp->m_sb.sb_rextsize;
  2233. } else
  2234. ap->rval = 0;
  2235. }
  2236. #else
  2237. if (rt)
  2238. ap->rval = 0;
  2239. #endif /* __KERNEL__ */
  2240. else if (nullfb)
  2241. ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2242. else
  2243. ap->rval = ap->firstblock;
  2244. /*
  2245. * If allocating at eof, and there's a previous real block,
  2246. * try to use it's last block as our starting point.
  2247. */
  2248. if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
  2249. !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
  2250. ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
  2251. ap->prevp->br_startblock)) {
  2252. ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
  2253. /*
  2254. * Adjust for the gap between prevp and us.
  2255. */
  2256. adjust = ap->off -
  2257. (ap->prevp->br_startoff + ap->prevp->br_blockcount);
  2258. if (adjust &&
  2259. ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
  2260. ap->rval += adjust;
  2261. }
  2262. /*
  2263. * If not at eof, then compare the two neighbor blocks.
  2264. * Figure out whether either one gives us a good starting point,
  2265. * and pick the better one.
  2266. */
  2267. else if (!ap->eof) {
  2268. xfs_fsblock_t gotbno; /* right side block number */
  2269. xfs_fsblock_t gotdiff=0; /* right side difference */
  2270. xfs_fsblock_t prevbno; /* left side block number */
  2271. xfs_fsblock_t prevdiff=0; /* left side difference */
  2272. /*
  2273. * If there's a previous (left) block, select a requested
  2274. * start block based on it.
  2275. */
  2276. if (ap->prevp->br_startoff != NULLFILEOFF &&
  2277. !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
  2278. (prevbno = ap->prevp->br_startblock +
  2279. ap->prevp->br_blockcount) &&
  2280. ISVALID(prevbno, ap->prevp->br_startblock)) {
  2281. /*
  2282. * Calculate gap to end of previous block.
  2283. */
  2284. adjust = prevdiff = ap->off -
  2285. (ap->prevp->br_startoff +
  2286. ap->prevp->br_blockcount);
  2287. /*
  2288. * Figure the startblock based on the previous block's
  2289. * end and the gap size.
  2290. * Heuristic!
  2291. * If the gap is large relative to the piece we're
  2292. * allocating, or using it gives us an invalid block
  2293. * number, then just use the end of the previous block.
  2294. */
  2295. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2296. ISVALID(prevbno + prevdiff,
  2297. ap->prevp->br_startblock))
  2298. prevbno += adjust;
  2299. else
  2300. prevdiff += adjust;
  2301. /*
  2302. * If the firstblock forbids it, can't use it,
  2303. * must use default.
  2304. */
  2305. if (!rt && !nullfb &&
  2306. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  2307. prevbno = NULLFSBLOCK;
  2308. }
  2309. /*
  2310. * No previous block or can't follow it, just default.
  2311. */
  2312. else
  2313. prevbno = NULLFSBLOCK;
  2314. /*
  2315. * If there's a following (right) block, select a requested
  2316. * start block based on it.
  2317. */
  2318. if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
  2319. /*
  2320. * Calculate gap to start of next block.
  2321. */
  2322. adjust = gotdiff = ap->gotp->br_startoff - ap->off;
  2323. /*
  2324. * Figure the startblock based on the next block's
  2325. * start and the gap size.
  2326. */
  2327. gotbno = ap->gotp->br_startblock;
  2328. /*
  2329. * Heuristic!
  2330. * If the gap is large relative to the piece we're
  2331. * allocating, or using it gives us an invalid block
  2332. * number, then just use the start of the next block
  2333. * offset by our length.
  2334. */
  2335. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2336. ISVALID(gotbno - gotdiff, gotbno))
  2337. gotbno -= adjust;
  2338. else if (ISVALID(gotbno - ap->alen, gotbno)) {
  2339. gotbno -= ap->alen;
  2340. gotdiff += adjust - ap->alen;
  2341. } else
  2342. gotdiff += adjust;
  2343. /*
  2344. * If the firstblock forbids it, can't use it,
  2345. * must use default.
  2346. */
  2347. if (!rt && !nullfb &&
  2348. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  2349. gotbno = NULLFSBLOCK;
  2350. }
  2351. /*
  2352. * No next block, just default.
  2353. */
  2354. else
  2355. gotbno = NULLFSBLOCK;
  2356. /*
  2357. * If both valid, pick the better one, else the only good
  2358. * one, else ap->rval is already set (to 0 or the inode block).
  2359. */
  2360. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  2361. ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
  2362. else if (prevbno != NULLFSBLOCK)
  2363. ap->rval = prevbno;
  2364. else if (gotbno != NULLFSBLOCK)
  2365. ap->rval = gotbno;
  2366. }
  2367. /*
  2368. * If allowed, use ap->rval; otherwise must use firstblock since
  2369. * it's in the right allocation group.
  2370. */
  2371. if (nullfb || rt || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
  2372. ;
  2373. else
  2374. ap->rval = ap->firstblock;
  2375. /*
  2376. * Realtime allocation, done through xfs_rtallocate_extent.
  2377. */
  2378. if (rt) {
  2379. #ifndef __KERNEL__
  2380. ASSERT(0);
  2381. #else
  2382. xfs_rtblock_t rtb;
  2383. atype = ap->rval == 0 ?
  2384. XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
  2385. do_div(ap->rval, mp->m_sb.sb_rextsize);
  2386. rtb = ap->rval;
  2387. ap->alen = ralen;
  2388. if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
  2389. &ralen, atype, ap->wasdel, prod, &rtb)))
  2390. return error;
  2391. if (rtb == NULLFSBLOCK && prod > 1 &&
  2392. (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
  2393. ap->alen, &ralen, atype,
  2394. ap->wasdel, 1, &rtb)))
  2395. return error;
  2396. ap->rval = rtb;
  2397. if (ap->rval != NULLFSBLOCK) {
  2398. ap->rval *= mp->m_sb.sb_rextsize;
  2399. ralen *= mp->m_sb.sb_rextsize;
  2400. ap->alen = ralen;
  2401. ap->ip->i_d.di_nblocks += ralen;
  2402. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2403. if (ap->wasdel)
  2404. ap->ip->i_delayed_blks -= ralen;
  2405. /*
  2406. * Adjust the disk quota also. This was reserved
  2407. * earlier.
  2408. */
  2409. XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
  2410. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  2411. XFS_TRANS_DQ_RTBCOUNT,
  2412. (long) ralen);
  2413. } else
  2414. ap->alen = 0;
  2415. #endif /* __KERNEL__ */
  2416. }
  2417. /*
  2418. * Normal allocation, done through xfs_alloc_vextent.
  2419. */
  2420. else {
  2421. xfs_agnumber_t ag;
  2422. xfs_alloc_arg_t args;
  2423. xfs_extlen_t blen;
  2424. xfs_extlen_t delta;
  2425. int isaligned;
  2426. xfs_extlen_t longest;
  2427. xfs_extlen_t need;
  2428. xfs_extlen_t nextminlen=0;
  2429. int notinit;
  2430. xfs_perag_t *pag;
  2431. xfs_agnumber_t startag;
  2432. int tryagain;
  2433. tryagain = isaligned = 0;
  2434. args.tp = ap->tp;
  2435. args.mp = mp;
  2436. args.fsbno = ap->rval;
  2437. args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
  2438. blen = 0;
  2439. if (nullfb) {
  2440. args.type = XFS_ALLOCTYPE_START_BNO;
  2441. args.total = ap->total;
  2442. /*
  2443. * Find the longest available space.
  2444. * We're going to try for the whole allocation at once.
  2445. */
  2446. startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
  2447. notinit = 0;
  2448. down_read(&mp->m_peraglock);
  2449. while (blen < ap->alen) {
  2450. pag = &mp->m_perag[ag];
  2451. if (!pag->pagf_init &&
  2452. (error = xfs_alloc_pagf_init(mp, args.tp,
  2453. ag, XFS_ALLOC_FLAG_TRYLOCK))) {
  2454. up_read(&mp->m_peraglock);
  2455. return error;
  2456. }
  2457. /*
  2458. * See xfs_alloc_fix_freelist...
  2459. */
  2460. if (pag->pagf_init) {
  2461. need = XFS_MIN_FREELIST_PAG(pag, mp);
  2462. delta = need > pag->pagf_flcount ?
  2463. need - pag->pagf_flcount : 0;
  2464. longest = (pag->pagf_longest > delta) ?
  2465. (pag->pagf_longest - delta) :
  2466. (pag->pagf_flcount > 0 ||
  2467. pag->pagf_longest > 0);
  2468. if (blen < longest)
  2469. blen = longest;
  2470. } else
  2471. notinit = 1;
  2472. if (++ag == mp->m_sb.sb_agcount)
  2473. ag = 0;
  2474. if (ag == startag)
  2475. break;
  2476. }
  2477. up_read(&mp->m_peraglock);
  2478. /*
  2479. * Since the above loop did a BUF_TRYLOCK, it is
  2480. * possible that there is space for this request.
  2481. */
  2482. if (notinit || blen < ap->minlen)
  2483. args.minlen = ap->minlen;
  2484. /*
  2485. * If the best seen length is less than the request
  2486. * length, use the best as the minimum.
  2487. */
  2488. else if (blen < ap->alen)
  2489. args.minlen = blen;
  2490. /*
  2491. * Otherwise we've seen an extent as big as alen,
  2492. * use that as the minimum.
  2493. */
  2494. else
  2495. args.minlen = ap->alen;
  2496. } else if (ap->low) {
  2497. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2498. args.total = args.minlen = ap->minlen;
  2499. } else {
  2500. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2501. args.total = ap->total;
  2502. args.minlen = ap->minlen;
  2503. }
  2504. if (ap->ip->i_d.di_extsize) {
  2505. args.prod = ap->ip->i_d.di_extsize;
  2506. if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
  2507. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2508. } else if (mp->m_sb.sb_blocksize >= NBPP) {
  2509. args.prod = 1;
  2510. args.mod = 0;
  2511. } else {
  2512. args.prod = NBPP >> mp->m_sb.sb_blocklog;
  2513. if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
  2514. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2515. }
  2516. /*
  2517. * If we are not low on available data blocks, and the
  2518. * underlying logical volume manager is a stripe, and
  2519. * the file offset is zero then try to allocate data
  2520. * blocks on stripe unit boundary.
  2521. * NOTE: ap->aeof is only set if the allocation length
  2522. * is >= the stripe unit and the allocation offset is
  2523. * at the end of file.
  2524. */
  2525. if (!ap->low && ap->aeof) {
  2526. if (!ap->off) {
  2527. args.alignment = mp->m_dalign;
  2528. atype = args.type;
  2529. isaligned = 1;
  2530. /*
  2531. * Adjust for alignment
  2532. */
  2533. if (blen > args.alignment && blen <= ap->alen)
  2534. args.minlen = blen - args.alignment;
  2535. args.minalignslop = 0;
  2536. } else {
  2537. /*
  2538. * First try an exact bno allocation.
  2539. * If it fails then do a near or start bno
  2540. * allocation with alignment turned on.
  2541. */
  2542. atype = args.type;
  2543. tryagain = 1;
  2544. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2545. args.alignment = 1;
  2546. /*
  2547. * Compute the minlen+alignment for the
  2548. * next case. Set slop so that the value
  2549. * of minlen+alignment+slop doesn't go up
  2550. * between the calls.
  2551. */
  2552. if (blen > mp->m_dalign && blen <= ap->alen)
  2553. nextminlen = blen - mp->m_dalign;
  2554. else
  2555. nextminlen = args.minlen;
  2556. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2557. args.minalignslop =
  2558. nextminlen + mp->m_dalign -
  2559. args.minlen - 1;
  2560. else
  2561. args.minalignslop = 0;
  2562. }
  2563. } else {
  2564. args.alignment = 1;
  2565. args.minalignslop = 0;
  2566. }
  2567. args.minleft = ap->minleft;
  2568. args.wasdel = ap->wasdel;
  2569. args.isfl = 0;
  2570. args.userdata = ap->userdata;
  2571. if ((error = xfs_alloc_vextent(&args)))
  2572. return error;
  2573. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2574. /*
  2575. * Exact allocation failed. Now try with alignment
  2576. * turned on.
  2577. */
  2578. args.type = atype;
  2579. args.fsbno = ap->rval;
  2580. args.alignment = mp->m_dalign;
  2581. args.minlen = nextminlen;
  2582. args.minalignslop = 0;
  2583. isaligned = 1;
  2584. if ((error = xfs_alloc_vextent(&args)))
  2585. return error;
  2586. }
  2587. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2588. /*
  2589. * allocation failed, so turn off alignment and
  2590. * try again.
  2591. */
  2592. args.type = atype;
  2593. args.fsbno = ap->rval;
  2594. args.alignment = 0;
  2595. if ((error = xfs_alloc_vextent(&args)))
  2596. return error;
  2597. }
  2598. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2599. args.minlen > ap->minlen) {
  2600. args.minlen = ap->minlen;
  2601. args.type = XFS_ALLOCTYPE_START_BNO;
  2602. args.fsbno = ap->rval;
  2603. if ((error = xfs_alloc_vextent(&args)))
  2604. return error;
  2605. }
  2606. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2607. args.fsbno = 0;
  2608. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2609. args.total = ap->minlen;
  2610. args.minleft = 0;
  2611. if ((error = xfs_alloc_vextent(&args)))
  2612. return error;
  2613. ap->low = 1;
  2614. }
  2615. if (args.fsbno != NULLFSBLOCK) {
  2616. ap->firstblock = ap->rval = args.fsbno;
  2617. ASSERT(nullfb || fb_agno == args.agno ||
  2618. (ap->low && fb_agno < args.agno));
  2619. ap->alen = args.len;
  2620. ap->ip->i_d.di_nblocks += args.len;
  2621. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2622. if (ap->wasdel)
  2623. ap->ip->i_delayed_blks -= args.len;
  2624. /*
  2625. * Adjust the disk quota also. This was reserved
  2626. * earlier.
  2627. */
  2628. XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
  2629. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2630. XFS_TRANS_DQ_BCOUNT,
  2631. (long) args.len);
  2632. } else {
  2633. ap->rval = NULLFSBLOCK;
  2634. ap->alen = 0;
  2635. }
  2636. }
  2637. return 0;
  2638. #undef ISVALID
  2639. }
  2640. /*
  2641. * Transform a btree format file with only one leaf node, where the
  2642. * extents list will fit in the inode, into an extents format file.
  2643. * Since the extent list is already in-core, all we have to do is
  2644. * give up the space for the btree root and pitch the leaf block.
  2645. */
  2646. STATIC int /* error */
  2647. xfs_bmap_btree_to_extents(
  2648. xfs_trans_t *tp, /* transaction pointer */
  2649. xfs_inode_t *ip, /* incore inode pointer */
  2650. xfs_btree_cur_t *cur, /* btree cursor */
  2651. int *logflagsp, /* inode logging flags */
  2652. int whichfork) /* data or attr fork */
  2653. {
  2654. /* REFERENCED */
  2655. xfs_bmbt_block_t *cblock;/* child btree block */
  2656. xfs_fsblock_t cbno; /* child block number */
  2657. xfs_buf_t *cbp; /* child block's buffer */
  2658. int error; /* error return value */
  2659. xfs_ifork_t *ifp; /* inode fork data */
  2660. xfs_mount_t *mp; /* mount point structure */
  2661. xfs_bmbt_ptr_t *pp; /* ptr to block address */
  2662. xfs_bmbt_block_t *rblock;/* root btree block */
  2663. ifp = XFS_IFORK_PTR(ip, whichfork);
  2664. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2665. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2666. rblock = ifp->if_broot;
  2667. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2668. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2669. ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
  2670. mp = ip->i_mount;
  2671. pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
  2672. *logflagsp = 0;
  2673. #ifdef DEBUG
  2674. if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
  2675. return error;
  2676. #endif
  2677. cbno = INT_GET(*pp, ARCH_CONVERT);
  2678. if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
  2679. XFS_BMAP_BTREE_REF)))
  2680. return error;
  2681. cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
  2682. if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
  2683. return error;
  2684. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2685. ip->i_d.di_nblocks--;
  2686. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2687. xfs_trans_binval(tp, cbp);
  2688. if (cur->bc_bufs[0] == cbp)
  2689. cur->bc_bufs[0] = NULL;
  2690. xfs_iroot_realloc(ip, -1, whichfork);
  2691. ASSERT(ifp->if_broot == NULL);
  2692. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2693. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2694. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2695. return 0;
  2696. }
  2697. /*
  2698. * Called by xfs_bmapi to update extent list structure and the btree
  2699. * after removing space (or undoing a delayed allocation).
  2700. */
  2701. STATIC int /* error */
  2702. xfs_bmap_del_extent(
  2703. xfs_inode_t *ip, /* incore inode pointer */
  2704. xfs_trans_t *tp, /* current transaction pointer */
  2705. xfs_extnum_t idx, /* extent number to update/delete */
  2706. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2707. xfs_btree_cur_t *cur, /* if null, not a btree */
  2708. xfs_bmbt_irec_t *del, /* data to remove from extent list */
  2709. int *logflagsp, /* inode logging flags */
  2710. int whichfork, /* data or attr fork */
  2711. int rsvd) /* OK to allocate reserved blocks */
  2712. {
  2713. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2714. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2715. xfs_fsblock_t del_endblock=0; /* first block past del */
  2716. xfs_fileoff_t del_endoff; /* first offset past del */
  2717. int delay; /* current block is delayed allocated */
  2718. int do_fx; /* free extent at end of routine */
  2719. xfs_bmbt_rec_t *ep; /* current extent entry pointer */
  2720. int error; /* error return value */
  2721. int flags; /* inode logging flags */
  2722. #ifdef XFS_BMAP_TRACE
  2723. static char fname[] = "xfs_bmap_del_extent";
  2724. #endif
  2725. xfs_bmbt_irec_t got; /* current extent entry */
  2726. xfs_fileoff_t got_endoff; /* first offset past got */
  2727. int i; /* temp state */
  2728. xfs_ifork_t *ifp; /* inode fork pointer */
  2729. xfs_mount_t *mp; /* mount structure */
  2730. xfs_filblks_t nblks; /* quota/sb block count */
  2731. xfs_bmbt_irec_t new; /* new record to be inserted */
  2732. /* REFERENCED */
  2733. xfs_extnum_t nextents; /* number of extents in list */
  2734. uint qfield; /* quota field to update */
  2735. xfs_filblks_t temp; /* for indirect length calculations */
  2736. xfs_filblks_t temp2; /* for indirect length calculations */
  2737. XFS_STATS_INC(xs_del_exlist);
  2738. mp = ip->i_mount;
  2739. ifp = XFS_IFORK_PTR(ip, whichfork);
  2740. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2741. ASSERT(idx >= 0 && idx < nextents);
  2742. ASSERT(del->br_blockcount > 0);
  2743. ep = &ifp->if_u1.if_extents[idx];
  2744. xfs_bmbt_get_all(ep, &got);
  2745. ASSERT(got.br_startoff <= del->br_startoff);
  2746. del_endoff = del->br_startoff + del->br_blockcount;
  2747. got_endoff = got.br_startoff + got.br_blockcount;
  2748. ASSERT(got_endoff >= del_endoff);
  2749. delay = ISNULLSTARTBLOCK(got.br_startblock);
  2750. ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
  2751. flags = 0;
  2752. qfield = 0;
  2753. error = 0;
  2754. /*
  2755. * If deleting a real allocation, must free up the disk space.
  2756. */
  2757. if (!delay) {
  2758. flags = XFS_ILOG_CORE;
  2759. /*
  2760. * Realtime allocation. Free it and record di_nblocks update.
  2761. */
  2762. if (whichfork == XFS_DATA_FORK &&
  2763. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
  2764. xfs_fsblock_t bno;
  2765. xfs_filblks_t len;
  2766. ASSERT(do_mod(del->br_blockcount,
  2767. mp->m_sb.sb_rextsize) == 0);
  2768. ASSERT(do_mod(del->br_startblock,
  2769. mp->m_sb.sb_rextsize) == 0);
  2770. bno = del->br_startblock;
  2771. len = del->br_blockcount;
  2772. do_div(bno, mp->m_sb.sb_rextsize);
  2773. do_div(len, mp->m_sb.sb_rextsize);
  2774. if ((error = xfs_rtfree_extent(ip->i_transp, bno,
  2775. (xfs_extlen_t)len)))
  2776. goto done;
  2777. do_fx = 0;
  2778. nblks = len * mp->m_sb.sb_rextsize;
  2779. qfield = XFS_TRANS_DQ_RTBCOUNT;
  2780. }
  2781. /*
  2782. * Ordinary allocation.
  2783. */
  2784. else {
  2785. do_fx = 1;
  2786. nblks = del->br_blockcount;
  2787. qfield = XFS_TRANS_DQ_BCOUNT;
  2788. }
  2789. /*
  2790. * Set up del_endblock and cur for later.
  2791. */
  2792. del_endblock = del->br_startblock + del->br_blockcount;
  2793. if (cur) {
  2794. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  2795. got.br_startblock, got.br_blockcount,
  2796. &i)))
  2797. goto done;
  2798. ASSERT(i == 1);
  2799. }
  2800. da_old = da_new = 0;
  2801. } else {
  2802. da_old = STARTBLOCKVAL(got.br_startblock);
  2803. da_new = 0;
  2804. nblks = 0;
  2805. do_fx = 0;
  2806. }
  2807. /*
  2808. * Set flag value to use in switch statement.
  2809. * Left-contig is 2, right-contig is 1.
  2810. */
  2811. switch (((got.br_startoff == del->br_startoff) << 1) |
  2812. (got_endoff == del_endoff)) {
  2813. case 3:
  2814. /*
  2815. * Matches the whole extent. Delete the entry.
  2816. */
  2817. xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
  2818. xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
  2819. ifp->if_lastex = idx;
  2820. if (delay)
  2821. break;
  2822. XFS_IFORK_NEXT_SET(ip, whichfork,
  2823. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2824. flags |= XFS_ILOG_CORE;
  2825. if (!cur) {
  2826. flags |= XFS_ILOG_FEXT(whichfork);
  2827. break;
  2828. }
  2829. if ((error = xfs_bmbt_delete(cur, &i)))
  2830. goto done;
  2831. ASSERT(i == 1);
  2832. break;
  2833. case 2:
  2834. /*
  2835. * Deleting the first part of the extent.
  2836. */
  2837. xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
  2838. xfs_bmbt_set_startoff(ep, del_endoff);
  2839. temp = got.br_blockcount - del->br_blockcount;
  2840. xfs_bmbt_set_blockcount(ep, temp);
  2841. ifp->if_lastex = idx;
  2842. if (delay) {
  2843. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2844. da_old);
  2845. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  2846. xfs_bmap_trace_post_update(fname, "2", ip, idx,
  2847. whichfork);
  2848. da_new = temp;
  2849. break;
  2850. }
  2851. xfs_bmbt_set_startblock(ep, del_endblock);
  2852. xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
  2853. if (!cur) {
  2854. flags |= XFS_ILOG_FEXT(whichfork);
  2855. break;
  2856. }
  2857. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  2858. got.br_blockcount - del->br_blockcount,
  2859. got.br_state)))
  2860. goto done;
  2861. break;
  2862. case 1:
  2863. /*
  2864. * Deleting the last part of the extent.
  2865. */
  2866. temp = got.br_blockcount - del->br_blockcount;
  2867. xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
  2868. xfs_bmbt_set_blockcount(ep, temp);
  2869. ifp->if_lastex = idx;
  2870. if (delay) {
  2871. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2872. da_old);
  2873. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  2874. xfs_bmap_trace_post_update(fname, "1", ip, idx,
  2875. whichfork);
  2876. da_new = temp;
  2877. break;
  2878. }
  2879. xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
  2880. if (!cur) {
  2881. flags |= XFS_ILOG_FEXT(whichfork);
  2882. break;
  2883. }
  2884. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  2885. got.br_startblock,
  2886. got.br_blockcount - del->br_blockcount,
  2887. got.br_state)))
  2888. goto done;
  2889. break;
  2890. case 0:
  2891. /*
  2892. * Deleting the middle of the extent.
  2893. */
  2894. temp = del->br_startoff - got.br_startoff;
  2895. xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
  2896. xfs_bmbt_set_blockcount(ep, temp);
  2897. new.br_startoff = del_endoff;
  2898. temp2 = got_endoff - del_endoff;
  2899. new.br_blockcount = temp2;
  2900. new.br_state = got.br_state;
  2901. if (!delay) {
  2902. new.br_startblock = del_endblock;
  2903. flags |= XFS_ILOG_CORE;
  2904. if (cur) {
  2905. if ((error = xfs_bmbt_update(cur,
  2906. got.br_startoff,
  2907. got.br_startblock, temp,
  2908. got.br_state)))
  2909. goto done;
  2910. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  2911. goto done;
  2912. cur->bc_rec.b = new;
  2913. error = xfs_bmbt_insert(cur, &i);
  2914. if (error && error != ENOSPC)
  2915. goto done;
  2916. /*
  2917. * If get no-space back from btree insert,
  2918. * it tried a split, and we have a zero
  2919. * block reservation.
  2920. * Fix up our state and return the error.
  2921. */
  2922. if (error == ENOSPC) {
  2923. /*
  2924. * Reset the cursor, don't trust
  2925. * it after any insert operation.
  2926. */
  2927. if ((error = xfs_bmbt_lookup_eq(cur,
  2928. got.br_startoff,
  2929. got.br_startblock,
  2930. temp, &i)))
  2931. goto done;
  2932. ASSERT(i == 1);
  2933. /*
  2934. * Update the btree record back
  2935. * to the original value.
  2936. */
  2937. if ((error = xfs_bmbt_update(cur,
  2938. got.br_startoff,
  2939. got.br_startblock,
  2940. got.br_blockcount,
  2941. got.br_state)))
  2942. goto done;
  2943. /*
  2944. * Reset the extent record back
  2945. * to the original value.
  2946. */
  2947. xfs_bmbt_set_blockcount(ep,
  2948. got.br_blockcount);
  2949. flags = 0;
  2950. error = XFS_ERROR(ENOSPC);
  2951. goto done;
  2952. }
  2953. ASSERT(i == 1);
  2954. } else
  2955. flags |= XFS_ILOG_FEXT(whichfork);
  2956. XFS_IFORK_NEXT_SET(ip, whichfork,
  2957. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2958. } else {
  2959. ASSERT(whichfork == XFS_DATA_FORK);
  2960. temp = xfs_bmap_worst_indlen(ip, temp);
  2961. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  2962. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  2963. new.br_startblock = NULLSTARTBLOCK((int)temp2);
  2964. da_new = temp + temp2;
  2965. while (da_new > da_old) {
  2966. if (temp) {
  2967. temp--;
  2968. da_new--;
  2969. xfs_bmbt_set_startblock(ep,
  2970. NULLSTARTBLOCK((int)temp));
  2971. }
  2972. if (da_new == da_old)
  2973. break;
  2974. if (temp2) {
  2975. temp2--;
  2976. da_new--;
  2977. new.br_startblock =
  2978. NULLSTARTBLOCK((int)temp2);
  2979. }
  2980. }
  2981. }
  2982. xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
  2983. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
  2984. whichfork);
  2985. xfs_bmap_insert_exlist(ip, idx + 1, 1, &new, whichfork);
  2986. ifp->if_lastex = idx + 1;
  2987. break;
  2988. }
  2989. /*
  2990. * If we need to, add to list of extents to delete.
  2991. */
  2992. if (do_fx)
  2993. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  2994. mp);
  2995. /*
  2996. * Adjust inode # blocks in the file.
  2997. */
  2998. if (nblks)
  2999. ip->i_d.di_nblocks -= nblks;
  3000. /*
  3001. * Adjust quota data.
  3002. */
  3003. if (qfield)
  3004. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
  3005. /*
  3006. * Account for change in delayed indirect blocks.
  3007. * Nothing to do for disk quota accounting here.
  3008. */
  3009. ASSERT(da_old >= da_new);
  3010. if (da_old > da_new)
  3011. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
  3012. rsvd);
  3013. done:
  3014. *logflagsp = flags;
  3015. return error;
  3016. }
  3017. /*
  3018. * Remove the entry "free" from the free item list. Prev points to the
  3019. * previous entry, unless "free" is the head of the list.
  3020. */
  3021. STATIC void
  3022. xfs_bmap_del_free(
  3023. xfs_bmap_free_t *flist, /* free item list header */
  3024. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  3025. xfs_bmap_free_item_t *free) /* list item to be freed */
  3026. {
  3027. if (prev)
  3028. prev->xbfi_next = free->xbfi_next;
  3029. else
  3030. flist->xbf_first = free->xbfi_next;
  3031. flist->xbf_count--;
  3032. kmem_zone_free(xfs_bmap_free_item_zone, free);
  3033. }
  3034. /*
  3035. * Remove count entries from the extents array for inode "ip", starting
  3036. * at index "idx". Copies the remaining items down over the deleted ones,
  3037. * and gives back the excess memory.
  3038. */
  3039. STATIC void
  3040. xfs_bmap_delete_exlist(
  3041. xfs_inode_t *ip, /* incore inode pointer */
  3042. xfs_extnum_t idx, /* starting delete index */
  3043. xfs_extnum_t count, /* count of items to delete */
  3044. int whichfork) /* data or attr fork */
  3045. {
  3046. xfs_bmbt_rec_t *base; /* base of extent list */
  3047. xfs_ifork_t *ifp; /* inode fork pointer */
  3048. xfs_extnum_t nextents; /* number of extents in list after */
  3049. ifp = XFS_IFORK_PTR(ip, whichfork);
  3050. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3051. base = ifp->if_u1.if_extents;
  3052. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - count;
  3053. memmove(&base[idx], &base[idx + count],
  3054. (nextents - idx) * sizeof(*base));
  3055. xfs_iext_realloc(ip, -count, whichfork);
  3056. }
  3057. /*
  3058. * Convert an extents-format file into a btree-format file.
  3059. * The new file will have a root block (in the inode) and a single child block.
  3060. */
  3061. STATIC int /* error */
  3062. xfs_bmap_extents_to_btree(
  3063. xfs_trans_t *tp, /* transaction pointer */
  3064. xfs_inode_t *ip, /* incore inode pointer */
  3065. xfs_fsblock_t *firstblock, /* first-block-allocated */
  3066. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  3067. xfs_btree_cur_t **curp, /* cursor returned to caller */
  3068. int wasdel, /* converting a delayed alloc */
  3069. int *logflagsp, /* inode logging flags */
  3070. int whichfork) /* data or attr fork */
  3071. {
  3072. xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
  3073. xfs_buf_t *abp; /* buffer for ablock */
  3074. xfs_alloc_arg_t args; /* allocation arguments */
  3075. xfs_bmbt_rec_t *arp; /* child record pointer */
  3076. xfs_bmbt_block_t *block; /* btree root block */
  3077. xfs_btree_cur_t *cur; /* bmap btree cursor */
  3078. xfs_bmbt_rec_t *ep; /* extent list pointer */
  3079. int error; /* error return value */
  3080. xfs_extnum_t i, cnt; /* extent list index */
  3081. xfs_ifork_t *ifp; /* inode fork pointer */
  3082. xfs_bmbt_key_t *kp; /* root block key pointer */
  3083. xfs_mount_t *mp; /* mount structure */
  3084. xfs_extnum_t nextents; /* extent list size */
  3085. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  3086. ifp = XFS_IFORK_PTR(ip, whichfork);
  3087. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  3088. ASSERT(ifp->if_ext_max ==
  3089. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  3090. /*
  3091. * Make space in the inode incore.
  3092. */
  3093. xfs_iroot_realloc(ip, 1, whichfork);
  3094. ifp->if_flags |= XFS_IFBROOT;
  3095. /*
  3096. * Fill in the root.
  3097. */
  3098. block = ifp->if_broot;
  3099. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3100. block->bb_level = cpu_to_be16(1);
  3101. block->bb_numrecs = cpu_to_be16(1);
  3102. block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3103. block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3104. /*
  3105. * Need a cursor. Can't allocate until bb_level is filled in.
  3106. */
  3107. mp = ip->i_mount;
  3108. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  3109. whichfork);
  3110. cur->bc_private.b.firstblock = *firstblock;
  3111. cur->bc_private.b.flist = flist;
  3112. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3113. /*
  3114. * Convert to a btree with two levels, one record in root.
  3115. */
  3116. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  3117. args.tp = tp;
  3118. args.mp = mp;
  3119. if (*firstblock == NULLFSBLOCK) {
  3120. args.type = XFS_ALLOCTYPE_START_BNO;
  3121. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  3122. } else if (flist->xbf_low) {
  3123. args.type = XFS_ALLOCTYPE_START_BNO;
  3124. args.fsbno = *firstblock;
  3125. } else {
  3126. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3127. args.fsbno = *firstblock;
  3128. }
  3129. args.minlen = args.maxlen = args.prod = 1;
  3130. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  3131. args.minalignslop = 0;
  3132. args.wasdel = wasdel;
  3133. *logflagsp = 0;
  3134. if ((error = xfs_alloc_vextent(&args))) {
  3135. xfs_iroot_realloc(ip, -1, whichfork);
  3136. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  3137. return error;
  3138. }
  3139. /*
  3140. * Allocation can't fail, the space was reserved.
  3141. */
  3142. ASSERT(args.fsbno != NULLFSBLOCK);
  3143. ASSERT(*firstblock == NULLFSBLOCK ||
  3144. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  3145. (flist->xbf_low &&
  3146. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  3147. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  3148. cur->bc_private.b.allocated++;
  3149. ip->i_d.di_nblocks++;
  3150. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  3151. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  3152. /*
  3153. * Fill in the child block.
  3154. */
  3155. ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
  3156. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3157. ablock->bb_level = 0;
  3158. ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3159. ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3160. arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
  3161. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3162. for (ep = ifp->if_u1.if_extents, cnt = i = 0; i < nextents; i++, ep++) {
  3163. if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
  3164. arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
  3165. arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
  3166. arp++; cnt++;
  3167. }
  3168. }
  3169. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  3170. ablock->bb_numrecs = cpu_to_be16(cnt);
  3171. /*
  3172. * Fill in the root key and pointer.
  3173. */
  3174. kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
  3175. arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
  3176. INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
  3177. pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
  3178. INT_SET(*pp, ARCH_CONVERT, args.fsbno);
  3179. /*
  3180. * Do all this logging at the end so that
  3181. * the root is at the right level.
  3182. */
  3183. xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
  3184. xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  3185. ASSERT(*curp == NULL);
  3186. *curp = cur;
  3187. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
  3188. return 0;
  3189. }
  3190. /*
  3191. * Insert new item(s) in the extent list for inode "ip".
  3192. * Count new items are inserted at offset idx.
  3193. */
  3194. STATIC void
  3195. xfs_bmap_insert_exlist(
  3196. xfs_inode_t *ip, /* incore inode pointer */
  3197. xfs_extnum_t idx, /* starting index of new items */
  3198. xfs_extnum_t count, /* number of inserted items */
  3199. xfs_bmbt_irec_t *new, /* items to insert */
  3200. int whichfork) /* data or attr fork */
  3201. {
  3202. xfs_bmbt_rec_t *base; /* extent list base */
  3203. xfs_ifork_t *ifp; /* inode fork pointer */
  3204. xfs_extnum_t nextents; /* extent list size */
  3205. xfs_extnum_t to; /* extent list index */
  3206. ifp = XFS_IFORK_PTR(ip, whichfork);
  3207. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3208. xfs_iext_realloc(ip, count, whichfork);
  3209. base = ifp->if_u1.if_extents;
  3210. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3211. memmove(&base[idx + count], &base[idx],
  3212. (nextents - (idx + count)) * sizeof(*base));
  3213. for (to = idx; to < idx + count; to++, new++)
  3214. xfs_bmbt_set_all(&base[to], new);
  3215. }
  3216. /*
  3217. * Helper routine to reset inode di_forkoff field when switching
  3218. * attribute fork from local to extent format - we reset it where
  3219. * possible to make space available for inline data fork extents.
  3220. */
  3221. STATIC void
  3222. xfs_bmap_forkoff_reset(
  3223. xfs_mount_t *mp,
  3224. xfs_inode_t *ip,
  3225. int whichfork)
  3226. {
  3227. if (whichfork == XFS_ATTR_FORK &&
  3228. (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
  3229. (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
  3230. ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
  3231. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3232. ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
  3233. (uint)sizeof(xfs_bmbt_rec_t);
  3234. ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
  3235. (uint)sizeof(xfs_bmbt_rec_t);
  3236. }
  3237. }
  3238. /*
  3239. * Convert a local file to an extents file.
  3240. * This code is out of bounds for data forks of regular files,
  3241. * since the file data needs to get logged so things will stay consistent.
  3242. * (The bmap-level manipulations are ok, though).
  3243. */
  3244. STATIC int /* error */
  3245. xfs_bmap_local_to_extents(
  3246. xfs_trans_t *tp, /* transaction pointer */
  3247. xfs_inode_t *ip, /* incore inode pointer */
  3248. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3249. xfs_extlen_t total, /* total blocks needed by transaction */
  3250. int *logflagsp, /* inode logging flags */
  3251. int whichfork) /* data or attr fork */
  3252. {
  3253. int error; /* error return value */
  3254. int flags; /* logging flags returned */
  3255. #ifdef XFS_BMAP_TRACE
  3256. static char fname[] = "xfs_bmap_local_to_extents";
  3257. #endif
  3258. xfs_ifork_t *ifp; /* inode fork pointer */
  3259. /*
  3260. * We don't want to deal with the case of keeping inode data inline yet.
  3261. * So sending the data fork of a regular inode is invalid.
  3262. */
  3263. ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
  3264. whichfork == XFS_DATA_FORK));
  3265. ifp = XFS_IFORK_PTR(ip, whichfork);
  3266. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3267. flags = 0;
  3268. error = 0;
  3269. if (ifp->if_bytes) {
  3270. xfs_alloc_arg_t args; /* allocation arguments */
  3271. xfs_buf_t *bp; /* buffer for extent list block */
  3272. xfs_bmbt_rec_t *ep; /* extent list pointer */
  3273. args.tp = tp;
  3274. args.mp = ip->i_mount;
  3275. ASSERT(ifp->if_flags & XFS_IFINLINE);
  3276. /*
  3277. * Allocate a block. We know we need only one, since the
  3278. * file currently fits in an inode.
  3279. */
  3280. if (*firstblock == NULLFSBLOCK) {
  3281. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3282. args.type = XFS_ALLOCTYPE_START_BNO;
  3283. } else {
  3284. args.fsbno = *firstblock;
  3285. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3286. }
  3287. args.total = total;
  3288. args.mod = args.minleft = args.alignment = args.wasdel =
  3289. args.isfl = args.minalignslop = 0;
  3290. args.minlen = args.maxlen = args.prod = 1;
  3291. if ((error = xfs_alloc_vextent(&args)))
  3292. goto done;
  3293. /*
  3294. * Can't fail, the space was reserved.
  3295. */
  3296. ASSERT(args.fsbno != NULLFSBLOCK);
  3297. ASSERT(args.len == 1);
  3298. *firstblock = args.fsbno;
  3299. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3300. memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
  3301. ifp->if_bytes);
  3302. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3303. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3304. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3305. xfs_iext_realloc(ip, 1, whichfork);
  3306. ep = ifp->if_u1.if_extents;
  3307. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3308. xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
  3309. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3310. ip->i_d.di_nblocks = 1;
  3311. XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
  3312. XFS_TRANS_DQ_BCOUNT, 1L);
  3313. flags |= XFS_ILOG_FEXT(whichfork);
  3314. } else {
  3315. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3316. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3317. }
  3318. ifp->if_flags &= ~XFS_IFINLINE;
  3319. ifp->if_flags |= XFS_IFEXTENTS;
  3320. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3321. flags |= XFS_ILOG_CORE;
  3322. done:
  3323. *logflagsp = flags;
  3324. return error;
  3325. }
  3326. xfs_bmbt_rec_t * /* pointer to found extent entry */
  3327. xfs_bmap_do_search_extents(
  3328. xfs_bmbt_rec_t *base, /* base of extent list */
  3329. xfs_extnum_t lastx, /* last extent index used */
  3330. xfs_extnum_t nextents, /* extent list size */
  3331. xfs_fileoff_t bno, /* block number searched for */
  3332. int *eofp, /* out: end of file found */
  3333. xfs_extnum_t *lastxp, /* out: last extent index */
  3334. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3335. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3336. {
  3337. xfs_bmbt_rec_t *ep; /* extent list entry pointer */
  3338. xfs_bmbt_irec_t got; /* extent list entry, decoded */
  3339. int high; /* high index of binary search */
  3340. int low; /* low index of binary search */
  3341. /*
  3342. * Initialize the extent entry structure to catch access to
  3343. * uninitialized br_startblock field.
  3344. */
  3345. got.br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3346. got.br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3347. got.br_state = XFS_EXT_INVALID;
  3348. #if XFS_BIG_BLKNOS
  3349. got.br_startblock = 0xffffa5a5a5a5a5a5LL;
  3350. #else
  3351. got.br_startblock = 0xffffa5a5;
  3352. #endif
  3353. if (lastx != NULLEXTNUM && lastx < nextents)
  3354. ep = base + lastx;
  3355. else
  3356. ep = NULL;
  3357. prevp->br_startoff = NULLFILEOFF;
  3358. if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
  3359. bno < got.br_startoff +
  3360. (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
  3361. *eofp = 0;
  3362. else if (ep && lastx < nextents - 1 &&
  3363. bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
  3364. bno < got.br_startoff +
  3365. (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
  3366. lastx++;
  3367. ep++;
  3368. *eofp = 0;
  3369. } else if (nextents == 0)
  3370. *eofp = 1;
  3371. else if (bno == 0 &&
  3372. (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
  3373. ep = base;
  3374. lastx = 0;
  3375. got.br_blockcount = xfs_bmbt_get_blockcount(ep);
  3376. *eofp = 0;
  3377. } else {
  3378. /* binary search the extents array */
  3379. low = 0;
  3380. high = nextents - 1;
  3381. while (low <= high) {
  3382. XFS_STATS_INC(xs_cmp_exlist);
  3383. lastx = (low + high) >> 1;
  3384. ep = base + lastx;
  3385. got.br_startoff = xfs_bmbt_get_startoff(ep);
  3386. got.br_blockcount = xfs_bmbt_get_blockcount(ep);
  3387. if (bno < got.br_startoff)
  3388. high = lastx - 1;
  3389. else if (bno >= got.br_startoff + got.br_blockcount)
  3390. low = lastx + 1;
  3391. else {
  3392. got.br_startblock = xfs_bmbt_get_startblock(ep);
  3393. got.br_state = xfs_bmbt_get_state(ep);
  3394. *eofp = 0;
  3395. *lastxp = lastx;
  3396. *gotp = got;
  3397. return ep;
  3398. }
  3399. }
  3400. if (bno >= got.br_startoff + got.br_blockcount) {
  3401. lastx++;
  3402. if (lastx == nextents) {
  3403. *eofp = 1;
  3404. got.br_startblock = xfs_bmbt_get_startblock(ep);
  3405. got.br_state = xfs_bmbt_get_state(ep);
  3406. *prevp = got;
  3407. ep = NULL;
  3408. } else {
  3409. *eofp = 0;
  3410. xfs_bmbt_get_all(ep, prevp);
  3411. ep++;
  3412. got.br_startoff = xfs_bmbt_get_startoff(ep);
  3413. got.br_blockcount = xfs_bmbt_get_blockcount(ep);
  3414. }
  3415. } else {
  3416. *eofp = 0;
  3417. if (ep > base)
  3418. xfs_bmbt_get_all(ep - 1, prevp);
  3419. }
  3420. }
  3421. if (ep) {
  3422. got.br_startblock = xfs_bmbt_get_startblock(ep);
  3423. got.br_state = xfs_bmbt_get_state(ep);
  3424. }
  3425. *lastxp = lastx;
  3426. *gotp = got;
  3427. return ep;
  3428. }
  3429. /*
  3430. * Search the extents list for the inode, for the extent containing bno.
  3431. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3432. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3433. * Else, *lastxp will be set to the index of the found
  3434. * entry; *gotp will contain the entry.
  3435. */
  3436. STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
  3437. xfs_bmap_search_extents(
  3438. xfs_inode_t *ip, /* incore inode pointer */
  3439. xfs_fileoff_t bno, /* block number searched for */
  3440. int whichfork, /* data or attr fork */
  3441. int *eofp, /* out: end of file found */
  3442. xfs_extnum_t *lastxp, /* out: last extent index */
  3443. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3444. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3445. {
  3446. xfs_ifork_t *ifp; /* inode fork pointer */
  3447. xfs_bmbt_rec_t *base; /* base of extent list */
  3448. xfs_extnum_t lastx; /* last extent index used */
  3449. xfs_extnum_t nextents; /* extent list size */
  3450. xfs_bmbt_rec_t *ep; /* extent list entry pointer */
  3451. int rt; /* realtime flag */
  3452. XFS_STATS_INC(xs_look_exlist);
  3453. ifp = XFS_IFORK_PTR(ip, whichfork);
  3454. lastx = ifp->if_lastex;
  3455. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3456. base = &ifp->if_u1.if_extents[0];
  3457. ep = xfs_bmap_do_search_extents(base, lastx, nextents, bno, eofp,
  3458. lastxp, gotp, prevp);
  3459. rt = ip->i_d.di_flags & XFS_DIFLAG_REALTIME;
  3460. if(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM)) {
  3461. cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
  3462. "start_block : %llx start_off : %llx blkcnt : %llx "
  3463. "extent-state : %x \n",
  3464. (ip->i_mount)->m_fsname,(long long)ip->i_ino,
  3465. gotp->br_startblock, gotp->br_startoff,
  3466. gotp->br_blockcount,gotp->br_state);
  3467. }
  3468. return ep;
  3469. }
  3470. #ifdef XFS_BMAP_TRACE
  3471. ktrace_t *xfs_bmap_trace_buf;
  3472. /*
  3473. * Add a bmap trace buffer entry. Base routine for the others.
  3474. */
  3475. STATIC void
  3476. xfs_bmap_trace_addentry(
  3477. int opcode, /* operation */
  3478. char *fname, /* function name */
  3479. char *desc, /* operation description */
  3480. xfs_inode_t *ip, /* incore inode pointer */
  3481. xfs_extnum_t idx, /* index of entry(ies) */
  3482. xfs_extnum_t cnt, /* count of entries, 1 or 2 */
  3483. xfs_bmbt_rec_t *r1, /* first record */
  3484. xfs_bmbt_rec_t *r2, /* second record or null */
  3485. int whichfork) /* data or attr fork */
  3486. {
  3487. xfs_bmbt_rec_t tr2;
  3488. ASSERT(cnt == 1 || cnt == 2);
  3489. ASSERT(r1 != NULL);
  3490. if (cnt == 1) {
  3491. ASSERT(r2 == NULL);
  3492. r2 = &tr2;
  3493. memset(&tr2, 0, sizeof(tr2));
  3494. } else
  3495. ASSERT(r2 != NULL);
  3496. ktrace_enter(xfs_bmap_trace_buf,
  3497. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3498. (void *)fname, (void *)desc, (void *)ip,
  3499. (void *)(__psint_t)idx,
  3500. (void *)(__psint_t)cnt,
  3501. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3502. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3503. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3504. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3505. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3506. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3507. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3508. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3509. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3510. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3511. );
  3512. ASSERT(ip->i_xtrace);
  3513. ktrace_enter(ip->i_xtrace,
  3514. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3515. (void *)fname, (void *)desc, (void *)ip,
  3516. (void *)(__psint_t)idx,
  3517. (void *)(__psint_t)cnt,
  3518. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3519. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3520. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3521. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3522. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3523. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3524. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3525. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3526. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3527. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3528. );
  3529. }
  3530. /*
  3531. * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
  3532. */
  3533. STATIC void
  3534. xfs_bmap_trace_delete(
  3535. char *fname, /* function name */
  3536. char *desc, /* operation description */
  3537. xfs_inode_t *ip, /* incore inode pointer */
  3538. xfs_extnum_t idx, /* index of entry(entries) deleted */
  3539. xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
  3540. int whichfork) /* data or attr fork */
  3541. {
  3542. xfs_ifork_t *ifp; /* inode fork pointer */
  3543. ifp = XFS_IFORK_PTR(ip, whichfork);
  3544. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
  3545. cnt, &ifp->if_u1.if_extents[idx],
  3546. cnt == 2 ? &ifp->if_u1.if_extents[idx + 1] : NULL,
  3547. whichfork);
  3548. }
  3549. /*
  3550. * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
  3551. * reading in the extents list from the disk (in the btree).
  3552. */
  3553. STATIC void
  3554. xfs_bmap_trace_insert(
  3555. char *fname, /* function name */
  3556. char *desc, /* operation description */
  3557. xfs_inode_t *ip, /* incore inode pointer */
  3558. xfs_extnum_t idx, /* index of entry(entries) inserted */
  3559. xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
  3560. xfs_bmbt_irec_t *r1, /* inserted record 1 */
  3561. xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
  3562. int whichfork) /* data or attr fork */
  3563. {
  3564. xfs_bmbt_rec_t tr1; /* compressed record 1 */
  3565. xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
  3566. xfs_bmbt_set_all(&tr1, r1);
  3567. if (cnt == 2) {
  3568. ASSERT(r2 != NULL);
  3569. xfs_bmbt_set_all(&tr2, r2);
  3570. } else {
  3571. ASSERT(cnt == 1);
  3572. ASSERT(r2 == NULL);
  3573. }
  3574. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
  3575. cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
  3576. }
  3577. /*
  3578. * Add bmap trace entry after updating an extent list entry in place.
  3579. */
  3580. STATIC void
  3581. xfs_bmap_trace_post_update(
  3582. char *fname, /* function name */
  3583. char *desc, /* operation description */
  3584. xfs_inode_t *ip, /* incore inode pointer */
  3585. xfs_extnum_t idx, /* index of entry updated */
  3586. int whichfork) /* data or attr fork */
  3587. {
  3588. xfs_ifork_t *ifp; /* inode fork pointer */
  3589. ifp = XFS_IFORK_PTR(ip, whichfork);
  3590. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
  3591. 1, &ifp->if_u1.if_extents[idx], NULL, whichfork);
  3592. }
  3593. /*
  3594. * Add bmap trace entry prior to updating an extent list entry in place.
  3595. */
  3596. STATIC void
  3597. xfs_bmap_trace_pre_update(
  3598. char *fname, /* function name */
  3599. char *desc, /* operation description */
  3600. xfs_inode_t *ip, /* incore inode pointer */
  3601. xfs_extnum_t idx, /* index of entry to be updated */
  3602. int whichfork) /* data or attr fork */
  3603. {
  3604. xfs_ifork_t *ifp; /* inode fork pointer */
  3605. ifp = XFS_IFORK_PTR(ip, whichfork);
  3606. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
  3607. &ifp->if_u1.if_extents[idx], NULL, whichfork);
  3608. }
  3609. #endif /* XFS_BMAP_TRACE */
  3610. /*
  3611. * Compute the worst-case number of indirect blocks that will be used
  3612. * for ip's delayed extent of length "len".
  3613. */
  3614. STATIC xfs_filblks_t
  3615. xfs_bmap_worst_indlen(
  3616. xfs_inode_t *ip, /* incore inode pointer */
  3617. xfs_filblks_t len) /* delayed extent length */
  3618. {
  3619. int level; /* btree level number */
  3620. int maxrecs; /* maximum record count at this level */
  3621. xfs_mount_t *mp; /* mount structure */
  3622. xfs_filblks_t rval; /* return value */
  3623. mp = ip->i_mount;
  3624. maxrecs = mp->m_bmap_dmxr[0];
  3625. for (level = 0, rval = 0;
  3626. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3627. level++) {
  3628. len += maxrecs - 1;
  3629. do_div(len, maxrecs);
  3630. rval += len;
  3631. if (len == 1)
  3632. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3633. level - 1;
  3634. if (level == 0)
  3635. maxrecs = mp->m_bmap_dmxr[1];
  3636. }
  3637. return rval;
  3638. }
  3639. #if defined(XFS_RW_TRACE)
  3640. STATIC void
  3641. xfs_bunmap_trace(
  3642. xfs_inode_t *ip,
  3643. xfs_fileoff_t bno,
  3644. xfs_filblks_t len,
  3645. int flags,
  3646. inst_t *ra)
  3647. {
  3648. if (ip->i_rwtrace == NULL)
  3649. return;
  3650. ktrace_enter(ip->i_rwtrace,
  3651. (void *)(__psint_t)XFS_BUNMAPI,
  3652. (void *)ip,
  3653. (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
  3654. (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
  3655. (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
  3656. (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
  3657. (void *)(__psint_t)len,
  3658. (void *)(__psint_t)flags,
  3659. (void *)(unsigned long)current_cpu(),
  3660. (void *)ra,
  3661. (void *)0,
  3662. (void *)0,
  3663. (void *)0,
  3664. (void *)0,
  3665. (void *)0,
  3666. (void *)0);
  3667. }
  3668. #endif
  3669. /*
  3670. * Convert inode from non-attributed to attributed.
  3671. * Must not be in a transaction, ip must not be locked.
  3672. */
  3673. int /* error code */
  3674. xfs_bmap_add_attrfork(
  3675. xfs_inode_t *ip, /* incore inode pointer */
  3676. int size, /* space new attribute needs */
  3677. int rsvd) /* xact may use reserved blks */
  3678. {
  3679. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3680. xfs_bmap_free_t flist; /* freed extent list */
  3681. xfs_mount_t *mp; /* mount structure */
  3682. xfs_trans_t *tp; /* transaction pointer */
  3683. unsigned long s; /* spinlock spl value */
  3684. int blks; /* space reservation */
  3685. int version = 1; /* superblock attr version */
  3686. int committed; /* xaction was committed */
  3687. int logflags; /* logging flags */
  3688. int error; /* error return value */
  3689. ASSERT(XFS_IFORK_Q(ip) == 0);
  3690. ASSERT(ip->i_df.if_ext_max ==
  3691. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3692. mp = ip->i_mount;
  3693. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3694. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3695. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3696. if (rsvd)
  3697. tp->t_flags |= XFS_TRANS_RESERVE;
  3698. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3699. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3700. goto error0;
  3701. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3702. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
  3703. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3704. XFS_QMOPT_RES_REGBLKS);
  3705. if (error) {
  3706. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3707. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3708. return error;
  3709. }
  3710. if (XFS_IFORK_Q(ip))
  3711. goto error1;
  3712. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3713. /*
  3714. * For inodes coming from pre-6.2 filesystems.
  3715. */
  3716. ASSERT(ip->i_d.di_aformat == 0);
  3717. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3718. }
  3719. ASSERT(ip->i_d.di_anextents == 0);
  3720. VN_HOLD(XFS_ITOV(ip));
  3721. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3722. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3723. switch (ip->i_d.di_format) {
  3724. case XFS_DINODE_FMT_DEV:
  3725. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3726. break;
  3727. case XFS_DINODE_FMT_UUID:
  3728. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3729. break;
  3730. case XFS_DINODE_FMT_LOCAL:
  3731. case XFS_DINODE_FMT_EXTENTS:
  3732. case XFS_DINODE_FMT_BTREE:
  3733. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3734. if (!ip->i_d.di_forkoff)
  3735. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3736. else if (!(mp->m_flags & XFS_MOUNT_COMPAT_ATTR))
  3737. version = 2;
  3738. break;
  3739. default:
  3740. ASSERT(0);
  3741. error = XFS_ERROR(EINVAL);
  3742. goto error1;
  3743. }
  3744. ip->i_df.if_ext_max =
  3745. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3746. ASSERT(ip->i_afp == NULL);
  3747. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3748. ip->i_afp->if_ext_max =
  3749. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3750. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3751. logflags = 0;
  3752. XFS_BMAP_INIT(&flist, &firstblock);
  3753. switch (ip->i_d.di_format) {
  3754. case XFS_DINODE_FMT_LOCAL:
  3755. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3756. &logflags);
  3757. break;
  3758. case XFS_DINODE_FMT_EXTENTS:
  3759. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3760. &flist, &logflags);
  3761. break;
  3762. case XFS_DINODE_FMT_BTREE:
  3763. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3764. &logflags);
  3765. break;
  3766. default:
  3767. error = 0;
  3768. break;
  3769. }
  3770. if (logflags)
  3771. xfs_trans_log_inode(tp, ip, logflags);
  3772. if (error)
  3773. goto error2;
  3774. if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
  3775. (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
  3776. __int64_t sbfields = 0;
  3777. s = XFS_SB_LOCK(mp);
  3778. if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
  3779. XFS_SB_VERSION_ADDATTR(&mp->m_sb);
  3780. sbfields |= XFS_SB_VERSIONNUM;
  3781. }
  3782. if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
  3783. XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
  3784. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3785. }
  3786. if (sbfields) {
  3787. XFS_SB_UNLOCK(mp, s);
  3788. xfs_mod_sb(tp, sbfields);
  3789. } else
  3790. XFS_SB_UNLOCK(mp, s);
  3791. }
  3792. if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
  3793. goto error2;
  3794. error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
  3795. ASSERT(ip->i_df.if_ext_max ==
  3796. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3797. return error;
  3798. error2:
  3799. xfs_bmap_cancel(&flist);
  3800. error1:
  3801. ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
  3802. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3803. error0:
  3804. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3805. ASSERT(ip->i_df.if_ext_max ==
  3806. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3807. return error;
  3808. }
  3809. /*
  3810. * Add the extent to the list of extents to be free at transaction end.
  3811. * The list is maintained sorted (by block number).
  3812. */
  3813. /* ARGSUSED */
  3814. void
  3815. xfs_bmap_add_free(
  3816. xfs_fsblock_t bno, /* fs block number of extent */
  3817. xfs_filblks_t len, /* length of extent */
  3818. xfs_bmap_free_t *flist, /* list of extents */
  3819. xfs_mount_t *mp) /* mount point structure */
  3820. {
  3821. xfs_bmap_free_item_t *cur; /* current (next) element */
  3822. xfs_bmap_free_item_t *new; /* new element */
  3823. xfs_bmap_free_item_t *prev; /* previous element */
  3824. #ifdef DEBUG
  3825. xfs_agnumber_t agno;
  3826. xfs_agblock_t agbno;
  3827. ASSERT(bno != NULLFSBLOCK);
  3828. ASSERT(len > 0);
  3829. ASSERT(len <= MAXEXTLEN);
  3830. ASSERT(!ISNULLSTARTBLOCK(bno));
  3831. agno = XFS_FSB_TO_AGNO(mp, bno);
  3832. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  3833. ASSERT(agno < mp->m_sb.sb_agcount);
  3834. ASSERT(agbno < mp->m_sb.sb_agblocks);
  3835. ASSERT(len < mp->m_sb.sb_agblocks);
  3836. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  3837. #endif
  3838. ASSERT(xfs_bmap_free_item_zone != NULL);
  3839. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  3840. new->xbfi_startblock = bno;
  3841. new->xbfi_blockcount = (xfs_extlen_t)len;
  3842. for (prev = NULL, cur = flist->xbf_first;
  3843. cur != NULL;
  3844. prev = cur, cur = cur->xbfi_next) {
  3845. if (cur->xbfi_startblock >= bno)
  3846. break;
  3847. }
  3848. if (prev)
  3849. prev->xbfi_next = new;
  3850. else
  3851. flist->xbf_first = new;
  3852. new->xbfi_next = cur;
  3853. flist->xbf_count++;
  3854. }
  3855. /*
  3856. * Compute and fill in the value of the maximum depth of a bmap btree
  3857. * in this filesystem. Done once, during mount.
  3858. */
  3859. void
  3860. xfs_bmap_compute_maxlevels(
  3861. xfs_mount_t *mp, /* file system mount structure */
  3862. int whichfork) /* data or attr fork */
  3863. {
  3864. int level; /* btree level */
  3865. uint maxblocks; /* max blocks at this level */
  3866. uint maxleafents; /* max leaf entries possible */
  3867. int maxrootrecs; /* max records in root block */
  3868. int minleafrecs; /* min records in leaf block */
  3869. int minnoderecs; /* min records in node block */
  3870. int sz; /* root block size */
  3871. /*
  3872. * The maximum number of extents in a file, hence the maximum
  3873. * number of leaf entries, is controlled by the type of di_nextents
  3874. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  3875. * (a signed 16-bit number, xfs_aextnum_t).
  3876. */
  3877. if (whichfork == XFS_DATA_FORK) {
  3878. maxleafents = MAXEXTNUM;
  3879. sz = (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) ?
  3880. mp->m_attroffset : XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  3881. } else {
  3882. maxleafents = MAXAEXTNUM;
  3883. sz = (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) ?
  3884. mp->m_sb.sb_inodesize - mp->m_attroffset :
  3885. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3886. }
  3887. maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
  3888. minleafrecs = mp->m_bmap_dmnr[0];
  3889. minnoderecs = mp->m_bmap_dmnr[1];
  3890. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  3891. for (level = 1; maxblocks > 1; level++) {
  3892. if (maxblocks <= maxrootrecs)
  3893. maxblocks = 1;
  3894. else
  3895. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  3896. }
  3897. mp->m_bm_maxlevels[whichfork] = level;
  3898. }
  3899. /*
  3900. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  3901. * caller. Frees all the extents that need freeing, which must be done
  3902. * last due to locking considerations. We never free any extents in
  3903. * the first transaction. This is to allow the caller to make the first
  3904. * transaction a synchronous one so that the pointers to the data being
  3905. * broken in this transaction will be permanent before the data is actually
  3906. * freed. This is necessary to prevent blocks from being reallocated
  3907. * and written to before the free and reallocation are actually permanent.
  3908. * We do not just make the first transaction synchronous here, because
  3909. * there are more efficient ways to gain the same protection in some cases
  3910. * (see the file truncation code).
  3911. *
  3912. * Return 1 if the given transaction was committed and a new one
  3913. * started, and 0 otherwise in the committed parameter.
  3914. */
  3915. /*ARGSUSED*/
  3916. int /* error */
  3917. xfs_bmap_finish(
  3918. xfs_trans_t **tp, /* transaction pointer addr */
  3919. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  3920. xfs_fsblock_t firstblock, /* controlled ag for allocs */
  3921. int *committed) /* xact committed or not */
  3922. {
  3923. xfs_efd_log_item_t *efd; /* extent free data */
  3924. xfs_efi_log_item_t *efi; /* extent free intention */
  3925. int error; /* error return value */
  3926. xfs_bmap_free_item_t *free; /* free extent list item */
  3927. unsigned int logres; /* new log reservation */
  3928. unsigned int logcount; /* new log count */
  3929. xfs_mount_t *mp; /* filesystem mount structure */
  3930. xfs_bmap_free_item_t *next; /* next item on free list */
  3931. xfs_trans_t *ntp; /* new transaction pointer */
  3932. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  3933. if (flist->xbf_count == 0) {
  3934. *committed = 0;
  3935. return 0;
  3936. }
  3937. ntp = *tp;
  3938. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  3939. for (free = flist->xbf_first; free; free = free->xbfi_next)
  3940. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  3941. free->xbfi_blockcount);
  3942. logres = ntp->t_log_res;
  3943. logcount = ntp->t_log_count;
  3944. ntp = xfs_trans_dup(*tp);
  3945. error = xfs_trans_commit(*tp, 0, NULL);
  3946. *tp = ntp;
  3947. *committed = 1;
  3948. /*
  3949. * We have a new transaction, so we should return committed=1,
  3950. * even though we're returning an error.
  3951. */
  3952. if (error) {
  3953. return error;
  3954. }
  3955. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  3956. logcount)))
  3957. return error;
  3958. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  3959. for (free = flist->xbf_first; free != NULL; free = next) {
  3960. next = free->xbfi_next;
  3961. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  3962. free->xbfi_blockcount))) {
  3963. /*
  3964. * The bmap free list will be cleaned up at a
  3965. * higher level. The EFI will be canceled when
  3966. * this transaction is aborted.
  3967. * Need to force shutdown here to make sure it
  3968. * happens, since this transaction may not be
  3969. * dirty yet.
  3970. */
  3971. mp = ntp->t_mountp;
  3972. if (!XFS_FORCED_SHUTDOWN(mp))
  3973. xfs_force_shutdown(mp,
  3974. (error == EFSCORRUPTED) ?
  3975. XFS_CORRUPT_INCORE :
  3976. XFS_METADATA_IO_ERROR);
  3977. return error;
  3978. }
  3979. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  3980. free->xbfi_blockcount);
  3981. xfs_bmap_del_free(flist, NULL, free);
  3982. }
  3983. return 0;
  3984. }
  3985. /*
  3986. * Free up any items left in the list.
  3987. */
  3988. void
  3989. xfs_bmap_cancel(
  3990. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  3991. {
  3992. xfs_bmap_free_item_t *free; /* free list item */
  3993. xfs_bmap_free_item_t *next;
  3994. if (flist->xbf_count == 0)
  3995. return;
  3996. ASSERT(flist->xbf_first != NULL);
  3997. for (free = flist->xbf_first; free; free = next) {
  3998. next = free->xbfi_next;
  3999. xfs_bmap_del_free(flist, NULL, free);
  4000. }
  4001. ASSERT(flist->xbf_count == 0);
  4002. }
  4003. /*
  4004. * Returns the file-relative block number of the first unused block(s)
  4005. * in the file with at least "len" logically contiguous blocks free.
  4006. * This is the lowest-address hole if the file has holes, else the first block
  4007. * past the end of file.
  4008. * Return 0 if the file is currently local (in-inode).
  4009. */
  4010. int /* error */
  4011. xfs_bmap_first_unused(
  4012. xfs_trans_t *tp, /* transaction pointer */
  4013. xfs_inode_t *ip, /* incore inode */
  4014. xfs_extlen_t len, /* size of hole to find */
  4015. xfs_fileoff_t *first_unused, /* unused block */
  4016. int whichfork) /* data or attr fork */
  4017. {
  4018. xfs_bmbt_rec_t *base; /* base of extent array */
  4019. xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
  4020. int error; /* error return value */
  4021. xfs_ifork_t *ifp; /* inode fork pointer */
  4022. xfs_fileoff_t lastaddr; /* last block number seen */
  4023. xfs_fileoff_t lowest; /* lowest useful block */
  4024. xfs_fileoff_t max; /* starting useful block */
  4025. xfs_fileoff_t off; /* offset for this block */
  4026. xfs_extnum_t nextents; /* number of extent entries */
  4027. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  4028. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  4029. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  4030. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4031. *first_unused = 0;
  4032. return 0;
  4033. }
  4034. ifp = XFS_IFORK_PTR(ip, whichfork);
  4035. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4036. (error = xfs_iread_extents(tp, ip, whichfork)))
  4037. return error;
  4038. lowest = *first_unused;
  4039. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4040. base = &ifp->if_u1.if_extents[0];
  4041. for (lastaddr = 0, max = lowest, ep = base;
  4042. ep < &base[nextents];
  4043. ep++) {
  4044. off = xfs_bmbt_get_startoff(ep);
  4045. /*
  4046. * See if the hole before this extent will work.
  4047. */
  4048. if (off >= lowest + len && off - max >= len) {
  4049. *first_unused = max;
  4050. return 0;
  4051. }
  4052. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  4053. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  4054. }
  4055. *first_unused = max;
  4056. return 0;
  4057. }
  4058. /*
  4059. * Returns the file-relative block number of the last block + 1 before
  4060. * last_block (input value) in the file.
  4061. * This is not based on i_size, it is based on the extent list.
  4062. * Returns 0 for local files, as they do not have an extent list.
  4063. */
  4064. int /* error */
  4065. xfs_bmap_last_before(
  4066. xfs_trans_t *tp, /* transaction pointer */
  4067. xfs_inode_t *ip, /* incore inode */
  4068. xfs_fileoff_t *last_block, /* last block */
  4069. int whichfork) /* data or attr fork */
  4070. {
  4071. xfs_fileoff_t bno; /* input file offset */
  4072. int eof; /* hit end of file */
  4073. xfs_bmbt_rec_t *ep; /* pointer to last extent */
  4074. int error; /* error return value */
  4075. xfs_bmbt_irec_t got; /* current extent value */
  4076. xfs_ifork_t *ifp; /* inode fork pointer */
  4077. xfs_extnum_t lastx; /* last extent used */
  4078. xfs_bmbt_irec_t prev; /* previous extent value */
  4079. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4080. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4081. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4082. return XFS_ERROR(EIO);
  4083. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4084. *last_block = 0;
  4085. return 0;
  4086. }
  4087. ifp = XFS_IFORK_PTR(ip, whichfork);
  4088. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4089. (error = xfs_iread_extents(tp, ip, whichfork)))
  4090. return error;
  4091. bno = *last_block - 1;
  4092. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4093. &prev);
  4094. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  4095. if (prev.br_startoff == NULLFILEOFF)
  4096. *last_block = 0;
  4097. else
  4098. *last_block = prev.br_startoff + prev.br_blockcount;
  4099. }
  4100. /*
  4101. * Otherwise *last_block is already the right answer.
  4102. */
  4103. return 0;
  4104. }
  4105. /*
  4106. * Returns the file-relative block number of the first block past eof in
  4107. * the file. This is not based on i_size, it is based on the extent list.
  4108. * Returns 0 for local files, as they do not have an extent list.
  4109. */
  4110. int /* error */
  4111. xfs_bmap_last_offset(
  4112. xfs_trans_t *tp, /* transaction pointer */
  4113. xfs_inode_t *ip, /* incore inode */
  4114. xfs_fileoff_t *last_block, /* last block */
  4115. int whichfork) /* data or attr fork */
  4116. {
  4117. xfs_bmbt_rec_t *base; /* base of extent array */
  4118. xfs_bmbt_rec_t *ep; /* pointer to last extent */
  4119. int error; /* error return value */
  4120. xfs_ifork_t *ifp; /* inode fork pointer */
  4121. xfs_extnum_t nextents; /* number of extent entries */
  4122. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4123. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4124. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4125. return XFS_ERROR(EIO);
  4126. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4127. *last_block = 0;
  4128. return 0;
  4129. }
  4130. ifp = XFS_IFORK_PTR(ip, whichfork);
  4131. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4132. (error = xfs_iread_extents(tp, ip, whichfork)))
  4133. return error;
  4134. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4135. if (!nextents) {
  4136. *last_block = 0;
  4137. return 0;
  4138. }
  4139. base = &ifp->if_u1.if_extents[0];
  4140. ASSERT(base != NULL);
  4141. ep = &base[nextents - 1];
  4142. *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
  4143. return 0;
  4144. }
  4145. /*
  4146. * Returns whether the selected fork of the inode has exactly one
  4147. * block or not. For the data fork we check this matches di_size,
  4148. * implying the file's range is 0..bsize-1.
  4149. */
  4150. int /* 1=>1 block, 0=>otherwise */
  4151. xfs_bmap_one_block(
  4152. xfs_inode_t *ip, /* incore inode */
  4153. int whichfork) /* data or attr fork */
  4154. {
  4155. xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
  4156. xfs_ifork_t *ifp; /* inode fork pointer */
  4157. int rval; /* return value */
  4158. xfs_bmbt_irec_t s; /* internal version of extent */
  4159. #ifndef DEBUG
  4160. if (whichfork == XFS_DATA_FORK)
  4161. return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
  4162. #endif /* !DEBUG */
  4163. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  4164. return 0;
  4165. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4166. return 0;
  4167. ifp = XFS_IFORK_PTR(ip, whichfork);
  4168. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  4169. ep = ifp->if_u1.if_extents;
  4170. xfs_bmbt_get_all(ep, &s);
  4171. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  4172. if (rval && whichfork == XFS_DATA_FORK)
  4173. ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
  4174. return rval;
  4175. }
  4176. /*
  4177. * Read in the extents to if_extents.
  4178. * All inode fields are set up by caller, we just traverse the btree
  4179. * and copy the records in. If the file system cannot contain unwritten
  4180. * extents, the records are checked for no "state" flags.
  4181. */
  4182. int /* error */
  4183. xfs_bmap_read_extents(
  4184. xfs_trans_t *tp, /* transaction pointer */
  4185. xfs_inode_t *ip, /* incore inode */
  4186. int whichfork) /* data or attr fork */
  4187. {
  4188. xfs_bmbt_block_t *block; /* current btree block */
  4189. xfs_fsblock_t bno; /* block # of "block" */
  4190. xfs_buf_t *bp; /* buffer for "block" */
  4191. int error; /* error return value */
  4192. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  4193. #ifdef XFS_BMAP_TRACE
  4194. static char fname[] = "xfs_bmap_read_extents";
  4195. #endif
  4196. xfs_extnum_t i, j; /* index into the extents list */
  4197. xfs_ifork_t *ifp; /* fork structure */
  4198. int level; /* btree level, for checking */
  4199. xfs_mount_t *mp; /* file system mount structure */
  4200. xfs_bmbt_ptr_t *pp; /* pointer to block address */
  4201. /* REFERENCED */
  4202. xfs_extnum_t room; /* number of entries there's room for */
  4203. xfs_bmbt_rec_t *trp; /* target record pointer */
  4204. bno = NULLFSBLOCK;
  4205. mp = ip->i_mount;
  4206. ifp = XFS_IFORK_PTR(ip, whichfork);
  4207. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  4208. XFS_EXTFMT_INODE(ip);
  4209. block = ifp->if_broot;
  4210. /*
  4211. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  4212. */
  4213. level = be16_to_cpu(block->bb_level);
  4214. ASSERT(level > 0);
  4215. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  4216. ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
  4217. ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
  4218. ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
  4219. bno = INT_GET(*pp, ARCH_CONVERT);
  4220. /*
  4221. * Go down the tree until leaf level is reached, following the first
  4222. * pointer (leftmost) at each level.
  4223. */
  4224. while (level-- > 0) {
  4225. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4226. XFS_BMAP_BTREE_REF)))
  4227. return error;
  4228. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  4229. XFS_WANT_CORRUPTED_GOTO(
  4230. XFS_BMAP_SANITY_CHECK(mp, block, level),
  4231. error0);
  4232. if (level == 0)
  4233. break;
  4234. pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
  4235. 1, mp->m_bmap_dmxr[1]);
  4236. XFS_WANT_CORRUPTED_GOTO(
  4237. XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
  4238. error0);
  4239. bno = INT_GET(*pp, ARCH_CONVERT);
  4240. xfs_trans_brelse(tp, bp);
  4241. }
  4242. /*
  4243. * Here with bp and block set to the leftmost leaf node in the tree.
  4244. */
  4245. room = ifp->if_bytes / (uint)sizeof(*trp);
  4246. trp = ifp->if_u1.if_extents;
  4247. i = 0;
  4248. /*
  4249. * Loop over all leaf nodes. Copy information to the extent list.
  4250. */
  4251. for (;;) {
  4252. xfs_bmbt_rec_t *frp, *temp;
  4253. xfs_fsblock_t nextbno;
  4254. xfs_extnum_t num_recs;
  4255. num_recs = be16_to_cpu(block->bb_numrecs);
  4256. if (unlikely(i + num_recs > room)) {
  4257. ASSERT(i + num_recs <= room);
  4258. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  4259. "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
  4260. (unsigned long long) ip->i_ino);
  4261. XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
  4262. XFS_ERRLEVEL_LOW,
  4263. ip->i_mount);
  4264. goto error0;
  4265. }
  4266. XFS_WANT_CORRUPTED_GOTO(
  4267. XFS_BMAP_SANITY_CHECK(mp, block, 0),
  4268. error0);
  4269. /*
  4270. * Read-ahead the next leaf block, if any.
  4271. */
  4272. nextbno = be64_to_cpu(block->bb_rightsib);
  4273. if (nextbno != NULLFSBLOCK)
  4274. xfs_btree_reada_bufl(mp, nextbno, 1);
  4275. /*
  4276. * Copy records into the extent list.
  4277. */
  4278. frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
  4279. block, 1, mp->m_bmap_dmxr[0]);
  4280. temp = trp;
  4281. for (j = 0; j < num_recs; j++, frp++, trp++) {
  4282. trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
  4283. trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
  4284. }
  4285. if (exntf == XFS_EXTFMT_NOSTATE) {
  4286. /*
  4287. * Check all attribute bmap btree records and
  4288. * any "older" data bmap btree records for a
  4289. * set bit in the "extent flag" position.
  4290. */
  4291. if (unlikely(xfs_check_nostate_extents(temp, num_recs))) {
  4292. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  4293. XFS_ERRLEVEL_LOW,
  4294. ip->i_mount);
  4295. goto error0;
  4296. }
  4297. }
  4298. i += num_recs;
  4299. xfs_trans_brelse(tp, bp);
  4300. bno = nextbno;
  4301. /*
  4302. * If we've reached the end, stop.
  4303. */
  4304. if (bno == NULLFSBLOCK)
  4305. break;
  4306. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4307. XFS_BMAP_BTREE_REF)))
  4308. return error;
  4309. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  4310. }
  4311. ASSERT(i == ifp->if_bytes / (uint)sizeof(*trp));
  4312. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  4313. xfs_bmap_trace_exlist(fname, ip, i, whichfork);
  4314. return 0;
  4315. error0:
  4316. xfs_trans_brelse(tp, bp);
  4317. return XFS_ERROR(EFSCORRUPTED);
  4318. }
  4319. #ifdef XFS_BMAP_TRACE
  4320. /*
  4321. * Add bmap trace insert entries for all the contents of the extent list.
  4322. */
  4323. void
  4324. xfs_bmap_trace_exlist(
  4325. char *fname, /* function name */
  4326. xfs_inode_t *ip, /* incore inode pointer */
  4327. xfs_extnum_t cnt, /* count of entries in the list */
  4328. int whichfork) /* data or attr fork */
  4329. {
  4330. xfs_bmbt_rec_t *base; /* base of extent list */
  4331. xfs_bmbt_rec_t *ep; /* current entry in extent list */
  4332. xfs_extnum_t idx; /* extent list entry number */
  4333. xfs_ifork_t *ifp; /* inode fork pointer */
  4334. xfs_bmbt_irec_t s; /* extent list record */
  4335. ifp = XFS_IFORK_PTR(ip, whichfork);
  4336. ASSERT(cnt == ifp->if_bytes / (uint)sizeof(*base));
  4337. base = ifp->if_u1.if_extents;
  4338. for (idx = 0, ep = base; idx < cnt; idx++, ep++) {
  4339. xfs_bmbt_get_all(ep, &s);
  4340. xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
  4341. whichfork);
  4342. }
  4343. }
  4344. #endif
  4345. #ifdef DEBUG
  4346. /*
  4347. * Validate that the bmbt_irecs being returned from bmapi are valid
  4348. * given the callers original parameters. Specifically check the
  4349. * ranges of the returned irecs to ensure that they only extent beyond
  4350. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  4351. */
  4352. STATIC void
  4353. xfs_bmap_validate_ret(
  4354. xfs_fileoff_t bno,
  4355. xfs_filblks_t len,
  4356. int flags,
  4357. xfs_bmbt_irec_t *mval,
  4358. int nmap,
  4359. int ret_nmap)
  4360. {
  4361. int i; /* index to map values */
  4362. ASSERT(ret_nmap <= nmap);
  4363. for (i = 0; i < ret_nmap; i++) {
  4364. ASSERT(mval[i].br_blockcount > 0);
  4365. if (!(flags & XFS_BMAPI_ENTIRE)) {
  4366. ASSERT(mval[i].br_startoff >= bno);
  4367. ASSERT(mval[i].br_blockcount <= len);
  4368. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  4369. bno + len);
  4370. } else {
  4371. ASSERT(mval[i].br_startoff < bno + len);
  4372. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  4373. bno);
  4374. }
  4375. ASSERT(i == 0 ||
  4376. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  4377. mval[i].br_startoff);
  4378. if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
  4379. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  4380. mval[i].br_startblock != HOLESTARTBLOCK);
  4381. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  4382. mval[i].br_state == XFS_EXT_UNWRITTEN);
  4383. }
  4384. }
  4385. #endif /* DEBUG */
  4386. /*
  4387. * Map file blocks to filesystem blocks.
  4388. * File range is given by the bno/len pair.
  4389. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  4390. * into a hole or past eof.
  4391. * Only allocates blocks from a single allocation group,
  4392. * to avoid locking problems.
  4393. * The returned value in "firstblock" from the first call in a transaction
  4394. * must be remembered and presented to subsequent calls in "firstblock".
  4395. * An upper bound for the number of blocks to be allocated is supplied to
  4396. * the first call in "total"; if no allocation group has that many free
  4397. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4398. */
  4399. int /* error */
  4400. xfs_bmapi(
  4401. xfs_trans_t *tp, /* transaction pointer */
  4402. xfs_inode_t *ip, /* incore inode */
  4403. xfs_fileoff_t bno, /* starting file offs. mapped */
  4404. xfs_filblks_t len, /* length to map in file */
  4405. int flags, /* XFS_BMAPI_... */
  4406. xfs_fsblock_t *firstblock, /* first allocated block
  4407. controls a.g. for allocs */
  4408. xfs_extlen_t total, /* total blocks needed */
  4409. xfs_bmbt_irec_t *mval, /* output: map values */
  4410. int *nmap, /* i/o: mval size/count */
  4411. xfs_bmap_free_t *flist) /* i/o: list extents to free */
  4412. {
  4413. xfs_fsblock_t abno; /* allocated block number */
  4414. xfs_extlen_t alen; /* allocated extent length */
  4415. xfs_fileoff_t aoff; /* allocated file offset */
  4416. xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
  4417. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4418. xfs_fileoff_t end; /* end of mapped file region */
  4419. int eof; /* we've hit the end of extent list */
  4420. char contig; /* allocation must be one extent */
  4421. char delay; /* this request is for delayed alloc */
  4422. char exact; /* don't do all of wasdelayed extent */
  4423. xfs_bmbt_rec_t *ep; /* extent list entry pointer */
  4424. int error; /* error return */
  4425. xfs_bmbt_irec_t got; /* current extent list record */
  4426. xfs_ifork_t *ifp; /* inode fork pointer */
  4427. xfs_extlen_t indlen; /* indirect blocks length */
  4428. xfs_extnum_t lastx; /* last useful extent number */
  4429. int logflags; /* flags for transaction logging */
  4430. xfs_extlen_t minleft; /* min blocks left after allocation */
  4431. xfs_extlen_t minlen; /* min allocation size */
  4432. xfs_mount_t *mp; /* xfs mount structure */
  4433. int n; /* current extent index */
  4434. int nallocs; /* number of extents alloc\'d */
  4435. xfs_extnum_t nextents; /* number of extents in file */
  4436. xfs_fileoff_t obno; /* old block number (offset) */
  4437. xfs_bmbt_irec_t prev; /* previous extent list record */
  4438. int tmp_logflags; /* temp flags holder */
  4439. int whichfork; /* data or attr fork */
  4440. char inhole; /* current location is hole in file */
  4441. char stateless; /* ignore state flag set */
  4442. char trim; /* output trimmed to match range */
  4443. char userdata; /* allocating non-metadata */
  4444. char wasdelay; /* old extent was delayed */
  4445. char wr; /* this is a write request */
  4446. char rt; /* this is a realtime file */
  4447. char rsvd; /* OK to allocate reserved blocks */
  4448. #ifdef DEBUG
  4449. xfs_fileoff_t orig_bno; /* original block number value */
  4450. int orig_flags; /* original flags arg value */
  4451. xfs_filblks_t orig_len; /* original value of len arg */
  4452. xfs_bmbt_irec_t *orig_mval; /* original value of mval */
  4453. int orig_nmap; /* original value of *nmap */
  4454. orig_bno = bno;
  4455. orig_len = len;
  4456. orig_flags = flags;
  4457. orig_mval = mval;
  4458. orig_nmap = *nmap;
  4459. #endif
  4460. ASSERT(*nmap >= 1);
  4461. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
  4462. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4463. XFS_ATTR_FORK : XFS_DATA_FORK;
  4464. mp = ip->i_mount;
  4465. if (unlikely(XFS_TEST_ERROR(
  4466. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4467. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4468. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4469. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4470. XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
  4471. return XFS_ERROR(EFSCORRUPTED);
  4472. }
  4473. if (XFS_FORCED_SHUTDOWN(mp))
  4474. return XFS_ERROR(EIO);
  4475. rt = XFS_IS_REALTIME_INODE(ip);
  4476. ifp = XFS_IFORK_PTR(ip, whichfork);
  4477. ASSERT(ifp->if_ext_max ==
  4478. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4479. if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
  4480. XFS_STATS_INC(xs_blk_mapw);
  4481. else
  4482. XFS_STATS_INC(xs_blk_mapr);
  4483. delay = (flags & XFS_BMAPI_DELAY) != 0;
  4484. trim = (flags & XFS_BMAPI_ENTIRE) == 0;
  4485. userdata = (flags & XFS_BMAPI_METADATA) == 0;
  4486. exact = (flags & XFS_BMAPI_EXACT) != 0;
  4487. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  4488. contig = (flags & XFS_BMAPI_CONTIG) != 0;
  4489. /*
  4490. * stateless is used to combine extents which
  4491. * differ only due to the state of the extents.
  4492. * This technique is used from xfs_getbmap()
  4493. * when the caller does not wish to see the
  4494. * separation (which is the default).
  4495. *
  4496. * This technique is also used when writing a
  4497. * buffer which has been partially written,
  4498. * (usually by being flushed during a chunkread),
  4499. * to ensure one write takes place. This also
  4500. * prevents a change in the xfs inode extents at
  4501. * this time, intentionally. This change occurs
  4502. * on completion of the write operation, in
  4503. * xfs_strat_comp(), where the xfs_bmapi() call
  4504. * is transactioned, and the extents combined.
  4505. */
  4506. stateless = (flags & XFS_BMAPI_IGSTATE) != 0;
  4507. if (stateless && wr) /* if writing unwritten space, no */
  4508. wr = 0; /* allocations are allowed */
  4509. ASSERT(wr || !delay);
  4510. logflags = 0;
  4511. nallocs = 0;
  4512. cur = NULL;
  4513. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4514. ASSERT(wr && tp);
  4515. if ((error = xfs_bmap_local_to_extents(tp, ip,
  4516. firstblock, total, &logflags, whichfork)))
  4517. goto error0;
  4518. }
  4519. if (wr && *firstblock == NULLFSBLOCK) {
  4520. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4521. minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4522. else
  4523. minleft = 1;
  4524. } else
  4525. minleft = 0;
  4526. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4527. (error = xfs_iread_extents(tp, ip, whichfork)))
  4528. goto error0;
  4529. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4530. &prev);
  4531. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4532. n = 0;
  4533. end = bno + len;
  4534. obno = bno;
  4535. bma.ip = NULL;
  4536. while (bno < end && n < *nmap) {
  4537. /*
  4538. * Reading past eof, act as though there's a hole
  4539. * up to end.
  4540. */
  4541. if (eof && !wr)
  4542. got.br_startoff = end;
  4543. inhole = eof || got.br_startoff > bno;
  4544. wasdelay = wr && !inhole && !delay &&
  4545. ISNULLSTARTBLOCK(got.br_startblock);
  4546. /*
  4547. * First, deal with the hole before the allocated space
  4548. * that we found, if any.
  4549. */
  4550. if (wr && (inhole || wasdelay)) {
  4551. /*
  4552. * For the wasdelay case, we could also just
  4553. * allocate the stuff asked for in this bmap call
  4554. * but that wouldn't be as good.
  4555. */
  4556. if (wasdelay && !exact) {
  4557. alen = (xfs_extlen_t)got.br_blockcount;
  4558. aoff = got.br_startoff;
  4559. if (lastx != NULLEXTNUM && lastx) {
  4560. ep = &ifp->if_u1.if_extents[lastx - 1];
  4561. xfs_bmbt_get_all(ep, &prev);
  4562. }
  4563. } else if (wasdelay) {
  4564. alen = (xfs_extlen_t)
  4565. XFS_FILBLKS_MIN(len,
  4566. (got.br_startoff +
  4567. got.br_blockcount) - bno);
  4568. aoff = bno;
  4569. } else {
  4570. alen = (xfs_extlen_t)
  4571. XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4572. if (!eof)
  4573. alen = (xfs_extlen_t)
  4574. XFS_FILBLKS_MIN(alen,
  4575. got.br_startoff - bno);
  4576. aoff = bno;
  4577. }
  4578. minlen = contig ? alen : 1;
  4579. if (delay) {
  4580. xfs_extlen_t extsz = 0;
  4581. /* Figure out the extent size, adjust alen */
  4582. if (rt) {
  4583. if (!(extsz = ip->i_d.di_extsize))
  4584. extsz = mp->m_sb.sb_rextsize;
  4585. alen = roundup(alen, extsz);
  4586. extsz = alen / mp->m_sb.sb_rextsize;
  4587. }
  4588. /*
  4589. * Make a transaction-less quota reservation for
  4590. * delayed allocation blocks. This number gets
  4591. * adjusted later.
  4592. * We return EDQUOT if we haven't allocated
  4593. * blks already inside this loop;
  4594. */
  4595. if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
  4596. mp, NULL, ip, (long)alen, 0,
  4597. rt ? XFS_QMOPT_RES_RTBLKS :
  4598. XFS_QMOPT_RES_REGBLKS)) {
  4599. if (n == 0) {
  4600. *nmap = 0;
  4601. ASSERT(cur == NULL);
  4602. return XFS_ERROR(EDQUOT);
  4603. }
  4604. break;
  4605. }
  4606. /*
  4607. * Split changing sb for alen and indlen since
  4608. * they could be coming from different places.
  4609. */
  4610. indlen = (xfs_extlen_t)
  4611. xfs_bmap_worst_indlen(ip, alen);
  4612. ASSERT(indlen > 0);
  4613. if (rt)
  4614. error = xfs_mod_incore_sb(mp,
  4615. XFS_SBS_FREXTENTS,
  4616. -(extsz), rsvd);
  4617. else
  4618. error = xfs_mod_incore_sb(mp,
  4619. XFS_SBS_FDBLOCKS,
  4620. -(alen), rsvd);
  4621. if (!error) {
  4622. error = xfs_mod_incore_sb(mp,
  4623. XFS_SBS_FDBLOCKS,
  4624. -(indlen), rsvd);
  4625. if (error && rt) {
  4626. xfs_mod_incore_sb(ip->i_mount,
  4627. XFS_SBS_FREXTENTS,
  4628. extsz, rsvd);
  4629. } else if (error) {
  4630. xfs_mod_incore_sb(ip->i_mount,
  4631. XFS_SBS_FDBLOCKS,
  4632. alen, rsvd);
  4633. }
  4634. }
  4635. if (error) {
  4636. if (XFS_IS_QUOTA_ON(ip->i_mount))
  4637. /* unreserve the blocks now */
  4638. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
  4639. mp, NULL, ip,
  4640. (long)alen, 0, rt ?
  4641. XFS_QMOPT_RES_RTBLKS :
  4642. XFS_QMOPT_RES_REGBLKS);
  4643. break;
  4644. }
  4645. ip->i_delayed_blks += alen;
  4646. abno = NULLSTARTBLOCK(indlen);
  4647. } else {
  4648. /*
  4649. * If first time, allocate and fill in
  4650. * once-only bma fields.
  4651. */
  4652. if (bma.ip == NULL) {
  4653. bma.tp = tp;
  4654. bma.ip = ip;
  4655. bma.prevp = &prev;
  4656. bma.gotp = &got;
  4657. bma.total = total;
  4658. bma.userdata = 0;
  4659. }
  4660. /* Indicate if this is the first user data
  4661. * in the file, or just any user data.
  4662. */
  4663. if (userdata) {
  4664. bma.userdata = (aoff == 0) ?
  4665. XFS_ALLOC_INITIAL_USER_DATA :
  4666. XFS_ALLOC_USERDATA;
  4667. }
  4668. /*
  4669. * Fill in changeable bma fields.
  4670. */
  4671. bma.eof = eof;
  4672. bma.firstblock = *firstblock;
  4673. bma.alen = alen;
  4674. bma.off = aoff;
  4675. bma.wasdel = wasdelay;
  4676. bma.minlen = minlen;
  4677. bma.low = flist->xbf_low;
  4678. bma.minleft = minleft;
  4679. /*
  4680. * Only want to do the alignment at the
  4681. * eof if it is userdata and allocation length
  4682. * is larger than a stripe unit.
  4683. */
  4684. if (mp->m_dalign && alen >= mp->m_dalign &&
  4685. userdata && whichfork == XFS_DATA_FORK) {
  4686. if ((error = xfs_bmap_isaeof(ip, aoff,
  4687. whichfork, &bma.aeof)))
  4688. goto error0;
  4689. } else
  4690. bma.aeof = 0;
  4691. /*
  4692. * Call allocator.
  4693. */
  4694. if ((error = xfs_bmap_alloc(&bma)))
  4695. goto error0;
  4696. /*
  4697. * Copy out result fields.
  4698. */
  4699. abno = bma.rval;
  4700. if ((flist->xbf_low = bma.low))
  4701. minleft = 0;
  4702. alen = bma.alen;
  4703. aoff = bma.off;
  4704. ASSERT(*firstblock == NULLFSBLOCK ||
  4705. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4706. XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
  4707. (flist->xbf_low &&
  4708. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4709. XFS_FSB_TO_AGNO(mp, bma.firstblock)));
  4710. *firstblock = bma.firstblock;
  4711. if (cur)
  4712. cur->bc_private.b.firstblock =
  4713. *firstblock;
  4714. if (abno == NULLFSBLOCK)
  4715. break;
  4716. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4717. cur = xfs_btree_init_cursor(mp,
  4718. tp, NULL, 0, XFS_BTNUM_BMAP,
  4719. ip, whichfork);
  4720. cur->bc_private.b.firstblock =
  4721. *firstblock;
  4722. cur->bc_private.b.flist = flist;
  4723. }
  4724. /*
  4725. * Bump the number of extents we've allocated
  4726. * in this call.
  4727. */
  4728. nallocs++;
  4729. }
  4730. if (cur)
  4731. cur->bc_private.b.flags =
  4732. wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
  4733. got.br_startoff = aoff;
  4734. got.br_startblock = abno;
  4735. got.br_blockcount = alen;
  4736. got.br_state = XFS_EXT_NORM; /* assume normal */
  4737. /*
  4738. * Determine state of extent, and the filesystem.
  4739. * A wasdelay extent has been initialized, so
  4740. * shouldn't be flagged as unwritten.
  4741. */
  4742. if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  4743. if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
  4744. got.br_state = XFS_EXT_UNWRITTEN;
  4745. }
  4746. error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
  4747. firstblock, flist, &tmp_logflags, whichfork,
  4748. rsvd);
  4749. logflags |= tmp_logflags;
  4750. if (error)
  4751. goto error0;
  4752. lastx = ifp->if_lastex;
  4753. ep = &ifp->if_u1.if_extents[lastx];
  4754. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4755. xfs_bmbt_get_all(ep, &got);
  4756. ASSERT(got.br_startoff <= aoff);
  4757. ASSERT(got.br_startoff + got.br_blockcount >=
  4758. aoff + alen);
  4759. #ifdef DEBUG
  4760. if (delay) {
  4761. ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
  4762. ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
  4763. }
  4764. ASSERT(got.br_state == XFS_EXT_NORM ||
  4765. got.br_state == XFS_EXT_UNWRITTEN);
  4766. #endif
  4767. /*
  4768. * Fall down into the found allocated space case.
  4769. */
  4770. } else if (inhole) {
  4771. /*
  4772. * Reading in a hole.
  4773. */
  4774. mval->br_startoff = bno;
  4775. mval->br_startblock = HOLESTARTBLOCK;
  4776. mval->br_blockcount =
  4777. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4778. mval->br_state = XFS_EXT_NORM;
  4779. bno += mval->br_blockcount;
  4780. len -= mval->br_blockcount;
  4781. mval++;
  4782. n++;
  4783. continue;
  4784. }
  4785. /*
  4786. * Then deal with the allocated space we found.
  4787. */
  4788. ASSERT(ep != NULL);
  4789. if (trim && (got.br_startoff + got.br_blockcount > obno)) {
  4790. if (obno > bno)
  4791. bno = obno;
  4792. ASSERT((bno >= obno) || (n == 0));
  4793. ASSERT(bno < end);
  4794. mval->br_startoff = bno;
  4795. if (ISNULLSTARTBLOCK(got.br_startblock)) {
  4796. ASSERT(!wr || delay);
  4797. mval->br_startblock = DELAYSTARTBLOCK;
  4798. } else
  4799. mval->br_startblock =
  4800. got.br_startblock +
  4801. (bno - got.br_startoff);
  4802. /*
  4803. * Return the minimum of what we got and what we
  4804. * asked for for the length. We can use the len
  4805. * variable here because it is modified below
  4806. * and we could have been there before coming
  4807. * here if the first part of the allocation
  4808. * didn't overlap what was asked for.
  4809. */
  4810. mval->br_blockcount =
  4811. XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
  4812. (bno - got.br_startoff));
  4813. mval->br_state = got.br_state;
  4814. ASSERT(mval->br_blockcount <= len);
  4815. } else {
  4816. *mval = got;
  4817. if (ISNULLSTARTBLOCK(mval->br_startblock)) {
  4818. ASSERT(!wr || delay);
  4819. mval->br_startblock = DELAYSTARTBLOCK;
  4820. }
  4821. }
  4822. /*
  4823. * Check if writing previously allocated but
  4824. * unwritten extents.
  4825. */
  4826. if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
  4827. ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
  4828. /*
  4829. * Modify (by adding) the state flag, if writing.
  4830. */
  4831. ASSERT(mval->br_blockcount <= len);
  4832. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4833. cur = xfs_btree_init_cursor(mp,
  4834. tp, NULL, 0, XFS_BTNUM_BMAP,
  4835. ip, whichfork);
  4836. cur->bc_private.b.firstblock =
  4837. *firstblock;
  4838. cur->bc_private.b.flist = flist;
  4839. }
  4840. mval->br_state = XFS_EXT_NORM;
  4841. error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
  4842. firstblock, flist, &tmp_logflags, whichfork,
  4843. rsvd);
  4844. logflags |= tmp_logflags;
  4845. if (error)
  4846. goto error0;
  4847. lastx = ifp->if_lastex;
  4848. ep = &ifp->if_u1.if_extents[lastx];
  4849. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4850. xfs_bmbt_get_all(ep, &got);
  4851. /*
  4852. * We may have combined previously unwritten
  4853. * space with written space, so generate
  4854. * another request.
  4855. */
  4856. if (mval->br_blockcount < len)
  4857. continue;
  4858. }
  4859. ASSERT(!trim ||
  4860. ((mval->br_startoff + mval->br_blockcount) <= end));
  4861. ASSERT(!trim || (mval->br_blockcount <= len) ||
  4862. (mval->br_startoff < obno));
  4863. bno = mval->br_startoff + mval->br_blockcount;
  4864. len = end - bno;
  4865. if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  4866. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  4867. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  4868. ASSERT(mval->br_state == mval[-1].br_state);
  4869. mval[-1].br_blockcount = mval->br_blockcount;
  4870. mval[-1].br_state = mval->br_state;
  4871. } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  4872. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  4873. mval[-1].br_startblock != HOLESTARTBLOCK &&
  4874. mval->br_startblock ==
  4875. mval[-1].br_startblock + mval[-1].br_blockcount &&
  4876. (stateless || mval[-1].br_state == mval->br_state)) {
  4877. ASSERT(mval->br_startoff ==
  4878. mval[-1].br_startoff + mval[-1].br_blockcount);
  4879. mval[-1].br_blockcount += mval->br_blockcount;
  4880. } else if (n > 0 &&
  4881. mval->br_startblock == DELAYSTARTBLOCK &&
  4882. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  4883. mval->br_startoff ==
  4884. mval[-1].br_startoff + mval[-1].br_blockcount) {
  4885. mval[-1].br_blockcount += mval->br_blockcount;
  4886. mval[-1].br_state = mval->br_state;
  4887. } else if (!((n == 0) &&
  4888. ((mval->br_startoff + mval->br_blockcount) <=
  4889. obno))) {
  4890. mval++;
  4891. n++;
  4892. }
  4893. /*
  4894. * If we're done, stop now. Stop when we've allocated
  4895. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4896. * the transaction may get too big.
  4897. */
  4898. if (bno >= end || n >= *nmap || nallocs >= *nmap)
  4899. break;
  4900. /*
  4901. * Else go on to the next record.
  4902. */
  4903. ep++;
  4904. lastx++;
  4905. if (lastx >= nextents) {
  4906. eof = 1;
  4907. prev = got;
  4908. } else
  4909. xfs_bmbt_get_all(ep, &got);
  4910. }
  4911. ifp->if_lastex = lastx;
  4912. *nmap = n;
  4913. /*
  4914. * Transform from btree to extents, give it cur.
  4915. */
  4916. if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  4917. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  4918. ASSERT(wr && cur);
  4919. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  4920. &tmp_logflags, whichfork);
  4921. logflags |= tmp_logflags;
  4922. if (error)
  4923. goto error0;
  4924. }
  4925. ASSERT(ifp->if_ext_max ==
  4926. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4927. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4928. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
  4929. error = 0;
  4930. error0:
  4931. /*
  4932. * Log everything. Do this after conversion, there's no point in
  4933. * logging the extent list if we've converted to btree format.
  4934. */
  4935. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  4936. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4937. logflags &= ~XFS_ILOG_FEXT(whichfork);
  4938. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  4939. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4940. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  4941. /*
  4942. * Log whatever the flags say, even if error. Otherwise we might miss
  4943. * detecting a case where the data is changed, there's an error,
  4944. * and it's not logged so we don't shutdown when we should.
  4945. */
  4946. if (logflags) {
  4947. ASSERT(tp && wr);
  4948. xfs_trans_log_inode(tp, ip, logflags);
  4949. }
  4950. if (cur) {
  4951. if (!error) {
  4952. ASSERT(*firstblock == NULLFSBLOCK ||
  4953. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4954. XFS_FSB_TO_AGNO(mp,
  4955. cur->bc_private.b.firstblock) ||
  4956. (flist->xbf_low &&
  4957. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4958. XFS_FSB_TO_AGNO(mp,
  4959. cur->bc_private.b.firstblock)));
  4960. *firstblock = cur->bc_private.b.firstblock;
  4961. }
  4962. xfs_btree_del_cursor(cur,
  4963. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4964. }
  4965. if (!error)
  4966. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4967. orig_nmap, *nmap);
  4968. return error;
  4969. }
  4970. /*
  4971. * Map file blocks to filesystem blocks, simple version.
  4972. * One block (extent) only, read-only.
  4973. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  4974. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  4975. * was set and all the others were clear.
  4976. */
  4977. int /* error */
  4978. xfs_bmapi_single(
  4979. xfs_trans_t *tp, /* transaction pointer */
  4980. xfs_inode_t *ip, /* incore inode */
  4981. int whichfork, /* data or attr fork */
  4982. xfs_fsblock_t *fsb, /* output: mapped block */
  4983. xfs_fileoff_t bno) /* starting file offs. mapped */
  4984. {
  4985. int eof; /* we've hit the end of extent list */
  4986. int error; /* error return */
  4987. xfs_bmbt_irec_t got; /* current extent list record */
  4988. xfs_ifork_t *ifp; /* inode fork pointer */
  4989. xfs_extnum_t lastx; /* last useful extent number */
  4990. xfs_bmbt_irec_t prev; /* previous extent list record */
  4991. ifp = XFS_IFORK_PTR(ip, whichfork);
  4992. if (unlikely(
  4993. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4994. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
  4995. XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
  4996. ip->i_mount);
  4997. return XFS_ERROR(EFSCORRUPTED);
  4998. }
  4999. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  5000. return XFS_ERROR(EIO);
  5001. XFS_STATS_INC(xs_blk_mapr);
  5002. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5003. (error = xfs_iread_extents(tp, ip, whichfork)))
  5004. return error;
  5005. (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5006. &prev);
  5007. /*
  5008. * Reading past eof, act as though there's a hole
  5009. * up to end.
  5010. */
  5011. if (eof || got.br_startoff > bno) {
  5012. *fsb = NULLFSBLOCK;
  5013. return 0;
  5014. }
  5015. ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
  5016. ASSERT(bno < got.br_startoff + got.br_blockcount);
  5017. *fsb = got.br_startblock + (bno - got.br_startoff);
  5018. ifp->if_lastex = lastx;
  5019. return 0;
  5020. }
  5021. /*
  5022. * Unmap (remove) blocks from a file.
  5023. * If nexts is nonzero then the number of extents to remove is limited to
  5024. * that value. If not all extents in the block range can be removed then
  5025. * *done is set.
  5026. */
  5027. int /* error */
  5028. xfs_bunmapi(
  5029. xfs_trans_t *tp, /* transaction pointer */
  5030. struct xfs_inode *ip, /* incore inode */
  5031. xfs_fileoff_t bno, /* starting offset to unmap */
  5032. xfs_filblks_t len, /* length to unmap in file */
  5033. int flags, /* misc flags */
  5034. xfs_extnum_t nexts, /* number of extents max */
  5035. xfs_fsblock_t *firstblock, /* first allocated block
  5036. controls a.g. for allocs */
  5037. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  5038. int *done) /* set if not done yet */
  5039. {
  5040. xfs_btree_cur_t *cur; /* bmap btree cursor */
  5041. xfs_bmbt_irec_t del; /* extent being deleted */
  5042. int eof; /* is deleting at eof */
  5043. xfs_bmbt_rec_t *ep; /* extent list entry pointer */
  5044. int error; /* error return value */
  5045. xfs_extnum_t extno; /* extent number in list */
  5046. xfs_bmbt_irec_t got; /* current extent list entry */
  5047. xfs_ifork_t *ifp; /* inode fork pointer */
  5048. int isrt; /* freeing in rt area */
  5049. xfs_extnum_t lastx; /* last extent index used */
  5050. int logflags; /* transaction logging flags */
  5051. xfs_extlen_t mod; /* rt extent offset */
  5052. xfs_mount_t *mp; /* mount structure */
  5053. xfs_extnum_t nextents; /* size of extent list */
  5054. xfs_bmbt_irec_t prev; /* previous extent list entry */
  5055. xfs_fileoff_t start; /* first file offset deleted */
  5056. int tmp_logflags; /* partial logging flags */
  5057. int wasdel; /* was a delayed alloc extent */
  5058. int whichfork; /* data or attribute fork */
  5059. int rsvd; /* OK to allocate reserved blocks */
  5060. xfs_fsblock_t sum;
  5061. xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
  5062. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  5063. XFS_ATTR_FORK : XFS_DATA_FORK;
  5064. ifp = XFS_IFORK_PTR(ip, whichfork);
  5065. if (unlikely(
  5066. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5067. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  5068. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  5069. ip->i_mount);
  5070. return XFS_ERROR(EFSCORRUPTED);
  5071. }
  5072. mp = ip->i_mount;
  5073. if (XFS_FORCED_SHUTDOWN(mp))
  5074. return XFS_ERROR(EIO);
  5075. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  5076. ASSERT(len > 0);
  5077. ASSERT(nexts >= 0);
  5078. ASSERT(ifp->if_ext_max ==
  5079. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5080. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5081. (error = xfs_iread_extents(tp, ip, whichfork)))
  5082. return error;
  5083. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5084. if (nextents == 0) {
  5085. *done = 1;
  5086. return 0;
  5087. }
  5088. XFS_STATS_INC(xs_blk_unmap);
  5089. isrt = (whichfork == XFS_DATA_FORK) &&
  5090. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
  5091. start = bno;
  5092. bno = start + len - 1;
  5093. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5094. &prev);
  5095. /*
  5096. * Check to see if the given block number is past the end of the
  5097. * file, back up to the last block if so...
  5098. */
  5099. if (eof) {
  5100. ep = &ifp->if_u1.if_extents[--lastx];
  5101. xfs_bmbt_get_all(ep, &got);
  5102. bno = got.br_startoff + got.br_blockcount - 1;
  5103. }
  5104. logflags = 0;
  5105. if (ifp->if_flags & XFS_IFBROOT) {
  5106. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  5107. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  5108. whichfork);
  5109. cur->bc_private.b.firstblock = *firstblock;
  5110. cur->bc_private.b.flist = flist;
  5111. cur->bc_private.b.flags = 0;
  5112. } else
  5113. cur = NULL;
  5114. extno = 0;
  5115. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  5116. (nexts == 0 || extno < nexts)) {
  5117. /*
  5118. * Is the found extent after a hole in which bno lives?
  5119. * Just back up to the previous extent, if so.
  5120. */
  5121. if (got.br_startoff > bno) {
  5122. if (--lastx < 0)
  5123. break;
  5124. ep--;
  5125. xfs_bmbt_get_all(ep, &got);
  5126. }
  5127. /*
  5128. * Is the last block of this extent before the range
  5129. * we're supposed to delete? If so, we're done.
  5130. */
  5131. bno = XFS_FILEOFF_MIN(bno,
  5132. got.br_startoff + got.br_blockcount - 1);
  5133. if (bno < start)
  5134. break;
  5135. /*
  5136. * Then deal with the (possibly delayed) allocated space
  5137. * we found.
  5138. */
  5139. ASSERT(ep != NULL);
  5140. del = got;
  5141. wasdel = ISNULLSTARTBLOCK(del.br_startblock);
  5142. if (got.br_startoff < start) {
  5143. del.br_startoff = start;
  5144. del.br_blockcount -= start - got.br_startoff;
  5145. if (!wasdel)
  5146. del.br_startblock += start - got.br_startoff;
  5147. }
  5148. if (del.br_startoff + del.br_blockcount > bno + 1)
  5149. del.br_blockcount = bno + 1 - del.br_startoff;
  5150. sum = del.br_startblock + del.br_blockcount;
  5151. if (isrt &&
  5152. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5153. /*
  5154. * Realtime extent not lined up at the end.
  5155. * The extent could have been split into written
  5156. * and unwritten pieces, or we could just be
  5157. * unmapping part of it. But we can't really
  5158. * get rid of part of a realtime extent.
  5159. */
  5160. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5161. !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  5162. /*
  5163. * This piece is unwritten, or we're not
  5164. * using unwritten extents. Skip over it.
  5165. */
  5166. ASSERT(bno >= mod);
  5167. bno -= mod > del.br_blockcount ?
  5168. del.br_blockcount : mod;
  5169. if (bno < got.br_startoff) {
  5170. if (--lastx >= 0)
  5171. xfs_bmbt_get_all(--ep, &got);
  5172. }
  5173. continue;
  5174. }
  5175. /*
  5176. * It's written, turn it unwritten.
  5177. * This is better than zeroing it.
  5178. */
  5179. ASSERT(del.br_state == XFS_EXT_NORM);
  5180. ASSERT(xfs_trans_get_block_res(tp) > 0);
  5181. /*
  5182. * If this spans a realtime extent boundary,
  5183. * chop it back to the start of the one we end at.
  5184. */
  5185. if (del.br_blockcount > mod) {
  5186. del.br_startoff += del.br_blockcount - mod;
  5187. del.br_startblock += del.br_blockcount - mod;
  5188. del.br_blockcount = mod;
  5189. }
  5190. del.br_state = XFS_EXT_UNWRITTEN;
  5191. error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
  5192. firstblock, flist, &logflags, XFS_DATA_FORK, 0);
  5193. if (error)
  5194. goto error0;
  5195. goto nodelete;
  5196. }
  5197. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5198. /*
  5199. * Realtime extent is lined up at the end but not
  5200. * at the front. We'll get rid of full extents if
  5201. * we can.
  5202. */
  5203. mod = mp->m_sb.sb_rextsize - mod;
  5204. if (del.br_blockcount > mod) {
  5205. del.br_blockcount -= mod;
  5206. del.br_startoff += mod;
  5207. del.br_startblock += mod;
  5208. } else if ((del.br_startoff == start &&
  5209. (del.br_state == XFS_EXT_UNWRITTEN ||
  5210. xfs_trans_get_block_res(tp) == 0)) ||
  5211. !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  5212. /*
  5213. * Can't make it unwritten. There isn't
  5214. * a full extent here so just skip it.
  5215. */
  5216. ASSERT(bno >= del.br_blockcount);
  5217. bno -= del.br_blockcount;
  5218. if (bno < got.br_startoff) {
  5219. if (--lastx >= 0)
  5220. xfs_bmbt_get_all(--ep, &got);
  5221. }
  5222. continue;
  5223. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5224. /*
  5225. * This one is already unwritten.
  5226. * It must have a written left neighbor.
  5227. * Unwrite the killed part of that one and
  5228. * try again.
  5229. */
  5230. ASSERT(lastx > 0);
  5231. xfs_bmbt_get_all(ep - 1, &prev);
  5232. ASSERT(prev.br_state == XFS_EXT_NORM);
  5233. ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
  5234. ASSERT(del.br_startblock ==
  5235. prev.br_startblock + prev.br_blockcount);
  5236. if (prev.br_startoff < start) {
  5237. mod = start - prev.br_startoff;
  5238. prev.br_blockcount -= mod;
  5239. prev.br_startblock += mod;
  5240. prev.br_startoff = start;
  5241. }
  5242. prev.br_state = XFS_EXT_UNWRITTEN;
  5243. error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
  5244. &prev, firstblock, flist, &logflags,
  5245. XFS_DATA_FORK, 0);
  5246. if (error)
  5247. goto error0;
  5248. goto nodelete;
  5249. } else {
  5250. ASSERT(del.br_state == XFS_EXT_NORM);
  5251. del.br_state = XFS_EXT_UNWRITTEN;
  5252. error = xfs_bmap_add_extent(ip, lastx, &cur,
  5253. &del, firstblock, flist, &logflags,
  5254. XFS_DATA_FORK, 0);
  5255. if (error)
  5256. goto error0;
  5257. goto nodelete;
  5258. }
  5259. }
  5260. if (wasdel) {
  5261. ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
  5262. /* Update realtim/data freespace, unreserve quota */
  5263. if (isrt) {
  5264. xfs_filblks_t rtexts;
  5265. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5266. do_div(rtexts, mp->m_sb.sb_rextsize);
  5267. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5268. (int)rtexts, rsvd);
  5269. XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, NULL, ip,
  5270. -((long)del.br_blockcount), 0,
  5271. XFS_QMOPT_RES_RTBLKS);
  5272. } else {
  5273. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
  5274. (int)del.br_blockcount, rsvd);
  5275. XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, NULL, ip,
  5276. -((long)del.br_blockcount), 0,
  5277. XFS_QMOPT_RES_REGBLKS);
  5278. }
  5279. ip->i_delayed_blks -= del.br_blockcount;
  5280. if (cur)
  5281. cur->bc_private.b.flags |=
  5282. XFS_BTCUR_BPRV_WASDEL;
  5283. } else if (cur)
  5284. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5285. /*
  5286. * If it's the case where the directory code is running
  5287. * with no block reservation, and the deleted block is in
  5288. * the middle of its extent, and the resulting insert
  5289. * of an extent would cause transformation to btree format,
  5290. * then reject it. The calling code will then swap
  5291. * blocks around instead.
  5292. * We have to do this now, rather than waiting for the
  5293. * conversion to btree format, since the transaction
  5294. * will be dirty.
  5295. */
  5296. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5297. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5298. XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
  5299. del.br_startoff > got.br_startoff &&
  5300. del.br_startoff + del.br_blockcount <
  5301. got.br_startoff + got.br_blockcount) {
  5302. error = XFS_ERROR(ENOSPC);
  5303. goto error0;
  5304. }
  5305. error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
  5306. &tmp_logflags, whichfork, rsvd);
  5307. logflags |= tmp_logflags;
  5308. if (error)
  5309. goto error0;
  5310. bno = del.br_startoff - 1;
  5311. nodelete:
  5312. lastx = ifp->if_lastex;
  5313. /*
  5314. * If not done go on to the next (previous) record.
  5315. * Reset ep in case the extents array was re-alloced.
  5316. */
  5317. ep = &ifp->if_u1.if_extents[lastx];
  5318. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5319. if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
  5320. xfs_bmbt_get_startoff(ep) > bno) {
  5321. lastx--;
  5322. ep--;
  5323. }
  5324. if (lastx >= 0)
  5325. xfs_bmbt_get_all(ep, &got);
  5326. extno++;
  5327. }
  5328. }
  5329. ifp->if_lastex = lastx;
  5330. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5331. ASSERT(ifp->if_ext_max ==
  5332. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5333. /*
  5334. * Convert to a btree if necessary.
  5335. */
  5336. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5337. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  5338. ASSERT(cur == NULL);
  5339. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5340. &cur, 0, &tmp_logflags, whichfork);
  5341. logflags |= tmp_logflags;
  5342. if (error)
  5343. goto error0;
  5344. }
  5345. /*
  5346. * transform from btree to extents, give it cur
  5347. */
  5348. else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5349. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5350. ASSERT(cur != NULL);
  5351. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5352. whichfork);
  5353. logflags |= tmp_logflags;
  5354. if (error)
  5355. goto error0;
  5356. }
  5357. /*
  5358. * transform from extents to local?
  5359. */
  5360. ASSERT(ifp->if_ext_max ==
  5361. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5362. error = 0;
  5363. error0:
  5364. /*
  5365. * Log everything. Do this after conversion, there's no point in
  5366. * logging the extent list if we've converted to btree format.
  5367. */
  5368. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  5369. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5370. logflags &= ~XFS_ILOG_FEXT(whichfork);
  5371. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  5372. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5373. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  5374. /*
  5375. * Log inode even in the error case, if the transaction
  5376. * is dirty we'll need to shut down the filesystem.
  5377. */
  5378. if (logflags)
  5379. xfs_trans_log_inode(tp, ip, logflags);
  5380. if (cur) {
  5381. if (!error) {
  5382. *firstblock = cur->bc_private.b.firstblock;
  5383. cur->bc_private.b.allocated = 0;
  5384. }
  5385. xfs_btree_del_cursor(cur,
  5386. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5387. }
  5388. return error;
  5389. }
  5390. /*
  5391. * Fcntl interface to xfs_bmapi.
  5392. */
  5393. int /* error code */
  5394. xfs_getbmap(
  5395. bhv_desc_t *bdp, /* XFS behavior descriptor*/
  5396. struct getbmap *bmv, /* user bmap structure */
  5397. void __user *ap, /* pointer to user's array */
  5398. int interface) /* interface flags */
  5399. {
  5400. __int64_t bmvend; /* last block requested */
  5401. int error; /* return value */
  5402. __int64_t fixlen; /* length for -1 case */
  5403. int i; /* extent number */
  5404. xfs_inode_t *ip; /* xfs incore inode pointer */
  5405. vnode_t *vp; /* corresponding vnode */
  5406. int lock; /* lock state */
  5407. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5408. xfs_mount_t *mp; /* file system mount point */
  5409. int nex; /* # of user extents can do */
  5410. int nexleft; /* # of user extents left */
  5411. int subnex; /* # of bmapi's can do */
  5412. int nmap; /* number of map entries */
  5413. struct getbmap out; /* output structure */
  5414. int whichfork; /* data or attr fork */
  5415. int prealloced; /* this is a file with
  5416. * preallocated data space */
  5417. int sh_unwritten; /* true, if unwritten */
  5418. /* extents listed separately */
  5419. int bmapi_flags; /* flags for xfs_bmapi */
  5420. __int32_t oflags; /* getbmapx bmv_oflags field */
  5421. vp = BHV_TO_VNODE(bdp);
  5422. ip = XFS_BHVTOI(bdp);
  5423. mp = ip->i_mount;
  5424. whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5425. sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
  5426. /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
  5427. * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
  5428. * bit is set for the file, generate a read event in order
  5429. * that the DMAPI application may do its thing before we return
  5430. * the extents. Usually this means restoring user file data to
  5431. * regions of the file that look like holes.
  5432. *
  5433. * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
  5434. * BMV_IF_NO_DMAPI_READ so that read events are generated.
  5435. * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
  5436. * could misinterpret holes in a DMAPI file as true holes,
  5437. * when in fact they may represent offline user data.
  5438. */
  5439. if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
  5440. && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
  5441. && whichfork == XFS_DATA_FORK) {
  5442. error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
  5443. if (error)
  5444. return XFS_ERROR(error);
  5445. }
  5446. if (whichfork == XFS_ATTR_FORK) {
  5447. if (XFS_IFORK_Q(ip)) {
  5448. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5449. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5450. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5451. return XFS_ERROR(EINVAL);
  5452. } else if (unlikely(
  5453. ip->i_d.di_aformat != 0 &&
  5454. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5455. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5456. ip->i_mount);
  5457. return XFS_ERROR(EFSCORRUPTED);
  5458. }
  5459. } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5460. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5461. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5462. return XFS_ERROR(EINVAL);
  5463. if (whichfork == XFS_DATA_FORK) {
  5464. if (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC) {
  5465. prealloced = 1;
  5466. fixlen = XFS_MAXIOFFSET(mp);
  5467. } else {
  5468. prealloced = 0;
  5469. fixlen = ip->i_d.di_size;
  5470. }
  5471. } else {
  5472. prealloced = 0;
  5473. fixlen = 1LL << 32;
  5474. }
  5475. if (bmv->bmv_length == -1) {
  5476. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5477. bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
  5478. (__int64_t)0);
  5479. } else if (bmv->bmv_length < 0)
  5480. return XFS_ERROR(EINVAL);
  5481. if (bmv->bmv_length == 0) {
  5482. bmv->bmv_entries = 0;
  5483. return 0;
  5484. }
  5485. nex = bmv->bmv_count - 1;
  5486. if (nex <= 0)
  5487. return XFS_ERROR(EINVAL);
  5488. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5489. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5490. if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
  5491. /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
  5492. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  5493. }
  5494. ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
  5495. lock = xfs_ilock_map_shared(ip);
  5496. /*
  5497. * Don't let nex be bigger than the number of extents
  5498. * we can have assuming alternating holes and real extents.
  5499. */
  5500. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5501. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5502. bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
  5503. ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
  5504. /*
  5505. * Allocate enough space to handle "subnex" maps at a time.
  5506. */
  5507. subnex = 16;
  5508. map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
  5509. bmv->bmv_entries = 0;
  5510. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
  5511. error = 0;
  5512. goto unlock_and_return;
  5513. }
  5514. nexleft = nex;
  5515. do {
  5516. nmap = (nexleft > subnex) ? subnex : nexleft;
  5517. error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5518. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5519. bmapi_flags, NULL, 0, map, &nmap, NULL);
  5520. if (error)
  5521. goto unlock_and_return;
  5522. ASSERT(nmap <= subnex);
  5523. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5524. nexleft--;
  5525. oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
  5526. BMV_OF_PREALLOC : 0;
  5527. out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5528. out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5529. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  5530. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5531. ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
  5532. whichfork == XFS_ATTR_FORK )) {
  5533. /*
  5534. * came to hole at end of file or the end of
  5535. attribute fork
  5536. */
  5537. goto unlock_and_return;
  5538. } else {
  5539. out.bmv_block =
  5540. (map[i].br_startblock == HOLESTARTBLOCK) ?
  5541. -1 :
  5542. XFS_FSB_TO_DB(ip, map[i].br_startblock);
  5543. /* return either getbmap/getbmapx structure. */
  5544. if (interface & BMV_IF_EXTENDED) {
  5545. struct getbmapx outx;
  5546. GETBMAP_CONVERT(out,outx);
  5547. outx.bmv_oflags = oflags;
  5548. outx.bmv_unused1 = outx.bmv_unused2 = 0;
  5549. if (copy_to_user(ap, &outx,
  5550. sizeof(outx))) {
  5551. error = XFS_ERROR(EFAULT);
  5552. goto unlock_and_return;
  5553. }
  5554. } else {
  5555. if (copy_to_user(ap, &out,
  5556. sizeof(out))) {
  5557. error = XFS_ERROR(EFAULT);
  5558. goto unlock_and_return;
  5559. }
  5560. }
  5561. bmv->bmv_offset =
  5562. out.bmv_offset + out.bmv_length;
  5563. bmv->bmv_length = MAX((__int64_t)0,
  5564. (__int64_t)(bmvend - bmv->bmv_offset));
  5565. bmv->bmv_entries++;
  5566. ap = (interface & BMV_IF_EXTENDED) ?
  5567. (void __user *)
  5568. ((struct getbmapx __user *)ap + 1) :
  5569. (void __user *)
  5570. ((struct getbmap __user *)ap + 1);
  5571. }
  5572. }
  5573. } while (nmap && nexleft && bmv->bmv_length);
  5574. unlock_and_return:
  5575. xfs_iunlock_map_shared(ip, lock);
  5576. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5577. kmem_free(map, subnex * sizeof(*map));
  5578. return error;
  5579. }
  5580. /*
  5581. * Check the last inode extent to determine whether this allocation will result
  5582. * in blocks being allocated at the end of the file. When we allocate new data
  5583. * blocks at the end of the file which do not start at the previous data block,
  5584. * we will try to align the new blocks at stripe unit boundaries.
  5585. */
  5586. STATIC int /* error */
  5587. xfs_bmap_isaeof(
  5588. xfs_inode_t *ip, /* incore inode pointer */
  5589. xfs_fileoff_t off, /* file offset in fsblocks */
  5590. int whichfork, /* data or attribute fork */
  5591. char *aeof) /* return value */
  5592. {
  5593. int error; /* error return value */
  5594. xfs_ifork_t *ifp; /* inode fork pointer */
  5595. xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
  5596. xfs_extnum_t nextents; /* size of extent list */
  5597. xfs_bmbt_irec_t s; /* expanded extent list entry */
  5598. ASSERT(whichfork == XFS_DATA_FORK);
  5599. ifp = XFS_IFORK_PTR(ip, whichfork);
  5600. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5601. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5602. return error;
  5603. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5604. if (nextents == 0) {
  5605. *aeof = 1;
  5606. return 0;
  5607. }
  5608. /*
  5609. * Go to the last extent
  5610. */
  5611. lastrec = &ifp->if_u1.if_extents[nextents - 1];
  5612. xfs_bmbt_get_all(lastrec, &s);
  5613. /*
  5614. * Check we are allocating in the last extent (for delayed allocations)
  5615. * or past the last extent for non-delayed allocations.
  5616. */
  5617. *aeof = (off >= s.br_startoff &&
  5618. off < s.br_startoff + s.br_blockcount &&
  5619. ISNULLSTARTBLOCK(s.br_startblock)) ||
  5620. off >= s.br_startoff + s.br_blockcount;
  5621. return 0;
  5622. }
  5623. /*
  5624. * Check if the endoff is outside the last extent. If so the caller will grow
  5625. * the allocation to a stripe unit boundary.
  5626. */
  5627. int /* error */
  5628. xfs_bmap_eof(
  5629. xfs_inode_t *ip, /* incore inode pointer */
  5630. xfs_fileoff_t endoff, /* file offset in fsblocks */
  5631. int whichfork, /* data or attribute fork */
  5632. int *eof) /* result value */
  5633. {
  5634. xfs_fsblock_t blockcount; /* extent block count */
  5635. int error; /* error return value */
  5636. xfs_ifork_t *ifp; /* inode fork pointer */
  5637. xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
  5638. xfs_extnum_t nextents; /* size of extent list */
  5639. xfs_fileoff_t startoff; /* extent starting file offset */
  5640. ASSERT(whichfork == XFS_DATA_FORK);
  5641. ifp = XFS_IFORK_PTR(ip, whichfork);
  5642. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5643. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5644. return error;
  5645. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5646. if (nextents == 0) {
  5647. *eof = 1;
  5648. return 0;
  5649. }
  5650. /*
  5651. * Go to the last extent
  5652. */
  5653. lastrec = &ifp->if_u1.if_extents[nextents - 1];
  5654. startoff = xfs_bmbt_get_startoff(lastrec);
  5655. blockcount = xfs_bmbt_get_blockcount(lastrec);
  5656. *eof = endoff >= startoff + blockcount;
  5657. return 0;
  5658. }
  5659. #ifdef DEBUG
  5660. /*
  5661. * Check that the extents list for the inode ip is in the right order.
  5662. */
  5663. STATIC void
  5664. xfs_bmap_check_extents(
  5665. xfs_inode_t *ip, /* incore inode pointer */
  5666. int whichfork) /* data or attr fork */
  5667. {
  5668. xfs_bmbt_rec_t *base; /* base of extents list */
  5669. xfs_bmbt_rec_t *ep; /* current extent entry */
  5670. xfs_ifork_t *ifp; /* inode fork pointer */
  5671. xfs_extnum_t nextents; /* number of extents in list */
  5672. ifp = XFS_IFORK_PTR(ip, whichfork);
  5673. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  5674. base = ifp->if_u1.if_extents;
  5675. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5676. for (ep = base; ep < &base[nextents - 1]; ep++) {
  5677. xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
  5678. (void *)(ep + 1));
  5679. }
  5680. }
  5681. STATIC
  5682. xfs_buf_t *
  5683. xfs_bmap_get_bp(
  5684. xfs_btree_cur_t *cur,
  5685. xfs_fsblock_t bno)
  5686. {
  5687. int i;
  5688. xfs_buf_t *bp;
  5689. if (!cur)
  5690. return(NULL);
  5691. bp = NULL;
  5692. for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5693. bp = cur->bc_bufs[i];
  5694. if (!bp) break;
  5695. if (XFS_BUF_ADDR(bp) == bno)
  5696. break; /* Found it */
  5697. }
  5698. if (i == XFS_BTREE_MAXLEVELS)
  5699. bp = NULL;
  5700. if (!bp) { /* Chase down all the log items to see if the bp is there */
  5701. xfs_log_item_chunk_t *licp;
  5702. xfs_trans_t *tp;
  5703. tp = cur->bc_tp;
  5704. licp = &tp->t_items;
  5705. while (!bp && licp != NULL) {
  5706. if (XFS_LIC_ARE_ALL_FREE(licp)) {
  5707. licp = licp->lic_next;
  5708. continue;
  5709. }
  5710. for (i = 0; i < licp->lic_unused; i++) {
  5711. xfs_log_item_desc_t *lidp;
  5712. xfs_log_item_t *lip;
  5713. xfs_buf_log_item_t *bip;
  5714. xfs_buf_t *lbp;
  5715. if (XFS_LIC_ISFREE(licp, i)) {
  5716. continue;
  5717. }
  5718. lidp = XFS_LIC_SLOT(licp, i);
  5719. lip = lidp->lid_item;
  5720. if (lip->li_type != XFS_LI_BUF)
  5721. continue;
  5722. bip = (xfs_buf_log_item_t *)lip;
  5723. lbp = bip->bli_buf;
  5724. if (XFS_BUF_ADDR(lbp) == bno) {
  5725. bp = lbp;
  5726. break; /* Found it */
  5727. }
  5728. }
  5729. licp = licp->lic_next;
  5730. }
  5731. }
  5732. return(bp);
  5733. }
  5734. void
  5735. xfs_check_block(
  5736. xfs_bmbt_block_t *block,
  5737. xfs_mount_t *mp,
  5738. int root,
  5739. short sz)
  5740. {
  5741. int i, j, dmxr;
  5742. xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
  5743. xfs_bmbt_key_t *prevp, *keyp;
  5744. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5745. prevp = NULL;
  5746. for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
  5747. dmxr = mp->m_bmap_dmxr[0];
  5748. if (root) {
  5749. keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
  5750. } else {
  5751. keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
  5752. xfs_bmbt, block, i, dmxr);
  5753. }
  5754. if (prevp) {
  5755. xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
  5756. }
  5757. prevp = keyp;
  5758. /*
  5759. * Compare the block numbers to see if there are dups.
  5760. */
  5761. if (root) {
  5762. pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
  5763. } else {
  5764. pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
  5765. xfs_bmbt, block, i, dmxr);
  5766. }
  5767. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5768. if (root) {
  5769. thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
  5770. } else {
  5771. thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
  5772. xfs_bmbt, block, j, dmxr);
  5773. }
  5774. if (INT_GET(*thispa, ARCH_CONVERT) ==
  5775. INT_GET(*pp, ARCH_CONVERT)) {
  5776. cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
  5777. __FUNCTION__, j, i,
  5778. INT_GET(*thispa, ARCH_CONVERT));
  5779. panic("%s: ptrs are equal in node\n",
  5780. __FUNCTION__);
  5781. }
  5782. }
  5783. }
  5784. }
  5785. /*
  5786. * Check that the extents for the inode ip are in the right order in all
  5787. * btree leaves.
  5788. */
  5789. STATIC void
  5790. xfs_bmap_check_leaf_extents(
  5791. xfs_btree_cur_t *cur, /* btree cursor or null */
  5792. xfs_inode_t *ip, /* incore inode pointer */
  5793. int whichfork) /* data or attr fork */
  5794. {
  5795. xfs_bmbt_block_t *block; /* current btree block */
  5796. xfs_fsblock_t bno; /* block # of "block" */
  5797. xfs_buf_t *bp; /* buffer for "block" */
  5798. int error; /* error return value */
  5799. xfs_extnum_t i=0; /* index into the extents list */
  5800. xfs_ifork_t *ifp; /* fork structure */
  5801. int level; /* btree level, for checking */
  5802. xfs_mount_t *mp; /* file system mount structure */
  5803. xfs_bmbt_ptr_t *pp; /* pointer to block address */
  5804. xfs_bmbt_rec_t *ep, *lastp; /* extent pointers in block entry */
  5805. int bp_release = 0;
  5806. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  5807. return;
  5808. }
  5809. bno = NULLFSBLOCK;
  5810. mp = ip->i_mount;
  5811. ifp = XFS_IFORK_PTR(ip, whichfork);
  5812. block = ifp->if_broot;
  5813. /*
  5814. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5815. */
  5816. level = be16_to_cpu(block->bb_level);
  5817. ASSERT(level > 0);
  5818. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  5819. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  5820. ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
  5821. ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
  5822. ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
  5823. bno = INT_GET(*pp, ARCH_CONVERT);
  5824. /*
  5825. * Go down the tree until leaf level is reached, following the first
  5826. * pointer (leftmost) at each level.
  5827. */
  5828. while (level-- > 0) {
  5829. /* See if buf is in cur first */
  5830. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5831. if (bp) {
  5832. bp_release = 0;
  5833. } else {
  5834. bp_release = 1;
  5835. }
  5836. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5837. XFS_BMAP_BTREE_REF)))
  5838. goto error_norelse;
  5839. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  5840. XFS_WANT_CORRUPTED_GOTO(
  5841. XFS_BMAP_SANITY_CHECK(mp, block, level),
  5842. error0);
  5843. if (level == 0)
  5844. break;
  5845. /*
  5846. * Check this block for basic sanity (increasing keys and
  5847. * no duplicate blocks).
  5848. */
  5849. xfs_check_block(block, mp, 0, 0);
  5850. pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
  5851. 1, mp->m_bmap_dmxr[1]);
  5852. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
  5853. bno = INT_GET(*pp, ARCH_CONVERT);
  5854. if (bp_release) {
  5855. bp_release = 0;
  5856. xfs_trans_brelse(NULL, bp);
  5857. }
  5858. }
  5859. /*
  5860. * Here with bp and block set to the leftmost leaf node in the tree.
  5861. */
  5862. i = 0;
  5863. /*
  5864. * Loop over all leaf nodes checking that all extents are in the right order.
  5865. */
  5866. lastp = NULL;
  5867. for (;;) {
  5868. xfs_bmbt_rec_t *frp;
  5869. xfs_fsblock_t nextbno;
  5870. xfs_extnum_t num_recs;
  5871. num_recs = be16_to_cpu(block->bb_numrecs);
  5872. /*
  5873. * Read-ahead the next leaf block, if any.
  5874. */
  5875. nextbno = be64_to_cpu(block->bb_rightsib);
  5876. /*
  5877. * Check all the extents to make sure they are OK.
  5878. * If we had a previous block, the last entry should
  5879. * conform with the first entry in this one.
  5880. */
  5881. frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
  5882. block, 1, mp->m_bmap_dmxr[0]);
  5883. for (ep = frp;ep < frp + (num_recs - 1); ep++) {
  5884. if (lastp) {
  5885. xfs_btree_check_rec(XFS_BTNUM_BMAP,
  5886. (void *)lastp, (void *)ep);
  5887. }
  5888. xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
  5889. (void *)(ep + 1));
  5890. }
  5891. lastp = frp + num_recs - 1; /* For the next iteration */
  5892. i += num_recs;
  5893. if (bp_release) {
  5894. bp_release = 0;
  5895. xfs_trans_brelse(NULL, bp);
  5896. }
  5897. bno = nextbno;
  5898. /*
  5899. * If we've reached the end, stop.
  5900. */
  5901. if (bno == NULLFSBLOCK)
  5902. break;
  5903. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5904. if (bp) {
  5905. bp_release = 0;
  5906. } else {
  5907. bp_release = 1;
  5908. }
  5909. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5910. XFS_BMAP_BTREE_REF)))
  5911. goto error_norelse;
  5912. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  5913. }
  5914. if (bp_release) {
  5915. bp_release = 0;
  5916. xfs_trans_brelse(NULL, bp);
  5917. }
  5918. return;
  5919. error0:
  5920. cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
  5921. if (bp_release)
  5922. xfs_trans_brelse(NULL, bp);
  5923. error_norelse:
  5924. cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
  5925. __FUNCTION__, i);
  5926. panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
  5927. return;
  5928. }
  5929. #endif
  5930. /*
  5931. * Count fsblocks of the given fork.
  5932. */
  5933. int /* error */
  5934. xfs_bmap_count_blocks(
  5935. xfs_trans_t *tp, /* transaction pointer */
  5936. xfs_inode_t *ip, /* incore inode */
  5937. int whichfork, /* data or attr fork */
  5938. int *count) /* out: count of blocks */
  5939. {
  5940. xfs_bmbt_block_t *block; /* current btree block */
  5941. xfs_fsblock_t bno; /* block # of "block" */
  5942. xfs_ifork_t *ifp; /* fork structure */
  5943. int level; /* btree level, for checking */
  5944. xfs_mount_t *mp; /* file system mount structure */
  5945. xfs_bmbt_ptr_t *pp; /* pointer to block address */
  5946. bno = NULLFSBLOCK;
  5947. mp = ip->i_mount;
  5948. ifp = XFS_IFORK_PTR(ip, whichfork);
  5949. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  5950. if (unlikely(xfs_bmap_count_leaves(ifp->if_u1.if_extents,
  5951. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  5952. count) < 0)) {
  5953. XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
  5954. XFS_ERRLEVEL_LOW, mp);
  5955. return XFS_ERROR(EFSCORRUPTED);
  5956. }
  5957. return 0;
  5958. }
  5959. /*
  5960. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5961. */
  5962. block = ifp->if_broot;
  5963. level = be16_to_cpu(block->bb_level);
  5964. ASSERT(level > 0);
  5965. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  5966. ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
  5967. ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
  5968. ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
  5969. bno = INT_GET(*pp, ARCH_CONVERT);
  5970. if (unlikely(xfs_bmap_count_tree(mp, tp, bno, level, count) < 0)) {
  5971. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  5972. mp);
  5973. return XFS_ERROR(EFSCORRUPTED);
  5974. }
  5975. return 0;
  5976. }
  5977. /*
  5978. * Recursively walks each level of a btree
  5979. * to count total fsblocks is use.
  5980. */
  5981. int /* error */
  5982. xfs_bmap_count_tree(
  5983. xfs_mount_t *mp, /* file system mount point */
  5984. xfs_trans_t *tp, /* transaction pointer */
  5985. xfs_fsblock_t blockno, /* file system block number */
  5986. int levelin, /* level in btree */
  5987. int *count) /* Count of blocks */
  5988. {
  5989. int error;
  5990. xfs_buf_t *bp, *nbp;
  5991. int level = levelin;
  5992. xfs_bmbt_ptr_t *pp;
  5993. xfs_fsblock_t bno = blockno;
  5994. xfs_fsblock_t nextbno;
  5995. xfs_bmbt_block_t *block, *nextblock;
  5996. int numrecs;
  5997. xfs_bmbt_rec_t *frp;
  5998. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
  5999. return error;
  6000. *count += 1;
  6001. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  6002. if (--level) {
  6003. /* Not at node above leafs, count this level of nodes */
  6004. nextbno = be64_to_cpu(block->bb_rightsib);
  6005. while (nextbno != NULLFSBLOCK) {
  6006. if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
  6007. 0, &nbp, XFS_BMAP_BTREE_REF)))
  6008. return error;
  6009. *count += 1;
  6010. nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
  6011. nextbno = be64_to_cpu(nextblock->bb_rightsib);
  6012. xfs_trans_brelse(tp, nbp);
  6013. }
  6014. /* Dive to the next level */
  6015. pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
  6016. xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
  6017. bno = INT_GET(*pp, ARCH_CONVERT);
  6018. if (unlikely((error =
  6019. xfs_bmap_count_tree(mp, tp, bno, level, count)) < 0)) {
  6020. xfs_trans_brelse(tp, bp);
  6021. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  6022. XFS_ERRLEVEL_LOW, mp);
  6023. return XFS_ERROR(EFSCORRUPTED);
  6024. }
  6025. xfs_trans_brelse(tp, bp);
  6026. } else {
  6027. /* count all level 1 nodes and their leaves */
  6028. for (;;) {
  6029. nextbno = be64_to_cpu(block->bb_rightsib);
  6030. numrecs = be16_to_cpu(block->bb_numrecs);
  6031. frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
  6032. xfs_bmbt, block, 1, mp->m_bmap_dmxr[0]);
  6033. if (unlikely(xfs_bmap_disk_count_leaves(frp, numrecs, count) < 0)) {
  6034. xfs_trans_brelse(tp, bp);
  6035. XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
  6036. XFS_ERRLEVEL_LOW, mp);
  6037. return XFS_ERROR(EFSCORRUPTED);
  6038. }
  6039. xfs_trans_brelse(tp, bp);
  6040. if (nextbno == NULLFSBLOCK)
  6041. break;
  6042. bno = nextbno;
  6043. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  6044. XFS_BMAP_BTREE_REF)))
  6045. return error;
  6046. *count += 1;
  6047. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  6048. }
  6049. }
  6050. return 0;
  6051. }
  6052. /*
  6053. * Count leaf blocks given a pointer to an extent list.
  6054. */
  6055. int
  6056. xfs_bmap_count_leaves(
  6057. xfs_bmbt_rec_t *frp,
  6058. int numrecs,
  6059. int *count)
  6060. {
  6061. int b;
  6062. for ( b = 1; b <= numrecs; b++, frp++)
  6063. *count += xfs_bmbt_get_blockcount(frp);
  6064. return 0;
  6065. }
  6066. /*
  6067. * Count leaf blocks given a pointer to an extent list originally in btree format.
  6068. */
  6069. int
  6070. xfs_bmap_disk_count_leaves(
  6071. xfs_bmbt_rec_t *frp,
  6072. int numrecs,
  6073. int *count)
  6074. {
  6075. int b;
  6076. for ( b = 1; b <= numrecs; b++, frp++)
  6077. *count += xfs_bmbt_disk_get_blockcount(frp);
  6078. return 0;
  6079. }