xfs_bmap.c 184 KB

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