xfs_bmap.c 178 KB

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