xfs_bmap.c 163 KB

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