xfs_bmap.c 172 KB

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