jfs_dmap.c 110 KB

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