mballoc.c 124 KB

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