mballoc.c 144 KB

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