xfs_bmap.c 185 KB

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