xfs_bmap.c 197 KB

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