xfs_bmap.c 182 KB

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