xfs_bmap.c 196 KB

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