mballoc.c 125 KB

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