mballoc.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  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 version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. */
  18. /*
  19. * mballoc.c contains the multiblocks allocation routines
  20. */
  21. #include <linux/time.h>
  22. #include <linux/fs.h>
  23. #include <linux/namei.h>
  24. #include <linux/ext4_jbd2.h>
  25. #include <linux/ext4_fs.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/module.h>
  29. #include <linux/swap.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/version.h>
  34. #include "group.h"
  35. /*
  36. * MUSTDO:
  37. * - test ext4_ext_search_left() and ext4_ext_search_right()
  38. * - search for metadata in few groups
  39. *
  40. * TODO v4:
  41. * - normalization should take into account whether file is still open
  42. * - discard preallocations if no free space left (policy?)
  43. * - don't normalize tails
  44. * - quota
  45. * - reservation for superuser
  46. *
  47. * TODO v3:
  48. * - bitmap read-ahead (proposed by Oleg Drokin aka green)
  49. * - track min/max extents in each group for better group selection
  50. * - mb_mark_used() may allocate chunk right after splitting buddy
  51. * - tree of groups sorted by number of free blocks
  52. * - error handling
  53. */
  54. /*
  55. * The allocation request involve request for multiple number of blocks
  56. * near to the goal(block) value specified.
  57. *
  58. * During initialization phase of the allocator we decide to use the group
  59. * preallocation or inode preallocation depending on the size file. The
  60. * size of the file could be the resulting file size we would have after
  61. * allocation or the current file size which ever is larger. If the size is
  62. * less that sbi->s_mb_stream_request we select the group
  63. * preallocation. The default value of s_mb_stream_request is 16
  64. * blocks. This can also be tuned via
  65. * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
  66. * of number of blocks.
  67. *
  68. * The main motivation for having small file use group preallocation is to
  69. * ensure that we have small file closer in the disk.
  70. *
  71. * First stage the allocator looks at the inode prealloc list
  72. * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
  73. * this particular inode. The inode prealloc space is represented as:
  74. *
  75. * pa_lstart -> the logical start block for this prealloc space
  76. * pa_pstart -> the physical start block for this prealloc space
  77. * pa_len -> lenght for this prealloc space
  78. * pa_free -> free space available in this prealloc space
  79. *
  80. * The inode preallocation space is used looking at the _logical_ start
  81. * block. If only the logical file block falls within the range of prealloc
  82. * space we will consume the particular prealloc space. This make sure that
  83. * that the we have contiguous physical blocks representing the file blocks
  84. *
  85. * The important thing to be noted in case of inode prealloc space is that
  86. * we don't modify the values associated to inode prealloc space except
  87. * pa_free.
  88. *
  89. * If we are not able to find blocks in the inode prealloc space and if we
  90. * have the group allocation flag set then we look at the locality group
  91. * prealloc space. These are per CPU prealloc list repreasented as
  92. *
  93. * ext4_sb_info.s_locality_groups[smp_processor_id()]
  94. *
  95. * The reason for having a per cpu locality group is to reduce the contention
  96. * between CPUs. It is possible to get scheduled at this point.
  97. *
  98. * The locality group prealloc space is used looking at whether we have
  99. * enough free space (pa_free) withing the prealloc space.
  100. *
  101. * If we can't allocate blocks via inode prealloc or/and locality group
  102. * prealloc then we look at the buddy cache. The buddy cache is represented
  103. * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
  104. * mapped to the buddy and bitmap information regarding different
  105. * groups. The buddy information is attached to buddy cache inode so that
  106. * we can access them through the page cache. The information regarding
  107. * each group is loaded via ext4_mb_load_buddy. The information involve
  108. * block bitmap and buddy information. The information are stored in the
  109. * inode as:
  110. *
  111. * { page }
  112. * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
  113. *
  114. *
  115. * one block each for bitmap and buddy information. So for each group we
  116. * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
  117. * blocksize) blocks. So it can have information regarding groups_per_page
  118. * which is blocks_per_page/2
  119. *
  120. * The buddy cache inode is not stored on disk. The inode is thrown
  121. * away when the filesystem is unmounted.
  122. *
  123. * We look for count number of blocks in the buddy cache. If we were able
  124. * to locate that many free blocks we return with additional information
  125. * regarding rest of the contiguous physical block available
  126. *
  127. * Before allocating blocks via buddy cache we normalize the request
  128. * blocks. This ensure we ask for more blocks that we needed. The extra
  129. * blocks that we get after allocation is added to the respective prealloc
  130. * list. In case of inode preallocation we follow a list of heuristics
  131. * based on file size. This can be found in ext4_mb_normalize_request. If
  132. * we are doing a group prealloc we try to normalize the request to
  133. * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
  134. * 512 blocks. This can be tuned via
  135. * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
  136. * terms of number of blocks. If we have mounted the file system with -O
  137. * stripe=<value> option the group prealloc request is normalized to the
  138. * stripe value (sbi->s_stripe)
  139. *
  140. * The regular allocator(using the buddy cache) support few tunables.
  141. *
  142. * /proc/fs/ext4/<partition>/min_to_scan
  143. * /proc/fs/ext4/<partition>/max_to_scan
  144. * /proc/fs/ext4/<partition>/order2_req
  145. *
  146. * The regular allocator use buddy scan only if the request len is power of
  147. * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
  148. * value of s_mb_order2_reqs can be tuned via
  149. * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
  150. * stripe size (sbi->s_stripe), we try to search for contigous block in
  151. * stripe size. This should result in better allocation on RAID setup. If
  152. * not we search in the specific group using bitmap for best extents. The
  153. * tunable min_to_scan and max_to_scan controll the behaviour here.
  154. * min_to_scan indicate how long the mballoc __must__ look for a best
  155. * extent and max_to_scanindicate how long the mballoc __can__ look for a
  156. * best extent in the found extents. Searching for the blocks starts with
  157. * the group specified as the goal value in allocation context via
  158. * ac_g_ex. Each group is first checked based on the criteria whether it
  159. * can used for allocation. ext4_mb_good_group explains how the groups are
  160. * checked.
  161. *
  162. * Both the prealloc space are getting populated as above. So for the first
  163. * request we will hit the buddy cache which will result in this prealloc
  164. * space getting filled. The prealloc space is then later used for the
  165. * subsequent request.
  166. */
  167. /*
  168. * mballoc operates on the following data:
  169. * - on-disk bitmap
  170. * - in-core buddy (actually includes buddy and bitmap)
  171. * - preallocation descriptors (PAs)
  172. *
  173. * there are two types of preallocations:
  174. * - inode
  175. * assiged to specific inode and can be used for this inode only.
  176. * it describes part of inode's space preallocated to specific
  177. * physical blocks. any block from that preallocated can be used
  178. * independent. the descriptor just tracks number of blocks left
  179. * unused. so, before taking some block from descriptor, one must
  180. * make sure corresponded logical block isn't allocated yet. this
  181. * also means that freeing any block within descriptor's range
  182. * must discard all preallocated blocks.
  183. * - locality group
  184. * assigned to specific locality group which does not translate to
  185. * permanent set of inodes: inode can join and leave group. space
  186. * from this type of preallocation can be used for any inode. thus
  187. * it's consumed from the beginning to the end.
  188. *
  189. * relation between them can be expressed as:
  190. * in-core buddy = on-disk bitmap + preallocation descriptors
  191. *
  192. * this mean blocks mballoc considers used are:
  193. * - allocated blocks (persistent)
  194. * - preallocated blocks (non-persistent)
  195. *
  196. * consistency in mballoc world means that at any time a block is either
  197. * free or used in ALL structures. notice: "any time" should not be read
  198. * literally -- time is discrete and delimited by locks.
  199. *
  200. * to keep it simple, we don't use block numbers, instead we count number of
  201. * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
  202. *
  203. * all operations can be expressed as:
  204. * - init buddy: buddy = on-disk + PAs
  205. * - new PA: buddy += N; PA = N
  206. * - use inode PA: on-disk += N; PA -= N
  207. * - discard inode PA buddy -= on-disk - PA; PA = 0
  208. * - use locality group PA on-disk += N; PA -= N
  209. * - discard locality group PA buddy -= PA; PA = 0
  210. * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
  211. * is used in real operation because we can't know actual used
  212. * bits from PA, only from on-disk bitmap
  213. *
  214. * if we follow this strict logic, then all operations above should be atomic.
  215. * given some of them can block, we'd have to use something like semaphores
  216. * killing performance on high-end SMP hardware. let's try to relax it using
  217. * the following knowledge:
  218. * 1) if buddy is referenced, it's already initialized
  219. * 2) while block is used in buddy and the buddy is referenced,
  220. * nobody can re-allocate that block
  221. * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
  222. * bit set and PA claims same block, it's OK. IOW, one can set bit in
  223. * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
  224. * block
  225. *
  226. * so, now we're building a concurrency table:
  227. * - init buddy vs.
  228. * - new PA
  229. * blocks for PA are allocated in the buddy, buddy must be referenced
  230. * until PA is linked to allocation group to avoid concurrent buddy init
  231. * - use inode PA
  232. * we need to make sure that either on-disk bitmap or PA has uptodate data
  233. * given (3) we care that PA-=N operation doesn't interfere with init
  234. * - discard inode PA
  235. * the simplest way would be to have buddy initialized by the discard
  236. * - use locality group PA
  237. * again PA-=N must be serialized with init
  238. * - discard locality group PA
  239. * the simplest way would be to have buddy initialized by the discard
  240. * - new PA vs.
  241. * - use inode PA
  242. * i_data_sem serializes them
  243. * - discard inode PA
  244. * discard process must wait until PA isn't used by another process
  245. * - use locality group PA
  246. * some mutex should serialize them
  247. * - discard locality group PA
  248. * discard process must wait until PA isn't used by another process
  249. * - use inode PA
  250. * - use inode PA
  251. * i_data_sem or another mutex should serializes them
  252. * - discard inode PA
  253. * discard process must wait until PA isn't used by another process
  254. * - use locality group PA
  255. * nothing wrong here -- they're different PAs covering different blocks
  256. * - discard locality group PA
  257. * discard process must wait until PA isn't used by another process
  258. *
  259. * now we're ready to make few consequences:
  260. * - PA is referenced and while it is no discard is possible
  261. * - PA is referenced until block isn't marked in on-disk bitmap
  262. * - PA changes only after on-disk bitmap
  263. * - discard must not compete with init. either init is done before
  264. * any discard or they're serialized somehow
  265. * - buddy init as sum of on-disk bitmap and PAs is done atomically
  266. *
  267. * a special case when we've used PA to emptiness. no need to modify buddy
  268. * in this case, but we should care about concurrent init
  269. *
  270. */
  271. /*
  272. * Logic in few words:
  273. *
  274. * - allocation:
  275. * load group
  276. * find blocks
  277. * mark bits in on-disk bitmap
  278. * release group
  279. *
  280. * - use preallocation:
  281. * find proper PA (per-inode or group)
  282. * load group
  283. * mark bits in on-disk bitmap
  284. * release group
  285. * release PA
  286. *
  287. * - free:
  288. * load group
  289. * mark bits in on-disk bitmap
  290. * release group
  291. *
  292. * - discard preallocations in group:
  293. * mark PAs deleted
  294. * move them onto local list
  295. * load on-disk bitmap
  296. * load group
  297. * remove PA from object (inode or locality group)
  298. * mark free blocks in-core
  299. *
  300. * - discard inode's preallocations:
  301. */
  302. /*
  303. * Locking rules
  304. *
  305. * Locks:
  306. * - bitlock on a group (group)
  307. * - object (inode/locality) (object)
  308. * - per-pa lock (pa)
  309. *
  310. * Paths:
  311. * - new pa
  312. * object
  313. * group
  314. *
  315. * - find and use pa:
  316. * pa
  317. *
  318. * - release consumed pa:
  319. * pa
  320. * group
  321. * object
  322. *
  323. * - generate in-core bitmap:
  324. * group
  325. * pa
  326. *
  327. * - discard all for given object (inode, locality group):
  328. * object
  329. * pa
  330. * group
  331. *
  332. * - discard all for given group:
  333. * group
  334. * pa
  335. * group
  336. * object
  337. *
  338. */
  339. /*
  340. * with AGGRESSIVE_CHECK allocator runs consistency checks over
  341. * structures. these checks slow things down a lot
  342. */
  343. #define AGGRESSIVE_CHECK__
  344. /*
  345. * with DOUBLE_CHECK defined mballoc creates persistent in-core
  346. * bitmaps, maintains and uses them to check for double allocations
  347. */
  348. #define DOUBLE_CHECK__
  349. /*
  350. */
  351. #define MB_DEBUG__
  352. #ifdef MB_DEBUG
  353. #define mb_debug(fmt, a...) printk(fmt, ##a)
  354. #else
  355. #define mb_debug(fmt, a...)
  356. #endif
  357. /*
  358. * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
  359. * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
  360. */
  361. #define EXT4_MB_HISTORY
  362. #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
  363. #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
  364. #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
  365. #define EXT4_MB_HISTORY_FREE 8 /* free */
  366. #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
  367. EXT4_MB_HISTORY_PREALLOC)
  368. /*
  369. * How long mballoc can look for a best extent (in found extents)
  370. */
  371. #define MB_DEFAULT_MAX_TO_SCAN 200
  372. /*
  373. * How long mballoc must look for a best extent
  374. */
  375. #define MB_DEFAULT_MIN_TO_SCAN 10
  376. /*
  377. * How many groups mballoc will scan looking for the best chunk
  378. */
  379. #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
  380. /*
  381. * with 'ext4_mb_stats' allocator will collect stats that will be
  382. * shown at umount. The collecting costs though!
  383. */
  384. #define MB_DEFAULT_STATS 1
  385. /*
  386. * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
  387. * by the stream allocator, which purpose is to pack requests
  388. * as close each to other as possible to produce smooth I/O traffic
  389. * We use locality group prealloc space for stream request.
  390. * We can tune the same via /proc/fs/ext4/<parition>/stream_req
  391. */
  392. #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
  393. /*
  394. * for which requests use 2^N search using buddies
  395. */
  396. #define MB_DEFAULT_ORDER2_REQS 2
  397. /*
  398. * default group prealloc size 512 blocks
  399. */
  400. #define MB_DEFAULT_GROUP_PREALLOC 512
  401. static struct kmem_cache *ext4_pspace_cachep;
  402. static struct kmem_cache *ext4_ac_cachep;
  403. #ifdef EXT4_BB_MAX_BLOCKS
  404. #undef EXT4_BB_MAX_BLOCKS
  405. #endif
  406. #define EXT4_BB_MAX_BLOCKS 30
  407. struct ext4_free_metadata {
  408. ext4_group_t group;
  409. unsigned short num;
  410. ext4_grpblk_t blocks[EXT4_BB_MAX_BLOCKS];
  411. struct list_head list;
  412. };
  413. struct ext4_group_info {
  414. unsigned long bb_state;
  415. unsigned long bb_tid;
  416. struct ext4_free_metadata *bb_md_cur;
  417. unsigned short bb_first_free;
  418. unsigned short bb_free;
  419. unsigned short bb_fragments;
  420. struct list_head bb_prealloc_list;
  421. #ifdef DOUBLE_CHECK
  422. void *bb_bitmap;
  423. #endif
  424. unsigned short bb_counters[];
  425. };
  426. #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
  427. #define EXT4_GROUP_INFO_LOCKED_BIT 1
  428. #define EXT4_MB_GRP_NEED_INIT(grp) \
  429. (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
  430. struct ext4_prealloc_space {
  431. struct list_head pa_inode_list;
  432. struct list_head pa_group_list;
  433. union {
  434. struct list_head pa_tmp_list;
  435. struct rcu_head pa_rcu;
  436. } u;
  437. spinlock_t pa_lock;
  438. atomic_t pa_count;
  439. unsigned pa_deleted;
  440. ext4_fsblk_t pa_pstart; /* phys. block */
  441. ext4_lblk_t pa_lstart; /* log. block */
  442. unsigned short pa_len; /* len of preallocated chunk */
  443. unsigned short pa_free; /* how many blocks are free */
  444. unsigned short pa_linear; /* consumed in one direction
  445. * strictly, for grp prealloc */
  446. spinlock_t *pa_obj_lock;
  447. struct inode *pa_inode; /* hack, for history only */
  448. };
  449. struct ext4_free_extent {
  450. ext4_lblk_t fe_logical;
  451. ext4_grpblk_t fe_start;
  452. ext4_group_t fe_group;
  453. int fe_len;
  454. };
  455. /*
  456. * Locality group:
  457. * we try to group all related changes together
  458. * so that writeback can flush/allocate them together as well
  459. */
  460. struct ext4_locality_group {
  461. /* for allocator */
  462. struct mutex lg_mutex; /* to serialize allocates */
  463. struct list_head lg_prealloc_list;/* list of preallocations */
  464. spinlock_t lg_prealloc_lock;
  465. };
  466. struct ext4_allocation_context {
  467. struct inode *ac_inode;
  468. struct super_block *ac_sb;
  469. /* original request */
  470. struct ext4_free_extent ac_o_ex;
  471. /* goal request (after normalization) */
  472. struct ext4_free_extent ac_g_ex;
  473. /* the best found extent */
  474. struct ext4_free_extent ac_b_ex;
  475. /* copy of the bext found extent taken before preallocation efforts */
  476. struct ext4_free_extent ac_f_ex;
  477. /* number of iterations done. we have to track to limit searching */
  478. unsigned long ac_ex_scanned;
  479. __u16 ac_groups_scanned;
  480. __u16 ac_found;
  481. __u16 ac_tail;
  482. __u16 ac_buddy;
  483. __u16 ac_flags; /* allocation hints */
  484. __u8 ac_status;
  485. __u8 ac_criteria;
  486. __u8 ac_repeats;
  487. __u8 ac_2order; /* if request is to allocate 2^N blocks and
  488. * N > 0, the field stores N, otherwise 0 */
  489. __u8 ac_op; /* operation, for history only */
  490. struct page *ac_bitmap_page;
  491. struct page *ac_buddy_page;
  492. struct ext4_prealloc_space *ac_pa;
  493. struct ext4_locality_group *ac_lg;
  494. };
  495. #define AC_STATUS_CONTINUE 1
  496. #define AC_STATUS_FOUND 2
  497. #define AC_STATUS_BREAK 3
  498. struct ext4_mb_history {
  499. struct ext4_free_extent orig; /* orig allocation */
  500. struct ext4_free_extent goal; /* goal allocation */
  501. struct ext4_free_extent result; /* result allocation */
  502. unsigned pid;
  503. unsigned ino;
  504. __u16 found; /* how many extents have been found */
  505. __u16 groups; /* how many groups have been scanned */
  506. __u16 tail; /* what tail broke some buddy */
  507. __u16 buddy; /* buddy the tail ^^^ broke */
  508. __u16 flags;
  509. __u8 cr:3; /* which phase the result extent was found at */
  510. __u8 op:4;
  511. __u8 merged:1;
  512. };
  513. struct ext4_buddy {
  514. struct page *bd_buddy_page;
  515. void *bd_buddy;
  516. struct page *bd_bitmap_page;
  517. void *bd_bitmap;
  518. struct ext4_group_info *bd_info;
  519. struct super_block *bd_sb;
  520. __u16 bd_blkbits;
  521. ext4_group_t bd_group;
  522. };
  523. #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
  524. #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
  525. #ifndef EXT4_MB_HISTORY
  526. static inline void ext4_mb_store_history(struct ext4_allocation_context *ac)
  527. {
  528. return;
  529. }
  530. #else
  531. static void ext4_mb_store_history(struct ext4_allocation_context *ac);
  532. #endif
  533. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  534. static struct proc_dir_entry *proc_root_ext4;
  535. struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
  536. ext4_fsblk_t ext4_new_blocks_old(handle_t *handle, struct inode *inode,
  537. ext4_fsblk_t goal, unsigned long *count, int *errp);
  538. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  539. ext4_group_t group);
  540. static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
  541. static void ext4_mb_free_committed_blocks(struct super_block *);
  542. static void ext4_mb_return_to_preallocation(struct inode *inode,
  543. struct ext4_buddy *e4b, sector_t block,
  544. int count);
  545. static void ext4_mb_put_pa(struct ext4_allocation_context *,
  546. struct super_block *, struct ext4_prealloc_space *pa);
  547. static int ext4_mb_init_per_dev_proc(struct super_block *sb);
  548. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
  549. static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
  550. {
  551. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  552. bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  553. }
  554. static inline void ext4_unlock_group(struct super_block *sb,
  555. ext4_group_t group)
  556. {
  557. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  558. bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT, &(grinfo->bb_state));
  559. }
  560. static inline int ext4_is_group_locked(struct super_block *sb,
  561. ext4_group_t group)
  562. {
  563. struct ext4_group_info *grinfo = ext4_get_group_info(sb, group);
  564. return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT,
  565. &(grinfo->bb_state));
  566. }
  567. static ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
  568. struct ext4_free_extent *fex)
  569. {
  570. ext4_fsblk_t block;
  571. block = (ext4_fsblk_t) fex->fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  572. + fex->fe_start
  573. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  574. return block;
  575. }
  576. #if BITS_PER_LONG == 64
  577. #define mb_correct_addr_and_bit(bit, addr) \
  578. { \
  579. bit += ((unsigned long) addr & 7UL) << 3; \
  580. addr = (void *) ((unsigned long) addr & ~7UL); \
  581. }
  582. #elif BITS_PER_LONG == 32
  583. #define mb_correct_addr_and_bit(bit, addr) \
  584. { \
  585. bit += ((unsigned long) addr & 3UL) << 3; \
  586. addr = (void *) ((unsigned long) addr & ~3UL); \
  587. }
  588. #else
  589. #error "how many bits you are?!"
  590. #endif
  591. static inline int mb_test_bit(int bit, void *addr)
  592. {
  593. /*
  594. * ext4_test_bit on architecture like powerpc
  595. * needs unsigned long aligned address
  596. */
  597. mb_correct_addr_and_bit(bit, addr);
  598. return ext4_test_bit(bit, addr);
  599. }
  600. static inline void mb_set_bit(int bit, void *addr)
  601. {
  602. mb_correct_addr_and_bit(bit, addr);
  603. ext4_set_bit(bit, addr);
  604. }
  605. static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
  606. {
  607. mb_correct_addr_and_bit(bit, addr);
  608. ext4_set_bit_atomic(lock, bit, addr);
  609. }
  610. static inline void mb_clear_bit(int bit, void *addr)
  611. {
  612. mb_correct_addr_and_bit(bit, addr);
  613. ext4_clear_bit(bit, addr);
  614. }
  615. static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
  616. {
  617. mb_correct_addr_and_bit(bit, addr);
  618. ext4_clear_bit_atomic(lock, bit, addr);
  619. }
  620. static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
  621. {
  622. char *bb;
  623. /* FIXME!! is this needed */
  624. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  625. BUG_ON(max == NULL);
  626. if (order > e4b->bd_blkbits + 1) {
  627. *max = 0;
  628. return NULL;
  629. }
  630. /* at order 0 we see each particular block */
  631. *max = 1 << (e4b->bd_blkbits + 3);
  632. if (order == 0)
  633. return EXT4_MB_BITMAP(e4b);
  634. bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
  635. *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
  636. return bb;
  637. }
  638. #ifdef DOUBLE_CHECK
  639. static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
  640. int first, int count)
  641. {
  642. int i;
  643. struct super_block *sb = e4b->bd_sb;
  644. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  645. return;
  646. BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
  647. for (i = 0; i < count; i++) {
  648. if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
  649. ext4_fsblk_t blocknr;
  650. blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
  651. blocknr += first + i;
  652. blocknr +=
  653. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  654. ext4_error(sb, __FUNCTION__, "double-free of inode"
  655. " %lu's block %llu(bit %u in group %lu)\n",
  656. inode ? inode->i_ino : 0, blocknr,
  657. first + i, e4b->bd_group);
  658. }
  659. mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
  660. }
  661. }
  662. static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
  663. {
  664. int i;
  665. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  666. return;
  667. BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
  668. for (i = 0; i < count; i++) {
  669. BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
  670. mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
  671. }
  672. }
  673. static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  674. {
  675. if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
  676. unsigned char *b1, *b2;
  677. int i;
  678. b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
  679. b2 = (unsigned char *) bitmap;
  680. for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
  681. if (b1[i] != b2[i]) {
  682. printk("corruption in group %lu at byte %u(%u):"
  683. " %x in copy != %x on disk/prealloc\n",
  684. e4b->bd_group, i, i * 8, b1[i], b2[i]);
  685. BUG();
  686. }
  687. }
  688. }
  689. }
  690. #else
  691. static inline void mb_free_blocks_double(struct inode *inode,
  692. struct ext4_buddy *e4b, int first, int count)
  693. {
  694. return;
  695. }
  696. static inline void mb_mark_used_double(struct ext4_buddy *e4b,
  697. int first, int count)
  698. {
  699. return;
  700. }
  701. static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  702. {
  703. return;
  704. }
  705. #endif
  706. #ifdef AGGRESSIVE_CHECK
  707. #define MB_CHECK_ASSERT(assert) \
  708. do { \
  709. if (!(assert)) { \
  710. printk(KERN_EMERG \
  711. "Assertion failure in %s() at %s:%d: \"%s\"\n", \
  712. function, file, line, # assert); \
  713. BUG(); \
  714. } \
  715. } while (0)
  716. static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
  717. const char *function, int line)
  718. {
  719. struct super_block *sb = e4b->bd_sb;
  720. int order = e4b->bd_blkbits + 1;
  721. int max;
  722. int max2;
  723. int i;
  724. int j;
  725. int k;
  726. int count;
  727. struct ext4_group_info *grp;
  728. int fragments = 0;
  729. int fstart;
  730. struct list_head *cur;
  731. void *buddy;
  732. void *buddy2;
  733. if (!test_opt(sb, MBALLOC))
  734. return 0;
  735. {
  736. static int mb_check_counter;
  737. if (mb_check_counter++ % 100 != 0)
  738. return 0;
  739. }
  740. while (order > 1) {
  741. buddy = mb_find_buddy(e4b, order, &max);
  742. MB_CHECK_ASSERT(buddy);
  743. buddy2 = mb_find_buddy(e4b, order - 1, &max2);
  744. MB_CHECK_ASSERT(buddy2);
  745. MB_CHECK_ASSERT(buddy != buddy2);
  746. MB_CHECK_ASSERT(max * 2 == max2);
  747. count = 0;
  748. for (i = 0; i < max; i++) {
  749. if (mb_test_bit(i, buddy)) {
  750. /* only single bit in buddy2 may be 1 */
  751. if (!mb_test_bit(i << 1, buddy2)) {
  752. MB_CHECK_ASSERT(
  753. mb_test_bit((i<<1)+1, buddy2));
  754. } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
  755. MB_CHECK_ASSERT(
  756. mb_test_bit(i << 1, buddy2));
  757. }
  758. continue;
  759. }
  760. /* both bits in buddy2 must be 0 */
  761. MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
  762. MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
  763. for (j = 0; j < (1 << order); j++) {
  764. k = (i * (1 << order)) + j;
  765. MB_CHECK_ASSERT(
  766. !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
  767. }
  768. count++;
  769. }
  770. MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
  771. order--;
  772. }
  773. fstart = -1;
  774. buddy = mb_find_buddy(e4b, 0, &max);
  775. for (i = 0; i < max; i++) {
  776. if (!mb_test_bit(i, buddy)) {
  777. MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
  778. if (fstart == -1) {
  779. fragments++;
  780. fstart = i;
  781. }
  782. continue;
  783. }
  784. fstart = -1;
  785. /* check used bits only */
  786. for (j = 0; j < e4b->bd_blkbits + 1; j++) {
  787. buddy2 = mb_find_buddy(e4b, j, &max2);
  788. k = i >> j;
  789. MB_CHECK_ASSERT(k < max2);
  790. MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
  791. }
  792. }
  793. MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
  794. MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
  795. grp = ext4_get_group_info(sb, e4b->bd_group);
  796. buddy = mb_find_buddy(e4b, 0, &max);
  797. list_for_each(cur, &grp->bb_prealloc_list) {
  798. ext4_group_t groupnr;
  799. struct ext4_prealloc_space *pa;
  800. pa = list_entry(cur, struct ext4_prealloc_space, group_list);
  801. ext4_get_group_no_and_offset(sb, pa->pstart, &groupnr, &k);
  802. MB_CHECK_ASSERT(groupnr == e4b->bd_group);
  803. for (i = 0; i < pa->len; i++)
  804. MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
  805. }
  806. return 0;
  807. }
  808. #undef MB_CHECK_ASSERT
  809. #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
  810. __FILE__, __FUNCTION__, __LINE__)
  811. #else
  812. #define mb_check_buddy(e4b)
  813. #endif
  814. /* FIXME!! need more doc */
  815. static void ext4_mb_mark_free_simple(struct super_block *sb,
  816. void *buddy, unsigned first, int len,
  817. struct ext4_group_info *grp)
  818. {
  819. struct ext4_sb_info *sbi = EXT4_SB(sb);
  820. unsigned short min;
  821. unsigned short max;
  822. unsigned short chunk;
  823. unsigned short border;
  824. BUG_ON(len >= EXT4_BLOCKS_PER_GROUP(sb));
  825. border = 2 << sb->s_blocksize_bits;
  826. while (len > 0) {
  827. /* find how many blocks can be covered since this position */
  828. max = ffs(first | border) - 1;
  829. /* find how many blocks of power 2 we need to mark */
  830. min = fls(len) - 1;
  831. if (max < min)
  832. min = max;
  833. chunk = 1 << min;
  834. /* mark multiblock chunks only */
  835. grp->bb_counters[min]++;
  836. if (min > 0)
  837. mb_clear_bit(first >> min,
  838. buddy + sbi->s_mb_offsets[min]);
  839. len -= chunk;
  840. first += chunk;
  841. }
  842. }
  843. static void ext4_mb_generate_buddy(struct super_block *sb,
  844. void *buddy, void *bitmap, ext4_group_t group)
  845. {
  846. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  847. unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
  848. unsigned short i = 0;
  849. unsigned short first;
  850. unsigned short len;
  851. unsigned free = 0;
  852. unsigned fragments = 0;
  853. unsigned long long period = get_cycles();
  854. /* initialize buddy from bitmap which is aggregation
  855. * of on-disk bitmap and preallocations */
  856. i = ext4_find_next_zero_bit(bitmap, max, 0);
  857. grp->bb_first_free = i;
  858. while (i < max) {
  859. fragments++;
  860. first = i;
  861. i = ext4_find_next_bit(bitmap, max, i);
  862. len = i - first;
  863. free += len;
  864. if (len > 1)
  865. ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
  866. else
  867. grp->bb_counters[0]++;
  868. if (i < max)
  869. i = ext4_find_next_zero_bit(bitmap, max, i);
  870. }
  871. grp->bb_fragments = fragments;
  872. if (free != grp->bb_free) {
  873. printk(KERN_DEBUG
  874. "EXT4-fs: group %lu: %u blocks in bitmap, %u in gd\n",
  875. group, free, grp->bb_free);
  876. grp->bb_free = free;
  877. }
  878. clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
  879. period = get_cycles() - period;
  880. spin_lock(&EXT4_SB(sb)->s_bal_lock);
  881. EXT4_SB(sb)->s_mb_buddies_generated++;
  882. EXT4_SB(sb)->s_mb_generation_time += period;
  883. spin_unlock(&EXT4_SB(sb)->s_bal_lock);
  884. }
  885. /* The buddy information is attached the buddy cache inode
  886. * for convenience. The information regarding each group
  887. * is loaded via ext4_mb_load_buddy. The information involve
  888. * block bitmap and buddy information. The information are
  889. * stored in the inode as
  890. *
  891. * { page }
  892. * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
  893. *
  894. *
  895. * one block each for bitmap and buddy information.
  896. * So for each group we take up 2 blocks. A page can
  897. * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
  898. * So it can have information regarding groups_per_page which
  899. * is blocks_per_page/2
  900. */
  901. static int ext4_mb_init_cache(struct page *page, char *incore)
  902. {
  903. int blocksize;
  904. int blocks_per_page;
  905. int groups_per_page;
  906. int err = 0;
  907. int i;
  908. ext4_group_t first_group;
  909. int first_block;
  910. struct super_block *sb;
  911. struct buffer_head *bhs;
  912. struct buffer_head **bh;
  913. struct inode *inode;
  914. char *data;
  915. char *bitmap;
  916. mb_debug("init page %lu\n", page->index);
  917. inode = page->mapping->host;
  918. sb = inode->i_sb;
  919. blocksize = 1 << inode->i_blkbits;
  920. blocks_per_page = PAGE_CACHE_SIZE / blocksize;
  921. groups_per_page = blocks_per_page >> 1;
  922. if (groups_per_page == 0)
  923. groups_per_page = 1;
  924. /* allocate buffer_heads to read bitmaps */
  925. if (groups_per_page > 1) {
  926. err = -ENOMEM;
  927. i = sizeof(struct buffer_head *) * groups_per_page;
  928. bh = kzalloc(i, GFP_NOFS);
  929. if (bh == NULL)
  930. goto out;
  931. } else
  932. bh = &bhs;
  933. first_group = page->index * blocks_per_page / 2;
  934. /* read all groups the page covers into the cache */
  935. for (i = 0; i < groups_per_page; i++) {
  936. struct ext4_group_desc *desc;
  937. if (first_group + i >= EXT4_SB(sb)->s_groups_count)
  938. break;
  939. err = -EIO;
  940. desc = ext4_get_group_desc(sb, first_group + i, NULL);
  941. if (desc == NULL)
  942. goto out;
  943. err = -ENOMEM;
  944. bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
  945. if (bh[i] == NULL)
  946. goto out;
  947. if (bh_uptodate_or_lock(bh[i]))
  948. continue;
  949. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  950. ext4_init_block_bitmap(sb, bh[i],
  951. first_group + i, desc);
  952. set_buffer_uptodate(bh[i]);
  953. unlock_buffer(bh[i]);
  954. continue;
  955. }
  956. get_bh(bh[i]);
  957. bh[i]->b_end_io = end_buffer_read_sync;
  958. submit_bh(READ, bh[i]);
  959. mb_debug("read bitmap for group %lu\n", first_group + i);
  960. }
  961. /* wait for I/O completion */
  962. for (i = 0; i < groups_per_page && bh[i]; i++)
  963. wait_on_buffer(bh[i]);
  964. err = -EIO;
  965. for (i = 0; i < groups_per_page && bh[i]; i++)
  966. if (!buffer_uptodate(bh[i]))
  967. goto out;
  968. first_block = page->index * blocks_per_page;
  969. for (i = 0; i < blocks_per_page; i++) {
  970. int group;
  971. struct ext4_group_info *grinfo;
  972. group = (first_block + i) >> 1;
  973. if (group >= EXT4_SB(sb)->s_groups_count)
  974. break;
  975. /*
  976. * data carry information regarding this
  977. * particular group in the format specified
  978. * above
  979. *
  980. */
  981. data = page_address(page) + (i * blocksize);
  982. bitmap = bh[group - first_group]->b_data;
  983. /*
  984. * We place the buddy block and bitmap block
  985. * close together
  986. */
  987. if ((first_block + i) & 1) {
  988. /* this is block of buddy */
  989. BUG_ON(incore == NULL);
  990. mb_debug("put buddy for group %u in page %lu/%x\n",
  991. group, page->index, i * blocksize);
  992. memset(data, 0xff, blocksize);
  993. grinfo = ext4_get_group_info(sb, group);
  994. grinfo->bb_fragments = 0;
  995. memset(grinfo->bb_counters, 0,
  996. sizeof(unsigned short)*(sb->s_blocksize_bits+2));
  997. /*
  998. * incore got set to the group block bitmap below
  999. */
  1000. ext4_mb_generate_buddy(sb, data, incore, group);
  1001. incore = NULL;
  1002. } else {
  1003. /* this is block of bitmap */
  1004. BUG_ON(incore != NULL);
  1005. mb_debug("put bitmap for group %u in page %lu/%x\n",
  1006. group, page->index, i * blocksize);
  1007. /* see comments in ext4_mb_put_pa() */
  1008. ext4_lock_group(sb, group);
  1009. memcpy(data, bitmap, blocksize);
  1010. /* mark all preallocated blks used in in-core bitmap */
  1011. ext4_mb_generate_from_pa(sb, data, group);
  1012. ext4_unlock_group(sb, group);
  1013. /* set incore so that the buddy information can be
  1014. * generated using this
  1015. */
  1016. incore = data;
  1017. }
  1018. }
  1019. SetPageUptodate(page);
  1020. out:
  1021. if (bh) {
  1022. for (i = 0; i < groups_per_page && bh[i]; i++)
  1023. brelse(bh[i]);
  1024. if (bh != &bhs)
  1025. kfree(bh);
  1026. }
  1027. return err;
  1028. }
  1029. static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
  1030. struct ext4_buddy *e4b)
  1031. {
  1032. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1033. struct inode *inode = sbi->s_buddy_cache;
  1034. int blocks_per_page;
  1035. int block;
  1036. int pnum;
  1037. int poff;
  1038. struct page *page;
  1039. mb_debug("load group %lu\n", group);
  1040. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1041. e4b->bd_blkbits = sb->s_blocksize_bits;
  1042. e4b->bd_info = ext4_get_group_info(sb, group);
  1043. e4b->bd_sb = sb;
  1044. e4b->bd_group = group;
  1045. e4b->bd_buddy_page = NULL;
  1046. e4b->bd_bitmap_page = NULL;
  1047. /*
  1048. * the buddy cache inode stores the block bitmap
  1049. * and buddy information in consecutive blocks.
  1050. * So for each group we need two blocks.
  1051. */
  1052. block = group * 2;
  1053. pnum = block / blocks_per_page;
  1054. poff = block % blocks_per_page;
  1055. /* we could use find_or_create_page(), but it locks page
  1056. * what we'd like to avoid in fast path ... */
  1057. page = find_get_page(inode->i_mapping, pnum);
  1058. if (page == NULL || !PageUptodate(page)) {
  1059. if (page)
  1060. page_cache_release(page);
  1061. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1062. if (page) {
  1063. BUG_ON(page->mapping != inode->i_mapping);
  1064. if (!PageUptodate(page)) {
  1065. ext4_mb_init_cache(page, NULL);
  1066. mb_cmp_bitmaps(e4b, page_address(page) +
  1067. (poff * sb->s_blocksize));
  1068. }
  1069. unlock_page(page);
  1070. }
  1071. }
  1072. if (page == NULL || !PageUptodate(page))
  1073. goto err;
  1074. e4b->bd_bitmap_page = page;
  1075. e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
  1076. mark_page_accessed(page);
  1077. block++;
  1078. pnum = block / blocks_per_page;
  1079. poff = block % blocks_per_page;
  1080. page = find_get_page(inode->i_mapping, pnum);
  1081. if (page == NULL || !PageUptodate(page)) {
  1082. if (page)
  1083. page_cache_release(page);
  1084. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1085. if (page) {
  1086. BUG_ON(page->mapping != inode->i_mapping);
  1087. if (!PageUptodate(page))
  1088. ext4_mb_init_cache(page, e4b->bd_bitmap);
  1089. unlock_page(page);
  1090. }
  1091. }
  1092. if (page == NULL || !PageUptodate(page))
  1093. goto err;
  1094. e4b->bd_buddy_page = page;
  1095. e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
  1096. mark_page_accessed(page);
  1097. BUG_ON(e4b->bd_bitmap_page == NULL);
  1098. BUG_ON(e4b->bd_buddy_page == NULL);
  1099. return 0;
  1100. err:
  1101. if (e4b->bd_bitmap_page)
  1102. page_cache_release(e4b->bd_bitmap_page);
  1103. if (e4b->bd_buddy_page)
  1104. page_cache_release(e4b->bd_buddy_page);
  1105. e4b->bd_buddy = NULL;
  1106. e4b->bd_bitmap = NULL;
  1107. return -EIO;
  1108. }
  1109. static void ext4_mb_release_desc(struct ext4_buddy *e4b)
  1110. {
  1111. if (e4b->bd_bitmap_page)
  1112. page_cache_release(e4b->bd_bitmap_page);
  1113. if (e4b->bd_buddy_page)
  1114. page_cache_release(e4b->bd_buddy_page);
  1115. }
  1116. static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
  1117. {
  1118. int order = 1;
  1119. void *bb;
  1120. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  1121. BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
  1122. bb = EXT4_MB_BUDDY(e4b);
  1123. while (order <= e4b->bd_blkbits + 1) {
  1124. block = block >> 1;
  1125. if (!mb_test_bit(block, bb)) {
  1126. /* this block is part of buddy of order 'order' */
  1127. return order;
  1128. }
  1129. bb += 1 << (e4b->bd_blkbits - order);
  1130. order++;
  1131. }
  1132. return 0;
  1133. }
  1134. static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
  1135. {
  1136. __u32 *addr;
  1137. len = cur + len;
  1138. while (cur < len) {
  1139. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1140. /* fast path: clear whole word at once */
  1141. addr = bm + (cur >> 3);
  1142. *addr = 0;
  1143. cur += 32;
  1144. continue;
  1145. }
  1146. mb_clear_bit_atomic(lock, cur, bm);
  1147. cur++;
  1148. }
  1149. }
  1150. static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
  1151. {
  1152. __u32 *addr;
  1153. len = cur + len;
  1154. while (cur < len) {
  1155. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1156. /* fast path: set whole word at once */
  1157. addr = bm + (cur >> 3);
  1158. *addr = 0xffffffff;
  1159. cur += 32;
  1160. continue;
  1161. }
  1162. mb_set_bit_atomic(lock, cur, bm);
  1163. cur++;
  1164. }
  1165. }
  1166. static int mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  1167. int first, int count)
  1168. {
  1169. int block = 0;
  1170. int max = 0;
  1171. int order;
  1172. void *buddy;
  1173. void *buddy2;
  1174. struct super_block *sb = e4b->bd_sb;
  1175. BUG_ON(first + count > (sb->s_blocksize << 3));
  1176. BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
  1177. mb_check_buddy(e4b);
  1178. mb_free_blocks_double(inode, e4b, first, count);
  1179. e4b->bd_info->bb_free += count;
  1180. if (first < e4b->bd_info->bb_first_free)
  1181. e4b->bd_info->bb_first_free = first;
  1182. /* let's maintain fragments counter */
  1183. if (first != 0)
  1184. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1185. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1186. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1187. if (block && max)
  1188. e4b->bd_info->bb_fragments--;
  1189. else if (!block && !max)
  1190. e4b->bd_info->bb_fragments++;
  1191. /* let's maintain buddy itself */
  1192. while (count-- > 0) {
  1193. block = first++;
  1194. order = 0;
  1195. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1196. ext4_fsblk_t blocknr;
  1197. blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
  1198. blocknr += block;
  1199. blocknr +=
  1200. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  1201. ext4_error(sb, __FUNCTION__, "double-free of inode"
  1202. " %lu's block %llu(bit %u in group %lu)\n",
  1203. inode ? inode->i_ino : 0, blocknr, block,
  1204. e4b->bd_group);
  1205. }
  1206. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1207. e4b->bd_info->bb_counters[order]++;
  1208. /* start of the buddy */
  1209. buddy = mb_find_buddy(e4b, order, &max);
  1210. do {
  1211. block &= ~1UL;
  1212. if (mb_test_bit(block, buddy) ||
  1213. mb_test_bit(block + 1, buddy))
  1214. break;
  1215. /* both the buddies are free, try to coalesce them */
  1216. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1217. if (!buddy2)
  1218. break;
  1219. if (order > 0) {
  1220. /* for special purposes, we don't set
  1221. * free bits in bitmap */
  1222. mb_set_bit(block, buddy);
  1223. mb_set_bit(block + 1, buddy);
  1224. }
  1225. e4b->bd_info->bb_counters[order]--;
  1226. e4b->bd_info->bb_counters[order]--;
  1227. block = block >> 1;
  1228. order++;
  1229. e4b->bd_info->bb_counters[order]++;
  1230. mb_clear_bit(block, buddy2);
  1231. buddy = buddy2;
  1232. } while (1);
  1233. }
  1234. mb_check_buddy(e4b);
  1235. return 0;
  1236. }
  1237. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1238. int needed, struct ext4_free_extent *ex)
  1239. {
  1240. int next = block;
  1241. int max;
  1242. int ord;
  1243. void *buddy;
  1244. BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
  1245. BUG_ON(ex == NULL);
  1246. buddy = mb_find_buddy(e4b, order, &max);
  1247. BUG_ON(buddy == NULL);
  1248. BUG_ON(block >= max);
  1249. if (mb_test_bit(block, buddy)) {
  1250. ex->fe_len = 0;
  1251. ex->fe_start = 0;
  1252. ex->fe_group = 0;
  1253. return 0;
  1254. }
  1255. /* FIXME dorp order completely ? */
  1256. if (likely(order == 0)) {
  1257. /* find actual order */
  1258. order = mb_find_order_for_block(e4b, block);
  1259. block = block >> order;
  1260. }
  1261. ex->fe_len = 1 << order;
  1262. ex->fe_start = block << order;
  1263. ex->fe_group = e4b->bd_group;
  1264. /* calc difference from given start */
  1265. next = next - ex->fe_start;
  1266. ex->fe_len -= next;
  1267. ex->fe_start += next;
  1268. while (needed > ex->fe_len &&
  1269. (buddy = mb_find_buddy(e4b, order, &max))) {
  1270. if (block + 1 >= max)
  1271. break;
  1272. next = (block + 1) * (1 << order);
  1273. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1274. break;
  1275. ord = mb_find_order_for_block(e4b, next);
  1276. order = ord;
  1277. block = next >> order;
  1278. ex->fe_len += 1 << order;
  1279. }
  1280. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1281. return ex->fe_len;
  1282. }
  1283. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1284. {
  1285. int ord;
  1286. int mlen = 0;
  1287. int max = 0;
  1288. int cur;
  1289. int start = ex->fe_start;
  1290. int len = ex->fe_len;
  1291. unsigned ret = 0;
  1292. int len0 = len;
  1293. void *buddy;
  1294. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1295. BUG_ON(e4b->bd_group != ex->fe_group);
  1296. BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
  1297. mb_check_buddy(e4b);
  1298. mb_mark_used_double(e4b, start, len);
  1299. e4b->bd_info->bb_free -= len;
  1300. if (e4b->bd_info->bb_first_free == start)
  1301. e4b->bd_info->bb_first_free += len;
  1302. /* let's maintain fragments counter */
  1303. if (start != 0)
  1304. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1305. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1306. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1307. if (mlen && max)
  1308. e4b->bd_info->bb_fragments++;
  1309. else if (!mlen && !max)
  1310. e4b->bd_info->bb_fragments--;
  1311. /* let's maintain buddy itself */
  1312. while (len) {
  1313. ord = mb_find_order_for_block(e4b, start);
  1314. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1315. /* the whole chunk may be allocated at once! */
  1316. mlen = 1 << ord;
  1317. buddy = mb_find_buddy(e4b, ord, &max);
  1318. BUG_ON((start >> ord) >= max);
  1319. mb_set_bit(start >> ord, buddy);
  1320. e4b->bd_info->bb_counters[ord]--;
  1321. start += mlen;
  1322. len -= mlen;
  1323. BUG_ON(len < 0);
  1324. continue;
  1325. }
  1326. /* store for history */
  1327. if (ret == 0)
  1328. ret = len | (ord << 16);
  1329. /* we have to split large buddy */
  1330. BUG_ON(ord <= 0);
  1331. buddy = mb_find_buddy(e4b, ord, &max);
  1332. mb_set_bit(start >> ord, buddy);
  1333. e4b->bd_info->bb_counters[ord]--;
  1334. ord--;
  1335. cur = (start >> ord) & ~1U;
  1336. buddy = mb_find_buddy(e4b, ord, &max);
  1337. mb_clear_bit(cur, buddy);
  1338. mb_clear_bit(cur + 1, buddy);
  1339. e4b->bd_info->bb_counters[ord]++;
  1340. e4b->bd_info->bb_counters[ord]++;
  1341. }
  1342. mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
  1343. EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1344. mb_check_buddy(e4b);
  1345. return ret;
  1346. }
  1347. /*
  1348. * Must be called under group lock!
  1349. */
  1350. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1351. struct ext4_buddy *e4b)
  1352. {
  1353. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1354. int ret;
  1355. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1356. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1357. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1358. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1359. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1360. /* preallocation can change ac_b_ex, thus we store actually
  1361. * allocated blocks for history */
  1362. ac->ac_f_ex = ac->ac_b_ex;
  1363. ac->ac_status = AC_STATUS_FOUND;
  1364. ac->ac_tail = ret & 0xffff;
  1365. ac->ac_buddy = ret >> 16;
  1366. /* XXXXXXX: SUCH A HORRIBLE **CK */
  1367. /*FIXME!! Why ? */
  1368. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1369. get_page(ac->ac_bitmap_page);
  1370. ac->ac_buddy_page = e4b->bd_buddy_page;
  1371. get_page(ac->ac_buddy_page);
  1372. /* store last allocated for subsequent stream allocation */
  1373. if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1374. spin_lock(&sbi->s_md_lock);
  1375. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1376. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1377. spin_unlock(&sbi->s_md_lock);
  1378. }
  1379. }
  1380. /*
  1381. * regular allocator, for general purposes allocation
  1382. */
  1383. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1384. struct ext4_buddy *e4b,
  1385. int finish_group)
  1386. {
  1387. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1388. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1389. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1390. struct ext4_free_extent ex;
  1391. int max;
  1392. /*
  1393. * We don't want to scan for a whole year
  1394. */
  1395. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1396. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1397. ac->ac_status = AC_STATUS_BREAK;
  1398. return;
  1399. }
  1400. /*
  1401. * Haven't found good chunk so far, let's continue
  1402. */
  1403. if (bex->fe_len < gex->fe_len)
  1404. return;
  1405. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1406. && bex->fe_group == e4b->bd_group) {
  1407. /* recheck chunk's availability - we don't know
  1408. * when it was found (within this lock-unlock
  1409. * period or not) */
  1410. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1411. if (max >= gex->fe_len) {
  1412. ext4_mb_use_best_found(ac, e4b);
  1413. return;
  1414. }
  1415. }
  1416. }
  1417. /*
  1418. * The routine checks whether found extent is good enough. If it is,
  1419. * then the extent gets marked used and flag is set to the context
  1420. * to stop scanning. Otherwise, the extent is compared with the
  1421. * previous found extent and if new one is better, then it's stored
  1422. * in the context. Later, the best found extent will be used, if
  1423. * mballoc can't find good enough extent.
  1424. *
  1425. * FIXME: real allocation policy is to be designed yet!
  1426. */
  1427. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1428. struct ext4_free_extent *ex,
  1429. struct ext4_buddy *e4b)
  1430. {
  1431. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1432. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1433. BUG_ON(ex->fe_len <= 0);
  1434. BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1435. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1436. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1437. ac->ac_found++;
  1438. /*
  1439. * The special case - take what you catch first
  1440. */
  1441. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1442. *bex = *ex;
  1443. ext4_mb_use_best_found(ac, e4b);
  1444. return;
  1445. }
  1446. /*
  1447. * Let's check whether the chuck is good enough
  1448. */
  1449. if (ex->fe_len == gex->fe_len) {
  1450. *bex = *ex;
  1451. ext4_mb_use_best_found(ac, e4b);
  1452. return;
  1453. }
  1454. /*
  1455. * If this is first found extent, just store it in the context
  1456. */
  1457. if (bex->fe_len == 0) {
  1458. *bex = *ex;
  1459. return;
  1460. }
  1461. /*
  1462. * If new found extent is better, store it in the context
  1463. */
  1464. if (bex->fe_len < gex->fe_len) {
  1465. /* if the request isn't satisfied, any found extent
  1466. * larger than previous best one is better */
  1467. if (ex->fe_len > bex->fe_len)
  1468. *bex = *ex;
  1469. } else if (ex->fe_len > gex->fe_len) {
  1470. /* if the request is satisfied, then we try to find
  1471. * an extent that still satisfy the request, but is
  1472. * smaller than previous one */
  1473. if (ex->fe_len < bex->fe_len)
  1474. *bex = *ex;
  1475. }
  1476. ext4_mb_check_limits(ac, e4b, 0);
  1477. }
  1478. static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1479. struct ext4_buddy *e4b)
  1480. {
  1481. struct ext4_free_extent ex = ac->ac_b_ex;
  1482. ext4_group_t group = ex.fe_group;
  1483. int max;
  1484. int err;
  1485. BUG_ON(ex.fe_len <= 0);
  1486. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1487. if (err)
  1488. return err;
  1489. ext4_lock_group(ac->ac_sb, group);
  1490. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1491. if (max > 0) {
  1492. ac->ac_b_ex = ex;
  1493. ext4_mb_use_best_found(ac, e4b);
  1494. }
  1495. ext4_unlock_group(ac->ac_sb, group);
  1496. ext4_mb_release_desc(e4b);
  1497. return 0;
  1498. }
  1499. static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1500. struct ext4_buddy *e4b)
  1501. {
  1502. ext4_group_t group = ac->ac_g_ex.fe_group;
  1503. int max;
  1504. int err;
  1505. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1506. struct ext4_super_block *es = sbi->s_es;
  1507. struct ext4_free_extent ex;
  1508. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1509. return 0;
  1510. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1511. if (err)
  1512. return err;
  1513. ext4_lock_group(ac->ac_sb, group);
  1514. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1515. ac->ac_g_ex.fe_len, &ex);
  1516. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1517. ext4_fsblk_t start;
  1518. start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
  1519. ex.fe_start + le32_to_cpu(es->s_first_data_block);
  1520. /* use do_div to get remainder (would be 64-bit modulo) */
  1521. if (do_div(start, sbi->s_stripe) == 0) {
  1522. ac->ac_found++;
  1523. ac->ac_b_ex = ex;
  1524. ext4_mb_use_best_found(ac, e4b);
  1525. }
  1526. } else if (max >= ac->ac_g_ex.fe_len) {
  1527. BUG_ON(ex.fe_len <= 0);
  1528. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1529. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1530. ac->ac_found++;
  1531. ac->ac_b_ex = ex;
  1532. ext4_mb_use_best_found(ac, e4b);
  1533. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1534. /* Sometimes, caller may want to merge even small
  1535. * number of blocks to an existing extent */
  1536. BUG_ON(ex.fe_len <= 0);
  1537. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1538. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1539. ac->ac_found++;
  1540. ac->ac_b_ex = ex;
  1541. ext4_mb_use_best_found(ac, e4b);
  1542. }
  1543. ext4_unlock_group(ac->ac_sb, group);
  1544. ext4_mb_release_desc(e4b);
  1545. return 0;
  1546. }
  1547. /*
  1548. * The routine scans buddy structures (not bitmap!) from given order
  1549. * to max order and tries to find big enough chunk to satisfy the req
  1550. */
  1551. static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1552. struct ext4_buddy *e4b)
  1553. {
  1554. struct super_block *sb = ac->ac_sb;
  1555. struct ext4_group_info *grp = e4b->bd_info;
  1556. void *buddy;
  1557. int i;
  1558. int k;
  1559. int max;
  1560. BUG_ON(ac->ac_2order <= 0);
  1561. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1562. if (grp->bb_counters[i] == 0)
  1563. continue;
  1564. buddy = mb_find_buddy(e4b, i, &max);
  1565. BUG_ON(buddy == NULL);
  1566. k = ext4_find_next_zero_bit(buddy, max, 0);
  1567. BUG_ON(k >= max);
  1568. ac->ac_found++;
  1569. ac->ac_b_ex.fe_len = 1 << i;
  1570. ac->ac_b_ex.fe_start = k << i;
  1571. ac->ac_b_ex.fe_group = e4b->bd_group;
  1572. ext4_mb_use_best_found(ac, e4b);
  1573. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1574. if (EXT4_SB(sb)->s_mb_stats)
  1575. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1576. break;
  1577. }
  1578. }
  1579. /*
  1580. * The routine scans the group and measures all found extents.
  1581. * In order to optimize scanning, caller must pass number of
  1582. * free blocks in the group, so the routine can know upper limit.
  1583. */
  1584. static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1585. struct ext4_buddy *e4b)
  1586. {
  1587. struct super_block *sb = ac->ac_sb;
  1588. void *bitmap = EXT4_MB_BITMAP(e4b);
  1589. struct ext4_free_extent ex;
  1590. int i;
  1591. int free;
  1592. free = e4b->bd_info->bb_free;
  1593. BUG_ON(free <= 0);
  1594. i = e4b->bd_info->bb_first_free;
  1595. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1596. i = ext4_find_next_zero_bit(bitmap,
  1597. EXT4_BLOCKS_PER_GROUP(sb), i);
  1598. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1599. BUG_ON(free != 0);
  1600. break;
  1601. }
  1602. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1603. BUG_ON(ex.fe_len <= 0);
  1604. BUG_ON(free < ex.fe_len);
  1605. ext4_mb_measure_extent(ac, &ex, e4b);
  1606. i += ex.fe_len;
  1607. free -= ex.fe_len;
  1608. }
  1609. ext4_mb_check_limits(ac, e4b, 1);
  1610. }
  1611. /*
  1612. * This is a special case for storages like raid5
  1613. * we try to find stripe-aligned chunks for stripe-size requests
  1614. * XXX should do so at least for multiples of stripe size as well
  1615. */
  1616. static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
  1617. struct ext4_buddy *e4b)
  1618. {
  1619. struct super_block *sb = ac->ac_sb;
  1620. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1621. void *bitmap = EXT4_MB_BITMAP(e4b);
  1622. struct ext4_free_extent ex;
  1623. ext4_fsblk_t first_group_block;
  1624. ext4_fsblk_t a;
  1625. ext4_grpblk_t i;
  1626. int max;
  1627. BUG_ON(sbi->s_stripe == 0);
  1628. /* find first stripe-aligned block in group */
  1629. first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
  1630. + le32_to_cpu(sbi->s_es->s_first_data_block);
  1631. a = first_group_block + sbi->s_stripe - 1;
  1632. do_div(a, sbi->s_stripe);
  1633. i = (a * sbi->s_stripe) - first_group_block;
  1634. while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
  1635. if (!mb_test_bit(i, bitmap)) {
  1636. max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
  1637. if (max >= sbi->s_stripe) {
  1638. ac->ac_found++;
  1639. ac->ac_b_ex = ex;
  1640. ext4_mb_use_best_found(ac, e4b);
  1641. break;
  1642. }
  1643. }
  1644. i += sbi->s_stripe;
  1645. }
  1646. }
  1647. static int ext4_mb_good_group(struct ext4_allocation_context *ac,
  1648. ext4_group_t group, int cr)
  1649. {
  1650. unsigned free, fragments;
  1651. unsigned i, bits;
  1652. struct ext4_group_desc *desc;
  1653. struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
  1654. BUG_ON(cr < 0 || cr >= 4);
  1655. BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
  1656. free = grp->bb_free;
  1657. fragments = grp->bb_fragments;
  1658. if (free == 0)
  1659. return 0;
  1660. if (fragments == 0)
  1661. return 0;
  1662. switch (cr) {
  1663. case 0:
  1664. BUG_ON(ac->ac_2order == 0);
  1665. /* If this group is uninitialized, skip it initially */
  1666. desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
  1667. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
  1668. return 0;
  1669. bits = ac->ac_sb->s_blocksize_bits + 1;
  1670. for (i = ac->ac_2order; i <= bits; i++)
  1671. if (grp->bb_counters[i] > 0)
  1672. return 1;
  1673. break;
  1674. case 1:
  1675. if ((free / fragments) >= ac->ac_g_ex.fe_len)
  1676. return 1;
  1677. break;
  1678. case 2:
  1679. if (free >= ac->ac_g_ex.fe_len)
  1680. return 1;
  1681. break;
  1682. case 3:
  1683. return 1;
  1684. default:
  1685. BUG();
  1686. }
  1687. return 0;
  1688. }
  1689. static int ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
  1690. {
  1691. ext4_group_t group;
  1692. ext4_group_t i;
  1693. int cr;
  1694. int err = 0;
  1695. int bsbits;
  1696. struct ext4_sb_info *sbi;
  1697. struct super_block *sb;
  1698. struct ext4_buddy e4b;
  1699. loff_t size, isize;
  1700. sb = ac->ac_sb;
  1701. sbi = EXT4_SB(sb);
  1702. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1703. /* first, try the goal */
  1704. err = ext4_mb_find_by_goal(ac, &e4b);
  1705. if (err || ac->ac_status == AC_STATUS_FOUND)
  1706. goto out;
  1707. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1708. goto out;
  1709. /*
  1710. * ac->ac2_order is set only if the fe_len is a power of 2
  1711. * if ac2_order is set we also set criteria to 0 so that we
  1712. * try exact allocation using buddy.
  1713. */
  1714. i = fls(ac->ac_g_ex.fe_len);
  1715. ac->ac_2order = 0;
  1716. /*
  1717. * We search using buddy data only if the order of the request
  1718. * is greater than equal to the sbi_s_mb_order2_reqs
  1719. * You can tune it via /proc/fs/ext4/<partition>/order2_req
  1720. */
  1721. if (i >= sbi->s_mb_order2_reqs) {
  1722. /*
  1723. * This should tell if fe_len is exactly power of 2
  1724. */
  1725. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1726. ac->ac_2order = i - 1;
  1727. }
  1728. bsbits = ac->ac_sb->s_blocksize_bits;
  1729. /* if stream allocation is enabled, use global goal */
  1730. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  1731. isize = i_size_read(ac->ac_inode) >> bsbits;
  1732. if (size < isize)
  1733. size = isize;
  1734. if (size < sbi->s_mb_stream_request &&
  1735. (ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1736. /* TBD: may be hot point */
  1737. spin_lock(&sbi->s_md_lock);
  1738. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1739. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1740. spin_unlock(&sbi->s_md_lock);
  1741. }
  1742. /* searching for the right group start from the goal value specified */
  1743. group = ac->ac_g_ex.fe_group;
  1744. /* Let's just scan groups to find more-less suitable blocks */
  1745. cr = ac->ac_2order ? 0 : 1;
  1746. /*
  1747. * cr == 0 try to get exact allocation,
  1748. * cr == 3 try to get anything
  1749. */
  1750. repeat:
  1751. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1752. ac->ac_criteria = cr;
  1753. for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
  1754. struct ext4_group_info *grp;
  1755. struct ext4_group_desc *desc;
  1756. if (group == EXT4_SB(sb)->s_groups_count)
  1757. group = 0;
  1758. /* quick check to skip empty groups */
  1759. grp = ext4_get_group_info(ac->ac_sb, group);
  1760. if (grp->bb_free == 0)
  1761. continue;
  1762. /*
  1763. * if the group is already init we check whether it is
  1764. * a good group and if not we don't load the buddy
  1765. */
  1766. if (EXT4_MB_GRP_NEED_INIT(grp)) {
  1767. /*
  1768. * we need full data about the group
  1769. * to make a good selection
  1770. */
  1771. err = ext4_mb_load_buddy(sb, group, &e4b);
  1772. if (err)
  1773. goto out;
  1774. ext4_mb_release_desc(&e4b);
  1775. }
  1776. /*
  1777. * If the particular group doesn't satisfy our
  1778. * criteria we continue with the next group
  1779. */
  1780. if (!ext4_mb_good_group(ac, group, cr))
  1781. continue;
  1782. err = ext4_mb_load_buddy(sb, group, &e4b);
  1783. if (err)
  1784. goto out;
  1785. ext4_lock_group(sb, group);
  1786. if (!ext4_mb_good_group(ac, group, cr)) {
  1787. /* someone did allocation from this group */
  1788. ext4_unlock_group(sb, group);
  1789. ext4_mb_release_desc(&e4b);
  1790. continue;
  1791. }
  1792. ac->ac_groups_scanned++;
  1793. desc = ext4_get_group_desc(sb, group, NULL);
  1794. if (cr == 0 || (desc->bg_flags &
  1795. cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
  1796. ac->ac_2order != 0))
  1797. ext4_mb_simple_scan_group(ac, &e4b);
  1798. else if (cr == 1 &&
  1799. ac->ac_g_ex.fe_len == sbi->s_stripe)
  1800. ext4_mb_scan_aligned(ac, &e4b);
  1801. else
  1802. ext4_mb_complex_scan_group(ac, &e4b);
  1803. ext4_unlock_group(sb, group);
  1804. ext4_mb_release_desc(&e4b);
  1805. if (ac->ac_status != AC_STATUS_CONTINUE)
  1806. break;
  1807. }
  1808. }
  1809. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1810. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1811. /*
  1812. * We've been searching too long. Let's try to allocate
  1813. * the best chunk we've found so far
  1814. */
  1815. ext4_mb_try_best_found(ac, &e4b);
  1816. if (ac->ac_status != AC_STATUS_FOUND) {
  1817. /*
  1818. * Someone more lucky has already allocated it.
  1819. * The only thing we can do is just take first
  1820. * found block(s)
  1821. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1822. */
  1823. ac->ac_b_ex.fe_group = 0;
  1824. ac->ac_b_ex.fe_start = 0;
  1825. ac->ac_b_ex.fe_len = 0;
  1826. ac->ac_status = AC_STATUS_CONTINUE;
  1827. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1828. cr = 3;
  1829. atomic_inc(&sbi->s_mb_lost_chunks);
  1830. goto repeat;
  1831. }
  1832. }
  1833. out:
  1834. return err;
  1835. }
  1836. #ifdef EXT4_MB_HISTORY
  1837. struct ext4_mb_proc_session {
  1838. struct ext4_mb_history *history;
  1839. struct super_block *sb;
  1840. int start;
  1841. int max;
  1842. };
  1843. static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
  1844. struct ext4_mb_history *hs,
  1845. int first)
  1846. {
  1847. if (hs == s->history + s->max)
  1848. hs = s->history;
  1849. if (!first && hs == s->history + s->start)
  1850. return NULL;
  1851. while (hs->orig.fe_len == 0) {
  1852. hs++;
  1853. if (hs == s->history + s->max)
  1854. hs = s->history;
  1855. if (hs == s->history + s->start)
  1856. return NULL;
  1857. }
  1858. return hs;
  1859. }
  1860. static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
  1861. {
  1862. struct ext4_mb_proc_session *s = seq->private;
  1863. struct ext4_mb_history *hs;
  1864. int l = *pos;
  1865. if (l == 0)
  1866. return SEQ_START_TOKEN;
  1867. hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1868. if (!hs)
  1869. return NULL;
  1870. while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
  1871. return hs;
  1872. }
  1873. static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
  1874. loff_t *pos)
  1875. {
  1876. struct ext4_mb_proc_session *s = seq->private;
  1877. struct ext4_mb_history *hs = v;
  1878. ++*pos;
  1879. if (v == SEQ_START_TOKEN)
  1880. return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1881. else
  1882. return ext4_mb_history_skip_empty(s, ++hs, 0);
  1883. }
  1884. static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
  1885. {
  1886. char buf[25], buf2[25], buf3[25], *fmt;
  1887. struct ext4_mb_history *hs = v;
  1888. if (v == SEQ_START_TOKEN) {
  1889. seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
  1890. "%-5s %-2s %-5s %-5s %-5s %-6s\n",
  1891. "pid", "inode", "original", "goal", "result", "found",
  1892. "grps", "cr", "flags", "merge", "tail", "broken");
  1893. return 0;
  1894. }
  1895. if (hs->op == EXT4_MB_HISTORY_ALLOC) {
  1896. fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
  1897. "%-5u %-5s %-5u %-6u\n";
  1898. sprintf(buf2, "%lu/%d/%u@%u", hs->result.fe_group,
  1899. hs->result.fe_start, hs->result.fe_len,
  1900. hs->result.fe_logical);
  1901. sprintf(buf, "%lu/%d/%u@%u", hs->orig.fe_group,
  1902. hs->orig.fe_start, hs->orig.fe_len,
  1903. hs->orig.fe_logical);
  1904. sprintf(buf3, "%lu/%d/%u@%u", hs->goal.fe_group,
  1905. hs->goal.fe_start, hs->goal.fe_len,
  1906. hs->goal.fe_logical);
  1907. seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
  1908. hs->found, hs->groups, hs->cr, hs->flags,
  1909. hs->merged ? "M" : "", hs->tail,
  1910. hs->buddy ? 1 << hs->buddy : 0);
  1911. } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
  1912. fmt = "%-5u %-8u %-23s %-23s %-23s\n";
  1913. sprintf(buf2, "%lu/%d/%u@%u", hs->result.fe_group,
  1914. hs->result.fe_start, hs->result.fe_len,
  1915. hs->result.fe_logical);
  1916. sprintf(buf, "%lu/%d/%u@%u", hs->orig.fe_group,
  1917. hs->orig.fe_start, hs->orig.fe_len,
  1918. hs->orig.fe_logical);
  1919. seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
  1920. } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
  1921. sprintf(buf2, "%lu/%d/%u", hs->result.fe_group,
  1922. hs->result.fe_start, hs->result.fe_len);
  1923. seq_printf(seq, "%-5u %-8u %-23s discard\n",
  1924. hs->pid, hs->ino, buf2);
  1925. } else if (hs->op == EXT4_MB_HISTORY_FREE) {
  1926. sprintf(buf2, "%lu/%d/%u", hs->result.fe_group,
  1927. hs->result.fe_start, hs->result.fe_len);
  1928. seq_printf(seq, "%-5u %-8u %-23s free\n",
  1929. hs->pid, hs->ino, buf2);
  1930. }
  1931. return 0;
  1932. }
  1933. static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
  1934. {
  1935. }
  1936. static struct seq_operations ext4_mb_seq_history_ops = {
  1937. .start = ext4_mb_seq_history_start,
  1938. .next = ext4_mb_seq_history_next,
  1939. .stop = ext4_mb_seq_history_stop,
  1940. .show = ext4_mb_seq_history_show,
  1941. };
  1942. static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
  1943. {
  1944. struct super_block *sb = PDE(inode)->data;
  1945. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1946. struct ext4_mb_proc_session *s;
  1947. int rc;
  1948. int size;
  1949. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1950. if (s == NULL)
  1951. return -ENOMEM;
  1952. s->sb = sb;
  1953. size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
  1954. s->history = kmalloc(size, GFP_KERNEL);
  1955. if (s->history == NULL) {
  1956. kfree(s);
  1957. return -ENOMEM;
  1958. }
  1959. spin_lock(&sbi->s_mb_history_lock);
  1960. memcpy(s->history, sbi->s_mb_history, size);
  1961. s->max = sbi->s_mb_history_max;
  1962. s->start = sbi->s_mb_history_cur % s->max;
  1963. spin_unlock(&sbi->s_mb_history_lock);
  1964. rc = seq_open(file, &ext4_mb_seq_history_ops);
  1965. if (rc == 0) {
  1966. struct seq_file *m = (struct seq_file *)file->private_data;
  1967. m->private = s;
  1968. } else {
  1969. kfree(s->history);
  1970. kfree(s);
  1971. }
  1972. return rc;
  1973. }
  1974. static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
  1975. {
  1976. struct seq_file *seq = (struct seq_file *)file->private_data;
  1977. struct ext4_mb_proc_session *s = seq->private;
  1978. kfree(s->history);
  1979. kfree(s);
  1980. return seq_release(inode, file);
  1981. }
  1982. static ssize_t ext4_mb_seq_history_write(struct file *file,
  1983. const char __user *buffer,
  1984. size_t count, loff_t *ppos)
  1985. {
  1986. struct seq_file *seq = (struct seq_file *)file->private_data;
  1987. struct ext4_mb_proc_session *s = seq->private;
  1988. struct super_block *sb = s->sb;
  1989. char str[32];
  1990. int value;
  1991. if (count >= sizeof(str)) {
  1992. printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
  1993. "mb_history", (int)sizeof(str));
  1994. return -EOVERFLOW;
  1995. }
  1996. if (copy_from_user(str, buffer, count))
  1997. return -EFAULT;
  1998. value = simple_strtol(str, NULL, 0);
  1999. if (value < 0)
  2000. return -ERANGE;
  2001. EXT4_SB(sb)->s_mb_history_filter = value;
  2002. return count;
  2003. }
  2004. static struct file_operations ext4_mb_seq_history_fops = {
  2005. .owner = THIS_MODULE,
  2006. .open = ext4_mb_seq_history_open,
  2007. .read = seq_read,
  2008. .write = ext4_mb_seq_history_write,
  2009. .llseek = seq_lseek,
  2010. .release = ext4_mb_seq_history_release,
  2011. };
  2012. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  2013. {
  2014. struct super_block *sb = seq->private;
  2015. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2016. ext4_group_t group;
  2017. if (*pos < 0 || *pos >= sbi->s_groups_count)
  2018. return NULL;
  2019. group = *pos + 1;
  2020. return (void *) group;
  2021. }
  2022. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  2023. {
  2024. struct super_block *sb = seq->private;
  2025. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2026. ext4_group_t group;
  2027. ++*pos;
  2028. if (*pos < 0 || *pos >= sbi->s_groups_count)
  2029. return NULL;
  2030. group = *pos + 1;
  2031. return (void *) group;;
  2032. }
  2033. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  2034. {
  2035. struct super_block *sb = seq->private;
  2036. long group = (long) v;
  2037. int i;
  2038. int err;
  2039. struct ext4_buddy e4b;
  2040. struct sg {
  2041. struct ext4_group_info info;
  2042. unsigned short counters[16];
  2043. } sg;
  2044. group--;
  2045. if (group == 0)
  2046. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  2047. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  2048. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  2049. "group", "free", "frags", "first",
  2050. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  2051. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  2052. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  2053. sizeof(struct ext4_group_info);
  2054. err = ext4_mb_load_buddy(sb, group, &e4b);
  2055. if (err) {
  2056. seq_printf(seq, "#%-5lu: I/O error\n", group);
  2057. return 0;
  2058. }
  2059. ext4_lock_group(sb, group);
  2060. memcpy(&sg, ext4_get_group_info(sb, group), i);
  2061. ext4_unlock_group(sb, group);
  2062. ext4_mb_release_desc(&e4b);
  2063. seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
  2064. sg.info.bb_fragments, sg.info.bb_first_free);
  2065. for (i = 0; i <= 13; i++)
  2066. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  2067. sg.info.bb_counters[i] : 0);
  2068. seq_printf(seq, " ]\n");
  2069. return 0;
  2070. }
  2071. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  2072. {
  2073. }
  2074. static struct seq_operations ext4_mb_seq_groups_ops = {
  2075. .start = ext4_mb_seq_groups_start,
  2076. .next = ext4_mb_seq_groups_next,
  2077. .stop = ext4_mb_seq_groups_stop,
  2078. .show = ext4_mb_seq_groups_show,
  2079. };
  2080. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  2081. {
  2082. struct super_block *sb = PDE(inode)->data;
  2083. int rc;
  2084. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  2085. if (rc == 0) {
  2086. struct seq_file *m = (struct seq_file *)file->private_data;
  2087. m->private = sb;
  2088. }
  2089. return rc;
  2090. }
  2091. static struct file_operations ext4_mb_seq_groups_fops = {
  2092. .owner = THIS_MODULE,
  2093. .open = ext4_mb_seq_groups_open,
  2094. .read = seq_read,
  2095. .llseek = seq_lseek,
  2096. .release = seq_release,
  2097. };
  2098. static void ext4_mb_history_release(struct super_block *sb)
  2099. {
  2100. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2101. remove_proc_entry("mb_groups", sbi->s_mb_proc);
  2102. remove_proc_entry("mb_history", sbi->s_mb_proc);
  2103. kfree(sbi->s_mb_history);
  2104. }
  2105. static void ext4_mb_history_init(struct super_block *sb)
  2106. {
  2107. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2108. int i;
  2109. if (sbi->s_mb_proc != NULL) {
  2110. struct proc_dir_entry *p;
  2111. p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
  2112. if (p) {
  2113. p->proc_fops = &ext4_mb_seq_history_fops;
  2114. p->data = sb;
  2115. }
  2116. p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
  2117. if (p) {
  2118. p->proc_fops = &ext4_mb_seq_groups_fops;
  2119. p->data = sb;
  2120. }
  2121. }
  2122. sbi->s_mb_history_max = 1000;
  2123. sbi->s_mb_history_cur = 0;
  2124. spin_lock_init(&sbi->s_mb_history_lock);
  2125. i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
  2126. sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
  2127. if (likely(sbi->s_mb_history != NULL))
  2128. memset(sbi->s_mb_history, 0, i);
  2129. /* if we can't allocate history, then we simple won't use it */
  2130. }
  2131. static void ext4_mb_store_history(struct ext4_allocation_context *ac)
  2132. {
  2133. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2134. struct ext4_mb_history h;
  2135. if (unlikely(sbi->s_mb_history == NULL))
  2136. return;
  2137. if (!(ac->ac_op & sbi->s_mb_history_filter))
  2138. return;
  2139. h.op = ac->ac_op;
  2140. h.pid = current->pid;
  2141. h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
  2142. h.orig = ac->ac_o_ex;
  2143. h.result = ac->ac_b_ex;
  2144. h.flags = ac->ac_flags;
  2145. h.found = ac->ac_found;
  2146. h.groups = ac->ac_groups_scanned;
  2147. h.cr = ac->ac_criteria;
  2148. h.tail = ac->ac_tail;
  2149. h.buddy = ac->ac_buddy;
  2150. h.merged = 0;
  2151. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
  2152. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2153. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2154. h.merged = 1;
  2155. h.goal = ac->ac_g_ex;
  2156. h.result = ac->ac_f_ex;
  2157. }
  2158. spin_lock(&sbi->s_mb_history_lock);
  2159. memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
  2160. if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
  2161. sbi->s_mb_history_cur = 0;
  2162. spin_unlock(&sbi->s_mb_history_lock);
  2163. }
  2164. #else
  2165. #define ext4_mb_history_release(sb)
  2166. #define ext4_mb_history_init(sb)
  2167. #endif
  2168. static int ext4_mb_init_backend(struct super_block *sb)
  2169. {
  2170. ext4_group_t i;
  2171. int j, len, metalen;
  2172. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2173. int num_meta_group_infos =
  2174. (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2175. EXT4_DESC_PER_BLOCK_BITS(sb);
  2176. struct ext4_group_info **meta_group_info;
  2177. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2178. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2179. * So a two level scheme suffices for now. */
  2180. sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
  2181. num_meta_group_infos, GFP_KERNEL);
  2182. if (sbi->s_group_info == NULL) {
  2183. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2184. return -ENOMEM;
  2185. }
  2186. sbi->s_buddy_cache = new_inode(sb);
  2187. if (sbi->s_buddy_cache == NULL) {
  2188. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2189. goto err_freesgi;
  2190. }
  2191. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2192. metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
  2193. for (i = 0; i < num_meta_group_infos; i++) {
  2194. if ((i + 1) == num_meta_group_infos)
  2195. metalen = sizeof(*meta_group_info) *
  2196. (sbi->s_groups_count -
  2197. (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
  2198. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2199. if (meta_group_info == NULL) {
  2200. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2201. "buddy group\n");
  2202. goto err_freemeta;
  2203. }
  2204. sbi->s_group_info[i] = meta_group_info;
  2205. }
  2206. /*
  2207. * calculate needed size. if change bb_counters size,
  2208. * don't forget about ext4_mb_generate_buddy()
  2209. */
  2210. len = sizeof(struct ext4_group_info);
  2211. len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
  2212. for (i = 0; i < sbi->s_groups_count; i++) {
  2213. struct ext4_group_desc *desc;
  2214. meta_group_info =
  2215. sbi->s_group_info[i >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2216. j = i & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2217. meta_group_info[j] = kzalloc(len, GFP_KERNEL);
  2218. if (meta_group_info[j] == NULL) {
  2219. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  2220. i--;
  2221. goto err_freebuddy;
  2222. }
  2223. desc = ext4_get_group_desc(sb, i, NULL);
  2224. if (desc == NULL) {
  2225. printk(KERN_ERR
  2226. "EXT4-fs: can't read descriptor %lu\n", i);
  2227. goto err_freebuddy;
  2228. }
  2229. memset(meta_group_info[j], 0, len);
  2230. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2231. &(meta_group_info[j]->bb_state));
  2232. /*
  2233. * initialize bb_free to be able to skip
  2234. * empty groups without initialization
  2235. */
  2236. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2237. meta_group_info[j]->bb_free =
  2238. ext4_free_blocks_after_init(sb, i, desc);
  2239. } else {
  2240. meta_group_info[j]->bb_free =
  2241. le16_to_cpu(desc->bg_free_blocks_count);
  2242. }
  2243. INIT_LIST_HEAD(&meta_group_info[j]->bb_prealloc_list);
  2244. #ifdef DOUBLE_CHECK
  2245. {
  2246. struct buffer_head *bh;
  2247. meta_group_info[j]->bb_bitmap =
  2248. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2249. BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
  2250. bh = read_block_bitmap(sb, i);
  2251. BUG_ON(bh == NULL);
  2252. memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
  2253. sb->s_blocksize);
  2254. put_bh(bh);
  2255. }
  2256. #endif
  2257. }
  2258. return 0;
  2259. err_freebuddy:
  2260. while (i >= 0) {
  2261. kfree(ext4_get_group_info(sb, i));
  2262. i--;
  2263. }
  2264. i = num_meta_group_infos;
  2265. err_freemeta:
  2266. while (--i >= 0)
  2267. kfree(sbi->s_group_info[i]);
  2268. iput(sbi->s_buddy_cache);
  2269. err_freesgi:
  2270. kfree(sbi->s_group_info);
  2271. return -ENOMEM;
  2272. }
  2273. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2274. {
  2275. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2276. unsigned i;
  2277. unsigned offset;
  2278. unsigned max;
  2279. if (!test_opt(sb, MBALLOC))
  2280. return 0;
  2281. i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
  2282. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2283. if (sbi->s_mb_offsets == NULL) {
  2284. clear_opt(sbi->s_mount_opt, MBALLOC);
  2285. return -ENOMEM;
  2286. }
  2287. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2288. if (sbi->s_mb_maxs == NULL) {
  2289. clear_opt(sbi->s_mount_opt, MBALLOC);
  2290. kfree(sbi->s_mb_maxs);
  2291. return -ENOMEM;
  2292. }
  2293. /* order 0 is regular bitmap */
  2294. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2295. sbi->s_mb_offsets[0] = 0;
  2296. i = 1;
  2297. offset = 0;
  2298. max = sb->s_blocksize << 2;
  2299. do {
  2300. sbi->s_mb_offsets[i] = offset;
  2301. sbi->s_mb_maxs[i] = max;
  2302. offset += 1 << (sb->s_blocksize_bits - i);
  2303. max = max >> 1;
  2304. i++;
  2305. } while (i <= sb->s_blocksize_bits + 1);
  2306. /* init file for buddy data */
  2307. i = ext4_mb_init_backend(sb);
  2308. if (i) {
  2309. clear_opt(sbi->s_mount_opt, MBALLOC);
  2310. kfree(sbi->s_mb_offsets);
  2311. kfree(sbi->s_mb_maxs);
  2312. return i;
  2313. }
  2314. spin_lock_init(&sbi->s_md_lock);
  2315. INIT_LIST_HEAD(&sbi->s_active_transaction);
  2316. INIT_LIST_HEAD(&sbi->s_closed_transaction);
  2317. INIT_LIST_HEAD(&sbi->s_committed_transaction);
  2318. spin_lock_init(&sbi->s_bal_lock);
  2319. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2320. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2321. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2322. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2323. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2324. sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
  2325. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2326. i = sizeof(struct ext4_locality_group) * NR_CPUS;
  2327. sbi->s_locality_groups = kmalloc(i, GFP_KERNEL);
  2328. if (sbi->s_locality_groups == NULL) {
  2329. clear_opt(sbi->s_mount_opt, MBALLOC);
  2330. kfree(sbi->s_mb_offsets);
  2331. kfree(sbi->s_mb_maxs);
  2332. return -ENOMEM;
  2333. }
  2334. for (i = 0; i < NR_CPUS; i++) {
  2335. struct ext4_locality_group *lg;
  2336. lg = &sbi->s_locality_groups[i];
  2337. mutex_init(&lg->lg_mutex);
  2338. INIT_LIST_HEAD(&lg->lg_prealloc_list);
  2339. spin_lock_init(&lg->lg_prealloc_lock);
  2340. }
  2341. ext4_mb_init_per_dev_proc(sb);
  2342. ext4_mb_history_init(sb);
  2343. printk("EXT4-fs: mballoc enabled\n");
  2344. return 0;
  2345. }
  2346. /* need to called with ext4 group lock (ext4_lock_group) */
  2347. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2348. {
  2349. struct ext4_prealloc_space *pa;
  2350. struct list_head *cur, *tmp;
  2351. int count = 0;
  2352. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2353. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2354. list_del(&pa->pa_group_list);
  2355. count++;
  2356. kfree(pa);
  2357. }
  2358. if (count)
  2359. mb_debug("mballoc: %u PAs left\n", count);
  2360. }
  2361. int ext4_mb_release(struct super_block *sb)
  2362. {
  2363. ext4_group_t i;
  2364. int num_meta_group_infos;
  2365. struct ext4_group_info *grinfo;
  2366. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2367. if (!test_opt(sb, MBALLOC))
  2368. return 0;
  2369. /* release freed, non-committed blocks */
  2370. spin_lock(&sbi->s_md_lock);
  2371. list_splice_init(&sbi->s_closed_transaction,
  2372. &sbi->s_committed_transaction);
  2373. list_splice_init(&sbi->s_active_transaction,
  2374. &sbi->s_committed_transaction);
  2375. spin_unlock(&sbi->s_md_lock);
  2376. ext4_mb_free_committed_blocks(sb);
  2377. if (sbi->s_group_info) {
  2378. for (i = 0; i < sbi->s_groups_count; i++) {
  2379. grinfo = ext4_get_group_info(sb, i);
  2380. #ifdef DOUBLE_CHECK
  2381. kfree(grinfo->bb_bitmap);
  2382. #endif
  2383. ext4_lock_group(sb, i);
  2384. ext4_mb_cleanup_pa(grinfo);
  2385. ext4_unlock_group(sb, i);
  2386. kfree(grinfo);
  2387. }
  2388. num_meta_group_infos = (sbi->s_groups_count +
  2389. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2390. EXT4_DESC_PER_BLOCK_BITS(sb);
  2391. for (i = 0; i < num_meta_group_infos; i++)
  2392. kfree(sbi->s_group_info[i]);
  2393. kfree(sbi->s_group_info);
  2394. }
  2395. kfree(sbi->s_mb_offsets);
  2396. kfree(sbi->s_mb_maxs);
  2397. if (sbi->s_buddy_cache)
  2398. iput(sbi->s_buddy_cache);
  2399. if (sbi->s_mb_stats) {
  2400. printk(KERN_INFO
  2401. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2402. atomic_read(&sbi->s_bal_allocated),
  2403. atomic_read(&sbi->s_bal_reqs),
  2404. atomic_read(&sbi->s_bal_success));
  2405. printk(KERN_INFO
  2406. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2407. "%u 2^N hits, %u breaks, %u lost\n",
  2408. atomic_read(&sbi->s_bal_ex_scanned),
  2409. atomic_read(&sbi->s_bal_goals),
  2410. atomic_read(&sbi->s_bal_2orders),
  2411. atomic_read(&sbi->s_bal_breaks),
  2412. atomic_read(&sbi->s_mb_lost_chunks));
  2413. printk(KERN_INFO
  2414. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2415. sbi->s_mb_buddies_generated++,
  2416. sbi->s_mb_generation_time);
  2417. printk(KERN_INFO
  2418. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2419. atomic_read(&sbi->s_mb_preallocated),
  2420. atomic_read(&sbi->s_mb_discarded));
  2421. }
  2422. kfree(sbi->s_locality_groups);
  2423. ext4_mb_history_release(sb);
  2424. ext4_mb_destroy_per_dev_proc(sb);
  2425. return 0;
  2426. }
  2427. static void ext4_mb_free_committed_blocks(struct super_block *sb)
  2428. {
  2429. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2430. int err;
  2431. int i;
  2432. int count = 0;
  2433. int count2 = 0;
  2434. struct ext4_free_metadata *md;
  2435. struct ext4_buddy e4b;
  2436. if (list_empty(&sbi->s_committed_transaction))
  2437. return;
  2438. /* there is committed blocks to be freed yet */
  2439. do {
  2440. /* get next array of blocks */
  2441. md = NULL;
  2442. spin_lock(&sbi->s_md_lock);
  2443. if (!list_empty(&sbi->s_committed_transaction)) {
  2444. md = list_entry(sbi->s_committed_transaction.next,
  2445. struct ext4_free_metadata, list);
  2446. list_del(&md->list);
  2447. }
  2448. spin_unlock(&sbi->s_md_lock);
  2449. if (md == NULL)
  2450. break;
  2451. mb_debug("gonna free %u blocks in group %lu (0x%p):",
  2452. md->num, md->group, md);
  2453. err = ext4_mb_load_buddy(sb, md->group, &e4b);
  2454. /* we expect to find existing buddy because it's pinned */
  2455. BUG_ON(err != 0);
  2456. /* there are blocks to put in buddy to make them really free */
  2457. count += md->num;
  2458. count2++;
  2459. ext4_lock_group(sb, md->group);
  2460. for (i = 0; i < md->num; i++) {
  2461. mb_debug(" %u", md->blocks[i]);
  2462. err = mb_free_blocks(NULL, &e4b, md->blocks[i], 1);
  2463. BUG_ON(err != 0);
  2464. }
  2465. mb_debug("\n");
  2466. ext4_unlock_group(sb, md->group);
  2467. /* balance refcounts from ext4_mb_free_metadata() */
  2468. page_cache_release(e4b.bd_buddy_page);
  2469. page_cache_release(e4b.bd_bitmap_page);
  2470. kfree(md);
  2471. ext4_mb_release_desc(&e4b);
  2472. } while (md);
  2473. mb_debug("freed %u blocks in %u structures\n", count, count2);
  2474. }
  2475. #define EXT4_ROOT "ext4"
  2476. #define EXT4_MB_STATS_NAME "stats"
  2477. #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
  2478. #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
  2479. #define EXT4_MB_ORDER2_REQ "order2_req"
  2480. #define EXT4_MB_STREAM_REQ "stream_req"
  2481. #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
  2482. #define MB_PROC_VALUE_READ(name) \
  2483. static int ext4_mb_read_##name(char *page, char **start, \
  2484. off_t off, int count, int *eof, void *data) \
  2485. { \
  2486. struct ext4_sb_info *sbi = data; \
  2487. int len; \
  2488. *eof = 1; \
  2489. if (off != 0) \
  2490. return 0; \
  2491. len = sprintf(page, "%ld\n", sbi->s_mb_##name); \
  2492. *start = page; \
  2493. return len; \
  2494. }
  2495. #define MB_PROC_VALUE_WRITE(name) \
  2496. static int ext4_mb_write_##name(struct file *file, \
  2497. const char __user *buf, unsigned long cnt, void *data) \
  2498. { \
  2499. struct ext4_sb_info *sbi = data; \
  2500. char str[32]; \
  2501. long value; \
  2502. if (cnt >= sizeof(str)) \
  2503. return -EINVAL; \
  2504. if (copy_from_user(str, buf, cnt)) \
  2505. return -EFAULT; \
  2506. value = simple_strtol(str, NULL, 0); \
  2507. if (value <= 0) \
  2508. return -ERANGE; \
  2509. sbi->s_mb_##name = value; \
  2510. return cnt; \
  2511. }
  2512. MB_PROC_VALUE_READ(stats);
  2513. MB_PROC_VALUE_WRITE(stats);
  2514. MB_PROC_VALUE_READ(max_to_scan);
  2515. MB_PROC_VALUE_WRITE(max_to_scan);
  2516. MB_PROC_VALUE_READ(min_to_scan);
  2517. MB_PROC_VALUE_WRITE(min_to_scan);
  2518. MB_PROC_VALUE_READ(order2_reqs);
  2519. MB_PROC_VALUE_WRITE(order2_reqs);
  2520. MB_PROC_VALUE_READ(stream_request);
  2521. MB_PROC_VALUE_WRITE(stream_request);
  2522. MB_PROC_VALUE_READ(group_prealloc);
  2523. MB_PROC_VALUE_WRITE(group_prealloc);
  2524. #define MB_PROC_HANDLER(name, var) \
  2525. do { \
  2526. proc = create_proc_entry(name, mode, sbi->s_mb_proc); \
  2527. if (proc == NULL) { \
  2528. printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
  2529. goto err_out; \
  2530. } \
  2531. proc->data = sbi; \
  2532. proc->read_proc = ext4_mb_read_##var ; \
  2533. proc->write_proc = ext4_mb_write_##var; \
  2534. } while (0)
  2535. static int ext4_mb_init_per_dev_proc(struct super_block *sb)
  2536. {
  2537. mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
  2538. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2539. struct proc_dir_entry *proc;
  2540. char devname[64];
  2541. snprintf(devname, sizeof(devname) - 1, "%s",
  2542. bdevname(sb->s_bdev, devname));
  2543. sbi->s_mb_proc = proc_mkdir(devname, proc_root_ext4);
  2544. MB_PROC_HANDLER(EXT4_MB_STATS_NAME, stats);
  2545. MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, max_to_scan);
  2546. MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, min_to_scan);
  2547. MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ, order2_reqs);
  2548. MB_PROC_HANDLER(EXT4_MB_STREAM_REQ, stream_request);
  2549. MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, group_prealloc);
  2550. return 0;
  2551. err_out:
  2552. printk(KERN_ERR "EXT4-fs: Unable to create %s\n", devname);
  2553. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
  2554. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
  2555. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
  2556. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
  2557. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
  2558. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_mb_proc);
  2559. remove_proc_entry(devname, proc_root_ext4);
  2560. sbi->s_mb_proc = NULL;
  2561. return -ENOMEM;
  2562. }
  2563. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
  2564. {
  2565. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2566. char devname[64];
  2567. if (sbi->s_mb_proc == NULL)
  2568. return -EINVAL;
  2569. snprintf(devname, sizeof(devname) - 1, "%s",
  2570. bdevname(sb->s_bdev, devname));
  2571. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_mb_proc);
  2572. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_mb_proc);
  2573. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_mb_proc);
  2574. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
  2575. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
  2576. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_mb_proc);
  2577. remove_proc_entry(devname, proc_root_ext4);
  2578. return 0;
  2579. }
  2580. int __init init_ext4_mballoc(void)
  2581. {
  2582. ext4_pspace_cachep =
  2583. kmem_cache_create("ext4_prealloc_space",
  2584. sizeof(struct ext4_prealloc_space),
  2585. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2586. if (ext4_pspace_cachep == NULL)
  2587. return -ENOMEM;
  2588. ext4_ac_cachep =
  2589. kmem_cache_create("ext4_alloc_context",
  2590. sizeof(struct ext4_allocation_context),
  2591. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2592. if (ext4_ac_cachep == NULL) {
  2593. kmem_cache_destroy(ext4_pspace_cachep);
  2594. return -ENOMEM;
  2595. }
  2596. #ifdef CONFIG_PROC_FS
  2597. proc_root_ext4 = proc_mkdir(EXT4_ROOT, proc_root_fs);
  2598. if (proc_root_ext4 == NULL)
  2599. printk(KERN_ERR "EXT4-fs: Unable to create %s\n", EXT4_ROOT);
  2600. #endif
  2601. return 0;
  2602. }
  2603. void exit_ext4_mballoc(void)
  2604. {
  2605. /* XXX: synchronize_rcu(); */
  2606. kmem_cache_destroy(ext4_pspace_cachep);
  2607. kmem_cache_destroy(ext4_ac_cachep);
  2608. #ifdef CONFIG_PROC_FS
  2609. remove_proc_entry(EXT4_ROOT, proc_root_fs);
  2610. #endif
  2611. }
  2612. /*
  2613. * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
  2614. * Returns 0 if success or error code
  2615. */
  2616. static int ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2617. handle_t *handle)
  2618. {
  2619. struct buffer_head *bitmap_bh = NULL;
  2620. struct ext4_super_block *es;
  2621. struct ext4_group_desc *gdp;
  2622. struct buffer_head *gdp_bh;
  2623. struct ext4_sb_info *sbi;
  2624. struct super_block *sb;
  2625. ext4_fsblk_t block;
  2626. int err;
  2627. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2628. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2629. sb = ac->ac_sb;
  2630. sbi = EXT4_SB(sb);
  2631. es = sbi->s_es;
  2632. ext4_debug("using block group %lu(%d)\n", ac->ac_b_ex.fe_group,
  2633. gdp->bg_free_blocks_count);
  2634. err = -EIO;
  2635. bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2636. if (!bitmap_bh)
  2637. goto out_err;
  2638. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2639. if (err)
  2640. goto out_err;
  2641. err = -EIO;
  2642. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2643. if (!gdp)
  2644. goto out_err;
  2645. err = ext4_journal_get_write_access(handle, gdp_bh);
  2646. if (err)
  2647. goto out_err;
  2648. block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  2649. + ac->ac_b_ex.fe_start
  2650. + le32_to_cpu(es->s_first_data_block);
  2651. if (block == ext4_block_bitmap(sb, gdp) ||
  2652. block == ext4_inode_bitmap(sb, gdp) ||
  2653. in_range(block, ext4_inode_table(sb, gdp),
  2654. EXT4_SB(sb)->s_itb_per_group)) {
  2655. ext4_error(sb, __FUNCTION__,
  2656. "Allocating block in system zone - block = %llu",
  2657. block);
  2658. }
  2659. #ifdef AGGRESSIVE_CHECK
  2660. {
  2661. int i;
  2662. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2663. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2664. bitmap_bh->b_data));
  2665. }
  2666. }
  2667. #endif
  2668. mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
  2669. ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
  2670. spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2671. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2672. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2673. gdp->bg_free_blocks_count =
  2674. cpu_to_le16(ext4_free_blocks_after_init(sb,
  2675. ac->ac_b_ex.fe_group,
  2676. gdp));
  2677. }
  2678. gdp->bg_free_blocks_count =
  2679. cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
  2680. - ac->ac_b_ex.fe_len);
  2681. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2682. spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2683. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2684. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  2685. if (err)
  2686. goto out_err;
  2687. err = ext4_journal_dirty_metadata(handle, gdp_bh);
  2688. out_err:
  2689. sb->s_dirt = 1;
  2690. brelse(bitmap_bh);
  2691. return err;
  2692. }
  2693. /*
  2694. * here we normalize request for locality group
  2695. * Group request are normalized to s_strip size if we set the same via mount
  2696. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2697. * /proc/fs/ext4/<partition>/group_prealloc
  2698. *
  2699. * XXX: should we try to preallocate more than the group has now?
  2700. */
  2701. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2702. {
  2703. struct super_block *sb = ac->ac_sb;
  2704. struct ext4_locality_group *lg = ac->ac_lg;
  2705. BUG_ON(lg == NULL);
  2706. if (EXT4_SB(sb)->s_stripe)
  2707. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2708. else
  2709. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2710. mb_debug("#%u: goal %lu blocks for locality group\n",
  2711. current->pid, ac->ac_g_ex.fe_len);
  2712. }
  2713. /*
  2714. * Normalization means making request better in terms of
  2715. * size and alignment
  2716. */
  2717. static void ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2718. struct ext4_allocation_request *ar)
  2719. {
  2720. int bsbits, max;
  2721. ext4_lblk_t end;
  2722. struct list_head *cur;
  2723. loff_t size, orig_size, start_off;
  2724. ext4_lblk_t start, orig_start;
  2725. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2726. /* do normalize only data requests, metadata requests
  2727. do not need preallocation */
  2728. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2729. return;
  2730. /* sometime caller may want exact blocks */
  2731. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2732. return;
  2733. /* caller may indicate that preallocation isn't
  2734. * required (it's a tail, for example) */
  2735. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2736. return;
  2737. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2738. ext4_mb_normalize_group_request(ac);
  2739. return ;
  2740. }
  2741. bsbits = ac->ac_sb->s_blocksize_bits;
  2742. /* first, let's learn actual file size
  2743. * given current request is allocated */
  2744. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2745. size = size << bsbits;
  2746. if (size < i_size_read(ac->ac_inode))
  2747. size = i_size_read(ac->ac_inode);
  2748. /* max available blocks in a free group */
  2749. max = EXT4_BLOCKS_PER_GROUP(ac->ac_sb) - 1 - 1 -
  2750. EXT4_SB(ac->ac_sb)->s_itb_per_group;
  2751. #define NRL_CHECK_SIZE(req, size, max,bits) \
  2752. (req <= (size) || max <= ((size) >> bits))
  2753. /* first, try to predict filesize */
  2754. /* XXX: should this table be tunable? */
  2755. start_off = 0;
  2756. if (size <= 16 * 1024) {
  2757. size = 16 * 1024;
  2758. } else if (size <= 32 * 1024) {
  2759. size = 32 * 1024;
  2760. } else if (size <= 64 * 1024) {
  2761. size = 64 * 1024;
  2762. } else if (size <= 128 * 1024) {
  2763. size = 128 * 1024;
  2764. } else if (size <= 256 * 1024) {
  2765. size = 256 * 1024;
  2766. } else if (size <= 512 * 1024) {
  2767. size = 512 * 1024;
  2768. } else if (size <= 1024 * 1024) {
  2769. size = 1024 * 1024;
  2770. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, bsbits)) {
  2771. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2772. (20 - bsbits)) << 20;
  2773. size = 1024 * 1024;
  2774. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, bsbits)) {
  2775. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2776. (22 - bsbits)) << 22;
  2777. size = 4 * 1024 * 1024;
  2778. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2779. (8<<20)>>bsbits, max, bsbits)) {
  2780. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2781. (23 - bsbits)) << 23;
  2782. size = 8 * 1024 * 1024;
  2783. } else {
  2784. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2785. size = ac->ac_o_ex.fe_len << bsbits;
  2786. }
  2787. orig_size = size = size >> bsbits;
  2788. orig_start = start = start_off >> bsbits;
  2789. /* don't cover already allocated blocks in selected range */
  2790. if (ar->pleft && start <= ar->lleft) {
  2791. size -= ar->lleft + 1 - start;
  2792. start = ar->lleft + 1;
  2793. }
  2794. if (ar->pright && start + size - 1 >= ar->lright)
  2795. size -= start + size - ar->lright;
  2796. end = start + size;
  2797. /* check we don't cross already preallocated blocks */
  2798. rcu_read_lock();
  2799. list_for_each_rcu(cur, &ei->i_prealloc_list) {
  2800. struct ext4_prealloc_space *pa;
  2801. unsigned long pa_end;
  2802. pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
  2803. if (pa->pa_deleted)
  2804. continue;
  2805. spin_lock(&pa->pa_lock);
  2806. if (pa->pa_deleted) {
  2807. spin_unlock(&pa->pa_lock);
  2808. continue;
  2809. }
  2810. pa_end = pa->pa_lstart + pa->pa_len;
  2811. /* PA must not overlap original request */
  2812. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2813. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2814. /* skip PA normalized request doesn't overlap with */
  2815. if (pa->pa_lstart >= end) {
  2816. spin_unlock(&pa->pa_lock);
  2817. continue;
  2818. }
  2819. if (pa_end <= start) {
  2820. spin_unlock(&pa->pa_lock);
  2821. continue;
  2822. }
  2823. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2824. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2825. BUG_ON(pa_end < start);
  2826. start = pa_end;
  2827. }
  2828. if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2829. BUG_ON(pa->pa_lstart > end);
  2830. end = pa->pa_lstart;
  2831. }
  2832. spin_unlock(&pa->pa_lock);
  2833. }
  2834. rcu_read_unlock();
  2835. size = end - start;
  2836. /* XXX: extra loop to check we really don't overlap preallocations */
  2837. rcu_read_lock();
  2838. list_for_each_rcu(cur, &ei->i_prealloc_list) {
  2839. struct ext4_prealloc_space *pa;
  2840. unsigned long pa_end;
  2841. pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
  2842. spin_lock(&pa->pa_lock);
  2843. if (pa->pa_deleted == 0) {
  2844. pa_end = pa->pa_lstart + pa->pa_len;
  2845. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2846. }
  2847. spin_unlock(&pa->pa_lock);
  2848. }
  2849. rcu_read_unlock();
  2850. if (start + size <= ac->ac_o_ex.fe_logical &&
  2851. start > ac->ac_o_ex.fe_logical) {
  2852. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2853. (unsigned long) start, (unsigned long) size,
  2854. (unsigned long) ac->ac_o_ex.fe_logical);
  2855. }
  2856. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2857. start > ac->ac_o_ex.fe_logical);
  2858. BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2859. /* now prepare goal request */
  2860. /* XXX: is it better to align blocks WRT to logical
  2861. * placement or satisfy big request as is */
  2862. ac->ac_g_ex.fe_logical = start;
  2863. ac->ac_g_ex.fe_len = size;
  2864. /* define goal start in order to merge */
  2865. if (ar->pright && (ar->lright == (start + size))) {
  2866. /* merge to the right */
  2867. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2868. &ac->ac_f_ex.fe_group,
  2869. &ac->ac_f_ex.fe_start);
  2870. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2871. }
  2872. if (ar->pleft && (ar->lleft + 1 == start)) {
  2873. /* merge to the left */
  2874. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2875. &ac->ac_f_ex.fe_group,
  2876. &ac->ac_f_ex.fe_start);
  2877. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2878. }
  2879. mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2880. (unsigned) orig_size, (unsigned) start);
  2881. }
  2882. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2883. {
  2884. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2885. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2886. atomic_inc(&sbi->s_bal_reqs);
  2887. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2888. if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
  2889. atomic_inc(&sbi->s_bal_success);
  2890. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2891. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2892. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2893. atomic_inc(&sbi->s_bal_goals);
  2894. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2895. atomic_inc(&sbi->s_bal_breaks);
  2896. }
  2897. ext4_mb_store_history(ac);
  2898. }
  2899. /*
  2900. * use blocks preallocated to inode
  2901. */
  2902. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2903. struct ext4_prealloc_space *pa)
  2904. {
  2905. ext4_fsblk_t start;
  2906. ext4_fsblk_t end;
  2907. int len;
  2908. /* found preallocated blocks, use them */
  2909. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2910. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2911. len = end - start;
  2912. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2913. &ac->ac_b_ex.fe_start);
  2914. ac->ac_b_ex.fe_len = len;
  2915. ac->ac_status = AC_STATUS_FOUND;
  2916. ac->ac_pa = pa;
  2917. BUG_ON(start < pa->pa_pstart);
  2918. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2919. BUG_ON(pa->pa_free < len);
  2920. pa->pa_free -= len;
  2921. mb_debug("use %llu/%lu from inode pa %p\n", start, len, pa);
  2922. }
  2923. /*
  2924. * use blocks preallocated to locality group
  2925. */
  2926. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2927. struct ext4_prealloc_space *pa)
  2928. {
  2929. unsigned len = ac->ac_o_ex.fe_len;
  2930. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2931. &ac->ac_b_ex.fe_group,
  2932. &ac->ac_b_ex.fe_start);
  2933. ac->ac_b_ex.fe_len = len;
  2934. ac->ac_status = AC_STATUS_FOUND;
  2935. ac->ac_pa = pa;
  2936. /* we don't correct pa_pstart or pa_plen here to avoid
  2937. * possible race when tte group is being loaded concurrently
  2938. * instead we correct pa later, after blocks are marked
  2939. * in on-disk bitmap -- see ext4_mb_release_context() */
  2940. /*
  2941. * FIXME!! but the other CPUs can look at this particular
  2942. * pa and think that it have enought free blocks if we
  2943. * don't update pa_free here right ?
  2944. */
  2945. mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2946. }
  2947. /*
  2948. * search goal blocks in preallocated space
  2949. */
  2950. static int ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2951. {
  2952. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2953. struct ext4_locality_group *lg;
  2954. struct ext4_prealloc_space *pa;
  2955. struct list_head *cur;
  2956. /* only data can be preallocated */
  2957. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2958. return 0;
  2959. /* first, try per-file preallocation */
  2960. rcu_read_lock();
  2961. list_for_each_rcu(cur, &ei->i_prealloc_list) {
  2962. pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
  2963. /* all fields in this condition don't change,
  2964. * so we can skip locking for them */
  2965. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2966. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  2967. continue;
  2968. /* found preallocated blocks, use them */
  2969. spin_lock(&pa->pa_lock);
  2970. if (pa->pa_deleted == 0 && pa->pa_free) {
  2971. atomic_inc(&pa->pa_count);
  2972. ext4_mb_use_inode_pa(ac, pa);
  2973. spin_unlock(&pa->pa_lock);
  2974. ac->ac_criteria = 10;
  2975. rcu_read_unlock();
  2976. return 1;
  2977. }
  2978. spin_unlock(&pa->pa_lock);
  2979. }
  2980. rcu_read_unlock();
  2981. /* can we use group allocation? */
  2982. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  2983. return 0;
  2984. /* inode may have no locality group for some reason */
  2985. lg = ac->ac_lg;
  2986. if (lg == NULL)
  2987. return 0;
  2988. rcu_read_lock();
  2989. list_for_each_rcu(cur, &lg->lg_prealloc_list) {
  2990. pa = list_entry(cur, struct ext4_prealloc_space, pa_inode_list);
  2991. spin_lock(&pa->pa_lock);
  2992. if (pa->pa_deleted == 0 && pa->pa_free >= ac->ac_o_ex.fe_len) {
  2993. atomic_inc(&pa->pa_count);
  2994. ext4_mb_use_group_pa(ac, pa);
  2995. spin_unlock(&pa->pa_lock);
  2996. ac->ac_criteria = 20;
  2997. rcu_read_unlock();
  2998. return 1;
  2999. }
  3000. spin_unlock(&pa->pa_lock);
  3001. }
  3002. rcu_read_unlock();
  3003. return 0;
  3004. }
  3005. /*
  3006. * the function goes through all preallocation in this group and marks them
  3007. * used in in-core bitmap. buddy must be generated from this bitmap
  3008. * Need to be called with ext4 group lock (ext4_lock_group)
  3009. */
  3010. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  3011. ext4_group_t group)
  3012. {
  3013. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3014. struct ext4_prealloc_space *pa;
  3015. struct list_head *cur;
  3016. ext4_group_t groupnr;
  3017. ext4_grpblk_t start;
  3018. int preallocated = 0;
  3019. int count = 0;
  3020. int len;
  3021. /* all form of preallocation discards first load group,
  3022. * so the only competing code is preallocation use.
  3023. * we don't need any locking here
  3024. * notice we do NOT ignore preallocations with pa_deleted
  3025. * otherwise we could leave used blocks available for
  3026. * allocation in buddy when concurrent ext4_mb_put_pa()
  3027. * is dropping preallocation
  3028. */
  3029. list_for_each(cur, &grp->bb_prealloc_list) {
  3030. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  3031. spin_lock(&pa->pa_lock);
  3032. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3033. &groupnr, &start);
  3034. len = pa->pa_len;
  3035. spin_unlock(&pa->pa_lock);
  3036. if (unlikely(len == 0))
  3037. continue;
  3038. BUG_ON(groupnr != group);
  3039. mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
  3040. bitmap, start, len);
  3041. preallocated += len;
  3042. count++;
  3043. }
  3044. mb_debug("prellocated %u for group %lu\n", preallocated, group);
  3045. }
  3046. static void ext4_mb_pa_callback(struct rcu_head *head)
  3047. {
  3048. struct ext4_prealloc_space *pa;
  3049. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  3050. kmem_cache_free(ext4_pspace_cachep, pa);
  3051. }
  3052. /*
  3053. * drops a reference to preallocated space descriptor
  3054. * if this was the last reference and the space is consumed
  3055. */
  3056. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  3057. struct super_block *sb, struct ext4_prealloc_space *pa)
  3058. {
  3059. unsigned long grp;
  3060. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  3061. return;
  3062. /* in this short window concurrent discard can set pa_deleted */
  3063. spin_lock(&pa->pa_lock);
  3064. if (pa->pa_deleted == 1) {
  3065. spin_unlock(&pa->pa_lock);
  3066. return;
  3067. }
  3068. pa->pa_deleted = 1;
  3069. spin_unlock(&pa->pa_lock);
  3070. /* -1 is to protect from crossing allocation group */
  3071. ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
  3072. /*
  3073. * possible race:
  3074. *
  3075. * P1 (buddy init) P2 (regular allocation)
  3076. * find block B in PA
  3077. * copy on-disk bitmap to buddy
  3078. * mark B in on-disk bitmap
  3079. * drop PA from group
  3080. * mark all PAs in buddy
  3081. *
  3082. * thus, P1 initializes buddy with B available. to prevent this
  3083. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  3084. * against that pair
  3085. */
  3086. ext4_lock_group(sb, grp);
  3087. list_del(&pa->pa_group_list);
  3088. ext4_unlock_group(sb, grp);
  3089. spin_lock(pa->pa_obj_lock);
  3090. list_del_rcu(&pa->pa_inode_list);
  3091. spin_unlock(pa->pa_obj_lock);
  3092. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3093. }
  3094. /*
  3095. * creates new preallocated space for given inode
  3096. */
  3097. static int ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3098. {
  3099. struct super_block *sb = ac->ac_sb;
  3100. struct ext4_prealloc_space *pa;
  3101. struct ext4_group_info *grp;
  3102. struct ext4_inode_info *ei;
  3103. /* preallocate only when found space is larger then requested */
  3104. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3105. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3106. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3107. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3108. if (pa == NULL)
  3109. return -ENOMEM;
  3110. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3111. int winl;
  3112. int wins;
  3113. int win;
  3114. int offs;
  3115. /* we can't allocate as much as normalizer wants.
  3116. * so, found space must get proper lstart
  3117. * to cover original request */
  3118. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3119. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3120. /* we're limited by original request in that
  3121. * logical block must be covered any way
  3122. * winl is window we can move our chunk within */
  3123. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3124. /* also, we should cover whole original request */
  3125. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  3126. /* the smallest one defines real window */
  3127. win = min(winl, wins);
  3128. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  3129. if (offs && offs < win)
  3130. win = offs;
  3131. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  3132. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3133. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3134. }
  3135. /* preallocation can change ac_b_ex, thus we store actually
  3136. * allocated blocks for history */
  3137. ac->ac_f_ex = ac->ac_b_ex;
  3138. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3139. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3140. pa->pa_len = ac->ac_b_ex.fe_len;
  3141. pa->pa_free = pa->pa_len;
  3142. atomic_set(&pa->pa_count, 1);
  3143. spin_lock_init(&pa->pa_lock);
  3144. pa->pa_deleted = 0;
  3145. pa->pa_linear = 0;
  3146. mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
  3147. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3148. ext4_mb_use_inode_pa(ac, pa);
  3149. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3150. ei = EXT4_I(ac->ac_inode);
  3151. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3152. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3153. pa->pa_inode = ac->ac_inode;
  3154. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3155. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3156. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3157. spin_lock(pa->pa_obj_lock);
  3158. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3159. spin_unlock(pa->pa_obj_lock);
  3160. return 0;
  3161. }
  3162. /*
  3163. * creates new preallocated space for locality group inodes belongs to
  3164. */
  3165. static int ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3166. {
  3167. struct super_block *sb = ac->ac_sb;
  3168. struct ext4_locality_group *lg;
  3169. struct ext4_prealloc_space *pa;
  3170. struct ext4_group_info *grp;
  3171. /* preallocate only when found space is larger then requested */
  3172. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3173. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3174. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3175. BUG_ON(ext4_pspace_cachep == NULL);
  3176. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3177. if (pa == NULL)
  3178. return -ENOMEM;
  3179. /* preallocation can change ac_b_ex, thus we store actually
  3180. * allocated blocks for history */
  3181. ac->ac_f_ex = ac->ac_b_ex;
  3182. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3183. pa->pa_lstart = pa->pa_pstart;
  3184. pa->pa_len = ac->ac_b_ex.fe_len;
  3185. pa->pa_free = pa->pa_len;
  3186. atomic_set(&pa->pa_count, 1);
  3187. spin_lock_init(&pa->pa_lock);
  3188. pa->pa_deleted = 0;
  3189. pa->pa_linear = 1;
  3190. mb_debug("new group pa %p: %llu/%u for %u\n", pa,
  3191. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3192. ext4_mb_use_group_pa(ac, pa);
  3193. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3194. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3195. lg = ac->ac_lg;
  3196. BUG_ON(lg == NULL);
  3197. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3198. pa->pa_inode = NULL;
  3199. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3200. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3201. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3202. spin_lock(pa->pa_obj_lock);
  3203. list_add_tail_rcu(&pa->pa_inode_list, &lg->lg_prealloc_list);
  3204. spin_unlock(pa->pa_obj_lock);
  3205. return 0;
  3206. }
  3207. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3208. {
  3209. int err;
  3210. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3211. err = ext4_mb_new_group_pa(ac);
  3212. else
  3213. err = ext4_mb_new_inode_pa(ac);
  3214. return err;
  3215. }
  3216. /*
  3217. * finds all unused blocks in on-disk bitmap, frees them in
  3218. * in-core bitmap and buddy.
  3219. * @pa must be unlinked from inode and group lists, so that
  3220. * nobody else can find/use it.
  3221. * the caller MUST hold group/inode locks.
  3222. * TODO: optimize the case when there are no in-core structures yet
  3223. */
  3224. static int ext4_mb_release_inode_pa(struct ext4_buddy *e4b,
  3225. struct buffer_head *bitmap_bh,
  3226. struct ext4_prealloc_space *pa)
  3227. {
  3228. struct ext4_allocation_context *ac;
  3229. struct super_block *sb = e4b->bd_sb;
  3230. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3231. unsigned long end;
  3232. unsigned long next;
  3233. ext4_group_t group;
  3234. ext4_grpblk_t bit;
  3235. sector_t start;
  3236. int err = 0;
  3237. int free = 0;
  3238. BUG_ON(pa->pa_deleted == 0);
  3239. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3240. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3241. end = bit + pa->pa_len;
  3242. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3243. if (ac) {
  3244. ac->ac_sb = sb;
  3245. ac->ac_inode = pa->pa_inode;
  3246. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3247. }
  3248. while (bit < end) {
  3249. bit = ext4_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3250. if (bit >= end)
  3251. break;
  3252. next = ext4_find_next_bit(bitmap_bh->b_data, end, bit);
  3253. if (next > end)
  3254. next = end;
  3255. start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
  3256. le32_to_cpu(sbi->s_es->s_first_data_block);
  3257. mb_debug(" free preallocated %u/%u in group %u\n",
  3258. (unsigned) start, (unsigned) next - bit,
  3259. (unsigned) group);
  3260. free += next - bit;
  3261. if (ac) {
  3262. ac->ac_b_ex.fe_group = group;
  3263. ac->ac_b_ex.fe_start = bit;
  3264. ac->ac_b_ex.fe_len = next - bit;
  3265. ac->ac_b_ex.fe_logical = 0;
  3266. ext4_mb_store_history(ac);
  3267. }
  3268. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3269. bit = next + 1;
  3270. }
  3271. if (free != pa->pa_free) {
  3272. printk(KERN_ERR "pa %p: logic %lu, phys. %lu, len %lu\n",
  3273. pa, (unsigned long) pa->pa_lstart,
  3274. (unsigned long) pa->pa_pstart,
  3275. (unsigned long) pa->pa_len);
  3276. printk(KERN_ERR "free %u, pa_free %u\n", free, pa->pa_free);
  3277. }
  3278. BUG_ON(free != pa->pa_free);
  3279. atomic_add(free, &sbi->s_mb_discarded);
  3280. if (ac)
  3281. kmem_cache_free(ext4_ac_cachep, ac);
  3282. return err;
  3283. }
  3284. static int ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3285. struct ext4_prealloc_space *pa)
  3286. {
  3287. struct ext4_allocation_context *ac;
  3288. struct super_block *sb = e4b->bd_sb;
  3289. ext4_group_t group;
  3290. ext4_grpblk_t bit;
  3291. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3292. if (ac)
  3293. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3294. BUG_ON(pa->pa_deleted == 0);
  3295. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3296. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3297. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3298. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3299. if (ac) {
  3300. ac->ac_sb = sb;
  3301. ac->ac_inode = NULL;
  3302. ac->ac_b_ex.fe_group = group;
  3303. ac->ac_b_ex.fe_start = bit;
  3304. ac->ac_b_ex.fe_len = pa->pa_len;
  3305. ac->ac_b_ex.fe_logical = 0;
  3306. ext4_mb_store_history(ac);
  3307. kmem_cache_free(ext4_ac_cachep, ac);
  3308. }
  3309. return 0;
  3310. }
  3311. /*
  3312. * releases all preallocations in given group
  3313. *
  3314. * first, we need to decide discard policy:
  3315. * - when do we discard
  3316. * 1) ENOSPC
  3317. * - how many do we discard
  3318. * 1) how many requested
  3319. */
  3320. static int ext4_mb_discard_group_preallocations(struct super_block *sb,
  3321. ext4_group_t group, int needed)
  3322. {
  3323. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3324. struct buffer_head *bitmap_bh = NULL;
  3325. struct ext4_prealloc_space *pa, *tmp;
  3326. struct list_head list;
  3327. struct ext4_buddy e4b;
  3328. int err;
  3329. int busy = 0;
  3330. int free = 0;
  3331. mb_debug("discard preallocation for group %lu\n", group);
  3332. if (list_empty(&grp->bb_prealloc_list))
  3333. return 0;
  3334. bitmap_bh = read_block_bitmap(sb, group);
  3335. if (bitmap_bh == NULL) {
  3336. /* error handling here */
  3337. ext4_mb_release_desc(&e4b);
  3338. BUG_ON(bitmap_bh == NULL);
  3339. }
  3340. err = ext4_mb_load_buddy(sb, group, &e4b);
  3341. BUG_ON(err != 0); /* error handling here */
  3342. if (needed == 0)
  3343. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3344. grp = ext4_get_group_info(sb, group);
  3345. INIT_LIST_HEAD(&list);
  3346. repeat:
  3347. ext4_lock_group(sb, group);
  3348. list_for_each_entry_safe(pa, tmp,
  3349. &grp->bb_prealloc_list, pa_group_list) {
  3350. spin_lock(&pa->pa_lock);
  3351. if (atomic_read(&pa->pa_count)) {
  3352. spin_unlock(&pa->pa_lock);
  3353. busy = 1;
  3354. continue;
  3355. }
  3356. if (pa->pa_deleted) {
  3357. spin_unlock(&pa->pa_lock);
  3358. continue;
  3359. }
  3360. /* seems this one can be freed ... */
  3361. pa->pa_deleted = 1;
  3362. /* we can trust pa_free ... */
  3363. free += pa->pa_free;
  3364. spin_unlock(&pa->pa_lock);
  3365. list_del(&pa->pa_group_list);
  3366. list_add(&pa->u.pa_tmp_list, &list);
  3367. }
  3368. /* if we still need more blocks and some PAs were used, try again */
  3369. if (free < needed && busy) {
  3370. busy = 0;
  3371. ext4_unlock_group(sb, group);
  3372. /*
  3373. * Yield the CPU here so that we don't get soft lockup
  3374. * in non preempt case.
  3375. */
  3376. yield();
  3377. goto repeat;
  3378. }
  3379. /* found anything to free? */
  3380. if (list_empty(&list)) {
  3381. BUG_ON(free != 0);
  3382. goto out;
  3383. }
  3384. /* now free all selected PAs */
  3385. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3386. /* remove from object (inode or locality group) */
  3387. spin_lock(pa->pa_obj_lock);
  3388. list_del_rcu(&pa->pa_inode_list);
  3389. spin_unlock(pa->pa_obj_lock);
  3390. if (pa->pa_linear)
  3391. ext4_mb_release_group_pa(&e4b, pa);
  3392. else
  3393. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
  3394. list_del(&pa->u.pa_tmp_list);
  3395. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3396. }
  3397. out:
  3398. ext4_unlock_group(sb, group);
  3399. ext4_mb_release_desc(&e4b);
  3400. put_bh(bitmap_bh);
  3401. return free;
  3402. }
  3403. /*
  3404. * releases all non-used preallocated blocks for given inode
  3405. *
  3406. * It's important to discard preallocations under i_data_sem
  3407. * We don't want another block to be served from the prealloc
  3408. * space when we are discarding the inode prealloc space.
  3409. *
  3410. * FIXME!! Make sure it is valid at all the call sites
  3411. */
  3412. void ext4_mb_discard_inode_preallocations(struct inode *inode)
  3413. {
  3414. struct ext4_inode_info *ei = EXT4_I(inode);
  3415. struct super_block *sb = inode->i_sb;
  3416. struct buffer_head *bitmap_bh = NULL;
  3417. struct ext4_prealloc_space *pa, *tmp;
  3418. ext4_group_t group = 0;
  3419. struct list_head list;
  3420. struct ext4_buddy e4b;
  3421. int err;
  3422. if (!test_opt(sb, MBALLOC) || !S_ISREG(inode->i_mode)) {
  3423. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3424. return;
  3425. }
  3426. mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
  3427. INIT_LIST_HEAD(&list);
  3428. repeat:
  3429. /* first, collect all pa's in the inode */
  3430. spin_lock(&ei->i_prealloc_lock);
  3431. while (!list_empty(&ei->i_prealloc_list)) {
  3432. pa = list_entry(ei->i_prealloc_list.next,
  3433. struct ext4_prealloc_space, pa_inode_list);
  3434. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3435. spin_lock(&pa->pa_lock);
  3436. if (atomic_read(&pa->pa_count)) {
  3437. /* this shouldn't happen often - nobody should
  3438. * use preallocation while we're discarding it */
  3439. spin_unlock(&pa->pa_lock);
  3440. spin_unlock(&ei->i_prealloc_lock);
  3441. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3442. WARN_ON(1);
  3443. schedule_timeout_uninterruptible(HZ);
  3444. goto repeat;
  3445. }
  3446. if (pa->pa_deleted == 0) {
  3447. pa->pa_deleted = 1;
  3448. spin_unlock(&pa->pa_lock);
  3449. list_del_rcu(&pa->pa_inode_list);
  3450. list_add(&pa->u.pa_tmp_list, &list);
  3451. continue;
  3452. }
  3453. /* someone is deleting pa right now */
  3454. spin_unlock(&pa->pa_lock);
  3455. spin_unlock(&ei->i_prealloc_lock);
  3456. /* we have to wait here because pa_deleted
  3457. * doesn't mean pa is already unlinked from
  3458. * the list. as we might be called from
  3459. * ->clear_inode() the inode will get freed
  3460. * and concurrent thread which is unlinking
  3461. * pa from inode's list may access already
  3462. * freed memory, bad-bad-bad */
  3463. /* XXX: if this happens too often, we can
  3464. * add a flag to force wait only in case
  3465. * of ->clear_inode(), but not in case of
  3466. * regular truncate */
  3467. schedule_timeout_uninterruptible(HZ);
  3468. goto repeat;
  3469. }
  3470. spin_unlock(&ei->i_prealloc_lock);
  3471. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3472. BUG_ON(pa->pa_linear != 0);
  3473. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3474. err = ext4_mb_load_buddy(sb, group, &e4b);
  3475. BUG_ON(err != 0); /* error handling here */
  3476. bitmap_bh = read_block_bitmap(sb, group);
  3477. if (bitmap_bh == NULL) {
  3478. /* error handling here */
  3479. ext4_mb_release_desc(&e4b);
  3480. BUG_ON(bitmap_bh == NULL);
  3481. }
  3482. ext4_lock_group(sb, group);
  3483. list_del(&pa->pa_group_list);
  3484. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
  3485. ext4_unlock_group(sb, group);
  3486. ext4_mb_release_desc(&e4b);
  3487. put_bh(bitmap_bh);
  3488. list_del(&pa->u.pa_tmp_list);
  3489. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3490. }
  3491. }
  3492. /*
  3493. * finds all preallocated spaces and return blocks being freed to them
  3494. * if preallocated space becomes full (no block is used from the space)
  3495. * then the function frees space in buddy
  3496. * XXX: at the moment, truncate (which is the only way to free blocks)
  3497. * discards all preallocations
  3498. */
  3499. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3500. struct ext4_buddy *e4b,
  3501. sector_t block, int count)
  3502. {
  3503. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3504. }
  3505. #ifdef MB_DEBUG
  3506. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3507. {
  3508. struct super_block *sb = ac->ac_sb;
  3509. ext4_group_t i;
  3510. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3511. " Allocation context details:\n");
  3512. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3513. ac->ac_status, ac->ac_flags);
  3514. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3515. "best %lu/%lu/%lu@%lu cr %d\n",
  3516. (unsigned long)ac->ac_o_ex.fe_group,
  3517. (unsigned long)ac->ac_o_ex.fe_start,
  3518. (unsigned long)ac->ac_o_ex.fe_len,
  3519. (unsigned long)ac->ac_o_ex.fe_logical,
  3520. (unsigned long)ac->ac_g_ex.fe_group,
  3521. (unsigned long)ac->ac_g_ex.fe_start,
  3522. (unsigned long)ac->ac_g_ex.fe_len,
  3523. (unsigned long)ac->ac_g_ex.fe_logical,
  3524. (unsigned long)ac->ac_b_ex.fe_group,
  3525. (unsigned long)ac->ac_b_ex.fe_start,
  3526. (unsigned long)ac->ac_b_ex.fe_len,
  3527. (unsigned long)ac->ac_b_ex.fe_logical,
  3528. (int)ac->ac_criteria);
  3529. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3530. ac->ac_found);
  3531. printk(KERN_ERR "EXT4-fs: groups: \n");
  3532. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  3533. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3534. struct ext4_prealloc_space *pa;
  3535. ext4_grpblk_t start;
  3536. struct list_head *cur;
  3537. ext4_lock_group(sb, i);
  3538. list_for_each(cur, &grp->bb_prealloc_list) {
  3539. pa = list_entry(cur, struct ext4_prealloc_space,
  3540. pa_group_list);
  3541. spin_lock(&pa->pa_lock);
  3542. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3543. NULL, &start);
  3544. spin_unlock(&pa->pa_lock);
  3545. printk(KERN_ERR "PA:%lu:%d:%u \n", i,
  3546. start, pa->pa_len);
  3547. }
  3548. ext4_lock_group(sb, i);
  3549. if (grp->bb_free == 0)
  3550. continue;
  3551. printk(KERN_ERR "%lu: %d/%d \n",
  3552. i, grp->bb_free, grp->bb_fragments);
  3553. }
  3554. printk(KERN_ERR "\n");
  3555. }
  3556. #else
  3557. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3558. {
  3559. return;
  3560. }
  3561. #endif
  3562. /*
  3563. * We use locality group preallocation for small size file. The size of the
  3564. * file is determined by the current size or the resulting size after
  3565. * allocation which ever is larger
  3566. *
  3567. * One can tune this size via /proc/fs/ext4/<partition>/stream_req
  3568. */
  3569. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3570. {
  3571. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3572. int bsbits = ac->ac_sb->s_blocksize_bits;
  3573. loff_t size, isize;
  3574. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3575. return;
  3576. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3577. isize = i_size_read(ac->ac_inode) >> bsbits;
  3578. size = max(size, isize);
  3579. /* don't use group allocation for large files */
  3580. if (size >= sbi->s_mb_stream_request)
  3581. return;
  3582. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3583. return;
  3584. BUG_ON(ac->ac_lg != NULL);
  3585. /*
  3586. * locality group prealloc space are per cpu. The reason for having
  3587. * per cpu locality group is to reduce the contention between block
  3588. * request from multiple CPUs.
  3589. */
  3590. ac->ac_lg = &sbi->s_locality_groups[get_cpu()];
  3591. put_cpu();
  3592. /* we're going to use group allocation */
  3593. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3594. /* serialize all allocations in the group */
  3595. mutex_lock(&ac->ac_lg->lg_mutex);
  3596. }
  3597. static int ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3598. struct ext4_allocation_request *ar)
  3599. {
  3600. struct super_block *sb = ar->inode->i_sb;
  3601. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3602. struct ext4_super_block *es = sbi->s_es;
  3603. ext4_group_t group;
  3604. unsigned long len;
  3605. unsigned long goal;
  3606. ext4_grpblk_t block;
  3607. /* we can't allocate > group size */
  3608. len = ar->len;
  3609. /* just a dirty hack to filter too big requests */
  3610. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3611. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3612. /* start searching from the goal */
  3613. goal = ar->goal;
  3614. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3615. goal >= ext4_blocks_count(es))
  3616. goal = le32_to_cpu(es->s_first_data_block);
  3617. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3618. /* set up allocation goals */
  3619. ac->ac_b_ex.fe_logical = ar->logical;
  3620. ac->ac_b_ex.fe_group = 0;
  3621. ac->ac_b_ex.fe_start = 0;
  3622. ac->ac_b_ex.fe_len = 0;
  3623. ac->ac_status = AC_STATUS_CONTINUE;
  3624. ac->ac_groups_scanned = 0;
  3625. ac->ac_ex_scanned = 0;
  3626. ac->ac_found = 0;
  3627. ac->ac_sb = sb;
  3628. ac->ac_inode = ar->inode;
  3629. ac->ac_o_ex.fe_logical = ar->logical;
  3630. ac->ac_o_ex.fe_group = group;
  3631. ac->ac_o_ex.fe_start = block;
  3632. ac->ac_o_ex.fe_len = len;
  3633. ac->ac_g_ex.fe_logical = ar->logical;
  3634. ac->ac_g_ex.fe_group = group;
  3635. ac->ac_g_ex.fe_start = block;
  3636. ac->ac_g_ex.fe_len = len;
  3637. ac->ac_f_ex.fe_len = 0;
  3638. ac->ac_flags = ar->flags;
  3639. ac->ac_2order = 0;
  3640. ac->ac_criteria = 0;
  3641. ac->ac_pa = NULL;
  3642. ac->ac_bitmap_page = NULL;
  3643. ac->ac_buddy_page = NULL;
  3644. ac->ac_lg = NULL;
  3645. /* we have to define context: we'll we work with a file or
  3646. * locality group. this is a policy, actually */
  3647. ext4_mb_group_or_file(ac);
  3648. mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3649. "left: %u/%u, right %u/%u to %swritable\n",
  3650. (unsigned) ar->len, (unsigned) ar->logical,
  3651. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3652. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3653. (unsigned) ar->lright, (unsigned) ar->pright,
  3654. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3655. return 0;
  3656. }
  3657. /*
  3658. * release all resource we used in allocation
  3659. */
  3660. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3661. {
  3662. if (ac->ac_pa) {
  3663. if (ac->ac_pa->pa_linear) {
  3664. /* see comment in ext4_mb_use_group_pa() */
  3665. spin_lock(&ac->ac_pa->pa_lock);
  3666. ac->ac_pa->pa_pstart += ac->ac_b_ex.fe_len;
  3667. ac->ac_pa->pa_lstart += ac->ac_b_ex.fe_len;
  3668. ac->ac_pa->pa_free -= ac->ac_b_ex.fe_len;
  3669. ac->ac_pa->pa_len -= ac->ac_b_ex.fe_len;
  3670. spin_unlock(&ac->ac_pa->pa_lock);
  3671. }
  3672. ext4_mb_put_pa(ac, ac->ac_sb, ac->ac_pa);
  3673. }
  3674. if (ac->ac_bitmap_page)
  3675. page_cache_release(ac->ac_bitmap_page);
  3676. if (ac->ac_buddy_page)
  3677. page_cache_release(ac->ac_buddy_page);
  3678. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3679. mutex_unlock(&ac->ac_lg->lg_mutex);
  3680. ext4_mb_collect_stats(ac);
  3681. return 0;
  3682. }
  3683. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3684. {
  3685. ext4_group_t i;
  3686. int ret;
  3687. int freed = 0;
  3688. for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
  3689. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3690. freed += ret;
  3691. needed -= ret;
  3692. }
  3693. return freed;
  3694. }
  3695. /*
  3696. * Main entry point into mballoc to allocate blocks
  3697. * it tries to use preallocation first, then falls back
  3698. * to usual allocation
  3699. */
  3700. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3701. struct ext4_allocation_request *ar, int *errp)
  3702. {
  3703. struct ext4_allocation_context *ac = NULL;
  3704. struct ext4_sb_info *sbi;
  3705. struct super_block *sb;
  3706. ext4_fsblk_t block = 0;
  3707. int freed;
  3708. int inquota;
  3709. sb = ar->inode->i_sb;
  3710. sbi = EXT4_SB(sb);
  3711. if (!test_opt(sb, MBALLOC)) {
  3712. block = ext4_new_blocks_old(handle, ar->inode, ar->goal,
  3713. &(ar->len), errp);
  3714. return block;
  3715. }
  3716. while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
  3717. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3718. ar->len--;
  3719. }
  3720. if (ar->len == 0) {
  3721. *errp = -EDQUOT;
  3722. return 0;
  3723. }
  3724. inquota = ar->len;
  3725. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3726. if (!ac) {
  3727. *errp = -ENOMEM;
  3728. return 0;
  3729. }
  3730. ext4_mb_poll_new_transaction(sb, handle);
  3731. *errp = ext4_mb_initialize_context(ac, ar);
  3732. if (*errp) {
  3733. ar->len = 0;
  3734. goto out;
  3735. }
  3736. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3737. if (!ext4_mb_use_preallocated(ac)) {
  3738. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3739. ext4_mb_normalize_request(ac, ar);
  3740. repeat:
  3741. /* allocate space in core */
  3742. ext4_mb_regular_allocator(ac);
  3743. /* as we've just preallocated more space than
  3744. * user requested orinally, we store allocated
  3745. * space in a special descriptor */
  3746. if (ac->ac_status == AC_STATUS_FOUND &&
  3747. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3748. ext4_mb_new_preallocation(ac);
  3749. }
  3750. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3751. ext4_mb_mark_diskspace_used(ac, handle);
  3752. *errp = 0;
  3753. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3754. ar->len = ac->ac_b_ex.fe_len;
  3755. } else {
  3756. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3757. if (freed)
  3758. goto repeat;
  3759. *errp = -ENOSPC;
  3760. ac->ac_b_ex.fe_len = 0;
  3761. ar->len = 0;
  3762. ext4_mb_show_ac(ac);
  3763. }
  3764. ext4_mb_release_context(ac);
  3765. out:
  3766. if (ar->len < inquota)
  3767. DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
  3768. kmem_cache_free(ext4_ac_cachep, ac);
  3769. return block;
  3770. }
  3771. static void ext4_mb_poll_new_transaction(struct super_block *sb,
  3772. handle_t *handle)
  3773. {
  3774. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3775. if (sbi->s_last_transaction == handle->h_transaction->t_tid)
  3776. return;
  3777. /* new transaction! time to close last one and free blocks for
  3778. * committed transaction. we know that only transaction can be
  3779. * active, so previos transaction can be being logged and we
  3780. * know that transaction before previous is known to be already
  3781. * logged. this means that now we may free blocks freed in all
  3782. * transactions before previous one. hope I'm clear enough ... */
  3783. spin_lock(&sbi->s_md_lock);
  3784. if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
  3785. mb_debug("new transaction %lu, old %lu\n",
  3786. (unsigned long) handle->h_transaction->t_tid,
  3787. (unsigned long) sbi->s_last_transaction);
  3788. list_splice_init(&sbi->s_closed_transaction,
  3789. &sbi->s_committed_transaction);
  3790. list_splice_init(&sbi->s_active_transaction,
  3791. &sbi->s_closed_transaction);
  3792. sbi->s_last_transaction = handle->h_transaction->t_tid;
  3793. }
  3794. spin_unlock(&sbi->s_md_lock);
  3795. ext4_mb_free_committed_blocks(sb);
  3796. }
  3797. static int ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  3798. ext4_group_t group, ext4_grpblk_t block, int count)
  3799. {
  3800. struct ext4_group_info *db = e4b->bd_info;
  3801. struct super_block *sb = e4b->bd_sb;
  3802. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3803. struct ext4_free_metadata *md;
  3804. int i;
  3805. BUG_ON(e4b->bd_bitmap_page == NULL);
  3806. BUG_ON(e4b->bd_buddy_page == NULL);
  3807. ext4_lock_group(sb, group);
  3808. for (i = 0; i < count; i++) {
  3809. md = db->bb_md_cur;
  3810. if (md && db->bb_tid != handle->h_transaction->t_tid) {
  3811. db->bb_md_cur = NULL;
  3812. md = NULL;
  3813. }
  3814. if (md == NULL) {
  3815. ext4_unlock_group(sb, group);
  3816. md = kmalloc(sizeof(*md), GFP_NOFS);
  3817. if (md == NULL)
  3818. return -ENOMEM;
  3819. md->num = 0;
  3820. md->group = group;
  3821. ext4_lock_group(sb, group);
  3822. if (db->bb_md_cur == NULL) {
  3823. spin_lock(&sbi->s_md_lock);
  3824. list_add(&md->list, &sbi->s_active_transaction);
  3825. spin_unlock(&sbi->s_md_lock);
  3826. /* protect buddy cache from being freed,
  3827. * otherwise we'll refresh it from
  3828. * on-disk bitmap and lose not-yet-available
  3829. * blocks */
  3830. page_cache_get(e4b->bd_buddy_page);
  3831. page_cache_get(e4b->bd_bitmap_page);
  3832. db->bb_md_cur = md;
  3833. db->bb_tid = handle->h_transaction->t_tid;
  3834. mb_debug("new md 0x%p for group %lu\n",
  3835. md, md->group);
  3836. } else {
  3837. kfree(md);
  3838. md = db->bb_md_cur;
  3839. }
  3840. }
  3841. BUG_ON(md->num >= EXT4_BB_MAX_BLOCKS);
  3842. md->blocks[md->num] = block + i;
  3843. md->num++;
  3844. if (md->num == EXT4_BB_MAX_BLOCKS) {
  3845. /* no more space, put full container on a sb's list */
  3846. db->bb_md_cur = NULL;
  3847. }
  3848. }
  3849. ext4_unlock_group(sb, group);
  3850. return 0;
  3851. }
  3852. /*
  3853. * Main entry point into mballoc to free blocks
  3854. */
  3855. void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
  3856. unsigned long block, unsigned long count,
  3857. int metadata, unsigned long *freed)
  3858. {
  3859. struct buffer_head *bitmap_bh = 0;
  3860. struct super_block *sb = inode->i_sb;
  3861. struct ext4_allocation_context *ac = NULL;
  3862. struct ext4_group_desc *gdp;
  3863. struct ext4_super_block *es;
  3864. unsigned long overflow;
  3865. ext4_grpblk_t bit;
  3866. struct buffer_head *gd_bh;
  3867. ext4_group_t block_group;
  3868. struct ext4_sb_info *sbi;
  3869. struct ext4_buddy e4b;
  3870. int err = 0;
  3871. int ret;
  3872. *freed = 0;
  3873. ext4_mb_poll_new_transaction(sb, handle);
  3874. sbi = EXT4_SB(sb);
  3875. es = EXT4_SB(sb)->s_es;
  3876. if (block < le32_to_cpu(es->s_first_data_block) ||
  3877. block + count < block ||
  3878. block + count > ext4_blocks_count(es)) {
  3879. ext4_error(sb, __FUNCTION__,
  3880. "Freeing blocks not in datazone - "
  3881. "block = %lu, count = %lu", block, count);
  3882. goto error_return;
  3883. }
  3884. ext4_debug("freeing block %lu\n", block);
  3885. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3886. if (ac) {
  3887. ac->ac_op = EXT4_MB_HISTORY_FREE;
  3888. ac->ac_inode = inode;
  3889. ac->ac_sb = sb;
  3890. }
  3891. do_more:
  3892. overflow = 0;
  3893. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  3894. /*
  3895. * Check to see if we are freeing blocks across a group
  3896. * boundary.
  3897. */
  3898. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  3899. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  3900. count -= overflow;
  3901. }
  3902. bitmap_bh = read_block_bitmap(sb, block_group);
  3903. if (!bitmap_bh)
  3904. goto error_return;
  3905. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  3906. if (!gdp)
  3907. goto error_return;
  3908. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  3909. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  3910. in_range(block, ext4_inode_table(sb, gdp),
  3911. EXT4_SB(sb)->s_itb_per_group) ||
  3912. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  3913. EXT4_SB(sb)->s_itb_per_group)) {
  3914. ext4_error(sb, __FUNCTION__,
  3915. "Freeing blocks in system zone - "
  3916. "Block = %lu, count = %lu", block, count);
  3917. }
  3918. BUFFER_TRACE(bitmap_bh, "getting write access");
  3919. err = ext4_journal_get_write_access(handle, bitmap_bh);
  3920. if (err)
  3921. goto error_return;
  3922. /*
  3923. * We are about to modify some metadata. Call the journal APIs
  3924. * to unshare ->b_data if a currently-committing transaction is
  3925. * using it
  3926. */
  3927. BUFFER_TRACE(gd_bh, "get_write_access");
  3928. err = ext4_journal_get_write_access(handle, gd_bh);
  3929. if (err)
  3930. goto error_return;
  3931. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  3932. if (err)
  3933. goto error_return;
  3934. #ifdef AGGRESSIVE_CHECK
  3935. {
  3936. int i;
  3937. for (i = 0; i < count; i++)
  3938. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  3939. }
  3940. #endif
  3941. mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
  3942. bit, count);
  3943. /* We dirtied the bitmap block */
  3944. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  3945. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  3946. if (ac) {
  3947. ac->ac_b_ex.fe_group = block_group;
  3948. ac->ac_b_ex.fe_start = bit;
  3949. ac->ac_b_ex.fe_len = count;
  3950. ext4_mb_store_history(ac);
  3951. }
  3952. if (metadata) {
  3953. /* blocks being freed are metadata. these blocks shouldn't
  3954. * be used until this transaction is committed */
  3955. ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
  3956. } else {
  3957. ext4_lock_group(sb, block_group);
  3958. err = mb_free_blocks(inode, &e4b, bit, count);
  3959. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  3960. ext4_unlock_group(sb, block_group);
  3961. BUG_ON(err != 0);
  3962. }
  3963. spin_lock(sb_bgl_lock(sbi, block_group));
  3964. gdp->bg_free_blocks_count =
  3965. cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
  3966. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  3967. spin_unlock(sb_bgl_lock(sbi, block_group));
  3968. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  3969. ext4_mb_release_desc(&e4b);
  3970. *freed += count;
  3971. /* And the group descriptor block */
  3972. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  3973. ret = ext4_journal_dirty_metadata(handle, gd_bh);
  3974. if (!err)
  3975. err = ret;
  3976. if (overflow && !err) {
  3977. block += count;
  3978. count = overflow;
  3979. put_bh(bitmap_bh);
  3980. goto do_more;
  3981. }
  3982. sb->s_dirt = 1;
  3983. error_return:
  3984. brelse(bitmap_bh);
  3985. ext4_std_error(sb, err);
  3986. if (ac)
  3987. kmem_cache_free(ext4_ac_cachep, ac);
  3988. return;
  3989. }