mballoc.c 126 KB

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