xfs_bmap.c 182 KB

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