mballoc.c 146 KB

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