xfs_bmap.c 176 KB

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