xfs_bmap.c 191 KB

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