xfs_bmap.c 164 KB

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