xfs_bmap.c 195 KB

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