jfs_dmap.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. * Portions Copyright (C) Tino Reichardt, 2012
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #include "jfs_incore.h"
  22. #include "jfs_superblock.h"
  23. #include "jfs_dmap.h"
  24. #include "jfs_imap.h"
  25. #include "jfs_lock.h"
  26. #include "jfs_metapage.h"
  27. #include "jfs_debug.h"
  28. #include "jfs_discard.h"
  29. /*
  30. * SERIALIZATION of the Block Allocation Map.
  31. *
  32. * the working state of the block allocation map is accessed in
  33. * two directions:
  34. *
  35. * 1) allocation and free requests that start at the dmap
  36. * level and move up through the dmap control pages (i.e.
  37. * the vast majority of requests).
  38. *
  39. * 2) allocation requests that start at dmap control page
  40. * level and work down towards the dmaps.
  41. *
  42. * the serialization scheme used here is as follows.
  43. *
  44. * requests which start at the bottom are serialized against each
  45. * other through buffers and each requests holds onto its buffers
  46. * as it works it way up from a single dmap to the required level
  47. * of dmap control page.
  48. * requests that start at the top are serialized against each other
  49. * and request that start from the bottom by the multiple read/single
  50. * write inode lock of the bmap inode. requests starting at the top
  51. * take this lock in write mode while request starting at the bottom
  52. * take the lock in read mode. a single top-down request may proceed
  53. * exclusively while multiple bottoms-up requests may proceed
  54. * simultaneously (under the protection of busy buffers).
  55. *
  56. * in addition to information found in dmaps and dmap control pages,
  57. * the working state of the block allocation map also includes read/
  58. * write information maintained in the bmap descriptor (i.e. total
  59. * free block count, allocation group level free block counts).
  60. * a single exclusive lock (BMAP_LOCK) is used to guard this information
  61. * in the face of multiple-bottoms up requests.
  62. * (lock ordering: IREAD_LOCK, BMAP_LOCK);
  63. *
  64. * accesses to the persistent state of the block allocation map (limited
  65. * to the persistent bitmaps in dmaps) is guarded by (busy) buffers.
  66. */
  67. #define BMAP_LOCK_INIT(bmp) mutex_init(&bmp->db_bmaplock)
  68. #define BMAP_LOCK(bmp) mutex_lock(&bmp->db_bmaplock)
  69. #define BMAP_UNLOCK(bmp) mutex_unlock(&bmp->db_bmaplock)
  70. /*
  71. * forward references
  72. */
  73. static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
  74. int nblocks);
  75. static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval);
  76. static int dbBackSplit(dmtree_t * tp, int leafno);
  77. static int dbJoin(dmtree_t * tp, int leafno, int newval);
  78. static void dbAdjTree(dmtree_t * tp, int leafno, int newval);
  79. static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc,
  80. int level);
  81. static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results);
  82. static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
  83. int nblocks);
  84. static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno,
  85. int nblocks,
  86. int l2nb, s64 * results);
  87. static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
  88. int nblocks);
  89. static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks,
  90. int l2nb,
  91. s64 * results);
  92. static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb,
  93. s64 * results);
  94. static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno,
  95. s64 * results);
  96. static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks);
  97. static int dbFindBits(u32 word, int l2nb);
  98. static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno);
  99. static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx);
  100. static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
  101. int nblocks);
  102. static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
  103. int nblocks);
  104. static int dbMaxBud(u8 * cp);
  105. static int blkstol2(s64 nb);
  106. static int cntlz(u32 value);
  107. static int cnttz(u32 word);
  108. static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
  109. int nblocks);
  110. static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks);
  111. static int dbInitDmapTree(struct dmap * dp);
  112. static int dbInitTree(struct dmaptree * dtp);
  113. static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i);
  114. static int dbGetL2AGSize(s64 nblocks);
  115. /*
  116. * buddy table
  117. *
  118. * table used for determining buddy sizes within characters of
  119. * dmap bitmap words. the characters themselves serve as indexes
  120. * into the table, with the table elements yielding the maximum
  121. * binary buddy of free bits within the character.
  122. */
  123. static const s8 budtab[256] = {
  124. 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  125. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  126. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  127. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  128. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  129. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  130. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  131. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  132. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  133. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  134. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  135. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  136. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  137. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  138. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
  139. 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
  140. };
  141. /*
  142. * NAME: dbMount()
  143. *
  144. * FUNCTION: initializate the block allocation map.
  145. *
  146. * memory is allocated for the in-core bmap descriptor and
  147. * the in-core descriptor is initialized from disk.
  148. *
  149. * PARAMETERS:
  150. * ipbmap - pointer to in-core inode for the block map.
  151. *
  152. * RETURN VALUES:
  153. * 0 - success
  154. * -ENOMEM - insufficient memory
  155. * -EIO - i/o error
  156. */
  157. int dbMount(struct inode *ipbmap)
  158. {
  159. struct bmap *bmp;
  160. struct dbmap_disk *dbmp_le;
  161. struct metapage *mp;
  162. int i;
  163. /*
  164. * allocate/initialize the in-memory bmap descriptor
  165. */
  166. /* allocate memory for the in-memory bmap descriptor */
  167. bmp = kmalloc(sizeof(struct bmap), GFP_KERNEL);
  168. if (bmp == NULL)
  169. return -ENOMEM;
  170. /* read the on-disk bmap descriptor. */
  171. mp = read_metapage(ipbmap,
  172. BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
  173. PSIZE, 0);
  174. if (mp == NULL) {
  175. kfree(bmp);
  176. return -EIO;
  177. }
  178. /* copy the on-disk bmap descriptor to its in-memory version. */
  179. dbmp_le = (struct dbmap_disk *) mp->data;
  180. bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
  181. bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
  182. bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
  183. bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
  184. bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
  185. bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
  186. bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
  187. bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
  188. bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
  189. bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth);
  190. bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart);
  191. bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size);
  192. for (i = 0; i < MAXAG; i++)
  193. bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]);
  194. bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize);
  195. bmp->db_maxfreebud = dbmp_le->dn_maxfreebud;
  196. /* release the buffer. */
  197. release_metapage(mp);
  198. /* bind the bmap inode and the bmap descriptor to each other. */
  199. bmp->db_ipbmap = ipbmap;
  200. JFS_SBI(ipbmap->i_sb)->bmap = bmp;
  201. memset(bmp->db_active, 0, sizeof(bmp->db_active));
  202. /*
  203. * allocate/initialize the bmap lock
  204. */
  205. BMAP_LOCK_INIT(bmp);
  206. return (0);
  207. }
  208. /*
  209. * NAME: dbUnmount()
  210. *
  211. * FUNCTION: terminate the block allocation map in preparation for
  212. * file system unmount.
  213. *
  214. * the in-core bmap descriptor is written to disk and
  215. * the memory for this descriptor is freed.
  216. *
  217. * PARAMETERS:
  218. * ipbmap - pointer to in-core inode for the block map.
  219. *
  220. * RETURN VALUES:
  221. * 0 - success
  222. * -EIO - i/o error
  223. */
  224. int dbUnmount(struct inode *ipbmap, int mounterror)
  225. {
  226. struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
  227. if (!(mounterror || isReadOnly(ipbmap)))
  228. dbSync(ipbmap);
  229. /*
  230. * Invalidate the page cache buffers
  231. */
  232. truncate_inode_pages(ipbmap->i_mapping, 0);
  233. /* free the memory for the in-memory bmap. */
  234. kfree(bmp);
  235. return (0);
  236. }
  237. /*
  238. * dbSync()
  239. */
  240. int dbSync(struct inode *ipbmap)
  241. {
  242. struct dbmap_disk *dbmp_le;
  243. struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
  244. struct metapage *mp;
  245. int i;
  246. /*
  247. * write bmap global control page
  248. */
  249. /* get the buffer for the on-disk bmap descriptor. */
  250. mp = read_metapage(ipbmap,
  251. BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
  252. PSIZE, 0);
  253. if (mp == NULL) {
  254. jfs_err("dbSync: read_metapage failed!");
  255. return -EIO;
  256. }
  257. /* copy the in-memory version of the bmap to the on-disk version */
  258. dbmp_le = (struct dbmap_disk *) mp->data;
  259. dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize);
  260. dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree);
  261. dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage);
  262. dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag);
  263. dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel);
  264. dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag);
  265. dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref);
  266. dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel);
  267. dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
  268. dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth);
  269. dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart);
  270. dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size);
  271. for (i = 0; i < MAXAG; i++)
  272. dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]);
  273. dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize);
  274. dbmp_le->dn_maxfreebud = bmp->db_maxfreebud;
  275. /* write the buffer */
  276. write_metapage(mp);
  277. /*
  278. * write out dirty pages of bmap
  279. */
  280. filemap_write_and_wait(ipbmap->i_mapping);
  281. diWriteSpecial(ipbmap, 0);
  282. return (0);
  283. }
  284. /*
  285. * NAME: dbFree()
  286. *
  287. * FUNCTION: free the specified block range from the working block
  288. * allocation map.
  289. *
  290. * the blocks will be free from the working map one dmap
  291. * at a time.
  292. *
  293. * PARAMETERS:
  294. * ip - pointer to in-core inode;
  295. * blkno - starting block number to be freed.
  296. * nblocks - number of blocks to be freed.
  297. *
  298. * RETURN VALUES:
  299. * 0 - success
  300. * -EIO - i/o error
  301. */
  302. int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
  303. {
  304. struct metapage *mp;
  305. struct dmap *dp;
  306. int nb, rc;
  307. s64 lblkno, rem;
  308. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  309. struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
  310. struct super_block *sb = ipbmap->i_sb;
  311. IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
  312. /* block to be freed better be within the mapsize. */
  313. if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
  314. IREAD_UNLOCK(ipbmap);
  315. printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
  316. (unsigned long long) blkno,
  317. (unsigned long long) nblocks);
  318. jfs_error(ip->i_sb,
  319. "dbFree: block to be freed is outside the map");
  320. return -EIO;
  321. }
  322. /**
  323. * TRIM the blocks, when mounted with discard option
  324. */
  325. if (JFS_SBI(sb)->flag & JFS_DISCARD)
  326. if (JFS_SBI(sb)->minblks_trim <= nblocks)
  327. jfs_issue_discard(ipbmap, blkno, nblocks);
  328. /*
  329. * free the blocks a dmap at a time.
  330. */
  331. mp = NULL;
  332. for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
  333. /* release previous dmap if any */
  334. if (mp) {
  335. write_metapage(mp);
  336. }
  337. /* get the buffer for the current dmap. */
  338. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  339. mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
  340. if (mp == NULL) {
  341. IREAD_UNLOCK(ipbmap);
  342. return -EIO;
  343. }
  344. dp = (struct dmap *) mp->data;
  345. /* determine the number of blocks to be freed from
  346. * this dmap.
  347. */
  348. nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
  349. /* free the blocks. */
  350. if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
  351. jfs_error(ip->i_sb, "dbFree: error in block map\n");
  352. release_metapage(mp);
  353. IREAD_UNLOCK(ipbmap);
  354. return (rc);
  355. }
  356. }
  357. /* write the last buffer. */
  358. write_metapage(mp);
  359. IREAD_UNLOCK(ipbmap);
  360. return (0);
  361. }
  362. /*
  363. * NAME: dbUpdatePMap()
  364. *
  365. * FUNCTION: update the allocation state (free or allocate) of the
  366. * specified block range in the persistent block allocation map.
  367. *
  368. * the blocks will be updated in the persistent map one
  369. * dmap at a time.
  370. *
  371. * PARAMETERS:
  372. * ipbmap - pointer to in-core inode for the block map.
  373. * free - 'true' if block range is to be freed from the persistent
  374. * map; 'false' if it is to be allocated.
  375. * blkno - starting block number of the range.
  376. * nblocks - number of contiguous blocks in the range.
  377. * tblk - transaction block;
  378. *
  379. * RETURN VALUES:
  380. * 0 - success
  381. * -EIO - i/o error
  382. */
  383. int
  384. dbUpdatePMap(struct inode *ipbmap,
  385. int free, s64 blkno, s64 nblocks, struct tblock * tblk)
  386. {
  387. int nblks, dbitno, wbitno, rbits;
  388. int word, nbits, nwords;
  389. struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
  390. s64 lblkno, rem, lastlblkno;
  391. u32 mask;
  392. struct dmap *dp;
  393. struct metapage *mp;
  394. struct jfs_log *log;
  395. int lsn, difft, diffp;
  396. unsigned long flags;
  397. /* the blocks better be within the mapsize. */
  398. if (blkno + nblocks > bmp->db_mapsize) {
  399. printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
  400. (unsigned long long) blkno,
  401. (unsigned long long) nblocks);
  402. jfs_error(ipbmap->i_sb,
  403. "dbUpdatePMap: blocks are outside the map");
  404. return -EIO;
  405. }
  406. /* compute delta of transaction lsn from log syncpt */
  407. lsn = tblk->lsn;
  408. log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
  409. logdiff(difft, lsn, log);
  410. /*
  411. * update the block state a dmap at a time.
  412. */
  413. mp = NULL;
  414. lastlblkno = 0;
  415. for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) {
  416. /* get the buffer for the current dmap. */
  417. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  418. if (lblkno != lastlblkno) {
  419. if (mp) {
  420. write_metapage(mp);
  421. }
  422. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
  423. 0);
  424. if (mp == NULL)
  425. return -EIO;
  426. metapage_wait_for_io(mp);
  427. }
  428. dp = (struct dmap *) mp->data;
  429. /* determine the bit number and word within the dmap of
  430. * the starting block. also determine how many blocks
  431. * are to be updated within this dmap.
  432. */
  433. dbitno = blkno & (BPERDMAP - 1);
  434. word = dbitno >> L2DBWORD;
  435. nblks = min(rem, (s64)BPERDMAP - dbitno);
  436. /* update the bits of the dmap words. the first and last
  437. * words may only have a subset of their bits updated. if
  438. * this is the case, we'll work against that word (i.e.
  439. * partial first and/or last) only in a single pass. a
  440. * single pass will also be used to update all words that
  441. * are to have all their bits updated.
  442. */
  443. for (rbits = nblks; rbits > 0;
  444. rbits -= nbits, dbitno += nbits) {
  445. /* determine the bit number within the word and
  446. * the number of bits within the word.
  447. */
  448. wbitno = dbitno & (DBWORD - 1);
  449. nbits = min(rbits, DBWORD - wbitno);
  450. /* check if only part of the word is to be updated. */
  451. if (nbits < DBWORD) {
  452. /* update (free or allocate) the bits
  453. * in this word.
  454. */
  455. mask =
  456. (ONES << (DBWORD - nbits) >> wbitno);
  457. if (free)
  458. dp->pmap[word] &=
  459. cpu_to_le32(~mask);
  460. else
  461. dp->pmap[word] |=
  462. cpu_to_le32(mask);
  463. word += 1;
  464. } else {
  465. /* one or more words are to have all
  466. * their bits updated. determine how
  467. * many words and how many bits.
  468. */
  469. nwords = rbits >> L2DBWORD;
  470. nbits = nwords << L2DBWORD;
  471. /* update (free or allocate) the bits
  472. * in these words.
  473. */
  474. if (free)
  475. memset(&dp->pmap[word], 0,
  476. nwords * 4);
  477. else
  478. memset(&dp->pmap[word], (int) ONES,
  479. nwords * 4);
  480. word += nwords;
  481. }
  482. }
  483. /*
  484. * update dmap lsn
  485. */
  486. if (lblkno == lastlblkno)
  487. continue;
  488. lastlblkno = lblkno;
  489. LOGSYNC_LOCK(log, flags);
  490. if (mp->lsn != 0) {
  491. /* inherit older/smaller lsn */
  492. logdiff(diffp, mp->lsn, log);
  493. if (difft < diffp) {
  494. mp->lsn = lsn;
  495. /* move bp after tblock in logsync list */
  496. list_move(&mp->synclist, &tblk->synclist);
  497. }
  498. /* inherit younger/larger clsn */
  499. logdiff(difft, tblk->clsn, log);
  500. logdiff(diffp, mp->clsn, log);
  501. if (difft > diffp)
  502. mp->clsn = tblk->clsn;
  503. } else {
  504. mp->log = log;
  505. mp->lsn = lsn;
  506. /* insert bp after tblock in logsync list */
  507. log->count++;
  508. list_add(&mp->synclist, &tblk->synclist);
  509. mp->clsn = tblk->clsn;
  510. }
  511. LOGSYNC_UNLOCK(log, flags);
  512. }
  513. /* write the last buffer. */
  514. if (mp) {
  515. write_metapage(mp);
  516. }
  517. return (0);
  518. }
  519. /*
  520. * NAME: dbNextAG()
  521. *
  522. * FUNCTION: find the preferred allocation group for new allocations.
  523. *
  524. * Within the allocation groups, we maintain a preferred
  525. * allocation group which consists of a group with at least
  526. * average free space. It is the preferred group that we target
  527. * new inode allocation towards. The tie-in between inode
  528. * allocation and block allocation occurs as we allocate the
  529. * first (data) block of an inode and specify the inode (block)
  530. * as the allocation hint for this block.
  531. *
  532. * We try to avoid having more than one open file growing in
  533. * an allocation group, as this will lead to fragmentation.
  534. * This differs from the old OS/2 method of trying to keep
  535. * empty ags around for large allocations.
  536. *
  537. * PARAMETERS:
  538. * ipbmap - pointer to in-core inode for the block map.
  539. *
  540. * RETURN VALUES:
  541. * the preferred allocation group number.
  542. */
  543. int dbNextAG(struct inode *ipbmap)
  544. {
  545. s64 avgfree;
  546. int agpref;
  547. s64 hwm = 0;
  548. int i;
  549. int next_best = -1;
  550. struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
  551. BMAP_LOCK(bmp);
  552. /* determine the average number of free blocks within the ags. */
  553. avgfree = (u32)bmp->db_nfree / bmp->db_numag;
  554. /*
  555. * if the current preferred ag does not have an active allocator
  556. * and has at least average freespace, return it
  557. */
  558. agpref = bmp->db_agpref;
  559. if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
  560. (bmp->db_agfree[agpref] >= avgfree))
  561. goto unlock;
  562. /* From the last preferred ag, find the next one with at least
  563. * average free space.
  564. */
  565. for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
  566. if (agpref == bmp->db_numag)
  567. agpref = 0;
  568. if (atomic_read(&bmp->db_active[agpref]))
  569. /* open file is currently growing in this ag */
  570. continue;
  571. if (bmp->db_agfree[agpref] >= avgfree) {
  572. /* Return this one */
  573. bmp->db_agpref = agpref;
  574. goto unlock;
  575. } else if (bmp->db_agfree[agpref] > hwm) {
  576. /* Less than avg. freespace, but best so far */
  577. hwm = bmp->db_agfree[agpref];
  578. next_best = agpref;
  579. }
  580. }
  581. /*
  582. * If no inactive ag was found with average freespace, use the
  583. * next best
  584. */
  585. if (next_best != -1)
  586. bmp->db_agpref = next_best;
  587. /* else leave db_agpref unchanged */
  588. unlock:
  589. BMAP_UNLOCK(bmp);
  590. /* return the preferred group.
  591. */
  592. return (bmp->db_agpref);
  593. }
  594. /*
  595. * NAME: dbAlloc()
  596. *
  597. * FUNCTION: attempt to allocate a specified number of contiguous free
  598. * blocks from the working allocation block map.
  599. *
  600. * the block allocation policy uses hints and a multi-step
  601. * approach.
  602. *
  603. * for allocation requests smaller than the number of blocks
  604. * per dmap, we first try to allocate the new blocks
  605. * immediately following the hint. if these blocks are not
  606. * available, we try to allocate blocks near the hint. if
  607. * no blocks near the hint are available, we next try to
  608. * allocate within the same dmap as contains the hint.
  609. *
  610. * if no blocks are available in the dmap or the allocation
  611. * request is larger than the dmap size, we try to allocate
  612. * within the same allocation group as contains the hint. if
  613. * this does not succeed, we finally try to allocate anywhere
  614. * within the aggregate.
  615. *
  616. * we also try to allocate anywhere within the aggregate for
  617. * for allocation requests larger than the allocation group
  618. * size or requests that specify no hint value.
  619. *
  620. * PARAMETERS:
  621. * ip - pointer to in-core inode;
  622. * hint - allocation hint.
  623. * nblocks - number of contiguous blocks in the range.
  624. * results - on successful return, set to the starting block number
  625. * of the newly allocated contiguous range.
  626. *
  627. * RETURN VALUES:
  628. * 0 - success
  629. * -ENOSPC - insufficient disk resources
  630. * -EIO - i/o error
  631. */
  632. int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
  633. {
  634. int rc, agno;
  635. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  636. struct bmap *bmp;
  637. struct metapage *mp;
  638. s64 lblkno, blkno;
  639. struct dmap *dp;
  640. int l2nb;
  641. s64 mapSize;
  642. int writers;
  643. /* assert that nblocks is valid */
  644. assert(nblocks > 0);
  645. /* get the log2 number of blocks to be allocated.
  646. * if the number of blocks is not a log2 multiple,
  647. * it will be rounded up to the next log2 multiple.
  648. */
  649. l2nb = BLKSTOL2(nblocks);
  650. bmp = JFS_SBI(ip->i_sb)->bmap;
  651. mapSize = bmp->db_mapsize;
  652. /* the hint should be within the map */
  653. if (hint >= mapSize) {
  654. jfs_error(ip->i_sb, "dbAlloc: the hint is outside the map");
  655. return -EIO;
  656. }
  657. /* if the number of blocks to be allocated is greater than the
  658. * allocation group size, try to allocate anywhere.
  659. */
  660. if (l2nb > bmp->db_agl2size) {
  661. IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
  662. rc = dbAllocAny(bmp, nblocks, l2nb, results);
  663. goto write_unlock;
  664. }
  665. /*
  666. * If no hint, let dbNextAG recommend an allocation group
  667. */
  668. if (hint == 0)
  669. goto pref_ag;
  670. /* we would like to allocate close to the hint. adjust the
  671. * hint to the block following the hint since the allocators
  672. * will start looking for free space starting at this point.
  673. */
  674. blkno = hint + 1;
  675. if (blkno >= bmp->db_mapsize)
  676. goto pref_ag;
  677. agno = blkno >> bmp->db_agl2size;
  678. /* check if blkno crosses over into a new allocation group.
  679. * if so, check if we should allow allocations within this
  680. * allocation group.
  681. */
  682. if ((blkno & (bmp->db_agsize - 1)) == 0)
  683. /* check if the AG is currently being written to.
  684. * if so, call dbNextAG() to find a non-busy
  685. * AG with sufficient free space.
  686. */
  687. if (atomic_read(&bmp->db_active[agno]))
  688. goto pref_ag;
  689. /* check if the allocation request size can be satisfied from a
  690. * single dmap. if so, try to allocate from the dmap containing
  691. * the hint using a tiered strategy.
  692. */
  693. if (nblocks <= BPERDMAP) {
  694. IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
  695. /* get the buffer for the dmap containing the hint.
  696. */
  697. rc = -EIO;
  698. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  699. mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
  700. if (mp == NULL)
  701. goto read_unlock;
  702. dp = (struct dmap *) mp->data;
  703. /* first, try to satisfy the allocation request with the
  704. * blocks beginning at the hint.
  705. */
  706. if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
  707. != -ENOSPC) {
  708. if (rc == 0) {
  709. *results = blkno;
  710. mark_metapage_dirty(mp);
  711. }
  712. release_metapage(mp);
  713. goto read_unlock;
  714. }
  715. writers = atomic_read(&bmp->db_active[agno]);
  716. if ((writers > 1) ||
  717. ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
  718. /*
  719. * Someone else is writing in this allocation
  720. * group. To avoid fragmenting, try another ag
  721. */
  722. release_metapage(mp);
  723. IREAD_UNLOCK(ipbmap);
  724. goto pref_ag;
  725. }
  726. /* next, try to satisfy the allocation request with blocks
  727. * near the hint.
  728. */
  729. if ((rc =
  730. dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
  731. != -ENOSPC) {
  732. if (rc == 0)
  733. mark_metapage_dirty(mp);
  734. release_metapage(mp);
  735. goto read_unlock;
  736. }
  737. /* try to satisfy the allocation request with blocks within
  738. * the same dmap as the hint.
  739. */
  740. if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
  741. != -ENOSPC) {
  742. if (rc == 0)
  743. mark_metapage_dirty(mp);
  744. release_metapage(mp);
  745. goto read_unlock;
  746. }
  747. release_metapage(mp);
  748. IREAD_UNLOCK(ipbmap);
  749. }
  750. /* try to satisfy the allocation request with blocks within
  751. * the same allocation group as the hint.
  752. */
  753. IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
  754. if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
  755. goto write_unlock;
  756. IWRITE_UNLOCK(ipbmap);
  757. pref_ag:
  758. /*
  759. * Let dbNextAG recommend a preferred allocation group
  760. */
  761. agno = dbNextAG(ipbmap);
  762. IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
  763. /* Try to allocate within this allocation group. if that fails, try to
  764. * allocate anywhere in the map.
  765. */
  766. if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
  767. rc = dbAllocAny(bmp, nblocks, l2nb, results);
  768. write_unlock:
  769. IWRITE_UNLOCK(ipbmap);
  770. return (rc);
  771. read_unlock:
  772. IREAD_UNLOCK(ipbmap);
  773. return (rc);
  774. }
  775. #ifdef _NOTYET
  776. /*
  777. * NAME: dbAllocExact()
  778. *
  779. * FUNCTION: try to allocate the requested extent;
  780. *
  781. * PARAMETERS:
  782. * ip - pointer to in-core inode;
  783. * blkno - extent address;
  784. * nblocks - extent length;
  785. *
  786. * RETURN VALUES:
  787. * 0 - success
  788. * -ENOSPC - insufficient disk resources
  789. * -EIO - i/o error
  790. */
  791. int dbAllocExact(struct inode *ip, s64 blkno, int nblocks)
  792. {
  793. int rc;
  794. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  795. struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
  796. struct dmap *dp;
  797. s64 lblkno;
  798. struct metapage *mp;
  799. IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
  800. /*
  801. * validate extent request:
  802. *
  803. * note: defragfs policy:
  804. * max 64 blocks will be moved.
  805. * allocation request size must be satisfied from a single dmap.
  806. */
  807. if (nblocks <= 0 || nblocks > BPERDMAP || blkno >= bmp->db_mapsize) {
  808. IREAD_UNLOCK(ipbmap);
  809. return -EINVAL;
  810. }
  811. if (nblocks > ((s64) 1 << bmp->db_maxfreebud)) {
  812. /* the free space is no longer available */
  813. IREAD_UNLOCK(ipbmap);
  814. return -ENOSPC;
  815. }
  816. /* read in the dmap covering the extent */
  817. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  818. mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
  819. if (mp == NULL) {
  820. IREAD_UNLOCK(ipbmap);
  821. return -EIO;
  822. }
  823. dp = (struct dmap *) mp->data;
  824. /* try to allocate the requested extent */
  825. rc = dbAllocNext(bmp, dp, blkno, nblocks);
  826. IREAD_UNLOCK(ipbmap);
  827. if (rc == 0)
  828. mark_metapage_dirty(mp);
  829. release_metapage(mp);
  830. return (rc);
  831. }
  832. #endif /* _NOTYET */
  833. /*
  834. * NAME: dbReAlloc()
  835. *
  836. * FUNCTION: attempt to extend a current allocation by a specified
  837. * number of blocks.
  838. *
  839. * this routine attempts to satisfy the allocation request
  840. * by first trying to extend the existing allocation in
  841. * place by allocating the additional blocks as the blocks
  842. * immediately following the current allocation. if these
  843. * blocks are not available, this routine will attempt to
  844. * allocate a new set of contiguous blocks large enough
  845. * to cover the existing allocation plus the additional
  846. * number of blocks required.
  847. *
  848. * PARAMETERS:
  849. * ip - pointer to in-core inode requiring allocation.
  850. * blkno - starting block of the current allocation.
  851. * nblocks - number of contiguous blocks within the current
  852. * allocation.
  853. * addnblocks - number of blocks to add to the allocation.
  854. * results - on successful return, set to the starting block number
  855. * of the existing allocation if the existing allocation
  856. * was extended in place or to a newly allocated contiguous
  857. * range if the existing allocation could not be extended
  858. * in place.
  859. *
  860. * RETURN VALUES:
  861. * 0 - success
  862. * -ENOSPC - insufficient disk resources
  863. * -EIO - i/o error
  864. */
  865. int
  866. dbReAlloc(struct inode *ip,
  867. s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
  868. {
  869. int rc;
  870. /* try to extend the allocation in place.
  871. */
  872. if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
  873. *results = blkno;
  874. return (0);
  875. } else {
  876. if (rc != -ENOSPC)
  877. return (rc);
  878. }
  879. /* could not extend the allocation in place, so allocate a
  880. * new set of blocks for the entire request (i.e. try to get
  881. * a range of contiguous blocks large enough to cover the
  882. * existing allocation plus the additional blocks.)
  883. */
  884. return (dbAlloc
  885. (ip, blkno + nblocks - 1, addnblocks + nblocks, results));
  886. }
  887. /*
  888. * NAME: dbExtend()
  889. *
  890. * FUNCTION: attempt to extend a current allocation by a specified
  891. * number of blocks.
  892. *
  893. * this routine attempts to satisfy the allocation request
  894. * by first trying to extend the existing allocation in
  895. * place by allocating the additional blocks as the blocks
  896. * immediately following the current allocation.
  897. *
  898. * PARAMETERS:
  899. * ip - pointer to in-core inode requiring allocation.
  900. * blkno - starting block of the current allocation.
  901. * nblocks - number of contiguous blocks within the current
  902. * allocation.
  903. * addnblocks - number of blocks to add to the allocation.
  904. *
  905. * RETURN VALUES:
  906. * 0 - success
  907. * -ENOSPC - insufficient disk resources
  908. * -EIO - i/o error
  909. */
  910. static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
  911. {
  912. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  913. s64 lblkno, lastblkno, extblkno;
  914. uint rel_block;
  915. struct metapage *mp;
  916. struct dmap *dp;
  917. int rc;
  918. struct inode *ipbmap = sbi->ipbmap;
  919. struct bmap *bmp;
  920. /*
  921. * We don't want a non-aligned extent to cross a page boundary
  922. */
  923. if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
  924. (rel_block + nblocks + addnblocks > sbi->nbperpage))
  925. return -ENOSPC;
  926. /* get the last block of the current allocation */
  927. lastblkno = blkno + nblocks - 1;
  928. /* determine the block number of the block following
  929. * the existing allocation.
  930. */
  931. extblkno = lastblkno + 1;
  932. IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
  933. /* better be within the file system */
  934. bmp = sbi->bmap;
  935. if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
  936. IREAD_UNLOCK(ipbmap);
  937. jfs_error(ip->i_sb,
  938. "dbExtend: the block is outside the filesystem");
  939. return -EIO;
  940. }
  941. /* we'll attempt to extend the current allocation in place by
  942. * allocating the additional blocks as the blocks immediately
  943. * following the current allocation. we only try to extend the
  944. * current allocation in place if the number of additional blocks
  945. * can fit into a dmap, the last block of the current allocation
  946. * is not the last block of the file system, and the start of the
  947. * inplace extension is not on an allocation group boundary.
  948. */
  949. if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
  950. (extblkno & (bmp->db_agsize - 1)) == 0) {
  951. IREAD_UNLOCK(ipbmap);
  952. return -ENOSPC;
  953. }
  954. /* get the buffer for the dmap containing the first block
  955. * of the extension.
  956. */
  957. lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
  958. mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
  959. if (mp == NULL) {
  960. IREAD_UNLOCK(ipbmap);
  961. return -EIO;
  962. }
  963. dp = (struct dmap *) mp->data;
  964. /* try to allocate the blocks immediately following the
  965. * current allocation.
  966. */
  967. rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
  968. IREAD_UNLOCK(ipbmap);
  969. /* were we successful ? */
  970. if (rc == 0)
  971. write_metapage(mp);
  972. else
  973. /* we were not successful */
  974. release_metapage(mp);
  975. return (rc);
  976. }
  977. /*
  978. * NAME: dbAllocNext()
  979. *
  980. * FUNCTION: attempt to allocate the blocks of the specified block
  981. * range within a dmap.
  982. *
  983. * PARAMETERS:
  984. * bmp - pointer to bmap descriptor
  985. * dp - pointer to dmap.
  986. * blkno - starting block number of the range.
  987. * nblocks - number of contiguous free blocks of the range.
  988. *
  989. * RETURN VALUES:
  990. * 0 - success
  991. * -ENOSPC - insufficient disk resources
  992. * -EIO - i/o error
  993. *
  994. * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
  995. */
  996. static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
  997. int nblocks)
  998. {
  999. int dbitno, word, rembits, nb, nwords, wbitno, nw;
  1000. int l2size;
  1001. s8 *leaf;
  1002. u32 mask;
  1003. if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
  1004. jfs_error(bmp->db_ipbmap->i_sb,
  1005. "dbAllocNext: Corrupt dmap page");
  1006. return -EIO;
  1007. }
  1008. /* pick up a pointer to the leaves of the dmap tree.
  1009. */
  1010. leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
  1011. /* determine the bit number and word within the dmap of the
  1012. * starting block.
  1013. */
  1014. dbitno = blkno & (BPERDMAP - 1);
  1015. word = dbitno >> L2DBWORD;
  1016. /* check if the specified block range is contained within
  1017. * this dmap.
  1018. */
  1019. if (dbitno + nblocks > BPERDMAP)
  1020. return -ENOSPC;
  1021. /* check if the starting leaf indicates that anything
  1022. * is free.
  1023. */
  1024. if (leaf[word] == NOFREE)
  1025. return -ENOSPC;
  1026. /* check the dmaps words corresponding to block range to see
  1027. * if the block range is free. not all bits of the first and
  1028. * last words may be contained within the block range. if this
  1029. * is the case, we'll work against those words (i.e. partial first
  1030. * and/or last) on an individual basis (a single pass) and examine
  1031. * the actual bits to determine if they are free. a single pass
  1032. * will be used for all dmap words fully contained within the
  1033. * specified range. within this pass, the leaves of the dmap
  1034. * tree will be examined to determine if the blocks are free. a
  1035. * single leaf may describe the free space of multiple dmap
  1036. * words, so we may visit only a subset of the actual leaves
  1037. * corresponding to the dmap words of the block range.
  1038. */
  1039. for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
  1040. /* determine the bit number within the word and
  1041. * the number of bits within the word.
  1042. */
  1043. wbitno = dbitno & (DBWORD - 1);
  1044. nb = min(rembits, DBWORD - wbitno);
  1045. /* check if only part of the word is to be examined.
  1046. */
  1047. if (nb < DBWORD) {
  1048. /* check if the bits are free.
  1049. */
  1050. mask = (ONES << (DBWORD - nb) >> wbitno);
  1051. if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
  1052. return -ENOSPC;
  1053. word += 1;
  1054. } else {
  1055. /* one or more dmap words are fully contained
  1056. * within the block range. determine how many
  1057. * words and how many bits.
  1058. */
  1059. nwords = rembits >> L2DBWORD;
  1060. nb = nwords << L2DBWORD;
  1061. /* now examine the appropriate leaves to determine
  1062. * if the blocks are free.
  1063. */
  1064. while (nwords > 0) {
  1065. /* does the leaf describe any free space ?
  1066. */
  1067. if (leaf[word] < BUDMIN)
  1068. return -ENOSPC;
  1069. /* determine the l2 number of bits provided
  1070. * by this leaf.
  1071. */
  1072. l2size =
  1073. min((int)leaf[word], NLSTOL2BSZ(nwords));
  1074. /* determine how many words were handled.
  1075. */
  1076. nw = BUDSIZE(l2size, BUDMIN);
  1077. nwords -= nw;
  1078. word += nw;
  1079. }
  1080. }
  1081. }
  1082. /* allocate the blocks.
  1083. */
  1084. return (dbAllocDmap(bmp, dp, blkno, nblocks));
  1085. }
  1086. /*
  1087. * NAME: dbAllocNear()
  1088. *
  1089. * FUNCTION: attempt to allocate a number of contiguous free blocks near
  1090. * a specified block (hint) within a dmap.
  1091. *
  1092. * starting with the dmap leaf that covers the hint, we'll
  1093. * check the next four contiguous leaves for sufficient free
  1094. * space. if sufficient free space is found, we'll allocate
  1095. * the desired free space.
  1096. *
  1097. * PARAMETERS:
  1098. * bmp - pointer to bmap descriptor
  1099. * dp - pointer to dmap.
  1100. * blkno - block number to allocate near.
  1101. * nblocks - actual number of contiguous free blocks desired.
  1102. * l2nb - log2 number of contiguous free blocks desired.
  1103. * results - on successful return, set to the starting block number
  1104. * of the newly allocated range.
  1105. *
  1106. * RETURN VALUES:
  1107. * 0 - success
  1108. * -ENOSPC - insufficient disk resources
  1109. * -EIO - i/o error
  1110. *
  1111. * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
  1112. */
  1113. static int
  1114. dbAllocNear(struct bmap * bmp,
  1115. struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
  1116. {
  1117. int word, lword, rc;
  1118. s8 *leaf;
  1119. if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
  1120. jfs_error(bmp->db_ipbmap->i_sb,
  1121. "dbAllocNear: Corrupt dmap page");
  1122. return -EIO;
  1123. }
  1124. leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
  1125. /* determine the word within the dmap that holds the hint
  1126. * (i.e. blkno). also, determine the last word in the dmap
  1127. * that we'll include in our examination.
  1128. */
  1129. word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
  1130. lword = min(word + 4, LPERDMAP);
  1131. /* examine the leaves for sufficient free space.
  1132. */
  1133. for (; word < lword; word++) {
  1134. /* does the leaf describe sufficient free space ?
  1135. */
  1136. if (leaf[word] < l2nb)
  1137. continue;
  1138. /* determine the block number within the file system
  1139. * of the first block described by this dmap word.
  1140. */
  1141. blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
  1142. /* if not all bits of the dmap word are free, get the
  1143. * starting bit number within the dmap word of the required
  1144. * string of free bits and adjust the block number with the
  1145. * value.
  1146. */
  1147. if (leaf[word] < BUDMIN)
  1148. blkno +=
  1149. dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
  1150. /* allocate the blocks.
  1151. */
  1152. if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
  1153. *results = blkno;
  1154. return (rc);
  1155. }
  1156. return -ENOSPC;
  1157. }
  1158. /*
  1159. * NAME: dbAllocAG()
  1160. *
  1161. * FUNCTION: attempt to allocate the specified number of contiguous
  1162. * free blocks within the specified allocation group.
  1163. *
  1164. * unless the allocation group size is equal to the number
  1165. * of blocks per dmap, the dmap control pages will be used to
  1166. * find the required free space, if available. we start the
  1167. * search at the highest dmap control page level which
  1168. * distinctly describes the allocation group's free space
  1169. * (i.e. the highest level at which the allocation group's
  1170. * free space is not mixed in with that of any other group).
  1171. * in addition, we start the search within this level at a
  1172. * height of the dmapctl dmtree at which the nodes distinctly
  1173. * describe the allocation group's free space. at this height,
  1174. * the allocation group's free space may be represented by 1
  1175. * or two sub-trees, depending on the allocation group size.
  1176. * we search the top nodes of these subtrees left to right for
  1177. * sufficient free space. if sufficient free space is found,
  1178. * the subtree is searched to find the leftmost leaf that
  1179. * has free space. once we have made it to the leaf, we
  1180. * move the search to the next lower level dmap control page
  1181. * corresponding to this leaf. we continue down the dmap control
  1182. * pages until we find the dmap that contains or starts the
  1183. * sufficient free space and we allocate at this dmap.
  1184. *
  1185. * if the allocation group size is equal to the dmap size,
  1186. * we'll start at the dmap corresponding to the allocation
  1187. * group and attempt the allocation at this level.
  1188. *
  1189. * the dmap control page search is also not performed if the
  1190. * allocation group is completely free and we go to the first
  1191. * dmap of the allocation group to do the allocation. this is
  1192. * done because the allocation group may be part (not the first
  1193. * part) of a larger binary buddy system, causing the dmap
  1194. * control pages to indicate no free space (NOFREE) within
  1195. * the allocation group.
  1196. *
  1197. * PARAMETERS:
  1198. * bmp - pointer to bmap descriptor
  1199. * agno - allocation group number.
  1200. * nblocks - actual number of contiguous free blocks desired.
  1201. * l2nb - log2 number of contiguous free blocks desired.
  1202. * results - on successful return, set to the starting block number
  1203. * of the newly allocated range.
  1204. *
  1205. * RETURN VALUES:
  1206. * 0 - success
  1207. * -ENOSPC - insufficient disk resources
  1208. * -EIO - i/o error
  1209. *
  1210. * note: IWRITE_LOCK(ipmap) held on entry/exit;
  1211. */
  1212. static int
  1213. dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
  1214. {
  1215. struct metapage *mp;
  1216. struct dmapctl *dcp;
  1217. int rc, ti, i, k, m, n, agperlev;
  1218. s64 blkno, lblkno;
  1219. int budmin;
  1220. /* allocation request should not be for more than the
  1221. * allocation group size.
  1222. */
  1223. if (l2nb > bmp->db_agl2size) {
  1224. jfs_error(bmp->db_ipbmap->i_sb,
  1225. "dbAllocAG: allocation request is larger than the "
  1226. "allocation group size");
  1227. return -EIO;
  1228. }
  1229. /* determine the starting block number of the allocation
  1230. * group.
  1231. */
  1232. blkno = (s64) agno << bmp->db_agl2size;
  1233. /* check if the allocation group size is the minimum allocation
  1234. * group size or if the allocation group is completely free. if
  1235. * the allocation group size is the minimum size of BPERDMAP (i.e.
  1236. * 1 dmap), there is no need to search the dmap control page (below)
  1237. * that fully describes the allocation group since the allocation
  1238. * group is already fully described by a dmap. in this case, we
  1239. * just call dbAllocCtl() to search the dmap tree and allocate the
  1240. * required space if available.
  1241. *
  1242. * if the allocation group is completely free, dbAllocCtl() is
  1243. * also called to allocate the required space. this is done for
  1244. * two reasons. first, it makes no sense searching the dmap control
  1245. * pages for free space when we know that free space exists. second,
  1246. * the dmap control pages may indicate that the allocation group
  1247. * has no free space if the allocation group is part (not the first
  1248. * part) of a larger binary buddy system.
  1249. */
  1250. if (bmp->db_agsize == BPERDMAP
  1251. || bmp->db_agfree[agno] == bmp->db_agsize) {
  1252. rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
  1253. if ((rc == -ENOSPC) &&
  1254. (bmp->db_agfree[agno] == bmp->db_agsize)) {
  1255. printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
  1256. (unsigned long long) blkno,
  1257. (unsigned long long) nblocks);
  1258. jfs_error(bmp->db_ipbmap->i_sb,
  1259. "dbAllocAG: dbAllocCtl failed in free AG");
  1260. }
  1261. return (rc);
  1262. }
  1263. /* the buffer for the dmap control page that fully describes the
  1264. * allocation group.
  1265. */
  1266. lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
  1267. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  1268. if (mp == NULL)
  1269. return -EIO;
  1270. dcp = (struct dmapctl *) mp->data;
  1271. budmin = dcp->budmin;
  1272. if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
  1273. jfs_error(bmp->db_ipbmap->i_sb,
  1274. "dbAllocAG: Corrupt dmapctl page");
  1275. release_metapage(mp);
  1276. return -EIO;
  1277. }
  1278. /* search the subtree(s) of the dmap control page that describes
  1279. * the allocation group, looking for sufficient free space. to begin,
  1280. * determine how many allocation groups are represented in a dmap
  1281. * control page at the control page level (i.e. L0, L1, L2) that
  1282. * fully describes an allocation group. next, determine the starting
  1283. * tree index of this allocation group within the control page.
  1284. */
  1285. agperlev =
  1286. (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
  1287. ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
  1288. /* dmap control page trees fan-out by 4 and a single allocation
  1289. * group may be described by 1 or 2 subtrees within the ag level
  1290. * dmap control page, depending upon the ag size. examine the ag's
  1291. * subtrees for sufficient free space, starting with the leftmost
  1292. * subtree.
  1293. */
  1294. for (i = 0; i < bmp->db_agwidth; i++, ti++) {
  1295. /* is there sufficient free space ?
  1296. */
  1297. if (l2nb > dcp->stree[ti])
  1298. continue;
  1299. /* sufficient free space found in a subtree. now search down
  1300. * the subtree to find the leftmost leaf that describes this
  1301. * free space.
  1302. */
  1303. for (k = bmp->db_agheight; k > 0; k--) {
  1304. for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
  1305. if (l2nb <= dcp->stree[m + n]) {
  1306. ti = m + n;
  1307. break;
  1308. }
  1309. }
  1310. if (n == 4) {
  1311. jfs_error(bmp->db_ipbmap->i_sb,
  1312. "dbAllocAG: failed descending stree");
  1313. release_metapage(mp);
  1314. return -EIO;
  1315. }
  1316. }
  1317. /* determine the block number within the file system
  1318. * that corresponds to this leaf.
  1319. */
  1320. if (bmp->db_aglevel == 2)
  1321. blkno = 0;
  1322. else if (bmp->db_aglevel == 1)
  1323. blkno &= ~(MAXL1SIZE - 1);
  1324. else /* bmp->db_aglevel == 0 */
  1325. blkno &= ~(MAXL0SIZE - 1);
  1326. blkno +=
  1327. ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
  1328. /* release the buffer in preparation for going down
  1329. * the next level of dmap control pages.
  1330. */
  1331. release_metapage(mp);
  1332. /* check if we need to continue to search down the lower
  1333. * level dmap control pages. we need to if the number of
  1334. * blocks required is less than maximum number of blocks
  1335. * described at the next lower level.
  1336. */
  1337. if (l2nb < budmin) {
  1338. /* search the lower level dmap control pages to get
  1339. * the starting block number of the dmap that
  1340. * contains or starts off the free space.
  1341. */
  1342. if ((rc =
  1343. dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
  1344. &blkno))) {
  1345. if (rc == -ENOSPC) {
  1346. jfs_error(bmp->db_ipbmap->i_sb,
  1347. "dbAllocAG: control page "
  1348. "inconsistent");
  1349. return -EIO;
  1350. }
  1351. return (rc);
  1352. }
  1353. }
  1354. /* allocate the blocks.
  1355. */
  1356. rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
  1357. if (rc == -ENOSPC) {
  1358. jfs_error(bmp->db_ipbmap->i_sb,
  1359. "dbAllocAG: unable to allocate blocks");
  1360. rc = -EIO;
  1361. }
  1362. return (rc);
  1363. }
  1364. /* no space in the allocation group. release the buffer and
  1365. * return -ENOSPC.
  1366. */
  1367. release_metapage(mp);
  1368. return -ENOSPC;
  1369. }
  1370. /*
  1371. * NAME: dbAllocAny()
  1372. *
  1373. * FUNCTION: attempt to allocate the specified number of contiguous
  1374. * free blocks anywhere in the file system.
  1375. *
  1376. * dbAllocAny() attempts to find the sufficient free space by
  1377. * searching down the dmap control pages, starting with the
  1378. * highest level (i.e. L0, L1, L2) control page. if free space
  1379. * large enough to satisfy the desired free space is found, the
  1380. * desired free space is allocated.
  1381. *
  1382. * PARAMETERS:
  1383. * bmp - pointer to bmap descriptor
  1384. * nblocks - actual number of contiguous free blocks desired.
  1385. * l2nb - log2 number of contiguous free blocks desired.
  1386. * results - on successful return, set to the starting block number
  1387. * of the newly allocated range.
  1388. *
  1389. * RETURN VALUES:
  1390. * 0 - success
  1391. * -ENOSPC - insufficient disk resources
  1392. * -EIO - i/o error
  1393. *
  1394. * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
  1395. */
  1396. static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
  1397. {
  1398. int rc;
  1399. s64 blkno = 0;
  1400. /* starting with the top level dmap control page, search
  1401. * down the dmap control levels for sufficient free space.
  1402. * if free space is found, dbFindCtl() returns the starting
  1403. * block number of the dmap that contains or starts off the
  1404. * range of free space.
  1405. */
  1406. if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
  1407. return (rc);
  1408. /* allocate the blocks.
  1409. */
  1410. rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
  1411. if (rc == -ENOSPC) {
  1412. jfs_error(bmp->db_ipbmap->i_sb,
  1413. "dbAllocAny: unable to allocate blocks");
  1414. return -EIO;
  1415. }
  1416. return (rc);
  1417. }
  1418. /*
  1419. * NAME: dbDiscardAG()
  1420. *
  1421. * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
  1422. *
  1423. * algorithm:
  1424. * 1) allocate blocks, as large as possible and save them
  1425. * while holding IWRITE_LOCK on ipbmap
  1426. * 2) trim all these saved block/length values
  1427. * 3) mark the blocks free again
  1428. *
  1429. * benefit:
  1430. * - we work only on one ag at some time, minimizing how long we
  1431. * need to lock ipbmap
  1432. * - reading / writing the fs is possible most time, even on
  1433. * trimming
  1434. *
  1435. * downside:
  1436. * - we write two times to the dmapctl and dmap pages
  1437. * - but for me, this seems the best way, better ideas?
  1438. * /TR 2012
  1439. *
  1440. * PARAMETERS:
  1441. * ip - pointer to in-core inode
  1442. * agno - ag to trim
  1443. * minlen - minimum value of contiguous blocks
  1444. *
  1445. * RETURN VALUES:
  1446. * s64 - actual number of blocks trimmed
  1447. */
  1448. s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
  1449. {
  1450. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  1451. struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
  1452. s64 nblocks, blkno;
  1453. u64 trimmed = 0;
  1454. int rc, l2nb;
  1455. struct super_block *sb = ipbmap->i_sb;
  1456. struct range2trim {
  1457. u64 blkno;
  1458. u64 nblocks;
  1459. } *totrim, *tt;
  1460. /* max blkno / nblocks pairs to trim */
  1461. int count = 0, range_cnt;
  1462. u64 max_ranges;
  1463. /* prevent others from writing new stuff here, while trimming */
  1464. IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
  1465. nblocks = bmp->db_agfree[agno];
  1466. max_ranges = nblocks;
  1467. do_div(max_ranges, minlen);
  1468. range_cnt = min_t(u64, max_ranges + 1, 32 * 1024);
  1469. totrim = kmalloc(sizeof(struct range2trim) * range_cnt, GFP_NOFS);
  1470. if (totrim == NULL) {
  1471. jfs_error(bmp->db_ipbmap->i_sb,
  1472. "dbDiscardAG: no memory for trim array");
  1473. IWRITE_UNLOCK(ipbmap);
  1474. return 0;
  1475. }
  1476. tt = totrim;
  1477. while (nblocks >= minlen) {
  1478. l2nb = BLKSTOL2(nblocks);
  1479. /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
  1480. rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
  1481. if (rc == 0) {
  1482. tt->blkno = blkno;
  1483. tt->nblocks = nblocks;
  1484. tt++; count++;
  1485. /* the whole ag is free, trim now */
  1486. if (bmp->db_agfree[agno] == 0)
  1487. break;
  1488. /* give a hint for the next while */
  1489. nblocks = bmp->db_agfree[agno];
  1490. continue;
  1491. } else if (rc == -ENOSPC) {
  1492. /* search for next smaller log2 block */
  1493. l2nb = BLKSTOL2(nblocks) - 1;
  1494. nblocks = 1 << l2nb;
  1495. } else {
  1496. /* Trim any already allocated blocks */
  1497. jfs_error(bmp->db_ipbmap->i_sb,
  1498. "dbDiscardAG: -EIO");
  1499. break;
  1500. }
  1501. /* check, if our trim array is full */
  1502. if (unlikely(count >= range_cnt - 1))
  1503. break;
  1504. }
  1505. IWRITE_UNLOCK(ipbmap);
  1506. tt->nblocks = 0; /* mark the current end */
  1507. for (tt = totrim; tt->nblocks != 0; tt++) {
  1508. /* when mounted with online discard, dbFree() will
  1509. * call jfs_issue_discard() itself */
  1510. if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
  1511. jfs_issue_discard(ip, tt->blkno, tt->nblocks);
  1512. dbFree(ip, tt->blkno, tt->nblocks);
  1513. trimmed += tt->nblocks;
  1514. }
  1515. kfree(totrim);
  1516. return trimmed;
  1517. }
  1518. /*
  1519. * NAME: dbFindCtl()
  1520. *
  1521. * FUNCTION: starting at a specified dmap control page level and block
  1522. * number, search down the dmap control levels for a range of
  1523. * contiguous free blocks large enough to satisfy an allocation
  1524. * request for the specified number of free blocks.
  1525. *
  1526. * if sufficient contiguous free blocks are found, this routine
  1527. * returns the starting block number within a dmap page that
  1528. * contains or starts a range of contiqious free blocks that
  1529. * is sufficient in size.
  1530. *
  1531. * PARAMETERS:
  1532. * bmp - pointer to bmap descriptor
  1533. * level - starting dmap control page level.
  1534. * l2nb - log2 number of contiguous free blocks desired.
  1535. * *blkno - on entry, starting block number for conducting the search.
  1536. * on successful return, the first block within a dmap page
  1537. * that contains or starts a range of contiguous free blocks.
  1538. *
  1539. * RETURN VALUES:
  1540. * 0 - success
  1541. * -ENOSPC - insufficient disk resources
  1542. * -EIO - i/o error
  1543. *
  1544. * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
  1545. */
  1546. static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
  1547. {
  1548. int rc, leafidx, lev;
  1549. s64 b, lblkno;
  1550. struct dmapctl *dcp;
  1551. int budmin;
  1552. struct metapage *mp;
  1553. /* starting at the specified dmap control page level and block
  1554. * number, search down the dmap control levels for the starting
  1555. * block number of a dmap page that contains or starts off
  1556. * sufficient free blocks.
  1557. */
  1558. for (lev = level, b = *blkno; lev >= 0; lev--) {
  1559. /* get the buffer of the dmap control page for the block
  1560. * number and level (i.e. L0, L1, L2).
  1561. */
  1562. lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
  1563. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  1564. if (mp == NULL)
  1565. return -EIO;
  1566. dcp = (struct dmapctl *) mp->data;
  1567. budmin = dcp->budmin;
  1568. if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
  1569. jfs_error(bmp->db_ipbmap->i_sb,
  1570. "dbFindCtl: Corrupt dmapctl page");
  1571. release_metapage(mp);
  1572. return -EIO;
  1573. }
  1574. /* search the tree within the dmap control page for
  1575. * sufficient free space. if sufficient free space is found,
  1576. * dbFindLeaf() returns the index of the leaf at which
  1577. * free space was found.
  1578. */
  1579. rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx);
  1580. /* release the buffer.
  1581. */
  1582. release_metapage(mp);
  1583. /* space found ?
  1584. */
  1585. if (rc) {
  1586. if (lev != level) {
  1587. jfs_error(bmp->db_ipbmap->i_sb,
  1588. "dbFindCtl: dmap inconsistent");
  1589. return -EIO;
  1590. }
  1591. return -ENOSPC;
  1592. }
  1593. /* adjust the block number to reflect the location within
  1594. * the dmap control page (i.e. the leaf) at which free
  1595. * space was found.
  1596. */
  1597. b += (((s64) leafidx) << budmin);
  1598. /* we stop the search at this dmap control page level if
  1599. * the number of blocks required is greater than or equal
  1600. * to the maximum number of blocks described at the next
  1601. * (lower) level.
  1602. */
  1603. if (l2nb >= budmin)
  1604. break;
  1605. }
  1606. *blkno = b;
  1607. return (0);
  1608. }
  1609. /*
  1610. * NAME: dbAllocCtl()
  1611. *
  1612. * FUNCTION: attempt to allocate a specified number of contiguous
  1613. * blocks starting within a specific dmap.
  1614. *
  1615. * this routine is called by higher level routines that search
  1616. * the dmap control pages above the actual dmaps for contiguous
  1617. * free space. the result of successful searches by these
  1618. * routines are the starting block numbers within dmaps, with
  1619. * the dmaps themselves containing the desired contiguous free
  1620. * space or starting a contiguous free space of desired size
  1621. * that is made up of the blocks of one or more dmaps. these
  1622. * calls should not fail due to insufficent resources.
  1623. *
  1624. * this routine is called in some cases where it is not known
  1625. * whether it will fail due to insufficient resources. more
  1626. * specifically, this occurs when allocating from an allocation
  1627. * group whose size is equal to the number of blocks per dmap.
  1628. * in this case, the dmap control pages are not examined prior
  1629. * to calling this routine (to save pathlength) and the call
  1630. * might fail.
  1631. *
  1632. * for a request size that fits within a dmap, this routine relies
  1633. * upon the dmap's dmtree to find the requested contiguous free
  1634. * space. for request sizes that are larger than a dmap, the
  1635. * requested free space will start at the first block of the
  1636. * first dmap (i.e. blkno).
  1637. *
  1638. * PARAMETERS:
  1639. * bmp - pointer to bmap descriptor
  1640. * nblocks - actual number of contiguous free blocks to allocate.
  1641. * l2nb - log2 number of contiguous free blocks to allocate.
  1642. * blkno - starting block number of the dmap to start the allocation
  1643. * from.
  1644. * results - on successful return, set to the starting block number
  1645. * of the newly allocated range.
  1646. *
  1647. * RETURN VALUES:
  1648. * 0 - success
  1649. * -ENOSPC - insufficient disk resources
  1650. * -EIO - i/o error
  1651. *
  1652. * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
  1653. */
  1654. static int
  1655. dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
  1656. {
  1657. int rc, nb;
  1658. s64 b, lblkno, n;
  1659. struct metapage *mp;
  1660. struct dmap *dp;
  1661. /* check if the allocation request is confined to a single dmap.
  1662. */
  1663. if (l2nb <= L2BPERDMAP) {
  1664. /* get the buffer for the dmap.
  1665. */
  1666. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  1667. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  1668. if (mp == NULL)
  1669. return -EIO;
  1670. dp = (struct dmap *) mp->data;
  1671. /* try to allocate the blocks.
  1672. */
  1673. rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
  1674. if (rc == 0)
  1675. mark_metapage_dirty(mp);
  1676. release_metapage(mp);
  1677. return (rc);
  1678. }
  1679. /* allocation request involving multiple dmaps. it must start on
  1680. * a dmap boundary.
  1681. */
  1682. assert((blkno & (BPERDMAP - 1)) == 0);
  1683. /* allocate the blocks dmap by dmap.
  1684. */
  1685. for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
  1686. /* get the buffer for the dmap.
  1687. */
  1688. lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
  1689. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  1690. if (mp == NULL) {
  1691. rc = -EIO;
  1692. goto backout;
  1693. }
  1694. dp = (struct dmap *) mp->data;
  1695. /* the dmap better be all free.
  1696. */
  1697. if (dp->tree.stree[ROOT] != L2BPERDMAP) {
  1698. release_metapage(mp);
  1699. jfs_error(bmp->db_ipbmap->i_sb,
  1700. "dbAllocCtl: the dmap is not all free");
  1701. rc = -EIO;
  1702. goto backout;
  1703. }
  1704. /* determine how many blocks to allocate from this dmap.
  1705. */
  1706. nb = min(n, (s64)BPERDMAP);
  1707. /* allocate the blocks from the dmap.
  1708. */
  1709. if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
  1710. release_metapage(mp);
  1711. goto backout;
  1712. }
  1713. /* write the buffer.
  1714. */
  1715. write_metapage(mp);
  1716. }
  1717. /* set the results (starting block number) and return.
  1718. */
  1719. *results = blkno;
  1720. return (0);
  1721. /* something failed in handling an allocation request involving
  1722. * multiple dmaps. we'll try to clean up by backing out any
  1723. * allocation that has already happened for this request. if
  1724. * we fail in backing out the allocation, we'll mark the file
  1725. * system to indicate that blocks have been leaked.
  1726. */
  1727. backout:
  1728. /* try to backout the allocations dmap by dmap.
  1729. */
  1730. for (n = nblocks - n, b = blkno; n > 0;
  1731. n -= BPERDMAP, b += BPERDMAP) {
  1732. /* get the buffer for this dmap.
  1733. */
  1734. lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
  1735. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  1736. if (mp == NULL) {
  1737. /* could not back out. mark the file system
  1738. * to indicate that we have leaked blocks.
  1739. */
  1740. jfs_error(bmp->db_ipbmap->i_sb,
  1741. "dbAllocCtl: I/O Error: Block Leakage.");
  1742. continue;
  1743. }
  1744. dp = (struct dmap *) mp->data;
  1745. /* free the blocks is this dmap.
  1746. */
  1747. if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
  1748. /* could not back out. mark the file system
  1749. * to indicate that we have leaked blocks.
  1750. */
  1751. release_metapage(mp);
  1752. jfs_error(bmp->db_ipbmap->i_sb,
  1753. "dbAllocCtl: Block Leakage.");
  1754. continue;
  1755. }
  1756. /* write the buffer.
  1757. */
  1758. write_metapage(mp);
  1759. }
  1760. return (rc);
  1761. }
  1762. /*
  1763. * NAME: dbAllocDmapLev()
  1764. *
  1765. * FUNCTION: attempt to allocate a specified number of contiguous blocks
  1766. * from a specified dmap.
  1767. *
  1768. * this routine checks if the contiguous blocks are available.
  1769. * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
  1770. * returned.
  1771. *
  1772. * PARAMETERS:
  1773. * mp - pointer to bmap descriptor
  1774. * dp - pointer to dmap to attempt to allocate blocks from.
  1775. * l2nb - log2 number of contiguous block desired.
  1776. * nblocks - actual number of contiguous block desired.
  1777. * results - on successful return, set to the starting block number
  1778. * of the newly allocated range.
  1779. *
  1780. * RETURN VALUES:
  1781. * 0 - success
  1782. * -ENOSPC - insufficient disk resources
  1783. * -EIO - i/o error
  1784. *
  1785. * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
  1786. * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
  1787. */
  1788. static int
  1789. dbAllocDmapLev(struct bmap * bmp,
  1790. struct dmap * dp, int nblocks, int l2nb, s64 * results)
  1791. {
  1792. s64 blkno;
  1793. int leafidx, rc;
  1794. /* can't be more than a dmaps worth of blocks */
  1795. assert(l2nb <= L2BPERDMAP);
  1796. /* search the tree within the dmap page for sufficient
  1797. * free space. if sufficient free space is found, dbFindLeaf()
  1798. * returns the index of the leaf at which free space was found.
  1799. */
  1800. if (dbFindLeaf((dmtree_t *) & dp->tree, l2nb, &leafidx))
  1801. return -ENOSPC;
  1802. /* determine the block number within the file system corresponding
  1803. * to the leaf at which free space was found.
  1804. */
  1805. blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
  1806. /* if not all bits of the dmap word are free, get the starting
  1807. * bit number within the dmap word of the required string of free
  1808. * bits and adjust the block number with this value.
  1809. */
  1810. if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
  1811. blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
  1812. /* allocate the blocks */
  1813. if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
  1814. *results = blkno;
  1815. return (rc);
  1816. }
  1817. /*
  1818. * NAME: dbAllocDmap()
  1819. *
  1820. * FUNCTION: adjust the disk allocation map to reflect the allocation
  1821. * of a specified block range within a dmap.
  1822. *
  1823. * this routine allocates the specified blocks from the dmap
  1824. * through a call to dbAllocBits(). if the allocation of the
  1825. * block range causes the maximum string of free blocks within
  1826. * the dmap to change (i.e. the value of the root of the dmap's
  1827. * dmtree), this routine will cause this change to be reflected
  1828. * up through the appropriate levels of the dmap control pages
  1829. * by a call to dbAdjCtl() for the L0 dmap control page that
  1830. * covers this dmap.
  1831. *
  1832. * PARAMETERS:
  1833. * bmp - pointer to bmap descriptor
  1834. * dp - pointer to dmap to allocate the block range from.
  1835. * blkno - starting block number of the block to be allocated.
  1836. * nblocks - number of blocks to be allocated.
  1837. *
  1838. * RETURN VALUES:
  1839. * 0 - success
  1840. * -EIO - i/o error
  1841. *
  1842. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  1843. */
  1844. static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
  1845. int nblocks)
  1846. {
  1847. s8 oldroot;
  1848. int rc;
  1849. /* save the current value of the root (i.e. maximum free string)
  1850. * of the dmap tree.
  1851. */
  1852. oldroot = dp->tree.stree[ROOT];
  1853. /* allocate the specified (blocks) bits */
  1854. dbAllocBits(bmp, dp, blkno, nblocks);
  1855. /* if the root has not changed, done. */
  1856. if (dp->tree.stree[ROOT] == oldroot)
  1857. return (0);
  1858. /* root changed. bubble the change up to the dmap control pages.
  1859. * if the adjustment of the upper level control pages fails,
  1860. * backout the bit allocation (thus making everything consistent).
  1861. */
  1862. if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
  1863. dbFreeBits(bmp, dp, blkno, nblocks);
  1864. return (rc);
  1865. }
  1866. /*
  1867. * NAME: dbFreeDmap()
  1868. *
  1869. * FUNCTION: adjust the disk allocation map to reflect the allocation
  1870. * of a specified block range within a dmap.
  1871. *
  1872. * this routine frees the specified blocks from the dmap through
  1873. * a call to dbFreeBits(). if the deallocation of the block range
  1874. * causes the maximum string of free blocks within the dmap to
  1875. * change (i.e. the value of the root of the dmap's dmtree), this
  1876. * routine will cause this change to be reflected up through the
  1877. * appropriate levels of the dmap control pages by a call to
  1878. * dbAdjCtl() for the L0 dmap control page that covers this dmap.
  1879. *
  1880. * PARAMETERS:
  1881. * bmp - pointer to bmap descriptor
  1882. * dp - pointer to dmap to free the block range from.
  1883. * blkno - starting block number of the block to be freed.
  1884. * nblocks - number of blocks to be freed.
  1885. *
  1886. * RETURN VALUES:
  1887. * 0 - success
  1888. * -EIO - i/o error
  1889. *
  1890. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  1891. */
  1892. static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
  1893. int nblocks)
  1894. {
  1895. s8 oldroot;
  1896. int rc = 0, word;
  1897. /* save the current value of the root (i.e. maximum free string)
  1898. * of the dmap tree.
  1899. */
  1900. oldroot = dp->tree.stree[ROOT];
  1901. /* free the specified (blocks) bits */
  1902. rc = dbFreeBits(bmp, dp, blkno, nblocks);
  1903. /* if error or the root has not changed, done. */
  1904. if (rc || (dp->tree.stree[ROOT] == oldroot))
  1905. return (rc);
  1906. /* root changed. bubble the change up to the dmap control pages.
  1907. * if the adjustment of the upper level control pages fails,
  1908. * backout the deallocation.
  1909. */
  1910. if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
  1911. word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
  1912. /* as part of backing out the deallocation, we will have
  1913. * to back split the dmap tree if the deallocation caused
  1914. * the freed blocks to become part of a larger binary buddy
  1915. * system.
  1916. */
  1917. if (dp->tree.stree[word] == NOFREE)
  1918. dbBackSplit((dmtree_t *) & dp->tree, word);
  1919. dbAllocBits(bmp, dp, blkno, nblocks);
  1920. }
  1921. return (rc);
  1922. }
  1923. /*
  1924. * NAME: dbAllocBits()
  1925. *
  1926. * FUNCTION: allocate a specified block range from a dmap.
  1927. *
  1928. * this routine updates the dmap to reflect the working
  1929. * state allocation of the specified block range. it directly
  1930. * updates the bits of the working map and causes the adjustment
  1931. * of the binary buddy system described by the dmap's dmtree
  1932. * leaves to reflect the bits allocated. it also causes the
  1933. * dmap's dmtree, as a whole, to reflect the allocated range.
  1934. *
  1935. * PARAMETERS:
  1936. * bmp - pointer to bmap descriptor
  1937. * dp - pointer to dmap to allocate bits from.
  1938. * blkno - starting block number of the bits to be allocated.
  1939. * nblocks - number of bits to be allocated.
  1940. *
  1941. * RETURN VALUES: none
  1942. *
  1943. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  1944. */
  1945. static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
  1946. int nblocks)
  1947. {
  1948. int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
  1949. dmtree_t *tp = (dmtree_t *) & dp->tree;
  1950. int size;
  1951. s8 *leaf;
  1952. /* pick up a pointer to the leaves of the dmap tree */
  1953. leaf = dp->tree.stree + LEAFIND;
  1954. /* determine the bit number and word within the dmap of the
  1955. * starting block.
  1956. */
  1957. dbitno = blkno & (BPERDMAP - 1);
  1958. word = dbitno >> L2DBWORD;
  1959. /* block range better be within the dmap */
  1960. assert(dbitno + nblocks <= BPERDMAP);
  1961. /* allocate the bits of the dmap's words corresponding to the block
  1962. * range. not all bits of the first and last words may be contained
  1963. * within the block range. if this is the case, we'll work against
  1964. * those words (i.e. partial first and/or last) on an individual basis
  1965. * (a single pass), allocating the bits of interest by hand and
  1966. * updating the leaf corresponding to the dmap word. a single pass
  1967. * will be used for all dmap words fully contained within the
  1968. * specified range. within this pass, the bits of all fully contained
  1969. * dmap words will be marked as free in a single shot and the leaves
  1970. * will be updated. a single leaf may describe the free space of
  1971. * multiple dmap words, so we may update only a subset of the actual
  1972. * leaves corresponding to the dmap words of the block range.
  1973. */
  1974. for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
  1975. /* determine the bit number within the word and
  1976. * the number of bits within the word.
  1977. */
  1978. wbitno = dbitno & (DBWORD - 1);
  1979. nb = min(rembits, DBWORD - wbitno);
  1980. /* check if only part of a word is to be allocated.
  1981. */
  1982. if (nb < DBWORD) {
  1983. /* allocate (set to 1) the appropriate bits within
  1984. * this dmap word.
  1985. */
  1986. dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
  1987. >> wbitno);
  1988. /* update the leaf for this dmap word. in addition
  1989. * to setting the leaf value to the binary buddy max
  1990. * of the updated dmap word, dbSplit() will split
  1991. * the binary system of the leaves if need be.
  1992. */
  1993. dbSplit(tp, word, BUDMIN,
  1994. dbMaxBud((u8 *) & dp->wmap[word]));
  1995. word += 1;
  1996. } else {
  1997. /* one or more dmap words are fully contained
  1998. * within the block range. determine how many
  1999. * words and allocate (set to 1) the bits of these
  2000. * words.
  2001. */
  2002. nwords = rembits >> L2DBWORD;
  2003. memset(&dp->wmap[word], (int) ONES, nwords * 4);
  2004. /* determine how many bits.
  2005. */
  2006. nb = nwords << L2DBWORD;
  2007. /* now update the appropriate leaves to reflect
  2008. * the allocated words.
  2009. */
  2010. for (; nwords > 0; nwords -= nw) {
  2011. if (leaf[word] < BUDMIN) {
  2012. jfs_error(bmp->db_ipbmap->i_sb,
  2013. "dbAllocBits: leaf page "
  2014. "corrupt");
  2015. break;
  2016. }
  2017. /* determine what the leaf value should be
  2018. * updated to as the minimum of the l2 number
  2019. * of bits being allocated and the l2 number
  2020. * of bits currently described by this leaf.
  2021. */
  2022. size = min((int)leaf[word], NLSTOL2BSZ(nwords));
  2023. /* update the leaf to reflect the allocation.
  2024. * in addition to setting the leaf value to
  2025. * NOFREE, dbSplit() will split the binary
  2026. * system of the leaves to reflect the current
  2027. * allocation (size).
  2028. */
  2029. dbSplit(tp, word, size, NOFREE);
  2030. /* get the number of dmap words handled */
  2031. nw = BUDSIZE(size, BUDMIN);
  2032. word += nw;
  2033. }
  2034. }
  2035. }
  2036. /* update the free count for this dmap */
  2037. le32_add_cpu(&dp->nfree, -nblocks);
  2038. BMAP_LOCK(bmp);
  2039. /* if this allocation group is completely free,
  2040. * update the maximum allocation group number if this allocation
  2041. * group is the new max.
  2042. */
  2043. agno = blkno >> bmp->db_agl2size;
  2044. if (agno > bmp->db_maxag)
  2045. bmp->db_maxag = agno;
  2046. /* update the free count for the allocation group and map */
  2047. bmp->db_agfree[agno] -= nblocks;
  2048. bmp->db_nfree -= nblocks;
  2049. BMAP_UNLOCK(bmp);
  2050. }
  2051. /*
  2052. * NAME: dbFreeBits()
  2053. *
  2054. * FUNCTION: free a specified block range from a dmap.
  2055. *
  2056. * this routine updates the dmap to reflect the working
  2057. * state allocation of the specified block range. it directly
  2058. * updates the bits of the working map and causes the adjustment
  2059. * of the binary buddy system described by the dmap's dmtree
  2060. * leaves to reflect the bits freed. it also causes the dmap's
  2061. * dmtree, as a whole, to reflect the deallocated range.
  2062. *
  2063. * PARAMETERS:
  2064. * bmp - pointer to bmap descriptor
  2065. * dp - pointer to dmap to free bits from.
  2066. * blkno - starting block number of the bits to be freed.
  2067. * nblocks - number of bits to be freed.
  2068. *
  2069. * RETURN VALUES: 0 for success
  2070. *
  2071. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  2072. */
  2073. static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
  2074. int nblocks)
  2075. {
  2076. int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
  2077. dmtree_t *tp = (dmtree_t *) & dp->tree;
  2078. int rc = 0;
  2079. int size;
  2080. /* determine the bit number and word within the dmap of the
  2081. * starting block.
  2082. */
  2083. dbitno = blkno & (BPERDMAP - 1);
  2084. word = dbitno >> L2DBWORD;
  2085. /* block range better be within the dmap.
  2086. */
  2087. assert(dbitno + nblocks <= BPERDMAP);
  2088. /* free the bits of the dmaps words corresponding to the block range.
  2089. * not all bits of the first and last words may be contained within
  2090. * the block range. if this is the case, we'll work against those
  2091. * words (i.e. partial first and/or last) on an individual basis
  2092. * (a single pass), freeing the bits of interest by hand and updating
  2093. * the leaf corresponding to the dmap word. a single pass will be used
  2094. * for all dmap words fully contained within the specified range.
  2095. * within this pass, the bits of all fully contained dmap words will
  2096. * be marked as free in a single shot and the leaves will be updated. a
  2097. * single leaf may describe the free space of multiple dmap words,
  2098. * so we may update only a subset of the actual leaves corresponding
  2099. * to the dmap words of the block range.
  2100. *
  2101. * dbJoin() is used to update leaf values and will join the binary
  2102. * buddy system of the leaves if the new leaf values indicate this
  2103. * should be done.
  2104. */
  2105. for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
  2106. /* determine the bit number within the word and
  2107. * the number of bits within the word.
  2108. */
  2109. wbitno = dbitno & (DBWORD - 1);
  2110. nb = min(rembits, DBWORD - wbitno);
  2111. /* check if only part of a word is to be freed.
  2112. */
  2113. if (nb < DBWORD) {
  2114. /* free (zero) the appropriate bits within this
  2115. * dmap word.
  2116. */
  2117. dp->wmap[word] &=
  2118. cpu_to_le32(~(ONES << (DBWORD - nb)
  2119. >> wbitno));
  2120. /* update the leaf for this dmap word.
  2121. */
  2122. rc = dbJoin(tp, word,
  2123. dbMaxBud((u8 *) & dp->wmap[word]));
  2124. if (rc)
  2125. return rc;
  2126. word += 1;
  2127. } else {
  2128. /* one or more dmap words are fully contained
  2129. * within the block range. determine how many
  2130. * words and free (zero) the bits of these words.
  2131. */
  2132. nwords = rembits >> L2DBWORD;
  2133. memset(&dp->wmap[word], 0, nwords * 4);
  2134. /* determine how many bits.
  2135. */
  2136. nb = nwords << L2DBWORD;
  2137. /* now update the appropriate leaves to reflect
  2138. * the freed words.
  2139. */
  2140. for (; nwords > 0; nwords -= nw) {
  2141. /* determine what the leaf value should be
  2142. * updated to as the minimum of the l2 number
  2143. * of bits being freed and the l2 (max) number
  2144. * of bits that can be described by this leaf.
  2145. */
  2146. size =
  2147. min(LITOL2BSZ
  2148. (word, L2LPERDMAP, BUDMIN),
  2149. NLSTOL2BSZ(nwords));
  2150. /* update the leaf.
  2151. */
  2152. rc = dbJoin(tp, word, size);
  2153. if (rc)
  2154. return rc;
  2155. /* get the number of dmap words handled.
  2156. */
  2157. nw = BUDSIZE(size, BUDMIN);
  2158. word += nw;
  2159. }
  2160. }
  2161. }
  2162. /* update the free count for this dmap.
  2163. */
  2164. le32_add_cpu(&dp->nfree, nblocks);
  2165. BMAP_LOCK(bmp);
  2166. /* update the free count for the allocation group and
  2167. * map.
  2168. */
  2169. agno = blkno >> bmp->db_agl2size;
  2170. bmp->db_nfree += nblocks;
  2171. bmp->db_agfree[agno] += nblocks;
  2172. /* check if this allocation group is not completely free and
  2173. * if it is currently the maximum (rightmost) allocation group.
  2174. * if so, establish the new maximum allocation group number by
  2175. * searching left for the first allocation group with allocation.
  2176. */
  2177. if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
  2178. (agno == bmp->db_numag - 1 &&
  2179. bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
  2180. while (bmp->db_maxag > 0) {
  2181. bmp->db_maxag -= 1;
  2182. if (bmp->db_agfree[bmp->db_maxag] !=
  2183. bmp->db_agsize)
  2184. break;
  2185. }
  2186. /* re-establish the allocation group preference if the
  2187. * current preference is right of the maximum allocation
  2188. * group.
  2189. */
  2190. if (bmp->db_agpref > bmp->db_maxag)
  2191. bmp->db_agpref = bmp->db_maxag;
  2192. }
  2193. BMAP_UNLOCK(bmp);
  2194. return 0;
  2195. }
  2196. /*
  2197. * NAME: dbAdjCtl()
  2198. *
  2199. * FUNCTION: adjust a dmap control page at a specified level to reflect
  2200. * the change in a lower level dmap or dmap control page's
  2201. * maximum string of free blocks (i.e. a change in the root
  2202. * of the lower level object's dmtree) due to the allocation
  2203. * or deallocation of a range of blocks with a single dmap.
  2204. *
  2205. * on entry, this routine is provided with the new value of
  2206. * the lower level dmap or dmap control page root and the
  2207. * starting block number of the block range whose allocation
  2208. * or deallocation resulted in the root change. this range
  2209. * is respresented by a single leaf of the current dmapctl
  2210. * and the leaf will be updated with this value, possibly
  2211. * causing a binary buddy system within the leaves to be
  2212. * split or joined. the update may also cause the dmapctl's
  2213. * dmtree to be updated.
  2214. *
  2215. * if the adjustment of the dmap control page, itself, causes its
  2216. * root to change, this change will be bubbled up to the next dmap
  2217. * control level by a recursive call to this routine, specifying
  2218. * the new root value and the next dmap control page level to
  2219. * be adjusted.
  2220. * PARAMETERS:
  2221. * bmp - pointer to bmap descriptor
  2222. * blkno - the first block of a block range within a dmap. it is
  2223. * the allocation or deallocation of this block range that
  2224. * requires the dmap control page to be adjusted.
  2225. * newval - the new value of the lower level dmap or dmap control
  2226. * page root.
  2227. * alloc - 'true' if adjustment is due to an allocation.
  2228. * level - current level of dmap control page (i.e. L0, L1, L2) to
  2229. * be adjusted.
  2230. *
  2231. * RETURN VALUES:
  2232. * 0 - success
  2233. * -EIO - i/o error
  2234. *
  2235. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  2236. */
  2237. static int
  2238. dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
  2239. {
  2240. struct metapage *mp;
  2241. s8 oldroot;
  2242. int oldval;
  2243. s64 lblkno;
  2244. struct dmapctl *dcp;
  2245. int rc, leafno, ti;
  2246. /* get the buffer for the dmap control page for the specified
  2247. * block number and control page level.
  2248. */
  2249. lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
  2250. mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
  2251. if (mp == NULL)
  2252. return -EIO;
  2253. dcp = (struct dmapctl *) mp->data;
  2254. if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
  2255. jfs_error(bmp->db_ipbmap->i_sb,
  2256. "dbAdjCtl: Corrupt dmapctl page");
  2257. release_metapage(mp);
  2258. return -EIO;
  2259. }
  2260. /* determine the leaf number corresponding to the block and
  2261. * the index within the dmap control tree.
  2262. */
  2263. leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
  2264. ti = leafno + le32_to_cpu(dcp->leafidx);
  2265. /* save the current leaf value and the current root level (i.e.
  2266. * maximum l2 free string described by this dmapctl).
  2267. */
  2268. oldval = dcp->stree[ti];
  2269. oldroot = dcp->stree[ROOT];
  2270. /* check if this is a control page update for an allocation.
  2271. * if so, update the leaf to reflect the new leaf value using
  2272. * dbSplit(); otherwise (deallocation), use dbJoin() to update
  2273. * the leaf with the new value. in addition to updating the
  2274. * leaf, dbSplit() will also split the binary buddy system of
  2275. * the leaves, if required, and bubble new values within the
  2276. * dmapctl tree, if required. similarly, dbJoin() will join
  2277. * the binary buddy system of leaves and bubble new values up
  2278. * the dmapctl tree as required by the new leaf value.
  2279. */
  2280. if (alloc) {
  2281. /* check if we are in the middle of a binary buddy
  2282. * system. this happens when we are performing the
  2283. * first allocation out of an allocation group that
  2284. * is part (not the first part) of a larger binary
  2285. * buddy system. if we are in the middle, back split
  2286. * the system prior to calling dbSplit() which assumes
  2287. * that it is at the front of a binary buddy system.
  2288. */
  2289. if (oldval == NOFREE) {
  2290. rc = dbBackSplit((dmtree_t *) dcp, leafno);
  2291. if (rc)
  2292. return rc;
  2293. oldval = dcp->stree[ti];
  2294. }
  2295. dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval);
  2296. } else {
  2297. rc = dbJoin((dmtree_t *) dcp, leafno, newval);
  2298. if (rc)
  2299. return rc;
  2300. }
  2301. /* check if the root of the current dmap control page changed due
  2302. * to the update and if the current dmap control page is not at
  2303. * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
  2304. * root changed and this is not the top level), call this routine
  2305. * again (recursion) for the next higher level of the mapping to
  2306. * reflect the change in root for the current dmap control page.
  2307. */
  2308. if (dcp->stree[ROOT] != oldroot) {
  2309. /* are we below the top level of the map. if so,
  2310. * bubble the root up to the next higher level.
  2311. */
  2312. if (level < bmp->db_maxlevel) {
  2313. /* bubble up the new root of this dmap control page to
  2314. * the next level.
  2315. */
  2316. if ((rc =
  2317. dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
  2318. level + 1))) {
  2319. /* something went wrong in bubbling up the new
  2320. * root value, so backout the changes to the
  2321. * current dmap control page.
  2322. */
  2323. if (alloc) {
  2324. dbJoin((dmtree_t *) dcp, leafno,
  2325. oldval);
  2326. } else {
  2327. /* the dbJoin() above might have
  2328. * caused a larger binary buddy system
  2329. * to form and we may now be in the
  2330. * middle of it. if this is the case,
  2331. * back split the buddies.
  2332. */
  2333. if (dcp->stree[ti] == NOFREE)
  2334. dbBackSplit((dmtree_t *)
  2335. dcp, leafno);
  2336. dbSplit((dmtree_t *) dcp, leafno,
  2337. dcp->budmin, oldval);
  2338. }
  2339. /* release the buffer and return the error.
  2340. */
  2341. release_metapage(mp);
  2342. return (rc);
  2343. }
  2344. } else {
  2345. /* we're at the top level of the map. update
  2346. * the bmap control page to reflect the size
  2347. * of the maximum free buddy system.
  2348. */
  2349. assert(level == bmp->db_maxlevel);
  2350. if (bmp->db_maxfreebud != oldroot) {
  2351. jfs_error(bmp->db_ipbmap->i_sb,
  2352. "dbAdjCtl: the maximum free buddy is "
  2353. "not the old root");
  2354. }
  2355. bmp->db_maxfreebud = dcp->stree[ROOT];
  2356. }
  2357. }
  2358. /* write the buffer.
  2359. */
  2360. write_metapage(mp);
  2361. return (0);
  2362. }
  2363. /*
  2364. * NAME: dbSplit()
  2365. *
  2366. * FUNCTION: update the leaf of a dmtree with a new value, splitting
  2367. * the leaf from the binary buddy system of the dmtree's
  2368. * leaves, as required.
  2369. *
  2370. * PARAMETERS:
  2371. * tp - pointer to the tree containing the leaf.
  2372. * leafno - the number of the leaf to be updated.
  2373. * splitsz - the size the binary buddy system starting at the leaf
  2374. * must be split to, specified as the log2 number of blocks.
  2375. * newval - the new value for the leaf.
  2376. *
  2377. * RETURN VALUES: none
  2378. *
  2379. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  2380. */
  2381. static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval)
  2382. {
  2383. int budsz;
  2384. int cursz;
  2385. s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
  2386. /* check if the leaf needs to be split.
  2387. */
  2388. if (leaf[leafno] > tp->dmt_budmin) {
  2389. /* the split occurs by cutting the buddy system in half
  2390. * at the specified leaf until we reach the specified
  2391. * size. pick up the starting split size (current size
  2392. * - 1 in l2) and the corresponding buddy size.
  2393. */
  2394. cursz = leaf[leafno] - 1;
  2395. budsz = BUDSIZE(cursz, tp->dmt_budmin);
  2396. /* split until we reach the specified size.
  2397. */
  2398. while (cursz >= splitsz) {
  2399. /* update the buddy's leaf with its new value.
  2400. */
  2401. dbAdjTree(tp, leafno ^ budsz, cursz);
  2402. /* on to the next size and buddy.
  2403. */
  2404. cursz -= 1;
  2405. budsz >>= 1;
  2406. }
  2407. }
  2408. /* adjust the dmap tree to reflect the specified leaf's new
  2409. * value.
  2410. */
  2411. dbAdjTree(tp, leafno, newval);
  2412. }
  2413. /*
  2414. * NAME: dbBackSplit()
  2415. *
  2416. * FUNCTION: back split the binary buddy system of dmtree leaves
  2417. * that hold a specified leaf until the specified leaf
  2418. * starts its own binary buddy system.
  2419. *
  2420. * the allocators typically perform allocations at the start
  2421. * of binary buddy systems and dbSplit() is used to accomplish
  2422. * any required splits. in some cases, however, allocation
  2423. * may occur in the middle of a binary system and requires a
  2424. * back split, with the split proceeding out from the middle of
  2425. * the system (less efficient) rather than the start of the
  2426. * system (more efficient). the cases in which a back split
  2427. * is required are rare and are limited to the first allocation
  2428. * within an allocation group which is a part (not first part)
  2429. * of a larger binary buddy system and a few exception cases
  2430. * in which a previous join operation must be backed out.
  2431. *
  2432. * PARAMETERS:
  2433. * tp - pointer to the tree containing the leaf.
  2434. * leafno - the number of the leaf to be updated.
  2435. *
  2436. * RETURN VALUES: none
  2437. *
  2438. * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
  2439. */
  2440. static int dbBackSplit(dmtree_t * tp, int leafno)
  2441. {
  2442. int budsz, bud, w, bsz, size;
  2443. int cursz;
  2444. s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
  2445. /* leaf should be part (not first part) of a binary
  2446. * buddy system.
  2447. */
  2448. assert(leaf[leafno] == NOFREE);
  2449. /* the back split is accomplished by iteratively finding the leaf
  2450. * that starts the buddy system that contains the specified leaf and
  2451. * splitting that system in two. this iteration continues until
  2452. * the specified leaf becomes the start of a buddy system.
  2453. *
  2454. * determine maximum possible l2 size for the specified leaf.
  2455. */
  2456. size =
  2457. LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
  2458. tp->dmt_budmin);
  2459. /* determine the number of leaves covered by this size. this
  2460. * is the buddy size that we will start with as we search for
  2461. * the buddy system that contains the specified leaf.
  2462. */
  2463. budsz = BUDSIZE(size, tp->dmt_budmin);
  2464. /* back split.
  2465. */
  2466. while (leaf[leafno] == NOFREE) {
  2467. /* find the leftmost buddy leaf.
  2468. */
  2469. for (w = leafno, bsz = budsz;; bsz <<= 1,
  2470. w = (w < bud) ? w : bud) {
  2471. if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
  2472. jfs_err("JFS: block map error in dbBackSplit");
  2473. return -EIO;
  2474. }
  2475. /* determine the buddy.
  2476. */
  2477. bud = w ^ bsz;
  2478. /* check if this buddy is the start of the system.
  2479. */
  2480. if (leaf[bud] != NOFREE) {
  2481. /* split the leaf at the start of the
  2482. * system in two.
  2483. */
  2484. cursz = leaf[bud] - 1;
  2485. dbSplit(tp, bud, cursz, cursz);
  2486. break;
  2487. }
  2488. }
  2489. }
  2490. if (leaf[leafno] != size) {
  2491. jfs_err("JFS: wrong leaf value in dbBackSplit");
  2492. return -EIO;
  2493. }
  2494. return 0;
  2495. }
  2496. /*
  2497. * NAME: dbJoin()
  2498. *
  2499. * FUNCTION: update the leaf of a dmtree with a new value, joining
  2500. * the leaf with other leaves of the dmtree into a multi-leaf
  2501. * binary buddy system, as required.
  2502. *
  2503. * PARAMETERS:
  2504. * tp - pointer to the tree containing the leaf.
  2505. * leafno - the number of the leaf to be updated.
  2506. * newval - the new value for the leaf.
  2507. *
  2508. * RETURN VALUES: none
  2509. */
  2510. static int dbJoin(dmtree_t * tp, int leafno, int newval)
  2511. {
  2512. int budsz, buddy;
  2513. s8 *leaf;
  2514. /* can the new leaf value require a join with other leaves ?
  2515. */
  2516. if (newval >= tp->dmt_budmin) {
  2517. /* pickup a pointer to the leaves of the tree.
  2518. */
  2519. leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
  2520. /* try to join the specified leaf into a large binary
  2521. * buddy system. the join proceeds by attempting to join
  2522. * the specified leafno with its buddy (leaf) at new value.
  2523. * if the join occurs, we attempt to join the left leaf
  2524. * of the joined buddies with its buddy at new value + 1.
  2525. * we continue to join until we find a buddy that cannot be
  2526. * joined (does not have a value equal to the size of the
  2527. * last join) or until all leaves have been joined into a
  2528. * single system.
  2529. *
  2530. * get the buddy size (number of words covered) of
  2531. * the new value.
  2532. */
  2533. budsz = BUDSIZE(newval, tp->dmt_budmin);
  2534. /* try to join.
  2535. */
  2536. while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
  2537. /* get the buddy leaf.
  2538. */
  2539. buddy = leafno ^ budsz;
  2540. /* if the leaf's new value is greater than its
  2541. * buddy's value, we join no more.
  2542. */
  2543. if (newval > leaf[buddy])
  2544. break;
  2545. /* It shouldn't be less */
  2546. if (newval < leaf[buddy])
  2547. return -EIO;
  2548. /* check which (leafno or buddy) is the left buddy.
  2549. * the left buddy gets to claim the blocks resulting
  2550. * from the join while the right gets to claim none.
  2551. * the left buddy is also eligible to participate in
  2552. * a join at the next higher level while the right
  2553. * is not.
  2554. *
  2555. */
  2556. if (leafno < buddy) {
  2557. /* leafno is the left buddy.
  2558. */
  2559. dbAdjTree(tp, buddy, NOFREE);
  2560. } else {
  2561. /* buddy is the left buddy and becomes
  2562. * leafno.
  2563. */
  2564. dbAdjTree(tp, leafno, NOFREE);
  2565. leafno = buddy;
  2566. }
  2567. /* on to try the next join.
  2568. */
  2569. newval += 1;
  2570. budsz <<= 1;
  2571. }
  2572. }
  2573. /* update the leaf value.
  2574. */
  2575. dbAdjTree(tp, leafno, newval);
  2576. return 0;
  2577. }
  2578. /*
  2579. * NAME: dbAdjTree()
  2580. *
  2581. * FUNCTION: update a leaf of a dmtree with a new value, adjusting
  2582. * the dmtree, as required, to reflect the new leaf value.
  2583. * the combination of any buddies must already be done before
  2584. * this is called.
  2585. *
  2586. * PARAMETERS:
  2587. * tp - pointer to the tree to be adjusted.
  2588. * leafno - the number of the leaf to be updated.
  2589. * newval - the new value for the leaf.
  2590. *
  2591. * RETURN VALUES: none
  2592. */
  2593. static void dbAdjTree(dmtree_t * tp, int leafno, int newval)
  2594. {
  2595. int lp, pp, k;
  2596. int max;
  2597. /* pick up the index of the leaf for this leafno.
  2598. */
  2599. lp = leafno + le32_to_cpu(tp->dmt_leafidx);
  2600. /* is the current value the same as the old value ? if so,
  2601. * there is nothing to do.
  2602. */
  2603. if (tp->dmt_stree[lp] == newval)
  2604. return;
  2605. /* set the new value.
  2606. */
  2607. tp->dmt_stree[lp] = newval;
  2608. /* bubble the new value up the tree as required.
  2609. */
  2610. for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
  2611. /* get the index of the first leaf of the 4 leaf
  2612. * group containing the specified leaf (leafno).
  2613. */
  2614. lp = ((lp - 1) & ~0x03) + 1;
  2615. /* get the index of the parent of this 4 leaf group.
  2616. */
  2617. pp = (lp - 1) >> 2;
  2618. /* determine the maximum of the 4 leaves.
  2619. */
  2620. max = TREEMAX(&tp->dmt_stree[lp]);
  2621. /* if the maximum of the 4 is the same as the
  2622. * parent's value, we're done.
  2623. */
  2624. if (tp->dmt_stree[pp] == max)
  2625. break;
  2626. /* parent gets new value.
  2627. */
  2628. tp->dmt_stree[pp] = max;
  2629. /* parent becomes leaf for next go-round.
  2630. */
  2631. lp = pp;
  2632. }
  2633. }
  2634. /*
  2635. * NAME: dbFindLeaf()
  2636. *
  2637. * FUNCTION: search a dmtree_t for sufficient free blocks, returning
  2638. * the index of a leaf describing the free blocks if
  2639. * sufficient free blocks are found.
  2640. *
  2641. * the search starts at the top of the dmtree_t tree and
  2642. * proceeds down the tree to the leftmost leaf with sufficient
  2643. * free space.
  2644. *
  2645. * PARAMETERS:
  2646. * tp - pointer to the tree to be searched.
  2647. * l2nb - log2 number of free blocks to search for.
  2648. * leafidx - return pointer to be set to the index of the leaf
  2649. * describing at least l2nb free blocks if sufficient
  2650. * free blocks are found.
  2651. *
  2652. * RETURN VALUES:
  2653. * 0 - success
  2654. * -ENOSPC - insufficient free blocks.
  2655. */
  2656. static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
  2657. {
  2658. int ti, n = 0, k, x = 0;
  2659. /* first check the root of the tree to see if there is
  2660. * sufficient free space.
  2661. */
  2662. if (l2nb > tp->dmt_stree[ROOT])
  2663. return -ENOSPC;
  2664. /* sufficient free space available. now search down the tree
  2665. * starting at the next level for the leftmost leaf that
  2666. * describes sufficient free space.
  2667. */
  2668. for (k = le32_to_cpu(tp->dmt_height), ti = 1;
  2669. k > 0; k--, ti = ((ti + n) << 2) + 1) {
  2670. /* search the four nodes at this level, starting from
  2671. * the left.
  2672. */
  2673. for (x = ti, n = 0; n < 4; n++) {
  2674. /* sufficient free space found. move to the next
  2675. * level (or quit if this is the last level).
  2676. */
  2677. if (l2nb <= tp->dmt_stree[x + n])
  2678. break;
  2679. }
  2680. /* better have found something since the higher
  2681. * levels of the tree said it was here.
  2682. */
  2683. assert(n < 4);
  2684. }
  2685. /* set the return to the leftmost leaf describing sufficient
  2686. * free space.
  2687. */
  2688. *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
  2689. return (0);
  2690. }
  2691. /*
  2692. * NAME: dbFindBits()
  2693. *
  2694. * FUNCTION: find a specified number of binary buddy free bits within a
  2695. * dmap bitmap word value.
  2696. *
  2697. * this routine searches the bitmap value for (1 << l2nb) free
  2698. * bits at (1 << l2nb) alignments within the value.
  2699. *
  2700. * PARAMETERS:
  2701. * word - dmap bitmap word value.
  2702. * l2nb - number of free bits specified as a log2 number.
  2703. *
  2704. * RETURN VALUES:
  2705. * starting bit number of free bits.
  2706. */
  2707. static int dbFindBits(u32 word, int l2nb)
  2708. {
  2709. int bitno, nb;
  2710. u32 mask;
  2711. /* get the number of bits.
  2712. */
  2713. nb = 1 << l2nb;
  2714. assert(nb <= DBWORD);
  2715. /* complement the word so we can use a mask (i.e. 0s represent
  2716. * free bits) and compute the mask.
  2717. */
  2718. word = ~word;
  2719. mask = ONES << (DBWORD - nb);
  2720. /* scan the word for nb free bits at nb alignments.
  2721. */
  2722. for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
  2723. if ((mask & word) == mask)
  2724. break;
  2725. }
  2726. ASSERT(bitno < 32);
  2727. /* return the bit number.
  2728. */
  2729. return (bitno);
  2730. }
  2731. /*
  2732. * NAME: dbMaxBud(u8 *cp)
  2733. *
  2734. * FUNCTION: determine the largest binary buddy string of free
  2735. * bits within 32-bits of the map.
  2736. *
  2737. * PARAMETERS:
  2738. * cp - pointer to the 32-bit value.
  2739. *
  2740. * RETURN VALUES:
  2741. * largest binary buddy of free bits within a dmap word.
  2742. */
  2743. static int dbMaxBud(u8 * cp)
  2744. {
  2745. signed char tmp1, tmp2;
  2746. /* check if the wmap word is all free. if so, the
  2747. * free buddy size is BUDMIN.
  2748. */
  2749. if (*((uint *) cp) == 0)
  2750. return (BUDMIN);
  2751. /* check if the wmap word is half free. if so, the
  2752. * free buddy size is BUDMIN-1.
  2753. */
  2754. if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
  2755. return (BUDMIN - 1);
  2756. /* not all free or half free. determine the free buddy
  2757. * size thru table lookup using quarters of the wmap word.
  2758. */
  2759. tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
  2760. tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
  2761. return (max(tmp1, tmp2));
  2762. }
  2763. /*
  2764. * NAME: cnttz(uint word)
  2765. *
  2766. * FUNCTION: determine the number of trailing zeros within a 32-bit
  2767. * value.
  2768. *
  2769. * PARAMETERS:
  2770. * value - 32-bit value to be examined.
  2771. *
  2772. * RETURN VALUES:
  2773. * count of trailing zeros
  2774. */
  2775. static int cnttz(u32 word)
  2776. {
  2777. int n;
  2778. for (n = 0; n < 32; n++, word >>= 1) {
  2779. if (word & 0x01)
  2780. break;
  2781. }
  2782. return (n);
  2783. }
  2784. /*
  2785. * NAME: cntlz(u32 value)
  2786. *
  2787. * FUNCTION: determine the number of leading zeros within a 32-bit
  2788. * value.
  2789. *
  2790. * PARAMETERS:
  2791. * value - 32-bit value to be examined.
  2792. *
  2793. * RETURN VALUES:
  2794. * count of leading zeros
  2795. */
  2796. static int cntlz(u32 value)
  2797. {
  2798. int n;
  2799. for (n = 0; n < 32; n++, value <<= 1) {
  2800. if (value & HIGHORDER)
  2801. break;
  2802. }
  2803. return (n);
  2804. }
  2805. /*
  2806. * NAME: blkstol2(s64 nb)
  2807. *
  2808. * FUNCTION: convert a block count to its log2 value. if the block
  2809. * count is not a l2 multiple, it is rounded up to the next
  2810. * larger l2 multiple.
  2811. *
  2812. * PARAMETERS:
  2813. * nb - number of blocks
  2814. *
  2815. * RETURN VALUES:
  2816. * log2 number of blocks
  2817. */
  2818. static int blkstol2(s64 nb)
  2819. {
  2820. int l2nb;
  2821. s64 mask; /* meant to be signed */
  2822. mask = (s64) 1 << (64 - 1);
  2823. /* count the leading bits.
  2824. */
  2825. for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
  2826. /* leading bit found.
  2827. */
  2828. if (nb & mask) {
  2829. /* determine the l2 value.
  2830. */
  2831. l2nb = (64 - 1) - l2nb;
  2832. /* check if we need to round up.
  2833. */
  2834. if (~mask & nb)
  2835. l2nb++;
  2836. return (l2nb);
  2837. }
  2838. }
  2839. assert(0);
  2840. return 0; /* fix compiler warning */
  2841. }
  2842. /*
  2843. * NAME: dbAllocBottomUp()
  2844. *
  2845. * FUNCTION: alloc the specified block range from the working block
  2846. * allocation map.
  2847. *
  2848. * the blocks will be alloc from the working map one dmap
  2849. * at a time.
  2850. *
  2851. * PARAMETERS:
  2852. * ip - pointer to in-core inode;
  2853. * blkno - starting block number to be freed.
  2854. * nblocks - number of blocks to be freed.
  2855. *
  2856. * RETURN VALUES:
  2857. * 0 - success
  2858. * -EIO - i/o error
  2859. */
  2860. int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
  2861. {
  2862. struct metapage *mp;
  2863. struct dmap *dp;
  2864. int nb, rc;
  2865. s64 lblkno, rem;
  2866. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  2867. struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
  2868. IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
  2869. /* block to be allocated better be within the mapsize. */
  2870. ASSERT(nblocks <= bmp->db_mapsize - blkno);
  2871. /*
  2872. * allocate the blocks a dmap at a time.
  2873. */
  2874. mp = NULL;
  2875. for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
  2876. /* release previous dmap if any */
  2877. if (mp) {
  2878. write_metapage(mp);
  2879. }
  2880. /* get the buffer for the current dmap. */
  2881. lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
  2882. mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
  2883. if (mp == NULL) {
  2884. IREAD_UNLOCK(ipbmap);
  2885. return -EIO;
  2886. }
  2887. dp = (struct dmap *) mp->data;
  2888. /* determine the number of blocks to be allocated from
  2889. * this dmap.
  2890. */
  2891. nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
  2892. /* allocate the blocks. */
  2893. if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
  2894. release_metapage(mp);
  2895. IREAD_UNLOCK(ipbmap);
  2896. return (rc);
  2897. }
  2898. }
  2899. /* write the last buffer. */
  2900. write_metapage(mp);
  2901. IREAD_UNLOCK(ipbmap);
  2902. return (0);
  2903. }
  2904. static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
  2905. int nblocks)
  2906. {
  2907. int rc;
  2908. int dbitno, word, rembits, nb, nwords, wbitno, agno;
  2909. s8 oldroot;
  2910. struct dmaptree *tp = (struct dmaptree *) & dp->tree;
  2911. /* save the current value of the root (i.e. maximum free string)
  2912. * of the dmap tree.
  2913. */
  2914. oldroot = tp->stree[ROOT];
  2915. /* determine the bit number and word within the dmap of the
  2916. * starting block.
  2917. */
  2918. dbitno = blkno & (BPERDMAP - 1);
  2919. word = dbitno >> L2DBWORD;
  2920. /* block range better be within the dmap */
  2921. assert(dbitno + nblocks <= BPERDMAP);
  2922. /* allocate the bits of the dmap's words corresponding to the block
  2923. * range. not all bits of the first and last words may be contained
  2924. * within the block range. if this is the case, we'll work against
  2925. * those words (i.e. partial first and/or last) on an individual basis
  2926. * (a single pass), allocating the bits of interest by hand and
  2927. * updating the leaf corresponding to the dmap word. a single pass
  2928. * will be used for all dmap words fully contained within the
  2929. * specified range. within this pass, the bits of all fully contained
  2930. * dmap words will be marked as free in a single shot and the leaves
  2931. * will be updated. a single leaf may describe the free space of
  2932. * multiple dmap words, so we may update only a subset of the actual
  2933. * leaves corresponding to the dmap words of the block range.
  2934. */
  2935. for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
  2936. /* determine the bit number within the word and
  2937. * the number of bits within the word.
  2938. */
  2939. wbitno = dbitno & (DBWORD - 1);
  2940. nb = min(rembits, DBWORD - wbitno);
  2941. /* check if only part of a word is to be allocated.
  2942. */
  2943. if (nb < DBWORD) {
  2944. /* allocate (set to 1) the appropriate bits within
  2945. * this dmap word.
  2946. */
  2947. dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
  2948. >> wbitno);
  2949. word++;
  2950. } else {
  2951. /* one or more dmap words are fully contained
  2952. * within the block range. determine how many
  2953. * words and allocate (set to 1) the bits of these
  2954. * words.
  2955. */
  2956. nwords = rembits >> L2DBWORD;
  2957. memset(&dp->wmap[word], (int) ONES, nwords * 4);
  2958. /* determine how many bits */
  2959. nb = nwords << L2DBWORD;
  2960. word += nwords;
  2961. }
  2962. }
  2963. /* update the free count for this dmap */
  2964. le32_add_cpu(&dp->nfree, -nblocks);
  2965. /* reconstruct summary tree */
  2966. dbInitDmapTree(dp);
  2967. BMAP_LOCK(bmp);
  2968. /* if this allocation group is completely free,
  2969. * update the highest active allocation group number
  2970. * if this allocation group is the new max.
  2971. */
  2972. agno = blkno >> bmp->db_agl2size;
  2973. if (agno > bmp->db_maxag)
  2974. bmp->db_maxag = agno;
  2975. /* update the free count for the allocation group and map */
  2976. bmp->db_agfree[agno] -= nblocks;
  2977. bmp->db_nfree -= nblocks;
  2978. BMAP_UNLOCK(bmp);
  2979. /* if the root has not changed, done. */
  2980. if (tp->stree[ROOT] == oldroot)
  2981. return (0);
  2982. /* root changed. bubble the change up to the dmap control pages.
  2983. * if the adjustment of the upper level control pages fails,
  2984. * backout the bit allocation (thus making everything consistent).
  2985. */
  2986. if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
  2987. dbFreeBits(bmp, dp, blkno, nblocks);
  2988. return (rc);
  2989. }
  2990. /*
  2991. * NAME: dbExtendFS()
  2992. *
  2993. * FUNCTION: extend bmap from blkno for nblocks;
  2994. * dbExtendFS() updates bmap ready for dbAllocBottomUp();
  2995. *
  2996. * L2
  2997. * |
  2998. * L1---------------------------------L1
  2999. * | |
  3000. * L0---------L0---------L0 L0---------L0---------L0
  3001. * | | | | | |
  3002. * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
  3003. * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
  3004. *
  3005. * <---old---><----------------------------extend----------------------->
  3006. */
  3007. int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks)
  3008. {
  3009. struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
  3010. int nbperpage = sbi->nbperpage;
  3011. int i, i0 = true, j, j0 = true, k, n;
  3012. s64 newsize;
  3013. s64 p;
  3014. struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
  3015. struct dmapctl *l2dcp, *l1dcp, *l0dcp;
  3016. struct dmap *dp;
  3017. s8 *l0leaf, *l1leaf, *l2leaf;
  3018. struct bmap *bmp = sbi->bmap;
  3019. int agno, l2agsize, oldl2agsize;
  3020. s64 ag_rem;
  3021. newsize = blkno + nblocks;
  3022. jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
  3023. (long long) blkno, (long long) nblocks, (long long) newsize);
  3024. /*
  3025. * initialize bmap control page.
  3026. *
  3027. * all the data in bmap control page should exclude
  3028. * the mkfs hidden dmap page.
  3029. */
  3030. /* update mapsize */
  3031. bmp->db_mapsize = newsize;
  3032. bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
  3033. /* compute new AG size */
  3034. l2agsize = dbGetL2AGSize(newsize);
  3035. oldl2agsize = bmp->db_agl2size;
  3036. bmp->db_agl2size = l2agsize;
  3037. bmp->db_agsize = 1 << l2agsize;
  3038. /* compute new number of AG */
  3039. agno = bmp->db_numag;
  3040. bmp->db_numag = newsize >> l2agsize;
  3041. bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
  3042. /*
  3043. * reconfigure db_agfree[]
  3044. * from old AG configuration to new AG configuration;
  3045. *
  3046. * coalesce contiguous k (newAGSize/oldAGSize) AGs;
  3047. * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
  3048. * note: new AG size = old AG size * (2**x).
  3049. */
  3050. if (l2agsize == oldl2agsize)
  3051. goto extend;
  3052. k = 1 << (l2agsize - oldl2agsize);
  3053. ag_rem = bmp->db_agfree[0]; /* save agfree[0] */
  3054. for (i = 0, n = 0; i < agno; n++) {
  3055. bmp->db_agfree[n] = 0; /* init collection point */
  3056. /* coalesce contiguous k AGs; */
  3057. for (j = 0; j < k && i < agno; j++, i++) {
  3058. /* merge AGi to AGn */
  3059. bmp->db_agfree[n] += bmp->db_agfree[i];
  3060. }
  3061. }
  3062. bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */
  3063. for (; n < MAXAG; n++)
  3064. bmp->db_agfree[n] = 0;
  3065. /*
  3066. * update highest active ag number
  3067. */
  3068. bmp->db_maxag = bmp->db_maxag / k;
  3069. /*
  3070. * extend bmap
  3071. *
  3072. * update bit maps and corresponding level control pages;
  3073. * global control page db_nfree, db_agfree[agno], db_maxfreebud;
  3074. */
  3075. extend:
  3076. /* get L2 page */
  3077. p = BMAPBLKNO + nbperpage; /* L2 page */
  3078. l2mp = read_metapage(ipbmap, p, PSIZE, 0);
  3079. if (!l2mp) {
  3080. jfs_error(ipbmap->i_sb, "dbExtendFS: L2 page could not be read");
  3081. return -EIO;
  3082. }
  3083. l2dcp = (struct dmapctl *) l2mp->data;
  3084. /* compute start L1 */
  3085. k = blkno >> L2MAXL1SIZE;
  3086. l2leaf = l2dcp->stree + CTLLEAFIND + k;
  3087. p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */
  3088. /*
  3089. * extend each L1 in L2
  3090. */
  3091. for (; k < LPERCTL; k++, p += nbperpage) {
  3092. /* get L1 page */
  3093. if (j0) {
  3094. /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
  3095. l1mp = read_metapage(ipbmap, p, PSIZE, 0);
  3096. if (l1mp == NULL)
  3097. goto errout;
  3098. l1dcp = (struct dmapctl *) l1mp->data;
  3099. /* compute start L0 */
  3100. j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
  3101. l1leaf = l1dcp->stree + CTLLEAFIND + j;
  3102. p = BLKTOL0(blkno, sbi->l2nbperpage);
  3103. j0 = false;
  3104. } else {
  3105. /* assign/init L1 page */
  3106. l1mp = get_metapage(ipbmap, p, PSIZE, 0);
  3107. if (l1mp == NULL)
  3108. goto errout;
  3109. l1dcp = (struct dmapctl *) l1mp->data;
  3110. /* compute start L0 */
  3111. j = 0;
  3112. l1leaf = l1dcp->stree + CTLLEAFIND;
  3113. p += nbperpage; /* 1st L0 of L1.k */
  3114. }
  3115. /*
  3116. * extend each L0 in L1
  3117. */
  3118. for (; j < LPERCTL; j++) {
  3119. /* get L0 page */
  3120. if (i0) {
  3121. /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
  3122. l0mp = read_metapage(ipbmap, p, PSIZE, 0);
  3123. if (l0mp == NULL)
  3124. goto errout;
  3125. l0dcp = (struct dmapctl *) l0mp->data;
  3126. /* compute start dmap */
  3127. i = (blkno & (MAXL0SIZE - 1)) >>
  3128. L2BPERDMAP;
  3129. l0leaf = l0dcp->stree + CTLLEAFIND + i;
  3130. p = BLKTODMAP(blkno,
  3131. sbi->l2nbperpage);
  3132. i0 = false;
  3133. } else {
  3134. /* assign/init L0 page */
  3135. l0mp = get_metapage(ipbmap, p, PSIZE, 0);
  3136. if (l0mp == NULL)
  3137. goto errout;
  3138. l0dcp = (struct dmapctl *) l0mp->data;
  3139. /* compute start dmap */
  3140. i = 0;
  3141. l0leaf = l0dcp->stree + CTLLEAFIND;
  3142. p += nbperpage; /* 1st dmap of L0.j */
  3143. }
  3144. /*
  3145. * extend each dmap in L0
  3146. */
  3147. for (; i < LPERCTL; i++) {
  3148. /*
  3149. * reconstruct the dmap page, and
  3150. * initialize corresponding parent L0 leaf
  3151. */
  3152. if ((n = blkno & (BPERDMAP - 1))) {
  3153. /* read in dmap page: */
  3154. mp = read_metapage(ipbmap, p,
  3155. PSIZE, 0);
  3156. if (mp == NULL)
  3157. goto errout;
  3158. n = min(nblocks, (s64)BPERDMAP - n);
  3159. } else {
  3160. /* assign/init dmap page */
  3161. mp = read_metapage(ipbmap, p,
  3162. PSIZE, 0);
  3163. if (mp == NULL)
  3164. goto errout;
  3165. n = min(nblocks, (s64)BPERDMAP);
  3166. }
  3167. dp = (struct dmap *) mp->data;
  3168. *l0leaf = dbInitDmap(dp, blkno, n);
  3169. bmp->db_nfree += n;
  3170. agno = le64_to_cpu(dp->start) >> l2agsize;
  3171. bmp->db_agfree[agno] += n;
  3172. write_metapage(mp);
  3173. l0leaf++;
  3174. p += nbperpage;
  3175. blkno += n;
  3176. nblocks -= n;
  3177. if (nblocks == 0)
  3178. break;
  3179. } /* for each dmap in a L0 */
  3180. /*
  3181. * build current L0 page from its leaves, and
  3182. * initialize corresponding parent L1 leaf
  3183. */
  3184. *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
  3185. write_metapage(l0mp);
  3186. l0mp = NULL;
  3187. if (nblocks)
  3188. l1leaf++; /* continue for next L0 */
  3189. else {
  3190. /* more than 1 L0 ? */
  3191. if (j > 0)
  3192. break; /* build L1 page */
  3193. else {
  3194. /* summarize in global bmap page */
  3195. bmp->db_maxfreebud = *l1leaf;
  3196. release_metapage(l1mp);
  3197. release_metapage(l2mp);
  3198. goto finalize;
  3199. }
  3200. }
  3201. } /* for each L0 in a L1 */
  3202. /*
  3203. * build current L1 page from its leaves, and
  3204. * initialize corresponding parent L2 leaf
  3205. */
  3206. *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
  3207. write_metapage(l1mp);
  3208. l1mp = NULL;
  3209. if (nblocks)
  3210. l2leaf++; /* continue for next L1 */
  3211. else {
  3212. /* more than 1 L1 ? */
  3213. if (k > 0)
  3214. break; /* build L2 page */
  3215. else {
  3216. /* summarize in global bmap page */
  3217. bmp->db_maxfreebud = *l2leaf;
  3218. release_metapage(l2mp);
  3219. goto finalize;
  3220. }
  3221. }
  3222. } /* for each L1 in a L2 */
  3223. jfs_error(ipbmap->i_sb,
  3224. "dbExtendFS: function has not returned as expected");
  3225. errout:
  3226. if (l0mp)
  3227. release_metapage(l0mp);
  3228. if (l1mp)
  3229. release_metapage(l1mp);
  3230. release_metapage(l2mp);
  3231. return -EIO;
  3232. /*
  3233. * finalize bmap control page
  3234. */
  3235. finalize:
  3236. return 0;
  3237. }
  3238. /*
  3239. * dbFinalizeBmap()
  3240. */
  3241. void dbFinalizeBmap(struct inode *ipbmap)
  3242. {
  3243. struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
  3244. int actags, inactags, l2nl;
  3245. s64 ag_rem, actfree, inactfree, avgfree;
  3246. int i, n;
  3247. /*
  3248. * finalize bmap control page
  3249. */
  3250. //finalize:
  3251. /*
  3252. * compute db_agpref: preferred ag to allocate from
  3253. * (the leftmost ag with average free space in it);
  3254. */
  3255. //agpref:
  3256. /* get the number of active ags and inacitve ags */
  3257. actags = bmp->db_maxag + 1;
  3258. inactags = bmp->db_numag - actags;
  3259. ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
  3260. /* determine how many blocks are in the inactive allocation
  3261. * groups. in doing this, we must account for the fact that
  3262. * the rightmost group might be a partial group (i.e. file
  3263. * system size is not a multiple of the group size).
  3264. */
  3265. inactfree = (inactags && ag_rem) ?
  3266. ((inactags - 1) << bmp->db_agl2size) + ag_rem
  3267. : inactags << bmp->db_agl2size;
  3268. /* determine how many free blocks are in the active
  3269. * allocation groups plus the average number of free blocks
  3270. * within the active ags.
  3271. */
  3272. actfree = bmp->db_nfree - inactfree;
  3273. avgfree = (u32) actfree / (u32) actags;
  3274. /* if the preferred allocation group has not average free space.
  3275. * re-establish the preferred group as the leftmost
  3276. * group with average free space.
  3277. */
  3278. if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
  3279. for (bmp->db_agpref = 0; bmp->db_agpref < actags;
  3280. bmp->db_agpref++) {
  3281. if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
  3282. break;
  3283. }
  3284. if (bmp->db_agpref >= bmp->db_numag) {
  3285. jfs_error(ipbmap->i_sb,
  3286. "cannot find ag with average freespace");
  3287. }
  3288. }
  3289. /*
  3290. * compute db_aglevel, db_agheight, db_width, db_agstart:
  3291. * an ag is covered in aglevel dmapctl summary tree,
  3292. * at agheight level height (from leaf) with agwidth number of nodes
  3293. * each, which starts at agstart index node of the smmary tree node
  3294. * array;
  3295. */
  3296. bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
  3297. l2nl =
  3298. bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
  3299. bmp->db_agheight = l2nl >> 1;
  3300. bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
  3301. for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
  3302. i--) {
  3303. bmp->db_agstart += n;
  3304. n <<= 2;
  3305. }
  3306. }
  3307. /*
  3308. * NAME: dbInitDmap()/ujfs_idmap_page()
  3309. *
  3310. * FUNCTION: initialize working/persistent bitmap of the dmap page
  3311. * for the specified number of blocks:
  3312. *
  3313. * at entry, the bitmaps had been initialized as free (ZEROS);
  3314. * The number of blocks will only account for the actually
  3315. * existing blocks. Blocks which don't actually exist in
  3316. * the aggregate will be marked as allocated (ONES);
  3317. *
  3318. * PARAMETERS:
  3319. * dp - pointer to page of map
  3320. * nblocks - number of blocks this page
  3321. *
  3322. * RETURNS: NONE
  3323. */
  3324. static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
  3325. {
  3326. int blkno, w, b, r, nw, nb, i;
  3327. /* starting block number within the dmap */
  3328. blkno = Blkno & (BPERDMAP - 1);
  3329. if (blkno == 0) {
  3330. dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
  3331. dp->start = cpu_to_le64(Blkno);
  3332. if (nblocks == BPERDMAP) {
  3333. memset(&dp->wmap[0], 0, LPERDMAP * 4);
  3334. memset(&dp->pmap[0], 0, LPERDMAP * 4);
  3335. goto initTree;
  3336. }
  3337. } else {
  3338. le32_add_cpu(&dp->nblocks, nblocks);
  3339. le32_add_cpu(&dp->nfree, nblocks);
  3340. }
  3341. /* word number containing start block number */
  3342. w = blkno >> L2DBWORD;
  3343. /*
  3344. * free the bits corresponding to the block range (ZEROS):
  3345. * note: not all bits of the first and last words may be contained
  3346. * within the block range.
  3347. */
  3348. for (r = nblocks; r > 0; r -= nb, blkno += nb) {
  3349. /* number of bits preceding range to be freed in the word */
  3350. b = blkno & (DBWORD - 1);
  3351. /* number of bits to free in the word */
  3352. nb = min(r, DBWORD - b);
  3353. /* is partial word to be freed ? */
  3354. if (nb < DBWORD) {
  3355. /* free (set to 0) from the bitmap word */
  3356. dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
  3357. >> b));
  3358. dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
  3359. >> b));
  3360. /* skip the word freed */
  3361. w++;
  3362. } else {
  3363. /* free (set to 0) contiguous bitmap words */
  3364. nw = r >> L2DBWORD;
  3365. memset(&dp->wmap[w], 0, nw * 4);
  3366. memset(&dp->pmap[w], 0, nw * 4);
  3367. /* skip the words freed */
  3368. nb = nw << L2DBWORD;
  3369. w += nw;
  3370. }
  3371. }
  3372. /*
  3373. * mark bits following the range to be freed (non-existing
  3374. * blocks) as allocated (ONES)
  3375. */
  3376. if (blkno == BPERDMAP)
  3377. goto initTree;
  3378. /* the first word beyond the end of existing blocks */
  3379. w = blkno >> L2DBWORD;
  3380. /* does nblocks fall on a 32-bit boundary ? */
  3381. b = blkno & (DBWORD - 1);
  3382. if (b) {
  3383. /* mark a partial word allocated */
  3384. dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
  3385. w++;
  3386. }
  3387. /* set the rest of the words in the page to allocated (ONES) */
  3388. for (i = w; i < LPERDMAP; i++)
  3389. dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
  3390. /*
  3391. * init tree
  3392. */
  3393. initTree:
  3394. return (dbInitDmapTree(dp));
  3395. }
  3396. /*
  3397. * NAME: dbInitDmapTree()/ujfs_complete_dmap()
  3398. *
  3399. * FUNCTION: initialize summary tree of the specified dmap:
  3400. *
  3401. * at entry, bitmap of the dmap has been initialized;
  3402. *
  3403. * PARAMETERS:
  3404. * dp - dmap to complete
  3405. * blkno - starting block number for this dmap
  3406. * treemax - will be filled in with max free for this dmap
  3407. *
  3408. * RETURNS: max free string at the root of the tree
  3409. */
  3410. static int dbInitDmapTree(struct dmap * dp)
  3411. {
  3412. struct dmaptree *tp;
  3413. s8 *cp;
  3414. int i;
  3415. /* init fixed info of tree */
  3416. tp = &dp->tree;
  3417. tp->nleafs = cpu_to_le32(LPERDMAP);
  3418. tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
  3419. tp->leafidx = cpu_to_le32(LEAFIND);
  3420. tp->height = cpu_to_le32(4);
  3421. tp->budmin = BUDMIN;
  3422. /* init each leaf from corresponding wmap word:
  3423. * note: leaf is set to NOFREE(-1) if all blocks of corresponding
  3424. * bitmap word are allocated.
  3425. */
  3426. cp = tp->stree + le32_to_cpu(tp->leafidx);
  3427. for (i = 0; i < LPERDMAP; i++)
  3428. *cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
  3429. /* build the dmap's binary buddy summary tree */
  3430. return (dbInitTree(tp));
  3431. }
  3432. /*
  3433. * NAME: dbInitTree()/ujfs_adjtree()
  3434. *
  3435. * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
  3436. *
  3437. * at entry, the leaves of the tree has been initialized
  3438. * from corresponding bitmap word or root of summary tree
  3439. * of the child control page;
  3440. * configure binary buddy system at the leaf level, then
  3441. * bubble up the values of the leaf nodes up the tree.
  3442. *
  3443. * PARAMETERS:
  3444. * cp - Pointer to the root of the tree
  3445. * l2leaves- Number of leaf nodes as a power of 2
  3446. * l2min - Number of blocks that can be covered by a leaf
  3447. * as a power of 2
  3448. *
  3449. * RETURNS: max free string at the root of the tree
  3450. */
  3451. static int dbInitTree(struct dmaptree * dtp)
  3452. {
  3453. int l2max, l2free, bsize, nextb, i;
  3454. int child, parent, nparent;
  3455. s8 *tp, *cp, *cp1;
  3456. tp = dtp->stree;
  3457. /* Determine the maximum free string possible for the leaves */
  3458. l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
  3459. /*
  3460. * configure the leaf levevl into binary buddy system
  3461. *
  3462. * Try to combine buddies starting with a buddy size of 1
  3463. * (i.e. two leaves). At a buddy size of 1 two buddy leaves
  3464. * can be combined if both buddies have a maximum free of l2min;
  3465. * the combination will result in the left-most buddy leaf having
  3466. * a maximum free of l2min+1.
  3467. * After processing all buddies for a given size, process buddies
  3468. * at the next higher buddy size (i.e. current size * 2) and
  3469. * the next maximum free (current free + 1).
  3470. * This continues until the maximum possible buddy combination
  3471. * yields maximum free.
  3472. */
  3473. for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
  3474. l2free++, bsize = nextb) {
  3475. /* get next buddy size == current buddy pair size */
  3476. nextb = bsize << 1;
  3477. /* scan each adjacent buddy pair at current buddy size */
  3478. for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
  3479. i < le32_to_cpu(dtp->nleafs);
  3480. i += nextb, cp += nextb) {
  3481. /* coalesce if both adjacent buddies are max free */
  3482. if (*cp == l2free && *(cp + bsize) == l2free) {
  3483. *cp = l2free + 1; /* left take right */
  3484. *(cp + bsize) = -1; /* right give left */
  3485. }
  3486. }
  3487. }
  3488. /*
  3489. * bubble summary information of leaves up the tree.
  3490. *
  3491. * Starting at the leaf node level, the four nodes described by
  3492. * the higher level parent node are compared for a maximum free and
  3493. * this maximum becomes the value of the parent node.
  3494. * when all lower level nodes are processed in this fashion then
  3495. * move up to the next level (parent becomes a lower level node) and
  3496. * continue the process for that level.
  3497. */
  3498. for (child = le32_to_cpu(dtp->leafidx),
  3499. nparent = le32_to_cpu(dtp->nleafs) >> 2;
  3500. nparent > 0; nparent >>= 2, child = parent) {
  3501. /* get index of 1st node of parent level */
  3502. parent = (child - 1) >> 2;
  3503. /* set the value of the parent node as the maximum
  3504. * of the four nodes of the current level.
  3505. */
  3506. for (i = 0, cp = tp + child, cp1 = tp + parent;
  3507. i < nparent; i++, cp += 4, cp1++)
  3508. *cp1 = TREEMAX(cp);
  3509. }
  3510. return (*tp);
  3511. }
  3512. /*
  3513. * dbInitDmapCtl()
  3514. *
  3515. * function: initialize dmapctl page
  3516. */
  3517. static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
  3518. { /* start leaf index not covered by range */
  3519. s8 *cp;
  3520. dcp->nleafs = cpu_to_le32(LPERCTL);
  3521. dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
  3522. dcp->leafidx = cpu_to_le32(CTLLEAFIND);
  3523. dcp->height = cpu_to_le32(5);
  3524. dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
  3525. /*
  3526. * initialize the leaves of current level that were not covered
  3527. * by the specified input block range (i.e. the leaves have no
  3528. * low level dmapctl or dmap).
  3529. */
  3530. cp = &dcp->stree[CTLLEAFIND + i];
  3531. for (; i < LPERCTL; i++)
  3532. *cp++ = NOFREE;
  3533. /* build the dmap's binary buddy summary tree */
  3534. return (dbInitTree((struct dmaptree *) dcp));
  3535. }
  3536. /*
  3537. * NAME: dbGetL2AGSize()/ujfs_getagl2size()
  3538. *
  3539. * FUNCTION: Determine log2(allocation group size) from aggregate size
  3540. *
  3541. * PARAMETERS:
  3542. * nblocks - Number of blocks in aggregate
  3543. *
  3544. * RETURNS: log2(allocation group size) in aggregate blocks
  3545. */
  3546. static int dbGetL2AGSize(s64 nblocks)
  3547. {
  3548. s64 sz;
  3549. s64 m;
  3550. int l2sz;
  3551. if (nblocks < BPERDMAP * MAXAG)
  3552. return (L2BPERDMAP);
  3553. /* round up aggregate size to power of 2 */
  3554. m = ((u64) 1 << (64 - 1));
  3555. for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
  3556. if (m & nblocks)
  3557. break;
  3558. }
  3559. sz = (s64) 1 << l2sz;
  3560. if (sz < nblocks)
  3561. l2sz += 1;
  3562. /* agsize = roundupSize/max_number_of_ag */
  3563. return (l2sz - L2MAXAG);
  3564. }
  3565. /*
  3566. * NAME: dbMapFileSizeToMapSize()
  3567. *
  3568. * FUNCTION: compute number of blocks the block allocation map file
  3569. * can cover from the map file size;
  3570. *
  3571. * RETURNS: Number of blocks which can be covered by this block map file;
  3572. */
  3573. /*
  3574. * maximum number of map pages at each level including control pages
  3575. */
  3576. #define MAXL0PAGES (1 + LPERCTL)
  3577. #define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
  3578. #define MAXL2PAGES (1 + LPERCTL * MAXL1PAGES)
  3579. /*
  3580. * convert number of map pages to the zero origin top dmapctl level
  3581. */
  3582. #define BMAPPGTOLEV(npages) \
  3583. (((npages) <= 3 + MAXL0PAGES) ? 0 : \
  3584. ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
  3585. s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
  3586. {
  3587. struct super_block *sb = ipbmap->i_sb;
  3588. s64 nblocks;
  3589. s64 npages, ndmaps;
  3590. int level, i;
  3591. int complete, factor;
  3592. nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
  3593. npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
  3594. level = BMAPPGTOLEV(npages);
  3595. /* At each level, accumulate the number of dmap pages covered by
  3596. * the number of full child levels below it;
  3597. * repeat for the last incomplete child level.
  3598. */
  3599. ndmaps = 0;
  3600. npages--; /* skip the first global control page */
  3601. /* skip higher level control pages above top level covered by map */
  3602. npages -= (2 - level);
  3603. npages--; /* skip top level's control page */
  3604. for (i = level; i >= 0; i--) {
  3605. factor =
  3606. (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
  3607. complete = (u32) npages / factor;
  3608. ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
  3609. ((i == 1) ? LPERCTL : 1));
  3610. /* pages in last/incomplete child */
  3611. npages = (u32) npages % factor;
  3612. /* skip incomplete child's level control page */
  3613. npages--;
  3614. }
  3615. /* convert the number of dmaps into the number of blocks
  3616. * which can be covered by the dmaps;
  3617. */
  3618. nblocks = ndmaps << L2BPERDMAP;
  3619. return (nblocks);
  3620. }