xfs_bmap.c 194 KB

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