jfs_dmap.c 109 KB

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