xfs_bmap.c 184 KB

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