xfs_bmap.c 199 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576
  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 it's 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 delta;
  2607. xfs_extlen_t longest;
  2608. xfs_extlen_t need;
  2609. xfs_extlen_t nextminlen = 0;
  2610. xfs_perag_t *pag;
  2611. int nullfb; /* true if ap->firstblock isn't set */
  2612. int isaligned;
  2613. int notinit;
  2614. int tryagain;
  2615. int error;
  2616. mp = ap->ip->i_mount;
  2617. align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
  2618. if (unlikely(align)) {
  2619. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2620. align, 0, ap->eof, 0, ap->conv,
  2621. &ap->off, &ap->alen);
  2622. ASSERT(!error);
  2623. ASSERT(ap->alen);
  2624. }
  2625. nullfb = ap->firstblock == NULLFSBLOCK;
  2626. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2627. if (nullfb) {
  2628. if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
  2629. ag = xfs_filestream_lookup_ag(ap->ip);
  2630. ag = (ag != NULLAGNUMBER) ? ag : 0;
  2631. ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
  2632. } else {
  2633. ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2634. }
  2635. } else
  2636. ap->rval = ap->firstblock;
  2637. xfs_bmap_adjacent(ap);
  2638. /*
  2639. * If allowed, use ap->rval; otherwise must use firstblock since
  2640. * it's in the right allocation group.
  2641. */
  2642. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
  2643. ;
  2644. else
  2645. ap->rval = ap->firstblock;
  2646. /*
  2647. * Normal allocation, done through xfs_alloc_vextent.
  2648. */
  2649. tryagain = isaligned = 0;
  2650. args.tp = ap->tp;
  2651. args.mp = mp;
  2652. args.fsbno = ap->rval;
  2653. args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
  2654. args.firstblock = ap->firstblock;
  2655. blen = 0;
  2656. if (nullfb) {
  2657. if (ap->userdata && xfs_inode_is_filestream(ap->ip))
  2658. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2659. else
  2660. args.type = XFS_ALLOCTYPE_START_BNO;
  2661. args.total = ap->total;
  2662. /*
  2663. * Search for an allocation group with a single extent
  2664. * large enough for the request.
  2665. *
  2666. * If one isn't found, then adjust the minimum allocation
  2667. * size to the largest space found.
  2668. */
  2669. startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
  2670. if (startag == NULLAGNUMBER)
  2671. startag = ag = 0;
  2672. notinit = 0;
  2673. down_read(&mp->m_peraglock);
  2674. while (blen < ap->alen) {
  2675. pag = &mp->m_perag[ag];
  2676. if (!pag->pagf_init &&
  2677. (error = xfs_alloc_pagf_init(mp, args.tp,
  2678. ag, XFS_ALLOC_FLAG_TRYLOCK))) {
  2679. up_read(&mp->m_peraglock);
  2680. return error;
  2681. }
  2682. /*
  2683. * See xfs_alloc_fix_freelist...
  2684. */
  2685. if (pag->pagf_init) {
  2686. need = XFS_MIN_FREELIST_PAG(pag, mp);
  2687. delta = need > pag->pagf_flcount ?
  2688. need - pag->pagf_flcount : 0;
  2689. longest = (pag->pagf_longest > delta) ?
  2690. (pag->pagf_longest - delta) :
  2691. (pag->pagf_flcount > 0 ||
  2692. pag->pagf_longest > 0);
  2693. if (blen < longest)
  2694. blen = longest;
  2695. } else
  2696. notinit = 1;
  2697. if (xfs_inode_is_filestream(ap->ip)) {
  2698. if (blen >= ap->alen)
  2699. break;
  2700. if (ap->userdata) {
  2701. /*
  2702. * If startag is an invalid AG, we've
  2703. * come here once before and
  2704. * xfs_filestream_new_ag picked the
  2705. * best currently available.
  2706. *
  2707. * Don't continue looping, since we
  2708. * could loop forever.
  2709. */
  2710. if (startag == NULLAGNUMBER)
  2711. break;
  2712. error = xfs_filestream_new_ag(ap, &ag);
  2713. if (error) {
  2714. up_read(&mp->m_peraglock);
  2715. return error;
  2716. }
  2717. /* loop again to set 'blen'*/
  2718. startag = NULLAGNUMBER;
  2719. continue;
  2720. }
  2721. }
  2722. if (++ag == mp->m_sb.sb_agcount)
  2723. ag = 0;
  2724. if (ag == startag)
  2725. break;
  2726. }
  2727. up_read(&mp->m_peraglock);
  2728. /*
  2729. * Since the above loop did a BUF_TRYLOCK, it is
  2730. * possible that there is space for this request.
  2731. */
  2732. if (notinit || blen < ap->minlen)
  2733. args.minlen = ap->minlen;
  2734. /*
  2735. * If the best seen length is less than the request
  2736. * length, use the best as the minimum.
  2737. */
  2738. else if (blen < ap->alen)
  2739. args.minlen = blen;
  2740. /*
  2741. * Otherwise we've seen an extent as big as alen,
  2742. * use that as the minimum.
  2743. */
  2744. else
  2745. args.minlen = ap->alen;
  2746. /*
  2747. * set the failure fallback case to look in the selected
  2748. * AG as the stream may have moved.
  2749. */
  2750. if (xfs_inode_is_filestream(ap->ip))
  2751. ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  2752. } else if (ap->low) {
  2753. if (xfs_inode_is_filestream(ap->ip))
  2754. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2755. else
  2756. args.type = XFS_ALLOCTYPE_START_BNO;
  2757. args.total = args.minlen = ap->minlen;
  2758. } else {
  2759. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2760. args.total = ap->total;
  2761. args.minlen = ap->minlen;
  2762. }
  2763. /* apply extent size hints if obtained earlier */
  2764. if (unlikely(align)) {
  2765. args.prod = align;
  2766. if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
  2767. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2768. } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
  2769. args.prod = 1;
  2770. args.mod = 0;
  2771. } else {
  2772. args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
  2773. if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
  2774. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2775. }
  2776. /*
  2777. * If we are not low on available data blocks, and the
  2778. * underlying logical volume manager is a stripe, and
  2779. * the file offset is zero then try to allocate data
  2780. * blocks on stripe unit boundary.
  2781. * NOTE: ap->aeof is only set if the allocation length
  2782. * is >= the stripe unit and the allocation offset is
  2783. * at the end of file.
  2784. */
  2785. if (!ap->low && ap->aeof) {
  2786. if (!ap->off) {
  2787. args.alignment = mp->m_dalign;
  2788. atype = args.type;
  2789. isaligned = 1;
  2790. /*
  2791. * Adjust for alignment
  2792. */
  2793. if (blen > args.alignment && blen <= ap->alen)
  2794. args.minlen = blen - args.alignment;
  2795. args.minalignslop = 0;
  2796. } else {
  2797. /*
  2798. * First try an exact bno allocation.
  2799. * If it fails then do a near or start bno
  2800. * allocation with alignment turned on.
  2801. */
  2802. atype = args.type;
  2803. tryagain = 1;
  2804. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2805. args.alignment = 1;
  2806. /*
  2807. * Compute the minlen+alignment for the
  2808. * next case. Set slop so that the value
  2809. * of minlen+alignment+slop doesn't go up
  2810. * between the calls.
  2811. */
  2812. if (blen > mp->m_dalign && blen <= ap->alen)
  2813. nextminlen = blen - mp->m_dalign;
  2814. else
  2815. nextminlen = args.minlen;
  2816. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2817. args.minalignslop =
  2818. nextminlen + mp->m_dalign -
  2819. args.minlen - 1;
  2820. else
  2821. args.minalignslop = 0;
  2822. }
  2823. } else {
  2824. args.alignment = 1;
  2825. args.minalignslop = 0;
  2826. }
  2827. args.minleft = ap->minleft;
  2828. args.wasdel = ap->wasdel;
  2829. args.isfl = 0;
  2830. args.userdata = ap->userdata;
  2831. if ((error = xfs_alloc_vextent(&args)))
  2832. return error;
  2833. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2834. /*
  2835. * Exact allocation failed. Now try with alignment
  2836. * turned on.
  2837. */
  2838. args.type = atype;
  2839. args.fsbno = ap->rval;
  2840. args.alignment = mp->m_dalign;
  2841. args.minlen = nextminlen;
  2842. args.minalignslop = 0;
  2843. isaligned = 1;
  2844. if ((error = xfs_alloc_vextent(&args)))
  2845. return error;
  2846. }
  2847. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2848. /*
  2849. * allocation failed, so turn off alignment and
  2850. * try again.
  2851. */
  2852. args.type = atype;
  2853. args.fsbno = ap->rval;
  2854. args.alignment = 0;
  2855. if ((error = xfs_alloc_vextent(&args)))
  2856. return error;
  2857. }
  2858. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2859. args.minlen > ap->minlen) {
  2860. args.minlen = ap->minlen;
  2861. args.type = XFS_ALLOCTYPE_START_BNO;
  2862. args.fsbno = ap->rval;
  2863. if ((error = xfs_alloc_vextent(&args)))
  2864. return error;
  2865. }
  2866. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2867. args.fsbno = 0;
  2868. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2869. args.total = ap->minlen;
  2870. args.minleft = 0;
  2871. if ((error = xfs_alloc_vextent(&args)))
  2872. return error;
  2873. ap->low = 1;
  2874. }
  2875. if (args.fsbno != NULLFSBLOCK) {
  2876. ap->firstblock = ap->rval = args.fsbno;
  2877. ASSERT(nullfb || fb_agno == args.agno ||
  2878. (ap->low && fb_agno < args.agno));
  2879. ap->alen = args.len;
  2880. ap->ip->i_d.di_nblocks += args.len;
  2881. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2882. if (ap->wasdel)
  2883. ap->ip->i_delayed_blks -= args.len;
  2884. /*
  2885. * Adjust the disk quota also. This was reserved
  2886. * earlier.
  2887. */
  2888. XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
  2889. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2890. XFS_TRANS_DQ_BCOUNT,
  2891. (long) args.len);
  2892. } else {
  2893. ap->rval = NULLFSBLOCK;
  2894. ap->alen = 0;
  2895. }
  2896. return 0;
  2897. }
  2898. /*
  2899. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2900. * It figures out where to ask the underlying allocator to put the new extent.
  2901. */
  2902. STATIC int
  2903. xfs_bmap_alloc(
  2904. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2905. {
  2906. if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
  2907. return xfs_bmap_rtalloc(ap);
  2908. return xfs_bmap_btalloc(ap);
  2909. }
  2910. /*
  2911. * Transform a btree format file with only one leaf node, where the
  2912. * extents list will fit in the inode, into an extents format file.
  2913. * Since the file extents are already in-core, all we have to do is
  2914. * give up the space for the btree root and pitch the leaf block.
  2915. */
  2916. STATIC int /* error */
  2917. xfs_bmap_btree_to_extents(
  2918. xfs_trans_t *tp, /* transaction pointer */
  2919. xfs_inode_t *ip, /* incore inode pointer */
  2920. xfs_btree_cur_t *cur, /* btree cursor */
  2921. int *logflagsp, /* inode logging flags */
  2922. int whichfork) /* data or attr fork */
  2923. {
  2924. /* REFERENCED */
  2925. struct xfs_btree_block *cblock;/* child btree block */
  2926. xfs_fsblock_t cbno; /* child block number */
  2927. xfs_buf_t *cbp; /* child block's buffer */
  2928. int error; /* error return value */
  2929. xfs_ifork_t *ifp; /* inode fork data */
  2930. xfs_mount_t *mp; /* mount point structure */
  2931. __be64 *pp; /* ptr to block address */
  2932. struct xfs_btree_block *rblock;/* root btree block */
  2933. mp = ip->i_mount;
  2934. ifp = XFS_IFORK_PTR(ip, whichfork);
  2935. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2936. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2937. rblock = ifp->if_broot;
  2938. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2939. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2940. ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
  2941. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
  2942. cbno = be64_to_cpu(*pp);
  2943. *logflagsp = 0;
  2944. #ifdef DEBUG
  2945. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  2946. return error;
  2947. #endif
  2948. if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
  2949. XFS_BMAP_BTREE_REF)))
  2950. return error;
  2951. cblock = XFS_BUF_TO_BLOCK(cbp);
  2952. if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
  2953. return error;
  2954. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2955. ip->i_d.di_nblocks--;
  2956. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2957. xfs_trans_binval(tp, cbp);
  2958. if (cur->bc_bufs[0] == cbp)
  2959. cur->bc_bufs[0] = NULL;
  2960. xfs_iroot_realloc(ip, -1, whichfork);
  2961. ASSERT(ifp->if_broot == NULL);
  2962. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2963. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2964. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2965. return 0;
  2966. }
  2967. /*
  2968. * Called by xfs_bmapi to update file extent records and the btree
  2969. * after removing space (or undoing a delayed allocation).
  2970. */
  2971. STATIC int /* error */
  2972. xfs_bmap_del_extent(
  2973. xfs_inode_t *ip, /* incore inode pointer */
  2974. xfs_trans_t *tp, /* current transaction pointer */
  2975. xfs_extnum_t idx, /* extent number to update/delete */
  2976. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2977. xfs_btree_cur_t *cur, /* if null, not a btree */
  2978. xfs_bmbt_irec_t *del, /* data to remove from extents */
  2979. int *logflagsp, /* inode logging flags */
  2980. xfs_extdelta_t *delta, /* Change made to incore extents */
  2981. int whichfork, /* data or attr fork */
  2982. int rsvd) /* OK to allocate reserved blocks */
  2983. {
  2984. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2985. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2986. xfs_fsblock_t del_endblock=0; /* first block past del */
  2987. xfs_fileoff_t del_endoff; /* first offset past del */
  2988. int delay; /* current block is delayed allocated */
  2989. int do_fx; /* free extent at end of routine */
  2990. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  2991. int error; /* error return value */
  2992. int flags; /* inode logging flags */
  2993. xfs_bmbt_irec_t got; /* current extent entry */
  2994. xfs_fileoff_t got_endoff; /* first offset past got */
  2995. int i; /* temp state */
  2996. xfs_ifork_t *ifp; /* inode fork pointer */
  2997. xfs_mount_t *mp; /* mount structure */
  2998. xfs_filblks_t nblks; /* quota/sb block count */
  2999. xfs_bmbt_irec_t new; /* new record to be inserted */
  3000. /* REFERENCED */
  3001. uint qfield; /* quota field to update */
  3002. xfs_filblks_t temp; /* for indirect length calculations */
  3003. xfs_filblks_t temp2; /* for indirect length calculations */
  3004. XFS_STATS_INC(xs_del_exlist);
  3005. mp = ip->i_mount;
  3006. ifp = XFS_IFORK_PTR(ip, whichfork);
  3007. ASSERT((idx >= 0) && (idx < ifp->if_bytes /
  3008. (uint)sizeof(xfs_bmbt_rec_t)));
  3009. ASSERT(del->br_blockcount > 0);
  3010. ep = xfs_iext_get_ext(ifp, idx);
  3011. xfs_bmbt_get_all(ep, &got);
  3012. ASSERT(got.br_startoff <= del->br_startoff);
  3013. del_endoff = del->br_startoff + del->br_blockcount;
  3014. got_endoff = got.br_startoff + got.br_blockcount;
  3015. ASSERT(got_endoff >= del_endoff);
  3016. delay = ISNULLSTARTBLOCK(got.br_startblock);
  3017. ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
  3018. flags = 0;
  3019. qfield = 0;
  3020. error = 0;
  3021. /*
  3022. * If deleting a real allocation, must free up the disk space.
  3023. */
  3024. if (!delay) {
  3025. flags = XFS_ILOG_CORE;
  3026. /*
  3027. * Realtime allocation. Free it and record di_nblocks update.
  3028. */
  3029. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  3030. xfs_fsblock_t bno;
  3031. xfs_filblks_t len;
  3032. ASSERT(do_mod(del->br_blockcount,
  3033. mp->m_sb.sb_rextsize) == 0);
  3034. ASSERT(do_mod(del->br_startblock,
  3035. mp->m_sb.sb_rextsize) == 0);
  3036. bno = del->br_startblock;
  3037. len = del->br_blockcount;
  3038. do_div(bno, mp->m_sb.sb_rextsize);
  3039. do_div(len, mp->m_sb.sb_rextsize);
  3040. if ((error = xfs_rtfree_extent(ip->i_transp, bno,
  3041. (xfs_extlen_t)len)))
  3042. goto done;
  3043. do_fx = 0;
  3044. nblks = len * mp->m_sb.sb_rextsize;
  3045. qfield = XFS_TRANS_DQ_RTBCOUNT;
  3046. }
  3047. /*
  3048. * Ordinary allocation.
  3049. */
  3050. else {
  3051. do_fx = 1;
  3052. nblks = del->br_blockcount;
  3053. qfield = XFS_TRANS_DQ_BCOUNT;
  3054. }
  3055. /*
  3056. * Set up del_endblock and cur for later.
  3057. */
  3058. del_endblock = del->br_startblock + del->br_blockcount;
  3059. if (cur) {
  3060. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  3061. got.br_startblock, got.br_blockcount,
  3062. &i)))
  3063. goto done;
  3064. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3065. }
  3066. da_old = da_new = 0;
  3067. } else {
  3068. da_old = STARTBLOCKVAL(got.br_startblock);
  3069. da_new = 0;
  3070. nblks = 0;
  3071. do_fx = 0;
  3072. }
  3073. /*
  3074. * Set flag value to use in switch statement.
  3075. * Left-contig is 2, right-contig is 1.
  3076. */
  3077. switch (((got.br_startoff == del->br_startoff) << 1) |
  3078. (got_endoff == del_endoff)) {
  3079. case 3:
  3080. /*
  3081. * Matches the whole extent. Delete the entry.
  3082. */
  3083. XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
  3084. xfs_iext_remove(ifp, idx, 1);
  3085. ifp->if_lastex = idx;
  3086. if (delay)
  3087. break;
  3088. XFS_IFORK_NEXT_SET(ip, whichfork,
  3089. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  3090. flags |= XFS_ILOG_CORE;
  3091. if (!cur) {
  3092. flags |= XFS_ILOG_FEXT(whichfork);
  3093. break;
  3094. }
  3095. if ((error = xfs_btree_delete(cur, &i)))
  3096. goto done;
  3097. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3098. break;
  3099. case 2:
  3100. /*
  3101. * Deleting the first part of the extent.
  3102. */
  3103. XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
  3104. xfs_bmbt_set_startoff(ep, del_endoff);
  3105. temp = got.br_blockcount - del->br_blockcount;
  3106. xfs_bmbt_set_blockcount(ep, temp);
  3107. ifp->if_lastex = idx;
  3108. if (delay) {
  3109. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  3110. da_old);
  3111. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3112. XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
  3113. whichfork);
  3114. da_new = temp;
  3115. break;
  3116. }
  3117. xfs_bmbt_set_startblock(ep, del_endblock);
  3118. XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
  3119. if (!cur) {
  3120. flags |= XFS_ILOG_FEXT(whichfork);
  3121. break;
  3122. }
  3123. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  3124. got.br_blockcount - del->br_blockcount,
  3125. got.br_state)))
  3126. goto done;
  3127. break;
  3128. case 1:
  3129. /*
  3130. * Deleting the last part of the extent.
  3131. */
  3132. temp = got.br_blockcount - del->br_blockcount;
  3133. XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
  3134. xfs_bmbt_set_blockcount(ep, temp);
  3135. ifp->if_lastex = idx;
  3136. if (delay) {
  3137. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  3138. da_old);
  3139. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3140. XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
  3141. whichfork);
  3142. da_new = temp;
  3143. break;
  3144. }
  3145. XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
  3146. if (!cur) {
  3147. flags |= XFS_ILOG_FEXT(whichfork);
  3148. break;
  3149. }
  3150. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  3151. got.br_startblock,
  3152. got.br_blockcount - del->br_blockcount,
  3153. got.br_state)))
  3154. goto done;
  3155. break;
  3156. case 0:
  3157. /*
  3158. * Deleting the middle of the extent.
  3159. */
  3160. temp = del->br_startoff - got.br_startoff;
  3161. XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
  3162. xfs_bmbt_set_blockcount(ep, temp);
  3163. new.br_startoff = del_endoff;
  3164. temp2 = got_endoff - del_endoff;
  3165. new.br_blockcount = temp2;
  3166. new.br_state = got.br_state;
  3167. if (!delay) {
  3168. new.br_startblock = del_endblock;
  3169. flags |= XFS_ILOG_CORE;
  3170. if (cur) {
  3171. if ((error = xfs_bmbt_update(cur,
  3172. got.br_startoff,
  3173. got.br_startblock, temp,
  3174. got.br_state)))
  3175. goto done;
  3176. if ((error = xfs_btree_increment(cur, 0, &i)))
  3177. goto done;
  3178. cur->bc_rec.b = new;
  3179. error = xfs_btree_insert(cur, &i);
  3180. if (error && error != ENOSPC)
  3181. goto done;
  3182. /*
  3183. * If get no-space back from btree insert,
  3184. * it tried a split, and we have a zero
  3185. * block reservation.
  3186. * Fix up our state and return the error.
  3187. */
  3188. if (error == ENOSPC) {
  3189. /*
  3190. * Reset the cursor, don't trust
  3191. * it after any insert operation.
  3192. */
  3193. if ((error = xfs_bmbt_lookup_eq(cur,
  3194. got.br_startoff,
  3195. got.br_startblock,
  3196. temp, &i)))
  3197. goto done;
  3198. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3199. /*
  3200. * Update the btree record back
  3201. * to the original value.
  3202. */
  3203. if ((error = xfs_bmbt_update(cur,
  3204. got.br_startoff,
  3205. got.br_startblock,
  3206. got.br_blockcount,
  3207. got.br_state)))
  3208. goto done;
  3209. /*
  3210. * Reset the extent record back
  3211. * to the original value.
  3212. */
  3213. xfs_bmbt_set_blockcount(ep,
  3214. got.br_blockcount);
  3215. flags = 0;
  3216. error = XFS_ERROR(ENOSPC);
  3217. goto done;
  3218. }
  3219. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3220. } else
  3221. flags |= XFS_ILOG_FEXT(whichfork);
  3222. XFS_IFORK_NEXT_SET(ip, whichfork,
  3223. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  3224. } else {
  3225. ASSERT(whichfork == XFS_DATA_FORK);
  3226. temp = xfs_bmap_worst_indlen(ip, temp);
  3227. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3228. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  3229. new.br_startblock = NULLSTARTBLOCK((int)temp2);
  3230. da_new = temp + temp2;
  3231. while (da_new > da_old) {
  3232. if (temp) {
  3233. temp--;
  3234. da_new--;
  3235. xfs_bmbt_set_startblock(ep,
  3236. NULLSTARTBLOCK((int)temp));
  3237. }
  3238. if (da_new == da_old)
  3239. break;
  3240. if (temp2) {
  3241. temp2--;
  3242. da_new--;
  3243. new.br_startblock =
  3244. NULLSTARTBLOCK((int)temp2);
  3245. }
  3246. }
  3247. }
  3248. XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
  3249. XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
  3250. whichfork);
  3251. xfs_iext_insert(ifp, idx + 1, 1, &new);
  3252. ifp->if_lastex = idx + 1;
  3253. break;
  3254. }
  3255. /*
  3256. * If we need to, add to list of extents to delete.
  3257. */
  3258. if (do_fx)
  3259. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  3260. mp);
  3261. /*
  3262. * Adjust inode # blocks in the file.
  3263. */
  3264. if (nblks)
  3265. ip->i_d.di_nblocks -= nblks;
  3266. /*
  3267. * Adjust quota data.
  3268. */
  3269. if (qfield)
  3270. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
  3271. /*
  3272. * Account for change in delayed indirect blocks.
  3273. * Nothing to do for disk quota accounting here.
  3274. */
  3275. ASSERT(da_old >= da_new);
  3276. if (da_old > da_new)
  3277. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
  3278. rsvd);
  3279. if (delta) {
  3280. /* DELTA: report the original extent. */
  3281. if (delta->xed_startoff > got.br_startoff)
  3282. delta->xed_startoff = got.br_startoff;
  3283. if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
  3284. delta->xed_blockcount = got.br_startoff +
  3285. got.br_blockcount;
  3286. }
  3287. done:
  3288. *logflagsp = flags;
  3289. return error;
  3290. }
  3291. /*
  3292. * Remove the entry "free" from the free item list. Prev points to the
  3293. * previous entry, unless "free" is the head of the list.
  3294. */
  3295. STATIC void
  3296. xfs_bmap_del_free(
  3297. xfs_bmap_free_t *flist, /* free item list header */
  3298. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  3299. xfs_bmap_free_item_t *free) /* list item to be freed */
  3300. {
  3301. if (prev)
  3302. prev->xbfi_next = free->xbfi_next;
  3303. else
  3304. flist->xbf_first = free->xbfi_next;
  3305. flist->xbf_count--;
  3306. kmem_zone_free(xfs_bmap_free_item_zone, free);
  3307. }
  3308. /*
  3309. * Convert an extents-format file into a btree-format file.
  3310. * The new file will have a root block (in the inode) and a single child block.
  3311. */
  3312. STATIC int /* error */
  3313. xfs_bmap_extents_to_btree(
  3314. xfs_trans_t *tp, /* transaction pointer */
  3315. xfs_inode_t *ip, /* incore inode pointer */
  3316. xfs_fsblock_t *firstblock, /* first-block-allocated */
  3317. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  3318. xfs_btree_cur_t **curp, /* cursor returned to caller */
  3319. int wasdel, /* converting a delayed alloc */
  3320. int *logflagsp, /* inode logging flags */
  3321. int whichfork) /* data or attr fork */
  3322. {
  3323. struct xfs_btree_block *ablock; /* allocated (child) bt block */
  3324. xfs_buf_t *abp; /* buffer for ablock */
  3325. xfs_alloc_arg_t args; /* allocation arguments */
  3326. xfs_bmbt_rec_t *arp; /* child record pointer */
  3327. struct xfs_btree_block *block; /* btree root block */
  3328. xfs_btree_cur_t *cur; /* bmap btree cursor */
  3329. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3330. int error; /* error return value */
  3331. xfs_extnum_t i, cnt; /* extent record index */
  3332. xfs_ifork_t *ifp; /* inode fork pointer */
  3333. xfs_bmbt_key_t *kp; /* root block key pointer */
  3334. xfs_mount_t *mp; /* mount structure */
  3335. xfs_extnum_t nextents; /* number of file extents */
  3336. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  3337. ifp = XFS_IFORK_PTR(ip, whichfork);
  3338. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  3339. ASSERT(ifp->if_ext_max ==
  3340. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  3341. /*
  3342. * Make space in the inode incore.
  3343. */
  3344. xfs_iroot_realloc(ip, 1, whichfork);
  3345. ifp->if_flags |= XFS_IFBROOT;
  3346. /*
  3347. * Fill in the root.
  3348. */
  3349. block = ifp->if_broot;
  3350. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3351. block->bb_level = cpu_to_be16(1);
  3352. block->bb_numrecs = cpu_to_be16(1);
  3353. block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3354. block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3355. /*
  3356. * Need a cursor. Can't allocate until bb_level is filled in.
  3357. */
  3358. mp = ip->i_mount;
  3359. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  3360. cur->bc_private.b.firstblock = *firstblock;
  3361. cur->bc_private.b.flist = flist;
  3362. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3363. /*
  3364. * Convert to a btree with two levels, one record in root.
  3365. */
  3366. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  3367. args.tp = tp;
  3368. args.mp = mp;
  3369. args.firstblock = *firstblock;
  3370. if (*firstblock == NULLFSBLOCK) {
  3371. args.type = XFS_ALLOCTYPE_START_BNO;
  3372. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  3373. } else if (flist->xbf_low) {
  3374. args.type = XFS_ALLOCTYPE_START_BNO;
  3375. args.fsbno = *firstblock;
  3376. } else {
  3377. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3378. args.fsbno = *firstblock;
  3379. }
  3380. args.minlen = args.maxlen = args.prod = 1;
  3381. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  3382. args.minalignslop = 0;
  3383. args.wasdel = wasdel;
  3384. *logflagsp = 0;
  3385. if ((error = xfs_alloc_vextent(&args))) {
  3386. xfs_iroot_realloc(ip, -1, whichfork);
  3387. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  3388. return error;
  3389. }
  3390. /*
  3391. * Allocation can't fail, the space was reserved.
  3392. */
  3393. ASSERT(args.fsbno != NULLFSBLOCK);
  3394. ASSERT(*firstblock == NULLFSBLOCK ||
  3395. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  3396. (flist->xbf_low &&
  3397. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  3398. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  3399. cur->bc_private.b.allocated++;
  3400. ip->i_d.di_nblocks++;
  3401. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  3402. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  3403. /*
  3404. * Fill in the child block.
  3405. */
  3406. ablock = XFS_BUF_TO_BLOCK(abp);
  3407. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3408. ablock->bb_level = 0;
  3409. ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3410. ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3411. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3412. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3413. for (cnt = i = 0; i < nextents; i++) {
  3414. ep = xfs_iext_get_ext(ifp, i);
  3415. if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
  3416. arp->l0 = cpu_to_be64(ep->l0);
  3417. arp->l1 = cpu_to_be64(ep->l1);
  3418. arp++; cnt++;
  3419. }
  3420. }
  3421. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  3422. xfs_btree_set_numrecs(ablock, cnt);
  3423. /*
  3424. * Fill in the root key and pointer.
  3425. */
  3426. kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
  3427. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3428. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  3429. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
  3430. be16_to_cpu(block->bb_level)));
  3431. *pp = cpu_to_be64(args.fsbno);
  3432. /*
  3433. * Do all this logging at the end so that
  3434. * the root is at the right level.
  3435. */
  3436. xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
  3437. xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  3438. ASSERT(*curp == NULL);
  3439. *curp = cur;
  3440. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
  3441. return 0;
  3442. }
  3443. /*
  3444. * Helper routine to reset inode di_forkoff field when switching
  3445. * attribute fork from local to extent format - we reset it where
  3446. * possible to make space available for inline data fork extents.
  3447. */
  3448. STATIC void
  3449. xfs_bmap_forkoff_reset(
  3450. xfs_mount_t *mp,
  3451. xfs_inode_t *ip,
  3452. int whichfork)
  3453. {
  3454. if (whichfork == XFS_ATTR_FORK &&
  3455. (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
  3456. (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
  3457. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3458. ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
  3459. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3460. ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
  3461. (uint)sizeof(xfs_bmbt_rec_t);
  3462. ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
  3463. (uint)sizeof(xfs_bmbt_rec_t);
  3464. }
  3465. }
  3466. /*
  3467. * Convert a local file to an extents file.
  3468. * This code is out of bounds for data forks of regular files,
  3469. * since the file data needs to get logged so things will stay consistent.
  3470. * (The bmap-level manipulations are ok, though).
  3471. */
  3472. STATIC int /* error */
  3473. xfs_bmap_local_to_extents(
  3474. xfs_trans_t *tp, /* transaction pointer */
  3475. xfs_inode_t *ip, /* incore inode pointer */
  3476. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3477. xfs_extlen_t total, /* total blocks needed by transaction */
  3478. int *logflagsp, /* inode logging flags */
  3479. int whichfork) /* data or attr fork */
  3480. {
  3481. int error; /* error return value */
  3482. int flags; /* logging flags returned */
  3483. xfs_ifork_t *ifp; /* inode fork pointer */
  3484. /*
  3485. * We don't want to deal with the case of keeping inode data inline yet.
  3486. * So sending the data fork of a regular inode is invalid.
  3487. */
  3488. ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
  3489. whichfork == XFS_DATA_FORK));
  3490. ifp = XFS_IFORK_PTR(ip, whichfork);
  3491. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3492. flags = 0;
  3493. error = 0;
  3494. if (ifp->if_bytes) {
  3495. xfs_alloc_arg_t args; /* allocation arguments */
  3496. xfs_buf_t *bp; /* buffer for extent block */
  3497. xfs_bmbt_rec_host_t *ep;/* extent record pointer */
  3498. args.tp = tp;
  3499. args.mp = ip->i_mount;
  3500. args.firstblock = *firstblock;
  3501. ASSERT((ifp->if_flags &
  3502. (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
  3503. /*
  3504. * Allocate a block. We know we need only one, since the
  3505. * file currently fits in an inode.
  3506. */
  3507. if (*firstblock == NULLFSBLOCK) {
  3508. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3509. args.type = XFS_ALLOCTYPE_START_BNO;
  3510. } else {
  3511. args.fsbno = *firstblock;
  3512. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3513. }
  3514. args.total = total;
  3515. args.mod = args.minleft = args.alignment = args.wasdel =
  3516. args.isfl = args.minalignslop = 0;
  3517. args.minlen = args.maxlen = args.prod = 1;
  3518. if ((error = xfs_alloc_vextent(&args)))
  3519. goto done;
  3520. /*
  3521. * Can't fail, the space was reserved.
  3522. */
  3523. ASSERT(args.fsbno != NULLFSBLOCK);
  3524. ASSERT(args.len == 1);
  3525. *firstblock = args.fsbno;
  3526. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3527. memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
  3528. ifp->if_bytes);
  3529. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3530. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3531. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3532. xfs_iext_add(ifp, 0, 1);
  3533. ep = xfs_iext_get_ext(ifp, 0);
  3534. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3535. XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
  3536. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3537. ip->i_d.di_nblocks = 1;
  3538. XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
  3539. XFS_TRANS_DQ_BCOUNT, 1L);
  3540. flags |= XFS_ILOG_FEXT(whichfork);
  3541. } else {
  3542. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3543. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3544. }
  3545. ifp->if_flags &= ~XFS_IFINLINE;
  3546. ifp->if_flags |= XFS_IFEXTENTS;
  3547. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3548. flags |= XFS_ILOG_CORE;
  3549. done:
  3550. *logflagsp = flags;
  3551. return error;
  3552. }
  3553. /*
  3554. * Search the extent records for the entry containing block bno.
  3555. * If bno lies in a hole, point to the next entry. If bno lies
  3556. * past eof, *eofp will be set, and *prevp will contain the last
  3557. * entry (null if none). Else, *lastxp will be set to the index
  3558. * of the found entry; *gotp will contain the entry.
  3559. */
  3560. xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3561. xfs_bmap_search_multi_extents(
  3562. xfs_ifork_t *ifp, /* inode fork pointer */
  3563. xfs_fileoff_t bno, /* block number searched for */
  3564. int *eofp, /* out: end of file found */
  3565. xfs_extnum_t *lastxp, /* out: last extent index */
  3566. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3567. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3568. {
  3569. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3570. xfs_extnum_t lastx; /* last extent index */
  3571. /*
  3572. * Initialize the extent entry structure to catch access to
  3573. * uninitialized br_startblock field.
  3574. */
  3575. gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3576. gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3577. gotp->br_state = XFS_EXT_INVALID;
  3578. #if XFS_BIG_BLKNOS
  3579. gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
  3580. #else
  3581. gotp->br_startblock = 0xffffa5a5;
  3582. #endif
  3583. prevp->br_startoff = NULLFILEOFF;
  3584. ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
  3585. if (lastx > 0) {
  3586. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
  3587. }
  3588. if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  3589. xfs_bmbt_get_all(ep, gotp);
  3590. *eofp = 0;
  3591. } else {
  3592. if (lastx > 0) {
  3593. *gotp = *prevp;
  3594. }
  3595. *eofp = 1;
  3596. ep = NULL;
  3597. }
  3598. *lastxp = lastx;
  3599. return ep;
  3600. }
  3601. /*
  3602. * Search the extents list for the inode, for the extent containing bno.
  3603. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3604. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3605. * Else, *lastxp will be set to the index of the found
  3606. * entry; *gotp will contain the entry.
  3607. */
  3608. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3609. xfs_bmap_search_extents(
  3610. xfs_inode_t *ip, /* incore inode pointer */
  3611. xfs_fileoff_t bno, /* block number searched for */
  3612. int fork, /* data or attr fork */
  3613. int *eofp, /* out: end of file found */
  3614. xfs_extnum_t *lastxp, /* out: last extent index */
  3615. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3616. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3617. {
  3618. xfs_ifork_t *ifp; /* inode fork pointer */
  3619. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3620. XFS_STATS_INC(xs_look_exlist);
  3621. ifp = XFS_IFORK_PTR(ip, fork);
  3622. ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
  3623. if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
  3624. !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
  3625. xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
  3626. "Access to block zero in inode %llu "
  3627. "start_block: %llx start_off: %llx "
  3628. "blkcnt: %llx extent-state: %x lastx: %x\n",
  3629. (unsigned long long)ip->i_ino,
  3630. (unsigned long long)gotp->br_startblock,
  3631. (unsigned long long)gotp->br_startoff,
  3632. (unsigned long long)gotp->br_blockcount,
  3633. gotp->br_state, *lastxp);
  3634. *lastxp = NULLEXTNUM;
  3635. *eofp = 1;
  3636. return NULL;
  3637. }
  3638. return ep;
  3639. }
  3640. #ifdef XFS_BMAP_TRACE
  3641. ktrace_t *xfs_bmap_trace_buf;
  3642. /*
  3643. * Add a bmap trace buffer entry. Base routine for the others.
  3644. */
  3645. STATIC void
  3646. xfs_bmap_trace_addentry(
  3647. int opcode, /* operation */
  3648. const char *fname, /* function name */
  3649. char *desc, /* operation description */
  3650. xfs_inode_t *ip, /* incore inode pointer */
  3651. xfs_extnum_t idx, /* index of entry(ies) */
  3652. xfs_extnum_t cnt, /* count of entries, 1 or 2 */
  3653. xfs_bmbt_rec_host_t *r1, /* first record */
  3654. xfs_bmbt_rec_host_t *r2, /* second record or null */
  3655. int whichfork) /* data or attr fork */
  3656. {
  3657. xfs_bmbt_rec_host_t tr2;
  3658. ASSERT(cnt == 1 || cnt == 2);
  3659. ASSERT(r1 != NULL);
  3660. if (cnt == 1) {
  3661. ASSERT(r2 == NULL);
  3662. r2 = &tr2;
  3663. memset(&tr2, 0, sizeof(tr2));
  3664. } else
  3665. ASSERT(r2 != NULL);
  3666. ktrace_enter(xfs_bmap_trace_buf,
  3667. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3668. (void *)fname, (void *)desc, (void *)ip,
  3669. (void *)(__psint_t)idx,
  3670. (void *)(__psint_t)cnt,
  3671. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3672. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3673. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3674. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3675. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3676. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3677. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3678. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3679. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3680. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3681. );
  3682. ASSERT(ip->i_xtrace);
  3683. ktrace_enter(ip->i_xtrace,
  3684. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3685. (void *)fname, (void *)desc, (void *)ip,
  3686. (void *)(__psint_t)idx,
  3687. (void *)(__psint_t)cnt,
  3688. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3689. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3690. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3691. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3692. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3693. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3694. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3695. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3696. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3697. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3698. );
  3699. }
  3700. /*
  3701. * Add bmap trace entry prior to a call to xfs_iext_remove.
  3702. */
  3703. STATIC void
  3704. xfs_bmap_trace_delete(
  3705. const char *fname, /* function name */
  3706. char *desc, /* operation description */
  3707. xfs_inode_t *ip, /* incore inode pointer */
  3708. xfs_extnum_t idx, /* index of entry(entries) deleted */
  3709. xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
  3710. int whichfork) /* data or attr fork */
  3711. {
  3712. xfs_ifork_t *ifp; /* inode fork pointer */
  3713. ifp = XFS_IFORK_PTR(ip, whichfork);
  3714. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
  3715. cnt, xfs_iext_get_ext(ifp, idx),
  3716. cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
  3717. whichfork);
  3718. }
  3719. /*
  3720. * Add bmap trace entry prior to a call to xfs_iext_insert, or
  3721. * reading in the extents list from the disk (in the btree).
  3722. */
  3723. STATIC void
  3724. xfs_bmap_trace_insert(
  3725. const char *fname, /* function name */
  3726. char *desc, /* operation description */
  3727. xfs_inode_t *ip, /* incore inode pointer */
  3728. xfs_extnum_t idx, /* index of entry(entries) inserted */
  3729. xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
  3730. xfs_bmbt_irec_t *r1, /* inserted record 1 */
  3731. xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
  3732. int whichfork) /* data or attr fork */
  3733. {
  3734. xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
  3735. xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
  3736. xfs_bmbt_set_all(&tr1, r1);
  3737. if (cnt == 2) {
  3738. ASSERT(r2 != NULL);
  3739. xfs_bmbt_set_all(&tr2, r2);
  3740. } else {
  3741. ASSERT(cnt == 1);
  3742. ASSERT(r2 == NULL);
  3743. }
  3744. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
  3745. cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
  3746. }
  3747. /*
  3748. * Add bmap trace entry after updating an extent record in place.
  3749. */
  3750. STATIC void
  3751. xfs_bmap_trace_post_update(
  3752. const char *fname, /* function name */
  3753. char *desc, /* operation description */
  3754. xfs_inode_t *ip, /* incore inode pointer */
  3755. xfs_extnum_t idx, /* index of entry updated */
  3756. int whichfork) /* data or attr fork */
  3757. {
  3758. xfs_ifork_t *ifp; /* inode fork pointer */
  3759. ifp = XFS_IFORK_PTR(ip, whichfork);
  3760. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
  3761. 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
  3762. }
  3763. /*
  3764. * Add bmap trace entry prior to updating an extent record in place.
  3765. */
  3766. STATIC void
  3767. xfs_bmap_trace_pre_update(
  3768. const char *fname, /* function name */
  3769. char *desc, /* operation description */
  3770. xfs_inode_t *ip, /* incore inode pointer */
  3771. xfs_extnum_t idx, /* index of entry to be updated */
  3772. int whichfork) /* data or attr fork */
  3773. {
  3774. xfs_ifork_t *ifp; /* inode fork pointer */
  3775. ifp = XFS_IFORK_PTR(ip, whichfork);
  3776. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
  3777. xfs_iext_get_ext(ifp, idx), NULL, whichfork);
  3778. }
  3779. #endif /* XFS_BMAP_TRACE */
  3780. /*
  3781. * Compute the worst-case number of indirect blocks that will be used
  3782. * for ip's delayed extent of length "len".
  3783. */
  3784. STATIC xfs_filblks_t
  3785. xfs_bmap_worst_indlen(
  3786. xfs_inode_t *ip, /* incore inode pointer */
  3787. xfs_filblks_t len) /* delayed extent length */
  3788. {
  3789. int level; /* btree level number */
  3790. int maxrecs; /* maximum record count at this level */
  3791. xfs_mount_t *mp; /* mount structure */
  3792. xfs_filblks_t rval; /* return value */
  3793. mp = ip->i_mount;
  3794. maxrecs = mp->m_bmap_dmxr[0];
  3795. for (level = 0, rval = 0;
  3796. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3797. level++) {
  3798. len += maxrecs - 1;
  3799. do_div(len, maxrecs);
  3800. rval += len;
  3801. if (len == 1)
  3802. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3803. level - 1;
  3804. if (level == 0)
  3805. maxrecs = mp->m_bmap_dmxr[1];
  3806. }
  3807. return rval;
  3808. }
  3809. #if defined(XFS_RW_TRACE)
  3810. STATIC void
  3811. xfs_bunmap_trace(
  3812. xfs_inode_t *ip,
  3813. xfs_fileoff_t bno,
  3814. xfs_filblks_t len,
  3815. int flags,
  3816. inst_t *ra)
  3817. {
  3818. if (ip->i_rwtrace == NULL)
  3819. return;
  3820. ktrace_enter(ip->i_rwtrace,
  3821. (void *)(__psint_t)XFS_BUNMAP,
  3822. (void *)ip,
  3823. (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
  3824. (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
  3825. (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
  3826. (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
  3827. (void *)(__psint_t)len,
  3828. (void *)(__psint_t)flags,
  3829. (void *)(unsigned long)current_cpu(),
  3830. (void *)ra,
  3831. (void *)0,
  3832. (void *)0,
  3833. (void *)0,
  3834. (void *)0,
  3835. (void *)0,
  3836. (void *)0);
  3837. }
  3838. #endif
  3839. /*
  3840. * Convert inode from non-attributed to attributed.
  3841. * Must not be in a transaction, ip must not be locked.
  3842. */
  3843. int /* error code */
  3844. xfs_bmap_add_attrfork(
  3845. xfs_inode_t *ip, /* incore inode pointer */
  3846. int size, /* space new attribute needs */
  3847. int rsvd) /* xact may use reserved blks */
  3848. {
  3849. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3850. xfs_bmap_free_t flist; /* freed extent records */
  3851. xfs_mount_t *mp; /* mount structure */
  3852. xfs_trans_t *tp; /* transaction pointer */
  3853. int blks; /* space reservation */
  3854. int version = 1; /* superblock attr version */
  3855. int committed; /* xaction was committed */
  3856. int logflags; /* logging flags */
  3857. int error; /* error return value */
  3858. ASSERT(XFS_IFORK_Q(ip) == 0);
  3859. ASSERT(ip->i_df.if_ext_max ==
  3860. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3861. mp = ip->i_mount;
  3862. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3863. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3864. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3865. if (rsvd)
  3866. tp->t_flags |= XFS_TRANS_RESERVE;
  3867. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3868. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3869. goto error0;
  3870. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3871. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
  3872. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3873. XFS_QMOPT_RES_REGBLKS);
  3874. if (error) {
  3875. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3876. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3877. return error;
  3878. }
  3879. if (XFS_IFORK_Q(ip))
  3880. goto error1;
  3881. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3882. /*
  3883. * For inodes coming from pre-6.2 filesystems.
  3884. */
  3885. ASSERT(ip->i_d.di_aformat == 0);
  3886. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3887. }
  3888. ASSERT(ip->i_d.di_anextents == 0);
  3889. IHOLD(ip);
  3890. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3891. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3892. switch (ip->i_d.di_format) {
  3893. case XFS_DINODE_FMT_DEV:
  3894. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3895. break;
  3896. case XFS_DINODE_FMT_UUID:
  3897. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3898. break;
  3899. case XFS_DINODE_FMT_LOCAL:
  3900. case XFS_DINODE_FMT_EXTENTS:
  3901. case XFS_DINODE_FMT_BTREE:
  3902. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3903. if (!ip->i_d.di_forkoff)
  3904. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3905. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  3906. version = 2;
  3907. break;
  3908. default:
  3909. ASSERT(0);
  3910. error = XFS_ERROR(EINVAL);
  3911. goto error1;
  3912. }
  3913. ip->i_df.if_ext_max =
  3914. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3915. ASSERT(ip->i_afp == NULL);
  3916. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3917. ip->i_afp->if_ext_max =
  3918. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3919. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3920. logflags = 0;
  3921. XFS_BMAP_INIT(&flist, &firstblock);
  3922. switch (ip->i_d.di_format) {
  3923. case XFS_DINODE_FMT_LOCAL:
  3924. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3925. &logflags);
  3926. break;
  3927. case XFS_DINODE_FMT_EXTENTS:
  3928. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3929. &flist, &logflags);
  3930. break;
  3931. case XFS_DINODE_FMT_BTREE:
  3932. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3933. &logflags);
  3934. break;
  3935. default:
  3936. error = 0;
  3937. break;
  3938. }
  3939. if (logflags)
  3940. xfs_trans_log_inode(tp, ip, logflags);
  3941. if (error)
  3942. goto error2;
  3943. if (!xfs_sb_version_hasattr(&mp->m_sb) ||
  3944. (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
  3945. __int64_t sbfields = 0;
  3946. spin_lock(&mp->m_sb_lock);
  3947. if (!xfs_sb_version_hasattr(&mp->m_sb)) {
  3948. xfs_sb_version_addattr(&mp->m_sb);
  3949. sbfields |= XFS_SB_VERSIONNUM;
  3950. }
  3951. if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
  3952. xfs_sb_version_addattr2(&mp->m_sb);
  3953. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3954. }
  3955. if (sbfields) {
  3956. spin_unlock(&mp->m_sb_lock);
  3957. xfs_mod_sb(tp, sbfields);
  3958. } else
  3959. spin_unlock(&mp->m_sb_lock);
  3960. }
  3961. if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
  3962. goto error2;
  3963. error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
  3964. ASSERT(ip->i_df.if_ext_max ==
  3965. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3966. return error;
  3967. error2:
  3968. xfs_bmap_cancel(&flist);
  3969. error1:
  3970. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3971. error0:
  3972. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3973. ASSERT(ip->i_df.if_ext_max ==
  3974. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3975. return error;
  3976. }
  3977. /*
  3978. * Add the extent to the list of extents to be free at transaction end.
  3979. * The list is maintained sorted (by block number).
  3980. */
  3981. /* ARGSUSED */
  3982. void
  3983. xfs_bmap_add_free(
  3984. xfs_fsblock_t bno, /* fs block number of extent */
  3985. xfs_filblks_t len, /* length of extent */
  3986. xfs_bmap_free_t *flist, /* list of extents */
  3987. xfs_mount_t *mp) /* mount point structure */
  3988. {
  3989. xfs_bmap_free_item_t *cur; /* current (next) element */
  3990. xfs_bmap_free_item_t *new; /* new element */
  3991. xfs_bmap_free_item_t *prev; /* previous element */
  3992. #ifdef DEBUG
  3993. xfs_agnumber_t agno;
  3994. xfs_agblock_t agbno;
  3995. ASSERT(bno != NULLFSBLOCK);
  3996. ASSERT(len > 0);
  3997. ASSERT(len <= MAXEXTLEN);
  3998. ASSERT(!ISNULLSTARTBLOCK(bno));
  3999. agno = XFS_FSB_TO_AGNO(mp, bno);
  4000. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  4001. ASSERT(agno < mp->m_sb.sb_agcount);
  4002. ASSERT(agbno < mp->m_sb.sb_agblocks);
  4003. ASSERT(len < mp->m_sb.sb_agblocks);
  4004. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  4005. #endif
  4006. ASSERT(xfs_bmap_free_item_zone != NULL);
  4007. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  4008. new->xbfi_startblock = bno;
  4009. new->xbfi_blockcount = (xfs_extlen_t)len;
  4010. for (prev = NULL, cur = flist->xbf_first;
  4011. cur != NULL;
  4012. prev = cur, cur = cur->xbfi_next) {
  4013. if (cur->xbfi_startblock >= bno)
  4014. break;
  4015. }
  4016. if (prev)
  4017. prev->xbfi_next = new;
  4018. else
  4019. flist->xbf_first = new;
  4020. new->xbfi_next = cur;
  4021. flist->xbf_count++;
  4022. }
  4023. /*
  4024. * Compute and fill in the value of the maximum depth of a bmap btree
  4025. * in this filesystem. Done once, during mount.
  4026. */
  4027. void
  4028. xfs_bmap_compute_maxlevels(
  4029. xfs_mount_t *mp, /* file system mount structure */
  4030. int whichfork) /* data or attr fork */
  4031. {
  4032. int level; /* btree level */
  4033. uint maxblocks; /* max blocks at this level */
  4034. uint maxleafents; /* max leaf entries possible */
  4035. int maxrootrecs; /* max records in root block */
  4036. int minleafrecs; /* min records in leaf block */
  4037. int minnoderecs; /* min records in node block */
  4038. int sz; /* root block size */
  4039. /*
  4040. * The maximum number of extents in a file, hence the maximum
  4041. * number of leaf entries, is controlled by the type of di_nextents
  4042. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  4043. * (a signed 16-bit number, xfs_aextnum_t).
  4044. *
  4045. * Note that we can no longer assume that if we are in ATTR1 that
  4046. * the fork offset of all the inodes will be (m_attroffset >> 3)
  4047. * because we could have mounted with ATTR2 and then mounted back
  4048. * with ATTR1, keeping the di_forkoff's fixed but probably at
  4049. * various positions. Therefore, for both ATTR1 and ATTR2
  4050. * we have to assume the worst case scenario of a minimum size
  4051. * available.
  4052. */
  4053. if (whichfork == XFS_DATA_FORK) {
  4054. maxleafents = MAXEXTNUM;
  4055. sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  4056. } else {
  4057. maxleafents = MAXAEXTNUM;
  4058. sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
  4059. }
  4060. maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
  4061. minleafrecs = mp->m_bmap_dmnr[0];
  4062. minnoderecs = mp->m_bmap_dmnr[1];
  4063. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  4064. for (level = 1; maxblocks > 1; level++) {
  4065. if (maxblocks <= maxrootrecs)
  4066. maxblocks = 1;
  4067. else
  4068. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  4069. }
  4070. mp->m_bm_maxlevels[whichfork] = level;
  4071. }
  4072. /*
  4073. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  4074. * caller. Frees all the extents that need freeing, which must be done
  4075. * last due to locking considerations. We never free any extents in
  4076. * the first transaction. This is to allow the caller to make the first
  4077. * transaction a synchronous one so that the pointers to the data being
  4078. * broken in this transaction will be permanent before the data is actually
  4079. * freed. This is necessary to prevent blocks from being reallocated
  4080. * and written to before the free and reallocation are actually permanent.
  4081. * We do not just make the first transaction synchronous here, because
  4082. * there are more efficient ways to gain the same protection in some cases
  4083. * (see the file truncation code).
  4084. *
  4085. * Return 1 if the given transaction was committed and a new one
  4086. * started, and 0 otherwise in the committed parameter.
  4087. */
  4088. /*ARGSUSED*/
  4089. int /* error */
  4090. xfs_bmap_finish(
  4091. xfs_trans_t **tp, /* transaction pointer addr */
  4092. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4093. int *committed) /* xact committed or not */
  4094. {
  4095. xfs_efd_log_item_t *efd; /* extent free data */
  4096. xfs_efi_log_item_t *efi; /* extent free intention */
  4097. int error; /* error return value */
  4098. xfs_bmap_free_item_t *free; /* free extent item */
  4099. unsigned int logres; /* new log reservation */
  4100. unsigned int logcount; /* new log count */
  4101. xfs_mount_t *mp; /* filesystem mount structure */
  4102. xfs_bmap_free_item_t *next; /* next item on free list */
  4103. xfs_trans_t *ntp; /* new transaction pointer */
  4104. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  4105. if (flist->xbf_count == 0) {
  4106. *committed = 0;
  4107. return 0;
  4108. }
  4109. ntp = *tp;
  4110. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  4111. for (free = flist->xbf_first; free; free = free->xbfi_next)
  4112. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  4113. free->xbfi_blockcount);
  4114. logres = ntp->t_log_res;
  4115. logcount = ntp->t_log_count;
  4116. ntp = xfs_trans_dup(*tp);
  4117. error = xfs_trans_commit(*tp, 0);
  4118. *tp = ntp;
  4119. *committed = 1;
  4120. /*
  4121. * We have a new transaction, so we should return committed=1,
  4122. * even though we're returning an error.
  4123. */
  4124. if (error)
  4125. return error;
  4126. /*
  4127. * transaction commit worked ok so we can drop the extra ticket
  4128. * reference that we gained in xfs_trans_dup()
  4129. */
  4130. xfs_log_ticket_put(ntp->t_ticket);
  4131. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  4132. logcount)))
  4133. return error;
  4134. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  4135. for (free = flist->xbf_first; free != NULL; free = next) {
  4136. next = free->xbfi_next;
  4137. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  4138. free->xbfi_blockcount))) {
  4139. /*
  4140. * The bmap free list will be cleaned up at a
  4141. * higher level. The EFI will be canceled when
  4142. * this transaction is aborted.
  4143. * Need to force shutdown here to make sure it
  4144. * happens, since this transaction may not be
  4145. * dirty yet.
  4146. */
  4147. mp = ntp->t_mountp;
  4148. if (!XFS_FORCED_SHUTDOWN(mp))
  4149. xfs_force_shutdown(mp,
  4150. (error == EFSCORRUPTED) ?
  4151. SHUTDOWN_CORRUPT_INCORE :
  4152. SHUTDOWN_META_IO_ERROR);
  4153. return error;
  4154. }
  4155. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  4156. free->xbfi_blockcount);
  4157. xfs_bmap_del_free(flist, NULL, free);
  4158. }
  4159. return 0;
  4160. }
  4161. /*
  4162. * Free up any items left in the list.
  4163. */
  4164. void
  4165. xfs_bmap_cancel(
  4166. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  4167. {
  4168. xfs_bmap_free_item_t *free; /* free list item */
  4169. xfs_bmap_free_item_t *next;
  4170. if (flist->xbf_count == 0)
  4171. return;
  4172. ASSERT(flist->xbf_first != NULL);
  4173. for (free = flist->xbf_first; free; free = next) {
  4174. next = free->xbfi_next;
  4175. xfs_bmap_del_free(flist, NULL, free);
  4176. }
  4177. ASSERT(flist->xbf_count == 0);
  4178. }
  4179. /*
  4180. * Returns the file-relative block number of the first unused block(s)
  4181. * in the file with at least "len" logically contiguous blocks free.
  4182. * This is the lowest-address hole if the file has holes, else the first block
  4183. * past the end of file.
  4184. * Return 0 if the file is currently local (in-inode).
  4185. */
  4186. int /* error */
  4187. xfs_bmap_first_unused(
  4188. xfs_trans_t *tp, /* transaction pointer */
  4189. xfs_inode_t *ip, /* incore inode */
  4190. xfs_extlen_t len, /* size of hole to find */
  4191. xfs_fileoff_t *first_unused, /* unused block */
  4192. int whichfork) /* data or attr fork */
  4193. {
  4194. int error; /* error return value */
  4195. int idx; /* extent record index */
  4196. xfs_ifork_t *ifp; /* inode fork pointer */
  4197. xfs_fileoff_t lastaddr; /* last block number seen */
  4198. xfs_fileoff_t lowest; /* lowest useful block */
  4199. xfs_fileoff_t max; /* starting useful block */
  4200. xfs_fileoff_t off; /* offset for this block */
  4201. xfs_extnum_t nextents; /* number of extent entries */
  4202. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  4203. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  4204. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  4205. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4206. *first_unused = 0;
  4207. return 0;
  4208. }
  4209. ifp = XFS_IFORK_PTR(ip, whichfork);
  4210. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4211. (error = xfs_iread_extents(tp, ip, whichfork)))
  4212. return error;
  4213. lowest = *first_unused;
  4214. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4215. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  4216. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  4217. off = xfs_bmbt_get_startoff(ep);
  4218. /*
  4219. * See if the hole before this extent will work.
  4220. */
  4221. if (off >= lowest + len && off - max >= len) {
  4222. *first_unused = max;
  4223. return 0;
  4224. }
  4225. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  4226. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  4227. }
  4228. *first_unused = max;
  4229. return 0;
  4230. }
  4231. /*
  4232. * Returns the file-relative block number of the last block + 1 before
  4233. * last_block (input value) in the file.
  4234. * This is not based on i_size, it is based on the extent records.
  4235. * Returns 0 for local files, as they do not have extent records.
  4236. */
  4237. int /* error */
  4238. xfs_bmap_last_before(
  4239. xfs_trans_t *tp, /* transaction pointer */
  4240. xfs_inode_t *ip, /* incore inode */
  4241. xfs_fileoff_t *last_block, /* last block */
  4242. int whichfork) /* data or attr fork */
  4243. {
  4244. xfs_fileoff_t bno; /* input file offset */
  4245. int eof; /* hit end of file */
  4246. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  4247. int error; /* error return value */
  4248. xfs_bmbt_irec_t got; /* current extent value */
  4249. xfs_ifork_t *ifp; /* inode fork pointer */
  4250. xfs_extnum_t lastx; /* last extent used */
  4251. xfs_bmbt_irec_t prev; /* previous extent value */
  4252. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4253. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4254. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4255. return XFS_ERROR(EIO);
  4256. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4257. *last_block = 0;
  4258. return 0;
  4259. }
  4260. ifp = XFS_IFORK_PTR(ip, whichfork);
  4261. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4262. (error = xfs_iread_extents(tp, ip, whichfork)))
  4263. return error;
  4264. bno = *last_block - 1;
  4265. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4266. &prev);
  4267. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  4268. if (prev.br_startoff == NULLFILEOFF)
  4269. *last_block = 0;
  4270. else
  4271. *last_block = prev.br_startoff + prev.br_blockcount;
  4272. }
  4273. /*
  4274. * Otherwise *last_block is already the right answer.
  4275. */
  4276. return 0;
  4277. }
  4278. /*
  4279. * Returns the file-relative block number of the first block past eof in
  4280. * the file. This is not based on i_size, it is based on the extent records.
  4281. * Returns 0 for local files, as they do not have extent records.
  4282. */
  4283. int /* error */
  4284. xfs_bmap_last_offset(
  4285. xfs_trans_t *tp, /* transaction pointer */
  4286. xfs_inode_t *ip, /* incore inode */
  4287. xfs_fileoff_t *last_block, /* last block */
  4288. int whichfork) /* data or attr fork */
  4289. {
  4290. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  4291. int error; /* error return value */
  4292. xfs_ifork_t *ifp; /* inode fork pointer */
  4293. xfs_extnum_t nextents; /* number of extent entries */
  4294. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4295. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4296. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4297. return XFS_ERROR(EIO);
  4298. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4299. *last_block = 0;
  4300. return 0;
  4301. }
  4302. ifp = XFS_IFORK_PTR(ip, whichfork);
  4303. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4304. (error = xfs_iread_extents(tp, ip, whichfork)))
  4305. return error;
  4306. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4307. if (!nextents) {
  4308. *last_block = 0;
  4309. return 0;
  4310. }
  4311. ep = xfs_iext_get_ext(ifp, nextents - 1);
  4312. *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
  4313. return 0;
  4314. }
  4315. /*
  4316. * Returns whether the selected fork of the inode has exactly one
  4317. * block or not. For the data fork we check this matches di_size,
  4318. * implying the file's range is 0..bsize-1.
  4319. */
  4320. int /* 1=>1 block, 0=>otherwise */
  4321. xfs_bmap_one_block(
  4322. xfs_inode_t *ip, /* incore inode */
  4323. int whichfork) /* data or attr fork */
  4324. {
  4325. xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
  4326. xfs_ifork_t *ifp; /* inode fork pointer */
  4327. int rval; /* return value */
  4328. xfs_bmbt_irec_t s; /* internal version of extent */
  4329. #ifndef DEBUG
  4330. if (whichfork == XFS_DATA_FORK) {
  4331. return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
  4332. (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
  4333. (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
  4334. }
  4335. #endif /* !DEBUG */
  4336. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  4337. return 0;
  4338. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4339. return 0;
  4340. ifp = XFS_IFORK_PTR(ip, whichfork);
  4341. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  4342. ep = xfs_iext_get_ext(ifp, 0);
  4343. xfs_bmbt_get_all(ep, &s);
  4344. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  4345. if (rval && whichfork == XFS_DATA_FORK)
  4346. ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
  4347. return rval;
  4348. }
  4349. STATIC int
  4350. xfs_bmap_sanity_check(
  4351. struct xfs_mount *mp,
  4352. struct xfs_buf *bp,
  4353. int level)
  4354. {
  4355. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  4356. if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
  4357. be16_to_cpu(block->bb_level) != level ||
  4358. be16_to_cpu(block->bb_numrecs) == 0 ||
  4359. be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  4360. return 0;
  4361. return 1;
  4362. }
  4363. /*
  4364. * Read in the extents to if_extents.
  4365. * All inode fields are set up by caller, we just traverse the btree
  4366. * and copy the records in. If the file system cannot contain unwritten
  4367. * extents, the records are checked for no "state" flags.
  4368. */
  4369. int /* error */
  4370. xfs_bmap_read_extents(
  4371. xfs_trans_t *tp, /* transaction pointer */
  4372. xfs_inode_t *ip, /* incore inode */
  4373. int whichfork) /* data or attr fork */
  4374. {
  4375. struct xfs_btree_block *block; /* current btree block */
  4376. xfs_fsblock_t bno; /* block # of "block" */
  4377. xfs_buf_t *bp; /* buffer for "block" */
  4378. int error; /* error return value */
  4379. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  4380. xfs_extnum_t i, j; /* index into the extents list */
  4381. xfs_ifork_t *ifp; /* fork structure */
  4382. int level; /* btree level, for checking */
  4383. xfs_mount_t *mp; /* file system mount structure */
  4384. __be64 *pp; /* pointer to block address */
  4385. /* REFERENCED */
  4386. xfs_extnum_t room; /* number of entries there's room for */
  4387. bno = NULLFSBLOCK;
  4388. mp = ip->i_mount;
  4389. ifp = XFS_IFORK_PTR(ip, whichfork);
  4390. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  4391. XFS_EXTFMT_INODE(ip);
  4392. block = ifp->if_broot;
  4393. /*
  4394. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  4395. */
  4396. level = be16_to_cpu(block->bb_level);
  4397. ASSERT(level > 0);
  4398. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  4399. bno = be64_to_cpu(*pp);
  4400. ASSERT(bno != NULLDFSBNO);
  4401. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  4402. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  4403. /*
  4404. * Go down the tree until leaf level is reached, following the first
  4405. * pointer (leftmost) at each level.
  4406. */
  4407. while (level-- > 0) {
  4408. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4409. XFS_BMAP_BTREE_REF)))
  4410. return error;
  4411. block = XFS_BUF_TO_BLOCK(bp);
  4412. XFS_WANT_CORRUPTED_GOTO(
  4413. xfs_bmap_sanity_check(mp, bp, level),
  4414. error0);
  4415. if (level == 0)
  4416. break;
  4417. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  4418. bno = be64_to_cpu(*pp);
  4419. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  4420. xfs_trans_brelse(tp, bp);
  4421. }
  4422. /*
  4423. * Here with bp and block set to the leftmost leaf node in the tree.
  4424. */
  4425. room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4426. i = 0;
  4427. /*
  4428. * Loop over all leaf nodes. Copy information to the extent records.
  4429. */
  4430. for (;;) {
  4431. xfs_bmbt_rec_t *frp;
  4432. xfs_fsblock_t nextbno;
  4433. xfs_extnum_t num_recs;
  4434. xfs_extnum_t start;
  4435. num_recs = xfs_btree_get_numrecs(block);
  4436. if (unlikely(i + num_recs > room)) {
  4437. ASSERT(i + num_recs <= room);
  4438. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  4439. "corrupt dinode %Lu, (btree extents).",
  4440. (unsigned long long) ip->i_ino);
  4441. XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
  4442. XFS_ERRLEVEL_LOW,
  4443. ip->i_mount);
  4444. goto error0;
  4445. }
  4446. XFS_WANT_CORRUPTED_GOTO(
  4447. xfs_bmap_sanity_check(mp, bp, 0),
  4448. error0);
  4449. /*
  4450. * Read-ahead the next leaf block, if any.
  4451. */
  4452. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  4453. if (nextbno != NULLFSBLOCK)
  4454. xfs_btree_reada_bufl(mp, nextbno, 1);
  4455. /*
  4456. * Copy records into the extent records.
  4457. */
  4458. frp = XFS_BMBT_REC_ADDR(mp, block, 1);
  4459. start = i;
  4460. for (j = 0; j < num_recs; j++, i++, frp++) {
  4461. xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
  4462. trp->l0 = be64_to_cpu(frp->l0);
  4463. trp->l1 = be64_to_cpu(frp->l1);
  4464. }
  4465. if (exntf == XFS_EXTFMT_NOSTATE) {
  4466. /*
  4467. * Check all attribute bmap btree records and
  4468. * any "older" data bmap btree records for a
  4469. * set bit in the "extent flag" position.
  4470. */
  4471. if (unlikely(xfs_check_nostate_extents(ifp,
  4472. start, num_recs))) {
  4473. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  4474. XFS_ERRLEVEL_LOW,
  4475. ip->i_mount);
  4476. goto error0;
  4477. }
  4478. }
  4479. xfs_trans_brelse(tp, bp);
  4480. bno = nextbno;
  4481. /*
  4482. * If we've reached the end, stop.
  4483. */
  4484. if (bno == NULLFSBLOCK)
  4485. break;
  4486. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4487. XFS_BMAP_BTREE_REF)))
  4488. return error;
  4489. block = XFS_BUF_TO_BLOCK(bp);
  4490. }
  4491. ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4492. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  4493. XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
  4494. return 0;
  4495. error0:
  4496. xfs_trans_brelse(tp, bp);
  4497. return XFS_ERROR(EFSCORRUPTED);
  4498. }
  4499. #ifdef XFS_BMAP_TRACE
  4500. /*
  4501. * Add bmap trace insert entries for all the contents of the extent records.
  4502. */
  4503. void
  4504. xfs_bmap_trace_exlist(
  4505. const char *fname, /* function name */
  4506. xfs_inode_t *ip, /* incore inode pointer */
  4507. xfs_extnum_t cnt, /* count of entries in the list */
  4508. int whichfork) /* data or attr fork */
  4509. {
  4510. xfs_bmbt_rec_host_t *ep; /* current extent record */
  4511. xfs_extnum_t idx; /* extent record index */
  4512. xfs_ifork_t *ifp; /* inode fork pointer */
  4513. xfs_bmbt_irec_t s; /* file extent record */
  4514. ifp = XFS_IFORK_PTR(ip, whichfork);
  4515. ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4516. for (idx = 0; idx < cnt; idx++) {
  4517. ep = xfs_iext_get_ext(ifp, idx);
  4518. xfs_bmbt_get_all(ep, &s);
  4519. XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
  4520. whichfork);
  4521. }
  4522. }
  4523. #endif
  4524. #ifdef DEBUG
  4525. /*
  4526. * Validate that the bmbt_irecs being returned from bmapi are valid
  4527. * given the callers original parameters. Specifically check the
  4528. * ranges of the returned irecs to ensure that they only extent beyond
  4529. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  4530. */
  4531. STATIC void
  4532. xfs_bmap_validate_ret(
  4533. xfs_fileoff_t bno,
  4534. xfs_filblks_t len,
  4535. int flags,
  4536. xfs_bmbt_irec_t *mval,
  4537. int nmap,
  4538. int ret_nmap)
  4539. {
  4540. int i; /* index to map values */
  4541. ASSERT(ret_nmap <= nmap);
  4542. for (i = 0; i < ret_nmap; i++) {
  4543. ASSERT(mval[i].br_blockcount > 0);
  4544. if (!(flags & XFS_BMAPI_ENTIRE)) {
  4545. ASSERT(mval[i].br_startoff >= bno);
  4546. ASSERT(mval[i].br_blockcount <= len);
  4547. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  4548. bno + len);
  4549. } else {
  4550. ASSERT(mval[i].br_startoff < bno + len);
  4551. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  4552. bno);
  4553. }
  4554. ASSERT(i == 0 ||
  4555. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  4556. mval[i].br_startoff);
  4557. if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
  4558. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  4559. mval[i].br_startblock != HOLESTARTBLOCK);
  4560. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  4561. mval[i].br_state == XFS_EXT_UNWRITTEN);
  4562. }
  4563. }
  4564. #endif /* DEBUG */
  4565. /*
  4566. * Map file blocks to filesystem blocks.
  4567. * File range is given by the bno/len pair.
  4568. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  4569. * into a hole or past eof.
  4570. * Only allocates blocks from a single allocation group,
  4571. * to avoid locking problems.
  4572. * The returned value in "firstblock" from the first call in a transaction
  4573. * must be remembered and presented to subsequent calls in "firstblock".
  4574. * An upper bound for the number of blocks to be allocated is supplied to
  4575. * the first call in "total"; if no allocation group has that many free
  4576. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4577. */
  4578. int /* error */
  4579. xfs_bmapi(
  4580. xfs_trans_t *tp, /* transaction pointer */
  4581. xfs_inode_t *ip, /* incore inode */
  4582. xfs_fileoff_t bno, /* starting file offs. mapped */
  4583. xfs_filblks_t len, /* length to map in file */
  4584. int flags, /* XFS_BMAPI_... */
  4585. xfs_fsblock_t *firstblock, /* first allocated block
  4586. controls a.g. for allocs */
  4587. xfs_extlen_t total, /* total blocks needed */
  4588. xfs_bmbt_irec_t *mval, /* output: map values */
  4589. int *nmap, /* i/o: mval size/count */
  4590. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4591. xfs_extdelta_t *delta) /* o: change made to incore extents */
  4592. {
  4593. xfs_fsblock_t abno; /* allocated block number */
  4594. xfs_extlen_t alen; /* allocated extent length */
  4595. xfs_fileoff_t aoff; /* allocated file offset */
  4596. xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
  4597. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4598. xfs_fileoff_t end; /* end of mapped file region */
  4599. int eof; /* we've hit the end of extents */
  4600. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4601. int error; /* error return */
  4602. xfs_bmbt_irec_t got; /* current file extent record */
  4603. xfs_ifork_t *ifp; /* inode fork pointer */
  4604. xfs_extlen_t indlen; /* indirect blocks length */
  4605. xfs_extnum_t lastx; /* last useful extent number */
  4606. int logflags; /* flags for transaction logging */
  4607. xfs_extlen_t minleft; /* min blocks left after allocation */
  4608. xfs_extlen_t minlen; /* min allocation size */
  4609. xfs_mount_t *mp; /* xfs mount structure */
  4610. int n; /* current extent index */
  4611. int nallocs; /* number of extents alloc\'d */
  4612. xfs_extnum_t nextents; /* number of extents in file */
  4613. xfs_fileoff_t obno; /* old block number (offset) */
  4614. xfs_bmbt_irec_t prev; /* previous file extent record */
  4615. int tmp_logflags; /* temp flags holder */
  4616. int whichfork; /* data or attr fork */
  4617. char inhole; /* current location is hole in file */
  4618. char wasdelay; /* old extent was delayed */
  4619. char wr; /* this is a write request */
  4620. char rt; /* this is a realtime file */
  4621. #ifdef DEBUG
  4622. xfs_fileoff_t orig_bno; /* original block number value */
  4623. int orig_flags; /* original flags arg value */
  4624. xfs_filblks_t orig_len; /* original value of len arg */
  4625. xfs_bmbt_irec_t *orig_mval; /* original value of mval */
  4626. int orig_nmap; /* original value of *nmap */
  4627. orig_bno = bno;
  4628. orig_len = len;
  4629. orig_flags = flags;
  4630. orig_mval = mval;
  4631. orig_nmap = *nmap;
  4632. #endif
  4633. ASSERT(*nmap >= 1);
  4634. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
  4635. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4636. XFS_ATTR_FORK : XFS_DATA_FORK;
  4637. mp = ip->i_mount;
  4638. if (unlikely(XFS_TEST_ERROR(
  4639. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4640. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4641. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4642. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4643. XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
  4644. return XFS_ERROR(EFSCORRUPTED);
  4645. }
  4646. if (XFS_FORCED_SHUTDOWN(mp))
  4647. return XFS_ERROR(EIO);
  4648. rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4649. ifp = XFS_IFORK_PTR(ip, whichfork);
  4650. ASSERT(ifp->if_ext_max ==
  4651. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4652. if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
  4653. XFS_STATS_INC(xs_blk_mapw);
  4654. else
  4655. XFS_STATS_INC(xs_blk_mapr);
  4656. /*
  4657. * IGSTATE flag is used to combine extents which
  4658. * differ only due to the state of the extents.
  4659. * This technique is used from xfs_getbmap()
  4660. * when the caller does not wish to see the
  4661. * separation (which is the default).
  4662. *
  4663. * This technique is also used when writing a
  4664. * buffer which has been partially written,
  4665. * (usually by being flushed during a chunkread),
  4666. * to ensure one write takes place. This also
  4667. * prevents a change in the xfs inode extents at
  4668. * this time, intentionally. This change occurs
  4669. * on completion of the write operation, in
  4670. * xfs_strat_comp(), where the xfs_bmapi() call
  4671. * is transactioned, and the extents combined.
  4672. */
  4673. if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
  4674. wr = 0; /* no allocations are allowed */
  4675. ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
  4676. logflags = 0;
  4677. nallocs = 0;
  4678. cur = NULL;
  4679. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4680. ASSERT(wr && tp);
  4681. if ((error = xfs_bmap_local_to_extents(tp, ip,
  4682. firstblock, total, &logflags, whichfork)))
  4683. goto error0;
  4684. }
  4685. if (wr && *firstblock == NULLFSBLOCK) {
  4686. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4687. minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4688. else
  4689. minleft = 1;
  4690. } else
  4691. minleft = 0;
  4692. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4693. (error = xfs_iread_extents(tp, ip, whichfork)))
  4694. goto error0;
  4695. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4696. &prev);
  4697. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4698. n = 0;
  4699. end = bno + len;
  4700. obno = bno;
  4701. bma.ip = NULL;
  4702. if (delta) {
  4703. delta->xed_startoff = NULLFILEOFF;
  4704. delta->xed_blockcount = 0;
  4705. }
  4706. while (bno < end && n < *nmap) {
  4707. /*
  4708. * Reading past eof, act as though there's a hole
  4709. * up to end.
  4710. */
  4711. if (eof && !wr)
  4712. got.br_startoff = end;
  4713. inhole = eof || got.br_startoff > bno;
  4714. wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
  4715. ISNULLSTARTBLOCK(got.br_startblock);
  4716. /*
  4717. * First, deal with the hole before the allocated space
  4718. * that we found, if any.
  4719. */
  4720. if (wr && (inhole || wasdelay)) {
  4721. /*
  4722. * For the wasdelay case, we could also just
  4723. * allocate the stuff asked for in this bmap call
  4724. * but that wouldn't be as good.
  4725. */
  4726. if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
  4727. alen = (xfs_extlen_t)got.br_blockcount;
  4728. aoff = got.br_startoff;
  4729. if (lastx != NULLEXTNUM && lastx) {
  4730. ep = xfs_iext_get_ext(ifp, lastx - 1);
  4731. xfs_bmbt_get_all(ep, &prev);
  4732. }
  4733. } else if (wasdelay) {
  4734. alen = (xfs_extlen_t)
  4735. XFS_FILBLKS_MIN(len,
  4736. (got.br_startoff +
  4737. got.br_blockcount) - bno);
  4738. aoff = bno;
  4739. } else {
  4740. alen = (xfs_extlen_t)
  4741. XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4742. if (!eof)
  4743. alen = (xfs_extlen_t)
  4744. XFS_FILBLKS_MIN(alen,
  4745. got.br_startoff - bno);
  4746. aoff = bno;
  4747. }
  4748. minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
  4749. if (flags & XFS_BMAPI_DELAY) {
  4750. xfs_extlen_t extsz;
  4751. /* Figure out the extent size, adjust alen */
  4752. extsz = xfs_get_extsz_hint(ip);
  4753. if (extsz) {
  4754. error = xfs_bmap_extsize_align(mp,
  4755. &got, &prev, extsz,
  4756. rt, eof,
  4757. flags&XFS_BMAPI_DELAY,
  4758. flags&XFS_BMAPI_CONVERT,
  4759. &aoff, &alen);
  4760. ASSERT(!error);
  4761. }
  4762. if (rt)
  4763. extsz = alen / mp->m_sb.sb_rextsize;
  4764. /*
  4765. * Make a transaction-less quota reservation for
  4766. * delayed allocation blocks. This number gets
  4767. * adjusted later. We return if we haven't
  4768. * allocated blocks already inside this loop.
  4769. */
  4770. if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
  4771. mp, NULL, ip, (long)alen, 0,
  4772. rt ? XFS_QMOPT_RES_RTBLKS :
  4773. XFS_QMOPT_RES_REGBLKS))) {
  4774. if (n == 0) {
  4775. *nmap = 0;
  4776. ASSERT(cur == NULL);
  4777. return error;
  4778. }
  4779. break;
  4780. }
  4781. /*
  4782. * Split changing sb for alen and indlen since
  4783. * they could be coming from different places.
  4784. */
  4785. indlen = (xfs_extlen_t)
  4786. xfs_bmap_worst_indlen(ip, alen);
  4787. ASSERT(indlen > 0);
  4788. if (rt) {
  4789. error = xfs_mod_incore_sb(mp,
  4790. XFS_SBS_FREXTENTS,
  4791. -((int64_t)extsz), (flags &
  4792. XFS_BMAPI_RSVBLOCKS));
  4793. } else {
  4794. error = xfs_mod_incore_sb(mp,
  4795. XFS_SBS_FDBLOCKS,
  4796. -((int64_t)alen), (flags &
  4797. XFS_BMAPI_RSVBLOCKS));
  4798. }
  4799. if (!error) {
  4800. error = xfs_mod_incore_sb(mp,
  4801. XFS_SBS_FDBLOCKS,
  4802. -((int64_t)indlen), (flags &
  4803. XFS_BMAPI_RSVBLOCKS));
  4804. if (error && rt)
  4805. xfs_mod_incore_sb(mp,
  4806. XFS_SBS_FREXTENTS,
  4807. (int64_t)extsz, (flags &
  4808. XFS_BMAPI_RSVBLOCKS));
  4809. else if (error)
  4810. xfs_mod_incore_sb(mp,
  4811. XFS_SBS_FDBLOCKS,
  4812. (int64_t)alen, (flags &
  4813. XFS_BMAPI_RSVBLOCKS));
  4814. }
  4815. if (error) {
  4816. if (XFS_IS_QUOTA_ON(mp))
  4817. /* unreserve the blocks now */
  4818. (void)
  4819. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
  4820. mp, NULL, ip,
  4821. (long)alen, 0, rt ?
  4822. XFS_QMOPT_RES_RTBLKS :
  4823. XFS_QMOPT_RES_REGBLKS);
  4824. break;
  4825. }
  4826. ip->i_delayed_blks += alen;
  4827. abno = NULLSTARTBLOCK(indlen);
  4828. } else {
  4829. /*
  4830. * If first time, allocate and fill in
  4831. * once-only bma fields.
  4832. */
  4833. if (bma.ip == NULL) {
  4834. bma.tp = tp;
  4835. bma.ip = ip;
  4836. bma.prevp = &prev;
  4837. bma.gotp = &got;
  4838. bma.total = total;
  4839. bma.userdata = 0;
  4840. }
  4841. /* Indicate if this is the first user data
  4842. * in the file, or just any user data.
  4843. */
  4844. if (!(flags & XFS_BMAPI_METADATA)) {
  4845. bma.userdata = (aoff == 0) ?
  4846. XFS_ALLOC_INITIAL_USER_DATA :
  4847. XFS_ALLOC_USERDATA;
  4848. }
  4849. /*
  4850. * Fill in changeable bma fields.
  4851. */
  4852. bma.eof = eof;
  4853. bma.firstblock = *firstblock;
  4854. bma.alen = alen;
  4855. bma.off = aoff;
  4856. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4857. bma.wasdel = wasdelay;
  4858. bma.minlen = minlen;
  4859. bma.low = flist->xbf_low;
  4860. bma.minleft = minleft;
  4861. /*
  4862. * Only want to do the alignment at the
  4863. * eof if it is userdata and allocation length
  4864. * is larger than a stripe unit.
  4865. */
  4866. if (mp->m_dalign && alen >= mp->m_dalign &&
  4867. (!(flags & XFS_BMAPI_METADATA)) &&
  4868. (whichfork == XFS_DATA_FORK)) {
  4869. if ((error = xfs_bmap_isaeof(ip, aoff,
  4870. whichfork, &bma.aeof)))
  4871. goto error0;
  4872. } else
  4873. bma.aeof = 0;
  4874. /*
  4875. * Call allocator.
  4876. */
  4877. if ((error = xfs_bmap_alloc(&bma)))
  4878. goto error0;
  4879. /*
  4880. * Copy out result fields.
  4881. */
  4882. abno = bma.rval;
  4883. if ((flist->xbf_low = bma.low))
  4884. minleft = 0;
  4885. alen = bma.alen;
  4886. aoff = bma.off;
  4887. ASSERT(*firstblock == NULLFSBLOCK ||
  4888. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4889. XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
  4890. (flist->xbf_low &&
  4891. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4892. XFS_FSB_TO_AGNO(mp, bma.firstblock)));
  4893. *firstblock = bma.firstblock;
  4894. if (cur)
  4895. cur->bc_private.b.firstblock =
  4896. *firstblock;
  4897. if (abno == NULLFSBLOCK)
  4898. break;
  4899. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4900. cur = xfs_bmbt_init_cursor(mp, tp,
  4901. ip, whichfork);
  4902. cur->bc_private.b.firstblock =
  4903. *firstblock;
  4904. cur->bc_private.b.flist = flist;
  4905. }
  4906. /*
  4907. * Bump the number of extents we've allocated
  4908. * in this call.
  4909. */
  4910. nallocs++;
  4911. }
  4912. if (cur)
  4913. cur->bc_private.b.flags =
  4914. wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
  4915. got.br_startoff = aoff;
  4916. got.br_startblock = abno;
  4917. got.br_blockcount = alen;
  4918. got.br_state = XFS_EXT_NORM; /* assume normal */
  4919. /*
  4920. * Determine state of extent, and the filesystem.
  4921. * A wasdelay extent has been initialized, so
  4922. * shouldn't be flagged as unwritten.
  4923. */
  4924. if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4925. if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
  4926. got.br_state = XFS_EXT_UNWRITTEN;
  4927. }
  4928. error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
  4929. firstblock, flist, &tmp_logflags, delta,
  4930. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4931. logflags |= tmp_logflags;
  4932. if (error)
  4933. goto error0;
  4934. lastx = ifp->if_lastex;
  4935. ep = xfs_iext_get_ext(ifp, lastx);
  4936. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4937. xfs_bmbt_get_all(ep, &got);
  4938. ASSERT(got.br_startoff <= aoff);
  4939. ASSERT(got.br_startoff + got.br_blockcount >=
  4940. aoff + alen);
  4941. #ifdef DEBUG
  4942. if (flags & XFS_BMAPI_DELAY) {
  4943. ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
  4944. ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
  4945. }
  4946. ASSERT(got.br_state == XFS_EXT_NORM ||
  4947. got.br_state == XFS_EXT_UNWRITTEN);
  4948. #endif
  4949. /*
  4950. * Fall down into the found allocated space case.
  4951. */
  4952. } else if (inhole) {
  4953. /*
  4954. * Reading in a hole.
  4955. */
  4956. mval->br_startoff = bno;
  4957. mval->br_startblock = HOLESTARTBLOCK;
  4958. mval->br_blockcount =
  4959. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4960. mval->br_state = XFS_EXT_NORM;
  4961. bno += mval->br_blockcount;
  4962. len -= mval->br_blockcount;
  4963. mval++;
  4964. n++;
  4965. continue;
  4966. }
  4967. /*
  4968. * Then deal with the allocated space we found.
  4969. */
  4970. ASSERT(ep != NULL);
  4971. if (!(flags & XFS_BMAPI_ENTIRE) &&
  4972. (got.br_startoff + got.br_blockcount > obno)) {
  4973. if (obno > bno)
  4974. bno = obno;
  4975. ASSERT((bno >= obno) || (n == 0));
  4976. ASSERT(bno < end);
  4977. mval->br_startoff = bno;
  4978. if (ISNULLSTARTBLOCK(got.br_startblock)) {
  4979. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4980. mval->br_startblock = DELAYSTARTBLOCK;
  4981. } else
  4982. mval->br_startblock =
  4983. got.br_startblock +
  4984. (bno - got.br_startoff);
  4985. /*
  4986. * Return the minimum of what we got and what we
  4987. * asked for for the length. We can use the len
  4988. * variable here because it is modified below
  4989. * and we could have been there before coming
  4990. * here if the first part of the allocation
  4991. * didn't overlap what was asked for.
  4992. */
  4993. mval->br_blockcount =
  4994. XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
  4995. (bno - got.br_startoff));
  4996. mval->br_state = got.br_state;
  4997. ASSERT(mval->br_blockcount <= len);
  4998. } else {
  4999. *mval = got;
  5000. if (ISNULLSTARTBLOCK(mval->br_startblock)) {
  5001. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  5002. mval->br_startblock = DELAYSTARTBLOCK;
  5003. }
  5004. }
  5005. /*
  5006. * Check if writing previously allocated but
  5007. * unwritten extents.
  5008. */
  5009. if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
  5010. ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
  5011. /*
  5012. * Modify (by adding) the state flag, if writing.
  5013. */
  5014. ASSERT(mval->br_blockcount <= len);
  5015. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  5016. cur = xfs_bmbt_init_cursor(mp,
  5017. tp, ip, whichfork);
  5018. cur->bc_private.b.firstblock =
  5019. *firstblock;
  5020. cur->bc_private.b.flist = flist;
  5021. }
  5022. mval->br_state = XFS_EXT_NORM;
  5023. error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
  5024. firstblock, flist, &tmp_logflags, delta,
  5025. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  5026. logflags |= tmp_logflags;
  5027. if (error)
  5028. goto error0;
  5029. lastx = ifp->if_lastex;
  5030. ep = xfs_iext_get_ext(ifp, lastx);
  5031. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5032. xfs_bmbt_get_all(ep, &got);
  5033. /*
  5034. * We may have combined previously unwritten
  5035. * space with written space, so generate
  5036. * another request.
  5037. */
  5038. if (mval->br_blockcount < len)
  5039. continue;
  5040. }
  5041. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  5042. ((mval->br_startoff + mval->br_blockcount) <= end));
  5043. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  5044. (mval->br_blockcount <= len) ||
  5045. (mval->br_startoff < obno));
  5046. bno = mval->br_startoff + mval->br_blockcount;
  5047. len = end - bno;
  5048. if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  5049. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  5050. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  5051. ASSERT(mval->br_state == mval[-1].br_state);
  5052. mval[-1].br_blockcount = mval->br_blockcount;
  5053. mval[-1].br_state = mval->br_state;
  5054. } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  5055. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  5056. mval[-1].br_startblock != HOLESTARTBLOCK &&
  5057. mval->br_startblock ==
  5058. mval[-1].br_startblock + mval[-1].br_blockcount &&
  5059. ((flags & XFS_BMAPI_IGSTATE) ||
  5060. mval[-1].br_state == mval->br_state)) {
  5061. ASSERT(mval->br_startoff ==
  5062. mval[-1].br_startoff + mval[-1].br_blockcount);
  5063. mval[-1].br_blockcount += mval->br_blockcount;
  5064. } else if (n > 0 &&
  5065. mval->br_startblock == DELAYSTARTBLOCK &&
  5066. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  5067. mval->br_startoff ==
  5068. mval[-1].br_startoff + mval[-1].br_blockcount) {
  5069. mval[-1].br_blockcount += mval->br_blockcount;
  5070. mval[-1].br_state = mval->br_state;
  5071. } else if (!((n == 0) &&
  5072. ((mval->br_startoff + mval->br_blockcount) <=
  5073. obno))) {
  5074. mval++;
  5075. n++;
  5076. }
  5077. /*
  5078. * If we're done, stop now. Stop when we've allocated
  5079. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  5080. * the transaction may get too big.
  5081. */
  5082. if (bno >= end || n >= *nmap || nallocs >= *nmap)
  5083. break;
  5084. /*
  5085. * Else go on to the next record.
  5086. */
  5087. ep = xfs_iext_get_ext(ifp, ++lastx);
  5088. prev = got;
  5089. if (lastx >= nextents)
  5090. eof = 1;
  5091. else
  5092. xfs_bmbt_get_all(ep, &got);
  5093. }
  5094. ifp->if_lastex = lastx;
  5095. *nmap = n;
  5096. /*
  5097. * Transform from btree to extents, give it cur.
  5098. */
  5099. if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5100. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5101. ASSERT(wr && cur);
  5102. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  5103. &tmp_logflags, whichfork);
  5104. logflags |= tmp_logflags;
  5105. if (error)
  5106. goto error0;
  5107. }
  5108. ASSERT(ifp->if_ext_max ==
  5109. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5110. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  5111. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
  5112. error = 0;
  5113. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5114. /* A change was actually made.
  5115. * Note that delta->xed_blockount is an offset at this
  5116. * point and needs to be converted to a block count.
  5117. */
  5118. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5119. delta->xed_blockcount -= delta->xed_startoff;
  5120. }
  5121. error0:
  5122. /*
  5123. * Log everything. Do this after conversion, there's no point in
  5124. * logging the extent records if we've converted to btree format.
  5125. */
  5126. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  5127. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5128. logflags &= ~XFS_ILOG_FEXT(whichfork);
  5129. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  5130. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5131. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  5132. /*
  5133. * Log whatever the flags say, even if error. Otherwise we might miss
  5134. * detecting a case where the data is changed, there's an error,
  5135. * and it's not logged so we don't shutdown when we should.
  5136. */
  5137. if (logflags) {
  5138. ASSERT(tp && wr);
  5139. xfs_trans_log_inode(tp, ip, logflags);
  5140. }
  5141. if (cur) {
  5142. if (!error) {
  5143. ASSERT(*firstblock == NULLFSBLOCK ||
  5144. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  5145. XFS_FSB_TO_AGNO(mp,
  5146. cur->bc_private.b.firstblock) ||
  5147. (flist->xbf_low &&
  5148. XFS_FSB_TO_AGNO(mp, *firstblock) <
  5149. XFS_FSB_TO_AGNO(mp,
  5150. cur->bc_private.b.firstblock)));
  5151. *firstblock = cur->bc_private.b.firstblock;
  5152. }
  5153. xfs_btree_del_cursor(cur,
  5154. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5155. }
  5156. if (!error)
  5157. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  5158. orig_nmap, *nmap);
  5159. return error;
  5160. }
  5161. /*
  5162. * Map file blocks to filesystem blocks, simple version.
  5163. * One block (extent) only, read-only.
  5164. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  5165. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  5166. * was set and all the others were clear.
  5167. */
  5168. int /* error */
  5169. xfs_bmapi_single(
  5170. xfs_trans_t *tp, /* transaction pointer */
  5171. xfs_inode_t *ip, /* incore inode */
  5172. int whichfork, /* data or attr fork */
  5173. xfs_fsblock_t *fsb, /* output: mapped block */
  5174. xfs_fileoff_t bno) /* starting file offs. mapped */
  5175. {
  5176. int eof; /* we've hit the end of extents */
  5177. int error; /* error return */
  5178. xfs_bmbt_irec_t got; /* current file extent record */
  5179. xfs_ifork_t *ifp; /* inode fork pointer */
  5180. xfs_extnum_t lastx; /* last useful extent number */
  5181. xfs_bmbt_irec_t prev; /* previous file extent record */
  5182. ifp = XFS_IFORK_PTR(ip, whichfork);
  5183. if (unlikely(
  5184. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  5185. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
  5186. XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
  5187. ip->i_mount);
  5188. return XFS_ERROR(EFSCORRUPTED);
  5189. }
  5190. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  5191. return XFS_ERROR(EIO);
  5192. XFS_STATS_INC(xs_blk_mapr);
  5193. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5194. (error = xfs_iread_extents(tp, ip, whichfork)))
  5195. return error;
  5196. (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5197. &prev);
  5198. /*
  5199. * Reading past eof, act as though there's a hole
  5200. * up to end.
  5201. */
  5202. if (eof || got.br_startoff > bno) {
  5203. *fsb = NULLFSBLOCK;
  5204. return 0;
  5205. }
  5206. ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
  5207. ASSERT(bno < got.br_startoff + got.br_blockcount);
  5208. *fsb = got.br_startblock + (bno - got.br_startoff);
  5209. ifp->if_lastex = lastx;
  5210. return 0;
  5211. }
  5212. /*
  5213. * Unmap (remove) blocks from a file.
  5214. * If nexts is nonzero then the number of extents to remove is limited to
  5215. * that value. If not all extents in the block range can be removed then
  5216. * *done is set.
  5217. */
  5218. int /* error */
  5219. xfs_bunmapi(
  5220. xfs_trans_t *tp, /* transaction pointer */
  5221. struct xfs_inode *ip, /* incore inode */
  5222. xfs_fileoff_t bno, /* starting offset to unmap */
  5223. xfs_filblks_t len, /* length to unmap in file */
  5224. int flags, /* misc flags */
  5225. xfs_extnum_t nexts, /* number of extents max */
  5226. xfs_fsblock_t *firstblock, /* first allocated block
  5227. controls a.g. for allocs */
  5228. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  5229. xfs_extdelta_t *delta, /* o: change made to incore
  5230. extents */
  5231. int *done) /* set if not done yet */
  5232. {
  5233. xfs_btree_cur_t *cur; /* bmap btree cursor */
  5234. xfs_bmbt_irec_t del; /* extent being deleted */
  5235. int eof; /* is deleting at eof */
  5236. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  5237. int error; /* error return value */
  5238. xfs_extnum_t extno; /* extent number in list */
  5239. xfs_bmbt_irec_t got; /* current extent record */
  5240. xfs_ifork_t *ifp; /* inode fork pointer */
  5241. int isrt; /* freeing in rt area */
  5242. xfs_extnum_t lastx; /* last extent index used */
  5243. int logflags; /* transaction logging flags */
  5244. xfs_extlen_t mod; /* rt extent offset */
  5245. xfs_mount_t *mp; /* mount structure */
  5246. xfs_extnum_t nextents; /* number of file extents */
  5247. xfs_bmbt_irec_t prev; /* previous extent record */
  5248. xfs_fileoff_t start; /* first file offset deleted */
  5249. int tmp_logflags; /* partial logging flags */
  5250. int wasdel; /* was a delayed alloc extent */
  5251. int whichfork; /* data or attribute fork */
  5252. int rsvd; /* OK to allocate reserved blocks */
  5253. xfs_fsblock_t sum;
  5254. xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
  5255. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  5256. XFS_ATTR_FORK : XFS_DATA_FORK;
  5257. ifp = XFS_IFORK_PTR(ip, whichfork);
  5258. if (unlikely(
  5259. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5260. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  5261. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  5262. ip->i_mount);
  5263. return XFS_ERROR(EFSCORRUPTED);
  5264. }
  5265. mp = ip->i_mount;
  5266. if (XFS_FORCED_SHUTDOWN(mp))
  5267. return XFS_ERROR(EIO);
  5268. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  5269. ASSERT(len > 0);
  5270. ASSERT(nexts >= 0);
  5271. ASSERT(ifp->if_ext_max ==
  5272. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5273. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5274. (error = xfs_iread_extents(tp, ip, whichfork)))
  5275. return error;
  5276. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5277. if (nextents == 0) {
  5278. *done = 1;
  5279. return 0;
  5280. }
  5281. XFS_STATS_INC(xs_blk_unmap);
  5282. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  5283. start = bno;
  5284. bno = start + len - 1;
  5285. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5286. &prev);
  5287. if (delta) {
  5288. delta->xed_startoff = NULLFILEOFF;
  5289. delta->xed_blockcount = 0;
  5290. }
  5291. /*
  5292. * Check to see if the given block number is past the end of the
  5293. * file, back up to the last block if so...
  5294. */
  5295. if (eof) {
  5296. ep = xfs_iext_get_ext(ifp, --lastx);
  5297. xfs_bmbt_get_all(ep, &got);
  5298. bno = got.br_startoff + got.br_blockcount - 1;
  5299. }
  5300. logflags = 0;
  5301. if (ifp->if_flags & XFS_IFBROOT) {
  5302. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  5303. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5304. cur->bc_private.b.firstblock = *firstblock;
  5305. cur->bc_private.b.flist = flist;
  5306. cur->bc_private.b.flags = 0;
  5307. } else
  5308. cur = NULL;
  5309. extno = 0;
  5310. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  5311. (nexts == 0 || extno < nexts)) {
  5312. /*
  5313. * Is the found extent after a hole in which bno lives?
  5314. * Just back up to the previous extent, if so.
  5315. */
  5316. if (got.br_startoff > bno) {
  5317. if (--lastx < 0)
  5318. break;
  5319. ep = xfs_iext_get_ext(ifp, lastx);
  5320. xfs_bmbt_get_all(ep, &got);
  5321. }
  5322. /*
  5323. * Is the last block of this extent before the range
  5324. * we're supposed to delete? If so, we're done.
  5325. */
  5326. bno = XFS_FILEOFF_MIN(bno,
  5327. got.br_startoff + got.br_blockcount - 1);
  5328. if (bno < start)
  5329. break;
  5330. /*
  5331. * Then deal with the (possibly delayed) allocated space
  5332. * we found.
  5333. */
  5334. ASSERT(ep != NULL);
  5335. del = got;
  5336. wasdel = ISNULLSTARTBLOCK(del.br_startblock);
  5337. if (got.br_startoff < start) {
  5338. del.br_startoff = start;
  5339. del.br_blockcount -= start - got.br_startoff;
  5340. if (!wasdel)
  5341. del.br_startblock += start - got.br_startoff;
  5342. }
  5343. if (del.br_startoff + del.br_blockcount > bno + 1)
  5344. del.br_blockcount = bno + 1 - del.br_startoff;
  5345. sum = del.br_startblock + del.br_blockcount;
  5346. if (isrt &&
  5347. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5348. /*
  5349. * Realtime extent not lined up at the end.
  5350. * The extent could have been split into written
  5351. * and unwritten pieces, or we could just be
  5352. * unmapping part of it. But we can't really
  5353. * get rid of part of a realtime extent.
  5354. */
  5355. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5356. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5357. /*
  5358. * This piece is unwritten, or we're not
  5359. * using unwritten extents. Skip over it.
  5360. */
  5361. ASSERT(bno >= mod);
  5362. bno -= mod > del.br_blockcount ?
  5363. del.br_blockcount : mod;
  5364. if (bno < got.br_startoff) {
  5365. if (--lastx >= 0)
  5366. xfs_bmbt_get_all(xfs_iext_get_ext(
  5367. ifp, lastx), &got);
  5368. }
  5369. continue;
  5370. }
  5371. /*
  5372. * It's written, turn it unwritten.
  5373. * This is better than zeroing it.
  5374. */
  5375. ASSERT(del.br_state == XFS_EXT_NORM);
  5376. ASSERT(xfs_trans_get_block_res(tp) > 0);
  5377. /*
  5378. * If this spans a realtime extent boundary,
  5379. * chop it back to the start of the one we end at.
  5380. */
  5381. if (del.br_blockcount > mod) {
  5382. del.br_startoff += del.br_blockcount - mod;
  5383. del.br_startblock += del.br_blockcount - mod;
  5384. del.br_blockcount = mod;
  5385. }
  5386. del.br_state = XFS_EXT_UNWRITTEN;
  5387. error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
  5388. firstblock, flist, &logflags, delta,
  5389. XFS_DATA_FORK, 0);
  5390. if (error)
  5391. goto error0;
  5392. goto nodelete;
  5393. }
  5394. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5395. /*
  5396. * Realtime extent is lined up at the end but not
  5397. * at the front. We'll get rid of full extents if
  5398. * we can.
  5399. */
  5400. mod = mp->m_sb.sb_rextsize - mod;
  5401. if (del.br_blockcount > mod) {
  5402. del.br_blockcount -= mod;
  5403. del.br_startoff += mod;
  5404. del.br_startblock += mod;
  5405. } else if ((del.br_startoff == start &&
  5406. (del.br_state == XFS_EXT_UNWRITTEN ||
  5407. xfs_trans_get_block_res(tp) == 0)) ||
  5408. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5409. /*
  5410. * Can't make it unwritten. There isn't
  5411. * a full extent here so just skip it.
  5412. */
  5413. ASSERT(bno >= del.br_blockcount);
  5414. bno -= del.br_blockcount;
  5415. if (bno < got.br_startoff) {
  5416. if (--lastx >= 0)
  5417. xfs_bmbt_get_all(--ep, &got);
  5418. }
  5419. continue;
  5420. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5421. /*
  5422. * This one is already unwritten.
  5423. * It must have a written left neighbor.
  5424. * Unwrite the killed part of that one and
  5425. * try again.
  5426. */
  5427. ASSERT(lastx > 0);
  5428. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  5429. lastx - 1), &prev);
  5430. ASSERT(prev.br_state == XFS_EXT_NORM);
  5431. ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
  5432. ASSERT(del.br_startblock ==
  5433. prev.br_startblock + prev.br_blockcount);
  5434. if (prev.br_startoff < start) {
  5435. mod = start - prev.br_startoff;
  5436. prev.br_blockcount -= mod;
  5437. prev.br_startblock += mod;
  5438. prev.br_startoff = start;
  5439. }
  5440. prev.br_state = XFS_EXT_UNWRITTEN;
  5441. error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
  5442. &prev, firstblock, flist, &logflags,
  5443. delta, XFS_DATA_FORK, 0);
  5444. if (error)
  5445. goto error0;
  5446. goto nodelete;
  5447. } else {
  5448. ASSERT(del.br_state == XFS_EXT_NORM);
  5449. del.br_state = XFS_EXT_UNWRITTEN;
  5450. error = xfs_bmap_add_extent(ip, lastx, &cur,
  5451. &del, firstblock, flist, &logflags,
  5452. delta, XFS_DATA_FORK, 0);
  5453. if (error)
  5454. goto error0;
  5455. goto nodelete;
  5456. }
  5457. }
  5458. if (wasdel) {
  5459. ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
  5460. /* Update realtime/data freespace, unreserve quota */
  5461. if (isrt) {
  5462. xfs_filblks_t rtexts;
  5463. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5464. do_div(rtexts, mp->m_sb.sb_rextsize);
  5465. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5466. (int64_t)rtexts, rsvd);
  5467. (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
  5468. NULL, ip, -((long)del.br_blockcount), 0,
  5469. XFS_QMOPT_RES_RTBLKS);
  5470. } else {
  5471. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
  5472. (int64_t)del.br_blockcount, rsvd);
  5473. (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
  5474. NULL, ip, -((long)del.br_blockcount), 0,
  5475. XFS_QMOPT_RES_REGBLKS);
  5476. }
  5477. ip->i_delayed_blks -= del.br_blockcount;
  5478. if (cur)
  5479. cur->bc_private.b.flags |=
  5480. XFS_BTCUR_BPRV_WASDEL;
  5481. } else if (cur)
  5482. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5483. /*
  5484. * If it's the case where the directory code is running
  5485. * with no block reservation, and the deleted block is in
  5486. * the middle of its extent, and the resulting insert
  5487. * of an extent would cause transformation to btree format,
  5488. * then reject it. The calling code will then swap
  5489. * blocks around instead.
  5490. * We have to do this now, rather than waiting for the
  5491. * conversion to btree format, since the transaction
  5492. * will be dirty.
  5493. */
  5494. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5495. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5496. XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
  5497. del.br_startoff > got.br_startoff &&
  5498. del.br_startoff + del.br_blockcount <
  5499. got.br_startoff + got.br_blockcount) {
  5500. error = XFS_ERROR(ENOSPC);
  5501. goto error0;
  5502. }
  5503. error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
  5504. &tmp_logflags, delta, whichfork, rsvd);
  5505. logflags |= tmp_logflags;
  5506. if (error)
  5507. goto error0;
  5508. bno = del.br_startoff - 1;
  5509. nodelete:
  5510. lastx = ifp->if_lastex;
  5511. /*
  5512. * If not done go on to the next (previous) record.
  5513. * Reset ep in case the extents array was re-alloced.
  5514. */
  5515. ep = xfs_iext_get_ext(ifp, lastx);
  5516. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5517. if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
  5518. xfs_bmbt_get_startoff(ep) > bno) {
  5519. if (--lastx >= 0)
  5520. ep = xfs_iext_get_ext(ifp, lastx);
  5521. }
  5522. if (lastx >= 0)
  5523. xfs_bmbt_get_all(ep, &got);
  5524. extno++;
  5525. }
  5526. }
  5527. ifp->if_lastex = lastx;
  5528. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5529. ASSERT(ifp->if_ext_max ==
  5530. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5531. /*
  5532. * Convert to a btree if necessary.
  5533. */
  5534. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5535. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  5536. ASSERT(cur == NULL);
  5537. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5538. &cur, 0, &tmp_logflags, whichfork);
  5539. logflags |= tmp_logflags;
  5540. if (error)
  5541. goto error0;
  5542. }
  5543. /*
  5544. * transform from btree to extents, give it cur
  5545. */
  5546. else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5547. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5548. ASSERT(cur != NULL);
  5549. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5550. whichfork);
  5551. logflags |= tmp_logflags;
  5552. if (error)
  5553. goto error0;
  5554. }
  5555. /*
  5556. * transform from extents to local?
  5557. */
  5558. ASSERT(ifp->if_ext_max ==
  5559. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5560. error = 0;
  5561. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5562. /* A change was actually made.
  5563. * Note that delta->xed_blockount is an offset at this
  5564. * point and needs to be converted to a block count.
  5565. */
  5566. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5567. delta->xed_blockcount -= delta->xed_startoff;
  5568. }
  5569. error0:
  5570. /*
  5571. * Log everything. Do this after conversion, there's no point in
  5572. * logging the extent records if we've converted to btree format.
  5573. */
  5574. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  5575. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5576. logflags &= ~XFS_ILOG_FEXT(whichfork);
  5577. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  5578. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5579. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  5580. /*
  5581. * Log inode even in the error case, if the transaction
  5582. * is dirty we'll need to shut down the filesystem.
  5583. */
  5584. if (logflags)
  5585. xfs_trans_log_inode(tp, ip, logflags);
  5586. if (cur) {
  5587. if (!error) {
  5588. *firstblock = cur->bc_private.b.firstblock;
  5589. cur->bc_private.b.allocated = 0;
  5590. }
  5591. xfs_btree_del_cursor(cur,
  5592. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5593. }
  5594. return error;
  5595. }
  5596. /*
  5597. * returns 1 for success, 0 if we failed to map the extent.
  5598. */
  5599. STATIC int
  5600. xfs_getbmapx_fix_eof_hole(
  5601. xfs_inode_t *ip, /* xfs incore inode pointer */
  5602. struct getbmapx *out, /* output structure */
  5603. int prealloced, /* this is a file with
  5604. * preallocated data space */
  5605. __int64_t end, /* last block requested */
  5606. xfs_fsblock_t startblock)
  5607. {
  5608. __int64_t fixlen;
  5609. xfs_mount_t *mp; /* file system mount point */
  5610. xfs_ifork_t *ifp; /* inode fork pointer */
  5611. xfs_extnum_t lastx; /* last extent pointer */
  5612. xfs_fileoff_t fileblock;
  5613. if (startblock == HOLESTARTBLOCK) {
  5614. mp = ip->i_mount;
  5615. out->bmv_block = -1;
  5616. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
  5617. fixlen -= out->bmv_offset;
  5618. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  5619. /* Came to hole at EOF. Trim it. */
  5620. if (fixlen <= 0)
  5621. return 0;
  5622. out->bmv_length = fixlen;
  5623. }
  5624. } else {
  5625. if (startblock == DELAYSTARTBLOCK)
  5626. out->bmv_block = -2;
  5627. else
  5628. out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
  5629. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  5630. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  5631. if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  5632. (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
  5633. out->bmv_oflags |= BMV_OF_LAST;
  5634. }
  5635. return 1;
  5636. }
  5637. /*
  5638. * Get inode's extents as described in bmv, and format for output.
  5639. * Calls formatter to fill the user's buffer until all extents
  5640. * are mapped, until the passed-in bmv->bmv_count slots have
  5641. * been filled, or until the formatter short-circuits the loop,
  5642. * if it is tracking filled-in extents on its own.
  5643. */
  5644. int /* error code */
  5645. xfs_getbmap(
  5646. xfs_inode_t *ip,
  5647. struct getbmapx *bmv, /* user bmap structure */
  5648. xfs_bmap_format_t formatter, /* format to user */
  5649. void *arg) /* formatter arg */
  5650. {
  5651. __int64_t bmvend; /* last block requested */
  5652. int error; /* return value */
  5653. __int64_t fixlen; /* length for -1 case */
  5654. int i; /* extent number */
  5655. int lock; /* lock state */
  5656. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5657. xfs_mount_t *mp; /* file system mount point */
  5658. int nex; /* # of user extents can do */
  5659. int nexleft; /* # of user extents left */
  5660. int subnex; /* # of bmapi's can do */
  5661. int nmap; /* number of map entries */
  5662. struct getbmapx out; /* output structure */
  5663. int whichfork; /* data or attr fork */
  5664. int prealloced; /* this is a file with
  5665. * preallocated data space */
  5666. int iflags; /* interface flags */
  5667. int bmapi_flags; /* flags for xfs_bmapi */
  5668. mp = ip->i_mount;
  5669. iflags = bmv->bmv_iflags;
  5670. whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5671. /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
  5672. * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
  5673. * bit is set for the file, generate a read event in order
  5674. * that the DMAPI application may do its thing before we return
  5675. * the extents. Usually this means restoring user file data to
  5676. * regions of the file that look like holes.
  5677. *
  5678. * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
  5679. * BMV_IF_NO_DMAPI_READ so that read events are generated.
  5680. * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
  5681. * could misinterpret holes in a DMAPI file as true holes,
  5682. * when in fact they may represent offline user data.
  5683. */
  5684. if ((iflags & BMV_IF_NO_DMAPI_READ) == 0 &&
  5685. DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
  5686. whichfork == XFS_DATA_FORK) {
  5687. error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
  5688. if (error)
  5689. return XFS_ERROR(error);
  5690. }
  5691. if (whichfork == XFS_ATTR_FORK) {
  5692. if (XFS_IFORK_Q(ip)) {
  5693. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5694. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5695. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5696. return XFS_ERROR(EINVAL);
  5697. } else if (unlikely(
  5698. ip->i_d.di_aformat != 0 &&
  5699. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5700. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5701. ip->i_mount);
  5702. return XFS_ERROR(EFSCORRUPTED);
  5703. }
  5704. } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5705. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5706. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5707. return XFS_ERROR(EINVAL);
  5708. if (whichfork == XFS_DATA_FORK) {
  5709. if (xfs_get_extsz_hint(ip) ||
  5710. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  5711. prealloced = 1;
  5712. fixlen = XFS_MAXIOFFSET(mp);
  5713. } else {
  5714. prealloced = 0;
  5715. fixlen = ip->i_size;
  5716. }
  5717. } else {
  5718. prealloced = 0;
  5719. fixlen = 1LL << 32;
  5720. }
  5721. if (bmv->bmv_length == -1) {
  5722. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5723. bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
  5724. (__int64_t)0);
  5725. } else if (bmv->bmv_length < 0)
  5726. return XFS_ERROR(EINVAL);
  5727. if (bmv->bmv_length == 0) {
  5728. bmv->bmv_entries = 0;
  5729. return 0;
  5730. }
  5731. nex = bmv->bmv_count - 1;
  5732. if (nex <= 0)
  5733. return XFS_ERROR(EINVAL);
  5734. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5735. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5736. if (((iflags & BMV_IF_DELALLOC) == 0) &&
  5737. (whichfork == XFS_DATA_FORK) &&
  5738. (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
  5739. /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
  5740. error = xfs_flush_pages(ip, (xfs_off_t)0,
  5741. -1, 0, FI_REMAPF);
  5742. if (error) {
  5743. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5744. return error;
  5745. }
  5746. }
  5747. ASSERT(whichfork == XFS_ATTR_FORK || (iflags & BMV_IF_DELALLOC) ||
  5748. ip->i_delayed_blks == 0);
  5749. lock = xfs_ilock_map_shared(ip);
  5750. /*
  5751. * Don't let nex be bigger than the number of extents
  5752. * we can have assuming alternating holes and real extents.
  5753. */
  5754. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5755. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5756. bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
  5757. ((iflags & BMV_IF_PREALLOC) ? 0 : XFS_BMAPI_IGSTATE);
  5758. /*
  5759. * Allocate enough space to handle "subnex" maps at a time.
  5760. */
  5761. subnex = 16;
  5762. map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
  5763. bmv->bmv_entries = 0;
  5764. if ((XFS_IFORK_NEXTENTS(ip, whichfork) == 0)) {
  5765. if (((iflags & BMV_IF_DELALLOC) == 0) ||
  5766. whichfork == XFS_ATTR_FORK) {
  5767. error = 0;
  5768. goto unlock_and_return;
  5769. }
  5770. }
  5771. nexleft = nex;
  5772. do {
  5773. nmap = (nexleft > subnex) ? subnex : nexleft;
  5774. error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5775. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5776. bmapi_flags, NULL, 0, map, &nmap,
  5777. NULL, NULL);
  5778. if (error)
  5779. goto unlock_and_return;
  5780. ASSERT(nmap <= subnex);
  5781. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5782. out.bmv_oflags = 0;
  5783. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  5784. out.bmv_oflags |= BMV_OF_PREALLOC;
  5785. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  5786. out.bmv_oflags |= BMV_OF_DELALLOC;
  5787. out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5788. out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5789. out.bmv_unused1 = out.bmv_unused2 = 0;
  5790. ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
  5791. (map[i].br_startblock != DELAYSTARTBLOCK));
  5792. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5793. whichfork == XFS_ATTR_FORK) {
  5794. /* came to the end of attribute fork */
  5795. out.bmv_oflags |= BMV_OF_LAST;
  5796. goto unlock_and_return;
  5797. } else {
  5798. int full = 0; /* user array is full */
  5799. if (!xfs_getbmapx_fix_eof_hole(ip, &out,
  5800. prealloced, bmvend,
  5801. map[i].br_startblock)) {
  5802. goto unlock_and_return;
  5803. }
  5804. /* format results & advance arg */
  5805. error = formatter(&arg, &out, &full);
  5806. if (error || full)
  5807. goto unlock_and_return;
  5808. nexleft--;
  5809. bmv->bmv_offset =
  5810. out.bmv_offset + out.bmv_length;
  5811. bmv->bmv_length = MAX((__int64_t)0,
  5812. (__int64_t)(bmvend - bmv->bmv_offset));
  5813. bmv->bmv_entries++;
  5814. }
  5815. }
  5816. } while (nmap && nexleft && bmv->bmv_length);
  5817. unlock_and_return:
  5818. xfs_iunlock_map_shared(ip, lock);
  5819. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5820. kmem_free(map);
  5821. return error;
  5822. }
  5823. /*
  5824. * Check the last inode extent to determine whether this allocation will result
  5825. * in blocks being allocated at the end of the file. When we allocate new data
  5826. * blocks at the end of the file which do not start at the previous data block,
  5827. * we will try to align the new blocks at stripe unit boundaries.
  5828. */
  5829. STATIC int /* error */
  5830. xfs_bmap_isaeof(
  5831. xfs_inode_t *ip, /* incore inode pointer */
  5832. xfs_fileoff_t off, /* file offset in fsblocks */
  5833. int whichfork, /* data or attribute fork */
  5834. char *aeof) /* return value */
  5835. {
  5836. int error; /* error return value */
  5837. xfs_ifork_t *ifp; /* inode fork pointer */
  5838. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5839. xfs_extnum_t nextents; /* number of file extents */
  5840. xfs_bmbt_irec_t s; /* expanded extent record */
  5841. ASSERT(whichfork == XFS_DATA_FORK);
  5842. ifp = XFS_IFORK_PTR(ip, whichfork);
  5843. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5844. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5845. return error;
  5846. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5847. if (nextents == 0) {
  5848. *aeof = 1;
  5849. return 0;
  5850. }
  5851. /*
  5852. * Go to the last extent
  5853. */
  5854. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5855. xfs_bmbt_get_all(lastrec, &s);
  5856. /*
  5857. * Check we are allocating in the last extent (for delayed allocations)
  5858. * or past the last extent for non-delayed allocations.
  5859. */
  5860. *aeof = (off >= s.br_startoff &&
  5861. off < s.br_startoff + s.br_blockcount &&
  5862. ISNULLSTARTBLOCK(s.br_startblock)) ||
  5863. off >= s.br_startoff + s.br_blockcount;
  5864. return 0;
  5865. }
  5866. /*
  5867. * Check if the endoff is outside the last extent. If so the caller will grow
  5868. * the allocation to a stripe unit boundary.
  5869. */
  5870. int /* error */
  5871. xfs_bmap_eof(
  5872. xfs_inode_t *ip, /* incore inode pointer */
  5873. xfs_fileoff_t endoff, /* file offset in fsblocks */
  5874. int whichfork, /* data or attribute fork */
  5875. int *eof) /* result value */
  5876. {
  5877. xfs_fsblock_t blockcount; /* extent block count */
  5878. int error; /* error return value */
  5879. xfs_ifork_t *ifp; /* inode fork pointer */
  5880. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5881. xfs_extnum_t nextents; /* number of file extents */
  5882. xfs_fileoff_t startoff; /* extent starting file offset */
  5883. ASSERT(whichfork == XFS_DATA_FORK);
  5884. ifp = XFS_IFORK_PTR(ip, whichfork);
  5885. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5886. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5887. return error;
  5888. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5889. if (nextents == 0) {
  5890. *eof = 1;
  5891. return 0;
  5892. }
  5893. /*
  5894. * Go to the last extent
  5895. */
  5896. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5897. startoff = xfs_bmbt_get_startoff(lastrec);
  5898. blockcount = xfs_bmbt_get_blockcount(lastrec);
  5899. *eof = endoff >= startoff + blockcount;
  5900. return 0;
  5901. }
  5902. #ifdef DEBUG
  5903. STATIC
  5904. xfs_buf_t *
  5905. xfs_bmap_get_bp(
  5906. xfs_btree_cur_t *cur,
  5907. xfs_fsblock_t bno)
  5908. {
  5909. int i;
  5910. xfs_buf_t *bp;
  5911. if (!cur)
  5912. return(NULL);
  5913. bp = NULL;
  5914. for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5915. bp = cur->bc_bufs[i];
  5916. if (!bp) break;
  5917. if (XFS_BUF_ADDR(bp) == bno)
  5918. break; /* Found it */
  5919. }
  5920. if (i == XFS_BTREE_MAXLEVELS)
  5921. bp = NULL;
  5922. if (!bp) { /* Chase down all the log items to see if the bp is there */
  5923. xfs_log_item_chunk_t *licp;
  5924. xfs_trans_t *tp;
  5925. tp = cur->bc_tp;
  5926. licp = &tp->t_items;
  5927. while (!bp && licp != NULL) {
  5928. if (xfs_lic_are_all_free(licp)) {
  5929. licp = licp->lic_next;
  5930. continue;
  5931. }
  5932. for (i = 0; i < licp->lic_unused; i++) {
  5933. xfs_log_item_desc_t *lidp;
  5934. xfs_log_item_t *lip;
  5935. xfs_buf_log_item_t *bip;
  5936. xfs_buf_t *lbp;
  5937. if (xfs_lic_isfree(licp, i)) {
  5938. continue;
  5939. }
  5940. lidp = xfs_lic_slot(licp, i);
  5941. lip = lidp->lid_item;
  5942. if (lip->li_type != XFS_LI_BUF)
  5943. continue;
  5944. bip = (xfs_buf_log_item_t *)lip;
  5945. lbp = bip->bli_buf;
  5946. if (XFS_BUF_ADDR(lbp) == bno) {
  5947. bp = lbp;
  5948. break; /* Found it */
  5949. }
  5950. }
  5951. licp = licp->lic_next;
  5952. }
  5953. }
  5954. return(bp);
  5955. }
  5956. void
  5957. xfs_check_block(
  5958. struct xfs_btree_block *block,
  5959. xfs_mount_t *mp,
  5960. int root,
  5961. short sz)
  5962. {
  5963. int i, j, dmxr;
  5964. __be64 *pp, *thispa; /* pointer to block address */
  5965. xfs_bmbt_key_t *prevp, *keyp;
  5966. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5967. prevp = NULL;
  5968. for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
  5969. dmxr = mp->m_bmap_dmxr[0];
  5970. keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
  5971. if (prevp) {
  5972. ASSERT(be64_to_cpu(prevp->br_startoff) <
  5973. be64_to_cpu(keyp->br_startoff));
  5974. }
  5975. prevp = keyp;
  5976. /*
  5977. * Compare the block numbers to see if there are dups.
  5978. */
  5979. if (root)
  5980. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
  5981. else
  5982. pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
  5983. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5984. if (root)
  5985. thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
  5986. else
  5987. thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
  5988. if (*thispa == *pp) {
  5989. cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
  5990. __func__, j, i,
  5991. (unsigned long long)be64_to_cpu(*thispa));
  5992. panic("%s: ptrs are equal in node\n",
  5993. __func__);
  5994. }
  5995. }
  5996. }
  5997. }
  5998. /*
  5999. * Check that the extents for the inode ip are in the right order in all
  6000. * btree leaves.
  6001. */
  6002. STATIC void
  6003. xfs_bmap_check_leaf_extents(
  6004. xfs_btree_cur_t *cur, /* btree cursor or null */
  6005. xfs_inode_t *ip, /* incore inode pointer */
  6006. int whichfork) /* data or attr fork */
  6007. {
  6008. struct xfs_btree_block *block; /* current btree block */
  6009. xfs_fsblock_t bno; /* block # of "block" */
  6010. xfs_buf_t *bp; /* buffer for "block" */
  6011. int error; /* error return value */
  6012. xfs_extnum_t i=0, j; /* index into the extents list */
  6013. xfs_ifork_t *ifp; /* fork structure */
  6014. int level; /* btree level, for checking */
  6015. xfs_mount_t *mp; /* file system mount structure */
  6016. __be64 *pp; /* pointer to block address */
  6017. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  6018. xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
  6019. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  6020. int bp_release = 0;
  6021. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  6022. return;
  6023. }
  6024. bno = NULLFSBLOCK;
  6025. mp = ip->i_mount;
  6026. ifp = XFS_IFORK_PTR(ip, whichfork);
  6027. block = ifp->if_broot;
  6028. /*
  6029. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  6030. */
  6031. level = be16_to_cpu(block->bb_level);
  6032. ASSERT(level > 0);
  6033. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  6034. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  6035. bno = be64_to_cpu(*pp);
  6036. ASSERT(bno != NULLDFSBNO);
  6037. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  6038. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  6039. /*
  6040. * Go down the tree until leaf level is reached, following the first
  6041. * pointer (leftmost) at each level.
  6042. */
  6043. while (level-- > 0) {
  6044. /* See if buf is in cur first */
  6045. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  6046. if (bp) {
  6047. bp_release = 0;
  6048. } else {
  6049. bp_release = 1;
  6050. }
  6051. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  6052. XFS_BMAP_BTREE_REF)))
  6053. goto error_norelse;
  6054. block = XFS_BUF_TO_BLOCK(bp);
  6055. XFS_WANT_CORRUPTED_GOTO(
  6056. xfs_bmap_sanity_check(mp, bp, level),
  6057. error0);
  6058. if (level == 0)
  6059. break;
  6060. /*
  6061. * Check this block for basic sanity (increasing keys and
  6062. * no duplicate blocks).
  6063. */
  6064. xfs_check_block(block, mp, 0, 0);
  6065. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  6066. bno = be64_to_cpu(*pp);
  6067. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  6068. if (bp_release) {
  6069. bp_release = 0;
  6070. xfs_trans_brelse(NULL, bp);
  6071. }
  6072. }
  6073. /*
  6074. * Here with bp and block set to the leftmost leaf node in the tree.
  6075. */
  6076. i = 0;
  6077. /*
  6078. * Loop over all leaf nodes checking that all extents are in the right order.
  6079. */
  6080. for (;;) {
  6081. xfs_fsblock_t nextbno;
  6082. xfs_extnum_t num_recs;
  6083. num_recs = xfs_btree_get_numrecs(block);
  6084. /*
  6085. * Read-ahead the next leaf block, if any.
  6086. */
  6087. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  6088. /*
  6089. * Check all the extents to make sure they are OK.
  6090. * If we had a previous block, the last entry should
  6091. * conform with the first entry in this one.
  6092. */
  6093. ep = XFS_BMBT_REC_ADDR(mp, block, 1);
  6094. if (i) {
  6095. ASSERT(xfs_bmbt_disk_get_startoff(&last) +
  6096. xfs_bmbt_disk_get_blockcount(&last) <=
  6097. xfs_bmbt_disk_get_startoff(ep));
  6098. }
  6099. for (j = 1; j < num_recs; j++) {
  6100. nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
  6101. ASSERT(xfs_bmbt_disk_get_startoff(ep) +
  6102. xfs_bmbt_disk_get_blockcount(ep) <=
  6103. xfs_bmbt_disk_get_startoff(nextp));
  6104. ep = nextp;
  6105. }
  6106. last = *ep;
  6107. i += num_recs;
  6108. if (bp_release) {
  6109. bp_release = 0;
  6110. xfs_trans_brelse(NULL, bp);
  6111. }
  6112. bno = nextbno;
  6113. /*
  6114. * If we've reached the end, stop.
  6115. */
  6116. if (bno == NULLFSBLOCK)
  6117. break;
  6118. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  6119. if (bp) {
  6120. bp_release = 0;
  6121. } else {
  6122. bp_release = 1;
  6123. }
  6124. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  6125. XFS_BMAP_BTREE_REF)))
  6126. goto error_norelse;
  6127. block = XFS_BUF_TO_BLOCK(bp);
  6128. }
  6129. if (bp_release) {
  6130. bp_release = 0;
  6131. xfs_trans_brelse(NULL, bp);
  6132. }
  6133. return;
  6134. error0:
  6135. cmn_err(CE_WARN, "%s: at error0", __func__);
  6136. if (bp_release)
  6137. xfs_trans_brelse(NULL, bp);
  6138. error_norelse:
  6139. cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
  6140. __func__, i);
  6141. panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
  6142. return;
  6143. }
  6144. #endif
  6145. /*
  6146. * Count fsblocks of the given fork.
  6147. */
  6148. int /* error */
  6149. xfs_bmap_count_blocks(
  6150. xfs_trans_t *tp, /* transaction pointer */
  6151. xfs_inode_t *ip, /* incore inode */
  6152. int whichfork, /* data or attr fork */
  6153. int *count) /* out: count of blocks */
  6154. {
  6155. struct xfs_btree_block *block; /* current btree block */
  6156. xfs_fsblock_t bno; /* block # of "block" */
  6157. xfs_ifork_t *ifp; /* fork structure */
  6158. int level; /* btree level, for checking */
  6159. xfs_mount_t *mp; /* file system mount structure */
  6160. __be64 *pp; /* pointer to block address */
  6161. bno = NULLFSBLOCK;
  6162. mp = ip->i_mount;
  6163. ifp = XFS_IFORK_PTR(ip, whichfork);
  6164. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  6165. xfs_bmap_count_leaves(ifp, 0,
  6166. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  6167. count);
  6168. return 0;
  6169. }
  6170. /*
  6171. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  6172. */
  6173. block = ifp->if_broot;
  6174. level = be16_to_cpu(block->bb_level);
  6175. ASSERT(level > 0);
  6176. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  6177. bno = be64_to_cpu(*pp);
  6178. ASSERT(bno != NULLDFSBNO);
  6179. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  6180. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  6181. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  6182. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  6183. mp);
  6184. return XFS_ERROR(EFSCORRUPTED);
  6185. }
  6186. return 0;
  6187. }
  6188. /*
  6189. * Recursively walks each level of a btree
  6190. * to count total fsblocks is use.
  6191. */
  6192. STATIC int /* error */
  6193. xfs_bmap_count_tree(
  6194. xfs_mount_t *mp, /* file system mount point */
  6195. xfs_trans_t *tp, /* transaction pointer */
  6196. xfs_ifork_t *ifp, /* inode fork pointer */
  6197. xfs_fsblock_t blockno, /* file system block number */
  6198. int levelin, /* level in btree */
  6199. int *count) /* Count of blocks */
  6200. {
  6201. int error;
  6202. xfs_buf_t *bp, *nbp;
  6203. int level = levelin;
  6204. __be64 *pp;
  6205. xfs_fsblock_t bno = blockno;
  6206. xfs_fsblock_t nextbno;
  6207. struct xfs_btree_block *block, *nextblock;
  6208. int numrecs;
  6209. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
  6210. return error;
  6211. *count += 1;
  6212. block = XFS_BUF_TO_BLOCK(bp);
  6213. if (--level) {
  6214. /* Not at node above leafs, count this level of nodes */
  6215. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  6216. while (nextbno != NULLFSBLOCK) {
  6217. if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
  6218. 0, &nbp, XFS_BMAP_BTREE_REF)))
  6219. return error;
  6220. *count += 1;
  6221. nextblock = XFS_BUF_TO_BLOCK(nbp);
  6222. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  6223. xfs_trans_brelse(tp, nbp);
  6224. }
  6225. /* Dive to the next level */
  6226. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  6227. bno = be64_to_cpu(*pp);
  6228. if (unlikely((error =
  6229. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  6230. xfs_trans_brelse(tp, bp);
  6231. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  6232. XFS_ERRLEVEL_LOW, mp);
  6233. return XFS_ERROR(EFSCORRUPTED);
  6234. }
  6235. xfs_trans_brelse(tp, bp);
  6236. } else {
  6237. /* count all level 1 nodes and their leaves */
  6238. for (;;) {
  6239. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  6240. numrecs = be16_to_cpu(block->bb_numrecs);
  6241. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  6242. xfs_trans_brelse(tp, bp);
  6243. if (nextbno == NULLFSBLOCK)
  6244. break;
  6245. bno = nextbno;
  6246. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  6247. XFS_BMAP_BTREE_REF)))
  6248. return error;
  6249. *count += 1;
  6250. block = XFS_BUF_TO_BLOCK(bp);
  6251. }
  6252. }
  6253. return 0;
  6254. }
  6255. /*
  6256. * Count leaf blocks given a range of extent records.
  6257. */
  6258. STATIC void
  6259. xfs_bmap_count_leaves(
  6260. xfs_ifork_t *ifp,
  6261. xfs_extnum_t idx,
  6262. int numrecs,
  6263. int *count)
  6264. {
  6265. int b;
  6266. for (b = 0; b < numrecs; b++) {
  6267. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  6268. *count += xfs_bmbt_get_blockcount(frp);
  6269. }
  6270. }
  6271. /*
  6272. * Count leaf blocks given a range of extent records originally
  6273. * in btree format.
  6274. */
  6275. STATIC void
  6276. xfs_bmap_disk_count_leaves(
  6277. struct xfs_mount *mp,
  6278. struct xfs_btree_block *block,
  6279. int numrecs,
  6280. int *count)
  6281. {
  6282. int b;
  6283. xfs_bmbt_rec_t *frp;
  6284. for (b = 1; b <= numrecs; b++) {
  6285. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  6286. *count += xfs_bmbt_disk_get_blockcount(frp);
  6287. }
  6288. }