xfs_bmap.c 178 KB

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