xfs_bmap.c 189 KB

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