mballoc.c 135 KB

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