xfs_bmap.c 179 KB

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