xfs_bmap.c 155 KB

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