xfs_bmap.c 200 KB

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