mballoc.c 146 KB

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