xfs_bmap.c 196 KB

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