jfs_dmap.c 111 KB

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