xfs_bmap.c 197 KB

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