jfs_dmap.c 109 KB

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