xfs_bmap.c 190 KB

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