xfs_bmap.c 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293
  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_nullfb(
  2409. struct xfs_bmalloca *ap,
  2410. struct xfs_alloc_arg *args,
  2411. xfs_extlen_t *blen)
  2412. {
  2413. struct xfs_mount *mp = ap->ip->i_mount;
  2414. struct xfs_perag *pag;
  2415. xfs_agnumber_t ag, startag;
  2416. int notinit = 0;
  2417. int error;
  2418. if (ap->userdata && xfs_inode_is_filestream(ap->ip))
  2419. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2420. else
  2421. args->type = XFS_ALLOCTYPE_START_BNO;
  2422. args->total = ap->total;
  2423. /*
  2424. * Search for an allocation group with a single extent large enough
  2425. * for the request. If one isn't found, then adjust the minimum
  2426. * allocation size to the largest space found.
  2427. */
  2428. startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2429. if (startag == NULLAGNUMBER)
  2430. startag = ag = 0;
  2431. pag = xfs_perag_get(mp, ag);
  2432. while (*blen < ap->alen) {
  2433. if (!pag->pagf_init) {
  2434. error = xfs_alloc_pagf_init(mp, args->tp, ag,
  2435. XFS_ALLOC_FLAG_TRYLOCK);
  2436. if (error) {
  2437. xfs_perag_put(pag);
  2438. return error;
  2439. }
  2440. }
  2441. /*
  2442. * See xfs_alloc_fix_freelist...
  2443. */
  2444. if (pag->pagf_init) {
  2445. xfs_extlen_t longest;
  2446. longest = xfs_alloc_longest_free_extent(mp, pag);
  2447. if (*blen < longest)
  2448. *blen = longest;
  2449. } else
  2450. notinit = 1;
  2451. if (xfs_inode_is_filestream(ap->ip)) {
  2452. if (*blen >= ap->alen)
  2453. break;
  2454. if (ap->userdata) {
  2455. /*
  2456. * If startag is an invalid AG, we've
  2457. * come here once before and
  2458. * xfs_filestream_new_ag picked the
  2459. * best currently available.
  2460. *
  2461. * Don't continue looping, since we
  2462. * could loop forever.
  2463. */
  2464. if (startag == NULLAGNUMBER)
  2465. break;
  2466. error = xfs_filestream_new_ag(ap, &ag);
  2467. xfs_perag_put(pag);
  2468. if (error)
  2469. return error;
  2470. /* loop again to set 'blen'*/
  2471. startag = NULLAGNUMBER;
  2472. pag = xfs_perag_get(mp, ag);
  2473. continue;
  2474. }
  2475. }
  2476. if (++ag == mp->m_sb.sb_agcount)
  2477. ag = 0;
  2478. if (ag == startag)
  2479. break;
  2480. xfs_perag_put(pag);
  2481. pag = xfs_perag_get(mp, ag);
  2482. }
  2483. xfs_perag_put(pag);
  2484. /*
  2485. * Since the above loop did a BUF_TRYLOCK, it is
  2486. * possible that there is space for this request.
  2487. */
  2488. if (notinit || *blen < ap->minlen)
  2489. args->minlen = ap->minlen;
  2490. /*
  2491. * If the best seen length is less than the request
  2492. * length, use the best as the minimum.
  2493. */
  2494. else if (*blen < ap->alen)
  2495. args->minlen = *blen;
  2496. /*
  2497. * Otherwise we've seen an extent as big as alen,
  2498. * use that as the minimum.
  2499. */
  2500. else
  2501. args->minlen = ap->alen;
  2502. /*
  2503. * set the failure fallback case to look in the selected
  2504. * AG as the stream may have moved.
  2505. */
  2506. if (xfs_inode_is_filestream(ap->ip))
  2507. ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  2508. return 0;
  2509. }
  2510. STATIC int
  2511. xfs_bmap_btalloc(
  2512. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2513. {
  2514. xfs_mount_t *mp; /* mount point structure */
  2515. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2516. xfs_extlen_t align; /* minimum allocation alignment */
  2517. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2518. xfs_agnumber_t ag;
  2519. xfs_alloc_arg_t args;
  2520. xfs_extlen_t blen;
  2521. xfs_extlen_t nextminlen = 0;
  2522. int nullfb; /* true if ap->firstblock isn't set */
  2523. int isaligned;
  2524. int tryagain;
  2525. int error;
  2526. mp = ap->ip->i_mount;
  2527. align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
  2528. if (unlikely(align)) {
  2529. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2530. align, 0, ap->eof, 0, ap->conv,
  2531. &ap->off, &ap->alen);
  2532. ASSERT(!error);
  2533. ASSERT(ap->alen);
  2534. }
  2535. nullfb = ap->firstblock == NULLFSBLOCK;
  2536. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2537. if (nullfb) {
  2538. if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
  2539. ag = xfs_filestream_lookup_ag(ap->ip);
  2540. ag = (ag != NULLAGNUMBER) ? ag : 0;
  2541. ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
  2542. } else {
  2543. ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2544. }
  2545. } else
  2546. ap->rval = ap->firstblock;
  2547. xfs_bmap_adjacent(ap);
  2548. /*
  2549. * If allowed, use ap->rval; otherwise must use firstblock since
  2550. * it's in the right allocation group.
  2551. */
  2552. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
  2553. ;
  2554. else
  2555. ap->rval = ap->firstblock;
  2556. /*
  2557. * Normal allocation, done through xfs_alloc_vextent.
  2558. */
  2559. tryagain = isaligned = 0;
  2560. args.tp = ap->tp;
  2561. args.mp = mp;
  2562. args.fsbno = ap->rval;
  2563. args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
  2564. args.firstblock = ap->firstblock;
  2565. blen = 0;
  2566. if (nullfb) {
  2567. error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
  2568. if (error)
  2569. return error;
  2570. } else if (ap->low) {
  2571. if (xfs_inode_is_filestream(ap->ip))
  2572. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2573. else
  2574. args.type = XFS_ALLOCTYPE_START_BNO;
  2575. args.total = args.minlen = ap->minlen;
  2576. } else {
  2577. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2578. args.total = ap->total;
  2579. args.minlen = ap->minlen;
  2580. }
  2581. /* apply extent size hints if obtained earlier */
  2582. if (unlikely(align)) {
  2583. args.prod = align;
  2584. if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
  2585. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2586. } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
  2587. args.prod = 1;
  2588. args.mod = 0;
  2589. } else {
  2590. args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
  2591. if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
  2592. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2593. }
  2594. /*
  2595. * If we are not low on available data blocks, and the
  2596. * underlying logical volume manager is a stripe, and
  2597. * the file offset is zero then try to allocate data
  2598. * blocks on stripe unit boundary.
  2599. * NOTE: ap->aeof is only set if the allocation length
  2600. * is >= the stripe unit and the allocation offset is
  2601. * at the end of file.
  2602. */
  2603. if (!ap->low && ap->aeof) {
  2604. if (!ap->off) {
  2605. args.alignment = mp->m_dalign;
  2606. atype = args.type;
  2607. isaligned = 1;
  2608. /*
  2609. * Adjust for alignment
  2610. */
  2611. if (blen > args.alignment && blen <= ap->alen)
  2612. args.minlen = blen - args.alignment;
  2613. args.minalignslop = 0;
  2614. } else {
  2615. /*
  2616. * First try an exact bno allocation.
  2617. * If it fails then do a near or start bno
  2618. * allocation with alignment turned on.
  2619. */
  2620. atype = args.type;
  2621. tryagain = 1;
  2622. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2623. args.alignment = 1;
  2624. /*
  2625. * Compute the minlen+alignment for the
  2626. * next case. Set slop so that the value
  2627. * of minlen+alignment+slop doesn't go up
  2628. * between the calls.
  2629. */
  2630. if (blen > mp->m_dalign && blen <= ap->alen)
  2631. nextminlen = blen - mp->m_dalign;
  2632. else
  2633. nextminlen = args.minlen;
  2634. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2635. args.minalignslop =
  2636. nextminlen + mp->m_dalign -
  2637. args.minlen - 1;
  2638. else
  2639. args.minalignslop = 0;
  2640. }
  2641. } else {
  2642. args.alignment = 1;
  2643. args.minalignslop = 0;
  2644. }
  2645. args.minleft = ap->minleft;
  2646. args.wasdel = ap->wasdel;
  2647. args.isfl = 0;
  2648. args.userdata = ap->userdata;
  2649. if ((error = xfs_alloc_vextent(&args)))
  2650. return error;
  2651. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2652. /*
  2653. * Exact allocation failed. Now try with alignment
  2654. * turned on.
  2655. */
  2656. args.type = atype;
  2657. args.fsbno = ap->rval;
  2658. args.alignment = mp->m_dalign;
  2659. args.minlen = nextminlen;
  2660. args.minalignslop = 0;
  2661. isaligned = 1;
  2662. if ((error = xfs_alloc_vextent(&args)))
  2663. return error;
  2664. }
  2665. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2666. /*
  2667. * allocation failed, so turn off alignment and
  2668. * try again.
  2669. */
  2670. args.type = atype;
  2671. args.fsbno = ap->rval;
  2672. args.alignment = 0;
  2673. if ((error = xfs_alloc_vextent(&args)))
  2674. return error;
  2675. }
  2676. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2677. args.minlen > ap->minlen) {
  2678. args.minlen = ap->minlen;
  2679. args.type = XFS_ALLOCTYPE_START_BNO;
  2680. args.fsbno = ap->rval;
  2681. if ((error = xfs_alloc_vextent(&args)))
  2682. return error;
  2683. }
  2684. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2685. args.fsbno = 0;
  2686. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2687. args.total = ap->minlen;
  2688. args.minleft = 0;
  2689. if ((error = xfs_alloc_vextent(&args)))
  2690. return error;
  2691. ap->low = 1;
  2692. }
  2693. if (args.fsbno != NULLFSBLOCK) {
  2694. ap->firstblock = ap->rval = args.fsbno;
  2695. ASSERT(nullfb || fb_agno == args.agno ||
  2696. (ap->low && fb_agno < args.agno));
  2697. ap->alen = args.len;
  2698. ap->ip->i_d.di_nblocks += args.len;
  2699. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2700. if (ap->wasdel)
  2701. ap->ip->i_delayed_blks -= args.len;
  2702. /*
  2703. * Adjust the disk quota also. This was reserved
  2704. * earlier.
  2705. */
  2706. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  2707. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2708. XFS_TRANS_DQ_BCOUNT,
  2709. (long) args.len);
  2710. } else {
  2711. ap->rval = NULLFSBLOCK;
  2712. ap->alen = 0;
  2713. }
  2714. return 0;
  2715. }
  2716. /*
  2717. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2718. * It figures out where to ask the underlying allocator to put the new extent.
  2719. */
  2720. STATIC int
  2721. xfs_bmap_alloc(
  2722. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2723. {
  2724. if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
  2725. return xfs_bmap_rtalloc(ap);
  2726. return xfs_bmap_btalloc(ap);
  2727. }
  2728. /*
  2729. * Transform a btree format file with only one leaf node, where the
  2730. * extents list will fit in the inode, into an extents format file.
  2731. * Since the file extents are already in-core, all we have to do is
  2732. * give up the space for the btree root and pitch the leaf block.
  2733. */
  2734. STATIC int /* error */
  2735. xfs_bmap_btree_to_extents(
  2736. xfs_trans_t *tp, /* transaction pointer */
  2737. xfs_inode_t *ip, /* incore inode pointer */
  2738. xfs_btree_cur_t *cur, /* btree cursor */
  2739. int *logflagsp, /* inode logging flags */
  2740. int whichfork) /* data or attr fork */
  2741. {
  2742. /* REFERENCED */
  2743. struct xfs_btree_block *cblock;/* child btree block */
  2744. xfs_fsblock_t cbno; /* child block number */
  2745. xfs_buf_t *cbp; /* child block's buffer */
  2746. int error; /* error return value */
  2747. xfs_ifork_t *ifp; /* inode fork data */
  2748. xfs_mount_t *mp; /* mount point structure */
  2749. __be64 *pp; /* ptr to block address */
  2750. struct xfs_btree_block *rblock;/* root btree block */
  2751. mp = ip->i_mount;
  2752. ifp = XFS_IFORK_PTR(ip, whichfork);
  2753. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2754. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2755. rblock = ifp->if_broot;
  2756. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2757. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2758. ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
  2759. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
  2760. cbno = be64_to_cpu(*pp);
  2761. *logflagsp = 0;
  2762. #ifdef DEBUG
  2763. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  2764. return error;
  2765. #endif
  2766. if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
  2767. XFS_BMAP_BTREE_REF)))
  2768. return error;
  2769. cblock = XFS_BUF_TO_BLOCK(cbp);
  2770. if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
  2771. return error;
  2772. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2773. ip->i_d.di_nblocks--;
  2774. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2775. xfs_trans_binval(tp, cbp);
  2776. if (cur->bc_bufs[0] == cbp)
  2777. cur->bc_bufs[0] = NULL;
  2778. xfs_iroot_realloc(ip, -1, whichfork);
  2779. ASSERT(ifp->if_broot == NULL);
  2780. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2781. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2782. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2783. return 0;
  2784. }
  2785. /*
  2786. * Called by xfs_bmapi to update file extent records and the btree
  2787. * after removing space (or undoing a delayed allocation).
  2788. */
  2789. STATIC int /* error */
  2790. xfs_bmap_del_extent(
  2791. xfs_inode_t *ip, /* incore inode pointer */
  2792. xfs_trans_t *tp, /* current transaction pointer */
  2793. xfs_extnum_t idx, /* extent number to update/delete */
  2794. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2795. xfs_btree_cur_t *cur, /* if null, not a btree */
  2796. xfs_bmbt_irec_t *del, /* data to remove from extents */
  2797. int *logflagsp, /* inode logging flags */
  2798. xfs_extdelta_t *delta, /* Change made to incore extents */
  2799. int whichfork, /* data or attr fork */
  2800. int rsvd) /* OK to allocate reserved blocks */
  2801. {
  2802. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2803. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2804. xfs_fsblock_t del_endblock=0; /* first block past del */
  2805. xfs_fileoff_t del_endoff; /* first offset past del */
  2806. int delay; /* current block is delayed allocated */
  2807. int do_fx; /* free extent at end of routine */
  2808. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  2809. int error; /* error return value */
  2810. int flags; /* inode logging flags */
  2811. xfs_bmbt_irec_t got; /* current extent entry */
  2812. xfs_fileoff_t got_endoff; /* first offset past got */
  2813. int i; /* temp state */
  2814. xfs_ifork_t *ifp; /* inode fork pointer */
  2815. xfs_mount_t *mp; /* mount structure */
  2816. xfs_filblks_t nblks; /* quota/sb block count */
  2817. xfs_bmbt_irec_t new; /* new record to be inserted */
  2818. /* REFERENCED */
  2819. uint qfield; /* quota field to update */
  2820. xfs_filblks_t temp; /* for indirect length calculations */
  2821. xfs_filblks_t temp2; /* for indirect length calculations */
  2822. int state = 0;
  2823. XFS_STATS_INC(xs_del_exlist);
  2824. if (whichfork == XFS_ATTR_FORK)
  2825. state |= BMAP_ATTRFORK;
  2826. mp = ip->i_mount;
  2827. ifp = XFS_IFORK_PTR(ip, whichfork);
  2828. ASSERT((idx >= 0) && (idx < ifp->if_bytes /
  2829. (uint)sizeof(xfs_bmbt_rec_t)));
  2830. ASSERT(del->br_blockcount > 0);
  2831. ep = xfs_iext_get_ext(ifp, idx);
  2832. xfs_bmbt_get_all(ep, &got);
  2833. ASSERT(got.br_startoff <= del->br_startoff);
  2834. del_endoff = del->br_startoff + del->br_blockcount;
  2835. got_endoff = got.br_startoff + got.br_blockcount;
  2836. ASSERT(got_endoff >= del_endoff);
  2837. delay = isnullstartblock(got.br_startblock);
  2838. ASSERT(isnullstartblock(del->br_startblock) == delay);
  2839. flags = 0;
  2840. qfield = 0;
  2841. error = 0;
  2842. /*
  2843. * If deleting a real allocation, must free up the disk space.
  2844. */
  2845. if (!delay) {
  2846. flags = XFS_ILOG_CORE;
  2847. /*
  2848. * Realtime allocation. Free it and record di_nblocks update.
  2849. */
  2850. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  2851. xfs_fsblock_t bno;
  2852. xfs_filblks_t len;
  2853. ASSERT(do_mod(del->br_blockcount,
  2854. mp->m_sb.sb_rextsize) == 0);
  2855. ASSERT(do_mod(del->br_startblock,
  2856. mp->m_sb.sb_rextsize) == 0);
  2857. bno = del->br_startblock;
  2858. len = del->br_blockcount;
  2859. do_div(bno, mp->m_sb.sb_rextsize);
  2860. do_div(len, mp->m_sb.sb_rextsize);
  2861. if ((error = xfs_rtfree_extent(ip->i_transp, bno,
  2862. (xfs_extlen_t)len)))
  2863. goto done;
  2864. do_fx = 0;
  2865. nblks = len * mp->m_sb.sb_rextsize;
  2866. qfield = XFS_TRANS_DQ_RTBCOUNT;
  2867. }
  2868. /*
  2869. * Ordinary allocation.
  2870. */
  2871. else {
  2872. do_fx = 1;
  2873. nblks = del->br_blockcount;
  2874. qfield = XFS_TRANS_DQ_BCOUNT;
  2875. }
  2876. /*
  2877. * Set up del_endblock and cur for later.
  2878. */
  2879. del_endblock = del->br_startblock + del->br_blockcount;
  2880. if (cur) {
  2881. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  2882. got.br_startblock, got.br_blockcount,
  2883. &i)))
  2884. goto done;
  2885. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2886. }
  2887. da_old = da_new = 0;
  2888. } else {
  2889. da_old = startblockval(got.br_startblock);
  2890. da_new = 0;
  2891. nblks = 0;
  2892. do_fx = 0;
  2893. }
  2894. /*
  2895. * Set flag value to use in switch statement.
  2896. * Left-contig is 2, right-contig is 1.
  2897. */
  2898. switch (((got.br_startoff == del->br_startoff) << 1) |
  2899. (got_endoff == del_endoff)) {
  2900. case 3:
  2901. /*
  2902. * Matches the whole extent. Delete the entry.
  2903. */
  2904. xfs_iext_remove(ip, idx, 1,
  2905. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  2906. ifp->if_lastex = idx;
  2907. if (delay)
  2908. break;
  2909. XFS_IFORK_NEXT_SET(ip, whichfork,
  2910. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2911. flags |= XFS_ILOG_CORE;
  2912. if (!cur) {
  2913. flags |= xfs_ilog_fext(whichfork);
  2914. break;
  2915. }
  2916. if ((error = xfs_btree_delete(cur, &i)))
  2917. goto done;
  2918. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2919. break;
  2920. case 2:
  2921. /*
  2922. * Deleting the first part of the extent.
  2923. */
  2924. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2925. xfs_bmbt_set_startoff(ep, del_endoff);
  2926. temp = got.br_blockcount - del->br_blockcount;
  2927. xfs_bmbt_set_blockcount(ep, temp);
  2928. ifp->if_lastex = idx;
  2929. if (delay) {
  2930. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2931. da_old);
  2932. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2933. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2934. da_new = temp;
  2935. break;
  2936. }
  2937. xfs_bmbt_set_startblock(ep, del_endblock);
  2938. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2939. if (!cur) {
  2940. flags |= xfs_ilog_fext(whichfork);
  2941. break;
  2942. }
  2943. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  2944. got.br_blockcount - del->br_blockcount,
  2945. got.br_state)))
  2946. goto done;
  2947. break;
  2948. case 1:
  2949. /*
  2950. * Deleting the last part of the extent.
  2951. */
  2952. temp = got.br_blockcount - del->br_blockcount;
  2953. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2954. xfs_bmbt_set_blockcount(ep, temp);
  2955. ifp->if_lastex = idx;
  2956. if (delay) {
  2957. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2958. da_old);
  2959. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2960. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2961. da_new = temp;
  2962. break;
  2963. }
  2964. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2965. if (!cur) {
  2966. flags |= xfs_ilog_fext(whichfork);
  2967. break;
  2968. }
  2969. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  2970. got.br_startblock,
  2971. got.br_blockcount - del->br_blockcount,
  2972. got.br_state)))
  2973. goto done;
  2974. break;
  2975. case 0:
  2976. /*
  2977. * Deleting the middle of the extent.
  2978. */
  2979. temp = del->br_startoff - got.br_startoff;
  2980. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2981. xfs_bmbt_set_blockcount(ep, temp);
  2982. new.br_startoff = del_endoff;
  2983. temp2 = got_endoff - del_endoff;
  2984. new.br_blockcount = temp2;
  2985. new.br_state = got.br_state;
  2986. if (!delay) {
  2987. new.br_startblock = del_endblock;
  2988. flags |= XFS_ILOG_CORE;
  2989. if (cur) {
  2990. if ((error = xfs_bmbt_update(cur,
  2991. got.br_startoff,
  2992. got.br_startblock, temp,
  2993. got.br_state)))
  2994. goto done;
  2995. if ((error = xfs_btree_increment(cur, 0, &i)))
  2996. goto done;
  2997. cur->bc_rec.b = new;
  2998. error = xfs_btree_insert(cur, &i);
  2999. if (error && error != ENOSPC)
  3000. goto done;
  3001. /*
  3002. * If get no-space back from btree insert,
  3003. * it tried a split, and we have a zero
  3004. * block reservation.
  3005. * Fix up our state and return the error.
  3006. */
  3007. if (error == ENOSPC) {
  3008. /*
  3009. * Reset the cursor, don't trust
  3010. * it after any insert operation.
  3011. */
  3012. if ((error = xfs_bmbt_lookup_eq(cur,
  3013. got.br_startoff,
  3014. got.br_startblock,
  3015. temp, &i)))
  3016. goto done;
  3017. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3018. /*
  3019. * Update the btree record back
  3020. * to the original value.
  3021. */
  3022. if ((error = xfs_bmbt_update(cur,
  3023. got.br_startoff,
  3024. got.br_startblock,
  3025. got.br_blockcount,
  3026. got.br_state)))
  3027. goto done;
  3028. /*
  3029. * Reset the extent record back
  3030. * to the original value.
  3031. */
  3032. xfs_bmbt_set_blockcount(ep,
  3033. got.br_blockcount);
  3034. flags = 0;
  3035. error = XFS_ERROR(ENOSPC);
  3036. goto done;
  3037. }
  3038. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3039. } else
  3040. flags |= xfs_ilog_fext(whichfork);
  3041. XFS_IFORK_NEXT_SET(ip, whichfork,
  3042. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  3043. } else {
  3044. ASSERT(whichfork == XFS_DATA_FORK);
  3045. temp = xfs_bmap_worst_indlen(ip, temp);
  3046. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  3047. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  3048. new.br_startblock = nullstartblock((int)temp2);
  3049. da_new = temp + temp2;
  3050. while (da_new > da_old) {
  3051. if (temp) {
  3052. temp--;
  3053. da_new--;
  3054. xfs_bmbt_set_startblock(ep,
  3055. nullstartblock((int)temp));
  3056. }
  3057. if (da_new == da_old)
  3058. break;
  3059. if (temp2) {
  3060. temp2--;
  3061. da_new--;
  3062. new.br_startblock =
  3063. nullstartblock((int)temp2);
  3064. }
  3065. }
  3066. }
  3067. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  3068. xfs_iext_insert(ip, idx + 1, 1, &new, state);
  3069. ifp->if_lastex = idx + 1;
  3070. break;
  3071. }
  3072. /*
  3073. * If we need to, add to list of extents to delete.
  3074. */
  3075. if (do_fx)
  3076. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  3077. mp);
  3078. /*
  3079. * Adjust inode # blocks in the file.
  3080. */
  3081. if (nblks)
  3082. ip->i_d.di_nblocks -= nblks;
  3083. /*
  3084. * Adjust quota data.
  3085. */
  3086. if (qfield)
  3087. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  3088. /*
  3089. * Account for change in delayed indirect blocks.
  3090. * Nothing to do for disk quota accounting here.
  3091. */
  3092. ASSERT(da_old >= da_new);
  3093. if (da_old > da_new)
  3094. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
  3095. rsvd);
  3096. if (delta) {
  3097. /* DELTA: report the original extent. */
  3098. if (delta->xed_startoff > got.br_startoff)
  3099. delta->xed_startoff = got.br_startoff;
  3100. if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
  3101. delta->xed_blockcount = got.br_startoff +
  3102. got.br_blockcount;
  3103. }
  3104. done:
  3105. *logflagsp = flags;
  3106. return error;
  3107. }
  3108. /*
  3109. * Remove the entry "free" from the free item list. Prev points to the
  3110. * previous entry, unless "free" is the head of the list.
  3111. */
  3112. STATIC void
  3113. xfs_bmap_del_free(
  3114. xfs_bmap_free_t *flist, /* free item list header */
  3115. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  3116. xfs_bmap_free_item_t *free) /* list item to be freed */
  3117. {
  3118. if (prev)
  3119. prev->xbfi_next = free->xbfi_next;
  3120. else
  3121. flist->xbf_first = free->xbfi_next;
  3122. flist->xbf_count--;
  3123. kmem_zone_free(xfs_bmap_free_item_zone, free);
  3124. }
  3125. /*
  3126. * Convert an extents-format file into a btree-format file.
  3127. * The new file will have a root block (in the inode) and a single child block.
  3128. */
  3129. STATIC int /* error */
  3130. xfs_bmap_extents_to_btree(
  3131. xfs_trans_t *tp, /* transaction pointer */
  3132. xfs_inode_t *ip, /* incore inode pointer */
  3133. xfs_fsblock_t *firstblock, /* first-block-allocated */
  3134. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  3135. xfs_btree_cur_t **curp, /* cursor returned to caller */
  3136. int wasdel, /* converting a delayed alloc */
  3137. int *logflagsp, /* inode logging flags */
  3138. int whichfork) /* data or attr fork */
  3139. {
  3140. struct xfs_btree_block *ablock; /* allocated (child) bt block */
  3141. xfs_buf_t *abp; /* buffer for ablock */
  3142. xfs_alloc_arg_t args; /* allocation arguments */
  3143. xfs_bmbt_rec_t *arp; /* child record pointer */
  3144. struct xfs_btree_block *block; /* btree root block */
  3145. xfs_btree_cur_t *cur; /* bmap btree cursor */
  3146. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3147. int error; /* error return value */
  3148. xfs_extnum_t i, cnt; /* extent record index */
  3149. xfs_ifork_t *ifp; /* inode fork pointer */
  3150. xfs_bmbt_key_t *kp; /* root block key pointer */
  3151. xfs_mount_t *mp; /* mount structure */
  3152. xfs_extnum_t nextents; /* number of file extents */
  3153. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  3154. ifp = XFS_IFORK_PTR(ip, whichfork);
  3155. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  3156. ASSERT(ifp->if_ext_max ==
  3157. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  3158. /*
  3159. * Make space in the inode incore.
  3160. */
  3161. xfs_iroot_realloc(ip, 1, whichfork);
  3162. ifp->if_flags |= XFS_IFBROOT;
  3163. /*
  3164. * Fill in the root.
  3165. */
  3166. block = ifp->if_broot;
  3167. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3168. block->bb_level = cpu_to_be16(1);
  3169. block->bb_numrecs = cpu_to_be16(1);
  3170. block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3171. block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3172. /*
  3173. * Need a cursor. Can't allocate until bb_level is filled in.
  3174. */
  3175. mp = ip->i_mount;
  3176. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  3177. cur->bc_private.b.firstblock = *firstblock;
  3178. cur->bc_private.b.flist = flist;
  3179. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3180. /*
  3181. * Convert to a btree with two levels, one record in root.
  3182. */
  3183. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  3184. args.tp = tp;
  3185. args.mp = mp;
  3186. args.firstblock = *firstblock;
  3187. if (*firstblock == NULLFSBLOCK) {
  3188. args.type = XFS_ALLOCTYPE_START_BNO;
  3189. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  3190. } else if (flist->xbf_low) {
  3191. args.type = XFS_ALLOCTYPE_START_BNO;
  3192. args.fsbno = *firstblock;
  3193. } else {
  3194. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3195. args.fsbno = *firstblock;
  3196. }
  3197. args.minlen = args.maxlen = args.prod = 1;
  3198. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  3199. args.minalignslop = 0;
  3200. args.wasdel = wasdel;
  3201. *logflagsp = 0;
  3202. if ((error = xfs_alloc_vextent(&args))) {
  3203. xfs_iroot_realloc(ip, -1, whichfork);
  3204. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  3205. return error;
  3206. }
  3207. /*
  3208. * Allocation can't fail, the space was reserved.
  3209. */
  3210. ASSERT(args.fsbno != NULLFSBLOCK);
  3211. ASSERT(*firstblock == NULLFSBLOCK ||
  3212. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  3213. (flist->xbf_low &&
  3214. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  3215. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  3216. cur->bc_private.b.allocated++;
  3217. ip->i_d.di_nblocks++;
  3218. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  3219. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  3220. /*
  3221. * Fill in the child block.
  3222. */
  3223. ablock = XFS_BUF_TO_BLOCK(abp);
  3224. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3225. ablock->bb_level = 0;
  3226. ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3227. ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3228. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3229. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3230. for (cnt = i = 0; i < nextents; i++) {
  3231. ep = xfs_iext_get_ext(ifp, i);
  3232. if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
  3233. arp->l0 = cpu_to_be64(ep->l0);
  3234. arp->l1 = cpu_to_be64(ep->l1);
  3235. arp++; cnt++;
  3236. }
  3237. }
  3238. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  3239. xfs_btree_set_numrecs(ablock, cnt);
  3240. /*
  3241. * Fill in the root key and pointer.
  3242. */
  3243. kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
  3244. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3245. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  3246. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
  3247. be16_to_cpu(block->bb_level)));
  3248. *pp = cpu_to_be64(args.fsbno);
  3249. /*
  3250. * Do all this logging at the end so that
  3251. * the root is at the right level.
  3252. */
  3253. xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
  3254. xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  3255. ASSERT(*curp == NULL);
  3256. *curp = cur;
  3257. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
  3258. return 0;
  3259. }
  3260. /*
  3261. * Calculate the default attribute fork offset for newly created inodes.
  3262. */
  3263. uint
  3264. xfs_default_attroffset(
  3265. struct xfs_inode *ip)
  3266. {
  3267. struct xfs_mount *mp = ip->i_mount;
  3268. uint offset;
  3269. if (mp->m_sb.sb_inodesize == 256) {
  3270. offset = XFS_LITINO(mp) -
  3271. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3272. } else {
  3273. offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  3274. }
  3275. ASSERT(offset < XFS_LITINO(mp));
  3276. return offset;
  3277. }
  3278. /*
  3279. * Helper routine to reset inode di_forkoff field when switching
  3280. * attribute fork from local to extent format - we reset it where
  3281. * possible to make space available for inline data fork extents.
  3282. */
  3283. STATIC void
  3284. xfs_bmap_forkoff_reset(
  3285. xfs_mount_t *mp,
  3286. xfs_inode_t *ip,
  3287. int whichfork)
  3288. {
  3289. if (whichfork == XFS_ATTR_FORK &&
  3290. ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
  3291. ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
  3292. ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
  3293. uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
  3294. if (dfl_forkoff > ip->i_d.di_forkoff) {
  3295. ip->i_d.di_forkoff = dfl_forkoff;
  3296. ip->i_df.if_ext_max =
  3297. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
  3298. ip->i_afp->if_ext_max =
  3299. XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
  3300. }
  3301. }
  3302. }
  3303. /*
  3304. * Convert a local file to an extents file.
  3305. * This code is out of bounds for data forks of regular files,
  3306. * since the file data needs to get logged so things will stay consistent.
  3307. * (The bmap-level manipulations are ok, though).
  3308. */
  3309. STATIC int /* error */
  3310. xfs_bmap_local_to_extents(
  3311. xfs_trans_t *tp, /* transaction pointer */
  3312. xfs_inode_t *ip, /* incore inode pointer */
  3313. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3314. xfs_extlen_t total, /* total blocks needed by transaction */
  3315. int *logflagsp, /* inode logging flags */
  3316. int whichfork) /* data or attr fork */
  3317. {
  3318. int error; /* error return value */
  3319. int flags; /* logging flags returned */
  3320. xfs_ifork_t *ifp; /* inode fork pointer */
  3321. /*
  3322. * We don't want to deal with the case of keeping inode data inline yet.
  3323. * So sending the data fork of a regular inode is invalid.
  3324. */
  3325. ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
  3326. whichfork == XFS_DATA_FORK));
  3327. ifp = XFS_IFORK_PTR(ip, whichfork);
  3328. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3329. flags = 0;
  3330. error = 0;
  3331. if (ifp->if_bytes) {
  3332. xfs_alloc_arg_t args; /* allocation arguments */
  3333. xfs_buf_t *bp; /* buffer for extent block */
  3334. xfs_bmbt_rec_host_t *ep;/* extent record pointer */
  3335. args.tp = tp;
  3336. args.mp = ip->i_mount;
  3337. args.firstblock = *firstblock;
  3338. ASSERT((ifp->if_flags &
  3339. (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
  3340. /*
  3341. * Allocate a block. We know we need only one, since the
  3342. * file currently fits in an inode.
  3343. */
  3344. if (*firstblock == NULLFSBLOCK) {
  3345. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3346. args.type = XFS_ALLOCTYPE_START_BNO;
  3347. } else {
  3348. args.fsbno = *firstblock;
  3349. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3350. }
  3351. args.total = total;
  3352. args.mod = args.minleft = args.alignment = args.wasdel =
  3353. args.isfl = args.minalignslop = 0;
  3354. args.minlen = args.maxlen = args.prod = 1;
  3355. if ((error = xfs_alloc_vextent(&args)))
  3356. goto done;
  3357. /*
  3358. * Can't fail, the space was reserved.
  3359. */
  3360. ASSERT(args.fsbno != NULLFSBLOCK);
  3361. ASSERT(args.len == 1);
  3362. *firstblock = args.fsbno;
  3363. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3364. memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
  3365. ifp->if_bytes);
  3366. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3367. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3368. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3369. xfs_iext_add(ifp, 0, 1);
  3370. ep = xfs_iext_get_ext(ifp, 0);
  3371. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3372. trace_xfs_bmap_post_update(ip, 0,
  3373. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
  3374. _THIS_IP_);
  3375. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3376. ip->i_d.di_nblocks = 1;
  3377. xfs_trans_mod_dquot_byino(tp, ip,
  3378. XFS_TRANS_DQ_BCOUNT, 1L);
  3379. flags |= xfs_ilog_fext(whichfork);
  3380. } else {
  3381. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3382. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3383. }
  3384. ifp->if_flags &= ~XFS_IFINLINE;
  3385. ifp->if_flags |= XFS_IFEXTENTS;
  3386. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3387. flags |= XFS_ILOG_CORE;
  3388. done:
  3389. *logflagsp = flags;
  3390. return error;
  3391. }
  3392. /*
  3393. * Search the extent records for the entry containing block bno.
  3394. * If bno lies in a hole, point to the next entry. If bno lies
  3395. * past eof, *eofp will be set, and *prevp will contain the last
  3396. * entry (null if none). Else, *lastxp will be set to the index
  3397. * of the found entry; *gotp will contain the entry.
  3398. */
  3399. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3400. xfs_bmap_search_multi_extents(
  3401. xfs_ifork_t *ifp, /* inode fork pointer */
  3402. xfs_fileoff_t bno, /* block number searched for */
  3403. int *eofp, /* out: end of file found */
  3404. xfs_extnum_t *lastxp, /* out: last extent index */
  3405. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3406. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3407. {
  3408. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3409. xfs_extnum_t lastx; /* last extent index */
  3410. /*
  3411. * Initialize the extent entry structure to catch access to
  3412. * uninitialized br_startblock field.
  3413. */
  3414. gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3415. gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3416. gotp->br_state = XFS_EXT_INVALID;
  3417. #if XFS_BIG_BLKNOS
  3418. gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
  3419. #else
  3420. gotp->br_startblock = 0xffffa5a5;
  3421. #endif
  3422. prevp->br_startoff = NULLFILEOFF;
  3423. ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
  3424. if (lastx > 0) {
  3425. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
  3426. }
  3427. if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  3428. xfs_bmbt_get_all(ep, gotp);
  3429. *eofp = 0;
  3430. } else {
  3431. if (lastx > 0) {
  3432. *gotp = *prevp;
  3433. }
  3434. *eofp = 1;
  3435. ep = NULL;
  3436. }
  3437. *lastxp = lastx;
  3438. return ep;
  3439. }
  3440. /*
  3441. * Search the extents list for the inode, for the extent containing bno.
  3442. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3443. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3444. * Else, *lastxp will be set to the index of the found
  3445. * entry; *gotp will contain the entry.
  3446. */
  3447. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3448. xfs_bmap_search_extents(
  3449. xfs_inode_t *ip, /* incore inode pointer */
  3450. xfs_fileoff_t bno, /* block number searched for */
  3451. int fork, /* data or attr fork */
  3452. int *eofp, /* out: end of file found */
  3453. xfs_extnum_t *lastxp, /* out: last extent index */
  3454. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3455. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3456. {
  3457. xfs_ifork_t *ifp; /* inode fork pointer */
  3458. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3459. XFS_STATS_INC(xs_look_exlist);
  3460. ifp = XFS_IFORK_PTR(ip, fork);
  3461. ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
  3462. if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
  3463. !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
  3464. xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
  3465. "Access to block zero in inode %llu "
  3466. "start_block: %llx start_off: %llx "
  3467. "blkcnt: %llx extent-state: %x lastx: %x\n",
  3468. (unsigned long long)ip->i_ino,
  3469. (unsigned long long)gotp->br_startblock,
  3470. (unsigned long long)gotp->br_startoff,
  3471. (unsigned long long)gotp->br_blockcount,
  3472. gotp->br_state, *lastxp);
  3473. *lastxp = NULLEXTNUM;
  3474. *eofp = 1;
  3475. return NULL;
  3476. }
  3477. return ep;
  3478. }
  3479. /*
  3480. * Compute the worst-case number of indirect blocks that will be used
  3481. * for ip's delayed extent of length "len".
  3482. */
  3483. STATIC xfs_filblks_t
  3484. xfs_bmap_worst_indlen(
  3485. xfs_inode_t *ip, /* incore inode pointer */
  3486. xfs_filblks_t len) /* delayed extent length */
  3487. {
  3488. int level; /* btree level number */
  3489. int maxrecs; /* maximum record count at this level */
  3490. xfs_mount_t *mp; /* mount structure */
  3491. xfs_filblks_t rval; /* return value */
  3492. mp = ip->i_mount;
  3493. maxrecs = mp->m_bmap_dmxr[0];
  3494. for (level = 0, rval = 0;
  3495. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3496. level++) {
  3497. len += maxrecs - 1;
  3498. do_div(len, maxrecs);
  3499. rval += len;
  3500. if (len == 1)
  3501. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3502. level - 1;
  3503. if (level == 0)
  3504. maxrecs = mp->m_bmap_dmxr[1];
  3505. }
  3506. return rval;
  3507. }
  3508. /*
  3509. * Convert inode from non-attributed to attributed.
  3510. * Must not be in a transaction, ip must not be locked.
  3511. */
  3512. int /* error code */
  3513. xfs_bmap_add_attrfork(
  3514. xfs_inode_t *ip, /* incore inode pointer */
  3515. int size, /* space new attribute needs */
  3516. int rsvd) /* xact may use reserved blks */
  3517. {
  3518. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3519. xfs_bmap_free_t flist; /* freed extent records */
  3520. xfs_mount_t *mp; /* mount structure */
  3521. xfs_trans_t *tp; /* transaction pointer */
  3522. int blks; /* space reservation */
  3523. int version = 1; /* superblock attr version */
  3524. int committed; /* xaction was committed */
  3525. int logflags; /* logging flags */
  3526. int error; /* error return value */
  3527. ASSERT(XFS_IFORK_Q(ip) == 0);
  3528. ASSERT(ip->i_df.if_ext_max ==
  3529. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3530. mp = ip->i_mount;
  3531. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3532. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3533. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3534. if (rsvd)
  3535. tp->t_flags |= XFS_TRANS_RESERVE;
  3536. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3537. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3538. goto error0;
  3539. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3540. error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
  3541. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3542. XFS_QMOPT_RES_REGBLKS);
  3543. if (error) {
  3544. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3545. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3546. return error;
  3547. }
  3548. if (XFS_IFORK_Q(ip))
  3549. goto error1;
  3550. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3551. /*
  3552. * For inodes coming from pre-6.2 filesystems.
  3553. */
  3554. ASSERT(ip->i_d.di_aformat == 0);
  3555. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3556. }
  3557. ASSERT(ip->i_d.di_anextents == 0);
  3558. IHOLD(ip);
  3559. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3560. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3561. switch (ip->i_d.di_format) {
  3562. case XFS_DINODE_FMT_DEV:
  3563. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3564. break;
  3565. case XFS_DINODE_FMT_UUID:
  3566. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3567. break;
  3568. case XFS_DINODE_FMT_LOCAL:
  3569. case XFS_DINODE_FMT_EXTENTS:
  3570. case XFS_DINODE_FMT_BTREE:
  3571. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3572. if (!ip->i_d.di_forkoff)
  3573. ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
  3574. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  3575. version = 2;
  3576. break;
  3577. default:
  3578. ASSERT(0);
  3579. error = XFS_ERROR(EINVAL);
  3580. goto error1;
  3581. }
  3582. ip->i_df.if_ext_max =
  3583. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3584. ASSERT(ip->i_afp == NULL);
  3585. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3586. ip->i_afp->if_ext_max =
  3587. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3588. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3589. logflags = 0;
  3590. xfs_bmap_init(&flist, &firstblock);
  3591. switch (ip->i_d.di_format) {
  3592. case XFS_DINODE_FMT_LOCAL:
  3593. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3594. &logflags);
  3595. break;
  3596. case XFS_DINODE_FMT_EXTENTS:
  3597. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3598. &flist, &logflags);
  3599. break;
  3600. case XFS_DINODE_FMT_BTREE:
  3601. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3602. &logflags);
  3603. break;
  3604. default:
  3605. error = 0;
  3606. break;
  3607. }
  3608. if (logflags)
  3609. xfs_trans_log_inode(tp, ip, logflags);
  3610. if (error)
  3611. goto error2;
  3612. if (!xfs_sb_version_hasattr(&mp->m_sb) ||
  3613. (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
  3614. __int64_t sbfields = 0;
  3615. spin_lock(&mp->m_sb_lock);
  3616. if (!xfs_sb_version_hasattr(&mp->m_sb)) {
  3617. xfs_sb_version_addattr(&mp->m_sb);
  3618. sbfields |= XFS_SB_VERSIONNUM;
  3619. }
  3620. if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
  3621. xfs_sb_version_addattr2(&mp->m_sb);
  3622. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3623. }
  3624. if (sbfields) {
  3625. spin_unlock(&mp->m_sb_lock);
  3626. xfs_mod_sb(tp, sbfields);
  3627. } else
  3628. spin_unlock(&mp->m_sb_lock);
  3629. }
  3630. if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
  3631. goto error2;
  3632. error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
  3633. ASSERT(ip->i_df.if_ext_max ==
  3634. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3635. return error;
  3636. error2:
  3637. xfs_bmap_cancel(&flist);
  3638. error1:
  3639. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3640. error0:
  3641. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3642. ASSERT(ip->i_df.if_ext_max ==
  3643. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3644. return error;
  3645. }
  3646. /*
  3647. * Add the extent to the list of extents to be free at transaction end.
  3648. * The list is maintained sorted (by block number).
  3649. */
  3650. /* ARGSUSED */
  3651. void
  3652. xfs_bmap_add_free(
  3653. xfs_fsblock_t bno, /* fs block number of extent */
  3654. xfs_filblks_t len, /* length of extent */
  3655. xfs_bmap_free_t *flist, /* list of extents */
  3656. xfs_mount_t *mp) /* mount point structure */
  3657. {
  3658. xfs_bmap_free_item_t *cur; /* current (next) element */
  3659. xfs_bmap_free_item_t *new; /* new element */
  3660. xfs_bmap_free_item_t *prev; /* previous element */
  3661. #ifdef DEBUG
  3662. xfs_agnumber_t agno;
  3663. xfs_agblock_t agbno;
  3664. ASSERT(bno != NULLFSBLOCK);
  3665. ASSERT(len > 0);
  3666. ASSERT(len <= MAXEXTLEN);
  3667. ASSERT(!isnullstartblock(bno));
  3668. agno = XFS_FSB_TO_AGNO(mp, bno);
  3669. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  3670. ASSERT(agno < mp->m_sb.sb_agcount);
  3671. ASSERT(agbno < mp->m_sb.sb_agblocks);
  3672. ASSERT(len < mp->m_sb.sb_agblocks);
  3673. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  3674. #endif
  3675. ASSERT(xfs_bmap_free_item_zone != NULL);
  3676. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  3677. new->xbfi_startblock = bno;
  3678. new->xbfi_blockcount = (xfs_extlen_t)len;
  3679. for (prev = NULL, cur = flist->xbf_first;
  3680. cur != NULL;
  3681. prev = cur, cur = cur->xbfi_next) {
  3682. if (cur->xbfi_startblock >= bno)
  3683. break;
  3684. }
  3685. if (prev)
  3686. prev->xbfi_next = new;
  3687. else
  3688. flist->xbf_first = new;
  3689. new->xbfi_next = cur;
  3690. flist->xbf_count++;
  3691. }
  3692. /*
  3693. * Compute and fill in the value of the maximum depth of a bmap btree
  3694. * in this filesystem. Done once, during mount.
  3695. */
  3696. void
  3697. xfs_bmap_compute_maxlevels(
  3698. xfs_mount_t *mp, /* file system mount structure */
  3699. int whichfork) /* data or attr fork */
  3700. {
  3701. int level; /* btree level */
  3702. uint maxblocks; /* max blocks at this level */
  3703. uint maxleafents; /* max leaf entries possible */
  3704. int maxrootrecs; /* max records in root block */
  3705. int minleafrecs; /* min records in leaf block */
  3706. int minnoderecs; /* min records in node block */
  3707. int sz; /* root block size */
  3708. /*
  3709. * The maximum number of extents in a file, hence the maximum
  3710. * number of leaf entries, is controlled by the type of di_nextents
  3711. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  3712. * (a signed 16-bit number, xfs_aextnum_t).
  3713. *
  3714. * Note that we can no longer assume that if we are in ATTR1 that
  3715. * the fork offset of all the inodes will be
  3716. * (xfs_default_attroffset(ip) >> 3) because we could have mounted
  3717. * with ATTR2 and then mounted back with ATTR1, keeping the
  3718. * di_forkoff's fixed but probably at various positions. Therefore,
  3719. * for both ATTR1 and ATTR2 we have to assume the worst case scenario
  3720. * of a minimum size available.
  3721. */
  3722. if (whichfork == XFS_DATA_FORK) {
  3723. maxleafents = MAXEXTNUM;
  3724. sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  3725. } else {
  3726. maxleafents = MAXAEXTNUM;
  3727. sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3728. }
  3729. maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
  3730. minleafrecs = mp->m_bmap_dmnr[0];
  3731. minnoderecs = mp->m_bmap_dmnr[1];
  3732. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  3733. for (level = 1; maxblocks > 1; level++) {
  3734. if (maxblocks <= maxrootrecs)
  3735. maxblocks = 1;
  3736. else
  3737. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  3738. }
  3739. mp->m_bm_maxlevels[whichfork] = level;
  3740. }
  3741. /*
  3742. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  3743. * caller. Frees all the extents that need freeing, which must be done
  3744. * last due to locking considerations. We never free any extents in
  3745. * the first transaction. This is to allow the caller to make the first
  3746. * transaction a synchronous one so that the pointers to the data being
  3747. * broken in this transaction will be permanent before the data is actually
  3748. * freed. This is necessary to prevent blocks from being reallocated
  3749. * and written to before the free and reallocation are actually permanent.
  3750. * We do not just make the first transaction synchronous here, because
  3751. * there are more efficient ways to gain the same protection in some cases
  3752. * (see the file truncation code).
  3753. *
  3754. * Return 1 if the given transaction was committed and a new one
  3755. * started, and 0 otherwise in the committed parameter.
  3756. */
  3757. /*ARGSUSED*/
  3758. int /* error */
  3759. xfs_bmap_finish(
  3760. xfs_trans_t **tp, /* transaction pointer addr */
  3761. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  3762. int *committed) /* xact committed or not */
  3763. {
  3764. xfs_efd_log_item_t *efd; /* extent free data */
  3765. xfs_efi_log_item_t *efi; /* extent free intention */
  3766. int error; /* error return value */
  3767. xfs_bmap_free_item_t *free; /* free extent item */
  3768. unsigned int logres; /* new log reservation */
  3769. unsigned int logcount; /* new log count */
  3770. xfs_mount_t *mp; /* filesystem mount structure */
  3771. xfs_bmap_free_item_t *next; /* next item on free list */
  3772. xfs_trans_t *ntp; /* new transaction pointer */
  3773. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  3774. if (flist->xbf_count == 0) {
  3775. *committed = 0;
  3776. return 0;
  3777. }
  3778. ntp = *tp;
  3779. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  3780. for (free = flist->xbf_first; free; free = free->xbfi_next)
  3781. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  3782. free->xbfi_blockcount);
  3783. logres = ntp->t_log_res;
  3784. logcount = ntp->t_log_count;
  3785. ntp = xfs_trans_dup(*tp);
  3786. error = xfs_trans_commit(*tp, 0);
  3787. *tp = ntp;
  3788. *committed = 1;
  3789. /*
  3790. * We have a new transaction, so we should return committed=1,
  3791. * even though we're returning an error.
  3792. */
  3793. if (error)
  3794. return error;
  3795. /*
  3796. * transaction commit worked ok so we can drop the extra ticket
  3797. * reference that we gained in xfs_trans_dup()
  3798. */
  3799. xfs_log_ticket_put(ntp->t_ticket);
  3800. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  3801. logcount)))
  3802. return error;
  3803. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  3804. for (free = flist->xbf_first; free != NULL; free = next) {
  3805. next = free->xbfi_next;
  3806. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  3807. free->xbfi_blockcount))) {
  3808. /*
  3809. * The bmap free list will be cleaned up at a
  3810. * higher level. The EFI will be canceled when
  3811. * this transaction is aborted.
  3812. * Need to force shutdown here to make sure it
  3813. * happens, since this transaction may not be
  3814. * dirty yet.
  3815. */
  3816. mp = ntp->t_mountp;
  3817. if (!XFS_FORCED_SHUTDOWN(mp))
  3818. xfs_force_shutdown(mp,
  3819. (error == EFSCORRUPTED) ?
  3820. SHUTDOWN_CORRUPT_INCORE :
  3821. SHUTDOWN_META_IO_ERROR);
  3822. return error;
  3823. }
  3824. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  3825. free->xbfi_blockcount);
  3826. xfs_bmap_del_free(flist, NULL, free);
  3827. }
  3828. return 0;
  3829. }
  3830. /*
  3831. * Free up any items left in the list.
  3832. */
  3833. void
  3834. xfs_bmap_cancel(
  3835. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  3836. {
  3837. xfs_bmap_free_item_t *free; /* free list item */
  3838. xfs_bmap_free_item_t *next;
  3839. if (flist->xbf_count == 0)
  3840. return;
  3841. ASSERT(flist->xbf_first != NULL);
  3842. for (free = flist->xbf_first; free; free = next) {
  3843. next = free->xbfi_next;
  3844. xfs_bmap_del_free(flist, NULL, free);
  3845. }
  3846. ASSERT(flist->xbf_count == 0);
  3847. }
  3848. /*
  3849. * Returns the file-relative block number of the first unused block(s)
  3850. * in the file with at least "len" logically contiguous blocks free.
  3851. * This is the lowest-address hole if the file has holes, else the first block
  3852. * past the end of file.
  3853. * Return 0 if the file is currently local (in-inode).
  3854. */
  3855. int /* error */
  3856. xfs_bmap_first_unused(
  3857. xfs_trans_t *tp, /* transaction pointer */
  3858. xfs_inode_t *ip, /* incore inode */
  3859. xfs_extlen_t len, /* size of hole to find */
  3860. xfs_fileoff_t *first_unused, /* unused block */
  3861. int whichfork) /* data or attr fork */
  3862. {
  3863. int error; /* error return value */
  3864. int idx; /* extent record index */
  3865. xfs_ifork_t *ifp; /* inode fork pointer */
  3866. xfs_fileoff_t lastaddr; /* last block number seen */
  3867. xfs_fileoff_t lowest; /* lowest useful block */
  3868. xfs_fileoff_t max; /* starting useful block */
  3869. xfs_fileoff_t off; /* offset for this block */
  3870. xfs_extnum_t nextents; /* number of extent entries */
  3871. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  3872. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  3873. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3874. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3875. *first_unused = 0;
  3876. return 0;
  3877. }
  3878. ifp = XFS_IFORK_PTR(ip, whichfork);
  3879. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3880. (error = xfs_iread_extents(tp, ip, whichfork)))
  3881. return error;
  3882. lowest = *first_unused;
  3883. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3884. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  3885. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  3886. off = xfs_bmbt_get_startoff(ep);
  3887. /*
  3888. * See if the hole before this extent will work.
  3889. */
  3890. if (off >= lowest + len && off - max >= len) {
  3891. *first_unused = max;
  3892. return 0;
  3893. }
  3894. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  3895. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  3896. }
  3897. *first_unused = max;
  3898. return 0;
  3899. }
  3900. /*
  3901. * Returns the file-relative block number of the last block + 1 before
  3902. * last_block (input value) in the file.
  3903. * This is not based on i_size, it is based on the extent records.
  3904. * Returns 0 for local files, as they do not have extent records.
  3905. */
  3906. int /* error */
  3907. xfs_bmap_last_before(
  3908. xfs_trans_t *tp, /* transaction pointer */
  3909. xfs_inode_t *ip, /* incore inode */
  3910. xfs_fileoff_t *last_block, /* last block */
  3911. int whichfork) /* data or attr fork */
  3912. {
  3913. xfs_fileoff_t bno; /* input file offset */
  3914. int eof; /* hit end of file */
  3915. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  3916. int error; /* error return value */
  3917. xfs_bmbt_irec_t got; /* current extent value */
  3918. xfs_ifork_t *ifp; /* inode fork pointer */
  3919. xfs_extnum_t lastx; /* last extent used */
  3920. xfs_bmbt_irec_t prev; /* previous extent value */
  3921. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3922. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3923. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  3924. return XFS_ERROR(EIO);
  3925. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3926. *last_block = 0;
  3927. return 0;
  3928. }
  3929. ifp = XFS_IFORK_PTR(ip, whichfork);
  3930. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3931. (error = xfs_iread_extents(tp, ip, whichfork)))
  3932. return error;
  3933. bno = *last_block - 1;
  3934. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  3935. &prev);
  3936. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  3937. if (prev.br_startoff == NULLFILEOFF)
  3938. *last_block = 0;
  3939. else
  3940. *last_block = prev.br_startoff + prev.br_blockcount;
  3941. }
  3942. /*
  3943. * Otherwise *last_block is already the right answer.
  3944. */
  3945. return 0;
  3946. }
  3947. /*
  3948. * Returns the file-relative block number of the first block past eof in
  3949. * the file. This is not based on i_size, it is based on the extent records.
  3950. * Returns 0 for local files, as they do not have extent records.
  3951. */
  3952. int /* error */
  3953. xfs_bmap_last_offset(
  3954. xfs_trans_t *tp, /* transaction pointer */
  3955. xfs_inode_t *ip, /* incore inode */
  3956. xfs_fileoff_t *last_block, /* last block */
  3957. int whichfork) /* data or attr fork */
  3958. {
  3959. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  3960. int error; /* error return value */
  3961. xfs_ifork_t *ifp; /* inode fork pointer */
  3962. xfs_extnum_t nextents; /* number of extent entries */
  3963. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3964. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3965. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  3966. return XFS_ERROR(EIO);
  3967. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3968. *last_block = 0;
  3969. return 0;
  3970. }
  3971. ifp = XFS_IFORK_PTR(ip, whichfork);
  3972. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3973. (error = xfs_iread_extents(tp, ip, whichfork)))
  3974. return error;
  3975. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3976. if (!nextents) {
  3977. *last_block = 0;
  3978. return 0;
  3979. }
  3980. ep = xfs_iext_get_ext(ifp, nextents - 1);
  3981. *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
  3982. return 0;
  3983. }
  3984. /*
  3985. * Returns whether the selected fork of the inode has exactly one
  3986. * block or not. For the data fork we check this matches di_size,
  3987. * implying the file's range is 0..bsize-1.
  3988. */
  3989. int /* 1=>1 block, 0=>otherwise */
  3990. xfs_bmap_one_block(
  3991. xfs_inode_t *ip, /* incore inode */
  3992. int whichfork) /* data or attr fork */
  3993. {
  3994. xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
  3995. xfs_ifork_t *ifp; /* inode fork pointer */
  3996. int rval; /* return value */
  3997. xfs_bmbt_irec_t s; /* internal version of extent */
  3998. #ifndef DEBUG
  3999. if (whichfork == XFS_DATA_FORK) {
  4000. return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
  4001. (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
  4002. (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
  4003. }
  4004. #endif /* !DEBUG */
  4005. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  4006. return 0;
  4007. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4008. return 0;
  4009. ifp = XFS_IFORK_PTR(ip, whichfork);
  4010. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  4011. ep = xfs_iext_get_ext(ifp, 0);
  4012. xfs_bmbt_get_all(ep, &s);
  4013. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  4014. if (rval && whichfork == XFS_DATA_FORK)
  4015. ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
  4016. return rval;
  4017. }
  4018. STATIC int
  4019. xfs_bmap_sanity_check(
  4020. struct xfs_mount *mp,
  4021. struct xfs_buf *bp,
  4022. int level)
  4023. {
  4024. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  4025. if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
  4026. be16_to_cpu(block->bb_level) != level ||
  4027. be16_to_cpu(block->bb_numrecs) == 0 ||
  4028. be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  4029. return 0;
  4030. return 1;
  4031. }
  4032. /*
  4033. * Read in the extents to if_extents.
  4034. * All inode fields are set up by caller, we just traverse the btree
  4035. * and copy the records in. If the file system cannot contain unwritten
  4036. * extents, the records are checked for no "state" flags.
  4037. */
  4038. int /* error */
  4039. xfs_bmap_read_extents(
  4040. xfs_trans_t *tp, /* transaction pointer */
  4041. xfs_inode_t *ip, /* incore inode */
  4042. int whichfork) /* data or attr fork */
  4043. {
  4044. struct xfs_btree_block *block; /* current btree block */
  4045. xfs_fsblock_t bno; /* block # of "block" */
  4046. xfs_buf_t *bp; /* buffer for "block" */
  4047. int error; /* error return value */
  4048. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  4049. xfs_extnum_t i, j; /* index into the extents list */
  4050. xfs_ifork_t *ifp; /* fork structure */
  4051. int level; /* btree level, for checking */
  4052. xfs_mount_t *mp; /* file system mount structure */
  4053. __be64 *pp; /* pointer to block address */
  4054. /* REFERENCED */
  4055. xfs_extnum_t room; /* number of entries there's room for */
  4056. bno = NULLFSBLOCK;
  4057. mp = ip->i_mount;
  4058. ifp = XFS_IFORK_PTR(ip, whichfork);
  4059. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  4060. XFS_EXTFMT_INODE(ip);
  4061. block = ifp->if_broot;
  4062. /*
  4063. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  4064. */
  4065. level = be16_to_cpu(block->bb_level);
  4066. ASSERT(level > 0);
  4067. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  4068. bno = be64_to_cpu(*pp);
  4069. ASSERT(bno != NULLDFSBNO);
  4070. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  4071. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  4072. /*
  4073. * Go down the tree until leaf level is reached, following the first
  4074. * pointer (leftmost) at each level.
  4075. */
  4076. while (level-- > 0) {
  4077. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4078. XFS_BMAP_BTREE_REF)))
  4079. return error;
  4080. block = XFS_BUF_TO_BLOCK(bp);
  4081. XFS_WANT_CORRUPTED_GOTO(
  4082. xfs_bmap_sanity_check(mp, bp, level),
  4083. error0);
  4084. if (level == 0)
  4085. break;
  4086. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  4087. bno = be64_to_cpu(*pp);
  4088. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  4089. xfs_trans_brelse(tp, bp);
  4090. }
  4091. /*
  4092. * Here with bp and block set to the leftmost leaf node in the tree.
  4093. */
  4094. room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4095. i = 0;
  4096. /*
  4097. * Loop over all leaf nodes. Copy information to the extent records.
  4098. */
  4099. for (;;) {
  4100. xfs_bmbt_rec_t *frp;
  4101. xfs_fsblock_t nextbno;
  4102. xfs_extnum_t num_recs;
  4103. xfs_extnum_t start;
  4104. num_recs = xfs_btree_get_numrecs(block);
  4105. if (unlikely(i + num_recs > room)) {
  4106. ASSERT(i + num_recs <= room);
  4107. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  4108. "corrupt dinode %Lu, (btree extents).",
  4109. (unsigned long long) ip->i_ino);
  4110. XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
  4111. XFS_ERRLEVEL_LOW,
  4112. ip->i_mount);
  4113. goto error0;
  4114. }
  4115. XFS_WANT_CORRUPTED_GOTO(
  4116. xfs_bmap_sanity_check(mp, bp, 0),
  4117. error0);
  4118. /*
  4119. * Read-ahead the next leaf block, if any.
  4120. */
  4121. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  4122. if (nextbno != NULLFSBLOCK)
  4123. xfs_btree_reada_bufl(mp, nextbno, 1);
  4124. /*
  4125. * Copy records into the extent records.
  4126. */
  4127. frp = XFS_BMBT_REC_ADDR(mp, block, 1);
  4128. start = i;
  4129. for (j = 0; j < num_recs; j++, i++, frp++) {
  4130. xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
  4131. trp->l0 = be64_to_cpu(frp->l0);
  4132. trp->l1 = be64_to_cpu(frp->l1);
  4133. }
  4134. if (exntf == XFS_EXTFMT_NOSTATE) {
  4135. /*
  4136. * Check all attribute bmap btree records and
  4137. * any "older" data bmap btree records for a
  4138. * set bit in the "extent flag" position.
  4139. */
  4140. if (unlikely(xfs_check_nostate_extents(ifp,
  4141. start, num_recs))) {
  4142. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  4143. XFS_ERRLEVEL_LOW,
  4144. ip->i_mount);
  4145. goto error0;
  4146. }
  4147. }
  4148. xfs_trans_brelse(tp, bp);
  4149. bno = nextbno;
  4150. /*
  4151. * If we've reached the end, stop.
  4152. */
  4153. if (bno == NULLFSBLOCK)
  4154. break;
  4155. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4156. XFS_BMAP_BTREE_REF)))
  4157. return error;
  4158. block = XFS_BUF_TO_BLOCK(bp);
  4159. }
  4160. ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4161. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  4162. XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
  4163. return 0;
  4164. error0:
  4165. xfs_trans_brelse(tp, bp);
  4166. return XFS_ERROR(EFSCORRUPTED);
  4167. }
  4168. #ifdef DEBUG
  4169. /*
  4170. * Add bmap trace insert entries for all the contents of the extent records.
  4171. */
  4172. void
  4173. xfs_bmap_trace_exlist(
  4174. xfs_inode_t *ip, /* incore inode pointer */
  4175. xfs_extnum_t cnt, /* count of entries in the list */
  4176. int whichfork, /* data or attr fork */
  4177. unsigned long caller_ip)
  4178. {
  4179. xfs_extnum_t idx; /* extent record index */
  4180. xfs_ifork_t *ifp; /* inode fork pointer */
  4181. int state = 0;
  4182. if (whichfork == XFS_ATTR_FORK)
  4183. state |= BMAP_ATTRFORK;
  4184. ifp = XFS_IFORK_PTR(ip, whichfork);
  4185. ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4186. for (idx = 0; idx < cnt; idx++)
  4187. trace_xfs_extlist(ip, idx, whichfork, caller_ip);
  4188. }
  4189. /*
  4190. * Validate that the bmbt_irecs being returned from bmapi are valid
  4191. * given the callers original parameters. Specifically check the
  4192. * ranges of the returned irecs to ensure that they only extent beyond
  4193. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  4194. */
  4195. STATIC void
  4196. xfs_bmap_validate_ret(
  4197. xfs_fileoff_t bno,
  4198. xfs_filblks_t len,
  4199. int flags,
  4200. xfs_bmbt_irec_t *mval,
  4201. int nmap,
  4202. int ret_nmap)
  4203. {
  4204. int i; /* index to map values */
  4205. ASSERT(ret_nmap <= nmap);
  4206. for (i = 0; i < ret_nmap; i++) {
  4207. ASSERT(mval[i].br_blockcount > 0);
  4208. if (!(flags & XFS_BMAPI_ENTIRE)) {
  4209. ASSERT(mval[i].br_startoff >= bno);
  4210. ASSERT(mval[i].br_blockcount <= len);
  4211. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  4212. bno + len);
  4213. } else {
  4214. ASSERT(mval[i].br_startoff < bno + len);
  4215. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  4216. bno);
  4217. }
  4218. ASSERT(i == 0 ||
  4219. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  4220. mval[i].br_startoff);
  4221. if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
  4222. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  4223. mval[i].br_startblock != HOLESTARTBLOCK);
  4224. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  4225. mval[i].br_state == XFS_EXT_UNWRITTEN);
  4226. }
  4227. }
  4228. #endif /* DEBUG */
  4229. /*
  4230. * Map file blocks to filesystem blocks.
  4231. * File range is given by the bno/len pair.
  4232. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  4233. * into a hole or past eof.
  4234. * Only allocates blocks from a single allocation group,
  4235. * to avoid locking problems.
  4236. * The returned value in "firstblock" from the first call in a transaction
  4237. * must be remembered and presented to subsequent calls in "firstblock".
  4238. * An upper bound for the number of blocks to be allocated is supplied to
  4239. * the first call in "total"; if no allocation group has that many free
  4240. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4241. */
  4242. int /* error */
  4243. xfs_bmapi(
  4244. xfs_trans_t *tp, /* transaction pointer */
  4245. xfs_inode_t *ip, /* incore inode */
  4246. xfs_fileoff_t bno, /* starting file offs. mapped */
  4247. xfs_filblks_t len, /* length to map in file */
  4248. int flags, /* XFS_BMAPI_... */
  4249. xfs_fsblock_t *firstblock, /* first allocated block
  4250. controls a.g. for allocs */
  4251. xfs_extlen_t total, /* total blocks needed */
  4252. xfs_bmbt_irec_t *mval, /* output: map values */
  4253. int *nmap, /* i/o: mval size/count */
  4254. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4255. xfs_extdelta_t *delta) /* o: change made to incore extents */
  4256. {
  4257. xfs_fsblock_t abno; /* allocated block number */
  4258. xfs_extlen_t alen; /* allocated extent length */
  4259. xfs_fileoff_t aoff; /* allocated file offset */
  4260. xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
  4261. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4262. xfs_fileoff_t end; /* end of mapped file region */
  4263. int eof; /* we've hit the end of extents */
  4264. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4265. int error; /* error return */
  4266. xfs_bmbt_irec_t got; /* current file extent record */
  4267. xfs_ifork_t *ifp; /* inode fork pointer */
  4268. xfs_extlen_t indlen; /* indirect blocks length */
  4269. xfs_extnum_t lastx; /* last useful extent number */
  4270. int logflags; /* flags for transaction logging */
  4271. xfs_extlen_t minleft; /* min blocks left after allocation */
  4272. xfs_extlen_t minlen; /* min allocation size */
  4273. xfs_mount_t *mp; /* xfs mount structure */
  4274. int n; /* current extent index */
  4275. int nallocs; /* number of extents alloc'd */
  4276. xfs_extnum_t nextents; /* number of extents in file */
  4277. xfs_fileoff_t obno; /* old block number (offset) */
  4278. xfs_bmbt_irec_t prev; /* previous file extent record */
  4279. int tmp_logflags; /* temp flags holder */
  4280. int whichfork; /* data or attr fork */
  4281. char inhole; /* current location is hole in file */
  4282. char wasdelay; /* old extent was delayed */
  4283. char wr; /* this is a write request */
  4284. char rt; /* this is a realtime file */
  4285. #ifdef DEBUG
  4286. xfs_fileoff_t orig_bno; /* original block number value */
  4287. int orig_flags; /* original flags arg value */
  4288. xfs_filblks_t orig_len; /* original value of len arg */
  4289. xfs_bmbt_irec_t *orig_mval; /* original value of mval */
  4290. int orig_nmap; /* original value of *nmap */
  4291. orig_bno = bno;
  4292. orig_len = len;
  4293. orig_flags = flags;
  4294. orig_mval = mval;
  4295. orig_nmap = *nmap;
  4296. #endif
  4297. ASSERT(*nmap >= 1);
  4298. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
  4299. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4300. XFS_ATTR_FORK : XFS_DATA_FORK;
  4301. mp = ip->i_mount;
  4302. if (unlikely(XFS_TEST_ERROR(
  4303. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4304. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4305. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4306. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4307. XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
  4308. return XFS_ERROR(EFSCORRUPTED);
  4309. }
  4310. if (XFS_FORCED_SHUTDOWN(mp))
  4311. return XFS_ERROR(EIO);
  4312. rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4313. ifp = XFS_IFORK_PTR(ip, whichfork);
  4314. ASSERT(ifp->if_ext_max ==
  4315. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4316. if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
  4317. XFS_STATS_INC(xs_blk_mapw);
  4318. else
  4319. XFS_STATS_INC(xs_blk_mapr);
  4320. /*
  4321. * IGSTATE flag is used to combine extents which
  4322. * differ only due to the state of the extents.
  4323. * This technique is used from xfs_getbmap()
  4324. * when the caller does not wish to see the
  4325. * separation (which is the default).
  4326. *
  4327. * This technique is also used when writing a
  4328. * buffer which has been partially written,
  4329. * (usually by being flushed during a chunkread),
  4330. * to ensure one write takes place. This also
  4331. * prevents a change in the xfs inode extents at
  4332. * this time, intentionally. This change occurs
  4333. * on completion of the write operation, in
  4334. * xfs_strat_comp(), where the xfs_bmapi() call
  4335. * is transactioned, and the extents combined.
  4336. */
  4337. if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
  4338. wr = 0; /* no allocations are allowed */
  4339. ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
  4340. logflags = 0;
  4341. nallocs = 0;
  4342. cur = NULL;
  4343. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4344. ASSERT(wr && tp);
  4345. if ((error = xfs_bmap_local_to_extents(tp, ip,
  4346. firstblock, total, &logflags, whichfork)))
  4347. goto error0;
  4348. }
  4349. if (wr && *firstblock == NULLFSBLOCK) {
  4350. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4351. minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4352. else
  4353. minleft = 1;
  4354. } else
  4355. minleft = 0;
  4356. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4357. (error = xfs_iread_extents(tp, ip, whichfork)))
  4358. goto error0;
  4359. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4360. &prev);
  4361. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4362. n = 0;
  4363. end = bno + len;
  4364. obno = bno;
  4365. bma.ip = NULL;
  4366. if (delta) {
  4367. delta->xed_startoff = NULLFILEOFF;
  4368. delta->xed_blockcount = 0;
  4369. }
  4370. while (bno < end && n < *nmap) {
  4371. /*
  4372. * Reading past eof, act as though there's a hole
  4373. * up to end.
  4374. */
  4375. if (eof && !wr)
  4376. got.br_startoff = end;
  4377. inhole = eof || got.br_startoff > bno;
  4378. wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
  4379. isnullstartblock(got.br_startblock);
  4380. /*
  4381. * First, deal with the hole before the allocated space
  4382. * that we found, if any.
  4383. */
  4384. if (wr && (inhole || wasdelay)) {
  4385. /*
  4386. * For the wasdelay case, we could also just
  4387. * allocate the stuff asked for in this bmap call
  4388. * but that wouldn't be as good.
  4389. */
  4390. if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
  4391. alen = (xfs_extlen_t)got.br_blockcount;
  4392. aoff = got.br_startoff;
  4393. if (lastx != NULLEXTNUM && lastx) {
  4394. ep = xfs_iext_get_ext(ifp, lastx - 1);
  4395. xfs_bmbt_get_all(ep, &prev);
  4396. }
  4397. } else if (wasdelay) {
  4398. alen = (xfs_extlen_t)
  4399. XFS_FILBLKS_MIN(len,
  4400. (got.br_startoff +
  4401. got.br_blockcount) - bno);
  4402. aoff = bno;
  4403. } else {
  4404. alen = (xfs_extlen_t)
  4405. XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4406. if (!eof)
  4407. alen = (xfs_extlen_t)
  4408. XFS_FILBLKS_MIN(alen,
  4409. got.br_startoff - bno);
  4410. aoff = bno;
  4411. }
  4412. minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
  4413. if (flags & XFS_BMAPI_DELAY) {
  4414. xfs_extlen_t extsz;
  4415. /* Figure out the extent size, adjust alen */
  4416. extsz = xfs_get_extsz_hint(ip);
  4417. if (extsz) {
  4418. error = xfs_bmap_extsize_align(mp,
  4419. &got, &prev, extsz,
  4420. rt, eof,
  4421. flags&XFS_BMAPI_DELAY,
  4422. flags&XFS_BMAPI_CONVERT,
  4423. &aoff, &alen);
  4424. ASSERT(!error);
  4425. }
  4426. if (rt)
  4427. extsz = alen / mp->m_sb.sb_rextsize;
  4428. /*
  4429. * Make a transaction-less quota reservation for
  4430. * delayed allocation blocks. This number gets
  4431. * adjusted later. We return if we haven't
  4432. * allocated blocks already inside this loop.
  4433. */
  4434. error = xfs_trans_reserve_quota_nblks(
  4435. NULL, ip, (long)alen, 0,
  4436. rt ? XFS_QMOPT_RES_RTBLKS :
  4437. XFS_QMOPT_RES_REGBLKS);
  4438. if (error) {
  4439. if (n == 0) {
  4440. *nmap = 0;
  4441. ASSERT(cur == NULL);
  4442. return error;
  4443. }
  4444. break;
  4445. }
  4446. /*
  4447. * Split changing sb for alen and indlen since
  4448. * they could be coming from different places.
  4449. */
  4450. indlen = (xfs_extlen_t)
  4451. xfs_bmap_worst_indlen(ip, alen);
  4452. ASSERT(indlen > 0);
  4453. if (rt) {
  4454. error = xfs_mod_incore_sb(mp,
  4455. XFS_SBS_FREXTENTS,
  4456. -((int64_t)extsz), (flags &
  4457. XFS_BMAPI_RSVBLOCKS));
  4458. } else {
  4459. error = xfs_mod_incore_sb(mp,
  4460. XFS_SBS_FDBLOCKS,
  4461. -((int64_t)alen), (flags &
  4462. XFS_BMAPI_RSVBLOCKS));
  4463. }
  4464. if (!error) {
  4465. error = xfs_mod_incore_sb(mp,
  4466. XFS_SBS_FDBLOCKS,
  4467. -((int64_t)indlen), (flags &
  4468. XFS_BMAPI_RSVBLOCKS));
  4469. if (error && rt)
  4470. xfs_mod_incore_sb(mp,
  4471. XFS_SBS_FREXTENTS,
  4472. (int64_t)extsz, (flags &
  4473. XFS_BMAPI_RSVBLOCKS));
  4474. else if (error)
  4475. xfs_mod_incore_sb(mp,
  4476. XFS_SBS_FDBLOCKS,
  4477. (int64_t)alen, (flags &
  4478. XFS_BMAPI_RSVBLOCKS));
  4479. }
  4480. if (error) {
  4481. if (XFS_IS_QUOTA_ON(mp))
  4482. /* unreserve the blocks now */
  4483. (void)
  4484. xfs_trans_unreserve_quota_nblks(
  4485. NULL, ip,
  4486. (long)alen, 0, rt ?
  4487. XFS_QMOPT_RES_RTBLKS :
  4488. XFS_QMOPT_RES_REGBLKS);
  4489. break;
  4490. }
  4491. ip->i_delayed_blks += alen;
  4492. abno = nullstartblock(indlen);
  4493. } else {
  4494. /*
  4495. * If first time, allocate and fill in
  4496. * once-only bma fields.
  4497. */
  4498. if (bma.ip == NULL) {
  4499. bma.tp = tp;
  4500. bma.ip = ip;
  4501. bma.prevp = &prev;
  4502. bma.gotp = &got;
  4503. bma.total = total;
  4504. bma.userdata = 0;
  4505. }
  4506. /* Indicate if this is the first user data
  4507. * in the file, or just any user data.
  4508. */
  4509. if (!(flags & XFS_BMAPI_METADATA)) {
  4510. bma.userdata = (aoff == 0) ?
  4511. XFS_ALLOC_INITIAL_USER_DATA :
  4512. XFS_ALLOC_USERDATA;
  4513. }
  4514. /*
  4515. * Fill in changeable bma fields.
  4516. */
  4517. bma.eof = eof;
  4518. bma.firstblock = *firstblock;
  4519. bma.alen = alen;
  4520. bma.off = aoff;
  4521. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4522. bma.wasdel = wasdelay;
  4523. bma.minlen = minlen;
  4524. bma.low = flist->xbf_low;
  4525. bma.minleft = minleft;
  4526. /*
  4527. * Only want to do the alignment at the
  4528. * eof if it is userdata and allocation length
  4529. * is larger than a stripe unit.
  4530. */
  4531. if (mp->m_dalign && alen >= mp->m_dalign &&
  4532. (!(flags & XFS_BMAPI_METADATA)) &&
  4533. (whichfork == XFS_DATA_FORK)) {
  4534. if ((error = xfs_bmap_isaeof(ip, aoff,
  4535. whichfork, &bma.aeof)))
  4536. goto error0;
  4537. } else
  4538. bma.aeof = 0;
  4539. /*
  4540. * Call allocator.
  4541. */
  4542. if ((error = xfs_bmap_alloc(&bma)))
  4543. goto error0;
  4544. /*
  4545. * Copy out result fields.
  4546. */
  4547. abno = bma.rval;
  4548. if ((flist->xbf_low = bma.low))
  4549. minleft = 0;
  4550. alen = bma.alen;
  4551. aoff = bma.off;
  4552. ASSERT(*firstblock == NULLFSBLOCK ||
  4553. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4554. XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
  4555. (flist->xbf_low &&
  4556. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4557. XFS_FSB_TO_AGNO(mp, bma.firstblock)));
  4558. *firstblock = bma.firstblock;
  4559. if (cur)
  4560. cur->bc_private.b.firstblock =
  4561. *firstblock;
  4562. if (abno == NULLFSBLOCK)
  4563. break;
  4564. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4565. cur = xfs_bmbt_init_cursor(mp, tp,
  4566. ip, whichfork);
  4567. cur->bc_private.b.firstblock =
  4568. *firstblock;
  4569. cur->bc_private.b.flist = flist;
  4570. }
  4571. /*
  4572. * Bump the number of extents we've allocated
  4573. * in this call.
  4574. */
  4575. nallocs++;
  4576. }
  4577. if (cur)
  4578. cur->bc_private.b.flags =
  4579. wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
  4580. got.br_startoff = aoff;
  4581. got.br_startblock = abno;
  4582. got.br_blockcount = alen;
  4583. got.br_state = XFS_EXT_NORM; /* assume normal */
  4584. /*
  4585. * Determine state of extent, and the filesystem.
  4586. * A wasdelay extent has been initialized, so
  4587. * shouldn't be flagged as unwritten.
  4588. */
  4589. if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4590. if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
  4591. got.br_state = XFS_EXT_UNWRITTEN;
  4592. }
  4593. error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
  4594. firstblock, flist, &tmp_logflags, delta,
  4595. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4596. logflags |= tmp_logflags;
  4597. if (error)
  4598. goto error0;
  4599. lastx = ifp->if_lastex;
  4600. ep = xfs_iext_get_ext(ifp, lastx);
  4601. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4602. xfs_bmbt_get_all(ep, &got);
  4603. ASSERT(got.br_startoff <= aoff);
  4604. ASSERT(got.br_startoff + got.br_blockcount >=
  4605. aoff + alen);
  4606. #ifdef DEBUG
  4607. if (flags & XFS_BMAPI_DELAY) {
  4608. ASSERT(isnullstartblock(got.br_startblock));
  4609. ASSERT(startblockval(got.br_startblock) > 0);
  4610. }
  4611. ASSERT(got.br_state == XFS_EXT_NORM ||
  4612. got.br_state == XFS_EXT_UNWRITTEN);
  4613. #endif
  4614. /*
  4615. * Fall down into the found allocated space case.
  4616. */
  4617. } else if (inhole) {
  4618. /*
  4619. * Reading in a hole.
  4620. */
  4621. mval->br_startoff = bno;
  4622. mval->br_startblock = HOLESTARTBLOCK;
  4623. mval->br_blockcount =
  4624. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4625. mval->br_state = XFS_EXT_NORM;
  4626. bno += mval->br_blockcount;
  4627. len -= mval->br_blockcount;
  4628. mval++;
  4629. n++;
  4630. continue;
  4631. }
  4632. /*
  4633. * Then deal with the allocated space we found.
  4634. */
  4635. ASSERT(ep != NULL);
  4636. if (!(flags & XFS_BMAPI_ENTIRE) &&
  4637. (got.br_startoff + got.br_blockcount > obno)) {
  4638. if (obno > bno)
  4639. bno = obno;
  4640. ASSERT((bno >= obno) || (n == 0));
  4641. ASSERT(bno < end);
  4642. mval->br_startoff = bno;
  4643. if (isnullstartblock(got.br_startblock)) {
  4644. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4645. mval->br_startblock = DELAYSTARTBLOCK;
  4646. } else
  4647. mval->br_startblock =
  4648. got.br_startblock +
  4649. (bno - got.br_startoff);
  4650. /*
  4651. * Return the minimum of what we got and what we
  4652. * asked for for the length. We can use the len
  4653. * variable here because it is modified below
  4654. * and we could have been there before coming
  4655. * here if the first part of the allocation
  4656. * didn't overlap what was asked for.
  4657. */
  4658. mval->br_blockcount =
  4659. XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
  4660. (bno - got.br_startoff));
  4661. mval->br_state = got.br_state;
  4662. ASSERT(mval->br_blockcount <= len);
  4663. } else {
  4664. *mval = got;
  4665. if (isnullstartblock(mval->br_startblock)) {
  4666. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4667. mval->br_startblock = DELAYSTARTBLOCK;
  4668. }
  4669. }
  4670. /*
  4671. * Check if writing previously allocated but
  4672. * unwritten extents.
  4673. */
  4674. if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
  4675. ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
  4676. /*
  4677. * Modify (by adding) the state flag, if writing.
  4678. */
  4679. ASSERT(mval->br_blockcount <= len);
  4680. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4681. cur = xfs_bmbt_init_cursor(mp,
  4682. tp, ip, whichfork);
  4683. cur->bc_private.b.firstblock =
  4684. *firstblock;
  4685. cur->bc_private.b.flist = flist;
  4686. }
  4687. mval->br_state = XFS_EXT_NORM;
  4688. error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
  4689. firstblock, flist, &tmp_logflags, delta,
  4690. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4691. logflags |= tmp_logflags;
  4692. if (error)
  4693. goto error0;
  4694. lastx = ifp->if_lastex;
  4695. ep = xfs_iext_get_ext(ifp, lastx);
  4696. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4697. xfs_bmbt_get_all(ep, &got);
  4698. /*
  4699. * We may have combined previously unwritten
  4700. * space with written space, so generate
  4701. * another request.
  4702. */
  4703. if (mval->br_blockcount < len)
  4704. continue;
  4705. }
  4706. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4707. ((mval->br_startoff + mval->br_blockcount) <= end));
  4708. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4709. (mval->br_blockcount <= len) ||
  4710. (mval->br_startoff < obno));
  4711. bno = mval->br_startoff + mval->br_blockcount;
  4712. len = end - bno;
  4713. if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  4714. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  4715. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  4716. ASSERT(mval->br_state == mval[-1].br_state);
  4717. mval[-1].br_blockcount = mval->br_blockcount;
  4718. mval[-1].br_state = mval->br_state;
  4719. } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  4720. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  4721. mval[-1].br_startblock != HOLESTARTBLOCK &&
  4722. mval->br_startblock ==
  4723. mval[-1].br_startblock + mval[-1].br_blockcount &&
  4724. ((flags & XFS_BMAPI_IGSTATE) ||
  4725. mval[-1].br_state == mval->br_state)) {
  4726. ASSERT(mval->br_startoff ==
  4727. mval[-1].br_startoff + mval[-1].br_blockcount);
  4728. mval[-1].br_blockcount += mval->br_blockcount;
  4729. } else if (n > 0 &&
  4730. mval->br_startblock == DELAYSTARTBLOCK &&
  4731. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  4732. mval->br_startoff ==
  4733. mval[-1].br_startoff + mval[-1].br_blockcount) {
  4734. mval[-1].br_blockcount += mval->br_blockcount;
  4735. mval[-1].br_state = mval->br_state;
  4736. } else if (!((n == 0) &&
  4737. ((mval->br_startoff + mval->br_blockcount) <=
  4738. obno))) {
  4739. mval++;
  4740. n++;
  4741. }
  4742. /*
  4743. * If we're done, stop now. Stop when we've allocated
  4744. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4745. * the transaction may get too big.
  4746. */
  4747. if (bno >= end || n >= *nmap || nallocs >= *nmap)
  4748. break;
  4749. /*
  4750. * Else go on to the next record.
  4751. */
  4752. ep = xfs_iext_get_ext(ifp, ++lastx);
  4753. prev = got;
  4754. if (lastx >= nextents)
  4755. eof = 1;
  4756. else
  4757. xfs_bmbt_get_all(ep, &got);
  4758. }
  4759. ifp->if_lastex = lastx;
  4760. *nmap = n;
  4761. /*
  4762. * Transform from btree to extents, give it cur.
  4763. */
  4764. if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  4765. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  4766. ASSERT(wr && cur);
  4767. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  4768. &tmp_logflags, whichfork);
  4769. logflags |= tmp_logflags;
  4770. if (error)
  4771. goto error0;
  4772. }
  4773. ASSERT(ifp->if_ext_max ==
  4774. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4775. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4776. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
  4777. error = 0;
  4778. if (delta && delta->xed_startoff != NULLFILEOFF) {
  4779. /* A change was actually made.
  4780. * Note that delta->xed_blockount is an offset at this
  4781. * point and needs to be converted to a block count.
  4782. */
  4783. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  4784. delta->xed_blockcount -= delta->xed_startoff;
  4785. }
  4786. error0:
  4787. /*
  4788. * Log everything. Do this after conversion, there's no point in
  4789. * logging the extent records if we've converted to btree format.
  4790. */
  4791. if ((logflags & xfs_ilog_fext(whichfork)) &&
  4792. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4793. logflags &= ~xfs_ilog_fext(whichfork);
  4794. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  4795. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4796. logflags &= ~xfs_ilog_fbroot(whichfork);
  4797. /*
  4798. * Log whatever the flags say, even if error. Otherwise we might miss
  4799. * detecting a case where the data is changed, there's an error,
  4800. * and it's not logged so we don't shutdown when we should.
  4801. */
  4802. if (logflags) {
  4803. ASSERT(tp && wr);
  4804. xfs_trans_log_inode(tp, ip, logflags);
  4805. }
  4806. if (cur) {
  4807. if (!error) {
  4808. ASSERT(*firstblock == NULLFSBLOCK ||
  4809. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4810. XFS_FSB_TO_AGNO(mp,
  4811. cur->bc_private.b.firstblock) ||
  4812. (flist->xbf_low &&
  4813. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4814. XFS_FSB_TO_AGNO(mp,
  4815. cur->bc_private.b.firstblock)));
  4816. *firstblock = cur->bc_private.b.firstblock;
  4817. }
  4818. xfs_btree_del_cursor(cur,
  4819. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4820. }
  4821. if (!error)
  4822. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4823. orig_nmap, *nmap);
  4824. return error;
  4825. }
  4826. /*
  4827. * Map file blocks to filesystem blocks, simple version.
  4828. * One block (extent) only, read-only.
  4829. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  4830. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  4831. * was set and all the others were clear.
  4832. */
  4833. int /* error */
  4834. xfs_bmapi_single(
  4835. xfs_trans_t *tp, /* transaction pointer */
  4836. xfs_inode_t *ip, /* incore inode */
  4837. int whichfork, /* data or attr fork */
  4838. xfs_fsblock_t *fsb, /* output: mapped block */
  4839. xfs_fileoff_t bno) /* starting file offs. mapped */
  4840. {
  4841. int eof; /* we've hit the end of extents */
  4842. int error; /* error return */
  4843. xfs_bmbt_irec_t got; /* current file extent record */
  4844. xfs_ifork_t *ifp; /* inode fork pointer */
  4845. xfs_extnum_t lastx; /* last useful extent number */
  4846. xfs_bmbt_irec_t prev; /* previous file extent record */
  4847. ifp = XFS_IFORK_PTR(ip, whichfork);
  4848. if (unlikely(
  4849. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4850. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
  4851. XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
  4852. ip->i_mount);
  4853. return XFS_ERROR(EFSCORRUPTED);
  4854. }
  4855. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  4856. return XFS_ERROR(EIO);
  4857. XFS_STATS_INC(xs_blk_mapr);
  4858. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4859. (error = xfs_iread_extents(tp, ip, whichfork)))
  4860. return error;
  4861. (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4862. &prev);
  4863. /*
  4864. * Reading past eof, act as though there's a hole
  4865. * up to end.
  4866. */
  4867. if (eof || got.br_startoff > bno) {
  4868. *fsb = NULLFSBLOCK;
  4869. return 0;
  4870. }
  4871. ASSERT(!isnullstartblock(got.br_startblock));
  4872. ASSERT(bno < got.br_startoff + got.br_blockcount);
  4873. *fsb = got.br_startblock + (bno - got.br_startoff);
  4874. ifp->if_lastex = lastx;
  4875. return 0;
  4876. }
  4877. /*
  4878. * Unmap (remove) blocks from a file.
  4879. * If nexts is nonzero then the number of extents to remove is limited to
  4880. * that value. If not all extents in the block range can be removed then
  4881. * *done is set.
  4882. */
  4883. int /* error */
  4884. xfs_bunmapi(
  4885. xfs_trans_t *tp, /* transaction pointer */
  4886. struct xfs_inode *ip, /* incore inode */
  4887. xfs_fileoff_t bno, /* starting offset to unmap */
  4888. xfs_filblks_t len, /* length to unmap in file */
  4889. int flags, /* misc flags */
  4890. xfs_extnum_t nexts, /* number of extents max */
  4891. xfs_fsblock_t *firstblock, /* first allocated block
  4892. controls a.g. for allocs */
  4893. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4894. xfs_extdelta_t *delta, /* o: change made to incore
  4895. extents */
  4896. int *done) /* set if not done yet */
  4897. {
  4898. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4899. xfs_bmbt_irec_t del; /* extent being deleted */
  4900. int eof; /* is deleting at eof */
  4901. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4902. int error; /* error return value */
  4903. xfs_extnum_t extno; /* extent number in list */
  4904. xfs_bmbt_irec_t got; /* current extent record */
  4905. xfs_ifork_t *ifp; /* inode fork pointer */
  4906. int isrt; /* freeing in rt area */
  4907. xfs_extnum_t lastx; /* last extent index used */
  4908. int logflags; /* transaction logging flags */
  4909. xfs_extlen_t mod; /* rt extent offset */
  4910. xfs_mount_t *mp; /* mount structure */
  4911. xfs_extnum_t nextents; /* number of file extents */
  4912. xfs_bmbt_irec_t prev; /* previous extent record */
  4913. xfs_fileoff_t start; /* first file offset deleted */
  4914. int tmp_logflags; /* partial logging flags */
  4915. int wasdel; /* was a delayed alloc extent */
  4916. int whichfork; /* data or attribute fork */
  4917. int rsvd; /* OK to allocate reserved blocks */
  4918. xfs_fsblock_t sum;
  4919. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4920. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4921. XFS_ATTR_FORK : XFS_DATA_FORK;
  4922. ifp = XFS_IFORK_PTR(ip, whichfork);
  4923. if (unlikely(
  4924. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4925. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4926. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4927. ip->i_mount);
  4928. return XFS_ERROR(EFSCORRUPTED);
  4929. }
  4930. mp = ip->i_mount;
  4931. if (XFS_FORCED_SHUTDOWN(mp))
  4932. return XFS_ERROR(EIO);
  4933. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  4934. ASSERT(len > 0);
  4935. ASSERT(nexts >= 0);
  4936. ASSERT(ifp->if_ext_max ==
  4937. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4938. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4939. (error = xfs_iread_extents(tp, ip, whichfork)))
  4940. return error;
  4941. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4942. if (nextents == 0) {
  4943. *done = 1;
  4944. return 0;
  4945. }
  4946. XFS_STATS_INC(xs_blk_unmap);
  4947. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4948. start = bno;
  4949. bno = start + len - 1;
  4950. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4951. &prev);
  4952. if (delta) {
  4953. delta->xed_startoff = NULLFILEOFF;
  4954. delta->xed_blockcount = 0;
  4955. }
  4956. /*
  4957. * Check to see if the given block number is past the end of the
  4958. * file, back up to the last block if so...
  4959. */
  4960. if (eof) {
  4961. ep = xfs_iext_get_ext(ifp, --lastx);
  4962. xfs_bmbt_get_all(ep, &got);
  4963. bno = got.br_startoff + got.br_blockcount - 1;
  4964. }
  4965. logflags = 0;
  4966. if (ifp->if_flags & XFS_IFBROOT) {
  4967. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  4968. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  4969. cur->bc_private.b.firstblock = *firstblock;
  4970. cur->bc_private.b.flist = flist;
  4971. cur->bc_private.b.flags = 0;
  4972. } else
  4973. cur = NULL;
  4974. extno = 0;
  4975. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  4976. (nexts == 0 || extno < nexts)) {
  4977. /*
  4978. * Is the found extent after a hole in which bno lives?
  4979. * Just back up to the previous extent, if so.
  4980. */
  4981. if (got.br_startoff > bno) {
  4982. if (--lastx < 0)
  4983. break;
  4984. ep = xfs_iext_get_ext(ifp, lastx);
  4985. xfs_bmbt_get_all(ep, &got);
  4986. }
  4987. /*
  4988. * Is the last block of this extent before the range
  4989. * we're supposed to delete? If so, we're done.
  4990. */
  4991. bno = XFS_FILEOFF_MIN(bno,
  4992. got.br_startoff + got.br_blockcount - 1);
  4993. if (bno < start)
  4994. break;
  4995. /*
  4996. * Then deal with the (possibly delayed) allocated space
  4997. * we found.
  4998. */
  4999. ASSERT(ep != NULL);
  5000. del = got;
  5001. wasdel = isnullstartblock(del.br_startblock);
  5002. if (got.br_startoff < start) {
  5003. del.br_startoff = start;
  5004. del.br_blockcount -= start - got.br_startoff;
  5005. if (!wasdel)
  5006. del.br_startblock += start - got.br_startoff;
  5007. }
  5008. if (del.br_startoff + del.br_blockcount > bno + 1)
  5009. del.br_blockcount = bno + 1 - del.br_startoff;
  5010. sum = del.br_startblock + del.br_blockcount;
  5011. if (isrt &&
  5012. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5013. /*
  5014. * Realtime extent not lined up at the end.
  5015. * The extent could have been split into written
  5016. * and unwritten pieces, or we could just be
  5017. * unmapping part of it. But we can't really
  5018. * get rid of part of a realtime extent.
  5019. */
  5020. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5021. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5022. /*
  5023. * This piece is unwritten, or we're not
  5024. * using unwritten extents. Skip over it.
  5025. */
  5026. ASSERT(bno >= mod);
  5027. bno -= mod > del.br_blockcount ?
  5028. del.br_blockcount : mod;
  5029. if (bno < got.br_startoff) {
  5030. if (--lastx >= 0)
  5031. xfs_bmbt_get_all(xfs_iext_get_ext(
  5032. ifp, lastx), &got);
  5033. }
  5034. continue;
  5035. }
  5036. /*
  5037. * It's written, turn it unwritten.
  5038. * This is better than zeroing it.
  5039. */
  5040. ASSERT(del.br_state == XFS_EXT_NORM);
  5041. ASSERT(xfs_trans_get_block_res(tp) > 0);
  5042. /*
  5043. * If this spans a realtime extent boundary,
  5044. * chop it back to the start of the one we end at.
  5045. */
  5046. if (del.br_blockcount > mod) {
  5047. del.br_startoff += del.br_blockcount - mod;
  5048. del.br_startblock += del.br_blockcount - mod;
  5049. del.br_blockcount = mod;
  5050. }
  5051. del.br_state = XFS_EXT_UNWRITTEN;
  5052. error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
  5053. firstblock, flist, &logflags, delta,
  5054. XFS_DATA_FORK, 0);
  5055. if (error)
  5056. goto error0;
  5057. goto nodelete;
  5058. }
  5059. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5060. /*
  5061. * Realtime extent is lined up at the end but not
  5062. * at the front. We'll get rid of full extents if
  5063. * we can.
  5064. */
  5065. mod = mp->m_sb.sb_rextsize - mod;
  5066. if (del.br_blockcount > mod) {
  5067. del.br_blockcount -= mod;
  5068. del.br_startoff += mod;
  5069. del.br_startblock += mod;
  5070. } else if ((del.br_startoff == start &&
  5071. (del.br_state == XFS_EXT_UNWRITTEN ||
  5072. xfs_trans_get_block_res(tp) == 0)) ||
  5073. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5074. /*
  5075. * Can't make it unwritten. There isn't
  5076. * a full extent here so just skip it.
  5077. */
  5078. ASSERT(bno >= del.br_blockcount);
  5079. bno -= del.br_blockcount;
  5080. if (bno < got.br_startoff) {
  5081. if (--lastx >= 0)
  5082. xfs_bmbt_get_all(--ep, &got);
  5083. }
  5084. continue;
  5085. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5086. /*
  5087. * This one is already unwritten.
  5088. * It must have a written left neighbor.
  5089. * Unwrite the killed part of that one and
  5090. * try again.
  5091. */
  5092. ASSERT(lastx > 0);
  5093. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  5094. lastx - 1), &prev);
  5095. ASSERT(prev.br_state == XFS_EXT_NORM);
  5096. ASSERT(!isnullstartblock(prev.br_startblock));
  5097. ASSERT(del.br_startblock ==
  5098. prev.br_startblock + prev.br_blockcount);
  5099. if (prev.br_startoff < start) {
  5100. mod = start - prev.br_startoff;
  5101. prev.br_blockcount -= mod;
  5102. prev.br_startblock += mod;
  5103. prev.br_startoff = start;
  5104. }
  5105. prev.br_state = XFS_EXT_UNWRITTEN;
  5106. error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
  5107. &prev, firstblock, flist, &logflags,
  5108. delta, XFS_DATA_FORK, 0);
  5109. if (error)
  5110. goto error0;
  5111. goto nodelete;
  5112. } else {
  5113. ASSERT(del.br_state == XFS_EXT_NORM);
  5114. del.br_state = XFS_EXT_UNWRITTEN;
  5115. error = xfs_bmap_add_extent(ip, lastx, &cur,
  5116. &del, firstblock, flist, &logflags,
  5117. delta, XFS_DATA_FORK, 0);
  5118. if (error)
  5119. goto error0;
  5120. goto nodelete;
  5121. }
  5122. }
  5123. if (wasdel) {
  5124. ASSERT(startblockval(del.br_startblock) > 0);
  5125. /* Update realtime/data freespace, unreserve quota */
  5126. if (isrt) {
  5127. xfs_filblks_t rtexts;
  5128. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5129. do_div(rtexts, mp->m_sb.sb_rextsize);
  5130. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5131. (int64_t)rtexts, rsvd);
  5132. (void)xfs_trans_reserve_quota_nblks(NULL,
  5133. ip, -((long)del.br_blockcount), 0,
  5134. XFS_QMOPT_RES_RTBLKS);
  5135. } else {
  5136. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
  5137. (int64_t)del.br_blockcount, rsvd);
  5138. (void)xfs_trans_reserve_quota_nblks(NULL,
  5139. ip, -((long)del.br_blockcount), 0,
  5140. XFS_QMOPT_RES_REGBLKS);
  5141. }
  5142. ip->i_delayed_blks -= del.br_blockcount;
  5143. if (cur)
  5144. cur->bc_private.b.flags |=
  5145. XFS_BTCUR_BPRV_WASDEL;
  5146. } else if (cur)
  5147. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5148. /*
  5149. * If it's the case where the directory code is running
  5150. * with no block reservation, and the deleted block is in
  5151. * the middle of its extent, and the resulting insert
  5152. * of an extent would cause transformation to btree format,
  5153. * then reject it. The calling code will then swap
  5154. * blocks around instead.
  5155. * We have to do this now, rather than waiting for the
  5156. * conversion to btree format, since the transaction
  5157. * will be dirty.
  5158. */
  5159. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5160. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5161. XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
  5162. del.br_startoff > got.br_startoff &&
  5163. del.br_startoff + del.br_blockcount <
  5164. got.br_startoff + got.br_blockcount) {
  5165. error = XFS_ERROR(ENOSPC);
  5166. goto error0;
  5167. }
  5168. error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
  5169. &tmp_logflags, delta, whichfork, rsvd);
  5170. logflags |= tmp_logflags;
  5171. if (error)
  5172. goto error0;
  5173. bno = del.br_startoff - 1;
  5174. nodelete:
  5175. lastx = ifp->if_lastex;
  5176. /*
  5177. * If not done go on to the next (previous) record.
  5178. * Reset ep in case the extents array was re-alloced.
  5179. */
  5180. ep = xfs_iext_get_ext(ifp, lastx);
  5181. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5182. if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
  5183. xfs_bmbt_get_startoff(ep) > bno) {
  5184. if (--lastx >= 0)
  5185. ep = xfs_iext_get_ext(ifp, lastx);
  5186. }
  5187. if (lastx >= 0)
  5188. xfs_bmbt_get_all(ep, &got);
  5189. extno++;
  5190. }
  5191. }
  5192. ifp->if_lastex = lastx;
  5193. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5194. ASSERT(ifp->if_ext_max ==
  5195. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5196. /*
  5197. * Convert to a btree if necessary.
  5198. */
  5199. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5200. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  5201. ASSERT(cur == NULL);
  5202. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5203. &cur, 0, &tmp_logflags, whichfork);
  5204. logflags |= tmp_logflags;
  5205. if (error)
  5206. goto error0;
  5207. }
  5208. /*
  5209. * transform from btree to extents, give it cur
  5210. */
  5211. else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5212. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5213. ASSERT(cur != NULL);
  5214. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5215. whichfork);
  5216. logflags |= tmp_logflags;
  5217. if (error)
  5218. goto error0;
  5219. }
  5220. /*
  5221. * transform from extents to local?
  5222. */
  5223. ASSERT(ifp->if_ext_max ==
  5224. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5225. error = 0;
  5226. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5227. /* A change was actually made.
  5228. * Note that delta->xed_blockount is an offset at this
  5229. * point and needs to be converted to a block count.
  5230. */
  5231. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5232. delta->xed_blockcount -= delta->xed_startoff;
  5233. }
  5234. error0:
  5235. /*
  5236. * Log everything. Do this after conversion, there's no point in
  5237. * logging the extent records if we've converted to btree format.
  5238. */
  5239. if ((logflags & xfs_ilog_fext(whichfork)) &&
  5240. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5241. logflags &= ~xfs_ilog_fext(whichfork);
  5242. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  5243. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5244. logflags &= ~xfs_ilog_fbroot(whichfork);
  5245. /*
  5246. * Log inode even in the error case, if the transaction
  5247. * is dirty we'll need to shut down the filesystem.
  5248. */
  5249. if (logflags)
  5250. xfs_trans_log_inode(tp, ip, logflags);
  5251. if (cur) {
  5252. if (!error) {
  5253. *firstblock = cur->bc_private.b.firstblock;
  5254. cur->bc_private.b.allocated = 0;
  5255. }
  5256. xfs_btree_del_cursor(cur,
  5257. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5258. }
  5259. return error;
  5260. }
  5261. /*
  5262. * returns 1 for success, 0 if we failed to map the extent.
  5263. */
  5264. STATIC int
  5265. xfs_getbmapx_fix_eof_hole(
  5266. xfs_inode_t *ip, /* xfs incore inode pointer */
  5267. struct getbmapx *out, /* output structure */
  5268. int prealloced, /* this is a file with
  5269. * preallocated data space */
  5270. __int64_t end, /* last block requested */
  5271. xfs_fsblock_t startblock)
  5272. {
  5273. __int64_t fixlen;
  5274. xfs_mount_t *mp; /* file system mount point */
  5275. xfs_ifork_t *ifp; /* inode fork pointer */
  5276. xfs_extnum_t lastx; /* last extent pointer */
  5277. xfs_fileoff_t fileblock;
  5278. if (startblock == HOLESTARTBLOCK) {
  5279. mp = ip->i_mount;
  5280. out->bmv_block = -1;
  5281. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
  5282. fixlen -= out->bmv_offset;
  5283. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  5284. /* Came to hole at EOF. Trim it. */
  5285. if (fixlen <= 0)
  5286. return 0;
  5287. out->bmv_length = fixlen;
  5288. }
  5289. } else {
  5290. if (startblock == DELAYSTARTBLOCK)
  5291. out->bmv_block = -2;
  5292. else
  5293. out->bmv_block = xfs_fsb_to_db(ip, startblock);
  5294. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  5295. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  5296. if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  5297. (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
  5298. out->bmv_oflags |= BMV_OF_LAST;
  5299. }
  5300. return 1;
  5301. }
  5302. /*
  5303. * Get inode's extents as described in bmv, and format for output.
  5304. * Calls formatter to fill the user's buffer until all extents
  5305. * are mapped, until the passed-in bmv->bmv_count slots have
  5306. * been filled, or until the formatter short-circuits the loop,
  5307. * if it is tracking filled-in extents on its own.
  5308. */
  5309. int /* error code */
  5310. xfs_getbmap(
  5311. xfs_inode_t *ip,
  5312. struct getbmapx *bmv, /* user bmap structure */
  5313. xfs_bmap_format_t formatter, /* format to user */
  5314. void *arg) /* formatter arg */
  5315. {
  5316. __int64_t bmvend; /* last block requested */
  5317. int error = 0; /* return value */
  5318. __int64_t fixlen; /* length for -1 case */
  5319. int i; /* extent number */
  5320. int lock; /* lock state */
  5321. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5322. xfs_mount_t *mp; /* file system mount point */
  5323. int nex; /* # of user extents can do */
  5324. int nexleft; /* # of user extents left */
  5325. int subnex; /* # of bmapi's can do */
  5326. int nmap; /* number of map entries */
  5327. struct getbmapx *out; /* output structure */
  5328. int whichfork; /* data or attr fork */
  5329. int prealloced; /* this is a file with
  5330. * preallocated data space */
  5331. int iflags; /* interface flags */
  5332. int bmapi_flags; /* flags for xfs_bmapi */
  5333. int cur_ext = 0;
  5334. mp = ip->i_mount;
  5335. iflags = bmv->bmv_iflags;
  5336. whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5337. if (whichfork == XFS_ATTR_FORK) {
  5338. if (XFS_IFORK_Q(ip)) {
  5339. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5340. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5341. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5342. return XFS_ERROR(EINVAL);
  5343. } else if (unlikely(
  5344. ip->i_d.di_aformat != 0 &&
  5345. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5346. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5347. ip->i_mount);
  5348. return XFS_ERROR(EFSCORRUPTED);
  5349. }
  5350. prealloced = 0;
  5351. fixlen = 1LL << 32;
  5352. } else {
  5353. /*
  5354. * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
  5355. * not generate a DMAPI read event. Otherwise, if the
  5356. * DM_EVENT_READ bit is set for the file, generate a read
  5357. * event in order that the DMAPI application may do its thing
  5358. * before we return the extents. Usually this means restoring
  5359. * user file data to regions of the file that look like holes.
  5360. *
  5361. * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
  5362. * BMV_IF_NO_DMAPI_READ so that read events are generated.
  5363. * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
  5364. * could misinterpret holes in a DMAPI file as true holes,
  5365. * when in fact they may represent offline user data.
  5366. */
  5367. if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
  5368. !(iflags & BMV_IF_NO_DMAPI_READ)) {
  5369. error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
  5370. 0, 0, 0, NULL);
  5371. if (error)
  5372. return XFS_ERROR(error);
  5373. }
  5374. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5375. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5376. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5377. return XFS_ERROR(EINVAL);
  5378. if (xfs_get_extsz_hint(ip) ||
  5379. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  5380. prealloced = 1;
  5381. fixlen = XFS_MAXIOFFSET(mp);
  5382. } else {
  5383. prealloced = 0;
  5384. fixlen = ip->i_size;
  5385. }
  5386. }
  5387. if (bmv->bmv_length == -1) {
  5388. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5389. bmv->bmv_length =
  5390. max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
  5391. } else if (bmv->bmv_length == 0) {
  5392. bmv->bmv_entries = 0;
  5393. return 0;
  5394. } else if (bmv->bmv_length < 0) {
  5395. return XFS_ERROR(EINVAL);
  5396. }
  5397. nex = bmv->bmv_count - 1;
  5398. if (nex <= 0)
  5399. return XFS_ERROR(EINVAL);
  5400. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5401. if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
  5402. return XFS_ERROR(ENOMEM);
  5403. out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
  5404. if (!out)
  5405. return XFS_ERROR(ENOMEM);
  5406. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5407. if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
  5408. if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
  5409. error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
  5410. if (error)
  5411. goto out_unlock_iolock;
  5412. }
  5413. ASSERT(ip->i_delayed_blks == 0);
  5414. }
  5415. lock = xfs_ilock_map_shared(ip);
  5416. /*
  5417. * Don't let nex be bigger than the number of extents
  5418. * we can have assuming alternating holes and real extents.
  5419. */
  5420. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5421. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5422. bmapi_flags = xfs_bmapi_aflag(whichfork);
  5423. if (!(iflags & BMV_IF_PREALLOC))
  5424. bmapi_flags |= XFS_BMAPI_IGSTATE;
  5425. /*
  5426. * Allocate enough space to handle "subnex" maps at a time.
  5427. */
  5428. error = ENOMEM;
  5429. subnex = 16;
  5430. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  5431. if (!map)
  5432. goto out_unlock_ilock;
  5433. bmv->bmv_entries = 0;
  5434. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  5435. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  5436. error = 0;
  5437. goto out_free_map;
  5438. }
  5439. nexleft = nex;
  5440. do {
  5441. nmap = (nexleft > subnex) ? subnex : nexleft;
  5442. error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5443. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5444. bmapi_flags, NULL, 0, map, &nmap,
  5445. NULL, NULL);
  5446. if (error)
  5447. goto out_free_map;
  5448. ASSERT(nmap <= subnex);
  5449. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5450. out[cur_ext].bmv_oflags = 0;
  5451. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  5452. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  5453. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  5454. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  5455. out[cur_ext].bmv_offset =
  5456. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5457. out[cur_ext].bmv_length =
  5458. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5459. out[cur_ext].bmv_unused1 = 0;
  5460. out[cur_ext].bmv_unused2 = 0;
  5461. ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
  5462. (map[i].br_startblock != DELAYSTARTBLOCK));
  5463. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5464. whichfork == XFS_ATTR_FORK) {
  5465. /* came to the end of attribute fork */
  5466. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  5467. goto out_free_map;
  5468. }
  5469. if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
  5470. prealloced, bmvend,
  5471. map[i].br_startblock))
  5472. goto out_free_map;
  5473. nexleft--;
  5474. bmv->bmv_offset =
  5475. out[cur_ext].bmv_offset +
  5476. out[cur_ext].bmv_length;
  5477. bmv->bmv_length =
  5478. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  5479. bmv->bmv_entries++;
  5480. cur_ext++;
  5481. }
  5482. } while (nmap && nexleft && bmv->bmv_length);
  5483. out_free_map:
  5484. kmem_free(map);
  5485. out_unlock_ilock:
  5486. xfs_iunlock_map_shared(ip, lock);
  5487. out_unlock_iolock:
  5488. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5489. for (i = 0; i < cur_ext; i++) {
  5490. int full = 0; /* user array is full */
  5491. /* format results & advance arg */
  5492. error = formatter(&arg, &out[i], &full);
  5493. if (error || full)
  5494. break;
  5495. }
  5496. kmem_free(out);
  5497. return error;
  5498. }
  5499. /*
  5500. * Check the last inode extent to determine whether this allocation will result
  5501. * in blocks being allocated at the end of the file. When we allocate new data
  5502. * blocks at the end of the file which do not start at the previous data block,
  5503. * we will try to align the new blocks at stripe unit boundaries.
  5504. */
  5505. STATIC int /* error */
  5506. xfs_bmap_isaeof(
  5507. xfs_inode_t *ip, /* incore inode pointer */
  5508. xfs_fileoff_t off, /* file offset in fsblocks */
  5509. int whichfork, /* data or attribute fork */
  5510. char *aeof) /* return value */
  5511. {
  5512. int error; /* error return value */
  5513. xfs_ifork_t *ifp; /* inode fork pointer */
  5514. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5515. xfs_extnum_t nextents; /* number of file extents */
  5516. xfs_bmbt_irec_t s; /* expanded extent record */
  5517. ASSERT(whichfork == XFS_DATA_FORK);
  5518. ifp = XFS_IFORK_PTR(ip, whichfork);
  5519. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5520. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5521. return error;
  5522. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5523. if (nextents == 0) {
  5524. *aeof = 1;
  5525. return 0;
  5526. }
  5527. /*
  5528. * Go to the last extent
  5529. */
  5530. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5531. xfs_bmbt_get_all(lastrec, &s);
  5532. /*
  5533. * Check we are allocating in the last extent (for delayed allocations)
  5534. * or past the last extent for non-delayed allocations.
  5535. */
  5536. *aeof = (off >= s.br_startoff &&
  5537. off < s.br_startoff + s.br_blockcount &&
  5538. isnullstartblock(s.br_startblock)) ||
  5539. off >= s.br_startoff + s.br_blockcount;
  5540. return 0;
  5541. }
  5542. /*
  5543. * Check if the endoff is outside the last extent. If so the caller will grow
  5544. * the allocation to a stripe unit boundary.
  5545. */
  5546. int /* error */
  5547. xfs_bmap_eof(
  5548. xfs_inode_t *ip, /* incore inode pointer */
  5549. xfs_fileoff_t endoff, /* file offset in fsblocks */
  5550. int whichfork, /* data or attribute fork */
  5551. int *eof) /* result value */
  5552. {
  5553. xfs_fsblock_t blockcount; /* extent block count */
  5554. int error; /* error return value */
  5555. xfs_ifork_t *ifp; /* inode fork pointer */
  5556. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5557. xfs_extnum_t nextents; /* number of file extents */
  5558. xfs_fileoff_t startoff; /* extent starting file offset */
  5559. ASSERT(whichfork == XFS_DATA_FORK);
  5560. ifp = XFS_IFORK_PTR(ip, whichfork);
  5561. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5562. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5563. return error;
  5564. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5565. if (nextents == 0) {
  5566. *eof = 1;
  5567. return 0;
  5568. }
  5569. /*
  5570. * Go to the last extent
  5571. */
  5572. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5573. startoff = xfs_bmbt_get_startoff(lastrec);
  5574. blockcount = xfs_bmbt_get_blockcount(lastrec);
  5575. *eof = endoff >= startoff + blockcount;
  5576. return 0;
  5577. }
  5578. #ifdef DEBUG
  5579. STATIC
  5580. xfs_buf_t *
  5581. xfs_bmap_get_bp(
  5582. xfs_btree_cur_t *cur,
  5583. xfs_fsblock_t bno)
  5584. {
  5585. int i;
  5586. xfs_buf_t *bp;
  5587. if (!cur)
  5588. return(NULL);
  5589. bp = NULL;
  5590. for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5591. bp = cur->bc_bufs[i];
  5592. if (!bp) break;
  5593. if (XFS_BUF_ADDR(bp) == bno)
  5594. break; /* Found it */
  5595. }
  5596. if (i == XFS_BTREE_MAXLEVELS)
  5597. bp = NULL;
  5598. if (!bp) { /* Chase down all the log items to see if the bp is there */
  5599. xfs_log_item_chunk_t *licp;
  5600. xfs_trans_t *tp;
  5601. tp = cur->bc_tp;
  5602. licp = &tp->t_items;
  5603. while (!bp && licp != NULL) {
  5604. if (xfs_lic_are_all_free(licp)) {
  5605. licp = licp->lic_next;
  5606. continue;
  5607. }
  5608. for (i = 0; i < licp->lic_unused; i++) {
  5609. xfs_log_item_desc_t *lidp;
  5610. xfs_log_item_t *lip;
  5611. xfs_buf_log_item_t *bip;
  5612. xfs_buf_t *lbp;
  5613. if (xfs_lic_isfree(licp, i)) {
  5614. continue;
  5615. }
  5616. lidp = xfs_lic_slot(licp, i);
  5617. lip = lidp->lid_item;
  5618. if (lip->li_type != XFS_LI_BUF)
  5619. continue;
  5620. bip = (xfs_buf_log_item_t *)lip;
  5621. lbp = bip->bli_buf;
  5622. if (XFS_BUF_ADDR(lbp) == bno) {
  5623. bp = lbp;
  5624. break; /* Found it */
  5625. }
  5626. }
  5627. licp = licp->lic_next;
  5628. }
  5629. }
  5630. return(bp);
  5631. }
  5632. STATIC void
  5633. xfs_check_block(
  5634. struct xfs_btree_block *block,
  5635. xfs_mount_t *mp,
  5636. int root,
  5637. short sz)
  5638. {
  5639. int i, j, dmxr;
  5640. __be64 *pp, *thispa; /* pointer to block address */
  5641. xfs_bmbt_key_t *prevp, *keyp;
  5642. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5643. prevp = NULL;
  5644. for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
  5645. dmxr = mp->m_bmap_dmxr[0];
  5646. keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
  5647. if (prevp) {
  5648. ASSERT(be64_to_cpu(prevp->br_startoff) <
  5649. be64_to_cpu(keyp->br_startoff));
  5650. }
  5651. prevp = keyp;
  5652. /*
  5653. * Compare the block numbers to see if there are dups.
  5654. */
  5655. if (root)
  5656. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
  5657. else
  5658. pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
  5659. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5660. if (root)
  5661. thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
  5662. else
  5663. thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
  5664. if (*thispa == *pp) {
  5665. cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
  5666. __func__, j, i,
  5667. (unsigned long long)be64_to_cpu(*thispa));
  5668. panic("%s: ptrs are equal in node\n",
  5669. __func__);
  5670. }
  5671. }
  5672. }
  5673. }
  5674. /*
  5675. * Check that the extents for the inode ip are in the right order in all
  5676. * btree leaves.
  5677. */
  5678. STATIC void
  5679. xfs_bmap_check_leaf_extents(
  5680. xfs_btree_cur_t *cur, /* btree cursor or null */
  5681. xfs_inode_t *ip, /* incore inode pointer */
  5682. int whichfork) /* data or attr fork */
  5683. {
  5684. struct xfs_btree_block *block; /* current btree block */
  5685. xfs_fsblock_t bno; /* block # of "block" */
  5686. xfs_buf_t *bp; /* buffer for "block" */
  5687. int error; /* error return value */
  5688. xfs_extnum_t i=0, j; /* index into the extents list */
  5689. xfs_ifork_t *ifp; /* fork structure */
  5690. int level; /* btree level, for checking */
  5691. xfs_mount_t *mp; /* file system mount structure */
  5692. __be64 *pp; /* pointer to block address */
  5693. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  5694. xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
  5695. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  5696. int bp_release = 0;
  5697. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  5698. return;
  5699. }
  5700. bno = NULLFSBLOCK;
  5701. mp = ip->i_mount;
  5702. ifp = XFS_IFORK_PTR(ip, whichfork);
  5703. block = ifp->if_broot;
  5704. /*
  5705. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5706. */
  5707. level = be16_to_cpu(block->bb_level);
  5708. ASSERT(level > 0);
  5709. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  5710. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5711. bno = be64_to_cpu(*pp);
  5712. ASSERT(bno != NULLDFSBNO);
  5713. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5714. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5715. /*
  5716. * Go down the tree until leaf level is reached, following the first
  5717. * pointer (leftmost) at each level.
  5718. */
  5719. while (level-- > 0) {
  5720. /* See if buf is in cur first */
  5721. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5722. if (bp) {
  5723. bp_release = 0;
  5724. } else {
  5725. bp_release = 1;
  5726. }
  5727. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5728. XFS_BMAP_BTREE_REF)))
  5729. goto error_norelse;
  5730. block = XFS_BUF_TO_BLOCK(bp);
  5731. XFS_WANT_CORRUPTED_GOTO(
  5732. xfs_bmap_sanity_check(mp, bp, level),
  5733. error0);
  5734. if (level == 0)
  5735. break;
  5736. /*
  5737. * Check this block for basic sanity (increasing keys and
  5738. * no duplicate blocks).
  5739. */
  5740. xfs_check_block(block, mp, 0, 0);
  5741. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5742. bno = be64_to_cpu(*pp);
  5743. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  5744. if (bp_release) {
  5745. bp_release = 0;
  5746. xfs_trans_brelse(NULL, bp);
  5747. }
  5748. }
  5749. /*
  5750. * Here with bp and block set to the leftmost leaf node in the tree.
  5751. */
  5752. i = 0;
  5753. /*
  5754. * Loop over all leaf nodes checking that all extents are in the right order.
  5755. */
  5756. for (;;) {
  5757. xfs_fsblock_t nextbno;
  5758. xfs_extnum_t num_recs;
  5759. num_recs = xfs_btree_get_numrecs(block);
  5760. /*
  5761. * Read-ahead the next leaf block, if any.
  5762. */
  5763. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5764. /*
  5765. * Check all the extents to make sure they are OK.
  5766. * If we had a previous block, the last entry should
  5767. * conform with the first entry in this one.
  5768. */
  5769. ep = XFS_BMBT_REC_ADDR(mp, block, 1);
  5770. if (i) {
  5771. ASSERT(xfs_bmbt_disk_get_startoff(&last) +
  5772. xfs_bmbt_disk_get_blockcount(&last) <=
  5773. xfs_bmbt_disk_get_startoff(ep));
  5774. }
  5775. for (j = 1; j < num_recs; j++) {
  5776. nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
  5777. ASSERT(xfs_bmbt_disk_get_startoff(ep) +
  5778. xfs_bmbt_disk_get_blockcount(ep) <=
  5779. xfs_bmbt_disk_get_startoff(nextp));
  5780. ep = nextp;
  5781. }
  5782. last = *ep;
  5783. i += num_recs;
  5784. if (bp_release) {
  5785. bp_release = 0;
  5786. xfs_trans_brelse(NULL, bp);
  5787. }
  5788. bno = nextbno;
  5789. /*
  5790. * If we've reached the end, stop.
  5791. */
  5792. if (bno == NULLFSBLOCK)
  5793. break;
  5794. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5795. if (bp) {
  5796. bp_release = 0;
  5797. } else {
  5798. bp_release = 1;
  5799. }
  5800. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5801. XFS_BMAP_BTREE_REF)))
  5802. goto error_norelse;
  5803. block = XFS_BUF_TO_BLOCK(bp);
  5804. }
  5805. if (bp_release) {
  5806. bp_release = 0;
  5807. xfs_trans_brelse(NULL, bp);
  5808. }
  5809. return;
  5810. error0:
  5811. cmn_err(CE_WARN, "%s: at error0", __func__);
  5812. if (bp_release)
  5813. xfs_trans_brelse(NULL, bp);
  5814. error_norelse:
  5815. cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
  5816. __func__, i);
  5817. panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
  5818. return;
  5819. }
  5820. #endif
  5821. /*
  5822. * Count fsblocks of the given fork.
  5823. */
  5824. int /* error */
  5825. xfs_bmap_count_blocks(
  5826. xfs_trans_t *tp, /* transaction pointer */
  5827. xfs_inode_t *ip, /* incore inode */
  5828. int whichfork, /* data or attr fork */
  5829. int *count) /* out: count of blocks */
  5830. {
  5831. struct xfs_btree_block *block; /* current btree block */
  5832. xfs_fsblock_t bno; /* block # of "block" */
  5833. xfs_ifork_t *ifp; /* fork structure */
  5834. int level; /* btree level, for checking */
  5835. xfs_mount_t *mp; /* file system mount structure */
  5836. __be64 *pp; /* pointer to block address */
  5837. bno = NULLFSBLOCK;
  5838. mp = ip->i_mount;
  5839. ifp = XFS_IFORK_PTR(ip, whichfork);
  5840. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  5841. xfs_bmap_count_leaves(ifp, 0,
  5842. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  5843. count);
  5844. return 0;
  5845. }
  5846. /*
  5847. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5848. */
  5849. block = ifp->if_broot;
  5850. level = be16_to_cpu(block->bb_level);
  5851. ASSERT(level > 0);
  5852. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5853. bno = be64_to_cpu(*pp);
  5854. ASSERT(bno != NULLDFSBNO);
  5855. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5856. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5857. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  5858. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  5859. mp);
  5860. return XFS_ERROR(EFSCORRUPTED);
  5861. }
  5862. return 0;
  5863. }
  5864. /*
  5865. * Recursively walks each level of a btree
  5866. * to count total fsblocks is use.
  5867. */
  5868. STATIC int /* error */
  5869. xfs_bmap_count_tree(
  5870. xfs_mount_t *mp, /* file system mount point */
  5871. xfs_trans_t *tp, /* transaction pointer */
  5872. xfs_ifork_t *ifp, /* inode fork pointer */
  5873. xfs_fsblock_t blockno, /* file system block number */
  5874. int levelin, /* level in btree */
  5875. int *count) /* Count of blocks */
  5876. {
  5877. int error;
  5878. xfs_buf_t *bp, *nbp;
  5879. int level = levelin;
  5880. __be64 *pp;
  5881. xfs_fsblock_t bno = blockno;
  5882. xfs_fsblock_t nextbno;
  5883. struct xfs_btree_block *block, *nextblock;
  5884. int numrecs;
  5885. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
  5886. return error;
  5887. *count += 1;
  5888. block = XFS_BUF_TO_BLOCK(bp);
  5889. if (--level) {
  5890. /* Not at node above leaves, count this level of nodes */
  5891. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5892. while (nextbno != NULLFSBLOCK) {
  5893. if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
  5894. 0, &nbp, XFS_BMAP_BTREE_REF)))
  5895. return error;
  5896. *count += 1;
  5897. nextblock = XFS_BUF_TO_BLOCK(nbp);
  5898. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  5899. xfs_trans_brelse(tp, nbp);
  5900. }
  5901. /* Dive to the next level */
  5902. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5903. bno = be64_to_cpu(*pp);
  5904. if (unlikely((error =
  5905. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  5906. xfs_trans_brelse(tp, bp);
  5907. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  5908. XFS_ERRLEVEL_LOW, mp);
  5909. return XFS_ERROR(EFSCORRUPTED);
  5910. }
  5911. xfs_trans_brelse(tp, bp);
  5912. } else {
  5913. /* count all level 1 nodes and their leaves */
  5914. for (;;) {
  5915. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5916. numrecs = be16_to_cpu(block->bb_numrecs);
  5917. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  5918. xfs_trans_brelse(tp, bp);
  5919. if (nextbno == NULLFSBLOCK)
  5920. break;
  5921. bno = nextbno;
  5922. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  5923. XFS_BMAP_BTREE_REF)))
  5924. return error;
  5925. *count += 1;
  5926. block = XFS_BUF_TO_BLOCK(bp);
  5927. }
  5928. }
  5929. return 0;
  5930. }
  5931. /*
  5932. * Count leaf blocks given a range of extent records.
  5933. */
  5934. STATIC void
  5935. xfs_bmap_count_leaves(
  5936. xfs_ifork_t *ifp,
  5937. xfs_extnum_t idx,
  5938. int numrecs,
  5939. int *count)
  5940. {
  5941. int b;
  5942. for (b = 0; b < numrecs; b++) {
  5943. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  5944. *count += xfs_bmbt_get_blockcount(frp);
  5945. }
  5946. }
  5947. /*
  5948. * Count leaf blocks given a range of extent records originally
  5949. * in btree format.
  5950. */
  5951. STATIC void
  5952. xfs_bmap_disk_count_leaves(
  5953. struct xfs_mount *mp,
  5954. struct xfs_btree_block *block,
  5955. int numrecs,
  5956. int *count)
  5957. {
  5958. int b;
  5959. xfs_bmbt_rec_t *frp;
  5960. for (b = 1; b <= numrecs; b++) {
  5961. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  5962. *count += xfs_bmbt_disk_get_blockcount(frp);
  5963. }
  5964. }