xfs_bmap.c 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949
  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. if (*blen < longest)
  3242. *blen = longest;
  3243. out:
  3244. xfs_perag_put(pag);
  3245. return error;
  3246. }
  3247. static void
  3248. xfs_bmap_select_minlen(
  3249. struct xfs_bmalloca *ap,
  3250. struct xfs_alloc_arg *args,
  3251. xfs_extlen_t *blen,
  3252. int notinit)
  3253. {
  3254. if (notinit || *blen < ap->minlen) {
  3255. /*
  3256. * Since we did a BUF_TRYLOCK above, it is possible that
  3257. * there is space for this request.
  3258. */
  3259. args->minlen = ap->minlen;
  3260. } else if (*blen < args->maxlen) {
  3261. /*
  3262. * If the best seen length is less than the request length,
  3263. * use the best as the minimum.
  3264. */
  3265. args->minlen = *blen;
  3266. } else {
  3267. /*
  3268. * Otherwise we've seen an extent as big as maxlen, use that
  3269. * as the minimum.
  3270. */
  3271. args->minlen = args->maxlen;
  3272. }
  3273. }
  3274. STATIC int
  3275. xfs_bmap_btalloc_nullfb(
  3276. struct xfs_bmalloca *ap,
  3277. struct xfs_alloc_arg *args,
  3278. xfs_extlen_t *blen)
  3279. {
  3280. struct xfs_mount *mp = ap->ip->i_mount;
  3281. xfs_agnumber_t ag, startag;
  3282. int notinit = 0;
  3283. int error;
  3284. args->type = XFS_ALLOCTYPE_START_BNO;
  3285. args->total = ap->total;
  3286. startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3287. if (startag == NULLAGNUMBER)
  3288. startag = ag = 0;
  3289. while (*blen < args->maxlen) {
  3290. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3291. &notinit);
  3292. if (error)
  3293. return error;
  3294. if (++ag == mp->m_sb.sb_agcount)
  3295. ag = 0;
  3296. if (ag == startag)
  3297. break;
  3298. }
  3299. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3300. return 0;
  3301. }
  3302. STATIC int
  3303. xfs_bmap_btalloc_filestreams(
  3304. struct xfs_bmalloca *ap,
  3305. struct xfs_alloc_arg *args,
  3306. xfs_extlen_t *blen)
  3307. {
  3308. struct xfs_mount *mp = ap->ip->i_mount;
  3309. xfs_agnumber_t ag;
  3310. int notinit = 0;
  3311. int error;
  3312. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  3313. args->total = ap->total;
  3314. ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3315. if (ag == NULLAGNUMBER)
  3316. ag = 0;
  3317. error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
  3318. if (error)
  3319. return error;
  3320. if (*blen < args->maxlen) {
  3321. error = xfs_filestream_new_ag(ap, &ag);
  3322. if (error)
  3323. return error;
  3324. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3325. &notinit);
  3326. if (error)
  3327. return error;
  3328. }
  3329. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3330. /*
  3331. * Set the failure fallback case to look in the selected AG as stream
  3332. * may have moved.
  3333. */
  3334. ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  3335. return 0;
  3336. }
  3337. STATIC int
  3338. xfs_bmap_btalloc(
  3339. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3340. {
  3341. xfs_mount_t *mp; /* mount point structure */
  3342. xfs_alloctype_t atype = 0; /* type for allocation routines */
  3343. xfs_extlen_t align; /* minimum allocation alignment */
  3344. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  3345. xfs_agnumber_t ag;
  3346. xfs_alloc_arg_t args;
  3347. xfs_extlen_t blen;
  3348. xfs_extlen_t nextminlen = 0;
  3349. int nullfb; /* true if ap->firstblock isn't set */
  3350. int isaligned;
  3351. int tryagain;
  3352. int error;
  3353. int stripe_align;
  3354. ASSERT(ap->length);
  3355. mp = ap->ip->i_mount;
  3356. /* stripe alignment for allocation is determined by mount parameters */
  3357. stripe_align = 0;
  3358. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  3359. stripe_align = mp->m_swidth;
  3360. else if (mp->m_dalign)
  3361. stripe_align = mp->m_dalign;
  3362. align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
  3363. if (unlikely(align)) {
  3364. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  3365. align, 0, ap->eof, 0, ap->conv,
  3366. &ap->offset, &ap->length);
  3367. ASSERT(!error);
  3368. ASSERT(ap->length);
  3369. }
  3370. nullfb = *ap->firstblock == NULLFSBLOCK;
  3371. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  3372. if (nullfb) {
  3373. if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
  3374. ag = xfs_filestream_lookup_ag(ap->ip);
  3375. ag = (ag != NULLAGNUMBER) ? ag : 0;
  3376. ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
  3377. } else {
  3378. ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  3379. }
  3380. } else
  3381. ap->blkno = *ap->firstblock;
  3382. xfs_bmap_adjacent(ap);
  3383. /*
  3384. * If allowed, use ap->blkno; otherwise must use firstblock since
  3385. * it's in the right allocation group.
  3386. */
  3387. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
  3388. ;
  3389. else
  3390. ap->blkno = *ap->firstblock;
  3391. /*
  3392. * Normal allocation, done through xfs_alloc_vextent.
  3393. */
  3394. tryagain = isaligned = 0;
  3395. memset(&args, 0, sizeof(args));
  3396. args.tp = ap->tp;
  3397. args.mp = mp;
  3398. args.fsbno = ap->blkno;
  3399. /* Trim the allocation back to the maximum an AG can fit. */
  3400. args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
  3401. args.firstblock = *ap->firstblock;
  3402. blen = 0;
  3403. if (nullfb) {
  3404. /*
  3405. * Search for an allocation group with a single extent large
  3406. * enough for the request. If one isn't found, then adjust
  3407. * the minimum allocation size to the largest space found.
  3408. */
  3409. if (ap->userdata && xfs_inode_is_filestream(ap->ip))
  3410. error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
  3411. else
  3412. error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
  3413. if (error)
  3414. return error;
  3415. } else if (ap->flist->xbf_low) {
  3416. if (xfs_inode_is_filestream(ap->ip))
  3417. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3418. else
  3419. args.type = XFS_ALLOCTYPE_START_BNO;
  3420. args.total = args.minlen = ap->minlen;
  3421. } else {
  3422. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3423. args.total = ap->total;
  3424. args.minlen = ap->minlen;
  3425. }
  3426. /* apply extent size hints if obtained earlier */
  3427. if (unlikely(align)) {
  3428. args.prod = align;
  3429. if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
  3430. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3431. } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
  3432. args.prod = 1;
  3433. args.mod = 0;
  3434. } else {
  3435. args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
  3436. if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
  3437. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3438. }
  3439. /*
  3440. * If we are not low on available data blocks, and the
  3441. * underlying logical volume manager is a stripe, and
  3442. * the file offset is zero then try to allocate data
  3443. * blocks on stripe unit boundary.
  3444. * NOTE: ap->aeof is only set if the allocation length
  3445. * is >= the stripe unit and the allocation offset is
  3446. * at the end of file.
  3447. */
  3448. if (!ap->flist->xbf_low && ap->aeof) {
  3449. if (!ap->offset) {
  3450. args.alignment = stripe_align;
  3451. atype = args.type;
  3452. isaligned = 1;
  3453. /*
  3454. * Adjust for alignment
  3455. */
  3456. if (blen > args.alignment && blen <= args.maxlen)
  3457. args.minlen = blen - args.alignment;
  3458. args.minalignslop = 0;
  3459. } else {
  3460. /*
  3461. * First try an exact bno allocation.
  3462. * If it fails then do a near or start bno
  3463. * allocation with alignment turned on.
  3464. */
  3465. atype = args.type;
  3466. tryagain = 1;
  3467. args.type = XFS_ALLOCTYPE_THIS_BNO;
  3468. args.alignment = 1;
  3469. /*
  3470. * Compute the minlen+alignment for the
  3471. * next case. Set slop so that the value
  3472. * of minlen+alignment+slop doesn't go up
  3473. * between the calls.
  3474. */
  3475. if (blen > stripe_align && blen <= args.maxlen)
  3476. nextminlen = blen - stripe_align;
  3477. else
  3478. nextminlen = args.minlen;
  3479. if (nextminlen + stripe_align > args.minlen + 1)
  3480. args.minalignslop =
  3481. nextminlen + stripe_align -
  3482. args.minlen - 1;
  3483. else
  3484. args.minalignslop = 0;
  3485. }
  3486. } else {
  3487. args.alignment = 1;
  3488. args.minalignslop = 0;
  3489. }
  3490. args.minleft = ap->minleft;
  3491. args.wasdel = ap->wasdel;
  3492. args.isfl = 0;
  3493. args.userdata = ap->userdata;
  3494. if ((error = xfs_alloc_vextent(&args)))
  3495. return error;
  3496. if (tryagain && args.fsbno == NULLFSBLOCK) {
  3497. /*
  3498. * Exact allocation failed. Now try with alignment
  3499. * turned on.
  3500. */
  3501. args.type = atype;
  3502. args.fsbno = ap->blkno;
  3503. args.alignment = stripe_align;
  3504. args.minlen = nextminlen;
  3505. args.minalignslop = 0;
  3506. isaligned = 1;
  3507. if ((error = xfs_alloc_vextent(&args)))
  3508. return error;
  3509. }
  3510. if (isaligned && args.fsbno == NULLFSBLOCK) {
  3511. /*
  3512. * allocation failed, so turn off alignment and
  3513. * try again.
  3514. */
  3515. args.type = atype;
  3516. args.fsbno = ap->blkno;
  3517. args.alignment = 0;
  3518. if ((error = xfs_alloc_vextent(&args)))
  3519. return error;
  3520. }
  3521. if (args.fsbno == NULLFSBLOCK && nullfb &&
  3522. args.minlen > ap->minlen) {
  3523. args.minlen = ap->minlen;
  3524. args.type = XFS_ALLOCTYPE_START_BNO;
  3525. args.fsbno = ap->blkno;
  3526. if ((error = xfs_alloc_vextent(&args)))
  3527. return error;
  3528. }
  3529. if (args.fsbno == NULLFSBLOCK && nullfb) {
  3530. args.fsbno = 0;
  3531. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3532. args.total = ap->minlen;
  3533. args.minleft = 0;
  3534. if ((error = xfs_alloc_vextent(&args)))
  3535. return error;
  3536. ap->flist->xbf_low = 1;
  3537. }
  3538. if (args.fsbno != NULLFSBLOCK) {
  3539. /*
  3540. * check the allocation happened at the same or higher AG than
  3541. * the first block that was allocated.
  3542. */
  3543. ASSERT(*ap->firstblock == NULLFSBLOCK ||
  3544. XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
  3545. XFS_FSB_TO_AGNO(mp, args.fsbno) ||
  3546. (ap->flist->xbf_low &&
  3547. XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
  3548. XFS_FSB_TO_AGNO(mp, args.fsbno)));
  3549. ap->blkno = args.fsbno;
  3550. if (*ap->firstblock == NULLFSBLOCK)
  3551. *ap->firstblock = args.fsbno;
  3552. ASSERT(nullfb || fb_agno == args.agno ||
  3553. (ap->flist->xbf_low && fb_agno < args.agno));
  3554. ap->length = args.len;
  3555. ap->ip->i_d.di_nblocks += args.len;
  3556. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  3557. if (ap->wasdel)
  3558. ap->ip->i_delayed_blks -= args.len;
  3559. /*
  3560. * Adjust the disk quota also. This was reserved
  3561. * earlier.
  3562. */
  3563. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  3564. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  3565. XFS_TRANS_DQ_BCOUNT,
  3566. (long) args.len);
  3567. } else {
  3568. ap->blkno = NULLFSBLOCK;
  3569. ap->length = 0;
  3570. }
  3571. return 0;
  3572. }
  3573. /*
  3574. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  3575. * It figures out where to ask the underlying allocator to put the new extent.
  3576. */
  3577. STATIC int
  3578. xfs_bmap_alloc(
  3579. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3580. {
  3581. if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
  3582. return xfs_bmap_rtalloc(ap);
  3583. return xfs_bmap_btalloc(ap);
  3584. }
  3585. /*
  3586. * Trim the returned map to the required bounds
  3587. */
  3588. STATIC void
  3589. xfs_bmapi_trim_map(
  3590. struct xfs_bmbt_irec *mval,
  3591. struct xfs_bmbt_irec *got,
  3592. xfs_fileoff_t *bno,
  3593. xfs_filblks_t len,
  3594. xfs_fileoff_t obno,
  3595. xfs_fileoff_t end,
  3596. int n,
  3597. int flags)
  3598. {
  3599. if ((flags & XFS_BMAPI_ENTIRE) ||
  3600. got->br_startoff + got->br_blockcount <= obno) {
  3601. *mval = *got;
  3602. if (isnullstartblock(got->br_startblock))
  3603. mval->br_startblock = DELAYSTARTBLOCK;
  3604. return;
  3605. }
  3606. if (obno > *bno)
  3607. *bno = obno;
  3608. ASSERT((*bno >= obno) || (n == 0));
  3609. ASSERT(*bno < end);
  3610. mval->br_startoff = *bno;
  3611. if (isnullstartblock(got->br_startblock))
  3612. mval->br_startblock = DELAYSTARTBLOCK;
  3613. else
  3614. mval->br_startblock = got->br_startblock +
  3615. (*bno - got->br_startoff);
  3616. /*
  3617. * Return the minimum of what we got and what we asked for for
  3618. * the length. We can use the len variable here because it is
  3619. * modified below and we could have been there before coming
  3620. * here if the first part of the allocation didn't overlap what
  3621. * was asked for.
  3622. */
  3623. mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
  3624. got->br_blockcount - (*bno - got->br_startoff));
  3625. mval->br_state = got->br_state;
  3626. ASSERT(mval->br_blockcount <= len);
  3627. return;
  3628. }
  3629. /*
  3630. * Update and validate the extent map to return
  3631. */
  3632. STATIC void
  3633. xfs_bmapi_update_map(
  3634. struct xfs_bmbt_irec **map,
  3635. xfs_fileoff_t *bno,
  3636. xfs_filblks_t *len,
  3637. xfs_fileoff_t obno,
  3638. xfs_fileoff_t end,
  3639. int *n,
  3640. int flags)
  3641. {
  3642. xfs_bmbt_irec_t *mval = *map;
  3643. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  3644. ((mval->br_startoff + mval->br_blockcount) <= end));
  3645. ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
  3646. (mval->br_startoff < obno));
  3647. *bno = mval->br_startoff + mval->br_blockcount;
  3648. *len = end - *bno;
  3649. if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  3650. /* update previous map with new information */
  3651. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  3652. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  3653. ASSERT(mval->br_state == mval[-1].br_state);
  3654. mval[-1].br_blockcount = mval->br_blockcount;
  3655. mval[-1].br_state = mval->br_state;
  3656. } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  3657. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  3658. mval[-1].br_startblock != HOLESTARTBLOCK &&
  3659. mval->br_startblock == mval[-1].br_startblock +
  3660. mval[-1].br_blockcount &&
  3661. ((flags & XFS_BMAPI_IGSTATE) ||
  3662. mval[-1].br_state == mval->br_state)) {
  3663. ASSERT(mval->br_startoff ==
  3664. mval[-1].br_startoff + mval[-1].br_blockcount);
  3665. mval[-1].br_blockcount += mval->br_blockcount;
  3666. } else if (*n > 0 &&
  3667. mval->br_startblock == DELAYSTARTBLOCK &&
  3668. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  3669. mval->br_startoff ==
  3670. mval[-1].br_startoff + mval[-1].br_blockcount) {
  3671. mval[-1].br_blockcount += mval->br_blockcount;
  3672. mval[-1].br_state = mval->br_state;
  3673. } else if (!((*n == 0) &&
  3674. ((mval->br_startoff + mval->br_blockcount) <=
  3675. obno))) {
  3676. mval++;
  3677. (*n)++;
  3678. }
  3679. *map = mval;
  3680. }
  3681. /*
  3682. * Map file blocks to filesystem blocks without allocation.
  3683. */
  3684. int
  3685. xfs_bmapi_read(
  3686. struct xfs_inode *ip,
  3687. xfs_fileoff_t bno,
  3688. xfs_filblks_t len,
  3689. struct xfs_bmbt_irec *mval,
  3690. int *nmap,
  3691. int flags)
  3692. {
  3693. struct xfs_mount *mp = ip->i_mount;
  3694. struct xfs_ifork *ifp;
  3695. struct xfs_bmbt_irec got;
  3696. struct xfs_bmbt_irec prev;
  3697. xfs_fileoff_t obno;
  3698. xfs_fileoff_t end;
  3699. xfs_extnum_t lastx;
  3700. int error;
  3701. int eof;
  3702. int n = 0;
  3703. int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  3704. XFS_ATTR_FORK : XFS_DATA_FORK;
  3705. ASSERT(*nmap >= 1);
  3706. ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
  3707. XFS_BMAPI_IGSTATE)));
  3708. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
  3709. if (unlikely(XFS_TEST_ERROR(
  3710. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3711. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  3712. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  3713. XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
  3714. return -EFSCORRUPTED;
  3715. }
  3716. if (XFS_FORCED_SHUTDOWN(mp))
  3717. return -EIO;
  3718. XFS_STATS_INC(xs_blk_mapr);
  3719. ifp = XFS_IFORK_PTR(ip, whichfork);
  3720. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  3721. error = xfs_iread_extents(NULL, ip, whichfork);
  3722. if (error)
  3723. return error;
  3724. }
  3725. xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
  3726. end = bno + len;
  3727. obno = bno;
  3728. while (bno < end && n < *nmap) {
  3729. /* Reading past eof, act as though there's a hole up to end. */
  3730. if (eof)
  3731. got.br_startoff = end;
  3732. if (got.br_startoff > bno) {
  3733. /* Reading in a hole. */
  3734. mval->br_startoff = bno;
  3735. mval->br_startblock = HOLESTARTBLOCK;
  3736. mval->br_blockcount =
  3737. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  3738. mval->br_state = XFS_EXT_NORM;
  3739. bno += mval->br_blockcount;
  3740. len -= mval->br_blockcount;
  3741. mval++;
  3742. n++;
  3743. continue;
  3744. }
  3745. /* set up the extent map to return. */
  3746. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  3747. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  3748. /* If we're done, stop now. */
  3749. if (bno >= end || n >= *nmap)
  3750. break;
  3751. /* Else go on to the next record. */
  3752. if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
  3753. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
  3754. else
  3755. eof = 1;
  3756. }
  3757. *nmap = n;
  3758. return 0;
  3759. }
  3760. STATIC int
  3761. xfs_bmapi_reserve_delalloc(
  3762. struct xfs_inode *ip,
  3763. xfs_fileoff_t aoff,
  3764. xfs_filblks_t len,
  3765. struct xfs_bmbt_irec *got,
  3766. struct xfs_bmbt_irec *prev,
  3767. xfs_extnum_t *lastx,
  3768. int eof)
  3769. {
  3770. struct xfs_mount *mp = ip->i_mount;
  3771. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  3772. xfs_extlen_t alen;
  3773. xfs_extlen_t indlen;
  3774. char rt = XFS_IS_REALTIME_INODE(ip);
  3775. xfs_extlen_t extsz;
  3776. int error;
  3777. alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
  3778. if (!eof)
  3779. alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
  3780. /* Figure out the extent size, adjust alen */
  3781. extsz = xfs_get_extsz_hint(ip);
  3782. if (extsz) {
  3783. error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
  3784. 1, 0, &aoff, &alen);
  3785. ASSERT(!error);
  3786. }
  3787. if (rt)
  3788. extsz = alen / mp->m_sb.sb_rextsize;
  3789. /*
  3790. * Make a transaction-less quota reservation for delayed allocation
  3791. * blocks. This number gets adjusted later. We return if we haven't
  3792. * allocated blocks already inside this loop.
  3793. */
  3794. error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
  3795. rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3796. if (error)
  3797. return error;
  3798. /*
  3799. * Split changing sb for alen and indlen since they could be coming
  3800. * from different places.
  3801. */
  3802. indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
  3803. ASSERT(indlen > 0);
  3804. if (rt) {
  3805. error = xfs_mod_frextents(mp, -((int64_t)extsz));
  3806. } else {
  3807. error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
  3808. }
  3809. if (error)
  3810. goto out_unreserve_quota;
  3811. error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
  3812. if (error)
  3813. goto out_unreserve_blocks;
  3814. ip->i_delayed_blks += alen;
  3815. got->br_startoff = aoff;
  3816. got->br_startblock = nullstartblock(indlen);
  3817. got->br_blockcount = alen;
  3818. got->br_state = XFS_EXT_NORM;
  3819. xfs_bmap_add_extent_hole_delay(ip, lastx, got);
  3820. /*
  3821. * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
  3822. * might have merged it into one of the neighbouring ones.
  3823. */
  3824. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
  3825. ASSERT(got->br_startoff <= aoff);
  3826. ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
  3827. ASSERT(isnullstartblock(got->br_startblock));
  3828. ASSERT(got->br_state == XFS_EXT_NORM);
  3829. return 0;
  3830. out_unreserve_blocks:
  3831. if (rt)
  3832. xfs_mod_frextents(mp, extsz);
  3833. else
  3834. xfs_mod_fdblocks(mp, alen, false);
  3835. out_unreserve_quota:
  3836. if (XFS_IS_QUOTA_ON(mp))
  3837. xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
  3838. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3839. return error;
  3840. }
  3841. /*
  3842. * Map file blocks to filesystem blocks, adding delayed allocations as needed.
  3843. */
  3844. int
  3845. xfs_bmapi_delay(
  3846. struct xfs_inode *ip, /* incore inode */
  3847. xfs_fileoff_t bno, /* starting file offs. mapped */
  3848. xfs_filblks_t len, /* length to map in file */
  3849. struct xfs_bmbt_irec *mval, /* output: map values */
  3850. int *nmap, /* i/o: mval size/count */
  3851. int flags) /* XFS_BMAPI_... */
  3852. {
  3853. struct xfs_mount *mp = ip->i_mount;
  3854. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  3855. struct xfs_bmbt_irec got; /* current file extent record */
  3856. struct xfs_bmbt_irec prev; /* previous file extent record */
  3857. xfs_fileoff_t obno; /* old block number (offset) */
  3858. xfs_fileoff_t end; /* end of mapped file region */
  3859. xfs_extnum_t lastx; /* last useful extent number */
  3860. int eof; /* we've hit the end of extents */
  3861. int n = 0; /* current extent index */
  3862. int error = 0;
  3863. ASSERT(*nmap >= 1);
  3864. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  3865. ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
  3866. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  3867. if (unlikely(XFS_TEST_ERROR(
  3868. (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
  3869. XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
  3870. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  3871. XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
  3872. return -EFSCORRUPTED;
  3873. }
  3874. if (XFS_FORCED_SHUTDOWN(mp))
  3875. return -EIO;
  3876. XFS_STATS_INC(xs_blk_mapw);
  3877. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  3878. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  3879. if (error)
  3880. return error;
  3881. }
  3882. xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
  3883. end = bno + len;
  3884. obno = bno;
  3885. while (bno < end && n < *nmap) {
  3886. if (eof || got.br_startoff > bno) {
  3887. error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
  3888. &prev, &lastx, eof);
  3889. if (error) {
  3890. if (n == 0) {
  3891. *nmap = 0;
  3892. return error;
  3893. }
  3894. break;
  3895. }
  3896. }
  3897. /* set up the extent map to return. */
  3898. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  3899. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  3900. /* If we're done, stop now. */
  3901. if (bno >= end || n >= *nmap)
  3902. break;
  3903. /* Else go on to the next record. */
  3904. prev = got;
  3905. if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
  3906. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
  3907. else
  3908. eof = 1;
  3909. }
  3910. *nmap = n;
  3911. return 0;
  3912. }
  3913. static int
  3914. xfs_bmapi_allocate(
  3915. struct xfs_bmalloca *bma)
  3916. {
  3917. struct xfs_mount *mp = bma->ip->i_mount;
  3918. int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
  3919. XFS_ATTR_FORK : XFS_DATA_FORK;
  3920. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  3921. int tmp_logflags = 0;
  3922. int error;
  3923. ASSERT(bma->length > 0);
  3924. /*
  3925. * For the wasdelay case, we could also just allocate the stuff asked
  3926. * for in this bmap call but that wouldn't be as good.
  3927. */
  3928. if (bma->wasdel) {
  3929. bma->length = (xfs_extlen_t)bma->got.br_blockcount;
  3930. bma->offset = bma->got.br_startoff;
  3931. if (bma->idx != NULLEXTNUM && bma->idx) {
  3932. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
  3933. &bma->prev);
  3934. }
  3935. } else {
  3936. bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
  3937. if (!bma->eof)
  3938. bma->length = XFS_FILBLKS_MIN(bma->length,
  3939. bma->got.br_startoff - bma->offset);
  3940. }
  3941. /*
  3942. * Indicate if this is the first user data in the file, or just any
  3943. * user data.
  3944. */
  3945. if (!(bma->flags & XFS_BMAPI_METADATA)) {
  3946. bma->userdata = (bma->offset == 0) ?
  3947. XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
  3948. }
  3949. bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
  3950. /*
  3951. * Only want to do the alignment at the eof if it is userdata and
  3952. * allocation length is larger than a stripe unit.
  3953. */
  3954. if (mp->m_dalign && bma->length >= mp->m_dalign &&
  3955. !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
  3956. error = xfs_bmap_isaeof(bma, whichfork);
  3957. if (error)
  3958. return error;
  3959. }
  3960. error = xfs_bmap_alloc(bma);
  3961. if (error)
  3962. return error;
  3963. if (bma->flist->xbf_low)
  3964. bma->minleft = 0;
  3965. if (bma->cur)
  3966. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3967. if (bma->blkno == NULLFSBLOCK)
  3968. return 0;
  3969. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  3970. bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
  3971. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3972. bma->cur->bc_private.b.flist = bma->flist;
  3973. }
  3974. /*
  3975. * Bump the number of extents we've allocated
  3976. * in this call.
  3977. */
  3978. bma->nallocs++;
  3979. if (bma->cur)
  3980. bma->cur->bc_private.b.flags =
  3981. bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3982. bma->got.br_startoff = bma->offset;
  3983. bma->got.br_startblock = bma->blkno;
  3984. bma->got.br_blockcount = bma->length;
  3985. bma->got.br_state = XFS_EXT_NORM;
  3986. /*
  3987. * A wasdelay extent has been initialized, so shouldn't be flagged
  3988. * as unwritten.
  3989. */
  3990. if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
  3991. xfs_sb_version_hasextflgbit(&mp->m_sb))
  3992. bma->got.br_state = XFS_EXT_UNWRITTEN;
  3993. if (bma->wasdel)
  3994. error = xfs_bmap_add_extent_delay_real(bma);
  3995. else
  3996. error = xfs_bmap_add_extent_hole_real(bma, whichfork);
  3997. bma->logflags |= tmp_logflags;
  3998. if (error)
  3999. return error;
  4000. /*
  4001. * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
  4002. * or xfs_bmap_add_extent_hole_real might have merged it into one of
  4003. * the neighbouring ones.
  4004. */
  4005. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  4006. ASSERT(bma->got.br_startoff <= bma->offset);
  4007. ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
  4008. bma->offset + bma->length);
  4009. ASSERT(bma->got.br_state == XFS_EXT_NORM ||
  4010. bma->got.br_state == XFS_EXT_UNWRITTEN);
  4011. return 0;
  4012. }
  4013. STATIC int
  4014. xfs_bmapi_convert_unwritten(
  4015. struct xfs_bmalloca *bma,
  4016. struct xfs_bmbt_irec *mval,
  4017. xfs_filblks_t len,
  4018. int flags)
  4019. {
  4020. int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4021. XFS_ATTR_FORK : XFS_DATA_FORK;
  4022. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  4023. int tmp_logflags = 0;
  4024. int error;
  4025. /* check if we need to do unwritten->real conversion */
  4026. if (mval->br_state == XFS_EXT_UNWRITTEN &&
  4027. (flags & XFS_BMAPI_PREALLOC))
  4028. return 0;
  4029. /* check if we need to do real->unwritten conversion */
  4030. if (mval->br_state == XFS_EXT_NORM &&
  4031. (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
  4032. (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
  4033. return 0;
  4034. /*
  4035. * Modify (by adding) the state flag, if writing.
  4036. */
  4037. ASSERT(mval->br_blockcount <= len);
  4038. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  4039. bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
  4040. bma->ip, whichfork);
  4041. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  4042. bma->cur->bc_private.b.flist = bma->flist;
  4043. }
  4044. mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
  4045. ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  4046. error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
  4047. &bma->cur, mval, bma->firstblock, bma->flist,
  4048. &tmp_logflags);
  4049. /*
  4050. * Log the inode core unconditionally in the unwritten extent conversion
  4051. * path because the conversion might not have done so (e.g., if the
  4052. * extent count hasn't changed). We need to make sure the inode is dirty
  4053. * in the transaction for the sake of fsync(), even if nothing has
  4054. * changed, because fsync() will not force the log for this transaction
  4055. * unless it sees the inode pinned.
  4056. */
  4057. bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
  4058. if (error)
  4059. return error;
  4060. /*
  4061. * Update our extent pointer, given that
  4062. * xfs_bmap_add_extent_unwritten_real might have merged it into one
  4063. * of the neighbouring ones.
  4064. */
  4065. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  4066. /*
  4067. * We may have combined previously unwritten space with written space,
  4068. * so generate another request.
  4069. */
  4070. if (mval->br_blockcount < len)
  4071. return -EAGAIN;
  4072. return 0;
  4073. }
  4074. /*
  4075. * Map file blocks to filesystem blocks, and allocate blocks or convert the
  4076. * extent state if necessary. Details behaviour is controlled by the flags
  4077. * parameter. Only allocates blocks from a single allocation group, to avoid
  4078. * locking problems.
  4079. *
  4080. * The returned value in "firstblock" from the first call in a transaction
  4081. * must be remembered and presented to subsequent calls in "firstblock".
  4082. * An upper bound for the number of blocks to be allocated is supplied to
  4083. * the first call in "total"; if no allocation group has that many free
  4084. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4085. */
  4086. int
  4087. xfs_bmapi_write(
  4088. struct xfs_trans *tp, /* transaction pointer */
  4089. struct xfs_inode *ip, /* incore inode */
  4090. xfs_fileoff_t bno, /* starting file offs. mapped */
  4091. xfs_filblks_t len, /* length to map in file */
  4092. int flags, /* XFS_BMAPI_... */
  4093. xfs_fsblock_t *firstblock, /* first allocated block
  4094. controls a.g. for allocs */
  4095. xfs_extlen_t total, /* total blocks needed */
  4096. struct xfs_bmbt_irec *mval, /* output: map values */
  4097. int *nmap, /* i/o: mval size/count */
  4098. struct xfs_bmap_free *flist) /* i/o: list extents to free */
  4099. {
  4100. struct xfs_mount *mp = ip->i_mount;
  4101. struct xfs_ifork *ifp;
  4102. struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
  4103. xfs_fileoff_t end; /* end of mapped file region */
  4104. int eof; /* after the end of extents */
  4105. int error; /* error return */
  4106. int n; /* current extent index */
  4107. xfs_fileoff_t obno; /* old block number (offset) */
  4108. int whichfork; /* data or attr fork */
  4109. char inhole; /* current location is hole in file */
  4110. char wasdelay; /* old extent was delayed */
  4111. #ifdef DEBUG
  4112. xfs_fileoff_t orig_bno; /* original block number value */
  4113. int orig_flags; /* original flags arg value */
  4114. xfs_filblks_t orig_len; /* original value of len arg */
  4115. struct xfs_bmbt_irec *orig_mval; /* original value of mval */
  4116. int orig_nmap; /* original value of *nmap */
  4117. orig_bno = bno;
  4118. orig_len = len;
  4119. orig_flags = flags;
  4120. orig_mval = mval;
  4121. orig_nmap = *nmap;
  4122. #endif
  4123. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4124. XFS_ATTR_FORK : XFS_DATA_FORK;
  4125. ASSERT(*nmap >= 1);
  4126. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  4127. ASSERT(!(flags & XFS_BMAPI_IGSTATE));
  4128. ASSERT(tp != NULL);
  4129. ASSERT(len > 0);
  4130. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
  4131. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4132. if (unlikely(XFS_TEST_ERROR(
  4133. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4134. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  4135. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4136. XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
  4137. return -EFSCORRUPTED;
  4138. }
  4139. if (XFS_FORCED_SHUTDOWN(mp))
  4140. return -EIO;
  4141. ifp = XFS_IFORK_PTR(ip, whichfork);
  4142. XFS_STATS_INC(xs_blk_mapw);
  4143. if (*firstblock == NULLFSBLOCK) {
  4144. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4145. bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4146. else
  4147. bma.minleft = 1;
  4148. } else {
  4149. bma.minleft = 0;
  4150. }
  4151. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4152. error = xfs_iread_extents(tp, ip, whichfork);
  4153. if (error)
  4154. goto error0;
  4155. }
  4156. xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
  4157. &bma.prev);
  4158. n = 0;
  4159. end = bno + len;
  4160. obno = bno;
  4161. bma.tp = tp;
  4162. bma.ip = ip;
  4163. bma.total = total;
  4164. bma.userdata = 0;
  4165. bma.flist = flist;
  4166. bma.firstblock = firstblock;
  4167. while (bno < end && n < *nmap) {
  4168. inhole = eof || bma.got.br_startoff > bno;
  4169. wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
  4170. /*
  4171. * First, deal with the hole before the allocated space
  4172. * that we found, if any.
  4173. */
  4174. if (inhole || wasdelay) {
  4175. bma.eof = eof;
  4176. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4177. bma.wasdel = wasdelay;
  4178. bma.offset = bno;
  4179. bma.flags = flags;
  4180. /*
  4181. * There's a 32/64 bit type mismatch between the
  4182. * allocation length request (which can be 64 bits in
  4183. * length) and the bma length request, which is
  4184. * xfs_extlen_t and therefore 32 bits. Hence we have to
  4185. * check for 32-bit overflows and handle them here.
  4186. */
  4187. if (len > (xfs_filblks_t)MAXEXTLEN)
  4188. bma.length = MAXEXTLEN;
  4189. else
  4190. bma.length = len;
  4191. ASSERT(len > 0);
  4192. ASSERT(bma.length > 0);
  4193. error = xfs_bmapi_allocate(&bma);
  4194. if (error)
  4195. goto error0;
  4196. if (bma.blkno == NULLFSBLOCK)
  4197. break;
  4198. }
  4199. /* Deal with the allocated space we found. */
  4200. xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
  4201. end, n, flags);
  4202. /* Execute unwritten extent conversion if necessary */
  4203. error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
  4204. if (error == -EAGAIN)
  4205. continue;
  4206. if (error)
  4207. goto error0;
  4208. /* update the extent map to return */
  4209. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4210. /*
  4211. * If we're done, stop now. Stop when we've allocated
  4212. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4213. * the transaction may get too big.
  4214. */
  4215. if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
  4216. break;
  4217. /* Else go on to the next record. */
  4218. bma.prev = bma.got;
  4219. if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
  4220. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
  4221. &bma.got);
  4222. } else
  4223. eof = 1;
  4224. }
  4225. *nmap = n;
  4226. /*
  4227. * Transform from btree to extents, give it cur.
  4228. */
  4229. if (xfs_bmap_wants_extents(ip, whichfork)) {
  4230. int tmp_logflags = 0;
  4231. ASSERT(bma.cur);
  4232. error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
  4233. &tmp_logflags, whichfork);
  4234. bma.logflags |= tmp_logflags;
  4235. if (error)
  4236. goto error0;
  4237. }
  4238. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4239. XFS_IFORK_NEXTENTS(ip, whichfork) >
  4240. XFS_IFORK_MAXEXT(ip, whichfork));
  4241. error = 0;
  4242. error0:
  4243. /*
  4244. * Log everything. Do this after conversion, there's no point in
  4245. * logging the extent records if we've converted to btree format.
  4246. */
  4247. if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
  4248. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4249. bma.logflags &= ~xfs_ilog_fext(whichfork);
  4250. else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
  4251. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4252. bma.logflags &= ~xfs_ilog_fbroot(whichfork);
  4253. /*
  4254. * Log whatever the flags say, even if error. Otherwise we might miss
  4255. * detecting a case where the data is changed, there's an error,
  4256. * and it's not logged so we don't shutdown when we should.
  4257. */
  4258. if (bma.logflags)
  4259. xfs_trans_log_inode(tp, ip, bma.logflags);
  4260. if (bma.cur) {
  4261. if (!error) {
  4262. ASSERT(*firstblock == NULLFSBLOCK ||
  4263. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4264. XFS_FSB_TO_AGNO(mp,
  4265. bma.cur->bc_private.b.firstblock) ||
  4266. (flist->xbf_low &&
  4267. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4268. XFS_FSB_TO_AGNO(mp,
  4269. bma.cur->bc_private.b.firstblock)));
  4270. *firstblock = bma.cur->bc_private.b.firstblock;
  4271. }
  4272. xfs_btree_del_cursor(bma.cur,
  4273. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4274. }
  4275. if (!error)
  4276. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4277. orig_nmap, *nmap);
  4278. return error;
  4279. }
  4280. /*
  4281. * Called by xfs_bmapi to update file extent records and the btree
  4282. * after removing space (or undoing a delayed allocation).
  4283. */
  4284. STATIC int /* error */
  4285. xfs_bmap_del_extent(
  4286. xfs_inode_t *ip, /* incore inode pointer */
  4287. xfs_trans_t *tp, /* current transaction pointer */
  4288. xfs_extnum_t *idx, /* extent number to update/delete */
  4289. xfs_bmap_free_t *flist, /* list of extents to be freed */
  4290. xfs_btree_cur_t *cur, /* if null, not a btree */
  4291. xfs_bmbt_irec_t *del, /* data to remove from extents */
  4292. int *logflagsp, /* inode logging flags */
  4293. int whichfork) /* data or attr fork */
  4294. {
  4295. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  4296. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  4297. xfs_fsblock_t del_endblock=0; /* first block past del */
  4298. xfs_fileoff_t del_endoff; /* first offset past del */
  4299. int delay; /* current block is delayed allocated */
  4300. int do_fx; /* free extent at end of routine */
  4301. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  4302. int error; /* error return value */
  4303. int flags; /* inode logging flags */
  4304. xfs_bmbt_irec_t got; /* current extent entry */
  4305. xfs_fileoff_t got_endoff; /* first offset past got */
  4306. int i; /* temp state */
  4307. xfs_ifork_t *ifp; /* inode fork pointer */
  4308. xfs_mount_t *mp; /* mount structure */
  4309. xfs_filblks_t nblks; /* quota/sb block count */
  4310. xfs_bmbt_irec_t new; /* new record to be inserted */
  4311. /* REFERENCED */
  4312. uint qfield; /* quota field to update */
  4313. xfs_filblks_t temp; /* for indirect length calculations */
  4314. xfs_filblks_t temp2; /* for indirect length calculations */
  4315. int state = 0;
  4316. XFS_STATS_INC(xs_del_exlist);
  4317. if (whichfork == XFS_ATTR_FORK)
  4318. state |= BMAP_ATTRFORK;
  4319. mp = ip->i_mount;
  4320. ifp = XFS_IFORK_PTR(ip, whichfork);
  4321. ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
  4322. (uint)sizeof(xfs_bmbt_rec_t)));
  4323. ASSERT(del->br_blockcount > 0);
  4324. ep = xfs_iext_get_ext(ifp, *idx);
  4325. xfs_bmbt_get_all(ep, &got);
  4326. ASSERT(got.br_startoff <= del->br_startoff);
  4327. del_endoff = del->br_startoff + del->br_blockcount;
  4328. got_endoff = got.br_startoff + got.br_blockcount;
  4329. ASSERT(got_endoff >= del_endoff);
  4330. delay = isnullstartblock(got.br_startblock);
  4331. ASSERT(isnullstartblock(del->br_startblock) == delay);
  4332. flags = 0;
  4333. qfield = 0;
  4334. error = 0;
  4335. /*
  4336. * If deleting a real allocation, must free up the disk space.
  4337. */
  4338. if (!delay) {
  4339. flags = XFS_ILOG_CORE;
  4340. /*
  4341. * Realtime allocation. Free it and record di_nblocks update.
  4342. */
  4343. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  4344. xfs_fsblock_t bno;
  4345. xfs_filblks_t len;
  4346. ASSERT(do_mod(del->br_blockcount,
  4347. mp->m_sb.sb_rextsize) == 0);
  4348. ASSERT(do_mod(del->br_startblock,
  4349. mp->m_sb.sb_rextsize) == 0);
  4350. bno = del->br_startblock;
  4351. len = del->br_blockcount;
  4352. do_div(bno, mp->m_sb.sb_rextsize);
  4353. do_div(len, mp->m_sb.sb_rextsize);
  4354. error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
  4355. if (error)
  4356. goto done;
  4357. do_fx = 0;
  4358. nblks = len * mp->m_sb.sb_rextsize;
  4359. qfield = XFS_TRANS_DQ_RTBCOUNT;
  4360. }
  4361. /*
  4362. * Ordinary allocation.
  4363. */
  4364. else {
  4365. do_fx = 1;
  4366. nblks = del->br_blockcount;
  4367. qfield = XFS_TRANS_DQ_BCOUNT;
  4368. }
  4369. /*
  4370. * Set up del_endblock and cur for later.
  4371. */
  4372. del_endblock = del->br_startblock + del->br_blockcount;
  4373. if (cur) {
  4374. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  4375. got.br_startblock, got.br_blockcount,
  4376. &i)))
  4377. goto done;
  4378. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4379. }
  4380. da_old = da_new = 0;
  4381. } else {
  4382. da_old = startblockval(got.br_startblock);
  4383. da_new = 0;
  4384. nblks = 0;
  4385. do_fx = 0;
  4386. }
  4387. /*
  4388. * Set flag value to use in switch statement.
  4389. * Left-contig is 2, right-contig is 1.
  4390. */
  4391. switch (((got.br_startoff == del->br_startoff) << 1) |
  4392. (got_endoff == del_endoff)) {
  4393. case 3:
  4394. /*
  4395. * Matches the whole extent. Delete the entry.
  4396. */
  4397. xfs_iext_remove(ip, *idx, 1,
  4398. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  4399. --*idx;
  4400. if (delay)
  4401. break;
  4402. XFS_IFORK_NEXT_SET(ip, whichfork,
  4403. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  4404. flags |= XFS_ILOG_CORE;
  4405. if (!cur) {
  4406. flags |= xfs_ilog_fext(whichfork);
  4407. break;
  4408. }
  4409. if ((error = xfs_btree_delete(cur, &i)))
  4410. goto done;
  4411. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4412. break;
  4413. case 2:
  4414. /*
  4415. * Deleting the first part of the extent.
  4416. */
  4417. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4418. xfs_bmbt_set_startoff(ep, del_endoff);
  4419. temp = got.br_blockcount - del->br_blockcount;
  4420. xfs_bmbt_set_blockcount(ep, temp);
  4421. if (delay) {
  4422. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4423. da_old);
  4424. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4425. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4426. da_new = temp;
  4427. break;
  4428. }
  4429. xfs_bmbt_set_startblock(ep, del_endblock);
  4430. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4431. if (!cur) {
  4432. flags |= xfs_ilog_fext(whichfork);
  4433. break;
  4434. }
  4435. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  4436. got.br_blockcount - del->br_blockcount,
  4437. got.br_state)))
  4438. goto done;
  4439. break;
  4440. case 1:
  4441. /*
  4442. * Deleting the last part of the extent.
  4443. */
  4444. temp = got.br_blockcount - del->br_blockcount;
  4445. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4446. xfs_bmbt_set_blockcount(ep, temp);
  4447. if (delay) {
  4448. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4449. da_old);
  4450. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4451. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4452. da_new = temp;
  4453. break;
  4454. }
  4455. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4456. if (!cur) {
  4457. flags |= xfs_ilog_fext(whichfork);
  4458. break;
  4459. }
  4460. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  4461. got.br_startblock,
  4462. got.br_blockcount - del->br_blockcount,
  4463. got.br_state)))
  4464. goto done;
  4465. break;
  4466. case 0:
  4467. /*
  4468. * Deleting the middle of the extent.
  4469. */
  4470. temp = del->br_startoff - got.br_startoff;
  4471. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4472. xfs_bmbt_set_blockcount(ep, temp);
  4473. new.br_startoff = del_endoff;
  4474. temp2 = got_endoff - del_endoff;
  4475. new.br_blockcount = temp2;
  4476. new.br_state = got.br_state;
  4477. if (!delay) {
  4478. new.br_startblock = del_endblock;
  4479. flags |= XFS_ILOG_CORE;
  4480. if (cur) {
  4481. if ((error = xfs_bmbt_update(cur,
  4482. got.br_startoff,
  4483. got.br_startblock, temp,
  4484. got.br_state)))
  4485. goto done;
  4486. if ((error = xfs_btree_increment(cur, 0, &i)))
  4487. goto done;
  4488. cur->bc_rec.b = new;
  4489. error = xfs_btree_insert(cur, &i);
  4490. if (error && error != -ENOSPC)
  4491. goto done;
  4492. /*
  4493. * If get no-space back from btree insert,
  4494. * it tried a split, and we have a zero
  4495. * block reservation.
  4496. * Fix up our state and return the error.
  4497. */
  4498. if (error == -ENOSPC) {
  4499. /*
  4500. * Reset the cursor, don't trust
  4501. * it after any insert operation.
  4502. */
  4503. if ((error = xfs_bmbt_lookup_eq(cur,
  4504. got.br_startoff,
  4505. got.br_startblock,
  4506. temp, &i)))
  4507. goto done;
  4508. XFS_WANT_CORRUPTED_GOTO(mp,
  4509. i == 1, done);
  4510. /*
  4511. * Update the btree record back
  4512. * to the original value.
  4513. */
  4514. if ((error = xfs_bmbt_update(cur,
  4515. got.br_startoff,
  4516. got.br_startblock,
  4517. got.br_blockcount,
  4518. got.br_state)))
  4519. goto done;
  4520. /*
  4521. * Reset the extent record back
  4522. * to the original value.
  4523. */
  4524. xfs_bmbt_set_blockcount(ep,
  4525. got.br_blockcount);
  4526. flags = 0;
  4527. error = -ENOSPC;
  4528. goto done;
  4529. }
  4530. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4531. } else
  4532. flags |= xfs_ilog_fext(whichfork);
  4533. XFS_IFORK_NEXT_SET(ip, whichfork,
  4534. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  4535. } else {
  4536. ASSERT(whichfork == XFS_DATA_FORK);
  4537. temp = xfs_bmap_worst_indlen(ip, temp);
  4538. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4539. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  4540. new.br_startblock = nullstartblock((int)temp2);
  4541. da_new = temp + temp2;
  4542. while (da_new > da_old) {
  4543. if (temp) {
  4544. temp--;
  4545. da_new--;
  4546. xfs_bmbt_set_startblock(ep,
  4547. nullstartblock((int)temp));
  4548. }
  4549. if (da_new == da_old)
  4550. break;
  4551. if (temp2) {
  4552. temp2--;
  4553. da_new--;
  4554. new.br_startblock =
  4555. nullstartblock((int)temp2);
  4556. }
  4557. }
  4558. }
  4559. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4560. xfs_iext_insert(ip, *idx + 1, 1, &new, state);
  4561. ++*idx;
  4562. break;
  4563. }
  4564. /*
  4565. * If we need to, add to list of extents to delete.
  4566. */
  4567. if (do_fx)
  4568. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  4569. mp);
  4570. /*
  4571. * Adjust inode # blocks in the file.
  4572. */
  4573. if (nblks)
  4574. ip->i_d.di_nblocks -= nblks;
  4575. /*
  4576. * Adjust quota data.
  4577. */
  4578. if (qfield)
  4579. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  4580. /*
  4581. * Account for change in delayed indirect blocks.
  4582. * Nothing to do for disk quota accounting here.
  4583. */
  4584. ASSERT(da_old >= da_new);
  4585. if (da_old > da_new)
  4586. xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
  4587. done:
  4588. *logflagsp = flags;
  4589. return error;
  4590. }
  4591. /*
  4592. * Unmap (remove) blocks from a file.
  4593. * If nexts is nonzero then the number of extents to remove is limited to
  4594. * that value. If not all extents in the block range can be removed then
  4595. * *done is set.
  4596. */
  4597. int /* error */
  4598. xfs_bunmapi(
  4599. xfs_trans_t *tp, /* transaction pointer */
  4600. struct xfs_inode *ip, /* incore inode */
  4601. xfs_fileoff_t bno, /* starting offset to unmap */
  4602. xfs_filblks_t len, /* length to unmap in file */
  4603. int flags, /* misc flags */
  4604. xfs_extnum_t nexts, /* number of extents max */
  4605. xfs_fsblock_t *firstblock, /* first allocated block
  4606. controls a.g. for allocs */
  4607. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4608. int *done) /* set if not done yet */
  4609. {
  4610. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4611. xfs_bmbt_irec_t del; /* extent being deleted */
  4612. int eof; /* is deleting at eof */
  4613. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4614. int error; /* error return value */
  4615. xfs_extnum_t extno; /* extent number in list */
  4616. xfs_bmbt_irec_t got; /* current extent record */
  4617. xfs_ifork_t *ifp; /* inode fork pointer */
  4618. int isrt; /* freeing in rt area */
  4619. xfs_extnum_t lastx; /* last extent index used */
  4620. int logflags; /* transaction logging flags */
  4621. xfs_extlen_t mod; /* rt extent offset */
  4622. xfs_mount_t *mp; /* mount structure */
  4623. xfs_extnum_t nextents; /* number of file extents */
  4624. xfs_bmbt_irec_t prev; /* previous extent record */
  4625. xfs_fileoff_t start; /* first file offset deleted */
  4626. int tmp_logflags; /* partial logging flags */
  4627. int wasdel; /* was a delayed alloc extent */
  4628. int whichfork; /* data or attribute fork */
  4629. xfs_fsblock_t sum;
  4630. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4631. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4632. XFS_ATTR_FORK : XFS_DATA_FORK;
  4633. ifp = XFS_IFORK_PTR(ip, whichfork);
  4634. if (unlikely(
  4635. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4636. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4637. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4638. ip->i_mount);
  4639. return -EFSCORRUPTED;
  4640. }
  4641. mp = ip->i_mount;
  4642. if (XFS_FORCED_SHUTDOWN(mp))
  4643. return -EIO;
  4644. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4645. ASSERT(len > 0);
  4646. ASSERT(nexts >= 0);
  4647. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4648. (error = xfs_iread_extents(tp, ip, whichfork)))
  4649. return error;
  4650. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4651. if (nextents == 0) {
  4652. *done = 1;
  4653. return 0;
  4654. }
  4655. XFS_STATS_INC(xs_blk_unmap);
  4656. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4657. start = bno;
  4658. bno = start + len - 1;
  4659. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4660. &prev);
  4661. /*
  4662. * Check to see if the given block number is past the end of the
  4663. * file, back up to the last block if so...
  4664. */
  4665. if (eof) {
  4666. ep = xfs_iext_get_ext(ifp, --lastx);
  4667. xfs_bmbt_get_all(ep, &got);
  4668. bno = got.br_startoff + got.br_blockcount - 1;
  4669. }
  4670. logflags = 0;
  4671. if (ifp->if_flags & XFS_IFBROOT) {
  4672. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  4673. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  4674. cur->bc_private.b.firstblock = *firstblock;
  4675. cur->bc_private.b.flist = flist;
  4676. cur->bc_private.b.flags = 0;
  4677. } else
  4678. cur = NULL;
  4679. if (isrt) {
  4680. /*
  4681. * Synchronize by locking the bitmap inode.
  4682. */
  4683. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  4684. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  4685. }
  4686. extno = 0;
  4687. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  4688. (nexts == 0 || extno < nexts)) {
  4689. /*
  4690. * Is the found extent after a hole in which bno lives?
  4691. * Just back up to the previous extent, if so.
  4692. */
  4693. if (got.br_startoff > bno) {
  4694. if (--lastx < 0)
  4695. break;
  4696. ep = xfs_iext_get_ext(ifp, lastx);
  4697. xfs_bmbt_get_all(ep, &got);
  4698. }
  4699. /*
  4700. * Is the last block of this extent before the range
  4701. * we're supposed to delete? If so, we're done.
  4702. */
  4703. bno = XFS_FILEOFF_MIN(bno,
  4704. got.br_startoff + got.br_blockcount - 1);
  4705. if (bno < start)
  4706. break;
  4707. /*
  4708. * Then deal with the (possibly delayed) allocated space
  4709. * we found.
  4710. */
  4711. ASSERT(ep != NULL);
  4712. del = got;
  4713. wasdel = isnullstartblock(del.br_startblock);
  4714. if (got.br_startoff < start) {
  4715. del.br_startoff = start;
  4716. del.br_blockcount -= start - got.br_startoff;
  4717. if (!wasdel)
  4718. del.br_startblock += start - got.br_startoff;
  4719. }
  4720. if (del.br_startoff + del.br_blockcount > bno + 1)
  4721. del.br_blockcount = bno + 1 - del.br_startoff;
  4722. sum = del.br_startblock + del.br_blockcount;
  4723. if (isrt &&
  4724. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  4725. /*
  4726. * Realtime extent not lined up at the end.
  4727. * The extent could have been split into written
  4728. * and unwritten pieces, or we could just be
  4729. * unmapping part of it. But we can't really
  4730. * get rid of part of a realtime extent.
  4731. */
  4732. if (del.br_state == XFS_EXT_UNWRITTEN ||
  4733. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4734. /*
  4735. * This piece is unwritten, or we're not
  4736. * using unwritten extents. Skip over it.
  4737. */
  4738. ASSERT(bno >= mod);
  4739. bno -= mod > del.br_blockcount ?
  4740. del.br_blockcount : mod;
  4741. if (bno < got.br_startoff) {
  4742. if (--lastx >= 0)
  4743. xfs_bmbt_get_all(xfs_iext_get_ext(
  4744. ifp, lastx), &got);
  4745. }
  4746. continue;
  4747. }
  4748. /*
  4749. * It's written, turn it unwritten.
  4750. * This is better than zeroing it.
  4751. */
  4752. ASSERT(del.br_state == XFS_EXT_NORM);
  4753. ASSERT(xfs_trans_get_block_res(tp) > 0);
  4754. /*
  4755. * If this spans a realtime extent boundary,
  4756. * chop it back to the start of the one we end at.
  4757. */
  4758. if (del.br_blockcount > mod) {
  4759. del.br_startoff += del.br_blockcount - mod;
  4760. del.br_startblock += del.br_blockcount - mod;
  4761. del.br_blockcount = mod;
  4762. }
  4763. del.br_state = XFS_EXT_UNWRITTEN;
  4764. error = xfs_bmap_add_extent_unwritten_real(tp, ip,
  4765. &lastx, &cur, &del, firstblock, flist,
  4766. &logflags);
  4767. if (error)
  4768. goto error0;
  4769. goto nodelete;
  4770. }
  4771. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  4772. /*
  4773. * Realtime extent is lined up at the end but not
  4774. * at the front. We'll get rid of full extents if
  4775. * we can.
  4776. */
  4777. mod = mp->m_sb.sb_rextsize - mod;
  4778. if (del.br_blockcount > mod) {
  4779. del.br_blockcount -= mod;
  4780. del.br_startoff += mod;
  4781. del.br_startblock += mod;
  4782. } else if ((del.br_startoff == start &&
  4783. (del.br_state == XFS_EXT_UNWRITTEN ||
  4784. xfs_trans_get_block_res(tp) == 0)) ||
  4785. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4786. /*
  4787. * Can't make it unwritten. There isn't
  4788. * a full extent here so just skip it.
  4789. */
  4790. ASSERT(bno >= del.br_blockcount);
  4791. bno -= del.br_blockcount;
  4792. if (got.br_startoff > bno) {
  4793. if (--lastx >= 0) {
  4794. ep = xfs_iext_get_ext(ifp,
  4795. lastx);
  4796. xfs_bmbt_get_all(ep, &got);
  4797. }
  4798. }
  4799. continue;
  4800. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  4801. /*
  4802. * This one is already unwritten.
  4803. * It must have a written left neighbor.
  4804. * Unwrite the killed part of that one and
  4805. * try again.
  4806. */
  4807. ASSERT(lastx > 0);
  4808. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  4809. lastx - 1), &prev);
  4810. ASSERT(prev.br_state == XFS_EXT_NORM);
  4811. ASSERT(!isnullstartblock(prev.br_startblock));
  4812. ASSERT(del.br_startblock ==
  4813. prev.br_startblock + prev.br_blockcount);
  4814. if (prev.br_startoff < start) {
  4815. mod = start - prev.br_startoff;
  4816. prev.br_blockcount -= mod;
  4817. prev.br_startblock += mod;
  4818. prev.br_startoff = start;
  4819. }
  4820. prev.br_state = XFS_EXT_UNWRITTEN;
  4821. lastx--;
  4822. error = xfs_bmap_add_extent_unwritten_real(tp,
  4823. ip, &lastx, &cur, &prev,
  4824. firstblock, flist, &logflags);
  4825. if (error)
  4826. goto error0;
  4827. goto nodelete;
  4828. } else {
  4829. ASSERT(del.br_state == XFS_EXT_NORM);
  4830. del.br_state = XFS_EXT_UNWRITTEN;
  4831. error = xfs_bmap_add_extent_unwritten_real(tp,
  4832. ip, &lastx, &cur, &del,
  4833. firstblock, flist, &logflags);
  4834. if (error)
  4835. goto error0;
  4836. goto nodelete;
  4837. }
  4838. }
  4839. if (wasdel) {
  4840. ASSERT(startblockval(del.br_startblock) > 0);
  4841. /* Update realtime/data freespace, unreserve quota */
  4842. if (isrt) {
  4843. xfs_filblks_t rtexts;
  4844. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  4845. do_div(rtexts, mp->m_sb.sb_rextsize);
  4846. xfs_mod_frextents(mp, (int64_t)rtexts);
  4847. (void)xfs_trans_reserve_quota_nblks(NULL,
  4848. ip, -((long)del.br_blockcount), 0,
  4849. XFS_QMOPT_RES_RTBLKS);
  4850. } else {
  4851. xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
  4852. false);
  4853. (void)xfs_trans_reserve_quota_nblks(NULL,
  4854. ip, -((long)del.br_blockcount), 0,
  4855. XFS_QMOPT_RES_REGBLKS);
  4856. }
  4857. ip->i_delayed_blks -= del.br_blockcount;
  4858. if (cur)
  4859. cur->bc_private.b.flags |=
  4860. XFS_BTCUR_BPRV_WASDEL;
  4861. } else if (cur)
  4862. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  4863. /*
  4864. * If it's the case where the directory code is running
  4865. * with no block reservation, and the deleted block is in
  4866. * the middle of its extent, and the resulting insert
  4867. * of an extent would cause transformation to btree format,
  4868. * then reject it. The calling code will then swap
  4869. * blocks around instead.
  4870. * We have to do this now, rather than waiting for the
  4871. * conversion to btree format, since the transaction
  4872. * will be dirty.
  4873. */
  4874. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  4875. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  4876. XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
  4877. XFS_IFORK_MAXEXT(ip, whichfork) &&
  4878. del.br_startoff > got.br_startoff &&
  4879. del.br_startoff + del.br_blockcount <
  4880. got.br_startoff + got.br_blockcount) {
  4881. error = -ENOSPC;
  4882. goto error0;
  4883. }
  4884. error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
  4885. &tmp_logflags, whichfork);
  4886. logflags |= tmp_logflags;
  4887. if (error)
  4888. goto error0;
  4889. bno = del.br_startoff - 1;
  4890. nodelete:
  4891. /*
  4892. * If not done go on to the next (previous) record.
  4893. */
  4894. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  4895. if (lastx >= 0) {
  4896. ep = xfs_iext_get_ext(ifp, lastx);
  4897. if (xfs_bmbt_get_startoff(ep) > bno) {
  4898. if (--lastx >= 0)
  4899. ep = xfs_iext_get_ext(ifp,
  4900. lastx);
  4901. }
  4902. xfs_bmbt_get_all(ep, &got);
  4903. }
  4904. extno++;
  4905. }
  4906. }
  4907. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  4908. /*
  4909. * Convert to a btree if necessary.
  4910. */
  4911. if (xfs_bmap_needs_btree(ip, whichfork)) {
  4912. ASSERT(cur == NULL);
  4913. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  4914. &cur, 0, &tmp_logflags, whichfork);
  4915. logflags |= tmp_logflags;
  4916. if (error)
  4917. goto error0;
  4918. }
  4919. /*
  4920. * transform from btree to extents, give it cur
  4921. */
  4922. else if (xfs_bmap_wants_extents(ip, whichfork)) {
  4923. ASSERT(cur != NULL);
  4924. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  4925. whichfork);
  4926. logflags |= tmp_logflags;
  4927. if (error)
  4928. goto error0;
  4929. }
  4930. /*
  4931. * transform from extents to local?
  4932. */
  4933. error = 0;
  4934. error0:
  4935. /*
  4936. * Log everything. Do this after conversion, there's no point in
  4937. * logging the extent records if we've converted to btree format.
  4938. */
  4939. if ((logflags & xfs_ilog_fext(whichfork)) &&
  4940. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4941. logflags &= ~xfs_ilog_fext(whichfork);
  4942. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  4943. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4944. logflags &= ~xfs_ilog_fbroot(whichfork);
  4945. /*
  4946. * Log inode even in the error case, if the transaction
  4947. * is dirty we'll need to shut down the filesystem.
  4948. */
  4949. if (logflags)
  4950. xfs_trans_log_inode(tp, ip, logflags);
  4951. if (cur) {
  4952. if (!error) {
  4953. *firstblock = cur->bc_private.b.firstblock;
  4954. cur->bc_private.b.allocated = 0;
  4955. }
  4956. xfs_btree_del_cursor(cur,
  4957. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4958. }
  4959. return error;
  4960. }
  4961. /*
  4962. * Determine whether an extent shift can be accomplished by a merge with the
  4963. * extent that precedes the target hole of the shift.
  4964. */
  4965. STATIC bool
  4966. xfs_bmse_can_merge(
  4967. struct xfs_bmbt_irec *left, /* preceding extent */
  4968. struct xfs_bmbt_irec *got, /* current extent to shift */
  4969. xfs_fileoff_t shift) /* shift fsb */
  4970. {
  4971. xfs_fileoff_t startoff;
  4972. startoff = got->br_startoff - shift;
  4973. /*
  4974. * The extent, once shifted, must be adjacent in-file and on-disk with
  4975. * the preceding extent.
  4976. */
  4977. if ((left->br_startoff + left->br_blockcount != startoff) ||
  4978. (left->br_startblock + left->br_blockcount != got->br_startblock) ||
  4979. (left->br_state != got->br_state) ||
  4980. (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
  4981. return false;
  4982. return true;
  4983. }
  4984. /*
  4985. * A bmap extent shift adjusts the file offset of an extent to fill a preceding
  4986. * hole in the file. If an extent shift would result in the extent being fully
  4987. * adjacent to the extent that currently precedes the hole, we can merge with
  4988. * the preceding extent rather than do the shift.
  4989. *
  4990. * This function assumes the caller has verified a shift-by-merge is possible
  4991. * with the provided extents via xfs_bmse_can_merge().
  4992. */
  4993. STATIC int
  4994. xfs_bmse_merge(
  4995. struct xfs_inode *ip,
  4996. int whichfork,
  4997. xfs_fileoff_t shift, /* shift fsb */
  4998. int current_ext, /* idx of gotp */
  4999. struct xfs_bmbt_rec_host *gotp, /* extent to shift */
  5000. struct xfs_bmbt_rec_host *leftp, /* preceding extent */
  5001. struct xfs_btree_cur *cur,
  5002. int *logflags) /* output */
  5003. {
  5004. struct xfs_bmbt_irec got;
  5005. struct xfs_bmbt_irec left;
  5006. xfs_filblks_t blockcount;
  5007. int error, i;
  5008. struct xfs_mount *mp = ip->i_mount;
  5009. xfs_bmbt_get_all(gotp, &got);
  5010. xfs_bmbt_get_all(leftp, &left);
  5011. blockcount = left.br_blockcount + got.br_blockcount;
  5012. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5013. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5014. ASSERT(xfs_bmse_can_merge(&left, &got, shift));
  5015. /*
  5016. * Merge the in-core extents. Note that the host record pointers and
  5017. * current_ext index are invalid once the extent has been removed via
  5018. * xfs_iext_remove().
  5019. */
  5020. xfs_bmbt_set_blockcount(leftp, blockcount);
  5021. xfs_iext_remove(ip, current_ext, 1, 0);
  5022. /*
  5023. * Update the on-disk extent count, the btree if necessary and log the
  5024. * inode.
  5025. */
  5026. XFS_IFORK_NEXT_SET(ip, whichfork,
  5027. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  5028. *logflags |= XFS_ILOG_CORE;
  5029. if (!cur) {
  5030. *logflags |= XFS_ILOG_DEXT;
  5031. return 0;
  5032. }
  5033. /* lookup and remove the extent to merge */
  5034. error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
  5035. got.br_blockcount, &i);
  5036. if (error)
  5037. return error;
  5038. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5039. error = xfs_btree_delete(cur, &i);
  5040. if (error)
  5041. return error;
  5042. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5043. /* lookup and update size of the previous extent */
  5044. error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
  5045. left.br_blockcount, &i);
  5046. if (error)
  5047. return error;
  5048. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5049. left.br_blockcount = blockcount;
  5050. return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
  5051. left.br_blockcount, left.br_state);
  5052. }
  5053. /*
  5054. * Shift a single extent.
  5055. */
  5056. STATIC int
  5057. xfs_bmse_shift_one(
  5058. struct xfs_inode *ip,
  5059. int whichfork,
  5060. xfs_fileoff_t offset_shift_fsb,
  5061. int *current_ext,
  5062. struct xfs_bmbt_rec_host *gotp,
  5063. struct xfs_btree_cur *cur,
  5064. int *logflags,
  5065. enum shift_direction direction)
  5066. {
  5067. struct xfs_ifork *ifp;
  5068. struct xfs_mount *mp;
  5069. xfs_fileoff_t startoff;
  5070. struct xfs_bmbt_rec_host *adj_irecp;
  5071. struct xfs_bmbt_irec got;
  5072. struct xfs_bmbt_irec adj_irec;
  5073. int error;
  5074. int i;
  5075. int total_extents;
  5076. mp = ip->i_mount;
  5077. ifp = XFS_IFORK_PTR(ip, whichfork);
  5078. total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
  5079. xfs_bmbt_get_all(gotp, &got);
  5080. /* delalloc extents should be prevented by caller */
  5081. XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
  5082. if (direction == SHIFT_LEFT) {
  5083. startoff = got.br_startoff - offset_shift_fsb;
  5084. /*
  5085. * Check for merge if we've got an extent to the left,
  5086. * otherwise make sure there's enough room at the start
  5087. * of the file for the shift.
  5088. */
  5089. if (!*current_ext) {
  5090. if (got.br_startoff < offset_shift_fsb)
  5091. return -EINVAL;
  5092. goto update_current_ext;
  5093. }
  5094. /*
  5095. * grab the left extent and check for a large
  5096. * enough hole.
  5097. */
  5098. adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
  5099. xfs_bmbt_get_all(adj_irecp, &adj_irec);
  5100. if (startoff <
  5101. adj_irec.br_startoff + adj_irec.br_blockcount)
  5102. return -EINVAL;
  5103. /* check whether to merge the extent or shift it down */
  5104. if (xfs_bmse_can_merge(&adj_irec, &got,
  5105. offset_shift_fsb)) {
  5106. return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
  5107. *current_ext, gotp, adj_irecp,
  5108. cur, logflags);
  5109. }
  5110. } else {
  5111. startoff = got.br_startoff + offset_shift_fsb;
  5112. /* nothing to move if this is the last extent */
  5113. if (*current_ext >= (total_extents - 1))
  5114. goto update_current_ext;
  5115. /*
  5116. * If this is not the last extent in the file, make sure there
  5117. * is enough room between current extent and next extent for
  5118. * accommodating the shift.
  5119. */
  5120. adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
  5121. xfs_bmbt_get_all(adj_irecp, &adj_irec);
  5122. if (startoff + got.br_blockcount > adj_irec.br_startoff)
  5123. return -EINVAL;
  5124. /*
  5125. * Unlike a left shift (which involves a hole punch),
  5126. * a right shift does not modify extent neighbors
  5127. * in any way. We should never find mergeable extents
  5128. * in this scenario. Check anyways and warn if we
  5129. * encounter two extents that could be one.
  5130. */
  5131. if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
  5132. WARN_ON_ONCE(1);
  5133. }
  5134. /*
  5135. * Increment the extent index for the next iteration, update the start
  5136. * offset of the in-core extent and update the btree if applicable.
  5137. */
  5138. update_current_ext:
  5139. if (direction == SHIFT_LEFT)
  5140. (*current_ext)++;
  5141. else
  5142. (*current_ext)--;
  5143. xfs_bmbt_set_startoff(gotp, startoff);
  5144. *logflags |= XFS_ILOG_CORE;
  5145. if (!cur) {
  5146. *logflags |= XFS_ILOG_DEXT;
  5147. return 0;
  5148. }
  5149. error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
  5150. got.br_blockcount, &i);
  5151. if (error)
  5152. return error;
  5153. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5154. got.br_startoff = startoff;
  5155. return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
  5156. got.br_blockcount, got.br_state);
  5157. }
  5158. /*
  5159. * Shift extent records to the left/right to cover/create a hole.
  5160. *
  5161. * The maximum number of extents to be shifted in a single operation is
  5162. * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
  5163. * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
  5164. * is the length by which each extent is shifted. If there is no hole to shift
  5165. * the extents into, this will be considered invalid operation and we abort
  5166. * immediately.
  5167. */
  5168. int
  5169. xfs_bmap_shift_extents(
  5170. struct xfs_trans *tp,
  5171. struct xfs_inode *ip,
  5172. xfs_fileoff_t *next_fsb,
  5173. xfs_fileoff_t offset_shift_fsb,
  5174. int *done,
  5175. xfs_fileoff_t stop_fsb,
  5176. xfs_fsblock_t *firstblock,
  5177. struct xfs_bmap_free *flist,
  5178. enum shift_direction direction,
  5179. int num_exts)
  5180. {
  5181. struct xfs_btree_cur *cur = NULL;
  5182. struct xfs_bmbt_rec_host *gotp;
  5183. struct xfs_bmbt_irec got;
  5184. struct xfs_mount *mp = ip->i_mount;
  5185. struct xfs_ifork *ifp;
  5186. xfs_extnum_t nexts = 0;
  5187. xfs_extnum_t current_ext;
  5188. xfs_extnum_t total_extents;
  5189. xfs_extnum_t stop_extent;
  5190. int error = 0;
  5191. int whichfork = XFS_DATA_FORK;
  5192. int logflags = 0;
  5193. if (unlikely(XFS_TEST_ERROR(
  5194. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5195. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5196. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  5197. XFS_ERROR_REPORT("xfs_bmap_shift_extents",
  5198. XFS_ERRLEVEL_LOW, mp);
  5199. return -EFSCORRUPTED;
  5200. }
  5201. if (XFS_FORCED_SHUTDOWN(mp))
  5202. return -EIO;
  5203. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5204. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5205. ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
  5206. ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
  5207. ifp = XFS_IFORK_PTR(ip, whichfork);
  5208. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5209. /* Read in all the extents */
  5210. error = xfs_iread_extents(tp, ip, whichfork);
  5211. if (error)
  5212. return error;
  5213. }
  5214. if (ifp->if_flags & XFS_IFBROOT) {
  5215. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5216. cur->bc_private.b.firstblock = *firstblock;
  5217. cur->bc_private.b.flist = flist;
  5218. cur->bc_private.b.flags = 0;
  5219. }
  5220. /*
  5221. * There may be delalloc extents in the data fork before the range we
  5222. * are collapsing out, so we cannot use the count of real extents here.
  5223. * Instead we have to calculate it from the incore fork.
  5224. */
  5225. total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
  5226. if (total_extents == 0) {
  5227. *done = 1;
  5228. goto del_cursor;
  5229. }
  5230. /*
  5231. * In case of first right shift, we need to initialize next_fsb
  5232. */
  5233. if (*next_fsb == NULLFSBLOCK) {
  5234. gotp = xfs_iext_get_ext(ifp, total_extents - 1);
  5235. xfs_bmbt_get_all(gotp, &got);
  5236. *next_fsb = got.br_startoff;
  5237. if (stop_fsb > *next_fsb) {
  5238. *done = 1;
  5239. goto del_cursor;
  5240. }
  5241. }
  5242. /* Lookup the extent index at which we have to stop */
  5243. if (direction == SHIFT_RIGHT) {
  5244. gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
  5245. /* Make stop_extent exclusive of shift range */
  5246. stop_extent--;
  5247. } else
  5248. stop_extent = total_extents;
  5249. /*
  5250. * Look up the extent index for the fsb where we start shifting. We can
  5251. * henceforth iterate with current_ext as extent list changes are locked
  5252. * out via ilock.
  5253. *
  5254. * gotp can be null in 2 cases: 1) if there are no extents or 2)
  5255. * *next_fsb lies in a hole beyond which there are no extents. Either
  5256. * way, we are done.
  5257. */
  5258. gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
  5259. if (!gotp) {
  5260. *done = 1;
  5261. goto del_cursor;
  5262. }
  5263. /* some sanity checking before we finally start shifting extents */
  5264. if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
  5265. (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
  5266. error = -EIO;
  5267. goto del_cursor;
  5268. }
  5269. while (nexts++ < num_exts) {
  5270. error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
  5271. &current_ext, gotp, cur, &logflags,
  5272. direction);
  5273. if (error)
  5274. goto del_cursor;
  5275. /*
  5276. * If there was an extent merge during the shift, the extent
  5277. * count can change. Update the total and grade the next record.
  5278. */
  5279. if (direction == SHIFT_LEFT) {
  5280. total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
  5281. stop_extent = total_extents;
  5282. }
  5283. if (current_ext == stop_extent) {
  5284. *done = 1;
  5285. *next_fsb = NULLFSBLOCK;
  5286. break;
  5287. }
  5288. gotp = xfs_iext_get_ext(ifp, current_ext);
  5289. }
  5290. if (!*done) {
  5291. xfs_bmbt_get_all(gotp, &got);
  5292. *next_fsb = got.br_startoff;
  5293. }
  5294. del_cursor:
  5295. if (cur)
  5296. xfs_btree_del_cursor(cur,
  5297. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5298. if (logflags)
  5299. xfs_trans_log_inode(tp, ip, logflags);
  5300. return error;
  5301. }
  5302. /*
  5303. * Splits an extent into two extents at split_fsb block such that it is
  5304. * the first block of the current_ext. @current_ext is a target extent
  5305. * to be split. @split_fsb is a block where the extents is split.
  5306. * If split_fsb lies in a hole or the first block of extents, just return 0.
  5307. */
  5308. STATIC int
  5309. xfs_bmap_split_extent_at(
  5310. struct xfs_trans *tp,
  5311. struct xfs_inode *ip,
  5312. xfs_fileoff_t split_fsb,
  5313. xfs_fsblock_t *firstfsb,
  5314. struct xfs_bmap_free *free_list)
  5315. {
  5316. int whichfork = XFS_DATA_FORK;
  5317. struct xfs_btree_cur *cur = NULL;
  5318. struct xfs_bmbt_rec_host *gotp;
  5319. struct xfs_bmbt_irec got;
  5320. struct xfs_bmbt_irec new; /* split extent */
  5321. struct xfs_mount *mp = ip->i_mount;
  5322. struct xfs_ifork *ifp;
  5323. xfs_fsblock_t gotblkcnt; /* new block count for got */
  5324. xfs_extnum_t current_ext;
  5325. int error = 0;
  5326. int logflags = 0;
  5327. int i = 0;
  5328. if (unlikely(XFS_TEST_ERROR(
  5329. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5330. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5331. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  5332. XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
  5333. XFS_ERRLEVEL_LOW, mp);
  5334. return -EFSCORRUPTED;
  5335. }
  5336. if (XFS_FORCED_SHUTDOWN(mp))
  5337. return -EIO;
  5338. ifp = XFS_IFORK_PTR(ip, whichfork);
  5339. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5340. /* Read in all the extents */
  5341. error = xfs_iread_extents(tp, ip, whichfork);
  5342. if (error)
  5343. return error;
  5344. }
  5345. /*
  5346. * gotp can be null in 2 cases: 1) if there are no extents
  5347. * or 2) split_fsb lies in a hole beyond which there are
  5348. * no extents. Either way, we are done.
  5349. */
  5350. gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
  5351. if (!gotp)
  5352. return 0;
  5353. xfs_bmbt_get_all(gotp, &got);
  5354. /*
  5355. * Check split_fsb lies in a hole or the start boundary offset
  5356. * of the extent.
  5357. */
  5358. if (got.br_startoff >= split_fsb)
  5359. return 0;
  5360. gotblkcnt = split_fsb - got.br_startoff;
  5361. new.br_startoff = split_fsb;
  5362. new.br_startblock = got.br_startblock + gotblkcnt;
  5363. new.br_blockcount = got.br_blockcount - gotblkcnt;
  5364. new.br_state = got.br_state;
  5365. if (ifp->if_flags & XFS_IFBROOT) {
  5366. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5367. cur->bc_private.b.firstblock = *firstfsb;
  5368. cur->bc_private.b.flist = free_list;
  5369. cur->bc_private.b.flags = 0;
  5370. error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  5371. got.br_startblock,
  5372. got.br_blockcount,
  5373. &i);
  5374. if (error)
  5375. goto del_cursor;
  5376. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5377. }
  5378. xfs_bmbt_set_blockcount(gotp, gotblkcnt);
  5379. got.br_blockcount = gotblkcnt;
  5380. logflags = XFS_ILOG_CORE;
  5381. if (cur) {
  5382. error = xfs_bmbt_update(cur, got.br_startoff,
  5383. got.br_startblock,
  5384. got.br_blockcount,
  5385. got.br_state);
  5386. if (error)
  5387. goto del_cursor;
  5388. } else
  5389. logflags |= XFS_ILOG_DEXT;
  5390. /* Add new extent */
  5391. current_ext++;
  5392. xfs_iext_insert(ip, current_ext, 1, &new, 0);
  5393. XFS_IFORK_NEXT_SET(ip, whichfork,
  5394. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  5395. if (cur) {
  5396. error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
  5397. new.br_startblock, new.br_blockcount,
  5398. &i);
  5399. if (error)
  5400. goto del_cursor;
  5401. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
  5402. cur->bc_rec.b.br_state = new.br_state;
  5403. error = xfs_btree_insert(cur, &i);
  5404. if (error)
  5405. goto del_cursor;
  5406. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5407. }
  5408. /*
  5409. * Convert to a btree if necessary.
  5410. */
  5411. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5412. int tmp_logflags; /* partial log flag return val */
  5413. ASSERT(cur == NULL);
  5414. error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
  5415. &cur, 0, &tmp_logflags, whichfork);
  5416. logflags |= tmp_logflags;
  5417. }
  5418. del_cursor:
  5419. if (cur) {
  5420. cur->bc_private.b.allocated = 0;
  5421. xfs_btree_del_cursor(cur,
  5422. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5423. }
  5424. if (logflags)
  5425. xfs_trans_log_inode(tp, ip, logflags);
  5426. return error;
  5427. }
  5428. int
  5429. xfs_bmap_split_extent(
  5430. struct xfs_inode *ip,
  5431. xfs_fileoff_t split_fsb)
  5432. {
  5433. struct xfs_mount *mp = ip->i_mount;
  5434. struct xfs_trans *tp;
  5435. struct xfs_bmap_free free_list;
  5436. xfs_fsblock_t firstfsb;
  5437. int committed;
  5438. int error;
  5439. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  5440. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  5441. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
  5442. if (error) {
  5443. xfs_trans_cancel(tp);
  5444. return error;
  5445. }
  5446. xfs_ilock(ip, XFS_ILOCK_EXCL);
  5447. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  5448. xfs_bmap_init(&free_list, &firstfsb);
  5449. error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
  5450. &firstfsb, &free_list);
  5451. if (error)
  5452. goto out;
  5453. error = xfs_bmap_finish(&tp, &free_list, &committed);
  5454. if (error)
  5455. goto out;
  5456. return xfs_trans_commit(tp);
  5457. out:
  5458. xfs_trans_cancel(tp);
  5459. return error;
  5460. }