xfs_bmap.c 171 KB

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