xfs_bmap.c 167 KB

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