xfs_bmap.c 196 KB

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