jfs_dmap.c 112 KB

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