mballoc.c 125 KB

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