mballoc.c 132 KB

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