mballoc.c 138 KB

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