jfs_dmap.c 111 KB

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