jfs_dmap.c 111 KB

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