mballoc.c 134 KB

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