xfs_bmap.c 191 KB

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