xfs_bmap.c 184 KB

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