xfs_bmap.c 154 KB

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