xfs_bmap.c 180 KB

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