jfs_dmap.c 117 KB

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