jfs_dmap.c 109 KB

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