jfs_dmap.c 111 KB

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