jfs_dmap.c 111 KB

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