jfs_dmap.c 110 KB

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