jfs_dmap.c 117 KB

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