jfs_dmap.c 111 KB

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