xfs_bmap.c 178 KB

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