mballoc.c 132 KB

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