xfs_bmap.c 197 KB

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