xfs_bmap.c 185 KB

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