xfs_bmap.c 191 KB

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