xfs_bmap.c 182 KB

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