jfs_dmap.c 112 KB

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